bgfx_utils.h 3.9 KB

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