Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions crates/strobe-scene/src/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! [`crate::directives`] (D199). [`Dialect::Es300`] (the ANGLE path) also
//! gets [`crate::fixups`]: global initializers that are not constant
//! expressions move into `main` (D192), an initialised uniform becomes
//! a global, and `pow(x, y)` is written as `exp2(log2(x) * (y))`, so a
//! a global, and `pow(x, y)` is written as `strobe_pow(x, y)`, so a
//! negative base is NaN as on the native drivers rather than ANGLE's
//! `|x|^y` (D301).

Expand All @@ -69,6 +69,18 @@ use crate::scene::SceneDef;
use std::borrow::Cow;
use std::fmt::Write as _;

/// `pow` with the native drivers' answer for a negative base, NaN, which
/// the ES rewrite calls in place of `pow` (D301, D700). Direct3D's compiler
/// under ANGLE gives `|x|^y` for `pow`, and folds `exp2(log2(x) * y)` back
/// into it, so the NaN is made explicitly. Measured on an RX 580: ANGLE then
/// matches native for exponents of 1 and 0.5 (R24).
const STROBE_POW: &str = "\
float strobe_pow(float x, float y) { return x < 0.0 ? uintBitsToFloat(0x7fc00000u) : exp2(log2(x) * y); }
vec2 strobe_pow(vec2 x, vec2 y) { return mix(exp2(log2(x) * y), vec2(uintBitsToFloat(0x7fc00000u)), lessThan(x, vec2(0.0))); }
vec3 strobe_pow(vec3 x, vec3 y) { return mix(exp2(log2(x) * y), vec3(uintBitsToFloat(0x7fc00000u)), lessThan(x, vec3(0.0))); }
vec4 strobe_pow(vec4 x, vec4 y) { return mix(exp2(log2(x) * y), vec4(uintBitsToFloat(0x7fc00000u)), lessThan(x, vec4(0.0))); }
";

/// The GLSL dialect a context compiles.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Dialect {
Expand Down Expand Up @@ -257,6 +269,9 @@ pub fn fragment(request: &Request<'_>) -> String {
if !strobe_ssf_lib::LIBRARY.ends_with('\n') {
out.push('\n');
}
if es {
out.push_str(STROBE_POW);
}

// `#if` on a name that is not a macro reads as 0 on every driver, as on
// NVIDIA. Then the desktop-only idioms, made explicit for ES, with
Expand All @@ -275,8 +290,8 @@ pub fn fragment(request: &Request<'_>) -> String {
out.push_str(UNSUPPORTED_SELECT);
}
// `pow` of a negative base is undefined: NaN on the native drivers,
// `|x|^y` under ANGLE. Spell out the native sequence (D301).
let guarded = fixups::pow_as_exp2_log2(&promoted.code);
// `|x|^y` under ANGLE. `strobe_pow` (above) gives NaN (D301, D700).
let guarded = fixups::pow_as_strobe_pow(&promoted.code);
let (code, hoisted) = fixups::hoist_global_initializers(&guarded, Branches::for_es(&out));
(Cow::Owned(code.into_owned()), hoisted)
} else {
Expand Down Expand Up @@ -658,7 +673,7 @@ mod tests {
})
};
assert!(
source(Dialect::Es300).contains("g = exp2(log2(TIME - 9.0) * ( 0.5));"),
source(Dialect::Es300).contains("g = strobe_pow(TIME - 9.0, 0.5);"),
"ES: the hoisted initializer is spelled out"
);
for dialect in [Dialect::Glsl330, Dialect::Glsl410] {
Expand Down
41 changes: 22 additions & 19 deletions crates/strobe-scene/src/fixups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
//! ANGLE the same call is `|x|^y`, and it folds `pow(x, 1.0)` to `x`. Real
//! scenes depend on the answer: a reaction-diffusion scene stops growing
//! and a pattern scene goes black on ANGLE. On ES, `pow` is written out as
//! the native sequence, so every ES driver gets the native answer (D301).
//! `strobe_pow`, which is NaN for a negative base, so every ES driver gets
//! the native answer (D301, D700).
//! Desktop is left to its driver.

use crate::directives::Branches;
Expand Down Expand Up @@ -91,17 +92,19 @@ pub fn passindex_float_compares(source: &str) -> Cow<'_, str> {
Cow::Owned(rewritten)
}

/// Rewrites `pow(x, y)` as `exp2(log2(x) * (y))`, the sequence the native
/// drivers compile `pow` to, so a negative base gives NaN on ES as it does
/// there, instead of Direct3D's `|x|^y` (D301). Returns `source` untouched
/// (no allocation) when nothing is rewritten.
/// Rewrites `pow(x, y)` as `strobe_pow(x, y)`, which the ES preamble
/// defines to be NaN for a negative base, as the native drivers give, instead
/// of Direct3D's `|x|^y` (D301). Spelling out `exp2(log2(x) * (y))` was not
/// enough: Direct3D's compiler folds it back, and takes `log2` of `|x|`
/// (D700, R24). Returns `source` untouched (no allocation) when nothing is
/// rewritten.
///
/// Left alone, because no driver disagrees on them: a base that is a plain
/// unsigned literal, and an exponent that is an integer literal other than
/// 1 or -1 (`pow(x, 2.0)`), which compilers expand into multiplications.
/// Nested calls are each rewritten, comments are copied, the arguments keep
/// their order and no newline is added, so lines still map.
pub fn pow_as_exp2_log2(source: &str) -> Cow<'_, str> {
pub fn pow_as_strobe_pow(source: &str) -> Cow<'_, str> {
if !source.contains("pow") {
return Cow::Borrowed(source);
}
Expand All @@ -113,7 +116,7 @@ pub fn pow_as_exp2_log2(source: &str) -> Cow<'_, str> {
}
}

/// [`pow_as_exp2_log2`] on any span of code, recursing into arguments.
/// [`pow_as_strobe_pow`] on any span of code, recursing into arguments.
fn rewrite_pows(source: &str) -> String {
const POW: &str = "pow";
let bytes = source.as_bytes();
Expand Down Expand Up @@ -153,11 +156,11 @@ fn rewrite_pows(source: &str) -> String {
out.push_str(&rewrite_pows(exponent));
out.push(')');
} else {
out.push_str("exp2(log2(");
out.push_str("strobe_pow(");
out.push_str(&rewrite_pows(base));
out.push_str(") * (");
out.push(',');
out.push_str(&rewrite_pows(exponent));
out.push_str("))");
out.push(')');
}
i = close + 1;
copied = i;
Expand Down Expand Up @@ -948,29 +951,29 @@ mod pow_tests {
use super::*;

fn fix(source: &str) -> String {
pow_as_exp2_log2(source).into_owned()
pow_as_strobe_pow(source).into_owned()
}

#[test]
fn pow_becomes_exp2_of_log2_in_argument_order() {
fn pow_becomes_strobe_pow_in_argument_order() {
assert_eq!(
fix("float g = pow(n - 0.5, 1.0);"),
"float g = exp2(log2(n - 0.5) * ( 1.0));",
"float g = strobe_pow(n - 0.5, 1.0);",
"exponent 1, which some compilers fold to the base"
);
assert_eq!(
fix("v = pow (tex(a, b).rgb, vec3(0.25));"),
"v = exp2(log2(tex(a, b).rgb) * ( vec3(0.25)));",
"v = strobe_pow(tex(a, b).rgb, vec3(0.25));",
"a call with its own commas, and a space before the parenthesis"
);
assert_eq!(
fix("x = pow(pow(a, k) - 1.0, f(i, j));"),
"x = exp2(log2(exp2(log2(a) * ( k)) - 1.0) * ( f(i, j)));",
"x = strobe_pow(strobe_pow(a, k) - 1.0, f(i, j));",
"nested calls are each rewritten"
);
assert_eq!(
fix("x = pow(-2.0, y) + pow(a, -1.0);"),
"x = exp2(log2(-2.0) * ( y)) + exp2(log2(a) * ( -1.0));",
"x = strobe_pow(-2.0, y) + strobe_pow(a, -1.0);",
"a negative literal base, and exponent -1"
);
}
Expand All @@ -987,13 +990,13 @@ mod pow_tests {
"x = pow(a);",
] {
assert!(
matches!(pow_as_exp2_log2(source), Cow::Borrowed(_)),
matches!(pow_as_strobe_pow(source), Cow::Borrowed(_)),
"`{source}` should be untouched"
);
}
assert_eq!(
fix("x = pow(pow(a, 0.5), 2.0);"),
"x = pow(exp2(log2(a) * ( 0.5)), 2.0);",
"x = pow(strobe_pow(a, 0.5), 2.0);",
"an integer power keeps pow, and its base is still rewritten"
);
}
Expand All @@ -1004,7 +1007,7 @@ mod pow_tests {
let fixed = fix(source);
assert_eq!(
fixed,
"float a = exp2(log2(\n n - 0.5) * ( // base\n 0.5));\n#define P(x) exp2(log2(x) * ( 0.25))\n",
"float a = strobe_pow(\n n - 0.5, // base\n 0.5);\n#define P(x) strobe_pow(x, 0.25)\n",
"multi-line call with a comment, and a macro body"
);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion crates/strobe-scene/tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn tag_lists_never_panic_and_stay_bounded() -> Result<()> {
fn the_glsl_rewriters_are_total_and_bounded() -> Result<()> {
check(512, glsl(), |source| {
bounded(&source, &fixups::passindex_float_compares(&source))?;
bounded(&source, &fixups::pow_as_exp2_log2(&source))?;
bounded(&source, &fixups::pow_as_strobe_pow(&source))?;
for es in [false, true] {
bounded(&source, &promote::int_to_float(&source, "", es))?;
let promoted = promote::rewrite(&source, "", es);
Expand Down
3 changes: 2 additions & 1 deletion docs/DECISIONS_BY_AREA.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ crate" when it names none. The log itself, and how to add to it, is

## strobe-bench

25 decision(s).
26 decision(s).

- [D55](decisions/D55.md): macOS CI is two-tier until strobe-gfx has a CGL context
- [D56](decisions/D56.md): CI keeps the rendered frames
Expand All @@ -149,6 +149,7 @@ crate" when it names none. The log itself, and how to add to it, is
- [D555](decisions/D555.md): a hand-loaded scene compiles in the background where the driver can, and the output plays on meanwhile
- [D562](decisions/D562.md): Scenes from a photo are made by one written pipeline: a project skill, the strobe-forge checker and its LLM loop
- [D578](decisions/D578.md): the meta stage follows Synesthesia: stored values, order, methods and names, with old presets converted on load
- [D700](decisions/D700.md): on ES, `pow` becomes `strobe_pow`, which makes the NaN for a negative base explicitly, because Direct3D folds `exp2(log2(x) * y)` back

## strobe-capture

Expand Down
15 changes: 15 additions & 0 deletions docs/decisions/D700.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# D700: on ES, `pow` becomes `strobe_pow`, which makes the NaN for a negative base explicitly, because Direct3D folds `exp2(log2(x) * y)` back (2026-12-02, R24)

**Context.** D301 spelled `pow(x, y)` as `exp2(log2(x) * (y))` in the ES dialect, so a negative base would give NaN on ANGLE as on the native drivers. It was inferred, not measured (Q28). AMD.md Task A15 measured it on LIGHTNING's RX 580 (R24). With D301 in place, ANGLE-D3D11 still gave `x` for an exponent of 1 and `|x|^0.5` for 0.5, where native GL gives NaN for both. The ES dump held D301's rewrite, so Direct3D's compiler folds it back, and takes `log2` of `|x|`. The two scenes D301 was written for still broke on ANGLE: `sprinkles` and `PLUS` rendered almost black (mean 4.3 and 2.6, against native's 48.2 and 66.4, at 640x360).

**Decision.** The ES rewrite now emits `strobe_pow(x, y)`. The ES preamble defines it for `float` and `vec2` to `vec4`, returning a NaN built from its bits (`uintBitsToFloat(0x7fc00000u)`) where the base is negative, and `exp2(log2(x) * y)` elsewhere. The vector forms select per component with `mix(..., lessThan(x, 0))`. What D301 leaves alone stays alone: a plain unsigned literal base, and an integer exponent other than 1 or -1. The desktop dialect still uses `pow` unchanged.

Two spellings were tried in a probe scene first, on native and ANGLE:
- the bit-built NaN gave native's answer on both;
- `sqrt(x)` for a negative base gave NaN on native but not under ANGLE, so it was rejected.

**Result (RX 580, `strobe-bench --context window`, 640x360).**
- `sprinkles` at 600 frames: ANGLE now matches native exactly (mean 48.2, 13.7, 32.7 on both).
- `PLUS` at 60 frames: ANGLE draws native's grid instead of black (mean 26.4, against 66.4). The rest of the gap is in what the drivers do with the NaN afterwards: native's NaN regions end white, Direct3D's black. Matching that would mean imitating each driver's NaN handling through arbitrary later code, which this does not attempt.

**Not measured:** Intel GPUs, native or under ANGLE, and ANGLE's Vulkan backend.
Loading