Skip to content

Commit

Permalink
feat: remove hi-z buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Dec 9, 2023
1 parent 31a9a21 commit 9ecd46d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 149 deletions.
7 changes: 0 additions & 7 deletions HPL2/include/graphics/RendererDeferred2.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ namespace hpl {
std::array<SharedRenderTarget, ForgeRenderer::SwapChainLength> m_depthBuffer;
std::array<SharedRenderTarget, ForgeRenderer::SwapChainLength> m_testBuffer; //encodes the parallax
std::array<SharedRenderTarget, ForgeRenderer::SwapChainLength> m_visiblityBuffer;

std::array<SharedRenderTarget, ForgeRenderer::SwapChainLength> m_hizDepthBuffer;
};

cRendererDeferred2(
Expand Down Expand Up @@ -160,11 +158,6 @@ namespace hpl {
SharedDescriptorSet m_sceneDescriptorConstSet;
std::array<SharedDescriptorSet, ForgeRenderer::SwapChainLength> m_sceneDescriptorPerFrameSet;

std::array<SharedDescriptorSet, ForgeRenderer::SwapChainLength> m_HIZGenerateConstSet;
SharedRootSignature m_generateHiZSignature;
SharedPipeline m_generateHiZPass;
SharedShader m_generateHiZShader;

SharedRootSignature m_rootSignatureCopyDepth;
std::array<SharedDescriptorSet, ForgeRenderer::SwapChainLength> m_descriptorCopyDepth;
SharedPipeline m_pipelineCopyDepth;
Expand Down
17 changes: 10 additions & 7 deletions HPL2/resource/scene_light_cluster.comp.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ void CS_MAIN(SV_GroupThreadID(uint3) threadInGroupId, SV_GroupID(uint3) groupId)
const float invClusterHeight = 1.0f / float(LIGHT_CLUSTER_HEIGHT);

ViewportInfo viewInfo = Get(viewports)[PRIMARY_VIEWPORT_INDEX];
mat4 invProjMat = mul(viewInfo.viewMat, viewInfo.invViewProj);
//float4x4 viewProjMat = mul(viewInfo.projMat, viewInfo.viewMat);
float2 screenDim = viewInfo.rect.zw;

//Near and far values of the cluster in view space
Expand All @@ -20,11 +18,16 @@ void CS_MAIN(SV_GroupThreadID(uint3) threadInGroupId, SV_GroupID(uint3) groupId)
const float tileFar = -getDepthClipPlane(float(groupId.z) + 1.0, LIGHT_CLUSTER_SLICE, viewInfo.zNear, viewInfo.zFar);

// Cluster coordinates in post perspective clip space
float clusterLeft = (float(threadInGroupId.x) * invClusterWidth);
float clusterTop = (float(threadInGroupId.y) * invClusterHeight);
float clusterRight = (clusterLeft + invClusterWidth);
float clusterBottom = (clusterTop + invClusterHeight);

// Transform coordinates from range [0..1] to range [-1..1]
float clusterLeft = (float(threadInGroupId.x) * invClusterWidth) * 2.0f - 1.0f;
float clusterTop = (float(threadInGroupId.y) * invClusterHeight) * 2.0f - 1.0f;
float clusterRight = (clusterLeft + invClusterWidth) * 2.0f - 1.0f;
float clusterBottom = (clusterTop + invClusterHeight) * 2.0f - 1.0f;
clusterLeft = clusterLeft * 2.0f - 1.0f;
clusterTop = clusterTop * 2.0f - 1.0f;
clusterRight = clusterRight * 2.0f - 1.0f;
clusterBottom = clusterBottom * 2.0f - 1.0f;

clusterTop *= -1.0;
clusterBottom *= -1.0;
Expand All @@ -42,7 +45,7 @@ void CS_MAIN(SV_GroupThreadID(uint3) threadInGroupId, SV_GroupID(uint3) groupId)

PointLight lightData = Get(pointLights)[groupId.x];
float4 lightViewPos = mul(viewInfo.viewMat, float4(lightData.lightPos.xyz, 1.0f));
if (testSphereAABB(lightViewPos.xyz, lightData.lightRadius, minPointAABB, maxPointAABB)) {
if (testSphereAABB(lightViewPos.xyz, lightData.lightRadius * 1.2f, minPointAABB, maxPointAABB)) {
// Increase light count on this cluster
uint lightArrayPos = 0;
AtomicAdd(Get(lightClustersCount)[LIGHT_FROXEL_COUNT_POS(threadInGroupId.x, threadInGroupId.y, groupId.z)], 1, lightArrayPos);
Expand Down
135 changes: 0 additions & 135 deletions HPL2/sources/graphics/RendererDeferred2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ namespace hpl {
addShader(forgeRenderer->Rend(), &loadDesc, shader);
return true;
});
m_generateHiZShader.Load(forgeRenderer->Rend(), [&](Shader** shader) {
ShaderLoadDesc loadDesc = {};
loadDesc.mStages[0].pFileName = "generate_hi_z.comp";
addShader(forgeRenderer->Rend(), &loadDesc, shader);
return true;
});
m_copyDepthShader.Load(forgeRenderer->Rend(), [&](Shader** shader) {
ShaderLoadDesc loadDesc = {};
loadDesc.mStages[0].pFileName = "fullscreen.vert";
Expand All @@ -175,14 +169,6 @@ namespace hpl {
addRootSignature(forgeRenderer->Rend(), &rootSignatureDesc, signature);
return true;
});
m_generateHiZSignature.Load(forgeRenderer->Rend(), [&](RootSignature** signature) {
std::array shaders = { m_generateHiZShader.m_handle};
RootSignatureDesc rootSignatureDesc = {};
rootSignatureDesc.ppShaders = shaders.data();
rootSignatureDesc.mShaderCount = shaders.size();
addRootSignature(forgeRenderer->Rend(), &rootSignatureDesc, signature);
return true;
});
m_lightClusterRootSignature.Load(forgeRenderer->Rend(), [&](RootSignature** signature) {
RootSignatureDesc rootSignatureDesc = {};
std::array shaders = { m_lightClusterSpotlightShader.m_handle,
Expand Down Expand Up @@ -271,15 +257,6 @@ namespace hpl {
addPipeline(forgeRenderer->Rend(), &pipelineDesc, handle);
return true;
});
m_generateHiZPass.Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
PipelineDesc pipelineDesc = {};
pipelineDesc.mType = PIPELINE_TYPE_COMPUTE;
ComputePipelineDesc& computePipelineDesc = pipelineDesc.mComputeDesc;
computePipelineDesc.pShaderProgram = m_generateHiZShader.m_handle;
computePipelineDesc.pRootSignature = m_generateHiZSignature.m_handle;
addPipeline(forgeRenderer->Rend(), &pipelineDesc, pipeline);
return true;
});
m_visbilityAlphaBufferPass.Load(forgeRenderer->Rend(), [&](Pipeline** pipeline) {
VertexLayout vertexLayout = {};
vertexLayout.mBindingCount = 4;
Expand Down Expand Up @@ -524,12 +501,6 @@ namespace hpl {
addDescriptorSet(forgeRenderer->Rend(), &setDesc, handle);
return true;
});
m_HIZGenerateConstSet[swapchainIndex].Load(forgeRenderer->Rend(), [&](DescriptorSet** descSet) {
DescriptorSetDesc descriptorSetDesc{ m_generateHiZSignature.m_handle, DESCRIPTOR_UPDATE_FREQ_PER_FRAME, 1};
addDescriptorSet(forgeRenderer->Rend(), &descriptorSetDesc, descSet);
return true;

});
m_sceneDescriptorPerFrameSet[swapchainIndex].Load(forgeRenderer->Rend(), [&](DescriptorSet** descSet) {
DescriptorSetDesc descriptorSetDesc{ m_sceneRootSignature.m_handle, DESCRIPTOR_UPDATE_FREQ_PER_FRAME, 1 };
addDescriptorSet(forgeRenderer->Rend(), &descriptorSetDesc, descSet);
Expand Down Expand Up @@ -784,33 +755,9 @@ namespace hpl {
addRenderTarget(forgeRenderer->Rend(), &renderTargetDesc, target);
return true;
});
updateDatum->m_hizDepthBuffer[i].Load(forgeRenderer->Rend(), [&](RenderTarget** handle) {
RenderTargetDesc renderTargetDesc = {};
renderTargetDesc.mArraySize = 1;
renderTargetDesc.mDepth = 1;
renderTargetDesc.mMipLevels = std::min<uint8_t>(std::floor(std::log2(std::max(updateDatum->m_size.x, updateDatum->m_size.y))), MaxHiZMipLevels);
renderTargetDesc.mFormat = TinyImageFormat_R32_SFLOAT;
renderTargetDesc.mDescriptors = DESCRIPTOR_TYPE_TEXTURE | DESCRIPTOR_TYPE_RW_TEXTURE;
renderTargetDesc.mWidth = updateDatum->m_size.x;
renderTargetDesc.mHeight = updateDatum->m_size.y;
renderTargetDesc.mSampleCount = SAMPLE_COUNT_1;
renderTargetDesc.mSampleQuality = 0;
renderTargetDesc.mStartState = RESOURCE_STATE_UNORDERED_ACCESS;
renderTargetDesc.pName = "hi-z depth buffer";
addRenderTarget(forgeRenderer->Rend(), &renderTargetDesc, handle);
return true;
});
}
viewportDatum = m_boundViewportData.update(viewport, std::move(updateDatum));
}
// {

// std::array params = {
// DescriptorData{ .pName = "hiZTexture", .ppTextures = &viewportDatum->m_hizDepthBuffer[frame.m_frameIndex].m_handle->pTexture },
// };
// updateDescriptorSet(
// forgeRenderer->Rend(), 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle, params.size(), params.data());
// }

frame.m_resourcePool->Push(viewportDatum->m_testBuffer[frame.m_frameIndex]);
frame.m_resourcePool->Push(viewportDatum->m_visiblityBuffer[frame.m_frameIndex]);
Expand Down Expand Up @@ -1086,78 +1033,6 @@ namespace hpl {
}
cmdEndDebugMarker(cmd);
}
// generate hi-z buffer
{
auto hizTarget = viewportDatum->m_hizDepthBuffer[frame.m_frameIndex].m_handle;
uint32_t width = static_cast<float>(viewportDatum->m_size.x);
uint32_t height = static_cast<float>(viewportDatum->m_size.y);
uint32_t rootConstantIndex = getDescriptorIndexFromName(m_generateHiZSignature.m_handle, "uRootConstants");
{
std::array<DescriptorData, 1> params = {
DescriptorData {.pName = "depthInput", .mBindMipChain = true, .ppTextures = &hizTarget->pTexture }
};
updateDescriptorSet(
frame.m_renderer->Rend(), 0, m_HIZGenerateConstSet[frame.m_frameIndex].m_handle, params.size(), params.data());
}
{
std::array<DescriptorData, 1> params = {
DescriptorData { .pName = "sourceInput", .ppTextures = &viewportDatum->m_depthBuffer[frame.m_frameIndex].m_handle->pTexture }
};
updateDescriptorSet(frame.m_renderer->Rend(), 0, m_descriptorCopyDepth[frame.m_frameIndex].m_handle, params.size(), params.data());
}
{
cmdBindRenderTargets(cmd, 0, NULL, NULL, NULL, NULL, NULL, -1, -1);
std::array rtBarriers = {
RenderTargetBarrier{ viewportDatum->m_depthBuffer[frame.m_frameIndex].m_handle, RESOURCE_STATE_DEPTH_WRITE, RESOURCE_STATE_SHADER_RESOURCE },
RenderTargetBarrier{ viewportDatum->m_hizDepthBuffer[frame.m_frameIndex].m_handle, RESOURCE_STATE_UNORDERED_ACCESS, RESOURCE_STATE_RENDER_TARGET },
};
cmdResourceBarrier(cmd, 0, NULL, 0, NULL, rtBarriers.size(), rtBarriers.data());
}
{
LoadActionsDesc loadActions = {};
loadActions.mLoadActionsColor[0] = LOAD_ACTION_LOAD;
loadActions.mLoadActionDepth = LOAD_ACTION_DONTCARE;
loadActions.mClearColorValues[0] = { .r = 1.0f, .g = 0.0f, .b = 0.0f, .a = 0.0f };
cmdBindRenderTargets(cmd, 1, &viewportDatum->m_hizDepthBuffer[frame.m_frameIndex].m_handle, NULL, &loadActions, NULL, NULL, -1, -1);

cmdSetViewport(cmd, 0.0f, 0.0f, viewportDatum->m_size.x, viewportDatum->m_size.y, 0.0f, 1.0f);
cmdSetScissor(cmd, 0, 0, viewportDatum->m_size.x, viewportDatum->m_size.y);
cmdBindPipeline(cmd, m_pipelineCopyDepth.m_handle);

cmdBindDescriptorSet(cmd, 0, m_descriptorCopyDepth[frame.m_frameIndex].m_handle);
cmdDraw(cmd, 3, 0);
}
{
cmdBindRenderTargets(cmd, 0, NULL, NULL, NULL, NULL, NULL, -1, -1);
std::array rtBarriers = {
RenderTargetBarrier{ viewportDatum->m_depthBuffer[frame.m_frameIndex].m_handle, RESOURCE_STATE_SHADER_RESOURCE, RESOURCE_STATE_DEPTH_WRITE },
RenderTargetBarrier{ viewportDatum->m_hizDepthBuffer[frame.m_frameIndex].m_handle, RESOURCE_STATE_RENDER_TARGET, RESOURCE_STATE_UNORDERED_ACCESS },
};
cmdResourceBarrier(cmd, 0, NULL, 0, NULL, rtBarriers.size(), rtBarriers.data());
}
for (uint32_t lod = 1; lod < hizTarget->mMipLevels; lod++) {
width /= 2;
height /= 2;
struct {
uint2 screenDim;
uint32_t mipLevel;
} pushConstants = {};

pushConstants.mipLevel = lod;
pushConstants.screenDim = uint2(viewportDatum->m_size.x, viewportDatum->m_size.y);

// bind lod to push constant
cmdBindPushConstants(cmd, m_generateHiZSignature.m_handle, rootConstantIndex, &pushConstants);
cmdBindDescriptorSet(cmd, 0, m_HIZGenerateConstSet[frame.m_frameIndex].m_handle);
cmdBindPipeline(cmd, m_generateHiZPass.m_handle);
cmdDispatch(cmd, static_cast<uint32_t>(width / 16) + 1, static_cast<uint32_t>(height / 16) + 1, 1);

std::array rtBarriers = {
RenderTargetBarrier{ hizTarget, RESOURCE_STATE_UNORDERED_ACCESS, RESOURCE_STATE_UNORDERED_ACCESS },
};
cmdResourceBarrier(cmd, 0, NULL, 0, NULL, rtBarriers.size(), rtBarriers.data());
}
}
{
{
std::array barriers = {
Expand Down Expand Up @@ -1191,16 +1066,6 @@ namespace hpl {
cmdBindPipeline(cmd, m_pointLightClusterPipeline.m_handle);
cmdBindDescriptorSet(cmd, 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle);
cmdDispatch(cmd, pointlightCount, 1, LightClusterSlices);
// {
// std::array barriers = { BufferBarrier{ m_lightClusterCountBuffer[frame.m_frameIndex].m_handle,
// RESOURCE_STATE_UNORDERED_ACCESS,
// RESOURCE_STATE_UNORDERED_ACCESS } };
// cmdResourceBarrier(
// cmd, barriers.size(), barriers.data(), 0, nullptr, 0, nullptr);

// }
// cmdBindPipeline(cmd, m_spotLightClusterPipeline.m_handle);
// cmdDispatch(cmd, spotlightCount, 1, 1);
}
{
cmdBindRenderTargets(cmd, 0, NULL, NULL, NULL, NULL, NULL, -1, -1);
Expand Down

0 comments on commit 9ecd46d

Please sign in to comment.