bgfx_utils.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #ifndef BGFX_UTILS_H_HEADER_GUARD
  6. #define BGFX_UTILS_H_HEADER_GUARD
  7. #include <bx/bounds.h>
  8. #include <bx/pixelformat.h>
  9. #include <bx/filepath.h>
  10. #include <bgfx/bgfx.h>
  11. #include <bimg/bimg.h>
  12. #include <tinystl/allocator.h>
  13. #include <tinystl/vector.h>
  14. namespace stl = tinystl;
  15. #include "args.h"
  16. ///
  17. void* load(const bx::FilePath& _filePath, uint32_t* _size = NULL);
  18. ///
  19. void unload(void* _ptr);
  20. ///
  21. bgfx::ShaderHandle loadShader(const bx::StringView& _name);
  22. ///
  23. bgfx::ProgramHandle loadProgram(const bx::StringView& _vsName, const bx::StringView& _fsName);
  24. ///
  25. bgfx::TextureHandle loadTexture(const bx::FilePath& _filePath, uint64_t _flags = BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE, uint8_t _skip = 0, bgfx::TextureInfo* _info = NULL, bimg::Orientation::Enum* _orientation = NULL);
  26. ///
  27. bimg::ImageContainer* imageLoad(const bx::FilePath& _filePath, bgfx::TextureFormat::Enum _dstFormat);
  28. ///
  29. void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices);
  30. /// Returns true if both internal transient index and vertex buffer have
  31. /// enough space.
  32. ///
  33. /// @param[in] _numVertices Number of vertices.
  34. /// @param[in] _layout Vertex layout.
  35. /// @param[in] _numIndices Number of indices.
  36. ///
  37. inline bool checkAvailTransientBuffers(uint32_t _numVertices, const bgfx::VertexLayout& _layout, uint32_t _numIndices)
  38. {
  39. return _numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, _layout)
  40. && (0 == _numIndices || _numIndices == bgfx::getAvailTransientIndexBuffer(_numIndices) )
  41. ;
  42. }
  43. ///
  44. inline uint32_t encodeNormalRgba8(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  45. {
  46. const float src[] =
  47. {
  48. _x * 0.5f + 0.5f,
  49. _y * 0.5f + 0.5f,
  50. _z * 0.5f + 0.5f,
  51. _w * 0.5f + 0.5f,
  52. };
  53. uint32_t dst;
  54. bx::packRgba8(&dst, src);
  55. return dst;
  56. }
  57. ///
  58. struct MeshState
  59. {
  60. struct Texture
  61. {
  62. uint32_t m_flags;
  63. bgfx::UniformHandle m_sampler;
  64. bgfx::TextureHandle m_texture;
  65. uint8_t m_stage;
  66. };
  67. Texture m_textures[4];
  68. uint64_t m_state;
  69. bgfx::ProgramHandle m_program;
  70. uint8_t m_numTextures;
  71. bgfx::ViewId m_viewId;
  72. };
  73. struct Primitive
  74. {
  75. uint32_t m_startIndex;
  76. uint32_t m_numIndices;
  77. uint32_t m_startVertex;
  78. uint32_t m_numVertices;
  79. bx::Sphere m_sphere;
  80. bx::Aabb m_aabb;
  81. bx::Obb m_obb;
  82. };
  83. typedef stl::vector<Primitive> PrimitiveArray;
  84. struct Group
  85. {
  86. Group();
  87. void reset();
  88. bgfx::VertexBufferHandle m_vbh;
  89. bgfx::IndexBufferHandle m_ibh;
  90. uint16_t m_numVertices;
  91. uint8_t* m_vertices;
  92. uint32_t m_numIndices;
  93. uint16_t* m_indices;
  94. bx::Sphere m_sphere;
  95. bx::Aabb m_aabb;
  96. bx::Obb m_obb;
  97. PrimitiveArray m_prims;
  98. };
  99. typedef stl::vector<Group> GroupArray;
  100. struct Mesh
  101. {
  102. void load(bx::ReaderSeekerI* _reader, bool _ramcopy);
  103. void unload();
  104. void submit(bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state) const;
  105. void submit(const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices) const;
  106. bgfx::VertexLayout m_layout;
  107. GroupArray m_groups;
  108. };
  109. ///
  110. Mesh* meshLoad(const bx::FilePath& _filePath, bool _ramcopy = false);
  111. ///
  112. void meshUnload(Mesh* _mesh);
  113. ///
  114. MeshState* meshStateCreate();
  115. ///
  116. void meshStateDestroy(MeshState* _meshState);
  117. ///
  118. void meshSubmit(const Mesh* _mesh, bgfx::ViewId _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state = BGFX_STATE_MASK);
  119. ///
  120. void meshSubmit(const Mesh* _mesh, const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices = 1);
  121. /// bgfx::RendererType::Enum to name.
  122. bx::StringView getName(bgfx::RendererType::Enum _type);
  123. /// Name to bgfx::RendererType::Enum.
  124. bgfx::RendererType::Enum getType(const bx::StringView& _name);
  125. #endif // BGFX_UTILS_H_HEADER_GUARD