bgfx.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef __BGFX_H__
  6. #define __BGFX_H__
  7. #include <stdint.h> // uint32_t
  8. #include <stdlib.h> // size_t
  9. namespace bgfx
  10. {
  11. #define BGFX_STATE_DEPTH_WRITE UINT64_C(0x0000000000000001)
  12. #define BGFX_STATE_ALPHA_TEST UINT64_C(0x0000000000000004)
  13. #define BGFX_STATE_ALPHA_WRITE UINT64_C(0x0000000000000008)
  14. #define BGFX_STATE_ALPHA_MASK UINT64_C(0x000000000000000c)
  15. #define BGFX_STATE_DEPTH_TEST_LESS UINT64_C(0x0000000000000010)
  16. #define BGFX_STATE_DEPTH_TEST_LEQUAL UINT64_C(0x0000000000000020)
  17. #define BGFX_STATE_DEPTH_TEST_EQUAL UINT64_C(0x0000000000000030)
  18. #define BGFX_STATE_DEPTH_TEST_GEQUAL UINT64_C(0x0000000000000040)
  19. #define BGFX_STATE_DEPTH_TEST_GREATER UINT64_C(0x0000000000000050)
  20. #define BGFX_STATE_DEPTH_TEST_NOTEQUAL UINT64_C(0x0000000000000060)
  21. #define BGFX_STATE_DEPTH_TEST_NEVER UINT64_C(0x0000000000000070)
  22. #define BGFX_STATE_DEPTH_TEST_ALWAYS UINT64_C(0x0000000000000080)
  23. #define BGFX_STATE_DEPTH_TEST_SHIFT 4
  24. #define BGFX_STATE_DEPTH_TEST_MASK UINT64_C(0x00000000000000f0)
  25. #define BGFX_STATE_ALPHA_TEST_LESS UINT64_C(0x0000000000000100)
  26. #define BGFX_STATE_ALPHA_TEST_LEQUAL UINT64_C(0x0000000000000200)
  27. #define BGFX_STATE_ALPHA_TEST_EQUAL UINT64_C(0x0000000000000300)
  28. #define BGFX_STATE_ALPHA_TEST_GEQUAL UINT64_C(0x0000000000000400)
  29. #define BGFX_STATE_ALPHA_TEST_GREATER UINT64_C(0x0000000000000500)
  30. #define BGFX_STATE_ALPHA_TEST_NOTEQUAL UINT64_C(0x0000000000000600)
  31. #define BGFX_STATE_ALPHA_TEST_NEVER UINT64_C(0x0000000000000700)
  32. #define BGFX_STATE_ALPHA_TEST_ALWAYS UINT64_C(0x0000000000000800)
  33. #define BGFX_STATE_ALPHA_TEST_SHIFT 8
  34. #define BGFX_STATE_ALPHA_TEST_MASK UINT64_C(0x0000000000000f00)
  35. #define BGFX_STATE_BLEND_ZERO UINT64_C(0x0000000000001000)
  36. #define BGFX_STATE_BLEND_ONE UINT64_C(0x0000000000002000)
  37. #define BGFX_STATE_BLEND_SRC_COLOR UINT64_C(0x0000000000003000)
  38. #define BGFX_STATE_BLEND_INV_SRC_COLOR UINT64_C(0x0000000000004000)
  39. #define BGFX_STATE_BLEND_SRC_ALPHA UINT64_C(0x0000000000005000)
  40. #define BGFX_STATE_BLEND_INV_SRC_ALPHA UINT64_C(0x0000000000006000)
  41. #define BGFX_STATE_BLEND_DST_ALPHA UINT64_C(0x0000000000007000)
  42. #define BGFX_STATE_BLEND_INV_DST_ALPHA UINT64_C(0x0000000000008000)
  43. #define BGFX_STATE_BLEND_DST_COLOR UINT64_C(0x0000000000009000)
  44. #define BGFX_STATE_BLEND_INV_DST_COLOR UINT64_C(0x000000000000a000)
  45. #define BGFX_STATE_BLEND_SRC_ALPHA_SAT UINT64_C(0x000000000000b000)
  46. #define BGFX_STATE_BLEND_SHIFT 12
  47. #define BGFX_STATE_BLEND_MASK UINT64_C(0x000000000ffff000)
  48. #define BGFX_STATE_CULL_CW UINT64_C(0x0000000010000000)
  49. #define BGFX_STATE_CULL_CCW UINT64_C(0x0000000020000000)
  50. #define BGFX_STATE_CULL_SHIFT 28
  51. #define BGFX_STATE_CULL_MASK UINT64_C(0x0000000030000000)
  52. #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000040000000)
  53. #define BGFX_STATE_ALPHA_REF_SHIFT 32
  54. #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x000000ff00000000)
  55. #define BGFX_STATE_PT_LINES UINT64_C(0x0000010000000000)
  56. #define BGFX_STATE_PT_POINTS UINT64_C(0x0000020000000000)
  57. #define BGFX_STATE_PT_SHIFT 40
  58. #define BGFX_STATE_PT_MASK UINT64_C(0x0000030000000000)
  59. #define BGFX_STATE_POINT_SIZE_SHIFT 44
  60. #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x000ff00000000000)
  61. #define BGFX_STATE_SRGBWRITE UINT64_C(0x0010000000000000)
  62. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  63. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  64. #define BGFX_STATE_DEFAULT (0 \
  65. | BGFX_STATE_RGB_WRITE \
  66. | BGFX_STATE_ALPHA_WRITE \
  67. | BGFX_STATE_DEPTH_TEST_LESS \
  68. | BGFX_STATE_DEPTH_WRITE \
  69. | BGFX_STATE_CULL_CW \
  70. )
  71. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  72. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  73. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  74. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  75. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  76. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  77. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  78. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  79. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  80. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  81. #define BGFX_TEXTURE_U_REPEAT UINT32_C(0x00000000)
  82. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  83. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  84. #define BGFX_TEXTURE_U_SHIFT 0
  85. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  86. #define BGFX_TEXTURE_V_REPEAT UINT32_C(0x00000000)
  87. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000010)
  88. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  89. #define BGFX_TEXTURE_V_SHIFT 4
  90. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  91. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00000100)
  92. #define BGFX_TEXTURE_MIN_SHIFT 8
  93. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00000100)
  94. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00001000)
  95. #define BGFX_TEXTURE_MAG_SHIFT 12
  96. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00001000)
  97. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00010000)
  98. #define BGFX_TEXTURE_MIP_SHIFT 16
  99. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00010000)
  100. #define BGFX_TEXTURE_SRGB UINT32_C(0x00020000)
  101. #define BGFX_RENDER_TARGET_NONE UINT32_C(0x00000000)
  102. #define BGFX_RENDER_TARGET_COLOR_RGBA UINT32_C(0x00000001)
  103. #define BGFX_RENDER_TARGET_COLOR_R32F UINT32_C(0x00000002)
  104. #define BGFX_RENDER_TARGET_COLOR_SHIFT 0
  105. #define BGFX_RENDER_TARGET_COLOR_MASK UINT32_C(0x000000ff)
  106. #define BGFX_RENDER_TARGET_DEPTH UINT32_C(0x00000100)
  107. #define BGFX_RENDER_TARGET_DEPTH_SHIFT 8
  108. #define BGFX_RENDER_TARGET_DEPTH_MASK UINT32_C(0x0000ff00)
  109. #define BGFX_RENDER_TARGET_MSAA_X2 UINT32_C(0x00010000)
  110. #define BGFX_RENDER_TARGET_MSAA_X4 UINT32_C(0x00020000)
  111. #define BGFX_RENDER_TARGET_MSAA_X8 UINT32_C(0x00030000)
  112. #define BGFX_RENDER_TARGET_MSAA_X16 UINT32_C(0x00040000)
  113. #define BGFX_RENDER_TARGET_MSAA_SHIFT 16
  114. #define BGFX_RENDER_TARGET_MSAA_MASK UINT32_C(0x00070000)
  115. #define BGFX_RENDER_TARGET_SRGBWRITE UINT32_C(0x00080000)
  116. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  117. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  118. #define BGFX_RESET_FULLSCREEN_FAKE UINT32_C(0x00000002)
  119. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  120. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000003)
  121. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  122. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  123. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  124. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  125. #define BGFX_RESET_MSAA_SHIFT 4
  126. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  127. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  128. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  129. struct Fatal
  130. {
  131. enum Enum
  132. {
  133. MinimumRequiredSpecs = 1,
  134. D3D9_UnableToCreateInterface,
  135. D3D9_UnableToCreateDevice,
  136. D3D9_UnableToCreateRenderTarget,
  137. OPENGL_UnableToCreateContext,
  138. };
  139. };
  140. struct RendererType
  141. {
  142. enum Enum
  143. {
  144. Null = 0,
  145. Direct3D9,
  146. OpenGLES,
  147. OpenGL,
  148. };
  149. };
  150. struct Attrib
  151. {
  152. enum Enum
  153. {
  154. Position = 0,
  155. Normal,
  156. Color0,
  157. Color1,
  158. Indices,
  159. Weight,
  160. TexCoord0,
  161. TexCoord1,
  162. TexCoord2,
  163. TexCoord3,
  164. TexCoord4,
  165. TexCoord5,
  166. TexCoord6,
  167. TexCoord7,
  168. Count,
  169. };
  170. };
  171. struct AttribType
  172. {
  173. enum Enum
  174. {
  175. Uint8,
  176. Uint16,
  177. Float,
  178. Count,
  179. };
  180. };
  181. static const uint16_t invalidHandle = 0xffff;
  182. typedef struct { uint16_t idx; } FragmentShaderHandle;
  183. typedef struct { uint16_t idx; } IndexBufferHandle;
  184. typedef struct { uint16_t idx; } MaterialHandle;
  185. typedef struct { uint16_t idx; } RenderTargetHandle;
  186. typedef struct { uint16_t idx; } TextureHandle;
  187. typedef struct { uint16_t idx; } UniformHandle;
  188. typedef struct { uint16_t idx; } VertexBufferHandle;
  189. typedef struct { uint16_t idx; } VertexDeclHandle;
  190. typedef struct { uint16_t idx; } VertexShaderHandle;
  191. struct Memory
  192. {
  193. uint8_t* data;
  194. uint32_t size;
  195. };
  196. struct DynamicIndexBuffer
  197. {
  198. uint8_t* data;
  199. uint32_t size;
  200. IndexBufferHandle handle;
  201. uint32_t startIndex;
  202. };
  203. struct DynamicVertexBuffer
  204. {
  205. uint8_t* data;
  206. uint32_t size;
  207. uint32_t startVertex;
  208. uint16_t stride;
  209. VertexBufferHandle handle;
  210. VertexDeclHandle decl;
  211. };
  212. struct ConstantType
  213. {
  214. enum Enum
  215. {
  216. Uniform1i,
  217. Uniform1f,
  218. End,
  219. Uniform1iv,
  220. Uniform1fv,
  221. Uniform2fv,
  222. Uniform3fv,
  223. Uniform4fv,
  224. Uniform3x3fv,
  225. Uniform4x4fv,
  226. Count
  227. };
  228. };
  229. typedef void (*fatalFn)(Fatal::Enum _code, const char* _str);
  230. typedef void* (*reallocFn)(void* _ptr, size_t _size);
  231. typedef void (*freeFn)(void* _ptr);
  232. typedef void (*cacheFn)(uint64_t _id, bool _store, void* _data, uint32_t& _length);
  233. struct VertexDecl
  234. {
  235. void begin();
  236. void end();
  237. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false);
  238. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized) const;
  239. uint32_t m_hash;
  240. uint16_t m_stride;
  241. uint16_t m_offset[Attrib::Count];
  242. uint8_t m_attributes[Attrib::Count];
  243. };
  244. ///
  245. RendererType::Enum getRendererType();
  246. ///
  247. void init(bool _createRenderThread = true, fatalFn _fatal = NULL, reallocFn _realloc = NULL, freeFn _free = NULL, cacheFn _cache = NULL);
  248. ///
  249. void shutdown();
  250. ///
  251. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  252. ///
  253. void frame();
  254. ///
  255. const Memory* alloc(uint32_t _size);
  256. ///
  257. const Memory* makeRef(void* _data, uint32_t _size);
  258. ///
  259. void setDebug(uint32_t _debug);
  260. ///
  261. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  262. ///
  263. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  264. ///
  265. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  266. ///
  267. void destroyIndexBuffer(IndexBufferHandle _handle);
  268. ///
  269. bool checkAvailDynamicIndexBuffer(uint16_t _num);
  270. ///
  271. const DynamicIndexBuffer* allocDynamicIndexBuffer(uint16_t _num);
  272. ///
  273. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  274. ///
  275. void destroyVertexBuffer(VertexBufferHandle _handle);
  276. ///
  277. bool checkAvailDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  278. ///
  279. const DynamicVertexBuffer* allocDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  280. ///
  281. VertexShaderHandle createVertexShader(const Memory* _mem);
  282. ///
  283. void destroyVertexShader(VertexShaderHandle _handle);
  284. ///
  285. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  286. ///
  287. void destroyFragmentShader(FragmentShaderHandle _handle);
  288. ///
  289. MaterialHandle createMaterial(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  290. ///
  291. void destroyMaterial(MaterialHandle _handle);
  292. ///
  293. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, uint16_t* _width = NULL, uint16_t* _height = NULL);
  294. ///
  295. void destroyTexture(TextureHandle _handle);
  296. ///
  297. RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags = BGFX_RENDER_TARGET_COLOR_RGBA);
  298. ///
  299. void destroyRenderTarget(RenderTargetHandle _handle);
  300. ///
  301. UniformHandle createUniform(const char* _name, ConstantType::Enum _type, uint16_t _num = 1);
  302. ///
  303. void destroyUniform(UniformHandle _handle);
  304. ///
  305. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  306. ///
  307. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  308. ///
  309. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  310. ///
  311. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  312. ///
  313. void setViewSeq(uint8_t _id, bool _enabled);
  314. ///
  315. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  316. ///
  317. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  318. ///
  319. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  320. ///
  321. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  322. ///
  323. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  324. ///
  325. void setState(uint64_t _state);
  326. ///
  327. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  328. ///
  329. void setTransform(uint32_t _cache = 0, uint16_t _num = 1);
  330. ///
  331. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  332. ///
  333. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);
  334. ///
  335. void setIndexBuffer(IndexBufferHandle _handle);
  336. ///
  337. void setIndexBuffer(const DynamicIndexBuffer* _ib, uint32_t _numIndices = 0xffffffff);
  338. ///
  339. void setVertexBuffer(VertexBufferHandle _handle);
  340. ///
  341. void setVertexBuffer(const DynamicVertexBuffer* _vb);
  342. ///
  343. void setMaterial(MaterialHandle _handle);
  344. ///
  345. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  346. ///
  347. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  348. ///
  349. void submit(uint8_t _id);
  350. ///
  351. void submitMask(uint32_t _viewMask);
  352. ///
  353. void saveScreenShot(const char* _filePath);
  354. } // namespace bgfx
  355. #endif // __BGFX_H__