aboutsummaryrefslogtreecommitdiffstats
path: root/shaders
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-08-01 18:45:50 +0200
committerladevieq <ladevie.quentin@gmail.com>2021-08-01 19:03:21 +0200
commit93fa0b2e214fbb70e6c03f5ca7536c7c32544aa7 (patch)
treed95fa69b1efb9f7d23f0ec61aee804bebe054350 /shaders
parent0dafba17af044532fa2f14c61176e7994336f56c (diff)
Pack bvh nodes, optimize vertices size
Diffstat (limited to 'shaders')
-rw-r--r--shaders/compute.comp54
1 files changed, 24 insertions, 30 deletions
diff --git a/shaders/compute.comp b/shaders/compute.comp
index dc82b91..3a4a043 100644
--- a/shaders/compute.comp
+++ b/shaders/compute.comp
@@ -28,15 +28,9 @@ struct hit_info {
bool front_face;
};
-struct aabb {
- vec4 min;
- vec4 max;
-};
-
struct sphere {
vec4 position;
material mat;
- aabb bb;
float radius;
float[3] padding;
};
@@ -44,11 +38,11 @@ struct sphere {
struct vertex {
vec4 position;
};
+
struct triangle {
vertex v1;
vertex v2;
vertex v3;
- aabb bb;
};
struct camera {
@@ -67,10 +61,10 @@ struct camera {
};
struct bvh_node {
- aabb bb;
+ vec3 min;
int next_id;
+ vec3 max;
int primitive_id;
- uint[2] padding;
};
layout(set = 0, binding = 0) buffer scene_metadata {
@@ -143,10 +137,10 @@ vec2 disk_vec(inout uint seed) {
return vec2(cos(a) * r, sin(a) * r);
}
-bool hit_aabb(aabb b, ray r, float min_t, float max_t) {
+bool hit_aabb(vec3 minimum, vec3 maximum, ray r, float min_t, float max_t) {
const vec3 invdir = 1.0 / r.direction;
- const vec3 f = (b.max.xyz - r.origin.xyz) * invdir;
- const vec3 n = (b.min.xyz - r.origin.xyz) * invdir;
+ const vec3 f = (maximum.xyz - r.origin.xyz) * invdir;
+ const vec3 n = (minimum.xyz - r.origin.xyz) * invdir;
const vec3 tmax = max(f, n);
const vec3 tmin = min(f, n);
@@ -157,20 +151,20 @@ bool hit_aabb(aabb b, ray r, float min_t, float max_t) {
return t1 >= t0;
}
-uint hit_aabbs(ray r, float min_t, float max_t) {
- uint hit_count = 0;
- // for (uint idx = 0; idx <= spheres_count * 2; idx++) {
- for (uint idx = 0; idx <= triangles_count * 2; idx++) {
- hit_count += uint(hit_aabb(nodes[idx].bb, r, min_t, max_t));
-
- if (nodes[idx].primitive_id >= 0) {
- // hit_count += uint(hit_aabb(spheres[nodes[idx].primitive_id].bb, r, min_t, max_t));
- hit_count += uint(hit_aabb(triangles[nodes[idx].primitive_id].bb, r, min_t, max_t));
- }
- }
-
- return hit_count;
-}
+// uint hit_aabbs(ray r, float min_t, float max_t) {
+// uint hit_count = 0;
+// // for (uint idx = 0; idx <= spheres_count * 2; idx++) {
+// for (uint idx = 0; idx <= triangles_count * 2; idx++) {
+// hit_count += uint(hit_aabb(nodes[idx].bb, r, min_t, max_t));
+//
+// if (nodes[idx].primitive_id >= 0) {
+// // hit_count += uint(hit_aabb(spheres[nodes[idx].primitive_id].bb, r, min_t, max_t));
+// // hit_count += uint(hit_aabb(triangles[nodes[idx].primitive_id].bb, r, min_t, max_t));
+// }
+// }
+//
+// return hit_count;
+// }
bool hit_sphere(sphere s, ray r, float min_t, float max_t, out hit_info info) {
vec3 origin_center = r.origin - s.position.xyz;
@@ -253,7 +247,7 @@ bool hit_node(ray r, float min_t, float max_t, out hit_info info) {
}
id = nodes[id].next_id;
} else { // Internal nodes
- if (hit_aabb(nodes[id].bb, r, min_t, closest_t)) {
+ if (hit_aabb(nodes[id].min, nodes[id].max, r, min_t, closest_t)) {
id++;
} else {
id = nodes[id].next_id;
@@ -372,9 +366,9 @@ void main() {
ray r = ray(cam.position.xyz + offset, proj_plane_pos.xyz - cam.position.xyz - offset);
vec4 out_color = ray_color(r, seed);
- if (debug_bvh) {
- out_color -= hit_aabbs(r, 0.001, 1e15) * vec4(0.0, 0.025, 0.025, 0.0);
- }
+ // if (debug_bvh) {
+ // out_color -= hit_aabbs(r, 0.001, 1e15) * vec4(0.0, 0.025, 0.025, 0.0);
+ // }
float spp_scale = (1.0 / sample_index);
vec4 acc_color = imageLoad(accumulation_image, ivec2(x, y));