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