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