bgfx_utils.h 3.6 KB

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