/src/renderer/device.cpp
https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 724 lines · 552 code · 128 blank · 44 comment · 75 complexity · 2a96d442cb91dd78c234e1173e777418 MD5 · raw file
- #include "device.h"
- #include "log.h"
- #include <ASSERT.h>
- //-----------------------------------------------------------------------------
-
- namespace renderer
- {
- //-----------------------------------------------------------------------------
-
- #ifndef SAFE_RELEASE
- #define SAFE_RELEASE(p) if(p)p->Release();p=0;
- #endif
- //-----------------------------------------------------------------------------
-
- #define MAX_BUFFERS 128
- #define MAX_TEXTURES 128
- //-----------------------------------------------------------------------------
-
- vbuffer_t vertex_buffers[MAX_BUFFERS];
- uint32 vertex_buffers_count = 0;
- //-----------------------------------------------------------------------------
-
- ibuffer_t index_buffers[MAX_BUFFERS];
- uint32 index_buffers_count = 0;
- //-----------------------------------------------------------------------------
-
- texture_t textures[MAX_TEXTURES];
- uint32 textures_count = 0;
- //-----------------------------------------------------------------------------
-
- vertex_layout_t vertex_decls[VE_MAX_COUNT] = {0};
- //-----------------------------------------------------------------------------
-
- sampler_state_t samplers[MAX_SAMPLERS];
- //-----------------------------------------------------------------------------
-
- renderer_desc_t renderer_desc;
- LPDIRECT3D9 d3d;
- LPDIRECT3DDEVICE9 d3d_device;
- D3DPRESENT_PARAMETERS d3d_presentParams;
- D3DCAPS9 d3d_deviceCaps;
- D3DADAPTER_IDENTIFIER9 d3d_deviceIdentifier;
- bool g_device_lost = false;
- //-----------------------------------------------------------------------------
-
- LPDIRECT3DDEVICE9 device_get()
- {
- return d3d_device;
- }
-
- //-----------------------------------------------------------------------------
- void on_lost_device()
- {
- for( uint32 i=0; i<index_buffers_count; i++ )
- if( index_buffers[i].dynamic )
- index_buffers[i].buffer->Release();
-
- for( uint32 i=0; i<vertex_buffers_count; i++ )
- if( vertex_buffers[i].dynamic )
- vertex_buffers[i].buffer->Release();
- }
- //-----------------------------------------------------------------------------
-
- void on_reset_device()
- {
- for( uint32 i=0; i<index_buffers_count; i++ )
- if( index_buffers[i].dynamic )
- {
- HRESULT hr = d3d_device->CreateIndexBuffer( index_buffers[i].size, D3DUSAGE_DYNAMIC, (D3DFORMAT)index_buffers[i].type, D3DPOOL_DEFAULT, &index_buffers[i].buffer, 0 );
- if( FAILED(hr) )
- {
- log_write("Error: unable to restore vertex buffer");
- continue;
- }
- void* pbuf;
- ibuffer_lock( &index_buffers[i], &pbuf );
- memcpy(pbuf, index_buffers[i].data, index_buffers[i].size);
- ibuffer_unlock( &index_buffers[i] );
- }
-
- for( uint32 i=0; i<vertex_buffers_count; i++ )
- if( vertex_buffers[i].dynamic )
- {
- HRESULT hr = d3d_device->CreateVertexBuffer( vertex_buffers[i].size, D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &vertex_buffers[i].buffer, 0 );
- if( FAILED(hr) )
- {
- log_write("Error: unable to restore vertex buffer");
- continue;
- }
- void* pbuf;
- vbuffer_lock( &vertex_buffers[i], &pbuf );
- memcpy(pbuf, vertex_buffers[i].data, vertex_buffers[i].size);
- vbuffer_unlock( &vertex_buffers[i] );
- }
- }
- //-----------------------------------------------------------------------------
-
- void set_initial_states()
- {
- d3d_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
- d3d_device->SetRenderState(D3DRS_LIGHTING, TRUE);
- d3d_device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
- d3d_device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
- if( renderer_desc.multisampling > 0 )
- {
- d3d_device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
- d3d_device->SetRenderState(D3DRS_ANTIALIASEDLINEENABLE , TRUE);
- }
- };
- //-----------------------------------------------------------------------------
-
- uint32 create_device()
- {
- HRESULT hr;
-
- d3d_presentParams.PresentationInterval = renderer_desc.vsync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
- d3d_presentParams.Windowed = renderer_desc.windowed;
- d3d_presentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3d_presentParams.EnableAutoDepthStencil = true;
- d3d_presentParams.hDeviceWindow = (HWND)renderer_desc.window;
-
- if( renderer_desc.windowed )
- {
- D3DDISPLAYMODE sys_display_mode;
- hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &sys_display_mode);
- if( FAILED(hr) )
- return ERR_NO_SYS_DISPLAY_MODE;
- d3d_presentParams.BackBufferFormat = sys_display_mode.Format;
- d3d_presentParams.BackBufferCount = 1;
- }
- else
- {
- d3d_presentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
- d3d_presentParams.BackBufferWidth = renderer_desc.width;
- d3d_presentParams.BackBufferHeight = renderer_desc.height;
- d3d_presentParams.FullScreen_RefreshRateInHz = renderer_desc.refresh_rate;
- d3d_presentParams.BackBufferCount = 1;
- }
-
- d3d_presentParams.AutoDepthStencilFormat = renderer_desc.depth;
-
- uint32 multisampling_max = 16;
- while( FAILED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3d_presentParams.BackBufferFormat, d3d_presentParams.Windowed, (D3DMULTISAMPLE_TYPE)multisampling_max, 0))
- || FAILED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3d_presentParams.AutoDepthStencilFormat, d3d_presentParams.Windowed, (D3DMULTISAMPLE_TYPE)multisampling_max, 0)) )
- {
- multisampling_max--;
- if( multisampling_max == 0 )
- break;
- }
-
- if( renderer_desc.multisampling > multisampling_max )
- renderer_desc.multisampling = multisampling_max;
-
- if( renderer_desc.multisampling > 0 )
- d3d_presentParams.MultiSampleType = (D3DMULTISAMPLE_TYPE)renderer_desc.multisampling;
-
- hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3d_presentParams.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3d_presentParams, &d3d_device);
- if( FAILED(hr) )
- {
- hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3d_presentParams.hDeviceWindow, D3DCREATE_MIXED_VERTEXPROCESSING, &d3d_presentParams, &d3d_device);
- if( FAILED(hr) )
- {
- hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3d_presentParams.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3d_presentParams, &d3d_device);
- if( FAILED(hr) )
- return ERR_NO_DEVICE;
- }
- }
-
- hr = d3d->GetAdapterIdentifier(D3DADAPTER_DEFAULT , 0, &d3d_deviceIdentifier);
- if( FAILED(hr) )
- return ERR_NO_DEV_ID;
-
- log_write("Device: %s", d3d_deviceIdentifier.Description);
- log_write("Driver: %s", d3d_deviceIdentifier.Driver);
- log_write("Version: %d.%d.%d.%d\n",
- HIWORD(d3d_deviceIdentifier.DriverVersion.HighPart),
- LOWORD(d3d_deviceIdentifier.DriverVersion.HighPart),
- HIWORD(d3d_deviceIdentifier.DriverVersion.LowPart),
- LOWORD(d3d_deviceIdentifier.DriverVersion.LowPart));
-
- set_initial_states();
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- uint32 init( const renderer_desc_t& desc )
- {
- ASSERT( d3d == 0 ); // wrong renderer state
- ASSERT( d3d_device == 0 ); // wrong renderer state
-
- HRESULT hr;
- uint32 res;
-
- memcpy( &renderer_desc, &desc, sizeof(renderer_desc_t) );
-
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- if( !d3d )
- {
- log_write("ERROR: failed to create D3D");
- return ERR_NO_D3D;
- }
-
- hr = d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d_deviceCaps);
- if( FAILED(hr) )
- {
- log_write("ERROR: unable to get device caps");
- term();
- return ERR_NO_CAPS;
- }
-
- res = create_device();
- if( res )
- {
- log_write("ERROR: unable to create device");
- term();
- return res;
- }
-
- // init samplers
- // default sampler
- samplers[SAMPLER_DEFAULT].address_u = D3DTADDRESS_WRAP;
- samplers[SAMPLER_DEFAULT].address_v = D3DTADDRESS_WRAP;
- samplers[SAMPLER_DEFAULT].filter_mag = D3DTEXF_ANISOTROPIC;
- samplers[SAMPLER_DEFAULT].filter_min = D3DTEXF_ANISOTROPIC;
- samplers[SAMPLER_DEFAULT].filter_anisotropy_level = 4;
- samplers[SAMPLER_DEFAULT].filter_mip = D3DTEXF_LINEAR;
- samplers[SAMPLER_DEFAULT].mipmap_lod_bias = 0;
-
- samplers[SAMPLER_LIGHTMAP].address_u = D3DTADDRESS_CLAMP;
- samplers[SAMPLER_LIGHTMAP].address_v = D3DTADDRESS_CLAMP;
- samplers[SAMPLER_LIGHTMAP].filter_mag = D3DTEXF_LINEAR;
- samplers[SAMPLER_LIGHTMAP].filter_min = D3DTEXF_LINEAR;
- // samplers[SAMPLER_LIGHTMAP].filter_anisotropy_level = 4;
- samplers[SAMPLER_LIGHTMAP].filter_mip = D3DTEXF_LINEAR;
- samplers[SAMPLER_LIGHTMAP].mipmap_lod_bias = 0;
-
- samplers[SAMPLER_GUI].address_u = D3DTADDRESS_WRAP;
- samplers[SAMPLER_GUI].address_v = D3DTADDRESS_WRAP;
- samplers[SAMPLER_GUI].filter_mag = D3DTEXF_POINT;
- samplers[SAMPLER_GUI].filter_min = D3DTEXF_POINT;
- // samplers[SAMPLER_GUI].filter_anisotropy_level = 4;
- samplers[SAMPLER_GUI].filter_mip = D3DTEXF_NONE;
- samplers[SAMPLER_GUI].mipmap_lod_bias = 0;
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- void term()
- {
- for( uint32 i=0; i<textures_count; i++ )
- {
- SAFE_RELEASE( textures[i].texture );
- }
-
- for( uint32 i=0; i<index_buffers_count; i++ )
- {
- SAFE_RELEASE( index_buffers[i].buffer );
- if( index_buffers[i].dynamic )
- free(index_buffers[i].data);
- }
-
- for( uint32 i=0; i<vertex_buffers_count; i++ )
- {
- SAFE_RELEASE( vertex_buffers[i].buffer );
- if( vertex_buffers[i].dynamic )
- free(vertex_buffers[i].data);
- }
-
- SAFE_RELEASE( d3d_device );
- SAFE_RELEASE( d3d );
- }
- //-----------------------------------------------------------------------------
-
- bool begin()
- {
- ASSERT( d3d_device );
-
- HRESULT hr;
- hr = d3d_device->TestCooperativeLevel();
-
- if( hr == D3DERR_DEVICELOST )
- {
- g_device_lost = true;
- on_lost_device();
- return false;
- }
- if( hr == D3DERR_DEVICENOTRESET )
- {
- if( reset() )
- return false;
- g_device_lost = false;
- on_reset_device();
- }
-
- d3d_device->BeginScene();
-
- return true;
- }
- //-----------------------------------------------------------------------------
-
- void end()
- {
- ASSERT( d3d_device );
-
- d3d_device->EndScene();
- d3d_device->Present(0, 0, 0, 0);
- }
- //-----------------------------------------------------------------------------
-
- void clear( uint32 color, float depth, uint8 stencil )
- {
- ASSERT( d3d_device );
-
- if( renderer_desc.depth == D3DFMT_D24S8 )
- d3d_device->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET | D3DCLEAR_STENCIL, color, depth, stencil);
- else
- d3d_device->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, color, depth, 0);
- }
- //-----------------------------------------------------------------------------
-
- void render( const render_op& rop )
- {
- ASSERT(d3d_device);
-
- d3d_device->SetMaterial(&rop.material.material);
-
- d3d_device->SetTexture(0, rop.material.stages[0].texture->texture);
- d3d_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
-
- if( rop.material.stages[1].texture )
- {
- d3d_device->SetTexture(1, rop.material.stages[1].texture->texture);
- d3d_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACENORMAL );
- d3d_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD );
- d3d_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- d3d_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- }
- else
- {
- d3d_device->SetTexture(1, 0);
- }
-
- D3DXMATRIX world((float*)&rop.transformation);
- d3d_device->SetTransform(D3DTS_WORLD, &world);
-
- d3d_device->SetVertexDeclaration(rop.vertex_layout->decl);
- d3d_device->SetIndices(rop.ib->buffer);
- d3d_device->SetStreamSource(0, rop.vb->buffer, rop.vertex_offset, rop.vertex_size);
-
- d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, rop.vertex_count, rop.index_offset, rop.index_count / 3);
- }
- //-----------------------------------------------------------------------------
-
- void set_environment_state( const environment_state_t& e_state )
- {
- ASSERT(d3d_device);
-
- for( uint32 i=0; i<8; i++ )
- {
- if( e_state.light_enable[i] )
- {
- d3d_device->LightEnable(i, true);
- d3d_device->SetLight(i, &e_state.light[i]);
- }
- }
- }
- //-----------------------------------------------------------------------------
-
- void set_view( const D3DXMATRIX& view )
- {
- ASSERT( d3d_device );
-
- d3d_device->SetTransform(D3DTS_VIEW, &view);
- }
- //-----------------------------------------------------------------------------
-
- void set_projection( const D3DXMATRIX& proj )
- {
- ASSERT( d3d_device );
-
- d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
- }
- //-----------------------------------------------------------------------------
-
- void set_camera( const camera_t& camera )
- {
- ASSERT( d3d_device );
-
- d3d_device->SetTransform(D3DTS_VIEW, &camera.view);
- d3d_device->SetTransform(D3DTS_PROJECTION, &camera.projection);
- }
- //-----------------------------------------------------------------------------
- sampler_state create_sampler_state( const sampler_state_t& desc )
- {
- ASSERT(0);
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- state_group create_state_group( const state_group_desc_t& desc )
- {
- ASSERT(0);
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- uint32 reset()
- {
- ASSERT( d3d_device );
-
- HRESULT hr;
- hr = d3d_device->Reset(&d3d_presentParams);
- if( FAILED(hr) )
- {
- log_write("ERROR: Unable to reset device");
- return ERR_CANT_RESET;
- }
-
- set_initial_states();
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- texture_h texture_create( const byte* data, uint32 data_size )
- {
- ASSERT(data);
- ASSERT(d3d_device);
- ASSERT(textures_count < MAX_TEXTURES);
-
- D3DXIMAGE_INFO info;
- HRESULT hr = D3DXCreateTextureFromFileInMemoryEx( d3d_device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
- 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &info, 0, &textures[textures_count].texture );
- if( FAILED(hr) )
- {
- log_write("Error: unable to create texture");
- return 0;
- }
- textures[textures_count].width = info.Width;
- textures[textures_count].height = info.Height;
-
- return &textures[textures_count++];
- }
- //-----------------------------------------------------------------------------
-
- vbuffer_h vbuffer_create( uint32 size, const byte* init_data, uint32 usage )
- {
- ASSERT(d3d_device);
- ASSERT(size > 0);
- ASSERT(vertex_buffers_count < MAX_BUFFERS);
-
- vbuffer_h buffer = &vertex_buffers[vertex_buffers_count];
-
- D3DPOOL pool = D3DPOOL_MANAGED;
- if( usage & D3DUSAGE_DYNAMIC )
- {
- buffer->data = (byte*)malloc(size);
- buffer->dynamic = true;
- pool = D3DPOOL_DEFAULT;
- }
-
- HRESULT hr = d3d_device->CreateVertexBuffer( size, usage, 0, pool, &buffer->buffer, 0 );
- if( FAILED(hr) )
- {
- log_write("Error: unable to create vertex buffer");
- return 0;
- }
-
- buffer->size = size;
-
- if( init_data )
- {
- void* pbuf;
- vbuffer_lock( buffer, &pbuf );
- memcpy(pbuf, init_data, size);
- vbuffer_unlock( buffer );
-
- if( usage & D3DUSAGE_DYNAMIC )
- memcpy(buffer->data, init_data, size);
- }
-
- vertex_buffers_count++;
-
- return buffer;
- }
- //-----------------------------------------------------------------------------
-
- uint32 vbuffer_lock( vbuffer_h buffer, void** mem, uint32 length, uint32 offset )
- {
- ASSERT(buffer);
- ASSERT(buffer->buffer);
-
- if( !length )
- length = buffer->size;
- ASSERT(length > 0);
-
- if( g_device_lost )
- {
- ASSERT( buffer->data );
- *mem = buffer->data + offset;
- return 0;
- }
-
- HRESULT hr = buffer->buffer->Lock(offset, length, mem, 0);
- if( FAILED(hr) )
- {
- log_write("Error: unable to lock vertex buffer");
- return 1;
- }
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- uint32 vbuffer_unlock( vbuffer_h buffer )
- {
- ASSERT(buffer);
- ASSERT(buffer->buffer);
-
- HRESULT hr = buffer->buffer->Unlock();
- if( FAILED(hr) )
- {
- log_write("ERROR: unable to unlock vertex buffer");
- return 1;
- }
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- ibuffer_h ibuffer_create( IB_TYPE type, uint32 size, const byte* init_data, uint32 usage )
- {
- ASSERT(d3d_device);
- ASSERT(size > 0);
- ASSERT(index_buffers_count < MAX_BUFFERS);
- ASSERT(type != IB_NONE);
-
- ibuffer_h buffer = &index_buffers[index_buffers_count];
-
- D3DPOOL pool = D3DPOOL_MANAGED;
- if( usage & D3DUSAGE_DYNAMIC )
- {
- buffer->data = (byte*)malloc(size);
- buffer->dynamic = true;
- pool = D3DPOOL_DEFAULT;
- }
-
- HRESULT hr = d3d_device->CreateIndexBuffer( size, usage, (D3DFORMAT)type, pool, &buffer->buffer, 0 );
- if( FAILED(hr) )
- {
- log_write("Error: unable to create index buffer");
- return 0;
- }
-
- buffer->size = size;
- buffer->type = type;
-
- if( init_data )
- {
- void* pbuf;
- ibuffer_lock( buffer, &pbuf );
- memcpy(pbuf, init_data, size);
- ibuffer_unlock( buffer );
-
- if( usage & D3DUSAGE_DYNAMIC )
- memcpy(buffer->data, init_data, size);
- }
-
- index_buffers_count++;
-
- return buffer;
- }
- //-----------------------------------------------------------------------------
-
- uint32 ibuffer_lock( ibuffer_h buffer, void** mem, uint32 length, uint32 offset )
- {
- ASSERT(buffer);
- ASSERT(buffer->buffer);
-
- if( !length )
- length = buffer->size;
- ASSERT(length > 0);
-
- if( !length )
- length = buffer->size;
- ASSERT(length > 0);
-
- if( g_device_lost )
- {
- ASSERT( buffer->data );
- *mem = buffer->data + offset;
- return 0;
- }
-
- HRESULT hr = buffer->buffer->Lock(offset, length, mem, 0);
- if( FAILED(hr) )
- {
- log_write("Error: unable to lock index buffer");
- return 1;
- }
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- uint32 ibuffer_unlock( ibuffer_h buffer )
- {
- ASSERT(buffer);
- ASSERT(buffer->buffer);
-
- HRESULT hr = buffer->buffer->Unlock();
- if( FAILED(hr) )
- {
- log_write("Error: unable to unlock index buffer");
- return 1;
- }
-
- return 0;
- }
- //-----------------------------------------------------------------------------
-
- vertex_layout_h get_vertex_layout( uint32 vertex_type )
- {
- ASSERT(vertex_type < VE_MAX_COUNT);
- ASSERT(d3d_device);
-
- if( !vertex_decls[vertex_type].decl )
- {
- D3DVERTEXELEMENT9 decl[8];
- uint16 element_count = 0;
- uint16 stride = 0;
-
- if( vertex_type & VE_POSITION )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(float) * 3;
- }
-
- if( vertex_type & VE_POSITION_RHW )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(float) * 4;
- }
-
- if( vertex_type & VE_NORMAL )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(float) * 3;
- }
-
- if( vertex_type & VE_COLOR )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(uint32);
- }
-
- if( vertex_type & VE_TEXTURE0 )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(float) * 2;
- }
-
- if( vertex_type & VE_TEXTURE1 )
- {
- D3DVERTEXELEMENT9 str = { 0, stride, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 };
- decl[element_count] = str;
- element_count++;
- stride += sizeof(float) * 2;
- }
-
- D3DVERTEXELEMENT9 str = D3DDECL_END();
- decl[element_count] = str;
-
- HRESULT hr;
- hr = d3d_device->CreateVertexDeclaration(decl, &vertex_decls[vertex_type].decl);
- if( FAILED(hr) )
- {
- log_write("Error: unable to create vertex declaration %X", vertex_type);
- return 0;
- }
-
- // vertex_sizes[vertex_type] = stride;
- vertex_decls[vertex_type].fvf = vertex_type;
- }
-
- // if( out_size )
- // *out_size = vertex_sizes[vertex_type];
-
- return &vertex_decls[vertex_type];
- }
- //-----------------------------------------------------------------------------
-
- void set_sampler( uint32 id, SAMPLER_TYPE sampler )
- {
- d3d_device->SetSamplerState(id, D3DSAMP_MIPFILTER, samplers[sampler].filter_mip);
- d3d_device->SetSamplerState(id, D3DSAMP_MINFILTER, samplers[sampler].filter_min);
- d3d_device->SetSamplerState(id, D3DSAMP_MAGFILTER, samplers[sampler].filter_mag);
-
- if( samplers[sampler].filter_min == D3DTEXF_ANISOTROPIC ||
- samplers[sampler].filter_mag == D3DTEXF_ANISOTROPIC)
- d3d_device->SetSamplerState(id, D3DSAMP_MAXANISOTROPY, samplers[sampler].filter_anisotropy_level);
-
- d3d_device->SetSamplerState(id, D3DSAMP_MIPMAPLODBIAS, samplers[sampler].mipmap_lod_bias);
-
- d3d_device->SetSamplerState(id, D3DSAMP_ADDRESSU, samplers[sampler].address_u);
- d3d_device->SetSamplerState(id, D3DSAMP_ADDRESSV, samplers[sampler].address_v);
- }
- //-----------------------------------------------------------------------------
-
- }