Skip to content

Instantly share code, notes, and snippets.

@porky11
Created August 13, 2019 15:33
Show Gist options
  • Save porky11/e909eda0e2953e4b2edfe836c18b3384 to your computer and use it in GitHub Desktop.
Save porky11/e909eda0e2953e4b2edfe836c18b3384 to your computer and use it in GitHub Desktop.
Compute Shader test
#version 460
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
struct Sphere {
vec4 center;
};
layout(binding = 1, std140) uniform Camera {
vec4 pos;
vec4 dir;
} camera;
layout(binding = 1, std430) buffer Shapes {
Sphere shapes[16];
} shapes;
layout(binding = 0, std430) buffer ShapeCount {
uint count;
} shape_count;
layout(binding = 0, r32f) uniform writeonly image3D voxel_image;
vec4 multiply_quat(vec4 quat, vec3 vec) {
return vec4(
quat.x * vec.x + quat.y * vec.y + quat.z * vec.z,
quat.x * vec.y - quat.y * vec.x + quat.w * vec.z,
quat.x * vec.z - quat.z * vec.x - quat.w * vec.y,
quat.y * vec.z - quat.z * vec.y + quat.w * vec.x
);
}
vec3 rotate_back(vec4 rotated, vec4 quat) {
return vec3(
rotated.x * quat.x - rotated.y * quat.y - rotated.z * quat.z - rotated.w * quat.w,
rotated.x * quat.y + rotated.y * quat.x - rotated.z * quat.w + rotated.w * quat.z,
rotated.x * quat.z + rotated.y * quat.w + rotated.z * quat.x - rotated.w * quat.y
);
}
float len2(vec3 disvec) {
return (disvec.x * disvec.x + disvec.y * disvec.y + disvec.z * disvec.z);
}
float field_distance(vec3 p) {
Sphere shapes[16] = shapes.shapes;
float minimum_distance = 4096.0;
for (uint i = 0u; i < shape_count.count; ++i) {
vec3 disvec = (p - shapes[i].center.xyz);
float new_distance = sqrt(abs(len2(disvec))) - 1.0;
minimum_distance = (new_distance < minimum_distance) ? new_distance : minimum_distance;
}
return minimum_distance;
}
void main() {
uvec3 index = (gl_GlobalInvocationID * gl_WorkGroupSize) + gl_LocalInvocationID;
vec3 p = vec3(index.x - 32u, index.y - 32u, -64.0);
float len = sqrt(abs(len2(p)));
vec4 conjugate_quat = vec4(1, -1, -1, -1);
float d = 1.0 + (float(index.z) / float(16u));
imageStore(voxel_image, ivec3(index), vec4(field_distance((camera.pos.xyz + rotate_back(multiply_quat(camera.dir, p / len), camera.dir * conjugate_quat) * d)), 0, 0, 0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment