From e411dc412f5cb9aabf3ccf2ae3fbdf608c52b518 Mon Sep 17 00:00:00 2001 From: Sebastian Gaida <gaida@ca-digit.com> Date: Fri, 16 Jul 2021 13:01:18 +0200 Subject: [PATCH] [#87] removed local methodes of meshlets use the meshlet methodes of the scene module --- projects/mesh_shader/CMakeLists.txt | 4 +- projects/mesh_shader/src/main.cpp | 164 +--------------------------- 2 files changed, 7 insertions(+), 161 deletions(-) diff --git a/projects/mesh_shader/CMakeLists.txt b/projects/mesh_shader/CMakeLists.txt index e270456d..1e1d2e86 100644 --- a/projects/mesh_shader/CMakeLists.txt +++ b/projects/mesh_shader/CMakeLists.txt @@ -24,7 +24,7 @@ if(MSVC) endif() # including headers of dependencies and the VkCV framework -target_include_directories(mesh_shader SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include} ${vkcv_gui_include}) +target_include_directories(mesh_shader SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include} ${vkcv_scene_include} ${vkcv_shader_compiler_include} ${vkcv_gui_include}) # linking with libraries from all dependencies and the VkCV framework -target_link_libraries(mesh_shader vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_testing vkcv_camera vkcv_shader_compiler vkcv_gui) \ No newline at end of file +target_link_libraries(mesh_shader vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_testing vkcv_camera vkcv_scene vkcv_shader_compiler vkcv_gui) \ No newline at end of file diff --git a/projects/mesh_shader/src/main.cpp b/projects/mesh_shader/src/main.cpp index b53c9758..c707a7bd 100644 --- a/projects/mesh_shader/src/main.cpp +++ b/projects/mesh_shader/src/main.cpp @@ -7,161 +7,7 @@ #include <vkcv/shader/GLSLCompiler.hpp> #include <vkcv/gui/GUI.hpp> #include <vkcv/asset/asset_loader.hpp> -#include "MeshStruct.hpp" -#include <Map> - -struct Vertex { - glm::vec3 position; - float padding0; - glm::vec3 normal; - float padding1; -}; - -struct Meshlet { - uint32_t vertexOffset; - uint32_t vertexCount; - uint32_t indexOffset; - uint32_t indexCount; -}; - -std::vector<Vertex> convertToVertices( - const std::vector<uint8_t>& vertexData, - const uint64_t vertexCount, - const vkcv::asset::VertexAttribute& positionAttribute, - const vkcv::asset::VertexAttribute& normalAttribute) { - - assert(positionAttribute.type == vkcv::asset::PrimitiveType::POSITION); - assert(normalAttribute.type == vkcv::asset::PrimitiveType::NORMAL); - - std::vector<Vertex> vertices; - vertices.reserve(vertexCount); - - const size_t positionStepSize = positionAttribute.stride == 0 ? sizeof(glm::vec3) : positionAttribute.stride; - const size_t normalStepSize = normalAttribute.stride == 0 ? sizeof(glm::vec3) : normalAttribute.stride; - - for (int i = 0; i < vertexCount; i++) { - Vertex v; - - const size_t positionOffset = positionAttribute.offset + positionStepSize * i; - const size_t normalOffset = normalAttribute.offset + normalStepSize * i; - - v.position = *reinterpret_cast<const glm::vec3*>(&(vertexData[positionOffset])); - v.normal = *reinterpret_cast<const glm::vec3*>(&(vertexData[normalOffset])); - vertices.push_back(v); - } - - return vertices; -} - -struct MeshShaderModelData { - std::vector<Vertex> vertices; - std::vector<uint32_t> localIndices; - std::vector<Meshlet> meshlets; -}; - -MeshShaderModelData createMeshShaderModelData( - const std::vector<Vertex>& inVertices, - const std::vector<uint32_t>& inIndices) { - - MeshShaderModelData data; - size_t currentIndex = 0; - - const size_t maxVerticesPerMeshlet = 64; - const size_t maxIndicesPerMeshlet = 126 * 3; - - bool indicesAreLeft = true; - - while (indicesAreLeft) { - Meshlet meshlet; - - meshlet.indexCount = 0; - meshlet.vertexCount = 0; - - meshlet.indexOffset = data.localIndices.size(); - meshlet.vertexOffset = data.vertices.size(); - - std::map<uint32_t, uint32_t> globalToLocalIndexMap; - std::vector<uint32_t> globalIndicesOrdered; - - while (true) { - - indicesAreLeft = currentIndex + 1 <= inIndices.size(); - if (!indicesAreLeft) { - break; - } - - bool enoughSpaceForIndices = meshlet.indexCount + 3 < maxIndicesPerMeshlet; - if (!enoughSpaceForIndices) { - break; - } - - size_t vertexCountToAdd = 0; - for (int i = 0; i < 3; i++) { - const uint32_t globalIndex = inIndices[currentIndex + i]; - const bool containsVertex = globalToLocalIndexMap.find(globalIndex) != globalToLocalIndexMap.end(); - if (!containsVertex) { - vertexCountToAdd++; - } - } - - bool enoughSpaceForVertices = meshlet.vertexCount + vertexCountToAdd < maxVerticesPerMeshlet; - if (!enoughSpaceForVertices) { - break; - } - - for (int i = 0; i < 3; i++) { - const uint32_t globalIndex = inIndices[currentIndex + i]; - - uint32_t localIndex; - const bool indexAlreadyExists = globalToLocalIndexMap.find(globalIndex) != globalToLocalIndexMap.end(); - if (indexAlreadyExists) { - localIndex = globalToLocalIndexMap[globalIndex]; - } - else { - localIndex = globalToLocalIndexMap.size(); - globalToLocalIndexMap[globalIndex] = localIndex; - globalIndicesOrdered.push_back(globalIndex); - } - - data.localIndices.push_back(localIndex); - } - - meshlet.indexCount += 3; - currentIndex += 3; - meshlet.vertexCount += vertexCountToAdd; - } - - for (const uint32_t globalIndex : globalIndicesOrdered) { - const Vertex v = inVertices[globalIndex]; - data.vertices.push_back(v); - } - - data.meshlets.push_back(meshlet); - } - - return data; -} - -std::vector<uint32_t> assetLoaderIndicesTo32BitIndices(const std::vector<uint8_t>& indexData, vkcv::asset::IndexType indexType) { - std::vector<uint32_t> indices; - if (indexType == vkcv::asset::IndexType::UINT16) { - for (int i = 0; i < indexData.size(); i += 2) { - const uint16_t index16Bit = *reinterpret_cast<const uint16_t*>(&(indexData[i])); - const uint32_t index32Bit = static_cast<uint32_t>(index16Bit); - indices.push_back(index32Bit); - } - } - else if (indexType == vkcv::asset::IndexType::UINT32) { - for (int i = 0; i < indexData.size(); i += 4) { - const uint32_t index32Bit = *reinterpret_cast<const uint32_t*>(&(indexData[i])); - indices.push_back(index32Bit); - } - } - else { - vkcv_log(vkcv::LogLevel::ERROR, "Unsupported index type"); - } - return indices; -} +#include <vkcv/scene/Meshlet.hpp> int main(int argc, const char** argv) { const char* applicationName = "Mesh shader"; @@ -224,14 +70,14 @@ int main(int argc, const char** argv) { vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[2].offset), vertexBuffer.getVulkanHandle()) }; const auto& bunny = mesh.vertexGroups[0]; - const std::vector<Vertex> interleavedVertices = convertToVertices(bunny.vertexBuffer.data, bunny.numVertices, attributes[0], attributes[1]); + const std::vector<vkcv::scene::Vertex> interleavedVertices = vkcv::scene::convertToVertices(bunny.vertexBuffer.data, bunny.numVertices, attributes[0], attributes[1]); // mesh shader buffers const auto& assetLoaderIndexBuffer = mesh.vertexGroups[0].indexBuffer; - const std::vector<uint32_t> indexBuffer32Bit = assetLoaderIndicesTo32BitIndices(assetLoaderIndexBuffer.data, assetLoaderIndexBuffer.type); + const std::vector<uint32_t> indexBuffer32Bit = vkcv::scene::assetLoaderIndicesTo32BitIndices(assetLoaderIndexBuffer.data, assetLoaderIndexBuffer.type); const auto meshShaderModelData = createMeshShaderModelData(interleavedVertices, indexBuffer32Bit); - auto meshShaderVertexBuffer = core.createBuffer<Vertex>( + auto meshShaderVertexBuffer = core.createBuffer<vkcv::scene::Vertex>( vkcv::BufferType::STORAGE, meshShaderModelData.vertices.size()); meshShaderVertexBuffer.fill(meshShaderModelData.vertices); @@ -241,7 +87,7 @@ int main(int argc, const char** argv) { meshShaderModelData.localIndices.size()); meshShaderIndexBuffer.fill(meshShaderModelData.localIndices); - auto meshletBuffer = core.createBuffer<Meshlet>( + auto meshletBuffer = core.createBuffer<vkcv::scene::Meshlet>( vkcv::BufferType::STORAGE, meshShaderModelData.meshlets.size(), vkcv::BufferMemoryType::DEVICE_LOCAL -- GitLab