From b34f38049ddad987b4aae3d1f006e3c1bf60e436 Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:10:28 -0400 Subject: [PATCH 1/7] Start work --- Source/Samples/Restir/GBuffer.slang | 77 +++++++---------------------- 1 file changed, 19 insertions(+), 58 deletions(-) diff --git a/Source/Samples/Restir/GBuffer.slang b/Source/Samples/Restir/GBuffer.slang index c27a340..08cde07 100644 --- a/Source/Samples/Restir/GBuffer.slang +++ b/Source/Samples/Restir/GBuffer.slang @@ -1,87 +1,48 @@ -import Scene.Raytracing; -import Utils.Sampling.TinyUniformSampleGenerator; +import Scene.Raster; import Rendering.Lights.LightHelpers; -RWTexture2D gPositionWs; -RWTexture2D gNormalWs; -RWTexture2D gAlbedo; -RWTexture2D gSpecular; - -cbuffer PerFrameCB +VSOut vsMain(VSIn vIn) { - float2 viewportDims; - uint sampleIndex; -}; + return defaultVS(vIn); +} -struct PrimaryRayData +struct GBufferPSOut { - bool dummy; + float4 positionWs : SV_TARGET0; + float4 normalWs : SV_TARGET1; + float4 albedo : SV_TARGET2; + float4 specular : SV_TARGET3; }; -[shader("miss")] -void primaryMiss(inout PrimaryRayData hitData) -{ - uint2 launchIndex = DispatchRaysIndex().xy; - gPositionWs[launchIndex].w = 0.0f; -} - -[shader("closesthit")] -void primaryClosestHit(inout PrimaryRayData hitData, BuiltInTriangleIntersectionAttributes attribs) +GBufferPSOut psMain(VSOut vsOut, uint triangleIndex : SV_PrimitiveID) : SV_TARGET { - // Get the hit-point data. - float3 rayDirW = WorldRayDirection(); - float hitT = RayTCurrent(); - uint triangleIndex = PrimitiveIndex(); + GBufferPSOut psOut = {}; - // Prepare the shading data. - const GeometryInstanceID instanceID = getGeometryInstanceID(); - VertexData v = getVertexData(instanceID, triangleIndex, attribs); - uint materialID = gScene.getMaterialID(instanceID); - ShadingData sd = gScene.materials.prepareShadingData(v, materialID, -rayDirW); + const ShadingData sd = prepareShadingData(vsOut, triangleIndex, viewDir); - // Create material instance and query its properties. - let lod = ExplicitLodTextureSampler(0.f); let mi = gScene.materials.getMaterialInstance(sd, lod); let bsdfProperties = mi.getProperties(sd); - // The launch index. - const uint2 launchIndex = DispatchRaysIndex().xy; - // Write the ws position. { - gPositionWs[launchIndex] = float4(sd.posW, 1.f); + psOut.posW = float4(sd.posW, 1.f); } + // Write the ws normal. { - gNormalWs[launchIndex] = float4(sd.faceN, hitT); + const float3 viewDir = gScene.camera.getPosition() - sd.posW; + psOut.normalWs = float4(sd.faceN, length(viewDir)); } // Write the albedo. { - gAlbedo[launchIndex] = float4(bsdfProperties.diffuseReflectionAlbedo, materialID); + psOut.albedo = float4(bsdfProperties.diffuseReflectionAlbedo, vsOut.materialID); } // Write the specular. { - gSpecular[launchIndex] = float4(bsdfProperties.specularReflectionAlbedo, bsdfProperties.roughness); + psOut.specular = float4(bsdfProperties.specularReflectionAlbedo, bsdfProperties.roughness); } -} - -[shader("anyhit")] -void primaryAnyHit(inout PrimaryRayData rayData, BuiltInTriangleIntersectionAttributes attribs) -{ -} -[shader("raygeneration")] -void rayGen() -{ - uint3 launchIndex = DispatchRaysIndex(); - if (any(launchIndex.xy > (uint2)viewportDims)) - return; - - const RayDesc ray = gScene.camera.computeRayPinhole(launchIndex.xy, viewportDims).toRayDesc(); - - PrimaryRayData hitData; - TraceRay(gScene.rtAccel, 0 /*rayFlags*/, 0xFF, 0 /* ray index*/, rayTypeCount, 0, ray, hitData); + return psOut; } - From 28cb48bd8166bf19280919373695cade7e1fe4ef Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:42:00 -0400 Subject: [PATCH 2/7] Implement raster pass --- Source/Samples/Restir/GBuffer.cpp | 48 +++++++++++++------------------ Source/Samples/Restir/GBuffer.h | 8 ++++-- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/Source/Samples/Restir/GBuffer.cpp b/Source/Samples/Restir/GBuffer.cpp index d1472b5..1719758 100644 --- a/Source/Samples/Restir/GBuffer.cpp +++ b/Source/Samples/Restir/GBuffer.cpp @@ -14,7 +14,7 @@ void GBuffer::init(ref pDevice, ref pScene, uint32_t width, uint3 mHeight = height; createTextures(); - compilePrograms(); + compileProgram(); } void GBuffer::createTextures() @@ -42,49 +42,41 @@ void GBuffer::createTextures() mSpecularTexture = mpDevice->createTexture2D( mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); + + mDepthTexture = mpDevice->createTexture2D( + mWidth, mHeight, ResourceFormat::D32Float, 1, 1, nullptr, ResourceBindFlags::DepthStencil + ); } -void GBuffer::compilePrograms() +void GBuffer::compileProgram() { auto shaderModules = mpScene->getShaderModules(); auto typeConformances = mpScene->getTypeConformances(); - auto defines = mpScene->getSceneDefines(); - ProgramDesc rtProgDesc; - rtProgDesc.addShaderModules(shaderModules); - rtProgDesc.addShaderLibrary("Samples/Restir/GBuffer.slang"); - rtProgDesc.addTypeConformances(typeConformances); - rtProgDesc.setMaxTraceRecursionDepth(1); - - rtProgDesc.setMaxPayloadSize(24); + ProgramDesc rasterProgDesc; + rasterProgDesc.addShaderModules(shaderModules); + rasterProgDesc.addShaderLibrary("Samples/Restir/GBuffer.slang").vsEntry("vsMain").psEntry("psMain"); + rasterProgDesc.addTypeConformances(typeConformances); - ref sbt = RtBindingTable::create(1, 1, mpScene->getGeometryCount()); - sbt->setRayGen(rtProgDesc.addRayGen("rayGen")); - sbt->setMiss(0, rtProgDesc.addMiss("primaryMiss")); - auto primary = rtProgDesc.addHitGroup("primaryClosestHit", "primaryAnyHit"); - - sbt->setHitGroup(0, mpScene->getGeometryIDs(Scene::GeometryType::TriangleMesh), primary); - - mpRaytraceProgram = Program::create(mpDevice, rtProgDesc, defines); - mpRtVars = RtProgramVars::create(mpDevice, mpRaytraceProgram, sbt); + mpRasterPass = RasterPass::create(mpDevice, rasterProgDesc, defines); + mpFbo = Fbo::create(mpDevice); } void GBuffer::render(RenderContext* pRenderContext) { FALCOR_PROFILE(pRenderContext, "GBuffer::render"); - auto var = mpRtVars->getRootVar(); - - var["PerFrameCB"]["viewportDims"] = float2(mWidth, mHeight); - var["PerFrameCB"]["sampleIndex"] = mSampleIndex++; + mpFbo->attachColorTarget(mCurrentPositionWsTexture, 0u); + mpFbo->attachColorTarget(mCurrentNormalWsTexture, 0u); + mpFbo->attachColorTarget(mAlbedoTexture, 0u); + mpFbo->attachColorTarget(mSpecularTexture, 0u); + mpFbo->attachDepthStencilTarget(mDepthTexture); - var["gPositionWs"] = mCurrentPositionWsTexture; - var["gNormalWs"] = mCurrentNormalWsTexture; - var["gAlbedo"] = mAlbedoTexture; - var["gSpecular"] = mSpecularTexture; + pRenderContext->clearDsv(mDepthTexture->getDSV().get(), 1.f, 0); + mpRasterPass->getState()->setFbo(mpFbo); - mpScene->raytrace(pRenderContext, mpRaytraceProgram.get(), mpRtVars, uint3(mWidth, mHeight, 1)); + mpScene->rasterize(pRenderContext, mpRasterPass->getState().get(), mpRasterPass->getVars().get()); } } // namespace Restir diff --git a/Source/Samples/Restir/GBuffer.h b/Source/Samples/Restir/GBuffer.h index 4d1f515..1c6ce2b 100644 --- a/Source/Samples/Restir/GBuffer.h +++ b/Source/Samples/Restir/GBuffer.h @@ -1,6 +1,7 @@ #pragma once #include "Singleton.h" +#include "Core/Pass/RasterPass.h" namespace Restir { @@ -30,7 +31,7 @@ class GBuffer private: void createTextures(); - void compilePrograms(); + void compileProgram(); Falcor::ref mpDevice; Falcor::ref mpScene; @@ -43,12 +44,13 @@ class GBuffer Falcor::ref mAlbedoTexture; Falcor::ref mSpecularTexture; + Falcor::ref mDepthTexture; Falcor::ref mCurrentNormalWsTexture; Falcor::ref mPreviousNormalWsTexture; - Falcor::ref mpRaytraceProgram; - Falcor::ref mpRtVars; + ref mpRasterPass; + ref mpFbo; uint32_t mSampleIndex = 0u; }; From d8673103cddb338cc03b9db0ee3c538c00acb8ce Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:52:58 -0400 Subject: [PATCH 3/7] Start fixing compilation errors --- Source/Samples/Restir/GBuffer.cpp | 6 +++--- Source/Samples/Restir/GBuffer.h | 4 ++-- Source/Samples/Restir/GBuffer.slang | 10 +++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Source/Samples/Restir/GBuffer.cpp b/Source/Samples/Restir/GBuffer.cpp index 1719758..e79d0f6 100644 --- a/Source/Samples/Restir/GBuffer.cpp +++ b/Source/Samples/Restir/GBuffer.cpp @@ -68,9 +68,9 @@ void GBuffer::render(RenderContext* pRenderContext) FALCOR_PROFILE(pRenderContext, "GBuffer::render"); mpFbo->attachColorTarget(mCurrentPositionWsTexture, 0u); - mpFbo->attachColorTarget(mCurrentNormalWsTexture, 0u); - mpFbo->attachColorTarget(mAlbedoTexture, 0u); - mpFbo->attachColorTarget(mSpecularTexture, 0u); + mpFbo->attachColorTarget(mCurrentNormalWsTexture, 1u); + mpFbo->attachColorTarget(mAlbedoTexture, 2u); + mpFbo->attachColorTarget(mSpecularTexture, 3u); mpFbo->attachDepthStencilTarget(mDepthTexture); pRenderContext->clearDsv(mDepthTexture->getDSV().get(), 1.f, 0); diff --git a/Source/Samples/Restir/GBuffer.h b/Source/Samples/Restir/GBuffer.h index 1c6ce2b..1eb8b75 100644 --- a/Source/Samples/Restir/GBuffer.h +++ b/Source/Samples/Restir/GBuffer.h @@ -49,8 +49,8 @@ class GBuffer Falcor::ref mCurrentNormalWsTexture; Falcor::ref mPreviousNormalWsTexture; - ref mpRasterPass; - ref mpFbo; + Falcor::ref mpRasterPass; + Falcor::ref mpFbo; uint32_t mSampleIndex = 0u; }; diff --git a/Source/Samples/Restir/GBuffer.slang b/Source/Samples/Restir/GBuffer.slang index 08cde07..59e6841 100644 --- a/Source/Samples/Restir/GBuffer.slang +++ b/Source/Samples/Restir/GBuffer.slang @@ -18,6 +18,11 @@ GBufferPSOut psMain(VSOut vsOut, uint triangleIndex : SV_PrimitiveID) : SV_TARGE { GBufferPSOut psOut = {}; + float3 faceNormal = gScene.getFaceNormalW(vsOut.instanceID, triangleIndex); + VertexData v = prepareVertexData(vsOut, faceNormal); + let lod = ImplicitLodTextureSampler(); + + const float3 viewDir = normalize(gScene.camera.getPosition() - v.posW); const ShadingData sd = prepareShadingData(vsOut, triangleIndex, viewDir); let mi = gScene.materials.getMaterialInstance(sd, lod); @@ -25,13 +30,12 @@ GBufferPSOut psMain(VSOut vsOut, uint triangleIndex : SV_PrimitiveID) : SV_TARGE // Write the ws position. { - psOut.posW = float4(sd.posW, 1.f); + psOut.positionWs = float4(sd.posW, 1.f); } // Write the ws normal. { - const float3 viewDir = gScene.camera.getPosition() - sd.posW; - psOut.normalWs = float4(sd.faceN, length(viewDir)); + psOut.normalWs = float4(sd.faceN, length(gScene.camera.getPosition() - sd.posW)); } // Write the albedo. From 842fbd43b971cc291faac517e3d1a652f4371c65 Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:33:56 -0400 Subject: [PATCH 4/7] Fix crash when attaching targets --- Source/Samples/Restir/GBuffer.cpp | 48 +++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/Source/Samples/Restir/GBuffer.cpp b/Source/Samples/Restir/GBuffer.cpp index e79d0f6..28a1c85 100644 --- a/Source/Samples/Restir/GBuffer.cpp +++ b/Source/Samples/Restir/GBuffer.cpp @@ -20,27 +20,63 @@ void GBuffer::init(ref pDevice, ref pScene, uint32_t width, uint3 void GBuffer::createTextures() { mCurrentPositionWsTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mPreviousPositionWsTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mCurrentNormalWsTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mPreviousNormalWsTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mAlbedoTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mSpecularTexture = mpDevice->createTexture2D( - mWidth, mHeight, ResourceFormat::RGBA32Float, 1, 1, nullptr, ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess + mWidth, + mHeight, + ResourceFormat::RGBA32Float, + 1, + 1, + nullptr, + ResourceBindFlags::RenderTarget | ResourceBindFlags::ShaderResource | ResourceBindFlags::UnorderedAccess ); mDepthTexture = mpDevice->createTexture2D( From 9999b6a6c688e9c30c6fd867212434129ff415bf Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:40:25 -0400 Subject: [PATCH 5/7] Rasterization works --- Source/Samples/Restir/GBuffer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Samples/Restir/GBuffer.cpp b/Source/Samples/Restir/GBuffer.cpp index 28a1c85..bdf4fa3 100644 --- a/Source/Samples/Restir/GBuffer.cpp +++ b/Source/Samples/Restir/GBuffer.cpp @@ -110,8 +110,10 @@ void GBuffer::render(RenderContext* pRenderContext) mpFbo->attachDepthStencilTarget(mDepthTexture); pRenderContext->clearDsv(mDepthTexture->getDSV().get(), 1.f, 0); - mpRasterPass->getState()->setFbo(mpFbo); + pRenderContext->clearFbo(mpFbo.get(), float4(0), 1.f, 0, FboAttachmentType::Color); + + mpRasterPass->getState()->setFbo(mpFbo); mpScene->rasterize(pRenderContext, mpRasterPass->getState().get(), mpRasterPass->getVars().get()); } From bbd0be535ef0c27cb044380e6140deb039f80570 Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:45:25 -0400 Subject: [PATCH 6/7] Finilize work --- README.md | 2 +- Source/Samples/Restir/GBuffer.cpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 65ca911..070953f 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Scenes specific tweaks are stored in the *SceneSettings* struct. ## Render passes ### GBuffer -GBuffer rendering is the first pass. It is generated by raytracing. Should normally be done by rasterization to save time but last time i tried it was crashing. See GBuffer.cpp. +GBuffer rendering is the first pass, it is generated by rasterization. This is faster than tracing primary rays. See GBuffer.cpp. ### RIS Resampled Importance Sampling is performed as described in paper. See RISPass.cpp diff --git a/Source/Samples/Restir/GBuffer.cpp b/Source/Samples/Restir/GBuffer.cpp index bdf4fa3..65867d0 100644 --- a/Source/Samples/Restir/GBuffer.cpp +++ b/Source/Samples/Restir/GBuffer.cpp @@ -109,9 +109,7 @@ void GBuffer::render(RenderContext* pRenderContext) mpFbo->attachColorTarget(mSpecularTexture, 3u); mpFbo->attachDepthStencilTarget(mDepthTexture); - pRenderContext->clearDsv(mDepthTexture->getDSV().get(), 1.f, 0); - - pRenderContext->clearFbo(mpFbo.get(), float4(0), 1.f, 0, FboAttachmentType::Color); + pRenderContext->clearFbo(mpFbo.get(), float4(0), 1.f, 0, FboAttachmentType::All); mpRasterPass->getState()->setFbo(mpFbo); mpScene->rasterize(pRenderContext, mpRasterPass->getState().get(), mpRasterPass->getVars().get()); From 4c2370a31f86a9820382ab09818d15c56f9a3e74 Mon Sep 17 00:00:00 2001 From: Cloyz2021 <84689194+Cloyz2021@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:53:35 -0400 Subject: [PATCH 7/7] Update GBuffer.slang --- Source/Samples/Restir/GBuffer.slang | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Samples/Restir/GBuffer.slang b/Source/Samples/Restir/GBuffer.slang index 59e6841..a2c5544 100644 --- a/Source/Samples/Restir/GBuffer.slang +++ b/Source/Samples/Restir/GBuffer.slang @@ -18,9 +18,9 @@ GBufferPSOut psMain(VSOut vsOut, uint triangleIndex : SV_PrimitiveID) : SV_TARGE { GBufferPSOut psOut = {}; - float3 faceNormal = gScene.getFaceNormalW(vsOut.instanceID, triangleIndex); - VertexData v = prepareVertexData(vsOut, faceNormal); - let lod = ImplicitLodTextureSampler(); + const float3 faceNormal = gScene.getFaceNormalW(vsOut.instanceID, triangleIndex); + const VertexData v = prepareVertexData(vsOut, faceNormal); + const let lod = ImplicitLodTextureSampler(); const float3 viewDir = normalize(gScene.camera.getPosition() - v.posW); const ShadingData sd = prepareShadingData(vsOut, triangleIndex, viewDir); @@ -33,17 +33,17 @@ GBufferPSOut psMain(VSOut vsOut, uint triangleIndex : SV_PrimitiveID) : SV_TARGE psOut.positionWs = float4(sd.posW, 1.f); } - // Write the ws normal. + // Write the ws normal and hit distance. { psOut.normalWs = float4(sd.faceN, length(gScene.camera.getPosition() - sd.posW)); } - // Write the albedo. + // Write the albedo and material id. { psOut.albedo = float4(bsdfProperties.diffuseReflectionAlbedo, vsOut.materialID); } - // Write the specular. + // Write the specular and roughness. { psOut.specular = float4(bsdfProperties.specularReflectionAlbedo, bsdfProperties.roughness); }