bgfx.h 36 KB

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