bgfx.h 44 KB

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