Skip to content

Commit 54d7fce

Browse files
author
Carmelo
committed
WIP: Introduce the lighting pass betwee the G-Buffer and post-process passes
1 parent 28c8f3e commit 54d7fce

File tree

8 files changed

+74
-50
lines changed

8 files changed

+74
-50
lines changed

engine/gfx/renderer/RenderPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace rev::gfx
8888

8989
void RenderPass::setClearColor(const std::vector<Vec4f>& colors)
9090
{
91-
m_clearColor = true;
91+
m_clearColor = !colors.empty();
9292
m_clearColors.reserve(colors.size());
9393
m_clearColors.clear();
9494

engine/gfx/renderer/RenderPass.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ namespace rev::gfx
4242

4343
void begin(vk::CommandBuffer cmd, const math::Vec2u& targetSize);
4444

45+
void resetClearDepth() { m_clearZ = false; }
4546
void setClearDepth(float depth);
4647

48+
void resetClearColor() { m_clearColor = false; }
4749
void setClearColor(const std::vector<math::Vec4f>& colors);
48-
4950
void setClearColor(const math::Vec4f& c);
50-
5151
void setClearColor(const math::Vec3f& c);
5252

5353
void setColorTargets(const std::vector<const gfx::ImageBuffer*>& colorTargets);

engine/gfx/renderer/deferred/DeferredRenderer.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ namespace rev::gfx
9898

9999
// Update render passes
100100
m_gBufferPass->setDepthTarget(*m_zBuffer);
101-
m_gBufferPass->setColorTargets({ m_hdrLightBuffer.get() });
101+
m_gBufferPass->setColorTargets({ m_emissiveBuffer.get(), m_baseColorMetalnessBuffer.get(), m_normalPBRBuffer.get() });
102102

103103
gfx::DescriptorSetUpdate renderBufferUpdates(*m_postProDescriptors, 0);
104104
renderBufferUpdates.addImage("HDR Light", m_hdrLightBuffer);
@@ -201,6 +201,45 @@ namespace rev::gfx
201201
m_gBufferPass->end(cmd);
202202
}
203203

204+
//---------------------------------------------------------------------------------------------------------------------
205+
void DeferredRenderer::renderLightingPass()
206+
{
207+
m_lightingConstants.windowSize = math::Vec2f((float)m_windowSize.x(), (float)m_windowSize.y());
208+
m_lightingConstants.lightDir = m_frameConstants.lightDir;
209+
m_lightingConstants.lightColor = m_frameConstants.lightColor;
210+
211+
auto cmd = m_ctxt->getNewRenderCmdBuffer();
212+
cmd.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
213+
214+
m_ctxt->allocator().transitionImageLayout(cmd, m_emissiveBuffer->image(), vk::ImageLayout::eGeneral, vk::ImageLayout::eShaderReadOnlyOptimal, false);
215+
m_ctxt->allocator().transitionImageLayout(cmd, m_baseColorMetalnessBuffer->image(), vk::ImageLayout::eGeneral, vk::ImageLayout::eShaderReadOnlyOptimal, false);
216+
m_ctxt->allocator().transitionImageLayout(cmd, m_normalPBRBuffer->image(), vk::ImageLayout::eGeneral, vk::ImageLayout::eShaderReadOnlyOptimal, false);
217+
m_lightingPass->begin(
218+
cmd,
219+
m_windowSize,
220+
*m_hdrLightBuffer,
221+
nullptr,
222+
m_lightingDescriptors->getDescriptor(0));
223+
224+
m_lightingPass->pushConstants(cmd, m_lightingConstants);
225+
m_lightingPass->render(cmd);
226+
227+
m_lightingPass->end(cmd);
228+
229+
m_ctxt->allocator().transitionImageLayout(cmd, m_emissiveBuffer->image(), vk::ImageLayout::eShaderReadOnlyOptimal, vk::ImageLayout::eGeneral, false);
230+
m_ctxt->allocator().transitionImageLayout(cmd, m_baseColorMetalnessBuffer->image(), vk::ImageLayout::eShaderReadOnlyOptimal, vk::ImageLayout::eGeneral, false);
231+
m_ctxt->allocator().transitionImageLayout(cmd, m_normalPBRBuffer->image(), vk::ImageLayout::eShaderReadOnlyOptimal, vk::ImageLayout::eGeneral, false);
232+
233+
cmd.end();
234+
235+
vk::PipelineStageFlags waitFlags = {};
236+
vk::SubmitInfo submitInfo(
237+
0, nullptr, &waitFlags, // wait
238+
1, &cmd, // commands
239+
0, nullptr); // signal
240+
static_cast<VulkanCommandQueue&>(m_ctxt->GfxQueue()).nativeQueue().submit(submitInfo);
241+
}
242+
204243
//---------------------------------------------------------------------------------------------------------------------
205244
void DeferredRenderer::renderPostProPass()
206245
{
@@ -219,7 +258,7 @@ namespace rev::gfx
219258
cmd,
220259
m_windowSize,
221260
swapchainImage,
222-
m_postProConstants.ambientColor,
261+
&m_postProConstants.ambientColor,
223262
m_postProDescriptors->getDescriptor(0));
224263

225264
m_postPass->pushConstants(cmd, m_postProConstants);
@@ -426,12 +465,20 @@ namespace rev::gfx
426465
auto windowSize = m_ctxt->windowSize();
427466
auto& alloc = m_ctxt->allocator();
428467

468+
m_emissiveBuffer = alloc.createImageBuffer(
469+
"HDR light",
470+
windowSize,
471+
m_HDRFormat,
472+
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eStorage,
473+
m_ctxt->graphicsQueueFamily());
474+
429475
m_hdrLightBuffer = alloc.createImageBuffer(
430476
"HDR light",
431477
windowSize,
432478
m_HDRFormat,
433479
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eStorage,
434480
m_ctxt->graphicsQueueFamily());
481+
435482
m_zBuffer = alloc.createDepthBuffer(
436483
"Depth",
437484
windowSize,
@@ -462,6 +509,7 @@ namespace rev::gfx
462509
void DeferredRenderer::destroyRenderTargets()
463510
{
464511
m_hdrLightBuffer = nullptr;
512+
m_emissiveBuffer = nullptr;
465513
m_zBuffer = nullptr;
466514
m_normalPBRBuffer = nullptr;
467515
m_normalPBRBuffer = nullptr;

engine/gfx/renderer/deferred/DeferredRenderer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ namespace rev::gfx
8181
void loadIBLLUT();
8282

8383
void renderGeometryPass(SceneDesc& scene);
84+
void renderLightingPass();
8485
void renderPostProPass();
8586

8687
// Init ImGui
@@ -113,6 +114,7 @@ namespace rev::gfx
113114
std::unique_ptr<gfx::RasterPipeline> m_postPipeline;
114115

115116
std::shared_ptr<gfx::ImageBuffer> m_hdrLightBuffer;
117+
std::shared_ptr<gfx::ImageBuffer> m_emissiveBuffer;
116118
std::shared_ptr<gfx::ImageBuffer> m_baseColorMetalnessBuffer;
117119
std::shared_ptr<gfx::ImageBuffer> m_normalPBRBuffer;
118120
std::shared_ptr<gfx::ImageBuffer> m_zBuffer;
@@ -138,9 +140,12 @@ namespace rev::gfx
138140

139141
struct LightingPushConstants
140142
{
143+
uint32_t renderFlags;
144+
math::Vec2f windowSize;
141145
math::Vec3f lightDir;
142146
math::Vec3f lightColor;
143-
};
147+
math::Vec3f ambientColor;
148+
} m_lightingConstants;
144149

145150
struct PostProPushConstants
146151
{

engine/gfx/renderer/renderPass/fullScreenPass.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ namespace rev::gfx
7878
const vk::CommandBuffer cmd,
7979
const math::Vec2u& targetSize,
8080
const ImageBuffer& colorTarget,
81-
const math::Vec3f& clearColor,
81+
const math::Vec3f* clearColor,
8282
vk::DescriptorSet descriptor)
8383
{
8484
m_renderPass->setColorTarget(colorTarget);
85-
m_renderPass->setClearColor(clearColor);
85+
if (clearColor)
86+
m_renderPass->setClearColor(*clearColor);
87+
else
88+
m_renderPass->resetClearColor();
8689

8790
RenderContextVk().allocator().transitionImageLayout(cmd,
8891
colorTarget.image(),

engine/gfx/renderer/renderPass/fullScreenPass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace rev::gfx {
4444
const vk::CommandBuffer cmd,
4545
const math::Vec2u& targetSize,
4646
const ImageBuffer& colorTargets,
47-
const math::Vec3f& clearColor,
47+
const math::Vec3f* clearColor,
4848
vk::DescriptorSet descriptor);
4949

5050
template<class PushConstants>

samples/ShaderToy/ShaderToy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ namespace rev
177177
cmd,
178178
RenderContext().windowSize(),
179179
RenderContextVk().swapchainAquireNextImage(m_imageAvailableSemaphore, cmd),
180-
Vec3f(0),
180+
nullptr,
181181
m_descSets->getDescriptor(0));
182182

183183
m_fullScreenFilter->pushConstants(cmd, m_frameConstants);

samples/pluto/shaders/lighting.frag

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "pbr.glsl"
2727

2828
layout(location = 0) in vec2 vPxlTexCoord;
29-
3029
layout(location = 0) out vec4 outColor;
3130

3231
layout(set = 0, binding = 0, rgba32f) uniform image2D HDRLight;
@@ -37,10 +36,10 @@ layout(set = 0, binding = 3, r32f) uniform image2D depth;
3736
layout(push_constant,scalar) uniform Constants
3837
{
3938
uint renderFlags;
40-
vec3 ambientColor;
4139
vec2 windowSize;
42-
float exposure;
43-
float bloom;
40+
vec3 lightDir;
41+
vec3 lightColor;
42+
vec3 ambientColor;
4443
} pushC;
4544

4645
void main()
@@ -49,44 +48,13 @@ void main()
4948
pushC.windowSize.x * vPxlTexCoord.x,
5049
pushC.windowSize.y * vPxlTexCoord.y);
5150

52-
// Gather slight bloom
53-
vec4 hdrColor = vec4(0);
54-
int bloomWin = 0;
55-
ivec2 minPos = max(pixelPos-bloomWin, ivec2(0));
56-
ivec2 maxPos = min(pixelPos+bloomWin, ivec2(pushC.windowSize-1));
51+
// Read from the G-Buffer
52+
vec4 litColor = imageLoad(HDRLight, pixelPos);
5753

58-
//if(bloomWin == 0)
54+
if(litColor.w < 0) // composite with the sky
5955
{
60-
vec4 bloomColor = imageLoad(HDRLight, pixelPos);
61-
if(bloomColor.w < 0)
62-
{
63-
bloomColor = vec4(pushC.ambientColor,1);
64-
}
65-
hdrColor += bloomColor;
56+
litColor = vec4(pushC.ambientColor, 1);
6657
}
67-
if(bloomWin > 0)
68-
{
69-
for(int j = minPos.y; j <= maxPos.y; ++j)
70-
{
71-
for(int i = minPos.x; i <= maxPos.x; ++i)
72-
{
73-
if(ivec2(i,j) == pixelPos)
74-
continue;
75-
float distance = 100 * (float(abs(i-pixelPos.x) * abs(j-pixelPos.y))) / pushC.windowSize.y;
76-
float weight = exp(-distance);
7758

78-
vec4 bloomColor = imageLoad(HDRLight, ivec2(i, j));
79-
if(bloomColor.w < 0)
80-
{
81-
bloomColor = vec4(pushC.ambientColor,1);
82-
}
83-
vec4 delta = weight * pow(bloomColor, vec4(2));
84-
//delta = pow(delta, vec4(2));
85-
hdrColor += 0.001 * delta;
86-
}
87-
}
88-
}
89-
hdrColor *= pushC.exposure;
90-
vec4 toneMapped = hdrColor / (1 + hdrColor);
91-
outColor = toneMapped;
59+
outColor = litColor;
9260
}

0 commit comments

Comments
 (0)