From d47567331a063dcb8c855084e88c961eb22dc35a Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Sat, 18 Jun 2022 19:28:26 +0200
Subject: [PATCH] Updated indirect draw project to new rendering path

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 .../resources/shaders/shader.vert             |  2 +-
 .../first_mesh/assets/shaders/shader.vert     |  2 +-
 .../first_scene/assets/shaders/shader.vert    |  2 +-
 .../resources/shaders/shader.frag             |  4 +-
 .../resources/shaders/shader.vert             | 21 +++++---
 projects/indirect_draw/src/main.cpp           | 52 +++++++++----------
 6 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/projects/bindless_textures/resources/shaders/shader.vert b/projects/bindless_textures/resources/shaders/shader.vert
index 46dee752..7c9438e1 100644
--- a/projects/bindless_textures/resources/shaders/shader.vert
+++ b/projects/bindless_textures/resources/shaders/shader.vert
@@ -8,7 +8,7 @@ struct vertex_t {
     float v;
 };
 
-layout(std430, set=0, binding=0) buffer buffer_vertexBuffer {
+layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer {
     vertex_t vertices [];
 };
 
diff --git a/projects/first_mesh/assets/shaders/shader.vert b/projects/first_mesh/assets/shaders/shader.vert
index 94829b60..d4f9a468 100644
--- a/projects/first_mesh/assets/shaders/shader.vert
+++ b/projects/first_mesh/assets/shaders/shader.vert
@@ -8,7 +8,7 @@ struct vertex_t {
     float v;
 };
 
-layout(std430, set=0, binding=2) buffer buffer_vertexBuffer {
+layout(std430, set=0, binding=2) readonly buffer buffer_vertexBuffer {
     vertex_t vertices [];
 };
 
diff --git a/projects/first_scene/assets/shaders/shader.vert b/projects/first_scene/assets/shaders/shader.vert
index 40305d21..bacbf909 100644
--- a/projects/first_scene/assets/shaders/shader.vert
+++ b/projects/first_scene/assets/shaders/shader.vert
@@ -8,7 +8,7 @@ struct vertex_t {
     float v;
 };
 
-layout(std430, set=0, binding=0) buffer buffer_vertexBuffer {
+layout(std430, set=0, binding=0) readonly buffer buffer_vertexBuffer {
     vertex_t vertices [];
 };
 
diff --git a/projects/indirect_draw/resources/shaders/shader.frag b/projects/indirect_draw/resources/shaders/shader.frag
index f0a0b493..d68e203e 100644
--- a/projects/indirect_draw/resources/shaders/shader.frag
+++ b/projects/indirect_draw/resources/shaders/shader.frag
@@ -8,8 +8,8 @@ layout(location = 2) in flat uint passDrawIndex;
 
 layout(location = 0) out vec3 outColor;
 
-layout(set=0, binding=0) uniform sampler standardSampler;
-layout(set=0, binding=2) uniform texture2D baseColorTex[];
+layout(set=0, binding=2) uniform sampler standardSampler;
+layout(set=0, binding=3) uniform texture2D baseColorTex[];
 
 
 void main()
diff --git a/projects/indirect_draw/resources/shaders/shader.vert b/projects/indirect_draw/resources/shaders/shader.vert
index 10b25076..17c3e1fd 100644
--- a/projects/indirect_draw/resources/shaders/shader.vert
+++ b/projects/indirect_draw/resources/shaders/shader.vert
@@ -2,11 +2,18 @@
 #extension GL_ARB_separate_shader_objects : enable
 #extension GL_ARB_shader_draw_parameters : 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=0, binding=0) readonly buffer buffer_vertexBuffer {
+    vertex_t vertices [];
+};
 
-layout(std430, binding=1) readonly buffer uModel {
+layout(std430, set=0, binding=1) readonly buffer uModel {
     mat4 modelMatrix[];
 };
 
@@ -20,8 +27,8 @@ layout( push_constant ) uniform constants{
 
 void main()
 {
-	gl_Position = vp * modelMatrix[gl_DrawID] * vec4(inPosition, 1.0);
-	passNormal  = inNormal;
-    passUV      = inUV;
+	gl_Position = vp * modelMatrix[gl_DrawID] * vec4(vertices[gl_VertexIndex].position, 1.0);
+	passNormal  = vertices[gl_VertexIndex].normal;
+    passUV      = vec2(vertices[gl_VertexIndex].u, vertices[gl_VertexIndex].v);
     passDrawIndex = gl_DrawID;
 }
\ No newline at end of file
diff --git a/projects/indirect_draw/src/main.cpp b/projects/indirect_draw/src/main.cpp
index 26f52395..750107bf 100644
--- a/projects/indirect_draw/src/main.cpp
+++ b/projects/indirect_draw/src/main.cpp
@@ -72,9 +72,8 @@ CameraPlanes computeCameraPlanes(const vkcv::camera::Camera& camera) {
 
 struct Vertex
 {
-	glm::vec3 position;
-	glm::vec3 normal;
-	glm::vec2 uv;
+	glm::vec4 positionU;
+	glm::vec4 normalV;
 };
 
 struct CompiledMaterial
@@ -130,12 +129,19 @@ void interleaveScene(vkcv::asset::Scene scene,
                 const size_t uvOffset       = uvAttribute.offset       + uvStride * i;
 
                 Vertex v;
+				
+				const glm::vec2& uv = *reinterpret_cast<const glm::vec2*>(
+						&(vertexData[uvOffset])
+				);
+				
+				const glm::vec3& position = *reinterpret_cast<const glm::vec3*>(
+						&(vertexData[positionOffset])
+				);
 
-                v.position = *reinterpret_cast<const glm::vec3*>(&(vertexData[positionOffset]));
-                v.normal   = *reinterpret_cast<const glm::vec3*>(&(vertexData[normalOffset]));
-                v.uv       = *reinterpret_cast<const glm::vec3*>(&(vertexData[uvOffset]));
+                v.positionU = glm::vec4(position, uv[0]);
+                v.normalV   = glm::vec4(*reinterpret_cast<const glm::vec3*>(&(vertexData[normalOffset])), uv[1]);
 
-                glm::vec3 posWorld = glm::make_mat4(mesh.modelMatrix.data()) * glm::vec4(v.position, 1);
+                glm::vec3 posWorld = glm::make_mat4(mesh.modelMatrix.data()) * glm::vec4(position, 1);
 
                 max_pos.x = glm::max(max_pos.x, posWorld.x);
                 max_pos.y = glm::max(max_pos.y, posWorld.y);
@@ -362,12 +368,6 @@ int main(int argc, const char** argv) {
         cullingProgram.addShader(shaderStage, path);
     });
 
-    // vertex layout for the pipeline. (assumed to be) used by all sponza meshes.
-    const std::vector<vkcv::VertexAttachment> vertexAttachments = sponzaProgram.getVertexAttachments();
-	const vkcv::VertexLayout sponzaVertexLayout {
-		{ vkcv::createVertexBinding(0, { vertexAttachments }) }
-	};
-
     std::vector<uint8_t> compiledVertexBuffer; // IGNORED, since the vertex buffer is not interleaved!
 
     std::vector<uint8_t> compiledIndexBuffer;
@@ -388,16 +388,16 @@ int main(int argc, const char** argv) {
                     compiledBoundingBoxBuffer);
 
 	std::vector<Vertex> compiledInterleavedVertexBuffer;
-	for(auto& vertexGroup : interleavedVertices )
-	{
+	for(auto& vertexGroup : interleavedVertices ) {
         compiledInterleavedVertexBuffer.insert(compiledInterleavedVertexBuffer.end(),vertexGroup.begin(),vertexGroup.end());
 	}
 
 	auto vkCompiledVertexBuffer = core.createBuffer<Vertex>(
-			vkcv::BufferType::VERTEX,
+			vkcv::BufferType::STORAGE,
             compiledInterleavedVertexBuffer.size(),
 			vkcv::BufferMemoryType::DEVICE_LOCAL
-			);
+	);
+	
     vkCompiledVertexBuffer.fill(compiledInterleavedVertexBuffer.data());
 
     auto vkCompiledIndexBuffer = core.createBuffer<uint8_t>(
@@ -429,16 +429,12 @@ int main(int argc, const char** argv) {
 			);
 	modelBuffer.fill(modelMatrix);
 
-	const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = {
-			vkcv::VertexBufferBinding(static_cast<vk::DeviceSize> (0), vkCompiledVertexBuffer.getVulkanHandle() )
-	};
-
-	const vkcv::Mesh compiledMesh(vertexBufferBindings, vkCompiledIndexBuffer.getVulkanHandle(), 0, vkcv::IndexBitCount::Bit32);
+	const vkcv::Mesh compiledMesh(vkCompiledIndexBuffer.getVulkanHandle(), 0, vkcv::IndexBitCount::Bit32);
 
     //assert(compiledMaterial.baseColor.size() == compiledMaterial.metalRough.size());
 
 	vkcv::DescriptorBindings descriptorBindings = sponzaProgram.getReflectedDescriptors().at(0);
-    descriptorBindings[2].descriptorCount = compiledMaterial.baseColor.size();
+    descriptorBindings[3].descriptorCount = compiledMaterial.baseColor.size();
 
 	vkcv::DescriptorSetLayoutHandle descriptorSetLayout = core.createDescriptorSetLayout(descriptorBindings);
 	vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorSetLayout);
@@ -451,15 +447,16 @@ int main(int argc, const char** argv) {
     );
 	
 	vkcv::DescriptorWrites setWrites;
+	setWrites.writeStorageBuffer(0, vkCompiledVertexBuffer.getHandle());
+	setWrites.writeStorageBuffer(1, modelBuffer.getHandle());
+	setWrites.writeSampler(2, standardSampler);
 	
     std::vector<vkcv::SampledImageDescriptorWrite> textureArrayWrites;
     for(uint32_t i = 0; i < compiledMaterial.baseColor.size(); i++)
     {
-		setWrites.writeSampledImage(2, compiledMaterial.baseColor[i].getHandle(), 0, false, i);
+		setWrites.writeSampledImage(3, compiledMaterial.baseColor[i].getHandle(), 0, false, i);
     }
-    
-    setWrites.writeSampler(0, standardSampler);
-	setWrites.writeStorageBuffer(1, modelBuffer.getHandle());
+	
     core.writeDescriptorSet(descriptorSet, setWrites);
 
 	const vkcv::GraphicsPipelineConfig sponzaPipelineConfig {
@@ -467,7 +464,6 @@ int main(int argc, const char** argv) {
         UINT32_MAX,
         UINT32_MAX,
         passHandle,
-        {sponzaVertexLayout},
 		{ descriptorSetLayout },
 		true
 	};
-- 
GitLab