bgfx.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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_WRITE UINT64_C(0x0000000000000008)
  12. #define BGFX_STATE_ALPHA_MASK UINT64_C(0x000000000000000c)
  13. #define BGFX_STATE_DEPTH_TEST_LESS UINT64_C(0x0000000000000010)
  14. #define BGFX_STATE_DEPTH_TEST_LEQUAL UINT64_C(0x0000000000000020)
  15. #define BGFX_STATE_DEPTH_TEST_EQUAL UINT64_C(0x0000000000000030)
  16. #define BGFX_STATE_DEPTH_TEST_GEQUAL UINT64_C(0x0000000000000040)
  17. #define BGFX_STATE_DEPTH_TEST_GREATER UINT64_C(0x0000000000000050)
  18. #define BGFX_STATE_DEPTH_TEST_NOTEQUAL UINT64_C(0x0000000000000060)
  19. #define BGFX_STATE_DEPTH_TEST_NEVER UINT64_C(0x0000000000000070)
  20. #define BGFX_STATE_DEPTH_TEST_ALWAYS UINT64_C(0x0000000000000080)
  21. #define BGFX_STATE_DEPTH_TEST_SHIFT 4
  22. #define BGFX_STATE_DEPTH_TEST_MASK UINT64_C(0x00000000000000f0)
  23. #define BGFX_STATE_BLEND_ZERO UINT64_C(0x0000000000001000)
  24. #define BGFX_STATE_BLEND_ONE UINT64_C(0x0000000000002000)
  25. #define BGFX_STATE_BLEND_SRC_COLOR UINT64_C(0x0000000000003000)
  26. #define BGFX_STATE_BLEND_INV_SRC_COLOR UINT64_C(0x0000000000004000)
  27. #define BGFX_STATE_BLEND_SRC_ALPHA UINT64_C(0x0000000000005000)
  28. #define BGFX_STATE_BLEND_INV_SRC_ALPHA UINT64_C(0x0000000000006000)
  29. #define BGFX_STATE_BLEND_DST_ALPHA UINT64_C(0x0000000000007000)
  30. #define BGFX_STATE_BLEND_INV_DST_ALPHA UINT64_C(0x0000000000008000)
  31. #define BGFX_STATE_BLEND_DST_COLOR UINT64_C(0x0000000000009000)
  32. #define BGFX_STATE_BLEND_INV_DST_COLOR UINT64_C(0x000000000000a000)
  33. #define BGFX_STATE_BLEND_SRC_ALPHA_SAT UINT64_C(0x000000000000b000)
  34. #define BGFX_STATE_BLEND_FACTOR UINT64_C(0x000000000000c000)
  35. #define BGFX_STATE_BLEND_INV_FACTOR UINT64_C(0x000000000000d000)
  36. #define BGFX_STATE_BLEND_SHIFT 12
  37. #define BGFX_STATE_BLEND_MASK UINT64_C(0x000000000ffff000)
  38. #define BGFX_STATE_CULL_CW UINT64_C(0x0000000010000000)
  39. #define BGFX_STATE_CULL_CCW UINT64_C(0x0000000020000000)
  40. #define BGFX_STATE_CULL_SHIFT 28
  41. #define BGFX_STATE_CULL_MASK UINT64_C(0x0000000030000000)
  42. #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000040000000)
  43. #define BGFX_STATE_ALPHA_REF_SHIFT 32
  44. #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x000000ff00000000)
  45. #define BGFX_STATE_PT_LINES UINT64_C(0x0000010000000000)
  46. #define BGFX_STATE_PT_POINTS UINT64_C(0x0000020000000000)
  47. #define BGFX_STATE_PT_SHIFT 40
  48. #define BGFX_STATE_PT_MASK UINT64_C(0x0000030000000000)
  49. #define BGFX_STATE_POINT_SIZE_SHIFT 44
  50. #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x000ff00000000000)
  51. #define BGFX_STATE_SRGBWRITE UINT64_C(0x0010000000000000)
  52. #define BGFX_STATE_MSAA UINT64_C(0x0020000000000000)
  53. #define BGFX_STATE_RESERVED UINT64_C(0xff00000000000000)
  54. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  55. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  56. #define BGFX_STATE_DEFAULT (0 \
  57. | BGFX_STATE_RGB_WRITE \
  58. | BGFX_STATE_ALPHA_WRITE \
  59. | BGFX_STATE_DEPTH_TEST_LESS \
  60. | BGFX_STATE_DEPTH_WRITE \
  61. | BGFX_STATE_CULL_CW \
  62. | BGFX_STATE_MSAA \
  63. )
  64. #define BGFX_STATE_ALPHA_REF(_ref) ( (uint64_t(_ref)<<BGFX_STATE_ALPHA_REF_SHIFT)&BGFX_STATE_ALPHA_REF_MASK)
  65. #define BGFX_STATE_POINT_SIZE(_size) ( (uint64_t(_size)<<BGFX_STATE_POINT_SIZE_SHIFT)&BGFX_STATE_POINT_SIZE_MASK)
  66. #define BGFX_STATE_BLEND_FUNC(_src, _dst) ( uint64_t(_src)|( uint64_t(_dst)<<4) )
  67. ///
  68. #define BGFX_STENCIL_FUNC_REF_SHIFT 0
  69. #define BGFX_STENCIL_FUNC_REF_MASK UINT32_C(0x000000ff)
  70. #define BGFX_STENCIL_FUNC_RMASK_SHIFT 8
  71. #define BGFX_STENCIL_FUNC_RMASK_MASK UINT32_C(0x0000ff00)
  72. #define BGFX_STENCIL_TEST_LESS UINT32_C(0x00010000)
  73. #define BGFX_STENCIL_TEST_LEQUAL UINT32_C(0x00020000)
  74. #define BGFX_STENCIL_TEST_EQUAL UINT32_C(0x00030000)
  75. #define BGFX_STENCIL_TEST_GEQUAL UINT32_C(0x00040000)
  76. #define BGFX_STENCIL_TEST_GREATER UINT32_C(0x00050000)
  77. #define BGFX_STENCIL_TEST_NOTEQUAL UINT32_C(0x00060000)
  78. #define BGFX_STENCIL_TEST_NEVER UINT32_C(0x00070000)
  79. #define BGFX_STENCIL_TEST_ALWAYS UINT32_C(0x00080000)
  80. #define BGFX_STENCIL_TEST_SHIFT 16
  81. #define BGFX_STENCIL_TEST_MASK UINT32_C(0x000f0000)
  82. #define BGFX_STENCIL_OP_FAIL_S_ZERO UINT32_C(0x00000000)
  83. #define BGFX_STENCIL_OP_FAIL_S_KEEP UINT32_C(0x00100000)
  84. #define BGFX_STENCIL_OP_FAIL_S_REPLACE UINT32_C(0x00200000)
  85. #define BGFX_STENCIL_OP_FAIL_S_INCR UINT32_C(0x00300000)
  86. #define BGFX_STENCIL_OP_FAIL_S_INCRSAT UINT32_C(0x00400000)
  87. #define BGFX_STENCIL_OP_FAIL_S_DECR UINT32_C(0x00500000)
  88. #define BGFX_STENCIL_OP_FAIL_S_DECRSAT UINT32_C(0x00600000)
  89. #define BGFX_STENCIL_OP_FAIL_S_INVERT UINT32_C(0x00700000)
  90. #define BGFX_STENCIL_OP_FAIL_S_SHIFT 20
  91. #define BGFX_STENCIL_OP_FAIL_S_MASK UINT32_C(0x00f00000)
  92. #define BGFX_STENCIL_OP_FAIL_Z_ZERO UINT32_C(0x00000000)
  93. #define BGFX_STENCIL_OP_FAIL_Z_KEEP UINT32_C(0x01000000)
  94. #define BGFX_STENCIL_OP_FAIL_Z_REPLACE UINT32_C(0x02000000)
  95. #define BGFX_STENCIL_OP_FAIL_Z_INCR UINT32_C(0x03000000)
  96. #define BGFX_STENCIL_OP_FAIL_Z_INCRSAT UINT32_C(0x04000000)
  97. #define BGFX_STENCIL_OP_FAIL_Z_DECR UINT32_C(0x05000000)
  98. #define BGFX_STENCIL_OP_FAIL_Z_DECRSAT UINT32_C(0x06000000)
  99. #define BGFX_STENCIL_OP_FAIL_Z_INVERT UINT32_C(0x07000000)
  100. #define BGFX_STENCIL_OP_FAIL_Z_SHIFT 24
  101. #define BGFX_STENCIL_OP_FAIL_Z_MASK UINT32_C(0x0f000000)
  102. #define BGFX_STENCIL_OP_PASS_Z_ZERO UINT32_C(0x00000000)
  103. #define BGFX_STENCIL_OP_PASS_Z_KEEP UINT32_C(0x10000000)
  104. #define BGFX_STENCIL_OP_PASS_Z_REPLACE UINT32_C(0x20000000)
  105. #define BGFX_STENCIL_OP_PASS_Z_INCR UINT32_C(0x30000000)
  106. #define BGFX_STENCIL_OP_PASS_Z_INCRSAT UINT32_C(0x40000000)
  107. #define BGFX_STENCIL_OP_PASS_Z_DECR UINT32_C(0x50000000)
  108. #define BGFX_STENCIL_OP_PASS_Z_DECRSAT UINT32_C(0x60000000)
  109. #define BGFX_STENCIL_OP_PASS_Z_INVERT UINT32_C(0x70000000)
  110. #define BGFX_STENCIL_OP_PASS_Z_SHIFT 28
  111. #define BGFX_STENCIL_OP_PASS_Z_MASK UINT32_C(0xf0000000)
  112. #define BGFX_STENCIL_NONE UINT32_C(0x00000000)
  113. #define BGFX_STENCIL_MASK UINT32_C(0xffffffff)
  114. #define BGFX_STENCIL_DEFAULT UINT32_C(0x00000000)
  115. #define BGFX_STENCIL_FUNC_REF(_ref) ( (uint32_t(_ref)<<BGFX_STENCIL_FUNC_REF_SHIFT)&BGFX_STENCIL_FUNC_REF_MASK)
  116. #define BGFX_STENCIL_FUNC_RMASK(_mask) ( (uint32_t(_mask)<<BGFX_STENCIL_FUNC_RMASK_SHIFT)&BGFX_STENCIL_FUNC_RMASK_MASK)
  117. ///
  118. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  119. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  120. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  121. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  122. ///
  123. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  124. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  125. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  126. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  127. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  128. ///
  129. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  130. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  131. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  132. #define BGFX_TEXTURE_U_SHIFT 0
  133. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  134. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000010)
  135. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000020)
  136. #define BGFX_TEXTURE_V_SHIFT 4
  137. #define BGFX_TEXTURE_V_MASK UINT32_C(0x00000030)
  138. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000100)
  139. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000200)
  140. #define BGFX_TEXTURE_W_SHIFT 8
  141. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000300)
  142. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00001000)
  143. #define BGFX_TEXTURE_MIN_ANISOTROPIC UINT32_C(0x00002000)
  144. #define BGFX_TEXTURE_MIN_SHIFT 12
  145. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x00003000)
  146. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00010000)
  147. #define BGFX_TEXTURE_MAG_ANISOTROPIC UINT32_C(0x00020000)
  148. #define BGFX_TEXTURE_MAG_SHIFT 16
  149. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00030000)
  150. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00100000)
  151. #define BGFX_TEXTURE_MIP_SHIFT 20
  152. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00100000)
  153. #define BGFX_TEXTURE_SRGB UINT32_C(0x00200000)
  154. ///
  155. #define BGFX_RENDER_TARGET_NONE UINT32_C(0x00000000)
  156. #define BGFX_RENDER_TARGET_COLOR_RGBA8 UINT32_C(0x00000001)
  157. #define BGFX_RENDER_TARGET_COLOR_RGB10A2 UINT32_C(0x00000002)
  158. #define BGFX_RENDER_TARGET_COLOR_RGBA16 UINT32_C(0x00000003)
  159. #define BGFX_RENDER_TARGET_COLOR_RGBA16F UINT32_C(0x00000004)
  160. #define BGFX_RENDER_TARGET_COLOR_R16F UINT32_C(0x00000005)
  161. #define BGFX_RENDER_TARGET_COLOR_R32F UINT32_C(0x00000006)
  162. #define BGFX_RENDER_TARGET_COLOR_SHIFT 0
  163. #define BGFX_RENDER_TARGET_COLOR_MASK UINT32_C(0x000000ff)
  164. #define BGFX_RENDER_TARGET_DEPTH UINT32_C(0x00000100)
  165. #define BGFX_RENDER_TARGET_DEPTH_SHIFT 8
  166. #define BGFX_RENDER_TARGET_DEPTH_MASK UINT32_C(0x0000ff00)
  167. #define BGFX_RENDER_TARGET_MSAA_X2 UINT32_C(0x00010000)
  168. #define BGFX_RENDER_TARGET_MSAA_X4 UINT32_C(0x00020000)
  169. #define BGFX_RENDER_TARGET_MSAA_X8 UINT32_C(0x00030000)
  170. #define BGFX_RENDER_TARGET_MSAA_X16 UINT32_C(0x00040000)
  171. #define BGFX_RENDER_TARGET_MSAA_SHIFT 16
  172. #define BGFX_RENDER_TARGET_MSAA_MASK UINT32_C(0x00070000)
  173. #define BGFX_RENDER_TARGET_SRGBWRITE UINT32_C(0x00080000)
  174. ///
  175. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  176. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  177. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  178. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000001)
  179. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  180. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  181. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  182. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  183. #define BGFX_RESET_MSAA_SHIFT 4
  184. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  185. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  186. #define BGFX_RESET_CAPTURE UINT32_C(0x00000100)
  187. ///
  188. #define BGFX_HANDLE(_name) struct _name { uint16_t idx; }
  189. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  190. /// BGFX
  191. namespace bgfx
  192. {
  193. struct Fatal
  194. {
  195. enum Enum
  196. {
  197. DebugCheck,
  198. MinimumRequiredSpecs,
  199. InvalidShader,
  200. UnableToInitialize,
  201. UnableToCreateRenderTarget,
  202. UnableToCreateTexture,
  203. };
  204. };
  205. struct RendererType
  206. {
  207. enum Enum
  208. {
  209. Null,
  210. Direct3D9,
  211. Direct3D11,
  212. OpenGLES2,
  213. OpenGLES3,
  214. OpenGL,
  215. Count
  216. };
  217. };
  218. struct Attrib
  219. {
  220. enum Enum // corresponds to vertex shader attribute:
  221. {
  222. Position, // a_position
  223. Normal, // a_normal
  224. Tangent, // a_tangent
  225. Color0, // a_color0
  226. Color1, // a_color1
  227. Indices, // a_indices
  228. Weight, // a_weight
  229. TexCoord0, // a_texcoord0
  230. TexCoord1, // a_texcoord1
  231. TexCoord2, // a_texcoord2
  232. TexCoord3, // a_texcoord3
  233. TexCoord4, // a_texcoord4
  234. TexCoord5, // a_texcoord5
  235. TexCoord6, // a_texcoord6
  236. TexCoord7, // a_texcoord7
  237. Count
  238. };
  239. };
  240. struct AttribType
  241. {
  242. enum Enum
  243. {
  244. Uint8,
  245. Int16,
  246. Half,
  247. Float,
  248. Count
  249. };
  250. };
  251. struct TextureFormat
  252. {
  253. enum Enum
  254. {
  255. BC1, // DXT1
  256. BC2, // DXT3
  257. BC3, // DXT5
  258. BC4, // LATC1/ATI1
  259. BC5, // LATC2/ATI2
  260. Unknown,
  261. L8,
  262. BGRX8,
  263. BGRA8,
  264. RGBA16,
  265. RGBA16F,
  266. R5G6B5,
  267. RGBA4,
  268. RGB5A1,
  269. RGB10A2,
  270. Count
  271. };
  272. };
  273. struct UniformType
  274. {
  275. enum Enum
  276. {
  277. Uniform1i,
  278. Uniform1f,
  279. End,
  280. Uniform1iv,
  281. Uniform1fv,
  282. Uniform2fv,
  283. Uniform3fv,
  284. Uniform4fv,
  285. Uniform3x3fv,
  286. Uniform4x4fv,
  287. Count
  288. };
  289. };
  290. static const uint16_t invalidHandle = UINT16_MAX;
  291. BGFX_HANDLE(DynamicIndexBufferHandle);
  292. BGFX_HANDLE(DynamicVertexBufferHandle);
  293. BGFX_HANDLE(FragmentShaderHandle);
  294. BGFX_HANDLE(IndexBufferHandle);
  295. BGFX_HANDLE(ProgramHandle);
  296. BGFX_HANDLE(RenderTargetHandle);
  297. BGFX_HANDLE(TextureHandle);
  298. BGFX_HANDLE(UniformHandle);
  299. BGFX_HANDLE(VertexBufferHandle);
  300. BGFX_HANDLE(VertexDeclHandle);
  301. BGFX_HANDLE(VertexShaderHandle);
  302. typedef void* (*ReallocFn)(void* _ptr, size_t _size);
  303. typedef void (*FreeFn)(void* _ptr);
  304. /// Callback interface to implement application specific behavior.
  305. /// Cached items are currently used only for OpenGL binary shaders.
  306. ///
  307. /// NOTE:
  308. /// 'fatal' callback can be called from any thread. Other callbacks
  309. /// are called from the render thread.
  310. ///
  311. struct CallbackI
  312. {
  313. virtual ~CallbackI() = 0;
  314. /// If fatal code code is not Fatal::DebugCheck this callback is
  315. /// called on unrecoverable error. It's not safe to continue, inform
  316. /// user and terminate application from this call.
  317. virtual void fatal(Fatal::Enum _code, const char* _str) = 0;
  318. /// Return size of for cached item. Return 0 if no cached item was
  319. /// found.
  320. virtual uint32_t cacheReadSize(uint64_t _id) = 0;
  321. /// Read cached item.
  322. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) = 0;
  323. /// Write cached item.
  324. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) = 0;
  325. /// Screenshot captured. Screenshot format is always 4-byte BGRA.
  326. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) = 0;
  327. /// Called when capture begins.
  328. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, bgfx::TextureFormat::Enum _format, bool _yflip) = 0;
  329. /// Called when capture ends.
  330. virtual void captureEnd() = 0;
  331. /// Captured frame.
  332. virtual void captureFrame(const void* _data, uint32_t _size) = 0;
  333. };
  334. inline CallbackI::~CallbackI()
  335. {
  336. }
  337. struct Memory
  338. {
  339. uint8_t* data;
  340. uint32_t size;
  341. };
  342. struct TransientIndexBuffer
  343. {
  344. uint8_t* data;
  345. uint32_t size;
  346. IndexBufferHandle handle;
  347. uint32_t startIndex;
  348. };
  349. struct TransientVertexBuffer
  350. {
  351. uint8_t* data;
  352. uint32_t size;
  353. uint32_t startVertex;
  354. uint16_t stride;
  355. VertexBufferHandle handle;
  356. VertexDeclHandle decl;
  357. };
  358. struct InstanceDataBuffer
  359. {
  360. uint8_t* data;
  361. uint32_t size;
  362. uint32_t offset;
  363. uint16_t stride;
  364. uint16_t num;
  365. VertexBufferHandle handle;
  366. };
  367. struct TextureInfo
  368. {
  369. TextureFormat::Enum format;
  370. uint32_t storageSize;
  371. uint16_t width;
  372. uint16_t height;
  373. uint16_t depth;
  374. uint8_t numMips;
  375. uint8_t bitsPerPixel;
  376. };
  377. /// Vertex declaration.
  378. struct VertexDecl
  379. {
  380. /// Start VertexDecl.
  381. void begin(RendererType::Enum _renderer = RendererType::Null);
  382. /// End VertexDecl.
  383. void end();
  384. /// Add attribute to VertexDecl.
  385. ///
  386. /// @param _attrib Attribute semantics.
  387. /// @param _num Number of elements 1, 2, 3 or 4.
  388. /// @param _type Element type.
  389. /// @param _normalized When using fixed point AttribType (f.e. Uint8)
  390. /// value will be normalized for vertex shader usage. When normalized
  391. /// is set to true, AttribType::Uint8 value in range 0-255 will be
  392. /// in range 0.0-1.0 in vertex shader.
  393. /// @param _asInt Packaging rule for vertexPack, vertexUnpack, and
  394. /// vertexConvert for AttribType::Uint8 and AttribType::Int16.
  395. /// Unpacking code must be implemented inside vertex shader.
  396. ///
  397. /// NOTE:
  398. /// Must be called between begin/end.
  399. ///
  400. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  401. /// Decode attribute.
  402. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  403. /// Returns true if VertexDecl contains attribute.
  404. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  405. /// Returns relative attribute offset from the vertex.
  406. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  407. /// Returns vertex stride.
  408. uint16_t getStride() const { return m_stride; }
  409. /// Returns size of vertex buffer for number of vertices.
  410. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  411. uint32_t m_hash;
  412. uint16_t m_stride;
  413. uint16_t m_offset[Attrib::Count];
  414. uint8_t m_attributes[Attrib::Count];
  415. };
  416. /// Pack vec4 into vertex stream format.
  417. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  418. /// Unpack vec4 from vertex stream format.
  419. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  420. /// Convert from one vertex stream format to another.
  421. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1);
  422. /// Returns renderer backend API type.
  423. RendererType::Enum getRendererType();
  424. /// Initialize bgfx library.
  425. ///
  426. /// @param _callback Provide application specific callback interface.
  427. /// See: CallbackI
  428. ///
  429. /// @param _realloc Custom realloc function.
  430. /// @param _free Custom free function.
  431. ///
  432. /// NOTE: In order to properly set custom allocator realloc and free,
  433. /// both functions must be provided. When custom allocator is not
  434. /// specified, library uses default CRT allocator. The library assumes
  435. /// custom allocator is thread safe.
  436. ///
  437. void init(CallbackI* _callback = NULL, ReallocFn _realloc = NULL, FreeFn _free = NULL);
  438. /// Shutdown bgfx library.
  439. void shutdown();
  440. /// Reset graphic settings.
  441. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  442. /// Advance to next frame. When using multithreaded renderer, this call
  443. /// just swaps internal buffers, kicks render thread, and returns. In
  444. /// singlethreaded renderer this call does frame rendering.
  445. void frame();
  446. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  447. const Memory* alloc(uint32_t _size);
  448. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  449. /// doesn't allocate memory for data. It just copies pointer to data.
  450. /// You must make sure data is available for at least 2 bgfx::frame calls.
  451. const Memory* makeRef(const void* _data, uint32_t _size);
  452. /// Set debug flags.
  453. ///
  454. /// @param _debug Available flags:
  455. ///
  456. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  457. /// all rendering calls will be skipped. It's useful when profiling
  458. /// to quickly assess bottleneck between CPU and GPU.
  459. ///
  460. /// BGFX_DEBUG_STATS - Display internal statistics.
  461. ///
  462. /// BGFX_DEBUG_TEXT - Display debug text.
  463. ///
  464. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  465. /// primitives will be rendered as lines.
  466. ///
  467. void setDebug(uint32_t _debug);
  468. /// Clear internal debug text buffer.
  469. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  470. /// Print into internal debug text buffer.
  471. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  472. /// Create static index buffer.
  473. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  474. /// Destroy static index buffer.
  475. void destroyIndexBuffer(IndexBufferHandle _handle);
  476. /// Create static vertex buffer.
  477. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  478. /// Destroy static vertex buffer.
  479. void destroyVertexBuffer(VertexBufferHandle _handle);
  480. /// Create empty dynamic index buffer.
  481. DynamicIndexBufferHandle createDynamicIndexBuffer(uint16_t _num);
  482. /// Create dynamic index buffer and initialized it.
  483. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  484. /// Update dynamic index buffer.
  485. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  486. /// Destroy dynamic index buffer.
  487. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  488. /// Create empty dynamic vertex buffer.
  489. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  490. /// Create dynamic vertex buffer and initialize it.
  491. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  492. /// Update dynamic vertex buffer.
  493. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  494. /// Destroy dynamic vertex buffer.
  495. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  496. /// Returns true if internal transient index buffer has enough space.
  497. ///
  498. /// @param _num Number of indices.
  499. ///
  500. bool checkAvailTransientIndexBuffer(uint32_t _num);
  501. /// Returns true if internal transient vertex buffer has enough space.
  502. ///
  503. /// @param _num Number of vertices.
  504. /// @param _decl Vertex declaration.
  505. ///
  506. bool checkAvailTransientVertexBuffer(uint32_t _num, const VertexDecl& _decl);
  507. /// Returns true if both internal transient index and vertex buffer have
  508. /// enough space.
  509. ///
  510. /// @param _numVertices Number of vertices.
  511. /// @param _decl Vertex declaration.
  512. /// @param _numIndices Number of indices.
  513. ///
  514. bool checkAvailTransientBuffers(uint32_t _numVertices, const VertexDecl& _decl, uint32_t _numIndices);
  515. /// Allocate transient index buffer.
  516. ///
  517. /// @param[out] _tib is valid for the duration of frame, and it can be
  518. /// reused for multiple draw calls.
  519. /// @param _num number of indices to allocate.
  520. ///
  521. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num);
  522. /// Allocate transient vertex buffer.
  523. ///
  524. /// @param[out] _tvb is valid for the duration of frame, and it can be
  525. /// reused for multiple draw calls.
  526. /// @param _num number of vertices to allocate.
  527. /// @param _decl vertex declaration.
  528. ///
  529. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl);
  530. /// Allocate instance data buffer.
  531. const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride);
  532. /// Create vertex shader from memory buffer.
  533. VertexShaderHandle createVertexShader(const Memory* _mem);
  534. /// Destroy vertex shader. Once program is created with vertex shader
  535. /// it is safe to destroy vertex shader.
  536. void destroyVertexShader(VertexShaderHandle _handle);
  537. /// Create fragment shader from memory buffer.
  538. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  539. /// Destroy fragment shader. Once program is created with fragment shader
  540. /// it is safe to destroy fragment shader.
  541. void destroyFragmentShader(FragmentShaderHandle _handle);
  542. /// Create program with vertex and fragment shaders.
  543. ///
  544. /// @param _vsh vertex shader.
  545. /// @param _fsh fragment shader.
  546. /// @returns Program handle if vertex shader output and fragment shader
  547. /// input are matching, otherwise returns invalid program handle.
  548. ///
  549. ProgramHandle createProgram(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  550. /// Destroy program.
  551. void destroyProgram(ProgramHandle _handle);
  552. /// Calculate amount of memory required for texture.
  553. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  554. /// Create texture from memory buffer.
  555. ///
  556. /// @param _mem DDS texture data.
  557. /// @param _flags Default texture sampling mode is linear, and wrap mode
  558. /// is repeat.
  559. ///
  560. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  561. /// mode.
  562. ///
  563. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  564. /// sampling.
  565. ///
  566. /// BGFX_TEXTURE_SRGB - Sample as sRGB texture.
  567. ///
  568. /// @param _info Returns parsed DDS texture information.
  569. /// @returns Texture handle.
  570. ///
  571. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, TextureInfo* _info = NULL);
  572. /// Create 2D texture.
  573. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  574. /// Create 3D texture.
  575. 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);
  576. /// Create Cube texture.
  577. TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  578. /// Update 2D texture.
  579. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  580. /// Update 3D texture.
  581. 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);
  582. /// Update Cube texture.
  583. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is
  584. /// -Y, 4 is +Z, and 5 is -Z.
  585. 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);
  586. /// Destroy texture.
  587. void destroyTexture(TextureHandle _handle);
  588. /// Create render target.
  589. RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags = BGFX_RENDER_TARGET_COLOR_RGBA8, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  590. /// Destroy render target.
  591. void destroyRenderTarget(RenderTargetHandle _handle);
  592. /// Create shader uniform parameter.
  593. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  594. /// Destroy shader uniform parameter.
  595. void destroyUniform(UniformHandle _handle);
  596. /// Set view rectangle. Draw primitive outside view will be clipped.
  597. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  598. /// Set view rectangle for multiple views .
  599. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  600. /// Set view clear flags.
  601. ///
  602. /// @param _id view id.
  603. /// @param _flags clear flags. See: BGFX_CLEAR_*
  604. /// @param _rgba color clear value.
  605. /// @param _depth depth clear value.
  606. /// @param _stencil stencil clear value.
  607. ///
  608. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  609. /// Set view clear flags for multiple views.
  610. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  611. /// Set view into sequential mode. Draw calls will be sorted in the same
  612. /// order in which submit calls were called.
  613. void setViewSeq(uint8_t _id, bool _enabled);
  614. /// Set mulitple views into sequential mode.
  615. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  616. /// Set view render target. View without render target draws primitives
  617. /// into default backbuffer.
  618. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  619. /// Set view render target for multiple views.
  620. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  621. /// Set view view and projection matrices, all draw primitives in this
  622. /// view will use these matrices.
  623. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  624. /// Set view view and projection matrices for multiple views.
  625. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  626. /// Set render states for draw primitive.
  627. ///
  628. /// @param _state State flags. Default state for primitive type is
  629. /// triangles. See: BGFX_STATE_DEFAULT.
  630. ///
  631. /// BGFX_STATE_ALPHA_WRITE - Enable alpha write.
  632. /// BGFX_STATE_DEPTH_WRITE - Enable depth write.
  633. /// BGFX_STATE_DEPTH_TEST_* - Depth test function.
  634. /// BGFX_STATE_BLEND_* - See NOTE: BGFX_STATE_BLEND_FUNC.
  635. /// BGFX_STATE_CULL_* - Backface culling mode.
  636. /// BGFX_STATE_RGB_WRITE - Enable RGB write.
  637. /// BGFX_STATE_SRGBWRITE - Enable sRGB write.
  638. /// BGFX_STATE_MSAA - Enable MSAA.
  639. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type.
  640. ///
  641. /// @param _rgba Sets blend factor used by BGFX_STATE_BLEND_FACTOR and
  642. /// BGFX_STATE_BLEND_INV_FACTOR blend modes.
  643. ///
  644. /// NOTE:
  645. /// Use BGFX_STATE_ALPHA_REF, BGFX_STATE_POINT_SIZE and
  646. /// BGFX_STATE_BLEND_FUNC macros to setup more complex states.
  647. ///
  648. void setState(uint64_t _state, uint32_t _rgba = UINT32_MAX);
  649. /// Set stencil test state.
  650. ///
  651. /// @param _fstencil Front stencil state.
  652. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  653. /// _fstencil is applied to both front and back facing primitives.
  654. ///
  655. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  656. /// Set model matrix for draw primitive. If it is not called model will
  657. /// be rendered with identity model matrix.
  658. ///
  659. /// @param _mtx pointer to first matrix in array.
  660. /// @param _num number of matrices in array.
  661. /// @returns index into matrix cache in case the same model matrix has
  662. /// to be used for other draw primitive call.
  663. ///
  664. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  665. /// Set model matrix from matrix cache for draw primitive.
  666. ///
  667. /// @param _cache index in matrix cache.
  668. /// @param _num number of matrices from cache.
  669. ///
  670. void setTransform(uint32_t _cache, uint16_t _num = 1);
  671. /// Set shader uniform parameter for draw primitive.
  672. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  673. /// Set index buffer for draw primitive.
  674. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  675. /// Set index buffer for draw primitive.
  676. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  677. /// Set index buffer for draw primitive.
  678. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  679. /// Set vertex buffer for draw primitive.
  680. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  681. /// Set vertex buffer for draw primitive.
  682. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  683. /// Set vertex buffer for draw primitive.
  684. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  685. /// Set instance data buffer for draw primitive.
  686. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  687. /// Set program for draw primitive.
  688. void setProgram(ProgramHandle _handle);
  689. /// Set texture stage for draw primitive.
  690. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  691. /// Set texture stage for draw primitive.
  692. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  693. /// Submit primitive for rendering into single view.
  694. ///
  695. /// @param _id View id.
  696. /// @param _depth depth for sorting.
  697. ///
  698. void submit(uint8_t _id, int32_t _depth = 0);
  699. /// Submit primitive for rendering into multiple views.
  700. ///
  701. /// @param _viewMask mask to which views to submit draw primitive calls.
  702. /// @param _depth depth for sorting.
  703. ///
  704. void submitMask(uint32_t _viewMask, int32_t _depth = 0);
  705. /// Request screen shot.
  706. void saveScreenShot(const char* _filePath);
  707. } // namespace bgfx
  708. #endif // __BGFX_H__