bgfx.h 13 KB

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