bgfx.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BGFX_H_HEADER_GUARD
  6. #define BGFX_H_HEADER_GUARD
  7. #include <stdint.h> // uint32_t
  8. #include <stdlib.h> // size_t
  9. ///
  10. #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001)
  11. #define BGFX_STATE_ALPHA_WRITE UINT64_C(0x0000000000000002)
  12. #define BGFX_STATE_DEPTH_WRITE UINT64_C(0x0000000000000004)
  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_BLEND_EQUATION_SUB UINT64_C(0x0000000010000000)
  39. #define BGFX_STATE_BLEND_EQUATION_REVSUB UINT64_C(0x0000000020000000)
  40. #define BGFX_STATE_BLEND_EQUATION_MIN UINT64_C(0x0000000030000000)
  41. #define BGFX_STATE_BLEND_EQUATION_MAX UINT64_C(0x0000000040000000)
  42. #define BGFX_STATE_BLEND_EQUATION_SHIFT 28
  43. #define BGFX_STATE_BLEND_EQUATION_MASK UINT64_C(0x00000003f0000000)
  44. #define BGFX_STATE_BLEND_INDEPENDENT UINT64_C(0x0000000400000000)
  45. #define BGFX_STATE_CULL_CW UINT64_C(0x0000001000000000)
  46. #define BGFX_STATE_CULL_CCW UINT64_C(0x0000002000000000)
  47. #define BGFX_STATE_CULL_SHIFT 36
  48. #define BGFX_STATE_CULL_MASK UINT64_C(0x0000003000000000)
  49. #define BGFX_STATE_ALPHA_REF_SHIFT 40
  50. #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x0000ff0000000000)
  51. #define BGFX_STATE_PT_LINES UINT64_C(0x0001000000000000)
  52. #define BGFX_STATE_PT_POINTS UINT64_C(0x0002000000000000)
  53. #define BGFX_STATE_PT_SHIFT 48
  54. #define BGFX_STATE_PT_MASK UINT64_C(0x0003000000000000)
  55. #define BGFX_STATE_POINT_SIZE_SHIFT 52
  56. #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x0ff0000000000000)
  57. #define BGFX_STATE_MSAA UINT64_C(0x1000000000000000)
  58. #define BGFX_STATE_RESERVED_MASK UINT64_C(0xe000000000000000)
  59. #define BGFX_STATE_NONE UINT64_C(0x0000000000000000)
  60. #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff)
  61. #define BGFX_STATE_DEFAULT (0 \
  62. | BGFX_STATE_RGB_WRITE \
  63. | BGFX_STATE_ALPHA_WRITE \
  64. | BGFX_STATE_DEPTH_TEST_LESS \
  65. | BGFX_STATE_DEPTH_WRITE \
  66. | BGFX_STATE_CULL_CW \
  67. | BGFX_STATE_MSAA \
  68. )
  69. #define BGFX_STATE_ALPHA_REF(_ref) ( (uint64_t(_ref)<<BGFX_STATE_ALPHA_REF_SHIFT)&BGFX_STATE_ALPHA_REF_MASK)
  70. #define BGFX_STATE_POINT_SIZE(_size) ( (uint64_t(_size)<<BGFX_STATE_POINT_SIZE_SHIFT)&BGFX_STATE_POINT_SIZE_MASK)
  71. ///
  72. #define BGFX_STATE_BLEND_FUNC_SEPARATE(_srcRGB, _dstRGB, _srcA, _dstA) (0 \
  73. | ( (uint64_t(_srcRGB)|(uint64_t(_dstRGB)<<4) ) ) \
  74. | ( (uint64_t(_srcA )|(uint64_t(_dstA )<<4) )<<8) \
  75. )
  76. #define BGFX_STATE_BLEND_EQUATION_SEPARATE(_rgb, _a) (uint64_t(_rgb)|(uint64_t(_a)<<3) )
  77. ///
  78. #define BGFX_STATE_BLEND_FUNC(_src, _dst) BGFX_STATE_BLEND_FUNC_SEPARATE(_src, _dst, _src, _dst)
  79. #define BGFX_STATE_BLEND_EQUATION(_equation) BGFX_STATE_BLEND_EQUATION_SEPARATE(_equation, _equation)
  80. #define BGFX_STATE_BLEND_ADD (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE ) )
  81. #define BGFX_STATE_BLEND_ALPHA (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) )
  82. #define BGFX_STATE_BLEND_DARKEN (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE ) | BGFX_STATE_BLEND_EQUATION(BGFX_STATE_BLEND_EQUATION_MIN) )
  83. #define BGFX_STATE_BLEND_LIGHTEN (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE ) | BGFX_STATE_BLEND_EQUATION(BGFX_STATE_BLEND_EQUATION_MAX) )
  84. #define BGFX_STATE_BLEND_MULTIPLY (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_DST_COLOR, BGFX_STATE_BLEND_ZERO ) )
  85. #define BGFX_STATE_BLEND_NORMAL (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_INV_SRC_ALPHA) )
  86. #define BGFX_STATE_BLEND_SCREEN (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_INV_SRC_COLOR) )
  87. #define BGFX_STATE_BLEND_LINEAR_BURN (BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_DST_COLOR, BGFX_STATE_BLEND_INV_DST_COLOR) | BGFX_STATE_BLEND_EQUATION(BGFX_STATE_BLEND_EQUATION_SUB) )
  88. ///
  89. #define BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst) (0 \
  90. | ( uint32_t( (_src)>>BGFX_STATE_BLEND_SHIFT) \
  91. | ( uint32_t( (_dst)>>BGFX_STATE_BLEND_SHIFT)<<4) ) \
  92. )
  93. #define BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation) (0 \
  94. | BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst) \
  95. | ( uint32_t( (_equation)>>BGFX_STATE_BLEND_EQUATION_SHIFT)<<8) \
  96. )
  97. #define BGFX_STATE_BLEND_FUNC_RT_1(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<< 0)
  98. #define BGFX_STATE_BLEND_FUNC_RT_2(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<<11)
  99. #define BGFX_STATE_BLEND_FUNC_RT_3(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<<22)
  100. #define BGFX_STATE_BLEND_FUNC_RT_1E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<< 0)
  101. #define BGFX_STATE_BLEND_FUNC_RT_2E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<11)
  102. #define BGFX_STATE_BLEND_FUNC_RT_3E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<22)
  103. ///
  104. #define BGFX_STENCIL_FUNC_REF_SHIFT 0
  105. #define BGFX_STENCIL_FUNC_REF_MASK UINT32_C(0x000000ff)
  106. #define BGFX_STENCIL_FUNC_RMASK_SHIFT 8
  107. #define BGFX_STENCIL_FUNC_RMASK_MASK UINT32_C(0x0000ff00)
  108. #define BGFX_STENCIL_TEST_LESS UINT32_C(0x00010000)
  109. #define BGFX_STENCIL_TEST_LEQUAL UINT32_C(0x00020000)
  110. #define BGFX_STENCIL_TEST_EQUAL UINT32_C(0x00030000)
  111. #define BGFX_STENCIL_TEST_GEQUAL UINT32_C(0x00040000)
  112. #define BGFX_STENCIL_TEST_GREATER UINT32_C(0x00050000)
  113. #define BGFX_STENCIL_TEST_NOTEQUAL UINT32_C(0x00060000)
  114. #define BGFX_STENCIL_TEST_NEVER UINT32_C(0x00070000)
  115. #define BGFX_STENCIL_TEST_ALWAYS UINT32_C(0x00080000)
  116. #define BGFX_STENCIL_TEST_SHIFT 16
  117. #define BGFX_STENCIL_TEST_MASK UINT32_C(0x000f0000)
  118. #define BGFX_STENCIL_OP_FAIL_S_ZERO UINT32_C(0x00000000)
  119. #define BGFX_STENCIL_OP_FAIL_S_KEEP UINT32_C(0x00100000)
  120. #define BGFX_STENCIL_OP_FAIL_S_REPLACE UINT32_C(0x00200000)
  121. #define BGFX_STENCIL_OP_FAIL_S_INCR UINT32_C(0x00300000)
  122. #define BGFX_STENCIL_OP_FAIL_S_INCRSAT UINT32_C(0x00400000)
  123. #define BGFX_STENCIL_OP_FAIL_S_DECR UINT32_C(0x00500000)
  124. #define BGFX_STENCIL_OP_FAIL_S_DECRSAT UINT32_C(0x00600000)
  125. #define BGFX_STENCIL_OP_FAIL_S_INVERT UINT32_C(0x00700000)
  126. #define BGFX_STENCIL_OP_FAIL_S_SHIFT 20
  127. #define BGFX_STENCIL_OP_FAIL_S_MASK UINT32_C(0x00f00000)
  128. #define BGFX_STENCIL_OP_FAIL_Z_ZERO UINT32_C(0x00000000)
  129. #define BGFX_STENCIL_OP_FAIL_Z_KEEP UINT32_C(0x01000000)
  130. #define BGFX_STENCIL_OP_FAIL_Z_REPLACE UINT32_C(0x02000000)
  131. #define BGFX_STENCIL_OP_FAIL_Z_INCR UINT32_C(0x03000000)
  132. #define BGFX_STENCIL_OP_FAIL_Z_INCRSAT UINT32_C(0x04000000)
  133. #define BGFX_STENCIL_OP_FAIL_Z_DECR UINT32_C(0x05000000)
  134. #define BGFX_STENCIL_OP_FAIL_Z_DECRSAT UINT32_C(0x06000000)
  135. #define BGFX_STENCIL_OP_FAIL_Z_INVERT UINT32_C(0x07000000)
  136. #define BGFX_STENCIL_OP_FAIL_Z_SHIFT 24
  137. #define BGFX_STENCIL_OP_FAIL_Z_MASK UINT32_C(0x0f000000)
  138. #define BGFX_STENCIL_OP_PASS_Z_ZERO UINT32_C(0x00000000)
  139. #define BGFX_STENCIL_OP_PASS_Z_KEEP UINT32_C(0x10000000)
  140. #define BGFX_STENCIL_OP_PASS_Z_REPLACE UINT32_C(0x20000000)
  141. #define BGFX_STENCIL_OP_PASS_Z_INCR UINT32_C(0x30000000)
  142. #define BGFX_STENCIL_OP_PASS_Z_INCRSAT UINT32_C(0x40000000)
  143. #define BGFX_STENCIL_OP_PASS_Z_DECR UINT32_C(0x50000000)
  144. #define BGFX_STENCIL_OP_PASS_Z_DECRSAT UINT32_C(0x60000000)
  145. #define BGFX_STENCIL_OP_PASS_Z_INVERT UINT32_C(0x70000000)
  146. #define BGFX_STENCIL_OP_PASS_Z_SHIFT 28
  147. #define BGFX_STENCIL_OP_PASS_Z_MASK UINT32_C(0xf0000000)
  148. #define BGFX_STENCIL_NONE UINT32_C(0x00000000)
  149. #define BGFX_STENCIL_MASK UINT32_C(0xffffffff)
  150. #define BGFX_STENCIL_DEFAULT UINT32_C(0x00000000)
  151. #define BGFX_STENCIL_FUNC_REF(_ref) ( (uint32_t(_ref)<<BGFX_STENCIL_FUNC_REF_SHIFT)&BGFX_STENCIL_FUNC_REF_MASK)
  152. #define BGFX_STENCIL_FUNC_RMASK(_mask) ( (uint32_t(_mask)<<BGFX_STENCIL_FUNC_RMASK_SHIFT)&BGFX_STENCIL_FUNC_RMASK_MASK)
  153. ///
  154. #define BGFX_CLEAR_NONE UINT8_C(0x00)
  155. #define BGFX_CLEAR_COLOR_BIT UINT8_C(0x01)
  156. #define BGFX_CLEAR_DEPTH_BIT UINT8_C(0x02)
  157. #define BGFX_CLEAR_STENCIL_BIT UINT8_C(0x04)
  158. ///
  159. #define BGFX_DEBUG_NONE UINT32_C(0x00000000)
  160. #define BGFX_DEBUG_WIREFRAME UINT32_C(0x00000001)
  161. #define BGFX_DEBUG_IFH UINT32_C(0x00000002)
  162. #define BGFX_DEBUG_STATS UINT32_C(0x00000004)
  163. #define BGFX_DEBUG_TEXT UINT32_C(0x00000008)
  164. ///
  165. #define BGFX_TEXTURE_NONE UINT32_C(0x00000000)
  166. #define BGFX_TEXTURE_U_MIRROR UINT32_C(0x00000001)
  167. #define BGFX_TEXTURE_U_CLAMP UINT32_C(0x00000002)
  168. #define BGFX_TEXTURE_U_SHIFT 0
  169. #define BGFX_TEXTURE_U_MASK UINT32_C(0x00000003)
  170. #define BGFX_TEXTURE_V_MIRROR UINT32_C(0x00000004)
  171. #define BGFX_TEXTURE_V_CLAMP UINT32_C(0x00000008)
  172. #define BGFX_TEXTURE_V_SHIFT 2
  173. #define BGFX_TEXTURE_V_MASK UINT32_C(0x0000000c)
  174. #define BGFX_TEXTURE_W_MIRROR UINT32_C(0x00000010)
  175. #define BGFX_TEXTURE_W_CLAMP UINT32_C(0x00000020)
  176. #define BGFX_TEXTURE_W_SHIFT 4
  177. #define BGFX_TEXTURE_W_MASK UINT32_C(0x00000030)
  178. #define BGFX_TEXTURE_MIN_POINT UINT32_C(0x00000040)
  179. #define BGFX_TEXTURE_MIN_ANISOTROPIC UINT32_C(0x00000080)
  180. #define BGFX_TEXTURE_MIN_SHIFT 6
  181. #define BGFX_TEXTURE_MIN_MASK UINT32_C(0x000000c0)
  182. #define BGFX_TEXTURE_MAG_POINT UINT32_C(0x00000100)
  183. #define BGFX_TEXTURE_MAG_ANISOTROPIC UINT32_C(0x00000200)
  184. #define BGFX_TEXTURE_MAG_SHIFT 8
  185. #define BGFX_TEXTURE_MAG_MASK UINT32_C(0x00000300)
  186. #define BGFX_TEXTURE_MIP_POINT UINT32_C(0x00000400)
  187. #define BGFX_TEXTURE_MIP_SHIFT 10
  188. #define BGFX_TEXTURE_MIP_MASK UINT32_C(0x00000400)
  189. #define BGFX_TEXTURE_RT UINT32_C(0x00001000)
  190. #define BGFX_TEXTURE_RT_MSAA_X2 UINT32_C(0x00002000)
  191. #define BGFX_TEXTURE_RT_MSAA_X4 UINT32_C(0x00003000)
  192. #define BGFX_TEXTURE_RT_MSAA_X8 UINT32_C(0x00004000)
  193. #define BGFX_TEXTURE_RT_MSAA_X16 UINT32_C(0x00005000)
  194. #define BGFX_TEXTURE_RT_MSAA_SHIFT 12
  195. #define BGFX_TEXTURE_RT_MSAA_MASK UINT32_C(0x00007000)
  196. #define BGFX_TEXTURE_RT_BUFFER_ONLY UINT32_C(0x00008000)
  197. #define BGFX_TEXTURE_RT_MASK UINT32_C(0x0000f000)
  198. #define BGFX_TEXTURE_COMPARE_LESS UINT32_C(0x00010000)
  199. #define BGFX_TEXTURE_COMPARE_LEQUAL UINT32_C(0x00020000)
  200. #define BGFX_TEXTURE_COMPARE_EQUAL UINT32_C(0x00030000)
  201. #define BGFX_TEXTURE_COMPARE_GEQUAL UINT32_C(0x00040000)
  202. #define BGFX_TEXTURE_COMPARE_GREATER UINT32_C(0x00050000)
  203. #define BGFX_TEXTURE_COMPARE_NOTEQUAL UINT32_C(0x00060000)
  204. #define BGFX_TEXTURE_COMPARE_NEVER UINT32_C(0x00070000)
  205. #define BGFX_TEXTURE_COMPARE_ALWAYS UINT32_C(0x00080000)
  206. #define BGFX_TEXTURE_COMPARE_SHIFT 16
  207. #define BGFX_TEXTURE_COMPARE_MASK UINT32_C(0x000f0000)
  208. #define BGFX_TEXTURE_RESERVED_SHIFT 24
  209. #define BGFX_TEXTURE_RESERVED_MASK UINT32_C(0xff000000)
  210. ///
  211. #define BGFX_RESET_NONE UINT32_C(0x00000000)
  212. #define BGFX_RESET_FULLSCREEN UINT32_C(0x00000001)
  213. #define BGFX_RESET_FULLSCREEN_SHIFT 0
  214. #define BGFX_RESET_FULLSCREEN_MASK UINT32_C(0x00000001)
  215. #define BGFX_RESET_MSAA_X2 UINT32_C(0x00000010)
  216. #define BGFX_RESET_MSAA_X4 UINT32_C(0x00000020)
  217. #define BGFX_RESET_MSAA_X8 UINT32_C(0x00000030)
  218. #define BGFX_RESET_MSAA_X16 UINT32_C(0x00000040)
  219. #define BGFX_RESET_MSAA_SHIFT 4
  220. #define BGFX_RESET_MSAA_MASK UINT32_C(0x00000070)
  221. #define BGFX_RESET_VSYNC UINT32_C(0x00000080)
  222. #define BGFX_RESET_CAPTURE UINT32_C(0x00000100)
  223. ///
  224. #define BGFX_CAPS_TEXTURE_FORMAT_BC1 UINT64_C(0x0000000000000001)
  225. #define BGFX_CAPS_TEXTURE_FORMAT_BC2 UINT64_C(0x0000000000000002)
  226. #define BGFX_CAPS_TEXTURE_FORMAT_BC3 UINT64_C(0x0000000000000004)
  227. #define BGFX_CAPS_TEXTURE_FORMAT_BC4 UINT64_C(0x0000000000000008)
  228. #define BGFX_CAPS_TEXTURE_FORMAT_BC5 UINT64_C(0x0000000000000010)
  229. #define BGFX_CAPS_TEXTURE_FORMAT_ETC1 UINT64_C(0x0000000000000020)
  230. #define BGFX_CAPS_TEXTURE_FORMAT_ETC2 UINT64_C(0x0000000000000040)
  231. #define BGFX_CAPS_TEXTURE_FORMAT_ETC2A UINT64_C(0x0000000000000080)
  232. #define BGFX_CAPS_TEXTURE_FORMAT_ETC2A1 UINT64_C(0x0000000000000100)
  233. #define BGFX_CAPS_TEXTURE_FORMAT_PTC12 UINT64_C(0x0000000000000200)
  234. #define BGFX_CAPS_TEXTURE_FORMAT_PTC14 UINT64_C(0x0000000000000400)
  235. #define BGFX_CAPS_TEXTURE_FORMAT_PTC14A UINT64_C(0x0000000000000800)
  236. #define BGFX_CAPS_TEXTURE_FORMAT_PTC12A UINT64_C(0x0000000000001000)
  237. #define BGFX_CAPS_TEXTURE_FORMAT_PTC22 UINT64_C(0x0000000000002000)
  238. #define BGFX_CAPS_TEXTURE_FORMAT_PTC24 UINT64_C(0x0000000000004000)
  239. #define BGFX_CAPS_TEXTURE_FORMAT_D16 UINT64_C(0x0000000000008000)
  240. #define BGFX_CAPS_TEXTURE_FORMAT_D24 UINT64_C(0x0000000000010000)
  241. #define BGFX_CAPS_TEXTURE_FORMAT_D24S8 UINT64_C(0x0000000000020000)
  242. #define BGFX_CAPS_TEXTURE_FORMAT_D32 UINT64_C(0x0000000000040000)
  243. #define BGFX_CAPS_TEXTURE_FORMAT_D16F UINT64_C(0x0000000000080000)
  244. #define BGFX_CAPS_TEXTURE_FORMAT_D24F UINT64_C(0x0000000000100000)
  245. #define BGFX_CAPS_TEXTURE_FORMAT_D32F UINT64_C(0x0000000000200000)
  246. #define BGFX_CAPS_TEXTURE_FORMAT_D0S8 UINT64_C(0x0000000000400000)
  247. #define BGFX_CAPS_TEXTURE_COMPARE_LEQUAL UINT64_C(0x0000000001000000)
  248. #define BGFX_CAPS_TEXTURE_COMPARE_ALL UINT64_C(0x0000000003000000)
  249. #define BGFX_CAPS_TEXTURE_3D UINT64_C(0x0000000004000000)
  250. #define BGFX_CAPS_VERTEX_ATTRIB_HALF UINT64_C(0x0000000008000000)
  251. #define BGFX_CAPS_INSTANCING UINT64_C(0x0000000010000000)
  252. #define BGFX_CAPS_RENDERER_MULTITHREADED UINT64_C(0x0000000020000000)
  253. #define BGFX_CAPS_FRAGMENT_DEPTH UINT64_C(0x0000000040000000)
  254. #define BGFX_CAPS_BLEND_INDEPENDENT UINT64_C(0x0000000080000000)
  255. #define BGFX_CAPS_TEXTURE_DEPTH_MASK (0 \
  256. | BGFX_CAPS_TEXTURE_FORMAT_D16 \
  257. | BGFX_CAPS_TEXTURE_FORMAT_D24 \
  258. | BGFX_CAPS_TEXTURE_FORMAT_D24S8 \
  259. | BGFX_CAPS_TEXTURE_FORMAT_D32 \
  260. | BGFX_CAPS_TEXTURE_FORMAT_D16F \
  261. | BGFX_CAPS_TEXTURE_FORMAT_D24F \
  262. | BGFX_CAPS_TEXTURE_FORMAT_D32F \
  263. | BGFX_CAPS_TEXTURE_FORMAT_D0S8 \
  264. )
  265. ///
  266. #define BGFX_HANDLE(_name) \
  267. struct _name { uint16_t idx; }; \
  268. inline bool isValid(_name _handle) { return bgfx::invalidHandle != _handle.idx; }
  269. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  270. namespace bx { struct ReallocatorI; }
  271. /// BGFX
  272. namespace bgfx
  273. {
  274. struct Fatal
  275. {
  276. enum Enum
  277. {
  278. DebugCheck,
  279. MinimumRequiredSpecs,
  280. InvalidShader,
  281. UnableToInitialize,
  282. UnableToCreateTexture,
  283. };
  284. };
  285. struct RendererType
  286. {
  287. enum Enum
  288. {
  289. Null,
  290. Direct3D9,
  291. Direct3D11,
  292. OpenGLES,
  293. OpenGL,
  294. Count
  295. };
  296. };
  297. struct Attrib
  298. {
  299. enum Enum // corresponds to vertex shader attribute:
  300. {
  301. Position, // a_position
  302. Normal, // a_normal
  303. Tangent, // a_tangent
  304. Color0, // a_color0
  305. Color1, // a_color1
  306. Indices, // a_indices
  307. Weight, // a_weight
  308. TexCoord0, // a_texcoord0
  309. TexCoord1, // a_texcoord1
  310. TexCoord2, // a_texcoord2
  311. TexCoord3, // a_texcoord3
  312. TexCoord4, // a_texcoord4
  313. TexCoord5, // a_texcoord5
  314. TexCoord6, // a_texcoord6
  315. TexCoord7, // a_texcoord7
  316. Count
  317. };
  318. };
  319. struct AttribType
  320. {
  321. enum Enum
  322. {
  323. Uint8,
  324. Int16,
  325. Half, // Availability depends on: BGFX_CAPS_VERTEX_ATTRIB_HALF.
  326. Float,
  327. Count
  328. };
  329. };
  330. struct TextureFormat
  331. {
  332. // Availability depends on BGFX_CAPS_TEXTURE_FORMAT_*.
  333. enum Enum
  334. {
  335. BC1, // DXT1
  336. BC2, // DXT3
  337. BC3, // DXT5
  338. BC4, // LATC1/ATI1
  339. BC5, // LATC2/ATI2
  340. ETC1, // ETC1 RGB8
  341. ETC2, // ETC2 RGB8
  342. ETC2A, // ETC2 RGBA8
  343. ETC2A1, // ETC2 RGB8A1
  344. PTC12, // PVRTC1 RGB 2BPP
  345. PTC14, // PVRTC1 RGB 4BPP
  346. PTC12A, // PVRTC1 RGBA 2BPP
  347. PTC14A, // PVRTC1 RGBA 4BPP
  348. PTC22, // PVRTC2 RGBA 2BPP
  349. PTC24, // PVRTC2 RGBA 4BPP
  350. Unknown, // compressed formats above
  351. R8,
  352. R16,
  353. R16F,
  354. BGRA8,
  355. RGBA16,
  356. RGBA16F,
  357. R5G6B5,
  358. RGBA4,
  359. RGB5A1,
  360. RGB10A2,
  361. UnknownDepth, // depth formats below
  362. D16,
  363. D24,
  364. D24S8,
  365. D32,
  366. D16F,
  367. D24F,
  368. D32F,
  369. D0S8,
  370. Count
  371. };
  372. };
  373. struct UniformType
  374. {
  375. enum Enum
  376. {
  377. Uniform1i,
  378. Uniform1f,
  379. End,
  380. Uniform1iv,
  381. Uniform1fv,
  382. Uniform2fv,
  383. Uniform3fv,
  384. Uniform4fv,
  385. Uniform3x3fv,
  386. Uniform4x4fv,
  387. Count
  388. };
  389. };
  390. static const uint16_t invalidHandle = UINT16_MAX;
  391. BGFX_HANDLE(DynamicIndexBufferHandle);
  392. BGFX_HANDLE(DynamicVertexBufferHandle);
  393. BGFX_HANDLE(FrameBufferHandle);
  394. BGFX_HANDLE(IndexBufferHandle);
  395. BGFX_HANDLE(ProgramHandle);
  396. BGFX_HANDLE(ShaderHandle);
  397. BGFX_HANDLE(TextureHandle);
  398. BGFX_HANDLE(UniformHandle);
  399. BGFX_HANDLE(VertexBufferHandle);
  400. BGFX_HANDLE(VertexDeclHandle);
  401. /// Callback interface to implement application specific behavior.
  402. /// Cached items are currently used only for OpenGL binary shaders.
  403. ///
  404. /// NOTE:
  405. /// 'fatal' callback can be called from any thread. Other callbacks
  406. /// are called from the render thread.
  407. ///
  408. struct CallbackI
  409. {
  410. virtual ~CallbackI() = 0;
  411. /// If fatal code code is not Fatal::DebugCheck this callback is
  412. /// called on unrecoverable error. It's not safe to continue, inform
  413. /// user and terminate application from this call.
  414. virtual void fatal(Fatal::Enum _code, const char* _str) = 0;
  415. /// Return size of for cached item. Return 0 if no cached item was
  416. /// found.
  417. virtual uint32_t cacheReadSize(uint64_t _id) = 0;
  418. /// Read cached item.
  419. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) = 0;
  420. /// Write cached item.
  421. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) = 0;
  422. /// Screenshot captured. Screenshot format is always 4-byte BGRA.
  423. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) = 0;
  424. /// Called when capture begins.
  425. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, bgfx::TextureFormat::Enum _format, bool _yflip) = 0;
  426. /// Called when capture ends.
  427. virtual void captureEnd() = 0;
  428. /// Captured frame.
  429. virtual void captureFrame(const void* _data, uint32_t _size) = 0;
  430. };
  431. inline CallbackI::~CallbackI()
  432. {
  433. }
  434. struct Memory
  435. {
  436. uint8_t* data;
  437. uint32_t size;
  438. };
  439. /// Renderer capabilities.
  440. struct Caps
  441. {
  442. /// Renderer backend type.
  443. RendererType::Enum rendererType;
  444. /// Supported functionality, it includes emulated functionality.
  445. /// Checking supported and not emulated will give functionality
  446. /// natively supported by renderer.
  447. uint64_t supported;
  448. /// Emulated functionality. For example some texture compression
  449. /// modes are not natively supported by all renderers. The library
  450. /// internally decompresses texture into supported format.
  451. uint64_t emulated;
  452. uint16_t maxTextureSize; ///< Maximum texture size.
  453. uint16_t maxDrawCalls; ///< Maximum draw calls.
  454. uint8_t maxFBAttachments; ///< Maximum frame buffer attachments.
  455. };
  456. struct TransientIndexBuffer
  457. {
  458. uint8_t* data;
  459. uint32_t size;
  460. IndexBufferHandle handle;
  461. uint32_t startIndex;
  462. };
  463. struct TransientVertexBuffer
  464. {
  465. uint8_t* data;
  466. uint32_t size;
  467. uint32_t startVertex;
  468. uint16_t stride;
  469. VertexBufferHandle handle;
  470. VertexDeclHandle decl;
  471. };
  472. struct InstanceDataBuffer
  473. {
  474. uint8_t* data;
  475. uint32_t size;
  476. uint32_t offset;
  477. uint16_t stride;
  478. uint16_t num;
  479. VertexBufferHandle handle;
  480. };
  481. struct TextureInfo
  482. {
  483. TextureFormat::Enum format;
  484. uint32_t storageSize;
  485. uint16_t width;
  486. uint16_t height;
  487. uint16_t depth;
  488. uint8_t numMips;
  489. uint8_t bitsPerPixel;
  490. };
  491. /// Vertex declaration.
  492. struct VertexDecl
  493. {
  494. /// Start VertexDecl.
  495. void begin(RendererType::Enum _renderer = RendererType::Null);
  496. /// End VertexDecl.
  497. void end();
  498. /// Add attribute to VertexDecl.
  499. ///
  500. /// @param _attrib Attribute semantics.
  501. /// @param _num Number of elements 1, 2, 3 or 4.
  502. /// @param _type Element type.
  503. /// @param _normalized When using fixed point AttribType (f.e. Uint8)
  504. /// value will be normalized for vertex shader usage. When normalized
  505. /// is set to true, AttribType::Uint8 value in range 0-255 will be
  506. /// in range 0.0-1.0 in vertex shader.
  507. /// @param _asInt Packaging rule for vertexPack, vertexUnpack, and
  508. /// vertexConvert for AttribType::Uint8 and AttribType::Int16.
  509. /// Unpacking code must be implemented inside vertex shader.
  510. ///
  511. /// NOTE:
  512. /// Must be called between begin/end.
  513. ///
  514. void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  515. /// Skip _num bytes in vertex stream.
  516. void skip(uint8_t _num);
  517. /// Decode attribute.
  518. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  519. /// Returns true if VertexDecl contains attribute.
  520. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  521. /// Returns relative attribute offset from the vertex.
  522. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  523. /// Returns vertex stride.
  524. uint16_t getStride() const { return m_stride; }
  525. /// Returns size of vertex buffer for number of vertices.
  526. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  527. uint32_t m_hash;
  528. uint16_t m_stride;
  529. uint16_t m_offset[Attrib::Count];
  530. uint8_t m_attributes[Attrib::Count];
  531. };
  532. /// Pack vec4 into vertex stream format.
  533. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  534. /// Unpack vec4 from vertex stream format.
  535. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  536. /// Converts vertex stream data from one vertex stream format to another.
  537. ///
  538. /// @param _destDecl Destination vertex stream declaration.
  539. /// @param _destData Destination vertex stream.
  540. /// @param _srcDecl Source vertex stream declaration.
  541. /// @param _srcData Source vertex stream data.
  542. /// @param _num Number of vertices to convert from source to destination.
  543. ///
  544. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1);
  545. /// Weld vertices.
  546. ///
  547. /// @param _output Welded vertices remapping table. The size of buffer
  548. /// must be the same as number of vertices.
  549. /// @param _decl Vertex stream declaration.
  550. /// @param _data Vertex stream.
  551. /// @param _num Number of vertices in vertex stream.
  552. /// @param _epsilon Error tolerance for vertex position comparison.
  553. /// @returns Number of unique vertices after vertex welding.
  554. ///
  555. uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon = 0.001f);
  556. /// Swizzle RGBA8 image to BGRA8.
  557. ///
  558. /// @param _width Width of input image (pixels).
  559. /// @param _height Height of input image (pixels).
  560. /// @param _pitch Pitch of input image (bytes).
  561. /// @param _src Source image.
  562. /// @param _dst Destination image. Must be the same size as input image.
  563. /// _dst might be pointer to the same memory as _src.
  564. ///
  565. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  566. /// Downsample RGBA8 image with 2x2 pixel average filter.
  567. ///
  568. /// @param _width Width of input image (pixels).
  569. /// @param _height Height of input image (pixels).
  570. /// @param _pitch Pitch of input image (bytes).
  571. /// @param _src Source image.
  572. /// @param _dst Destination image. Must be at least quarter size of
  573. /// input image. _dst might be pointer to the same memory as _src.
  574. ///
  575. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  576. /// Returns renderer backend API type.
  577. RendererType::Enum getRendererType();
  578. /// Initialize bgfx library.
  579. ///
  580. /// @param _callback Provide application specific callback interface.
  581. /// See: CallbackI
  582. ///
  583. /// @param _reallocator Custom allocator. When custom allocator is not
  584. /// specified, library uses default CRT allocator. The library assumes
  585. /// custom allocator is thread safe.
  586. ///
  587. void init(CallbackI* _callback = NULL, bx::ReallocatorI* _reallocator = NULL);
  588. /// Shutdown bgfx library.
  589. void shutdown();
  590. /// Reset graphic settings.
  591. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  592. /// Advance to next frame. When using multithreaded renderer, this call
  593. /// just swaps internal buffers, kicks render thread, and returns. In
  594. /// singlethreaded renderer this call does frame rendering.
  595. ///
  596. /// @returns Current frame number. This might be used in conjunction with
  597. /// double/multi buffering data outside the library and passing it to
  598. /// library via makeRef calls.
  599. ///
  600. uint32_t frame();
  601. /// Returns renderer capabilities.
  602. const Caps* getCaps();
  603. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  604. const Memory* alloc(uint32_t _size);
  605. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  606. /// doesn't allocate memory for data. It just copies pointer to data.
  607. /// You must make sure data is available for at least 2 bgfx::frame calls.
  608. const Memory* makeRef(const void* _data, uint32_t _size);
  609. /// Set debug flags.
  610. ///
  611. /// @param _debug Available flags:
  612. ///
  613. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  614. /// all rendering calls will be skipped. It's useful when profiling
  615. /// to quickly assess bottleneck between CPU and GPU.
  616. ///
  617. /// BGFX_DEBUG_STATS - Display internal statistics.
  618. ///
  619. /// BGFX_DEBUG_TEXT - Display debug text.
  620. ///
  621. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  622. /// primitives will be rendered as lines.
  623. ///
  624. void setDebug(uint32_t _debug);
  625. /// Clear internal debug text buffer.
  626. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  627. /// Print into internal debug text buffer.
  628. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  629. /// Create static index buffer.
  630. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  631. /// Destroy static index buffer.
  632. void destroyIndexBuffer(IndexBufferHandle _handle);
  633. /// Create static vertex buffer.
  634. ///
  635. /// @param _mem Vertex buffer data.
  636. /// @param _decl Vertex declaration.
  637. /// @returns Static vertex buffer handle.
  638. ///
  639. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  640. /// Destroy static vertex buffer.
  641. ///
  642. /// @param _handle Static vertex buffer handle.
  643. ///
  644. void destroyVertexBuffer(VertexBufferHandle _handle);
  645. /// Create empty dynamic index buffer.
  646. ///
  647. /// @param _num Number of indices.
  648. ///
  649. DynamicIndexBufferHandle createDynamicIndexBuffer(uint32_t _num);
  650. /// Create dynamic index buffer and initialized it.
  651. ///
  652. /// @param _mem Index buffer data.
  653. ///
  654. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  655. /// Update dynamic index buffer.
  656. ///
  657. /// @param _handle Dynamic index buffer handle.
  658. /// @param _mem Index buffer data.
  659. ///
  660. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  661. /// Destroy dynamic index buffer.
  662. ///
  663. /// @param _handle Dynamic index buffer handle.
  664. ///
  665. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  666. /// Create empty dynamic vertex buffer.
  667. ///
  668. /// @param _num Number of vertices.
  669. /// @param _decl Vertex declaration.
  670. ///
  671. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  672. /// Create dynamic vertex buffer and initialize it.
  673. ///
  674. /// @param _mem Vertex buffer data.
  675. /// @param _decl Vertex declaration.
  676. ///
  677. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  678. /// Update dynamic vertex buffer.
  679. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  680. /// Destroy dynamic vertex buffer.
  681. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  682. /// Returns true if internal transient index buffer has enough space.
  683. ///
  684. /// @param _num Number of indices.
  685. ///
  686. bool checkAvailTransientIndexBuffer(uint32_t _num);
  687. /// Returns true if internal transient vertex buffer has enough space.
  688. ///
  689. /// @param _num Number of vertices.
  690. /// @param _decl Vertex declaration.
  691. ///
  692. bool checkAvailTransientVertexBuffer(uint32_t _num, const VertexDecl& _decl);
  693. /// Returns true if internal instance data buffer has enough space.
  694. ///
  695. /// @param _num Number of instances.
  696. /// @param _stride Stride per instance.
  697. ///
  698. bool checkAvailInstanceDataBuffer(uint32_t _num, uint16_t _stride);
  699. /// Returns true if both internal transient index and vertex buffer have
  700. /// enough space.
  701. ///
  702. /// @param _numVertices Number of vertices.
  703. /// @param _decl Vertex declaration.
  704. /// @param _numIndices Number of indices.
  705. ///
  706. bool checkAvailTransientBuffers(uint32_t _numVertices, const VertexDecl& _decl, uint32_t _numIndices);
  707. /// Allocate transient index buffer.
  708. ///
  709. /// @param[out] _tib TransientIndexBuffer structure is filled and is valid
  710. /// for the duration of frame, and it can be reused for multiple draw
  711. /// calls.
  712. /// @param _num Number of indices to allocate.
  713. ///
  714. /// NOTE:
  715. /// You must call setIndexBuffer after alloc in order to avoid memory
  716. /// leak.
  717. ///
  718. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num);
  719. /// Allocate transient vertex buffer.
  720. ///
  721. /// @param[out] _tvb TransientVertexBuffer structure is filled and is valid
  722. /// for the duration of frame, and it can be reused for multiple draw
  723. /// calls.
  724. /// @param _num Number of vertices to allocate.
  725. /// @param _decl Vertex declaration.
  726. ///
  727. /// NOTE:
  728. /// You must call setVertexBuffer after alloc in order to avoid memory
  729. /// leak.
  730. ///
  731. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl);
  732. /// Allocate instance data buffer.
  733. ///
  734. /// NOTE:
  735. /// You must call setInstanceDataBuffer after alloc in order to avoid
  736. /// memory leak.
  737. ///
  738. const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride);
  739. /// Create shader from memory buffer.
  740. ShaderHandle createShader(const Memory* _mem);
  741. /// Destroy shader. Once program is created with shader it is safe to
  742. /// destroy shader.
  743. void destroyShader(ShaderHandle _handle);
  744. /// Create program with vertex and fragment shaders.
  745. ///
  746. /// @param _vsh Vertex shader.
  747. /// @param _fsh Fragment shader.
  748. /// @param _destroyShaders If true, shaders will be destroyed when
  749. /// program is destroyed.
  750. /// @returns Program handle if vertex shader output and fragment shader
  751. /// input are matching, otherwise returns invalid program handle.
  752. ///
  753. ProgramHandle createProgram(ShaderHandle _vsh, ShaderHandle _fsh, bool _destroyShaders = false);
  754. /// Destroy program.
  755. void destroyProgram(ProgramHandle _handle);
  756. /// Calculate amount of memory required for texture.
  757. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  758. /// Create texture from memory buffer.
  759. ///
  760. /// @param _mem DDS, KTX or PVR texture data.
  761. /// @param _flags Default texture sampling mode is linear, and wrap mode
  762. /// is repeat.
  763. ///
  764. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  765. /// mode.
  766. ///
  767. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  768. /// sampling.
  769. ///
  770. /// @param _skip Skip top level mips when parsing texture.
  771. /// @param _info Returns parsed DDS texture information.
  772. /// @returns Texture handle.
  773. ///
  774. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, uint8_t _skip = 0, TextureInfo* _info = NULL);
  775. /// Create 2D texture.
  776. ///
  777. /// @param _width
  778. /// @param _height
  779. /// @param _numMips
  780. /// @param _format
  781. /// @param _flags
  782. /// @param _mem
  783. ///
  784. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  785. /// Create 3D texture.
  786. ///
  787. /// @param _width
  788. /// @param _height
  789. /// @param _depth
  790. /// @param _numMips
  791. /// @param _format
  792. /// @param _flags
  793. /// @param _mem
  794. ///
  795. 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);
  796. /// Create Cube texture.
  797. ///
  798. /// @param _size
  799. /// @param _numMips
  800. /// @param _format
  801. /// @param _flags
  802. /// @param _mem
  803. ///
  804. TextureHandle createTextureCube(uint16_t _size, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  805. /// Update 2D texture.
  806. ///
  807. /// @param _handle
  808. /// @param _mip
  809. /// @param _x
  810. /// @param _y
  811. /// @param _width
  812. /// @param _height
  813. /// @param _mem
  814. /// @param _pitch Pitch of input image (bytes). When _pitch is set to
  815. /// UINT16_MAX, it will be calculated internally based on _width.
  816. ///
  817. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem, uint16_t _pitch = UINT16_MAX);
  818. /// Update 3D texture.
  819. ///
  820. /// @param _handle
  821. /// @param _mip
  822. /// @param _x
  823. /// @param _y
  824. /// @param _z
  825. /// @param _width
  826. /// @param _height
  827. /// @param _depth
  828. /// @param _mem
  829. ///
  830. 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);
  831. /// Update Cube texture.
  832. ///
  833. /// @param _handle
  834. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is
  835. /// -Y, 4 is +Z, and 5 is -Z.
  836. ///
  837. /// +----------+
  838. /// |-z 2|
  839. /// | ^ +y |
  840. /// | | |
  841. /// | +---->+x |
  842. /// +----------+----------+----------+----------+
  843. /// |+y 1|+y 4|+y 0|+y 5|
  844. /// | ^ -x | ^ +z | ^ +x | ^ -z |
  845. /// | | | | | | | | |
  846. /// | +---->+z | +---->+x | +---->-z | +---->-x |
  847. /// +----------+----------+----------+----------+
  848. /// |+z 3|
  849. /// | ^ -y |
  850. /// | | |
  851. /// | +---->+x |
  852. /// +----------+
  853. ///
  854. /// @param _mip
  855. /// @param _x
  856. /// @param _y
  857. /// @param _width
  858. /// @param _height
  859. /// @param _mem
  860. /// @param _pitch Pitch of input image (bytes). When _pitch is set to
  861. /// UINT16_MAX, it will be calculated internally based on _width.
  862. ///
  863. 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, uint16_t _pitch = UINT16_MAX);
  864. /// Destroy texture.
  865. void destroyTexture(TextureHandle _handle);
  866. /// Create frame buffer (simple).
  867. ///
  868. /// @param _width Texture width.
  869. /// @param _height Texture height.
  870. /// @param _format Texture format.
  871. /// @param _textureFlags Texture flags.
  872. ///
  873. FrameBufferHandle createFrameBuffer(uint16_t _width, uint16_t _height, TextureFormat::Enum _format, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  874. /// Create frame buffer.
  875. ///
  876. /// @param _num Number of texture attachments.
  877. /// @param _handles Texture attachments.
  878. /// @param _destroyTextures If true, textures will be destroyed when
  879. /// frame buffer is destroyed.
  880. ///
  881. FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles, bool _destroyTextures = false);
  882. /// Destroy frame buffer.
  883. void destroyFrameBuffer(FrameBufferHandle _handle);
  884. /// Create shader uniform parameter.
  885. ///
  886. /// @param _name Uniform name in shader.
  887. /// @param _type Type of uniform (See: UniformType).
  888. /// @param _num Number of elements in array.
  889. ///
  890. /// Predefined uniforms:
  891. ///
  892. /// u_viewRect vec4(x, y, width, height) - view rectangle for current
  893. /// view.
  894. ///
  895. /// u_viewTexel vec4(1.0/width, 1.0/height, undef, undef) - inverse
  896. /// width and height
  897. ///
  898. /// u_view mat4 - view matrix
  899. ///
  900. /// u_viewProj mat4 - concatenated view projection matrix
  901. ///
  902. /// u_model mat4[BGFX_CONFIG_MAX_BONES] - array of model matrices.
  903. ///
  904. /// u_modelView mat4 - concatenated model view matrix, only first
  905. /// model matrix from array is used.
  906. ///
  907. /// u_modelViewProj mat4 - concatenated model view projection matrix.
  908. ///
  909. /// u_alphaRef float - alpha reference value for alpha test.
  910. ///
  911. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  912. /// Destroy shader uniform parameter.
  913. void destroyUniform(UniformHandle _handle);
  914. /// Set view name.
  915. ///
  916. /// @param _id View id.
  917. /// @param _name View name.
  918. ///
  919. /// NOTE:
  920. /// This is debug only feature.
  921. ///
  922. void setViewName(uint8_t _id, const char* _name);
  923. /// Set view rectangle. Draw primitive outside view will be clipped.
  924. ///
  925. /// @param _id View id.
  926. /// @param _x Position x from the left corner of the window.
  927. /// @param _y Position y from the top corner of the window.
  928. /// @param _width Width of view port region.
  929. /// @param _height Height of view port region.
  930. ///
  931. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  932. /// Set view rectangle for multiple views.
  933. ///
  934. /// @param _viewMask Bit mask representing affected views.
  935. /// @param _x Position x from the left corner of the window.
  936. /// @param _y Position y from the top corner of the window.
  937. /// @param _width Width of view port region.
  938. /// @param _height Height of view port region.
  939. ///
  940. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  941. /// Set view scissor. Draw primitive outside view will be clipped. When
  942. /// _x, _y, _width and _height are set to 0, scissor will be disabled.
  943. ///
  944. /// @param _x Position x from the left corner of the window.
  945. /// @param _y Position y from the top corner of the window.
  946. /// @param _width Width of scissor region.
  947. /// @param _height Height of scissor region.
  948. ///
  949. void setViewScissor(uint8_t _id, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0);
  950. /// Set view scissor for multiple views. When _x, _y, _width and _height
  951. /// are set to 0, scissor will be disabled.
  952. ///
  953. /// @param _id View id.
  954. /// @param _viewMask Bit mask representing affected views.
  955. /// @param _x Position x from the left corner of the window.
  956. /// @param _y Position y from the top corner of the window.
  957. /// @param _width Width of scissor region.
  958. /// @param _height Height of scissor region.
  959. ///
  960. void setViewScissorMask(uint32_t _viewMask, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0);
  961. /// Set view clear flags.
  962. ///
  963. /// @param _id View id.
  964. /// @param _flags Clear flags. Use BGFX_CLEAR_NONE to remove any clear
  965. /// operation. See: BGFX_CLEAR_*.
  966. /// @param _rgba Color clear value.
  967. /// @param _depth Depth clear value.
  968. /// @param _stencil Stencil clear value.
  969. ///
  970. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  971. /// Set view clear flags for multiple views.
  972. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  973. /// Set view into sequential mode. Draw calls will be sorted in the same
  974. /// order in which submit calls were called.
  975. void setViewSeq(uint8_t _id, bool _enabled);
  976. /// Set multiple views into sequential mode.
  977. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  978. /// Set view frame buffer.
  979. ///
  980. /// @param _id View id.
  981. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as
  982. /// frame buffer handle will draw primitives from this view into
  983. /// default back buffer.
  984. ///
  985. void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle);
  986. /// Set view frame buffer for multiple views.
  987. ///
  988. /// @param _viewMask View mask.
  989. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as
  990. /// frame buffer handle will draw primitives from this view into
  991. /// default back buffer.
  992. ///
  993. void setViewFrameBufferMask(uint32_t _viewMask, FrameBufferHandle _handle);
  994. /// Set view view and projection matrices, all draw primitives in this
  995. /// view will use these matrices.
  996. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff);
  997. /// Set view view and projection matrices for multiple views.
  998. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff);
  999. /// Sets debug marker.
  1000. void setMarker(const char* _marker);
  1001. /// Set render states for draw primitive.
  1002. ///
  1003. /// @param _state State flags. Default state for primitive type is
  1004. /// triangles. See: BGFX_STATE_DEFAULT.
  1005. ///
  1006. /// BGFX_STATE_ALPHA_WRITE - Enable alpha write.
  1007. /// BGFX_STATE_DEPTH_WRITE - Enable depth write.
  1008. /// BGFX_STATE_DEPTH_TEST_* - Depth test function.
  1009. /// BGFX_STATE_BLEND_* - See NOTE 1: BGFX_STATE_BLEND_FUNC.
  1010. /// BGFX_STATE_BLEND_EQUATION_* - See NOTE 2.
  1011. /// BGFX_STATE_CULL_* - Backface culling mode.
  1012. /// BGFX_STATE_RGB_WRITE - Enable RGB write.
  1013. /// BGFX_STATE_MSAA - Enable MSAA.
  1014. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type.
  1015. ///
  1016. /// @param _rgba Sets blend factor used by BGFX_STATE_BLEND_FACTOR and
  1017. /// BGFX_STATE_BLEND_INV_FACTOR blend modes.
  1018. ///
  1019. /// NOTE:
  1020. /// 1. Use BGFX_STATE_ALPHA_REF, BGFX_STATE_POINT_SIZE and
  1021. /// BGFX_STATE_BLEND_FUNC macros to setup more complex states.
  1022. /// 2. BGFX_STATE_BLEND_EQUATION_ADD is set when no other blend
  1023. /// equation is specified.
  1024. ///
  1025. void setState(uint64_t _state, uint32_t _rgba = 0);
  1026. /// Set stencil test state.
  1027. ///
  1028. /// @param _fstencil Front stencil state.
  1029. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  1030. /// _fstencil is applied to both front and back facing primitives.
  1031. ///
  1032. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  1033. /// Set scissor for draw primitive. For scissor for all primitives in
  1034. /// view see setViewScissor.
  1035. ///
  1036. /// @param _x Position x from the left corner of the window.
  1037. /// @param _y Position y from the top corner of the window.
  1038. /// @param _width Width of scissor region.
  1039. /// @param _height Height of scissor region.
  1040. /// @returns Scissor cache index.
  1041. ///
  1042. uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  1043. /// Set scissor from cache for draw primitive.
  1044. ///
  1045. /// @param _cache Index in scissor cache. Passing UINT16_MAX unset primitive
  1046. /// scissor and primitive will use view scissor instead.
  1047. ///
  1048. void setScissor(uint16_t _cache = UINT16_MAX);
  1049. /// Set model matrix for draw primitive. If it is not called model will
  1050. /// be rendered with identity model matrix.
  1051. ///
  1052. /// @param _mtx Pointer to first matrix in array.
  1053. /// @param _num Number of matrices in array.
  1054. /// @returns index into matrix cache in case the same model matrix has
  1055. /// to be used for other draw primitive call.
  1056. ///
  1057. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  1058. /// Set model matrix from matrix cache for draw primitive.
  1059. ///
  1060. /// @param _cache Index in matrix cache.
  1061. /// @param _num Number of matrices from cache.
  1062. ///
  1063. void setTransform(uint32_t _cache, uint16_t _num = 1);
  1064. /// Set shader uniform parameter for draw primitive.
  1065. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  1066. /// Set index buffer for draw primitive.
  1067. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  1068. /// Set index buffer for draw primitive.
  1069. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  1070. /// Set index buffer for draw primitive.
  1071. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _numIndices = UINT32_MAX);
  1072. /// Set vertex buffer for draw primitive.
  1073. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  1074. /// Set vertex buffer for draw primitive.
  1075. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  1076. /// Set vertex buffer for draw primitive.
  1077. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _numVertices = UINT32_MAX);
  1078. /// Set instance data buffer for draw primitive.
  1079. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  1080. /// Set program for draw primitive.
  1081. void setProgram(ProgramHandle _handle);
  1082. /// Set texture stage for draw primitive.
  1083. ///
  1084. /// @param _stage Texture unit.
  1085. /// @param _sampler Program sampler.
  1086. /// @param _handle Texture handle.
  1087. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses
  1088. /// texture sampling settings from the texture.
  1089. ///
  1090. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  1091. /// mode.
  1092. ///
  1093. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  1094. /// sampling.
  1095. ///
  1096. /// @param _flags Texture sampler filtering flags. UINT32_MAX use the
  1097. /// sampler filtering mode set by texture.
  1098. ///
  1099. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags = UINT32_MAX);
  1100. /// Set texture stage for draw primitive.
  1101. ///
  1102. /// @param _stage Texture unit.
  1103. /// @param _sampler Program sampler.
  1104. /// @param _handle Frame buffer handle.
  1105. /// @param _attachment Attachment index.
  1106. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses
  1107. /// texture sampling settings from the texture.
  1108. ///
  1109. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  1110. /// mode.
  1111. ///
  1112. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  1113. /// sampling.
  1114. ///
  1115. void setTexture(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment = 0, uint32_t _flags = UINT32_MAX);
  1116. /// Submit primitive for rendering into single view.
  1117. ///
  1118. /// @param _id View id.
  1119. /// @param _depth Depth for sorting.
  1120. /// @returns Number of draw calls.
  1121. ///
  1122. uint32_t submit(uint8_t _id, int32_t _depth = 0);
  1123. /// Submit primitive for rendering into multiple views.
  1124. ///
  1125. /// @param _viewMask Mask to which views to submit draw primitive calls.
  1126. /// @param _depth Depth for sorting.
  1127. /// @returns Number of draw calls.
  1128. ///
  1129. uint32_t submitMask(uint32_t _viewMask, int32_t _depth = 0);
  1130. /// Discard all previously set state for draw call.
  1131. void discard();
  1132. /// Request screen shot.
  1133. ///
  1134. /// @param _filePath Will be passed to CallbackI::screenShot callback.
  1135. ///
  1136. /// NOTE:
  1137. /// CallbackI::screenShot must be implemented.
  1138. ///
  1139. void saveScreenShot(const char* _filePath);
  1140. } // namespace bgfx
  1141. #endif // BGFX_H_HEADER_GUARD