34
34
#include " ThreadedRenderingGL.h"
35
35
36
36
#include " Commands.h"
37
+ #include " cmds/GLCommands.h"
37
38
38
39
#include " NvAppBase/NvFramerateCounter.h"
39
40
#include " NvAppBase/NvInputHandler_CameraFly.h"
@@ -403,6 +404,37 @@ void ThreadedRenderingGL::helperJobFunction()
403
404
CB_DEBUG_COMMAND_TAG (cmd);
404
405
}
405
406
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
+
406
438
updateStats ();
407
439
408
440
signalWorkComplete (1 );
@@ -514,6 +546,11 @@ ThreadedRenderingGL::ThreadedRenderingGL() :
514
546
m_meanGPUFrameMS(0 .0f ),
515
547
m_frameID(0 )
516
548
{
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
+
517
554
cb::DrawKey::sanityChecks ();
518
555
m_geometryCommands.resize (10000 , 3 * 1024 );
519
556
#if STRESS_TEST
@@ -639,8 +676,19 @@ void ThreadedRenderingGL::initRendering(void)
639
676
m_shader_Skybox = NvGLSLProgram::createFromFiles (" src_shaders/skyboxcolor_VS.glsl" , " src_shaders/skyboxcolor_FS.glsl" );
640
677
m_shader_Fish = NvGLSLProgram::createFromFiles (" src_shaders/staticfish_VS.glsl" , " src_shaders/staticfish_FS.glsl" );
641
678
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
+
642
689
if ((nullptr == m_shader_GroundPlane) ||
643
690
(nullptr == m_shader_Skybox) ||
691
+ (nullptr == m_shader_DirectionalLight) ||
644
692
(nullptr == m_shader_Fish))
645
693
{
646
694
showDialog (" Fatal: Cannot Find Assets" , " The shader assets cannot be loaded.\n "
@@ -677,6 +725,7 @@ void ThreadedRenderingGL::initRendering(void)
677
725
initThreads ();
678
726
#if CB_DEBUG_COMMANDS_PRINT
679
727
m_geometryCommands.setLogFunction (&commandLogFunction);
728
+ m_deferredCommands.setLogFunction (&commandLogFunction);
680
729
#endif
681
730
682
731
const auto key = cb::DrawKey::makeCustom (cb::ViewLayerType::eHighest, 0 );
@@ -707,8 +756,8 @@ void ThreadedRenderingGL::initGL()
707
756
708
757
// Assign some values which apply to the entire scene and update once per frame.
709
758
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 );
712
761
m_lightingUBO_Data.m_causticOffset = m_currentTime * m_causticSpeed;
713
762
m_lightingUBO_Data.m_causticTiling = m_causticTiling;
714
763
@@ -1532,6 +1581,16 @@ void ThreadedRenderingGL::reshape(int32_t width, int32_t height)
1532
1581
{
1533
1582
glViewport (0 , 0 , NvSampleApp::m_width, NvSampleApp::m_height);
1534
1583
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
+
1535
1594
// setting the perspective projection matrix
1536
1595
nv::perspective (m_projUBO_Data.m_projectionMatrix , NV_PI / 3 .0f ,
1537
1596
static_cast <float >(NvSampleApp::m_width) /
@@ -1768,6 +1827,7 @@ void ThreadedRenderingGL::draw(void)
1768
1827
// Rendering
1769
1828
{
1770
1829
m_geometryCommands.sort ();
1830
+ m_deferredCommands.sort ();
1771
1831
1772
1832
CPU_TIMER_SCOPE (CPU_TIMER_MAIN_CMD_BUILD);
1773
1833
GPU_TIMER_SCOPE ();
@@ -1776,6 +1836,7 @@ void ThreadedRenderingGL::draw(void)
1776
1836
1777
1837
// nothing to pass as GL doesn't have any contexts
1778
1838
m_geometryCommands.submit (nullptr , clearCommands);
1839
+ m_deferredCommands.submit (nullptr , clearCommands);
1779
1840
}
1780
1841
m_forceUpdateMode = ForceUpdateMode::eNone;
1781
1842
#if FISH_DEBUG
@@ -2126,12 +2187,88 @@ void ThreadedRenderingGL::buildFullStatsString(char* buffer, int32_t size)
2126
2187
}
2127
2188
}
2128
2189
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
+
2129
2242
// -----------------------------------------------------------------------------
2130
2243
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
+
2131
2266
const cb::RenderContext::function_t ThreadedRenderingGL::InitializeCommand::kDispatchFunction = &ThreadedRenderingGL::InitializeCommand::execute;
2132
2267
const cb::RenderContext::function_t ThreadedRenderingGL::BeginFrameCommand::kDispatchFunction = &ThreadedRenderingGL::BeginFrameCommand::execute;
2133
2268
const cb::RenderContext::function_t ThreadedRenderingGL::EndFrameCommand::kDispatchFunction = &ThreadedRenderingGL::EndFrameCommand::execute;
2134
2269
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;
2135
2272
2136
2273
// -----------------------------------------------------------------------------
2137
2274
// FUNCTION NEEDED BY THE FRAMEWORK
0 commit comments