3D engine that AI agents drive
Specialize the Cycles kernel per scene: feature flags, lobe and candidate counts (#894)
Nguyen Xuan Anc148764bf19c
diff --git a/packages/render-native/src/cycles/abi.wgsl b/packages/render-native/src/cycles/abi.wgslindex 1782a77c..4d5feb63 100644--- a/packages/render-native/src/cycles/abi.wgsl+++ b/packages/render-native/src/cycles/abi.wgsl@@ -89,7 +89,26 @@ const ALPHA_MODE_MASK: u32 = 1u;const ALPHA_MODE_BLEND: u32 = 2u;// Closure lobe capacity and `Lobe.lobe_type` discriminants.-const MAX_CLOSURE_LOBES: u32 = 4u;+const MAX_CLOSURE_LOBES: u32 = 4u; // SPECIALIZE_LOBES+const MAX_CLOSURE_CANDIDATES: u32 = 7u; // SPECIALIZE_CANDIDATES++// Scene feature flags. The host rewrites `true` to `false` for features the packed scene never uses,+// so the guarded code is dead in that pipeline. `true` is the safe default.+const SCENE_HAS_VOLUME: bool = true;+const SCENE_HAS_ALPHA_MASK: bool = true;+const SCENE_HAS_TRANSPARENT: bool = true;+const SCENE_HAS_GLASS: bool = true;+const SCENE_HAS_COAT: bool = true;+const SCENE_HAS_SHEEN: bool = true;+const SCENE_HAS_ANISOTROPY: bool = true;+const SCENE_HAS_THIN_FILM: bool = true;+const SCENE_HAS_F82: bool = true;+const SCENE_HAS_NORMAL_MAP: bool = true;+const SCENE_HAS_EMISSIVE_TRIANGLES: bool = true;+const SCENE_HAS_ANALYTIC_LIGHTS: bool = true;+const SCENE_HAS_FINITE_LIGHTS: bool = true;+const SCENE_HAS_ENV_IMPORTANCE: bool = true;+const SCENE_HAS_LIGHT_LINKING: bool = true;const LOBE_NONE: u32 = 0u;const LOBE_DIFFUSE: u32 = 1u;const LOBE_GLOSSY_GGX: u32 = 2u;@@ -468,7 +487,7 @@ struct Lobe {// Stacked multi-lobe surface closure evaluated/sampled at `ShadingPoint`.struct Closure {- lobes: array<Lobe, 4>,+ lobes: array<Lobe, MAX_CLOSURE_LOBES>,emission: vec3<f32>,lobe_count: u32,diff --git a/packages/render-native/src/cycles/closure.wgsl b/packages/render-native/src/cycles/closure.wgslindex 098f3a89..6fb27510 100644--- a/packages/render-native/src/cycles/closure.wgsl+++ b/packages/render-native/src/cycles/closure.wgsl@@ -379,12 +379,9 @@ fn closure_thin_glass_fresnel(fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {var closure: Closure;- closure.lobes = array<Lobe, 4>(- closure_empty_lobe(),- closure_empty_lobe(),- closure_empty_lobe(),- closure_empty_lobe(),- );+ for (var lobe_slot: u32 = 0u; lobe_slot < MAX_CLOSURE_LOBES; lobe_slot = lobe_slot + 1u) {+ closure.lobes[lobe_slot] = closure_empty_lobe();+ }closure.emission = vec3<f32>(0.0);closure.lobe_count = 0u;closure.absorption_sigma = vec3<f32>(0.0);@@ -425,13 +422,13 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {base_color = max(base_color * bc_tex.rgb, vec3<f32>(0.0));tex_alpha = bc_tex.a;}- if (mat.alpha_tex >= 0) {+ if (SCENE_HAS_ALPHA_MASK && mat.alpha_tex >= 0) {tex_alpha = tex_alpha * closure_sample_texture(mat.alpha_tex, point).r;}var opacity = 1.0;let raw_alpha = clamp(mat.alpha * tex_alpha, 0.0, 1.0);- if (mat.alpha_mode == ALPHA_MODE_MASK) {+ if (SCENE_HAS_ALPHA_MASK && mat.alpha_mode == ALPHA_MODE_MASK) {if (raw_alpha < mat.alpha_cutoff) {let trans_lobe = closure_transparent_setup_lobe(smooth_normal, vec3<f32>(1.0));closure.lobes[0] = trans_lobe;@@ -439,7 +436,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {closure.total_sample_weight = trans_lobe.sample_weight;return closure;}- } else if (mat.alpha_mode == ALPHA_MODE_BLEND) {+ } else if (SCENE_HAS_TRANSPARENT && mat.alpha_mode == ALPHA_MODE_BLEND) {opacity = raw_alpha;if (opacity <= 0.0) {let trans_lobe = closure_transparent_setup_lobe(smooth_normal, vec3<f32>(1.0));@@ -458,35 +455,35 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {metallic = clamp(metallic * mr_tex.b, 0.0, 1.0);}- var transmission_weight = clamp(mat.transmission_weight, 0.0, 1.0);- if (mat.transmission_tex >= 0) {+ var transmission_weight = select(0.0, clamp(mat.transmission_weight, 0.0, 1.0), SCENE_HAS_GLASS);+ if (SCENE_HAS_GLASS && mat.transmission_tex >= 0) {transmission_weight = clamp(transmission_weight * closure_sample_texture(mat.transmission_tex, point).r, 0.0, 1.0);}- var coat_weight = max(mat.coat_weight, 0.0);+ var coat_weight = select(0.0, max(mat.coat_weight, 0.0), SCENE_HAS_COAT);var coat_roughness = clamp(mat.coat_roughness, 0.0, 1.0);- if (mat.coat_weight_roughness_tex >= 0) {+ if (SCENE_HAS_COAT && mat.coat_weight_roughness_tex >= 0) {let cwr_tex = closure_sample_texture(mat.coat_weight_roughness_tex, point);coat_weight = max(coat_weight * cwr_tex.r, 0.0);coat_roughness = clamp(coat_roughness * cwr_tex.g, 0.0, 1.0);}var coat_tint = max(mat.coat_tint, vec3<f32>(0.0));- if (mat.coat_tint_tex >= 0) {+ if (SCENE_HAS_COAT && mat.coat_tint_tex >= 0) {coat_tint = max(coat_tint * closure_sample_texture(mat.coat_tint_tex, point).rgb, vec3<f32>(0.0));}- var sheen_weight = max(mat.sheen_weight, 0.0);+ var sheen_weight = select(0.0, max(mat.sheen_weight, 0.0), SCENE_HAS_SHEEN);var sheen_tint = max(mat.sheen_tint, vec3<f32>(0.0));var sheen_roughness = clamp(mat.sheen_roughness, 0.0, 1.0);- if (mat.sheen_color_roughness_tex >= 0) {+ if (SCENE_HAS_SHEEN && mat.sheen_color_roughness_tex >= 0) {let scr_tex = closure_sample_texture(mat.sheen_color_roughness_tex, point);sheen_tint = max(sheen_tint * scr_tex.rgb, vec3<f32>(0.0));sheen_roughness = clamp(sheen_roughness * scr_tex.a, 0.0, 1.0);}- var anisotropic = clamp(mat.anisotropy, 0.0, 1.0);+ var anisotropic = select(0.0, clamp(mat.anisotropy, 0.0, 1.0), SCENE_HAS_ANISOTROPY);var aniso_rotation = mat.anisotropy_rotation;- if (mat.anisotropy_tex >= 0) {+ if (SCENE_HAS_ANISOTROPY && mat.anisotropy_tex >= 0) {let ani_tex = closure_sample_texture(mat.anisotropy_tex, point);anisotropic = clamp(anisotropic * ani_tex.b, 0.0, 1.0);let dir_xy = ani_tex.rg * 2.0 - vec2<f32>(1.0);@@ -500,20 +497,25 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {emission_color = max(emission_color * closure_sample_texture(mat.emission_tex, point).rgb, vec3<f32>(0.0));}- var shading_n = closure_apply_normal_map(mat.normal_tex, mat.normal_scale, point, smooth_normal, frame_t, frame_b);- shading_n = closure_apply_bump_map(mat.bump_tex, mat.bump_distance, point, shading_n, frame_t, frame_b);+ var shading_n = smooth_normal;+ if (SCENE_HAS_NORMAL_MAP) {+ shading_n = closure_apply_normal_map(mat.normal_tex, mat.normal_scale, point, smooth_normal, frame_t, frame_b);+ shading_n = closure_apply_bump_map(mat.bump_tex, mat.bump_distance, point, shading_n, frame_t, frame_b);+ }var raw_coat_n = bsdf_mf_safe_normalize_fallback(point.coat_normal, shading_n);- raw_coat_n = closure_apply_normal_map(mat.coat_normal_tex, mat.coat_normal_scale, point, raw_coat_n, frame_t, frame_b);+ if (SCENE_HAS_NORMAL_MAP) {+ raw_coat_n = closure_apply_normal_map(mat.coat_normal_tex, mat.coat_normal_scale, point, raw_coat_n, frame_t, frame_b);+ }let preserve_energy = (mat.flags & MATERIAL_FLAG_MULTISCATTER_GGX) != 0u;let thin_walled = (mat.flags & MATERIAL_FLAG_THIN_WALLED) != 0u;- var candidates: array<Lobe, 7>;+ var candidates: array<Lobe, MAX_CLOSURE_CANDIDATES>;var cand_count: u32 = 0u;var layer_weight = vec3<f32>(1.0);- if (opacity < 1.0) {+ if (SCENE_HAS_TRANSPARENT && opacity < 1.0) {let trans_lobe = closure_transparent_setup_lobe(shading_n, vec3<f32>(1.0 - opacity));if (trans_lobe.sample_weight > 0.0) {candidates[cand_count] = trans_lobe;@@ -522,7 +524,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {layer_weight = vec3<f32>(opacity);}- if (sheen_weight > CLOSURE_WEIGHT_CUTOFF) {+ if (SCENE_HAS_SHEEN && sheen_weight > CLOSURE_WEIGHT_CUTOFF) {let sheen_n = bsdf_mf_safe_normalize_fallback(mix(shading_n, raw_coat_n, clamp(coat_weight, 0.0, 1.0)), shading_n);let sheen_w = sheen_weight * sheen_tint * layer_weight;let sheen_setup = closure_sheen_setup_lobe(sheen_n, wo_dir, sheen_w, sheen_roughness);@@ -533,7 +535,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {}}- if (coat_weight > CLOSURE_WEIGHT_CUTOFF) {+ if (SCENE_HAS_COAT && coat_weight > CLOSURE_WEIGHT_CUTOFF) {let coat_w = coat_weight * layer_weight;let coat_setup = bsdf_coat_setup_lobe(point.geometric_normal,@@ -564,7 +566,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {if ((mat.flags & MATERIAL_FLAG_F82_METALLIC) == 0u && all(specular_tint == vec3<f32>(0.0))) {specular_tint = vec3<f32>(1.0);}- let thin_film_thickness = mat.thin_film_thickness;+ let thin_film_thickness = select(0.0, mat.thin_film_thickness, SCENE_HAS_THIN_FILM);let thin_film_ior = select(0.0, max(mat.thin_film_ior, 1e-5), thin_film_thickness > BSDF_MF_THINFILM_CUTOFF);var alpha_x = roughness * roughness;@@ -603,7 +605,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {layer_weight = layer_weight * (1.0 - metallic);}- if (transmission_weight > CLOSURE_WEIGHT_CUTOFF && bsdf_mf_max_component(layer_weight) > CLOSURE_WEIGHT_CUTOFF) {+ if (SCENE_HAS_GLASS && transmission_weight > CLOSURE_WEIGHT_CUTOFF && bsdf_mf_max_component(layer_weight) > CLOSURE_WEIGHT_CUTOFF) {let trans_w = transmission_weight * layer_weight;if (thin_walled) {let cos_no = max(dot(valid_refl_n, wo_dir), 0.0);@@ -725,7 +727,7 @@ fn closure_build(point: ShadingPoint, wo: vec3<f32>) -> Closure {}}- if (cand_count <= MAX_CLOSURE_LOBES) {+ if (MAX_CLOSURE_CANDIDATES <= MAX_CLOSURE_LOBES || cand_count <= MAX_CLOSURE_LOBES) {for (var i: u32 = 0u; i < cand_count; i = i + 1u) {closure.lobes[i] = candidates[i];closure.total_sample_weight = closure.total_sample_weight + candidates[i].sample_weight;diff --git a/packages/render-native/src/cycles/integrator.wgsl b/packages/render-native/src/cycles/integrator.wgslindex 9d1026ce..6dcc87ab 100644--- a/packages/render-native/src/cycles/integrator.wgsl+++ b/packages/render-native/src/cycles/integrator.wgsl@@ -364,7 +364,7 @@ fn int_trace_shadow(}}- if ((kernel_params.kernel_flags & KERNEL_FLAG_HAS_VOLUME) != 0u && kernel_params.volume_density > 0.0) {+ if (SCENE_HAS_VOLUME && (kernel_params.kernel_flags & KERNEL_FLAG_HAS_VOLUME) != 0u && kernel_params.volume_density > 0.0) {let overlap = int_volume_segment_overlap(ray_origin, ray_dir, 0.0, shadow_t_max);if (overlap.y > overlap.x) {transmittance *= exp(-kernel_params.volume_density * (overlap.y - overlap.x));@@ -693,7 +693,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {let limit_volume = kernel_params.max_volume_bounces + 1u;let sample_count = max(kernel_params.samples_per_dispatch, 1u);let exposure = select(1.0, kernel_params.film_exposure, kernel_params.film_exposure > 0.0);- let has_world_volume = (kernel_params.kernel_flags & KERNEL_FLAG_HAS_VOLUME) != 0u && kernel_params.volume_density > 0.0;+ let has_world_volume = SCENE_HAS_VOLUME && (kernel_params.kernel_flags & KERNEL_FLAG_HAS_VOLUME) != 0u && kernel_params.volume_density > 0.0;let filter_glossy_enabled = kernel_params.filter_glossy > 0.0;let inv_filter_glossy = select(1.0e30, 1.0 / max(kernel_params.filter_glossy, 1.0e-8), filter_glossy_enabled);@@ -809,11 +809,11 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {recorded_features = true;}- if (kernel_params.light_count > 0u) {+ if (SCENE_HAS_ANALYTIC_LIGHTS && kernel_params.light_count > 0u) {let light_samp = lgt_sample_analytic_light(scatter_pos, 0xFFFFFFFFu, 0xFFFFFFFFu, smp_next_1d(&rng), smp_next_2d(&rng));int_accumulate_volume_direct_sample(light_samp, scatter_pos, ray_dir, throughput, bounce, &sample_radiance);}- if (kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {+ if (SCENE_HAS_ENV_IMPORTANCE && kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {let env_samp = lgt_sample_environment(smp_next_4d(&rng));int_accumulate_volume_direct_sample(env_samp, scatter_pos, ray_dir, throughput, bounce, &sample_radiance);}@@ -942,7 +942,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {direct_sample.valid = 0u;direct_sample.pdf = 0.0;if (direct_source == 0u) {- if (kernel_params.light_count > 0u) {+ if (SCENE_HAS_ANALYTIC_LIGHTS && kernel_params.light_count > 0u) {direct_sample = lgt_sample_analytic_light(point.position,point.light_group_mask_lo,@@ -952,11 +952,11 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {);}} else if (direct_source == 1u) {- if (kernel_params.emissive_triangle_count > 0u) {+ if (SCENE_HAS_EMISSIVE_TRIANGLES && kernel_params.emissive_triangle_count > 0u) {direct_sample = lgt_sample_emissive_triangle(point, smp_next_1d(&rng), smp_next_2d(&rng));}} else {- if (kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {+ if (SCENE_HAS_ENV_IMPORTANCE && kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {direct_sample = lgt_sample_environment(smp_next_4d(&rng));}}diff --git a/packages/render-native/src/cycles/lights.wgsl b/packages/render-native/src/cycles/lights.wgslindex 7a01302c..17288a64 100644--- a/packages/render-native/src/cycles/lights.wgsl+++ b/packages/render-native/src/cycles/lights.wgsl@@ -46,6 +46,9 @@ fn lgt_power_heuristic(pdf_a: f32, pdf_b: f32) -> f32 {}fn lgt_links_match(receiver_lo: u32, receiver_hi: u32, emitter_lo: u32, emitter_hi: u32) -> bool {+ if (!SCENE_HAS_LIGHT_LINKING) {+ return true;+ }return ((receiver_lo & emitter_lo) | (receiver_hi & emitter_hi)) != 0u;}@@ -871,7 +874,7 @@ fn lgt_eval_escaped_ray(if (!is_transparent_background_ray) {let env_radiance = lgt_eval_hdri_radiance(ray_dir);var env_mis = 1.0;- if (!is_camera_ray && !mis_skip && kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {+ if (!is_camera_ray && !mis_skip && SCENE_HAS_ENV_IMPORTANCE && kernel_params.env_width > 0u && kernel_params.env_height > 0u && kernel_params.background_mis_weight > 0.0) {let pdf_env = lgt_environment_pdf(ray_dir);env_mis = lgt_power_heuristic(prev_bsdf_pdf, pdf_env);}diff --git a/packages/render-native/src/cycles/mod.rs b/packages/render-native/src/cycles/mod.rsindex 230a672a..1f2e736b 100644--- a/packages/render-native/src/cycles/mod.rs+++ b/packages/render-native/src/cycles/mod.rs@@ -113,6 +113,7 @@ pub(crate) fn specialize_cycles_kernel(source: &str, input: &NativeTraceInputV2)let mut has_f82 = false;let mut has_normal_map = false;let mut max_closure_lobes: u32 = if mat_count > 0 { 1 } else { 4 };+ let mut max_closure_candidates: u32 = if mat_count > 0 { 1 } else { 7 };for m in 0..mat_count {let base = m * MATERIAL_RECORD_STRIDE;@@ -188,8 +189,9 @@ pub(crate) fn specialize_cycles_kernel(source: &str, input: &NativeTraceInputV2)} else {u32::from(metallic > 1.0e-5 || metallic_roughness_tex >= 0) + glass_lobes + 2};- let mat_lobes = (trans_lobe + sheen_lobe + coat_lobe + base_lobes).clamp(1, 4);- max_closure_lobes = max_closure_lobes.max(mat_lobes);+ let candidate_lobes = trans_lobe + sheen_lobe + coat_lobe + base_lobes;+ max_closure_candidates = max_closure_candidates.max(candidate_lobes.clamp(1, 7));+ max_closure_lobes = max_closure_lobes.max(candidate_lobes.clamp(1, 4));}// --- Per-light flags ---@@ -281,6 +283,12 @@ pub(crate) fn specialize_cycles_kernel(source: &str, input: &NativeTraceInputV2)&format!("const MAX_CLOSURE_LOBES: u32 = {max_closure_lobes}u; // SPECIALIZE_LOBES"),);}+ if max_closure_candidates < 7 {+ result = result.replace(+ "const MAX_CLOSURE_CANDIDATES: u32 = 7u; // SPECIALIZE_CANDIDATES",+ &format!("const MAX_CLOSURE_CANDIDATES: u32 = {max_closure_candidates}u; // SPECIALIZE_CANDIDATES"),+ );+ }result}@@ -387,7 +395,8 @@ mod specialization_tests {const SCENE_HAS_FINITE_LIGHTS: bool = true;\n\const SCENE_HAS_ENV_IMPORTANCE: bool = true;\n\const SCENE_HAS_LIGHT_LINKING: bool = true;\n\- const MAX_CLOSURE_LOBES: u32 = 4u; // SPECIALIZE_LOBES";+ const MAX_CLOSURE_LOBES: u32 = 4u; // SPECIALIZE_LOBES+ const MAX_CLOSURE_CANDIDATES: u32 = 7u; // SPECIALIZE_CANDIDATES";let result = specialize_cycles_kernel(source, &input);assert!(result.contains("const SCENE_HAS_VOLUME: bool = false;"), "VOLUME should be false");assert!(result.contains("const SCENE_HAS_GLASS: bool = false;"), "GLASS should be false");@@ -405,6 +414,10 @@ mod specialization_tests {assert!(result.contains("const SCENE_HAS_ENV_IMPORTANCE: bool = false;"), "ENV_IMPORTANCE should be false");assert!(result.contains("const SCENE_HAS_LIGHT_LINKING: bool = false;"), "LIGHT_LINKING should be false");assert!(result.contains("const MAX_CLOSURE_LOBES: u32 = 2u; // SPECIALIZE_LOBES"), "MAX_CLOSURE_LOBES should be 2u");+ assert!(+ result.contains("const MAX_CLOSURE_CANDIDATES: u32 = 2u; // SPECIALIZE_CANDIDATES"),+ "MAX_CLOSURE_CANDIDATES should be 2u"+ );}#[test]