Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A three.js shader's uniforms get 254 vec4 slots, not 62: lettra's default scramble() runs as its graph

shinyobjectzbe8bdd597e3d
diff --git a/sdk/r3f/src/input/kinds.ts b/sdk/r3f/src/input/kinds.ts
index ed93a185..0ba6fb03 100644
--- a/sdk/r3f/src/input/kinds.ts
+++ b/sdk/r3f/src/input/kinds.ts
@@ -58,13 +58,13 @@ export const CLASSES: Record<string, { status: InputStatus; raea: string; note?:
PointsMaterial: { status: "drawn", raea: "MeshBasicMaterial on the points' quads" },
LineBasicMaterial: { status: "drawn", raea: "the Gizmo's colours" },
LineDashedMaterial: { status: "drawn", raea: "the Gizmo's colours, the line cut into its dashes by its line distances" },
- ShaderMaterial: { status: "drawn", raea: "ThreeShaderMaterial: its GLSL translated by naga and run with three's built-ins", note: "at most 4 textures, 62 vec4s of uniforms and 4 attributes of its own; a render target's texture is white; onBeforeCompile isn't read" },
- RawShaderMaterial: { status: "drawn", raea: "ThreeShaderMaterial: its GLSL translated by naga and run with three's built-ins", note: "at most 4 textures, 62 vec4s of uniforms and 4 attributes of its own; a render target's texture is white; onBeforeCompile isn't read" },
+ ShaderMaterial: { status: "drawn", raea: "ThreeShaderMaterial: its GLSL translated by naga and run with three's built-ins", note: "at most 4 textures, 254 vec4s of uniforms and 4 attributes of its own; a render target's texture is white; onBeforeCompile isn't read" },
+ RawShaderMaterial: { status: "drawn", raea: "ThreeShaderMaterial: its GLSL translated by naga and run with three's built-ins", note: "at most 4 textures, 254 vec4s of uniforms and 4 attributes of its own; a render target's texture is white; onBeforeCompile isn't read" },
Material: { status: "none", raea: "the base class" },
// three/webgpu's node materials (TSL): one with no node of its own is the classic material it extends, drawn natively;
// one with a graph (colorNode, positionNode, ...) on a mesh is compiled to WGSL by three's own WGSLNodeBuilder and run
// by raea's engine (tsl.ts: ThreeShaderMaterial), lit by three's light nodes when it is a lit material
- ...Object.fromEntries(["NodeMaterial", "MeshBasicNodeMaterial", "MeshLambertNodeMaterial", "MeshPhongNodeMaterial", "MeshStandardNodeMaterial", "MeshPhysicalNodeMaterial", "MeshSSSNodeMaterial", "MeshToonNodeMaterial", "MeshMatcapNodeMaterial", "MeshNormalNodeMaterial", "VolumeNodeMaterial"].map((k) => [k, { status: "drawn" as const, raea: "its classic material, or its graph as WGSL (ThreeShaderMaterial)", note: "a graph needing more than 4 textures, 62 vec4s of uniforms (uniformArrays included) or 4 attributes of the mesh's own, a storage buffer or a render target is named and drawn flat" }])),
+ ...Object.fromEntries(["NodeMaterial", "MeshBasicNodeMaterial", "MeshLambertNodeMaterial", "MeshPhongNodeMaterial", "MeshStandardNodeMaterial", "MeshPhysicalNodeMaterial", "MeshSSSNodeMaterial", "MeshToonNodeMaterial", "MeshMatcapNodeMaterial", "MeshNormalNodeMaterial", "VolumeNodeMaterial"].map((k) => [k, { status: "drawn" as const, raea: "its classic material, or its graph as WGSL (ThreeShaderMaterial)", note: "a graph needing more than 4 textures, 254 vec4s of uniforms (uniformArrays included) or 4 attributes of the mesh's own, a storage buffer or a render target is named and drawn flat" }])),
...Object.fromEntries(["LineBasicNodeMaterial", "LineDashedNodeMaterial", "Line2NodeMaterial", "PointsNodeMaterial", "SpriteNodeMaterial", "ShadowNodeMaterial"].map((k) => [k, { status: "near" as const, raea: "its classic material (lines, points, a sprite, a shadow catcher)", note: "a graph of its own on lines, points or a sprite is drawn flat" }])),
// helpers: lines (and a plane's mesh), drawn as any lines are
...Object.fromEntries(["ArrowHelper", "AxesHelper", "Box3Helper", "BoxHelper", "CameraHelper", "DirectionalLightHelper", "GridHelper", "HemisphereLightHelper", "PlaneHelper", "PointLightHelper", "PolarGridHelper", "SkeletonHelper", "SpotLightHelper"].map((k) => [k, { status: "drawn" as const, raea: "its lines as Gizmo lines (an arrow's head and a plane's face as meshes)" }])),
diff --git a/sdk/r3f/src/input/tsl.ts b/sdk/r3f/src/input/tsl.ts
index 6a73a59e..00884840 100644
--- a/sdk/r3f/src/input/tsl.ts
+++ b/sdk/r3f/src/input/tsl.ts
@@ -5,7 +5,7 @@
// the vertex stage fn three_vertex(v: ThreeVertex) -> ThreeVaryings
// the fragment stage fn three_fragment(in: ThreeVaryings, front: bool) -> vec4<f32> (linear: `output: "linear"`)
// both declare struct ThreeVaryings { @builtin(position) clip: vec4<f32>, @location(0) ... }
-// three's uniform groups and textures become the prelude's u_NAME() accessors (64 vec4 slots) and tex_NAME/samp_NAME;
+// three's uniform groups and textures become the prelude's u_NAME() accessors (254 vec4 slots of the 256: three-shader module.rs) and tex_NAME/samp_NAME;
// the camera's projection and view and the model's world matrix become three_projection(), three_view() and
// three_model(instance), so Bevy's own camera and transform place it (Bevy's projection is reverse-Z). Every other
// uniform takes the value three's own node updates give it this frame (time from R3F's clock, a light's colour), so a
@@ -48,7 +48,7 @@ export interface ShaderSpec {
}
/** the engine's limits (systems/three-shader) */
-export const SLOTS = 64;
+export const SLOTS = 254;
export const TEXTURES = 4;
type AnyNode = { isNode?: boolean; name?: string; value?: unknown; uniformNode?: AnyNode; scope?: string };
diff --git a/systems/three-shader/src/lib.rs b/systems/three-shader/src/lib.rs
index c1742892..2248dbd6 100644
--- a/systems/three-shader/src/lib.rs
+++ b/systems/three-shader/src/lib.rs
@@ -2,7 +2,7 @@
//! naga translates (glsl.rs), and a node material's WGSL, which three's own TSL builder writes in the SDK. Either is
//! a vertex and a fragment source meeting module.rs's contract; this crate's Material runs them with three's built-ins
//! (the model, view and projection matrices, the camera, the mesh's attributes and its own), the material's uniforms
-//! in 62 vec4 slots (changed frame by frame by `three:uniforms` component rows, `patch`), up to four textures, and
+//! in 254 vec4 slots (changed frame by frame by `three:uniforms` component rows, `patch`), up to four textures, and
//! three's time.
//!
//! What three puts on screen is what shows: a ShaderMaterial's colour is the value its canvas gets (`display`), so the
diff --git a/systems/three-shader/src/module.rs b/systems/three-shader/src/module.rs
index adb9532b..7bca0adb 100644
--- a/systems/three-shader/src/module.rs
+++ b/systems/three-shader/src/module.rs
@@ -11,12 +11,13 @@
//! Z), `three_camera_position()`, `three_viewport()`, `three_time()`, a `u_NAME()` per uniform (`u_NAME(i)` for an
//! array), and `tex_NAME` / `samp_NAME` per texture.
-/// vec4 slots in the uniform buffer, and the two the material keeps for itself: 62 is three's time (time, delta,
-/// frame), 63 how the fragment's colour is read (lib.rs's `display`)
-pub const SLOTS: usize = 64;
-pub const USER_SLOTS: usize = 62;
-pub const TIME_SLOT: usize = 62;
-pub const DISPLAY_SLOT: usize = 63;
+/// vec4 slots in the uniform buffer (4 KB, under every device's 16 KB uniform binding; lettra's default scramble pool
+/// takes 78), and the two the material keeps for itself, its last: three's time (time, delta, frame), then how the
+/// fragment's colour is read (lib.rs's `display`)
+pub const SLOTS: usize = 256;
+pub const USER_SLOTS: usize = SLOTS - 2;
+pub const TIME_SLOT: usize = SLOTS - 2;
+pub const DISPLAY_SLOT: usize = SLOTS - 1;
/// textures a material may bind, and a mesh's own attributes (each up to four floats)
pub const TEXTURES: usize = 4;
pub const ATTRIBUTES: usize = 4;
@@ -238,7 +239,7 @@ pub fn vertex_entry(attributes: usize) -> String {
/// The fragment entry: the source's `three_fragment`, its colour read as `display` says (tone.wesl's three_untone).
pub fn fragment_entry() -> String {
let mut m = String::from(TONE);
- m = m.replace("GROUP", GROUP);
+ m = m.replace("GROUP", GROUP).replace("DISPLAY_SLOT", &DISPLAY_SLOT.to_string());
m + "\n@fragment\nfn fragment(in: ThreeVaryings, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> {\n return three_untone(three_fragment(in, front));\n}\n"
}
@@ -352,6 +353,8 @@ mod tests {
assert!(p.contains("fn u_m() -> mat3x3<f32> { return mat3x3<f32>(three_slots.s[1u + 0u].xyz, three_slots.s[1u + 1u].xyz, three_slots.s[1u + 2u].xyz); }"));
assert!(p.contains("fn u_pts(i: u32) -> vec2<f32> { let k = 4u + i * 1u; return three_slots.s[k].xy; }"));
assert!(p.contains("var tex_map: texture_2d<f32>;"));
- assert!(layout(&[("big".into(), Ty::Mat(4), Some(16))]).is_err());
+ // 63 mat4s fit in the 254 slots of its own, 64 don't
+ assert!(layout(&[("big".into(), Ty::Mat(4), Some(63))]).is_ok());
+ assert!(layout(&[("big".into(), Ty::Mat(4), Some(64))]).is_err());
}
}
diff --git a/systems/three-shader/src/tone.wesl b/systems/three-shader/src/tone.wesl
index d0a731c7..0da7f632 100644
--- a/systems/three-shader/src/tone.wesl
+++ b/systems/three-shader/src/tone.wesl
@@ -1,7 +1,7 @@
// How a three shader's colour reaches the screen as three puts it there (docs/r3f.feature R3F11). A ShaderMaterial
// writes the value the canvas shows (three applies no tone mapping or colour space to it unless its GLSL includes
// them), where Bevy tone-maps and encodes every pixel of the frame after the fact. So a "display" colour is decoded
-// from sRGB and taken back through the camera's tone mapping: the linear value Bevy's curve turns into it. Slot 63:
+// from sRGB and taken back through the camera's tone mapping: the linear value Bevy's curve turns into it. Slot DISPLAY_SLOT (the last):
// x is 0 for a linear (scene-referred) colour, left as it is, or 1 + the camera's tone mapping (lib.rs's `display`).
// The curves are Bevy 0.20's own (bevy_core_pipeline tonemapping.wesl), inverted by a damped fixed point, so one
// method covers the LUT-based ones too; the LUT is the camera's (TonemappingLuts), at bindings 9 and 10.
@@ -103,8 +103,8 @@ fn three_exposure() -> f32 {
fn three_untone(c: vec4<f32>) -> vec4<f32> {
// a camera without HDR tone-maps in its materials' own shaders (Bevy's pbr_functions), which this one isn't:
// nothing maps it after, so the canvas's value is only decoded to the sRGB target's linear
- let mode = u32(three_slots.s[63].x + 0.5);
- let display = three_slots.s[63].y > 0.5;
+ let mode = u32(three_slots.s[DISPLAY_SLOT].x + 0.5);
+ let display = three_slots.s[DISPLAY_SLOT].y > 0.5;
@if(TONEMAP_IN_SHADER) {
// and a linear (scene-light) colour is mapped here, as Bevy's own materials map theirs
if !display {