/src/renderer/device.h
https://bitbucket.org/vivkin/gam3b00bs/ · C Header · 277 lines · 174 code · 57 blank · 46 comment · 0 complexity · c51e8021df59862e6fa0a553e468c4f5 MD5 · raw file
- #ifndef __RENDERER_H__
- #define __RENDERER_H__
- //-----------------------------------------------------------------------------
-
- #include "common.h"
- #include "camera.h"
- #include <d3d9.h>
- #include <d3dx9.h>
- //-----------------------------------------------------------------------------
-
- struct renderer_desc_t
- {
- uint32 width;
- uint32 height;
- D3DFORMAT depth;
- HWND window;
- uint32 multisampling;
- uint32 refresh_rate;
- bool windowed;
- bool vsync;
- };
- //-----------------------------------------------------------------------------
-
- enum IB_TYPE
- {
- IB_NONE,
- IB_16BIT = D3DFMT_INDEX16,
- IB_32BIT = D3DFMT_INDEX32,
- };
- //-----------------------------------------------------------------------------
-
- enum VERTEX_ELEMENT
- {
- VE_POSITION = 0x0001, // 3-component vector
- VE_NORMAL = 0x0002, // 3-component vector
- VE_COLOR = 0x0004, // uint32 color
- VE_TEXTURE0 = 0x0008, // 2-component vector
- VE_TEXTURE1 = 0x0010, // 2-component vector
- VE_POSITION_RHW = 0x0020, // 4-component vector
-
- VE_MAX_COUNT = 0x0040,
- };
- //-----------------------------------------------------------------------------
-
- struct vbuffer_t
- {
- LPDIRECT3DVERTEXBUFFER9 buffer;
- uint32 size;
- bool dynamic;
- byte* data; // valid when resource is dynamic
- };
-
- typedef vbuffer_t* vbuffer_h;
- //-----------------------------------------------------------------------------
-
- struct ibuffer_t
- {
- LPDIRECT3DINDEXBUFFER9 buffer;
- uint32 size;
- bool dynamic;
- byte* data; // valid when resource is dynamic
- IB_TYPE type;
- };
-
- typedef ibuffer_t* ibuffer_h;
- //-----------------------------------------------------------------------------
-
- struct texture_t
- {
- LPDIRECT3DTEXTURE9 texture;
- uint32 width;
- uint32 height;
- };
-
- typedef texture_t* texture_h;
- //-----------------------------------------------------------------------------
-
- struct vertex_layout_t
- {
- LPDIRECT3DVERTEXDECLARATION9 decl;
- uint32 fvf;
- };
-
- typedef vertex_layout_t* vertex_layout_h;
- //-----------------------------------------------------------------------------
-
- #define ERR_NO_D3D 1
- #define ERR_NO_CAPS 2
- #define ERR_NO_SYS_DISPLAY_MODE 3
- #define ERR_NO_DEVICE 4
- #define ERR_NO_DEV_ID 5
- #define ERR_CANT_RESET 6
- //-----------------------------------------------------------------------------
-
- struct sampler_state_t
- {
- D3DTEXTUREFILTERTYPE filter_min;
- D3DTEXTUREFILTERTYPE filter_mag;
- D3DTEXTUREFILTERTYPE filter_mip;
- uint32 filter_anisotropy_level; // 0-16
- D3DTEXTUREADDRESS address_u;
- D3DTEXTUREADDRESS address_v;
- int32 mipmap_lod_bias;
- };
-
- typedef uint32 sampler_state;
- //-----------------------------------------------------------------------------
-
- struct state_group_desc_t
- {
- bool alphablend_enable;
- bool alphatest_enable;
- uint32 alpharef;
- D3DBLENDOP alphaop;
- D3DBLEND srcblend;
- D3DBLEND destblend;
- uint32 writemask;
-
- texture_t texture[2];
- sampler_state sampler[2];
-
- D3DXMATRIX texture_matrix;
- };
-
- typedef uint32 state_group;
- //-----------------------------------------------------------------------------
-
- enum STAGEARG
- {
- ARG_CONSTANT = D3DTA_CONSTANT,
- ARG_CURRENT = D3DTA_CURRENT,
- ARG_DIFFUSE = D3DTA_DIFFUSE,
- ARG_SPECULAR = D3DTA_SPECULAR,
- ARG_TEXTURE = D3DTA_TEXTURE,
- };
- //-----------------------------------------------------------------------------
-
- struct stage_t
- {
- texture_h texture;
-
- D3DTEXTUREOP color_op;
- STAGEARG color_arg1;
- STAGEARG color_arg2;
- D3DTEXTUREOP alpha_op;
- STAGEARG alpha_arg1;
- STAGEARG alpha_arg2;
-
- };
- //-----------------------------------------------------------------------------
-
- struct material_state_t
- {
- D3DMATERIAL9 material;
- stage_t stages[4];
- };
- //-----------------------------------------------------------------------------
-
- struct render_op
- {
- // geometry
- vbuffer_h vb;
- ibuffer_h ib;
- uint32 vertex_count;
- uint32 index_count;
- uint32 vertex_size;
- uint32 vertex_offset;
- uint32 index_offset;
- uint32 primitive_count;
- vertex_layout_h vertex_layout;
- D3DXMATRIX transformation;
-
- // material
- material_state_t material;
- };
- //-----------------------------------------------------------------------------
-
- struct environment_state_t
- {
- D3DLIGHT9 light[8];
- bool light_enable[8];
- texture_t lightmap;
- texture_t radiositymap;
- };
- //-----------------------------------------------------------------------------
-
- enum SAMPLER_TYPE
- {
- SAMPLER_DEFAULT = 0,
- SAMPLER_LIGHTMAP = 1,
- SAMPLER_GUI = 2,
-
- MAX_SAMPLERS = 4,
- };
- //-----------------------------------------------------------------------------
-
- namespace renderer
- {
- //-----------------------------------------------------------------------------
- LPDIRECT3DDEVICE9 device_get();
-
- //-----------------------------------------------------------------------------
-
- uint32 init( const renderer_desc_t& desc );
- //-----------------------------------------------------------------------------
-
- void term();
- //-----------------------------------------------------------------------------
-
- uint32 reset();
- //-----------------------------------------------------------------------------
-
- bool begin();
- //-----------------------------------------------------------------------------
-
- void end();
- //-----------------------------------------------------------------------------
-
- void clear( uint32 color = 0xFF000000, float depth = 1.0f, uint8 stencil = 0 );
- //-----------------------------------------------------------------------------
-
- void render( const render_op& rop );
- //-----------------------------------------------------------------------------
-
- void set_environment_state( const environment_state_t& e_state );
- //-----------------------------------------------------------------------------
-
- void set_view( const D3DXMATRIX& view );
- //-----------------------------------------------------------------------------
-
- void set_projection( const D3DXMATRIX& proj );
- //-----------------------------------------------------------------------------
-
- void set_camera( const camera_t& camera );
- //-----------------------------------------------------------------------------
-
- sampler_state create_sampler_state( const sampler_state_t& desc );
- //-----------------------------------------------------------------------------
-
- state_group create_state_group( const state_group_desc_t& desc );
- //-----------------------------------------------------------------------------
-
- texture_h texture_create( const byte* data, uint32 data_size );
- //-----------------------------------------------------------------------------
-
- vbuffer_h vbuffer_create( uint32 size, const byte* init_data = 0, uint32 usage = D3DUSAGE_WRITEONLY );
- //-----------------------------------------------------------------------------
-
- ibuffer_h ibuffer_create( IB_TYPE type, uint32 size, const byte* init_data = 0, uint32 usage = D3DUSAGE_WRITEONLY );
- //-----------------------------------------------------------------------------
-
- uint32 vbuffer_lock( vbuffer_h buffer, void** mem, uint32 length = 0, uint32 offset = 0 );
- //-----------------------------------------------------------------------------
-
- uint32 vbuffer_unlock( vbuffer_h buffer );
- //-----------------------------------------------------------------------------
-
- uint32 ibuffer_lock( ibuffer_h buffer, void** mem, uint32 length = 0, uint32 offset = 0 );
- //-----------------------------------------------------------------------------
-
- uint32 ibuffer_unlock( ibuffer_h buffer );
- //-----------------------------------------------------------------------------
-
- vertex_layout_h get_vertex_layout( uint32 vertex_type );
- //-----------------------------------------------------------------------------
-
- void tick();
- //-----------------------------------------------------------------------------
-
- void set_sampler( uint32 id, SAMPLER_TYPE sampler );
- //-----------------------------------------------------------------------------
- }
-
- // for tests
- bool test_init();
-
- #endif