-
Notifications
You must be signed in to change notification settings - Fork 27
/
Renderer.cpp
285 lines (236 loc) · 9.33 KB
/
Renderer.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "Renderer.h"
#include "Scene/Scene.h"
#include <bigg.hpp>
#include <bx/macros.h>
#include <bx/string.h>
#include <bx/math.h>
#include <glm/common.hpp>
#include <glm/gtx/component_wise.hpp>
#include <glm/gtc/color_space.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/matrix_operation.hpp>
bgfx::VertexLayout Renderer::PosVertex::layout;
Renderer::Renderer(const Scene* scene) : scene(scene) { }
void Renderer::initialize()
{
PosVertex::init();
blitSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
camPosUniform = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
normalMatrixUniform = bgfx::createUniform("u_normalMatrix", bgfx::UniformType::Mat3);
exposureVecUniform = bgfx::createUniform("u_exposureVec", bgfx::UniformType::Vec4);
tonemappingModeVecUniform = bgfx::createUniform("u_tonemappingModeVec", bgfx::UniformType::Vec4);
// triangle used for blitting
constexpr float BOTTOM = -1.0f, TOP = 3.0f, LEFT = -1.0f, RIGHT = 3.0f;
const PosVertex vertices[3] = { { LEFT, BOTTOM, 0.0f }, { RIGHT, BOTTOM, 0.0f }, { LEFT, TOP, 0.0f } };
blitTriangleBuffer = bgfx::createVertexBuffer(bgfx::copy(&vertices, sizeof(vertices)), PosVertex::layout);
char vsName[128], fsName[128];
bx::snprintf(vsName, BX_COUNTOF(vsName), "%s%s", shaderDir(), "vs_tonemap.bin");
bx::snprintf(fsName, BX_COUNTOF(fsName), "%s%s", shaderDir(), "fs_tonemap.bin");
blitProgram = bigg::loadProgram(vsName, fsName);
pbr.initialize();
pbr.generateAlbedoLUT();
lights.initialize();
onInitialize();
// finish any queued precomputations before rendering the scene
bgfx::frame();
}
void Renderer::reset(uint16_t width, uint16_t height)
{
if(!bgfx::isValid(frameBuffer))
{
frameBuffer = createFrameBuffer(true, true);
bgfx::setName(frameBuffer, "Render framebuffer (pre-postprocessing)");
}
this->width = width;
this->height = height;
onReset();
}
void Renderer::render(float dt)
{
time += dt;
if(scene->loaded)
{
glm::vec4 camPos = glm::vec4(scene->camera.position(), 1.0f);
bgfx::setUniform(camPosUniform, glm::value_ptr(camPos));
glm::vec3 linear = pbr.whiteFurnaceEnabled
? glm::vec3(PBRShader::WHITE_FURNACE_RADIANCE)
: glm::convertSRGBToLinear(scene->skyColor); // tonemapping expects linear colors
glm::u8vec3 result = glm::u8vec3(glm::round(glm::clamp(linear, 0.0f, 1.0f) * 255.0f));
clearColor = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | 255;
}
else
clearColor = 0x303030FF; // gray
onRender(dt);
blitToScreen(MAX_VIEW);
// bigg doesn't do this
bgfx::setViewName(MAX_VIEW + 1, "imgui");
}
void Renderer::shutdown()
{
onShutdown();
pbr.shutdown();
lights.shutdown();
bgfx::destroy(blitProgram);
bgfx::destroy(blitSampler);
bgfx::destroy(camPosUniform);
bgfx::destroy(normalMatrixUniform);
bgfx::destroy(exposureVecUniform);
bgfx::destroy(tonemappingModeVecUniform);
bgfx::destroy(blitTriangleBuffer);
if(bgfx::isValid(frameBuffer))
bgfx::destroy(frameBuffer);
blitProgram = BGFX_INVALID_HANDLE;
blitSampler = camPosUniform = normalMatrixUniform = exposureVecUniform = tonemappingModeVecUniform =
BGFX_INVALID_HANDLE;
blitTriangleBuffer = BGFX_INVALID_HANDLE;
frameBuffer = BGFX_INVALID_HANDLE;
for(bgfx::ViewId i = 0; i < MAX_VIEW; i++)
{
bgfx::resetView(i);
}
bgfx::discard();
}
void Renderer::setVariable(const std::string& name, const std::string& val)
{
variables[name] = val;
}
void Renderer::setTonemappingMode(TonemappingMode mode)
{
tonemappingMode = mode;
}
void Renderer::setMultipleScattering(bool enabled)
{
pbr.multipleScatteringEnabled = enabled;
}
void Renderer::setWhiteFurnace(bool enabled)
{
pbr.whiteFurnaceEnabled = enabled;
}
bool Renderer::supported()
{
const bgfx::Caps* caps = bgfx::getCaps();
return
// SDR color attachment
(caps->formats[bgfx::TextureFormat::BGRA8] & BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER) != 0 &&
// HDR color attachment
(caps->formats[bgfx::TextureFormat::RGBA16F] & BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER) != 0;
}
void Renderer::setViewProjection(bgfx::ViewId view)
{
// view matrix
viewMat = scene->camera.matrix();
// projection matrix
bx::mtxProj(glm::value_ptr(projMat),
scene->camera.fov,
float(width) / height,
scene->camera.zNear,
scene->camera.zFar,
bgfx::getCaps()->homogeneousDepth,
bx::Handness::Left);
bgfx::setViewTransform(view, glm::value_ptr(viewMat), glm::value_ptr(projMat));
}
void Renderer::setNormalMatrix(const glm::mat4& modelMat)
{
// usually the normal matrix is based on the model view matrix
// but shading is done in world space (not eye space) so it's just the model matrix
//glm::mat4 modelViewMat = viewMat * modelMat;
// if we don't do non-uniform scaling, the normal matrix is the same as the model-view matrix
// (only the magnitude of the normal is changed, but we normalize either way)
//glm::mat3 normalMat = glm::mat3(modelMat);
// use adjugate instead of inverse
// see https://github.com/graphitemaster/normals_revisited#the-details-of-transforming-normals
// cofactor is the transpose of the adjugate
glm::mat3 normalMat = glm::transpose(glm::adjugate(glm::mat3(modelMat)));
bgfx::setUniform(normalMatrixUniform, glm::value_ptr(normalMat));
}
void Renderer::blitToScreen(bgfx::ViewId view)
{
bgfx::setViewName(view, "Tonemapping");
bgfx::setViewClear(view, BGFX_CLEAR_NONE);
bgfx::setViewRect(view, 0, 0, width, height);
bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_CULL_CW);
bgfx::TextureHandle frameBufferTexture = bgfx::getTexture(frameBuffer, 0);
bgfx::setTexture(0, blitSampler, frameBufferTexture);
float exposureVec[4] = { scene->loaded ? scene->camera.exposure : 1.0f };
bgfx::setUniform(exposureVecUniform, exposureVec);
float tonemappingModeVec[4] = { (float)tonemappingMode };
bgfx::setUniform(tonemappingModeVecUniform, tonemappingModeVec);
bgfx::setVertexBuffer(0, blitTriangleBuffer);
bgfx::submit(view, blitProgram);
}
bgfx::TextureFormat::Enum Renderer::findDepthFormat(uint64_t textureFlags, bool stencil)
{
const bgfx::TextureFormat::Enum depthFormats[] = { bgfx::TextureFormat::D16, bgfx::TextureFormat::D32 };
const bgfx::TextureFormat::Enum depthStencilFormats[] = { bgfx::TextureFormat::D24S8 };
const bgfx::TextureFormat::Enum* formats = stencil ? depthStencilFormats : depthFormats;
size_t count = stencil ? BX_COUNTOF(depthStencilFormats) : BX_COUNTOF(depthFormats);
bgfx::TextureFormat::Enum depthFormat = bgfx::TextureFormat::Count;
for(size_t i = 0; i < count; i++)
{
if(bgfx::isTextureValid(0, false, 1, formats[i], textureFlags))
{
depthFormat = formats[i];
break;
}
}
assert(depthFormat != bgfx::TextureFormat::Enum::Count);
return depthFormat;
}
bgfx::FrameBufferHandle Renderer::createFrameBuffer(bool hdr, bool depth)
{
bgfx::TextureHandle textures[2];
uint8_t attachments = 0;
const uint64_t samplerFlags = BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_MIP_POINT |
BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP;
bgfx::TextureFormat::Enum format =
hdr ? bgfx::TextureFormat::RGBA16F : bgfx::TextureFormat::BGRA8; // BGRA is often faster (internal GPU format)
assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT | samplerFlags));
textures[attachments++] =
bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, format, BGFX_TEXTURE_RT | samplerFlags);
if(depth)
{
bgfx::TextureFormat::Enum depthFormat = findDepthFormat(BGFX_TEXTURE_RT_WRITE_ONLY | samplerFlags);
assert(depthFormat != bgfx::TextureFormat::Enum::Count);
textures[attachments++] = bgfx::createTexture2D(
bgfx::BackbufferRatio::Equal, false, 1, depthFormat, BGFX_TEXTURE_RT_WRITE_ONLY | samplerFlags);
}
bgfx::FrameBufferHandle fb = bgfx::createFrameBuffer(attachments, textures, true);
if(!bgfx::isValid(fb))
Log->warn("Failed to create framebuffer");
return fb;
}
const char* Renderer::shaderDir()
{
const char* path = "???";
switch(bgfx::getRendererType())
{
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D9:
path = "shaders/dx9/";
break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12:
path = "shaders/dx11/";
break;
case bgfx::RendererType::Gnm:
break;
case bgfx::RendererType::Metal:
path = "shaders/metal/";
break;
case bgfx::RendererType::OpenGL:
path = "shaders/glsl/";
break;
case bgfx::RendererType::OpenGLES:
path = "shaders/essl/";
break;
case bgfx::RendererType::Vulkan:
path = "shaders/spirv/";
break;
default:
assert(false);
break;
}
return path;
}