bgfx.h 29 KB

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