From a47c167edf4af0a648497a6cdc1e4041863b8770 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 10 Oct 2024 22:35:16 +0200 Subject: [PATCH] shaders use #include --- include/vulkan/vertex.hpp | 1 - shaders/compile.sh | 2 +- shaders/grab.comp | 16 +----------- shaders/normal.comp | 16 +----------- shaders/pbd.comp | 54 ++++++++++++--------------------------- shaders/shader.frag | 24 +++++++++++++---- shaders/shader.vert | 11 +++----- shaders/structs.comp | 38 +++++++++++++++++++++++++++ src/mesh.cpp | 3 +-- src/soft_body.cpp | 20 +++++---------- src/vulkan/vertex.cpp | 37 ++++++++++++++------------- 11 files changed, 107 insertions(+), 115 deletions(-) create mode 100644 shaders/structs.comp diff --git a/include/vulkan/vertex.hpp b/include/vulkan/vertex.hpp index dba3086..efec90e 100644 --- a/include/vulkan/vertex.hpp +++ b/include/vulkan/vertex.hpp @@ -7,7 +7,6 @@ struct Vertex { alignas(16) glm::vec3 position; - alignas(16) glm::vec3 color; alignas(16) glm::vec2 uv; alignas(16) glm::vec3 normal; alignas(16) glm::vec3 velocity; diff --git a/shaders/compile.sh b/shaders/compile.sh index 37f194d..9ebefe4 100755 --- a/shaders/compile.sh +++ b/shaders/compile.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash glslc shader.vert -o vert.spv glslc shader.frag -o frag.spv -glslc pbd.comp -o pbd.spv +glslc --target-env=vulkan1.1 pbd.comp -o pbd.spv glslc normal.comp -o normal.spv glslc grab.comp -o grab.spv \ No newline at end of file diff --git a/shaders/grab.comp b/shaders/grab.comp index a8949d7..eb13d31 100644 --- a/shaders/grab.comp +++ b/shaders/grab.comp @@ -2,21 +2,7 @@ layout (local_size_x = 32) in; -struct Vertex { - vec3 position; - vec3 color; - vec2 uv; - vec3 normal; - vec3 velocity; - vec3 prevPosition; - float inverseMass; -}; - -struct Face { - uint a; - uint b; - uint c; -}; +#include "structs.comp" layout (std430, set = 0, binding = 0) buffer VertexBuffer { Vertex vertices[]; diff --git a/shaders/normal.comp b/shaders/normal.comp index 4b580e1..d2e7060 100644 --- a/shaders/normal.comp +++ b/shaders/normal.comp @@ -2,21 +2,7 @@ layout (local_size_x = 32) in; -struct Vertex { - vec3 position; - vec3 color; - vec2 uv; - uvec3 normal; - vec3 velocity; - vec3 prevPosition; - float invM; -}; - -struct Face { - uint a; - uint b; - uint c; -}; +#include "structs.comp" layout (std430, set = 0, binding = 0) buffer VertexBuffer { Vertex vertices[]; diff --git a/shaders/pbd.comp b/shaders/pbd.comp index 248a26a..219df7e 100644 --- a/shaders/pbd.comp +++ b/shaders/pbd.comp @@ -1,32 +1,10 @@ #version 450 -layout (local_size_x = 32) in; - -struct Vertex { - vec3 position; - vec3 color; - vec2 uv; - vec3 normal; - vec3 velocity; - vec3 prevPosition; - float w; -}; +#extension GL_EXT_shader_atomic_float : enable -struct Edge { - uint a; - uint b; - float restLength; - float compliance; -}; +layout (local_size_x = 32) in; -struct Tetrahedron { - uint a; - uint b; - uint c; - uint d; - float restVolume; - float compliance; -}; +#include "structs.comp" layout (std430, set = 0, binding = 0) buffer VertexBuffer { @@ -65,7 +43,7 @@ layout (push_constant, std430) uniform PushConstants { void preSolve(uint vID){ vertices[vID].prevPosition = vertices[vID].position; - if (vertices[vID].w == 0){ + if (vertices[vID].inverseMass == 0){ return; } vertices[vID].velocity += dt / k * gravity; @@ -104,13 +82,13 @@ void solveEdge(uint eID){ float alpha = edge.compliance / (dt / k) / (dt / k); - float s = -(currentLength - edge.restLength) / (v1.w + v2.w + alpha); + float s = -(currentLength - edge.restLength) / (v1.inverseMass + v2.inverseMass + alpha); vec3 g1 = normalize(diff); vec3 g2 = -g1; - vec3 delta1 = s * v1.w * g1; - vec3 delta2 = s * v2.w * g2; + vec3 delta1 = s * v1.inverseMass * g1; + vec3 delta2 = s * v2.inverseMass * g2; vertices[edge.a].position += delta1; vertices[edge.b].position += delta2; @@ -137,10 +115,10 @@ void solveTetrahedron(uint tetID){ vec3 gd = cross(b - a, c - a) / 6; float w = - va.w * dot(ga, ga) + - vb.w * dot(gb, gb) + - vc.w * dot(gc, gc) + - vd.w * dot(gd, gd); + va.inverseMass * dot(ga, ga) + + vb.inverseMass * dot(gb, gb) + + vc.inverseMass * dot(gc, gc) + + vd.inverseMass * dot(gd, gd); if (w == 0) return; @@ -148,10 +126,10 @@ void solveTetrahedron(uint tetID){ float s = -volumeError / (w + alpha); - vec3 addA = s * ga * va.w; - vec3 addB = s * gb * vb.w; - vec3 addC = s * gc * vc.w; - vec3 addD = s * gd * vd.w; + vec3 addA = s * ga * va.inverseMass; + vec3 addB = s * gb * vb.inverseMass; + vec3 addC = s * gc * vc.inverseMass; + vec3 addD = s * gd * vd.inverseMass; vertices[tetrahedron.a].position += addA; vertices[tetrahedron.b].position += addB; @@ -160,7 +138,7 @@ void solveTetrahedron(uint tetID){ } void postSolve(uint vID){ - if (vertices[vID].w == 0){ + if (vertices[vID].inverseMass == 0){ return; } vertices[vID].velocity = (vertices[vID].position - vertices[vID].prevPosition) / (dt / k); diff --git a/shaders/shader.frag b/shaders/shader.frag index 0cfb143..6fd2d13 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -2,11 +2,25 @@ layout (location = 0) out vec4 outColor; -layout (location = 0) in vec3 color; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 normal; +layout (location = 0) in vec2 uv; +layout (location = 1) in vec3 normal; void main() { - outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0); - // outColor = vec4(uv.x, uv.y, 0.0, 1.0); + // outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0); + + vec3 L = vec3(1, 1, 1); + L = normalize(L); + + vec3 N = normal; + + vec3 albedo = vec3(1, 1, 1); + + float lambertian = max(dot(L, N), 0); + vec3 diffuse = lambertian * albedo; + + vec3 ambient = vec3(1, 1, 1) * 0.01; + + vec3 C = ambient + diffuse; + + outColor = vec4(C, 1); } \ No newline at end of file diff --git a/shaders/shader.vert b/shaders/shader.vert index 29bafbd..73ec978 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -1,13 +1,11 @@ #version 450 layout (location = 0) in vec3 inPosition; -layout (location = 1) in vec3 inColor; -layout (location = 2) in vec2 inUV; -layout (location = 3) in vec3 inNormal; +layout (location = 1) in vec2 inUV; +layout (location = 2) in vec3 inNormal; -layout (location = 0) out vec3 color; -layout (location = 1) out vec2 uv; -layout (location = 2) out vec3 normal; +layout (location = 0) out vec2 uv; +layout (location = 1) out vec3 normal; layout (set = 0, binding = 0) uniform UniformBufferObject { mat4 view; @@ -17,7 +15,6 @@ layout (set = 0, binding = 0) uniform UniformBufferObject { void main() { gl_Position = camera.projection * camera.view * vec4(inPosition, 1.0); - color = inColor; normal = inNormal; uv = inUV; } \ No newline at end of file diff --git a/shaders/structs.comp b/shaders/structs.comp new file mode 100644 index 0000000..73ca294 --- /dev/null +++ b/shaders/structs.comp @@ -0,0 +1,38 @@ +struct Vertex { + vec3 position; + vec2 uv; + uvec3 normal; + vec3 velocity; + vec3 prevPosition; + float inverseMass; +}; + +struct Face { + uint a; + uint b; + uint c; +}; + +struct Edge { + uint a; + uint b; + float restLength; + float compliance; +}; + +struct Triangle { + uint a; + uint b; + uint c; + float restArea; + float compliance; +}; + +struct Tetrahedron { + uint a; + uint b; + uint c; + uint d; + float restVolume; + float compliance; +}; \ No newline at end of file diff --git a/src/mesh.cpp b/src/mesh.cpp index 0ce3062..18fe0dd 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -13,14 +13,13 @@ Mesh::Mesh(const std::string &fileName) { for (size_t i = 0; i < mesh->mNumVertices; i++){ glm::vec3 pos = *reinterpret_cast(&mesh->mVertices[i]); - glm::vec3 color = {0, 0, 0}; glm::vec2 uv = {0, 0}; if (mesh->mNumUVComponents[0] == 2) uv = *reinterpret_cast(&mesh->mTextureCoords[0][i]); glm::vec3 normal = {0, 0, 0}; if (mesh->mNormals != nullptr) normal = *reinterpret_cast(&mesh->mNormals[i]); - vertices.push_back({pos, color, uv, normal}); + vertices.push_back({pos, uv, normal}); } for (size_t i = 0; i < mesh->mNumFaces; i++){ diff --git a/src/soft_body.cpp b/src/soft_body.cpp index d251103..16b7022 100644 --- a/src/soft_body.cpp +++ b/src/soft_body.cpp @@ -16,18 +16,15 @@ SoftBody::SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance, f in.pointlist = new REAL[mesh->vertices.size() * 3]; in.numberoffacets = static_cast(mesh->faces.size()); in.facetlist = new tetgenio::facet[mesh->faces.size()]; - in.numberofpointattributes = 5; // color, uv - in.pointattributelist = new REAL[mesh->vertices.size() * 5]; + in.numberofpointattributes = 2; // uv + in.pointattributelist = new REAL[mesh->vertices.size() * 2]; for (size_t i = 0; i < mesh->vertices.size(); i++){ in.pointlist[i * 3 + 0] = mesh->vertices[i].position.x; in.pointlist[i * 3 + 1] = mesh->vertices[i].position.y; in.pointlist[i * 3 + 2] = mesh->vertices[i].position.z; - in.pointattributelist[i * 5 + 0] = mesh->vertices[i].color.r; - in.pointattributelist[i * 5 + 1] = mesh->vertices[i].color.g; - in.pointattributelist[i * 5 + 2] = mesh->vertices[i].color.b; - in.pointattributelist[i * 5 + 3] = mesh->vertices[i].uv.s; - in.pointattributelist[i * 5 + 4] = mesh->vertices[i].uv.t; + in.pointattributelist[i * 2 + 0] = mesh->vertices[i].uv.s; + in.pointattributelist[i * 2 + 1] = mesh->vertices[i].uv.t; } for (size_t i = 0; i < mesh->faces.size(); i++){ @@ -55,12 +52,9 @@ SoftBody::SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance, f float x = static_cast(out.pointlist[i * 3 + 0]); float y = static_cast(out.pointlist[i * 3 + 1]); float z = static_cast(out.pointlist[i * 3 + 2]); - float r = static_cast(out.pointattributelist[i * 5 + 0]); - float g = static_cast(out.pointattributelist[i * 5 + 1]); - float b = static_cast(out.pointattributelist[i * 5 + 2]); - float s = static_cast(out.pointattributelist[i * 5 + 3]); - float t = static_cast(out.pointattributelist[i * 5 + 4]); - vertices.emplace_back(Vertex({x, y, z}, {r, g, b}, {s, t})); + float s = static_cast(out.pointattributelist[i * 2 + 0]); + float t = static_cast(out.pointattributelist[i * 2 + 1]); + vertices.emplace_back(Vertex({x, y, z}, {s, t})); } faces.reserve(out.numberoftrifaces); diff --git a/src/vulkan/vertex.cpp b/src/vulkan/vertex.cpp index b840521..a33592f 100644 --- a/src/vulkan/vertex.cpp +++ b/src/vulkan/vertex.cpp @@ -9,27 +9,28 @@ VkVertexInputBindingDescription Vertex::getBindingDescription() { } std::vector Vertex::getAttributeDescriptions() { - std::vector attributeDescriptions(4); + std::vector attributeDescriptions(3); - attributeDescriptions[0].binding = 0; - attributeDescriptions[0].location = 0; - attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[0].offset = offsetof(Vertex, position); + attributeDescriptions[0] = { + .location = 0, + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(Vertex, position), + }; - attributeDescriptions[1].binding = 0; - attributeDescriptions[1].location = 1; - attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[1].offset = offsetof(Vertex, color); + attributeDescriptions[1] = { + .location = 1, + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(Vertex, uv), + }; - attributeDescriptions[2].binding = 0; - attributeDescriptions[2].location = 2; - attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[2].offset = offsetof(Vertex, uv); - - attributeDescriptions[3].binding = 0; - attributeDescriptions[3].location = 3; - attributeDescriptions[3].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[3].offset = offsetof(Vertex, normal); + attributeDescriptions[2] = { + .location = 2, + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(Vertex, normal), + }; return attributeDescriptions; }