bgfx.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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_MSAA UINT64_C(0x0020000000000000)
  63. #define BGFX_STATE_RESERVED UINT64_C(0xff00000000000000)
  64. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  65. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  66. #define BGFX_STATE_DEFAULT (0 \
  67. | BGFX_STATE_RGB_WRITE \
  68. | BGFX_STATE_ALPHA_WRITE \
  69. | BGFX_STATE_DEPTH_TEST_LESS \
  70. | BGFX_STATE_DEPTH_WRITE \
  71. | BGFX_STATE_CULL_CW \
  72. | BGFX_STATE_MSAA \
  73. )
  74. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  75. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  76. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  77. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  78. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  79. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  80. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  81. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  82. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  83. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  84. #define BGFX_TEXTURE_U_REPEAT UINT32_C(0x00000000)
  85. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  86. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  87. #define BGFX_TEXTURE_U_SHIFT 0
  88. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  89. #define BGFX_TEXTURE_V_REPEAT UINT32_C(0x00000000)
  90. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000010)
  91. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  92. #define BGFX_TEXTURE_V_SHIFT 4
  93. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  94. #define BGFX_TEXTURE_W_REPEAT UINT32_C(0x00000000)
  95. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000100)
  96. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000200)
  97. #define BGFX_TEXTURE_W_SHIFT 4
  98. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000300)
  99. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00001000)
  100. #define BGFX_TEXTURE_MIN_SHIFT 8
  101. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00001000)
  102. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00010000)
  103. #define BGFX_TEXTURE_MAG_SHIFT 12
  104. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00010000)
  105. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00100000)
  106. #define BGFX_TEXTURE_MIP_SHIFT 16
  107. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00100000)
  108. #define BGFX_TEXTURE_SRGB UINT32_C(0x00200000)
  109. #define BGFX_RENDER_TARGET_NONE UINT32_C(0x00000000)
  110. #define BGFX_RENDER_TARGET_COLOR_RGBA UINT32_C(0x00000001)
  111. #define BGFX_RENDER_TARGET_COLOR_R32F UINT32_C(0x00000002)
  112. #define BGFX_RENDER_TARGET_COLOR_SHIFT 0
  113. #define BGFX_RENDER_TARGET_COLOR_MASK UINT32_C(0x000000ff)
  114. #define BGFX_RENDER_TARGET_DEPTH UINT32_C(0x00000100)
  115. #define BGFX_RENDER_TARGET_DEPTH_SHIFT 8
  116. #define BGFX_RENDER_TARGET_DEPTH_MASK UINT32_C(0x0000ff00)
  117. #define BGFX_RENDER_TARGET_MSAA_X2 UINT32_C(0x00010000)
  118. #define BGFX_RENDER_TARGET_MSAA_X4 UINT32_C(0x00020000)
  119. #define BGFX_RENDER_TARGET_MSAA_X8 UINT32_C(0x00030000)
  120. #define BGFX_RENDER_TARGET_MSAA_X16 UINT32_C(0x00040000)
  121. #define BGFX_RENDER_TARGET_MSAA_SHIFT 16
  122. #define BGFX_RENDER_TARGET_MSAA_MASK UINT32_C(0x00070000)
  123. #define BGFX_RENDER_TARGET_SRGBWRITE UINT32_C(0x00080000)
  124. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  125. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  126. #define BGFX_RESET_FULLSCREEN_FAKE UINT32_C(0x00000002)
  127. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  128. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000003)
  129. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  130. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  131. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  132. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  133. #define BGFX_RESET_MSAA_SHIFT 4
  134. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  135. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  136. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  137. struct Fatal
  138. {
  139. enum Enum
  140. {
  141. MinimumRequiredSpecs = 1,
  142. D3D9_UnableToCreateInterface,
  143. D3D9_UnableToCreateDevice,
  144. D3D9_UnableToCreateRenderTarget,
  145. D3D9_UnableToCreateTexture,
  146. OPENGL_UnableToCreateContext,
  147. };
  148. };
  149. struct RendererType
  150. {
  151. enum Enum
  152. {
  153. Null = 0,
  154. Direct3D9,
  155. OpenGLES,
  156. OpenGL,
  157. };
  158. };
  159. struct Attrib
  160. {
  161. enum Enum
  162. {
  163. Position = 0,
  164. Normal,
  165. Color0,
  166. Color1,
  167. Indices,
  168. Weight,
  169. TexCoord0,
  170. TexCoord1,
  171. TexCoord2,
  172. TexCoord3,
  173. TexCoord4,
  174. TexCoord5,
  175. TexCoord6,
  176. TexCoord7,
  177. Count,
  178. };
  179. };
  180. struct AttribType
  181. {
  182. enum Enum
  183. {
  184. Uint8,
  185. Uint16,
  186. Float,
  187. Count,
  188. };
  189. };
  190. static const uint16_t invalidHandle = 0xffff;
  191. typedef struct { uint16_t idx; } FragmentShaderHandle;
  192. typedef struct { uint16_t idx; } IndexBufferHandle;
  193. typedef struct { uint16_t idx; } MaterialHandle;
  194. typedef struct { uint16_t idx; } RenderTargetHandle;
  195. typedef struct { uint16_t idx; } TextureHandle;
  196. typedef struct { uint16_t idx; } UniformHandle;
  197. typedef struct { uint16_t idx; } VertexBufferHandle;
  198. typedef struct { uint16_t idx; } VertexDeclHandle;
  199. typedef struct { uint16_t idx; } VertexShaderHandle;
  200. struct Memory
  201. {
  202. uint8_t* data;
  203. uint32_t size;
  204. };
  205. struct DynamicIndexBuffer
  206. {
  207. uint8_t* data;
  208. uint32_t size;
  209. IndexBufferHandle handle;
  210. uint32_t startIndex;
  211. };
  212. struct DynamicVertexBuffer
  213. {
  214. uint8_t* data;
  215. uint32_t size;
  216. uint32_t startVertex;
  217. uint16_t stride;
  218. VertexBufferHandle handle;
  219. VertexDeclHandle decl;
  220. };
  221. struct ConstantType
  222. {
  223. enum Enum
  224. {
  225. Uniform1i,
  226. Uniform1f,
  227. End,
  228. Uniform1iv,
  229. Uniform1fv,
  230. Uniform2fv,
  231. Uniform3fv,
  232. Uniform4fv,
  233. Uniform3x3fv,
  234. Uniform4x4fv,
  235. Count
  236. };
  237. };
  238. typedef void (*fatalFn)(Fatal::Enum _code, const char* _str);
  239. typedef void* (*reallocFn)(void* _ptr, size_t _size);
  240. typedef void (*freeFn)(void* _ptr);
  241. typedef void (*cacheFn)(uint64_t _id, bool _store, void* _data, uint32_t& _length);
  242. struct VertexDecl
  243. {
  244. void begin();
  245. void end();
  246. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false);
  247. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized) const;
  248. uint32_t m_hash;
  249. uint16_t m_stride;
  250. uint16_t m_offset[Attrib::Count];
  251. uint8_t m_attributes[Attrib::Count];
  252. };
  253. ///
  254. RendererType::Enum getRendererType();
  255. ///
  256. void init(bool _createRenderThread = true, fatalFn _fatal = NULL, reallocFn _realloc = NULL, freeFn _free = NULL, cacheFn _cache = NULL);
  257. ///
  258. void shutdown();
  259. ///
  260. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  261. ///
  262. void frame();
  263. ///
  264. const Memory* alloc(uint32_t _size);
  265. ///
  266. const Memory* makeRef(void* _data, uint32_t _size);
  267. ///
  268. void setDebug(uint32_t _debug);
  269. ///
  270. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  271. ///
  272. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  273. ///
  274. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  275. ///
  276. void destroyIndexBuffer(IndexBufferHandle _handle);
  277. ///
  278. bool checkAvailDynamicIndexBuffer(uint16_t _num);
  279. ///
  280. const DynamicIndexBuffer* allocDynamicIndexBuffer(uint16_t _num);
  281. ///
  282. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  283. ///
  284. void destroyVertexBuffer(VertexBufferHandle _handle);
  285. ///
  286. bool checkAvailDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  287. ///
  288. const DynamicVertexBuffer* allocDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  289. ///
  290. VertexShaderHandle createVertexShader(const Memory* _mem);
  291. ///
  292. void destroyVertexShader(VertexShaderHandle _handle);
  293. ///
  294. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  295. ///
  296. void destroyFragmentShader(FragmentShaderHandle _handle);
  297. ///
  298. MaterialHandle createMaterial(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  299. ///
  300. void destroyMaterial(MaterialHandle _handle);
  301. ///
  302. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, uint16_t* _width = NULL, uint16_t* _height = NULL);
  303. ///
  304. void destroyTexture(TextureHandle _handle);
  305. ///
  306. RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags = BGFX_RENDER_TARGET_COLOR_RGBA);
  307. ///
  308. void destroyRenderTarget(RenderTargetHandle _handle);
  309. ///
  310. UniformHandle createUniform(const char* _name, ConstantType::Enum _type, uint16_t _num = 1);
  311. ///
  312. void destroyUniform(UniformHandle _handle);
  313. ///
  314. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  315. ///
  316. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  317. ///
  318. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  319. ///
  320. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  321. ///
  322. void setViewSeq(uint8_t _id, bool _enabled);
  323. ///
  324. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  325. ///
  326. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  327. ///
  328. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  329. ///
  330. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  331. ///
  332. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  333. ///
  334. void setState(uint64_t _state);
  335. ///
  336. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  337. ///
  338. void setTransform(uint32_t _cache = 0, uint16_t _num = 1);
  339. ///
  340. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  341. ///
  342. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);
  343. ///
  344. void setIndexBuffer(IndexBufferHandle _handle);
  345. ///
  346. void setIndexBuffer(const DynamicIndexBuffer* _ib, uint32_t _numIndices = 0xffffffff);
  347. ///
  348. void setVertexBuffer(VertexBufferHandle _handle);
  349. ///
  350. void setVertexBuffer(const DynamicVertexBuffer* _vb);
  351. ///
  352. void setMaterial(MaterialHandle _handle);
  353. ///
  354. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  355. ///
  356. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  357. ///
  358. void submit(uint8_t _id);
  359. ///
  360. void submitMask(uint32_t _viewMask);
  361. ///
  362. void saveScreenShot(const char* _filePath);
  363. } // namespace bgfx
  364. #endif // __BGFX_H__