Fixed meshlet culling

This commit is contained in:
Dynamitos
2024-03-31 10:21:09 +02:00
parent 82baf898a6
commit 578320cf07
16 changed files with 160 additions and 67 deletions
+15 -14
View File
@@ -4,25 +4,26 @@ struct DebugVertex
{
float3 position;
float3 color;
}
struct VertexOutput
{
float4 clipPosition : SV_Position;
float3 color : COLOR0;
};
[shader("vertex")]
VertexOutput vertexMain(DebugVertex input)
struct Params
{
VertexOutput output;
output.clipPosition = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, float4(input.position, 1.0f)));
output.color = input.color;
return output;
float4 pos: SV_Position;
float3 color: COLOR0;
}
[shader("vertex")]
Params vertexMain(
DebugVertex vert
){
Params result;
result.pos = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, float4(vert.position, 1)));
result.color = vert.color;
return result;
}
[shader("pixel")]
float4 fragmentMain(VertexOutput input)
float4 fragmentMain(in Params params) : SV_Target
{
return float4(input.color, 1.0f);
return float4(params.color, 1);
}
+14 -15
View File
@@ -12,7 +12,7 @@ struct MeshPayload
groupshared MeshPayload p;
groupshared uint head;
groupshared float4x4 localToClip;
groupshared float4x4 localToView;
groupshared Frustum viewFrustum;
[numthreads(TASK_GROUP_SIZE, 1, 1)]
@@ -26,19 +26,18 @@ void taskMain(
if(threadID == 0)
{
head = 0;
localToClip = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, instance.transformMatrix));
// Left
viewFrustum.sides[0].n = float3(1, 0, 0);
viewFrustum.sides[0].d = -1;
// Right
viewFrustum.sides[1].n = float3(-1, 0, 0);
viewFrustum.sides[1].d = -1;
// Top
viewFrustum.sides[2].n = float3(0, -1, 0);
viewFrustum.sides[2].d = -1;
// Bottom
viewFrustum.sides[3].n = float3(0, 1, 0);
viewFrustum.sides[3].d = -1;
localToView = mul(pViewParams.viewMatrix, instance.transformMatrix);
float3 origin = float3(0, 0, 0);
float3 corners[4] = {
screenToView(float4(pViewParams.screenDimensions, -1.0f, 1.0f)).xyz,
screenToView(float4(pViewParams.screenDimensions, -1.0f, 1.0f)).xyz,
screenToView(float4(pViewParams.screenDimensions, -1.0f, 1.0f)).xyz,
screenToView(float4(pViewParams.screenDimensions, -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];
@@ -48,7 +47,7 @@ void taskMain(
{
uint m = mesh.meshletOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
//if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum))
if(meshlet.bounding.insideFrustum(localToView, viewFrustum))
{
uint index;
InterlockedAdd(head, 1, index);
+13 -10
View File
@@ -11,17 +11,20 @@ struct BoundingSphere
{
return centerRadius.w;
}
bool pointInside(float3 p)
{
if(abs(getCenter() - p) > getRadius())
{
return false;
}
return true;
}
bool insideFrustum(float4x4 transform, Frustum frustum)
{
float3 transformed = mul(transform, float4(getCenter(), 1)).xyz;
float maxScale = max(max(transform[0][0], transform[1][1]), transform[2][2]);
float scaledRange = getRadius() * maxScale;
bool result = true;
for(int i = 0; i < 4 && result; ++i)
{
if(dot(frustum.sides[i].n, transformed) - frustum.sides[i].d < scaledRange)
{
result = false;
}
}
return result;
}
};
@@ -45,7 +48,7 @@ struct AABB
for(int i = 0; i < 8; ++i)
{
float3 adjusted = corners[i].xyz / corners[i].w;
if(frustum.pointInside(adjusted))
if(frustum.pointInside(corners[i].xyz))
{
return true;
}
+1 -1
View File
@@ -2,7 +2,7 @@ import Common;
import VertexData;
import MaterialParameter;
struct StaticMeshVertexData : IVertexData
struct DebugVertexData : IVertexData
{
VertexAttributes getAttributes(uint index)
{
+2 -2
View File
@@ -2,7 +2,7 @@ import Bounding;
struct MeshletDescription
{
BoundingSphere bounding;
AABB bounding;
uint32_t vertexCount;
uint32_t primitiveCount;
uint32_t vertexOffset;
@@ -13,7 +13,7 @@ struct MeshletDescription
struct MeshData
{
BoundingSphere bounding;
AABB bounding;
uint32_t numMeshlets;
uint32_t meshletOffset;
uint32_t firstIndex;
+1 -1
View File
@@ -194,7 +194,7 @@ struct ColorBlendState
SeBlendFactor srcAlphaBlendFactor = Gfx::SE_BLEND_FACTOR_SRC_ALPHA;
SeBlendFactor dstAlphaBlendFactor = Gfx::SE_BLEND_FACTOR_SRC_ALPHA;
SeBlendOp alphaBlendOp = Gfx::SE_BLEND_OP_ADD;
SeColorComponentFlags colorWriteMask = 0;
SeColorComponentFlags colorWriteMask = Gfx::SE_COLOR_COMPONENT_R_BIT | Gfx::SE_COLOR_COMPONENT_G_BIT | Gfx::SE_COLOR_COMPONENT_B_BIT | Gfx::SE_COLOR_COMPONENT_A_BIT;
};
uint32 logicOpEnable;
SeLogicOp logicOp = Gfx::SE_LOGIC_OP_OR;
+1 -1
View File
@@ -58,7 +58,7 @@ void BasePass::render()
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
depthAttachment.getTexture()->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
descriptorSets[INDEX_VIEW_PARAMS] = viewParamsSet;
descriptorSets[INDEX_LIGHT_ENV] = scene->getLightEnvironment()->getDescriptorSet();
+37 -8
View File
@@ -56,6 +56,7 @@ void DebugPass::render()
renderCommand->draw((uint32)gDebugVertices.size(), 1, 0, 0);
graphics->executeCommands(Array{renderCommand});
graphics->endRenderPass();
gDebugVertices.clear();
}
void DebugPass::endFrame()
@@ -80,15 +81,41 @@ void DebugPass::createRenderPass()
.depthAttachment = depthAttachment,
};
Gfx::SubPassDependency dependency = {
.srcSubpass = ~0U,
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
Array<Gfx::SubPassDependency> dependency = {
{
.srcSubpass = ~0U,
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
{
.srcSubpass = ~0U,
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccess = Gfx::SE_ACCESS_NONE,
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
},
{
.srcSubpass = 0,
.dstSubpass = ~0U,
.srcStage = Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
{
.srcSubpass = 0,
.dstSubpass = ~0U,
.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,
.dstAccess = Gfx::SE_ACCESS_NONE,
},
};
renderPass = graphics->createRenderPass(std::move(layout), {dependency}, viewport);
renderPass = graphics->createRenderPass(std::move(layout), dependency, viewport);
Gfx::OPipelineLayout pipelineLayout = graphics->createPipelineLayout();
pipelineLayout->addDescriptorLayout(0, viewParamsLayout);
@@ -138,5 +165,7 @@ void DebugPass::createRenderPass()
gfxInfo.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_LINE_LIST;
gfxInfo.pipelineLayout = std::move(pipelineLayout);
gfxInfo.renderPass = renderPass;
gfxInfo.colorBlend.attachmentCount = 1;
gfxInfo.rasterizationState.lineWidth = 5.f;
pipeline = graphics->createGraphicsPipeline(std::move(gfxInfo));
}
@@ -52,7 +52,7 @@ void LightCullingPass::beginFrame(const Component::Camera& cam)
void LightCullingPass::render()
{
depthAttachment->pipelineBarrier(
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
depthAttachment->transferOwnership(Gfx::QueueType::COMPUTE);
cullingDescriptorSet->updateTexture(0, depthAttachment);
@@ -70,6 +70,7 @@ void LightCullingPass::render()
Array<Gfx::PComputeCommand> commands = {computeCommand};
//std::cout << "Execute" << std::endl;
graphics->executeCommands(commands);
graphics->waitDeviceIdle();
}
void LightCullingPass::endFrame()
+39 -2
View File
@@ -44,6 +44,43 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
},
.data = data,
});
for (size_t i = 0; i < data.numMeshlets; ++i)
{
auto bounding = meshlets[data.meshletOffset + i].bounding;
StaticArray<Vector, 8> corners;
corners[0] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.min.z, 1);
corners[1] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.max.z, 1);
corners[2] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.min.z, 1);
corners[3] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.max.z, 1);
corners[4] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.min.z, 1);
corners[5] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.max.z, 1);
corners[6] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.min.z, 1);
corners[7] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.max.z, 1);
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
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();
@@ -127,7 +164,7 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3));
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8));
meshlets.add(MeshletDescription{
.boundingBox = m.boundingBox,
.bounding = m.boundingBox,
.vertexCount = m.numVertices,
.primitiveCount = m.numPrimitives,
.vertexOffset = vertexOffset,
@@ -136,7 +173,7 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
});
}
meshData[id].add(MeshData{
.boundingBox = meshAABB,
.bounding = meshAABB,
.numMeshlets = numMeshlets,
.meshletOffset = meshletOffset,
.indicesOffset = (uint32)meshOffsets[id],
+2 -2
View File
@@ -32,7 +32,7 @@ public:
};
struct MeshData
{
AABB boundingBox;
AABB bounding;
uint32 numMeshlets = 0;
uint32 meshletOffset = 0;
uint32 firstIndex = 0;
@@ -85,7 +85,7 @@ protected:
VertexData();
struct MeshletDescription
{
AABB boundingBox;
AABB bounding;
uint32_t vertexCount;
uint32_t primitiveCount;
uint32_t vertexOffset;
-4
View File
@@ -10,10 +10,6 @@ VkBool32 Seele::Vulkan::debugCallback(
void* pUserData)
{
std::cerr << pCallbackData->pMessage << std::endl;
if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) || (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT))
{
return VK_FALSE;
}
return VK_FALSE;
}
+1 -1
View File
@@ -430,7 +430,7 @@ void Graphics::setupDebugCallback()
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
.pNext = nullptr,
.flags = 0,
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT| VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
.pfnUserCallback = &debugCallback,
.pUserData = nullptr,
+27
View File
@@ -5,12 +5,39 @@
#include "Graphics/DebugVertex.h"
namespace Seele
{
struct BoundingSphere
{
Vector center;
float radius;
};
struct AABB
{
Vector min = Vector(std::numeric_limits<float>::max());
float pad0; // So that it can be used directly in shaders
Vector max = Vector(std::numeric_limits<float>::lowest());// cause of reasons
float pad1;
BoundingSphere toSphere() const
{
Vector center = (min + max) / 2.f;
StaticArray<Vector, 8> corners;
corners[0] = Vector(min.x, min.y, min.z);
corners[1] = Vector(min.x, min.y, max.z);
corners[2] = Vector(min.x, max.y, min.z);
corners[3] = Vector(min.x, max.y, max.z);
corners[4] = Vector(max.x, min.y, min.z);
corners[5] = Vector(max.x, min.y, max.z);
corners[6] = Vector(max.x, max.y, min.z);
corners[7] = Vector(max.x, max.y, max.z);
float radius = 0;
for (const auto& corner : corners)
{
radius = std::max<float>(radius, glm::length(center - corner));
}
return BoundingSphere{
.center = center,
.radius = radius,
};
}
void visualize(Array<DebugVertex>& vertices) const
{
StaticArray<DebugVertex, 8> corners;
+2 -2
View File
@@ -19,8 +19,8 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate
, renderGraph(RenderGraphBuilder::build(
DepthPrepass(graphics, scene),
LightCullingPass(graphics, scene),
BasePass(graphics, scene)
//DebugPass(graphics, scene),
BasePass(graphics, scene),
DebugPass(graphics, scene)
//SkyboxRenderPass(graphics, scene)
))
{
+2 -2
View File
@@ -35,8 +35,8 @@ protected:
RenderGraph<
DepthPrepass,
LightCullingPass,
BasePass
//DebugPass,
BasePass,
DebugPass
//SkyboxRenderPass
> renderGraph;