From 54e66df4cdc58d0c16c74648ae46847985646541 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 27 Nov 2023 21:08:27 +0100 Subject: [PATCH] it works now??? --- res/shaders/LegacyBasePass.slang | 2 +- res/shaders/MeshletBasePass.slang | 103 +++++++-------------- res/shaders/lib/MaterialParameter.slang | 15 +-- res/shaders/lib/StaticMeshVertexData.slang | 9 +- res/shaders/lib/VertexData.slang | 2 +- src/Engine/Graphics/Vulkan/Graphics.cpp | 2 +- src/Engine/Graphics/Vulkan/Shader.cpp | 19 +++- 7 files changed, 66 insertions(+), 86 deletions(-) diff --git a/res/shaders/LegacyBasePass.slang b/res/shaders/LegacyBasePass.slang index 04b3afe..21755a3 100644 --- a/res/shaders/LegacyBasePass.slang +++ b/res/shaders/LegacyBasePass.slang @@ -9,6 +9,6 @@ FragmentParameter vertexMain( uint instanceId: SV_InstanceID, ){ InstanceData inst = pScene.instances[instanceId]; - VertexAttributes attr = pVertexData.getAttributes(vertexId, inst.transformMatrix); + VertexAttributes attr = pVertexData.getAttributes(vertexId); return attr.getParameter(inst.transformMatrix); } \ No newline at end of file diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index 9786193..007a455 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -64,21 +64,11 @@ void taskMain( DispatchMesh(head, 1, 1, p); } -groupshared VertexAttributes gs_vertices[MAX_VERTICES]; -groupshared uint3 gs_indices[MAX_PRIMITIVES]; -groupshared uint gs_numVertices; -groupshared uint gs_numPrimitives; - struct PrimitiveAttributes { uint cull: SV_CullPrimitive; }; -struct MeshOutput -{ - FragmentParameter parameter; -}; - [numthreads(MESH_GROUP_SIZE, 1, 1)] [outputtopology("triangle")] [shader("mesh")] @@ -86,73 +76,50 @@ void meshMain( in uint threadID: SV_GroupIndex, in uint groupID: SV_GroupID, in payload MeshPayload meshPayload, - out Vertices vertices, + out Vertices vertices, out Indices indices ){ InstanceData inst = pScene.instances[meshPayload.instanceId[groupID]]; MeshData md = pScene.meshData[meshPayload.instanceId[groupID]]; MeshletDescription m = pScene.meshletInfos[meshPayload.meshletId[groupID]]; - const uint vertexLoops = (MAX_VERTICES + MESH_GROUP_SIZE - 1) / MESH_GROUP_SIZE; - for(uint i = threadID; i < MAX_VERTICES; i += MESH_GROUP_SIZE) + SetMeshOutputCounts(m.vertexCount, m.primitiveCount); + + uint p = min(threadID, m.primitiveCount - 1); + { + uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; + uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; + uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; + indices[p] = uint3(local_idx0, local_idx1, local_idx2); + } + p = min(threadID + 32, m.primitiveCount - 1); + { + uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; + uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; + uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; + indices[p] = uint3(local_idx0, local_idx1, local_idx2); + } + p = min(threadID + 64, m.primitiveCount - 1); + { + uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; + uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; + uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; + indices[p] = uint3(local_idx0, local_idx1, local_idx2); + } + p = min(threadID + 96, m.primitiveCount - 1); + { + uint local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; + uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; + uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; + indices[p] = uint3(local_idx0, local_idx1, local_idx2); + } + + GroupMemoryBarrierWithGroupSync(); + for(uint i = threadID; i < MAX_VERTICES; i+=MESH_GROUP_SIZE) { uint v = min(i, m.vertexCount - 1); - InterlockedMax(gs_numVertices, v + 1); { int vertexIndex = pScene.vertexIndices[m.vertexOffset + v]; - gs_vertices[v] = pVertexData.getAttributes(md.indicesOffset + vertexIndex, inst.transformMatrix); + vertices[v] = pVertexData.getAttributes(md.indicesOffset + vertexIndex).getParameter(inst.transformMatrix); } } - GroupMemoryBarrierWithGroupSync(); - const uint primitiveLoops = (MAX_PRIMITIVES + MESH_GROUP_SIZE - 1) / MESH_GROUP_SIZE; - for(uint i = threadID; i < MAX_PRIMITIVES; i += MESH_GROUP_SIZE) - { - uint p = min(i, m.primitiveCount - 1); - InterlockedMax(gs_numPrimitives, p + 1); - { - uint32_t local_idx0 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 0]; - uint32_t local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; - uint32_t local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; - gs_indices[p].x = local_idx0; - gs_indices[p].y = local_idx1; - gs_indices[p].z = local_idx2; - } - } - GroupMemoryBarrierWithGroupSync(); - SetMeshOutputCounts(gs_numVertices, gs_numPrimitives); - GroupMemoryBarrierWithGroupSync(); - - uint v = threadID; - v = min(v, m.vertexCount - 1); - FragmentParameter parameter = gs_vertices[v].getParameter(inst.transformMatrix); - vertices[v].parameter = parameter; - if(vertexLoops >= 1) - { - uint v = threadID + MESH_GROUP_SIZE; - v = min(v, m.vertexCount - 1); - FragmentParameter parameter = gs_vertices[v].getParameter(inst.transformMatrix); - vertices[v].parameter = parameter; - } - - uint p = threadID; - p = min(p, m.primitiveCount - 1); - indices[p] = gs_indices[p]; - - if(primitiveLoops >= 1) - { - p += MESH_GROUP_SIZE; - p = min(p, m.primitiveCount - 1); - indices[p] = gs_indices[p]; - } - if(primitiveLoops >= 2) - { - p += MESH_GROUP_SIZE; - p = min(p, m.primitiveCount - 1); - indices[p] = gs_indices[p]; - } - if(primitiveLoops >= 3) - { - p += MESH_GROUP_SIZE; - p = min(p, m.primitiveCount - 1); - indices[p] = gs_indices[p]; - } } \ No newline at end of file diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index 5835b6a..9bebd86 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -51,28 +51,31 @@ struct FragmentParameter // data retrieved from VertexData struct VertexAttributes { + float3 position_MS; float3 normal_MS; float3 tangent_MS; float3 biTangent_MS; - float3 position_WS; - float4 position_CS; float2 texCoords; float3 vertexColor; FragmentParameter getParameter(float4x4 transformMatrix) { + float4 modelPos = float4(position_MS, 1); + float4 worldPos = mul(transformMatrix, modelPos); + float4 viewPos = mul(pViewParams.viewMatrix, worldPos); + float4 clipPos = mul(pViewParams.projectionMatrix, viewPos); float3 tangent_WS = mul(transformMatrix, float4(normalize(tangent_MS), 0)).xyz; float3 biTangent_WS = mul(transformMatrix, float4(normalize(biTangent_MS), 0)).xyz; float3 normal_WS = mul(transformMatrix, float4(normalize(normal_MS), 0)).xyz; // Transforms from world space into tangent space float3x3 tbn = transpose(float3x3(tangent_WS, biTangent_WS, normal_WS)); FragmentParameter result; - result.position_TS = mul(tbn, position_WS); - result.viewDir_TS = mul(tbn, pViewParams.cameraPos_WS.xyz - position_WS); + result.position_TS = mul(tbn, worldPos.xyz); + result.viewDir_TS = mul(tbn, pViewParams.cameraPos_WS.xyz - worldPos.xyz); result.normal_WS = normal_WS; result.tangent_WS = tangent_WS; result.biTangent_WS = biTangent_WS; - result.position_WS = position_WS; - result.position_CS = position_CS; + result.position_WS = worldPos.xyz; + result.position_CS = clipPos; result.texCoords = texCoords; result.vertexColor = vertexColor; return result; diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index 3112a7f..9d7d647 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -4,18 +4,13 @@ import MaterialParameter; struct StaticMeshVertexData : IVertexData { - VertexAttributes getAttributes(uint index, float4x4 transform) + VertexAttributes getAttributes(uint index) { VertexAttributes attributes; - float4 modelPos = float4(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2], 1); - float4 worldPos = mul(transform, modelPos); - float4 viewPos = mul(pViewParams.viewMatrix, worldPos); - float4 clipPos = mul(pViewParams.projectionMatrix, viewPos); + attributes.position_MS = float3(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2]); attributes.normal_MS = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]); attributes.tangent_MS = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]); attributes.biTangent_MS = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]); - attributes.position_WS = worldPos.xyz; - attributes.position_CS = clipPos; attributes.texCoords = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]); attributes.vertexColor = float3(color[3 * index + 0], color[3 * index + 1], color[3 * index + 2]); return attributes; diff --git a/res/shaders/lib/VertexData.slang b/res/shaders/lib/VertexData.slang index a6ac73a..2bb39d7 100644 --- a/res/shaders/lib/VertexData.slang +++ b/res/shaders/lib/VertexData.slang @@ -2,7 +2,7 @@ import MaterialParameter; interface IVertexData { - VertexAttributes getAttributes(uint index, float4x4 transform); + VertexAttributes getAttributes(uint index); }; layout(set = 1) ParameterBlock pVertexData; \ No newline at end of file diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index 6044a38..68870b8 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -399,7 +399,7 @@ void Graphics::pickPhysicalDevice() { if (std::strcmp(VK_EXT_MESH_SHADER_EXTENSION_NAME, extensionProps[i].extensionName) == 0) { - //meshShadingEnabled = true; + meshShadingEnabled = true; break; } } diff --git a/src/Engine/Graphics/Vulkan/Shader.cpp b/src/Engine/Graphics/Vulkan/Shader.cpp index 83fa0f1..eadb6cb 100644 --- a/src/Engine/Graphics/Vulkan/Shader.cpp +++ b/src/Engine/Graphics/Vulkan/Shader.cpp @@ -55,8 +55,12 @@ void Shader::create(const ShaderCreateInfo& createInfo) slang::TargetDesc vulkan; vulkan.profile = globalSession->findProfile("sm_6_6"); vulkan.format = SLANG_SPIRV; - sessionDesc.targetCount = 1; - sessionDesc.targets = &vulkan; + slang::TargetDesc glsl; + glsl.profile = globalSession->findProfile("sm_6_6"); + glsl.format = SLANG_GLSL; + slang::TargetDesc targets[2] = { vulkan, glsl }; + sessionDesc.targetCount = 2; + sessionDesc.targets = targets; StaticArray searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"}; sessionDesc.searchPaths = searchPaths.data(); sessionDesc.searchPathCount = searchPaths.size(); @@ -132,4 +136,15 @@ void Shader::create(const ShaderCreateInfo& createInfo) hash = CRC::Calculate(entryPointName.data(), entryPointName.size(), CRC::CRC_32()); hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32(), hash); + + specializedComponent->getEntryPointCode( + 0, + 1, + kernelBlob.writeRef(), + diagnostics.writeRef() + ); + CHECK_DIAGNOSTICS(); + std::ofstream shaderStream(createInfo.name + createInfo.entryPoint + ".glsl"); + shaderStream << (char*)kernelBlob->getBufferPointer(); + shaderStream.close(); } \ No newline at end of file