PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1#ifndef __RENDERER_H__
  2#define __RENDERER_H__
  3//-----------------------------------------------------------------------------
  4
  5#include "common.h"
  6#include "camera.h"
  7#include <d3d9.h>
  8#include <d3dx9.h>
  9//-----------------------------------------------------------------------------
 10
 11struct renderer_desc_t
 12{
 13	uint32      width;
 14	uint32      height;
 15	D3DFORMAT   depth;
 16	HWND		window;
 17	uint32      multisampling;
 18	uint32      refresh_rate;
 19	bool        windowed;
 20	bool        vsync;
 21};
 22//-----------------------------------------------------------------------------
 23
 24enum IB_TYPE
 25{
 26	IB_NONE,
 27	IB_16BIT = D3DFMT_INDEX16,
 28	IB_32BIT = D3DFMT_INDEX32,
 29};
 30//-----------------------------------------------------------------------------
 31
 32enum VERTEX_ELEMENT
 33{
 34	VE_POSITION		= 0x0001,	//	3-component vector
 35	VE_NORMAL		= 0x0002,	//	3-component vector
 36	VE_COLOR		= 0x0004,	//	uint32 color
 37	VE_TEXTURE0		= 0x0008,	//	2-component vector
 38	VE_TEXTURE1		= 0x0010,	//	2-component vector
 39	VE_POSITION_RHW	= 0x0020,	//	4-component vector
 40
 41	VE_MAX_COUNT	= 0x0040,
 42};
 43//-----------------------------------------------------------------------------
 44
 45struct vbuffer_t
 46{
 47    LPDIRECT3DVERTEXBUFFER9 buffer;
 48    uint32                  size;
 49    bool                    dynamic;
 50    byte*                   data;       // valid when resource is dynamic
 51};
 52
 53typedef vbuffer_t* vbuffer_h;
 54//-----------------------------------------------------------------------------
 55
 56struct ibuffer_t
 57{
 58    LPDIRECT3DINDEXBUFFER9  buffer;
 59    uint32                  size;
 60    bool                    dynamic;
 61    byte*                   data;       // valid when resource is dynamic
 62	IB_TYPE					type;
 63};
 64
 65typedef ibuffer_t* ibuffer_h;
 66//-----------------------------------------------------------------------------
 67
 68struct texture_t
 69{
 70    LPDIRECT3DTEXTURE9  texture;
 71    uint32              width;
 72    uint32              height;
 73};
 74
 75typedef texture_t* texture_h;
 76//-----------------------------------------------------------------------------
 77
 78struct vertex_layout_t
 79{
 80	LPDIRECT3DVERTEXDECLARATION9	decl;
 81	uint32							fvf;
 82};
 83
 84typedef vertex_layout_t* vertex_layout_h;
 85//-----------------------------------------------------------------------------
 86
 87#define ERR_NO_D3D              1
 88#define ERR_NO_CAPS             2
 89#define ERR_NO_SYS_DISPLAY_MODE 3
 90#define ERR_NO_DEVICE           4
 91#define ERR_NO_DEV_ID           5
 92#define ERR_CANT_RESET          6
 93//-----------------------------------------------------------------------------
 94
 95struct sampler_state_t
 96{
 97    D3DTEXTUREFILTERTYPE    filter_min;
 98    D3DTEXTUREFILTERTYPE    filter_mag;
 99    D3DTEXTUREFILTERTYPE    filter_mip;
100    uint32                  filter_anisotropy_level;    // 0-16
101    D3DTEXTUREADDRESS       address_u;
102    D3DTEXTUREADDRESS       address_v;
103    int32                   mipmap_lod_bias;
104};
105
106typedef uint32 sampler_state;
107//-----------------------------------------------------------------------------
108
109struct state_group_desc_t
110{  
111    bool            alphablend_enable;
112    bool            alphatest_enable;
113    uint32          alpharef;
114    D3DBLENDOP      alphaop;
115    D3DBLEND        srcblend;
116    D3DBLEND        destblend;
117    uint32          writemask;
118    
119    texture_t       texture[2];
120    sampler_state   sampler[2];
121    
122	D3DXMATRIX      texture_matrix;
123};
124
125typedef uint32 state_group;
126//-----------------------------------------------------------------------------
127
128enum STAGEARG
129{
130	ARG_CONSTANT	= D3DTA_CONSTANT,
131	ARG_CURRENT		= D3DTA_CURRENT,
132	ARG_DIFFUSE		= D3DTA_DIFFUSE,
133	ARG_SPECULAR	= D3DTA_SPECULAR,
134	ARG_TEXTURE		= D3DTA_TEXTURE,
135};
136//-----------------------------------------------------------------------------
137
138struct stage_t
139{
140	texture_h		texture;
141
142	D3DTEXTUREOP	color_op;
143	STAGEARG		color_arg1;
144	STAGEARG		color_arg2;
145	D3DTEXTUREOP	alpha_op;
146	STAGEARG		alpha_arg1;
147	STAGEARG		alpha_arg2;
148
149};
150//-----------------------------------------------------------------------------
151
152struct material_state_t
153{
154    D3DMATERIAL9        material;
155    stage_t				stages[4];
156};
157//-----------------------------------------------------------------------------
158
159struct render_op
160{
161    // geometry
162    vbuffer_h           vb;
163    ibuffer_h           ib;
164    uint32              vertex_count;
165    uint32              index_count;
166    uint32              vertex_size;
167    uint32              vertex_offset;
168    uint32              index_offset;
169    uint32              primitive_count;
170	vertex_layout_h		vertex_layout;
171    D3DXMATRIX          transformation;
172    
173    // material
174    material_state_t    material;
175};
176//-----------------------------------------------------------------------------
177
178struct environment_state_t
179{
180    D3DLIGHT9   light[8];
181	bool		light_enable[8];
182    texture_t   lightmap;
183    texture_t   radiositymap;
184};
185//-----------------------------------------------------------------------------
186
187enum SAMPLER_TYPE
188{
189	SAMPLER_DEFAULT		= 0,
190	SAMPLER_LIGHTMAP	= 1,
191	SAMPLER_GUI			= 2,
192
193	MAX_SAMPLERS		= 4,
194};
195//-----------------------------------------------------------------------------
196
197namespace renderer
198{
199//-----------------------------------------------------------------------------
200LPDIRECT3DDEVICE9 device_get();
201
202//-----------------------------------------------------------------------------
203
204uint32  init( const renderer_desc_t& desc );
205//-----------------------------------------------------------------------------
206
207void    term();
208//-----------------------------------------------------------------------------
209
210uint32  reset();
211//-----------------------------------------------------------------------------
212
213bool    begin();
214//-----------------------------------------------------------------------------
215
216void    end();
217//-----------------------------------------------------------------------------
218
219void    clear( uint32 color = 0xFF000000, float depth = 1.0f, uint8 stencil = 0 );
220//-----------------------------------------------------------------------------
221
222void    render( const render_op& rop );
223//-----------------------------------------------------------------------------
224
225void    set_environment_state( const environment_state_t& e_state );
226//-----------------------------------------------------------------------------
227
228void    set_view( const D3DXMATRIX& view );
229//-----------------------------------------------------------------------------
230
231void    set_projection( const D3DXMATRIX& proj );
232//-----------------------------------------------------------------------------
233
234void    set_camera( const camera_t& camera );
235//-----------------------------------------------------------------------------
236
237sampler_state   create_sampler_state( const sampler_state_t& desc );
238//-----------------------------------------------------------------------------
239
240state_group     create_state_group( const state_group_desc_t& desc );
241//-----------------------------------------------------------------------------
242
243texture_h   texture_create( const byte* data, uint32 data_size );
244//-----------------------------------------------------------------------------
245
246vbuffer_h   vbuffer_create( uint32 size, const byte* init_data = 0, uint32 usage = D3DUSAGE_WRITEONLY );
247//-----------------------------------------------------------------------------
248
249ibuffer_h   ibuffer_create( IB_TYPE type, uint32 size, const byte* init_data = 0, uint32 usage = D3DUSAGE_WRITEONLY );
250//-----------------------------------------------------------------------------
251
252uint32      vbuffer_lock( vbuffer_h buffer, void** mem, uint32 length = 0, uint32 offset = 0 );
253//-----------------------------------------------------------------------------
254
255uint32      vbuffer_unlock( vbuffer_h buffer );
256//-----------------------------------------------------------------------------
257
258uint32      ibuffer_lock( ibuffer_h buffer, void** mem, uint32 length = 0, uint32 offset = 0 );
259//-----------------------------------------------------------------------------
260
261uint32      ibuffer_unlock( ibuffer_h buffer );
262//-----------------------------------------------------------------------------
263
264vertex_layout_h	get_vertex_layout( uint32 vertex_type );
265//-----------------------------------------------------------------------------
266
267void		tick();
268//-----------------------------------------------------------------------------
269
270void		set_sampler( uint32 id, SAMPLER_TYPE sampler );
271//-----------------------------------------------------------------------------
272}
273
274// for tests
275bool test_init();
276
277#endif