shaders use #include

feature/softbody-runtime-control
Benjamin Kraft 3 months ago
parent 3f010bc2f6
commit a47c167edf
  1. 1
      include/vulkan/vertex.hpp
  2. 2
      shaders/compile.sh
  3. 16
      shaders/grab.comp
  4. 16
      shaders/normal.comp
  5. 54
      shaders/pbd.comp
  6. 24
      shaders/shader.frag
  7. 11
      shaders/shader.vert
  8. 38
      shaders/structs.comp
  9. 3
      src/mesh.cpp
  10. 20
      src/soft_body.cpp
  11. 37
      src/vulkan/vertex.cpp

@ -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;

@ -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

@ -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[];

@ -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[];

@ -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);

@ -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);
}

@ -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;
}

@ -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;
};

@ -13,14 +13,13 @@ Mesh::Mesh(const std::string &fileName) {
for (size_t i = 0; i < mesh->mNumVertices; i++){
glm::vec3 pos = *reinterpret_cast<glm::vec3*>(&mesh->mVertices[i]);
glm::vec3 color = {0, 0, 0};
glm::vec2 uv = {0, 0};
if (mesh->mNumUVComponents[0] == 2)
uv = *reinterpret_cast<glm::vec2*>(&mesh->mTextureCoords[0][i]);
glm::vec3 normal = {0, 0, 0};
if (mesh->mNormals != nullptr)
normal = *reinterpret_cast<glm::vec3*>(&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++){

@ -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<int>(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<float>(out.pointlist[i * 3 + 0]);
float y = static_cast<float>(out.pointlist[i * 3 + 1]);
float z = static_cast<float>(out.pointlist[i * 3 + 2]);
float r = static_cast<float>(out.pointattributelist[i * 5 + 0]);
float g = static_cast<float>(out.pointattributelist[i * 5 + 1]);
float b = static_cast<float>(out.pointattributelist[i * 5 + 2]);
float s = static_cast<float>(out.pointattributelist[i * 5 + 3]);
float t = static_cast<float>(out.pointattributelist[i * 5 + 4]);
vertices.emplace_back(Vertex({x, y, z}, {r, g, b}, {s, t}));
float s = static_cast<float>(out.pointattributelist[i * 2 + 0]);
float t = static_cast<float>(out.pointattributelist[i * 2 + 1]);
vertices.emplace_back(Vertex({x, y, z}, {s, t}));
}
faces.reserve(out.numberoftrifaces);

@ -9,27 +9,28 @@ VkVertexInputBindingDescription Vertex::getBindingDescription() {
}
std::vector<VkVertexInputAttributeDescription> Vertex::getAttributeDescriptions() {
std::vector<VkVertexInputAttributeDescription> attributeDescriptions(4);
std::vector<VkVertexInputAttributeDescription> 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;
}

Loading…
Cancel
Save