-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_utils.h
192 lines (159 loc) · 5.49 KB
/
gl_utils.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @author Ondøej Sabela
* @brief Realistic Atmosphere - Thesis implementation.
* @date 2021-2022
* Copyright 2022 Ondøej Sabela. All rights reserved.
* Uses ray tracing, path tracing and ray marching to create visually plausible outdoor scenes with atmosphere, terrain, clouds and analytical objects.
* This file is based on bgfx_utils.h from BGFX library.
*/
#pragma once
/*
* Original copyright:
* Copyright 2011-2021 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 <bx/string.h>
#include <bgfx/bgfx.h>
#include <bimg/bimg.h>
#include <bimg/decode.h>
#include <tinystl/allocator.h>
#include <tinystl/vector.h>
#include <string>
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;
}
/// bgfx::RendererType::Enum to name.
bx::StringView getName(bgfx::RendererType::Enum _type);
/// Name to bgfx::RendererType::Enum.
bgfx::RendererType::Enum getType(const bx::StringView& _name);
///
struct Args
{
Args(int _argc, const char* const* _argv);
bgfx::RendererType::Enum m_type;
uint16_t m_pciId;
};
namespace bgfx_utils
{
bgfx::DynamicIndexBufferHandle createDynamicComputeReadBuffer(
uint32_t _num
, uint16_t _flags = BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_ALLOW_RESIZE
)
{
return bgfx::createDynamicIndexBuffer(_num / 2 /* because BGFX expects 2-byte indices */, _flags);
}
static void imageReleaseCb(void* _ptr, void* _userData)
{
BX_UNUSED(_ptr);
bimg::ImageContainer* imageContainer = (bimg::ImageContainer*)_userData;
bimg::imageFree(imageContainer);
}
static void textureArrayRelease(void* _ptr, void* _userData)
{
BX_UNUSED(_ptr);
delete[] _userData;
}
static bgfx::TextureHandle createTextureArray(stl::vector<std::string> filePaths, uint64_t _flags)
{
assert(filePaths.size() > 0);
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
char* arrayData = nullptr;
bgfx::TextureFormat::Enum format;
uint16_t width;
uint16_t height;
uint16_t numLayers = 0;
uint32_t totalMemorySize = 0;
bool hasMips;
int i = 0;
do
{
auto filePath = filePaths[i];
uint32_t size;
void* currentImageData = load(filePath.c_str(), &size);
if (NULL != currentImageData)
{
bimg::ImageContainer* imageContainer = bimg::imageParse(entry::getAllocator(), currentImageData, size);
if (NULL != imageContainer)
{
unload(currentImageData);
auto thisFormat = bgfx::TextureFormat::Enum(imageContainer->m_format);
if (bgfx::isTextureValid(0, false, imageContainer->m_numLayers, thisFormat, _flags))
{
if (i == 0)//First texture sets the dimensions
{
width = imageContainer->m_width;
height = imageContainer->m_height;
format = thisFormat;
hasMips = 1 < imageContainer->m_numMips;
totalMemorySize = filePaths.size() * imageContainer->m_size;
arrayData = new char[totalMemorySize];
}
else
{
BX_ASSERT(width == imageContainer->m_width, "All images must have the same size. The first had width %d and the %dth has %d.", width, i+1, imageContainer->m_width);
BX_ASSERT(height == imageContainer->m_height, "All images must have the same size. The first had height %d and the %dth has %d.", height, i + 1, imageContainer->m_height);
BX_ASSERT(format == thisFormat, "All images must have the same format");
BX_ASSERT(hasMips == 1 < imageContainer->m_numMips, "All array images must have same mip format");
}
//Copy image to array
bx::memCopy(arrayData + i * imageContainer->m_size, imageContainer->m_data, imageContainer->m_size);
numLayers++;
}
bimg::imageFree(imageContainer);
}
}
} while (filePaths.size() > ++i);
const bgfx::Memory* mem = bgfx::makeRef(
arrayData, totalMemorySize, textureArrayRelease, arrayData
);
handle = bgfx::createTexture2D(width, height, hasMips, numLayers, format, _flags, mem);
if (bgfx::isValid(handle))
{
bgfx::setName(handle, filePaths[0].c_str());
}
return handle;
}
}
#endif // BGFX_UTILS_H_HEADER_GUARD