bgfx_utils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2011-2017 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. ///
  11. void* load(const char* _filePath, uint32_t* _size = NULL);
  12. ///
  13. void unload(void* _ptr);
  14. ///
  15. bgfx::ShaderHandle loadShader(const char* _name);
  16. ///
  17. bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName);
  18. ///
  19. bgfx::TextureHandle loadTexture(const char* _name, uint32_t _flags = BGFX_TEXTURE_NONE, uint8_t _skip = 0, bgfx::TextureInfo* _info = NULL);
  20. ///
  21. bimg::ImageContainer* imageLoad(const char* _filePath, bgfx::TextureFormat::Enum _dstFormat);
  22. ///
  23. void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices);
  24. /// Returns true if both internal transient index and vertex buffer have
  25. /// enough space.
  26. ///
  27. /// @param[in] _numVertices Number of vertices.
  28. /// @param[in] _decl Vertex declaration.
  29. /// @param[in] _numIndices Number of indices.
  30. ///
  31. inline bool checkAvailTransientBuffers(uint32_t _numVertices, const bgfx::VertexDecl& _decl, uint32_t _numIndices)
  32. {
  33. return _numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, _decl)
  34. && _numIndices == bgfx::getAvailTransientIndexBuffer(_numIndices)
  35. ;
  36. }
  37. ///
  38. inline uint32_t encodeNormalRgba8(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  39. {
  40. const float src[] =
  41. {
  42. _x * 0.5f + 0.5f,
  43. _y * 0.5f + 0.5f,
  44. _z * 0.5f + 0.5f,
  45. _w * 0.5f + 0.5f,
  46. };
  47. uint32_t dst;
  48. bx::packRgba8(&dst, src);
  49. return dst;
  50. }
  51. ///
  52. struct MeshState
  53. {
  54. struct Texture
  55. {
  56. uint32_t m_flags;
  57. bgfx::UniformHandle m_sampler;
  58. bgfx::TextureHandle m_texture;
  59. uint8_t m_stage;
  60. };
  61. Texture m_textures[4];
  62. uint64_t m_state;
  63. bgfx::ProgramHandle m_program;
  64. uint8_t m_numTextures;
  65. uint8_t m_viewId;
  66. };
  67. struct Mesh;
  68. ///
  69. Mesh* meshLoad(const char* _filePath);
  70. ///
  71. void meshUnload(Mesh* _mesh);
  72. ///
  73. MeshState* meshStateCreate();
  74. ///
  75. void meshStateDestroy(MeshState* _meshState);
  76. ///
  77. void meshSubmit(const Mesh* _mesh, uint8_t _id, bgfx::ProgramHandle _program, const float* _mtx, uint64_t _state = BGFX_STATE_MASK);
  78. ///
  79. void meshSubmit(const Mesh* _mesh, const MeshState*const* _state, uint8_t _numPasses, const float* _mtx, uint16_t _numMatrices = 1);
  80. ///
  81. struct Args
  82. {
  83. Args(int _argc, const char* const* _argv);
  84. bgfx::RendererType::Enum m_type;
  85. uint16_t m_pciId;
  86. };
  87. #endif // BGFX_UTILS_H_HEADER_GUARD