From 620089fc3c84a99080477943a8b0139c8922d708 Mon Sep 17 00:00:00 2001 From: Tobias Frisch <tfrisch@uni-koblenz.de> Date: Sat, 18 Jun 2022 23:28:33 +0200 Subject: [PATCH] Adjusted rest of the projects to the new rendering path Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de> --- .../assets/shaders/mesh.vert | 19 ++- .../assets/shaders/prepass.vert | 15 +- .../indirect_dispatch/assets/shaders/sky.vert | 13 +- .../assets/shaders/skyPrepass.vert | 15 +- projects/indirect_dispatch/src/App.cpp | 16 +- projects/indirect_dispatch/src/AppSetup.cpp | 114 +++++++++---- projects/indirect_dispatch/src/AppSetup.hpp | 2 + .../mesh_shader/assets/shaders/shader.vert | 16 +- projects/mesh_shader/src/main.cpp | 91 +++++++---- .../particle_simulation/shaders/shader.vert | 12 +- projects/particle_simulation/src/main.cpp | 34 +--- projects/saf_r/src/main.cpp | 11 +- projects/sph/shaders/shader.vert | 16 +- projects/sph/shaders/updateData.comp | 8 - projects/sph/src/PipelineInit.cpp | 8 - projects/sph/src/main.cpp | 25 +-- .../assets/shaders/depthPrepass.vert | 11 +- .../voxelization/assets/shaders/shader.vert | 24 +-- .../voxelization/assets/shaders/shadow.vert | 8 +- .../voxelization/assets/shaders/vertex.inc | 12 ++ .../assets/shaders/voxelization.vert | 18 ++- projects/voxelization/src/ShadowMapping.cpp | 14 +- projects/voxelization/src/ShadowMapping.hpp | 3 +- projects/voxelization/src/Voxelization.cpp | 14 +- projects/voxelization/src/Voxelization.hpp | 6 +- projects/voxelization/src/main.cpp | 153 +++++++++++------- projects/wobble_bobble/shaders/grid.vert | 12 +- projects/wobble_bobble/shaders/lines.vert | 15 +- projects/wobble_bobble/shaders/particle.vert | 12 +- projects/wobble_bobble/src/main.cpp | 36 ----- 30 files changed, 438 insertions(+), 315 deletions(-) create mode 100644 projects/voxelization/assets/shaders/vertex.inc diff --git a/projects/indirect_dispatch/assets/shaders/mesh.vert b/projects/indirect_dispatch/assets/shaders/mesh.vert index 734fd63c..3b8f7da2 100644 --- a/projects/indirect_dispatch/assets/shaders/mesh.vert +++ b/projects/indirect_dispatch/assets/shaders/mesh.vert @@ -1,9 +1,16 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inNormal; -layout(location = 2) in vec2 inUV; +struct vertex_t { + vec3 position; + float u; + vec3 normal; + float v; +}; + +layout(std430, set=1, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec3 passNormal; layout(location = 1) out vec2 passUV; @@ -14,7 +21,7 @@ layout( push_constant ) uniform constants{ }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); - passNormal = (model * vec4(inNormal, 0)).xyz; - passUV = inUV; + gl_Position = mvp * vec4(vertices[gl_VertexIndex].position, 1.0); + passNormal = (model * vec4(vertices[gl_VertexIndex].normal, 0)).xyz; + passUV = vec2(vertices[gl_VertexIndex].u, vertices[gl_VertexIndex].v); } \ No newline at end of file diff --git a/projects/indirect_dispatch/assets/shaders/prepass.vert b/projects/indirect_dispatch/assets/shaders/prepass.vert index 23034620..628e41f5 100644 --- a/projects/indirect_dispatch/assets/shaders/prepass.vert +++ b/projects/indirect_dispatch/assets/shaders/prepass.vert @@ -1,7 +1,16 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; +struct vertex_t { + vec3 position; + float u; + vec3 normal; + float v; +}; + +layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec4 passNDC; layout(location = 1) out vec4 passNDCPrevious; @@ -12,7 +21,7 @@ layout( push_constant ) uniform constants{ }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); + gl_Position = mvp * vec4(vertices[gl_VertexIndex].position, 1.0); passNDC = gl_Position; - passNDCPrevious = mvpPrevious * vec4(inPosition, 1.0); + passNDCPrevious = mvpPrevious * vec4(vertices[gl_VertexIndex].position, 1.0); } \ No newline at end of file diff --git a/projects/indirect_dispatch/assets/shaders/sky.vert b/projects/indirect_dispatch/assets/shaders/sky.vert index 44b48cd7..2465d450 100644 --- a/projects/indirect_dispatch/assets/shaders/sky.vert +++ b/projects/indirect_dispatch/assets/shaders/sky.vert @@ -1,13 +1,22 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; +struct vertex_t { + vec3 position; + float u; + vec3 normal; + float v; +}; + +layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout( push_constant ) uniform constants{ mat4 viewProjection; }; void main() { - gl_Position = viewProjection * vec4(inPosition, 0.0); + gl_Position = viewProjection * vec4(vertices[gl_VertexIndex].position, 0.0); gl_Position.w = gl_Position.z; } \ No newline at end of file diff --git a/projects/indirect_dispatch/assets/shaders/skyPrepass.vert b/projects/indirect_dispatch/assets/shaders/skyPrepass.vert index 31b9016a..7feafe88 100644 --- a/projects/indirect_dispatch/assets/shaders/skyPrepass.vert +++ b/projects/indirect_dispatch/assets/shaders/skyPrepass.vert @@ -1,7 +1,16 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; +struct vertex_t { + vec3 position; + float u; + vec3 normal; + float v; +}; + +layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout( push_constant ) uniform constants{ mat4 viewProjection; @@ -12,11 +21,11 @@ layout(location = 0) out vec4 passNDC; layout(location = 1) out vec4 passNDCPrevious; void main() { - gl_Position = viewProjection * vec4(inPosition, 0.0); + gl_Position = viewProjection * vec4(vertices[gl_VertexIndex].position, 0.0); gl_Position.w = gl_Position.z; passNDC = gl_Position; - passNDCPrevious = viewProjectionPrevious * vec4(inPosition, 0.0); + passNDCPrevious = viewProjectionPrevious * vec4(vertices[gl_VertexIndex].position, 0.0); passNDCPrevious.w = passNDCPrevious.z; } \ No newline at end of file diff --git a/projects/indirect_dispatch/src/App.cpp b/projects/indirect_dispatch/src/App.cpp index e6885b8e..9c78d384 100644 --- a/projects/indirect_dispatch/src/App.cpp +++ b/projects/indirect_dispatch/src/App.cpp @@ -85,7 +85,9 @@ void App::run() { auto frameStartTime = std::chrono::system_clock::now(); const auto appStartTime = std::chrono::system_clock::now(); const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); - const vkcv::DrawcallInfo skyDrawcall(m_cubeMesh.mesh, {}, 1); + const vkcv::DrawcallInfo skyDrawcall(m_cubeMesh.mesh, { + vkcv::DescriptorSetUsage(0, m_cubeMesh.descSet) + }, 1); vkcv::gui::GUI gui(m_core, m_windowHandle); @@ -218,7 +220,9 @@ void App::run() { std::vector<vkcv::DrawcallInfo> prepassSceneDrawcalls; for (const Object& obj : sceneObjects) { - prepassSceneDrawcalls.push_back(vkcv::DrawcallInfo(obj.meshResources.mesh, {})); + prepassSceneDrawcalls.push_back(vkcv::DrawcallInfo(obj.meshResources.mesh, { + vkcv::DescriptorSetUsage(0, obj.meshResources.descSet) + })); } m_core.recordDrawcallsToCmdStream( @@ -260,8 +264,12 @@ void App::run() { std::vector<vkcv::DrawcallInfo> forwardSceneDrawcalls; for (const Object& obj : sceneObjects) { forwardSceneDrawcalls.push_back(vkcv::DrawcallInfo( - obj.meshResources.mesh, - { vkcv::DescriptorSetUsage(0, m_meshPass.descriptorSet) })); + obj.meshResources.mesh, + { + vkcv::DescriptorSetUsage(0, m_meshPass.descriptorSet), + vkcv::DescriptorSetUsage(1, obj.meshResources.descSet), + } + )); } m_core.recordDrawcallsToCmdStream( diff --git a/projects/indirect_dispatch/src/AppSetup.cpp b/projects/indirect_dispatch/src/AppSetup.cpp index 933f20db..40a8a0d1 100644 --- a/projects/indirect_dispatch/src/AppSetup.cpp +++ b/projects/indirect_dispatch/src/AppSetup.cpp @@ -3,6 +3,11 @@ #include <vkcv/asset/asset_loader.hpp> #include <vkcv/shader/GLSLCompiler.hpp> +struct vertex_t { + float positionU [4]; + float normalV [4]; +}; + bool loadMesh(vkcv::Core& core, const std::filesystem::path& path, MeshResources* outMesh) { assert(outMesh); @@ -20,38 +25,80 @@ bool loadMesh(vkcv::Core& core, const std::filesystem::path& path, MeshResources } assert(!scene.vertexGroups.empty()); - auto& vertexData = scene.vertexGroups[0].vertexBuffer; - auto& indexData = scene.vertexGroups[0].indexBuffer; - - vkcv::Buffer vertexBuffer = core.createBuffer<uint8_t>( - vkcv::BufferType::VERTEX, - vertexData.data.size(), + vkcv::Buffer vertexBuffer = core.createBuffer<vertex_t>( + vkcv::BufferType::STORAGE, + scene.vertexGroups[0].numVertices, vkcv::BufferMemoryType::DEVICE_LOCAL); + + std::vector<vertex_t> vertices; + vertices.reserve(vertexBuffer.getCount()); + + for (const auto& attribute : scene.vertexGroups[0].vertexBuffer.attributes) { + if (attribute.componentType != vkcv::asset::ComponentType::FLOAT32) { + continue; + } + + size_t offset = attribute.offset; + + for (size_t i = 0; i < vertexBuffer.getCount(); i++) { + const auto *data = reinterpret_cast<const float*>( + scene.vertexGroups[0].vertexBuffer.data.data() + offset + ); + + switch (attribute.type) { + case vkcv::asset::PrimitiveType::POSITION: + memcpy(vertices[i].positionU, data, sizeof(float) * attribute.componentCount); + break; + case vkcv::asset::PrimitiveType::NORMAL: + memcpy(vertices[i].normalV, data, sizeof(float) * attribute.componentCount); + break; + case vkcv::asset::PrimitiveType::TEXCOORD_0: + if (attribute.componentCount != 2) { + break; + } + + vertices[i].positionU[3] = data[0]; + vertices[i].normalV[3] = data[1]; + break; + default: + break; + } + + offset += attribute.stride; + } + } + + vertexBuffer.fill(vertices); vkcv::Buffer indexBuffer = core.createBuffer<uint8_t>( vkcv::BufferType::INDEX, - indexData.data.size(), + scene.vertexGroups[0].indexBuffer.data.size(), vkcv::BufferMemoryType::DEVICE_LOCAL); - vertexBuffer.fill(vertexData.data); - indexBuffer.fill(indexData.data); + indexBuffer.fill(scene.vertexGroups[0].indexBuffer.data); outMesh->vertexBuffer = vertexBuffer.getHandle(); outMesh->indexBuffer = indexBuffer.getHandle(); - auto& attributes = vertexData.attributes; - - std::sort(attributes.begin(), attributes.end(), - [](const vkcv::asset::VertexAttribute& x, const vkcv::asset::VertexAttribute& y) { - return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); - }); - - const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[0].offset), vertexBuffer.getVulkanHandle()), - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[1].offset), vertexBuffer.getVulkanHandle()), - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[2].offset), vertexBuffer.getVulkanHandle()) }; - - outMesh->mesh = vkcv::Mesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), scene.vertexGroups[0].numIndices); + outMesh->mesh = vkcv::Mesh(indexBuffer.getVulkanHandle(), scene.vertexGroups[0].numIndices); + + vkcv::DescriptorBindings descriptorBindings; + descriptorBindings.insert(std::make_pair(0, vkcv::DescriptorBinding { + 0, + vkcv::DescriptorType::STORAGE_BUFFER, + 1, + vkcv::ShaderStage::VERTEX, + false, + false + })); + + outMesh->descSetLayout = core.createDescriptorSetLayout(descriptorBindings); + outMesh->descSet = core.createDescriptorSet(outMesh->descSetLayout); + + core.writeDescriptorSet( + outMesh->descSet, + vkcv::DescriptorWrites().writeStorageBuffer(0, outMesh->vertexBuffer) + ); return true; } @@ -116,31 +163,27 @@ bool loadGraphicPass( shaderProgram.addShader(shaderStage, path); }); - const std::vector<vkcv::VertexAttachment> vertexAttachments = shaderProgram.getVertexAttachments(); - std::vector<vkcv::VertexBinding> bindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - bindings.push_back(vkcv::createVertexBinding(i, { vertexAttachments[i] })); - } - - const vkcv::VertexLayout vertexLayout { bindings }; - const auto descriptorBindings = shaderProgram.getReflectedDescriptors(); const bool hasDescriptor = descriptorBindings.size() > 0; std::vector<vkcv::DescriptorSetLayoutHandle> descriptorSetLayouts = {}; + if (hasDescriptor) { - outPassHandles->descriptorSetLayout = core.createDescriptorSetLayout(descriptorBindings.at(0)); + descriptorSetLayouts.reserve(descriptorBindings.size()); + + for (size_t i = 0; i < descriptorBindings.size(); i++) { + descriptorSetLayouts.push_back(core.createDescriptorSetLayout(descriptorBindings.at(i))); + } + + outPassHandles->descriptorSetLayout = descriptorSetLayouts[0]; outPassHandles->descriptorSet = core.createDescriptorSet(outPassHandles->descriptorSetLayout); - descriptorSetLayouts.push_back(outPassHandles->descriptorSetLayout); } - vkcv::GraphicsPipelineConfig pipelineConfig{ shaderProgram, UINT32_MAX, UINT32_MAX, outPassHandles->renderPass, - { vertexLayout }, descriptorSetLayouts, true }; @@ -148,7 +191,8 @@ bool loadGraphicPass( outPassHandles->pipeline = core.createGraphicsPipeline(pipelineConfig); if (!outPassHandles->pipeline) { - vkcv_log(vkcv::LogLevel::ERROR, "Error: Could not create graphics pipeline"); + vkcv_log(vkcv::LogLevel::ERROR, "Error: Could not create graphics pipeline [%s]", + vertexPath.c_str()); return false; } diff --git a/projects/indirect_dispatch/src/AppSetup.hpp b/projects/indirect_dispatch/src/AppSetup.hpp index b0ade431..5982bb63 100644 --- a/projects/indirect_dispatch/src/AppSetup.hpp +++ b/projects/indirect_dispatch/src/AppSetup.hpp @@ -24,6 +24,8 @@ struct MeshResources { vkcv::Mesh mesh; vkcv::BufferHandle vertexBuffer; vkcv::BufferHandle indexBuffer; + vkcv::DescriptorSetLayoutHandle descSetLayout; + vkcv::DescriptorSetHandle descSet; }; // loads position, uv and normal of the first mesh in a scene diff --git a/projects/mesh_shader/assets/shaders/shader.vert b/projects/mesh_shader/assets/shaders/shader.vert index fca50579..547e5a8c 100644 --- a/projects/mesh_shader/assets/shaders/shader.vert +++ b/projects/mesh_shader/assets/shaders/shader.vert @@ -4,8 +4,16 @@ #include "common.inc" -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inNormal; +struct vertex_t { + vec3 position; + float pad0; + vec3 normal; + float pad1; +}; + +layout(std430, set=0, binding=1) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec3 passNormal; layout(location = 1) out uint dummyOutput; @@ -22,8 +30,8 @@ layout( push_constant ) uniform constants{ void main() { - gl_Position = objectMatrices[matrixIndex].mvp * vec4(inPosition, 1.0); - passNormal = inNormal; + gl_Position = objectMatrices[matrixIndex].mvp * vec4(vertices[gl_VertexIndex].position, 1.0); + passNormal = vertices[gl_VertexIndex].normal; dummyOutput = padding * 0; // padding must be used, else compiler shrinks constant size } \ No newline at end of file diff --git a/projects/mesh_shader/src/main.cpp b/projects/mesh_shader/src/main.cpp index a74561ca..6f3527ca 100644 --- a/projects/mesh_shader/src/main.cpp +++ b/projects/mesh_shader/src/main.cpp @@ -75,6 +75,13 @@ CameraPlanes computeCameraPlanes(const vkcv::camera::Camera& camera) { return cameraPlanes; } +struct vertex_t { + float position [3]; + float pad0; + float normal [3]; + float pad1; +}; + int main(int argc, const char** argv) { const char* applicationName = "Mesh shader"; @@ -103,12 +110,61 @@ int main(int argc, const char** argv) { assert(!mesh.vertexGroups.empty()); - auto vertexBuffer = core.createBuffer<uint8_t>( - vkcv::BufferType::VERTEX, - mesh.vertexGroups[0].vertexBuffer.data.size(), + auto vertexBuffer = core.createBuffer<vertex_t>( + vkcv::BufferType::STORAGE, + mesh.vertexGroups[0].numVertices, vkcv::BufferMemoryType::DEVICE_LOCAL ); - vertexBuffer.fill(mesh.vertexGroups[0].vertexBuffer.data); + + std::vector<vertex_t> vertices; + vertices.reserve(vertexBuffer.getCount()); + + const vkcv::asset::VertexAttribute* attr_position = nullptr; + const vkcv::asset::VertexAttribute* attr_normal = nullptr; + + for (const auto& attribute : mesh.vertexGroups[0].vertexBuffer.attributes) { + if (attribute.componentType != vkcv::asset::ComponentType::FLOAT32) { + continue; + } + + switch (attribute.type) { + case vkcv::asset::PrimitiveType::POSITION: + attr_position = &attribute; + break; + case vkcv::asset::PrimitiveType::NORMAL: + attr_normal = &attribute; + break; + default: + break; + } + + size_t offset = attribute.offset; + + for (size_t i = 0; i < vertexBuffer.getCount(); i++) { + const auto *data = reinterpret_cast<const float*>( + mesh.vertexGroups[0].vertexBuffer.data.data() + offset + ); + + switch (attribute.type) { + case vkcv::asset::PrimitiveType::POSITION: + memcpy(vertices[i].position, data, sizeof(float) * attribute.componentCount); + break; + case vkcv::asset::PrimitiveType::NORMAL: + memcpy(vertices[i].normal, data, sizeof(float) * attribute.componentCount); + break; + default: + break; + } + + offset += attribute.stride; + } + } + + vertexBuffer.fill(vertices); + + if ((nullptr == attr_position) || (nullptr == attr_normal)) { + return 1; + } auto indexBuffer = core.createBuffer<uint8_t>( vkcv::BufferType::INDEX, @@ -117,20 +173,8 @@ int main(int argc, const char** argv) { ); indexBuffer.fill(mesh.vertexGroups[0].indexBuffer.data); - // format data for mesh shader - auto& attributes = mesh.vertexGroups[0].vertexBuffer.attributes; - - std::sort(attributes.begin(), attributes.end(), [](const vkcv::asset::VertexAttribute& x, const vkcv::asset::VertexAttribute& y) { - return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); - }); - - const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[0].offset), vertexBuffer.getVulkanHandle()), - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[1].offset), vertexBuffer.getVulkanHandle()), - vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[2].offset), vertexBuffer.getVulkanHandle()) }; - const auto& bunny = mesh.vertexGroups[0]; - std::vector<vkcv::meshlet::Vertex> interleavedVertices = vkcv::meshlet::convertToVertices(bunny.vertexBuffer.data, bunny.numVertices, attributes[0], attributes[1]); + std::vector<vkcv::meshlet::Vertex> interleavedVertices = vkcv::meshlet::convertToVertices(bunny.vertexBuffer.data, bunny.numVertices, *attr_position, *attr_normal); // mesh shader buffers const auto& assetLoaderIndexBuffer = mesh.vertexGroups[0].indexBuffer; std::vector<uint32_t> indexBuffer32Bit = vkcv::meshlet::assetLoaderIndicesTo32BitIndices(assetLoaderIndexBuffer.data, assetLoaderIndexBuffer.type); @@ -194,13 +238,6 @@ int main(int argc, const char** argv) { bunnyShaderProgram.addShader(shaderStage, path); }); - const std::vector<vkcv::VertexAttachment> vertexAttachments = bunnyShaderProgram.getVertexAttachments(); - std::vector<vkcv::VertexBinding> bindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - bindings.push_back(vkcv::createVertexBinding(i, { vertexAttachments[i] })); - } - const vkcv::VertexLayout bunnyLayout { bindings }; - vkcv::DescriptorSetLayoutHandle vertexShaderDescriptorSetLayout = core.createDescriptorSetLayout(bunnyShaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle vertexShaderDescriptorSet = core.createDescriptorSet(vertexShaderDescriptorSetLayout); @@ -209,7 +246,6 @@ int main(int argc, const char** argv) { UINT32_MAX, UINT32_MAX, renderPass, - { bunnyLayout }, { vertexShaderDescriptorSetLayout }, true }; @@ -223,6 +259,7 @@ int main(int argc, const char** argv) { vkcv::DescriptorWrites vertexShaderDescriptorWrites; vertexShaderDescriptorWrites.writeStorageBuffer(0, matrixBuffer.getHandle()); + vertexShaderDescriptorWrites.writeStorageBuffer(1, vertexBuffer.getHandle()); core.writeDescriptorSet(vertexShaderDescriptorSet, vertexShaderDescriptorWrites); vkcv::GraphicsPipelineHandle bunnyPipeline = core.createGraphicsPipeline(bunnyPipelineDefinition); @@ -252,14 +289,12 @@ int main(int argc, const char** argv) { vkcv::DescriptorSetLayoutHandle meshShaderDescriptorSetLayout = core.createDescriptorSetLayout(meshShaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle meshShaderDescriptorSet = core.createDescriptorSet(meshShaderDescriptorSetLayout); - const vkcv::VertexLayout meshShaderLayout(bindings); const vkcv::GraphicsPipelineConfig meshShaderPipelineDefinition{ meshShaderProgram, UINT32_MAX, UINT32_MAX, renderPass, - {meshShaderLayout}, {meshShaderDescriptorSetLayout}, true }; @@ -297,7 +332,7 @@ int main(int argc, const char** argv) { vkcv::ImageHandle swapchainImageHandle = vkcv::ImageHandle::createSwapchainImageHandle(); - const vkcv::Mesh renderMesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), mesh.vertexGroups[0].numIndices, vkcv::IndexBitCount::Bit32); + const vkcv::Mesh renderMesh(indexBuffer.getVulkanHandle(), mesh.vertexGroups[0].numIndices, vkcv::IndexBitCount::Bit32); const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); diff --git a/projects/particle_simulation/shaders/shader.vert b/projects/particle_simulation/shaders/shader.vert index 0a889b35..caf87e51 100644 --- a/projects/particle_simulation/shaders/shader.vert +++ b/projects/particle_simulation/shaders/shader.vert @@ -1,8 +1,6 @@ #version 460 core #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 particle; - struct Particle { vec3 position; @@ -29,17 +27,23 @@ layout(location = 2) out float passlifeTime; void main() { + vec2 positions[3] = { + vec2(-0.012f, 0.012f), + vec2(0.012f, 0.012f), + vec2(0.0f, -0.012f) + }; + int id = gl_InstanceIndex; passVelocity = inParticle[id].velocity; passlifeTime = inParticle[id].lifeTime; // particle position in view space vec4 positionView = view * vec4(inParticle[id].position, 1); // by adding the triangle position in view space the mesh is always camera facing - positionView.xyz += particle; + positionView.xyz += vec3(positions[gl_VertexIndex], 0.0f); // multiply with projection matrix for final position gl_Position = projection * positionView; // 0.01 corresponds to vertex position size in main float normalizationDivider = 0.012; - passTriangleCoordinates = particle.xy / normalizationDivider; + passTriangleCoordinates = positions[gl_VertexIndex] / normalizationDivider; } \ No newline at end of file diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index db538796..5247d87c 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -79,14 +79,6 @@ int main(int argc, const char **argv) { vkcv::DescriptorSetLayoutHandle computeDescriptorSetLayout = core.createDescriptorSetLayout(computeShaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle computeDescriptorSet = core.createDescriptorSet(computeDescriptorSetLayout); - const std::vector<vkcv::VertexAttachment> computeVertexAttachments = computeShaderProgram.getVertexAttachments(); - - std::vector<vkcv::VertexBinding> computeBindings; - for (size_t i = 0; i < computeVertexAttachments.size(); i++) { - computeBindings.push_back(vkcv::createVertexBinding(i, { computeVertexAttachments[i] })); - } - const vkcv::VertexLayout computeLayout { computeBindings }; - vkcv::ShaderProgram particleShaderProgram{}; compiler.compile(vkcv::ShaderStage::VERTEX, "shaders/shader.vert", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { particleShaderProgram.addShader(shaderStage, path); @@ -99,39 +91,16 @@ int main(int argc, const char **argv) { particleShaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorSetLayout); - vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>( - vkcv::BufferType::VERTEX, - 3 - ); - const std::vector<vkcv::VertexAttachment> vertexAttachments = particleShaderProgram.getVertexAttachments(); - - const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle())}; - - std::vector<vkcv::VertexBinding> bindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - bindings.push_back(vkcv::createVertexBinding(i, {vertexAttachments[i]})); - } - - const vkcv::VertexLayout particleLayout { bindings }; - vkcv::GraphicsPipelineConfig particlePipelineDefinition{ particleShaderProgram, UINT32_MAX, UINT32_MAX, particlePass, - {particleLayout}, {descriptorSetLayout}, true }; particlePipelineDefinition.m_blendMode = vkcv::BlendMode::Additive; - const std::vector<glm::vec3> vertices = {glm::vec3(-0.012, 0.012, 0), - glm::vec3(0.012, 0.012, 0), - glm::vec3(0, -0.012, 0)}; - - vertexBuffer.fill(vertices); - vkcv::GraphicsPipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition); vkcv::ComputePipelineHandle computePipeline = core.createComputePipeline({ @@ -163,6 +132,7 @@ int main(int argc, const char **argv) { vkcv::DescriptorWrites setWrites; setWrites.writeUniformBuffer(0, color.getHandle()).writeUniformBuffer(1, position.getHandle()); setWrites.writeStorageBuffer(2, particleBuffer.getHandle()); + core.writeDescriptorSet(descriptorSet, setWrites); vkcv::DescriptorWrites computeWrites; @@ -177,7 +147,7 @@ int main(int argc, const char **argv) { const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); - const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), + const vkcv::Mesh renderMesh(particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount()); vkcv::DescriptorSetUsage descriptorUsage(0, descriptorSet); diff --git a/projects/saf_r/src/main.cpp b/projects/saf_r/src/main.cpp index 68bc546d..5885ea6c 100644 --- a/projects/saf_r/src/main.cpp +++ b/projects/saf_r/src/main.cpp @@ -81,14 +81,6 @@ int main(int argc, const char** argv) { vkcv::DescriptorSetLayoutHandle computeDescriptorSetLayout = core.createDescriptorSetLayout(computeDescriptorBindings); vkcv::DescriptorSetHandle computeDescriptorSet = core.createDescriptorSet(computeDescriptorSetLayout); - - const std::vector<vkcv::VertexAttachment> computeVertexAttachments = computeShaderProgram.getVertexAttachments(); - - std::vector<vkcv::VertexBinding> computeBindings; - for (size_t i = 0; i < computeVertexAttachments.size(); i++) { - computeBindings.push_back(vkcv::createVertexBinding(i, { computeVertexAttachments[i] })); - } - const vkcv::VertexLayout computeLayout { computeBindings }; /* * create the scene @@ -178,7 +170,6 @@ int main(int argc, const char** argv) { UINT32_MAX, UINT32_MAX, safrPass, - {}, { descriptorSetLayout }, true }; @@ -200,7 +191,7 @@ int main(int argc, const char** argv) { auto start = std::chrono::system_clock::now(); - const vkcv::Mesh renderMesh({}, safrIndexBuffer.getVulkanHandle(), 3); + const vkcv::Mesh renderMesh(safrIndexBuffer.getVulkanHandle(), 3); vkcv::DescriptorSetUsage descriptorUsage(0, descriptorSet); vkcv::DrawcallInfo drawcall(renderMesh, { descriptorUsage }, 1); diff --git a/projects/sph/shaders/shader.vert b/projects/sph/shaders/shader.vert index f5531ffa..b7cc12a2 100644 --- a/projects/sph/shaders/shader.vert +++ b/projects/sph/shaders/shader.vert @@ -1,8 +1,6 @@ #version 460 core #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 particle; - struct Particle { vec3 position; @@ -33,17 +31,25 @@ layout(location = 1) out vec3 passVelocity; void main() { + const float particle_size = 0.02f; + + vec2 positions[3] = { + vec2(-particle_size, particle_size), + vec2(particle_size, particle_size), + vec2(0.0f, -particle_size) + }; + int id = gl_InstanceIndex; passVelocity = inParticle1[id].velocity; // particle position in view space vec4 positionView = view * vec4(inParticle1[id].position, 1); // by adding the triangle position in view space the mesh is always camera facing - positionView.xyz += particle; + positionView.xyz += vec3(positions[gl_VertexIndex], 0.0f); // multiply with projection matrix for final position gl_Position = projection * positionView; // 0.01 corresponds to vertex position size in main - float normalizationDivider = 0.012; - passTriangleCoordinates = particle.xy / normalizationDivider; + float normalizationDivider = particle_size; + passTriangleCoordinates = positions[gl_VertexIndex] / normalizationDivider; } \ No newline at end of file diff --git a/projects/sph/shaders/updateData.comp b/projects/sph/shaders/updateData.comp index 3c935b23..3c2321b0 100644 --- a/projects/sph/shaders/updateData.comp +++ b/projects/sph/shaders/updateData.comp @@ -53,14 +53,6 @@ void main() { vec3 out_force = inParticle[id].force; float out_density = inParticle[id].density; float out_pressure = inParticle[id].pressure; - - if (length(vel_new) > 100.f) - { - vel_new = normalize(vel_new)*50; - out_density = 0.01f; - out_pressure = 0.01f; - out_force = gravity * vec3(-gravityDir.x,gravityDir.y,gravityDir.z); - } vec3 pos_new = inParticle[id].position + (dt * vel_new); diff --git a/projects/sph/src/PipelineInit.cpp b/projects/sph/src/PipelineInit.cpp index e507f1ed..7f2c6ed4 100644 --- a/projects/sph/src/PipelineInit.cpp +++ b/projects/sph/src/PipelineInit.cpp @@ -11,14 +11,6 @@ vkcv::DescriptorSetHandle PipelineInit::ComputePipelineInit(vkcv::Core *pCore, v shaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle descriptorSet = pCore->createDescriptorSet(descriptorSetLayout); - const std::vector<vkcv::VertexAttachment> vertexAttachments = shaderProgram.getVertexAttachments(); - - std::vector<vkcv::VertexBinding> bindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - bindings.push_back(vkcv::createVertexBinding(i, { vertexAttachments[i] })); - } - const vkcv::VertexLayout layout { bindings }; - pipeline = pCore->createComputePipeline({ shaderProgram, { descriptorSetLayout } diff --git a/projects/sph/src/main.cpp b/projects/sph/src/main.cpp index 60107542..3d70c0b4 100644 --- a/projects/sph/src/main.cpp +++ b/projects/sph/src/main.cpp @@ -98,40 +98,17 @@ int main(int argc, const char **argv) { particleShaderProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorSetLayout); - vkcv::Buffer<glm::vec3> vertexBuffer = core.createBuffer<glm::vec3>( - vkcv::BufferType::VERTEX, - 3 - ); - const std::vector<vkcv::VertexAttachment> vertexAttachments = particleShaderProgram.getVertexAttachments(); - - const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle())}; - - std::vector<vkcv::VertexBinding> bindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - bindings.push_back(vkcv::createVertexBinding(i, {vertexAttachments[i]})); - } - - const vkcv::VertexLayout particleLayout { bindings }; - // initializing graphics pipeline vkcv::GraphicsPipelineConfig particlePipelineDefinition{ particleShaderProgram, UINT32_MAX, UINT32_MAX, particlePass, - {particleLayout}, {descriptorSetLayout}, true }; particlePipelineDefinition.m_blendMode = vkcv::BlendMode::Additive; - const std::vector<glm::vec3> vertices = {glm::vec3(-0.012, 0.012, 0), - glm::vec3(0.012, 0.012, 0), - glm::vec3(0, -0.012, 0)}; - - vertexBuffer.fill(vertices); - vkcv::GraphicsPipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition); vkcv::Buffer<glm::vec4> color = core.createBuffer<glm::vec4>( @@ -205,7 +182,7 @@ int main(int argc, const char **argv) { const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); - const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), + const vkcv::Mesh renderMesh(particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount()); vkcv::DescriptorSetUsage descriptorUsage(0, descriptorSet); diff --git a/projects/voxelization/assets/shaders/depthPrepass.vert b/projects/voxelization/assets/shaders/depthPrepass.vert index 4bb3500e..a3901f82 100644 --- a/projects/voxelization/assets/shaders/depthPrepass.vert +++ b/projects/voxelization/assets/shaders/depthPrepass.vert @@ -3,8 +3,11 @@ #extension GL_GOOGLE_include_directive : enable -layout(location = 0) in vec3 inPosition; -layout(location = 2) in vec2 inUV; +#include "vertex.inc" + +layout(std430, set=2, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec2 passUV; @@ -13,6 +16,6 @@ layout( push_constant ) uniform constants{ }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); - passUV = inUV; + gl_Position = mvp * vec4(vertices[gl_VertexIndex].position, 1.0); + passUV = vec2(vertices[gl_VertexIndex].u, vertices[gl_VertexIndex].v); } \ No newline at end of file diff --git a/projects/voxelization/assets/shaders/shader.vert b/projects/voxelization/assets/shaders/shader.vert index e3873f98..02232c41 100644 --- a/projects/voxelization/assets/shaders/shader.vert +++ b/projects/voxelization/assets/shaders/shader.vert @@ -1,10 +1,13 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inNormal; -layout(location = 2) in vec2 inUV; -layout(location = 3) in vec4 inTangent; +#extension GL_GOOGLE_include_directive : enable + +#include "vertex.inc" + +layout(std430, set=2, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec3 passNormal; layout(location = 1) out vec2 passUV; @@ -17,9 +20,12 @@ layout( push_constant ) uniform constants{ }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); - passNormal = mat3(model) * inNormal; // assuming no weird stuff like shearing or non-uniform scaling - passUV = inUV; - passPos = (model * vec4(inPosition, 1)).xyz; - passTangent = vec4(mat3(model) * inTangent.xyz, inTangent.w); + vec3 position = vertices[gl_VertexIndex].position; + vec4 tangent = vertices[gl_VertexIndex].tangent; + + gl_Position = mvp * vec4(position, 1.0); + passNormal = mat3(model) * vertices[gl_VertexIndex].normal; // assuming no weird stuff like shearing or non-uniform scaling + passUV = vec2(vertices[gl_VertexIndex].u, vertices[gl_VertexIndex].v); + passPos = (model * vec4(position, 1)).xyz; + passTangent = vec4(mat3(model) * tangent.xyz, tangent.w); } \ No newline at end of file diff --git a/projects/voxelization/assets/shaders/shadow.vert b/projects/voxelization/assets/shaders/shadow.vert index d800c547..65c2ddf9 100644 --- a/projects/voxelization/assets/shaders/shadow.vert +++ b/projects/voxelization/assets/shaders/shadow.vert @@ -3,12 +3,16 @@ #extension GL_GOOGLE_include_directive : enable -layout(location = 0) in vec3 inPosition; +#include "vertex.inc" + +layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout( push_constant ) uniform constants{ mat4 mvp; }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); + gl_Position = mvp * vec4(vertices[gl_VertexIndex].position, 1.0); } \ No newline at end of file diff --git a/projects/voxelization/assets/shaders/vertex.inc b/projects/voxelization/assets/shaders/vertex.inc new file mode 100644 index 00000000..4c3856a3 --- /dev/null +++ b/projects/voxelization/assets/shaders/vertex.inc @@ -0,0 +1,12 @@ +#ifndef VERTEX_INC +#define VERTEX_INC + +struct vertex_t { + vec3 position; + float u; + vec3 normal; + float v; + vec4 tangent; +}; + +#endif // #ifndef VERTEX_INC \ No newline at end of file diff --git a/projects/voxelization/assets/shaders/voxelization.vert b/projects/voxelization/assets/shaders/voxelization.vert index 221d0f6d..6619e1e8 100644 --- a/projects/voxelization/assets/shaders/voxelization.vert +++ b/projects/voxelization/assets/shaders/voxelization.vert @@ -1,9 +1,13 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inNormal; -layout(location = 2) in vec2 inUV; +#extension GL_GOOGLE_include_directive : enable + +#include "vertex.inc" + +layout(std430, set=2, binding=0) readonly buffer buffer_vertexBuffer { + vertex_t vertices []; +}; layout(location = 0) out vec3 passPos; layout(location = 1) out vec2 passUV; @@ -15,8 +19,8 @@ layout( push_constant ) uniform constants{ }; void main() { - gl_Position = mvp * vec4(inPosition, 1.0); - passPos = (model * vec4(inPosition, 1)).xyz; - passUV = inUV; - passN = mat3(model) * inNormal; + gl_Position = mvp * vec4(vertices[gl_VertexIndex].position, 1.0); + passPos = (model * vec4(vertices[gl_VertexIndex].position, 1)).xyz; + passUV = vec2(vertices[gl_VertexIndex].u, vertices[gl_VertexIndex].v); + passN = mat3(model) * vertices[gl_VertexIndex].normal; } \ No newline at end of file diff --git a/projects/voxelization/src/ShadowMapping.cpp b/projects/voxelization/src/ShadowMapping.cpp index a4b967b3..b32d2cc8 100644 --- a/projects/voxelization/src/ShadowMapping.cpp +++ b/projects/voxelization/src/ShadowMapping.cpp @@ -149,7 +149,7 @@ glm::mat4 computeShadowViewProjectionMatrix( return vulkanCorrectionMatrix * crop * view; } -ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vertexLayout) : +ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::DescriptorSetLayoutHandle &layoutHandle) : m_corePtr(corePtr), m_shadowMap(corePtr->createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1, true, true)), m_shadowMapIntermediate(corePtr->createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1, false, true)), @@ -171,8 +171,7 @@ ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vert shadowMapResolution, shadowMapResolution, m_shadowMapPass, - vertexLayout, - {}, + { layoutHandle }, false }; shadowPipeConfig.m_multisampling = msaa; @@ -232,6 +231,7 @@ void ShadowMapping::recordShadowMapRendering( float lightStrength, float maxShadowDistance, const std::vector<vkcv::Mesh>& meshes, + const std::vector<vkcv::DescriptorSetHandle> &vertexDescriptorSets, const std::vector<glm::mat4>& modelMatrices, const vkcv::camera::Camera& camera, const glm::vec3& voxelVolumeOffset, @@ -262,8 +262,14 @@ void ShadowMapping::recordShadowMapRendering( } std::vector<vkcv::DrawcallInfo> drawcalls; + drawcalls.reserve(meshes.size()); + for (const auto& mesh : meshes) { - drawcalls.push_back(vkcv::DrawcallInfo(mesh, {})); + const size_t meshIndex = drawcalls.size(); + + drawcalls.push_back(vkcv::DrawcallInfo(mesh, { + vkcv::DescriptorSetUsage(0, vertexDescriptorSets[meshIndex]) + })); } m_corePtr->recordBeginDebugLabel(cmdStream, "Shadow map depth", {1, 1, 1, 1}); diff --git a/projects/voxelization/src/ShadowMapping.hpp b/projects/voxelization/src/ShadowMapping.hpp index 14fef77d..b5b23dbd 100644 --- a/projects/voxelization/src/ShadowMapping.hpp +++ b/projects/voxelization/src/ShadowMapping.hpp @@ -17,7 +17,7 @@ struct LightInfo { class ShadowMapping { public: - ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vertexLayout); + ShadowMapping(vkcv::Core* corePtr, const vkcv::DescriptorSetLayoutHandle &layoutHandle); void recordShadowMapRendering( const vkcv::CommandStreamHandle& cmdStream, @@ -26,6 +26,7 @@ public: float lightStrength, float maxShadowDistance, const std::vector<vkcv::Mesh>& meshes, + const std::vector<vkcv::DescriptorSetHandle> &vertexDescriptorSets, const std::vector<glm::mat4>& modelMatrices, const vkcv::camera::Camera& camera, const glm::vec3& voxelVolumeOffset, diff --git a/projects/voxelization/src/Voxelization.cpp b/projects/voxelization/src/Voxelization.cpp index b30d8a7f..b6f2373d 100644 --- a/projects/voxelization/src/Voxelization.cpp +++ b/projects/voxelization/src/Voxelization.cpp @@ -81,7 +81,8 @@ Voxelization::Voxelization( vkcv::ImageHandle shadowMap, vkcv::SamplerHandle shadowSampler, vkcv::SamplerHandle voxelSampler, - vkcv::Multisampling msaa) + vkcv::Multisampling msaa, + const vkcv::DescriptorSetLayoutHandle &layoutHandle) : m_corePtr(corePtr), m_voxelImageIntermediate(m_corePtr->createImage(vk::Format::eR16G16B16A16Sfloat, voxelResolution, voxelResolution, voxelResolution, true, true)), @@ -105,15 +106,13 @@ Voxelization::Voxelization( m_voxelizationDescriptorSet = m_corePtr->createDescriptorSet(m_voxelizationDescriptorSetLayout); vkcv::DescriptorSetLayoutHandle dummyPerMeshDescriptorSetLayout = m_corePtr->createDescriptorSetLayout(voxelizationShader.getReflectedDescriptors().at(1)); - vkcv::DescriptorSetHandle dummyPerMeshDescriptorSet = m_corePtr->createDescriptorSet(dummyPerMeshDescriptorSetLayout); const vkcv::GraphicsPipelineConfig voxelizationPipeConfig{ voxelizationShader, voxelResolution, voxelResolution, m_voxelizationPass, - dependencies.vertexLayout, - { m_voxelizationDescriptorSetLayout, dummyPerMeshDescriptorSetLayout }, + { m_voxelizationDescriptorSetLayout, dummyPerMeshDescriptorSetLayout, layoutHandle }, false, true }; @@ -161,7 +160,6 @@ Voxelization::Voxelization( 0, 0, m_visualisationPass, - {}, { m_visualisationDescriptorSetLayout }, true, false, @@ -229,6 +227,7 @@ void Voxelization::voxelizeMeshes( const std::vector<vkcv::Mesh>& meshes, const std::vector<glm::mat4>& modelMatrices, const std::vector<vkcv::DescriptorSetHandle>& perMeshDescriptorSets, + const std::vector<vkcv::DescriptorSetHandle>& vertexDescriptorSets, const vkcv::WindowHandle& windowHandle, vkcv::Downsampler& downsampler) { @@ -279,7 +278,8 @@ void Voxelization::voxelizeMeshes( meshes[i], { vkcv::DescriptorSetUsage(0, m_voxelizationDescriptorSet), - vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]) + vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]), + vkcv::DescriptorSetUsage(2, vertexDescriptorSets[i]) },1)); } @@ -357,7 +357,7 @@ void Voxelization::renderVoxelVisualisation( uint32_t drawVoxelCount = voxelCount / exp2(mipLevel); const auto drawcall = vkcv::DrawcallInfo( - vkcv::Mesh({}, nullptr, drawVoxelCount), + vkcv::Mesh(nullptr, drawVoxelCount), { vkcv::DescriptorSetUsage(0, m_visualisationDescriptorSet) },1); m_corePtr->recordBeginDebugLabel(cmdStream, "Voxel visualisation", { 1, 1, 1, 1 }); diff --git a/projects/voxelization/src/Voxelization.hpp b/projects/voxelization/src/Voxelization.hpp index 866efaf4..b94a68a5 100644 --- a/projects/voxelization/src/Voxelization.hpp +++ b/projects/voxelization/src/Voxelization.hpp @@ -7,10 +7,10 @@ class Voxelization{ public: struct Dependencies { - vkcv::VertexLayout vertexLayout; vk::Format colorBufferFormat; vk::Format depthBufferFormat; }; + Voxelization( vkcv::Core* corePtr, const Dependencies& dependencies, @@ -18,13 +18,15 @@ public: vkcv::ImageHandle shadowMap, vkcv::SamplerHandle shadowSampler, vkcv::SamplerHandle voxelSampler, - vkcv::Multisampling msaa); + vkcv::Multisampling msaa, + const vkcv::DescriptorSetLayoutHandle& layoutHandle); void voxelizeMeshes( vkcv::CommandStreamHandle cmdStream, const std::vector<vkcv::Mesh>& meshes, const std::vector<glm::mat4>& modelMatrices, const std::vector<vkcv::DescriptorSetHandle>& perMeshDescriptorSets, + const std::vector<vkcv::DescriptorSetHandle>& vertexDescriptorSets, const vkcv::WindowHandle& windowHandle, vkcv::Downsampler& downsampler); diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp index fe096114..34f65f61 100644 --- a/projects/voxelization/src/main.cpp +++ b/projects/voxelization/src/main.cpp @@ -14,6 +14,12 @@ #include <vkcv/effects/BloomAndFlaresEffect.hpp> #include <vkcv/algorithm/SinglePassDownsampler.hpp> +struct vertex_t { + float positionU [4]; + float normalV [4]; + float tangent [4]; +}; + int main(int argc, const char** argv) { const char* applicationName = "Voxelization"; @@ -132,31 +138,91 @@ int main(int argc, const char** argv) { // build index and vertex buffers assert(!scene.vertexGroups.empty()); - std::vector<std::vector<uint8_t>> vBuffers; std::vector<std::vector<uint8_t>> iBuffers; - std::vector<vkcv::VertexBufferBinding> vBufferBindings; - std::vector<std::vector<vkcv::VertexBufferBinding>> vertexBufferBindings; std::vector<vkcv::asset::VertexAttribute> vAttributes; for (size_t i = 0; i < scene.vertexGroups.size(); i++) { - - vBuffers.push_back(scene.vertexGroups[i].vertexBuffer.data); iBuffers.push_back(scene.vertexGroups[i].indexBuffer.data); - - auto& attributes = scene.vertexGroups[i].vertexBuffer.attributes; - - std::sort(attributes.begin(), attributes.end(), [](const vkcv::asset::VertexAttribute& x, const vkcv::asset::VertexAttribute& y) { - return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); - }); } - std::vector<vkcv::Buffer<uint8_t>> vertexBuffers; + std::vector<vkcv::Buffer<vertex_t>> vertexBuffers; + vertexBuffers.reserve(scene.vertexGroups.size()); + for (const vkcv::asset::VertexGroup& group : scene.vertexGroups) { - vertexBuffers.push_back(core.createBuffer<uint8_t>( - vkcv::BufferType::VERTEX, - group.vertexBuffer.data.size())); - vertexBuffers.back().fill(group.vertexBuffer.data); + vertexBuffers.push_back(core.createBuffer<vertex_t>( + vkcv::BufferType::STORAGE, + group.numVertices)); + + std::vector<vertex_t> vertices; + vertices.reserve(group.numVertices); + + for (const auto& attribute : group.vertexBuffer.attributes) { + if (attribute.componentType != vkcv::asset::ComponentType::FLOAT32) { + continue; + } + + size_t offset = attribute.offset; + + for (size_t i = 0; i < group.numVertices; i++) { + const auto *data = reinterpret_cast<const float*>( + group.vertexBuffer.data.data() + offset + ); + + switch (attribute.type) { + case vkcv::asset::PrimitiveType::POSITION: + memcpy(vertices[i].positionU, data, sizeof(float) * attribute.componentCount); + break; + case vkcv::asset::PrimitiveType::NORMAL: + memcpy(vertices[i].normalV, data, sizeof(float) * attribute.componentCount); + break; + case vkcv::asset::PrimitiveType::TEXCOORD_0: + if (attribute.componentCount != 2) { + break; + } + + vertices[i].positionU[3] = data[0]; + vertices[i].normalV[3] = data[1]; + break; + case vkcv::asset::PrimitiveType::TANGENT: + memcpy(vertices[i].tangent, data, sizeof(float) * attribute.componentCount); + break; + default: + break; + } + + offset += attribute.stride; + } + } + + vertexBuffers.back().fill(vertices); + } + + vkcv::DescriptorBindings vertexDescriptorBindings; + vertexDescriptorBindings.insert(std::make_pair(0, vkcv::DescriptorBinding{ + 0, + vkcv::DescriptorType::STORAGE_BUFFER, + 1, + vkcv::ShaderStage::VERTEX, + false, + false + })); + + vkcv::DescriptorSetLayoutHandle vertexDescriptorSetLayout = core.createDescriptorSetLayout( + vertexDescriptorBindings + ); + + std::vector<vkcv::DescriptorSetHandle> vertexDescriptorSets; + vertexDescriptorSets.reserve(vertexBuffers.size()); + + for (const auto& buffer : vertexBuffers) { + auto descriptorSet = core.createDescriptorSet(vertexDescriptorSetLayout); + + vkcv::DescriptorWrites writes; + writes.writeStorageBuffer(0, buffer.getHandle()); + core.writeDescriptorSet(descriptorSet, writes); + + vertexDescriptorSets.push_back(descriptorSet); } std::vector<vkcv::Buffer<uint8_t>> indexBuffers; @@ -167,17 +233,6 @@ int main(int argc, const char** argv) { indexBuffers.back().fill(dataBuffer); } - int vertexBufferIndex = 0; - for (const auto& vertexGroup : scene.vertexGroups) { - for (const auto& attribute : vertexGroup.vertexBuffer.attributes) { - vAttributes.push_back(attribute); - vBufferBindings.push_back(vkcv::VertexBufferBinding(attribute.offset, vertexBuffers[vertexBufferIndex].getVulkanHandle())); - } - vertexBufferBindings.push_back(vBufferBindings); - vBufferBindings.clear(); - vertexBufferIndex++; - } - const vk::Format colorBufferFormat = vk::Format::eB10G11R11UfloatPack32; const vkcv::AttachmentDescription color_attachment( vkcv::AttachmentOperation::STORE, @@ -208,14 +263,6 @@ int main(int argc, const char** argv) { forwardProgram.addShader(shaderStage, path); }); - const std::vector<vkcv::VertexAttachment> vertexAttachments = forwardProgram.getVertexAttachments(); - - std::vector<vkcv::VertexBinding> vertexBindings; - for (size_t i = 0; i < vertexAttachments.size(); i++) { - vertexBindings.push_back(vkcv::createVertexBinding(i, { vertexAttachments[i] })); - } - const vkcv::VertexLayout vertexLayout { vertexBindings }; - vkcv::DescriptorSetLayoutHandle forwardShadingDescriptorSetLayout = core.createDescriptorSetLayout(forwardProgram.getReflectedDescriptors().at(0)); vkcv::DescriptorSetHandle forwardShadingDescriptorSet = core.createDescriptorSet(forwardShadingDescriptorSetLayout); @@ -230,14 +277,6 @@ int main(int argc, const char** argv) { depthPrepassShader.addShader(shaderStage, path); }); - const std::vector<vkcv::VertexAttachment> prepassVertexAttachments = depthPrepassShader.getVertexAttachments(); - - std::vector<vkcv::VertexBinding> prepassVertexBindings; - for (size_t i = 0; i < prepassVertexAttachments.size(); i++) { - prepassVertexBindings.push_back(vkcv::createVertexBinding(i, { prepassVertexAttachments[i] })); - } - const vkcv::VertexLayout prepassVertexLayout { prepassVertexBindings }; - const vkcv::AttachmentDescription prepassAttachment( vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, @@ -259,7 +298,6 @@ int main(int argc, const char** argv) { std::vector<vkcv::Image> sceneImages; vkcv::algorithm::SinglePassDownsampler spdDownsampler (core, colorSampler); - vkcv::Downsampler &downsampler = core.getDownsampler(); auto mipStream = core.createCommandStream(vkcv::QueueType::Graphics); @@ -339,8 +377,7 @@ int main(int argc, const char** argv) { swapchainExtent.width, swapchainExtent.height, prepassPass, - vertexLayout, - { prepassDescriptorSetLayout, perMeshDescriptorSetLayouts[0] }, + { prepassDescriptorSetLayout, perMeshDescriptorSetLayouts[0], vertexDescriptorSetLayout }, true }; prepassPipelineConfig.m_culling = vkcv::CullMode::Back; @@ -356,8 +393,7 @@ int main(int argc, const char** argv) { swapchainExtent.width, swapchainExtent.height, forwardPass, - vertexLayout, - { forwardShadingDescriptorSetLayout, perMeshDescriptorSetLayouts[0] }, + { forwardShadingDescriptorSetLayout, perMeshDescriptorSetLayouts[0], vertexDescriptorSetLayout }, true }; forwardPipelineConfig.m_culling = vkcv::CullMode::Back; @@ -409,7 +445,6 @@ int main(int argc, const char** argv) { skyPipeConfig.m_Width = swapchainExtent.width; skyPipeConfig.m_Height = swapchainExtent.height; skyPipeConfig.m_PassHandle = skyPass; - skyPipeConfig.m_VertexLayout = vkcv::VertexLayout(); skyPipeConfig.m_DescriptorLayouts = {}; skyPipeConfig.m_UseDynamicViewport = true; skyPipeConfig.m_multisampling = msaa; @@ -541,7 +576,7 @@ int main(int argc, const char** argv) { // prepare meshes std::vector<vkcv::Mesh> meshes; for (size_t i = 0; i < scene.vertexGroups.size(); i++) { - vkcv::Mesh mesh(vertexBufferBindings[i], indexBuffers[i].getVulkanHandle(), scene.vertexGroups[i].numIndices); + vkcv::Mesh mesh(indexBuffers[i].getVulkanHandle(), scene.vertexGroups[i].numIndices); meshes.push_back(mesh); } @@ -551,10 +586,14 @@ int main(int argc, const char** argv) { drawcalls.push_back(vkcv::DrawcallInfo(meshes[i], { vkcv::DescriptorSetUsage(0, forwardShadingDescriptorSet), - vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]) })); + vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]), + vkcv::DescriptorSetUsage(2, vertexDescriptorSets[i]) + })); prepassDrawcalls.push_back(vkcv::DrawcallInfo(meshes[i], { vkcv::DescriptorSetUsage(0, prepassDescriptorSet), - vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]) })); + vkcv::DescriptorSetUsage(1, perMeshDescriptorSets[i]), + vkcv::DescriptorSetUsage(2, vertexDescriptorSets[i]) + })); } vkcv::SamplerHandle voxelSampler = core.createSampler( @@ -564,12 +603,11 @@ int main(int argc, const char** argv) { vkcv::SamplerAddressMode::CLAMP_TO_EDGE ); - ShadowMapping shadowMapping(&core, vertexLayout); + ShadowMapping shadowMapping(&core, vertexDescriptorSetLayout); Voxelization::Dependencies voxelDependencies; voxelDependencies.colorBufferFormat = colorBufferFormat; voxelDependencies.depthBufferFormat = depthBufferFormat; - voxelDependencies.vertexLayout = vertexLayout; Voxelization voxelization( &core, voxelDependencies, @@ -577,7 +615,8 @@ int main(int argc, const char** argv) { shadowMapping.getShadowMap(), shadowMapping.getShadowSampler(), voxelSampler, - msaa); + msaa, + vertexDescriptorSetLayout); vkcv::effects::BloomAndFlaresEffect bloomFlares (core, true); vkcv::Buffer<glm::vec3> cameraPosBuffer = core.createBuffer<glm::vec3>(vkcv::BufferType::UNIFORM, 1); @@ -778,6 +817,7 @@ int main(int argc, const char** argv) { lightStrength, maxShadowDistance, meshes, + vertexDescriptorSets, modelMatrices, cameraManager.getActiveCamera(), voxelization.getVoxelOffset(), @@ -793,6 +833,7 @@ int main(int argc, const char** argv) { meshes, modelMatrices, perMeshDescriptorSets, + vertexDescriptorSets, windowHandle, spdDownsampler ); @@ -863,7 +904,7 @@ int main(int argc, const char** argv) { skyPass, skyPipe, skySettingsPushConstants, - { vkcv::DrawcallInfo(vkcv::Mesh({}, nullptr, 3), {}) }, + { vkcv::DrawcallInfo(vkcv::Mesh(nullptr, 3), {}) }, renderTargets, windowHandle); core.recordEndDebugLabel(cmdStream); diff --git a/projects/wobble_bobble/shaders/grid.vert b/projects/wobble_bobble/shaders/grid.vert index 54de3f3e..7000db7d 100644 --- a/projects/wobble_bobble/shaders/grid.vert +++ b/projects/wobble_bobble/shaders/grid.vert @@ -10,8 +10,6 @@ layout(set=0, binding=2) uniform simulationBlock { Simulation simulation; }; -layout(location = 0) in vec2 vertexPos; - layout(location = 0) out vec2 passPos; layout(location = 1) out vec3 passVelocity; layout(location = 2) out float passMass; @@ -25,6 +23,12 @@ ivec3 actual_mod(ivec3 x, ivec3 y) { } void main() { + const vec2 positions[3] = { + vec2(-1.0f, -1.0f), + vec2(+0.0f, +1.5f), + vec2(+1.0f, -1.0f) + }; + ivec3 gridResolution = textureSize(sampler3D(gridImage, gridSampler), 0); ivec3 gridID = ivec3( @@ -48,11 +52,11 @@ void main() { float alpha = clamp(density / simulation.density, 0.0f, 1.0f); - passPos = vertexPos; + passPos = positions[gl_VertexIndex]; passVelocity = gridData.xyz; passMass = mass; // align voxel to face camera gl_Position = mvp * vec4(position, 1); // transform position into projected view space - gl_Position.xy += vertexPos * (radius * 2.0f) * alpha; // move position directly in view space + gl_Position.xy += positions[gl_VertexIndex] * (radius * 2.0f) * alpha; // move position directly in view space } \ No newline at end of file diff --git a/projects/wobble_bobble/shaders/lines.vert b/projects/wobble_bobble/shaders/lines.vert index b8e3b01c..1eb6561b 100644 --- a/projects/wobble_bobble/shaders/lines.vert +++ b/projects/wobble_bobble/shaders/lines.vert @@ -1,11 +1,20 @@ #version 450 -layout(location = 0) in vec3 vertexPos; - layout( push_constant ) uniform constants{ mat4 mvp; }; void main() { - gl_Position = mvp * vec4(vertexPos, 1); + const vec3 positions[8] = { + vec3(0.0f, 0.0f, 0.0f), + vec3(1.0f, 0.0f, 0.0f), + vec3(0.0f, 1.0f, 0.0f), + vec3(1.0f, 1.0f, 0.0f), + vec3(0.0f, 0.0f, 1.0f), + vec3(1.0f, 0.0f, +1.0f), + vec3(0.0f, 1.0f, 1.0f), + vec3(1.0f, 1.0f, 1.0f) + }; + + gl_Position = mvp * vec4(positions[gl_VertexIndex], 1); } \ No newline at end of file diff --git a/projects/wobble_bobble/shaders/particle.vert b/projects/wobble_bobble/shaders/particle.vert index a8f697e7..ffe6f732 100644 --- a/projects/wobble_bobble/shaders/particle.vert +++ b/projects/wobble_bobble/shaders/particle.vert @@ -7,8 +7,6 @@ layout(set=0, binding=0, std430) readonly buffer particleBuffer { Particle particles []; }; -layout(location = 0) in vec2 vertexPos; - layout(location = 0) out vec2 passPos; layout(location = 1) out float passMass; @@ -17,15 +15,21 @@ layout( push_constant ) uniform constants{ }; void main() { + const vec2 positions[3] = { + vec2(-1.0f, -1.0f), + vec2(+0.0f, +1.5f), + vec2(+1.0f, -1.0f) + }; + vec3 position = particles[gl_InstanceIndex].minimal.position; float size = particles[gl_InstanceIndex].minimal.size; float mass = particles[gl_InstanceIndex].minimal.mass; - passPos = vertexPos; + passPos = positions[gl_VertexIndex]; passMass = mass; // align particle to face camera gl_Position = mvp * vec4(position, 1); // transform position into projected view space - gl_Position.xy += vertexPos * size * 2.0f; // move position directly in view space + gl_Position.xy += positions[gl_VertexIndex] * size * 2.0f; // move position directly in view space } \ No newline at end of file diff --git a/projects/wobble_bobble/src/main.cpp b/projects/wobble_bobble/src/main.cpp index d554687b..27080d74 100644 --- a/projects/wobble_bobble/src/main.cpp +++ b/projects/wobble_bobble/src/main.cpp @@ -521,42 +521,27 @@ int main(int argc, const char **argv) { vkcv::PassHandle gfxPassParticles = core.createPass(passConfigParticles); vkcv::PassHandle gfxPassLines = core.createPass(passConfigLines); - vkcv::VertexLayout vertexLayoutGrid ({ - vkcv::createVertexBinding(0, gfxProgramGrid.getVertexAttachments()) - }); - vkcv::GraphicsPipelineConfig gfxPipelineConfigGrid; gfxPipelineConfigGrid.m_ShaderProgram = gfxProgramGrid; gfxPipelineConfigGrid.m_Width = windowWidth; gfxPipelineConfigGrid.m_Height = windowHeight; gfxPipelineConfigGrid.m_PassHandle = gfxPassGrid; - gfxPipelineConfigGrid.m_VertexLayout = vertexLayoutGrid; gfxPipelineConfigGrid.m_DescriptorLayouts = { gfxSetLayoutGrid }; gfxPipelineConfigGrid.m_UseDynamicViewport = true; - vkcv::VertexLayout vertexLayoutParticles ({ - vkcv::createVertexBinding(0, gfxProgramParticles.getVertexAttachments()) - }); - vkcv::GraphicsPipelineConfig gfxPipelineConfigParticles; gfxPipelineConfigParticles.m_ShaderProgram = gfxProgramParticles; gfxPipelineConfigParticles.m_Width = windowWidth; gfxPipelineConfigParticles.m_Height = windowHeight; gfxPipelineConfigParticles.m_PassHandle = gfxPassParticles; - gfxPipelineConfigParticles.m_VertexLayout = vertexLayoutParticles; gfxPipelineConfigParticles.m_DescriptorLayouts = { gfxSetLayoutParticles }; gfxPipelineConfigParticles.m_UseDynamicViewport = true; - vkcv::VertexLayout vertexLayoutLines ({ - vkcv::createVertexBinding(0, gfxProgramLines.getVertexAttachments()) - }); - vkcv::GraphicsPipelineConfig gfxPipelineConfigLines; gfxPipelineConfigLines.m_ShaderProgram = gfxProgramLines; gfxPipelineConfigLines.m_Width = windowWidth; gfxPipelineConfigLines.m_Height = windowHeight; gfxPipelineConfigLines.m_PassHandle = gfxPassLines; - gfxPipelineConfigLines.m_VertexLayout = vertexLayoutLines; gfxPipelineConfigLines.m_DescriptorLayouts = {}; gfxPipelineConfigLines.m_UseDynamicViewport = true; gfxPipelineConfigLines.m_PrimitiveTopology = vkcv::PrimitiveTopology::LineList; @@ -565,36 +550,16 @@ int main(int argc, const char **argv) { vkcv::GraphicsPipelineHandle gfxPipelineParticles = core.createGraphicsPipeline(gfxPipelineConfigParticles); vkcv::GraphicsPipelineHandle gfxPipelineLines = core.createGraphicsPipeline(gfxPipelineConfigLines); - vkcv::Buffer<glm::vec2> trianglePositions = core.createBuffer<glm::vec2>(vkcv::BufferType::VERTEX, 3); - trianglePositions.fill({ - glm::vec2(-1.0f, -1.0f), - glm::vec2(+0.0f, +1.5f), - glm::vec2(+1.0f, -1.0f) - }); - vkcv::Buffer<uint16_t> triangleIndices = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3); triangleIndices.fill({ 0, 1, 2 }); vkcv::Mesh triangleMesh ( - { vkcv::VertexBufferBinding(0, trianglePositions.getVulkanHandle()) }, triangleIndices.getVulkanHandle(), triangleIndices.getCount() ); - vkcv::Buffer<glm::vec3> linesPositions = core.createBuffer<glm::vec3>(vkcv::BufferType::VERTEX, 8); - linesPositions.fill({ - glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(1.0f, 0.0f, 0.0f), - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(1.0f, 1.0f, 0.0f), - glm::vec3(0.0f, 0.0f, 1.0f), - glm::vec3(1.0f, 0.0f, +1.0f), - glm::vec3(0.0f, 1.0f, 1.0f), - glm::vec3(1.0f, 1.0f, 1.0f) - }); - vkcv::Buffer<uint16_t> linesIndices = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 24); linesIndices.fill({ 0, 1, @@ -614,7 +579,6 @@ int main(int argc, const char **argv) { }); vkcv::Mesh linesMesh ( - { vkcv::VertexBufferBinding(0, linesPositions.getVulkanHandle()) }, linesIndices.getVulkanHandle(), linesIndices.getCount() ); -- GitLab