bgfx.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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_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_MIRROR UINT32_C(0x00000010)
  144. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  145. #define BGFX_TEXTURE_V_SHIFT 4
  146. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  147. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000100)
  148. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000200)
  149. #define BGFX_TEXTURE_W_SHIFT 8
  150. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000300)
  151. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00001000)
  152. #define BGFX_TEXTURE_MIN_ANISOTROPIC UINT32_C(0x00002000)
  153. #define BGFX_TEXTURE_MIN_SHIFT 12
  154. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00003000)
  155. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00010000)
  156. #define BGFX_TEXTURE_MAG_ANISOTROPIC UINT32_C(0x00020000)
  157. #define BGFX_TEXTURE_MAG_SHIFT 16
  158. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00030000)
  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_SHIFT 0
  183. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000001)
  184. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  185. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  186. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  187. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  188. #define BGFX_RESET_MSAA_SHIFT 4
  189. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  190. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  191. #define BGFX_RESET_CAPTURE UINT32_C(0x00000100)
  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. UnableToInitialize,
  205. UnableToCreateRenderTarget,
  206. UnableToCreateTexture,
  207. };
  208. };
  209. struct RendererType
  210. {
  211. enum Enum
  212. {
  213. Null,
  214. Direct3D9,
  215. Direct3D11,
  216. OpenGLES2,
  217. OpenGLES3,
  218. OpenGL,
  219. Count
  220. };
  221. };
  222. struct Attrib
  223. {
  224. enum Enum
  225. {
  226. Position,
  227. Normal,
  228. Tangent,
  229. Color0,
  230. Color1,
  231. Indices,
  232. Weight,
  233. TexCoord0,
  234. TexCoord1,
  235. TexCoord2,
  236. TexCoord3,
  237. TexCoord4,
  238. TexCoord5,
  239. TexCoord6,
  240. TexCoord7,
  241. Count
  242. };
  243. };
  244. struct AttribType
  245. {
  246. enum Enum
  247. {
  248. Uint8,
  249. Int16,
  250. Half,
  251. Float,
  252. Count
  253. };
  254. };
  255. struct TextureFormat
  256. {
  257. enum Enum
  258. {
  259. BC1, // DXT1
  260. BC2, // DXT3
  261. BC3, // DXT5
  262. BC4, // LATC1/ATI1
  263. BC5, // LATC2/ATI2
  264. Unknown,
  265. L8,
  266. BGRX8,
  267. BGRA8,
  268. RGBA16,
  269. RGBA16F,
  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* (*ReallocFn)(void* _ptr, size_t _size);
  307. typedef void (*FreeFn)(void* _ptr);
  308. /// Callback interface to implement application specific behavior.
  309. /// Cached items are currently used only when for OpenGL binary shaders.
  310. ///
  311. /// NOTE:
  312. /// 'fatal' callback can be called from any thread. Other callbacks
  313. /// are called from the render thread.
  314. struct CallbackI
  315. {
  316. virtual ~CallbackI() = 0;
  317. /// Called on unrecoverable error. It's not safe to continue, inform
  318. /// user and terminate application from this call.
  319. virtual void fatal(Fatal::Enum _code, const char* _str) = 0;
  320. /// Return size of for cached item. Return 0 if no cached item was
  321. /// found.
  322. virtual uint32_t cacheReadSize(uint64_t _id) = 0;
  323. /// Read cached item.
  324. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) = 0;
  325. /// Write cached item.
  326. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) = 0;
  327. /// Screenshot captured. Screenshot format is always 4-byte BGRA.
  328. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) = 0;
  329. /// Called when capture begins.
  330. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, bgfx::TextureFormat::Enum _format, bool _yflip) = 0;
  331. /// Called when capture ends.
  332. virtual void captureEnd() = 0;
  333. /// Captured frame.
  334. virtual void captureFrame(const void* _data, uint32_t _size) = 0;
  335. };
  336. inline CallbackI::~CallbackI()
  337. {
  338. }
  339. struct Memory
  340. {
  341. uint8_t* data;
  342. uint32_t size;
  343. };
  344. struct TransientIndexBuffer
  345. {
  346. uint8_t* data;
  347. uint32_t size;
  348. IndexBufferHandle handle;
  349. uint32_t startIndex;
  350. };
  351. struct TransientVertexBuffer
  352. {
  353. uint8_t* data;
  354. uint32_t size;
  355. uint32_t startVertex;
  356. uint16_t stride;
  357. VertexBufferHandle handle;
  358. VertexDeclHandle decl;
  359. };
  360. struct InstanceDataBuffer
  361. {
  362. uint8_t* data;
  363. uint32_t size;
  364. uint32_t offset;
  365. uint16_t stride;
  366. uint16_t num;
  367. VertexBufferHandle handle;
  368. };
  369. struct TextureInfo
  370. {
  371. TextureFormat::Enum format;
  372. uint32_t storageSize;
  373. uint16_t width;
  374. uint16_t height;
  375. uint16_t depth;
  376. uint8_t numMips;
  377. uint8_t bitsPerPixel;
  378. };
  379. /// Vertex declaration.
  380. struct VertexDecl
  381. {
  382. /// Start VertexDecl.
  383. void begin(RendererType::Enum _renderer = RendererType::Null);
  384. /// End VertexDecl.
  385. void end();
  386. /// Add attribute to VertexDecl.
  387. ///
  388. /// @param _attrib Attribute semantics.
  389. /// @param _num Number of elements 1, 2, 3 or 4.
  390. /// @param _type Element type.
  391. /// @param _normalized Value is normalized for vertex shader.
  392. /// @param _asInt Packaging rule for vertexPack, vertexUnpack, and
  393. /// vertexConvert for AttribType::Uint8 and AttribType::Int16.
  394. /// Unpacking code must be implemented inside vertex shader.
  395. ///
  396. /// NOTE:
  397. /// Must be called between begin/end.
  398. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  399. /// Decode attribute.
  400. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  401. /// Returns true if VertexDecl contains attribute.
  402. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  403. /// Returns relative attribute offset from the vertex.
  404. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  405. /// Returns vertex stride.
  406. uint16_t getStride() const { return m_stride; }
  407. /// Returns size of vertex buffer for number of vertices.
  408. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  409. uint32_t m_hash;
  410. uint16_t m_stride;
  411. uint16_t m_offset[Attrib::Count];
  412. uint8_t m_attributes[Attrib::Count];
  413. };
  414. /// Pack vec4 into vertex stream format.
  415. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  416. /// Unpack vec4 from vertex stream format.
  417. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  418. /// Convert from one vertex stream format to another.
  419. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1);
  420. /// Returns renderer backend API type.
  421. RendererType::Enum getRendererType();
  422. /// Initialize bgfx library.
  423. void init(CallbackI* _callback = NULL, ReallocFn _realloc = NULL, FreeFn _free = NULL);
  424. /// Shutdown bgfx library.
  425. void shutdown();
  426. /// Reset graphic settings.
  427. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  428. /// Advance to next frame. When using multithreaded renderer, this call
  429. /// just swaps internal buffers, kicks render thread, and returns. In
  430. /// singlethreaded renderer this call does frame rendering.
  431. void frame();
  432. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  433. const Memory* alloc(uint32_t _size);
  434. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  435. /// doesn't allocate memory for data. It just copies pointer to data.
  436. /// You must make sure data is available for at least 2 bgfx::frame calls.
  437. const Memory* makeRef(const void* _data, uint32_t _size);
  438. /// Set debug flags.
  439. ///
  440. /// @param _debug Available flags:
  441. ///
  442. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  443. /// all rendering calls will be skipped. It's useful when profiling
  444. /// to quickly assess bottleneck between CPU and GPU.
  445. ///
  446. /// BGFX_DEBUG_STATS - Display internal statistics.
  447. ///
  448. /// BGFX_DEBUG_TEXT - Display debug text.
  449. ///
  450. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  451. /// primitives will be rendered as lines.
  452. ///
  453. void setDebug(uint32_t _debug);
  454. /// Clear internal debug text buffer.
  455. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  456. /// Print into internal debug text buffer.
  457. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  458. /// Create static index buffer.
  459. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  460. /// Destroy static index buffer.
  461. void destroyIndexBuffer(IndexBufferHandle _handle);
  462. /// Create static vertex buffer.
  463. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  464. /// Destroy static vertex buffer.
  465. void destroyVertexBuffer(VertexBufferHandle _handle);
  466. /// Create empty dynamic index buffer.
  467. DynamicIndexBufferHandle createDynamicIndexBuffer(uint16_t _num);
  468. /// Create dynamic index buffer and initialized it.
  469. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  470. /// Update dynamic index buffer.
  471. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  472. /// Destroy dynamic index buffer.
  473. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  474. /// Create empty dynamic vertex buffer.
  475. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  476. /// Create dynamic vertex buffer and initialize it.
  477. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  478. /// Update dynamic vertex buffer.
  479. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  480. /// Destory dynamic vertex buffer.
  481. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  482. /// Returns true if internal transient index buffer has enough space.
  483. bool checkAvailTransientIndexBuffer(uint16_t _num);
  484. /// Allocate transient index buffer.
  485. ///
  486. /// @param[out] _tib is valid for the duration of frame, and it can be reused for
  487. /// multiple draw calls.
  488. /// @param _num number of indices to allocate.
  489. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint16_t _num);
  490. /// Returns true if internal transient vertex buffer has enough space.
  491. bool checkAvailTransientVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  492. /// Allocate transient vertex buffer.
  493. ///
  494. /// @param[out] _tvb is valid for the duration of frame, and it can be reused for
  495. /// multiple draw calls.
  496. /// @param _num number of vertices to allocate.
  497. /// @param _decl vertex declaration.
  498. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint16_t _num, const VertexDecl& _decl);
  499. /// Allocate instance data buffer.
  500. const InstanceDataBuffer* allocInstanceDataBuffer(uint16_t _num, uint16_t _stride);
  501. /// Create vertex shader from memory buffer.
  502. VertexShaderHandle createVertexShader(const Memory* _mem);
  503. /// Destroy vertex shader. Once program is created with vertex shader
  504. /// it is safe to destroy vertex shader.
  505. void destroyVertexShader(VertexShaderHandle _handle);
  506. /// Create fragment shader from memory buffer.
  507. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  508. /// Destroy fragment shader. Once program is created with fragment shader
  509. /// it is safe to destroy fragment shader.
  510. void destroyFragmentShader(FragmentShaderHandle _handle);
  511. /// Create program with vertex and fragment shaders.
  512. ///
  513. /// @param _vsh vertex shader.
  514. /// @param _fsh fragment shader.
  515. /// @returns Program handle if vertex shader output and fragment shader
  516. /// input are matching, otherwise returns invalid program handle.
  517. ProgramHandle createProgram(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  518. /// Destroy program.
  519. void destroyProgram(ProgramHandle _handle);
  520. /// Calculate amount of memory required for texture.
  521. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  522. /// Create texture from memory buffer.
  523. /// @param _mem DDS texture data.
  524. /// @param _flags Default texture sampling mode is linear, and wrap mode
  525. /// is repeat.
  526. ///
  527. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  528. /// mode.
  529. ///
  530. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  531. /// sampling.
  532. ///
  533. /// BGFX_TEXTURE_SRGB - Sample as sRGB texture.
  534. ///
  535. /// @param _info Returns parsed DDS texture information.
  536. /// @returns Texture handle.
  537. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, TextureInfo* _info = NULL);
  538. /// Create 2D texture.
  539. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  540. /// Create 3D texture.
  541. 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);
  542. /// Create Cube texture.
  543. TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  544. /// Update 2D texture.
  545. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  546. /// Update 3D texture.
  547. 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);
  548. /// Update Cube texture.
  549. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is -Y, 4 is +Z, and 5 is -Z.
  550. 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);
  551. /// Destroy texture.
  552. void destroyTexture(TextureHandle _handle);
  553. /// Create render target.
  554. 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);
  555. /// Destroy render target.
  556. void destroyRenderTarget(RenderTargetHandle _handle);
  557. /// Create shader uniform parameter.
  558. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  559. /// Destroy shader uniform parameter.
  560. void destroyUniform(UniformHandle _handle);
  561. /// Set view rectangle. Draw primitive outside view will be clipped.
  562. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  563. /// Set view rectangle for multiple views .
  564. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  565. /// Set view clear flags.
  566. ///
  567. /// @param _id view id.
  568. /// @param _flags clear flags. See: BGFX_CLEAR_*
  569. /// @param _rgba color clear value.
  570. /// @param _depth depth clear value.
  571. /// @param _stencil stencil clear value.
  572. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  573. /// Set view clear flags for multiple views.
  574. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  575. /// Set view into sequential mode. Draw calls will be sorted in the same
  576. /// order in which submit calls were called.
  577. void setViewSeq(uint8_t _id, bool _enabled);
  578. /// Set mulitple views into sequential mode.
  579. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  580. /// Set view render target. View without render target draws primitives
  581. /// into default backbuffer.
  582. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  583. /// Set view render target for multiple views.
  584. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  585. /// Set view view and projection matrices, all draw primitives in this
  586. /// view will use these matrices.
  587. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  588. /// Set view view and projection matrices for multiple views.
  589. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  590. /// Set render states for draw primitive.
  591. /// @param _state State flags. Default state for primitive type is
  592. /// triangles. See: BGFX_STATE_DEFAULT.
  593. ///
  594. /// BGFX_STATE_ALPHA_TEST - Enable alpha test.
  595. /// BGFX_STATE_ALPHA_WRITE - Enable alpha write.
  596. /// BGFX_STATE_ALPHA_TEST_* - Alpha test function.
  597. /// BGFX_STATE_DEPTH_WRITE - Enable depth write.
  598. /// BGFX_STATE_DEPTH_TEST_* - Depth test function.
  599. /// BGFX_STATE_BLEND_* - See NOTE: BGFX_STATE_BLEND_FUNC.
  600. /// BGFX_STATE_CULL_* - Backface culling mode.
  601. /// BGFX_STATE_RGB_WRITE - Enable RGB write.
  602. /// BGFX_STATE_SRGBWRITE - Enable sRGB write.
  603. /// BGFX_STATE_MSAA - Enable MSAA.
  604. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type.
  605. ///
  606. /// NOTE:
  607. /// Use BGFX_STATE_ALPHA_REF, BGFX_STATE_BLEND_FUNC and
  608. /// BGFX_STATE_BLEND_FUNC macros to setup more complex states.
  609. void setState(uint64_t _state);
  610. /// Set stencil test state.
  611. ///
  612. /// @param _fstencil Front stencil state.
  613. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  614. /// _fstencil is applied to both front and back facing primitives.
  615. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  616. /// Set model matrix for draw primitive. If it is not called model will
  617. /// be rendered with identity model matrix.
  618. ///
  619. /// @param _mtx pointer to first matrix in array.
  620. /// @param _num number of matrices in array.
  621. /// @returns index into matrix cache in case the same model matrix has to
  622. /// be used for other draw primitive call.
  623. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  624. /// Set model matrix from matrix cache for draw primitive.
  625. ///
  626. /// @param _cache index in matrix cache.
  627. /// @param _num number of matrices from cache.
  628. void setTransform(uint32_t _cache, uint16_t _num = 1);
  629. /// Set shader uniform parameter for draw primitive.
  630. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  631. /// Set index buffer for draw primitive.
  632. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  633. /// Set index buffer for draw primitive.
  634. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  635. /// Set index buffer for draw primitive.
  636. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  637. /// Set vertex buffer for draw primitive.
  638. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  639. /// Set vertex buffer for draw primitive.
  640. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  641. /// Set vertex buffer for draw primitive.
  642. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  643. /// Set instance data buffer for draw primitive.
  644. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  645. /// Set program for draw primitive.
  646. void setProgram(ProgramHandle _handle);
  647. /// Set texture stage for draw primitive.
  648. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  649. /// Set texture stage for draw primitive.
  650. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  651. /// Submit primitive for rendering into single view.
  652. ///
  653. /// @param _id View id.
  654. /// @param _depth depth for sorting.
  655. void submit(uint8_t _id, int32_t _depth = 0);
  656. /// Submit primitive for rendering into multiple views.
  657. ///
  658. /// @param _viewMask mask to which views to submit draw primitive calls.
  659. /// @param _depth depth for sorting.
  660. void submitMask(uint32_t _viewMask, int32_t _depth = 0);
  661. /// Request screen shot.
  662. void saveScreenShot(const char* _filePath);
  663. } // namespace bgfx
  664. #endif // __BGFX_H__