From 85db7b9be08a11f66f0dc9cf6b192139bdefdf1e Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 26 Aug 2024 00:43:42 -0700 Subject: [PATCH] Ray-caster: pass the sphere count as a uniform In the process, start exploring array size limits of various kinds. --- .../inversive-display/src/inversive.frag | 16 ++++++------- app-proto/inversive-display/src/main.rs | 23 +++++++++++++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app-proto/inversive-display/src/inversive.frag b/app-proto/inversive-display/src/inversive.frag index ff6ad6b..fdfcc0b 100644 --- a/app-proto/inversive-display/src/inversive.frag +++ b/app-proto/inversive-display/src/inversive.frag @@ -23,8 +23,10 @@ vecInv sphere(vec3 center, float radius) { // --- uniforms --- -// construction -const int SPHERE_MAX = 256; +// 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 = 12; +uniform int sphere_cnt; uniform vecInv sphere_list[SPHERE_MAX]; // view @@ -128,13 +130,11 @@ vec2 sphere_cast(vecInv v, vec3 dir) { } void main() { - const int sphere_cnt = 3; - vec2 scr = (2.*gl_FragCoord.xy - resolution) / shortdim; vec3 dir = vec3(focal_slope * scr, -1.); // initialize two spheres - vecInv sphere_list_internal [sphere_cnt]; + vecInv sphere_list_internal [SPHERE_MAX]; if (use_test_construction) { /* DEBUG */ // spheres 0 and 1 are identical in the test construction hard-coded @@ -149,14 +149,14 @@ void main() { sphere_list_internal[i] = sphere_list[i]; } } - vec3 color_list [sphere_cnt]; + vec3 color_list [SPHERE_MAX]; color_list[0] = vec3(1., 0.25, 0.); color_list[1] = vec3(0., 0.25, 1.); color_list[2] = vec3(0.25, 0., 1.0); // cast rays through the spheres - vec2 depth_pairs [sphere_cnt]; - taggedFrag frags [2*sphere_cnt]; + vec2 depth_pairs [SPHERE_MAX]; + taggedFrag frags [2*SPHERE_MAX]; int frag_cnt = 0; for (int i = 0; i < sphere_cnt; ++i) { vec2 hit_depths = sphere_cast(sphere_list_internal[i], dir); diff --git a/app-proto/inversive-display/src/main.rs b/app-proto/inversive-display/src/main.rs index e2097e7..eceecd7 100644 --- a/app-proto/inversive-display/src/main.rs +++ b/app-proto/inversive-display/src/main.rs @@ -101,7 +101,7 @@ fn main() { on_mount(move || { // list construction elements - const SPHERE_MAX: usize = 256; + const SPHERE_MAX: usize = 12; let mut sphere_vec = Vec::>::new(); // get the display canvas @@ -142,14 +142,32 @@ fn main() { console::log_1(&JsValue::from(link_msg)); ctx.use_program(Some(&program)); + /* DEBUG */ + // print the maximum number of vectors that can be passed as + // uniforms to a fragment shader. the OpenGL ES 3.0 standard + // requires this maximum to be at least 224, as discussed in the + // documentation of the GL_MAX_FRAGMENT_UNIFORM_VECTORS parameter + // here: + // + // https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glGet.xhtml + // + // there are also other size limits. for example, on Aaron's + // machine, the the length of a float or genType array seems to be + // capped at 1024 elements + console::log_2( + &ctx.get_parameter(WebGl2RenderingContext::MAX_FRAGMENT_UNIFORM_VECTORS).unwrap(), + &JsValue::from("uniform vectors available") + ); + // find indices of vertex attributes and uniforms + let position_index = ctx.get_attrib_location(&program, "position") as u32; + let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt"); let sphere_sp_locs = get_uniform_array_locations::( &ctx, &program, "sphere_list", "sp" ); let sphere_lt_locs = get_uniform_array_locations::( &ctx, &program, "sphere_list", "lt" ); - let position_index = ctx.get_attrib_location(&program, "position") as u32; let resolution_loc = ctx.get_uniform_location(&program, "resolution"); let shortdim_loc = ctx.get_uniform_location(&program, "shortdim"); let ctrl_loc = ctx.get_uniform_location(&program, "ctrl"); /* DEBUG */ @@ -192,6 +210,7 @@ fn main() { ctx.uniform1f(shortdim_loc.as_ref(), width.min(height)); // pass the construction + ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_vec.len() as i32); for n in 0..sphere_vec.len() { let v = &sphere_vec[n]; ctx.uniform3f(