Working on visibility pass

This commit is contained in:
Dynamitos
2024-06-07 09:19:47 +02:00
parent ad00e16cf9
commit cb21e8a85a
24 changed files with 450 additions and 373 deletions
+5 -4
View File
@@ -3,17 +3,18 @@ import Scene;
groupshared MeshPayload p;
groupshared uint head;
groupshared MeshData mesh;
[numthreads(TASK_GROUP_SIZE, 1, 1)]
[shader("amplification")]
void taskMain(
uint threadID: SV_GroupIndex,
uint threadID: SV_GroupThreadID,
uint groupID: SV_GroupID, )
{
MeshData mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
if (threadID == 0)
{
head = 0;
mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
p.instanceId = pOffsets.instanceOffset + groupID;
p.meshletOffset = mesh.meshletOffset;
p.cullingOffset = pScene.cullingOffsets[p.instanceId];
@@ -24,8 +25,8 @@ void taskMain(
uint m = p.meshletOffset + i;
uint cull = p.cullingOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
MeshletCullingInfo culling = pScene.culledMeshlets[cull];
if(false)
MeshletCullingInfo culling = pScene.cullingInfos[cull];
//if(!culling.anyVisible())
{
uint index;
InterlockedAdd(head, 1, index);
+2 -2
View File
@@ -8,7 +8,7 @@ groupshared MeshData mesh;
[numthreads(TASK_GROUP_SIZE, 1, 1)]
[shader("amplification")]
void taskMain(
uint threadID: SV_GroupIndex,
uint threadID: SV_GroupThreadID,
uint groupID: SV_GroupID, )
{
if (threadID == 0)
@@ -25,7 +25,7 @@ void taskMain(
uint m = p.meshletOffset + i;
uint cull = p.cullingOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
MeshletCullingInfo culling = pScene.culledMeshlets[cull];
MeshletCullingInfo culling = pScene.cullingInfos[cull];
if(culling.anyVisible())
{
uint index;
+2 -2
View File
@@ -15,7 +15,7 @@ struct PrimitiveAttributes
[outputtopology("triangle")]
[shader("mesh")]
void meshMain(
in uint threadID: SV_GroupIndex,
in uint threadID: SV_GroupThreadID,
in uint groupID: SV_GroupID,
in payload MeshPayload meshPayload,
out vertices FragmentParameter vertices[MAX_VERTICES],
@@ -27,7 +27,7 @@ void meshMain(
uint meshletId = meshPayload.cullingOffset + meshletNumber;
InstanceData inst = pScene.instances[meshPayload.instanceId];
MeshletDescription m = pScene.meshletInfos[meshPayload.meshletOffset + meshletNumber];
MeshletCullingInfo cull = pScene.culledMeshlets[meshletId];
MeshletCullingInfo cull = pScene.cullingInfos[meshletId];
SetMeshOutputCounts(m.vertexCount, m.primitiveCount);
for(uint i = threadID; i < MAX_PRIMITIVES; i += MESH_GROUP_SIZE)
+4 -24
View File
@@ -8,35 +8,15 @@ struct VisibilityCullingData
};
ParameterBlock<VisibilityCullingData> pVisibilityParams;
groupshared MeshletCullingInfo cullInfo;
[numthreads(BLOCK_SIZE, 1, 1)]
[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)]
[shader("compute")]
void computeMain(
uint threadID: SV_GroupIndex,
uint groupID: SV_GroupID,
uint3 dispatchThreadID: SV_DispatchThreadID,
){
if (threadID < MAX_PRIMITIVES / 32)
{
cullInfo.visible[threadID] = 0;
}
GroupMemoryBarrierWithGroupSync();
for (uint y = 0; y < pViewParams.screenDimensions.y; y++)
{
for (uint x = threadID; x < pViewParams.screenDimensions.x; x += BLOCK_SIZE)
{
int3 texCoords = int3(x, y, 0);
int3 texCoords = int3(dispatchThreadID.xy, 0);
uint encoded = pVisibilityParams.visibilityTexture.Load(texCoords).r;
uint2 decoded = decodePrimitive(encoded);
uint base = decoded.y == groupID ? 1 : 0;
uint arrIdx = decoded.x / 32;
uint bit = decoded.x % 32;
cullInfo.visible[arrIdx] |= (base << bit);
}
}
GroupMemoryBarrierWithGroupSync();
if (threadID < MAX_PRIMITIVES / 32)
{
pVisibilityParams.cullingInfos[groupID].visible[threadID] = cullInfo.visible[threadID];
}
pVisibilityParams.cullingInfos[decoded.y].visible[arrIdx] |= (1 << bit);
}
+1 -1
View File
@@ -66,8 +66,8 @@ struct Scene
StructuredBuffer<MeshletDescription> meshletInfos;
StructuredBuffer<uint8_t> primitiveIndices;
StructuredBuffer<uint32_t> vertexIndices;
StructuredBuffer<MeshletCullingInfo> culledMeshlets;
StructuredBuffer<uint32_t> cullingOffsets;
StructuredBuffer<MeshletCullingInfo> cullingInfos;
};
layout(set=2)
ParameterBlock<Scene> pScene;
+2
View File
@@ -89,6 +89,8 @@ public:
bool writeOnly = true) = 0;
virtual void unmap() = 0;
virtual void clear() = 0;
protected:
// Inherited via QueueOwnedResource
virtual void executeOwnershipBarrier(QueueType newOwner) = 0;
+6 -15
View File
@@ -85,19 +85,6 @@ void BasePass::beginFrame(const Component::Camera& cam)
void BasePass::render()
{
oLightIndexList->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
tLightIndexList->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
oLightGrid->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
tLightGrid->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
opaqueCulling->updateBuffer(0, oLightIndexList);
opaqueCulling->updateTexture(1, oLightGrid);
transparentCulling->updateBuffer(0, tLightIndexList);
@@ -112,6 +99,8 @@ void BasePass::render()
permutation.setViewCulling(useViewCulling);
for (VertexData* vertexData : VertexData::getList())
{
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
vertexData->getInstanceDataSet()->writeChanges();
permutation.setVertexData(vertexData->getTypeName());
const auto& materials = vertexData->getMaterialData();
for (const auto& materialData : materials)
@@ -176,11 +165,11 @@ void BasePass::render()
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(viewParamsSet);
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(vertexData->getInstanceDataSet());
command->bindDescriptor(scene->getLightEnvironment()->getDescriptorSet());
command->bindDescriptor(opaqueCulling);
command->bindDescriptor(vertexData->getInstanceDataSet());
for (const auto& drawCall : materialData.instances)
{
command->bindDescriptor(drawCall.materialInstance->getDescriptorSet());
@@ -221,6 +210,8 @@ void BasePass::publishOutputs()
void BasePass::createRenderPass()
{
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
@@ -32,6 +32,8 @@ private:
PCameraActor source;
Gfx::OPipelineLayout basePassLayout;
Gfx::ODescriptorLayout lightCullingLayout;
Gfx::PShaderBuffer cullingBuffer;
};
DEFINE_REF(BasePass)
} // namespace Seele
@@ -66,6 +66,8 @@ void CachedDepthPass::render()
for (VertexData *vertexData : VertexData::getList())
{
permutation.setVertexData(vertexData->getTypeName());
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
vertexData->getInstanceDataSet()->writeChanges();
// Create Pipeline(VertexData)
// Descriptors:
@@ -120,8 +122,8 @@ void CachedDepthPass::render()
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(viewParamsSet);
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(vertexData->getInstanceDataSet());
uint32 offset = 0;
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets), &offset);
@@ -205,6 +207,8 @@ void CachedDepthPass::publishOutputs()
void CachedDepthPass::createRenderPass()
{
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
.colorAttachments = {visibilityAttachment},
.depthAttachment = depthAttachment,
@@ -21,6 +21,8 @@ private:
Gfx::OTexture2D depthBuffer;
Gfx::OTexture2D visibilityBuffer;
Gfx::OPipelineLayout depthPrepassLayout;
Gfx::PShaderBuffer cullingBuffer;
};
DEFINE_REF(CachedDepthPass)
}
@@ -66,6 +66,8 @@ void DepthPrepass::render()
for (VertexData *vertexData : VertexData::getList())
{
permutation.setVertexData(vertexData->getTypeName());
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
vertexData->getInstanceDataSet()->writeChanges();
// Create Pipeline(VertexData)
// Descriptors:
// ViewData => global, static
@@ -119,14 +121,14 @@ void DepthPrepass::render()
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(viewParamsSet);
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(vertexData->getInstanceDataSet());
uint32 offset = 0;
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets), &offset);
if (graphics->supportMeshShading())
{
//command->drawMesh(vertexData->getNumInstances(), 1, 1);
command->drawMesh(vertexData->getNumInstances(), 1, 1);
}
else
{
@@ -174,6 +176,13 @@ void DepthPrepass::render()
Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT
);
// Sync culling reads to compute write
cullingBuffer->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_MESH_SHADER_BIT_EXT,
Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT
);
}
void DepthPrepass::endFrame()
@@ -186,13 +195,15 @@ void DepthPrepass::publishOutputs()
void DepthPrepass::createRenderPass()
{
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
depthAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
visibilityAttachment = resources->requestRenderTarget("VISIBILITY");
visibilityAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
visibilityAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
visibilityAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
visibilityAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
@@ -20,6 +20,8 @@ private:
Gfx::RenderTargetAttachment depthAttachment;
Gfx::RenderTargetAttachment visibilityAttachment;
Gfx::OPipelineLayout depthPrepassLayout;
Gfx::PShaderBuffer cullingBuffer;
};
DEFINE_REF(DepthPrepass)
} // namespace Seele
@@ -81,6 +81,19 @@ void LightCullingPass::render()
commands.add(std::move(computeCommand));
//std::cout << "Execute" << std::endl;
graphics->executeCommands(std::move(commands));
oLightIndexList->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
tLightIndexList->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
oLightGrid->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
tLightGrid->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
}
void LightCullingPass::endFrame()
@@ -1,4 +1,5 @@
#include "VisibilityPass.h"
#include "Graphics/Shader.h"
using namespace Seele;
@@ -15,15 +16,46 @@ VisibilityPass::~VisibilityPass()
void VisibilityPass::beginFrame(const Component::Camera& cam)
{
RenderPass::beginFrame(cam);
visibilityDescriptor->reset();
visibilitySet = visibilityDescriptor->allocateDescriptorSet();
visibilitySet->updateTexture(0, visibilityAttachment->getTexture());
visibliitySet->updateBuffer(StaticMeshVertexData::getInstance()->get)
//Array<VertexData::MeshletCullingInfo> cullingData(VertexData::getMeshletCount());
//std::memset(cullingData.data(), 0xffff, cullingData.size() * sizeof(VertexData::MeshletCullingInfo));
//cullingBuffer->updateContents(ShaderBufferCreateInfo{
// .sourceData = {
// .size = VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo),
// .data = (uint8 *)cullingData.data(),
// },
// .numElements = VertexData::getMeshletCount()});
//cullingBuffer->pipelineBarrier(
// Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
// Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
// Gfx::SE_ACCESS_MEMORY_WRITE_BIT,
// Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
}
void VisibilityPass::render()
{
cullingBuffer->rotateBuffer(VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo));
cullingBuffer->clear();
visibilityDescriptor->reset();
visibilitySet = visibilityDescriptor->allocateDescriptorSet();
visibilitySet->updateTexture(0, visibilityAttachment.getTexture());
visibilitySet->updateBuffer(1, cullingBuffer);
visibilitySet->writeChanges();
Gfx::OComputeCommand command = graphics->createComputeCommand("VisibilityCommand");
command->bindPipeline(visibilityPipeline);
command->bindDescriptor({viewParamsSet, visibilitySet});
command->dispatch(threadGroupSize.x, threadGroupSize.y, threadGroupSize.z);
Array<Gfx::OComputeCommand> commands;
commands.add(std::move(command));
graphics->executeCommands(std::move(commands));
cullingBuffer->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);
}
void VisibilityPass::endFrame()
@@ -33,7 +65,10 @@ void VisibilityPass::endFrame()
void VisibilityPass::publishOutputs()
{
visibilityDescriptor = graphcis->createDescriptorLayout("pVisibilityParams");
uint32_t viewportWidth = viewport->getWidth();
uint32_t viewportHeight = viewport->getHeight();
threadGroupSize = glm::ceil(glm::vec3(viewportWidth / (float)BLOCK_SIZE, viewportHeight / (float)BLOCK_SIZE, 1));
visibilityDescriptor = graphics->createDescriptorLayout("pVisibilityParams");
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, });
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT, });
visibilityDescriptor->create();
@@ -55,6 +90,9 @@ void VisibilityPass::publishOutputs()
pipelineInfo.computeShader = visibilityShader;
pipelineInfo.pipelineLayout = std::move(visibilityLayout);
visibilityPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
cullingBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{.dynamic = true, .name = "CullingBuffer"});
resources->registerBufferOutput("CULLINGBUFFER", cullingBuffer);
}
void VisibilityPass::createRenderPass()
@@ -15,16 +15,18 @@ public:
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
uint64 allocate
private:
static constexpr uint32 BLOCK_SIZE = 32;
Gfx::RenderTargetAttachment visibilityAttachment;
Gfx::PDescriptorSet visibilitySet;
Gfx::ODescriptorLayout visibilityDescriptor;
Gfx::OPipelineLayout visibilityLayout;
Gfx::OComputeShader visibilityShader;
Gfx::PComputePipeline visibilityPipeline;
// Holds culling information for every meshlet for each instance
Gfx::OShaderBuffer cullingBuffer;
glm::uvec3 threadGroupSize;
};
DEFINE_REF(VisibilityPass)
}
+2 -1
View File
@@ -36,12 +36,13 @@ void ShaderCompiler::registerVertexData(VertexData *vd)
void ShaderCompiler::registerRenderPass(std::string name, PassConfig config)
{
passes[name] = config;
passes[name] = std::move(config);
compile();
}
ShaderPermutation ShaderCompiler::getTemplate(std::string name)
{
std::scoped_lock lock(shadersLock);
ShaderPermutation permutation;
PassConfig &pass = passes[name];
if (pass.useMeshShading)
+23 -28
View File
@@ -12,6 +12,9 @@
using namespace Seele;
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024 * 1024;
Map<VertexData::MeshMapping, VertexData::CullingMapping> VertexData::instanceIdMap;
uint64 VertexData::instanceCount = 0;
uint64 VertexData::meshletCount = 0;
void VertexData::resetMeshData()
{
@@ -35,7 +38,7 @@ void VertexData::resetMeshData()
}
}
void VertexData::updateMesh(PMesh mesh, Component::Transform &transform)
void VertexData::updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Component::Transform &transform)
{
std::unique_lock l(materialDataLock);
PMaterialInstance referencedInstance = mesh->referencedMaterial->getHandle();
@@ -59,7 +62,9 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform &transform)
.inverseTransformMatrix = glm::inverse(transformMatrix),
});
const auto &data = meshData[mesh->id];
auto [instanceId, meshletOffset] = getCullingMapping(id, meshIndex, data.numMeshlets);
matInstanceData.instanceMeshData.add(data);
matInstanceData.cullingOffsets.add(meshletOffset);
referencedInstance->updateDescriptor();
for (size_t i = 0; i < 0; ++i)
{
@@ -120,7 +125,7 @@ void VertexData::createDescriptors()
// instance.numMeshlets = 0;
for (size_t i = 0; i < instance.instanceData.size(); ++i)
{
cullingOffsets.add(numMeshlets);
cullingOffsets.add(instance.cullingOffsets[i]);
instanceData.add(instance.instanceData[i]);
instanceMeshData.add(instance.instanceMeshData[i]);
// instance.numMeshlets += instance.instanceMeshData[i].numMeshlets;
@@ -129,8 +134,6 @@ void VertexData::createDescriptors()
}
}
}
Array<MeshletCullingInfo> cullingData(numMeshlets);
std::memset(cullingData.data(), 0xffff, cullingData.size() * sizeof(MeshletCullingInfo));
cullingOffsetBuffer->rotateBuffer(cullingOffsets.size() * sizeof(uint32));
cullingOffsetBuffer->updateContents(ShaderBufferCreateInfo{
.sourceData = {
@@ -144,19 +147,6 @@ void VertexData::createDescriptors()
Gfx::SE_ACCESS_MEMORY_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
cullingBuffer->rotateBuffer(cullingData.size() * sizeof(MeshletCullingInfo));
cullingBuffer->updateContents(ShaderBufferCreateInfo{
.sourceData = {
.size = cullingData.size() * sizeof(MeshletCullingInfo),
.data = (uint8 *)cullingData.data(),
},
.numElements = cullingData.size()});
cullingBuffer->pipelineBarrier(
Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_MEMORY_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
instanceBuffer->rotateBuffer(instanceData.size() * sizeof(InstanceData));
instanceBuffer->updateContents(ShaderBufferCreateInfo{
.sourceData = {
@@ -189,10 +179,7 @@ void VertexData::createDescriptors()
descriptorSet->updateBuffer(2, meshletBuffer);
descriptorSet->updateBuffer(3, primitiveIndicesBuffer);
descriptorSet->updateBuffer(4, vertexIndicesBuffer);
descriptorSet->updateBuffer(5, cullingBuffer);
descriptorSet->updateBuffer(6, cullingOffsetBuffer);
descriptorSet->writeChanges();
descriptorSet->updateBuffer(5, cullingOffsetBuffer);
}
void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet> loadedMeshlets)
@@ -339,19 +326,17 @@ void VertexData::init(Gfx::PGraphics _graphics)
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
// vertexIndices
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
// cullingList
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
// cullingOffset
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
// cullingInfos
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 6, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
instanceDataLayout->create();
cullingOffsetBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.dynamic = true,
.name = "MeshletOffset",
});
cullingBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.dynamic = true,
.name = "MeshletCulling",
});
instanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.dynamic = true,
.name = "InstanceBuffer",
@@ -360,7 +345,6 @@ void VertexData::init(Gfx::PGraphics _graphics)
.dynamic = true,
.name = "MeshDataBuffer",
});
instanceDataLayout->create();
resizeBuffers();
graphics->getShaderCompiler()->registerVertexData(this);
}
@@ -378,6 +362,17 @@ void VertexData::destroy()
materialData.clear();
}
VertexData::CullingMapping VertexData::getCullingMapping(entt::entity id, uint32 meshIndex, uint32 numMeshlets)
{
MeshMapping key = MeshMapping{.id = id, .meshId = meshIndex};
if (!instanceIdMap.contains(key))
{
instanceIdMap[key] = CullingMapping{.instanceId = instanceCount++, .cullingOffset = uint32(meshletCount)};
meshletCount += numMeshlets;
}
return instanceIdMap[key];
}
VertexData::VertexData()
: idCounter(0), head(0), verticesAllocated(0), dirty(false)
{
+23 -1
View File
@@ -7,6 +7,7 @@
#include "Graphics/Descriptor.h"
#include "Graphics/Buffer.h"
#include "Meshlet.h"
#include <entt/entt.hpp>
constexpr uint64 MAX_TEXCOORDS = 8;
@@ -56,6 +57,7 @@ namespace Seele
DrawCallOffsets offsets;
Array<InstanceData> instanceData;
Array<MeshData> instanceMeshData;
Array<uint32> cullingOffsets;
};
struct MaterialData
{
@@ -63,7 +65,7 @@ namespace Seele
Array<BatchedDrawCall> instances;
};
void resetMeshData();
void updateMesh(PMesh mesh, Component::Transform &transform);
void updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Component::Transform &transform);
void createDescriptors();
void loadMesh(MeshId id, Array<uint32> indices, Array<Meshlet> meshlets);
MeshId allocateVertexData(uint64 numVertices);
@@ -87,6 +89,15 @@ namespace Seele
virtual void init(Gfx::PGraphics graphics);
virtual void destroy();
struct CullingMapping
{
uint64 instanceId;
uint32 cullingOffset;
};
static CullingMapping getCullingMapping(entt::entity id, uint32 meshIndex, uint32 numMeshlets);
static uint64 getInstanceCount() { return instanceCount; }
static uint64 getMeshletCount() { return meshletCount; }
protected:
virtual void resizeBuffers() = 0;
virtual void updateBuffers() = 0;
@@ -111,6 +122,17 @@ namespace Seele
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
Array<uint32> indices;
struct MeshMapping
{
entt::entity id;
uint32 meshId;
auto operator<=>(const MeshMapping& other) const = default;
};
static Map<MeshMapping, CullingMapping> instanceIdMap;
static uint64 instanceCount;
static uint64 meshletCount;
Gfx::PGraphics graphics;
Gfx::ODescriptorLayout instanceDataLayout;
// for mesh shading
+5
View File
@@ -389,6 +389,11 @@ void *ShaderBuffer::mapRegion(uint64 offset, uint64 size, bool writeOnly) {
void ShaderBuffer::unmap() { Vulkan::Buffer::unmap(); }
void ShaderBuffer::clear()
{
vkCmdFillBuffer(graphics->getQueueCommands(owner)->getCommands()->getHandle(), Vulkan::Buffer::getHandle(), 0, VK_WHOLE_SIZE, 0);
}
void ShaderBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
Gfx::QueueOwnedResource::transferOwnership(newOwner);
}
+2
View File
@@ -133,6 +133,8 @@ public:
virtual void *mapRegion(uint64 offset, uint64 size, bool writeOnly) override;
virtual void unmap() override;
virtual void clear() override;
protected:
// Inherited via Vulkan::Buffer
virtual VkAccessFlags getSourceAccessMask() override;
+4 -2
View File
@@ -27,10 +27,11 @@ public:
void setupView(Dependencies<Deps...>)
{
//List<std::function<void()>> work;
registry.view<Components..., Deps...>().each([&](Components&... comp, Deps&... deps){
registry.view<Components..., Deps...>().each([&](entt::entity id, Components&... comp, Deps&... deps){
//work.add([&]() {
(accessComponent(deps), ...);
update(comp...);
update(id, comp...);
//});
});
//getThreadPool().runAndWait(std::move(work));
@@ -41,7 +42,8 @@ public:
setupView((getDependencies<Components>() | ...));
}
virtual void update() override {}
virtual void update(Components&... components) = 0;
virtual void update(Components&... components) {}
virtual void update(entt::entity id, Components&... components) {}
};
} // namespace System
} // namespace Seele
+3 -3
View File
@@ -14,10 +14,10 @@ MeshUpdater::~MeshUpdater()
{
}
void MeshUpdater::update(Component::Transform& transform, Component::Mesh& comp)
void MeshUpdater::update(entt::entity id, Component::Transform& transform, Component::Mesh& comp)
{
for (auto& mesh : comp.asset->meshes)
for (uint32 i = 0; i < comp.asset->meshes.size(); ++i)
{
mesh->vertexData->updateMesh(mesh, transform);
comp.asset->meshes[i]->vertexData->updateMesh(id, i, comp.asset->meshes[i], transform);
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ class MeshUpdater : public ComponentSystem<Component::Transform, Component::Mesh
public:
MeshUpdater(PScene scene);
virtual ~MeshUpdater();
virtual void update(Component::Transform& transform, Component::Mesh& mesh) override;
virtual void update(entt::entity id, Component::Transform& transform, Component::Mesh& mesh) override;
private:
};
} // namespace System
+2
View File
@@ -9,6 +9,7 @@
#include "System/CameraUpdater.h"
#include "Graphics/RenderPass/CachedDepthPass.h"
#include "Graphics/RenderPass/DepthPrepass.h"
#include "Graphics/RenderPass/VisibilityPass.h"
#include "Graphics/RenderPass/LightCullingPass.h"
#include "Graphics/RenderPass/BasePass.h"
#include "Graphics/RenderPass/SkyboxRenderPass.h"
@@ -28,6 +29,7 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate
reloadGame();
renderGraph.addPass(new CachedDepthPass(graphics, scene));
renderGraph.addPass(new DepthPrepass(graphics, scene));
renderGraph.addPass(new VisibilityPass(graphics, scene));
renderGraph.addPass(new LightCullingPass(graphics, scene));
renderGraph.addPass(new BasePass(graphics, scene));
renderGraph.addPass(new DebugPass(graphics, scene));