diff --git a/FORMAT-v2.md b/FORMAT-v2.md index 5270e42..79a1ad0 100644 --- a/FORMAT-v2.md +++ b/FORMAT-v2.md @@ -95,7 +95,7 @@ Total size: **336 bytes** (`21 × 16 B`), alignment: **16 bytes**. | `308` | `4` | `4` | `u32` | `portal_count` | Light portals stored after the `light_count` emitters in the light table (section 6); `0` for none | | `312` | `8` | `8` | `vec2` | `tile_origin` | Pixel origin `[x, y]` of the tile one dispatch covers. The packer writes `[0, 0]`; the host writes it for every dispatch (zero when the frame is not split), so whatever a pack carries here never shifts the picture. `dispatchBudgetMs` splits a frame into tiles; the kernel adds the origin to the global invocation id | | `320` | `4` | `4` | `f32` | `aperture_ratio` | Anamorphic bokeh: the width of the aperture over its height (Cycles `aperture_ratio`). The kernel multiplies the lens sample's x by `1 / aperture_ratio`; the packer writes `1.0` by default and accepts `0.01` to `100`; a block with `0.0` here is read as `1.0` | -| `324` | `4` | `4` | `u32` | `reserved_aperture_0` | Reserved (`0`), pads the block to a multiple of 16 bytes | +| `324` | `4` | `4` | `u32` | `caustic_photons` | Light paths traced from the emitters per pixel sample (`1` to `64`) while the `kernel_flags` bit `8192=CAUSTIC_LIGHT_TRACING` is set, `0` otherwise. A word of an older block is `0`, so it renders as before. See "Caustic light tracing" in the README | | `328` | `4` | `4` | `u32` | `reserved_aperture_1` | Reserved (`0`) | | `332` | `4` | `4` | `u32` | `reserved_aperture_2` | Reserved (`0`) | diff --git a/README.md b/README.md index cc0ad60..fca20d5 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,8 @@ A rect area light with `portal: true` is a light portal, as in Cycles: it emits Cook's `cycles` kernel samples with Cycles' Sobol-Burley blue-noise pattern (`sampler: 'sobol-burley'`, default; `'white'` keeps the earlier hash sampler, and `'pcg'` and `'sobol'` are other names for `'white'` and `'sobol-burley'`). It picks lights through the Cycles light tree over the analytic lights, a sun and the emissive triangles (`lightSampling: 'auto'`, default: tree from more than one emitter; `'tree'`; `'uniform'` for the flat pick, and `useLightTree: true|false` is another name for `'tree'|'uniform'`). Naming an option twice with different values is an error. `envCompensation: true` (a fraction from 0 to 1 also works; off by default) subtracts the mean radiance from the environment sampling table so light sampling covers only the bright part of the map, which lowers the error under a sunny HDRI. The pixel filter is `box` unless `filterType` says otherwise. Details and measurements: [docs/cycles-parity.md](https://github.com/codepawl/crucible/blob/main/docs/cycles-parity.md), layout: [FORMAT-v2.md](./FORMAT-v2.md). +`caustics: "light-tracing"` (a packing option, off by default; `causticPhotons`, 1 to 64, default 1, sets the light paths per pixel sample) renders what a small light does through glass onto a diffuse surface, such as the rainbow of a prism, which camera paths find too rarely to be anything but speckle. Paths start on the emissive triangles, follow only smooth glass and mirror bounces (dispersion included, with the hero wavelength of the path), and at the first rough or diffuse hit, after at least one such bounce through a solid (not thin-walled) surface, are joined to the pinhole camera with a visibility ray and splatted into the image. The camera paths drop the same light (a diffuse vertex, then a smooth bounce, then an emitter), so nothing counts twice; the two are not combined by MIS. Limits: emissive triangles only (analytic lights and the environment are not light traced), a pinhole (no depth of field in the caustic, no `orthographic`), no `noiseThreshold`, and a caustic seen through glass is not joined. `filterGlossy` does not blur a smooth glass or mirror lobe while it is on (the light paths follow those lobes exactly, and a blurred camera-path copy of the caustic would count twice). Every count is bounded and an option the kernel cannot use throws with what to pass. With the option off the kernel is compiled without any of this and renders the same pixels as before. + `shadowRayPick: "contribution"` (a packing option; the default `"all"` keeps one shadow ray per light sample) traces one shadow ray per vertex: the light tree sample and the environment sample are both evaluated without a shadow ray, one is picked in proportion to the luminance of its unshadowed contribution, and its value is divided by the probability of the pick. The mean is the same; the noise and the time per sample are being measured, so it stays off. Four more options of the `cycles` kernel; `pathRegeneration` is on by default, the other three are off: diff --git a/pack-input-v2.mjs b/pack-input-v2.mjs index b7a926d..4e781bf 100644 --- a/pack-input-v2.mjs +++ b/pack-input-v2.mjs @@ -66,6 +66,14 @@ export const KERNEL_FLAG_PATH_REGENERATION = 1024; /** Set when any texture of the pool stores mip levels; the kernel then carries a ray footprint (see FORMAT-v2.md). */ export const KERNEL_FLAG_TEXTURE_MIPS = 2048; export const KERNEL_FLAG_SHADOW_RAY_PICK = 4096; +/** Set by `caustics: "light-tracing"`: see `KERNEL_FLAG_CAUSTIC_LIGHT_TRACING` in abi.wgsl. */ +export const KERNEL_FLAG_CAUSTIC_LIGHT_TRACING = 8192; +/** `caustics` values: "off" (default) or "light-tracing" (paths from the emitters through glass, connected to the camera). */ +export const VALID_CAUSTICS = Object.freeze(['off', 'light-tracing']); +export const MAX_CAUSTIC_PHOTONS = 64; +export const DEFAULT_CAUSTIC_PHOTONS = 1; +/** Byte offset of `caustic_photons` inside a `KernelParamsV2` buffer (the first word after `aperture_ratio`). */ +export const KERNEL_PARAMS_V2_CAUSTIC_PHOTONS_BYTE_OFFSET = 324; /** `flags` of a material extension record: the diffuse transmission colour multiplies the base colour. */ const MATERIAL_EXTENSION_FLAG_TINTS_BASE_COLOR = 1; @@ -2194,6 +2202,41 @@ export function validateShadowRayPickOption(shadowRayPick, callerName) { } } +/** + * Throws unless `caustics` is undefined, "off" or "light-tracing", and `causticPhotons` is a whole number from 1 to 64 that comes + * with "light-tracing". Light tracing needs a perspective camera and fixed sampling (no `noiseThreshold`), which it names. + */ +export function validateCausticsOptions(options, callerName) { + const { caustics, causticPhotons } = options; + if (caustics !== undefined && !VALID_CAUSTICS.includes(caustics)) { + throw new RangeError( + `${callerName}: \`caustics\` must be "off" (default) or "light-tracing" (paths from the emitters through glass, connected to the camera), got ${JSON.stringify(caustics)}.` + ); + } + if (causticPhotons !== undefined) { + if (!Number.isInteger(causticPhotons) || causticPhotons < 1 || causticPhotons > MAX_CAUSTIC_PHOTONS) { + throw new RangeError( + `${callerName}: \`causticPhotons\` must be a whole number from 1 to ${MAX_CAUSTIC_PHOTONS} (light paths per pixel sample), got ${JSON.stringify(causticPhotons)}.` + ); + } + if (caustics !== 'light-tracing') { + throw new RangeError( + `${callerName}: \`causticPhotons\` only applies with \`caustics: "light-tracing"\`; pass that option or remove \`causticPhotons\`.` + ); + } + } + if (caustics !== 'light-tracing') { + return; + } + if (options.orthographic === true) { + throw new RangeError(`${callerName}: \`caustics: "light-tracing"\` connects light paths to a pinhole camera and does not support \`orthographic: true\`; use a perspective camera or remove \`caustics\`.`); + } + const adaptiveThreshold = Number(options.noiseThreshold ?? options.adaptiveThreshold ?? 0); + if (adaptiveThreshold > 0) { + throw new RangeError(`${callerName}: \`caustics: "light-tracing"\` splats photons over the whole image, so a pixel cannot stop early; remove \`noiseThreshold\`/\`adaptiveThreshold\` or \`caustics\`.`); + } +} + export function validatePackOptions(options, callerName, requireDimensionsAndSamples = false) { if (!options || typeof options !== 'object') { throw new TypeError(`${callerName}: \`options\` must be an object.`); @@ -2215,6 +2258,7 @@ export function validatePackOptions(options, callerName, requireDimensionsAndSam resolveSamplingOptions(options, callerName); resolveEnvCompensationFraction(options.envCompensation, callerName); validateShadowRayPickOption(options.shadowRayPick, callerName); + validateCausticsOptions(options, callerName); validateDenoiseOption(options.denoise, callerName); validateTextureMipsOption(options.textureMips, 'textureMips', callerName); validateFilterOptions(options, callerName); @@ -2652,6 +2696,11 @@ function packKernelParamsV2({ if (options.shadowRayPick === 'contribution') { kernelFlags |= KERNEL_FLAG_SHADOW_RAY_PICK; } + validateCausticsOptions(options, 'packKernelParamsV2'); + const causticPhotons = options.caustics === 'light-tracing' ? (options.causticPhotons ?? DEFAULT_CAUSTIC_PHOTONS) : 0; + if (causticPhotons > 0) { + kernelFlags |= KERNEL_FLAG_CAUSTIC_LIGHT_TRACING; + } const maxBounces = Math.max(0, Math.round(Number(options.bounces ?? options.maxBounces ?? 8))); const maxDiffuseBounces = Math.max(0, Math.round(Number(options.diffuseBounces ?? maxBounces))); @@ -2747,6 +2796,7 @@ function packKernelParamsV2({ u32[77] = (counts.portalCount ?? 0) >>> 0; u32[78] = 0; u32[79] = 0; + u32[KERNEL_PARAMS_V2_CAUSTIC_PHOTONS_BYTE_OFFSET / 4] = causticPhotons >>> 0; return new Uint8Array(buffer); } diff --git a/src/cycles/abi.wgsl b/src/cycles/abi.wgsl index 836c359..5ea633b 100644 --- a/src/cycles/abi.wgsl +++ b/src/cycles/abi.wgsl @@ -30,6 +30,7 @@ const BINDING_TEXTURE_DESCRIPTORS: u32 = 8u; const BINDING_TEXTURE_TEXELS: u32 = 9u; const BINDING_ENVIRONMENT_TEXELS: u32 = 10u; const BINDING_FILM_ACCUMULATION: u32 = 11u; +const BINDING_CAUSTIC_SPLAT: u32 = 15u; // `KernelParamsV2.kernel_flags` bits. const KERNEL_FLAG_USE_LIGHT_TREE: u32 = 1u; @@ -56,6 +57,12 @@ const KERNEL_FLAG_TEXTURE_MIPS: u32 = 2048u; // Set: each vertex traces one shadow ray for one of its direct light samples, picked in proportion to the luminance of the unshadowed // contribution, instead of one ray per sample. Same mean, fewer rays. Clear (default): every direct sample gets its own shadow ray. const KERNEL_FLAG_SHADOW_RAY_PICK: u32 = 4096u; +// Set (`caustics: "light-tracing"`): paths also start on the emissive triangles, follow only smooth glass and mirror bounces, and from +// the first diffuse hit after at least one such bounce are connected to the pinhole camera and splatted into `caustic_splat` +// (`KernelParamsV2.caustic_photons` paths per pixel sample). The camera paths then drop the same light: a path that leaves a diffuse +// vertex, crosses a smooth glass or mirror bounce and reaches an emitter counts nothing. Clear (default): none of this exists in the +// compiled kernel (the host sets `FRAME_HAS_CAUSTIC_LIGHT_TRACING` to false), so the image is the one without the option, bit for bit. +const KERNEL_FLAG_CAUSTIC_LIGHT_TRACING: u32 = 8192u; // `KernelParamsV2.filter_type` values. const FILTER_BOX: u32 = 0u; @@ -148,6 +155,8 @@ const SCENE_HAS_FINITE_LIGHTS: bool = true; const SCENE_HAS_ENV_IMPORTANCE: bool = true; const SCENE_HAS_LIGHT_LINKING: bool = true; const SCENE_HAS_LIGHT_TREE: bool = true; +// Frame setting, not scene: false unless the frame's `kernel_flags` carry KERNEL_FLAG_CAUSTIC_LIGHT_TRACING. `true` is the safe default. +const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = true; const LOBE_NONE: u32 = 0u; const LOBE_DIFFUSE: u32 = 1u; const LOBE_GLOSSY_GGX: u32 = 2u; @@ -251,7 +260,8 @@ struct KernelParamsV2 { // Anamorphic bokeh: the aperture width over its height (Cycles `aperture_ratio`); `0.0` is read as `1.0`. aperture_ratio: f32, - reserved_aperture_0: u32, + // Light paths traced per pixel sample when KERNEL_FLAG_CAUSTIC_LIGHT_TRACING is set (1 to 64); `0` otherwise. + caustic_photons: u32, reserved_aperture_1: u32, reserved_aperture_2: u32, } @@ -599,6 +609,8 @@ struct ClosureSample { @group(0) @binding(12) var cycles_lut: array; @group(0) @binding(13) var light_tree_emitters: array; @group(0) @binding(14) var light_to_tree: array; +// Caustic light tracing: four u32 per pixel (red, green, blue, unused), fixed point with `LT_FIXED_POINT_SCALE`, summed with atomicAdd. +@group(0) @binding(15) var caustic_splat: array>; // --------------------------------------------------------------------------------------------------------------------- // Cross-lane closure interface contract (defined in Lane L4 `src/cycles/closure.wgsl`, called by Lane L3 integrator) diff --git a/src/cycles/integrator.wgsl b/src/cycles/integrator.wgsl index 60f52b2..3dec594 100644 --- a/src/cycles/integrator.wgsl +++ b/src/cycles/integrator.wgsl @@ -681,6 +681,12 @@ fn int_apply_filter_glossy(closure: ptr, inv_filter_glossy: f for (var i = 0u; i < count; i += 1u) { let lobe_type = (*closure).lobes[i].lobe_type; if (lobe_type == LOBE_GLOSSY_GGX || lobe_type == LOBE_GLASS_GGX || lobe_type == LOBE_COAT_GGX) { + // With caustic light tracing a smooth glass or mirror lobe stays smooth: the light paths follow it exactly, and the camera + // paths must see it as singular too, or they would keep a blurred copy of the caustic the light paths already carry. + if (FRAME_HAS_CAUSTIC_LIGHT_TRACING && kernel_params.caustic_photons > 0u + && bsdf_mf_is_singular((*closure).lobes[i].alpha_x, (*closure).lobes[i].alpha_y)) { + continue; + } (*closure).lobes[i].alpha_x = max(blur_roughness, (*closure).lobes[i].alpha_x); (*closure).lobes[i].alpha_y = max(blur_roughness, (*closure).lobes[i].alpha_y); } @@ -933,6 +939,271 @@ fn int_accumulate_volume_direct_sample( *radiance += int_clamp_contribution(unshadowed * visibility, i32(bounce)); } +// ---- Caustic light tracing (KERNEL_FLAG_CAUSTIC_LIGHT_TRACING) ------------------------------------------------------------------- +// A light path starts on an emissive triangle (chosen by the emissive CDF, a point uniform in area, a cosine direction on a random +// side), follows only smooth glass and mirror bounces (the samples `int_is_singular_bsdf_sample` calls singular) and at every vertex +// with a non-singular lobe joins the pinhole camera, if the path crossed at least one non-thin-walled caster. The contribution is +// splatted into `caustic_splat`. A path that meets a rough or diffuse lobe by sampling ends there. The camera paths in `main` drop +// the light that these paths carry (a diffuse vertex, then a smooth caster, then an emitter), so no light counts twice. There is no +// MIS between the two: the camera path's chance to find a small emitter through glass is what light tracing replaces. + +const LT_FIXED_POINT_SCALE: f32 = 4096.0; +// One photon adds at most this much to a channel of a pixel, before the fixed point scale; a firefly past it is cut, not wrapped. +const LT_MAX_CHANNEL_CONTRIBUTION: f32 = 65536.0; +const LT_MAX_VERTICES: u32 = 24u; + +fn lt_draw(photon_key: vec3, draw_counter: ptr) -> vec4 { + let words = smp_pcg4d(vec4(photon_key.x, photon_key.y, photon_key.z, *draw_counter + 0x1B873593u)); + *draw_counter += 1u; + return vec4( + smp_word_to_unit_float(words.x), + smp_word_to_unit_float(words.y), + smp_word_to_unit_float(words.z), + smp_word_to_unit_float(words.w), + ); +} + +struct LtCameraConnection { + valid: bool, + pixel_x: u32, + pixel_y: u32, + // Direction from the surface point to the camera and the distance to it. + to_camera: vec3, + distance: f32, + // Radiance times this and the number of photons gives the pixel value: the area of the surface per unit of raster area. + area_weight: f32, +} + +// Where a surface point lands on the image of the pinhole camera `cam_generate_ray` builds, and how much surface area a unit of +// raster area covers there (the pixel is one unit). Not valid behind the camera, outside the frame or outside the clip planes. +fn lt_camera_connection(position: vec3, geometric_normal: vec3) -> LtCameraConnection { + var connection: LtCameraConnection; + connection.valid = false; + let offset_to_camera = kernel_params.camera_position - position; + let distance = length(offset_to_camera); + if (distance <= 1.0e-6) { + return connection; + } + let to_camera = offset_to_camera / distance; + let view = -to_camera; + let forward = cam_safe_unit_axis(kernel_params.camera_forward, vec3(0.0, 0.0, -1.0)); + let depth_cosine = dot(view, forward); + if (depth_cosine <= 1.0e-6) { + return connection; + } + let depth = distance * depth_cosine; + let near_plane = max(kernel_params.camera_near, 0.0); + if (depth < near_plane || (kernel_params.camera_far > kernel_params.camera_near && depth > kernel_params.camera_far)) { + return connection; + } + let right_axis = kernel_params.camera_right; + let up_axis = kernel_params.camera_up; + let right_length_sq = max(dot(right_axis, right_axis), 1.0e-20); + let up_length_sq = max(dot(up_axis, up_axis), 1.0e-20); + let sensor_x = dot(view, right_axis) / (depth_cosine * right_length_sq); + let sensor_y = dot(view, up_axis) / (depth_cosine * up_length_sq); + let width = f32(kernel_params.image_width); + let height = f32(kernel_params.image_height); + let raster_x = (sensor_x - 2.0 * kernel_params.lens_shift.x + 1.0) * 0.5 * width; + let raster_y = (1.0 - sensor_y + 2.0 * kernel_params.lens_shift.y) * 0.5 * height; + if (!(raster_x >= 0.0 && raster_x < width && raster_y >= 0.0 && raster_y < height)) { + return connection; + } + let surface_cosine = abs(dot(geometric_normal, to_camera)); + // dA = d(solid angle) * dist^2 / cos(surface); d(solid angle) = |right| |up| / |w|^3 d(sensor), |w| = 1 / depth_cosine, and one raster + // unit is (2 / width) * (2 / height) of sensor area. + let area_weight = surface_cosine * width * height + / (4.0 * sqrt(right_length_sq) * sqrt(up_length_sq) * depth_cosine * depth_cosine * depth_cosine * distance * distance); + connection.valid = area_weight > 0.0 && area_weight < 1.0e30; + connection.pixel_x = u32(raster_x); + connection.pixel_y = u32(raster_y); + connection.to_camera = to_camera; + connection.distance = distance; + connection.area_weight = area_weight; + return connection; +} + +fn lt_splat(pixel_x: u32, pixel_y: u32, value: vec3, rounding: vec3) { + let clean = clamp(int_sanitize_radiance(value), vec3(0.0), vec3(LT_MAX_CHANNEL_CONTRIBUTION)); + // Stochastic rounding keeps the sum unbiased for contributions below one fixed point step. + let fixed = vec3(floor(clean * LT_FIXED_POINT_SCALE + rounding)); + let base = (pixel_y * kernel_params.image_width + pixel_x) * 4u; + atomicAdd(&caustic_splat[base], fixed.x); + atomicAdd(&caustic_splat[base + 1u], fixed.y); + atomicAdd(&caustic_splat[base + 2u], fixed.z); +} + +fn int_trace_caustic_photon(pixel_x: u32, pixel_y: u32, sample_index: u32, photon_index: u32) { + let emitter_count = kernel_params.emissive_triangle_count; + if (!SCENE_HAS_EMISSIVE_TRIANGLES || emitter_count == 0u) { + return; + } + let photons_per_sample = kernel_params.caustic_photons; + let photon_key = vec3( + pixel_x ^ (kernel_params.seed * 0x9E3779B9u + 0x68E31DA4u), + pixel_y ^ (kernel_params.seed * 0x85EBCA6Bu + 0x3C6EF372u), + sample_index * photons_per_sample + photon_index, + ); + var draw_counter = 0u; + let pick = lt_draw(photon_key, &draw_counter); + let emitter_index = lgt_binary_search_emissive_cdf(pick.x, emitter_count); + let emitter = emissive_triangles[emitter_index]; + if (emitter.selection_pdf <= 0.0 || emitter.area <= 0.0) { + return; + } + let sqrt_u = sqrt(pick.y); + let bary_u = 1.0 - sqrt_u; + let bary_v = pick.z * sqrt_u; + let start = emitter.p0 + emitter.edge1 * bary_u + emitter.edge2 * bary_v; + + // Both faces emit (a light sample uses |cos|), so a face is chosen at random and the direction pdf is cos / (2 pi). + let direction_draw = lt_draw(photon_key, &draw_counter); + let emitter_normal = emitter.geometric_normal * select(-1.0, 1.0, direction_draw.z < 0.5); + var tangent = vec3(0.0); + var bitangent = vec3(0.0); + lgt_make_orthonormal_basis(emitter_normal, &tangent, &bitangent); + let disk_radius = sqrt(direction_draw.x); + let disk_angle = TWO_PI * direction_draw.y; + let first_direction = normalize( + tangent * (disk_radius * cos(disk_angle)) + + bitangent * (disk_radius * sin(disk_angle)) + + emitter_normal * sqrt(max(1.0 - direction_draw.x, 1.0e-8)), + ); + + let emitter_material = materials[emitter.material_index]; + var emitted = emitter.radiance; + if (SCENE_HAS_EMISSION_MAP && emitter_material.emission_tex >= 0) { + emitted = lgt_textured_emitter_radiance(emitter, bary_u, bary_v); + } + var throughput = emitted * (emitter.area * TWO_PI / emitter.selection_pdf); + + var wavelength_um = SPECTRAL_D_LINE_UM; + var has_spectral_weight = false; + if (SCENE_HAS_DISPERSION) { + wavelength_um = spectral_wavelength_um(lt_draw(photon_key, &draw_counter).x); + } + + let photon_total = f32(kernel_params.image_width) * f32(kernel_params.image_height) + * f32(max(kernel_params.total_samples, 1u)) * f32(max(photons_per_sample, 1u)); + let visibility_mask = INSTANCE_VISIBILITY_DIFFUSE | INSTANCE_VISIBILITY_GLOSSY | INSTANCE_VISIBILITY_TRANSMISSION; + + var ray_origin = start; + var ray_dir = first_direction; + var ray_t_min = 0.0; + var self_instance = emitter.instance_index; + var self_triangle = emitter.triangle_index; + var caster_crossings = 0u; + var medium_sigma = vec3(0.0); + var in_medium = false; + var medium_stack_instances: array; + var medium_stack_sigmas: array, 4>; + var medium_stack_depth = 0u; + + for (var vertex = 0u; vertex < LT_MAX_VERTICES; vertex += 1u) { + let surface_hit = int_trace_closest_surface(ray_origin, ray_dir, ray_t_min, 1.0e30, visibility_mask, self_instance, self_triangle); + if (surface_hit.hit == 0u) { + return; + } + if (in_medium) { + throughput *= exp(-medium_sigma * max(surface_hit.t - ray_t_min, 0.0)); + } + let wo = -ray_dir; + var point = int_build_shading_point(surface_hit, wo, vec2(0.0)); + var closure = closure_build(&point, wo, wavelength_um); + let material = materials[point.material_index]; + let is_dispersive_glass = SCENE_HAS_DISPERSION + && (material.transmission_weight > 0.0 || material.transmission_tex >= 0) + && (material.flags & MATERIAL_FLAG_THIN_WALLED) == 0u + && closure_material_dispersion(point.material_index) > 0.0; + if (is_dispersive_glass && !has_spectral_weight) { + throughput *= spectral_rgb_weight(wavelength_um); + has_spectral_weight = true; + } + if (closure.lobe_count == 0u || closure.total_sample_weight <= 0.0) { + return; + } + + if (caster_crossings > 0u && int_closure_has_evaluable_lobe(&closure)) { + let connection = lt_camera_connection(point.position, point.geometric_normal); + let receives_light = lgt_links_match( + point.light_group_mask_lo, + point.light_group_mask_hi, + emitter.light_group_mask_lo, + emitter.light_group_mask_hi, + ); + if (connection.valid && receives_light) { + let bsdf = closure_eval(&closure, wo, connection.to_camera); + if (bsdf.value.x > 0.0 || bsdf.value.y > 0.0 || bsdf.value.z > 0.0) { + let shadow_origin = int_ray_offset_origin(&point, connection.to_camera); + let camera_visibility = int_trace_shadow( + shadow_origin, + connection.to_camera, + connection.distance, + point.instance_index, + point.triangle_index, + INVALID_INDEX, + INVALID_INDEX, + 0u, + ); + let contribution = throughput * bsdf.value * camera_visibility * (connection.area_weight / photon_total); + if (contribution.x > 0.0 || contribution.y > 0.0 || contribution.z > 0.0) { + lt_splat(connection.pixel_x, connection.pixel_y, contribution, lt_draw(photon_key, &draw_counter).xyz); + } + } + } + } + + let step_draw = lt_draw(photon_key, &draw_counter); + let bsdf_sample = closure_sample(&closure, wo, step_draw.xyz); + if (bsdf_sample.pdf <= 0.0 || (bsdf_sample.eval.x <= 0.0 && bsdf_sample.eval.y <= 0.0 && bsdf_sample.eval.z <= 0.0)) { + return; + } + if (bsdf_sample.sampled_lobe_type == LOBE_TRANSPARENT) { + throughput *= bsdf_sample.eval / bsdf_sample.pdf; + ray_t_min = surface_hit.t + max(kernel_params.ray_epsilon, 1.0e-6); + self_instance = point.instance_index; + self_triangle = point.triangle_index; + continue; + } + if (!int_is_singular_bsdf_sample(&closure, &point, bsdf_sample)) { + return; + } + throughput *= bsdf_sample.eval / bsdf_sample.pdf; + + let is_thin_walled = (material.flags & MATERIAL_FLAG_THIN_WALLED) != 0u; + let cos_ng_wi = dot(point.geometric_normal, bsdf_sample.wi); + let is_transmission = (bsdf_sample.sampled_lobe_type == LOBE_GLASS_GGX || bsdf_sample.sampled_lobe_type == LOBE_GLOSSY_GGX) + && cos_ng_wi < 0.0; + if (!is_thin_walled) { + caster_crossings += 1u; + if (is_transmission) { + if (point.front_face != 0u) { + int_medium_stack_push( + &medium_stack_instances, + &medium_stack_sigmas, + &medium_stack_depth, + point.instance_index, + max(closure.absorption_sigma, vec3(0.0)), + ); + } else { + int_medium_stack_remove(&medium_stack_instances, &medium_stack_sigmas, &medium_stack_depth, point.instance_index); + } + if (medium_stack_depth > 0u) { + medium_sigma = medium_stack_sigmas[medium_stack_depth - 1u]; + } else { + medium_sigma = vec3(0.0); + } + in_medium = medium_sigma.x > 0.0 || medium_sigma.y > 0.0 || medium_sigma.z > 0.0; + } + } + ray_origin = int_ray_offset_origin(&point, bsdf_sample.wi); + ray_dir = bsdf_sample.wi; + ray_t_min = 0.0; + self_instance = point.instance_index; + self_triangle = point.triangle_index; + } +} + @compute @workgroup_size(8, 8, 1) fn main(@builtin(global_invocation_id) global_id: vec3) { let pixel_x = global_id.x + kernel_params.tile_origin.x; @@ -1016,6 +1287,10 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { var prev_recv_hi = 0xFFFFFFFFu; var terminate_on_hit = false; var terminate_on_next_surface = false; + // Caustic light tracing: whether the path left a surface vertex with a non-singular lobe, and how many smooth casters (not thin + // sheets) it crossed since. An emitter reached with both set is the light the photon paths carry, so it counts nothing here. + var lt_left_surface_vertex = false; + var lt_caster_chain = 0u; var bounce = 0u; var diffuse_bounces = 0u; @@ -1077,6 +1352,8 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { prev_recv_hi = 0xFFFFFFFFu; terminate_on_hit = false; terminate_on_next_surface = false; + lt_left_surface_vertex = false; + lt_caster_chain = 0u; bounce = 0u; diffuse_bounces = 0u; @@ -1093,6 +1370,12 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { first_background_coverage = 0.0; path_step = 0u; path_active = true; + + if (FRAME_HAS_CAUSTIC_LIGHT_TRACING && kernel_params.caustic_photons > 0u) { + for (var photon_index = 0u; photon_index < kernel_params.caustic_photons; photon_index += 1u) { + int_trace_caustic_photon(pixel_x, pixel_y, sample_index, photon_index); + } + } } for (var iteration_step = 0u; iteration_step < steps_per_iteration; iteration_step += 1u) { @@ -1213,6 +1496,8 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { prev_recv_lo = 0xFFFFFFFFu; prev_recv_hi = 0xFFFFFFFFu; lgt_remember_volume_mis_origin(scatter_pos, ray_dir); + lt_left_surface_vertex = false; + lt_caster_chain = 0u; path_active = true; continue; } @@ -1275,7 +1560,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { } if (closure.emission.x > 0.0 || closure.emission.y > 0.0 || closure.emission.z > 0.0) { - let emission_weight = lgt_emissive_surface_weight( + var emission_weight = lgt_emissive_surface_weight( point.emissive_index, surface_hit.t, ray_dir, @@ -1285,6 +1570,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { prev_recv_lo, prev_recv_hi, ); + if (FRAME_HAS_CAUSTIC_LIGHT_TRACING && lt_left_surface_vertex && lt_caster_chain > 0u) { + emission_weight = 0.0; + } if (emission_weight > 0.0) { sample_radiance += int_clamp_contribution(throughput * closure.emission * emission_weight, i32(bounce) - 1); } @@ -1402,6 +1690,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { let is_singular = int_is_singular_bsdf_sample(&closure, &point, bsdf_sample); throughput *= bsdf_sample.eval / bsdf_sample.pdf; + if (FRAME_HAS_CAUSTIC_LIGHT_TRACING) { + if (!is_singular) { + lt_left_surface_vertex = true; + lt_caster_chain = 0u; + } else if ((material.flags & MATERIAL_FLAG_THIN_WALLED) == 0u) { + lt_caster_chain += 1u; + } + } bounce += 1u; let cos_ng_wi = dot(point.geometric_normal, bsdf_sample.wi); diff --git a/src/cycles/mod.rs b/src/cycles/mod.rs index 4e72aad..acba4e7 100644 --- a/src/cycles/mod.rs +++ b/src/cycles/mod.rs @@ -18,6 +18,9 @@ include!(concat!(env!("OUT_DIR"), "/cycles_manifest_files.rs")); // ---- KernelParamsV2 byte offsets (verified against pack-input-v2.mjs and abi.wgsl) ---- const KERNEL_FLAG_LIGHT_TREE_PROBE: u32 = 128; const KERNEL_FLAG_TEXTURE_MIPS: u32 = 2048; +const KERNEL_FLAG_CAUSTIC_LIGHT_TRACING: u32 = 8192; +/// Byte offset of `caustic_photons` (u32[81]): light paths per pixel sample while the caustic flag is set. +const KP_CAUSTIC_PHOTONS_BYTE: usize = 324; const KP_KERNEL_FLAGS_BYTE: usize = 32; // u32[8] const KP_ENV_WIDTH_BYTE: usize = 192; // u32[48] const KP_ENV_HEIGHT_BYTE: usize = 196; // u32[49] @@ -262,6 +265,19 @@ pub(crate) fn check_update_params_keep_scene_shape(session_kernel_params: &[u8], Ok(()) } +/// How many light paths per pixel sample the frame traces for caustics: the `caustic_photons` word when the frame carries +/// `KERNEL_FLAG_CAUSTIC_LIGHT_TRACING`, else 0 (no light tracing, no splat buffer, the kernel without the feature). +pub(crate) fn caustic_photons_for_params(kernel_params: &[u8]) -> u32 { + if kernel_params.len() < KP_CAUSTIC_PHOTONS_BYTE + 4 { + return 0; + } + let kernel_flags = read_u32_le(kernel_params, KP_KERNEL_FLAGS_BYTE); + if (kernel_flags & KERNEL_FLAG_CAUSTIC_LIGHT_TRACING) == 0 { + return 0; + } + read_u32_le(kernel_params, KP_CAUSTIC_PHOTONS_BYTE) +} + /// The compute entry point to run for a kernel parameter block: `main`, or `light_tree_probe` when it carries /// `KERNEL_FLAG_LIGHT_TREE_PROBE` (tests). pub(crate) fn trace_entry_point_for_params(kernel_params: &[u8]) -> &'static str { @@ -564,6 +580,13 @@ pub(crate) fn specialize_kernel_source(source: &str, scene: &SceneSpecialization specialize!(F82, has_f82); specialize!(NORMAL_MAP, has_normal_map); specialize!(TEXTURE_MIPS, (kernel_flags & KERNEL_FLAG_TEXTURE_MIPS) != 0); + // A frame setting: without the flag (and a photon count) the light tracing code is dead and the kernel renders what it did before. + if caustic_photons_for_params(kp) == 0 { + result = result.replace( + "const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = true;", + "const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = false;", + ); + } // Unlike the other sentinels this one defaults to `false`, so a kernel compiled without specialization reads no extension records. if has_diffuse_transmission { result = result.replace("const SCENE_HAS_DIFFUSE_TRANSMISSION: bool = false;", "const SCENE_HAS_DIFFUSE_TRANSMISSION: bool = true;"); @@ -906,6 +929,44 @@ const SCENE_HAS_DIFFUSE_TRANSMISSION: bool = false;"; assert_eq!(specialize_kernel_source(MIP_TEST_SOURCE, &scene, &with_mips), MIP_TEST_SOURCE); } + #[test] + fn caustic_light_tracing_compiles_in_only_for_a_frame_that_sets_the_flag_and_a_photon_count() { + const LIGHT_TRACING_TEST_SOURCE: &str = "const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = true;"; + const OFF_SOURCE: &str = "const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = false;"; + let mat = make_mat(0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, -1, -1); + let plain = make_kp(0, 0.0, 0, 0, 0.0); + let mut traced = make_kp(KERNEL_FLAG_CAUSTIC_LIGHT_TRACING, 0.0, 0, 0, 0.0); + traced[KP_CAUSTIC_PHOTONS_BYTE..KP_CAUSTIC_PHOTONS_BYTE + 4].copy_from_slice(&4u32.to_le_bytes()); + let flag_without_photons = make_kp(KERNEL_FLAG_CAUSTIC_LIGHT_TRACING, 0.0, 0, 0, 0.0); + let input = dummy_input(&plain, &mat, &make_default_light(), &make_default_inst()); + let scene = scan_scene_specialization(&input); + assert_eq!(specialize_kernel_source(LIGHT_TRACING_TEST_SOURCE, &scene, &plain), OFF_SOURCE); + assert_eq!(specialize_kernel_source(LIGHT_TRACING_TEST_SOURCE, &scene, &flag_without_photons), OFF_SOURCE); + assert_eq!(specialize_kernel_source(LIGHT_TRACING_TEST_SOURCE, &scene, &traced), LIGHT_TRACING_TEST_SOURCE); + assert_eq!(caustic_photons_for_params(&plain), 0); + assert_eq!(caustic_photons_for_params(&traced), 4); + } + + /// The kernel and the host must agree on the fixed point scale of the splat buffer, and the camera paths must drop the light + /// the photon paths carry only behind the frame flag (so the kernel without it is the one without the feature). + #[test] + fn the_caustic_light_tracing_code_keeps_its_scale_and_hides_behind_the_frame_flag() { + let kernel = assemble_cycles_kernel().expect("the kernel assembles"); + let scale = crate::host::film::CAUSTIC_FIXED_POINT_SCALE; + assert!(kernel.contains(&format!("const LT_FIXED_POINT_SCALE: f32 = {scale:.1};"))); + assert!(kernel.contains("const KERNEL_FLAG_CAUSTIC_LIGHT_TRACING: u32 = 8192u;")); + assert!(kernel.contains("@group(0) @binding(15) var caustic_splat: array>;")); + assert!(kernel.contains("if (FRAME_HAS_CAUSTIC_LIGHT_TRACING && kernel_params.caustic_photons > 0u) {")); + assert!(kernel.contains("if (FRAME_HAS_CAUSTIC_LIGHT_TRACING && lt_left_surface_vertex && lt_caster_chain > 0u) {")); + assert!(kernel.contains("lt_caster_chain += 1u;")); + let photon_start = kernel.find("fn int_trace_caustic_photon(").expect("the photon trace exists"); + let photon_end = photon_start + kernel[photon_start..].find("\n}\n").expect("the photon trace ends"); + let photon_body = &kernel[photon_start..photon_end]; + assert!(photon_body.contains("caster_crossings > 0u && int_closure_has_evaluable_lobe(&closure)"), "only caustics: a path that crossed no caster is direct light"); + assert!(photon_body.contains("if (!is_thin_walled) {") && photon_body.contains("caster_crossings += 1u;"), "a thin pane is lit by the camera paths' shadow rays, so it is no caster"); + assert!(photon_body.contains("int_trace_shadow("), "the join to the camera needs a visibility ray"); + } + #[test] fn the_emission_map_read_of_a_light_sample_compiles_in_only_for_a_scene_with_an_emission_map() { const EMISSION_MAP_TEST_SOURCE: &str = "const SCENE_HAS_EMISSION_MAP: bool = true;"; diff --git a/src/host/cycles_session.rs b/src/host/cycles_session.rs index f477c7a..efdbe3a 100644 --- a/src/host/cycles_session.rs +++ b/src/host/cycles_session.rs @@ -12,7 +12,7 @@ use napi::Result; use crate::cycles::{ assemble_cycles_kernel_entry, check_counts_match_kernel_params, check_frame_params_keep_scene_counts, check_update_params_keep_scene_shape, scan_scene_specialization, specialize_kernel_source, - trace_entry_point_for_params, SceneSpecialization, + caustic_photons_for_params, trace_entry_point_for_params, SceneSpecialization, }; use crate::host::accel::{ build_cycles_acceleration_structure, rebuild_cycles_tlas, tlas_instance_limit, CyclesAcceleration, INSTANCE_RECORD_BYTES, @@ -21,7 +21,9 @@ use crate::host::cycles_update::{choose_film_use, plan_instance_update, FilmRequ use crate::host::capacity::TEXEL_BYTES; use crate::host::device::{check_buffer_sizes_fit, failure, run_in_error_scopes, storage_buffer}; use crate::host::post::PostPipelines; -use crate::host::film::{image_accumulation_bytes, image_workgroups, read_accumulation}; +use crate::host::film::{ + caustic_splat_bytes, image_accumulation_bytes, image_workgroups, read_film_sums, CausticSplat, +}; use crate::host::session::{ create_cycles_bind_group, get_cycles_device_context, get_or_compile_cycles_pipeline_on, pipeline_key, trace_cycles_samples, validate_cycles_input, validate_kernel_params_v2, validate_light_tree_flag_has_tables, @@ -123,9 +125,29 @@ struct SessionFilm { width: u32, height: u32, accumulation: wgpu::Buffer, + /// Caustic light tracing sums here (four u32 per pixel). One pixel's worth when no frame has asked for it. + splat: wgpu::Buffer, + splat_is_image_sized: bool, + /// What the frame that filled the film traced: photons per pixel sample (0 for none) and the sample count they were divided by. + caustic_photons: u32, + caustic_planned_samples: u32, bind_group: wgpu::BindGroup, } +impl SessionFilm { + fn caustic_splat(&self) -> Option> { + if self.caustic_photons == 0 { + return None; + } + Some(CausticSplat { + buffer: &self.splat, + pixel_count: self.width as u64 * self.height as u64, + photons_per_sample: self.caustic_photons, + planned_samples: self.caustic_planned_samples, + }) + } +} + /// Everything on the card. Dropping it frees the card, which is what `close` does. struct SessionGpuState { scene_tables: Vec, @@ -478,7 +500,8 @@ impl CyclesSession { let compile_seconds = compile_started.elapsed().as_secs_f64(); let upload_started = Instant::now(); - let film_reused = ensure_film(context, gpu, request.width, request.height)?; + let caustic_photons = caustic_photons_for_params(&frame_kernel_params); + let film_reused = ensure_film(context, gpu, request.width, request.height, caustic_photons > 0)?; let render_parameters = patch_render_parameters(&frame_kernel_params, request.width, request.height, film_use.total_samples); context.queue.write_buffer(&gpu.parameters, 0, &render_parameters); @@ -490,6 +513,10 @@ impl CyclesSession { // From here the film changes: a call that fails leaves it unusable, so nothing may claim it holds an image. self.progressive = None; self.film_holds_samples = false; + if let Some(held_film) = gpu.film.as_mut() { + held_film.caustic_photons = caustic_photons; + held_film.caustic_planned_samples = film_use.total_samples; + } let film = gpu.film()?; let target = CyclesDispatchTarget { @@ -499,6 +526,7 @@ impl CyclesSession { bind_group: &film.bind_group, parameters: &gpu.parameters, accumulation: &film.accumulation, + caustics: film.caustic_splat(), device_lost: &context.device_lost, device_error: &context.device_error, uncaptured_error: &context.uncaptured_error, @@ -567,11 +595,12 @@ impl CyclesSession { )) } }; - let sums = read_accumulation( + let sums = read_film_sums( &self.context.device, &self.context.queue, &film.accumulation, image_accumulation_bytes(film.width, film.height), + film.caustic_splat().as_ref(), )?; Ok(CyclesFilmRead { sums, @@ -717,12 +746,23 @@ fn ensure_pipeline( } /// Makes `gpu.film` a film of this size with a bind group over it; true when the previous frame's film was kept. -fn ensure_film(context: &CyclesDeviceContext, gpu: &mut SessionGpuState, width: u32, height: u32) -> Result { - if gpu.film.as_ref().is_some_and(|film| film.width == width && film.height == height) { +fn ensure_film( + context: &CyclesDeviceContext, + gpu: &mut SessionGpuState, + width: u32, + height: u32, + needs_caustic_splat: bool, +) -> Result { + let kept = gpu.film.as_ref().is_some_and(|film| { + film.width == width && film.height == height && (film.splat_is_image_sized || !needs_caustic_splat) + }); + if kept { return Ok(true); } - let (accumulation, bind_group) = run_in_error_scopes(&context.device, "allocating the film", || { + let (accumulation, splat, bind_group) = run_in_error_scopes(&context.device, "allocating the film", || { let accumulation = create_cycles_film(&context.device, image_accumulation_bytes(width, height)); + let splat_bytes = if needs_caustic_splat { caustic_splat_bytes(width, height) } else { 16 }; + let splat = create_cycles_film(&context.device, splat_bytes); let bind_group = with_cycles_cache_on(Some(&context.device_lost), |device, _queue, layout, lut| { create_cycles_bind_group( device, @@ -732,11 +772,21 @@ fn ensure_film(context: &CyclesDeviceContext, gpu: &mut SessionGpuState, width: &gpu.scene_tables, &accumulation, lut, + &splat, ) })?; - Ok::<_, napi::Error>((accumulation, bind_group)) + Ok::<_, napi::Error>((accumulation, splat, bind_group)) })??; - gpu.film = Some(SessionFilm { width, height, accumulation, bind_group }); + gpu.film = Some(SessionFilm { + width, + height, + accumulation, + splat, + splat_is_image_sized: needs_caustic_splat, + caustic_photons: 0, + caustic_planned_samples: 0, + bind_group, + }); Ok(false) } @@ -754,6 +804,7 @@ fn rebind_film(context: &CyclesDeviceContext, gpu: &mut SessionGpuState) -> Resu &gpu.scene_tables, &film.accumulation, lut, + &film.splat, ) })?; if let Some(film) = gpu.film.as_mut() { @@ -767,6 +818,7 @@ fn clear_film(context: &CyclesDeviceContext, film: &SessionFilm) { label: Some("cycles film clear"), }); encoder.clear_buffer(&film.accumulation, 0, None); + encoder.clear_buffer(&film.splat, 0, None); context.queue.submit(Some(encoder.finish())); } diff --git a/src/host/film.rs b/src/host/film.rs index 4bb3de6..086274b 100644 --- a/src/host/film.rs +++ b/src/host/film.rs @@ -44,12 +44,83 @@ pub(crate) fn texel_workgroups(count: u32) -> (u32, u32) { (groups_x, groups_y) } +/// Must match `LT_FIXED_POINT_SCALE` in integrator.wgsl: the splat buffer holds the photon contributions times this. +pub(crate) const CAUSTIC_FIXED_POINT_SCALE: f32 = 4096.0; + +/// The caustic splat buffer of a film and what the host needs to turn it into radiance. +pub(crate) struct CausticSplat<'a> { + pub(crate) buffer: &'a wgpu::Buffer, + pub(crate) pixel_count: u64, + pub(crate) photons_per_sample: u32, + /// The `total_samples` the kernel divided every photon by. + pub(crate) planned_samples: u32, +} + +/// Four u32 per pixel (red, green, blue and one unused word). +pub(crate) fn caustic_splat_bytes(width: u32, height: u32) -> u64 { + width as u64 * height as u64 * 16 +} + pub(crate) fn read_accumulation( device: &wgpu::Device, queue: &wgpu::Queue, accumulation: &wgpu::Buffer, accumulation_bytes: u64, ) -> Result> { + read_buffer_words::(device, queue, accumulation, accumulation_bytes) +} + +/// The film sums, with the caustic splats added to the radiance when the frame light traced. +pub(crate) fn read_film_sums( + device: &wgpu::Device, + queue: &wgpu::Queue, + accumulation: &wgpu::Buffer, + accumulation_bytes: u64, + caustics: Option<&CausticSplat<'_>>, +) -> Result> { + let mut sums = read_accumulation(device, queue, accumulation, accumulation_bytes)?; + if let Some(splat) = caustics { + let splat_words = read_buffer_words::(device, queue, splat.buffer, splat.pixel_count * 16)?; + add_caustic_splat(&mut sums, &splat_words, splat.pixel_count, splat.photons_per_sample, splat.planned_samples); + } + Ok(sums) +} + +/// Adds the splatted photons to the radiance sums. The kernel divided each photon by the photons it planned +/// (`pixel_count * planned_samples * photons_per_sample`); the samples actually taken can differ (a time limit, an unfinished +/// accumulation), so the sum is rescaled to the photons really traced. `resolve_image` divides the radiance of a pixel by its own +/// sample count, so the mean is multiplied by that count here. +pub(crate) fn add_caustic_splat( + sums: &mut [f32], + splat_words: &[u32], + pixel_count: u64, + photons_per_sample: u32, + planned_samples: u32, +) { + let pixel_total = pixel_count as usize; + let plane_length = pixel_total * 4; + let taken_total: f64 = (0..pixel_total).map(|pixel| sums[3 * plane_length + pixel * 4 + 3] as f64).sum(); + if taken_total <= 0.0 || photons_per_sample == 0 { + return; + } + let planned_photons = pixel_count as f64 * planned_samples.max(1) as f64; + let rescale = planned_photons / taken_total; + for pixel in 0..pixel_total { + let taken = sums[3 * plane_length + pixel * 4 + 3].max(1.0) as f64; + for channel in 0..3 { + let fixed_point = splat_words[pixel * 4 + channel] as f64; + let mean_radiance = fixed_point / CAUSTIC_FIXED_POINT_SCALE as f64 * rescale; + sums[pixel * 4 + channel] += (mean_radiance * taken) as f32; + } + } +} + +fn read_buffer_words( + device: &wgpu::Device, + queue: &wgpu::Queue, + accumulation: &wgpu::Buffer, + accumulation_bytes: u64, +) -> Result> { let readback = device.create_buffer(&wgpu::BufferDescriptor { label: Some("readback"), size: accumulation_bytes, @@ -67,7 +138,7 @@ pub(crate) fn read_accumulation( .slice(..) .get_mapped_range() .map_err(|error| failure(format!("Could not read the result back: {error}")))?; - let sums: Vec = bytemuck::cast_slice::(&mapped).to_vec(); + let sums: Vec = bytemuck::cast_slice::(&mapped).to_vec(); drop(mapped); // Release the one-frame staging buffer now instead of when a later poll collects the dropped handle. readback.unmap(); @@ -164,6 +235,31 @@ mod tests { ] } + #[test] + fn a_splat_adds_its_mean_radiance_to_the_pixel_it_landed_in() { + // Two pixels of 4 samples each; the kernel planned 4 samples. Pixel 0 holds 2.0 in red (fixed point), pixel 1 nothing. + let mut sums = vec![0.0f32; 2 * 16]; + sums[2 * 12 + 3] = 4.0; + sums[2 * 12 + 4 + 3] = 4.0; + let mut splat_words = vec![0u32; 8]; + splat_words[0] = (2.0 * CAUSTIC_FIXED_POINT_SCALE) as u32; + add_caustic_splat(&mut sums, &splat_words, 2, 1, 4); + let image = resolve_image(&sums, 2); + assert!((image.rgba[0] - 2.0).abs() < 1.0e-6, "{:?}", image.rgba); + assert_eq!(image.rgba[4], 0.0); + } + + #[test] + fn a_splat_is_rescaled_when_fewer_samples_were_taken_than_planned() { + let mut sums = vec![0.0f32; 16]; + sums[12 + 3] = 2.0; + let mut splat_words = vec![0u32; 4]; + splat_words[1] = (1.0 * CAUSTIC_FIXED_POINT_SCALE) as u32; + add_caustic_splat(&mut sums, &splat_words, 1, 1, 4); + let image = resolve_image(&sums, 1); + assert!((image.rgba[1] - 2.0).abs() < 1.0e-6, "{:?}", image.rgba); + } + #[test] fn a_film_that_never_wrote_coverage_resolves_to_alpha_one() { let image = resolve_image(&one_pixel_sums(0.0, 4.0), 1); diff --git a/src/host/session.rs b/src/host/session.rs index b1caebc..f438103 100644 --- a/src/host/session.rs +++ b/src/host/session.rs @@ -15,7 +15,7 @@ use crate::host::accel::{ }; use crate::host::dispatch::{next_tile, DispatchPlanner, TileBudgetPlanner, DISPATCH_BUDGET_SECONDS}; use crate::host::device::{check_buffer_sizes_fit, failure, layout_entry, open_device, run_in_error_scopes, storage_buffer, storage_entry}; -use crate::host::film::{image_accumulation_bytes, read_accumulation}; +use crate::host::film::{image_accumulation_bytes, read_accumulation, read_film_sums, CausticSplat}; use crate::host::textures::upload_scene_buffers; use crate::{NativeTraceInput, NativeTraceInputV2, ProbeCyclesLobeOptions}; @@ -388,6 +388,8 @@ pub(crate) struct CyclesDispatchTarget<'a> { pub(crate) bind_group: &'a wgpu::BindGroup, pub(crate) parameters: &'a wgpu::Buffer, pub(crate) accumulation: &'a wgpu::Buffer, + /// Set when the frame light traces caustics: the splat buffer is read back and added to the radiance. + pub(crate) caustics: Option>, pub(crate) device_lost: &'a AtomicBool, pub(crate) device_error: &'a Mutex, pub(crate) uncaptured_error: &'a Mutex>, @@ -456,6 +458,7 @@ fn create_cycles_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayo layout_entries.push(storage_entry(12, true)); // cycles_lut (read-only: static LUTs, then the two pixel filter tables) layout_entries.push(storage_entry(13, true)); // light_tree_emitters layout_entries.push(storage_entry(14, true)); // light_to_tree + layout_entries.push(storage_entry(15, false)); // caustic_splat (read_write, atomics) device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("cycles path tracer"), entries: &layout_entries, @@ -535,6 +538,7 @@ pub(crate) fn create_cycles_bind_group( buffers: &[wgpu::Buffer], accumulation: &wgpu::Buffer, lut_buffer: &wgpu::Buffer, + caustic_splat: &wgpu::Buffer, ) -> wgpu::BindGroup { let mut entries = vec![ wgpu::BindGroupEntry { binding: 0, resource: parameters.as_entire_binding() }, @@ -560,6 +564,10 @@ pub(crate) fn create_cycles_bind_group( resource: buffer.as_entire_binding(), }); } + entries.push(wgpu::BindGroupEntry { + binding: 15, + resource: caustic_splat.as_entire_binding(), + }); device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("cycles path tracer"), layout: bind_group_layout, @@ -1015,7 +1023,7 @@ pub(crate) fn trace_cycles_samples( } } - let sums = read_accumulation(device, queue, target.accumulation, accumulation_bytes); + let sums = read_film_sums(device, queue, target.accumulation, accumulation_bytes, target.caustics.as_ref()); fail_if_dispatch_failed(target, end_sample.saturating_sub(largest_batch), largest_batch, end_sample)?; Ok(CyclesTrace { sums: sums?, @@ -1106,7 +1114,7 @@ fn trace_cycles_tiles( // Leave the block at the origin for whoever reads it next; every whole-image dispatch writes it again anyway. queue.write_buffer(target.parameters, CYCLES_TILE_ORIGIN_BYTE_OFFSET, &[0u8; 8]); - let sums = read_accumulation(device, queue, target.accumulation, accumulation_bytes); + let sums = read_film_sums(device, queue, target.accumulation, accumulation_bytes, target.caustics.as_ref()); fail_if_dispatch_failed(target, end_sample.saturating_sub(largest_batch), largest_batch, end_sample)?; Ok(CyclesTrace { sums: sums?, @@ -1217,6 +1225,8 @@ pub(crate) fn run_cycles_lobe_probe(options: &ProbeCyclesLobeOptions, shader_sou usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false, }); + // The lobe probe never light traces; the kernel only needs something bound at 15. + let caustic_splat = storage_buffer(&device, "dummy caustic splat", &[0u8; 16]); let bind_group = with_cycles_cache(|_device, _queue, layout, lut| { create_cycles_bind_group( &device, @@ -1226,6 +1236,7 @@ pub(crate) fn run_cycles_lobe_probe(options: &ProbeCyclesLobeOptions, shader_sou &buffers, &accumulation, lut, + &caustic_splat, ) })?; diff --git a/tests/caustic-light-tracing.test.mjs b/tests/caustic-light-tracing.test.mjs new file mode 100644 index 0000000..3e6faab --- /dev/null +++ b/tests/caustic-light-tracing.test.mjs @@ -0,0 +1,116 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { test } from 'node:test'; + +import { findNativeAddon, traceNative } from '../index.mjs'; +import { + KERNEL_FLAG_CAUSTIC_LIGHT_TRACING, + KERNEL_PARAMS_V2_CAUSTIC_PHOTONS_BYTE_OFFSET, + buildNativeInputV2, +} from '../pack-input-v2.mjs'; +import { dispersionScene, packDispersionScene } from '../tools/dispersion-scenes.mjs'; +import { pinholeCamera } from '../tools/make-fixture-pack.mjs'; + +const KERNEL_FLAGS_BYTE_OFFSET = 32; +const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; + +function packLitFloor(options) { + const floor = { + positions: [-2, 0, -2, 2, 0, -2, 2, 0, 2, -2, 0, 2], + normals: [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0], + indices: [0, 2, 1, 0, 3, 2], + }; + const lamp = { + positions: [-0.2, 1, -0.2, 0.2, 1, -0.2, 0.2, 1, 0.2, -0.2, 1, 0.2], + normals: [0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0], + indices: [0, 1, 2, 0, 2, 3], + }; + const scene = { + materials: [{ color: [0.5, 0.5, 0.5], roughness: 1 }, { color: [1, 1, 1], roughness: 1, emission: 20 }], + instances: [ + { geometry: floor, matrix: IDENTITY, materialIndex: 0 }, + { geometry: lamp, matrix: IDENTITY, materialIndex: 1 }, + ], + lights: {}, + }; + const camera = pinholeCamera({ eye: [0, 1.5, 3], target: [0, 0, 0], fieldOfViewDegrees: 40, width: 32, height: 32 }); + return buildNativeInputV2(scene, camera, { width: 32, height: 32, samples: 4, seed: 5, backgroundColor: [0, 0, 0], ...options }); +} + +function wordsOf(input) { + const params = input.kernelParams; + const view = new DataView(params.buffer, params.byteOffset, params.byteLength); + return { + flags: view.getUint32(KERNEL_FLAGS_BYTE_OFFSET, true), + photons: view.getUint32(KERNEL_PARAMS_V2_CAUSTIC_PHOTONS_BYTE_OFFSET, true), + }; +} + +test('caustics off, "off" and no option pack the same kernel parameters, with the flag and the photon word clear', () => { + const plain = packLitFloor({}); + const off = packLitFloor({ caustics: 'off' }); + assert.deepEqual(Buffer.from(off.kernelParams), Buffer.from(plain.kernelParams)); + assert.deepEqual(wordsOf(plain), { flags: wordsOf(plain).flags & ~KERNEL_FLAG_CAUSTIC_LIGHT_TRACING, photons: 0 }); +}); + +test('caustics "light-tracing" sets the flag and the photon word (1 by default) and changes no other byte', () => { + const plain = packLitFloor({}); + const traced = packLitFloor({ caustics: 'light-tracing' }); + assert.deepEqual(wordsOf(traced), { flags: wordsOf(plain).flags | KERNEL_FLAG_CAUSTIC_LIGHT_TRACING, photons: 1 }); + const differing = []; + for (let byte = 0; byte < plain.kernelParams.byteLength; byte++) { + if (plain.kernelParams[byte] !== traced.kernelParams[byte]) differing.push(byte); + } + assert.ok(differing.every(byte => (byte >= 32 && byte < 36) || (byte >= 324 && byte < 328)), `bytes ${differing}`); + assert.equal(wordsOf(packLitFloor({ caustics: 'light-tracing', causticPhotons: 7 })).photons, 7); +}); + +test('a caustics option the kernel cannot use is refused with what to pass', () => { + assert.throws(() => packLitFloor({ caustics: 'photon-map' }), /`caustics` must be "off".*"light-tracing".*got "photon-map"/); + assert.throws(() => packLitFloor({ caustics: 'light-tracing', causticPhotons: 0 }), /`causticPhotons` must be a whole number from 1 to 64.*got 0/); + assert.throws(() => packLitFloor({ caustics: 'light-tracing', causticPhotons: 65 }), /from 1 to 64.*got 65/); + assert.throws(() => packLitFloor({ caustics: 'light-tracing', causticPhotons: 1.5 }), /whole number/); + assert.throws(() => packLitFloor({ causticPhotons: 2 }), /only applies with `caustics: "light-tracing"`/); + assert.throws(() => packLitFloor({ caustics: 'light-tracing', noiseThreshold: 0.01 }), /a pixel cannot stop early/); + assert.throws(() => packLitFloor({ caustics: 'light-tracing', orthographic: true }), /does not support `orthographic: true`/); +}); + +test('the kernel text keeps the light tracing behind the frame flag and the splat at binding 15', () => { + const integrator = readFileSync(new URL('../src/cycles/integrator.wgsl', import.meta.url), 'utf8'); + const abi = readFileSync(new URL('../src/cycles/abi.wgsl', import.meta.url), 'utf8'); + assert.match(abi, /const KERNEL_FLAG_CAUSTIC_LIGHT_TRACING: u32 = 8192u;/); + assert.match(abi, /const FRAME_HAS_CAUSTIC_LIGHT_TRACING: bool = true;/); + assert.match(abi, /@group\(0\) @binding\(15\) var caustic_splat: array>;/); + assert.match(integrator, /if \(FRAME_HAS_CAUSTIC_LIGHT_TRACING && kernel_params\.caustic_photons > 0u\)/); + assert.match(integrator, /if \(FRAME_HAS_CAUSTIC_LIGHT_TRACING && lt_left_surface_vertex && lt_caster_chain > 0u\)/); +}); + +const addonMissing = (() => { + try { + return findNativeAddon() === null ? 'the native addon is not built (run `node build.mjs`)' : false; + } catch (environmentError) { + return environmentError.message; + } +})(); + +// No glass, so no photon crosses a caster: nothing is splatted and no camera path drops its emitter hit. The image must not move. +test('light tracing changes no pixel of a scene without a caster', { skip: addonMissing }, () => { + const options = { width: 32, height: 32, samples: 4, kernel: 'cycles' }; + const without = traceNative(packLitFloor({}), options); + const withLightTracing = traceNative(packLitFloor({ caustics: 'light-tracing', causticPhotons: 2 }), options); + assert.deepEqual(Buffer.from(withLightTracing.rgba.buffer), Buffer.from(without.rgba.buffer)); +}); + +// `filterGlossy` blurs a glass lobe a camera path meets after a diffuse bounce. With light tracing that blur would turn the smooth +// glass into a non-singular lobe: the camera path would then keep a blurred copy of the caustic the light paths already carry. +// So the blur skips smooth glass, and the setting must not move a light traced image of a scene with only smooth glass. +test('light tracing keeps smooth glass sharp under filterGlossy, and the blur does change the image without it', { skip: addonMissing }, () => { + const render = options => { + const description = dispersionScene('prism-floor', { dispersion: 0, width: 48, height: 27 }); + const input = packDispersionScene(description, { samples: 16, seed: 3, options }); + return Buffer.from(traceNative(input, { width: 48, height: 27, samples: 16, kernel: 'cycles' }).rgba.buffer); + }; + const traced = { caustics: 'light-tracing', causticPhotons: 2 }; + assert.deepEqual(render({ ...traced, filterGlossy: 0 }), render({ ...traced, filterGlossy: 1 })); + assert.notDeepEqual(render({ filterGlossy: 0 }), render({ filterGlossy: 1 })); +}); diff --git a/tools/caustic-diff.mjs b/tools/caustic-diff.mjs new file mode 100644 index 0000000..20f13d8 --- /dev/null +++ b/tools/caustic-diff.mjs @@ -0,0 +1,66 @@ +// Where did caustic light tracing add light? Subtracts the render without it from the render with it (linear), writes the difference +// as a tone-mapped picture and prints the added energy, the box around the pixels that gained a lot and their centroid. +// node tools/caustic-diff.mjs [] +import { readFileSync, writeFileSync } from 'node:fs'; + +import { encodePng } from '../output/png.mjs'; +import { toneMapToRgba8 } from '../output/tone-map.mjs'; +import { decodeNpy } from '../tests/support/images.mjs'; + +const [offPath, onPath, prefix, thresholdArgument = '0.1'] = process.argv.slice(2); +if (!offPath || !onPath || !prefix) { + throw new TypeError('Usage: node tools/caustic-diff.mjs []'); +} + +function readRender(path) { + const { shape, data } = decodeNpy(readFileSync(path)); + return { width: shape[1], height: shape[0], values: new Float32Array(data.buffer, data.byteOffset, data.byteLength / 4) }; +} + +const off = readRender(offPath); +const on = readRender(onPath); +const { width, height } = off; +const difference = new Float32Array(width * height * 4); +const luminanceGain = new Float32Array(width * height); +let totalEnergy = [0, 0, 0]; +let peak = 0; +for (let pixel = 0; pixel < width * height; pixel++) { + for (let channel = 0; channel < 3; channel++) { + const gain = on.values[pixel * 4 + channel] - off.values[pixel * 4 + channel]; + totalEnergy[channel] += gain; + difference[pixel * 4 + channel] = Math.max(gain, 0); + } + difference[pixel * 4 + 3] = 1; + luminanceGain[pixel] = 0.2126 * difference[pixel * 4] + 0.7152 * difference[pixel * 4 + 1] + 0.0722 * difference[pixel * 4 + 2]; + peak = Math.max(peak, luminanceGain[pixel]); +} +const threshold = peak * Number(thresholdArgument); +let [x0, y0, x1, y1] = [width, height, -1, -1]; +let weight = 0; +let [sumX, sumY] = [0, 0]; +let bigPixels = 0; +for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const gain = luminanceGain[y * width + x]; + if (gain < threshold) continue; + bigPixels += 1; + [x0, y0, x1, y1] = [Math.min(x0, x), Math.min(y0, y), Math.max(x1, x), Math.max(y1, y)]; + weight += gain; + sumX += gain * x; + sumY += gain * y; + } +} +// Scale the picture so its peak is mid grey, whatever the absolute level. +const exposure = peak > 0 ? 0.5 / peak : 1; +const scaled = difference.map((value, index) => (index % 4 === 3 ? 1 : value * exposure)); +writeFileSync(`${prefix}.png`, encodePng(toneMapToRgba8(scaled, width, height), width, height)); +const report = { + addedEnergyRgb: totalEnergy, + peakGainLuminance: peak, + bigPixels, + threshold, + box: bigPixels > 0 ? [x0, y0, x1, y1] : null, + centroid: weight > 0 ? [sumX / weight, sumY / weight] : null, +}; +writeFileSync(`${prefix}.json`, JSON.stringify(report, null, 1)); +console.log(JSON.stringify(report)); diff --git a/tools/caustic-noise.mjs b/tools/caustic-noise.mjs new file mode 100644 index 0000000..a909c03 --- /dev/null +++ b/tools/caustic-noise.mjs @@ -0,0 +1,34 @@ +// Measures how rough a pixel box of a linear .npy render is: the mean absolute difference of horizontally neighbouring pixels (in +// luminance) divided by the mean luminance of the box. A smooth caustic gives a small number, speckle a large one. +// node tools/caustic-noise.mjs [ ...] +import { readFileSync } from 'node:fs'; + +import { decodeNpy } from '../tests/support/images.mjs'; + +const [boxArgument, ...paths] = process.argv.slice(2); +if (!boxArgument || paths.length === 0) { + throw new TypeError('Usage: node tools/caustic-noise.mjs [ ...]'); +} +const [x0, y0, x1, y1] = boxArgument.split(',').map(Number); + +for (const path of paths) { + const { shape, data } = decodeNpy(readFileSync(path)); + const width = shape[1]; + const values = new Float32Array(data.buffer, data.byteOffset, data.byteLength / 4); + const luminanceAt = (x, y) => { + const base = (y * width + x) * 4; + return 0.2126 * values[base] + 0.7152 * values[base + 1] + 0.0722 * values[base + 2]; + }; + let sum = 0; + let differenceSum = 0; + let pixels = 0; + for (let y = y0; y < y1; y++) { + for (let x = x0; x < x1 - 1; x++) { + sum += luminanceAt(x, y); + differenceSum += Math.abs(luminanceAt(x, y) - luminanceAt(x + 1, y)); + pixels += 1; + } + } + const mean = sum / pixels; + console.log(`${path}: mean ${mean.toFixed(5)} roughness ${(differenceSum / pixels / mean).toFixed(3)}`); +} diff --git a/tools/caustic-reference.sh b/tools/caustic-reference.sh new file mode 100644 index 0000000..24be858 --- /dev/null +++ b/tools/caustic-reference.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# Compares the caustic of the prism-floor scene from two estimators of the same light (codepawl/crucible#1715): +# reference camera paths only, a sun of radius 0.3 (large enough that a path from the floor through the prism finds it) +# traced caustic light tracing on the same scene +# for dispersion 0 and 1, and prints each box mean and the ratio traced / reference. Run from the repository root. +# bash tools/caustic-reference.sh [reference-samples] [traced-samples] [photons] +# Set RUN_PREFIX to a command that wraps each render (a GPU queue, for example). +set -euo pipefail +output_directory="${1:?output directory}" +reference_samples="${2:-512}" +traced_samples="${3:-128}" +photons="${4:-4}" +box="${BOX:-60,55,110,85}" +# SCENE_FLAGS and SIZE select another scene or camera, and BOX the pixel box on its caustic, for example +# SCENE_FLAGS="--storyboard --prism-tilt=-30 --prism-lift=0.5" SIZE="270 480" BOX=150,270,210,320 +read -r size_width size_height <<<"${SIZE:-240 135}" +common=(--width "$size_width" --height "$size_height" --sun-radius 0.3 --box "$box" ${SCENE_FLAGS:-}) +mkdir -p "$output_directory" +for dispersion in 0 1; do + ${RUN_PREFIX:-} node tools/caustic-render.mjs "$output_directory/reference-d$dispersion" \ + "${common[@]}" --dispersion "$dispersion" --samples "$reference_samples" + ${RUN_PREFIX:-} node tools/caustic-render.mjs "$output_directory/traced-d$dispersion" \ + "${common[@]}" --dispersion "$dispersion" --samples "$traced_samples" --light-tracing --photons "$photons" +done +node -e ' +const fs = require("node:fs"); +const directory = process.argv[1]; +for (const dispersion of [0, 1]) { + const reference = JSON.parse(fs.readFileSync(`${directory}/reference-d${dispersion}.json`, "utf8")).boxMean; + const traced = JSON.parse(fs.readFileSync(`${directory}/traced-d${dispersion}.json`, "utf8")).boxMean; + const ratios = traced.map((value, channel) => (value / reference[channel]).toFixed(3)); + console.log(`dispersion ${dispersion}: reference ${reference.map(v => v.toFixed(4))} traced ${traced.map(v => v.toFixed(4))} ratio ${ratios}`); +} +' "$output_directory" diff --git a/tools/caustic-render.mjs b/tools/caustic-render.mjs new file mode 100644 index 0000000..e132ce7 --- /dev/null +++ b/tools/caustic-render.mjs @@ -0,0 +1,114 @@ +// Renders the prism-floor caustic scene (codepawl/crucible#1715) with or without caustic light tracing and measures a pixel box. +// node tools/caustic-render.mjs [--light-tracing] [--photons N] [--samples N] [--dispersion D] [--seed S] +// [--width W] [--height H] [--sun-radius R] [--box x0,y0,x1,y1] [--storyboard] +// `--storyboard` is the dispersion short's scene: the hero camera, a warm sun and the blue fill panel. +// Writes .npy (linear RGBA float32), .png (tone mapped) and .json (wall seconds and the box means), and +// prints the json. `--sun-radius` makes the sun bigger and dimmer in step (the same power), so a camera-path-only render of it +// converges and can serve as the reference for the light traced one. +import { writeFileSync } from 'node:fs'; +import { parseArgs } from 'node:util'; + +import { traceNative } from '../index.mjs'; +import { encodeNpy } from '../output/npy.mjs'; +import { encodePng } from '../output/png.mjs'; +import { toneMapToRgba8 } from '../output/tone-map.mjs'; +import { dispersionScene, packDispersionScene, panelGeometry, placement } from './dispersion-scenes.mjs'; + +const SUN_RADIUS = 0.09; +const SUN_EMISSION = 900; + +const { values, positionals } = parseArgs({ + allowPositionals: true, + options: { + 'light-tracing': { type: 'boolean', default: false }, + photons: { type: 'string', default: '1' }, + samples: { type: 'string', default: '64' }, + dispersion: { type: 'string', default: '1' }, + seed: { type: 'string', default: '1' }, + width: { type: 'string', default: '540' }, + height: { type: 'string', default: '960' }, + 'sun-radius': { type: 'string', default: String(SUN_RADIUS) }, + box: { type: 'string' }, + storyboard: { type: 'boolean', default: false }, + 'prism-tilt': { type: 'string', default: '0' }, + 'prism-lift': { type: 'string', default: '0' }, + 'no-fill': { type: 'boolean', default: false }, + 'filter-glossy': { type: 'string' }, + }, +}); +const [prefix] = positionals; +if (!prefix) { + throw new TypeError('Usage: node tools/caustic-render.mjs [--light-tracing] [--photons N] [--samples N] [--dispersion D] [--seed S] [--width W] [--height H] [--sun-radius R] [--box x0,y0,x1,y1]'); +} + +const width = Number(values.width); +const height = Number(values.height); +const samples = Number(values.samples); +const sunRadius = Number(values['sun-radius']); +const description = dispersionScene('prism-floor', { dispersion: Number(values.dispersion), width, height }); +const sun = description.objects.find(object => object.name === 'sun'); +const scale = sunRadius / SUN_RADIUS; +sun.matrix = sun.matrix.map((value, index) => (index < 12 && index % 4 !== 3 ? value * scale : value)); +sun.material = { ...sun.material, emission: SUN_EMISSION / (scale * scale) }; + +if (values.storyboard) { + description.camera = { eye: [1.6, 1.1, 1.5], target: [0.35, 0.05, 0], fieldOfViewDegrees: 40 }; + sun.material = { ...sun.material, color: [1, 0.949, 0.863] }; + if (!values['no-fill']) description.objects.push({ + name: 'fill', + geometry: panelGeometry(1.2, 0.8), + matrix: placement({ turnY: -0.7, translation: [2.0, 2.4, 2.6] }), + material: { color: [0.624, 0.706, 0.847], roughness: 1, emission: 0.35 }, + }); +} + +// `--prism-lift` raises it further, on an unseen stand, for a longer throw to the floor. +// A prism of this many degrees of turn around its own axis (z), lifted until its lowest edge rests on the floor. The scene's sun +// sits high and low in the sky: a flat prism sends that light into total internal reflection and out through its base, so no +// rainbow reaches the floor. A tilt of -30 degrees puts the entry face at a useful angle. +const prismTilt = (Number(values['prism-tilt']) * Math.PI) / 180; +if (prismTilt !== 0) { + const prism = description.objects.find(object => object.name === 'prism'); + const [cosine, sine] = [Math.cos(prismTilt), Math.sin(prismTilt)]; + const positions = prism.geometry.positions; + let lowest = Infinity; + for (let index = 0; index < positions.length; index += 3) { + lowest = Math.min(lowest, positions[index] * sine + positions[index + 1] * cosine); + } + prism.matrix = [cosine, sine, 0, 0, -sine, cosine, 0, 0, 0, 0, 1, 0, 0, -lowest + Number(values['prism-lift']), 0, 1]; +} + +const extraOptions = values['filter-glossy'] === undefined ? {} : { filterGlossy: Number(values['filter-glossy']) }; +const options = values['light-tracing'] + ? { caustics: 'light-tracing', causticPhotons: Number(values.photons) } + : {}; +Object.assign(options, extraOptions); +const input = packDispersionScene(description, { samples, seed: Number(values.seed), options }); +const started = performance.now(); +const traced = traceNative(input, { width, height, samples, kernel: 'cycles' }); +const wallSeconds = (performance.now() - started) / 1000; + +function boxMean(box) { + const [x0, y0, x1, y1] = box; + const sum = [0, 0, 0]; + let count = 0; + for (let y = y0; y < y1; y++) { + for (let x = x0; x < x1; x++) { + for (let channel = 0; channel < 3; channel++) { + sum[channel] += traced.rgba[(y * width + x) * 4 + channel]; + } + count += 1; + } + } + return sum.map(value => value / count); +} + +const report = { samples, photons: values['light-tracing'] ? Number(values.photons) : 0, dispersion: Number(values.dispersion), sunRadius, width, height, wallSeconds }; +if (values.box) { + report.box = values.box.split(',').map(Number); + report.boxMean = boxMean(report.box); +} +writeFileSync(`${prefix}.npy`, encodeNpy(traced.rgba, width, height)); +writeFileSync(`${prefix}.png`, encodePng(toneMapToRgba8(traced.rgba, width, height), width, height)); +writeFileSync(`${prefix}.json`, JSON.stringify(report, null, 1)); +console.log(JSON.stringify(report)); diff --git a/tools/caustic-sweep.sh b/tools/caustic-sweep.sh new file mode 100644 index 0000000..29df25e --- /dev/null +++ b/tools/caustic-sweep.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Renders the dispersion short's prism scene with caustic light tracing at several sample and photon counts, one still each, and +# prints the wall seconds of every run (codepawl/crucible#1715). Run from the repository root. +# bash tools/caustic-sweep.sh [ ...] +# Set RUN_PREFIX to a command that wraps each render (a GPU queue, for example). +set -euo pipefail +output_directory="${1:?output directory}" +width="${2:?width}" +height="${3:?height}" +shift 3 +mkdir -p "$output_directory" +for configuration in "$@"; do + samples="${configuration%%:*}" + photons="${configuration##*:}" + ${RUN_PREFIX:-} node tools/caustic-render.mjs \ + "$output_directory/${width}x${height}-s${samples}-p${photons}" --storyboard --prism-tilt=-30 --prism-lift=0.5 --light-tracing \ + --width "$width" --height "$height" --samples "$samples" --photons "$photons" +done diff --git a/tools/dispersion-scenes.mjs b/tools/dispersion-scenes.mjs index 9b85900..d74939c 100644 --- a/tools/dispersion-scenes.mjs +++ b/tools/dispersion-scenes.mjs @@ -239,7 +239,7 @@ export function dispersionScene(name, { dispersion = 1, width, height } = {}) { } /** The FORMAT-v2 input Cook traces for a scene description. */ -export function packDispersionScene(description, { samples, seed = 1 }) { +export function packDispersionScene(description, { samples, seed = 1, options = {} }) { const materials = description.objects.map(object => object.material); const instances = description.objects.map((object, index) => ({ geometry: { @@ -258,5 +258,6 @@ export function packDispersionScene(description, { samples, seed = 1 }) { seed, bounces: description.bounces, backgroundColor: [0, 0, 0], + ...options, }); }