bgfx.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright 2011-2013 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. #define BGFX_STATE_BLEND_FUNC(_src, _dst) ( uint64_t(_src)|( uint64_t(_dst)<<4) )
  76. ///
  77. #define BGFX_STENCIL_FUNC_REF_SHIFT 0
  78. #define BGFX_STENCIL_FUNC_REF_MASK UINT32_C(0x000000ff)
  79. #define BGFX_STENCIL_FUNC_RMASK_SHIFT 8
  80. #define BGFX_STENCIL_FUNC_RMASK_MASK UINT32_C(0x0000ff00)
  81. #define BGFX_STENCIL_TEST_LESS UINT32_C(0x00010000)
  82. #define BGFX_STENCIL_TEST_LEQUAL UINT32_C(0x00020000)
  83. #define BGFX_STENCIL_TEST_EQUAL UINT32_C(0x00030000)
  84. #define BGFX_STENCIL_TEST_GEQUAL UINT32_C(0x00040000)
  85. #define BGFX_STENCIL_TEST_GREATER UINT32_C(0x00050000)
  86. #define BGFX_STENCIL_TEST_NOTEQUAL UINT32_C(0x00060000)
  87. #define BGFX_STENCIL_TEST_NEVER UINT32_C(0x00070000)
  88. #define BGFX_STENCIL_TEST_ALWAYS UINT32_C(0x00080000)
  89. #define BGFX_STENCIL_TEST_SHIFT 16
  90. #define BGFX_STENCIL_TEST_MASK UINT32_C(0x000f0000)
  91. #define BGFX_STENCIL_OP_FAIL_S_ZERO UINT32_C(0x00000000)
  92. #define BGFX_STENCIL_OP_FAIL_S_KEEP UINT32_C(0x00100000)
  93. #define BGFX_STENCIL_OP_FAIL_S_REPLACE UINT32_C(0x00200000)
  94. #define BGFX_STENCIL_OP_FAIL_S_INCR UINT32_C(0x00300000)
  95. #define BGFX_STENCIL_OP_FAIL_S_INCRSAT UINT32_C(0x00400000)
  96. #define BGFX_STENCIL_OP_FAIL_S_DECR UINT32_C(0x00500000)
  97. #define BGFX_STENCIL_OP_FAIL_S_DECRSAT UINT32_C(0x00600000)
  98. #define BGFX_STENCIL_OP_FAIL_S_INVERT UINT32_C(0x00700000)
  99. #define BGFX_STENCIL_OP_FAIL_S_SHIFT 20
  100. #define BGFX_STENCIL_OP_FAIL_S_MASK UINT32_C(0x00f00000)
  101. #define BGFX_STENCIL_OP_FAIL_Z_ZERO UINT32_C(0x00000000)
  102. #define BGFX_STENCIL_OP_FAIL_Z_KEEP UINT32_C(0x01000000)
  103. #define BGFX_STENCIL_OP_FAIL_Z_REPLACE UINT32_C(0x02000000)
  104. #define BGFX_STENCIL_OP_FAIL_Z_INCR UINT32_C(0x03000000)
  105. #define BGFX_STENCIL_OP_FAIL_Z_INCRSAT UINT32_C(0x04000000)
  106. #define BGFX_STENCIL_OP_FAIL_Z_DECR UINT32_C(0x05000000)
  107. #define BGFX_STENCIL_OP_FAIL_Z_DECRSAT UINT32_C(0x06000000)
  108. #define BGFX_STENCIL_OP_FAIL_Z_INVERT UINT32_C(0x07000000)
  109. #define BGFX_STENCIL_OP_FAIL_Z_SHIFT 24
  110. #define BGFX_STENCIL_OP_FAIL_Z_MASK UINT32_C(0x0f000000)
  111. #define BGFX_STENCIL_OP_PASS_Z_ZERO UINT32_C(0x00000000)
  112. #define BGFX_STENCIL_OP_PASS_Z_KEEP UINT32_C(0x10000000)
  113. #define BGFX_STENCIL_OP_PASS_Z_REPLACE UINT32_C(0x20000000)
  114. #define BGFX_STENCIL_OP_PASS_Z_INCR UINT32_C(0x30000000)
  115. #define BGFX_STENCIL_OP_PASS_Z_INCRSAT UINT32_C(0x40000000)
  116. #define BGFX_STENCIL_OP_PASS_Z_DECR UINT32_C(0x50000000)
  117. #define BGFX_STENCIL_OP_PASS_Z_DECRSAT UINT32_C(0x60000000)
  118. #define BGFX_STENCIL_OP_PASS_Z_INVERT UINT32_C(0x70000000)
  119. #define BGFX_STENCIL_OP_PASS_Z_SHIFT 28
  120. #define BGFX_STENCIL_OP_PASS_Z_MASK UINT32_C(0xf0000000)
  121. #define BGFX_STENCIL_NONE UINT32_C(0x00000000)
  122. #define BGFX_STENCIL_MASK UINT32_C(0xffffffff)
  123. #define BGFX_STENCIL_DEFAULT UINT32_C(0x00000000)
  124. #define BGFX_STENCIL_FUNC_REF(_ref) ( (uint32_t(_ref)<<BGFX_STENCIL_FUNC_REF_SHIFT)&BGFX_STENCIL_FUNC_REF_MASK)
  125. #define BGFX_STENCIL_FUNC_RMASK(_mask) ( (uint32_t(_mask)<<BGFX_STENCIL_FUNC_RMASK_SHIFT)&BGFX_STENCIL_FUNC_RMASK_MASK)
  126. ///
  127. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  128. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  129. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  130. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  131. ///
  132. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  133. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  134. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  135. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  136. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  137. ///
  138. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  139. #define BGFX_TEXTURE_U_REPEAT UINT32_C(0x00000000)
  140. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  141. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  142. #define BGFX_TEXTURE_U_SHIFT 0
  143. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  144. #define BGFX_TEXTURE_V_REPEAT UINT32_C(0x00000000)
  145. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000010)
  146. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  147. #define BGFX_TEXTURE_V_SHIFT 4
  148. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  149. #define BGFX_TEXTURE_W_REPEAT UINT32_C(0x00000000)
  150. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000100)
  151. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000200)
  152. #define BGFX_TEXTURE_W_SHIFT 8
  153. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000300)
  154. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00001000)
  155. #define BGFX_TEXTURE_MIN_ANISOTROPIC UINT32_C(0x00002000)
  156. #define BGFX_TEXTURE_MIN_SHIFT 12
  157. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00003000)
  158. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00010000)
  159. #define BGFX_TEXTURE_MAG_ANISOTROPIC UINT32_C(0x00020000)
  160. #define BGFX_TEXTURE_MAG_SHIFT 16
  161. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00030000)
  162. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00100000)
  163. #define BGFX_TEXTURE_MIP_SHIFT 20
  164. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00100000)
  165. #define BGFX_TEXTURE_SRGB UINT32_C(0x00200000)
  166. ///
  167. #define BGFX_RENDER_TARGET_NONE UINT32_C(0x00000000)
  168. #define BGFX_RENDER_TARGET_COLOR_RGBA UINT32_C(0x00000001)
  169. #define BGFX_RENDER_TARGET_COLOR_R32F UINT32_C(0x00000002)
  170. #define BGFX_RENDER_TARGET_COLOR_SHIFT 0
  171. #define BGFX_RENDER_TARGET_COLOR_MASK UINT32_C(0x000000ff)
  172. #define BGFX_RENDER_TARGET_DEPTH UINT32_C(0x00000100)
  173. #define BGFX_RENDER_TARGET_DEPTH_SHIFT 8
  174. #define BGFX_RENDER_TARGET_DEPTH_MASK UINT32_C(0x0000ff00)
  175. #define BGFX_RENDER_TARGET_MSAA_X2 UINT32_C(0x00010000)
  176. #define BGFX_RENDER_TARGET_MSAA_X4 UINT32_C(0x00020000)
  177. #define BGFX_RENDER_TARGET_MSAA_X8 UINT32_C(0x00030000)
  178. #define BGFX_RENDER_TARGET_MSAA_X16 UINT32_C(0x00040000)
  179. #define BGFX_RENDER_TARGET_MSAA_SHIFT 16
  180. #define BGFX_RENDER_TARGET_MSAA_MASK UINT32_C(0x00070000)
  181. #define BGFX_RENDER_TARGET_SRGBWRITE UINT32_C(0x00080000)
  182. ///
  183. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  184. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  185. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  186. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000001)
  187. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  188. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  189. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  190. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  191. #define BGFX_RESET_MSAA_SHIFT 4
  192. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  193. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  194. #define BGFX_RESET_CAPTURE UINT32_C(0x00000100)
  195. ///
  196. #define BGFX_HANDLE(_name) struct _name { uint16_t idx; }
  197. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  198. /// BGFX
  199. namespace bgfx
  200. {
  201. struct Fatal
  202. {
  203. enum Enum
  204. {
  205. MinimumRequiredSpecs = 1,
  206. InvalidShader,
  207. UnableToInitialize,
  208. UnableToCreateRenderTarget,
  209. UnableToCreateTexture,
  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. BC1, // DXT1
  263. BC2, // DXT3
  264. BC3, // DXT5
  265. BC4, // LATC1/ATI1
  266. BC5, // LATC2/ATI2
  267. Unknown,
  268. L8,
  269. BGRX8,
  270. BGRA8,
  271. RGBA16,
  272. RGBA16F,
  273. R5G6B5,
  274. RGBA4,
  275. RGB5A1,
  276. RGB10A2,
  277. Count
  278. };
  279. };
  280. struct UniformType
  281. {
  282. enum Enum
  283. {
  284. Uniform1i,
  285. Uniform1f,
  286. End,
  287. Uniform1iv,
  288. Uniform1fv,
  289. Uniform2fv,
  290. Uniform3fv,
  291. Uniform4fv,
  292. Uniform3x3fv,
  293. Uniform4x4fv,
  294. Count
  295. };
  296. };
  297. static const uint16_t invalidHandle = UINT16_MAX;
  298. BGFX_HANDLE(DynamicIndexBufferHandle);
  299. BGFX_HANDLE(DynamicVertexBufferHandle);
  300. BGFX_HANDLE(FragmentShaderHandle);
  301. BGFX_HANDLE(IndexBufferHandle);
  302. BGFX_HANDLE(ProgramHandle);
  303. BGFX_HANDLE(RenderTargetHandle);
  304. BGFX_HANDLE(TextureHandle);
  305. BGFX_HANDLE(UniformHandle);
  306. BGFX_HANDLE(VertexBufferHandle);
  307. BGFX_HANDLE(VertexDeclHandle);
  308. BGFX_HANDLE(VertexShaderHandle);
  309. typedef void* (*ReallocFn)(void* _ptr, size_t _size);
  310. typedef void (*FreeFn)(void* _ptr);
  311. /// Callback interface to implement application specific behavior.
  312. /// Cached items are currently used only when for OpenGL binary shaders.
  313. ///
  314. /// NOTE: 'fatal' callback can be called from any thread. Other callbacks
  315. /// are called from the render thread.
  316. struct CallbackI
  317. {
  318. virtual ~CallbackI() = 0;
  319. /// Called on unrecoverable error. It's not safe to continue, inform
  320. /// user and terminate application from this call.
  321. virtual void fatal(Fatal::Enum _code, const char* _str) = 0;
  322. /// Return size of for cached item. Return 0 if no cached item was
  323. /// found.
  324. virtual uint32_t cacheReadSize(uint64_t _id) = 0;
  325. /// Read cached item.
  326. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) = 0;
  327. /// Write cached item.
  328. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) = 0;
  329. /// Screenshot captured. Screenshot format is always 4-byte BGRA.
  330. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) = 0;
  331. /// Called when capture begins.
  332. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, bgfx::TextureFormat::Enum _format, bool _yflip) = 0;
  333. /// Called when capture ends.
  334. virtual void captureEnd() = 0;
  335. /// Captured frame.
  336. virtual void captureFrame(const void* _data, uint32_t _size) = 0;
  337. };
  338. inline CallbackI::~CallbackI()
  339. {
  340. }
  341. struct Memory
  342. {
  343. uint8_t* data;
  344. uint32_t size;
  345. };
  346. struct TransientIndexBuffer
  347. {
  348. uint8_t* data;
  349. uint32_t size;
  350. IndexBufferHandle handle;
  351. uint32_t startIndex;
  352. };
  353. struct TransientVertexBuffer
  354. {
  355. uint8_t* data;
  356. uint32_t size;
  357. uint32_t startVertex;
  358. uint16_t stride;
  359. VertexBufferHandle handle;
  360. VertexDeclHandle decl;
  361. };
  362. struct InstanceDataBuffer
  363. {
  364. uint8_t* data;
  365. uint32_t size;
  366. uint32_t offset;
  367. uint16_t stride;
  368. uint16_t num;
  369. VertexBufferHandle handle;
  370. };
  371. struct TextureInfo
  372. {
  373. TextureFormat::Enum format;
  374. uint32_t storageSize;
  375. uint16_t width;
  376. uint16_t height;
  377. uint16_t depth;
  378. uint8_t numMips;
  379. uint8_t bitsPerPixel;
  380. };
  381. /// Vertex declaration.
  382. struct VertexDecl
  383. {
  384. /// Start VertexDecl.
  385. void begin(RendererType::Enum _renderer = RendererType::Null);
  386. /// End VertexDecl.
  387. void end();
  388. /// Add attribute to VertexDecl. Note: Must be called between begin/end.
  389. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  390. /// Decode attribute.
  391. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  392. /// Returns true if VertexDecl contains attribute.
  393. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  394. /// Returns relative attribute offset from the vertex.
  395. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  396. /// Returns vertex stride.
  397. uint16_t getStride() const { return m_stride; }
  398. /// Returns size of vertex buffer for number of vertices.
  399. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  400. uint32_t m_hash;
  401. uint16_t m_stride;
  402. uint16_t m_offset[Attrib::Count];
  403. uint8_t m_attributes[Attrib::Count];
  404. };
  405. /// Pack vec4 into vertex stream format.
  406. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  407. /// Unpack vec4 from vertex stream format.
  408. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  409. /// Convert from one vertex stream format to another.
  410. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1);
  411. /// Returns renderer backend API type.
  412. RendererType::Enum getRendererType();
  413. /// Initialize bgfx library.
  414. void init(CallbackI* _callback = NULL, ReallocFn _realloc = NULL, FreeFn _free = NULL);
  415. /// Shutdown bgfx library.
  416. void shutdown();
  417. /// Reset graphic settings.
  418. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  419. /// Advance to next frame. When using multithreaded renderer, this call
  420. /// just swaps internal buffers, kicks render thread, and returns. In
  421. /// singlethreaded renderer this call does frame rendering.
  422. void frame();
  423. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  424. const Memory* alloc(uint32_t _size);
  425. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  426. /// doesn't allocate memory for data. It just copies pointer to data.
  427. /// You must make sure data is available for at least 2 bgfx::frame calls.
  428. const Memory* makeRef(const void* _data, uint32_t _size);
  429. /// Set debug flags.
  430. ///
  431. /// @param _debug Available flags:
  432. ///
  433. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  434. /// all rendering calls will be skipped. It's useful when profiling
  435. /// to quickly assess bottleneck between CPU and GPU.
  436. ///
  437. /// BGFX_DEBUG_STATS - Display internal statistics.
  438. ///
  439. /// BGFX_DEBUG_TEXT - Display debug text.
  440. ///
  441. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  442. /// primitives will be rendered as lines.
  443. ///
  444. void setDebug(uint32_t _debug);
  445. /// Clear internal debug text buffer.
  446. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  447. /// Print into internal debug text buffer.
  448. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  449. /// Create static index buffer.
  450. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  451. /// Destroy static index buffer.
  452. void destroyIndexBuffer(IndexBufferHandle _handle);
  453. /// Create static vertex buffer.
  454. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  455. /// Destroy static vertex buffer.
  456. void destroyVertexBuffer(VertexBufferHandle _handle);
  457. /// Create empty dynamic index buffer.
  458. DynamicIndexBufferHandle createDynamicIndexBuffer(uint16_t _num);
  459. /// Create dynamic index buffer and initialized it.
  460. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  461. /// Update dynamic index buffer.
  462. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  463. /// Destroy dynamic index buffer.
  464. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  465. /// Create empty dynamic vertex buffer.
  466. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  467. /// Create dynamic vertex buffer and initialize it.
  468. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  469. /// Update dynamic vertex buffer.
  470. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  471. /// Destory dynamic vertex buffer.
  472. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  473. /// Returns true if internal transient index buffer has enough space.
  474. bool checkAvailTransientIndexBuffer(uint16_t _num);
  475. /// Allocate transient index buffer.
  476. ///
  477. /// @param[out] _tib is valid for the duration of frame, and it can be reused for
  478. /// multiple draw calls.
  479. /// @param _num number of indices to allocate.
  480. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint16_t _num);
  481. /// Returns true if internal transient vertex buffer has enough space.
  482. bool checkAvailTransientVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  483. /// Allocate transient vertex buffer.
  484. ///
  485. /// @param[out] _tvb is valid for the duration of frame, and it can be reused for
  486. /// multiple draw calls.
  487. /// @param _num number of vertices to allocate.
  488. /// @param _decl vertex declaration.
  489. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint16_t _num, const VertexDecl& _decl);
  490. /// Allocate instance data buffer.
  491. const InstanceDataBuffer* allocInstanceDataBuffer(uint16_t _num, uint16_t _stride);
  492. /// Create vertex shader from memory buffer.
  493. VertexShaderHandle createVertexShader(const Memory* _mem);
  494. /// Destroy vertex shader. Once program is created with vertex shader
  495. /// it is safe to destroy vertex shader.
  496. void destroyVertexShader(VertexShaderHandle _handle);
  497. /// Create fragment shader from memory buffer.
  498. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  499. /// Destroy fragment shader. Once program is created with fragment shader
  500. /// it is safe to destroy fragment shader.
  501. void destroyFragmentShader(FragmentShaderHandle _handle);
  502. /// Create program with vertex and fragment shaders.
  503. ///
  504. /// @param _vsh vertex shader.
  505. /// @param _fsh fragment shader.
  506. /// @returns Program handle if vertex shader output and fragment shader
  507. /// input are matching, otherwise returns invalid program handle.
  508. ProgramHandle createProgram(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  509. /// Destroy program.
  510. void destroyProgram(ProgramHandle _handle);
  511. /// Calculate amount of memory required for texture.
  512. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  513. /// Create texture from memory buffer.
  514. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, TextureInfo* _info = NULL);
  515. /// Create 2D texture.
  516. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  517. /// Create 3D texture.
  518. 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);
  519. /// Create Cube texture.
  520. TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  521. /// Update 2D texture.
  522. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  523. /// Update 3D texture.
  524. 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);
  525. /// Update Cube texture.
  526. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is -Y, 4 is +Z, and 5 is -Z.
  527. 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);
  528. /// Destroy texture.
  529. void destroyTexture(TextureHandle _handle);
  530. /// Create render target.
  531. 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);
  532. /// Destroy render target.
  533. void destroyRenderTarget(RenderTargetHandle _handle);
  534. /// Create shader uniform parameter.
  535. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  536. /// Destroy shader uniform parameter.
  537. void destroyUniform(UniformHandle _handle);
  538. /// Set view rectangle. Draw primitive outside view will be clipped.
  539. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  540. /// Set view rectangle for multiple views .
  541. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  542. /// Set view clear flags.
  543. ///
  544. /// @param _id view id.
  545. /// @param _flags clear flags.
  546. /// See: BGFX_CLEAR_*
  547. ///
  548. /// @param _rgba color clear value.
  549. /// @param _depth depth clear value.
  550. /// @param _stencil stencil clear value.
  551. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  552. /// Set view clear flags for multiple views.
  553. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  554. /// Set view into sequential mode. Draw calls will be sorted in the same
  555. /// order in which submit calls were called.
  556. void setViewSeq(uint8_t _id, bool _enabled);
  557. /// Set mulitple views into sequential mode.
  558. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  559. /// Set view render target. View without render target draws primitives
  560. /// into default backbuffer.
  561. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  562. /// Set view render target for multiple views.
  563. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  564. /// Set view view and projection matrices, all draw primitives in this
  565. /// view will use these matrices.
  566. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  567. /// Set view view and projection matrices for multiple views.
  568. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  569. /// Set render states for draw primitive.
  570. /// BGFX_STATE_*
  571. void setState(uint64_t _state);
  572. /// Set stencil test state.
  573. ///
  574. /// @param _fstencil Front stencil state.
  575. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  576. /// _fstencil is applied to both front and back facing primitives.
  577. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  578. /// Set model matrix for draw primitive. If it is not called model will
  579. /// be rendered with identity model matrix.
  580. ///
  581. /// @param _mtx pointer to first matrix in array.
  582. /// @param _num number of matrices in array.
  583. /// @returns index into matrix cache in case the same model matrix has to
  584. /// be used for other draw primitive call.
  585. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  586. /// Set model matrix from matrix cache for draw primitive.
  587. ///
  588. /// @param _cache index in matrix cache.
  589. /// @param _num number of matrices from cache.
  590. void setTransform(uint32_t _cache, uint16_t _num = 1);
  591. /// Set shader uniform parameter for draw primitive.
  592. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  593. /// Set index buffer for draw primitive.
  594. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  595. /// Set index buffer for draw primitive.
  596. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  597. /// Set index buffer for draw primitive.
  598. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  599. /// Set vertex buffer for draw primitive.
  600. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  601. /// Set vertex buffer for draw primitive.
  602. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  603. /// Set vertex buffer for draw primitive.
  604. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  605. /// Set instance data buffer for draw primitive.
  606. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  607. /// Set program for draw primitive.
  608. void setProgram(ProgramHandle _handle);
  609. /// Set texture stage for draw primitive.
  610. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  611. /// Set texture stage for draw primitive.
  612. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  613. /// Submit primitive for rendering into single view.
  614. ///
  615. /// @param _id View id.
  616. /// @param _depth depth for sorting.
  617. void submit(uint8_t _id, int32_t _depth = 0);
  618. /// Submit primitive for rendering into multiple views.
  619. ///
  620. /// @param _viewMask mask to which views to submit draw primitive calls.
  621. /// @param _depth depth for sorting.
  622. void submitMask(uint32_t _viewMask, int32_t _depth = 0);
  623. /// Request screen shot.
  624. void saveScreenShot(const char* _filePath);
  625. } // namespace bgfx
  626. #endif // __BGFX_H__