Skip to content

Commit c7bcbbc

Browse files
author
Alin
committed
adapted the example with deferred rendering
1 parent 6764b43 commit c7bcbbc

16 files changed

+559
-42
lines changed

example/GraphicsSamples/extensions/include/NvGLUtils/NvGLSLProgram.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#ifndef NV_GLSL_PROGRAM_H
3636
#define NV_GLSL_PROGRAM_H
3737

38-
#include <NvSimpleTypes.h>
3938
#include "NV/NvPlatformGL.h"
39+
#include <NvSimpleTypes.h>
4040

4141
/// \file
4242
/// GLSL shader program wrapper
@@ -321,15 +321,15 @@ class NvGLSLProgram
321321
/// \param[in] m array of matrices
322322
/// \param[in] count number of values in array unform
323323
/// \param[in] transpose if true, the matrices are transposed on input
324-
void setUniformMatrix4fv(const GLchar *name, GLfloat *m, int32_t count=1, bool transpose=false);
324+
void setUniformMatrix4fv(const GLchar *name, const GLfloat *m, int32_t count=1, bool transpose=false);
325325

326326
/// Set matrix array program uniform array by index
327327
/// Assumes that the given shader is bound via #enable
328328
/// \param[in] index the index of the uniform
329329
/// \param[in] m array of matrices
330330
/// \param[in] count number of values in array unform
331331
/// \param[in] transpose if true, the matrices are transposed on input
332-
void setUniformMatrix4fv(GLint index, GLfloat *m, int32_t count=1, bool transpose=false);
332+
void setUniformMatrix4fv(GLint index, const GLfloat *m, int32_t count=1, bool transpose=false);
333333

334334
/// Returns the index containing the named vertex attribute
335335
/// \param[in] uniform the null-terminated string name of the attribute

example/GraphicsSamples/extensions/src/NvGLUtils/NvGLSLProgram.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
//
3333
//----------------------------------------------------------------------------------
3434
#include "NvGLUtils/NvGLSLProgram.h"
35-
#include "NvAssetLoader/NvAssetLoader.h"
3635
#include "NV/NvLogs.h"
36+
#include "NvAssetLoader/NvAssetLoader.h"
3737
#include <string>
3838

3939
bool NvGLSLProgram::ms_logAllMissing = false;
@@ -668,7 +668,7 @@ NvGLSLProgram::setUniform4fv(GLint index, const float *value, int32_t count)
668668
}
669669

670670
void
671-
NvGLSLProgram::setUniformMatrix4fv(const char *name, float *m, int32_t count, bool transpose)
671+
NvGLSLProgram::setUniformMatrix4fv(const char *name, const float *m, int32_t count, bool transpose)
672672
{
673673
GLint loc = getUniformLocation(name, false);
674674
if (loc >= 0) {
@@ -677,7 +677,7 @@ NvGLSLProgram::setUniformMatrix4fv(const char *name, float *m, int32_t count, bo
677677
}
678678

679679
void
680-
NvGLSLProgram::setUniformMatrix4fv(GLint index, float *m, int32_t count, bool transpose)
680+
NvGLSLProgram::setUniformMatrix4fv(GLint index, const float *m, int32_t count, bool transpose)
681681
{
682682
if (index >= 0) {
683683
glUniformMatrix4fv(index, count, transpose, m);

example/ThreadedRenderingGL/Buffers.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ namespace Nv
6262
}
6363

6464
typedef cb::CommandBuffer<cb::DrawKey, cb::DefaultKeyDecoder, Nv::MaterialBinder> GeometryCommandBuffer;
65+
typedef cb::CommandBuffer<uint32_t,cb::DummyKeyDecoder<uint32_t>> DeferredCommandBuffer;

example/ThreadedRenderingGL/Commands.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,28 @@ namespace cmds
138138
cmd.vbo->EndUpdate();
139139
}
140140

141-
const cb::RenderContext::function_t WaitFenceCommand::kDispatchFunction = &waitFenceCommand;
141+
void clearRenderTarget(const void* data, cb::RenderContext* rc)
142+
{
143+
auto& cmd = *reinterpret_cast<const ClearRenderTarget*>(data);
144+
145+
GLenum drawBuffers[GL_MAX_COLOR_ATTACHMENTS];
146+
for (int i = 0; i < cmd.bufferCount; ++i)
147+
{
148+
drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
149+
}
150+
glDrawBuffers(cmd.bufferCount, drawBuffers);
151+
152+
float clearColorZero[4] = { 0.f, 0.f, 0.f, 0.f };
153+
for (int i = 0; i < cmd.bufferCount; ++i)
154+
glClearBufferfv(GL_COLOR, i, clearColorZero);
155+
156+
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
157+
}
158+
159+
const cb::RenderContext::function_t WaitFenceCommand::kDispatchFunction = &waitFenceCommand;
142160
const cb::RenderContext::function_t VboPoolUpdateCommand::kDispatchFunction = &vboPoolUpdateCommand;
143161
const cb::RenderContext::function_t DrawSkyboxCommand::kDispatchFunction = &drawSkyboxCommand;
144162
const cb::RenderContext::function_t DrawGroundCommand::kDispatchFunction = &drawGroundCommand;
145163
const cb::RenderContext::function_t VboUpdate::kDispatchFunction = &vboUpdate;
164+
const cb::RenderContext::function_t ClearRenderTarget::kDispatchFunction = &clearRenderTarget;
146165
}

example/ThreadedRenderingGL/Commands.h

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ namespace cmds
6868
Nv::NvSharedVBOGL* vbo;
6969
const void* data;
7070
size_t size;
71+
};
72+
73+
struct ClearRenderTarget
74+
{
75+
static const cb::RenderContext::function_t kDispatchFunction;
76+
77+
int bufferCount;
7178
};
7279

7380
} // namespace cmd

example/ThreadedRenderingGL/ThreadedRenderingGL.cpp

+139-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "ThreadedRenderingGL.h"
3535

3636
#include "Commands.h"
37+
#include "cmds/GLCommands.h"
3738

3839
#include "NvAppBase/NvFramerateCounter.h"
3940
#include "NvAppBase/NvInputHandler_CameraFly.h"
@@ -403,6 +404,37 @@ void ThreadedRenderingGL::helperJobFunction()
403404
CB_DEBUG_COMMAND_TAG(cmd);
404405
}
405406

407+
{
408+
cb::DrawKey key = cb::DrawKey::makeCustom(cb::ViewLayerType::eHighest, 10);
409+
auto& cmd = *m_geometryCommands.addCommand<cmds::BindFramebuffer>(key);
410+
cmd.target = GL_FRAMEBUFFER;
411+
cmd.fbo = m_texGBufferFboId;
412+
CB_DEBUG_COMMAND_TAG(cmd);
413+
414+
auto& clearCmd = *m_geometryCommands.appendCommand<cmds::ClearRenderTarget>(&cmd);
415+
clearCmd.bufferCount = GBUFFER_COUNT;
416+
CB_DEBUG_COMMAND_SET_MSG(clearCmd, "Clear GBUffer");
417+
}
418+
419+
//deferred commands
420+
{
421+
auto& cmd = *m_deferredCommands.addCommand<BeginDeferredCommand>(100);
422+
cmd.mainFboId = getMainFBO();
423+
CB_DEBUG_COMMAND_TAG(cmd);
424+
425+
auto& drawCmd = *m_deferredCommands.appendCommand<DrawDirectionalLightCommand>(&cmd);
426+
drawCmd.brdf = m_brdf;
427+
drawCmd.shader = m_shader_DirectionalLight;
428+
drawCmd.fullscreenMVP = nv::matrix4f(); // identity
429+
drawCmd.lightingUBO_Id = m_lightingUBO_Id;
430+
drawCmd.lightingUBO_Location = m_lightingUBO_Location;
431+
drawCmd.projUBO_Id = m_projUBO_Id;
432+
drawCmd.projUBO_Location = m_projUBO_Location;
433+
for (int i = 0; i < GBUFFER_COUNT; ++i)
434+
drawCmd.texGBuffer[i] = m_texGBuffer[i];
435+
CB_DEBUG_COMMAND_TAG(drawCmd);
436+
}
437+
406438
updateStats();
407439

408440
signalWorkComplete(1);
@@ -514,6 +546,11 @@ ThreadedRenderingGL::ThreadedRenderingGL() :
514546
m_meanGPUFrameMS(0.0f),
515547
m_frameID(0)
516548
{
549+
m_imageWidth = m_imageHeight = 0;
550+
m_texGBuffer[2] = m_texGBuffer[1] = m_texGBuffer[0] = 0;
551+
m_texGBufferFboId = m_texDepthStencilBuffer = 0;
552+
m_brdf = BRDF_LAMBERT;
553+
517554
cb::DrawKey::sanityChecks();
518555
m_geometryCommands.resize(10000, 3 * 1024);
519556
#if STRESS_TEST
@@ -639,8 +676,19 @@ void ThreadedRenderingGL::initRendering(void)
639676
m_shader_Skybox = NvGLSLProgram::createFromFiles("src_shaders/skyboxcolor_VS.glsl", "src_shaders/skyboxcolor_FS.glsl");
640677
m_shader_Fish = NvGLSLProgram::createFromFiles("src_shaders/staticfish_VS.glsl", "src_shaders/staticfish_FS.glsl");
641678

679+
{
680+
int32_t len;
681+
char* lightingVSSrc = NvAssetLoaderRead("src_shaders/Lighting_VS.glsl", len);
682+
683+
char* lightingFSSrc[2];
684+
lightingFSSrc[0] = NvAssetLoaderRead("src_shaders/Lighting_FS_Shared.h", len);
685+
lightingFSSrc[1] = NvAssetLoaderRead("src_shaders/DirectionalLighting_FS.glsl", len);
686+
m_shader_DirectionalLight = NvGLSLProgram::createFromStrings((const char**)(&lightingVSSrc), 1, (const char**)lightingFSSrc, 2, false);
687+
}
688+
642689
if ((nullptr == m_shader_GroundPlane) ||
643690
(nullptr == m_shader_Skybox) ||
691+
(nullptr == m_shader_DirectionalLight) ||
644692
(nullptr == m_shader_Fish))
645693
{
646694
showDialog("Fatal: Cannot Find Assets", "The shader assets cannot be loaded.\n"
@@ -677,6 +725,7 @@ void ThreadedRenderingGL::initRendering(void)
677725
initThreads();
678726
#if CB_DEBUG_COMMANDS_PRINT
679727
m_geometryCommands.setLogFunction(&commandLogFunction);
728+
m_deferredCommands.setLogFunction(&commandLogFunction);
680729
#endif
681730

682731
const auto key = cb::DrawKey::makeCustom(cb::ViewLayerType::eHighest, 0);
@@ -707,8 +756,8 @@ void ThreadedRenderingGL::initGL()
707756

708757
// Assign some values which apply to the entire scene and update once per frame.
709758
m_lightingUBO_Data.m_lightPosition = nv::vec4f(1.0f, 1.0f, 1.0f, 0.0f);
710-
m_lightingUBO_Data.m_lightAmbient = nv::vec4f(0.4f, 0.4f, 0.4f, 1.0f);
711-
m_lightingUBO_Data.m_lightDiffuse = nv::vec4f(0.7f, 0.7f, 0.7f, 1.0f);
759+
m_lightingUBO_Data.m_lightAmbient = nv::vec4f(0.2f, 0.2f, 0.2f, 1.0f);
760+
m_lightingUBO_Data.m_lightDiffuse = nv::vec4f(0.45f, 0.45f, 0.6f, 1.0f);
712761
m_lightingUBO_Data.m_causticOffset = m_currentTime * m_causticSpeed;
713762
m_lightingUBO_Data.m_causticTiling = m_causticTiling;
714763

@@ -1532,6 +1581,16 @@ void ThreadedRenderingGL::reshape(int32_t width, int32_t height)
15321581
{
15331582
glViewport(0, 0, NvSampleApp::m_width, NvSampleApp::m_height);
15341583

1584+
if (m_imageWidth != width || m_imageHeight != height)
1585+
{
1586+
m_imageWidth = width;
1587+
m_imageHeight = height;
1588+
1589+
// Destroy the current render targets and let them get recreated on our next draw
1590+
DestroyRenderTargets();
1591+
InitRenderTargets();
1592+
}
1593+
15351594
//setting the perspective projection matrix
15361595
nv::perspective(m_projUBO_Data.m_projectionMatrix, NV_PI / 3.0f,
15371596
static_cast<float>(NvSampleApp::m_width) /
@@ -1768,6 +1827,7 @@ void ThreadedRenderingGL::draw(void)
17681827
// Rendering
17691828
{
17701829
m_geometryCommands.sort();
1830+
m_deferredCommands.sort();
17711831

17721832
CPU_TIMER_SCOPE(CPU_TIMER_MAIN_CMD_BUILD);
17731833
GPU_TIMER_SCOPE();
@@ -1776,6 +1836,7 @@ void ThreadedRenderingGL::draw(void)
17761836

17771837
// nothing to pass as GL doesn't have any contexts
17781838
m_geometryCommands.submit(nullptr, clearCommands);
1839+
m_deferredCommands.submit(nullptr, clearCommands);
17791840
}
17801841
m_forceUpdateMode = ForceUpdateMode::eNone;
17811842
#if FISH_DEBUG
@@ -2126,12 +2187,88 @@ void ThreadedRenderingGL::buildFullStatsString(char* buffer, int32_t size)
21262187
}
21272188
}
21282189

2190+
2191+
void ThreadedRenderingGL::DestroyRenderTargets()
2192+
{
2193+
// Validating our render targets at the start of rendering each frame means that
2194+
// we can always destroy them when circumstances change, such as a resize event,
2195+
// and not have to immediately re-create them. This helps with resize events
2196+
// that happen multiple times between draw calls, which would otherwise have us
2197+
// recreating surfaces that would just be destroyed again before being used.
2198+
{
2199+
glDeleteFramebuffers(1, &m_texGBufferFboId);
2200+
m_texGBufferFboId = 0;
2201+
glDeleteTextures(1, &m_texDepthStencilBuffer);
2202+
m_texDepthStencilBuffer = 0;
2203+
glDeleteTextures(GBUFFER_COUNT, m_texGBuffer);
2204+
for(int i = 0; i < GBUFFER_COUNT; ++i)
2205+
m_texGBuffer[i] = 0;
2206+
}
2207+
}
2208+
2209+
void ThreadedRenderingGL::InitRenderTargets()
2210+
{
2211+
glDisable(GL_MULTISAMPLE);
2212+
2213+
// Create textures for the G-Buffer
2214+
{
2215+
glGenTextures(GBUFFER_COUNT, m_texGBuffer);
2216+
// Ensure that the unused entry in our array is 0 so that it doesn't cause problems when deleting
2217+
m_texGBuffer[GBUFFER_COUNT] = 0;
2218+
2219+
// Buffer 0 holds Normals and Depth
2220+
glBindTexture(GL_TEXTURE_RECTANGLE, m_texGBuffer[0]);
2221+
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA16F, m_imageWidth, m_imageHeight, 0, GL_RGBA, GL_FLOAT, 0);
2222+
2223+
// Buffer 1 holds Color
2224+
glBindTexture(GL_TEXTURE_RECTANGLE, m_texGBuffer[1]);
2225+
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA16F, m_imageWidth, m_imageHeight, 0, GL_RGBA, GL_FLOAT, 0);
2226+
}
2227+
2228+
// Create the Depth/Stencil
2229+
glGenTextures(1, &m_texDepthStencilBuffer);
2230+
glBindTexture(GL_TEXTURE_RECTANGLE, m_texDepthStencilBuffer);
2231+
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_DEPTH24_STENCIL8, m_imageWidth, m_imageHeight, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0);
2232+
2233+
glBindTexture(GL_TEXTURE_RECTANGLE, 0);
2234+
2235+
glGenFramebuffers(1, &m_texGBufferFboId);
2236+
glBindFramebuffer(GL_FRAMEBUFFER, m_texGBufferFboId);
2237+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, m_texGBuffer[0], 0);
2238+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_RECTANGLE, m_texGBuffer[1], 0);
2239+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_RECTANGLE, m_texDepthStencilBuffer, 0);
2240+
}
2241+
21292242
//-----------------------------------------------------------------------------
21302243

2244+
void ThreadedRenderingGL::DrawDirectionalLightCommand::execute(const void* data, cb::RenderContext* rc)
2245+
{
2246+
auto& cmd = *reinterpret_cast<const DrawDirectionalLightCommand*>(data);
2247+
2248+
glDisable(GL_DEPTH_TEST);
2249+
glDisable(GL_STENCIL_TEST);
2250+
2251+
cmd.shader->enable();
2252+
2253+
glBindBufferBase(GL_UNIFORM_BUFFER, cmd.projUBO_Location, cmd.projUBO_Id);
2254+
glBindBufferBase(GL_UNIFORM_BUFFER, cmd.lightingUBO_Location, cmd.lightingUBO_Id);
2255+
2256+
cmd.shader->setUniform1i("uLightingModel", cmd.brdf);
2257+
cmd.shader->bindTextureRect("uNormalDepth", 0, cmd.texGBuffer[0]);
2258+
cmd.shader->bindTextureRect("uDiffuseRoughness", 1, cmd.texGBuffer[1]);
2259+
2260+
cmd.shader->setUniformMatrix4fv("uModelViewMatrix", cmd.fullscreenMVP._array);
2261+
NvDrawQuadGL(0);
2262+
2263+
cmd.shader->disable();
2264+
}
2265+
21312266
const cb::RenderContext::function_t ThreadedRenderingGL::InitializeCommand::kDispatchFunction = &ThreadedRenderingGL::InitializeCommand::execute;
21322267
const cb::RenderContext::function_t ThreadedRenderingGL::BeginFrameCommand::kDispatchFunction = &ThreadedRenderingGL::BeginFrameCommand::execute;
21332268
const cb::RenderContext::function_t ThreadedRenderingGL::EndFrameCommand::kDispatchFunction = &ThreadedRenderingGL::EndFrameCommand::execute;
21342269
const cb::RenderContext::function_t ThreadedRenderingGL::SetVBOPolicyCommand::kDispatchFunction = &ThreadedRenderingGL::SetVBOPolicyCommand::execute;
2270+
const cb::RenderContext::function_t ThreadedRenderingGL::BeginDeferredCommand::kDispatchFunction = &ThreadedRenderingGL::BeginDeferredCommand::execute;
2271+
const cb::RenderContext::function_t ThreadedRenderingGL::DrawDirectionalLightCommand::kDispatchFunction = &ThreadedRenderingGL::DrawDirectionalLightCommand::execute;
21352272

21362273
//-----------------------------------------------------------------------------
21372274
// FUNCTION NEEDED BY THE FRAMEWORK

0 commit comments

Comments
 (0)