bgfx.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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. #include "bgfxdefines.h"
  10. ///
  11. #define BGFX_HANDLE(_name) \
  12. struct _name { uint16_t idx; }; \
  13. inline bool isValid(_name _handle) { return bgfx::invalidHandle != _handle.idx; }
  14. #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
  15. namespace bx { struct ReallocatorI; }
  16. /// BGFX
  17. namespace bgfx
  18. {
  19. struct Fatal
  20. {
  21. enum Enum
  22. {
  23. DebugCheck,
  24. MinimumRequiredSpecs,
  25. InvalidShader,
  26. UnableToInitialize,
  27. UnableToCreateTexture,
  28. };
  29. };
  30. struct RendererType
  31. {
  32. enum Enum
  33. {
  34. Null,
  35. Direct3D9,
  36. Direct3D11,
  37. OpenGLES,
  38. OpenGL,
  39. Count
  40. };
  41. };
  42. struct Access
  43. {
  44. enum Enum
  45. {
  46. Read,
  47. Write,
  48. ReadWrite,
  49. Count
  50. };
  51. };
  52. struct Attrib
  53. {
  54. enum Enum // corresponds to vertex shader attribute:
  55. {
  56. Position, // a_position
  57. Normal, // a_normal
  58. Tangent, // a_tangent
  59. Color0, // a_color0
  60. Color1, // a_color1
  61. Indices, // a_indices
  62. Weight, // a_weight
  63. TexCoord0, // a_texcoord0
  64. TexCoord1, // a_texcoord1
  65. TexCoord2, // a_texcoord2
  66. TexCoord3, // a_texcoord3
  67. TexCoord4, // a_texcoord4
  68. TexCoord5, // a_texcoord5
  69. TexCoord6, // a_texcoord6
  70. TexCoord7, // a_texcoord7
  71. Count
  72. };
  73. };
  74. struct AttribType
  75. {
  76. enum Enum
  77. {
  78. Uint8,
  79. Int16,
  80. Half, // Availability depends on: BGFX_CAPS_VERTEX_ATTRIB_HALF.
  81. Float,
  82. Count
  83. };
  84. };
  85. struct TextureFormat
  86. {
  87. // Availability depends on BGFX_CAPS_TEXTURE_FORMAT_*.
  88. enum Enum
  89. {
  90. BC1, // DXT1
  91. BC2, // DXT3
  92. BC3, // DXT5
  93. BC4, // LATC1/ATI1
  94. BC5, // LATC2/ATI2
  95. ETC1, // ETC1 RGB8
  96. ETC2, // ETC2 RGB8
  97. ETC2A, // ETC2 RGBA8
  98. ETC2A1, // ETC2 RGB8A1
  99. PTC12, // PVRTC1 RGB 2BPP
  100. PTC14, // PVRTC1 RGB 4BPP
  101. PTC12A, // PVRTC1 RGBA 2BPP
  102. PTC14A, // PVRTC1 RGBA 4BPP
  103. PTC22, // PVRTC2 RGBA 2BPP
  104. PTC24, // PVRTC2 RGBA 4BPP
  105. Unknown, // compressed formats above
  106. R8,
  107. R16,
  108. R16F,
  109. BGRA8,
  110. RGBA16,
  111. RGBA16F,
  112. R5G6B5,
  113. RGBA4,
  114. RGB5A1,
  115. RGB10A2,
  116. UnknownDepth, // depth formats below
  117. D16,
  118. D24,
  119. D24S8,
  120. D32,
  121. D16F,
  122. D24F,
  123. D32F,
  124. D0S8,
  125. Count
  126. };
  127. };
  128. struct UniformType
  129. {
  130. enum Enum
  131. {
  132. Uniform1i,
  133. Uniform1f,
  134. End,
  135. Uniform1iv,
  136. Uniform1fv,
  137. Uniform2fv,
  138. Uniform3fv,
  139. Uniform4fv,
  140. Uniform3x3fv,
  141. Uniform4x4fv,
  142. Count
  143. };
  144. };
  145. static const uint16_t invalidHandle = UINT16_MAX;
  146. BGFX_HANDLE(DynamicIndexBufferHandle);
  147. BGFX_HANDLE(DynamicVertexBufferHandle);
  148. BGFX_HANDLE(FrameBufferHandle);
  149. BGFX_HANDLE(IndexBufferHandle);
  150. BGFX_HANDLE(ProgramHandle);
  151. BGFX_HANDLE(ShaderHandle);
  152. BGFX_HANDLE(TextureHandle);
  153. BGFX_HANDLE(UniformHandle);
  154. BGFX_HANDLE(VertexBufferHandle);
  155. BGFX_HANDLE(VertexDeclHandle);
  156. /// Callback interface to implement application specific behavior.
  157. /// Cached items are currently used only for OpenGL binary shaders.
  158. ///
  159. /// NOTE:
  160. /// 'fatal' callback can be called from any thread. Other callbacks
  161. /// are called from the render thread.
  162. ///
  163. struct CallbackI
  164. {
  165. virtual ~CallbackI() = 0;
  166. /// If fatal code code is not Fatal::DebugCheck this callback is
  167. /// called on unrecoverable error. It's not safe to continue, inform
  168. /// user and terminate application from this call.
  169. virtual void fatal(Fatal::Enum _code, const char* _str) = 0;
  170. /// Return size of for cached item. Return 0 if no cached item was
  171. /// found.
  172. virtual uint32_t cacheReadSize(uint64_t _id) = 0;
  173. /// Read cached item.
  174. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) = 0;
  175. /// Write cached item.
  176. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) = 0;
  177. /// Screenshot captured. Screenshot format is always 4-byte BGRA.
  178. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) = 0;
  179. /// Called when capture begins.
  180. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format, bool _yflip) = 0;
  181. /// Called when capture ends.
  182. virtual void captureEnd() = 0;
  183. /// Captured frame.
  184. virtual void captureFrame(const void* _data, uint32_t _size) = 0;
  185. };
  186. inline CallbackI::~CallbackI()
  187. {
  188. }
  189. struct Memory
  190. {
  191. uint8_t* data;
  192. uint32_t size;
  193. };
  194. /// Renderer capabilities.
  195. struct Caps
  196. {
  197. /// Renderer backend type.
  198. RendererType::Enum rendererType;
  199. /// Supported functionality, it includes emulated functionality.
  200. /// Checking supported and not emulated will give functionality
  201. /// natively supported by renderer.
  202. uint64_t supported;
  203. /// Emulated functionality. For example some texture compression
  204. /// modes are not natively supported by all renderers. The library
  205. /// internally decompresses texture into supported format.
  206. uint64_t emulated;
  207. uint16_t maxTextureSize; ///< Maximum texture size.
  208. uint16_t maxDrawCalls; ///< Maximum draw calls.
  209. uint8_t maxFBAttachments; ///< Maximum frame buffer attachments.
  210. };
  211. struct TransientIndexBuffer
  212. {
  213. uint8_t* data;
  214. uint32_t size;
  215. IndexBufferHandle handle;
  216. uint32_t startIndex;
  217. };
  218. struct TransientVertexBuffer
  219. {
  220. uint8_t* data;
  221. uint32_t size;
  222. uint32_t startVertex;
  223. uint16_t stride;
  224. VertexBufferHandle handle;
  225. VertexDeclHandle decl;
  226. };
  227. struct InstanceDataBuffer
  228. {
  229. uint8_t* data;
  230. uint32_t size;
  231. uint32_t offset;
  232. uint16_t stride;
  233. uint16_t num;
  234. VertexBufferHandle handle;
  235. };
  236. struct TextureInfo
  237. {
  238. TextureFormat::Enum format;
  239. uint32_t storageSize;
  240. uint16_t width;
  241. uint16_t height;
  242. uint16_t depth;
  243. uint8_t numMips;
  244. uint8_t bitsPerPixel;
  245. };
  246. /// Vertex declaration.
  247. struct VertexDecl
  248. {
  249. /// Start VertexDecl.
  250. VertexDecl& begin(RendererType::Enum _renderer = RendererType::Null);
  251. /// End VertexDecl.
  252. void end();
  253. /// Add attribute to VertexDecl.
  254. ///
  255. /// @param _attrib Attribute semantics.
  256. /// @param _num Number of elements 1, 2, 3 or 4.
  257. /// @param _type Element type.
  258. /// @param _normalized When using fixed point AttribType (f.e. Uint8)
  259. /// value will be normalized for vertex shader usage. When normalized
  260. /// is set to true, AttribType::Uint8 value in range 0-255 will be
  261. /// in range 0.0-1.0 in vertex shader.
  262. /// @param _asInt Packaging rule for vertexPack, vertexUnpack, and
  263. /// vertexConvert for AttribType::Uint8 and AttribType::Int16.
  264. /// Unpacking code must be implemented inside vertex shader.
  265. ///
  266. /// NOTE:
  267. /// Must be called between begin/end.
  268. ///
  269. VertexDecl& add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
  270. /// Skip _num bytes in vertex stream.
  271. VertexDecl& skip(uint8_t _num);
  272. /// Decode attribute.
  273. void decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const;
  274. /// Returns true if VertexDecl contains attribute.
  275. bool has(Attrib::Enum _attrib) const { return 0xff != m_attributes[_attrib]; }
  276. /// Returns relative attribute offset from the vertex.
  277. uint16_t getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; }
  278. /// Returns vertex stride.
  279. uint16_t getStride() const { return m_stride; }
  280. /// Returns size of vertex buffer for number of vertices.
  281. uint32_t getSize(uint32_t _num) const { return _num*m_stride; }
  282. uint32_t m_hash;
  283. uint16_t m_stride;
  284. uint16_t m_offset[Attrib::Count];
  285. uint8_t m_attributes[Attrib::Count];
  286. };
  287. /// Pack vec4 into vertex stream format.
  288. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index = 0);
  289. /// Unpack vec4 from vertex stream format.
  290. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index = 0);
  291. /// Converts vertex stream data from one vertex stream format to another.
  292. ///
  293. /// @param _destDecl Destination vertex stream declaration.
  294. /// @param _destData Destination vertex stream.
  295. /// @param _srcDecl Source vertex stream declaration.
  296. /// @param _srcData Source vertex stream data.
  297. /// @param _num Number of vertices to convert from source to destination.
  298. ///
  299. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1);
  300. /// Weld vertices.
  301. ///
  302. /// @param _output Welded vertices remapping table. The size of buffer
  303. /// must be the same as number of vertices.
  304. /// @param _decl Vertex stream declaration.
  305. /// @param _data Vertex stream.
  306. /// @param _num Number of vertices in vertex stream.
  307. /// @param _epsilon Error tolerance for vertex position comparison.
  308. /// @returns Number of unique vertices after vertex welding.
  309. ///
  310. uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon = 0.001f);
  311. /// Swizzle RGBA8 image to BGRA8.
  312. ///
  313. /// @param _width Width of input image (pixels).
  314. /// @param _height Height of input image (pixels).
  315. /// @param _pitch Pitch of input image (bytes).
  316. /// @param _src Source image.
  317. /// @param _dst Destination image. Must be the same size as input image.
  318. /// _dst might be pointer to the same memory as _src.
  319. ///
  320. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  321. /// Downsample RGBA8 image with 2x2 pixel average filter.
  322. ///
  323. /// @param _width Width of input image (pixels).
  324. /// @param _height Height of input image (pixels).
  325. /// @param _pitch Pitch of input image (bytes).
  326. /// @param _src Source image.
  327. /// @param _dst Destination image. Must be at least quarter size of
  328. /// input image. _dst might be pointer to the same memory as _src.
  329. ///
  330. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  331. /// Returns supported backend API renderers.
  332. uint8_t getSupportedRenderers(RendererType::Enum _enum[RendererType::Count]);
  333. /// Returns name of renderer.
  334. const char* getRendererName(RendererType::Enum _type);
  335. /// Initialize bgfx library.
  336. ///
  337. /// @param _type Select rendering backend. When set to RendererType::Count
  338. /// default rendering backend will be selected.
  339. ///
  340. /// @param _callback Provide application specific callback interface.
  341. /// See: CallbackI
  342. ///
  343. /// @param _reallocator Custom allocator. When custom allocator is not
  344. /// specified, library uses default CRT allocator. The library assumes
  345. /// custom allocator is thread safe.
  346. ///
  347. void init(RendererType::Enum _type = RendererType::Count, CallbackI* _callback = NULL, bx::ReallocatorI* _reallocator = NULL);
  348. /// Shutdown bgfx library.
  349. void shutdown();
  350. /// Reset graphic settings.
  351. void reset(uint32_t _width, uint32_t _height, uint32_t _flags = BGFX_RESET_NONE);
  352. /// Advance to next frame. When using multithreaded renderer, this call
  353. /// just swaps internal buffers, kicks render thread, and returns. In
  354. /// singlethreaded renderer this call does frame rendering.
  355. ///
  356. /// @returns Current frame number. This might be used in conjunction with
  357. /// double/multi buffering data outside the library and passing it to
  358. /// library via makeRef calls.
  359. ///
  360. uint32_t frame();
  361. /// Returns current renderer backend API type.
  362. ///
  363. /// NOTE:
  364. /// Library must be initialized.
  365. ///
  366. RendererType::Enum getRendererType();
  367. /// Returns renderer capabilities.
  368. ///
  369. /// NOTE:
  370. /// Library must be initialized.
  371. ///
  372. const Caps* getCaps();
  373. /// Allocate buffer to pass to bgfx calls. Data will be freed inside bgfx.
  374. const Memory* alloc(uint32_t _size);
  375. /// Allocate buffer and copy data into it. Data will be freed inside bgfx.
  376. const Memory* copy(const void* _data, uint32_t _size);
  377. /// Make reference to data to pass to bgfx. Unlike bgfx::alloc this call
  378. /// doesn't allocate memory for data. It just copies pointer to data.
  379. /// You must make sure data is available for at least 2 bgfx::frame calls.
  380. const Memory* makeRef(const void* _data, uint32_t _size);
  381. /// Set debug flags.
  382. ///
  383. /// @param _debug Available flags:
  384. ///
  385. /// BGFX_DEBUG_IFH - Infinitely fast hardware. When this flag is set
  386. /// all rendering calls will be skipped. It's useful when profiling
  387. /// to quickly assess bottleneck between CPU and GPU.
  388. ///
  389. /// BGFX_DEBUG_STATS - Display internal statistics.
  390. ///
  391. /// BGFX_DEBUG_TEXT - Display debug text.
  392. ///
  393. /// BGFX_DEBUG_WIREFRAME - Wireframe rendering. All rendering
  394. /// primitives will be rendered as lines.
  395. ///
  396. void setDebug(uint32_t _debug);
  397. /// Clear internal debug text buffer.
  398. void dbgTextClear(uint8_t _attr = 0, bool _small = false);
  399. /// Print into internal debug text buffer.
  400. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...);
  401. /// Create static index buffer.
  402. ///
  403. /// NOTE:
  404. /// Only 16-bit index buffer is supported.
  405. ///
  406. IndexBufferHandle createIndexBuffer(const Memory* _mem);
  407. /// Destroy static index buffer.
  408. void destroyIndexBuffer(IndexBufferHandle _handle);
  409. /// Create static vertex buffer.
  410. ///
  411. /// @param _mem Vertex buffer data.
  412. /// @param _decl Vertex declaration.
  413. /// @returns Static vertex buffer handle.
  414. ///
  415. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  416. /// Destroy static vertex buffer.
  417. ///
  418. /// @param _handle Static vertex buffer handle.
  419. ///
  420. void destroyVertexBuffer(VertexBufferHandle _handle);
  421. /// Create empty dynamic index buffer.
  422. ///
  423. /// @param _num Number of indices.
  424. ///
  425. /// NOTE:
  426. /// Only 16-bit index buffer is supported.
  427. ///
  428. DynamicIndexBufferHandle createDynamicIndexBuffer(uint32_t _num);
  429. /// Create dynamic index buffer and initialized it.
  430. ///
  431. /// @param _mem Index buffer data.
  432. ///
  433. /// NOTE:
  434. /// Only 16-bit index buffer is supported.
  435. ///
  436. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem);
  437. /// Update dynamic index buffer.
  438. ///
  439. /// @param _handle Dynamic index buffer handle.
  440. /// @param _mem Index buffer data.
  441. ///
  442. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem);
  443. /// Destroy dynamic index buffer.
  444. ///
  445. /// @param _handle Dynamic index buffer handle.
  446. ///
  447. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle);
  448. /// Create empty dynamic vertex buffer.
  449. ///
  450. /// @param _num Number of vertices.
  451. /// @param _decl Vertex declaration.
  452. ///
  453. DynamicVertexBufferHandle createDynamicVertexBuffer(uint16_t _num, const VertexDecl& _decl);
  454. /// Create dynamic vertex buffer and initialize it.
  455. ///
  456. /// @param _mem Vertex buffer data.
  457. /// @param _decl Vertex declaration.
  458. ///
  459. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl);
  460. /// Update dynamic vertex buffer.
  461. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem);
  462. /// Destroy dynamic vertex buffer.
  463. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle);
  464. /// Returns true if internal transient index buffer has enough space.
  465. ///
  466. /// @param _num Number of indices.
  467. ///
  468. bool checkAvailTransientIndexBuffer(uint32_t _num);
  469. /// Returns true if internal transient vertex buffer has enough space.
  470. ///
  471. /// @param _num Number of vertices.
  472. /// @param _decl Vertex declaration.
  473. ///
  474. bool checkAvailTransientVertexBuffer(uint32_t _num, const VertexDecl& _decl);
  475. /// Returns true if internal instance data buffer has enough space.
  476. ///
  477. /// @param _num Number of instances.
  478. /// @param _stride Stride per instance.
  479. ///
  480. bool checkAvailInstanceDataBuffer(uint32_t _num, uint16_t _stride);
  481. /// Returns true if both internal transient index and vertex buffer have
  482. /// enough space.
  483. ///
  484. /// @param _numVertices Number of vertices.
  485. /// @param _decl Vertex declaration.
  486. /// @param _numIndices Number of indices.
  487. ///
  488. bool checkAvailTransientBuffers(uint32_t _numVertices, const VertexDecl& _decl, uint32_t _numIndices);
  489. /// Allocate transient index buffer.
  490. ///
  491. /// @param[out] _tib TransientIndexBuffer structure is filled and is valid
  492. /// for the duration of frame, and it can be reused for multiple draw
  493. /// calls.
  494. /// @param _num Number of indices to allocate.
  495. ///
  496. /// NOTE:
  497. /// 1. You must call setIndexBuffer after alloc in order to avoid memory
  498. /// leak.
  499. /// 2. Only 16-bit index buffer is supported.
  500. ///
  501. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num);
  502. /// Allocate transient vertex buffer.
  503. ///
  504. /// @param[out] _tvb TransientVertexBuffer structure is filled and is valid
  505. /// for the duration of frame, and it can be reused for multiple draw
  506. /// calls.
  507. /// @param _num Number of vertices to allocate.
  508. /// @param _decl Vertex declaration.
  509. ///
  510. /// NOTE:
  511. /// You must call setVertexBuffer after alloc in order to avoid memory
  512. /// leak.
  513. ///
  514. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl);
  515. /// Check for required space and allocate transient vertex and index
  516. /// buffers. If both space requirements are satisfied function returns
  517. /// true.
  518. ///
  519. /// NOTE:
  520. /// Only 16-bit index buffer is supported.
  521. ///
  522. bool allocTransientBuffers(TransientVertexBuffer* _tvb, const VertexDecl& _decl, uint16_t _numVertices, TransientIndexBuffer* _tib, uint16_t _numIndices);
  523. /// Allocate instance data buffer.
  524. ///
  525. /// NOTE:
  526. /// You must call setInstanceDataBuffer after alloc in order to avoid
  527. /// memory leak.
  528. ///
  529. const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride);
  530. /// Create shader from memory buffer.
  531. ShaderHandle createShader(const Memory* _mem);
  532. /// Returns num of uniforms, and uniform handles used inside shader.
  533. ///
  534. /// @param _handle Shader handle.
  535. /// @param _uniforms UniformHandle array where data will be stored.
  536. /// @param _max Maximum capacity of array.
  537. /// @returns Number of uniforms used by shader.
  538. ///
  539. /// NOTE:
  540. /// Only non-predefined uniforms are returned.
  541. ///
  542. uint16_t getShaderUniforms(ShaderHandle _handle, UniformHandle* _uniforms = NULL, uint16_t _max = 0);
  543. /// Destroy shader. Once program is created with shader it is safe to
  544. /// destroy shader.
  545. void destroyShader(ShaderHandle _handle);
  546. /// Create program with vertex and fragment shaders.
  547. ///
  548. /// @param _vsh Vertex shader.
  549. /// @param _fsh Fragment shader.
  550. /// @param _destroyShaders If true, shaders will be destroyed when
  551. /// program is destroyed.
  552. /// @returns Program handle if vertex shader output and fragment shader
  553. /// input are matching, otherwise returns invalid program handle.
  554. ///
  555. ProgramHandle createProgram(ShaderHandle _vsh, ShaderHandle _fsh, bool _destroyShaders = false);
  556. /// Destroy program.
  557. void destroyProgram(ProgramHandle _handle);
  558. /// Calculate amount of memory required for texture.
  559. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format);
  560. /// Create texture from memory buffer.
  561. ///
  562. /// @param _mem DDS, KTX or PVR texture data.
  563. /// @param _flags Default texture sampling mode is linear, and wrap mode
  564. /// is repeat.
  565. ///
  566. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  567. /// mode.
  568. ///
  569. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  570. /// sampling.
  571. ///
  572. /// @param _skip Skip top level mips when parsing texture.
  573. /// @param _info Returns parsed texture information.
  574. /// @returns Texture handle.
  575. ///
  576. TextureHandle createTexture(const Memory* _mem, uint32_t _flags = BGFX_TEXTURE_NONE, uint8_t _skip = 0, TextureInfo* _info = NULL);
  577. /// Create 2D texture.
  578. ///
  579. /// @param _width
  580. /// @param _height
  581. /// @param _numMips
  582. /// @param _format
  583. /// @param _flags
  584. /// @param _mem
  585. ///
  586. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  587. /// Create 3D texture.
  588. ///
  589. /// @param _width
  590. /// @param _height
  591. /// @param _depth
  592. /// @param _numMips
  593. /// @param _format
  594. /// @param _flags
  595. /// @param _mem
  596. ///
  597. 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);
  598. /// Create Cube texture.
  599. ///
  600. /// @param _size
  601. /// @param _numMips
  602. /// @param _format
  603. /// @param _flags
  604. /// @param _mem
  605. ///
  606. TextureHandle createTextureCube(uint16_t _size, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
  607. /// Update 2D texture.
  608. ///
  609. /// @param _handle
  610. /// @param _mip
  611. /// @param _x
  612. /// @param _y
  613. /// @param _width
  614. /// @param _height
  615. /// @param _mem
  616. /// @param _pitch Pitch of input image (bytes). When _pitch is set to
  617. /// UINT16_MAX, it will be calculated internally based on _width.
  618. ///
  619. 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);
  620. /// Update 3D texture.
  621. ///
  622. /// @param _handle
  623. /// @param _mip
  624. /// @param _x
  625. /// @param _y
  626. /// @param _z
  627. /// @param _width
  628. /// @param _height
  629. /// @param _depth
  630. /// @param _mem
  631. ///
  632. 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);
  633. /// Update Cube texture.
  634. ///
  635. /// @param _handle
  636. /// @param _side Cubemap side, where 0 is +X, 1 is -X, 2 is +Y, 3 is
  637. /// -Y, 4 is +Z, and 5 is -Z.
  638. ///
  639. /// +----------+
  640. /// |-z 2|
  641. /// | ^ +y |
  642. /// | | |
  643. /// | +---->+x |
  644. /// +----------+----------+----------+----------+
  645. /// |+y 1|+y 4|+y 0|+y 5|
  646. /// | ^ -x | ^ +z | ^ +x | ^ -z |
  647. /// | | | | | | | | |
  648. /// | +---->+z | +---->+x | +---->-z | +---->-x |
  649. /// +----------+----------+----------+----------+
  650. /// |+z 3|
  651. /// | ^ -y |
  652. /// | | |
  653. /// | +---->+x |
  654. /// +----------+
  655. ///
  656. /// @param _mip
  657. /// @param _x
  658. /// @param _y
  659. /// @param _width
  660. /// @param _height
  661. /// @param _mem
  662. /// @param _pitch Pitch of input image (bytes). When _pitch is set to
  663. /// UINT16_MAX, it will be calculated internally based on _width.
  664. ///
  665. 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);
  666. /// Destroy texture.
  667. void destroyTexture(TextureHandle _handle);
  668. /// Create frame buffer (simple).
  669. ///
  670. /// @param _width Texture width.
  671. /// @param _height Texture height.
  672. /// @param _format Texture format.
  673. /// @param _textureFlags Texture flags.
  674. ///
  675. FrameBufferHandle createFrameBuffer(uint16_t _width, uint16_t _height, TextureFormat::Enum _format, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  676. /// Create frame buffer.
  677. ///
  678. /// @param _num Number of texture attachments.
  679. /// @param _handles Texture attachments.
  680. /// @param _destroyTextures If true, textures will be destroyed when
  681. /// frame buffer is destroyed.
  682. ///
  683. FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles, bool _destroyTextures = false);
  684. /// Destroy frame buffer.
  685. void destroyFrameBuffer(FrameBufferHandle _handle);
  686. /// Create shader uniform parameter.
  687. ///
  688. /// @param _name Uniform name in shader.
  689. /// @param _type Type of uniform (See: UniformType).
  690. /// @param _num Number of elements in array.
  691. ///
  692. /// Predefined uniforms:
  693. ///
  694. /// u_viewRect vec4(x, y, width, height) - view rectangle for current
  695. /// view.
  696. ///
  697. /// u_viewTexel vec4(1.0/width, 1.0/height, undef, undef) - inverse
  698. /// width and height
  699. ///
  700. /// u_view mat4 - view matrix
  701. ///
  702. /// u_invView mat4 - inverted view matrix
  703. ///
  704. /// u_proj mat4 - projection matrix
  705. ///
  706. /// u_invProj mat4 - inverted projection matrix
  707. ///
  708. /// u_viewProj mat4 - concatenated view projection matrix
  709. ///
  710. /// u_invViewProj mat4 - concatenated inverted view projection matrix
  711. ///
  712. /// u_model mat4[BGFX_CONFIG_MAX_BONES] - array of model matrices.
  713. ///
  714. /// u_modelView mat4 - concatenated model view matrix, only first
  715. /// model matrix from array is used.
  716. ///
  717. /// u_modelViewProj mat4 - concatenated model view projection matrix.
  718. ///
  719. /// u_alphaRef float - alpha reference value for alpha test.
  720. ///
  721. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1);
  722. /// Destroy shader uniform parameter.
  723. void destroyUniform(UniformHandle _handle);
  724. /// Set view name.
  725. ///
  726. /// @param _id View id.
  727. /// @param _name View name.
  728. ///
  729. /// NOTE:
  730. /// This is debug only feature.
  731. ///
  732. void setViewName(uint8_t _id, const char* _name);
  733. /// Set view rectangle. Draw primitive outside view will be clipped.
  734. ///
  735. /// @param _id View id.
  736. /// @param _x Position x from the left corner of the window.
  737. /// @param _y Position y from the top corner of the window.
  738. /// @param _width Width of view port region.
  739. /// @param _height Height of view port region.
  740. ///
  741. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  742. /// Set view rectangle for multiple views.
  743. ///
  744. /// @param _viewMask Bit mask representing affected views.
  745. /// @param _x Position x from the left corner of the window.
  746. /// @param _y Position y from the top corner of the window.
  747. /// @param _width Width of view port region.
  748. /// @param _height Height of view port region.
  749. ///
  750. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  751. /// Set view scissor. Draw primitive outside view will be clipped. When
  752. /// _x, _y, _width and _height are set to 0, scissor will be disabled.
  753. ///
  754. /// @param _x Position x from the left corner of the window.
  755. /// @param _y Position y from the top corner of the window.
  756. /// @param _width Width of scissor region.
  757. /// @param _height Height of scissor region.
  758. ///
  759. void setViewScissor(uint8_t _id, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0);
  760. /// Set view scissor for multiple views. When _x, _y, _width and _height
  761. /// are set to 0, scissor will be disabled.
  762. ///
  763. /// @param _id View id.
  764. /// @param _viewMask Bit mask representing affected views.
  765. /// @param _x Position x from the left corner of the window.
  766. /// @param _y Position y from the top corner of the window.
  767. /// @param _width Width of scissor region.
  768. /// @param _height Height of scissor region.
  769. ///
  770. void setViewScissorMask(uint32_t _viewMask, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0);
  771. /// Set view clear flags.
  772. ///
  773. /// @param _id View id.
  774. /// @param _flags Clear flags. Use BGFX_CLEAR_NONE to remove any clear
  775. /// operation. See: BGFX_CLEAR_*.
  776. /// @param _rgba Color clear value.
  777. /// @param _depth Depth clear value.
  778. /// @param _stencil Stencil clear value.
  779. ///
  780. void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  781. /// Set view clear flags for multiple views.
  782. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0);
  783. /// Set view into sequential mode. Draw calls will be sorted in the same
  784. /// order in which submit calls were called.
  785. void setViewSeq(uint8_t _id, bool _enabled);
  786. /// Set multiple views into sequential mode.
  787. void setViewSeqMask(uint32_t _viewMask, bool _enabled);
  788. /// Set view frame buffer.
  789. ///
  790. /// @param _id View id.
  791. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as
  792. /// frame buffer handle will draw primitives from this view into
  793. /// default back buffer.
  794. ///
  795. void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle);
  796. /// Set view frame buffer for multiple views.
  797. ///
  798. /// @param _viewMask View mask.
  799. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as
  800. /// frame buffer handle will draw primitives from this view into
  801. /// default back buffer.
  802. ///
  803. void setViewFrameBufferMask(uint32_t _viewMask, FrameBufferHandle _handle);
  804. /// Set view view and projection matrices, all draw primitives in this
  805. /// view will use these matrices.
  806. void setViewTransform(uint8_t _id, const void* _view, const void* _proj);
  807. /// Set view view and projection matrices for multiple views.
  808. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj);
  809. /// Sets debug marker.
  810. void setMarker(const char* _marker);
  811. /// Set render states for draw primitive.
  812. ///
  813. /// @param _state State flags. Default state for primitive type is
  814. /// triangles. See: BGFX_STATE_DEFAULT.
  815. ///
  816. /// BGFX_STATE_ALPHA_WRITE - Enable alpha write.
  817. /// BGFX_STATE_DEPTH_WRITE - Enable depth write.
  818. /// BGFX_STATE_DEPTH_TEST_* - Depth test function.
  819. /// BGFX_STATE_BLEND_* - See NOTE 1: BGFX_STATE_BLEND_FUNC.
  820. /// BGFX_STATE_BLEND_EQUATION_* - See NOTE 2.
  821. /// BGFX_STATE_CULL_* - Backface culling mode.
  822. /// BGFX_STATE_RGB_WRITE - Enable RGB write.
  823. /// BGFX_STATE_MSAA - Enable MSAA.
  824. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type.
  825. ///
  826. /// @param _rgba Sets blend factor used by BGFX_STATE_BLEND_FACTOR and
  827. /// BGFX_STATE_BLEND_INV_FACTOR blend modes.
  828. ///
  829. /// NOTE:
  830. /// 1. Use BGFX_STATE_ALPHA_REF, BGFX_STATE_POINT_SIZE and
  831. /// BGFX_STATE_BLEND_FUNC macros to setup more complex states.
  832. /// 2. BGFX_STATE_BLEND_EQUATION_ADD is set when no other blend
  833. /// equation is specified.
  834. ///
  835. void setState(uint64_t _state, uint32_t _rgba = 0);
  836. /// Set stencil test state.
  837. ///
  838. /// @param _fstencil Front stencil state.
  839. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE
  840. /// _fstencil is applied to both front and back facing primitives.
  841. ///
  842. void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE);
  843. /// Set scissor for draw primitive. For scissor for all primitives in
  844. /// view see setViewScissor.
  845. ///
  846. /// @param _x Position x from the left corner of the window.
  847. /// @param _y Position y from the top corner of the window.
  848. /// @param _width Width of scissor region.
  849. /// @param _height Height of scissor region.
  850. /// @returns Scissor cache index.
  851. ///
  852. uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height);
  853. /// Set scissor from cache for draw primitive.
  854. ///
  855. /// @param _cache Index in scissor cache. Passing UINT16_MAX unset primitive
  856. /// scissor and primitive will use view scissor instead.
  857. ///
  858. void setScissor(uint16_t _cache = UINT16_MAX);
  859. /// Set model matrix for draw primitive. If it is not called model will
  860. /// be rendered with identity model matrix.
  861. ///
  862. /// @param _mtx Pointer to first matrix in array.
  863. /// @param _num Number of matrices in array.
  864. /// @returns index into matrix cache in case the same model matrix has
  865. /// to be used for other draw primitive call.
  866. ///
  867. uint32_t setTransform(const void* _mtx, uint16_t _num = 1);
  868. /// Set model matrix from matrix cache for draw primitive.
  869. ///
  870. /// @param _cache Index in matrix cache.
  871. /// @param _num Number of matrices from cache.
  872. ///
  873. void setTransform(uint32_t _cache, uint16_t _num = 1);
  874. /// Set shader uniform parameter for draw primitive.
  875. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);
  876. /// Set index buffer for draw primitive.
  877. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  878. /// Set index buffer for draw primitive.
  879. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
  880. /// Set index buffer for draw primitive.
  881. void setIndexBuffer(const TransientIndexBuffer* _tib);
  882. /// Set index buffer for draw primitive.
  883. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices);
  884. /// Set vertex buffer for draw primitive.
  885. void setVertexBuffer(VertexBufferHandle _handle);
  886. /// Set vertex buffer for draw primitive.
  887. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices);
  888. /// Set vertex buffer for draw primitive.
  889. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
  890. /// Set vertex buffer for draw primitive.
  891. void setVertexBuffer(const TransientVertexBuffer* _tvb);
  892. /// Set vertex buffer for draw primitive.
  893. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices);
  894. /// Set instance data buffer for draw primitive.
  895. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX);
  896. /// Set program for draw primitive.
  897. void setProgram(ProgramHandle _handle);
  898. /// Set texture stage for draw primitive.
  899. ///
  900. /// @param _stage Texture unit.
  901. /// @param _sampler Program sampler.
  902. /// @param _handle Texture handle.
  903. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses
  904. /// texture sampling settings from the texture.
  905. ///
  906. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  907. /// mode.
  908. ///
  909. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  910. /// sampling.
  911. ///
  912. /// @param _flags Texture sampler filtering flags. UINT32_MAX use the
  913. /// sampler filtering mode set by texture.
  914. ///
  915. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags = UINT32_MAX);
  916. /// Set texture stage for draw primitive.
  917. ///
  918. /// @param _stage Texture unit.
  919. /// @param _sampler Program sampler.
  920. /// @param _handle Frame buffer handle.
  921. /// @param _attachment Attachment index.
  922. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses
  923. /// texture sampling settings from the texture.
  924. ///
  925. /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap
  926. /// mode.
  927. ///
  928. /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic
  929. /// sampling.
  930. ///
  931. void setTexture(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment = 0, uint32_t _flags = UINT32_MAX);
  932. /// Submit primitive for rendering into single view.
  933. ///
  934. /// @param _id View id.
  935. /// @param _depth Depth for sorting.
  936. /// @returns Number of draw calls.
  937. ///
  938. uint32_t submit(uint8_t _id, int32_t _depth = 0);
  939. /// Submit primitive for rendering into multiple views.
  940. ///
  941. /// @param _viewMask Mask to which views to submit draw primitive calls.
  942. /// @param _depth Depth for sorting.
  943. /// @returns Number of draw calls.
  944. ///
  945. uint32_t submitMask(uint32_t _viewMask, int32_t _depth = 0);
  946. ///
  947. void setImage(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint8_t _mip, TextureFormat::Enum _format, Access::Enum _access);
  948. ///
  949. void setImage(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment, TextureFormat::Enum _format, Access::Enum _access);
  950. /// Dispatch compute.
  951. void dispatch(uint8_t _id, ProgramHandle _handle, uint16_t _numX = 1, uint16_t _numY = 1, uint16_t _numZ = 1);
  952. /// Discard all previously set state for draw or compute call.
  953. void discard();
  954. /// Request screen shot.
  955. ///
  956. /// @param _filePath Will be passed to CallbackI::screenShot callback.
  957. ///
  958. /// NOTE:
  959. /// CallbackI::screenShot must be implemented.
  960. ///
  961. void saveScreenShot(const char* _filePath);
  962. } // namespace bgfx
  963. #endif // BGFX_H_HEADER_GUARD