From b55cdfb5a445af73f5c6146a9d9e51bb7b0fd8f5 Mon Sep 17 00:00:00 2001 From: FueledByRedBull Date: Tue, 10 Nov 2026 21:04:59 +0200 Subject: [PATCH] Stop using the reserved word packed as a shader name NVIDIA's compiler rejects packed, a GLSL reserved word, so sand_pack.comp failed to build there. AMD's compiler and glslang accept it. gpu-compile now fails any shader that uses a reserved word. --- README.md | 2 +- VALIDATION.md | 12 +++++++++++ shaders/sand_pack.comp | 29 +++++++++++++------------ shaders/water_pack.comp | 8 +++---- tests/GpuTests.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 0fb7b3f..401776e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Windows 11 x64: download `PowderGame--win11-x64.zip` from the [latest r ### Requirements -You need a GPU and driver with OpenGL 4.3 core. Every shader stays within the limits OpenGL 4.3 guarantees (8 image units, 8 SSBO bindings, 16 texture units per stage), so it does not need the extra image units that AMD drivers offer and NVIDIA and Intel drivers usually do not. `gpu-compile` checks every shader against those limits. Building needs CMake 3.24 or newer, a C++20 compiler and Python (for the GLAD generator). CMake fetches pinned copies of GLFW and GLAD when no system copies are found. +You need a GPU and driver with OpenGL 4.3 core. Every shader stays within the limits OpenGL 4.3 guarantees (8 image units, 8 SSBO bindings, 16 texture units per stage), so it does not need the extra image units that AMD drivers offer and NVIDIA and Intel drivers usually do not. `gpu-compile` checks every shader against those limits and rejects GLSL reserved words such as `packed`, which NVIDIA's compiler refuses as names. Building needs CMake 3.24 or newer, a C++20 compiler and Python (for the GLAD generator). CMake fetches pinned copies of GLFW and GLAD when no system copies are found. ### Build from source on Windows 11 diff --git a/VALIDATION.md b/VALIDATION.md index f54a3d4..b10c280 100644 --- a/VALIDATION.md +++ b/VALIDATION.md @@ -138,6 +138,18 @@ Replay fails if: - combined scene has missing water/sand or inactive gas, violates source-free mass conservation, or exceeds the configured GPU-time budget +## Reserved words on NVIDIA - 2026-11-10 (on `c06b1fc`, v0.7.2) + +v0.7.2 got past the image-unit limits on an NVIDIA GPU and then failed to compile `sand_pack.comp`: +NVIDIA's compiler rejects `packed`, a GLSL reserved word, as a variable name. AMD's compiler and +glslang 16.6.0 both accept it, so neither the AMD runs nor a glslang pass over every shader caught +it. `sand_pack.comp` and `water_pack.comp` now say `packedCount`, and `gpu-compile` fails any shader +file that uses a word GLSL reserves for future use (checked by adding a probe shader with +`uint packed`). A scan of all shaders found no other reserved word in use. + +Machine: Windows 11, AMD Radeon RX 7800 XT, driver 26.6.2. `ctest -C Release -j 1`: 103/103 pass; +`POWDER_REPLAY=all`: exit 0 with every signature identical to v0.7.2. Not yet run on NVIDIA. + ## OpenGL 4.3 minimum resources - 2026-11-10 (on `b09f29f`) v0.7.1 refused to start on GPUs that offer only the OpenGL 4.3 minimum of 8 image units (the usual diff --git a/shaders/sand_pack.comp b/shaders/sand_pack.comp index eb8b892..aa7ba92 100644 --- a/shaders/sand_pack.comp +++ b/shaders/sand_pack.comp @@ -183,15 +183,15 @@ bool heldUp(ivec2 c) { void decide(ivec2 c) { uint t = tally[cellIndex(c)]; uint total = t & 4095u; - uint packed = imageLoad(packedTex, c).r; - if (packed == 0u && total == 0u) return; // nothing to pack or wake + uint packedCount = imageLoad(packedTex, c).r; + if (packedCount == 0u && total == 0u) return; // nothing to pack or wake ivec2 below = c + ivec2(0, -1); bool calm = !sideOpen(c) && !disturbed(c); uint flags = 0u; - if (packed > 0u) { + if (packedCount > 0u) { // Grains still moving below hold the cell up until they leave (or move fast enough to disturb it). if (!heldUp(c) || !calm) flags = kWake; - else if (allReady(c) && packed + total <= uint(maxPacked)) flags = kPack; + else if (allReady(c) && packedCount + total <= uint(maxPacked)) flags = kPack; } else if (allReady(c) && total <= uint(maxPacked) && calm && (solidAt(below) || packedAt(below) || allReady(below) || archedOver(below))) { flags = kPack; @@ -209,21 +209,21 @@ void apply(ivec2 c) { sum = vec4(fsum[i], fsum[i + 1], fsum[i + 2], fsum[i + 3]) / 4096.0; for (int k = 0; k < 4; ++k) fsum[i + k] = 0; } - uint packed = imageLoad(packedTex, c).r; + uint packedCount = imageLoad(packedTex, c).r; vec4 meanF = imageLoad(packedFTex, c); if ((t & kPack) != 0u) { uint total = t & 4095u; - imageStore(packedTex, c, uvec4(packed + total)); - imageStore(packedFTex, c, (meanF * float(packed) + sum) / float(packed + total)); + imageStore(packedTex, c, uvec4(packedCount + total)); + imageStore(packedFTex, c, (meanF * float(packedCount) + sum) / float(packedCount + total)); } else if ((t & kWake) != 0u) { // All of the cell's grains or none: a full pool leaves it packed (no loss) and counts the overflow. uint slot = atomicAdd(spawnCursor, 0u); - while (slot + packed <= uint(maxParticles)) { - uint observed = atomicCompSwap(spawnCursor, slot, slot + packed); + while (slot + packedCount <= uint(maxParticles)) { + uint observed = atomicCompSwap(spawnCursor, slot, slot + packedCount); if (observed == slot) break; slot = observed; } - if (slot + packed > uint(maxParticles)) { + if (slot + packedCount > uint(maxParticles)) { atomicAdd(particleOverflowBlocked, 1u); return; } @@ -243,9 +243,10 @@ void apply(ivec2 c) { // The cell's grains were bonded (q about 0) or cracked (q large): the mean bond is the intact share, and the // cracked ones carry the rest of the mean hardening, so a mixed clump wakes with the split it packed with // instead of every grain at the mean q (whose bond is the exponential of the mean, far below the mean bond). - uint intact = min(packed, uint(clamp(meanF.w, 0.0, 1.0) * float(packed) + 0.5)); - float cracked = intact == packed ? 0.0 : clamp(hardened * float(packed) / float(packed - intact), 0.0, 0.9999); - for (uint i = 0u; i < packed; ++i) { + uint intact = min(packedCount, uint(clamp(meanF.w, 0.0, 1.0) * float(packedCount) + 0.5)); + float cracked = + intact == packedCount ? 0.0 : clamp(hardened * float(packedCount) / float(packedCount - intact), 0.0, 0.9999); + for (uint i = 0u; i < packedCount; ++i) { uvec3 key = uvec3(uvec2(c), 3u * i); uint index = slot + i; particles[index].state0 = vec4(vec2(c) + uniform2(key), velocity); @@ -257,7 +258,7 @@ void apply(ivec2 c) { } imageStore(packedTex, c, uvec4(0u)); imageStore(packedFTex, c, vec4(0.0)); - } else if (packed > 0u) { + } else if (packedCount > 0u) { // Sleeping sand soaks up water around it and dries like moving grains (sand_particle_step.comp's moisture). float fill = 0.0; for (int j = -1; j <= 1; ++j) { diff --git a/shaders/water_pack.comp b/shaders/water_pack.comp index 9a9c6d9..8cf71ed 100644 --- a/shaders/water_pack.comp +++ b/shaders/water_pack.comp @@ -116,18 +116,18 @@ void apply(ivec2 c) { return; } if ((value & kWake) == 0u) return; - uint packed = value & 127u; + uint packedCount = value & 127u; // All of the cell's particles or none: a full pool leaves it packed (no loss), counts the overflow and retries. One // atomicAdd per cell (a compare-swap loop serialised mass wakes); slots a failed reservation leaves below the cap // keep the inactive records compaction left there, and the host clamps the cursor to the pool. - uint slot = atomicAdd(spawnCursor, packed); - if (slot + packed > uint(maxParticles)) { + uint slot = atomicAdd(spawnCursor, packedCount); + if (slot + packedCount > uint(maxParticles)) { atomicAdd(particleOverflowBlocked, 1u); return; } vec2 velocity = imageLoad(wakeVelocityTex, c).xy; atomicMax(fastestSpeedBits, floatBitsToUint(length(velocity))); - for (uint i = 0u; i < packed; ++i) { + for (uint i = 0u; i < packedCount; ++i) { // One particle per quarter of the cell in turn, jittered within it: uniform positions would wake as the // seeding noise the density correction then has to remove. vec2 quarter = vec2(float(i & 1u), float((i >> 1) & 1u)); diff --git a/tests/GpuTests.cpp b/tests/GpuTests.cpp index 9a3aacf..6de2f60 100644 --- a/tests/GpuTests.cpp +++ b/tests/GpuTests.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include #include @@ -2832,6 +2834,43 @@ void RequireGl43Minimums(GLuint program) { CheckGl("query program resources"); } +// GLSL words reserved for future use. AMD's compiler and glslang accept some as names, but NVIDIA's rejects them +// ("packed", reserved since GLSL 1.10, stopped v0.7.2 from starting there). +void RequireNoReservedWords(const std::filesystem::path& path) { + static constexpr std::array kReserved{ + "common", "partition", "active", "asm", "class", "union", "enum", "typedef", "template", "this", + "resource", "goto", "inline", "noinline", "public", "static", "extern", "external", "interface", "long", + "short", "half", "fixed", "unsigned", "superp", "input", "output", "hvec2", "hvec3", "hvec4", "fvec2", + "fvec3", "fvec4", "sampler3DRect", "filter", "sizeof", "cast", "namespace", "using"}; + const std::string source = glutil::ReadTextFile(path); + std::string code; + for (std::size_t i = 0; i < source.size(); ++i) { + if (source.compare(i, 2, "//") == 0) { + i = source.find('\n', i); + if (i == std::string::npos) break; + } else if (source.compare(i, 2, "/*") == 0) { + i = source.find("*/", i + 2); + if (i == std::string::npos) break; + ++i; + continue; + } + code += source[i]; + } + const auto is_word = [](char c) { return std::isalnum(static_cast(c)) != 0 || c == '_'; }; + for (std::size_t i = 0; i < code.size();) { + if (!is_word(code[i])) { + ++i; + continue; + } + std::size_t end = i; + while (end < code.size() && is_word(code[end])) ++end; + const std::string_view word(code.data() + i, end - i); + Require(word != "packed" && std::find(kReserved.begin(), kReserved.end(), word) == kReserved.end(), + "uses the reserved word '" + std::string(word) + "'"); + i = end; + } +} + void CompileShaders() { std::vector paths; for (const auto& entry : std::filesystem::directory_iterator(kShaderRoot)) { @@ -2842,6 +2881,14 @@ void CompileShaders() { Require(!paths.empty(), "no compute shaders found in " + kShaderRoot.string()); std::sort(paths.begin(), paths.end()); int failures = 0; + for (const auto& entry : std::filesystem::directory_iterator(kShaderRoot)) { + try { + RequireNoReservedWords(entry.path()); + } catch (const std::exception& error) { + ++failures; + std::cerr << " FAIL " << entry.path().filename().string() << ": " << error.what() << '\n'; + } + } for (const auto& path : paths) { try { Resources resources;