88#include " window.hpp"
99
1010#include < common/assert.hpp>
11+ #include < common/r_sequence.hpp>
1112
1213#include < algorithm>
1314#include < array>
@@ -107,7 +108,8 @@ DeferredRenderer::DeferredRenderer(
107108 mLightingPass(),
108109 mResolvePass(),
109110 mGbufferPassDurationsNs(),
110- mLightingPassDurationsNs()
111+ mLightingPassDurationsNs(),
112+ mFrameCount(0 )
111113{
112114 {
113115 const std::array<WGPUTextureFormat, 1 > depthFormats{
@@ -240,6 +242,7 @@ DeferredRenderer::DeferredRenderer(DeferredRenderer&& other)
240242 mResolvePass = std::move (other.mResolvePass );
241243 mGbufferPassDurationsNs = std::move (other.mGbufferPassDurationsNs );
242244 mLightingPassDurationsNs = std::move (other.mLightingPassDurationsNs );
245+ mFrameCount = other.mFrameCount ;
243246 }
244247}
245248
@@ -270,6 +273,7 @@ DeferredRenderer& DeferredRenderer::operator=(DeferredRenderer&& other)
270273 mResolvePass = std::move (other.mResolvePass );
271274 mGbufferPassDurationsNs = std::move (other.mGbufferPassDurationsNs );
272275 mLightingPassDurationsNs = std::move (other.mLightingPassDurationsNs );
276+ mFrameCount = other.mFrameCount ;
273277 }
274278 return *this ;
275279}
@@ -293,13 +297,25 @@ void DeferredRenderer::render(
293297 return wgpuDeviceCreateCommandEncoder (gpuContext.device , &cmdEncoderDesc);
294298 }();
295299
300+ const Extent2f framebufferSize = Extent2f (renderDesc.framebufferSize );
301+ const std::uint32_t frameCount = mFrameCount ++;
302+ const glm::mat4 jitterMat = [framebufferSize, frameCount]() -> glm::mat4 {
303+ glm::mat4 jitterMat = glm::mat4 (1 .0f );
304+ const glm::vec2 j = r2Sequence (frameCount, 1 << 20 );
305+ jitterMat[3 ][0 ] = (j.x - 0 .5f ) / framebufferSize.x ;
306+ jitterMat[3 ][1 ] = (j.y - 0 .5f ) / framebufferSize.y ;
307+ return jitterMat;
308+ }();
309+
310+ // GBuffer pass
311+
296312 wgpuCommandEncoderWriteTimestamp (
297313 encoder,
298314 mQuerySet ,
299315 offsetof (TimestampsLayout, gbufferPassStart) / TimestampsLayout::MEMBER_SIZE);
300316 mGbufferPass .render (
301317 gpuContext,
302- renderDesc.viewReverseZProjectionMatrix ,
318+ jitterMat * renderDesc.viewReverseZProjectionMatrix ,
303319 encoder,
304320 mDepthTextureView ,
305321 mAlbedoTextureView ,
@@ -309,27 +325,31 @@ void DeferredRenderer::render(
309325 mQuerySet ,
310326 offsetof (TimestampsLayout, gbufferPassEnd) / TimestampsLayout::MEMBER_SIZE);
311327
328+ // Lighting pass
329+
312330 wgpuCommandEncoderWriteTimestamp (
313331 encoder,
314332 mQuerySet ,
315333 offsetof (TimestampsLayout, lightingPassStart) / TimestampsLayout::MEMBER_SIZE);
316- const Extent2f framebufferSize = Extent2f (renderDesc.framebufferSize );
317334 {
318335 const glm::mat4 inverseViewProjectionMat =
319- glm::inverse (renderDesc.viewReverseZProjectionMatrix );
336+ glm::inverse (jitterMat * renderDesc.viewReverseZProjectionMatrix );
320337 mLightingPass .render (
321338 gpuContext,
322339 encoder,
323340 inverseViewProjectionMat,
324341 renderDesc.cameraPosition ,
325342 framebufferSize,
326- renderDesc.sky );
343+ renderDesc.sky ,
344+ frameCount);
327345 }
328346 wgpuCommandEncoderWriteTimestamp (
329347 encoder,
330348 mQuerySet ,
331349 offsetof (TimestampsLayout, lightingPassEnd) / TimestampsLayout::MEMBER_SIZE);
332350
351+ // Resolve pass
352+
333353 wgpuCommandEncoderWriteTimestamp (
334354 encoder,
335355 mQuerySet ,
@@ -341,13 +361,16 @@ void DeferredRenderer::render(
341361 renderDesc.targetTextureView ,
342362 framebufferSize,
343363 renderDesc.exposure ,
364+ frameCount,
344365 gui);
345366 }
346367 wgpuCommandEncoderWriteTimestamp (
347368 encoder,
348369 mQuerySet ,
349370 offsetof (TimestampsLayout, resolvePassEnd) / TimestampsLayout::MEMBER_SIZE);
350371
372+ // Resolve timestamp queries
373+
351374 wgpuCommandEncoderResolveQuerySet (
352375 encoder, mQuerySet , 0 , TimestampsLayout::QUERY_COUNT, mQueryBuffer .ptr (), 0 );
353376 wgpuCommandEncoderCopyBufferToBuffer (
@@ -676,15 +699,15 @@ DeferredRenderer::GbufferPass::GbufferPass(
676699 gpuContext.device,
677700 " Uniform buffer" ,
678701 {GpuBufferUsage::Uniform, GpuBufferUsage::CopyDst},
679- sizeof (glm::mat4 )),
702+ sizeof (Uniforms )),
680703 mUniformBindGroup (),
681704 mSamplerBindGroup (),
682705 mPipeline (nullptr )
683706{
684707 const GpuBindGroupLayout uniformBindGroupLayout{
685708 gpuContext.device ,
686709 " Uniform bind group layout" ,
687- mUniformBuffer .bindGroupLayoutEntry (0 , WGPUShaderStage_Vertex, sizeof (glm::mat4 ))};
710+ mUniformBuffer .bindGroupLayoutEntry (0 , WGPUShaderStage_Vertex, sizeof (Uniforms ))};
688711
689712 mUniformBindGroup = GpuBindGroup{
690713 gpuContext.device ,
@@ -990,12 +1013,8 @@ void DeferredRenderer::GbufferPass::render(
9901013 const WGPUTextureView albedoTextureView,
9911014 const WGPUTextureView normalTextureView)
9921015{
993- wgpuQueueWriteBuffer (
994- gpuContext.queue ,
995- mUniformBuffer .ptr (),
996- 0 ,
997- &viewReverseZProjectionMatrix[0 ][0 ],
998- sizeof (glm::mat4));
1016+ const Uniforms uniforms{viewReverseZProjectionMatrix};
1017+ wgpuQueueWriteBuffer (gpuContext.queue , mUniformBuffer .ptr (), 0 , &uniforms, sizeof (Uniforms));
9991018
10001019 const WGPURenderPassEncoder renderPassEncoder = [cmdEncoder,
10011020 depthTextureView,
@@ -1644,7 +1663,6 @@ DeferredRenderer::LightingPass::LightingPass(LightingPass&& other) noexcept
16441663 mSampleBindGroup = std::move (other.mSampleBindGroup );
16451664 mPipeline = other.mPipeline ;
16461665 other.mPipeline = nullptr ;
1647- mFrameCount = other.mFrameCount ;
16481666 }
16491667}
16501668
@@ -1670,7 +1688,6 @@ DeferredRenderer::LightingPass& DeferredRenderer::LightingPass::operator=(
16701688 computePipelineSafeRelease (mPipeline );
16711689 mPipeline = other.mPipeline ;
16721690 other.mPipeline = nullptr ;
1673- mFrameCount = other.mFrameCount ;
16741691 }
16751692 return *this ;
16761693}
@@ -1681,7 +1698,8 @@ void DeferredRenderer::LightingPass::render(
16811698 const glm::mat4& inverseViewReverseZProjectionMatrix,
16821699 const glm::vec3& cameraPosition,
16831700 const Extent2f& fbsize,
1684- const Sky& sky)
1701+ const Sky& sky,
1702+ const std::uint32_t frameCount)
16851703{
16861704 if (mCurrentSky != sky)
16871705 {
@@ -1696,7 +1714,7 @@ void DeferredRenderer::LightingPass::render(
16961714 inverseViewReverseZProjectionMatrix,
16971715 glm::vec4 (cameraPosition, 1 .f ),
16981716 glm::vec2 (fbsize.x , fbsize.y ),
1699- mFrameCount ++ ,
1717+ frameCount ,
17001718 0 };
17011719 wgpuQueueWriteBuffer (
17021720 gpuContext.queue , mUniformBuffer .ptr (), 0 , &uniforms, sizeof (Uniforms));
@@ -1953,10 +1971,11 @@ void DeferredRenderer::ResolvePass::render(
19531971 WGPUTextureView targetTextureView,
19541972 const Extent2f& fbsize,
19551973 const float exposure,
1974+ const std::uint32_t frameCount,
19561975 Gui& gui)
19571976{
19581977 {
1959- const Uniforms uniforms{glm::vec2 (fbsize.x , fbsize.y ), exposure, 0 . f };
1978+ const Uniforms uniforms{glm::vec2 (fbsize.x , fbsize.y ), exposure, frameCount };
19601979 wgpuQueueWriteBuffer (
19611980 gpuContext.queue , mUniformBuffer .ptr (), 0 , &uniforms, sizeof (Uniforms));
19621981 }
0 commit comments