Fixing depth mip generation

This commit is contained in:
Dynamitos
2024-06-17 16:21:41 +02:00
parent 0fd331b3b5
commit 7cdbb19331
5 changed files with 221 additions and 43 deletions
@@ -1,5 +1,6 @@
#include "DepthCullingPass.h"
#include "Graphics/Shader.h"
#include <minmax.h>
using namespace Seele;
@@ -7,25 +8,40 @@ extern bool usePositionOnly;
extern bool useDepthCulling;
DepthCullingPass::DepthCullingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {
depthTextureLayout = graphics->createDescriptorLayout("pDepthAttachment");
depthTextureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
depthAttachmentLayout = graphics->createDescriptorLayout("pDepthAttachment");
depthAttachmentLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = 0,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.shaderStages = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_MESH_BIT_EXT,
.shaderStages = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_MESH_BIT_EXT | Gfx::SE_SHADER_STAGE_COMPUTE_BIT,
});
depthTextureLayout->create();
depthAttachmentLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = 1,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.shaderStages = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_COMPUTE_BIT,
});
depthAttachmentLayout->create();
depthPrepassLayout = graphics->createPipelineLayout("DepthPrepassLayout");
depthPrepassLayout->addDescriptorLayout(viewParamsLayout);
depthPrepassLayout->addDescriptorLayout(depthTextureLayout);
depthPrepassLayout->addPushConstants(Gfx::SePushConstantRange{
depthCullingLayout = graphics->createPipelineLayout("DepthPrepassLayout");
depthCullingLayout->addDescriptorLayout(viewParamsLayout);
depthCullingLayout->addDescriptorLayout(depthAttachmentLayout);
depthCullingLayout->addPushConstants(Gfx::SePushConstantRange{
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT,
.offset = 0,
.size = sizeof(VertexData::DrawCallOffsets),
});
depthComputeLayout = graphics->createPipelineLayout("DepthComputeLayout");
depthComputeLayout->addDescriptorLayout(viewParamsLayout);
depthComputeLayout->addDescriptorLayout(depthAttachmentLayout);
depthComputeLayout->addPushConstants(Gfx::SePushConstantRange{
.stageFlags = Gfx::SE_SHADER_STAGE_COMPUTE_BIT,
.offset = 0,
.size = sizeof(MipParam),
});
if (graphics->supportMeshShading()) {
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
.baseLayout = depthPrepassLayout,
.baseLayout = depthCullingLayout,
.taskFile = "DepthCullingTask",
.mainFile = "DepthCullingMesh",
.fragmentFile = "VisibilityPass",
@@ -37,7 +53,7 @@ DepthCullingPass::DepthCullingPass(Gfx::PGraphics graphics, PScene scene) : Rend
});
} else {
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
.baseLayout = depthPrepassLayout,
.baseLayout = depthCullingLayout,
.taskFile = "",
.mainFile = "LegacyPass",
.fragmentFile = "VisibilityPass",
@@ -55,26 +71,49 @@ DepthCullingPass::~DepthCullingPass() {}
void DepthCullingPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); }
void DepthCullingPass::render() {
depthAttachment.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
depthAttachment.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, Gfx::SE_ACCESS_TRANSFER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
depthMipTexture->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
graphics->copyTexture(depthAttachment.getTexture(), Gfx::PTexture2D(depthMipTexture));
depthMipTexture->generateMipmaps();
Gfx::PDescriptorSet set = depthAttachmentLayout->allocateDescriptorSet();
set->updateTexture(0, Gfx::PTexture2D(depthAttachment.getTexture()));
set->updateBuffer(1, depthMipBuffer);
set->writeChanges();
Gfx::OComputeCommand computeCommand = graphics->createComputeCommand("DepthMipGenCommand");
computeCommand->bindPipeline(depthInitialReduce);
computeCommand->bindDescriptor({viewParamsSet, set});
UVector2 reduceDimensions = UVector2(viewport->getOwner()->getFramebufferWidth() + BLOCK_SIZE - 1,
viewport->getOwner()->getFramebufferHeight() + BLOCK_SIZE - 1) /
uint32(BLOCK_SIZE);
computeCommand->dispatch(reduceDimensions.x, reduceDimensions.y, 1);
computeCommand->bindPipeline(depthMipGen);
computeCommand->bindDescriptor({viewParamsSet, set});
for (uint32 i = 0; i < mipOffsets.size() - 1; ++i) {
depthMipBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT | Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
MipParam params = {
.srcMipOffset = mipOffsets[i],
.dstMipOffset = mipOffsets[i + 1],
.srcMipDim = mipDims[i],
.dstMipDim = mipDims[i + 1],
};
computeCommand->pushConstants(Gfx::SE_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(MipParam), &params);
UVector2 threadGroups = (((mipDims[i] + UVector2(1, 1)) / 2u) + UVector2(BLOCK_SIZE - 1, BLOCK_SIZE - 1)) / uint32(BLOCK_SIZE);
computeCommand->dispatch(threadGroups.x, threadGroups.y, 1);
}
depthMipBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT);
depthAttachment.getTexture()->changeLayout(
Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
depthMipTexture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT);
Gfx::PDescriptorSet set = depthTextureLayout->allocateDescriptorSet();
set->updateTexture(0, Gfx::PTexture2D(depthMipTexture));
set->writeChanges();
query->beginQuery();
graphics->beginRenderPass(renderPass);
if (useDepthCulling) {
@@ -187,12 +226,51 @@ void DepthCullingPass::render() {
void DepthCullingPass::endFrame() {}
void DepthCullingPass::publishOutputs() {
uint32 width = viewport->getOwner()->getFramebufferWidth();
uint32 height = viewport->getOwner()->getFramebufferHeight();
uint32 mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;
TextureCreateInfo depthMipInfo = {
.format = Gfx::SE_FORMAT_D32_SFLOAT, .width = width, .height = height, .mipLevels = mipLevels, .name = "DepthMipTexture"};
depthMipTexture = graphics->createTexture2D(depthMipInfo);
uint32 width = (viewport->getOwner()->getFramebufferWidth() + BLOCK_SIZE - 1) / BLOCK_SIZE;
uint32 height = (viewport->getOwner()->getFramebufferHeight() + BLOCK_SIZE - 1) / BLOCK_SIZE;
uint32 bufferSize = 0;
while (width > 1 && height > 1) {
mipOffsets.add(bufferSize);
mipDims.add(UVector2(width, height));
bufferSize += width * height;
width = max((width + 1) / 2, 1);
height = max((height + 1) / 2, 1);
}
ShaderBufferCreateInfo depthMipInfo = {
.sourceData =
{
.size = bufferSize * sizeof(uint32),
.data = nullptr,
},
.numElements = bufferSize,
.name = "DepthMipBuffer",
};
depthMipBuffer = graphics->createShaderBuffer(depthMipInfo);
ShaderCreateInfo mipComputeInfo = {
.name = "DepthMipCompute",
.mainModule = "DepthMipGen",
.entryPoint = "initialReduce",
.rootSignature = depthComputeLayout,
};
depthInitialReduceShader = graphics->createComputeShader(mipComputeInfo);
depthComputeLayout->create();
Gfx::ComputePipelineCreateInfo pipelineCreateInfo = {
.computeShader = depthInitialReduceShader,
.pipelineLayout = depthComputeLayout,
};
depthInitialReduce = graphics->createComputePipeline(pipelineCreateInfo);
mipComputeInfo.entryPoint = "reduceLevel";
depthMipGenShader = graphics->createComputeShader(mipComputeInfo);
pipelineCreateInfo.computeShader = depthMipGenShader;
depthMipGen = graphics->createComputePipeline(pipelineCreateInfo);
query = graphics->createPipelineStatisticsQuery();
resources->registerQueryOutput("DEPTH_QUERY", query);
}
@@ -1,6 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "RenderPass.h"
#include "Graphics/Pipeline.h"
#include "Graphics/Query.h"
namespace Seele {
@@ -17,13 +18,28 @@ class DepthCullingPass : public RenderPass {
virtual void createRenderPass() override;
private:
Gfx::OTexture2D depthMipTexture;
constexpr static uint64 BLOCK_SIZE = 32;
struct MipParam {
uint32 srcMipOffset;
uint32 dstMipOffset;
UVector2 srcMipDim;
UVector2 dstMipDim;
};
Array<uint32> mipOffsets;
Array<UVector2> mipDims;
Gfx::OShaderBuffer depthMipBuffer;
Gfx::RenderTargetAttachment depthAttachment;
Gfx::RenderTargetAttachment visibilityAttachment;
Gfx::ODescriptorLayout depthTextureLayout;
Gfx::OPipelineLayout depthPrepassLayout;
Gfx::ODescriptorLayout depthAttachmentLayout;
Gfx::OPipelineLayout depthCullingLayout;
Gfx::OPipelineStatisticsQuery query;
Gfx::OPipelineLayout depthComputeLayout;
Gfx::OComputeShader depthInitialReduceShader;
Gfx::PComputePipeline depthInitialReduce;
Gfx::OComputeShader depthMipGenShader;
Gfx::PComputePipeline depthMipGen;
Gfx::PShaderBuffer cullingBuffer;
};
DEFINE_REF(DepthCullingPass)