bgfx.h 35 KB

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