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