diff --git a/README.md b/README.md index 71dede4..0fb7b3f 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 and enough shader resources: at least 18 image units and compute image uniforms, 19 combined shader output resources and 8 SSBO binding points. The app checks these at startup. The scene shader also uses 16 fragment texture units, the OpenGL 4.3 minimum. 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. 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 f2f3334..f54a3d4 100644 --- a/VALIDATION.md +++ b/VALIDATION.md @@ -138,6 +138,34 @@ Replay fails if: - combined scene has missing water/sand or inactive gas, violates source-free mass conservation, or exceeds the configured GPU-time budget +## 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 +NVIDIA and Intel value): it required 18. Every shader now samples the fields it only reads +(`loadTexel`, `texelFetch` that returns zero outside the texture as `imageLoad` does) and keeps +image units for writes, numbered 0..7. The host's `BindImage` binds a read-only field to a texture +unit, a write-only one to an image unit and a read-write one to both, and every image barrier +also carries `GL_TEXTURE_FETCH_BARRIER_BIT`. The startup limit check is gone; `gpu-compile` now +fails any program over the 4.3 minimums (image uniforms on units 0..7, at most 8 images plus +storage blocks, storage bindings 0..7, at most 16 samplers). + +Machine: Windows 11, AMD Radeon RX 7800 XT, `4.3.0 Core Profile Context 26.6.2.260602` (48 image +units, so the 8-unit limit itself was checked by `gpu-compile`, not by an NVIDIA or Intel run). +Binaries: `powder_game.exe` `f76bc22e5305`, `powder_gpu_tests.exe` `4ad2c9f0ff02`, +`powder_app_lifecycle_tests.exe` `042ad5d050a6`. + +- `ctest -C Release -j 1`: 103/103 pass. `POWDER_REPLAY=all`: exit 0, all 12 scene runs pass. +- `benchmark-realtime`: PASS; idle GPU mean 1.70 ms, combined GPU mean/p95/max 18.52/25.21/30.19 ms. +- Replay signatures against `b09f29f` built from the same dependencies: water, sand, boundary + and leakage are bit-identical. Gas and combined differ (gas smoke 2803.48 to 3080.86) and both + pass. Bisected with every other shader restored: the difference comes from `smoke_correct` and + `gas_velocity_correct` alone, and no read there leaves its texture (an out-of-range sentinel + leaves the result unchanged) while a variant without the range check reproduces the old + signature exactly. The values read are the same; the fire scenes amplify the compiler's + different rounding. +- Replay frame avg ms, old/new: combined 12.34/12.79, gas 6.19/6.41, water 8.11/8.21 (single + runs each, other programs open). + ## Current status - v0.7.0 integration, 2026-10-23 (committed as `12c1343`; measured on the pre-commit tree on `61e2965`) This is the current result. The five pass sections that follow it are the work merged here, each measured in its own diff --git a/packaging/README.txt b/packaging/README.txt index 71ca36e..bc9c31c 100644 --- a/packaging/README.txt +++ b/packaging/README.txt @@ -28,9 +28,7 @@ light over the simulation. Notes: - powder_game.exe is self-contained (shaders built in, C/C++ runtime linked statically); run it from any folder. -- Requires OpenGL 4.3 core with at least 18 image units, 18 compute image - uniforms, 19 combined shader output resources, 8 SSBO bindings and 16 - fragment texture units. The game checks the first four at startup and names - a missing one in a dialog. +- Requires an OpenGL 4.3 core GPU and driver; the + shaders stay within the limits OpenGL 4.3 guarantees. - Source, documentation and measured limitations (VALIDATION.md): https://github.com/FueledByRedBull/Powder-Game diff --git a/shaders/boundary_build.comp b/shaders/boundary_build.comp index 179dbbb..b656536 100644 --- a/shaders/boundary_build.comp +++ b/shaders/boundary_build.comp @@ -2,21 +2,29 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16ui, binding = 0) uniform readonly uimage2D materialTex; -layout(r16f, binding = 1) uniform readonly image2D sandOccupancyTex; +layout(binding = 0) uniform usampler2D materialTex; +layout(binding = 1) uniform sampler2D sandOccupancyTex; layout(r8ui, binding = 2) uniform writeonly uimage2D boundaryTex; layout(r8ui, binding = 3) uniform writeonly uimage2D staticBoundaryTex; uniform ivec2 gridSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (coord.x >= gridSize.x || coord.y >= gridSize.y) { return; } - uint material = imageLoad(materialTex, coord).r; - float sand_occupancy = imageLoad(sandOccupancyTex, coord).x; + uint material = loadTexel(materialTex, coord).r; + float sand_occupancy = loadTexel(sandOccupancyTex, coord).x; // Painted rock (2) and wood (3, 256 and up while burning or charred; wood_burn.comp) are solids. bool solid = material >= 2u; uint static_blocked = solid ? 1u : 0u; diff --git a/shaders/boundary_distance.comp b/shaders/boundary_distance.comp index d6ac9a1..0facf3d 100644 --- a/shaders/boundary_distance.comp +++ b/shaders/boundary_distance.comp @@ -2,13 +2,21 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryMaskTex; -layout(rg32i, binding = 1) uniform readonly iimage2D boundarySeedTex; +layout(binding = 0) uniform usampler2D boundaryMaskTex; +layout(binding = 1) uniform isampler2D boundarySeedTex; layout(r16f, binding = 2) uniform writeonly image2D boundarySdfTex; uniform ivec2 gridSize; uniform float maxDistance; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} +ivec4 loadTexel(isampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : ivec4(0); +} + bool inBounds(ivec2 coord) { return coord.x >= 0 && coord.y >= 0 && coord.x < gridSize.x && coord.y < gridSize.y; } @@ -19,8 +27,8 @@ void main() { return; } - bool inside = imageLoad(boundaryMaskTex, coord).r != 0u; - ivec2 seed = imageLoad(boundarySeedTex, coord).xy; + bool inside = loadTexel(boundaryMaskTex, coord).r != 0u; + ivec2 seed = loadTexel(boundarySeedTex, coord).xy; float distance = maxDistance; if (seed.x >= 0 && seed.y >= 0) { // Interface seeds are cell centers on both sides; a half-cell offset preserves their signs. diff --git a/shaders/boundary_downsample.comp b/shaders/boundary_downsample.comp index abe0633..985d97f 100644 --- a/shaders/boundary_downsample.comp +++ b/shaders/boundary_downsample.comp @@ -2,10 +2,10 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D srcBoundaryTex; +layout(binding = 0) uniform usampler2D srcBoundaryTex; layout(r8ui, binding = 1) uniform writeonly uimage2D dstBoundaryTex; -layout(r16f, binding = 2) uniform readonly image2D liquidVolumeTex; // water plus oil volume per source cell -layout(rg16f, binding = 3) uniform readonly image2D oilTex; // x: oil share of that liquid (oil_field.comp) +layout(binding = 2) uniform sampler2D liquidVolumeTex; // water plus oil volume per source cell +layout(binding = 3) uniform sampler2D oilTex; // x: oil share of that liquid (oil_field.comp) uniform ivec2 srcSize; uniform ivec2 dstSize; @@ -15,6 +15,14 @@ uniform int scale; // Zero (unset) leaves water open. uniform float waterBlockVolume; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool inBounds(ivec2 coord, ivec2 size) { return coord.x >= 0 && coord.y >= 0 && coord.x < size.x && coord.y < size.y; } @@ -32,7 +40,7 @@ void main() { uint blocked = 0u; for (int y = src_min.y; y < src_max.y && blocked != 1u; ++y) { for (int x = src_min.x; x < src_max.x; ++x) { - uint source = imageLoad(srcBoundaryTex, ivec2(x, y)).r; + uint source = loadTexel(srcBoundaryTex, ivec2(x, y)).r; if (source == 1u) { blocked = 1u; break; @@ -46,7 +54,7 @@ void main() { bool submerged = true; for (int i = 0; i < scale * scale && submerged; ++i) { ivec2 cell = min(dst * scale + ivec2(i % scale, i / scale), srcSize - 1); - submerged = imageLoad(liquidVolumeTex, cell).x * (1.0 - imageLoad(oilTex, cell).x) >= waterBlockVolume; + submerged = loadTexel(liquidVolumeTex, cell).x * (1.0 - loadTexel(oilTex, cell).x) >= waterBlockVolume; } if (submerged) blocked = 3u; } diff --git a/shaders/boundary_jumpflood.comp b/shaders/boundary_jumpflood.comp index f3adbbb..05dcf24 100644 --- a/shaders/boundary_jumpflood.comp +++ b/shaders/boundary_jumpflood.comp @@ -2,12 +2,17 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(rg32i, binding = 0) uniform readonly iimage2D srcSeedTex; +layout(binding = 0) uniform isampler2D srcSeedTex; layout(rg32i, binding = 1) uniform writeonly iimage2D dstSeedTex; uniform ivec2 gridSize; uniform int stepSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +ivec4 loadTexel(isampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : ivec4(0); +} + bool inBounds(ivec2 coord) { return coord.x >= 0 && coord.y >= 0 && coord.x < gridSize.x && coord.y < gridSize.y; } @@ -27,7 +32,7 @@ void main() { return; } - ivec2 best_seed = imageLoad(srcSeedTex, coord).xy; + ivec2 best_seed = loadTexel(srcSeedTex, coord).xy; float best_distance = validSeed(best_seed) ? seedDistanceSq(coord, best_seed) : 3.402823466e+38; for (int y = -1; y <= 1; ++y) { @@ -36,7 +41,7 @@ void main() { if (!inBounds(sample_coord)) { continue; } - ivec2 candidate_seed = imageLoad(srcSeedTex, sample_coord).xy; + ivec2 candidate_seed = loadTexel(srcSeedTex, sample_coord).xy; if (!validSeed(candidate_seed)) { continue; } diff --git a/shaders/boundary_mask.comp b/shaders/boundary_mask.comp index 1f09edf..5fb8955 100644 --- a/shaders/boundary_mask.comp +++ b/shaders/boundary_mask.comp @@ -2,19 +2,24 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D boundarySdfTex; +layout(binding = 0) uniform sampler2D boundarySdfTex; layout(r8ui, binding = 1) uniform writeonly uimage2D boundaryMaskTex; uniform ivec2 gridSize; uniform float solidThreshold; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} + void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (coord.x >= gridSize.x || coord.y >= gridSize.y) { return; } - float sdf = imageLoad(boundarySdfTex, coord).x; + float sdf = loadTexel(boundarySdfTex, coord).x; uint blocked = sdf <= solidThreshold ? 1u : 0u; imageStore(boundaryMaskTex, coord, uvec4(blocked, 0u, 0u, 0u)); } diff --git a/shaders/boundary_seed.comp b/shaders/boundary_seed.comp index 84d87c3..7d697e5 100644 --- a/shaders/boundary_seed.comp +++ b/shaders/boundary_seed.comp @@ -2,17 +2,22 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryMaskTex; +layout(binding = 0) uniform usampler2D boundaryMaskTex; layout(rg32i, binding = 1) uniform writeonly iimage2D boundarySeedTex; uniform ivec2 gridSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool inBounds(ivec2 coord) { return coord.x >= 0 && coord.y >= 0 && coord.x < gridSize.x && coord.y < gridSize.y; } bool occupied(ivec2 coord) { - return inBounds(coord) && imageLoad(boundaryMaskTex, coord).r != 0u; + return inBounds(coord) && loadTexel(boundaryMaskTex, coord).r != 0u; } void main() { diff --git a/shaders/coupling_fire.comp b/shaders/coupling_fire.comp index f4f2ef1..c0129e4 100644 --- a/shaders/coupling_fire.comp +++ b/shaders/coupling_fire.comp @@ -2,14 +2,14 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 1) uniform readonly image2D waterAmountTex; -layout(r16f, binding = 2) uniform readonly image2D temperatureTex; +layout(binding = 0) uniform usampler2D boundaryTex; +layout(binding = 1) uniform sampler2D waterAmountTex; +layout(binding = 2) uniform sampler2D temperatureTex; layout(rg16f, binding = 3) uniform image2D combustionTex; layout(r16f, binding = 4) uniform image2D oxidizerTex; layout(r16f, binding = 5) uniform image2D fuelTex; -layout(rg16f, binding = 6) uniform readonly image2D oilTex; // x: oil share of the liquid (oil_field.comp) -layout(r16f, binding = 7) uniform readonly image2D sandOccupancyTex; +layout(binding = 6) uniform sampler2D oilTex; // x: oil share of the liquid (oil_field.comp) +layout(binding = 7) uniform sampler2D sandOccupancyTex; uniform ivec2 gridSize; uniform ivec2 heatSize; @@ -18,6 +18,14 @@ uniform float waterCooling; // Sand occupancy at which sand covering a cell smothers its fire completely (cuts fuel and oxygen like water does). uniform float sandSmother; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Liquid that is water, not oil (oil is fuel, not a quencher), and sand, averaged over the gas cell's 2x2 cells. float sampleWater(ivec2 gas_coord, out float sand) { ivec2 full = clamp(gas_coord * 2, ivec2(0), gridSize - ivec2(2)); @@ -25,8 +33,8 @@ float sampleWater(ivec2 gas_coord, out float sand) { sand = 0.0; for (int i = 0; i < 4; ++i) { ivec2 cell = full + ivec2(i & 1, i >> 1); - water += imageLoad(waterAmountTex, cell).x * (1.0 - imageLoad(oilTex, cell).x); - sand += imageLoad(sandOccupancyTex, cell).x; + water += loadTexel(waterAmountTex, cell).x * (1.0 - loadTexel(oilTex, cell).x); + sand += loadTexel(sandOccupancyTex, cell).x; } sand *= 0.25; return 0.25 * water; @@ -44,7 +52,7 @@ void main() { float oxidizer = imageLoad(oxidizerTex, coord).x; float fuel = imageLoad(fuelTex, coord).x; - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(combustionTex, coord, vec4(0.0)); imageStore(oxidizerTex, coord, vec4(0.0)); imageStore(fuelTex, coord, vec4(0.0)); diff --git a/shaders/coupling_heat.comp b/shaders/coupling_heat.comp index db17e72..1f357a5 100644 --- a/shaders/coupling_heat.comp +++ b/shaders/coupling_heat.comp @@ -3,17 +3,22 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r16f, binding = 0) uniform image2D temperatureTex; -layout(r16f, binding = 1) uniform readonly image2D waterAmountTex; -layout(rg16f, binding = 2) uniform readonly image2D oilTex; // x: oil share of the liquid; oil does not cool +layout(binding = 1) uniform sampler2D waterAmountTex; +layout(binding = 2) uniform sampler2D oilTex; // x: oil share of the liquid; oil does not cool uniform ivec2 heatSize; uniform ivec2 gridSize; uniform float dt; uniform float waterCooling; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} + float waterSample(ivec2 full_coord) { ivec2 clamped = clamp(full_coord, ivec2(0), gridSize - ivec2(1)); - return imageLoad(waterAmountTex, clamped).x * (1.0 - imageLoad(oilTex, clamped).x); + return loadTexel(waterAmountTex, clamped).x * (1.0 - loadTexel(oilTex, clamped).x); } void main() { diff --git a/shaders/fire_rd.comp b/shaders/fire_rd.comp index 38812e1..94942b4 100644 --- a/shaders/fire_rd.comp +++ b/shaders/fire_rd.comp @@ -2,17 +2,17 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D fuelSrc; -layout(r16f, binding = 1) uniform readonly image2D oxidizerSrc; -layout(r16f, binding = 2) uniform readonly image2D temperatureSrc; -layout(r16f, binding = 3) uniform readonly image2D smokeSrc; -layout(r8ui, binding = 4) uniform readonly uimage2D boundaryTex; -layout(rg16f, binding = 5) uniform readonly image2D reactionSrc; -layout(r16f, binding = 6) uniform writeonly image2D fuelDst; -layout(r16f, binding = 7) uniform writeonly image2D oxidizerDst; -layout(r16f, binding = 8) uniform writeonly image2D temperatureDst; -layout(rg16f, binding = 9) uniform writeonly image2D reactionDst; -layout(r16f, binding = 10) uniform writeonly image2D smokeDst; +layout(binding = 0) uniform sampler2D fuelSrc; +layout(binding = 1) uniform sampler2D oxidizerSrc; +layout(binding = 2) uniform sampler2D temperatureSrc; +layout(binding = 3) uniform sampler2D smokeSrc; +layout(binding = 4) uniform usampler2D boundaryTex; +layout(binding = 5) uniform sampler2D reactionSrc; +layout(r16f, binding = 0) uniform writeonly image2D fuelDst; +layout(r16f, binding = 1) uniform writeonly image2D oxidizerDst; +layout(r16f, binding = 2) uniform writeonly image2D temperatureDst; +layout(rg16f, binding = 3) uniform writeonly image2D reactionDst; +layout(r16f, binding = 4) uniform writeonly image2D smokeDst; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -34,28 +34,36 @@ uniform float smokeDissipation; // Oxygen relaxes toward fresh air (oxidizer 1) at this rate (1/s): the flame entrains surrounding air. uniform float oxygenRate; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + ivec2 clampGrid(ivec2 coord) { return clamp(coord, ivec2(0), gridSize - ivec2(1)); } float fuelAt(ivec2 coord) { - return imageLoad(fuelSrc, clampGrid(coord)).x; + return loadTexel(fuelSrc, clampGrid(coord)).x; } float oxidizerAt(ivec2 coord) { - return imageLoad(oxidizerSrc, clampGrid(coord)).x; + return loadTexel(oxidizerSrc, clampGrid(coord)).x; } float temperatureAt(ivec2 coord) { - return imageLoad(temperatureSrc, clampGrid(coord)).x; + return loadTexel(temperatureSrc, clampGrid(coord)).x; } float smokeAt(ivec2 coord) { - return imageLoad(smokeSrc, clampGrid(coord)).x; + return loadTexel(smokeSrc, clampGrid(coord)).x; } vec2 reactionAt(ivec2 coord) { - return imageLoad(reactionSrc, clampGrid(coord)).xy; + return loadTexel(reactionSrc, clampGrid(coord)).xy; } bool invalid(float value) { @@ -64,7 +72,7 @@ bool invalid(float value) { bool fluidAt(ivec2 coord) { return all(greaterThanEqual(coord, ivec2(0))) && all(lessThan(coord, gridSize)) && - imageLoad(boundaryTex, coord).r == 0u; + loadTexel(boundaryTex, coord).r == 0u; } float laplacianTemperature(ivec2 coord, float center) { @@ -87,7 +95,7 @@ void main() { return; } - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(reactionDst, coord, vec4(0.0)); imageStore(fuelDst, coord, vec4(0.0)); imageStore(oxidizerDst, coord, vec4(0.0)); diff --git a/shaders/gas_cg.comp b/shaders/gas_cg.comp index 7656300..deaf283 100644 --- a/shaders/gas_cg.comp +++ b/shaders/gas_cg.comp @@ -8,13 +8,13 @@ // s -= alpha q, partial s.z_old, z = 0; 4 partial s.z; 5 beta (one workgroup); 6 d = z + beta d. layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D rhsTex; +layout(binding = 0) uniform sampler2D rhsTex; layout(r32f, binding = 1) uniform image2D xTex; layout(r32f, binding = 2) uniform image2D sTex; layout(r32f, binding = 3) uniform image2D zTex; layout(r32f, binding = 4) uniform image2D dTex; layout(r32f, binding = 5) uniform image2D qTex; -layout(r8ui, binding = 6) uniform readonly uimage2D kindTex; +layout(binding = 6) uniform usampler2D kindTex; // Per workgroup: x s.z, y s.z_old, z d.q. layout(std430, binding = 0) buffer Partials { @@ -37,13 +37,21 @@ uniform float sandLeak; uniform int partialCount; uniform int first; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + shared float sums[256]; shared float sums2[256]; uint cellKind(ivec2 coord) { if (coord.x < 0 || coord.y < 0 || coord.x >= gridSize.x) return 1u; if (coord.y >= gridSize.y) return openTop != 0 ? 2u : 1u; - return imageLoad(kindTex, coord).r; + return loadTexel(kindTex, coord).r; } // The fine operator of pressure_residual.comp: neighbours that are fluid add their value, every @@ -129,7 +137,7 @@ void main() { float a = 0.0, b = 0.0; if (phase == 0) { if (inside) { - imageStore(sTex, coord, vec4(fluid ? imageLoad(rhsTex, coord).x - laplacian(false, coord) : 0.0)); + imageStore(sTex, coord, vec4(fluid ? loadTexel(rhsTex, coord).x - laplacian(false, coord) : 0.0)); imageStore(zTex, coord, vec4(0.0)); } return; diff --git a/shaders/gas_face_to_center.comp b/shaders/gas_face_to_center.comp index 61a73f9..cb8820b 100644 --- a/shaders/gas_face_to_center.comp +++ b/shaders/gas_face_to_center.comp @@ -2,26 +2,34 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D gasUTex; -layout(r32f, binding = 1) uniform readonly image2D gasVTex; +layout(binding = 0) uniform sampler2D gasUTex; +layout(binding = 1) uniform sampler2D gasVTex; layout(rg16f, binding = 2) uniform writeonly image2D gasCenterVelocityTex; -layout(r8ui, binding = 3) uniform readonly uimage2D boundaryTex; +layout(binding = 3) uniform usampler2D boundaryTex; uniform ivec2 gasSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (coord.x >= gasSize.x || coord.y >= gasSize.y) { return; } - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(gasCenterVelocityTex, coord, vec4(0.0)); return; } - float uL = imageLoad(gasUTex, ivec2(coord.x, coord.y)).x; - float uR = imageLoad(gasUTex, ivec2(coord.x + 1, coord.y)).x; - float vB = imageLoad(gasVTex, ivec2(coord.x, coord.y)).x; - float vT = imageLoad(gasVTex, ivec2(coord.x, coord.y + 1)).x; + float uL = loadTexel(gasUTex, ivec2(coord.x, coord.y)).x; + float uR = loadTexel(gasUTex, ivec2(coord.x + 1, coord.y)).x; + float vB = loadTexel(gasVTex, ivec2(coord.x, coord.y)).x; + float vT = loadTexel(gasVTex, ivec2(coord.x, coord.y + 1)).x; imageStore(gasCenterVelocityTex, coord, vec4(0.5 * (uL + uR), 0.5 * (vB + vT), 0.0, 0.0)); } diff --git a/shaders/gas_pressure_kind.comp b/shaders/gas_pressure_kind.comp index 6a067b2..c4f2ef8 100644 --- a/shaders/gas_pressure_kind.comp +++ b/shaders/gas_pressure_kind.comp @@ -2,7 +2,7 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D fineKindTex; +layout(binding = 0) uniform usampler2D fineKindTex; layout(r8ui, binding = 1) uniform writeonly uimage2D coarseKindTex; uniform ivec2 fineSize; @@ -10,6 +10,11 @@ uniform ivec2 coarseSize; // 1 classifies the gas mask itself (level 0); 0 coarsens the level below. uniform int fromMask; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Footprint kinds of one multigrid level: 0 fluid, 1 solid, 4 porous sand (conducts at sandLeak, see // gas_pressure_relax.comp; it keeps gas sealed inside sand from making the system singular). A coarse cell is fluid when any child is: walls // thinner than a coarse cell are left to the finer levels. (Omitting mixed footprints as zero-correction cells @@ -21,7 +26,7 @@ void main() { } uint kind; if (fromMask != 0) { - uint mask = imageLoad(fineKindTex, coord).r; + uint mask = loadTexel(fineKindTex, coord).r; kind = mask == 0u ? 0u : (mask == 3u ? 4u : 1u); } else { bool fluid = false; @@ -31,7 +36,7 @@ void main() { // Odd extents: a footprint past the domain edge is classified by its children inside it. As a wall // it would cut the last fine cells off every coarser correction. if (any(greaterThanEqual(child, fineSize))) continue; - uint childKind = imageLoad(fineKindTex, child).r; + uint childKind = loadTexel(fineKindTex, child).r; fluid = fluid || childKind == 0u; porous = porous || childKind == 4u; } diff --git a/shaders/gas_pressure_prolongate.comp b/shaders/gas_pressure_prolongate.comp index 96a1b9a..80a402d 100644 --- a/shaders/gas_pressure_prolongate.comp +++ b/shaders/gas_pressure_prolongate.comp @@ -5,17 +5,25 @@ // applies exactly the transpose, which keeps the V-cycle symmetric for the conjugate gradient. layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D coarsePressureTex; +layout(binding = 0) uniform sampler2D coarsePressureTex; layout(r32f, binding = 1) uniform image2D finePressureTex; -layout(r8ui, binding = 2) uniform readonly uimage2D fineKindTex; -layout(r8ui, binding = 3) uniform readonly uimage2D coarseKindTex; +layout(binding = 2) uniform usampler2D fineKindTex; +layout(binding = 3) uniform usampler2D coarseKindTex; uniform ivec2 fineSize; uniform ivec2 coarseSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool coarseFluid(ivec2 coord) { return all(greaterThanEqual(coord, ivec2(0))) && all(lessThan(coord, coarseSize)) && - imageLoad(coarseKindTex, coord).r == 0u; + loadTexel(coarseKindTex, coord).r == 0u; } void main() { @@ -23,7 +31,7 @@ void main() { if (coord.x >= fineSize.x || coord.y >= fineSize.y) { return; } - if (imageLoad(fineKindTex, coord).r != 0u) { + if (loadTexel(fineKindTex, coord).r != 0u) { imageStore(finePressureTex, coord, vec4(0.0)); return; } @@ -35,7 +43,7 @@ void main() { ivec2 offset = ivec2(i & 1, i >> 1); ivec2 donor = parent + offset * toward; float weight = (offset.x == 0 ? 0.75 : 0.25) * (offset.y == 0 ? 0.75 : 0.25); - value += weight * imageLoad(coarsePressureTex, coarseFluid(donor) ? donor : parent).x; + value += weight * loadTexel(coarsePressureTex, coarseFluid(donor) ? donor : parent).x; } imageStore(finePressureTex, coord, vec4(value, 0.0, 0.0, 0.0)); } diff --git a/shaders/gas_pressure_relax.comp b/shaders/gas_pressure_relax.comp index 45af6da..cdd468c 100644 --- a/shaders/gas_pressure_relax.comp +++ b/shaders/gas_pressure_relax.comp @@ -2,16 +2,16 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D rhsTex; +layout(binding = 0) uniform sampler2D rhsTex; layout(r32f, binding = 1) uniform coherent image2D pressureTex; -layout(r8ui, binding = 3) uniform readonly uimage2D kindTex; +layout(binding = 3) uniform usampler2D kindTex; // Fused smoothing (smoothSteps > 0) reads pressureTex and writes the result here, so neighbouring workgroups never // see each other's updates; the pre-smoothing also writes its residual and the post-smoothing first adds the coarse // correction (gas_pressure_restrict.comp's pass 0 and gas_pressure_prolongate.comp, same arithmetic). layout(r32f, binding = 2) uniform writeonly image2D smoothedTex; layout(r32f, binding = 4) uniform writeonly image2D residualTex; -layout(r32f, binding = 5) uniform readonly image2D coarsePressureTex; -layout(r8ui, binding = 6) uniform readonly uimage2D coarseKindTex; +layout(binding = 5) uniform sampler2D coarsePressureTex; +layout(binding = 6) uniform usampler2D coarseKindTex; uniform ivec2 gridSize; uniform int boundaryScale; @@ -35,6 +35,14 @@ uniform ivec2 coarseSize; // Height of the finest grid: with an open top it holds zero pressure at its ghost row, fineHeight + 0.5. uniform int fineHeight; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Open top: a level with spacing h reaches that zero from its top centres, (top + 0.5) h fine cells up, so the // ghost weighs h / distance (1 on the finest grid; odd levels whose top row straddles the edge weigh more). float openTopWeight(int top) { @@ -47,7 +55,7 @@ float openTopWeight(int top) { uint cellKind(ivec2 coord) { if (coord.x < 0 || coord.y < 0 || coord.x >= gridSize.x) return 1u; if (coord.y >= gridSize.y) return openTop != 0 ? 3u : 1u; - return imageLoad(kindTex, coord).r; + return loadTexel(kindTex, coord).r; } void relaxCell(ivec2 coord, int colour) { @@ -76,7 +84,7 @@ void relaxCell(ivec2 coord, int colour) { } } // Every level uses the unscaled 5-point operator; restriction (weights summing to 4) carries the h^2. - float pressure = diagonal > 0.0 ? (pressure_sum - imageLoad(rhsTex, coord).x) / diagonal : 0.0; + float pressure = diagonal > 0.0 ? (pressure_sum - loadTexel(rhsTex, coord).x) / diagonal : 0.0; imageStore(pressureTex, coord, vec4(pressure, 0.0, 0.0, 0.0)); } @@ -111,8 +119,8 @@ void fusedSmooth(int stepCount, bool whole) { if (all(lessThan(at, ivec2(coarseSpan)))) { ivec2 c = coarseOrigin + at; bool inside = all(greaterThanEqual(c, ivec2(0))) && all(lessThan(c, coarseSize)); - float value = imageLoad(coarsePressureTex, c).x; - uint kind = imageLoad(coarseKindTex, c).r; + float value = loadTexel(coarsePressureTex, c).x; + uint kind = loadTexel(coarseKindTex, c).r; coarseValue[at.y * coarseSpan + at.x] = inside ? value : 0.0; coarseIsFluid[at.y * coarseSpan + at.x] = inside && kind == 0u; } @@ -125,7 +133,7 @@ void fusedSmooth(int stepCount, bool whole) { int k = at.y * span + at.x; // Independent loads (zero outside the image), selected by the kind afterwards. float pressure = imageLoad(pressureTex, coord).x; - float rhs = imageLoad(rhsTex, coord).x; + float rhs = loadTexel(rhsTex, coord).x; uint kind = cellKind(coord); float value = 0.0; if (kind == 0u) { diff --git a/shaders/gas_pressure_restrict.comp b/shaders/gas_pressure_restrict.comp index 5182e27..ace7b05 100644 --- a/shaders/gas_pressure_restrict.comp +++ b/shaders/gas_pressure_restrict.comp @@ -4,13 +4,13 @@ // coarse cell in the interior), so every level uses the same unscaled 5-point operator. layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D rhsFineTex; -layout(r32f, binding = 1) uniform readonly image2D pressureFineTex; +layout(binding = 0) uniform sampler2D rhsFineTex; +layout(binding = 1) uniform sampler2D pressureFineTex; layout(r32f, binding = 2) uniform writeonly image2D rhsCoarseTex; -layout(r8ui, binding = 3) uniform readonly uimage2D kindTex; +layout(binding = 3) uniform usampler2D kindTex; // Each V-cycle's coarse correction starts from zero. layout(r32f, binding = 4) uniform writeonly image2D pressureCoarseTex; -layout(r8ui, binding = 5) uniform readonly uimage2D coarseKindTex; +layout(binding = 5) uniform usampler2D coarseKindTex; // Fine residual, written by the first pass (gatherPass 0, over the fine grid) and gathered by the second. layout(r32f, binding = 6) uniform image2D residualTex; @@ -25,6 +25,14 @@ uniform float sandLeak; uniform int fineHeight; uniform int gatherPass; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Open top: a level with spacing h reaches that zero from its top centres, (top + 0.5) h fine cells up, so the // ghost weighs h / distance (1 on the finest grid; odd levels whose top row straddles the edge weigh more). float openTopWeight(int top) { @@ -36,7 +44,7 @@ float openTopWeight(int top) { uint cellKind(ivec2 coord) { if (coord.x < 0 || coord.y < 0 || coord.x >= fineSize.x) return 1u; if (coord.y >= fineSize.y) return openTop != 0 ? 3u : 1u; - return imageLoad(kindTex, coord).r; + return loadTexel(kindTex, coord).r; } float residualAt(ivec2 coord) { @@ -47,7 +55,7 @@ float residualAt(ivec2 coord) { ivec2 neighbor = coord + offsets[i]; uint kind = cellKind(neighbor); if (kind == 0u) { - pressure_sum += imageLoad(pressureFineTex, neighbor).x; + pressure_sum += loadTexel(pressureFineTex, neighbor).x; diagonal += 1.0; } else if (kind == 3u) { diagonal += openTopWeight(fineSize.y - 1); @@ -55,20 +63,20 @@ float residualAt(ivec2 coord) { diagonal += sandLeak; } } - float lap = pressure_sum - diagonal * imageLoad(pressureFineTex, coord).x; - return imageLoad(rhsFineTex, coord).x - lap; + float lap = pressure_sum - diagonal * loadTexel(pressureFineTex, coord).x; + return loadTexel(rhsFineTex, coord).x - lap; } bool coarseFluid(ivec2 coord) { return all(greaterThanEqual(coord, ivec2(0))) && all(lessThan(coord, coarseSize)) && - imageLoad(coarseKindTex, coord).r == 0u; + loadTexel(coarseKindTex, coord).r == 0u; } void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (gatherPass == 0) { if (all(lessThan(coord, fineSize))) - imageStore(residualTex, coord, vec4(imageLoad(kindTex, coord).r == 0u ? residualAt(coord) : 0.0)); + imageStore(residualTex, coord, vec4(loadTexel(kindTex, coord).r == 0u ? residualAt(coord) : 0.0)); return; } if (coord.x >= coarseSize.x || coord.y >= coarseSize.y) { @@ -83,7 +91,7 @@ void main() { for (int x = 2 * coord.x - 1; x <= 2 * coord.x + 2; ++x) { ivec2 fine = ivec2(x, y); if (any(lessThan(fine, ivec2(0))) || any(greaterThanEqual(fine, fineSize)) || - imageLoad(kindTex, fine).r != 0u) continue; + loadTexel(kindTex, fine).r != 0u) continue; ivec2 parent = fine / 2; ivec2 toward = ivec2(fine.x & 1, fine.y & 1) * 2 - 1; float weight = 0.0; diff --git a/shaders/gas_velocity_advect.comp b/shaders/gas_velocity_advect.comp index 334f137..9ced220 100644 --- a/shaders/gas_velocity_advect.comp +++ b/shaders/gas_velocity_advect.comp @@ -2,23 +2,31 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D gasUSrc; -layout(r32f, binding = 1) uniform readonly image2D gasVSrc; -layout(rg16f, binding = 2) uniform readonly image2D gasCenterVelocityTex; +layout(binding = 0) uniform sampler2D gasUSrc; +layout(binding = 1) uniform sampler2D gasVSrc; +layout(binding = 2) uniform sampler2D gasCenterVelocityTex; layout(r32f, binding = 3) uniform writeonly image2D gasUDst; layout(r32f, binding = 4) uniform writeonly image2D gasVDst; -layout(r8ui, binding = 5) uniform readonly uimage2D boundaryTex; +layout(binding = 5) uniform usampler2D boundaryTex; uniform ivec2 gasSize; uniform float dt; uniform float maxVelocity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + ivec2 clampCell(ivec2 coord) { return clamp(coord, ivec2(0), gasSize - ivec2(1)); } bool blockedCell(ivec2 coord) { - return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || imageLoad(boundaryTex, coord).r != 0u; + return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || loadTexel(boundaryTex, coord).r != 0u; } @@ -90,22 +98,22 @@ vec2 sampleCenterVelocity(vec2 position) { vec2 p = clamp(position - vec2(0.5), vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(p)); vec2 frac = fract(p); - vec2 v00 = imageLoad(gasCenterVelocityTex, base).xy; - vec2 v10 = imageLoad(gasCenterVelocityTex, clampCell(base + ivec2(1, 0))).xy; - vec2 v01 = imageLoad(gasCenterVelocityTex, clampCell(base + ivec2(0, 1))).xy; - vec2 v11 = imageLoad(gasCenterVelocityTex, clampCell(base + ivec2(1, 1))).xy; + vec2 v00 = loadTexel(gasCenterVelocityTex, base).xy; + vec2 v10 = loadTexel(gasCenterVelocityTex, clampCell(base + ivec2(1, 0))).xy; + vec2 v01 = loadTexel(gasCenterVelocityTex, clampCell(base + ivec2(0, 1))).xy; + vec2 v11 = loadTexel(gasCenterVelocityTex, clampCell(base + ivec2(1, 1))).xy; return clamp(mix(mix(v00, v10, frac.x), mix(v01, v11, frac.x), frac.y), vec2(-maxVelocity), vec2(maxVelocity)); } float sampleU(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(gasUSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(gasUSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } float sampleV(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(gasVSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(gasVSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } diff --git a/shaders/gas_velocity_correct.comp b/shaders/gas_velocity_correct.comp index 2340cac..676dd2c 100644 --- a/shaders/gas_velocity_correct.comp +++ b/shaders/gas_velocity_correct.comp @@ -2,17 +2,17 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D gasUSrc; -layout(r32f, binding = 1) uniform readonly image2D gasVSrc; -layout(rg16f, binding = 2) uniform readonly image2D gasCenterVelocitySrc; -layout(r32f, binding = 3) uniform readonly image2D gasUFwd; -layout(r32f, binding = 4) uniform readonly image2D gasVFwd; -layout(rg16f, binding = 5) uniform readonly image2D gasCenterVelocityFwd; -layout(r16f, binding = 6) uniform readonly image2D smokeDensityTex; -layout(r16f, binding = 7) uniform readonly image2D temperatureTex; -layout(r32f, binding = 8) uniform writeonly image2D gasUDst; -layout(r32f, binding = 9) uniform writeonly image2D gasVDst; -layout(r8ui, binding = 10) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform sampler2D gasUSrc; +layout(binding = 1) uniform sampler2D gasVSrc; +layout(binding = 2) uniform sampler2D gasCenterVelocitySrc; +layout(binding = 3) uniform sampler2D gasUFwd; +layout(binding = 4) uniform sampler2D gasVFwd; +layout(binding = 5) uniform sampler2D gasCenterVelocityFwd; +layout(binding = 6) uniform sampler2D smokeDensityTex; +layout(binding = 7) uniform sampler2D temperatureTex; +layout(r32f, binding = 0) uniform writeonly image2D gasUDst; +layout(r32f, binding = 1) uniform writeonly image2D gasVDst; +layout(binding = 10) uniform usampler2D boundaryTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -30,12 +30,20 @@ uniform float maxVelocity; // Vorticity confinement strength (1/s): puts back the small swirls that interpolation smooths away. uniform float vorticity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + ivec2 clampCell(ivec2 coord) { return clamp(coord, ivec2(0), gasSize - ivec2(1)); } bool blockedCell(ivec2 coord) { - return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || imageLoad(boundaryTex, coord).r != 0u; + return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || loadTexel(boundaryTex, coord).r != 0u; } @@ -107,10 +115,10 @@ vec2 sampleCenterVelocitySrc(vec2 position) { vec2 p = clamp(position - vec2(0.5), vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(p)); vec2 frac = fract(p); - vec2 v00 = imageLoad(gasCenterVelocitySrc, base).xy; - vec2 v10 = imageLoad(gasCenterVelocitySrc, clampCell(base + ivec2(1, 0))).xy; - vec2 v01 = imageLoad(gasCenterVelocitySrc, clampCell(base + ivec2(0, 1))).xy; - vec2 v11 = imageLoad(gasCenterVelocitySrc, clampCell(base + ivec2(1, 1))).xy; + vec2 v00 = loadTexel(gasCenterVelocitySrc, base).xy; + vec2 v10 = loadTexel(gasCenterVelocitySrc, clampCell(base + ivec2(1, 0))).xy; + vec2 v01 = loadTexel(gasCenterVelocitySrc, clampCell(base + ivec2(0, 1))).xy; + vec2 v11 = loadTexel(gasCenterVelocitySrc, clampCell(base + ivec2(1, 1))).xy; return clamp(mix(mix(v00, v10, frac.x), mix(v01, v11, frac.x), frac.y), vec2(-maxVelocity), vec2(maxVelocity)); } @@ -119,7 +127,7 @@ vec2 sampleCenterVelocitySrc(vec2 position) { float sampleUFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(gasUFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(gasUFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -127,7 +135,7 @@ float sampleUFwd(TransportStencil stencil) { float sampleVFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(gasVFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(gasVFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -135,7 +143,7 @@ float clampLocalU(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(gasUSrc, stencil.cells[i]).x; + float donor = loadTexel(gasUSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -147,7 +155,7 @@ float clampLocalV(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(gasVSrc, stencil.cells[i]).x; + float donor = loadTexel(gasVSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -156,20 +164,20 @@ float clampLocalV(TransportStencil stencil, float value) { } float sampleTemperature(ivec2 coord) { - return imageLoad(temperatureTex, clampCell(coord)).x; + return loadTexel(temperatureTex, clampCell(coord)).x; } float sampleSmoke(ivec2 coord) { - return imageLoad(smokeDensityTex, clampCell(coord)).x; + return loadTexel(smokeDensityTex, clampCell(coord)).x; } // Curl of the centred velocity at a cell; walls and the domain edge contribute none. float curlAt(ivec2 c) { if (blockedCell(c)) return 0.0; - vec2 l = imageLoad(gasCenterVelocitySrc, clampCell(c - ivec2(1, 0))).xy; - vec2 r = imageLoad(gasCenterVelocitySrc, clampCell(c + ivec2(1, 0))).xy; - vec2 b = imageLoad(gasCenterVelocitySrc, clampCell(c - ivec2(0, 1))).xy; - vec2 t = imageLoad(gasCenterVelocitySrc, clampCell(c + ivec2(0, 1))).xy; + vec2 l = loadTexel(gasCenterVelocitySrc, clampCell(c - ivec2(1, 0))).xy; + vec2 r = loadTexel(gasCenterVelocitySrc, clampCell(c + ivec2(1, 0))).xy; + vec2 b = loadTexel(gasCenterVelocitySrc, clampCell(c - ivec2(0, 1))).xy; + vec2 t = loadTexel(gasCenterVelocitySrc, clampCell(c + ivec2(0, 1))).xy; return 0.5 * ((r.y - l.y) - (t.x - b.x)); } @@ -204,7 +212,7 @@ void main() { vec2(gasSize.x, gasSize.y - 0.5)); TransportStencil backStencil = transportStencil(facePos, backtrace, vec2(0.0, 0.5), ivec2(gasSize.x, gasSize.y - 1), 0); TransportStencil reverseStencil = transportStencil(facePos, reverseTrace, vec2(0.0, 0.5), ivec2(gasSize.x, gasSize.y - 1), 0); - value = imageLoad(gasUFwd, coord).x + 0.5 * (imageLoad(gasUSrc, coord).x - sampleUFwd(reverseStencil)); + value = loadTexel(gasUFwd, coord).x + 0.5 * (loadTexel(gasUSrc, coord).x - sampleUFwd(reverseStencil)); value = clampLocalU(backStencil, value) * velocityDissipation; value += 0.5 * (confinementAt(ivec2(coord.x - 1, coord.y)).x + confinementAt(coord).x) * dt; value = clamp(value, -maxVelocity, maxVelocity); @@ -229,7 +237,7 @@ void main() { vec2(gasSize.x - 0.5, gasSize.y)); TransportStencil backStencil = transportStencil(facePos, backtrace, vec2(0.5, 0.0), ivec2(gasSize.x - 1, gasSize.y), 1); TransportStencil reverseStencil = transportStencil(facePos, reverseTrace, vec2(0.5, 0.0), ivec2(gasSize.x - 1, gasSize.y), 1); - value = imageLoad(gasVFwd, coord).x + 0.5 * (imageLoad(gasVSrc, coord).x - sampleVFwd(reverseStencil)); + value = loadTexel(gasVFwd, coord).x + 0.5 * (loadTexel(gasVSrc, coord).x - sampleVFwd(reverseStencil)); value = clampLocalV(backStencil, value) * velocityDissipation; float temperature = diff --git a/shaders/phase_particles.comp b/shaders/phase_particles.comp index 8b4f371..96c053f 100644 --- a/shaders/phase_particles.comp +++ b/shaders/phase_particles.comp @@ -8,11 +8,11 @@ // for oil_field.comp. layout(local_size_x = 256) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryMaskTex; -layout(r16f, binding = 1) uniform readonly image2D liquidVolumeTex; -layout(r16f, binding = 2) uniform readonly image2D temperatureTex; // gas grid -layout(rg16f, binding = 3) uniform readonly image2D reactionTex; // gas grid, y is the flame -layout(rg16f, binding = 4) uniform readonly image2D oilFieldTex; // x: oil share per cell (oil_field.comp, last tick) +layout(binding = 0) uniform usampler2D boundaryMaskTex; +layout(binding = 1) uniform sampler2D liquidVolumeTex; +layout(binding = 2) uniform sampler2D temperatureTex; // gas grid +layout(binding = 3) uniform sampler2D reactionTex; // gas grid, y is the flame +layout(binding = 4) uniform sampler2D oilFieldTex; // x: oil share per cell (oil_field.comp, last tick) struct WaterParticle { vec4 state0; @@ -49,6 +49,15 @@ uniform uint seed; uniform float evaporateRate; // 1/s for surface water under gas at full heat uniform float burnRate; // 1/s for surface oil above its ignition temperature uniform float ignitionTemperature; + +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform float restVolume; // liquid volume of a cell at rest density // Water meeting burning oil (oil under it, and in its own cell unless open air is beside or above it, with gas at the // ignition temperature over it) boils at once: the slick is far above 100 C, so water that lands on it or sinks through @@ -60,7 +69,7 @@ float heatAbove(ivec2 gas, int span) { float hot = 0.0; for (int i = 0; i < span; ++i) { ivec2 g = min(gas + ivec2(0, i), gasSize - ivec2(1)); - hot = max(hot, max(imageLoad(temperatureTex, g).x, imageLoad(reactionTex, g).y)); + hot = max(hot, max(loadTexel(temperatureTex, g).x, loadTexel(reactionTex, g).y)); } return hot; } @@ -68,7 +77,7 @@ float heatAbove(ivec2 gas, int span) { // Open air next to the particle: a cell that is neither wall nor liquid. bool airCell(ivec2 cell) { return all(greaterThanEqual(cell, ivec2(0))) && all(lessThan(cell, gridSize)) && - imageLoad(boundaryMaskTex, cell).x == 0u && imageLoad(liquidVolumeTex, cell).x < 0.3 * restVolume; + loadTexel(boundaryMaskTex, cell).x == 0u && loadTexel(liquidVolumeTex, cell).x < 0.3 * restVolume; } void main() { @@ -101,8 +110,8 @@ void main() { } bool flash = false; if (!oil && flashRate > 0.0) { - bool onOil = imageLoad(oilFieldTex, max(cell - ivec2(0, 1), ivec2(0))).x > 0.25 && - (exposed || imageLoad(oilFieldTex, cell).x > 0.25); + bool onOil = loadTexel(oilFieldTex, max(cell - ivec2(0, 1), ivec2(0))).x > 0.25 && + (exposed || loadTexel(oilFieldTex, cell).x > 0.25); // From the record's own gas cell up 12 cells (6 cm): water a few cells down in a slick still meets its flame. flash = onOil && heatAbove(clamp(cell / 2, ivec2(0), gasSize - ivec2(1)), 6) >= ignitionTemperature; if (flash) rate = max(rate, flashRate); diff --git a/shaders/pressure_cg.comp b/shaders/pressure_cg.comp index 9702b2e..be08ab5 100644 --- a/shaders/pressure_cg.comp +++ b/shaders/pressure_cg.comp @@ -1,10 +1,10 @@ #version 430 layout(local_size_x = 256) in; -layout(r16f, binding = 0) uniform readonly image2D rhsTex; +layout(binding = 0) uniform sampler2D rhsTex; layout(r32f, binding = 1) uniform image2D pressureTex; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 3) uniform readonly image2D liquidPhiTex; +layout(binding = 2) uniform usampler2D boundaryTex; +layout(binding = 3) uniform sampler2D liquidPhiTex; // Per cell: residual, search direction, A*direction or preconditioned residual, diagonal of A=-L. layout(std430, binding = 0) buffer Vectors { vec4 vectors[]; }; @@ -64,6 +64,15 @@ uniform float thetaMin; // iteration runs 2: alpha, the stored d and the p and r update; pressure_mg.comp's z = M^-1 r; 1: the convergence test, // beta, the next d = z + beta*d and A*d. uniform int phase; + +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform int parity; // iteration number & 1, from the host uniform int firstIteration; // 1 on the first iteration, which tests the initial preconditioned residual uniform int gated; // 1 in a restart: it only acts when phase 10 found it due, so the host may queue it blind @@ -82,12 +91,12 @@ float airConductance(float phiLiquid, float phiAir) { bool blocked(ivec2 cell) { return cell.x < 0 || cell.y < 0 || cell.x >= gridSize.x || cell.y >= gridSize.y || - imageLoad(boundaryTex, cell).r != 0u; + loadTexel(boundaryTex, cell).r != 0u; } bool sandWall(ivec2 cell) { return cell.x >= 0 && cell.y >= 0 && cell.x < gridSize.x && cell.y < gridSize.y && - imageLoad(boundaryTex, cell).r >= 2u; + loadTexel(boundaryTex, cell).r >= 2u; } uint tileSlots() { return uint(((gridSize.x + 15) / 16) * ((gridSize.y + 15) / 16)); } @@ -207,7 +216,7 @@ void initialize() { imageStore(pressureTex, cell, vec4(0.0)); return; } - float phi = imageLoad(liquidPhiTex, cell).x; + float phi = loadTexel(liquidPhiTex, cell).x; if (invalid(phi)) { imageStore(pressureTex, cell, vec4(0.0)); vectors[index].w = -1.0; @@ -217,7 +226,7 @@ void initialize() { imageStore(pressureTex, cell, vec4(0.0)); return; } - float rhs = -imageLoad(rhsTex, cell).x; + float rhs = -loadTexel(rhsTex, cell).x; float pressure = imageLoad(pressureTex, cell).x; if (invalid(rhs) || invalid(pressure)) { imageStore(pressureTex, cell, vec4(0.0)); @@ -232,7 +241,7 @@ void initialize() { if (sandWall(neighbor)) diagonal += sandLeak; continue; } - float neighbor_phi = imageLoad(liquidPhiTex, neighbor).x; + float neighbor_phi = loadTexel(liquidPhiTex, neighbor).x; if (neighbor_phi < liquidPhiThreshold) { diagonal += 1.0; neighbor_sum += imageLoad(pressureTex, neighbor).x; diff --git a/shaders/pressure_mg.comp b/shaders/pressure_mg.comp index 459b8e4..446f9db 100644 --- a/shaders/pressure_mg.comp +++ b/shaders/pressure_mg.comp @@ -13,7 +13,7 @@ // Prolongation treats walls as Neumann: a wall partner of the // bilinear stencil takes the fine cell's own coarse value (pulling it toward zero instead multiplied the iterations). layout(local_size_x = 8, local_size_y = 8) in; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; +layout(binding = 2) uniform usampler2D boundaryTex; // Level 0: residual, search direction, A*direction or preconditioned residual, diagonal (pressure_cg.comp). layout(std430, binding = 0) buffer Vectors { vec4 vectors[]; }; @@ -54,6 +54,12 @@ uniform float sandLeak; // steps run one workgroup per listed CG tile, the others over a level's box. uniform int phase; uniform int level; + +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform int slot; // tile sum slot of z = M^-1 r (pressure_cg.comp) uniform int initial; // 1: also start the search direction, d = z uniform int gated; // 1 in a restart of the CG, which only acts when pressure_cg.comp phase 10 found it due @@ -106,7 +112,7 @@ vec2 fineCell(ivec2 c) { // Level-0 child type: walls from the mask, liquid where the CG has a row (or invalid input), air elsewhere. float baseType(ivec2 c) { if (any(lessThan(c, ivec2(0))) || any(greaterThanEqual(c, gridSize))) return wall; - uint mask = imageLoad(boundaryTex, c).r; + uint mask = loadTexel(boundaryTex, c).r; if (mask != 0u) return mask >= 2u ? sandWall : wall; return vectors[c.y * gridSize.x + c.x].w != 0.0 ? liquid : air; } diff --git a/shaders/pressure_pockets.comp b/shaders/pressure_pockets.comp index 12af33f..ee6339e 100644 --- a/shaders/pressure_pockets.comp +++ b/shaders/pressure_pockets.comp @@ -18,7 +18,7 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r16f, binding = 0) uniform image2D rhs16; layout(r32f, binding = 1) uniform image2D rhs32; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; // water: the full mask; gas: level-0 footprint kinds +layout(binding = 2) uniform usampler2D boundaryTex; // water: the full mask; gas: level-0 footprint kinds layout(r16f, binding = 3) uniform image2D liquidPhiTex; // water only // Per cell: parent, then for a root the sum's low and high words and its cell count, whose @@ -32,6 +32,11 @@ uniform int openTop; // gas: zero pressure above the top row uniform int wide; // 1: the right-hand side is rhs32, else rhs16 uniform float liquidPhiThreshold; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + const uint kGrounded = 0x80000000u; const float kFixedScale = 1024.0; @@ -39,7 +44,7 @@ const float kFixedScale = 1024.0; int cellClass(ivec2 c) { if (gas != 0 && openTop != 0 && c.y >= gridSize.y) return 2; if (any(lessThan(c, ivec2(0))) || any(greaterThanEqual(c, gridSize))) return 0; - uint mask = imageLoad(boundaryTex, c).r; + uint mask = loadTexel(boundaryTex, c).r; if (gas != 0) return mask == 0u ? 1 : (mask == 4u ? 2 : 0); if (mask != 0u) return mask >= 2u ? 2 : 0; float phi = imageLoad(liquidPhiTex, c).x; diff --git a/shaders/pressure_residual.comp b/shaders/pressure_residual.comp index ed585d4..77dc82e 100644 --- a/shaders/pressure_residual.comp +++ b/shaders/pressure_residual.comp @@ -2,11 +2,11 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D rhsTex; -layout(r32f, binding = 4) uniform readonly image2D rhsWideTex; // the gas right-hand side -uniform sampler2D pressureTex; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 3) uniform readonly image2D liquidPhiTex; +layout(binding = 0) uniform sampler2D rhsTex; +layout(binding = 4) uniform sampler2D rhsWideTex; // the gas right-hand side +layout(binding = 1) uniform sampler2D pressureTex; +layout(binding = 2) uniform usampler2D boundaryTex; +layout(binding = 3) uniform sampler2D liquidPhiTex; layout(std430, binding = 0) buffer DebugCounters { uint nanInfDetected; @@ -33,6 +33,14 @@ uniform int porousSand; // Water: ghost-fluid conductance of a liquid-air face (pressure_cg.comp), 0 for a plain Dirichlet cell. uniform float thetaMin; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + float airConductance(float phiLiquid, float phiAir) { if (thetaMin <= 0.0) return 1.0; float inside = phiLiquid - liquidPhiThreshold; @@ -49,7 +57,7 @@ bool blockedCell(ivec2 coord) { ivec2 fineMax = fineMin + ivec2(boundaryScale); for (int y = fineMin.y; y < fineMax.y; ++y) { for (int x = fineMin.x; x < fineMax.x; ++x) { - if (imageLoad(boundaryTex, ivec2(x, y)).r != 0u) { + if (loadTexel(boundaryTex, ivec2(x, y)).r != 0u) { return true; } } @@ -68,7 +76,7 @@ vec4 residualStats(ivec2 coord) { } float cellPhi = 0.0; if (useLiquidMask != 0) { - cellPhi = imageLoad(liquidPhiTex, coord).x; + cellPhi = loadTexel(liquidPhiTex, coord).x; if (invalid(cellPhi)) { return vec4(0.0, 0.0, 1.0, 1.0); } @@ -77,7 +85,7 @@ vec4 residualStats(ivec2 coord) { } } - float rhs = wideRhs != 0 ? imageLoad(rhsWideTex, coord).x : imageLoad(rhsTex, coord).x; + float rhs = wideRhs != 0 ? loadTexel(rhsWideTex, coord).x : loadTexel(rhsTex, coord).x; float pressure = texelFetch(pressureTex, coord, 0).x; const ivec2 offsets[4] = ivec2[4](ivec2(-1, 0), ivec2(1, 0), ivec2(0, -1), ivec2(0, 1)); float pressure_sum = 0.0; @@ -93,13 +101,13 @@ vec4 residualStats(ivec2 coord) { if (blockedCell(neighbor)) { // Water's sand walls leak (see pressure_cg.comp); the gas solve has none. if ((useLiquidMask != 0 || porousSand != 0) && neighbor.x >= 0 && neighbor.y >= 0 && neighbor.x < gridSize.x && - neighbor.y < gridSize.y && imageLoad(boundaryTex, neighbor).r >= 2u) { + neighbor.y < gridSize.y && loadTexel(boundaryTex, neighbor).r >= 2u) { diagonal += sandLeak; } continue; } if (useLiquidMask != 0) { - float phi = imageLoad(liquidPhiTex, neighbor).x; + float phi = loadTexel(liquidPhiTex, neighbor).x; invalid_input = invalid_input || invalid(phi); if (phi >= liquidPhiThreshold) { diagonal += airConductance(cellPhi, phi); diff --git a/shaders/render_extract.comp b/shaders/render_extract.comp index 1ab4398..5800544 100644 --- a/shaders/render_extract.comp +++ b/shaders/render_extract.comp @@ -2,10 +2,10 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(rg16f, binding = 0) uniform readonly image2D gasVelocityTex; -layout(r16f, binding = 1) uniform readonly image2D smokeDensityTex; -layout(r16f, binding = 2) uniform readonly image2D temperatureTex; -layout(rg16f, binding = 3) uniform readonly image2D combustionTex; +layout(binding = 0) uniform sampler2D gasVelocityTex; +layout(binding = 1) uniform sampler2D smokeDensityTex; +layout(binding = 2) uniform sampler2D temperatureTex; +layout(binding = 3) uniform sampler2D combustionTex; layout(rg16f, binding = 4) uniform writeonly image2D renderGasVelocityTex; layout(r16f, binding = 5) uniform writeonly image2D renderSmokeDensityTex; layout(r16f, binding = 6) uniform writeonly image2D renderTemperatureTex; @@ -14,6 +14,11 @@ layout(r16f, binding = 7) uniform writeonly image2D renderFireEmissiveTex; uniform ivec2 fullSize; uniform ivec2 gasSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} + ivec2 clampGasCoord(ivec2 coord) { return clamp(coord, ivec2(0), gasSize - ivec2(1)); } @@ -22,10 +27,10 @@ float sampleSmokeLinear(vec2 coord) { vec2 samplePos = clamp(coord, vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(samplePos)); vec2 frac = fract(samplePos); - float s00 = imageLoad(smokeDensityTex, clampGasCoord(base)).x; - float s10 = imageLoad(smokeDensityTex, clampGasCoord(base + ivec2(1, 0))).x; - float s01 = imageLoad(smokeDensityTex, clampGasCoord(base + ivec2(0, 1))).x; - float s11 = imageLoad(smokeDensityTex, clampGasCoord(base + ivec2(1, 1))).x; + float s00 = loadTexel(smokeDensityTex, clampGasCoord(base)).x; + float s10 = loadTexel(smokeDensityTex, clampGasCoord(base + ivec2(1, 0))).x; + float s01 = loadTexel(smokeDensityTex, clampGasCoord(base + ivec2(0, 1))).x; + float s11 = loadTexel(smokeDensityTex, clampGasCoord(base + ivec2(1, 1))).x; return mix(mix(s00, s10, frac.x), mix(s01, s11, frac.x), frac.y); } @@ -33,10 +38,10 @@ float sampleTemperatureLinear(vec2 coord) { vec2 samplePos = clamp(coord, vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(samplePos)); vec2 frac = fract(samplePos); - float s00 = imageLoad(temperatureTex, clampGasCoord(base)).x; - float s10 = imageLoad(temperatureTex, clampGasCoord(base + ivec2(1, 0))).x; - float s01 = imageLoad(temperatureTex, clampGasCoord(base + ivec2(0, 1))).x; - float s11 = imageLoad(temperatureTex, clampGasCoord(base + ivec2(1, 1))).x; + float s00 = loadTexel(temperatureTex, clampGasCoord(base)).x; + float s10 = loadTexel(temperatureTex, clampGasCoord(base + ivec2(1, 0))).x; + float s01 = loadTexel(temperatureTex, clampGasCoord(base + ivec2(0, 1))).x; + float s11 = loadTexel(temperatureTex, clampGasCoord(base + ivec2(1, 1))).x; return mix(mix(s00, s10, frac.x), mix(s01, s11, frac.x), frac.y); } @@ -44,10 +49,10 @@ vec2 sampleGasVelocityLinear(vec2 coord) { vec2 samplePos = clamp(coord, vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(samplePos)); vec2 frac = fract(samplePos); - vec2 s00 = imageLoad(gasVelocityTex, clampGasCoord(base)).xy; - vec2 s10 = imageLoad(gasVelocityTex, clampGasCoord(base + ivec2(1, 0))).xy; - vec2 s01 = imageLoad(gasVelocityTex, clampGasCoord(base + ivec2(0, 1))).xy; - vec2 s11 = imageLoad(gasVelocityTex, clampGasCoord(base + ivec2(1, 1))).xy; + vec2 s00 = loadTexel(gasVelocityTex, clampGasCoord(base)).xy; + vec2 s10 = loadTexel(gasVelocityTex, clampGasCoord(base + ivec2(1, 0))).xy; + vec2 s01 = loadTexel(gasVelocityTex, clampGasCoord(base + ivec2(0, 1))).xy; + vec2 s11 = loadTexel(gasVelocityTex, clampGasCoord(base + ivec2(1, 1))).xy; return mix(mix(s00, s10, frac.x), mix(s01, s11, frac.x), frac.y); } @@ -55,10 +60,10 @@ vec2 sampleCombustionLinear(vec2 coord) { vec2 samplePos = clamp(coord, vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(samplePos)); vec2 frac = fract(samplePos); - vec2 s00 = imageLoad(combustionTex, clampGasCoord(base)).xy; - vec2 s10 = imageLoad(combustionTex, clampGasCoord(base + ivec2(1, 0))).xy; - vec2 s01 = imageLoad(combustionTex, clampGasCoord(base + ivec2(0, 1))).xy; - vec2 s11 = imageLoad(combustionTex, clampGasCoord(base + ivec2(1, 1))).xy; + vec2 s00 = loadTexel(combustionTex, clampGasCoord(base)).xy; + vec2 s10 = loadTexel(combustionTex, clampGasCoord(base + ivec2(1, 0))).xy; + vec2 s01 = loadTexel(combustionTex, clampGasCoord(base + ivec2(0, 1))).xy; + vec2 s11 = loadTexel(combustionTex, clampGasCoord(base + ivec2(1, 1))).xy; return mix(mix(s00, s10, frac.x), mix(s01, s11, frac.x), frac.y); } diff --git a/shaders/sand_grid_update.comp b/shaders/sand_grid_update.comp index d35e601..549814a 100644 --- a/shaders/sand_grid_update.comp +++ b/shaders/sand_grid_update.comp @@ -2,14 +2,14 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D boundarySdfTex; -layout(r16f, binding = 1) uniform readonly image2D waterAmountTex; -layout(rg16f, binding = 2) uniform readonly image2D waterVelocityTex; +layout(binding = 0) uniform sampler2D boundarySdfTex; +layout(binding = 1) uniform sampler2D waterAmountTex; +layout(binding = 2) uniform sampler2D waterVelocityTex; layout(rg32f, binding = 3) uniform writeonly image2D sandVelocityTex; // Sleeping grains per cell (sand_pack.comp): a packed cell is a wall to the moving sand. -layout(r8ui, binding = 4) uniform readonly uimage2D packedTex; +layout(binding = 4) uniform usampler2D packedTex; // Mean volume ratio J of a packed cell's grains (.x, sand_pack.comp). -layout(rgba32f, binding = 5) uniform readonly image2D packedFTex; +layout(binding = 5) uniform sampler2D packedFTex; // sand_particle_p2g.comp's deposit tiles; each workgroup is one tile (nodeOrigin is tile-aligned) and resets its flag. layout(std430, binding = 3) buffer SandTileFlags { @@ -63,6 +63,14 @@ uniform float referenceDensity; uniform float frictionAlpha; uniform float cohesion; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + int linearIndex(ivec2 coord) { return coord.y * (gridSize.x + 2) + coord.x; } @@ -74,7 +82,7 @@ ivec2 clampCell(ivec2 coord) { // Static walls and packed sand as -0.5, open cells as 0.5; the domain edge is handled separately. float packedField(ivec2 cell) { ivec2 c = clampCell(cell); - return imageLoad(boundarySdfTex, c).x <= 0.0 || imageLoad(packedTex, c).r > 0u ? -0.5 : 0.5; + return loadTexel(boundarySdfTex, c).x <= 0.0 || loadTexel(packedTex, c).r > 0u ? -0.5 : 0.5; } bool invalid(float value) { @@ -101,9 +109,9 @@ vec2 packedImpulse(ivec2 node, out vec2 contact, out float bond) { for (int i = -1; i <= 1; ++i) { ivec2 c = node + ivec2(i, j); if (any(lessThan(c, ivec2(0))) || any(greaterThanEqual(c, gridSize))) continue; - uint count = imageLoad(packedTex, c).r; + uint count = loadTexel(packedTex, c).r; if (count == 0u) continue; - vec4 mean = imageLoad(packedFTex, c); + vec4 mean = loadTexel(packedFTex, c); float stress = (lameLambda + shearModulus) * min(log(max(mean.x, 1e-3)), 0.0); ivec2 o = ivec2(1) - ivec2(i, j); // the node's offset from the cell, as an index into the averages vec2 gradient = vec2(kGradient[o.x] * kWeight[o.y], kWeight[o.x] * kGradient[o.y]); @@ -145,7 +153,7 @@ void main() { bool halo = any(lessThan(cell, ivec2(0))) || any(greaterThanEqual(cell, gridSize)); int index = linearIndex(coord); uint massAccum = gridMass[index]; - float sdf = halo ? 1.0 : imageLoad(boundarySdfTex, cell).x; + float sdf = halo ? 1.0 : loadTexel(boundarySdfTex, cell).x; if (massAccum == 0u) { // A far stencil weight can round to zero mass but not to zero momentum. gridMomentumX[index] = ivec2(0); @@ -168,11 +176,11 @@ void main() { for (int j = -1; j <= 1; ++j) { for (int i = -1; i <= 1; ++i) { ivec2 sampleCell = clampCell(cell + ivec2(i, j)); - float amount = imageLoad(waterAmountTex, sampleCell).x; + float amount = loadTexel(waterAmountTex, sampleCell).x; if (amount > 0.0) { wettest = max(wettest, amount); volume += amount; - flow += amount * imageLoad(waterVelocityTex, sampleCell).xy; + flow += amount * loadTexel(waterVelocityTex, sampleCell).xy; } } } @@ -214,10 +222,10 @@ void main() { if (cell.y < 0) separate(velocity, vec2(0.0, 1.0)); if (cell.y >= gridSize.y) separate(velocity, vec2(0.0, -1.0)); if (!halo && sdf <= 0.0) { - float left = imageLoad(boundarySdfTex, clampCell(cell + ivec2(-1, 0))).x; - float right = imageLoad(boundarySdfTex, clampCell(cell + ivec2(1, 0))).x; - float down = imageLoad(boundarySdfTex, clampCell(cell + ivec2(0, -1))).x; - float up = imageLoad(boundarySdfTex, clampCell(cell + ivec2(0, 1))).x; + float left = loadTexel(boundarySdfTex, clampCell(cell + ivec2(-1, 0))).x; + float right = loadTexel(boundarySdfTex, clampCell(cell + ivec2(1, 0))).x; + float down = loadTexel(boundarySdfTex, clampCell(cell + ivec2(0, -1))).x; + float up = loadTexel(boundarySdfTex, clampCell(cell + ivec2(0, 1))).x; vec2 normal = vec2(right - left, up - down); if (length(normal) > 0.00001) { separate(velocity, normalize(normal)); @@ -237,7 +245,7 @@ void main() { } if (!openX && !openY) velocity = vec2(0.0); } - } else if (!halo && imageLoad(packedTex, cell).r > 0u) { + } else if (!halo && loadTexel(packedTex, cell).r > 0u) { vec2 normal = vec2(packedField(cell + ivec2(1, 0)) - packedField(cell + ivec2(-1, 0)), packedField(cell + ivec2(0, 1)) - packedField(cell + ivec2(0, -1))); if (length(normal) > 0.0) separate(velocity, normalize(normal)); diff --git a/shaders/sand_pack.comp b/shaders/sand_pack.comp index 2c18d12..eb8b892 100644 --- a/shaders/sand_pack.comp +++ b/shaders/sand_pack.comp @@ -9,10 +9,10 @@ layout(r8ui, binding = 0) uniform uimage2D packedTex; // Mean volume ratio J (.x), hardening fraction (.y), moisture (.z) and bond (.w, the mean of exp(-q / softening)) of a // cell's sleeping grains, restored when they wake. 32-bit: a tick's drying is below the half-float step of the moisture. layout(rgba32f, binding = 1) uniform image2D packedFTex; -layout(r16f, binding = 2) uniform readonly image2D boundarySdfTex; -layout(r16f, binding = 3) uniform readonly image2D waterAmountTex; -layout(rg16f, binding = 4) uniform readonly image2D waterVelocityTex; -layout(rg32f, binding = 5) uniform readonly image2D sandVelocityTex; +layout(binding = 2) uniform sampler2D boundarySdfTex; +layout(binding = 3) uniform sampler2D waterAmountTex; +layout(binding = 4) uniform sampler2D waterVelocityTex; +layout(binding = 5) uniform sampler2D sandVelocityTex; struct SandParticle { vec4 state0; @@ -67,6 +67,11 @@ uniform float dt; uniform int maxParticles; uniform uint seed; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} + const uint kWake = 1u << 30; const uint kPack = 1u << 31; @@ -80,7 +85,7 @@ int cellIndex(ivec2 c) { // Outside the domain is solid, like a painted wall (the grid separates grains at the halo nodes). bool solidAt(ivec2 c) { - return !inside(c) || imageLoad(boundarySdfTex, c).x <= 0.0; + return !inside(c) || loadTexel(boundarySdfTex, c).x <= 0.0; } bool packedAt(ivec2 c) { @@ -104,7 +109,7 @@ bool open(ivec2 c) { vec2 nodeVelocity(ivec2 c) { // Sand grid nodes sit at cell centres with a one-node halo. - return imageLoad(sandVelocityTex, clamp(c, ivec2(-1), gridSize) + ivec2(1)).xy; + return loadTexel(sandVelocityTex, clamp(c, ivec2(-1), gridSize) + ivec2(1)).xy; } // Gives way under a packed neighbour: open, or moving sand falling faster than sleepSpeed (a shaft's collapsing column @@ -131,8 +136,8 @@ bool disturbed(ivec2 c) { // (sand_grid_update.comp's packed reaction cancels only up to their stored stress). Below it, grains rolling // down a settled slope (up to ~10 cells/s) set off wake cascades. if ((i != 0 || j != 0) && !packedAt(n) && !solidAt(n) && dot(v, normalize(vec2(-i, -j))) > pressSpeed) return true; - if (inside(n) && imageLoad(waterAmountTex, n).x >= 0.5 * waterFullVolume && - length(imageLoad(waterVelocityTex, n).xy) > waterSpeed) return true; + if (inside(n) && loadTexel(waterAmountTex, n).x >= 0.5 * waterFullVolume && + length(loadTexel(waterVelocityTex, n).xy) > waterSpeed) return true; } } return false; @@ -222,7 +227,7 @@ void apply(ivec2 c) { atomicAdd(particleOverflowBlocked, 1u); return; } - vec2 velocity = imageLoad(sandVelocityTex, c + ivec2(1)).xy; + vec2 velocity = loadTexel(sandVelocityTex, c + ivec2(1)).xy; if (!any(isnan(velocity)) && !any(isinf(velocity))) atomicMax(fastestSpeedBits, floatBitsToUint(length(velocity))); atomicMax(boxMinX, 0xffffffffu - uint(c.x)); atomicMax(boxMinY, 0xffffffffu - uint(c.y)); @@ -257,7 +262,7 @@ void apply(ivec2 c) { float fill = 0.0; for (int j = -1; j <= 1; ++j) { for (int i = -1; i <= 1; ++i) { - fill = max(fill, imageLoad(waterAmountTex, clamp(c + ivec2(i, j), ivec2(0), gridSize - ivec2(1))).x); + fill = max(fill, loadTexel(waterAmountTex, clamp(c + ivec2(i, j), ivec2(0), gridSize - ivec2(1))).x); } } float saturation = clamp(fill / waterFullVolume, 0.0, 1.0); diff --git a/shaders/sand_particle_spawn.comp b/shaders/sand_particle_spawn.comp index ce93d00..146ae35 100644 --- a/shaders/sand_particle_spawn.comp +++ b/shaders/sand_particle_spawn.comp @@ -2,8 +2,8 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16ui, binding = 0) uniform readonly uimage2D materialTex; -layout(r16f, binding = 1) uniform readonly image2D sandOccupancyTex; +layout(binding = 0) uniform usampler2D materialTex; +layout(binding = 1) uniform sampler2D sandOccupancyTex; struct SandParticle { vec4 state0; @@ -42,6 +42,14 @@ uniform ivec2 skipCenter; uniform uint spawnSeed; uniform float velocitySpread; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // PCG-style integer hash: per-particle jitter that varies with the cell, the particle and the tick (spawnSeed). uint hashBits(uvec3 v) { uint h = v.x * 1664525u + v.y * 22695477u + v.z * 2654435761u + spawnSeed * 2246822519u; @@ -76,10 +84,10 @@ void main() { if (!insideBrush(coord) || alreadyStamped(coord)) { return; } - if (blockedBySolid == 1 && imageLoad(materialTex, coord).r >= 2u) { + if (blockedBySolid == 1 && loadTexel(materialTex, coord).r >= 2u) { return; } - if (imageLoad(sandOccupancyTex, coord).x >= occupancyLimit) { + if (loadTexel(sandOccupancyTex, coord).x >= occupancyLimit) { return; } diff --git a/shaders/sand_particle_step.comp b/shaders/sand_particle_step.comp index 282e695..54703e2 100644 --- a/shaders/sand_particle_step.comp +++ b/shaders/sand_particle_step.comp @@ -2,11 +2,11 @@ layout(local_size_x = 256) in; -layout(r16f, binding = 0) uniform readonly image2D boundarySdfTex; -layout(rg32f, binding = 1) uniform readonly image2D sandVelocityTex; +layout(binding = 0) uniform sampler2D boundarySdfTex; +layout(binding = 1) uniform sampler2D sandVelocityTex; // Sleeping grains per cell (sand_pack.comp): moving grains collide with packed cells like with walls. -layout(r8ui, binding = 2) uniform readonly uimage2D packedTex; -layout(r16f, binding = 3) uniform readonly image2D waterAmountTex; +layout(binding = 2) uniform usampler2D packedTex; +layout(binding = 3) uniform sampler2D waterAmountTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -64,9 +64,17 @@ uniform float dryingRate; uniform float moistureDt; uniform float waterFullVolume; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool blockedCell(ivec2 cell) { return cell.x < 0 || cell.y < 0 || cell.x >= gridSize.x || cell.y >= gridSize.y || - imageLoad(boundarySdfTex, cell).x <= 0.0 || imageLoad(packedTex, cell).r > 0u; + loadTexel(boundarySdfTex, cell).x <= 0.0 || loadTexel(packedTex, cell).r > 0u; } float sweepGrid(vec2 position, vec2 displacement, out vec2 hitNormal, out ivec2 lastCell) { @@ -204,7 +212,7 @@ mat2 gatherGridVelocity(vec2 position, out vec2 velocity, out mat2 velocityGradi continue; } float weight = wx[i] * wy[j]; - vec2 value = imageLoad(sandVelocityTex, cell + ivec2(1)).xy; + vec2 value = loadTexel(sandVelocityTex, cell + ivec2(1)).xy; gridMass[(cell.y + 1) * (gridSize.x + 2) + cell.x + 1] = 0u; vec2 delta = vec2(cell) + vec2(0.5) - position; velocity += weight * value; @@ -332,6 +340,7 @@ const float kHardeningScale = 4.0; // Varies per substep (the host passes a counter), so the same grain rounds differently each time. uniform uint roundSeed; + // timer + fraction as a float. Where the float's step is coarse (timer 16 and up: grains that have been still a while, // the sleep timer counts to 255, step 1.5e-5), increments below half a step would round away every substep and slow // creep would never harden. There the sum is rounded stochastically: the exact error of the nearest float (timer >= 1 > @@ -404,7 +413,7 @@ void main() { float fill = 0.0; for (int j = -1; j <= 1; ++j) { for (int i = -1; i <= 1; ++i) { - fill = max(fill, imageLoad(waterAmountTex, clamp(cell + ivec2(i, j), ivec2(0), gridSize - ivec2(1))).x); + fill = max(fill, loadTexel(waterAmountTex, clamp(cell + ivec2(i, j), ivec2(0), gridSize - ivec2(1))).x); } } float saturation = clamp(fill / waterFullVolume, 0.0, 1.0); diff --git a/shaders/sand_rasterize.comp b/shaders/sand_rasterize.comp index 7b5e6cf..f82b536 100644 --- a/shaders/sand_rasterize.comp +++ b/shaders/sand_rasterize.comp @@ -4,11 +4,11 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r16f, binding = 0) uniform writeonly image2D sandOccupancyTex; // Sleeping grains per cell (sand_pack.comp). -layout(r8ui, binding = 1) uniform readonly uimage2D packedTex; +layout(binding = 1) uniform usampler2D packedTex; // Mean moisture of the cell's sand (moving and packed), drawn darker by composite.frag. layout(r8, binding = 2) uniform writeonly image2D sandMoistureTex; // Sleeping grains' mean stretch; .z is their mean moisture (sand_pack.comp). -layout(rgba32f, binding = 3) uniform readonly image2D packedFTex; +layout(binding = 3) uniform sampler2D packedFTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -30,6 +30,14 @@ uniform ivec2 gridSize; uniform float massScale; uniform float referenceDensity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + int linearIndex(ivec2 coord) { return (coord.y + 1) * (gridSize.x + 2) + coord.x + 1; } @@ -54,11 +62,11 @@ void main() { for (int i = -1; i <= 1; ++i) { ivec2 n = coord + ivec2(i, j); if (all(greaterThanEqual(n, ivec2(0))) && all(lessThan(n, gridSize))) { - uint count = imageLoad(packedTex, n).r; + uint count = loadTexel(packedTex, n).r; if (count > 0u) { float mass = kernel[i + 1] * kernel[j + 1] * float(count); packedMass += mass; - packedMoisture += mass * imageLoad(packedFTex, n).z; + packedMoisture += mass * loadTexel(packedFTex, n).z; } } } diff --git a/shaders/smoke_advect.comp b/shaders/smoke_advect.comp index ad6f795..9d117a2 100644 --- a/shaders/smoke_advect.comp +++ b/shaders/smoke_advect.comp @@ -2,34 +2,42 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(rg16f, binding = 0) uniform readonly image2D gasVelocityTex; -layout(r16f, binding = 1) uniform readonly image2D smokeDenSrc; -layout(r16f, binding = 2) uniform readonly image2D temperatureSrc; -layout(r16f, binding = 3) uniform readonly image2D fuelSrc; -layout(r16f, binding = 4) uniform readonly image2D oxidizerSrc; -layout(rg16f, binding = 5) uniform readonly image2D reactionSrc; -layout(r16f, binding = 6) uniform writeonly image2D smokeDenDst; -layout(r16f, binding = 7) uniform writeonly image2D temperatureDst; -layout(r16f, binding = 8) uniform writeonly image2D fuelDst; -layout(r16f, binding = 9) uniform writeonly image2D oxidizerDst; -layout(rg16f, binding = 10) uniform writeonly image2D reactionDst; -layout(r8ui, binding = 11) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform sampler2D gasVelocityTex; +layout(binding = 1) uniform sampler2D smokeDenSrc; +layout(binding = 2) uniform sampler2D temperatureSrc; +layout(binding = 3) uniform sampler2D fuelSrc; +layout(binding = 4) uniform sampler2D oxidizerSrc; +layout(binding = 5) uniform sampler2D reactionSrc; +layout(r16f, binding = 0) uniform writeonly image2D smokeDenDst; +layout(r16f, binding = 1) uniform writeonly image2D temperatureDst; +layout(r16f, binding = 2) uniform writeonly image2D fuelDst; +layout(r16f, binding = 3) uniform writeonly image2D oxidizerDst; +layout(rg16f, binding = 4) uniform writeonly image2D reactionDst; +layout(binding = 11) uniform usampler2D boundaryTex; uniform ivec2 gasSize; uniform float dt; uniform float maxVelocity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + ivec2 clampCoord(ivec2 coord) { return clamp(coord, ivec2(0), gasSize - ivec2(1)); } bool blocked(ivec2 coord) { - return imageLoad(boundaryTex, clampCoord(coord)).r != 0u; + return loadTexel(boundaryTex, clampCoord(coord)).r != 0u; } bool blockedCell(ivec2 cell) { return cell.x < 0 || cell.y < 0 || cell.x >= gasSize.x || cell.y >= gasSize.y || - imageLoad(boundaryTex, cell).r != 0u; + loadTexel(boundaryTex, cell).r != 0u; } struct TransportStencil { @@ -100,40 +108,40 @@ vec2 sampleVelocity(vec2 position) { vec2 p = clamp(position - vec2(0.5), vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(p)); vec2 frac = fract(p); - vec2 v00 = imageLoad(gasVelocityTex, base).xy; - vec2 v10 = imageLoad(gasVelocityTex, clampCoord(base + ivec2(1, 0))).xy; - vec2 v01 = imageLoad(gasVelocityTex, clampCoord(base + ivec2(0, 1))).xy; - vec2 v11 = imageLoad(gasVelocityTex, clampCoord(base + ivec2(1, 1))).xy; + vec2 v00 = loadTexel(gasVelocityTex, base).xy; + vec2 v10 = loadTexel(gasVelocityTex, clampCoord(base + ivec2(1, 0))).xy; + vec2 v01 = loadTexel(gasVelocityTex, clampCoord(base + ivec2(0, 1))).xy; + vec2 v11 = loadTexel(gasVelocityTex, clampCoord(base + ivec2(1, 1))).xy; return clamp(mix(mix(v00, v10, frac.x), mix(v01, v11, frac.x), frac.y), vec2(-maxVelocity), vec2(maxVelocity)); } float sampleSmoke(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(smokeDenSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(smokeDenSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } float sampleTemperature(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(temperatureSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(temperatureSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } float sampleFuel(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(fuelSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(fuelSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } float sampleOxidizer(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(oxidizerSrc, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(oxidizerSrc, stencil.cells[i]).x; return dot(stencil.weights, values); } vec2 sampleReaction(TransportStencil stencil) { vec2 value = vec2(0.0); - for (int i = 0; i < 4; ++i) value += stencil.weights[i] * imageLoad(reactionSrc, stencil.cells[i]).xy; + for (int i = 0; i < 4; ++i) value += stencil.weights[i] * loadTexel(reactionSrc, stencil.cells[i]).xy; return value; } diff --git a/shaders/smoke_correct.comp b/shaders/smoke_correct.comp index 386e0fa..bcba93d 100644 --- a/shaders/smoke_correct.comp +++ b/shaders/smoke_correct.comp @@ -2,24 +2,24 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(rg16f, binding = 0) uniform readonly image2D gasVelocitySrc; -layout(rg16f, binding = 1) uniform readonly image2D gasVelocityFwd; -layout(r16f, binding = 2) uniform readonly image2D smokeDenSrc; -layout(r16f, binding = 3) uniform readonly image2D temperatureSrc; -layout(r16f, binding = 4) uniform readonly image2D fuelSrc; -layout(r16f, binding = 5) uniform readonly image2D oxidizerSrc; -layout(rg16f, binding = 6) uniform readonly image2D reactionSrc; -layout(r16f, binding = 7) uniform readonly image2D smokeDenFwd; -layout(r16f, binding = 8) uniform readonly image2D temperatureFwd; -layout(r16f, binding = 9) uniform readonly image2D fuelFwd; -layout(r16f, binding = 10) uniform readonly image2D oxidizerFwd; -layout(rg16f, binding = 11) uniform readonly image2D reactionFwd; -layout(r16f, binding = 12) uniform writeonly image2D smokeDenDst; -layout(r16f, binding = 13) uniform writeonly image2D temperatureDst; -layout(r16f, binding = 14) uniform writeonly image2D fuelDst; -layout(r16f, binding = 15) uniform writeonly image2D oxidizerDst; -layout(rg16f, binding = 16) uniform writeonly image2D reactionDst; -layout(r8ui, binding = 17) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform sampler2D gasVelocitySrc; +layout(binding = 1) uniform sampler2D gasVelocityFwd; +layout(binding = 2) uniform sampler2D smokeDenSrc; +layout(binding = 3) uniform sampler2D temperatureSrc; +layout(binding = 4) uniform sampler2D fuelSrc; +layout(binding = 5) uniform sampler2D oxidizerSrc; +layout(binding = 6) uniform sampler2D reactionSrc; +layout(binding = 7) uniform sampler2D smokeDenFwd; +layout(binding = 8) uniform sampler2D temperatureFwd; +layout(binding = 9) uniform sampler2D fuelFwd; +layout(binding = 10) uniform sampler2D oxidizerFwd; +layout(binding = 11) uniform sampler2D reactionFwd; +layout(r16f, binding = 0) uniform writeonly image2D smokeDenDst; +layout(r16f, binding = 1) uniform writeonly image2D temperatureDst; +layout(r16f, binding = 2) uniform writeonly image2D fuelDst; +layout(r16f, binding = 3) uniform writeonly image2D oxidizerDst; +layout(rg16f, binding = 4) uniform writeonly image2D reactionDst; +layout(binding = 17) uniform usampler2D boundaryTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -34,17 +34,25 @@ uniform float maxVelocity; uniform float densityDissipation; uniform float scalarDissipation; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + ivec2 clampCoord(ivec2 coord) { return clamp(coord, ivec2(0), gasSize - ivec2(1)); } bool blocked(ivec2 coord) { - return imageLoad(boundaryTex, clampCoord(coord)).r != 0u; + return loadTexel(boundaryTex, clampCoord(coord)).r != 0u; } bool blockedCell(ivec2 cell) { return cell.x < 0 || cell.y < 0 || cell.x >= gasSize.x || cell.y >= gasSize.y || - imageLoad(boundaryTex, cell).r != 0u; + loadTexel(boundaryTex, cell).r != 0u; } struct TransportStencil { @@ -115,10 +123,10 @@ vec2 sampleVelocitySrc(vec2 position) { vec2 p = clamp(position - vec2(0.5), vec2(0.0), vec2(gasSize) - vec2(1.0)); ivec2 base = ivec2(floor(p)); vec2 frac = fract(p); - vec2 v00 = imageLoad(gasVelocitySrc, base).xy; - vec2 v10 = imageLoad(gasVelocitySrc, clampCoord(base + ivec2(1, 0))).xy; - vec2 v01 = imageLoad(gasVelocitySrc, clampCoord(base + ivec2(0, 1))).xy; - vec2 v11 = imageLoad(gasVelocitySrc, clampCoord(base + ivec2(1, 1))).xy; + vec2 v00 = loadTexel(gasVelocitySrc, base).xy; + vec2 v10 = loadTexel(gasVelocitySrc, clampCoord(base + ivec2(1, 0))).xy; + vec2 v01 = loadTexel(gasVelocitySrc, clampCoord(base + ivec2(0, 1))).xy; + vec2 v11 = loadTexel(gasVelocitySrc, clampCoord(base + ivec2(1, 1))).xy; return clamp(mix(mix(v00, v10, frac.x), mix(v01, v11, frac.x), frac.y), vec2(-maxVelocity), vec2(maxVelocity)); } @@ -127,7 +135,7 @@ vec2 sampleVelocitySrc(vec2 position) { float sampleSmokeFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(smokeDenFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(smokeDenFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -135,7 +143,7 @@ float sampleSmokeFwd(TransportStencil stencil) { float sampleTemperatureFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(temperatureFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(temperatureFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -143,7 +151,7 @@ float sampleTemperatureFwd(TransportStencil stencil) { float sampleFuelFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(fuelFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(fuelFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -151,7 +159,7 @@ float sampleFuelFwd(TransportStencil stencil) { float sampleOxidizerFwd(TransportStencil stencil) { vec4 values; - for (int i = 0; i < 4; ++i) values[i] = imageLoad(oxidizerFwd, stencil.cells[i]).x; + for (int i = 0; i < 4; ++i) values[i] = loadTexel(oxidizerFwd, stencil.cells[i]).x; return dot(stencil.weights, values); } @@ -159,7 +167,7 @@ float sampleOxidizerFwd(TransportStencil stencil) { vec2 sampleReactionFwd(TransportStencil stencil) { vec2 value = vec2(0.0); - for (int i = 0; i < 4; ++i) value += stencil.weights[i] * imageLoad(reactionFwd, stencil.cells[i]).xy; + for (int i = 0; i < 4; ++i) value += stencil.weights[i] * loadTexel(reactionFwd, stencil.cells[i]).xy; return value; } @@ -167,7 +175,7 @@ float clampLocalSmoke(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(smokeDenSrc, stencil.cells[i]).x; + float donor = loadTexel(smokeDenSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -179,7 +187,7 @@ float clampLocalTemperature(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(temperatureSrc, stencil.cells[i]).x; + float donor = loadTexel(temperatureSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -191,7 +199,7 @@ float clampLocalFuel(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(fuelSrc, stencil.cells[i]).x; + float donor = loadTexel(fuelSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -203,7 +211,7 @@ float clampLocalOxidizer(TransportStencil stencil, float value) { float minV = float(1e30), maxV = float(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - float donor = imageLoad(oxidizerSrc, stencil.cells[i]).x; + float donor = loadTexel(oxidizerSrc, stencil.cells[i]).x; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -215,7 +223,7 @@ vec2 clampLocalReaction(TransportStencil stencil, vec2 value) { vec2 minV = vec2(1e30), maxV = vec2(-1e30); for (int i = 0; i < 4; ++i) { if (stencil.weights[i] > 0.0) { - vec2 donor = imageLoad(reactionSrc, stencil.cells[i]).xy; + vec2 donor = loadTexel(reactionSrc, stencil.cells[i]).xy; minV = min(minV, donor); maxV = max(maxV, donor); } @@ -248,14 +256,14 @@ void main() { TransportStencil backStencil = transportStencil(center, backtrace, vec2(0.5), gasSize - ivec2(1), -1); TransportStencil reverseStencil = transportStencil(center, reverseTrace, vec2(0.5), gasSize - ivec2(1), -1); - float smoke = imageLoad(smokeDenFwd, coord).x + 0.5 * (imageLoad(smokeDenSrc, coord).x - sampleSmokeFwd(reverseStencil)); + float smoke = loadTexel(smokeDenFwd, coord).x + 0.5 * (loadTexel(smokeDenSrc, coord).x - sampleSmokeFwd(reverseStencil)); float temperature = - imageLoad(temperatureFwd, coord).x + 0.5 * (imageLoad(temperatureSrc, coord).x - sampleTemperatureFwd(reverseStencil)); - float fuel = imageLoad(fuelFwd, coord).x + 0.5 * (imageLoad(fuelSrc, coord).x - sampleFuelFwd(reverseStencil)); + loadTexel(temperatureFwd, coord).x + 0.5 * (loadTexel(temperatureSrc, coord).x - sampleTemperatureFwd(reverseStencil)); + float fuel = loadTexel(fuelFwd, coord).x + 0.5 * (loadTexel(fuelSrc, coord).x - sampleFuelFwd(reverseStencil)); float oxidizer = - imageLoad(oxidizerFwd, coord).x + 0.5 * (imageLoad(oxidizerSrc, coord).x - sampleOxidizerFwd(reverseStencil)); + loadTexel(oxidizerFwd, coord).x + 0.5 * (loadTexel(oxidizerSrc, coord).x - sampleOxidizerFwd(reverseStencil)); vec2 reaction = - imageLoad(reactionFwd, coord).xy + 0.5 * (imageLoad(reactionSrc, coord).xy - sampleReactionFwd(reverseStencil)); + loadTexel(reactionFwd, coord).xy + 0.5 * (loadTexel(reactionSrc, coord).xy - sampleReactionFwd(reverseStencil)); // Decay follows the limiter so the original extrema cannot restore it. imageStore(smokeDenDst, coord, vec4(clampLocalSmoke(backStencil, smoke) * densityDissipation, 0.0, 0.0, 0.0)); diff --git a/shaders/smoke_divergence.comp b/shaders/smoke_divergence.comp index 56736a3..115854d 100644 --- a/shaders/smoke_divergence.comp +++ b/shaders/smoke_divergence.comp @@ -2,33 +2,41 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D gasUTex; -layout(r32f, binding = 1) uniform readonly image2D gasVTex; +layout(binding = 0) uniform sampler2D gasUTex; +layout(binding = 1) uniform sampler2D gasVTex; // R32F like the faces: stored as half floats, the right-hand side of a fire scene was rounded by up to 0.5 /s, twice the // residual gate, and the projected field kept that error (app-gas-rhs-precision). layout(r32f, binding = 2) uniform writeonly image2D divergenceTex; -layout(r8ui, binding = 3) uniform readonly uimage2D boundaryTex; +layout(binding = 3) uniform usampler2D boundaryTex; // x: fuel burned per second (fire_rd.comp). -layout(rg16f, binding = 4) uniform readonly image2D reactionTex; +layout(binding = 4) uniform sampler2D reactionTex; uniform ivec2 gasSize; // Gas volume released per unit of fuel burned (Nguyen et al. 2002): burning cells are asked to expand. uniform float expansion; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (coord.x >= gasSize.x || coord.y >= gasSize.y) { return; } - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(divergenceTex, coord, vec4(0.0)); return; } - float uL = imageLoad(gasUTex, ivec2(coord.x, coord.y)).x; - float uR = imageLoad(gasUTex, ivec2(coord.x + 1, coord.y)).x; - float vB = imageLoad(gasVTex, ivec2(coord.x, coord.y)).x; - float vT = imageLoad(gasVTex, ivec2(coord.x, coord.y + 1)).x; - float source = expansion * max(imageLoad(reactionTex, coord).x, 0.0); + float uL = loadTexel(gasUTex, ivec2(coord.x, coord.y)).x; + float uR = loadTexel(gasUTex, ivec2(coord.x + 1, coord.y)).x; + float vB = loadTexel(gasVTex, ivec2(coord.x, coord.y)).x; + float vT = loadTexel(gasVTex, ivec2(coord.x, coord.y + 1)).x; + float source = expansion * max(loadTexel(reactionTex, coord).x, 0.0); imageStore(divergenceTex, coord, vec4((uR - uL) + (vT - vB) - source, 0.0, 0.0, 0.0)); } diff --git a/shaders/smoke_project.comp b/shaders/smoke_project.comp index af72380..51ec9b3 100644 --- a/shaders/smoke_project.comp +++ b/shaders/smoke_project.comp @@ -2,12 +2,12 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r32f, binding = 0) uniform readonly image2D gasUSrc; -layout(r32f, binding = 1) uniform readonly image2D gasVSrc; -layout(r32f, binding = 2) uniform readonly image2D pressureTex; +layout(binding = 0) uniform sampler2D gasUSrc; +layout(binding = 1) uniform sampler2D gasVSrc; +layout(binding = 2) uniform sampler2D pressureTex; layout(r32f, binding = 3) uniform writeonly image2D gasUDst; layout(r32f, binding = 4) uniform writeonly image2D gasVDst; -layout(r8ui, binding = 5) uniform readonly uimage2D boundaryTex; +layout(binding = 5) uniform usampler2D boundaryTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -20,24 +20,32 @@ uniform ivec2 gasSize; uniform int openTop; uniform float sandLeak; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + float pressureAt(ivec2 coord) { coord = clamp(coord, ivec2(0), gasSize - ivec2(1)); - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { return 0.0; } - return imageLoad(pressureTex, coord).x; + return loadTexel(pressureTex, coord).x; } bool blockedCell(ivec2 coord) { - return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || imageLoad(boundaryTex, coord).r != 0u; + return coord.x < 0 || coord.y < 0 || coord.x >= gasSize.x || coord.y >= gasSize.y || loadTexel(boundaryTex, coord).r != 0u; } // A face between open gas and dense sand (mask 3) conducts at sandLeak in the pressure operator (gas_cg.comp, // pressure_residual.comp), so its correction carries the same weight; the sand side's pressure is zero. Both cells are // inside the grid (interior faces only). bool sandFace(ivec2 a, ivec2 b) { - uint maskA = imageLoad(boundaryTex, a).r; - uint maskB = imageLoad(boundaryTex, b).r; + uint maskA = loadTexel(boundaryTex, a).r; + uint maskB = loadTexel(boundaryTex, b).r; return min(maskA, maskB) == 0u && max(maskA, maskB) == 3u; } @@ -52,13 +60,13 @@ void main() { float value = 0.0; if (coord.x > 0 && coord.x < gasSize.x && !blockedCell(ivec2(coord.x - 1, coord.y)) && !blockedCell(ivec2(coord.x, coord.y))) { - value = imageLoad(gasUSrc, coord).x - (pressureAt(ivec2(coord.x, coord.y)) - pressureAt(ivec2(coord.x - 1, coord.y))); + value = loadTexel(gasUSrc, coord).x - (pressureAt(ivec2(coord.x, coord.y)) - pressureAt(ivec2(coord.x - 1, coord.y))); if (invalid(value)) { atomicAdd(nanInfDetected, 1u); value = 0.0; } } else if (coord.x > 0 && coord.x < gasSize.x && sandFace(ivec2(coord.x - 1, coord.y), coord)) { - value = imageLoad(gasUSrc, coord).x - sandLeak * (pressureAt(coord) - pressureAt(ivec2(coord.x - 1, coord.y))); + value = loadTexel(gasUSrc, coord).x - sandLeak * (pressureAt(coord) - pressureAt(ivec2(coord.x - 1, coord.y))); if (invalid(value)) { atomicAdd(nanInfDetected, 1u); value = 0.0; @@ -71,20 +79,20 @@ void main() { float value = 0.0; if (coord.y > 0 && coord.y < gasSize.y && !blockedCell(ivec2(coord.x, coord.y - 1)) && !blockedCell(ivec2(coord.x, coord.y))) { - value = imageLoad(gasVSrc, coord).x - (pressureAt(ivec2(coord.x, coord.y)) - pressureAt(ivec2(coord.x, coord.y - 1))); + value = loadTexel(gasVSrc, coord).x - (pressureAt(ivec2(coord.x, coord.y)) - pressureAt(ivec2(coord.x, coord.y - 1))); if (invalid(value)) { atomicAdd(nanInfDetected, 1u); value = 0.0; } } else if (coord.y > 0 && coord.y < gasSize.y && sandFace(ivec2(coord.x, coord.y - 1), coord)) { - value = imageLoad(gasVSrc, coord).x - sandLeak * (pressureAt(coord) - pressureAt(ivec2(coord.x, coord.y - 1))); + value = loadTexel(gasVSrc, coord).x - sandLeak * (pressureAt(coord) - pressureAt(ivec2(coord.x, coord.y - 1))); if (invalid(value)) { atomicAdd(nanInfDetected, 1u); value = 0.0; } } else if (openTop != 0 && coord.y == gasSize.y && !blockedCell(ivec2(coord.x, coord.y - 1))) { // Open top: zero pressure outside, so the top face carries the outflow (or inflow) the solve needs. - value = imageLoad(gasVSrc, coord).x + pressureAt(ivec2(coord.x, coord.y - 1)); + value = loadTexel(gasVSrc, coord).x + pressureAt(ivec2(coord.x, coord.y - 1)); } imageStore(gasVDst, coord, vec4(value, 0.0, 0.0, 0.0)); } diff --git a/shaders/spark_pick.comp b/shaders/spark_pick.comp index 7b6a6ed..be4e44a 100644 --- a/shaders/spark_pick.comp +++ b/shaders/spark_pick.comp @@ -7,10 +7,10 @@ // Key: priority (13 bits) | ember (1 bit) | gas cell index + 1 (18 bits); 0 is an empty bucket. layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D temperatureTex; -layout(rg16f, binding = 1) uniform readonly image2D reactionTex; -layout(r8ui, binding = 2) uniform readonly uimage2D gasMaskTex; -layout(r16ui, binding = 3) uniform readonly uimage2D materialTex; +layout(binding = 0) uniform sampler2D temperatureTex; +layout(binding = 1) uniform sampler2D reactionTex; +layout(binding = 2) uniform usampler2D gasMaskTex; +layout(binding = 3) uniform usampler2D materialTex; layout(std430, binding = 0) buffer Buckets { uint buckets[]; @@ -21,6 +21,14 @@ uniform uint tick; uniform uint bucketCount; uniform int woodPresent; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Per gas cell and tick: chance of a flame spark and of an ember at full burn, and of an ember beside a fully burning // wood neighbourhood. A cell draws one number below kGate or does nothing, so the rest of the cells never read a texture. const float kGate = 0.02; @@ -45,10 +53,10 @@ void main() { } uint h = hash(uvec2(gas), tick); float draw = float(h & 0xFFFFu) * (1.0 / 65536.0); - if (draw >= kGate || imageLoad(gasMaskTex, gas).r != 0u) { + if (draw >= kGate || loadTexel(gasMaskTex, gas).r != 0u) { return; } - float heat = max(imageLoad(temperatureTex, gas).x, imageLoad(reactionTex, gas).y); + float heat = max(loadTexel(temperatureTex, gas).x, loadTexel(reactionTex, gas).y); float burn = smoothstep(0.35, 0.8, heat); float flame = kFlameChance * burn; float ember = kEmberChance * smoothstep(0.45, 0.9, heat); @@ -57,7 +65,7 @@ void main() { float wood = 0.0; for (int y = -1; y < 3; ++y) { for (int x = -1; x < 3; ++x) { - uint material = imageLoad(materialTex, clamp(gas * 2 + ivec2(x, y), ivec2(0), gasSize * 2 - 1)).r; + uint material = loadTexel(materialTex, clamp(gas * 2 + ivec2(x, y), ivec2(0), gasSize * 2 - 1)).r; if (material >= 256u && material < 512u) wood += 1.0 / 16.0; } } diff --git a/shaders/spark_step.comp b/shaders/spark_step.comp index 0928ff0..8e4c050 100644 --- a/shaders/spark_step.comp +++ b/shaders/spark_step.comp @@ -16,9 +16,9 @@ layout(std430, binding = 1) buffer Buckets { uint buckets[]; }; -layout(rg16f, binding = 0) uniform readonly image2D gasVelocityTex; // gas cell centres, gas cells/s -layout(r8ui, binding = 1) uniform readonly uimage2D boundaryTex; // 0 open, 1 rock, 2-3 sand -layout(r16f, binding = 2) uniform readonly image2D waterAmountTex; +layout(binding = 0) uniform sampler2D gasVelocityTex; // gas cell centres, gas cells/s +layout(binding = 1) uniform usampler2D boundaryTex; // 0 open, 1 rock, 2-3 sand +layout(binding = 2) uniform sampler2D waterAmountTex; uniform ivec2 gridSize; uniform ivec2 gasSize; @@ -26,6 +26,15 @@ uniform uint tick; uniform float dt; uniform uint capacity; uniform uint perTick; + +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform float gravity; // cells/s^2 const float kMaxSpeed = 700.0; // cells/s, above the 600 of a gas at its cap @@ -51,8 +60,8 @@ vec2 gasVelocity(vec2 p) { ivec2 b = ivec2(floor(g)); vec2 f = g - vec2(b); ivec2 hi = min(b + 1, gasSize - 1); - vec2 v = mix(mix(imageLoad(gasVelocityTex, b).xy, imageLoad(gasVelocityTex, ivec2(hi.x, b.y)).xy, f.x), - mix(imageLoad(gasVelocityTex, ivec2(b.x, hi.y)).xy, imageLoad(gasVelocityTex, hi).xy, f.x), f.y); + vec2 v = mix(mix(loadTexel(gasVelocityTex, b).xy, loadTexel(gasVelocityTex, ivec2(hi.x, b.y)).xy, f.x), + mix(loadTexel(gasVelocityTex, ivec2(b.x, hi.y)).xy, loadTexel(gasVelocityTex, hi).xy, f.x), f.y); return 2.0 * v; } @@ -61,7 +70,7 @@ uint cellKind(vec2 p) { ivec2 c = ivec2(floor(p)); if (c.y >= gridSize.y) return 2u; if (any(lessThan(c, ivec2(0))) || c.x >= gridSize.x) return 1u; - return min(imageLoad(boundaryTex, c).r, 2u); + return min(loadTexel(boundaryTex, c).r, 2u); } void main() { @@ -147,7 +156,7 @@ void main() { pos = next; } // A hard hit can cool a spark past its life: it dies now rather than being stored older than its life. - if (dead || age >= state.y || imageLoad(waterAmountTex, ivec2(pos)).x > kWaterThreshold) { + if (dead || age >= state.y || loadTexel(waterAmountTex, ivec2(pos)).x > kWaterThreshold) { sparks[2u * i + 1u] = vec4(0.0); return; } diff --git a/shaders/spawn_smoke.comp b/shaders/spawn_smoke.comp index 5e3dce2..2be4c08 100644 --- a/shaders/spawn_smoke.comp +++ b/shaders/spawn_smoke.comp @@ -10,8 +10,8 @@ layout(r16f, binding = 4) uniform image2D temperatureTex; layout(r16f, binding = 5) uniform image2D fuelTex; layout(r16f, binding = 6) uniform image2D oxidizerTex; layout(rg16f, binding = 7) uniform image2D reactionTex; -layout(r16f, binding = 9) uniform readonly image2D waterAmountTex; -layout(r8ui, binding = 10) uniform readonly uimage2D boundaryTex; +layout(binding = 9) uniform sampler2D waterAmountTex; +layout(binding = 10) uniform usampler2D boundaryTex; uniform ivec2 gasSize; uniform ivec2 gridSize; @@ -32,6 +32,14 @@ uniform float maxVelocity; uniform float velocitySpread; uniform uint spawnSeed; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uint hashCell(uvec2 cell, uint salt) { uint h = cell.x * 0x8da6b343u ^ cell.y * 0xd8163841u ^ salt * 0xcb1ab31fu; h ^= h >> 16; @@ -72,7 +80,7 @@ bool overlapsWater(ivec2 coord) { for (int y = fullMin.y; y < fullMax.y; ++y) { for (int x = fullMin.x; x < fullMax.x; ++x) { ivec2 fullCoord = ivec2(x, y); - if (fullCellInsideBrush(fullCoord) && imageLoad(waterAmountTex, fullCoord).x > 0.015) { + if (fullCellInsideBrush(fullCoord) && loadTexel(waterAmountTex, fullCoord).x > 0.015) { return true; } } @@ -126,7 +134,7 @@ void main() { return; } - if (blockedBySolid == 1 && imageLoad(boundaryTex, coord).r != 0u) { + if (blockedBySolid == 1 && loadTexel(boundaryTex, coord).r != 0u) { return; } if (blockedByWater == 1 && overlapsWater(coord)) { diff --git a/shaders/steam_update.comp b/shaders/steam_update.comp index 9b72b45..26785d4 100644 --- a/shaders/steam_update.comp +++ b/shaders/steam_update.comp @@ -8,15 +8,15 @@ // condenses (decays) within a second or two. Steam is presentation and does not push the gas. layout(local_size_x = 16, local_size_y = 16) in; -layout(rg16f, binding = 0) uniform readonly image2D gasVelocityTex; // cell centres, gas cells/s +layout(binding = 0) uniform sampler2D gasVelocityTex; // cell centres, gas cells/s layout(r16f, binding = 1) uniform writeonly image2D steamDst; -layout(r8ui, binding = 2) uniform readonly uimage2D gasMaskTex; +layout(binding = 2) uniform usampler2D gasMaskTex; layout(r16f, binding = 3) uniform image2D fuelTex; layout(r16f, binding = 4) uniform image2D temperatureTex; layout(rg16f, binding = 5) uniform image2D reactionTex; layout(r16f, binding = 6) uniform writeonly image2D oxidizerTex; -uniform sampler2D steamSrc; +layout(binding = 7) uniform sampler2D steamSrc; // Per gas cell: evaporated water records (bits 0-15) and records that flashed on burning oil at most four cells below // (bits 16-31), then lit oil records under it. @@ -30,8 +30,16 @@ uniform float steamRise; // gas cells/s relative to the gas uniform float steamCondense; // 1/s uniform float steamPerParticle; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool blocked(ivec2 cell) { - return any(lessThan(cell, ivec2(0))) || any(greaterThanEqual(cell, gasSize)) || imageLoad(gasMaskTex, cell).r != 0u; + return any(lessThan(cell, ivec2(0))) || any(greaterThanEqual(cell, gasSize)) || loadTexel(gasMaskTex, cell).r != 0u; } void main() { @@ -45,14 +53,14 @@ void main() { uint litOil = gasSources[2 * linear + 1]; gasSources[2 * linear] = 0u; gasSources[2 * linear + 1] = 0u; - if (imageLoad(gasMaskTex, gas).r != 0u) { + if (loadTexel(gasMaskTex, gas).r != 0u) { imageStore(steamDst, gas, vec4(0.0)); return; } // Semi-Lagrangian: a trace that ends in a wall takes the cell's own value (no steam through walls). vec2 here = vec2(gas) + 0.5; - vec2 back = here - (imageLoad(gasVelocityTex, gas).xy + vec2(0.0, steamRise)) * dt; + vec2 back = here - (loadTexel(gasVelocityTex, gas).xy + vec2(0.0, steamRise)) * dt; if (blocked(ivec2(floor(back)))) back = here; float steam = textureLod(steamSrc, back / vec2(gasSize), 0.0).r; steam = clamp(steam * exp(-steamCondense * dt) + float(steamCount) * steamPerParticle, 0.0, 1.0); diff --git a/shaders/water_divergence.comp b/shaders/water_divergence.comp index d7cfd22..492d4cf 100644 --- a/shaders/water_divergence.comp +++ b/shaders/water_divergence.comp @@ -2,14 +2,14 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D waterUFaceTex; -layout(r16f, binding = 1) uniform readonly image2D waterVFaceTex; +layout(binding = 0) uniform sampler2D waterUFaceTex; +layout(binding = 1) uniform sampler2D waterVFaceTex; layout(r16f, binding = 2) uniform writeonly image2D divergenceTex; -layout(r8ui, binding = 3) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 4) uniform readonly image2D liquidPhiTex; +layout(binding = 3) uniform usampler2D boundaryTex; +layout(binding = 4) uniform sampler2D liquidPhiTex; // Packed calm water (water_pack.comp, bits 0-6 a count): packed cells and their neighbours, whose counts the packed // deposit shapes, take no density correction, so packing is not itself a density error that drives flow. -layout(r8ui, binding = 5) uniform readonly uimage2D waterPackedTex; +layout(binding = 5) uniform usampler2D waterPackedTex; layout(std430, binding = 0) readonly buffer WaterCellCounts { uint counts[]; @@ -30,6 +30,14 @@ uniform float weightScale; uniform float restDensity; uniform float densityRate; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Largest density error (relative to rest) the correction acts on, so a source that overfills a cell is not blown out. const float kMaxDensityError = 0.5; // A cell is inside the bulk when the cell above holds at least this share of the rest density (or is a wall or the @@ -40,14 +48,14 @@ bool nearPacked(ivec2 coord) { for (int j = -1; j <= 1; ++j) { for (int i = -1; i <= 1; ++i) { ivec2 n = clamp(coord + ivec2(i, j), ivec2(0), gridSize - ivec2(1)); - if ((imageLoad(waterPackedTex, n).r & 127u) != 0u) return true; + if ((loadTexel(waterPackedTex, n).r & 127u) != 0u) return true; } } return false; } bool liquidCell(ivec2 coord) { - return imageLoad(liquidPhiTex, coord).x < liquidPhiThreshold; + return loadTexel(liquidPhiTex, coord).x < liquidPhiThreshold; } float densityError(ivec2 coord) { @@ -59,7 +67,7 @@ float densityError(ivec2 coord) { // nearly full, as before, left clusters of entrained voids at real gravity: a dam break kept 24% voids.) bool bulkCell(ivec2 coord) { ivec2 above = coord + ivec2(0, 1); - if (above.y >= gridSize.y || imageLoad(boundaryTex, above).r != 0u) { + if (above.y >= gridSize.y || loadTexel(boundaryTex, above).r != 0u) { return true; } return densityError(above) + 1.0 >= kBulkDensity; @@ -69,13 +77,13 @@ float forcedV(ivec2 face) { if (face.y == 0 || face.y == gridSize.y) { return 0.0; } - uint below = imageLoad(boundaryTex, face - ivec2(0, 1)).r; - uint above = imageLoad(boundaryTex, face).r; + uint below = loadTexel(boundaryTex, face - ivec2(0, 1)).r; + uint above = loadTexel(boundaryTex, face).r; if (below == 1u || above == 1u) { return 0.0; } // A face of moving sand keeps the sand velocity water_mac_build gave it; gravity acts only on open faces. - float stored = imageLoad(waterVFaceTex, face).x; + float stored = loadTexel(waterVFaceTex, face).x; return below != 0u || above != 0u ? stored : stored + gravityDelta; } @@ -87,13 +95,13 @@ void main() { // Sealed pockets, down to a pinhole walled in on all four sides, get their right-hand side's mean removed afterwards // (pressure_pockets.comp). - if (imageLoad(boundaryTex, coord).r != 0u || !liquidCell(coord)) { + if (loadTexel(boundaryTex, coord).r != 0u || !liquidCell(coord)) { imageStore(divergenceTex, coord, vec4(0.0)); return; } - float u_left = imageLoad(waterUFaceTex, ivec2(coord.x, coord.y)).x; - float u_right = imageLoad(waterUFaceTex, ivec2(coord.x + 1, coord.y)).x; + float u_left = loadTexel(waterUFaceTex, ivec2(coord.x, coord.y)).x; + float u_right = loadTexel(waterUFaceTex, ivec2(coord.x + 1, coord.y)).x; float v_down = forcedV(ivec2(coord.x, coord.y)); float v_up = forcedV(ivec2(coord.x, coord.y + 1)); diff --git a/shaders/water_mac_build.comp b/shaders/water_mac_build.comp index 9f46df0..3361798 100644 --- a/shaders/water_mac_build.comp +++ b/shaders/water_mac_build.comp @@ -4,8 +4,8 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r16f, binding = 0) uniform writeonly image2D waterUFaceTex; layout(r16f, binding = 1) uniform writeonly image2D waterVFaceTex; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; -layout(rg32f, binding = 3) uniform readonly image2D sandVelocityTex; +layout(binding = 2) uniform usampler2D boundaryTex; +layout(binding = 3) uniform sampler2D sandVelocityTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -32,7 +32,7 @@ layout(std430, binding = 3) readonly buffer VFaceVelocitySums { // Packed calm water (water_pack.comp): a packed cell deposits here as rest-density water at rest, whatever count it // stores (packing admits the rest count +-1), so it asks the solve for no density correction. -layout(r8ui, binding = 4) uniform readonly uimage2D waterPackedTex; +layout(binding = 4) uniform usampler2D waterPackedTex; layout(std430, binding = 4) buffer WaterCellCounts { uint cellCounts[]; @@ -41,6 +41,15 @@ layout(std430, binding = 4) buffer WaterCellCounts { uniform ivec2 gridSize; uniform float weightScale; uniform float velocityScale; + +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform float restMass; // particle mass of a cell at rest density // A packed cell's particles sit anywhere in it, so it deposits the P2G kernels averaged over the cell: the quadratic @@ -76,12 +85,12 @@ float packedFaceWeight(ivec2 face, bool vertical) { // Velocity component of the wall on a face between in-bounds cells a and b, at least one of them blocked. Painted // solids (mask 1) rest; sand (mask 2 and 3) moves with the sand grid, so the pressure solve pushes water out of its way. float wallVelocity(ivec2 a, ivec2 b, int axis) { - uint maskA = imageLoad(boundaryTex, a).r; - uint maskB = imageLoad(boundaryTex, b).r; + uint maskA = loadTexel(boundaryTex, a).r; + uint maskB = loadTexel(boundaryTex, b).r; if (maskA == 1u || maskB == 1u || (maskA != 0u && maskB != 0u)) { return 0.0; } - return imageLoad(sandVelocityTex, (maskA != 0u ? a : b) + ivec2(1))[axis]; + return loadTexel(sandVelocityTex, (maskA != 0u ? a : b) + ivec2(1))[axis]; } int uFaceIndex(ivec2 face) { @@ -111,7 +120,7 @@ void main() { for (uint i = gl_LocalInvocationIndex; i < 400u; i += 256u) { ivec2 cell = tileOrigin + ivec2(int(i % 20u), int(i / 20u)); bool inGrid = all(greaterThanEqual(cell, ivec2(0))) && all(lessThan(cell, gridSize)); - packedTile[i / 20u][i % 20u] = inGrid ? min(imageLoad(waterPackedTex, cell).r & 127u, 1u) : 0u; + packedTile[i / 20u][i % 20u] = inGrid ? min(loadTexel(waterPackedTex, cell).r & 127u, 1u) : 0u; } barrier(); @@ -130,7 +139,7 @@ void main() { float face_u = 0.0; if (coord.x > 0 && coord.x < gridSize.x) { ivec2 left = ivec2(coord.x - 1, coord.y); - if (imageLoad(boundaryTex, left).r != 0u || imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, left).r != 0u || loadTexel(boundaryTex, coord).r != 0u) { face_u = wallVelocity(left, coord, 0); } else { int index = uFaceIndex(coord); @@ -147,7 +156,7 @@ void main() { float face_v = 0.0; if (coord.y > 0 && coord.y < gridSize.y) { ivec2 down = ivec2(coord.x, coord.y - 1); - if (imageLoad(boundaryTex, down).r != 0u || imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, down).r != 0u || loadTexel(boundaryTex, coord).r != 0u) { face_v = wallVelocity(down, coord, 1); } else { int index = vFaceIndex(coord); diff --git a/shaders/water_mac_finalize.comp b/shaders/water_mac_finalize.comp index f6d1899..ecef52a 100644 --- a/shaders/water_mac_finalize.comp +++ b/shaders/water_mac_finalize.comp @@ -2,28 +2,36 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D waterUFaceTex; -layout(r16f, binding = 1) uniform readonly image2D waterVFaceTex; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform sampler2D waterUFaceTex; +layout(binding = 1) uniform sampler2D waterVFaceTex; +layout(binding = 2) uniform usampler2D boundaryTex; layout(rg16f, binding = 3) uniform writeonly image2D waterCellVelocityTex; uniform ivec2 gridSize; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); if (coord.x >= gridSize.x || coord.y >= gridSize.y) { return; } - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(waterCellVelocityTex, coord, vec4(0.0)); return; } - float u_left = imageLoad(waterUFaceTex, ivec2(coord.x, coord.y)).x; - float u_right = imageLoad(waterUFaceTex, ivec2(coord.x + 1, coord.y)).x; - float v_down = imageLoad(waterVFaceTex, ivec2(coord.x, coord.y)).x; - float v_up = imageLoad(waterVFaceTex, ivec2(coord.x, coord.y + 1)).x; + float u_left = loadTexel(waterUFaceTex, ivec2(coord.x, coord.y)).x; + float u_right = loadTexel(waterUFaceTex, ivec2(coord.x + 1, coord.y)).x; + float v_down = loadTexel(waterVFaceTex, ivec2(coord.x, coord.y)).x; + float v_up = loadTexel(waterVFaceTex, ivec2(coord.x, coord.y + 1)).x; vec2 velocity = vec2(0.5 * (u_left + u_right), 0.5 * (v_down + v_up)); imageStore(waterCellVelocityTex, coord, vec4(velocity, 0.0, 0.0)); } diff --git a/shaders/water_pack.comp b/shaders/water_pack.comp index b52ad5f..9a9c6d9 100644 --- a/shaders/water_pack.comp +++ b/shaders/water_pack.comp @@ -12,9 +12,9 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r8ui, binding = 0) uniform uimage2D packedTex; layout(rg16f, binding = 1) uniform image2D wakeVelocityTex; -layout(r8ui, binding = 2) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 3) uniform readonly image2D waterUFaceTex; -layout(r16f, binding = 4) uniform readonly image2D waterVFaceTex; +layout(binding = 2) uniform usampler2D boundaryTex; +layout(binding = 3) uniform sampler2D waterUFaceTex; +layout(binding = 4) uniform sampler2D waterVFaceTex; struct WaterParticle { vec4 state0; @@ -56,6 +56,14 @@ uniform float particleMass; uniform int maxParticles; uniform uint seed; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + const uint kPack = 1u << 31; const uint kWake = 128u; @@ -68,7 +76,7 @@ int cellIndex(ivec2 c) { } bool liquid(ivec2 c) { - return inside(c) && imageLoad(boundaryTex, c).r == 0u && float(counts[cellIndex(c)]) >= liquidCount; + return inside(c) && loadTexel(boundaryTex, c).r == 0u && float(counts[cellIndex(c)]) >= liquidCount; } bool deepLiquid(ivec2 c, int radius) { @@ -133,10 +141,10 @@ void apply(ivec2 c) { void decideWake(ivec2 c) { uint value = imageLoad(packedTex, c).r; if (value == 0u || (value & kWake) != 0u) return; - float uLeft = imageLoad(waterUFaceTex, c).x; - float uRight = imageLoad(waterUFaceTex, c + ivec2(1, 0)).x; - float vDown = imageLoad(waterVFaceTex, c).x; - float vUp = imageLoad(waterVFaceTex, c + ivec2(0, 1)).x; + float uLeft = loadTexel(waterUFaceTex, c).x; + float uRight = loadTexel(waterUFaceTex, c + ivec2(1, 0)).x; + float vDown = loadTexel(waterVFaceTex, c).x; + float vUp = loadTexel(waterVFaceTex, c + ivec2(0, 1)).x; float fastest = max(max(abs(uLeft), abs(uRight)), max(abs(vDown), abs(vUp))); if (fastest > wakeSpeed || !deepLiquid(c, 2)) { imageStore(packedTex, c, uvec4(value | kWake)); diff --git a/shaders/water_particle_finalize.comp b/shaders/water_particle_finalize.comp index d4b88fc..119e057 100644 --- a/shaders/water_particle_finalize.comp +++ b/shaders/water_particle_finalize.comp @@ -2,7 +2,7 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform usampler2D boundaryTex; layout(r16f, binding = 1) uniform writeonly image2D liquidVolumeTex; layout(r16f, binding = 2) uniform writeonly image2D liquidPhiTex; layout(r16f, binding = 3) uniform writeonly image2D waterAmountTex; @@ -22,6 +22,11 @@ uniform float surfaceLevel; uniform float restMass; uniform float liquidPhiThreshold; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + int linearIndex(ivec2 coord) { return coord.y * gridSize.x + coord.x; } @@ -41,7 +46,7 @@ float surfacePhi(ivec2 coord) { for (int y = -1; y <= 1; ++y) { for (int x = -1; x <= 1; ++x) { ivec2 sample_coord = clampCell(coord + ivec2(x, y)); - if (imageLoad(boundaryTex, sample_coord).r != 0u) continue; + if (loadTexel(boundaryTex, sample_coord).r != 0u) continue; float w = (x == 0 ? 2.0 : 1.0) * (y == 0 ? 2.0 : 1.0); total += w * particleEquivalentAt(sample_coord); weights += w; @@ -57,7 +62,7 @@ void main() { return; } - if (imageLoad(boundaryTex, coord).r != 0u) { + if (loadTexel(boundaryTex, coord).r != 0u) { imageStore(liquidVolumeTex, coord, vec4(0.0)); imageStore(liquidPhiTex, coord, vec4(-1.0, 0.0, 0.0, 0.0)); imageStore(waterAmountTex, coord, vec4(0.0)); diff --git a/shaders/water_particle_p2g.comp b/shaders/water_particle_p2g.comp index 0da7299..16d4120 100644 --- a/shaders/water_particle_p2g.comp +++ b/shaders/water_particle_p2g.comp @@ -2,7 +2,7 @@ layout(local_size_x = 256) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryTex; +layout(binding = 0) uniform usampler2D boundaryTex; struct WaterParticle { vec4 state0; @@ -47,12 +47,17 @@ uniform int maxParticles; uniform float velocityScale; uniform float weightScale; +// Sampled reads keep imageLoad's result outside the texture: zero. +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool cellInBounds(ivec2 coord) { return coord.x >= 0 && coord.y >= 0 && coord.x < gridSize.x && coord.y < gridSize.y; } bool blockedCell(ivec2 coord) { - return !cellInBounds(coord) || imageLoad(boundaryTex, coord).r != 0u; + return !cellInBounds(coord) || loadTexel(boundaryTex, coord).r != 0u; } void quadraticWeights(float x, out vec3 w) { diff --git a/shaders/water_particle_spawn.comp b/shaders/water_particle_spawn.comp index 8ae8b5c..ca21a86 100644 --- a/shaders/water_particle_spawn.comp +++ b/shaders/water_particle_spawn.comp @@ -2,8 +2,8 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 1) uniform readonly image2D liquidVolumeTex; +layout(binding = 0) uniform usampler2D boundaryTex; +layout(binding = 1) uniform sampler2D liquidVolumeTex; struct WaterParticle { vec4 state0; @@ -48,6 +48,14 @@ uniform float oilFlag; uniform float tickDt; uniform vec2 gravity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // PCG-style integer hash: per-particle jitter that varies with the cell, the particle and the tick (spawnSeed). uint hashBits(uvec3 v) { uint h = v.x * 1664525u + v.y * 22695477u + v.z * 2654435761u + spawnSeed * 2246822519u; @@ -62,7 +70,7 @@ vec2 uniform2(uvec3 v) { bool blocked(vec2 p) { ivec2 c = ivec2(floor(p)); - return any(lessThan(c, ivec2(0))) || any(greaterThanEqual(c, gridSize)) || imageLoad(boundaryTex, c).r != 0u; + return any(lessThan(c, ivec2(0))) || any(greaterThanEqual(c, gridSize)) || loadTexel(boundaryTex, c).r != 0u; } bool insideBrush(ivec2 coord) { @@ -83,12 +91,12 @@ void main() { if (coord.x >= gridSize.x || coord.y >= gridSize.y) { return; } - if (!insideBrush(coord) || alreadyStamped(coord) || imageLoad(boundaryTex, coord).r != 0u) { + if (!insideBrush(coord) || alreadyStamped(coord) || loadTexel(boundaryTex, coord).r != 0u) { return; } // A held brush must not keep stacking particles into a cell already as dense as the brush emits. // A zero limit keeps the exact per-cell emission of scripted brushes. - if (densityLimit > 0.0 && imageLoad(liquidVolumeTex, coord).x >= densityLimit) { + if (densityLimit > 0.0 && loadTexel(liquidVolumeTex, coord).x >= densityLimit) { return; } diff --git a/shaders/water_particle_step.comp b/shaders/water_particle_step.comp index 857421c..95c4628 100644 --- a/shaders/water_particle_step.comp +++ b/shaders/water_particle_step.comp @@ -2,15 +2,15 @@ layout(local_size_x = 256) in; -layout(r8ui, binding = 0) uniform readonly uimage2D boundaryMaskTex; -layout(r16f, binding = 1) uniform readonly image2D waterUPrevTex; -layout(r16f, binding = 2) uniform readonly image2D waterVPrevTex; -layout(r16f, binding = 3) uniform readonly image2D waterUTex; -layout(r16f, binding = 4) uniform readonly image2D waterVTex; -layout(rg32f, binding = 5) uniform readonly image2D sandVelocityTex; -layout(r16f, binding = 6) uniform readonly image2D sandOccupancyTex; -layout(r16f, binding = 7) uniform readonly image2D liquidVolumeTex; -layout(rg16f, binding = 8) uniform readonly image2D oilTex; // x: oil share of the cell's records (oil_field.comp) +layout(binding = 0) uniform usampler2D boundaryMaskTex; +layout(binding = 1) uniform sampler2D waterUPrevTex; +layout(binding = 2) uniform sampler2D waterVPrevTex; +layout(binding = 3) uniform sampler2D waterUTex; +layout(binding = 4) uniform sampler2D waterVTex; +layout(binding = 5) uniform sampler2D sandVelocityTex; +layout(binding = 6) uniform sampler2D sandOccupancyTex; +layout(binding = 7) uniform sampler2D liquidVolumeTex; +layout(binding = 8) uniform sampler2D oilTex; // x: oil share of the cell's records (oil_field.comp) struct WaterParticle { vec4 state0; @@ -59,12 +59,20 @@ uniform float restVolume; // liquid volume of a cell at rest density uniform float bulkDamping; uniform vec2 gravity; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + // Set when a speed cap cut this particle's velocity; each such particle is counted, so clipping is never silent. bool speedClamped = false; bool blockedCell(ivec2 cell) { return cell.x < 0 || cell.y < 0 || cell.x >= gridSize.x || cell.y >= gridSize.y || - imageLoad(boundaryMaskTex, cell).x != 0u; + loadTexel(boundaryMaskTex, cell).x != 0u; } // The domain edge is a wall like a painted one (blockedCell is true outside the grid): particles sweep to its face, as @@ -134,7 +142,7 @@ void escapeSand(inout vec2 position, inout vec2 velocity) { for (int steps = 1; steps < bestSteps; ++steps) { ivec2 probe = cell + direction * steps; if (probe.x < 0 || probe.y < 0 || probe.x >= gridSize.x || probe.y >= gridSize.y) break; - uint mask = imageLoad(boundaryMaskTex, probe).x; + uint mask = loadTexel(boundaryMaskTex, probe).x; if (mask == 1u) break; if (mask == 0u) { bestSteps = steps; @@ -146,7 +154,7 @@ void escapeSand(inout vec2 position, inout vec2 velocity) { if (bestDirection == ivec2(0)) return; position += vec2(bestDirection * bestSteps); vec2 outward = vec2(bestDirection); - vec2 sandVelocity = imageLoad(sandVelocityTex, cell + ivec2(1)).xy; + vec2 sandVelocity = loadTexel(sandVelocityTex, cell + ivec2(1)).xy; velocity += outward * max(dot(sandVelocity - velocity, outward), 0.0); float speed = length(velocity); if (speed > maxVelocity) { @@ -158,7 +166,7 @@ void escapeSand(inout vec2 position, inout vec2 velocity) { void moveWithCollisions(inout vec2 position, inout vec2 velocity, inout mat2 affine, vec2 displacement, float bounce) { position = clamp(position, vec2(0.0), vec2(gridSize) - vec2(1.0 / 256.0)); - if (imageLoad(boundaryMaskTex, ivec2(floor(position))).x >= 2u) { + if (loadTexel(boundaryMaskTex, ivec2(floor(position))).x >= 2u) { escapeSand(position, velocity); } // A particle still inside painted geometry waits for the brush's explicit clearing. @@ -230,19 +238,19 @@ ivec2 liveV(ivec2 face, vec2 position) { } float sampleUPrev(ivec2 face) { - return imageLoad(waterUPrevTex, face).x; + return loadTexel(waterUPrevTex, face).x; } float sampleVPrev(ivec2 face) { - return imageLoad(waterVPrevTex, face).x; + return loadTexel(waterVPrevTex, face).x; } float sampleUCurrent(ivec2 face) { - return imageLoad(waterUTex, face).x; + return loadTexel(waterUTex, face).x; } float sampleVCurrent(ivec2 face) { - return imageLoad(waterVTex, face).x; + return loadTexel(waterVTex, face).x; } float bilerpUCurrent(vec2 position) { @@ -366,11 +374,11 @@ float sampleSand(vec2 position, out vec2 velocity) { for (int j = 0; j < 2; ++j) { for (int i = 0; i < 2; ++i) { ivec2 cell = clamp(base + ivec2(i, j), ivec2(0), gridSize - ivec2(1)); - float occupancy = imageLoad(sandOccupancyTex, cell).x; + float occupancy = loadTexel(sandOccupancyTex, cell).x; if (occupancy > 0.0) { float weight = (i == 1 ? f.x : 1.0 - f.x) * (j == 1 ? f.y : 1.0 - f.y) * occupancy; fraction += weight; - momentum += weight * imageLoad(sandVelocityTex, cell + ivec2(1)).xy; + momentum += weight * loadTexel(sandVelocityTex, cell + ivec2(1)).xy; } } } @@ -389,7 +397,7 @@ vec2 cappedVelocity(vec2 value) { vec2 advectionVelocity(vec2 position, vec2 particleVelocity) { ivec2 cell = clamp(ivec2(floor(position)), ivec2(0), gridSize - ivec2(1)); - return cappedVelocity(imageLoad(liquidVolumeTex, cell).x > 0.0 ? sampleProjectedGridVelocity(position) : particleVelocity); + return cappedVelocity(loadTexel(liquidVolumeTex, cell).x > 0.0 ? sampleProjectedGridVelocity(position) : particleVelocity); } void main() { @@ -448,7 +456,7 @@ void main() { for (int i = 0; i < 4; ++i) { ivec2 o = ivec2(i & 1, i >> 1); ivec2 c = clamp(c0 + o, ivec2(0), gridSize - ivec2(1)); - share += imageLoad(oilTex, c).x * (o.x == 1 ? f.x : 1.0 - f.x) * (o.y == 1 ? f.y : 1.0 - f.y); + share += loadTexel(oilTex, c).x * (o.x == 1 ? f.x : 1.0 - f.x) * (o.y == 1 ? f.y : 1.0 - f.y); } // Capped at 2 g: an impact's pressure spike must not fling the oil. float magnitude = length(pressureAcceleration); @@ -495,7 +503,7 @@ void main() { atomicAdd(waterVelocityClamped, 1u); } ivec2 finalCell = clamp(ivec2(floor(position)), ivec2(0), gridSize - ivec2(1)); - if (clumpVolume > 0.0 && imageLoad(liquidVolumeTex, finalCell).x > clumpVolume) { + if (clumpVolume > 0.0 && loadTexel(liquidVolumeTex, finalCell).x > clumpVolume) { // Hashed from the particle's own state, not its slot: slots are reordered nondeterministically. // The record tag keeps coincident records apart: once FLIP has made their velocities equal too (the 0.97 blend // shrinks a difference below float precision within seconds), the state alone drew one jitter for all of them @@ -520,8 +528,8 @@ void main() { for (int i = 0; i < 2; ++i) { ivec2 c = cell + ivec2(0, i == 0 ? 0 : (oil ? 1 : -1)); if (blockedCell(c)) continue; - float share = imageLoad(oilTex, c).x; - other = max(other, imageLoad(liquidVolumeTex, c).x * (oil ? 1.0 - share : share)); + float share = loadTexel(oilTex, c).x; + other = max(other, loadTexel(liquidVolumeTex, c).x * (oil ? 1.0 - share : share)); } float drive = clamp(2.0 * other / restVolume, 0.0, 1.0) * support; if (drive > 0.0) { diff --git a/shaders/water_project.comp b/shaders/water_project.comp index f5b85e3..9b10def 100644 --- a/shaders/water_project.comp +++ b/shaders/water_project.comp @@ -2,13 +2,13 @@ layout(local_size_x = 16, local_size_y = 16) in; -layout(r16f, binding = 0) uniform readonly image2D waterUFaceSrc; -layout(r16f, binding = 1) uniform readonly image2D waterVFaceSrc; -layout(r32f, binding = 2) uniform readonly image2D pressureTex; +layout(binding = 0) uniform sampler2D waterUFaceSrc; +layout(binding = 1) uniform sampler2D waterVFaceSrc; +layout(binding = 2) uniform sampler2D pressureTex; layout(r16f, binding = 3) uniform writeonly image2D waterUFaceDst; layout(r16f, binding = 4) uniform writeonly image2D waterVFaceDst; -layout(r8ui, binding = 5) uniform readonly uimage2D boundaryTex; -layout(r16f, binding = 6) uniform readonly image2D liquidPhiTex; +layout(binding = 5) uniform usampler2D boundaryTex; +layout(binding = 6) uniform sampler2D liquidPhiTex; layout(std430, binding = 7) buffer DebugCounters { uint nanInfDetected; @@ -22,6 +22,14 @@ uniform float liquidPhiThreshold; uniform float gravityDelta; uniform float sandLeak; +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + bool cellInBounds(ivec2 coord) { return coord.x >= 0 && coord.y >= 0 && coord.x < gridSize.x && coord.y < gridSize.y; } @@ -30,31 +38,32 @@ bool blocked(ivec2 coord) { if (!cellInBounds(coord)) { return true; } - return imageLoad(boundaryTex, coord).r != 0u; + return loadTexel(boundaryTex, coord).r != 0u; } // A face between sand and water (no painted solid) moves with the sand and conducts at a fraction of an open face's rate. bool sandFace(ivec2 a, ivec2 b) { - uint maskA = imageLoad(boundaryTex, a).r; - uint maskB = imageLoad(boundaryTex, b).r; + uint maskA = loadTexel(boundaryTex, a).r; + uint maskB = loadTexel(boundaryTex, b).r; return maskA != 1u && maskB != 1u && (maskA >= 2u || maskB >= 2u); } float pressureAt(ivec2 coord) { - if (!cellInBounds(coord) || blocked(coord) || imageLoad(liquidPhiTex, coord).x >= liquidPhiThreshold) { + if (!cellInBounds(coord) || blocked(coord) || loadTexel(liquidPhiTex, coord).x >= liquidPhiThreshold) { return 0.0; } - return imageLoad(pressureTex, coord).x; + return loadTexel(pressureTex, coord).x; } // Ghost-fluid free surface (pressure_cg.comp): a face between a liquid and an air cell conducts 1 / theta, the same // coefficient the pressure operator used; thetaMin 0 is the plain Dirichlet cell, conductance 1. uniform float thetaMin; + float faceConductance(ivec2 a, ivec2 b) { if (thetaMin <= 0.0) return 1.0; - float phiA = imageLoad(liquidPhiTex, a).x; - float phiB = imageLoad(liquidPhiTex, b).x; + float phiA = loadTexel(liquidPhiTex, a).x; + float phiB = loadTexel(liquidPhiTex, b).x; bool liquidA = phiA < liquidPhiThreshold, liquidB = phiB < liquidPhiThreshold; if (liquidA == liquidB) return 1.0; float inside = (liquidA ? phiA : phiB) - liquidPhiThreshold; @@ -83,9 +92,9 @@ void main() { if (coord.x > 0 && coord.x < gridSize.x && !blocked(left_cell) && !blocked(right_cell)) { float p_left = pressureAt(left_cell); float p_right = pressureAt(right_cell); - face_u = imageLoad(waterUFaceSrc, coord).x - faceConductance(left_cell, right_cell) * (p_right - p_left); + face_u = loadTexel(waterUFaceSrc, coord).x - faceConductance(left_cell, right_cell) * (p_right - p_left); } else if (coord.x > 0 && coord.x < gridSize.x && sandFace(left_cell, right_cell)) { - face_u = imageLoad(waterUFaceSrc, coord).x - sandLeak * (pressureAt(right_cell) - pressureAt(left_cell)); + face_u = loadTexel(waterUFaceSrc, coord).x - sandLeak * (pressureAt(right_cell) - pressureAt(left_cell)); } imageStore(waterUFaceDst, coord, vec4(checkedHalf(face_u), 0.0, 0.0, 0.0)); } @@ -97,9 +106,9 @@ void main() { if (coord.y > 0 && coord.y < gridSize.y && !blocked(down_cell) && !blocked(up_cell)) { float p_down = pressureAt(down_cell); float p_up = pressureAt(up_cell); - face_v = imageLoad(waterVFaceSrc, coord).x + gravityDelta - faceConductance(down_cell, up_cell) * (p_up - p_down); + face_v = loadTexel(waterVFaceSrc, coord).x + gravityDelta - faceConductance(down_cell, up_cell) * (p_up - p_down); } else if (coord.y > 0 && coord.y < gridSize.y && sandFace(down_cell, up_cell)) { - face_v = imageLoad(waterVFaceSrc, coord).x - sandLeak * (pressureAt(up_cell) - pressureAt(down_cell)); + face_v = loadTexel(waterVFaceSrc, coord).x - sandLeak * (pressureAt(up_cell) - pressureAt(down_cell)); } imageStore(waterVFaceDst, coord, vec4(checkedHalf(face_v), 0.0, 0.0, 0.0)); } diff --git a/shaders/wood_burn.comp b/shaders/wood_burn.comp index 9f8812b..b6ee843 100644 --- a/shaders/wood_burn.comp +++ b/shaders/wood_burn.comp @@ -10,11 +10,11 @@ layout(local_size_x = 16, local_size_y = 16) in; layout(r16ui, binding = 0) uniform uimage2D materialTex; -layout(r8ui, binding = 1) uniform readonly uimage2D gasMaskTex; +layout(binding = 1) uniform usampler2D gasMaskTex; layout(r16f, binding = 2) uniform image2D temperatureTex; layout(r16f, binding = 3) uniform image2D fuelTex; layout(rg16f, binding = 4) uniform image2D reactionTex; -layout(r16f, binding = 5) uniform readonly image2D waterAmountTex; +layout(binding = 5) uniform sampler2D waterAmountTex; // On a sample tick (tally 1), mode 0 adds the wood cells left after its update to woodCells (intact, burning or charred). layout(std430, binding = 0) buffer WoodTally { @@ -26,6 +26,15 @@ uniform int mode; uniform int tally; uniform uint seed; uniform float ignitionTemperature; + +// Sampled reads keep imageLoad's result outside the texture: zero. +vec4 loadTexel(sampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : vec4(0.0); +} +uvec4 loadTexel(usampler2D t, ivec2 c) { + return all(lessThan(uvec2(c), uvec2(textureSize(t, 0)))) ? texelFetch(t, c, 0) : uvec4(0u); +} + uniform float igniteChance; // per tick while hot bool isWood(uint material) { @@ -55,7 +64,7 @@ void main() { for (int y = -2; y <= 2; ++y) { for (int x = -2; x <= 2; ++x) { ivec2 n = clamp(gas + ivec2(x, y), ivec2(0), gasSize - 1); - if (imageLoad(gasMaskTex, n).r == 0u) heat = max(heat, imageLoad(temperatureTex, n).x); + if (loadTexel(gasMaskTex, n).r == 0u) heat = max(heat, imageLoad(temperatureTex, n).x); } } uint remaining = 0u; @@ -66,7 +75,7 @@ void main() { float wet = 0.0; for (int y = -1; y <= 1; ++y) for (int x = -1; x <= 1; ++x) - wet = max(wet, imageLoad(waterAmountTex, clamp(cell + ivec2(x, y), ivec2(0), gasSize * 2 - 1)).x); + wet = max(wet, loadTexel(waterAmountTex, clamp(cell + ivec2(x, y), ivec2(0), gasSize * 2 - 1)).x); uint fuel = material == 3u ? 255u : (material & 255u); uint next = material; if (wet > 0.05) { @@ -83,7 +92,7 @@ void main() { if (tally != 0 && remaining > 0u) atomicAdd(woodCells, remaining); return; } - if (imageLoad(gasMaskTex, gas).r != 0u) { + if (loadTexel(gasMaskTex, gas).r != 0u) { return; } // Burning wood cells among the 10x10 simulation cells around this open gas cell (past the mask halo). diff --git a/src/PowderApp.cpp b/src/PowderApp.cpp index a65bfa6..4ed583a 100644 --- a/src/PowderApp.cpp +++ b/src/PowderApp.cpp @@ -94,6 +94,19 @@ void ShowErrorDialog(const std::string& message) { } #endif +// Shaders sample what they only read (texelFetch) and keep image units for writes, so no shader needs more than the +// 8 image units OpenGL 4.3 guarantees. A read-write binding serves both kinds of shader. Unit 47, which every +// OpenGL 4.3 context has and no shader samples, is left active so a later glBindTexture for an upload or readback +// cannot replace a bound input. +void BindImage(GLuint unit, GLuint texture, GLenum access, GLenum format) { + if (access != GL_WRITE_ONLY) { + glActiveTexture(GL_TEXTURE0 + unit); + glBindTexture(GL_TEXTURE_2D, texture); + glActiveTexture(GL_TEXTURE0 + 47); + } + if (access != GL_READ_ONLY) glBindImageTexture(unit, texture, 0, GL_FALSE, 0, access, format); +} + } // namespace int PowderApp::Run() { @@ -228,21 +241,6 @@ void PowderApp::Initialize() { gl_ready_ = true; - struct GpuLimit { GLenum parameter; GLint minimum; const char* name; }; - const std::array limits{{ - {GL_MAX_IMAGE_UNITS, 18, "image units"}, - {GL_MAX_COMPUTE_IMAGE_UNIFORMS, 18, "compute image uniforms"}, - {GL_MAX_COMBINED_SHADER_OUTPUT_RESOURCES, 19, "combined shader output resources"}, - {GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, 8, "shader storage buffer bindings"}}}; - for (const auto& limit : limits) { - GLint available = 0; - glGetIntegerv(limit.parameter, &available); - if (available < limit.minimum) { - throw std::runtime_error(std::string("GPU supports ") + std::to_string(available) + " " + limit.name + - "; Powder Game requires " + std::to_string(limit.minimum)); - } - } - glDisable(GL_DEPTH_TEST); glGenVertexArrays(1, &fullscreen_vao_); @@ -869,7 +867,8 @@ void PowderApp::CreatePrograms() { } void PowderApp::InitializeState() { - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT | + GL_BUFFER_UPDATE_BARRIER_BIT); gas_mac_ping_ = smoke_ping_ = true; frame_index_ = 0U; boundary_state_.static_dirty = true; @@ -884,11 +883,11 @@ void PowderApp::InitializeState() { glUseProgram(init_state_program_); glUniform2i(CachedUniform(init_state_program_, "gridSize"), kGridWidth, kGridHeight); - glBindImageTexture(0, material_a_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16UI); - glBindImageTexture(3, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(4, water_state_.render_velocity, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); + BindImage(0, material_a_, GL_WRITE_ONLY, GL_R16UI); + BindImage(3, water_state_.liquid_volume, GL_WRITE_ONLY, GL_R16F); + BindImage(4, water_state_.render_velocity, GL_WRITE_ONLY, GL_RG16F); DispatchGrid(kGridWidth, kGridHeight); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); const GLuint zero_uint = 0U; const GLint zero_int = 0; @@ -1530,13 +1529,13 @@ void PowderApp::RunSpawnFullRes(const BrushConfig& config, const DispatchRegion& glUniform1i(CachedUniform(spawn_full_program_, "clearWater"), 0); glUniform1i(CachedUniform(spawn_full_program_, "clearMode"), 0); - glBindImageTexture(0, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16UI); - glBindImageTexture(3, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentWaterVelocity(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(5, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(6, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(7, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); - glBindImageTexture(1, water_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); + BindImage(0, CurrentMaterial(), GL_READ_WRITE, GL_R16UI); + BindImage(3, CurrentWaterAmount(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentWaterVelocity(), GL_READ_WRITE, GL_RG16F); + BindImage(5, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(6, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(7, sand_state_.packed, GL_READ_WRITE, GL_R8UI); + BindImage(1, water_state_.packed, GL_READ_WRITE, GL_R8UI); DispatchRegionGrid(region); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -1569,26 +1568,26 @@ void PowderApp::RunSpawnGas(const BrushConfig& config, const DispatchRegion& reg glUniform1f(CachedUniform(spawn_gas_program_, "velocitySpread"), config.velocity_spread); glUniform1ui(CachedUniform(spawn_gas_program_, "spawnSeed"), frame_index_ * 977U + static_cast(brush_x_ * 131 + brush_y_)); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(3, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(5, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(6, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(7, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(9, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(10, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_WRITE, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_WRITE, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_READ_WRITE, GL_RG16F); + BindImage(3, CurrentSmokeDen(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(5, CurrentFuel(), GL_READ_WRITE, GL_R16F); + BindImage(6, CurrentOxidizer(), GL_READ_WRITE, GL_R16F); + BindImage(7, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(9, CurrentWaterAmount(), GL_READ_ONLY, GL_R16F); + BindImage(10, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchRegionGrid(region); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); glUseProgram(gas_face_to_center_program_); glUniform2i(CachedUniform(gas_face_to_center_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(3, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_WRITE_ONLY, GL_RG16F); + BindImage(3, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } @@ -1622,8 +1621,8 @@ void PowderApp::RunSpawnSandParticles(const BrushConfig& config, const DispatchR glUniform2i(CachedUniform(sand_particle_spawn_program_, "skipCenter"), config.previous_stamp[0], config.previous_stamp[1]); glUniform1ui(CachedUniform(sand_particle_spawn_program_, "spawnSeed"), frame_index_ * 977U + static_cast(brush_x_ * 131 + brush_y_)); glUniform1f(CachedUniform(sand_particle_spawn_program_, "velocitySpread"), config.velocity_spread); - glBindImageTexture(0, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16UI); - glBindImageTexture(1, sand_state_.occupancy, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, CurrentMaterial(), GL_READ_ONLY, GL_R16UI); + BindImage(1, sand_state_.occupancy, GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, sand_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, sand_state_.spawn_cursor); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, debug_state_.counters); @@ -1666,8 +1665,8 @@ void PowderApp::RunSpawnWaterParticles(const BrushConfig& config, const Dispatch glUniform1f(CachedUniform(water_particle_spawn_program_, "tickDt"), config_.fixed_dt); glUniform2f(CachedUniform(water_particle_spawn_program_, "gravity"), 0.0F, config_.water_gravity); if (config.oil) oil_present_ = true; - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(1, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, water_state_.spawn_cursor); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, debug_state_.counters); @@ -1723,13 +1722,13 @@ void PowderApp::RunClearFullRes(const DispatchRegion& region) { glUniform1i(CachedUniform(spawn_full_program_, "clearWater"), 1); glUniform1i(CachedUniform(spawn_full_program_, "clearMode"), 1); - glBindImageTexture(0, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16UI); - glBindImageTexture(3, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentWaterVelocity(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(5, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(6, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(7, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); - glBindImageTexture(1, water_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); + BindImage(0, CurrentMaterial(), GL_READ_WRITE, GL_R16UI); + BindImage(3, CurrentWaterAmount(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentWaterVelocity(), GL_READ_WRITE, GL_RG16F); + BindImage(5, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(6, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(7, sand_state_.packed, GL_READ_WRITE, GL_R8UI); + BindImage(1, water_state_.packed, GL_READ_WRITE, GL_R8UI); DispatchRegionGrid(region); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -1756,24 +1755,24 @@ void PowderApp::RunClearGas(const DispatchRegion& region) { glUniform1i(CachedUniform(spawn_gas_program_, "blockedByWater"), 0); glUniform1i(CachedUniform(spawn_gas_program_, "clearMode"), 1); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(3, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(5, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(6, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(7, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); + BindImage(0, CurrentGasU(), GL_READ_WRITE, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_WRITE, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_READ_WRITE, GL_RG16F); + BindImage(3, CurrentSmokeDen(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(5, CurrentFuel(), GL_READ_WRITE, GL_R16F); + BindImage(6, CurrentOxidizer(), GL_READ_WRITE, GL_R16F); + BindImage(7, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); DispatchRegionGrid(region); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); glUseProgram(gas_face_to_center_program_); glUniform2i(CachedUniform(gas_face_to_center_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(3, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_WRITE_ONLY, GL_RG16F); + BindImage(3, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } @@ -1781,10 +1780,10 @@ void PowderApp::RunClearGas(const DispatchRegion& region) { void PowderApp::RunBoundaryPass() { glUseProgram(boundary_build_program_); glUniform2i(CachedUniform(boundary_build_program_, "gridSize"), kGridWidth, kGridHeight); - glBindImageTexture(0, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16UI); - glBindImageTexture(1, sand_state_.occupancy, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8UI); - glBindImageTexture(3, boundary_state_.mask_static, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8UI); + BindImage(0, CurrentMaterial(), GL_READ_ONLY, GL_R16UI); + BindImage(1, sand_state_.occupancy, GL_READ_ONLY, GL_R16F); + BindImage(2, boundary_state_.mask_full, GL_WRITE_ONLY, GL_R8UI); + BindImage(3, boundary_state_.mask_static, GL_WRITE_ONLY, GL_R8UI); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -1792,8 +1791,8 @@ void PowderApp::RunBoundaryPass() { if (boundary_state_.static_dirty) { glUseProgram(boundary_seed_program_); glUniform2i(CachedUniform(boundary_seed_program_, "gridSize"), kGridWidth, kGridHeight); - glBindImageTexture(0, boundary_state_.mask_static, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, boundary_state_.seed_full_a, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG32I); + BindImage(0, boundary_state_.mask_static, GL_READ_ONLY, GL_R8UI); + BindImage(1, boundary_state_.seed_full_a, GL_WRITE_ONLY, GL_RG32I); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -1806,8 +1805,8 @@ void PowderApp::RunBoundaryPass() { glUseProgram(boundary_jumpflood_program_); glUniform2i(CachedUniform(boundary_jumpflood_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1i(CachedUniform(boundary_jumpflood_program_, "stepSize"), step); - glBindImageTexture(0, current_seed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32I); - glBindImageTexture(1, next_seed, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG32I); + BindImage(0, current_seed, GL_READ_ONLY, GL_RG32I); + BindImage(1, next_seed, GL_WRITE_ONLY, GL_RG32I); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); std::swap(current_seed, next_seed); @@ -1818,9 +1817,9 @@ void PowderApp::RunBoundaryPass() { glUniform2i(CachedUniform(boundary_distance_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1f(CachedUniform(boundary_distance_program_, "maxDistance"), std::sqrt(static_cast(kGridWidth * kGridWidth + kGridHeight * kGridHeight))); - glBindImageTexture(0, boundary_state_.mask_static, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, current_seed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32I); - glBindImageTexture(2, boundary_state_.sdf_static, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); + BindImage(0, boundary_state_.mask_static, GL_READ_ONLY, GL_R8UI); + BindImage(1, current_seed, GL_READ_ONLY, GL_RG32I); + BindImage(2, boundary_state_.sdf_static, GL_WRITE_ONLY, GL_R16F); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); boundary_state_.static_dirty = false; @@ -1833,10 +1832,10 @@ void PowderApp::RunBoundaryPass() { // Half the rest volume: a settled body, not spray or a thin film. The liquid volume is the previous tick's. glUniform1f(CachedUniform(boundary_downsample_program_, "waterBlockVolume"), 0.5F * config_.water_rest_density * config_.water_amount_per_particle); - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8UI); - glBindImageTexture(2, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, phase_state_.oil_field, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(1, boundary_state_.mask_gas, GL_WRITE_ONLY, GL_R8UI); + BindImage(2, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(3, phase_state_.oil_field, GL_READ_ONLY, GL_RG16F); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } @@ -1919,7 +1918,8 @@ void PowderApp::RunSandPass(float dt) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, sand_state_.tile_flags); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); if (particle_groups() > 0U) glDispatchCompute(particle_groups(), 1, 1); // zero groups is an error on some drivers - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); }; // Sleep-packing: tally still grains per cell and decide pack/wake per cell before compaction, which drops exactly @@ -1942,19 +1942,20 @@ void PowderApp::RunSandPass(float dt) { glUniform1f(CachedUniform(sand_pack_program_, "dt"), dt); glUniform1i(CachedUniform(sand_pack_program_, "maxParticles"), kMaxSandParticles); glUniform1ui(CachedUniform(sand_pack_program_, "seed"), frame_index_ * 2654435761U + 12345U); - glBindImageTexture(0, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); - glBindImageTexture(1, sand_state_.packed_f, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); - glBindImageTexture(2, boundary_state_.sdf_static, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, CurrentWaterVelocity(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(5, sand_state_.grid_velocity, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); + BindImage(0, sand_state_.packed, GL_READ_WRITE, GL_R8UI); + BindImage(1, sand_state_.packed_f, GL_READ_WRITE, GL_RGBA32F); + BindImage(2, boundary_state_.sdf_static, GL_READ_ONLY, GL_R16F); + BindImage(3, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(4, CurrentWaterVelocity(), GL_READ_ONLY, GL_RG16F); + BindImage(5, sand_state_.grid_velocity, GL_READ_ONLY, GL_RG32F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, sand_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, sand_state_.cell_tally); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, sand_state_.cell_fsum); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, sand_state_.spawn_cursor); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGridWidth, kGridHeight); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); }; if (sleeping) { glUseProgram(sand_sleep_program_); @@ -2050,12 +2051,12 @@ void PowderApp::RunSandPass(float dt) { glUniform1f(CachedUniform(sand_grid_update_program_, "frictionAlpha"), config_.sand_friction_alpha); glUniform1f(CachedUniform(sand_grid_update_program_, "cohesion"), config_.sand_cohesion); glUniform1i(CachedUniform(sand_grid_update_program_, "skipEmptyTiles"), substep + 1 < sand_state_.substeps ? 1 : 0); - glBindImageTexture(0, boundary_state_.sdf_static, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentWaterVelocity(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(3, sand_state_.grid_velocity, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG32F); - glBindImageTexture(4, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(5, sand_state_.packed_f, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F); + BindImage(0, boundary_state_.sdf_static, GL_READ_ONLY, GL_R16F); + BindImage(1, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentWaterVelocity(), GL_READ_ONLY, GL_RG16F); + BindImage(3, sand_state_.grid_velocity, GL_WRITE_ONLY, GL_RG32F); + BindImage(4, sand_state_.packed, GL_READ_ONLY, GL_R8UI); + BindImage(5, sand_state_.packed_f, GL_READ_ONLY, GL_RGBA32F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, sand_state_.grid_mass); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, sand_state_.grid_momentum_x); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, sand_state_.grid_momentum_y); @@ -2064,7 +2065,8 @@ void PowderApp::RunSandPass(float dt) { if (grid_box[2] > grid_box[0] && grid_box[3] > grid_box[1]) { DispatchGrid(grid_box[2] - tile_x0, grid_box[3] - tile_y0); } - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | + GL_SHADER_STORAGE_BARRIER_BIT); glUseProgram(sand_particle_step_program_); glUniform2i(CachedUniform(sand_particle_step_program_, "gridSize"), kGridWidth, kGridHeight); @@ -2090,15 +2092,16 @@ void PowderApp::RunSandPass(float dt) { glUniform1f(CachedUniform(sand_particle_step_program_, "waterFullVolume"), 4.0F * config_.water_amount_per_particle); glUniform1ui(CachedUniform(sand_particle_step_program_, "roundSeed"), frame_index_ * 2654435761U + static_cast(substep)); - glBindImageTexture(0, boundary_state_.sdf_static, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, sand_state_.grid_velocity, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); - glBindImageTexture(2, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, boundary_state_.sdf_static, GL_READ_ONLY, GL_R16F); + BindImage(1, sand_state_.grid_velocity, GL_READ_ONLY, GL_RG32F); + BindImage(2, sand_state_.packed, GL_READ_ONLY, GL_R8UI); + BindImage(3, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, sand_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, sand_state_.grid_mass); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); if (particle_groups() > 0U) glDispatchCompute(particle_groups(), 1, 1); // zero groups is an error on some drivers - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); } // Moisture is gathered only here, so like the mass it is zero outside the rows this clear covers. @@ -2115,10 +2118,10 @@ void PowderApp::RunSandPass(float dt) { glUniform2i(CachedUniform(sand_rasterize_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1f(CachedUniform(sand_rasterize_program_, "massScale"), 4096.0F); glUniform1f(CachedUniform(sand_rasterize_program_, "referenceDensity"), config_.sand_reference_density); - glBindImageTexture(0, sand_state_.occupancy, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(1, sand_state_.packed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(2, sand_state_.moisture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8); - glBindImageTexture(3, sand_state_.packed_f, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F); + BindImage(0, sand_state_.occupancy, GL_WRITE_ONLY, GL_R16F); + BindImage(1, sand_state_.packed, GL_READ_ONLY, GL_R8UI); + BindImage(2, sand_state_.moisture, GL_WRITE_ONLY, GL_R8); + BindImage(3, sand_state_.packed_f, GL_READ_ONLY, GL_RGBA32F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, sand_state_.grid_mass); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, sand_state_.grid_moisture); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); @@ -2159,7 +2162,7 @@ void PowderApp::RunWaterPass(float dt) { glUniform1i(CachedUniform(water_particle_p2g_program_, "maxParticles"), static_cast(water_state_.live_count)); glUniform1f(CachedUniform(water_particle_p2g_program_, "velocityScale"), 4096.0F); glUniform1f(CachedUniform(water_particle_p2g_program_, "weightScale"), 4096.0F); - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, water_state_.u_face_weights); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, water_state_.v_face_weights); @@ -2168,7 +2171,8 @@ void PowderApp::RunWaterPass(float dt) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, water_state_.cell_counts); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); if (water_state_.live_count > 0U) glDispatchCompute((water_state_.live_count + 255U) / 256U, 1, 1); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); }; const auto build_mac_faces = [this](bool previous_grid) { @@ -2176,14 +2180,12 @@ void PowderApp::RunWaterPass(float dt) { glUniform2i(CachedUniform(water_mac_build_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1f(CachedUniform(water_mac_build_program_, "velocityScale"), 4096.0F); glUniform1f(CachedUniform(water_mac_build_program_, "weightScale"), 4096.0F); - glBindImageTexture(0, previous_grid ? water_state_.mac_u_prev : water_state_.mac_u, - 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(1, previous_grid ? water_state_.mac_v_prev : water_state_.mac_v, - 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(2, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, sand_state_.grid_velocity, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); + BindImage(0, previous_grid ? water_state_.mac_u_prev : water_state_.mac_u, GL_WRITE_ONLY, GL_R16F); + BindImage(1, previous_grid ? water_state_.mac_v_prev : water_state_.mac_v, GL_WRITE_ONLY, GL_R16F); + BindImage(2, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(3, sand_state_.grid_velocity, GL_READ_ONLY, GL_RG32F); glUniform1f(CachedUniform(water_mac_build_program_, "restMass"), config_.water_rest_density); - glBindImageTexture(4, water_state_.packed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(4, water_state_.packed, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, water_state_.cell_counts); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.u_face_weights); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, water_state_.v_face_weights); @@ -2191,7 +2193,8 @@ void PowderApp::RunWaterPass(float dt) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, water_state_.v_face_velocity_sums); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGridWidth + 1, kGridHeight + 1); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | + GL_SHADER_STORAGE_BARRIER_BIT); }; const auto finalize_particle_fields = [this]() { @@ -2203,10 +2206,10 @@ void PowderApp::RunWaterPass(float dt) { glUniform1f(CachedUniform(water_particle_finalize_program_, "surfaceLevel"), config_.water_surface_level); glUniform1f(CachedUniform(water_particle_finalize_program_, "restMass"), config_.water_rest_density); glUniform1f(CachedUniform(water_particle_finalize_program_, "liquidPhiThreshold"), config_.water_liquid_phi_threshold); - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(2, water_state_.liquid_phi, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(3, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(1, water_state_.liquid_volume, GL_WRITE_ONLY, GL_R16F); + BindImage(2, water_state_.liquid_phi, GL_WRITE_ONLY, GL_R16F); + BindImage(3, CurrentWaterAmount(), GL_WRITE_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.cell_counts); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -2215,10 +2218,10 @@ void PowderApp::RunWaterPass(float dt) { const auto finalize_mac_faces = [this]() { glUseProgram(water_mac_finalize_program_); glUniform2i(CachedUniform(water_mac_finalize_program_, "gridSize"), kGridWidth, kGridHeight); - glBindImageTexture(0, water_state_.mac_u, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, water_state_.mac_v, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, CurrentWaterVelocity(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); + BindImage(0, water_state_.mac_u, GL_READ_ONLY, GL_R16F); + BindImage(1, water_state_.mac_v, GL_READ_ONLY, GL_R16F); + BindImage(2, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(3, CurrentWaterVelocity(), GL_WRITE_ONLY, GL_RG16F); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); }; @@ -2239,18 +2242,19 @@ void PowderApp::RunWaterPass(float dt) { glUniform1f(CachedUniform(water_pack_program_, "particleMass"), config_.water_particle_mass); glUniform1i(CachedUniform(water_pack_program_, "maxParticles"), kMaxWaterParticles); glUniform1ui(CachedUniform(water_pack_program_, "seed"), frame_index_ * 2654435761U + 777U); - glBindImageTexture(0, water_state_.packed, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8UI); - glBindImageTexture(1, water_state_.wake_velocity, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(2, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, water_state_.mac_u, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, water_state_.mac_v, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, water_state_.packed, GL_READ_WRITE, GL_R8UI); + BindImage(1, water_state_.wake_velocity, GL_READ_WRITE, GL_RG16F); + BindImage(2, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(3, water_state_.mac_u, GL_READ_ONLY, GL_R16F); + BindImage(4, water_state_.mac_v, GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, water_state_.cell_tally); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, water_state_.cell_counts); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, water_state_.spawn_cursor); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGridWidth, kGridHeight); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); }; if (packing) { glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT); @@ -2330,16 +2334,17 @@ void PowderApp::RunWaterPass(float dt) { glUniform1f(CachedUniform(water_divergence_program_, "weightScale"), 4096.0F); glUniform1f(CachedUniform(water_divergence_program_, "restDensity"), config_.water_rest_density); glUniform1f(CachedUniform(water_divergence_program_, "densityRate"), config_.water_density_rate); - glBindImageTexture(0, water_state_.mac_u_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, water_state_.mac_v_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentWaterDivergence(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(3, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(4, water_state_.liquid_phi, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(5, water_state_.packed, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, water_state_.mac_u_prev, GL_READ_ONLY, GL_R16F); + BindImage(1, water_state_.mac_v_prev, GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentWaterDivergence(), GL_WRITE_ONLY, GL_R16F); + BindImage(3, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(4, water_state_.liquid_phi, GL_READ_ONLY, GL_R16F); + BindImage(5, water_state_.packed, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.cell_counts); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGridWidth, kGridHeight); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | + GL_SHADER_STORAGE_BARRIER_BIT); RemovePocketMeans(false); RunWaterProjection(); @@ -2350,16 +2355,17 @@ void PowderApp::RunWaterPass(float dt) { glUniform1f(CachedUniform(water_project_program_, "liquidPhiThreshold"), config_.water_liquid_phi_threshold); glUniform1f(CachedUniform(water_project_program_, "sandLeak"), kSandLeak); glUniform1f(CachedUniform(water_project_program_, "thetaMin"), config_.water_ghost_theta_min); - glBindImageTexture(0, water_state_.mac_u_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, water_state_.mac_v_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentWaterPressure(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(3, water_state_.mac_u, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(4, water_state_.mac_v, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(5, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(6, water_state_.liquid_phi, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, water_state_.mac_u_prev, GL_READ_ONLY, GL_R16F); + BindImage(1, water_state_.mac_v_prev, GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentWaterPressure(), GL_READ_ONLY, GL_R32F); + BindImage(3, water_state_.mac_u, GL_WRITE_ONLY, GL_R16F); + BindImage(4, water_state_.mac_v, GL_WRITE_ONLY, GL_R16F); + BindImage(5, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(6, water_state_.liquid_phi, GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGridWidth + 1, kGridHeight + 1); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | + GL_SHADER_STORAGE_BARRIER_BIT); // Wake decisions read the tick's final projected faces; the next tick's phase 1 applies them. if (packing && step == substeps - 1) pack_phase(2); @@ -2376,15 +2382,15 @@ void PowderApp::RunWaterPass(float dt) { glUniform1i(CachedUniform(water_particle_step_program_, "maxParticles"), static_cast(water_state_.live_count)); glUniform1f(CachedUniform(water_particle_step_program_, "sandDrag"), config_.sand_drag * config_.sand_reference_density); - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, water_state_.mac_u_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, water_state_.mac_v_prev, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, water_state_.mac_u, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, water_state_.mac_v, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(5, sand_state_.grid_velocity, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); - glBindImageTexture(6, sand_state_.occupancy, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(7, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(8, phase_state_.oil_field, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(1, water_state_.mac_u_prev, GL_READ_ONLY, GL_R16F); + BindImage(2, water_state_.mac_v_prev, GL_READ_ONLY, GL_R16F); + BindImage(3, water_state_.mac_u, GL_READ_ONLY, GL_R16F); + BindImage(4, water_state_.mac_v, GL_READ_ONLY, GL_R16F); + BindImage(5, sand_state_.grid_velocity, GL_READ_ONLY, GL_RG32F); + BindImage(6, sand_state_.occupancy, GL_READ_ONLY, GL_R16F); + BindImage(7, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(8, phase_state_.oil_field, GL_READ_ONLY, GL_RG16F); glUniform1f(CachedUniform(water_particle_step_program_, "oilDrift"), oil_present_ ? kOilDrift : 0.0F); glUniform1f(CachedUniform(water_particle_step_program_, "restVolume"), config_.water_rest_density * config_.water_amount_per_particle); @@ -2397,7 +2403,8 @@ void PowderApp::RunWaterPass(float dt) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, debug_state_.counters); if (water_state_.live_count > 0U) glDispatchCompute((water_state_.live_count + 255U) / 256U, 1, 1); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); } build_particle_fields(); @@ -2420,10 +2427,10 @@ void PowderApp::BindWaterSolver() { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, water_state_.pressure_sums); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, water_state_.pressure_levels); glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, water_state_.pressure_tiles); - glBindImageTexture(0, CurrentWaterDivergence(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, CurrentWaterPressure(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(2, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, water_state_.liquid_phi, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, CurrentWaterDivergence(), GL_READ_ONLY, GL_R16F); + BindImage(1, CurrentWaterPressure(), GL_READ_WRITE, GL_R32F); + BindImage(2, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(3, water_state_.liquid_phi, GL_READ_ONLY, GL_R16F); glUseProgram(pressure_mg_program_); glUniform2i(CachedUniform(pressure_mg_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1i(CachedUniform(pressure_mg_program_, "levels"), kWaterMultigridLevels); @@ -2493,8 +2500,8 @@ void PowderApp::RemovePocketMeans(bool gas) { glUniform1f(CachedUniform(pressure_pockets_program_, "liquidPhiThreshold"), config_.water_liquid_phi_threshold); glBindImageTexture(wide ? 1 : 0, rhs, 0, GL_FALSE, 0, GL_READ_WRITE, rhs_format); glBindImageTexture(wide ? 0 : 1, 0, 0, GL_FALSE, 0, GL_READ_WRITE, wide ? GL_R16F : GL_R32F); - glBindImageTexture(2, gas ? gas_state_.level_kind[0] : boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, water_state_.liquid_phi, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); + BindImage(2, gas ? gas_state_.level_kind[0] : boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(3, water_state_.liquid_phi, GL_READ_WRITE, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.pockets); const GLint phase = CachedUniform(pressure_pockets_program_, "phase"); for (int p = 0; p < 5; ++p) { @@ -2534,14 +2541,15 @@ void PowderApp::RunWaterProjection() { glUseProgram(program); glUniform1i(location, phase); glDispatchCompute(groups, 1, 1); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); }; const auto tile_step = [&](int phase) { glUniform1i(cg_phase, phase); glDispatchComputeIndirect(0); // A stopped solve zeroes the group counts; the command barrier lets the next dispatches launch no workgroups. glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT | - (phase == 2 ? GL_SHADER_IMAGE_ACCESS_BARRIER_BIT : 0U)); + (phase == 2 ? GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT : 0U)); }; int dispatched = 0; // Each iteration ends with phase 1, which tests it and already forms the next direction and A*direction. The sum @@ -2562,7 +2570,8 @@ void PowderApp::RunWaterProjection() { glUseProgram(program); glUniform1i(location, phase); glDispatchComputeIndirect(offset); - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); }; // Forms b - Ap from the stored pressure and starts CG on it. A restart keeps the tile list and iteration count and // is queued blind: phase 10 arms it only for a solve that has stopped after iterating, and every pass of it @@ -2652,9 +2661,7 @@ void PowderApp::RunWaterProjection() { glUniform1f(CachedUniform(pressure_residual_program_, "thetaMin"), config_.water_ghost_theta_min); glUniform1f(CachedUniform(pressure_residual_program_, "sandLeak"), kSandLeak); glUniform1f(CachedUniform(pressure_residual_program_, "liquidPhiThreshold"), config_.water_liquid_phi_threshold); - glUniform1i(CachedUniform(pressure_residual_program_, "pressureTex"), 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, CurrentWaterPressure()); + BindImage(1, CurrentWaterPressure(), GL_READ_ONLY, GL_R32F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, debug_state_.counters); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); @@ -2700,21 +2707,21 @@ void PowderApp::RunSmokePass(float dt, bool project) { glUniform2i(CachedUniform(gas_velocity_advect_program_, "gasSize"), kGasWidth, kGasHeight); glUniform1f(CachedUniform(gas_velocity_advect_program_, "dt"), dt); glUniform1f(CachedUniform(gas_velocity_advect_program_, "maxVelocity"), config_.gas_max_velocity); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(3, gas_state_.mac_u_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(4, gas_state_.mac_v_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(5, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(3, gas_state_.mac_u_tmp, GL_WRITE_ONLY, GL_R32F); + BindImage(4, gas_state_.mac_v_tmp, GL_WRITE_ONLY, GL_R32F); + BindImage(5, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth + 1, kGasHeight + 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); glUseProgram(gas_face_to_center_program_); glUniform2i(CachedUniform(gas_face_to_center_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, gas_state_.mac_u_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, gas_state_.mac_v_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, gas_state_.velocity_center_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(3, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, gas_state_.mac_u_tmp, GL_READ_ONLY, GL_R32F); + BindImage(1, gas_state_.mac_v_tmp, GL_READ_ONLY, GL_R32F); + BindImage(2, gas_state_.velocity_center_tmp, GL_WRITE_ONLY, GL_RG16F); + BindImage(3, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -2722,18 +2729,18 @@ void PowderApp::RunSmokePass(float dt, bool project) { glUniform2i(CachedUniform(smoke_advect_program_, "gasSize"), kGasWidth, kGasHeight); glUniform1f(CachedUniform(smoke_advect_program_, "dt"), dt); glUniform1f(CachedUniform(smoke_advect_program_, "maxVelocity"), config_.gas_max_velocity); - glBindImageTexture(0, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(1, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(5, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(6, gas_state_.smoke_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(7, gas_state_.temperature_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(8, gas_state_.fuel_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(9, gas_state_.oxidizer_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(10, gas_state_.reaction_tmp, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(11, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(1, CurrentSmokeDen(), GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentFuel(), GL_READ_ONLY, GL_R16F); + BindImage(4, CurrentOxidizer(), GL_READ_ONLY, GL_R16F); + BindImage(5, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(0, gas_state_.smoke_tmp, GL_WRITE_ONLY, GL_R16F); + BindImage(1, gas_state_.temperature_tmp, GL_WRITE_ONLY, GL_R16F); + BindImage(2, gas_state_.fuel_tmp, GL_WRITE_ONLY, GL_R16F); + BindImage(3, gas_state_.oxidizer_tmp, GL_WRITE_ONLY, GL_R16F); + BindImage(4, gas_state_.reaction_tmp, GL_WRITE_ONLY, GL_RG16F); + BindImage(11, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -2745,24 +2752,24 @@ void PowderApp::RunSmokePass(float dt, bool project) { std::pow(config_.gas_density_dissipation, dt * 60.0F)); glUniform1f(CachedUniform(smoke_correct_program_, "scalarDissipation"), std::pow(config_.gas_scalar_dissipation, dt * 60.0F)); - glBindImageTexture(0, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(1, gas_state_.velocity_center_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(2, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(5, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(6, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(7, gas_state_.smoke_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(8, gas_state_.temperature_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(9, gas_state_.fuel_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(10, gas_state_.oxidizer_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(11, gas_state_.reaction_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(12, NextSmokeDen(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(13, NextTemperature(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(14, NextFuel(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(15, NextOxidizer(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(16, NextReactionRate(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(17, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(1, gas_state_.velocity_center_tmp, GL_READ_ONLY, GL_RG16F); + BindImage(2, CurrentSmokeDen(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(4, CurrentFuel(), GL_READ_ONLY, GL_R16F); + BindImage(5, CurrentOxidizer(), GL_READ_ONLY, GL_R16F); + BindImage(6, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(7, gas_state_.smoke_tmp, GL_READ_ONLY, GL_R16F); + BindImage(8, gas_state_.temperature_tmp, GL_READ_ONLY, GL_R16F); + BindImage(9, gas_state_.fuel_tmp, GL_READ_ONLY, GL_R16F); + BindImage(10, gas_state_.oxidizer_tmp, GL_READ_ONLY, GL_R16F); + BindImage(11, gas_state_.reaction_tmp, GL_READ_ONLY, GL_RG16F); + BindImage(0, NextSmokeDen(), GL_WRITE_ONLY, GL_R16F); + BindImage(1, NextTemperature(), GL_WRITE_ONLY, GL_R16F); + BindImage(2, NextFuel(), GL_WRITE_ONLY, GL_R16F); + BindImage(3, NextOxidizer(), GL_WRITE_ONLY, GL_R16F); + BindImage(4, NextReactionRate(), GL_WRITE_ONLY, GL_RG16F); + BindImage(17, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -2776,17 +2783,17 @@ void PowderApp::RunSmokePass(float dt, bool project) { glUniform1f(CachedUniform(gas_velocity_correct_program_, "buoyancySmoke"), config_.gas_buoyancy_smoke); glUniform1f(CachedUniform(gas_velocity_correct_program_, "maxVelocity"), config_.gas_max_velocity); glUniform1f(CachedUniform(gas_velocity_correct_program_, "vorticity"), config_.gas_vorticity); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(3, gas_state_.mac_u_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(4, gas_state_.mac_v_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(5, gas_state_.velocity_center_tmp, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(6, NextSmokeDen(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(7, NextTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(8, NextGasU(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(9, NextGasV(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(10, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(3, gas_state_.mac_u_tmp, GL_READ_ONLY, GL_R32F); + BindImage(4, gas_state_.mac_v_tmp, GL_READ_ONLY, GL_R32F); + BindImage(5, gas_state_.velocity_center_tmp, GL_READ_ONLY, GL_RG16F); + BindImage(6, NextSmokeDen(), GL_READ_ONLY, GL_R16F); + BindImage(7, NextTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(0, NextGasU(), GL_WRITE_ONLY, GL_R32F); + BindImage(1, NextGasV(), GL_WRITE_ONLY, GL_R32F); + BindImage(10, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGasWidth + 1, kGasHeight + 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -2804,21 +2811,23 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud if (begin) { // The operator, residual checks and projection all use this copy: water, sand and rock keep moving the live mask // between the halves of a tick pair. - glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); glCopyImageSubData(boundary_state_.mask_gas, GL_TEXTURE_2D, 0, 0, 0, 0, gas_state_.mask_solve, GL_TEXTURE_2D, 0, 0, 0, 0, kGasWidth, kGasHeight, 1); - glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT); } const GLuint solve_mask = gas_state_.mask_solve; if (begin) { glUseProgram(smoke_divergence_program_); glUniform2i(CachedUniform(smoke_divergence_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, gas_state_.divergence, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(4, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, gas_state_.divergence, GL_WRITE_ONLY, GL_R32F); + BindImage(4, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); glUniform1f(CachedUniform(smoke_divergence_program_, "expansion"), config_.gas_expansion); - glBindImageTexture(3, solve_mask, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(3, solve_mask, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } @@ -2840,11 +2849,10 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud sizes[std::max(level - 1, 0)]); glUniform2i(CachedUniform(gas_pressure_kind_program_, "coarseSize"), sizes[level], sizes[level]); glUniform1i(CachedUniform(gas_pressure_kind_program_, "fromMask"), level == 0 ? 1 : 0); - glBindImageTexture(0, level == 0 ? solve_mask : gas_state_.level_kind[level - 1], 0, GL_FALSE, 0, - GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, gas_state_.level_kind[level], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8UI); + BindImage(0, level == 0 ? solve_mask : gas_state_.level_kind[level - 1], GL_READ_ONLY, GL_R8UI); + BindImage(1, gas_state_.level_kind[level], GL_WRITE_ONLY, GL_R8UI); DispatchGrid(sizes[level], sizes[level]); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } // Only burning (fire, wood, lit oil) adds a source; wall faces carry no flux, so without one a sealed pocket's // divergence already sums to zero. @@ -2863,11 +2871,9 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform1f(CachedUniform(pressure_residual_program_, "sandLeak"), kSandLeak); glUniform1f(CachedUniform(pressure_residual_program_, "liquidPhiThreshold"), 0.0F); glUniform1i(CachedUniform(pressure_residual_program_, "wideRhs"), 1); - glBindImageTexture(4, gas_state_.divergence, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glUniform1i(CachedUniform(pressure_residual_program_, "pressureTex"), 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, gas_state_.pressure_a); - glBindImageTexture(2, solve_mask, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(4, gas_state_.divergence, GL_READ_ONLY, GL_R32F); + BindImage(1, gas_state_.pressure_a, GL_READ_ONLY, GL_R32F); + BindImage(2, solve_mask, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, debug_state_.counters); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, gas_state_.pressure_check); DispatchGrid(kGasWidth, kGasHeight); @@ -2910,14 +2916,14 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform1f(CachedUniform(gas_pressure_relax_program_, "sandLeak"), kSandLeak); glUniform1i(CachedUniform(gas_pressure_relax_program_, "fineHeight"), kGasHeight); glUniform1i(CachedUniform(gas_pressure_relax_program_, "bottomSweeps"), level == kGasLevels - 1 ? sweeps : 0); - glBindImageTexture(0, level_rhs(level), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(3, gas_state_.level_kind[level], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, level_rhs(level), GL_READ_ONLY, GL_R32F); + BindImage(3, gas_state_.level_kind[level], GL_READ_ONLY, GL_R8UI); const bool bottom = level == kGasLevels - 1; glUniform1i(CachedUniform(gas_pressure_relax_program_, "smoothSteps"), bottom ? 0 : 2 * sweeps); if (bottom) { - glBindImageTexture(1, level_pressure(level), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); + BindImage(1, level_pressure(level), GL_READ_WRITE, GL_R32F); DispatchGrid(sizes[level], sizes[level]); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); return; } const GLuint from = reverse ? gas_state_.level_smoothed[level] : level_pressure(level); @@ -2925,13 +2931,14 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform1i(CachedUniform(gas_pressure_relax_program_, "parity"), reverse ? 1 : 0); glUniform1i(CachedUniform(gas_pressure_relax_program_, "prolongate"), reverse ? 1 : 0); glUniform2i(CachedUniform(gas_pressure_relax_program_, "coarseSize"), sizes[level + 1], sizes[level + 1]); - glBindImageTexture(1, from, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, to, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(4, gas_state_.level_residual[level], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(5, level_pressure(level + 1), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(6, gas_state_.level_kind[level + 1], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + // pressureTex is a read-write image (the bottom level relaxes in place), so this read binds an image unit too. + BindImage(1, from, GL_READ_WRITE, GL_R32F); + BindImage(2, to, GL_WRITE_ONLY, GL_R32F); + BindImage(4, gas_state_.level_residual[level], GL_WRITE_ONLY, GL_R32F); + BindImage(5, level_pressure(level + 1), GL_READ_ONLY, GL_R32F); + BindImage(6, gas_state_.level_kind[level + 1], GL_READ_ONLY, GL_R8UI); DispatchGrid(sizes[level], sizes[level]); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); }; // The preconditioner: one V-cycle from zero for z ~ L^-1 s. const auto v_cycle = [&]() { @@ -2944,17 +2951,17 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform1i(CachedUniform(gas_pressure_restrict_program_, "openTop"), open_top); glUniform1f(CachedUniform(gas_pressure_restrict_program_, "sandLeak"), kSandLeak); glUniform1i(CachedUniform(gas_pressure_restrict_program_, "fineHeight"), kGasHeight); - glBindImageTexture(0, level_rhs(level), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, level_pressure(level), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, level_rhs(level + 1), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(3, gas_state_.level_kind[level], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(4, level_pressure(level + 1), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(5, gas_state_.level_kind[level + 1], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(6, gas_state_.level_residual[level], 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); + BindImage(0, level_rhs(level), GL_READ_ONLY, GL_R32F); + BindImage(1, level_pressure(level), GL_READ_ONLY, GL_R32F); + BindImage(2, level_rhs(level + 1), GL_WRITE_ONLY, GL_R32F); + BindImage(3, gas_state_.level_kind[level], GL_READ_ONLY, GL_R8UI); + BindImage(4, level_pressure(level + 1), GL_WRITE_ONLY, GL_R32F); + BindImage(5, gas_state_.level_kind[level + 1], GL_READ_ONLY, GL_R8UI); + BindImage(6, gas_state_.level_residual[level], GL_READ_WRITE, GL_R32F); // The residual (gatherPass 0) came with the fused pre-smoothing. glUniform1i(CachedUniform(gas_pressure_restrict_program_, "gatherPass"), 1); DispatchGrid(sizes[level + 1], sizes[level + 1]); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } relax_level(kGasLevels - 1, kGasCoarsestSweeps, false); // Prolongation is fused into each level's post-smoothing. @@ -2969,13 +2976,13 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform1f(CachedUniform(gas_cg_program_, "sandLeak"), kSandLeak); glUniform1i(CachedUniform(gas_cg_program_, "partialCount"), partial_count); glUniform1i(CachedUniform(gas_cg_program_, "first"), first ? 1 : 0); - glBindImageTexture(0, gas_state_.divergence, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, gas_state_.pressure_a, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(2, gas_state_.cg_s, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(3, gas_state_.cg_z, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(4, gas_state_.cg_d, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(5, gas_state_.cg_q, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F); - glBindImageTexture(6, gas_state_.level_kind[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, gas_state_.divergence, GL_READ_ONLY, GL_R32F); + BindImage(1, gas_state_.pressure_a, GL_READ_WRITE, GL_R32F); + BindImage(2, gas_state_.cg_s, GL_READ_WRITE, GL_R32F); + BindImage(3, gas_state_.cg_z, GL_READ_WRITE, GL_R32F); + BindImage(4, gas_state_.cg_d, GL_READ_WRITE, GL_R32F); + BindImage(5, gas_state_.cg_q, GL_READ_WRITE, GL_R32F); + BindImage(6, gas_state_.level_kind[0], GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, gas_state_.cg_partials); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, gas_state_.cg_scalars); if (phase == 2 || phase == 5) { @@ -2983,7 +2990,8 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud } else { DispatchGrid(kGasWidth, kGasHeight); } - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | + GL_SHADER_STORAGE_BARRIER_BIT); }; // z = M^-1 s, then rz and the new direction. const auto precondition = [&](bool first) { @@ -3034,22 +3042,22 @@ void PowderApp::RunGasProjectionStage(bool begin, bool finish, int iteration_bud glUniform2i(CachedUniform(smoke_project_program_, "gasSize"), kGasWidth, kGasHeight); glUniform1i(CachedUniform(smoke_project_program_, "openTop"), open_top); glUniform1f(CachedUniform(smoke_project_program_, "sandLeak"), kSandLeak); - glBindImageTexture(0, CurrentGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, CurrentGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, gas_state_.pressure_a, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(3, NextGasU(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(4, NextGasV(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); - glBindImageTexture(5, solve_mask, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, CurrentGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, CurrentGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, gas_state_.pressure_a, GL_READ_ONLY, GL_R32F); + BindImage(3, NextGasU(), GL_WRITE_ONLY, GL_R32F); + BindImage(4, NextGasV(), GL_WRITE_ONLY, GL_R32F); + BindImage(5, solve_mask, GL_READ_ONLY, GL_R8UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGasWidth + 1, kGasHeight + 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); glUseProgram(gas_face_to_center_program_); glUniform2i(CachedUniform(gas_face_to_center_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, NextGasU(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(1, NextGasV(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, NextGasVelocityCenter(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(3, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + BindImage(0, NextGasU(), GL_READ_ONLY, GL_R32F); + BindImage(1, NextGasV(), GL_READ_ONLY, GL_R32F); + BindImage(2, NextGasVelocityCenter(), GL_WRITE_ONLY, GL_RG16F); + BindImage(3, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -3069,17 +3077,17 @@ void PowderApp::RunFireHeatPass(float dt) { glUniform1f(CachedUniform(fire_rd_program_, "smokeYield"), config_.gas_smoke_yield); glUniform1f(CachedUniform(fire_rd_program_, "smokeDissipation"), config_.gas_smoke_dissipation); glUniform1f(CachedUniform(fire_rd_program_, "oxygenRate"), config_.gas_oxygen_rate); - glBindImageTexture(0, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(4, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(5, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(6, NextFuel(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(7, NextOxidizer(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(8, NextTemperature(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(9, NextReactionRate(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(10, NextSmokeDen(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); + BindImage(0, CurrentFuel(), GL_READ_ONLY, GL_R16F); + BindImage(1, CurrentOxidizer(), GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentSmokeDen(), GL_READ_ONLY, GL_R16F); + BindImage(4, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); + BindImage(5, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(0, NextFuel(), GL_WRITE_ONLY, GL_R16F); + BindImage(1, NextOxidizer(), GL_WRITE_ONLY, GL_R16F); + BindImage(2, NextTemperature(), GL_WRITE_ONLY, GL_R16F); + BindImage(3, NextReactionRate(), GL_WRITE_ONLY, GL_RG16F); + BindImage(4, NextSmokeDen(), GL_WRITE_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, debug_state_.counters); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -3096,14 +3104,14 @@ void PowderApp::RunCouplingPass(float dt) { glUniform2i(CachedUniform(coupling_fire_program_, "heatSize"), kGasWidth, kGasHeight); glUniform1f(CachedUniform(coupling_fire_program_, "dt"), dt); glUniform1f(CachedUniform(coupling_fire_program_, "waterCooling"), config_.gas_water_cooling); - glBindImageTexture(0, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(4, CurrentOxidizer(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(5, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(6, phase_state_.oil_field, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(7, sand_state_.occupancy, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); + BindImage(1, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(4, CurrentOxidizer(), GL_READ_WRITE, GL_R16F); + BindImage(5, CurrentFuel(), GL_READ_WRITE, GL_R16F); + BindImage(6, phase_state_.oil_field, GL_READ_ONLY, GL_RG16F); + BindImage(7, sand_state_.occupancy, GL_READ_ONLY, GL_R16F); glUniform1f(CachedUniform(coupling_fire_program_, "sandSmother"), kSandSmother); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -3113,9 +3121,9 @@ void PowderApp::RunCouplingPass(float dt) { glUniform2i(CachedUniform(coupling_heat_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1f(CachedUniform(coupling_heat_program_, "dt"), dt); glUniform1f(CachedUniform(coupling_heat_program_, "waterCooling"), config_.gas_water_cooling * 0.7F); - glBindImageTexture(0, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, phase_state_.oil_field, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); + BindImage(0, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(1, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(2, phase_state_.oil_field, GL_READ_ONLY, GL_RG16F); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); @@ -3138,11 +3146,11 @@ void PowderApp::RunPhasePass(float dt) { glUniform1f(CachedUniform(phase_particles_program_, "ignitionTemperature"), kOilIgnitionTemperature); glUniform1f(CachedUniform(phase_particles_program_, "restVolume"), config_.water_rest_density * config_.water_amount_per_particle); - glBindImageTexture(0, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(1, water_state_.liquid_volume, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(4, phase_state_.oil_field, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); + BindImage(0, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(1, water_state_.liquid_volume, GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(4, phase_state_.oil_field, GL_READ_ONLY, GL_RG16F); glUniform1f(CachedUniform(phase_particles_program_, "flashRate"), oil_present_ ? kWaterFlashRate : 0.0F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, water_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, phase_state_.cell_counts); @@ -3154,7 +3162,7 @@ void PowderApp::RunPhasePass(float dt) { glUseProgram(oil_field_program_); glUniform2i(CachedUniform(oil_field_program_, "gridSize"), kGridWidth, kGridHeight); glUniform1f(CachedUniform(oil_field_program_, "restDensity"), config_.water_rest_density); - glBindImageTexture(0, phase_state_.oil_field, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); + BindImage(0, phase_state_.oil_field, GL_WRITE_ONLY, GL_RG16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, phase_state_.cell_counts); DispatchGrid(kGridWidth, kGridHeight); @@ -3164,17 +3172,14 @@ void PowderApp::RunPhasePass(float dt) { glUniform1f(CachedUniform(steam_update_program_, "steamRise"), kSteamRise); glUniform1f(CachedUniform(steam_update_program_, "steamCondense"), kSteamCondense); glUniform1f(CachedUniform(steam_update_program_, "steamPerParticle"), kSteamPerParticle); - glBindImageTexture(0, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(1, phase_state_.steam_ping ? phase_state_.steam_b : phase_state_.steam_a, 0, GL_FALSE, 0, - GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(2, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(5, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(6, CurrentOxidizer(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, CurrentSteam()); - glUniform1i(CachedUniform(steam_update_program_, "steamSrc"), 0); + BindImage(0, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(1, phase_state_.steam_ping ? phase_state_.steam_b : phase_state_.steam_a, GL_WRITE_ONLY, GL_R16F); + BindImage(2, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); + BindImage(3, CurrentFuel(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(5, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(6, CurrentOxidizer(), GL_WRITE_ONLY, GL_R16F); + BindImage(7, CurrentSteam(), GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, phase_state_.gas_sources); DispatchGrid(kGasWidth, kGasHeight); glBindTexture(GL_TEXTURE_2D, 0); @@ -3235,12 +3240,12 @@ void PowderApp::RunWoodPass() { glUniform1f(CachedUniform(wood_burn_program_, "ignitionTemperature"), 0.35F); // About 0.8 s to catch while hot, so fire creeps along a beam instead of jumping its length in one tick. glUniform1f(CachedUniform(wood_burn_program_, "igniteChance"), 0.02F); - glBindImageTexture(0, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16UI); - glBindImageTexture(1, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(3, CurrentFuel(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_R16F); - glBindImageTexture(4, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16F); - glBindImageTexture(5, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, CurrentMaterial(), GL_READ_WRITE, GL_R16UI); + BindImage(1, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); + BindImage(2, CurrentTemperature(), GL_READ_WRITE, GL_R16F); + BindImage(3, CurrentFuel(), GL_READ_WRITE, GL_R16F); + BindImage(4, CurrentReactionRate(), GL_READ_WRITE, GL_RG16F); + BindImage(5, CurrentWaterAmount(), GL_READ_ONLY, GL_R16F); for (int mode = 0; mode < 2; ++mode) { glUniform1i(CachedUniform(wood_burn_program_, "mode"), mode); DispatchGrid(kGasWidth, kGasHeight); @@ -3261,14 +3266,14 @@ void PowderApp::RunRenderFieldExtractionPass() { glUseProgram(render_extract_program_); glUniform2i(CachedUniform(render_extract_program_, "fullSize"), kGridWidth, kGridHeight); glUniform2i(CachedUniform(render_extract_program_, "gasSize"), kGasWidth, kGasHeight); - glBindImageTexture(0, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(1, CurrentSmokeDen(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(2, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(3, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(4, render_gas_velocity_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RG16F); - glBindImageTexture(5, render_smoke_density_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(6, render_temperature_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); - glBindImageTexture(7, render_fire_emissive_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16F); + BindImage(0, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(1, CurrentSmokeDen(), GL_READ_ONLY, GL_R16F); + BindImage(2, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(3, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(4, render_gas_velocity_, GL_WRITE_ONLY, GL_RG16F); + BindImage(5, render_smoke_density_, GL_WRITE_ONLY, GL_R16F); + BindImage(6, render_temperature_, GL_WRITE_ONLY, GL_R16F); + BindImage(7, render_fire_emissive_, GL_WRITE_ONLY, GL_R16F); DispatchGrid(kGridWidth, kGridHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); } @@ -3283,7 +3288,7 @@ void PowderApp::RunLightBuildPass() { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, render_fire_emissive_); glUniform1i(CachedUniform(light_build_program_, "fireEmissiveTex"), 1); - glBindImageTexture(0, light_texture_, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA16F); + BindImage(0, light_texture_, GL_WRITE_ONLY, GL_RGBA16F); DispatchGrid(kLightWidth, kLightHeight); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT); glBindTexture(GL_TEXTURE_2D, light_texture_); @@ -3299,10 +3304,10 @@ void PowderApp::RunSparkPass(float dt) { glUniform1ui(CachedUniform(spark_pick_program_, "tick"), frame_index_); glUniform1ui(CachedUniform(spark_pick_program_, "bucketCount"), static_cast(kSparkSpawnsPerTick)); glUniform1i(CachedUniform(spark_pick_program_, "woodPresent"), wood_present_ ? 1 : 0); - glBindImageTexture(0, CurrentTemperature(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); - glBindImageTexture(1, CurrentReactionRate(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(2, boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(3, CurrentMaterial(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16UI); + BindImage(0, CurrentTemperature(), GL_READ_ONLY, GL_R16F); + BindImage(1, CurrentReactionRate(), GL_READ_ONLY, GL_RG16F); + BindImage(2, boundary_state_.mask_gas, GL_READ_ONLY, GL_R8UI); + BindImage(3, CurrentMaterial(), GL_READ_ONLY, GL_R16UI); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, spark_state_.buckets); DispatchGrid(kGasWidth, kGasHeight); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); @@ -3315,9 +3320,9 @@ void PowderApp::RunSparkPass(float dt) { glUniform1ui(CachedUniform(spark_step_program_, "capacity"), static_cast(kSparkCapacity)); glUniform1ui(CachedUniform(spark_step_program_, "perTick"), static_cast(kSparkSpawnsPerTick)); glUniform1f(CachedUniform(spark_step_program_, "gravity"), std::abs(config_.water_gravity)); - glBindImageTexture(0, CurrentGasVelocityCenter(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RG16F); - glBindImageTexture(1, boundary_state_.mask_full, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); - glBindImageTexture(2, CurrentWaterAmount(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_R16F); + BindImage(0, CurrentGasVelocityCenter(), GL_READ_ONLY, GL_RG16F); + BindImage(1, boundary_state_.mask_full, GL_READ_ONLY, GL_R8UI); + BindImage(2, CurrentWaterAmount(), GL_READ_ONLY, GL_R16F); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, spark_state_.particles); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, spark_state_.buckets); glDispatchCompute((kSparkCapacity + 63U) / 64U, 1, 1); diff --git a/tests/AppLifecycleTests.cpp b/tests/AppLifecycleTests.cpp index c39f4a9..bf96c12 100644 --- a/tests/AppLifecycleTests.cpp +++ b/tests/AppLifecycleTests.cpp @@ -963,12 +963,13 @@ struct AppLifecycleTests { glUniform1i(glGetUniformLocation(program, "useLiquidMask"), 0); glUniform1i(glGetUniformLocation(program, "collectStats"), 0); glUniform1f(glGetUniformLocation(program, "residualThreshold"), threshold); - glUniform1i(glGetUniformLocation(program, "pressureTex"), 0); - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, app.gas_state_.pressure_a); glUniform1i(glGetUniformLocation(program, "wideRhs"), 1); - glBindImageTexture(4, app.gas_state_.divergence, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); - glBindImageTexture(2, app.boundary_state_.mask_gas, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI); + glActiveTexture(GL_TEXTURE0 + 4); + glBindTexture(GL_TEXTURE_2D, app.gas_state_.divergence); + glActiveTexture(GL_TEXTURE0 + 2); + glBindTexture(GL_TEXTURE_2D, app.boundary_state_.mask_gas); const GLuint zero = 0; glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, app.gas_state_.pressure_check); diff --git a/tests/GpuTests.cpp b/tests/GpuTests.cpp index 7b8c0de..9a3aacf 100644 --- a/tests/GpuTests.cpp +++ b/tests/GpuTests.cpp @@ -84,8 +84,15 @@ struct Resources { } }; +// Matches PowderApp: shaders sample what they only read and use image units for writes. Unit 47 is left active so +// textures created or read back later cannot replace a bound input. void BindImage(GLuint binding, GLuint texture, GLenum access, GLenum format) { - glBindImageTexture(binding, texture, 0, GL_FALSE, 0, access, format); + if (access != GL_WRITE_ONLY) { + glActiveTexture(GL_TEXTURE0 + binding); + glBindTexture(GL_TEXTURE_2D, texture); + glActiveTexture(GL_TEXTURE0 + 47); + } + if (access != GL_READ_ONLY) glBindImageTexture(binding, texture, 0, GL_FALSE, 0, access, format); } // water_particle_step samples the sand fields; an empty world leaves the water untouched. @@ -104,7 +111,7 @@ void BindNoSand(Resources& resources, int width, int height) { void Dispatch(int width, int height) { glDispatchCompute(static_cast((width + 15) / 16), static_cast((height + 15) / 16), 1); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT); CheckGl("dispatch test shader"); } @@ -1196,7 +1203,8 @@ void WaterWallPressure() { } else { glDispatchCompute(1, 1, 1); } - glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | + GL_TEXTURE_FETCH_BARRIER_BIT | GL_COMMAND_BARRIER_BIT); }; const auto precondition = [&](int slot, bool initial) { glUseProgram(cg_multigrid); @@ -1348,9 +1356,7 @@ void WaterWallPressure() { glUniform1i(glGetUniformLocation(program, "useLiquidMask"), 1); glUniform1f(glGetUniformLocation(program, "liquidPhiThreshold"), phi_threshold); BindImage(0, chamber_divergence, GL_READ_ONLY, GL_R16F); - glUniform1i(glGetUniformLocation(program, "pressureTex"), 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, pressure); + BindImage(1, pressure, GL_READ_ONLY, GL_R32F); BindImage(2, chamber_boundary, GL_READ_ONLY, GL_R8UI); BindImage(3, chamber_phi, GL_READ_ONLY, GL_R16F); Dispatch(size, size); @@ -1753,10 +1759,10 @@ void SmokeDissipation() { BindImage(0, velocity, GL_READ_ONLY, GL_RG16F); for (GLuint field = 0; field < source.size(); ++field) { BindImage(1 + field, source[field], GL_READ_ONLY, GL_R16F); - BindImage(6 + field, forward[field], GL_WRITE_ONLY, GL_R16F); + BindImage(field, forward[field], GL_WRITE_ONLY, GL_R16F); } BindImage(5, reaction_source, GL_READ_ONLY, GL_RG16F); - BindImage(10, reaction_forward, GL_WRITE_ONLY, GL_RG16F); + BindImage(4, reaction_forward, GL_WRITE_ONLY, GL_RG16F); BindImage(11, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size, size); for (std::size_t field = 0; field < source.size(); ++field) { @@ -1777,11 +1783,11 @@ void SmokeDissipation() { for (GLuint field = 0; field < source.size(); ++field) { BindImage(2 + field, source[field], GL_READ_ONLY, GL_R16F); BindImage(7 + field, forward[field], GL_READ_ONLY, GL_R16F); - BindImage(12 + field, corrected[field], GL_WRITE_ONLY, GL_R16F); + BindImage(field, corrected[field], GL_WRITE_ONLY, GL_R16F); } BindImage(6, reaction_source, GL_READ_ONLY, GL_RG16F); BindImage(11, reaction_forward, GL_READ_ONLY, GL_RG16F); - BindImage(16, reaction_corrected, GL_WRITE_ONLY, GL_RG16F); + BindImage(4, reaction_corrected, GL_WRITE_ONLY, GL_RG16F); BindImage(17, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size, size); std::swap(source, corrected); @@ -1912,8 +1918,8 @@ void GasVelocityDissipation() { BindImage(5, forward_center, GL_READ_ONLY, GL_RG16F); BindImage(6, smoke, GL_READ_ONLY, GL_R16F); BindImage(7, temperature, GL_READ_ONLY, GL_R16F); - BindImage(8, corrected_u, GL_WRITE_ONLY, GL_R32F); - BindImage(9, corrected_v, GL_WRITE_ONLY, GL_R32F); + BindImage(0, corrected_u, GL_WRITE_ONLY, GL_R32F); + BindImage(1, corrected_v, GL_WRITE_ONLY, GL_R32F); BindImage(10, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size + 1, size + 1); std::swap(source_u, corrected_u); @@ -2055,10 +2061,10 @@ void GasWallTransport() { BindImage(0, velocity, GL_READ_ONLY, GL_RG16F); for (GLuint field = 0; field < forward.size(); ++field) { BindImage(1 + field, source, GL_READ_ONLY, GL_R16F); - BindImage(6 + field, forward[field], GL_WRITE_ONLY, GL_R16F); + BindImage(field, forward[field], GL_WRITE_ONLY, GL_R16F); } BindImage(5, reaction_source, GL_READ_ONLY, GL_RG16F); - BindImage(10, reaction_forward, GL_WRITE_ONLY, GL_RG16F); + BindImage(4, reaction_forward, GL_WRITE_ONLY, GL_RG16F); BindImage(11, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size, size); std::array endpoint_forward{}; @@ -2094,11 +2100,11 @@ void GasWallTransport() { for (GLuint field = 0; field < forward.size(); ++field) { BindImage(2 + field, source, GL_READ_ONLY, GL_R16F); BindImage(7 + field, forward[field], GL_READ_ONLY, GL_R16F); - BindImage(12 + field, corrected[field], GL_WRITE_ONLY, GL_R16F); + BindImage(field, corrected[field], GL_WRITE_ONLY, GL_R16F); } BindImage(6, reaction_source, GL_READ_ONLY, GL_RG16F); BindImage(11, reaction_forward, GL_READ_ONLY, GL_RG16F); - BindImage(16, reaction_corrected, GL_WRITE_ONLY, GL_RG16F); + BindImage(4, reaction_corrected, GL_WRITE_ONLY, GL_RG16F); BindImage(17, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size, size); @@ -2178,8 +2184,8 @@ void GasWallTransport() { BindImage(5, bounded_trace ? reverse_velocity : velocity, GL_READ_ONLY, GL_RG16F); BindImage(6, source, GL_READ_ONLY, GL_R16F); BindImage(7, source, GL_READ_ONLY, GL_R16F); - BindImage(8, corrected_u, GL_WRITE_ONLY, GL_R32F); - BindImage(9, corrected_v, GL_WRITE_ONLY, GL_R32F); + BindImage(0, corrected_u, GL_WRITE_ONLY, GL_R32F); + BindImage(1, corrected_v, GL_WRITE_ONLY, GL_R32F); BindImage(10, boundary, GL_READ_ONLY, GL_R8UI); Dispatch(size + 1, size + 1); @@ -2573,9 +2579,7 @@ void PressureInvalid() { glUniform1i(glGetUniformLocation(program, "useLiquidMask"), 0); glUniform1f(glGetUniformLocation(program, "liquidPhiThreshold"), 0.42F); BindImage(0, rhs, GL_READ_ONLY, GL_R16F); - glUniform1i(glGetUniformLocation(program, "pressureTex"), 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, pressure); + BindImage(1, pressure, GL_READ_ONLY, GL_R32F); BindImage(2, boundary, GL_READ_ONLY, GL_R8UI); BindImage(3, rhs, GL_READ_ONLY, GL_R16F); Dispatch(size, size); @@ -2596,6 +2600,7 @@ void PressureInvalid() { Require(counters[0] > 0U, "NaN in a fluid pressure cell must increment nanInfDetected; got 0"); const GLuint pressure32 = resources.Texture(size, size, GL_R32F, GL_RED, GL_FLOAT, zeros.data()); + BindImage(1, pressure32, GL_READ_ONLY, GL_R32F); glBindTexture(GL_TEXTURE_2D, pressure32); glTexSubImage2D(GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RED, GL_FLOAT, &invalid); counters.fill(0U); @@ -2624,7 +2629,7 @@ void PressureInvalid() { BindImage(0, stats_rhs_tex, GL_READ_ONLY, GL_R16F); BindImage(2, stats_mask_tex, GL_READ_ONLY, GL_R8UI); BindImage(3, stats_phi_tex, GL_READ_ONLY, GL_R16F); - glBindTexture(GL_TEXTURE_2D, stats_pressure_tex); + BindImage(1, stats_pressure_tex, GL_READ_ONLY, GL_R32F); const auto read_stats = [&]() { Dispatch(stats_width, stats_height); glBindBuffer(GL_SHADER_STORAGE_BUFFER, stats_buffer); @@ -2733,11 +2738,11 @@ void FireReaction() { glUniform1f(glGetUniformLocation(program, "smokeDissipation"), 0.0F); for (GLuint field = 0; field < source.size(); ++field) { BindImage(field, source[field], GL_READ_ONLY, GL_R16F); - BindImage(field == 3 ? 10 : 6 + field, output[field], GL_WRITE_ONLY, GL_R16F); + BindImage(field == 3 ? 4 : field, output[field], GL_WRITE_ONLY, GL_R16F); } BindImage(4, boundary, GL_READ_ONLY, GL_R8UI); BindImage(5, reaction, GL_READ_ONLY, GL_RG16F); - BindImage(9, reaction_out, GL_WRITE_ONLY, GL_RG16F); + BindImage(3, reaction_out, GL_WRITE_ONLY, GL_RG16F); Dispatch(size, size); std::array, 4> result; for (std::size_t field = 0; field < result.size(); ++field) result[field] = ReadScalar(output[field], size, size); @@ -2788,6 +2793,45 @@ void FireReaction() { Require(failures == 0, "fire reaction/diffusion regressions failed"); } +// Every shader must fit the OpenGL 4.3 minimums (8 image units and output resources, 8 SSBO bindings, 16 texture +// units per stage) so the game runs on GPUs that offer no more, such as NVIDIA and Intel. +void RequireGl43Minimums(GLuint program) { + GLint uniforms = 0; + glGetProgramInterfaceiv(program, GL_UNIFORM, GL_ACTIVE_RESOURCES, &uniforms); + int images = 0, samplers = 0; + for (GLint i = 0; i < uniforms; ++i) { + const GLenum properties[] = {GL_TYPE, GL_LOCATION}; + GLint values[2] = {}; + glGetProgramResourceiv(program, GL_UNIFORM, static_cast(i), 2, properties, 2, nullptr, values); + const GLenum type = static_cast(values[0]); + const bool image = type == GL_IMAGE_2D || type == GL_INT_IMAGE_2D || type == GL_UNSIGNED_INT_IMAGE_2D; + const bool sampler = type == GL_SAMPLER_2D || type == GL_INT_SAMPLER_2D || type == GL_UNSIGNED_INT_SAMPLER_2D; + if (!image && !sampler) continue; + GLint unit = 0; + glGetUniformiv(program, values[1], &unit); + if (image) { + ++images; + Require(unit < 8, "image uniform on unit " + std::to_string(unit)); + } else { + ++samplers; + Require(unit < 80, "sampler on texture unit " + std::to_string(unit)); + } + } + GLint blocks = 0; + glGetProgramInterfaceiv(program, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &blocks); + for (GLint i = 0; i < blocks; ++i) { + const GLenum property = GL_BUFFER_BINDING; + GLint binding = 0; + glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, static_cast(i), 1, &property, 1, nullptr, + &binding); + Require(binding < 8, "storage block on binding " + std::to_string(binding)); + } + Require(images <= 8, std::to_string(images) + " image uniforms"); + Require(samplers <= 16, std::to_string(samplers) + " samplers"); + Require(images + blocks <= 8, std::to_string(images + blocks) + " images and storage blocks"); + CheckGl("query program resources"); +} + void CompileShaders() { std::vector paths; for (const auto& entry : std::filesystem::directory_iterator(kShaderRoot)) { @@ -2801,7 +2845,7 @@ void CompileShaders() { for (const auto& path : paths) { try { Resources resources; - resources.Program(path.filename().string().c_str()); + RequireGl43Minimums(resources.Program(path.filename().string().c_str())); std::cout << " compiled " << path.filename().string() << '\n'; } catch (const std::exception& error) { ++failures; @@ -2814,6 +2858,7 @@ void CompileShaders() { Resources resources; resources.programs.push_back(glutil::CreateProgramFromFiles((kShaderRoot / "fullscreen.vert").string(), (kShaderRoot / fragment).string())); + RequireGl43Minimums(resources.programs.back()); CheckGl("compile render shaders"); std::cout << " compiled fullscreen.vert + " << fragment << '\n'; } catch (const std::exception& error) { diff --git a/tests/SandTests.cpp b/tests/SandTests.cpp index ddbb20e..0e1b742 100644 --- a/tests/SandTests.cpp +++ b/tests/SandTests.cpp @@ -35,13 +35,20 @@ void CheckGl() { } void Barrier() { - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | + glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT); CheckGl(); } +// Matches PowderApp: shaders sample what they only read and use image units for writes. Unit 47 is left active so +// textures created or read back later cannot replace a bound input. void Image(GLuint binding, GLuint texture, GLenum access, GLenum format) { - glBindImageTexture(binding, texture, 0, GL_FALSE, 0, access, format); + if (access != GL_WRITE_ONLY) { + glActiveTexture(GL_TEXTURE0 + binding); + glBindTexture(GL_TEXTURE_2D, texture); + glActiveTexture(GL_TEXTURE0 + 47); + } + if (access != GL_READ_ONLY) glBindImageTexture(binding, texture, 0, GL_FALSE, 0, access, format); } struct SandFixture {