Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Windows 11 x64: download `PowderGame-<version>-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

Expand Down
12 changes: 12 additions & 0 deletions VALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 15 additions & 14 deletions shaders/sand_pack.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions shaders/water_pack.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
47 changes: 47 additions & 0 deletions tests/GpuTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <filesystem>
Expand All @@ -14,6 +15,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -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<std::string_view, 39> 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<unsigned char>(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<std::filesystem::path> paths;
for (const auto& entry : std::filesystem::directory_iterator(kShaderRoot)) {
Expand All @@ -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;
Expand Down
Loading