Skip to main content

pack_raytrace_frame

Function pack_raytrace_frame 

Source
pub fn pack_raytrace_frame(
    out: &mut Vec<f32>,
    frame: &RayTraceFrame<'_>,
) -> Result<(), String>
Expand description

Packs the per-frame camera, lighting, and settings block consumed by both shader backends.

The buffer layout is frozen by the shader contract:

0: eye.xyz, tan(fov / 2)
1: forward.xyz, aspect
2: right.xyz, width
3: up.xyz, height
4: ambient.xyz, light_count
5: lighting_eye.xyz, max_bounces
6: near_t, far_t, samples_per_axis, 0
7: 0, 0, 0, 0

followed by four vec4 per light:

0: position.xyz, kind
1: direction.xyz, intensity
2: color.xyz, falloff
3: spot_cos, 0, 0, 0

The camera basis is recomputed from position, target, and up, so a caller never supplies a basis that disagrees with target. A degenerate basis — a zero forward, a zero right, or an up parallel to forward — is rejected rather than packed as a zero direction that would collapse every ray onto one axis. The field of view must lie in (0, PI) and the depth range must satisfy 0 <= near < far; both are packed through pack_finite, so a f64 far plane beyond the f32 range is reported rather than written as infinity.

The light count is taken from the input and never capped: a frame with more lights than the packed-buffer limit allows is reported through the overflow check, not silently truncated.

The supplied out is cleared and then reused, so its capacity is kept and a caller packing every frame performs no allocation. Every value the packer reads arrives through one RayTraceFrame, the same parameter type the CPU renderer takes, so the two backends cannot be called with mismatched camera, lights, settings, or dimensions.

out is cleared only after the settings and the camera have been validated, so the rejections a caller is most likely to hit — an out-of-range sample grid, a zero or overflowing dimension, a degenerate camera — leave the previous frame’s contents intact. A light whose components cannot be represented is a different case: that check runs inside the write loop, after the clear, so such a frame leaves a partially written buffer behind rather than the old one.

§Arguments

  • &mut Vec<f32> - The destination packed buffer, cleared then reused.
  • &RayTraceFrame<'_> - The camera, lights, settings, and dimensions to pack.

§Returns

  • Result<(), String> - Ok once the block is packed, or the reason the frame cannot be represented.