Skip to content

Commit 1a7f132

Browse files
committed
Generate random lights
1 parent 31eefcb commit 1a7f132

19 files changed

Lines changed: 208 additions & 112 deletions

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ set(SOURCES
3333
Scene/Mesh.cpp
3434
Scene/Material.h
3535
Scene/Light.h
36+
Scene/LightList.h
37+
Scene/LightList.cpp
3638
)
3739

3840
set(SHADERS

src/Cluster.cpp

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <bx/string.h>
1212
#include <bimg/bimg.h>
1313
#include <glm/gtc/type_ptr.hpp>
14+
#include <glm/gtx/component_wise.hpp>
1415
#include <glm/gtc/random.hpp>
1516
#include <spdlog/sinks/basic_file_sink.h>
1617
#include <thread>
@@ -49,11 +50,33 @@ int Cluster::run(int argc, char* argv[])
4950

5051
void Cluster::initialize(int _argc, char* _argv[])
5152
{
52-
// is _mt (thread safe) necessary?
53-
spdlog::sink_ptr fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(config->logFile, true);
54-
fileSink->set_level(spdlog::level::trace);
55-
fileSink->set_pattern("[%H:%M:%S][%l] %v");
56-
Sinks->add_sink(fileSink);
53+
if(!ForwardRenderer::supported())
54+
{
55+
Log->error("Forward renderer not supported on this hardware");
56+
close();
57+
return;
58+
}
59+
if(!DeferredRenderer::supported())
60+
{
61+
Log->error("Deferred renderer not supported on this hardware");
62+
close();
63+
return;
64+
}
65+
if(!ClusteredRenderer::supported())
66+
{
67+
Log->error("Clustered renderer not supported on this hardware");
68+
close();
69+
return;
70+
}
71+
72+
if(config->writeLog)
73+
{
74+
// is _mt (thread safe) necessary?
75+
spdlog::sink_ptr fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(config->logFile, true);
76+
fileSink->set_level(spdlog::level::trace);
77+
fileSink->set_pattern("[%H:%M:%S][%l] %v");
78+
Sinks->add_sink(fileSink);
79+
}
5780

5881
Log->flush_on(spdlog::level::info);
5982
Log->set_level(spdlog::level::trace);
@@ -71,13 +94,6 @@ void Cluster::initialize(int _argc, char* _argv[])
7194
renderer->initialize();
7295
ui->initialize();
7396

74-
if(!ForwardRenderer::supported())
75-
Log->warn("Forward renderer not supported on this hardware");
76-
if(!DeferredRenderer::supported())
77-
Log->warn("Deferred renderer not supported on this hardware");
78-
if(!ClusteredRenderer::supported())
79-
Log->warn("Clustered renderer not supported on this hardware");
80-
8197
Scene::init();
8298
// TODO multithreaded
8399
// textures still have to be loaded from main thread
@@ -88,6 +104,17 @@ void Cluster::initialize(int _argc, char* _argv[])
88104
close();
89105
return;
90106
}
107+
108+
/*
109+
scene->pointLights.lights = {
110+
// pos, flux (color)
111+
{ { -5.0f, 1.1f, 0.0f }, { 1.0f, 0.0f, 1.0f } },
112+
{ { 0.0f, 1.1f, 0.0f }, { 0.0f, 1.0f, 1.0f } },
113+
{ { 5.0f, 1.1f, 0.0f }, { 1.0f, 1.0f, 0.0f } }
114+
};
115+
scene->pointLights.update();
116+
*/
117+
generateLights(config->lights);
91118
}
92119

93120
void Cluster::onReset()
@@ -358,3 +385,26 @@ void Cluster::saveFrameBuffer(bgfx::FrameBufferHandle frameBuffer, const char* n
358385
}
359386
}
360387
}
388+
389+
void Cluster::generateLights(unsigned int count)
390+
{
391+
// TODO normalize flux
392+
393+
auto& lights = scene->pointLights.lights;
394+
395+
size_t keep = lights.size();
396+
if(count < keep)
397+
keep = count;
398+
399+
lights.resize(count);
400+
401+
glm::vec3 scale = glm::abs(scene->maxBounds - scene->minBounds);
402+
403+
for(size_t i = keep; i < count; i++)
404+
{
405+
lights[i].position = glm::linearRand(-scale * glm::vec3(0.5f, 0.0f, 0.5f), glm::vec3(scale * 0.5f));
406+
lights[i].flux = glm::linearRand(glm::vec3(0.001f), glm::vec3(0.003f));
407+
}
408+
409+
scene->pointLights.update();
410+
}

src/Cluster.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Cluster : public bigg::Application
4646

4747
void saveFrameBuffer(bgfx::FrameBufferHandle frameBuffer, const char* path);
4848

49+
void generateLights(unsigned int count);
50+
4951
private:
5052
class BgfxCallbacks : public bgfx::CallbackI
5153
{

src/Config.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Config::Config() :
99
//sceneFile("assets/models/duck/Duck.gltf"),
1010
sceneFile("assets/models/Sponza/glTF/Sponza.gltf"),
1111
lights(1),
12+
maxLights(500),
13+
movingLights(false),
1214
fullscreen(false),
1315
showUI(true),
1416
showConfigWindow(true),
@@ -38,6 +40,4 @@ void Config::readArgv(int argc, char* argv[])
3840
showStatsOverlay = false;
3941
showLog = false;
4042
showBuffers = false;
41-
//fullscreen = true;
42-
lights = 50;
4343
}

src/Config.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Config
88
public:
99
Config();
1010

11+
void readArgv(int argc, char* argv[]);
12+
1113
// Log
1214

1315
bool writeLog; // not exposed to UI
@@ -24,21 +26,23 @@ class Config
2426

2527
const char* sceneFile; // not exposed to UI
2628
int lights;
29+
int maxLights; // not exposed to UI
30+
bool movingLights;
2731

2832
// UI
2933

3034
bool fullscreen;
3135
bool showUI;
3236
bool showConfigWindow;
3337
bool showLog;
38+
3439
bool showStatsOverlay;
3540
struct
3641
{
3742
bool fps;
3843
bool frameTime;
3944
bool gpuMemory;
4045
} overlays;
41-
bool showBuffers;
4246

43-
void readArgv(int argc, char* argv[]);
47+
bool showBuffers;
4448
};

src/Renderer/ForwardRenderer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <bigg.hpp>
55
#include <bx/string.h>
66
#include <glm/matrix.hpp>
7+
#include <glm/gtc/type_ptr.hpp>
78

89
ForwardRenderer::ForwardRenderer(const Scene* scene) :
910
Renderer(scene),
@@ -50,18 +51,17 @@ void ForwardRenderer::onRender(float dt)
5051

5152
uint64_t state = BGFX_STATE_DEFAULT & ~BGFX_STATE_CULL_MASK;
5253

53-
5454
for(const Mesh& mesh : scene->meshes)
5555
{
5656
glm::mat4 model = glm::mat4();
57-
bgfx::setTransform(&model[0][0]);
57+
bgfx::setTransform(glm::value_ptr(model));
5858
setNormalMatrix(model);
5959
bgfx::setVertexBuffer(0, mesh.vertexBuffer);
6060
bgfx::setIndexBuffer(mesh.indexBuffer);
6161
const Material& mat = scene->materials[mesh.material];
6262
uint64_t materialState = pbr.bindMaterial(mat);
63-
uint64_t lightState = lights.bindLights();
64-
bgfx::setState(state | lightState | materialState);
63+
uint64_t lightState = lights.bindLights(scene);
64+
bgfx::setState(state | materialState | lightState);
6565
bgfx::submit(vDefault, program);
6666
}
6767
}

src/Renderer/LightShader.cpp

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,35 @@
11
#include "LightShader.h"
22

3-
bgfx::VertexDecl LightShader::VertexVec3::decl;
3+
#include "Scene/Scene.h"
4+
#include <cassert>
45

56
LightShader::LightShader() :
6-
numLights(3),
7-
lightCountVecUniform(BGFX_INVALID_HANDLE),
8-
lightPosBuffer(BGFX_INVALID_HANDLE),
9-
lightFluxBuffer(BGFX_INVALID_HANDLE),
10-
lightPositions(nullptr),
11-
lightFluxs(nullptr)
7+
lightCountVecUniform(BGFX_INVALID_HANDLE)
128
{
139
}
1410

1511
void LightShader::initialize()
1612
{
17-
VertexVec3::init();
18-
1913
lightCountVecUniform = bgfx::createUniform("u_lightCountVec", bgfx::UniformType::Vec4);
20-
21-
lightPositions = new VertexVec3[numLights];
22-
lightFluxs = new VertexVec3[numLights];
23-
24-
lightPosBuffer = bgfx::createDynamicVertexBuffer(numLights, VertexVec3::decl, BGFX_BUFFER_COMPUTE_READ);
25-
lightFluxBuffer = bgfx::createDynamicVertexBuffer(numLights, VertexVec3::decl, BGFX_BUFFER_COMPUTE_READ);
26-
27-
lightPositions[0] = { -5.0f, 1.1f, 0.0f };
28-
lightPositions[1] = { 0.0f, 1.1f, 0.0f };
29-
lightPositions[2] = { 5.0f, 1.1f, 0.0f };
30-
31-
lightFluxs[1] = { 1.0f, 0.0f, 1.0f };
32-
lightFluxs[0] = { 0.0f, 1.0f, 1.0f };
33-
lightFluxs[2] = { 1.0f, 1.0f, 0.0f };
34-
35-
//uint32_t structsize = sizeof(VertexVec3);
36-
//uint32_t declsize = VertexVec3::decl.getStride();
37-
38-
bgfx::update(lightPosBuffer, 0, bgfx::makeRef(lightPositions, sizeof(VertexVec3) * numLights));
39-
bgfx::update(lightFluxBuffer, 0, bgfx::makeRef(lightFluxs, sizeof(VertexVec3) * numLights));
4014
}
4115

4216
void LightShader::shutdown()
4317
{
4418
bgfx::destroy(lightCountVecUniform);
45-
bgfx::destroy(lightPosBuffer);
46-
bgfx::destroy(lightFluxBuffer);
47-
48-
lightPosBuffer = lightFluxBuffer = BGFX_INVALID_HANDLE;
4919
lightCountVecUniform = BGFX_INVALID_HANDLE;
50-
51-
delete[] lightPositions;
52-
delete[] lightFluxs;
53-
lightPositions = lightFluxs = nullptr;
5420
}
5521

56-
uint64_t LightShader::bindLights()
22+
uint64_t LightShader::bindLights(const Scene* scene) const
5723
{
24+
assert(scene != nullptr);
25+
5826
// a 32-bit IEEE 754 float can represent all integers up to 2^24 (~16.7 million) correctly
5927
// should be enough for this use case (comparison in for loop)
60-
float lightCountVec[4] = { (float)numLights };
28+
float lightCountVec[4] = { (float)scene->pointLights.lights.size() };
6129
bgfx::setUniform(lightCountVecUniform, lightCountVec);
6230

63-
bgfx::setBuffer(SAMPLER_START, lightPosBuffer, bgfx::Access::Read);
64-
bgfx::setBuffer(SAMPLER_START + 1, lightFluxBuffer, bgfx::Access::Read);
31+
bgfx::setBuffer(SAMPLER_START, scene->pointLights.positionBuffer, bgfx::Access::Read);
32+
bgfx::setBuffer(SAMPLER_START + 1, scene->pointLights.fluxBuffer, bgfx::Access::Read);
6533

6634
return 0;
6735
}

src/Renderer/LightShader.h

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <bgfx/bgfx.h>
44
#include "Renderer/PBRShader.h"
55

6+
class Scene;
7+
68
class LightShader
79
{
810
public:
@@ -14,34 +16,8 @@ class LightShader
1416
void initialize();
1517
void shutdown();
1618

17-
uint64_t bindLights();
19+
uint64_t bindLights(const Scene* scene) const;
1820

1921
private:
2022
bgfx::UniformHandle lightCountVecUniform;
21-
bgfx::DynamicVertexBufferHandle lightPosBuffer;
22-
bgfx::DynamicVertexBufferHandle lightFluxBuffer;
23-
24-
struct VertexVec3
25-
{
26-
float x;
27-
float y;
28-
float z;
29-
// we only need 3 floats but the buffer values end up in funny ways when using 3
30-
// not sure if this is a problem with padding/stride on the CPU or if buffers need to be padded to vec4
31-
float padding;
32-
33-
static void init()
34-
{
35-
decl.begin()
36-
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
37-
.skip(sizeof(float))
38-
.end();
39-
}
40-
static bgfx::VertexDecl decl;
41-
};
42-
43-
uint32_t numLights;
44-
45-
VertexVec3* lightPositions;
46-
VertexVec3* lightFluxs;
4723
};

src/Renderer/Renderer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <glm/gtx/component_wise.hpp>
1010
#include <glm/gtc/color_space.hpp>
1111
#include <glm/gtc/matrix_transform.hpp>
12+
#include <glm/gtc/type_ptr.hpp>
1213

1314
bgfx::VertexDecl Renderer::PosTexCoord0Vertex::decl;
1415

@@ -139,7 +140,7 @@ void Renderer::setViewProjection(bgfx::ViewId view)
139140
// view matrix
140141
viewMat = scene->camera.matrix();
141142
// projection matrix
142-
bx::mtxProj(&projMat[0][0],
143+
bx::mtxProj(glm::value_ptr(projMat),
143144
scene->camera.fov,
144145
float(width) / height,
145146
scene->camera.zNear,
@@ -148,7 +149,7 @@ void Renderer::setViewProjection(bgfx::ViewId view)
148149

149150
glm::mat4 scaleMat = glm::scale(glm::mat4(), glm::vec3(scale));
150151
viewMat = scaleMat * viewMat;
151-
bgfx::setViewTransform(view, &viewMat[0][0], &projMat[0][0]);
152+
bgfx::setViewTransform(view, glm::value_ptr(viewMat), glm::value_ptr(projMat));
152153
}
153154

154155
void Renderer::setNormalMatrix(const glm::mat4& modelMat)
@@ -158,7 +159,7 @@ void Renderer::setNormalMatrix(const glm::mat4& modelMat)
158159
// it's enough to calculate the adjugate instead of the inverse, it always exists (requires GLM 0.9.9.3)
159160
//glm::mat3 normalMat = glm::transpose(glm::inverse(glm::mat3(modelViewMat)));
160161
glm::mat3 normalMat = modelViewMat;
161-
bgfx::setUniform(normalMatrixUniform, &normalMat[0][0]);
162+
bgfx::setUniform(normalMatrixUniform, glm::value_ptr(normalMat));
162163
}
163164

164165
void Renderer::blitToScreen(bgfx::ViewId view)
@@ -170,7 +171,7 @@ void Renderer::blitToScreen(bgfx::ViewId view)
170171
bgfx::setState(BGFX_STATE_WRITE_RGB);
171172
bgfx::TextureHandle frameBufferTexture = bgfx::getTexture(frameBuffer);
172173
bgfx::setTexture(0, blitSampler, frameBufferTexture);
173-
float exposure[4] = { scene->loaded ? scene->camera.exposure : 1.0f, 0.0f, 0.0f, 0.0f };
174+
float exposure[4] = { scene->loaded ? scene->camera.exposure : 1.0f };
174175
bgfx::setUniform(exposureVecUniform, &exposure[0]);
175176
bgfx::setVertexBuffer(0, quadVB);
176177
bgfx::submit(view, blitProgram);

src/Renderer/Shaders/fs_forward.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main()
2929
// normal map
3030

3131
vec3 normal = v_normal;
32-
//if(length(mat.normal) != 0.0)
32+
if(u_hasNormalTexture)
3333
{
3434
// convert normal map from tangent space -> eye space (= space of v_tangent, etc.)
3535
mat3 TBN = mtx3FromCols(

0 commit comments

Comments
 (0)