From dc28979cfd492b57885b1833785e729d8f854e02 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Tue, 12 Dec 2023 11:37:00 +0100 Subject: [PATCH] Graceful exit --- res/shaders/MeshletBasePass.slang | 2 +- res/shaders/lib/AABB.slang | 2 +- res/shaders/lib/Scene.slang | 12 +- res/shaders/test.glsl | 175 +------------------ res/shaders/test.slang | 101 +++++------ src/Editor/main.cpp | 7 +- src/Engine/Graphics/StaticMeshVertexData.cpp | 13 +- src/Engine/Graphics/StaticMeshVertexData.h | 1 + src/Engine/Graphics/VertexData.cpp | 21 ++- src/Engine/Graphics/VertexData.h | 8 +- src/Engine/Graphics/Vulkan/Buffer.cpp | 3 - src/Engine/Graphics/Vulkan/Graphics.cpp | 8 +- src/Engine/Graphics/Vulkan/Graphics.h | 1 + 13 files changed, 99 insertions(+), 255 deletions(-) diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index c67749d..75847a9 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -51,7 +51,7 @@ void taskMain( { uint m = mesh.meshletOffset + i; MeshletDescription meshlet = pScene.meshletInfos[m]; - if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) + //if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) { uint index; InterlockedAdd(head, 1, index); diff --git a/res/shaders/lib/AABB.slang b/res/shaders/lib/AABB.slang index ac565c4..89fe623 100644 --- a/res/shaders/lib/AABB.slang +++ b/res/shaders/lib/AABB.slang @@ -5,7 +5,7 @@ struct AABB float3 min; float pad0; float3 max; - float3 pad1; + float pad1; bool insideFrustum(float4x4 transform, Frustum frustum) { float3 corners[8]; diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index 5eac612..9c6b16e 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -2,7 +2,6 @@ import AABB; struct MeshletDescription { - AABB boundingBox; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; @@ -11,12 +10,11 @@ struct MeshletDescription struct MeshData { - AABB boundingBox; - uint numMeshlets; - uint meshletOffset; - uint firstIndex; - uint numIndices; - uint indicesOffset; + uint32_t meshletOffset = 0; + uint16_t numMeshlets = 0; + uint16_t indicesOffset = 0; + uint32_t firstIndex = 0; + uint32_t numIndices = 0; }; static const uint MAX_VERTICES = 64; diff --git a/res/shaders/test.glsl b/res/shaders/test.glsl index c4c67dd..f9fb965 100755 --- a/res/shaders/test.glsl +++ b/res/shaders/test.glsl @@ -1,178 +1,19 @@ #version 450 +#extension GL_EXT_mesh_shader : require layout(row_major) uniform; layout(row_major) buffer; -#line 5 0 -struct InstanceData_0 -{ - mat4x4 transformMatrix_0; -}; - - -#line 22 -layout(std430, binding = 0) readonly buffer StructuredBuffer_InstanceData_t_0 { - InstanceData_0 _data[]; -} pScene_instances_0; - -#line 5 1 -layout(std430, binding = 0, set = 2) readonly buffer StructuredBuffer_float3_t_0 { - vec3 _data[]; -} pVertexData_positions_0; - -#line 5 -layout(std430, binding = 1, set = 2) readonly buffer StructuredBuffer_float2_t_0 { - vec2 _data[]; -} pVertexData_texCoords_0; - -#line 5 -layout(std430, binding = 2, set = 2) readonly buffer StructuredBuffer_float3_t_1 { - vec3 _data[]; -} pVertexData_normals_0; - -#line 5 -layout(std430, binding = 3, set = 2) readonly buffer StructuredBuffer_float3_t_2 { - vec3 _data[]; -} pVertexData_tangents_0; - -#line 5 -layout(std430, binding = 4, set = 2) readonly buffer StructuredBuffer_float3_t_3 { - vec3 _data[]; -} pVertexData_biTangents_0; - -#line 5 2 -struct ViewParameter_0 -{ - mat4x4 viewMatrix_0; - mat4x4 projectionMatrix_0; - vec4 cameraPos_WS_0; - vec2 screenDimensions_0; -}; - - -#line 12 -layout(binding = 0, set = 1) -layout(std140) uniform _S1 -{ - mat4x4 viewMatrix_0; - mat4x4 projectionMatrix_0; - vec4 cameraPos_WS_0; - vec2 screenDimensions_0; -}pViewParams_0; - -#line 4206 3 -layout(location = 0) -out vec3 _S2; - - -#line 4206 -layout(location = 1) -out vec3 _S3; - - -#line 4206 -layout(location = 2) -out vec2 _S4; - - -#line 4206 -layout(location = 3) -out vec3 _S5; - - -#line 4206 -layout(location = 4) -out vec3 _S6; - - -#line 4206 -layout(location = 5) -out vec3 _S7; - - -#line 4206 -layout(location = 6) -out vec3 _S8; - - -#line 1 4 -struct MaterialParameter_0 -{ - vec3 position_TS_0; - vec3 worldPosition_0; - vec2 texCoords_0; - vec3 normal_0; - vec3 tangent_0; - vec3 biTangent_0; - vec3 viewDir_TS_0; -}; - -struct VertexAttributes_0 -{ - MaterialParameter_0 parameter_0; - vec4 clipPosition_0; -}; - - -#line 9 1 -VertexAttributes_0 StaticMeshVertexData_getAttributes_0(uint _S9, mat4x4 _S10) -{ - - vec4 worldPos_0 = (((vec4(pVertexData_positions_0._data[_S9], 1.0)) * (_S10))); - - vec4 clipPos_0 = ((((((worldPos_0) * (pViewParams_0.viewMatrix_0)))) * (pViewParams_0.projectionMatrix_0))); - -#line 10 - MaterialParameter_0 params_0; - -#line 15 - params_0.worldPosition_0 = worldPos_0.xyz; - params_0.texCoords_0 = pVertexData_texCoords_0._data[_S9]; - params_0.normal_0 = pVertexData_normals_0._data[_S9]; - params_0.tangent_0 = pVertexData_tangents_0._data[_S9]; - params_0.biTangent_0 = pVertexData_biTangents_0._data[_S9]; - -#line 9 - VertexAttributes_0 attributes_0; - -#line 20 - attributes_0.parameter_0 = params_0; - attributes_0.clipPosition_0 = clipPos_0; - return attributes_0; -} - - -#line 18 0 +#line 27 0 +layout(local_size_x = 3, local_size_y = 1, local_size_z = 1) in; +layout(max_vertices = 12) out; +layout(max_primitives = 4) out; +layout(triangles) out; void main() { -#line 18 - VertexAttributes_0 _S11 = StaticMeshVertexData_getAttributes_0(uint(gl_VertexIndex), pScene_instances_0._data[uint(gl_InstanceIndex)].transformMatrix_0); + SetMeshOutputsEXT(12U, 4U); -#line 18 - _S2 = _S11.parameter_0.position_TS_0; - -#line 18 - _S3 = _S11.parameter_0.worldPosition_0; - -#line 18 - _S4 = _S11.parameter_0.texCoords_0; - -#line 18 - _S5 = _S11.parameter_0.normal_0; - -#line 18 - _S6 = _S11.parameter_0.tangent_0; - -#line 18 - _S7 = _S11.parameter_0.biTangent_0; - -#line 18 - _S8 = _S11.parameter_0.viewDir_TS_0; - -#line 18 - gl_Position = _S11.clipPosition_0; - -#line 18 +#line 46 return; } diff --git a/res/shaders/test.slang b/res/shaders/test.slang index 9db3b68..bebc62f 100644 --- a/res/shaders/test.slang +++ b/res/shaders/test.slang @@ -1,63 +1,44 @@ + const static float2 positions[3] = { + float2(0.0, -0.5), + float2(0.5, 0.5), + float2(-0.5, 0.5) + }; -struct MeshPayload -{ - int exponent; -}; - -groupshared MeshPayload payload; - -[numthreads(1, 1, 1)] -[outputtopology("triangle")] -void taskMain( - uint3 threadID: SV_GroupIndex, - uint3 groupID: SV_GroupID -){ - DispatchMesh(1,1,1,payload); -} - - - -const static float2 positions[3] = { - float2(0.0, -0.5), - float2(0.5, 0.5), - float2(-0.5, 0.5) -}; - -const static float3 colors[3] = { - float3(1.0, 1.0, 0.0), - float3(0.0, 1.0, 1.0), - float3(1.0, 0.0, 1.0) -}; -struct Vertex -{ - float4 pos : SV_Position; - float3 color : Color; - int index : Index; - int value : Value; -}; - -const static uint MAX_VERTS = 12; -const static uint MAX_PRIMS = 4; - -[outputtopology("triangle")] -[shader("mesh")] -[numthreads(12, 1, 1)] -void meshMain( - in uint tig : SV_GroupIndex, - in payload MeshPayload meshPayload, - out Vertices verts, - out Indices triangles) -{ - const uint numVertices = 12; - const uint numPrimitives = 4; - SetMeshOutputCounts(numVertices, numPrimitives); - - if(tig < numVertices) + const static float3 colors[3] = { + float3(1.0, 1.0, 0.0), + float3(0.0, 1.0, 1.0), + float3(1.0, 0.0, 1.0) + }; + struct Vertex { - const int tri = tig / 3; - verts[tig] = {float4(positions[tig % 3], 0, 1), colors[tig % 3], tri, int(pow(tri, meshPayload.exponent))}; - } + float4 pos : SV_Position; + float3 color : Color; + int index : Index; + }; - if(tig < numPrimitives) - triangles[tig] = tig * 3 + uint3(0,1,2); -} \ No newline at end of file + const static uint MAX_VERTS = 12; + const static uint MAX_PRIMS = 4; + + [outputtopology("triangle")] + [shader("mesh")] + [numthreads(3, 1, 1)] + void meshMain( + in uint tig : SV_GroupIndex, + out Vertices verts, + out Indices triangles) + { + const uint numVertices = 12; + const uint numPrimitives = 4; + SetMeshOutputCounts(numVertices, numPrimitives); + + for(uint i = tig; i < numVertices; ++i) + { + const int tri = i / 3; + verts[i] = {float4(positions[i % 3], 0, 1), colors[i % 3], tri}; + } + + for(uint i = tig; i < numPrimitives; ++i) + { + triangles[i] = i * 3 + uint3(0,1,2); + } + } \ No newline at end of file diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 0920765..f09658b 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -17,6 +17,9 @@ using namespace Seele; using namespace Seele::Editor; +// make it global so it gets deleted last and automatically +static Gfx::OGraphics graphics; + int main() { #ifdef WIN32 @@ -31,7 +34,7 @@ int main() std::string gameName = "TrackClear"; std::filesystem::path cmakePath = outputPath / "cmake"; - Gfx::OGraphics graphics = new Vulkan::Graphics(); + graphics = new Vulkan::Graphics(); GraphicsInitializer initializer; graphics->init(initializer); @@ -246,7 +249,7 @@ int main() { windowManager->render(); } - vd->~StaticMeshVertexData(); + vd->destroy(); //export game if (false) { diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 033e5c6..1941ed2 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -6,7 +6,6 @@ using namespace Seele; extern List vertexDataList; - StaticMeshVertexData::StaticMeshVertexData() { vertexDataList.add(this); @@ -128,6 +127,18 @@ void StaticMeshVertexData::init(Gfx::PGraphics graphics) descriptorSet = descriptorLayout->allocateDescriptorSet(); } +void StaticMeshVertexData::destroy() +{ + VertexData::destroy(); + positions = nullptr; + texCoords = nullptr; + normals = nullptr; + tangents = nullptr; + biTangents = nullptr; + colors = nullptr; + descriptorLayout = nullptr; +} + void StaticMeshVertexData::bindBuffers(Gfx::PRenderCommand) { // TODO: for legacy vertex buffer binding diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index 3a09111..75caf3d 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -21,6 +21,7 @@ public: virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override; virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) override; virtual void init(Gfx::PGraphics graphics) override; + virtual void destroy() override; virtual void bindBuffers(Gfx::PRenderCommand command) override; virtual Gfx::PDescriptorLayout getVertexDataLayout() override; virtual Gfx::PDescriptorSet getVertexDataSet() override; diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 1a715b6..01eebae 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -97,11 +97,11 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array { uint32 numMeshlets = std::min(512, loadedMeshlets.size() - currentMesh); uint32 meshletOffset = meshlets.size(); - AABB meshAABB; + //AABB meshAABB; for (uint32 i = 0; i < numMeshlets; ++i) { Meshlet& m = loadedMeshlets[currentMesh + i]; - meshAABB = meshAABB.combine(m.boundingBox); + //meshAABB = meshAABB.combine(m.boundingBox); uint32 vertexOffset = vertexIndices.size(); vertexIndices.resize(vertexOffset + m.numVertices); std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32)); @@ -109,7 +109,6 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3)); std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8)); meshlets.add(MeshletDescription{ - .boundingBox = m.boundingBox, .vertexCount = m.numVertices, .primitiveCount = m.numPrimitives, .vertexOffset = vertexOffset, @@ -117,10 +116,9 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }); } meshData[id].add(MeshData{ - .boundingBox = meshAABB, - .numMeshlets = numMeshlets, .meshletOffset = meshletOffset, - .indicesOffset = static_cast(meshOffsets[id]), + .numMeshlets = (uint16)numMeshlets, + .indicesOffset = (uint16)meshOffsets[id], }); currentMesh += numMeshlets; } @@ -225,6 +223,17 @@ void Seele::VertexData::init(Gfx::PGraphics _graphics) graphics->getShaderCompiler()->registerVertexData(this); } +void VertexData::destroy() +{ + instanceDataLayout = nullptr; + meshletBuffer = nullptr; + vertexIndicesBuffer = nullptr; + primitiveIndicesBuffer = nullptr; + indexBuffer = nullptr; + meshData.clear(); + materialData.clear(); +} + VertexData::VertexData() : idCounter(0) , head(0) diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 5b2ed15..ca84478 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -41,13 +41,13 @@ public: }; struct MeshData { - AABB boundingBox; - uint32 numMeshlets = 0; uint32 meshletOffset = 0; + uint16 numMeshlets = 0; + uint16 indicesOffset = 0; uint32 firstIndex = 0; uint32 numIndices = 0; - uint32 indicesOffset = 0; }; + static_assert(sizeof(MeshData) == 16); struct MeshInstanceData { InstanceData instance; @@ -86,13 +86,13 @@ public: static List getList(); static VertexData* findByTypeName(std::string name); virtual void init(Gfx::PGraphics graphics); + virtual void destroy(); protected: virtual void resizeBuffers() = 0; virtual void updateBuffers() = 0; VertexData(); struct MeshletDescription { - AABB boundingBox; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; diff --git a/src/Engine/Graphics/Vulkan/Buffer.cpp b/src/Engine/Graphics/Vulkan/Buffer.cpp index addbe9f..2f8bf16 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.cpp +++ b/src/Engine/Graphics/Vulkan/Buffer.cpp @@ -267,7 +267,6 @@ void Buffer::unmap() .size = pending.size, }; vkCmdCopyBuffer(cmdHandle, stagingBuffer->getHandle(), buffers[currentBuffer].buffer, 1, ®ion); - graphics->getQueueCommands(owner)->submitCommands(); } //requestOwnershipTransfer(pending.prevQueue); pendingBuffers.erase(this); @@ -328,7 +327,6 @@ void UniformBuffer::unmap() std::memset(®ion, 0, sizeof(VkBufferCopy)); region.size = Vulkan::Buffer::size; vkCmdCopyBuffer(cmdHandle, dedicatedStagingBuffer->getHandle(), buffers[currentBuffer].buffer, 1, ®ion); - graphics->getQueueCommands(currentOwner)->submitCommands(); } else { @@ -416,7 +414,6 @@ void ShaderBuffer::unmap() .size = Vulkan::Buffer::size, }; vkCmdCopyBuffer(cmdHandle, dedicatedStagingBuffer->getHandle(), buffers[currentBuffer].buffer, 1, ®ion); - graphics->getQueueCommands(currentOwner)->submitCommands(); } else { diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index 86e9da0..5f17417 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -407,7 +407,9 @@ void Graphics::pickPhysicalDevice() VkPhysicalDevice bestDevice = VK_NULL_HANDLE; uint32 deviceRating = 0; features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; - features.pNext = &features12; + features.pNext = &features11; + features11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES; + features11.pNext = &features12; features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; features12.pNext = nullptr; for (auto dev : physicalDevices) @@ -446,7 +448,7 @@ void Graphics::pickPhysicalDevice() { if (std::strcmp(VK_EXT_MESH_SHADER_EXTENSION_NAME, extensionProps[i].extensionName) == 0) { - meshShadingEnabled = true; + //meshShadingEnabled = true; break; } } @@ -565,7 +567,7 @@ void Graphics::createDevice(GraphicsInitializer initializer) } VkDeviceCreateInfo deviceInfo = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, - .pNext = &features12, + .pNext = &features11, .queueCreateInfoCount = (uint32)queueInfos.size(), .pQueueCreateInfos = queueInfos.data(), .enabledExtensionCount = (uint32)initializer.deviceExtensions.size(), diff --git a/src/Engine/Graphics/Vulkan/Graphics.h b/src/Engine/Graphics/Vulkan/Graphics.h index 4b7a872..dbe8195 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.h +++ b/src/Engine/Graphics/Vulkan/Graphics.h @@ -93,6 +93,7 @@ protected: thread_local static OCommandPool dedicatedTransferCommands; VkPhysicalDeviceProperties props; VkPhysicalDeviceFeatures2 features; + VkPhysicalDeviceVulkan11Features features11; VkPhysicalDeviceVulkan12Features features12; VkDebugReportCallbackEXT callback; Map allocatedFramebuffers;