bgfx.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. ///
  10. #define BGFX_STATE_DEPTH_WRITE UINT64_C(0x0000000000000001)
  11. #define BGFX_STATE_ALPHA_TEST UINT64_C(0x0000000000000004)
  12. #define BGFX_STATE_ALPHA_WRITE UINT64_C(0x0000000000000008)
  13. #define BGFX_STATE_ALPHA_MASK UINT64_C(0x000000000000000c)
  14. #define BGFX_STATE_DEPTH_TEST_LESS UINT64_C(0x0000000000000010)
  15. #define BGFX_STATE_DEPTH_TEST_LEQUAL UINT64_C(0x0000000000000020)
  16. #define BGFX_STATE_DEPTH_TEST_EQUAL UINT64_C(0x0000000000000030)
  17. #define BGFX_STATE_DEPTH_TEST_GEQUAL UINT64_C(0x0000000000000040)
  18. #define BGFX_STATE_DEPTH_TEST_GREATER UINT64_C(0x0000000000000050)
  19. #define BGFX_STATE_DEPTH_TEST_NOTEQUAL UINT64_C(0x0000000000000060)
  20. #define BGFX_STATE_DEPTH_TEST_NEVER UINT64_C(0x0000000000000070)
  21. #define BGFX_STATE_DEPTH_TEST_ALWAYS UINT64_C(0x0000000000000080)
  22. #define BGFX_STATE_DEPTH_TEST_SHIFT 4
  23. #define BGFX_STATE_DEPTH_TEST_MASK UINT64_C(0x00000000000000f0)
  24. #define BGFX_STATE_ALPHA_TEST_LESS UINT64_C(0x0000000000000100)
  25. #define BGFX_STATE_ALPHA_TEST_LEQUAL UINT64_C(0x0000000000000200)
  26. #define BGFX_STATE_ALPHA_TEST_EQUAL UINT64_C(0x0000000000000300)
  27. #define BGFX_STATE_ALPHA_TEST_GEQUAL UINT64_C(0x0000000000000400)
  28. #define BGFX_STATE_ALPHA_TEST_GREATER UINT64_C(0x0000000000000500)
  29. #define BGFX_STATE_ALPHA_TEST_NOTEQUAL UINT64_C(0x0000000000000600)
  30. #define BGFX_STATE_ALPHA_TEST_NEVER UINT64_C(0x0000000000000700)
  31. #define BGFX_STATE_ALPHA_TEST_ALWAYS UINT64_C(0x0000000000000800)
  32. #define BGFX_STATE_ALPHA_TEST_SHIFT 8
  33. #define BGFX_STATE_ALPHA_TEST_MASK UINT64_C(0x0000000000000f00)
  34. #define BGFX_STATE_BLEND_ZERO UINT64_C(0x0000000000001000)
  35. #define BGFX_STATE_BLEND_ONE UINT64_C(0x0000000000002000)
  36. #define BGFX_STATE_BLEND_SRC_COLOR UINT64_C(0x0000000000003000)
  37. #define BGFX_STATE_BLEND_INV_SRC_COLOR UINT64_C(0x0000000000004000)
  38. #define BGFX_STATE_BLEND_SRC_ALPHA UINT64_C(0x0000000000005000)
  39. #define BGFX_STATE_BLEND_INV_SRC_ALPHA UINT64_C(0x0000000000006000)
  40. #define BGFX_STATE_BLEND_DST_ALPHA UINT64_C(0x0000000000007000)
  41. #define BGFX_STATE_BLEND_INV_DST_ALPHA UINT64_C(0x0000000000008000)
  42. #define BGFX_STATE_BLEND_DST_COLOR UINT64_C(0x0000000000009000)
  43. #define BGFX_STATE_BLEND_INV_DST_COLOR UINT64_C(0x000000000000a000)
  44. #define BGFX_STATE_BLEND_SRC_ALPHA_SAT UINT64_C(0x000000000000b000)
  45. #define BGFX_STATE_BLEND_SHIFT 12
  46. #define BGFX_STATE_BLEND_MASK UINT64_C(0x000000000ffff000)
  47. #define BGFX_STATE_CULL_CW UINT64_C(0x0000000010000000)
  48. #define BGFX_STATE_CULL_CCW UINT64_C(0x0000000020000000)
  49. #define BGFX_STATE_CULL_SHIFT 28
  50. #define BGFX_STATE_CULL_MASK UINT64_C(0x0000000030000000)
  51. #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000040000000)
  52. #define BGFX_STATE_ALPHA_REF_SHIFT 32
  53. #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x000000ff00000000)
  54. #define BGFX_STATE_PT_LINES UINT64_C(0x0000010000000000)
  55. #define BGFX_STATE_PT_POINTS UINT64_C(0x0000020000000000)
  56. #define BGFX_STATE_PT_SHIFT 40
  57. #define BGFX_STATE_PT_MASK UINT64_C(0x0000030000000000)
  58. #define BGFX_STATE_POINT_SIZE_SHIFT 44
  59. #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x000ff00000000000)
  60. #define BGFX_STATE_SRGBWRITE UINT64_C(0x0010000000000000)
  61. #define BGFX_STATE_MSAA UINT64_C(0x0020000000000000)
  62. #define BGFX_STATE_RESERVED UINT64_C(0xff00000000000000)
  63. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  64. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  65. #define BGFX_STATE_DEFAULT (0 \
  66. | BGFX_STATE_RGB_WRITE \
  67. | BGFX_STATE_ALPHA_WRITE \
  68. | BGFX_STATE_DEPTH_TEST_LESS \
  69. | BGFX_STATE_DEPTH_WRITE \
  70. | BGFX_STATE_CULL_CW \
  71. | BGFX_STATE_MSAA \
  72. )
  73. #define BGFX_STATE_ALPHA_REF(_ref) ( (uint64_t(_ref)<<BGFX_STATE_ALPHA_REF_SHIFT)&BGFX_STATE_ALPHA_REF_MASK)
  74. #define BGFX_STATE_POINT_SIZE(_size) ( (uint64_t(_size)<<BGFX_STATE_POINT_SIZE_SHIFT)&BGFX_STATE_POINT_SIZE_MASK)
  75. ///
  76. #define BGFX_STENCIL_FUNC_REF_SHIFT 0
  77. #define BGFX_STENCIL_FUNC_REF_MASK UINT32_C(0x000000ff)
  78. #define BGFX_STENCIL_FUNC_RMASK_SHIFT 8
  79. #define BGFX_STENCIL_FUNC_RMASK_MASK UINT32_C(0x0000ff00)
  80. #define BGFX_STENCIL_TEST_LESS UINT32_C(0x00010000)
  81. #define BGFX_STENCIL_TEST_LEQUAL UINT32_C(0x00020000)
  82. #define BGFX_STENCIL_TEST_EQUAL UINT32_C(0x00030000)
  83. #define BGFX_STENCIL_TEST_GEQUAL UINT32_C(0x00040000)
  84. #define BGFX_STENCIL_TEST_GREATER UINT32_C(0x00050000)
  85. #define BGFX_STENCIL_TEST_NOTEQUAL UINT32_C(0x00060000)
  86. #define BGFX_STENCIL_TEST_NEVER UINT32_C(0x00070000)
  87. #define BGFX_STENCIL_TEST_ALWAYS UINT32_C(0x00080000)
  88. #define BGFX_STENCIL_TEST_SHIFT 16
  89. #define BGFX_STENCIL_TEST_MASK UINT32_C(0x000f0000)
  90. #define BGFX_STENCIL_OP_FAIL_S_ZERO UINT32_C(0x00000000)
  91. #define BGFX_STENCIL_OP_FAIL_S_KEEP UINT32_C(0x00100000)
  92. #define BGFX_STENCIL_OP_FAIL_S_REPLACE UINT32_C(0x00200000)
  93. #define BGFX_STENCIL_OP_FAIL_S_INCR UINT32_C(0x00300000)
  94. #define BGFX_STENCIL_OP_FAIL_S_INCRSAT UINT32_C(0x00400000)
  95. #define BGFX_STENCIL_OP_FAIL_S_DECR UINT32_C(0x00500000)
  96. #define BGFX_STENCIL_OP_FAIL_S_DECRSAT UINT32_C(0x00600000)
  97. #define BGFX_STENCIL_OP_FAIL_S_INVERT UINT32_C(0x00700000)
  98. #define BGFX_STENCIL_OP_FAIL_S_SHIFT 20
  99. #define BGFX_STENCIL_OP_FAIL_S_MASK UINT32_C(0x00f00000)
  100. #define BGFX_STENCIL_OP_FAIL_Z_ZERO UINT32_C(0x00000000)
  101. #define BGFX_STENCIL_OP_FAIL_Z_KEEP UINT32_C(0x01000000)
  102. #define BGFX_STENCIL_OP_FAIL_Z_REPLACE UINT32_C(0x02000000)
  103. #define BGFX_STENCIL_OP_FAIL_Z_INCR UINT32_C(0x03000000)
  104. #define BGFX_STENCIL_OP_FAIL_Z_INCRSAT UINT32_C(0x04000000)
  105. #define BGFX_STENCIL_OP_FAIL_Z_DECR UINT32_C(0x05000000)
  106. #define BGFX_STENCIL_OP_FAIL_Z_DECRSAT UINT32_C(0x06000000)
  107. #define BGFX_STENCIL_OP_FAIL_Z_INVERT UINT32_C(0x07000000)
  108. #define BGFX_STENCIL_OP_FAIL_Z_SHIFT 24
  109. #define BGFX_STENCIL_OP_FAIL_Z_MASK UINT32_C(0x0f000000)
  110. #define BGFX_STENCIL_OP_PASS_Z_ZERO UINT32_C(0x00000000)
  111. #define BGFX_STENCIL_OP_PASS_Z_KEEP UINT32_C(0x10000000)
  112. #define BGFX_STENCIL_OP_PASS_Z_REPLACE UINT32_C(0x20000000)
  113. #define BGFX_STENCIL_OP_PASS_Z_INCR UINT32_C(0x30000000)
  114. #define BGFX_STENCIL_OP_PASS_Z_INCRSAT UINT32_C(0x40000000)
  115. #define BGFX_STENCIL_OP_PASS_Z_DECR UINT32_C(0x50000000)
  116. #define BGFX_STENCIL_OP_PASS_Z_DECRSAT UINT32_C(0x60000000)
  117. #define BGFX_STENCIL_OP_PASS_Z_INVERT UINT32_C(0x70000000)
  118. #define BGFX_STENCIL_OP_PASS_Z_SHIFT 28
  119. #define BGFX_STENCIL_OP_PASS_Z_MASK UINT32_C(0xf0000000)
  120. #define BGFX_STENCIL_NONE UINT32_C(0x00000000)
  121. #define BGFX_STENCIL_MASK UINT32_C(0xffffffff)
  122. #define BGFX_STENCIL_DEFAULT UINT32_C(0x00000000)
  123. #define BGFX_STENCIL_FUNC_REF(_ref) ( (uint32_t(_ref)<<BGFX_STENCIL_FUNC_REF_SHIFT)&BGFX_STENCIL_FUNC_REF_MASK)
  124. #define BGFX_STENCIL_FUNC_RMASK(_mask) ( (uint32_t(_mask)<<BGFX_STENCIL_FUNC_RMASK_SHIFT)&BGFX_STENCIL_FUNC_RMASK_MASK)
  125. ///
  126. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  127. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  128. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  129. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  130. ///
  131. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  132. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  133. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  134. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  135. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  136. ///
  137. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  138. #define BGFX_TEXTURE_U_REPEAT UINT32_C(0x00000000)
  139. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  140. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  141. #define BGFX_TEXTURE_U_SHIFT 0
  142. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  143. #define BGFX_TEXTURE_V_REPEAT UINT32_C(0x00000000)
  144. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000010)
  145. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  146. #define BGFX_TEXTURE_V_SHIFT 4
  147. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  148. #define BGFX_TEXTURE_W_REPEAT UINT32_C(0x00000000)
  149. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000100)
  150. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000200)
  151. #define BGFX_TEXTURE_W_SHIFT 8
  152. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000300)
  153. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00001000)
  154. #define BGFX_TEXTURE_MIN_SHIFT 12
  155. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00001000)
  156. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00010000)
  157. #define BGFX_TEXTURE_MAG_SHIFT 16
  158. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00010000)
  159. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00100000)
  160. #define BGFX_TEXTURE_MIP_SHIFT 20
  161. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00100000)
  162. #define BGFX_TEXTURE_SRGB UINT32_C(0x00200000)
  163. ///
  164. #define BGFX_RENDER_TARGET_NONE UINT32_C(0x00000000)
  165. #define BGFX_RENDER_TARGET_COLOR_RGBA UINT32_C(0x00000001)
  166. #define BGFX_RENDER_TARGET_COLOR_R32F UINT32_C(0x00000002)
  167. #define BGFX_RENDER_TARGET_COLOR_SHIFT 0
  168. #define BGFX_RENDER_TARGET_COLOR_MASK UINT32_C(0x000000ff)
  169. #define BGFX_RENDER_TARGET_DEPTH UINT32_C(0x00000100)
  170. #define BGFX_RENDER_TARGET_DEPTH_SHIFT 8
  171. #define BGFX_RENDER_TARGET_DEPTH_MASK UINT32_C(0x0000ff00)
  172. #define BGFX_RENDER_TARGET_MSAA_X2 UINT32_C(0x00010000)
  173. #define BGFX_RENDER_TARGET_MSAA_X4 UINT32_C(0x00020000)
  174. #define BGFX_RENDER_TARGET_MSAA_X8 UINT32_C(0x00030000)
  175. #define BGFX_RENDER_TARGET_MSAA_X16 UINT32_C(0x00040000)
  176. #define BGFX_RENDER_TARGET_MSAA_SHIFT 16
  177. #define BGFX_RENDER_TARGET_MSAA_MASK UINT32_C(0x00070000)
  178. #define BGFX_RENDER_TARGET_SRGBWRITE UINT32_C(0x00080000)
  179. ///
  180. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  181. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  182. #define BGFX_RESET_FULLSCREEN_FAKE UINT32_C(0x00000002)
  183. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  184. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000003)
  185. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  186. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  187. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  188. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  189. #define BGFX_RESET_MSAA_SHIFT 4
  190. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  191. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  192. ///
  193. #define BGFX_HANDLE(_name) struct _name { uint16_t idx; }
  194. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  195. /// BGFX
  196. namespace bgfx
  197. {
  198. struct Fatal
  199. {
  200. enum Enum
  201. {
  202. MinimumRequiredSpecs = 1,
  203. InvalidShader,
  204. D3D9_UnableToCreateInterface,
  205. D3D9_UnableToCreateDevice,
  206. D3D9_UnableToCreateRenderTarget,
  207. D3D9_UnableToCreateTexture,
  208. D3D11_UnableToInitialize,
  209. OPENGL_UnableToCreateContext,
  210. };
  211. };
  212. struct RendererType
  213. {
  214. enum Enum
  215. {
  216. Null,
  217. Direct3D9,
  218. Direct3D11,
  219. OpenGLES2,
  220. OpenGLES3,
  221. OpenGL,
  222. Count
  223. };
  224. };
  225. struct Attrib
  226. {
  227. enum Enum
  228. {
  229. Position,
  230. Normal,
  231. Tangent,
  232. Color0,
  233. Color1,
  234. Indices,
  235. Weight,
  236. TexCoord0,
  237. TexCoord1,
  238. TexCoord2,
  239. TexCoord3,
  240. TexCoord4,
  241. TexCoord5,
  242. TexCoord6,
  243. TexCoord7,
  244. Count
  245. };
  246. };
  247. struct AttribType
  248. {
  249. enum Enum
  250. {
  251. Uint8,
  252. Uint16,
  253. Half,
  254. Float,
  255. Count
  256. };
  257. };
  258. struct TextureFormat
  259. {
  260. enum Enum
  261. {
  262. Dxt1,
  263. Dxt3,
  264. Dxt5,
  265. Unknown,
  266. L8,
  267. XRGB8,
  268. ARGB8,
  269. ABGR16,
  270. R5G6B5,
  271. RGBA4,
  272. RGB5A1,
  273. RGB10A2,
  274. Count
  275. };
  276. };
  277. struct UniformType
  278. {
  279. enum Enum
  280. {
  281. Uniform1i,
  282. Uniform1f,
  283. End,
  284. Uniform1iv,
  285. Uniform1fv,
  286. Uniform2fv,
  287. Uniform3fv,
  288. Uniform4fv,
  289. Uniform3x3fv,
  290. Uniform4x4fv,
  291. Count
  292. };
  293. };
  294. static const uint16_t invalidHandle = UINT16_MAX;
  295. BGFX_HANDLE(DynamicIndexBufferHandle);
  296. BGFX_HANDLE(DynamicVertexBufferHandle);
  297. BGFX_HANDLE(FragmentShaderHandle);
  298. BGFX_HANDLE(IndexBufferHandle);
  299. BGFX_HANDLE(ProgramHandle);
  300. BGFX_HANDLE(RenderTargetHandle);
  301. BGFX_HANDLE(TextureHandle);
  302. BGFX_HANDLE(UniformHandle);
  303. BGFX_HANDLE(VertexBufferHandle);
  304. BGFX_HANDLE(VertexDeclHandle);
  305. BGFX_HANDLE(VertexShaderHandle);
  306. typedef void (*FatalFn)(Fatal::Enum _code, const char* _str);
  307. typedef void* (*ReallocFn)(void* _ptr, size_t _size);
  308. typedef void (*FreeFn)(void* _ptr);
  309. typedef void (*CacheFn)(uint64_t _id, bool _store, void* _data, uint32_t& _length);
  310. struct Memory
  311. {
  312. uint8_t* data;
  313. uint32_t size;
  314. };
  315. struct TransientIndexBuffer
  316. {
  317. uint8_t* data;
  318. uint32_t size;
  319. IndexBufferHandle handle;
  320. uint32_t startIndex;
  321. };
  322. struct TransientVertexBuffer
  323. {
  324. uint8_t* data;
  325. uint32_t size;
  326. uint32_t startVertex;
  327. uint16_t stride;
  328. VertexBufferHandle handle;
  329. VertexDeclHandle decl;
  330. };
  331. struct InstanceDataBuffer
  332. {
  333. uint8_t* data;
  334. uint32_t size;
  335. uint32_t offset;
  336. uint16_t stride;
  337. uint16_t num;
  338. VertexBufferHandle handle;
  339. };
  340. struct TextureInfo
  341. {
  342. TextureFormat::Enum format;
  343. uint16_t width;
  344. uint16_t height;
  345. uint16_t depth;
  346. };
  347. struct VertexDecl
  348. {
  349. /// Start VertexDecl.
  350. void begin(RendererType::Enum _renderer = RendererType::Null);
  351. /// End VertexDecl.
  352. void end();
  353. /// Add attribute to VertexDecl. Note: Must be called between begin/end.
  354. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false);
  355. /// Decode attribute.
  356. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized) const;
  357. /// Returns true if VertexDecl contains attribute.
  358. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  359. /// Returns relative attribute offset from the vertex.
  360. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  361. /// Returns vertex stride.
  362. uint16_t getStride() const { return m_stride; }
  363. /// Returns size of vertex buffer for number of vertices.
  364. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  365. uint32_t m_hash;
  366. uint16_t m_stride;
  367. uint16_t m_offset[Attrib::Count];
  368. uint8_t m_attributes[Attrib::Count];
  369. };
  370. /// Returns renderer backend API type.
  371. RendererType::Enum getRendererType();
  372. ///
  373. void init(FatalFn _fatal = NULL, ReallocFn _realloc = NULL, FreeFn _free = NULL, CacheFn _cache = NULL);
  374. ///
  375. void shutdown();
  376. ///
  377. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  378. /// Advance to next frame. When using multithreaded renderer, this call
  379. /// just swaps internal buffers, kicks render thread, and returns. In
  380. /// singlethreaded renderer this call does frame rendering.
  381. void frame();
  382. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  383. const Memory* alloc(uint32_t _size);
  384. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  385. /// doesn't allocate memory for data. It just copies pointer to data.
  386. /// You must make sure data is available for at least 2 bgfx::frame calls.
  387. const Memory* makeRef(const void* _data, uint32_t _size);
  388. /// Set debug flags.
  389. ///
  390. /// @param _debug Available flags:
  391. ///
  392. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  393. /// all rendering calls will be skipped. It's useful when profiling
  394. /// to quickly assess bottleneck between CPU and GPU.
  395. ///
  396. /// BGFX_DEBUG_STATS - Display internal statistics.
  397. ///
  398. /// BGFX_DEBUG_TEXT - Display debug text.
  399. ///
  400. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  401. /// primitives will be rendered as lines.
  402. ///
  403. void setDebug(uint32_t _debug);
  404. /// Clear internal debug text buffer.
  405. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  406. /// Print into internal debug text buffer.
  407. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  408. ///
  409. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  410. ///
  411. void destroyIndexBuffer(IndexBufferHandle _handle);
  412. ///
  413. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  414. ///
  415. void destroyVertexBuffer(VertexBufferHandle _handle);
  416. ///
  417. DynamicIndexBufferHandle createDynamicIndexBuffer(uint16_t _num);
  418. ///
  419. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  420. ///
  421. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  422. ///
  423. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  424. ///
  425. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  426. ///
  427. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  428. ///
  429. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  430. ///
  431. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  432. /// Returns true if internal transient index buffer has enough space.
  433. bool checkAvailTransientIndexBuffer(uint16_t _num);
  434. /// Allocate transient index buffer.
  435. ///
  436. /// @param[out] _tib is valid for the duration of frame, and it can be reused for
  437. /// multiple draw calls.
  438. /// @param _num number of indices to allocate.
  439. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint16_t _num);
  440. /// Returns true if internal transient vertex buffer has enough space.
  441. bool checkAvailTransientVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  442. /// Allocate transient vertex buffer.
  443. ///
  444. /// @param[out] _tvb is valid for the duration of frame, and it can be reused for
  445. /// multiple draw calls.
  446. /// @param _num number of vertices to allocate.
  447. /// @param _decl vertex declaration.
  448. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint16_t _num, const VertexDecl& _decl);
  449. ///
  450. const InstanceDataBuffer* allocInstanceDataBuffer(uint16_t _num, uint16_t _stride);
  451. /// Create vertex shader from memory buffer.
  452. VertexShaderHandle createVertexShader(const Memory* _mem);
  453. /// Destroy vertex shader. Once program is created with vertex shader
  454. /// it is safe to destroy vertex shader.
  455. void destroyVertexShader(VertexShaderHandle _handle);
  456. /// Create fragment shader from memory buffer.
  457. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  458. /// Destroy fragment shader. Once program is created with fragment shader
  459. /// it is safe to destroy fragment shader.
  460. void destroyFragmentShader(FragmentShaderHandle _handle);
  461. /// Create program with vertex and fragment shaders.
  462. ///
  463. /// @param _vsh vertex shader.
  464. /// @param _fsh fragment shader.
  465. /// @returns Program handle if vertex shader output and fragment shader
  466. /// input are matching, otherwise returns invalid program handle.
  467. ProgramHandle createProgram(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  468. /// Destroy program.
  469. void destroyProgram(ProgramHandle _handle);
  470. /// Create texture from memory buffer.
  471. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, TextureInfo* _info = NULL);
  472. ///
  473. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  474. ///
  475. TextureHandle createTexture3D(uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  476. ///
  477. TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  478. ///
  479. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  480. ///
  481. void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem);
  482. ///
  483. void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  484. ///
  485. void destroyTexture(TextureHandle _handle);
  486. ///
  487. RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags = BGFX_RENDER_TARGET_COLOR_RGBA, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  488. ///
  489. void destroyRenderTarget(RenderTargetHandle _handle);
  490. ///
  491. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  492. ///
  493. void destroyUniform(UniformHandle _handle);
  494. /// Set view rectangle. Draw primitive outside view will be clipped.
  495. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  496. ///
  497. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  498. /// Set view clear flags.
  499. ///
  500. /// @param _id view id.
  501. /// @param _flags clear flags.
  502. /// See: BGFX_CLEAR_*
  503. ///
  504. /// @param _rgba color clear value.
  505. /// @param _depth depth clear value.
  506. /// @param _stencil stencil clear value.
  507. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  508. ///
  509. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  510. /// Set view into sequential mode. Draw calls will be sorted in the same
  511. /// order in which submit calls were called.
  512. void setViewSeq(uint8_t _id, bool _enabled);
  513. ///
  514. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  515. /// Set view render target. View without render target draws primitives
  516. /// into default backbuffer.
  517. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  518. ///
  519. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  520. /// Set view view and projection matrices, all draw primitives in this
  521. /// view will use these matrices.
  522. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  523. ///
  524. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  525. /// Set render states for draw primitive.
  526. /// BGFX_STATE_*
  527. void setState(uint64_t _state);
  528. /// Set stencil test state.
  529. ///
  530. /// @param _fstencil Front stencil state.
  531. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  532. /// _fstencil is applied to both front and back facing primitives.
  533. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  534. /// Set model matrix for draw primitive. If it is not called model will
  535. /// be rendered with identity model matrix.
  536. ///
  537. /// @param _mtx pointer to first matrix in array.
  538. /// @param _num number of matrices in array.
  539. /// @returns index into matrix cache in case the same model matrix has to
  540. /// be used for other draw primitive call.
  541. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  542. /// Set model matrix from matrix cache for draw primitive.
  543. ///
  544. /// @param _cache index in matrix cache.
  545. /// @param _num number of matrices from cache.
  546. void setTransform(uint32_t _cache, uint16_t _num = 1);
  547. ///
  548. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  549. ///
  550. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);
  551. ///
  552. void setIndexBuffer(IndexBufferHandle _handle);
  553. ///
  554. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);
  555. ///
  556. void setIndexBuffer(DynamicIndexBufferHandle _handle);
  557. ///
  558. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  559. ///
  560. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  561. ///
  562. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  563. ///
  564. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  565. ///
  566. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  567. ///
  568. void setProgram(ProgramHandle _handle);
  569. ///
  570. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  571. ///
  572. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  573. /// Submit primitive for rendering into single view.
  574. ///
  575. /// @param _id View id.
  576. /// @param _depth depth for sorting.
  577. void submit(uint8_t _id, int32_t _depth = 0);
  578. /// Submit primitive for rendering into multiple views.
  579. ///
  580. /// @param _viewMask mask to which views to submit draw primitive calls.
  581. /// @param _depth depth for sorting.
  582. void submitMask(uint32_t _viewMask, int32_t _depth = 0);
  583. ///
  584. void saveScreenShot(const char* _filePath);
  585. } // namespace bgfx
  586. #endif // __BGFX_H__