bgfx.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef __BGFX_H__
  6. #define __BGFX_H__
  7. #include <stdint.h> // uint32_t
  8. #include <stdlib.h> // size_t
  9. ///
  10. #define BGFX_STATE_DEPTH_WRITE UINT64_C(0x0000000000000001)
  11. #define BGFX_STATE_ALPHA_TEST UINT64_C(0x0000000000000004)
  12. #define BGFX_STATE_ALPHA_WRITE UINT64_C(0x0000000000000008)
  13. #define BGFX_STATE_ALPHA_MASK UINT64_C(0x000000000000000c)
  14. #define BGFX_STATE_DEPTH_TEST_LESS UINT64_C(0x0000000000000010)
  15. #define BGFX_STATE_DEPTH_TEST_LEQUAL UINT64_C(0x0000000000000020)
  16. #define BGFX_STATE_DEPTH_TEST_EQUAL UINT64_C(0x0000000000000030)
  17. #define BGFX_STATE_DEPTH_TEST_GEQUAL UINT64_C(0x0000000000000040)
  18. #define BGFX_STATE_DEPTH_TEST_GREATER UINT64_C(0x0000000000000050)
  19. #define BGFX_STATE_DEPTH_TEST_NOTEQUAL UINT64_C(0x0000000000000060)
  20. #define BGFX_STATE_DEPTH_TEST_NEVER UINT64_C(0x0000000000000070)
  21. #define BGFX_STATE_DEPTH_TEST_ALWAYS UINT64_C(0x0000000000000080)
  22. #define BGFX_STATE_DEPTH_TEST_SHIFT 4
  23. #define BGFX_STATE_DEPTH_TEST_MASK UINT64_C(0x00000000000000f0)
  24. #define BGFX_STATE_ALPHA_TEST_LESS UINT64_C(0x0000000000000100)
  25. #define BGFX_STATE_ALPHA_TEST_LEQUAL UINT64_C(0x0000000000000200)
  26. #define BGFX_STATE_ALPHA_TEST_EQUAL UINT64_C(0x0000000000000300)
  27. #define BGFX_STATE_ALPHA_TEST_GEQUAL UINT64_C(0x0000000000000400)
  28. #define BGFX_STATE_ALPHA_TEST_GREATER UINT64_C(0x0000000000000500)
  29. #define BGFX_STATE_ALPHA_TEST_NOTEQUAL UINT64_C(0x0000000000000600)
  30. #define BGFX_STATE_ALPHA_TEST_NEVER UINT64_C(0x0000000000000700)
  31. #define BGFX_STATE_ALPHA_TEST_ALWAYS UINT64_C(0x0000000000000800)
  32. #define BGFX_STATE_ALPHA_TEST_SHIFT 8
  33. #define BGFX_STATE_ALPHA_TEST_MASK UINT64_C(0x0000000000000f00)
  34. #define BGFX_STATE_BLEND_ZERO UINT64_C(0x0000000000001000)
  35. #define BGFX_STATE_BLEND_ONE UINT64_C(0x0000000000002000)
  36. #define BGFX_STATE_BLEND_SRC_COLOR UINT64_C(0x0000000000003000)
  37. #define BGFX_STATE_BLEND_INV_SRC_COLOR UINT64_C(0x0000000000004000)
  38. #define BGFX_STATE_BLEND_SRC_ALPHA UINT64_C(0x0000000000005000)
  39. #define BGFX_STATE_BLEND_INV_SRC_ALPHA UINT64_C(0x0000000000006000)
  40. #define BGFX_STATE_BLEND_DST_ALPHA UINT64_C(0x0000000000007000)
  41. #define BGFX_STATE_BLEND_INV_DST_ALPHA UINT64_C(0x0000000000008000)
  42. #define BGFX_STATE_BLEND_DST_COLOR UINT64_C(0x0000000000009000)
  43. #define BGFX_STATE_BLEND_INV_DST_COLOR UINT64_C(0x000000000000a000)
  44. #define BGFX_STATE_BLEND_SRC_ALPHA_SAT UINT64_C(0x000000000000b000)
  45. #define BGFX_STATE_BLEND_SHIFT 12
  46. #define BGFX_STATE_BLEND_MASK UINT64_C(0x000000000ffff000)
  47. #define BGFX_STATE_CULL_CW UINT64_C(0x0000000010000000)
  48. #define BGFX_STATE_CULL_CCW UINT64_C(0x0000000020000000)
  49. #define BGFX_STATE_CULL_SHIFT 28
  50. #define BGFX_STATE_CULL_MASK UINT64_C(0x0000000030000000)
  51. #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000040000000)
  52. #define BGFX_STATE_ALPHA_REF_SHIFT 32
  53. #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x000000ff00000000)
  54. #define BGFX_STATE_PT_LINES UINT64_C(0x0000010000000000)
  55. #define BGFX_STATE_PT_POINTS UINT64_C(0x0000020000000000)
  56. #define BGFX_STATE_PT_SHIFT 40
  57. #define BGFX_STATE_PT_MASK UINT64_C(0x0000030000000000)
  58. #define BGFX_STATE_POINT_SIZE_SHIFT 44
  59. #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x000ff00000000000)
  60. #define BGFX_STATE_SRGBWRITE UINT64_C(0x0010000000000000)
  61. #define BGFX_STATE_MSAA UINT64_C(0x0020000000000000)
  62. #define BGFX_STATE_RESERVED UINT64_C(0xff00000000000000)
  63. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  64. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  65. #define BGFX_STATE_DEFAULT (0 \
  66. | BGFX_STATE_RGB_WRITE \
  67. | BGFX_STATE_ALPHA_WRITE \
  68. | BGFX_STATE_DEPTH_TEST_LESS \
  69. | BGFX_STATE_DEPTH_WRITE \
  70. | BGFX_STATE_CULL_CW \
  71. | BGFX_STATE_MSAA \
  72. )
  73. #define BGFX_STATE_ALPHA_REF(_ref) ( (uint64_t(_ref)<<BGFX_STATE_ALPHA_REF_SHIFT)&BGFX_STATE_ALPHA_REF_MASK)
  74. #define BGFX_STATE_POINT_SIZE(_size) ( (uint64_t(_size)<<BGFX_STATE_POINT_SIZE_SHIFT)&BGFX_STATE_POINT_SIZE_MASK)
  75. #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_FAKE UINT32_C(0x00000002)
  186. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  187. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000003)
  188. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  189. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  190. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  191. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  192. #define BGFX_RESET_MSAA_SHIFT 4
  193. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  194. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  195. #define BGFX_RESET_CAPTURE UINT32_C(0x00000100)
  196. ///
  197. #define BGFX_HANDLE(_name) struct _name { uint16_t idx; }
  198. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  199. /// BGFX
  200. namespace bgfx
  201. {
  202. struct Fatal
  203. {
  204. enum Enum
  205. {
  206. MinimumRequiredSpecs = 1,
  207. InvalidShader,
  208. UnableToInitialize,
  209. UnableToCreateRenderTarget,
  210. UnableToCreateTexture,
  211. };
  212. };
  213. struct RendererType
  214. {
  215. enum Enum
  216. {
  217. Null,
  218. Direct3D9,
  219. Direct3D11,
  220. OpenGLES2,
  221. OpenGLES3,
  222. OpenGL,
  223. Count
  224. };
  225. };
  226. struct Attrib
  227. {
  228. enum Enum
  229. {
  230. Position,
  231. Normal,
  232. Tangent,
  233. Color0,
  234. Color1,
  235. Indices,
  236. Weight,
  237. TexCoord0,
  238. TexCoord1,
  239. TexCoord2,
  240. TexCoord3,
  241. TexCoord4,
  242. TexCoord5,
  243. TexCoord6,
  244. TexCoord7,
  245. Count
  246. };
  247. };
  248. struct AttribType
  249. {
  250. enum Enum
  251. {
  252. Uint8,
  253. Uint16,
  254. Half,
  255. Float,
  256. Count
  257. };
  258. };
  259. struct TextureFormat
  260. {
  261. enum Enum
  262. {
  263. Dxt1,
  264. Dxt3,
  265. Dxt5,
  266. Unknown,
  267. L8,
  268. BGRX8,
  269. BGRA8,
  270. RGBA16,
  271. R5G6B5,
  272. RGBA4,
  273. RGB5A1,
  274. RGB10A2,
  275. Count
  276. };
  277. };
  278. struct UniformType
  279. {
  280. enum Enum
  281. {
  282. Uniform1i,
  283. Uniform1f,
  284. End,
  285. Uniform1iv,
  286. Uniform1fv,
  287. Uniform2fv,
  288. Uniform3fv,
  289. Uniform4fv,
  290. Uniform3x3fv,
  291. Uniform4x4fv,
  292. Count
  293. };
  294. };
  295. static const uint16_t invalidHandle = UINT16_MAX;
  296. BGFX_HANDLE(DynamicIndexBufferHandle);
  297. BGFX_HANDLE(DynamicVertexBufferHandle);
  298. BGFX_HANDLE(FragmentShaderHandle);
  299. BGFX_HANDLE(IndexBufferHandle);
  300. BGFX_HANDLE(ProgramHandle);
  301. BGFX_HANDLE(RenderTargetHandle);
  302. BGFX_HANDLE(TextureHandle);
  303. BGFX_HANDLE(UniformHandle);
  304. BGFX_HANDLE(VertexBufferHandle);
  305. BGFX_HANDLE(VertexDeclHandle);
  306. BGFX_HANDLE(VertexShaderHandle);
  307. typedef void* (*ReallocFn)(void* _ptr, size_t _size);
  308. typedef void (*FreeFn)(void* _ptr);
  309. /// Callback interface to implement application specific behavior.
  310. /// Cached items are currently used only when for OpenGL binary shaders.
  311. ///
  312. /// NOTE: '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. Note: Must be called between begin/end.
  387. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  388. /// Decode attribute.
  389. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  390. /// Returns true if VertexDecl contains attribute.
  391. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  392. /// Returns relative attribute offset from the vertex.
  393. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  394. /// Returns vertex stride.
  395. uint16_t getStride() const { return m_stride; }
  396. /// Returns size of vertex buffer for number of vertices.
  397. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  398. uint32_t m_hash;
  399. uint16_t m_stride;
  400. uint16_t m_offset[Attrib::Count];
  401. uint8_t m_attributes[Attrib::Count];
  402. };
  403. /// Pack vec4 into vertex stream format.
  404. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  405. /// Unpack vec4 from vertex stream format.
  406. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  407. /// Returns renderer backend API type.
  408. RendererType::Enum getRendererType();
  409. /// Initialize bgfx library.
  410. void init(CallbackI* _callback = NULL, ReallocFn _realloc = NULL, FreeFn _free = NULL);
  411. /// Shutdown bgfx library.
  412. void shutdown();
  413. /// Reset graphic settings.
  414. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  415. /// Advance to next frame. When using multithreaded renderer, this call
  416. /// just swaps internal buffers, kicks render thread, and returns. In
  417. /// singlethreaded renderer this call does frame rendering.
  418. void frame();
  419. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  420. const Memory* alloc(uint32_t _size);
  421. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  422. /// doesn't allocate memory for data. It just copies pointer to data.
  423. /// You must make sure data is available for at least 2 bgfx::frame calls.
  424. const Memory* makeRef(const void* _data, uint32_t _size);
  425. /// Set debug flags.
  426. ///
  427. /// @param _debug Available flags:
  428. ///
  429. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  430. /// all rendering calls will be skipped. It's useful when profiling
  431. /// to quickly assess bottleneck between CPU and GPU.
  432. ///
  433. /// BGFX_DEBUG_STATS - Display internal statistics.
  434. ///
  435. /// BGFX_DEBUG_TEXT - Display debug text.
  436. ///
  437. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  438. /// primitives will be rendered as lines.
  439. ///
  440. void setDebug(uint32_t _debug);
  441. /// Clear internal debug text buffer.
  442. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  443. /// Print into internal debug text buffer.
  444. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  445. /// Create static index buffer.
  446. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  447. /// Destroy static index buffer.
  448. void destroyIndexBuffer(IndexBufferHandle _handle);
  449. /// Create static vertex buffer.
  450. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  451. /// Destroy static vertex buffer.
  452. void destroyVertexBuffer(VertexBufferHandle _handle);
  453. /// Create empty dynamic index buffer.
  454. DynamicIndexBufferHandle createDynamicIndexBuffer(uint16_t _num);
  455. /// Create dynamic index buffer and initialized it.
  456. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  457. /// Update dynamic index buffer.
  458. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  459. /// Destroy dynamic index buffer.
  460. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  461. /// Create empty dynamic vertex buffer.
  462. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  463. /// Create dynamic vertex buffer and initialize it.
  464. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  465. /// Update dynamic vertex buffer.
  466. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  467. /// Destory dynamic vertex buffer.
  468. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  469. /// Returns true if internal transient index buffer has enough space.
  470. bool checkAvailTransientIndexBuffer(uint16_t _num);
  471. /// Allocate transient index buffer.
  472. ///
  473. /// @param[out] _tib is valid for the duration of frame, and it can be reused for
  474. /// multiple draw calls.
  475. /// @param _num number of indices to allocate.
  476. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint16_t _num);
  477. /// Returns true if internal transient vertex buffer has enough space.
  478. bool checkAvailTransientVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  479. /// Allocate transient vertex buffer.
  480. ///
  481. /// @param[out] _tvb is valid for the duration of frame, and it can be reused for
  482. /// multiple draw calls.
  483. /// @param _num number of vertices to allocate.
  484. /// @param _decl vertex declaration.
  485. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint16_t _num, const VertexDecl& _decl);
  486. /// Allocate instance data buffer.
  487. const InstanceDataBuffer* allocInstanceDataBuffer(uint16_t _num, uint16_t _stride);
  488. /// Create vertex shader from memory buffer.
  489. VertexShaderHandle createVertexShader(const Memory* _mem);
  490. /// Destroy vertex shader. Once program is created with vertex shader
  491. /// it is safe to destroy vertex shader.
  492. void destroyVertexShader(VertexShaderHandle _handle);
  493. /// Create fragment shader from memory buffer.
  494. FragmentShaderHandle createFragmentShader(const Memory* _mem);
  495. /// Destroy fragment shader. Once program is created with fragment shader
  496. /// it is safe to destroy fragment shader.
  497. void destroyFragmentShader(FragmentShaderHandle _handle);
  498. /// Create program with vertex and fragment shaders.
  499. ///
  500. /// @param _vsh vertex shader.
  501. /// @param _fsh fragment shader.
  502. /// @returns Program handle if vertex shader output and fragment shader
  503. /// input are matching, otherwise returns invalid program handle.
  504. ProgramHandle createProgram(VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
  505. /// Destroy program.
  506. void destroyProgram(ProgramHandle _handle);
  507. /// Calculate amount of memory required for texture.
  508. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  509. /// Create texture from memory buffer.
  510. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, TextureInfo* _info = NULL);
  511. /// Create 2D texture.
  512. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  513. /// Create 3D texture.
  514. 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);
  515. /// Create Cube texture.
  516. TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  517. /// Update 2D texture.
  518. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
  519. /// Update 3D texture.
  520. 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);
  521. /// Update Cube texture.
  522. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is -Y, 4 is +Z, and 5 is -Z.
  523. 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);
  524. /// Destroy texture.
  525. void destroyTexture(TextureHandle _handle);
  526. /// Create render target.
  527. 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);
  528. /// Destroy render target.
  529. void destroyRenderTarget(RenderTargetHandle _handle);
  530. /// Create shader uniform parameter.
  531. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  532. /// Destroy shader uniform parameter.
  533. void destroyUniform(UniformHandle _handle);
  534. /// Set view rectangle. Draw primitive outside view will be clipped.
  535. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  536. /// Set view rectangle for multiple views .
  537. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  538. /// Set view clear flags.
  539. ///
  540. /// @param _id view id.
  541. /// @param _flags clear flags.
  542. /// See: BGFX_CLEAR_*
  543. ///
  544. /// @param _rgba color clear value.
  545. /// @param _depth depth clear value.
  546. /// @param _stencil stencil clear value.
  547. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  548. /// Set view clear flags for multiple views.
  549. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  550. /// Set view into sequential mode. Draw calls will be sorted in the same
  551. /// order in which submit calls were called.
  552. void setViewSeq(uint8_t _id, bool _enabled);
  553. /// Set mulitple views into sequential mode.
  554. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  555. /// Set view render target. View without render target draws primitives
  556. /// into default backbuffer.
  557. void setViewRenderTarget(uint8_t _id, RenderTargetHandle _handle);
  558. /// Set view render target for multiple views.
  559. void setViewRenderTargetMask(uint32_t _viewMask, RenderTargetHandle _handle);
  560. /// Set view view and projection matrices, all draw primitives in this
  561. /// view will use these matrices.
  562. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  563. /// Set view view and projection matrices for multiple views.
  564. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  565. /// Set render states for draw primitive.
  566. /// BGFX_STATE_*
  567. void setState(uint64_t _state);
  568. /// Set stencil test state.
  569. ///
  570. /// @param _fstencil Front stencil state.
  571. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  572. /// _fstencil is applied to both front and back facing primitives.
  573. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  574. /// Set model matrix for draw primitive. If it is not called model will
  575. /// be rendered with identity model matrix.
  576. ///
  577. /// @param _mtx pointer to first matrix in array.
  578. /// @param _num number of matrices in array.
  579. /// @returns index into matrix cache in case the same model matrix has to
  580. /// be used for other draw primitive call.
  581. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  582. /// Set model matrix from matrix cache for draw primitive.
  583. ///
  584. /// @param _cache index in matrix cache.
  585. /// @param _num number of matrices from cache.
  586. void setTransform(uint32_t _cache, uint16_t _num = 1);
  587. /// Set shader uniform parameter for draw primitive.
  588. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  589. /// Set index buffer for draw primitive.
  590. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  591. /// Set index buffer for draw primitive.
  592. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  593. /// Set index buffer for draw primitive.
  594. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  595. /// Set vertex buffer for draw primitive.
  596. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  597. /// Set vertex buffer for draw primitive.
  598. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  599. /// Set vertex buffer for draw primitive.
  600. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  601. /// Set instance data buffer for draw primitive.
  602. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  603. /// Set program for draw primitive.
  604. void setProgram(ProgramHandle _handle);
  605. /// Set texture stage for draw primitive.
  606. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle);
  607. /// Set texture stage for draw primitive.
  608. void setTexture(uint8_t _stage, UniformHandle _sampler, RenderTargetHandle _handle, bool _depth = false);
  609. /// Submit primitive for rendering into single view.
  610. ///
  611. /// @param _id View id.
  612. /// @param _depth depth for sorting.
  613. void submit(uint8_t _id, int32_t _depth = 0);
  614. /// Submit primitive for rendering into multiple views.
  615. ///
  616. /// @param _viewMask mask to which views to submit draw primitive calls.
  617. /// @param _depth depth for sorting.
  618. void submitMask(uint32_t _viewMask, int32_t _depth = 0);
  619. /// Request screen shot.
  620. void saveScreenShot(const char* _filePath);
  621. } // namespace bgfx
  622. #endif // __BGFX_H__