Ray-caster: pass the sphere count as a uniform

In the process, start exploring array size limits of various kinds.
This commit is contained in:
Aaron Fenyes 2024-08-26 00:43:42 -07:00
parent c5fe725b1b
commit 85db7b9be0
2 changed files with 29 additions and 10 deletions

View File

@ -23,8 +23,10 @@ vecInv sphere(vec3 center, float radius) {
// --- uniforms --- // --- uniforms ---
// construction // construction. the SPHERE_MAX array size seems to affect frame rate a lot,
const int SPHERE_MAX = 256; // 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]; uniform vecInv sphere_list[SPHERE_MAX];
// view // view
@ -128,13 +130,11 @@ vec2 sphere_cast(vecInv v, vec3 dir) {
} }
void main() { void main() {
const int sphere_cnt = 3;
vec2 scr = (2.*gl_FragCoord.xy - resolution) / shortdim; vec2 scr = (2.*gl_FragCoord.xy - resolution) / shortdim;
vec3 dir = vec3(focal_slope * scr, -1.); vec3 dir = vec3(focal_slope * scr, -1.);
// initialize two spheres // initialize two spheres
vecInv sphere_list_internal [sphere_cnt]; vecInv sphere_list_internal [SPHERE_MAX];
if (use_test_construction) { if (use_test_construction) {
/* DEBUG */ /* DEBUG */
// spheres 0 and 1 are identical in the test construction hard-coded // 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]; 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[0] = vec3(1., 0.25, 0.);
color_list[1] = vec3(0., 0.25, 1.); color_list[1] = vec3(0., 0.25, 1.);
color_list[2] = vec3(0.25, 0., 1.0); color_list[2] = vec3(0.25, 0., 1.0);
// cast rays through the spheres // cast rays through the spheres
vec2 depth_pairs [sphere_cnt]; vec2 depth_pairs [SPHERE_MAX];
taggedFrag frags [2*sphere_cnt]; taggedFrag frags [2*SPHERE_MAX];
int frag_cnt = 0; int frag_cnt = 0;
for (int i = 0; i < sphere_cnt; ++i) { for (int i = 0; i < sphere_cnt; ++i) {
vec2 hit_depths = sphere_cast(sphere_list_internal[i], dir); vec2 hit_depths = sphere_cast(sphere_list_internal[i], dir);

View File

@ -101,7 +101,7 @@ fn main() {
on_mount(move || { on_mount(move || {
// list construction elements // list construction elements
const SPHERE_MAX: usize = 256; const SPHERE_MAX: usize = 12;
let mut sphere_vec = Vec::<DVector<f64>>::new(); let mut sphere_vec = Vec::<DVector<f64>>::new();
// get the display canvas // get the display canvas
@ -142,14 +142,32 @@ fn main() {
console::log_1(&JsValue::from(link_msg)); console::log_1(&JsValue::from(link_msg));
ctx.use_program(Some(&program)); 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 // 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::<SPHERE_MAX>( let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "sp" &ctx, &program, "sphere_list", "sp"
); );
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>( let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "lt" &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 resolution_loc = ctx.get_uniform_location(&program, "resolution");
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim"); let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
let ctrl_loc = ctx.get_uniform_location(&program, "ctrl"); /* DEBUG */ 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)); ctx.uniform1f(shortdim_loc.as_ref(), width.min(height));
// pass the construction // pass the construction
ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_vec.len() as i32);
for n in 0..sphere_vec.len() { for n in 0..sphere_vec.len() {
let v = &sphere_vec[n]; let v = &sphere_vec[n];
ctx.uniform3f( ctx.uniform3f(