You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.7 KiB

4 months ago
#version 450
layout (local_size_x = 32) in;
4 months ago
#include "structs.comp"
4 months ago
layout (std430, set = 0, binding = 0) buffer VertexBuffer {
4 months ago
Vertex vertices[];
4 months ago
};
4 months ago
layout (std430, set = 0, binding = 1) buffer FaceBuffer {
4 months ago
Face faces[];
4 months ago
};
4 months ago
layout (std140, set = 0, binding = 5) uniform Sizes {
4 months ago
uint vertexCount;
uint faceCount;
4 months ago
};
4 months ago
4 months ago
layout (push_constant) uniform PushConstants {
4 months ago
uint state;
4 months ago
};
void atomicAddVec3(uint vID, vec3 add){
4 months ago
for (int i = 0; i < 3; i++) {
uint expected_memory = vertices[vID].normal[i];
float floatInput = uintBitsToFloat(vertices[vID].normal[i]) + add[i];
uint actual_content = atomicCompSwap(vertices[vID].normal[i], expected_memory, floatBitsToUint(floatInput));
while (actual_content != expected_memory) {
expected_memory = actual_content;
floatInput = uintBitsToFloat(expected_memory) + add[i];
actual_content = atomicCompSwap(vertices[vID].normal[i], expected_memory, floatBitsToUint(floatInput));
}
}
4 months ago
}
4 months ago
void reset(uint vID){
4 months ago
vertices[vID].normal = floatBitsToUint(vec3(0, 0, 0));
4 months ago
}
4 months ago
void accumulate(uint fID){
4 months ago
Face f = faces[fID];
4 months ago
4 months ago
Vertex v1 = vertices[f.a];
Vertex v2 = vertices[f.b];
Vertex v3 = vertices[f.c];
4 months ago
4 months ago
vec3 weightedNormal = cross(v3.position - v1.position, v2.position - v1.position);
atomicAddVec3(f.a, weightedNormal);
atomicAddVec3(f.b, weightedNormal);
atomicAddVec3(f.c, weightedNormal);
4 months ago
}
4 months ago
void norm(uint vID){
4 months ago
vertices[vID].normal = floatBitsToUint(normalize(uintBitsToFloat(vertices[vID].normal)));
4 months ago
}
void main() {
4 months ago
uint id = gl_GlobalInvocationID.x;
switch (state){
case 0:
if (id < vertexCount){
reset(id);
}
break;
case 1:
if (id < faceCount){
accumulate(id);
}
break;
case 2:
if (id < vertexCount){
norm(id);
}
break;
}
4 months ago
}