Using global meshlet culling list

This commit is contained in:
Dynamitos
2024-05-12 19:36:32 +02:00
parent 7fc7ba56d4
commit 2762f9e729
22 changed files with 399 additions and 324 deletions
+2 -59
View File
@@ -1,63 +1,8 @@
import Common;
import BRDF;
import Scene;
import VertexData;
import MaterialParameter;
struct MeshPayload
{
uint instanceId[MAX_MESHLETS_PER_MESH];
uint meshletId[MAX_MESHLETS_PER_MESH];
};
groupshared MeshPayload p;
groupshared uint head;
groupshared Frustum viewFrustum;
[numthreads(TASK_GROUP_SIZE, 1, 1)]
[outputtopology("triangle")]
[shader("amplification")]
void taskMain(
uint threadID: SV_GroupIndex,
uint groupID: SV_GroupID
){
InstanceData instance = pScene.instances[groupID];
if(threadID == 0)
{
head = 0;
float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz;
const float offset = 0.0f;
float3 corners[4] = {
screenToModel(instance.inverseTransformMatrix, float4(offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions.x - offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(offset, pViewParams.screenDimensions.y - offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions - float2(offset, offset), -1.0f, 1.0f)).xyz
};
viewFrustum.sides[0] = computePlane(origin, corners[2], corners[0]);
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]);
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
}
GroupMemoryBarrierWithGroupSync();
MeshData mesh = pScene.meshData[groupID];
for(uint i = threadID; i < MAX_MESHLETS_PER_MESH; i += TASK_GROUP_SIZE)
{
if(i < mesh.numMeshlets)
{
uint m = mesh.meshletOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
if(meshlet.bounding.insideFrustum(viewFrustum))
{
uint index;
InterlockedAdd(head, 1, index);
p.meshletId[index] = m;
p.instanceId[index] = groupID;
}
}
}
GroupMemoryBarrierWithGroupSync();
DispatchMesh(head, 1, 1, p);
}
struct PrimitiveAttributes
{
@@ -74,9 +19,8 @@ void meshMain(
out vertices FragmentParameter vertices[MAX_VERTICES],
out indices uint3 indices[MAX_PRIMITIVES]
){
InstanceData inst = pScene.instances[meshPayload.instanceId[groupID]];
MeshData md = pScene.meshData[meshPayload.instanceId[groupID]];
MeshletDescription m = pScene.meshletInfos[meshPayload.meshletId[groupID]];
InstanceData inst = pScene.instances[meshPayload.instanceId];
MeshletDescription m = pScene.meshletInfos[pScene.culledMeshlets[meshPayload.cullingOffset + groupID]];
SetMeshOutputCounts(m.vertexCount, m.primitiveCount);
for(uint i = threadID; i < MAX_PRIMITIVES; i += MESH_GROUP_SIZE)
@@ -97,7 +41,6 @@ void meshMain(
VertexAttributes attr = pVertexData.getAttributes(m.indicesOffset + vertexIndex);
attr.meshletId = -1;
vertices[v] = attr.getParameter(inst.transformMatrix);
//vertices[v].vertexColor = m.color;
}
}
}
+49
View File
@@ -0,0 +1,49 @@
import Common;
import Scene;
groupshared MeshPayload p;
groupshared uint head;
groupshared Frustum viewFrustum;
[numthreads(TASK_GROUP_SIZE, 1, 1)]
[outputtopology("triangle")]
[shader("amplification")]
void taskMain(
uint threadID: SV_GroupIndex,
uint groupID: SV_GroupID
){
InstanceData instance = pScene.instances[pOffsets.instanceOffset + groupID];
MeshData mesh = pScene.meshData[pOffsets.instanceOffset + groupID];
if(threadID == 0)
{
head = 0;
float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz;
const float offset = 0.0f;
float3 corners[4] = {
screenToModel(instance.inverseTransformMatrix, float4(offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions.x - offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(offset, pViewParams.screenDimensions.y - offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions - float2(offset, offset), -1.0f, 1.0f)).xyz
};
viewFrustum.sides[0] = computePlane(origin, corners[2], corners[0]);
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]);
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
p.instanceId = pOffsets.instanceOffset + groupID;
p.cullingOffset = pScene.cullingOffsets[pOffsets.cullingCounterOffset + groupID];
}
GroupMemoryBarrierWithGroupSync();
for(uint i = threadID; i < mesh.numMeshlets; i += TASK_GROUP_SIZE)
{
uint m = mesh.meshletOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
//if(meshlet.bounding.insideFrustum(viewFrustum))
{
uint index;
InterlockedAdd(head, 1, index);
pScene.culledMeshlets[p.cullingOffset + index] = m;
}
}
GroupMemoryBarrierWithGroupSync();
DispatchMesh(head, 1, 1, p);
}
+16 -1
View File
@@ -24,7 +24,6 @@ static const uint MAX_VERTICES = 256;
static const uint MAX_PRIMITIVES = 256;
static const uint TASK_GROUP_SIZE = 128;
static const uint MESH_GROUP_SIZE = 32;
static const uint MAX_MESHLETS_PER_MESH = 512;
struct InstanceData
{
@@ -32,6 +31,14 @@ struct InstanceData
float4x4 inverseTransformMatrix;
};
struct DrawCallOffsets
{
uint32_t instanceOffset;
uint32_t cullingCounterOffset;
};
layout(push_constant)
ConstantBuffer<DrawCallOffsets> pOffsets;
struct Scene
{
StructuredBuffer<InstanceData> instances;
@@ -39,7 +46,15 @@ struct Scene
StructuredBuffer<MeshletDescription> meshletInfos;
StructuredBuffer<uint8_t> primitiveIndices;
StructuredBuffer<uint32_t> vertexIndices;
StructuredBuffer<uint32_t> cullingOffsets;
RWStructuredBuffer<uint32_t> culledMeshlets;
};
layout(set=2)
ParameterBlock<Scene> pScene;
struct MeshPayload
{
uint instanceId;
uint cullingOffset;
};
+1 -4
View File
@@ -645,10 +645,7 @@ private:
else
{
// And move the current elements into that one
for(size_type i = 0; i < arraySize; ++i)
{
newData[i] = std::forward<Type>(_data[i]);
}
std::uninitialized_move(begin(), end(), Iterator(newData));
// As well as default initialize the others
for (size_type i = arraySize; i < allocated; ++i)
{
+2 -2
View File
@@ -18,7 +18,7 @@ public:
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets, Array<uint32> dynamicOffsets = {}) = 0;
virtual void bindVertexBuffer(const Array<PVertexBuffer>& buffer) = 0;
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) = 0;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) = 0;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) = 0;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) = 0;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) = 0;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) = 0;
@@ -34,7 +34,7 @@ public:
virtual void bindPipeline(Gfx::PComputePipeline pipeline) = 0;
virtual void bindDescriptor(Gfx::PDescriptorSet set, Array<uint32> dynamicOffsets = {}) = 0;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets, Array<uint32> dynamicOffsets = {}) = 0;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) = 0;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) = 0;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) = 0;
std::string name;
};
+31 -13
View File
@@ -32,10 +32,15 @@ BasePass::BasePass(Gfx::PGraphics graphics, PScene scene)
lightCullingLayout->create();
basePassLayout->addDescriptorLayout(lightCullingLayout);
basePassLayout->addPushConstants(Gfx::SePushConstantRange{
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT,
.offset = 0,
.size = sizeof(VertexData::DrawCallOffsets),
});
if (graphics->supportMeshShading())
{
graphics->getShaderCompiler()->registerRenderPass(basePassLayout, "BasePass", "MeshletPass", true, true, "BasePass", true, true, "MeshletPass");
graphics->getShaderCompiler()->registerRenderPass(basePassLayout, "BasePass", "MeshletPass", true, true, "BasePass", true, true, "ViewCullingTask");
}
else
{
@@ -84,7 +89,7 @@ void BasePass::render()
Gfx::ShaderPermutation permutation;
if (graphics->supportMeshShading())
{
permutation.setTaskFile("MeshletPass");
permutation.setTaskFile("ViewCullingTask");
permutation.setMeshFile("MeshletPass");
}
else
@@ -98,6 +103,9 @@ void BasePass::render()
const auto& materials = vertexData->getMaterialData();
for (const auto& materialData : materials)
{
// material not used for any active meshes, skip
if (materialData.instances.size() == 0)
continue;
// Create Pipeline(Material, VertexData)
// Descriptors:
// ViewData => global, static
@@ -124,7 +132,7 @@ void BasePass::render()
pipelineInfo.renderPass = renderPass;
pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL;
pipelineInfo.multisampleState.samples = viewport->getSamples();
pipelineInfo.colorBlend.attachmentCount = 1;
pipelineInfo.colorBlend.attachmentCount = 2;
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
@@ -145,13 +153,14 @@ void BasePass::render()
command->bindDescriptor(viewParamsSet);
command->bindDescriptor(scene->getLightEnvironment()->getDescriptorSet());
command->bindDescriptor(opaqueCulling);
for (const auto& instance : materialData.instances)
for (const auto& drawCall : materialData.instances)
{
command->bindDescriptor(instance.materialInstance->getDescriptorSet());
command->bindDescriptor(vertexData->getInstanceDataSet(), {instance.descriptorOffset, instance.descriptorOffset});
command->bindDescriptor(drawCall.materialInstance->getDescriptorSet());
command->bindDescriptor(vertexData->getInstanceDataSet());
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT, 0, sizeof(VertexData::DrawCallOffsets), &drawCall.offsets);
if (graphics->supportMeshShading())
{
command->drawMesh(vertexData->getMeshData(instance.meshId).size(), 1, 1);
command->drawMesh(drawCall.numMeshes, 1, 1);
}
else
{
@@ -171,7 +180,6 @@ void BasePass::render()
}
}
graphics->executeCommands(std::move(commands));
graphics->endRenderPass();
}
@@ -183,17 +191,27 @@ void BasePass::endFrame()
void BasePass::publishOutputs()
{
colorAttachment = Gfx::RenderTargetAttachment(viewport,
Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Gfx::SE_ATTACHMENT_LOAD_OP_LOAD, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment);
meshletIdTexture = graphics->createTexture2D(TextureCreateInfo{
.format = Gfx::SE_FORMAT_R32_UINT,
.width = viewport->getWidth(),
.height = viewport->getHeight(),
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
});
meshletIdAttachment = Gfx::RenderTargetAttachment(meshletIdTexture,
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("BASEPASS_MESHLETID", meshletIdAttachment);
}
void BasePass::createRenderPass()
{
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
meshletIdAttachment = resources->requestRenderTarget("BASEPASS_MESHLETID");
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
depthAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
.colorAttachments = { colorAttachment, meshletIdAttachment },
@@ -213,7 +231,7 @@ void BasePass::createRenderPass()
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.srcAccess = Gfx::SE_ACCESS_NONE,
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
},
{
@@ -25,6 +25,7 @@ private:
Gfx::PShaderBuffer tLightIndexList;
Gfx::PTexture2D oLightGrid;
Gfx::PTexture2D tLightGrid;
Gfx::OTexture2D meshletIdTexture;
Gfx::PDescriptorSet opaqueCulling;
Gfx::PDescriptorSet transparentCulling;
+30 -11
View File
@@ -18,9 +18,14 @@ DepthPrepass::DepthPrepass(Gfx::PGraphics graphics, PScene scene)
{
depthPrepassLayout = graphics->createPipelineLayout("DepthPrepassLayout");
depthPrepassLayout->addDescriptorLayout(viewParamsLayout);
depthPrepassLayout->addPushConstants(Gfx::SePushConstantRange{
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT,
.offset = 0,
.size = sizeof(VertexData::DrawCallOffsets),
});
if (graphics->supportMeshShading())
{
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "MeshletPass", false, false, "", true, true, "MeshletPass");
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "MeshletPass", false, false, "", true, true, "ViewCullingTask");
}
else
{
@@ -45,7 +50,7 @@ void DepthPrepass::render()
Gfx::ShaderPermutation permutation;
if (graphics->supportMeshShading())
{
permutation.setTaskFile("MeshletPass");
permutation.setTaskFile("ViewCullingTask");
permutation.setMeshFile("MeshletPass");
}
else
@@ -58,14 +63,17 @@ void DepthPrepass::render()
const auto& materials = vertexData->getMaterialData();
for (const auto& materialData : materials)
{
// material not used for any active meshes, skip
if (materialData.instances.size() == 0)
continue;
// Create Pipeline(VertexData)
// Descriptors:
// ViewData => global, static
// VertexData => per meshtype
// SceneData => per material instance
// SceneData => per meshtype
Gfx::PermutationId id(permutation);
Gfx::ORenderCommand command = graphics->createRenderCommand("BaseRender");
Gfx::ORenderCommand command = graphics->createRenderCommand("DepthRender");
command->setViewport(viewport);
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
@@ -105,12 +113,13 @@ void DepthPrepass::render()
}
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(viewParamsSet);
for (const auto& instance : materialData.instances)
for (const auto& drawCall : materialData.instances)
{
command->bindDescriptor(vertexData->getInstanceDataSet(), { instance.descriptorOffset, instance.descriptorOffset });
command->bindDescriptor(vertexData->getInstanceDataSet());
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT, 0, sizeof(VertexData::DrawCallOffsets), &drawCall.offsets);
if (graphics->supportMeshShading())
{
command->drawMesh(vertexData->getMeshData(instance.meshId).size(), 1, 1);
command->drawMesh(drawCall.numMeshes, 1, 1);
}
else
{
@@ -140,14 +149,24 @@ void DepthPrepass::endFrame()
void DepthPrepass::publishOutputs()
{
// If we render to a part of an image, the depth buffer itself must
// still match the size of the whole image or their coordinate systems go out of sync
TextureCreateInfo depthBufferInfo = {
.format = Gfx::SE_FORMAT_D32_SFLOAT,
.width = viewport->getOwner()->getFramebufferWidth(),
.height = viewport->getOwner()->getFramebufferHeight(),
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
};
depthBuffer = graphics->createTexture2D(depthBufferInfo);
depthAttachment =
Gfx::RenderTargetAttachment(depthBuffer,
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_GENERAL,
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
}
void DepthPrepass::createRenderPass()
{
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
depthAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
.depthAttachment = depthAttachment,
};
@@ -18,6 +18,9 @@ public:
virtual void createRenderPass() override;
private:
Gfx::RenderTargetAttachment depthAttachment;
Gfx::OTexture2D depthBuffer;
Gfx::OPipelineLayout depthPrepassLayout;
};
DEFINE_REF(DepthPrepass)
@@ -163,16 +163,6 @@ void StaticBasePass::publishOutputs()
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment);
meshletIdTexture = graphics->createTexture2D(TextureCreateInfo{
.format = Gfx::SE_FORMAT_R32_UINT,
.width = viewport->getWidth(),
.height = viewport->getHeight(),
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
});
meshletIdAttachment = Gfx::RenderTargetAttachment(meshletIdTexture,
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("BASEPASS_MESHLETID", meshletIdAttachment);
}
void StaticBasePass::createRenderPass()
@@ -132,20 +132,7 @@ void StaticDepthPrepass::endFrame()
void StaticDepthPrepass::publishOutputs()
{
// If we render to a part of an image, the depth buffer itself must
// still match the size of the whole image or their coordinate systems go out of sync
TextureCreateInfo depthBufferInfo = {
.format = Gfx::SE_FORMAT_D32_SFLOAT,
.width = viewport->getOwner()->getFramebufferWidth(),
.height = viewport->getOwner()->getFramebufferHeight(),
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
};
depthBuffer = graphics->createTexture2D(depthBufferInfo);
depthAttachment =
Gfx::RenderTargetAttachment(depthBuffer,
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
}
void StaticDepthPrepass::createRenderPass()
+1 -1
View File
@@ -85,7 +85,7 @@ void TextPass::render()
command->bindDescriptor({generalSet, resource.textureArraySet});
//command->bindVertexBuffer({resource.vertexBuffer});
command->pushConstants(pipelineLayout, Gfx::SE_SHADER_STAGE_VERTEX_BIT | Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextData), &resource.textData);
command->pushConstants(Gfx::SE_SHADER_STAGE_VERTEX_BIT | Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextData), &resource.textData);
//command->draw(4, static_cast<uint32>(resource.vertexBuffer->getNumVertices()), 0, 0);
}
commands.add(std::move(command));
+4 -5
View File
@@ -228,14 +228,13 @@ void StaticMeshVertexData::registerStaticMesh(const Array<OMesh>& meshes, const
Array<uint32> ids;
// Get references to loaded meshlets
const auto& meshData = VertexData::getMeshData(mapped);
for (const auto& md : meshData)
for (size_t i = 0; i < meshData.numMeshlets; ++i)
{
for (size_t i = 0; i < md.numMeshlets; ++i)
{
ids.add(md.meshletOffset + i);
}
ids.add(meshData.meshletOffset + i);
}
// Get Static instance array
PMaterialInstance instance = mesh->referencedMaterial->getHandle();
PMaterial baseMat = instance->getBaseMaterial();
+72 -26
View File
@@ -17,9 +17,12 @@ void VertexData::resetMeshData()
{
std::unique_lock l(materialDataLock);
for (auto& mat : materialData)
{
if (mat.material != nullptr)
{
mat.material->getDescriptorLayout()->reset();
}
}
materialData.clear();
if (dirty)
{
@@ -35,22 +38,23 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
PMaterial mat = referencedInstance->getBaseMaterial();
if (materialData.size() <= mat->getId())
{
materialData.resize(mat->getId());
materialData.resize(mat->getId() + 1);
}
MaterialData& matData = materialData[mat->getId()];
matData.material = mat;
MaterialInstanceData& matInstanceData = matData.instances[referencedInstance->getId()];
matInstanceData.descriptorOffset = instanceData.size();
matInstanceData.numMeshes = 0;
matInstanceData.meshId = mesh->id;
for (const auto& data : meshData[mesh->id])
if (matData.instances.size() <= referencedInstance->getId())
{
matData.instances.resize(referencedInstance->getId() + 1);
}
BatchedDrawCall& matInstanceData = matData.instances[referencedInstance->getId()];
const auto& data = meshData[mesh->id];
Matrix4 transformMatrix = transform.toMatrix() * mesh->transform;
instanceData.add(InstanceData {
matInstanceData.instanceData.add(InstanceData{
.transformMatrix = transformMatrix,
.inverseTransformMatrix = glm::inverse(transformMatrix),
});
instanceMeshData.add(data);
matInstanceData.instanceMeshData.add(data);
matInstanceData.numMeshes++;
for (size_t i = 0; i < 0; ++i)
{
@@ -91,7 +95,7 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
}
}
matInstanceData.materialInstance = referencedInstance;
referencedInstance->updateDescriptor();
}
@@ -99,6 +103,47 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
void VertexData::createDescriptors()
{
std::unique_lock l(materialDataLock);
instanceData.clear();
instanceMeshData.clear();
uint32 numMeshlets = 0;
Array<uint32> cullingOffsets;
for (auto& mat : materialData)
{
for (auto& instance : mat.instances)
{
instance.offsets.instanceOffset = instanceData.size();
instance.offsets.cullingCounterOffset = cullingOffsets.size();
instance.numMeshes = instance.instanceData.size();
instance.numMeshlets = 0;
for (size_t i = 0; i < instance.instanceData.size(); ++i)
{
instanceData.add(instance.instanceData[i]);
instanceMeshData.add(instance.instanceMeshData[i]);
instance.numMeshlets += instance.instanceMeshData[i].numMeshlets;
cullingOffsets.add(numMeshlets);
numMeshlets += instance.numMeshlets;
}
}
}
cullingOffsetBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = cullingOffsets.size() * sizeof(uint32),
.data = (uint8*)cullingOffsets.data(),
},
.numElements = cullingOffsets.size(),
.name = "MeshletOffset"
});
cullingBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = numMeshlets * sizeof(uint32),
},
.numElements = numMeshlets,
.name = "MeshletCulling"
});
instanceDataLayout->reset();
instanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
@@ -137,6 +182,8 @@ void VertexData::createDescriptors()
descriptorSet->updateBuffer(2, meshletBuffer);
descriptorSet->updateBuffer(3, primitiveIndicesBuffer);
descriptorSet->updateBuffer(4, vertexIndicesBuffer);
descriptorSet->updateBuffer(5, cullingOffsetBuffer);
descriptorSet->updateBuffer(6, cullingBuffer);
descriptorSet->writeChanges();
}
@@ -147,15 +194,11 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
meshlets.reserve(meshlets.size() + loadedMeshlets.size());
vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet);
primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3);
uint32 currentMesh = 0;
while (currentMesh < loadedMeshlets.size())
{
uint32 numMeshlets = std::min<uint32>(512, loadedMeshlets.size() - currentMesh);
uint32 meshletOffset = meshlets.size();
AABB meshAABB;
for (uint32 i = 0; i < numMeshlets; ++i)
for (uint32 i = 0; i < loadedMeshlets.size(); ++i)
{
Meshlet& m = loadedMeshlets[currentMesh + i];
Meshlet& m = loadedMeshlets[i];
meshAABB = meshAABB.combine(m.boundingBox);
uint32 vertexOffset = vertexIndices.size();
vertexIndices.resize(vertexOffset + m.numVertices);
@@ -173,19 +216,18 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
.indicesOffset = (uint32)meshOffsets[id],
});
}
meshData[id].add(MeshData{
meshData[id] = MeshData{
.bounding = meshAABB,//.toSphere(),
.numMeshlets = numMeshlets,
.numMeshlets = (uint32)loadedMeshlets.size(),
.meshletOffset = meshletOffset,
});
currentMesh += numMeshlets;
}
meshData[id][0].firstIndex = indices.size();
meshData[id][0].numIndices = loadedIndices.size();
};
meshData[id].firstIndex = indices.size();
meshData[id].numIndices = loadedIndices.size();
if (!graphics->supportMeshShading())
{
indices.resize(indices.size() + loadedIndices.size());
std::memcpy(indices.data() + meshData[id][0].firstIndex, loadedIndices.data(), loadedIndices.size() * sizeof(uint32));
std::memcpy(indices.data() + meshData[id].firstIndex, loadedIndices.data(), loadedIndices.size() * sizeof(uint32));
indexBuffer = graphics->createIndexBuffer(IndexBufferCreateInfo{
.sourceData = {
.size = sizeof(uint32) * indices.size(),
@@ -276,15 +318,19 @@ void VertexData::init(Gfx::PGraphics _graphics)
instanceDataLayout = graphics->createDescriptorLayout("pScene");
// instanceData
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC,});
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, });
// meshData
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC,});
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, });
// meshletData
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
// primitiveIndices
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
// vetexIndices
// 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 = 6, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
instanceDataLayout->create();
resizeBuffers();
+17 -7
View File
@@ -41,17 +41,24 @@ public:
uint32 firstIndex = 0;
uint32 numIndices = 0;
};
struct MaterialInstanceData
struct DrawCallOffsets
{
uint32 instanceOffset = 0;
uint32 cullingCounterOffset = 0;
};
struct BatchedDrawCall
{
PMaterialInstance materialInstance;
uint32 descriptorOffset;
uint64 numMeshes;
MeshId meshId;
uint64 numMeshes = 0;
uint64 numMeshlets = 0;
DrawCallOffsets offsets;
Array<InstanceData> instanceData;
Array<MeshData> instanceMeshData;
};
struct MaterialData
{
PMaterial material;
Array<MaterialInstanceData> instances;
Array<BatchedDrawCall> instances;
};
void resetMeshData();
void updateMesh(PMesh mesh, Component::Transform& transform);
@@ -70,7 +77,7 @@ public:
Gfx::PDescriptorLayout getInstanceDataLayout() { return instanceDataLayout; }
Gfx::PDescriptorSet getInstanceDataSet() { return descriptorSet; }
const Array<MaterialData>& getMaterialData() const { return materialData; }
const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; }
const MeshData& getMeshData(MeshId id) { return meshData[id]; }
static List<VertexData*> getList();
static VertexData* findByTypeName(std::string name);
virtual void init(Gfx::PGraphics graphics);
@@ -92,7 +99,7 @@ protected:
std::mutex materialDataLock;
Array<MaterialData> materialData;
std::mutex vertexDataLock;
Map<MeshId, Array<MeshData>> meshData;
Map<MeshId, MeshData> meshData;
Map<MeshId, uint64> meshOffsets;
Map<MeshId, uint64> meshVertexCounts;
Array<MeshletDescription> meshlets;
@@ -105,6 +112,9 @@ protected:
Gfx::OShaderBuffer meshletBuffer;
Gfx::OShaderBuffer vertexIndicesBuffer;
Gfx::OShaderBuffer primitiveIndicesBuffer;
// temporary meshlet culling buffer, passed from task to mesh shader
Gfx::OShaderBuffer cullingBuffer;
Gfx::OShaderBuffer cullingOffsetBuffer;
// for legacy pipeline
Gfx::OIndexBuffer indexBuffer;
// Material data
+4 -4
View File
@@ -317,10 +317,10 @@ void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer indexBuffer)
vkCmdBindIndexBuffer(handle, buf->getHandle(), 0, cast(buf->getIndexType()));
}
void RenderCommand::pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
void RenderCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
{
assert(threadId == std::this_thread::get_id());
vkCmdPushConstants(handle, layout.cast<PipelineLayout>()->getHandle(), stage, offset, size, data);
vkCmdPushConstants(handle, pipeline->getLayout(), stage, offset, size, data);
}
void RenderCommand::draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance)
@@ -443,10 +443,10 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
delete[] sets;
}
void ComputeCommand::pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
void ComputeCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
{
assert(threadId == std::this_thread::get_id());
vkCmdPushConstants(handle, layout.cast<PipelineLayout>()->getHandle(), stage, offset, size, data);
vkCmdPushConstants(handle, pipeline->getLayout(), stage, offset, size, data);
}
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ)
+2 -2
View File
@@ -76,7 +76,7 @@ public:
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets, Array<uint32> dynamicOffsets) override;
virtual void bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) override;
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset,
@@ -110,7 +110,7 @@ public:
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set, Array<uint32> dynamicOffsets) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets, Array<uint32> dynamicOffsets) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
+3 -4
View File
@@ -184,7 +184,7 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBu
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.pBufferInfo = &bufferInfos.back(),
});
@@ -203,7 +203,6 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuff
.offset = 0,
.range = vulkanBuffer->getSize(),
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
@@ -211,7 +210,7 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuff
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.pBufferInfo = &bufferInfos.back(),
});
@@ -238,7 +237,7 @@ void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuf
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.pBufferInfo = &bufferInfos.back(),
});
+5 -4
View File
@@ -16,15 +16,16 @@ Slang::ComPtr<slang::IBlob> Seele::generateShader(const ShaderCreateInfo& create
}
slang::SessionDesc sessionDesc;
sessionDesc.flags = 0;
StaticArray<slang::CompilerOptionEntry, 2> option;
StaticArray<slang::CompilerOptionEntry, 3> option;
option[0].name = slang::CompilerOptionName::IgnoreCapabilities;
option[0].value = slang::CompilerOptionValue();
option[0].value.kind = slang::CompilerOptionValueKind::Int;
option[0].value.intValue0 = 1;
option[1].name = slang::CompilerOptionName::EmitSpirvViaGLSL;
option[1].value = slang::CompilerOptionValue();
option[1].value.kind = slang::CompilerOptionValueKind::Int;
option[1].value.intValue0 = 1;
option[2].name = slang::CompilerOptionName::DebugInformationFormat;
option[2].value.kind = slang::CompilerOptionValueKind::Int;
option[2].value.intValue0 = 0;
sessionDesc.compilerOptionEntries = option.data();
sessionDesc.compilerOptionEntryCount = option.size();
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
@@ -61,7 +62,7 @@ Slang::ComPtr<slang::IBlob> Seele::generateShader(const ShaderCreateInfo& create
modules.add(mainModule);
CHECK_DIAGNOSTICS();
mainModule->findEntryPointByName(createInfo.entryPoint.c_str(), entrypoint.writeRef());
CHECK_RESULT(mainModule->findEntryPointByName(createInfo.entryPoint.c_str(), entrypoint.writeRef()));
modules.add(entrypoint);
Slang::ComPtr<slang::IComponentType> moduleComposition;
+1
View File
@@ -114,6 +114,7 @@ void Material::load(ArchiveBuffer& buffer)
layout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = binding, .descriptorType = descriptorType, .textureType = textureType, .descriptorCount = descriptorCount, .bindingFlags = bindingFlags, .shaderStages = shaderStages,});
}
layout->create();
materialId = materialIdCounter++;
}
void Material::compile()
-3
View File
@@ -17,10 +17,7 @@ MeshUpdater::~MeshUpdater()
void MeshUpdater::update(Component::Transform& transform, Component::Mesh& comp)
{
for (auto& mesh : comp.asset->meshes)
{
if (!comp.isStatic)
{
mesh->vertexData->updateMesh(mesh, transform);
}
}
}
+2 -2
View File
@@ -18,10 +18,10 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate
, gameInterface(dllPath)
{
reloadGame();
renderGraph.addPass(new StaticDepthPrepass(graphics, scene));
//renderGraph.addPass(new StaticDepthPrepass(graphics, scene));
renderGraph.addPass(new DepthPrepass(graphics, scene));
renderGraph.addPass(new LightCullingPass(graphics, scene));
renderGraph.addPass(new StaticBasePass(graphics, scene));
//renderGraph.addPass(new StaticBasePass(graphics, scene));
renderGraph.addPass(new BasePass(graphics, scene));
//renderGraph.addPass(new DebugPass(graphics, scene));
//renderGraph.addPass(new SkyboxRenderPass(graphics, scene));