From f14855296485250277a7216c711eb598148d8346 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Thu, 29 Aug 2024 15:01:38 -0700 Subject: [PATCH] Ray-caster: only draw the top few fragments This seems to increase graphics pipe use from 50--60% to 55--65%. --- .../inversive-display/src/inversive.frag | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/app-proto/inversive-display/src/inversive.frag b/app-proto/inversive-display/src/inversive.frag index 7eb16b5..2fcd86e 100644 --- a/app-proto/inversive-display/src/inversive.frag +++ b/app-proto/inversive-display/src/inversive.frag @@ -25,10 +25,10 @@ vecInv sphere(vec3 center, float radius) { // construction. the SPHERE_MAX array size seems to affect frame rate a lot, // even though we should only be using the first few elements of each array -const int SPHERE_MAX_UNIFORM = 200; +const int SPHERE_MAX = 200; uniform int sphere_cnt; -uniform vecInv sphere_list[SPHERE_MAX_UNIFORM]; -uniform vec3 color_list[SPHERE_MAX_UNIFORM]; +uniform vecInv sphere_list[SPHERE_MAX]; +uniform vec3 color_list[SPHERE_MAX]; // view uniform vec2 resolution; @@ -135,8 +135,8 @@ void main() { vec3 dir = vec3(focal_slope * scr, -1.); // cast rays through the spheres - const int SPHERE_MAX_INTERNAL = 6; - taggedFrag frags [2*SPHERE_MAX_INTERNAL]; + const int LAYER_MAX = 12; + taggedFrag frags [LAYER_MAX]; int layer_cnt = 0; for (int id = 0; id < sphere_cnt; ++id) { // find out where the ray hits the sphere @@ -144,17 +144,20 @@ void main() { // insertion-sort the fragments we hit into the fragment list for (int side = 0; side < 2; ++side) { - if (hit_depths[side] > 0.) { + float hit_z = -hit_depths[side]; + if (0. > hit_z) { for (int layer = layer_cnt; layer >= 0; --layer) { - if (layer < 1 || frags[layer-1].pt.z >= -hit_depths[side]) { + if (layer < 1 || frags[layer-1].pt.z >= hit_z) { // we're not as close to the screen as the fragment // before the empty slot, so insert here - frags[layer] = sphere_shading( - sphere_list[id], - hit_depths[side] * dir, - color_list[id], - id - ); + if (layer < LAYER_MAX) { + frags[layer] = sphere_shading( + sphere_list[id], + hit_depths[side] * dir, + color_list[id], + id + ); + } break; } else { // we're closer to the screen than the fragment before @@ -163,7 +166,7 @@ void main() { frags[layer] = frags[layer-1]; } } - ++layer_cnt; + layer_cnt = min(layer_cnt + 1, LAYER_MAX); } } }