grow compute shaders

This commit is contained in:
zomseffen 2025-04-17 15:47:35 +02:00
parent 62e4062cbf
commit 8902c2a0e3
16 changed files with 648 additions and 56 deletions

View file

@ -9,4 +9,7 @@ C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cuboid.geom -o shaders/compiled/geo
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_quad.vert -o shaders/compiled/vert_rt_quad.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_quad.frag -o shaders/compiled/frag_rt_quad.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute.comp -o shaders/compiled/rt_compute.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_rasterize.comp -o shaders/compiled/rt_compute_rasterize.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_one.comp -o shaders/compiled/rt_compute_grow_one.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_two.comp -o shaders/compiled/rt_compute_grow_two.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_three.comp -o shaders/compiled/rt_compute_grow_three.spv

View file

@ -8,4 +8,9 @@ glslc shaders/cuboid.frag -o shaders/compiled/frag_cuboid.spv
glslc shaders/cuboid.geom -o shaders/compiled/geo_cuboid.spv
glslc shaders/rt_quad.vert -o shaders/compiled/vert_rt_quad.spv
glslc shaders/rt_quad.frag -o shaders/compiled/frag_rt_quad.spv
glslc shaders/rt_quad.frag -o shaders/compiled/frag_rt_quad.spv
glslc shaders/rt_compute_rasterize.comp -o shaders/compiled/rt_compute_rasterize.spv
glslc shaders/rt_compute_grow_one.comp -o shaders/compiled/rt_compute_grow_one.spv
glslc shaders/rt_compute_grow_two.comp -o shaders/compiled/rt_compute_grow_two.spv
glslc shaders/rt_compute_grow_three.comp -o shaders/compiled/rt_compute_grow_three.spv

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,82 @@
#version 450
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;
layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 4) readonly buffer ColorBuffer {
uint grid_in[];
};
layout(binding = 9) readonly buffer TransparentBuffer {
bool transparent_grid[];
};
layout(binding = 8) buffer SizeBuffer3D {
uint grid_out[];
};
layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
void main() {
uint index = gl_GlobalInvocationID.x;
uint output_offset = 0;
uint compound_start = 0;
// iterate over the compounds and find the work index inside of it
while (index > compounds[compound_start] * compounds[compound_start]) {
output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start];
index -= compounds[compound_start] * compounds[compound_start];
compound_start = compounds[compound_start + 2];
}
// grid pos in the task
uint compound_grid_size = compounds[compound_start];
float compound_scale = uintBitsToFloat(compounds[compound_start + 1]);
vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5);
uint y = index % compound_grid_size;
uint z = (index - y) / compound_grid_size;
vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7]));
// iterate upwards along the x axis
bool seen_empty = false;
uint start = 0;
uint last_col = 0;
for (uint x=0; x < compound_grid_size; x++) {
uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = 0;
// check if we need to stop a volume
if (color_val != 0 && !transparent) {
// check if we are in a volume right now
if (seen_empty) {
// close the current volume
grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = x - start;
seen_empty = false;
last_col = 0;
}
} else {
// check if transparency changed
if (seen_empty && transparent && last_col != color_val) {
// if we switch colors close the current volume and prepare for a new one
grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = x - start;
seen_empty = false;
}
// start a new volume if we are not in one right now
if (!seen_empty) {
seen_empty = true;
start = x;
last_col = color_val;
}
}
}
if (seen_empty) {
grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = compound_grid_size - start;
}
}

View file

@ -0,0 +1,101 @@
#version 450
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;
layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 4) readonly buffer ColorBuffer {
uint grid_in[];
};
layout(binding = 9) readonly buffer TransparentBuffer {
bool transparent_grid[];
};
layout(binding = 7) readonly buffer SizeBuffer2D {
uint grid_size_in[];
};
layout(binding = 8) buffer SizeBuffer3D {
uint grid_out[];
};
layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
void main() {
uint index = gl_GlobalInvocationID.x;
uint output_offset = 0;
uint compound_start = 0;
// iterate over the compounds and find the work index inside of it
while (index > compounds[compound_start] * compounds[compound_start]) {
output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start] * 2;
index -= compounds[compound_start] * compounds[compound_start];
compound_start = compounds[compound_start + 2];
}
// grid pos in the task
uint compound_grid_size = compounds[compound_start];
float compound_scale = uintBitsToFloat(compounds[compound_start + 1]);
vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5);
uint x = index % compound_grid_size;
uint y = (index - x) / compound_grid_size;
vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7]));
// iterate upwards along the x axis
bool seen_empty = false;
uint start = 0;
uint start_x_size = 0;
uint start_y_size = 0;
uint last_col = 0;
for (uint z=0; z < compound_grid_size; z++) {
uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
uint current_x_size = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2];
uint current_y_size = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2 + 1];
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3] = 0;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 1] = 0;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 2] = 0;
// check if we need to stop a volume
if (color_val != 0 && !transparent) {
// check if we are in a volume right now
if (seen_empty) {
// close the current volume
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = z - start;
seen_empty = false;
last_col = 0;
}
} else {
// check if transparency changed
if (seen_empty && ((transparent && last_col != color_val) || (start_x_size != current_x_size) || (start_y_size != current_y_size))) {
// if we switch colors or size close the current volume and prepare for a new one
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = z - start;
seen_empty = false;
}
// start a new volume if we are not in one right now
if (!seen_empty && current_x_size != 0 && current_y_size != 0) {
seen_empty = true;
start = z;
start_x_size = current_x_size;
start_y_size = current_y_size;
last_col = color_val;
}
}
}
if (seen_empty) {
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = compound_grid_size - start;
}
}

View file

@ -0,0 +1,94 @@
#version 450
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;
layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 4) readonly buffer ColorBuffer {
uint grid_in[];
};
layout(binding = 9) readonly buffer TransparentBuffer {
bool transparent_grid[];
};
layout(binding = 7) buffer SizeBuffer2D {
uint grid_out[];
};
layout(binding = 8) readonly buffer SizeBuffer3D {
uint grid_size_in[];
};
layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
void main() {
uint index = gl_GlobalInvocationID.x;
uint output_offset = 0;
uint compound_start = 0;
// iterate over the compounds and find the work index inside of it
while (index > compounds[compound_start] * compounds[compound_start]) {
output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start] * 2;
index -= compounds[compound_start] * compounds[compound_start];
compound_start = compounds[compound_start + 2];
}
// grid pos in the task
uint compound_grid_size = compounds[compound_start];
float compound_scale = uintBitsToFloat(compounds[compound_start + 1]);
vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5);
uint x = index % compound_grid_size;
uint z = (index - x) / compound_grid_size;
vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7]));
// iterate upwards along the x axis
bool seen_empty = false;
uint start = 0;
uint start_x_size = 0;
uint last_col = 0;
for (uint y=0; y < compound_grid_size; y++) {
uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
uint current_x_size = grid_size_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2] = 0;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2 + 1] = 0;
// check if we need to stop a volume
if (color_val != 0 && !transparent) {
// check if we are in a volume right now
if (seen_empty) {
// close the current volume
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = y - start;
seen_empty = false;
last_col = 0;
}
} else {
// check if transparency changed
if (seen_empty && ((transparent && last_col != color_val) || (start_x_size != current_x_size))) {
// if we switch colors or size close the current volume and prepare for a new one
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = y - start;
seen_empty = false;
}
// start a new volume if we are not in one right now
if (!seen_empty && current_x_size != 0) {
seen_empty = true;
start = y;
start_x_size = current_x_size;
last_col = color_val;
}
}
}
if (seen_empty) {
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size;
grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = compound_grid_size - start;
}
}

View file

@ -1,6 +1,5 @@
#version 450
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 geom_rot;
@ -15,7 +14,7 @@ layout(binding = 3) readonly buffer SceneInfoBuffer {
};
layout(binding = 4) buffer SceneInfoBuffer2 {
uint volumes[];
uint grid[];
};
layout(binding = 5) buffer SizedVertices {
@ -26,6 +25,10 @@ layout(binding = 6) buffer Indices {
uint indices[];
};
layout(binding = 9) buffer transparencies {
bool transparent_grid[];
};
layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
uvec4 unpack_color(uint val) {
@ -190,6 +193,7 @@ void main() {
for (uint z=0; z < compound_grid_size; z++) {
// iterate over the included shapes
vec3 check_pos = compound_pos + vec3(float(x) * compound_scale, float(y) * compound_scale, float(z) * compound_scale) + mid_offset;
uint color_int;
uvec4 color_roughness;
bool render = false;
vec3 color = vec3(0.0, 0.0, 1.0);
@ -214,9 +218,10 @@ void main() {
vec3(0.0, 0.0, 1.0)
);
uvec4 component_color = unpack_color(compounds[component_index + 7]);
color_int = compounds[component_index + 7];
uvec4 component_color = unpack_color(color_int);
uint transparent = compounds[component_index + 8];
transparent = compounds[component_index + 8] != 0;
if (component_type == 0) {
// handle sphere
@ -292,8 +297,6 @@ void main() {
);
uvec4 color = unpack_color(compounds[component_index + 7]);
uint transparent = compounds[component_index + 8];
if (component_type == 0) {
// handle sphere
float radius = uintBitsToFloat(compounds[component_index + 9]);
@ -344,9 +347,12 @@ void main() {
}
if (render) {
grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = color_int;
transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = transparent;
add_cube(output_offset + index * compound_grid_size + z, compound_scale, check_pos, color);
} else {
grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = 0;
transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = false;
}
}
//volumes[index] = compounds[index];
}