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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto eol=lf
*.png binary
21 changes: 1 addition & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,7 @@ jobs:

Copy-Item $exe.FullName (Join-Path $outDir "powder_game.exe") -Force

$readme = @(
"Powder Game (Windows 11 x64)",
"",
"Run:",
"- Double-click powder_game.exe",
"",
"Controls:",
"- 1: Sand, 2: Water, 3: Solid, 4: Erase",
"- 5: Smoke, 6: Fire, 7: Wood, 8: Oil",
"- [ / ]: Decrease / increase brush radius (1 to 96)",
"- Left mouse: paint",
"- H: show / hide the on-screen help",
"- F1: normal view; F2-F10: debug views",
"- Esc: quit",
"",
"Notes:",
"- powder_game.exe is self-contained (shaders built in, static runtime); run it from any folder.",
"- Requires OpenGL 4.3 and the resource limits listed in the source README."
)
$readme | Set-Content -Path (Join-Path $outDir "README.txt")
Copy-Item packaging/README.txt (Join-Path $outDir "README.txt")

New-Item -ItemType Directory -Force -Path "dist/win11" | Out-Null
Compress-Archive -Path "$outDir/*" -DestinationPath "dist/win11/PowderGame-win11-x64.zip" -Force
Expand Down
24 changes: 3 additions & 21 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,9 @@ jobs:
Copy-Item build/_deps/glad_src-src/LICENSE (Join-Path $licenseDir "GLAD-LICENSE") -Recurse
Copy-Item build/_deps/glad_src-src/glad/files/khrplatform.h $licenseDir

$readme = @(
"Powder Game $env:GITHUB_REF_NAME (Windows 11 x64)",
"",
"Run:",
"- Double-click powder_game.exe",
"",
"Controls:",
"- 1: Sand, 2: Water, 3: Solid, 4: Erase",
"- 5: Smoke, 6: Fire, 7: Wood, 8: Oil",
"- [ / ]: Decrease / increase brush radius (1 to 96)",
"- Left mouse: paint",
"- H: show / hide the on-screen help",
"- F1: normal view; F2-F10: debug views",
"- Esc: quit",
"",
"Notes:",
"- powder_game.exe is self-contained (shaders built in, static runtime); run it from any folder.",
"- Requires OpenGL 4.3 and the resource limits listed in the source README.",
"- See the release notes and source VALIDATION.md for measured limitations."
)
$readme | Set-Content -Path (Join-Path $outDir "README.txt")
$readmePath = Join-Path $outDir "README.txt"
Copy-Item packaging/README.txt $readmePath
Add-Content -Path $readmePath -Value "", "Version: $env:GITHUB_REF_NAME"

New-Item -ItemType Directory -Force -Path "dist/win11" | Out-Null
$binaryZip = "dist/win11/PowderGame-$env:GITHUB_REF_NAME-win11-x64.zip"
Expand Down
513 changes: 1 addition & 512 deletions ROADMAP.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions VALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ This is the current result. The five pass sections that follow it are the work m
tree on a GPU shared with the other passes (their timings are noisy; their correctness numbers stand). Everything after
them is a historical record; where an older section contradicts this one, this section wins.

Line endings: the repository stores LF only (`.gitattributes`, then a renormalization commit). Shader byte hashes recorded
before it differ from the current ones for `shaders/fullscreen.vert` only by line endings.

Tree: the integration is committed as `12c1343`. The evidence below was measured on the tree before that commit, which
was `61e2965` plus the gas scheduling pass (one gas pressure solve split across a tick pair), the water host
synchronisation pass (restart from the true residual armed on the GPU), sand pass 5 (hole collapse, per-grain damp
Expand Down
36 changes: 1 addition & 35 deletions build-win11.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,7 @@ rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"
cp "$BUILD_DIR/powder_game.exe" "$DIST_DIR/"

cat > "$DIST_DIR/README.txt" <<'TXT'
Powder Game (Windows 11 x64)

Run:
- Double-click powder_game.exe

Controls:
- 1: Sand
- 2: Water
- 3: Solid
- 4: Erase
- 5: Smoke
- 6: Fire
- 7: Wood
- 8: Oil
- [ / ]: Decrease / increase brush radius (1 to 96)
- Left mouse: paint
- H: Show / hide the on-screen help
- F1: Normal view
- F2-F10: Debug views (boundary, water, water velocity, gas velocity,
gas pressure, smoke, temperature, fuel/reaction, failure counters)
- Esc: quit

The window shows a dark cave. A compact inventory (top left) holds the eight
materials, the selected one's name and the brush radius, with a help panel under
it that hides itself after a few seconds. The brush is a ring and a warm lantern
light over the simulation.

Notes:
- powder_game.exe is self-contained (shaders built in, MinGW 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 app checks the first four at startup.
TXT
cp "$ROOT_DIR/packaging/README.txt" "$DIST_DIR/README.txt"

mkdir -p "$(dirname "$ZIP_PATH")"
rm -f "$ZIP_PATH"
Expand Down
5 changes: 3 additions & 2 deletions docs/ALGORITHMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ cells elsewhere (that once deleted water under sand across the map).
- The distance field is approximate (jump flooding can pick a non-nearest seed in rare configurations); sweeps use only
its sign and normals use its local gradient.
- Wood burning away changes the geometry without a readback, so the distance field is refreshed every quarter second
once wood has been painted since the last reset, even after it has all burnt (a marked shortcut in `RunWoodPass`).
while any wood is left. The wood pass counts its cells on the same tick, reads the count at the next refresh through
a fence, and stops once a count comes back zero.

---

Expand Down Expand Up @@ -1488,7 +1489,7 @@ invocation owns the four material cells it writes:
$`T = \max(T, 0.6\, \text{feed})`$, $`\text{flame} = \max(\text{flame}, 0.3\, \text{feed})`$. The gas burns that
vapour into flame, heat and smoke, and that heat lights the next wood.

The static distance field is rebuilt every 15 ticks once wood has been painted since the last reset, since burnt cells vanish without a readback.
The static distance field is rebuilt every 15 ticks while wood remains, since burnt cells vanish without a readback; the same tick counts the wood cells, and a zero count ends the rebuilds.
The composite draws wood as planks that char to grey ash as fuel goes and glow while burning (11.3).

### 9.2 Why
Expand Down
Binary file modified docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions packaging/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Powder Game (Windows 11 x64)

Run:
- Double-click powder_game.exe

Controls:
- 1: Sand
- 2: Water
- 3: Solid
- 4: Erase
- 5: Smoke
- 6: Fire
- 7: Wood
- 8: Oil
- [ / ]: Decrease / increase brush radius (1 to 96)
- Left mouse: paint
- H: Show / hide the on-screen help
- F1: Normal view
- F2-F10: Debug views (boundary, water, water velocity, gas velocity,
gas pressure, smoke, temperature, fuel/reaction, failure counters)
- Esc: quit

The window shows a dark cave. A compact inventory (top left) holds the eight
materials, the selected one's name and the brush radius, with a help panel under
it that hides itself after a few seconds. The brush is a ring and a warm lantern
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.
- Source, documentation and measured limitations (VALIDATION.md):
https://github.com/FueledByRedBull/Powder-Game
2 changes: 1 addition & 1 deletion shaders/fullscreen.vert
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ void main() {
vec2 position = positions[gl_VertexID];
uv = position * 0.5 + 0.5;
gl_Position = vec4(position, 0.0, 1.0);
}
}
9 changes: 9 additions & 0 deletions shaders/wood_burn.comp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ layout(r16f, binding = 3) uniform image2D fuelTex;
layout(rg16f, binding = 4) uniform image2D reactionTex;
layout(r16f, binding = 5) uniform readonly image2D 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 {
uint woodCells;
};

uniform ivec2 gasSize;
uniform int mode;
uniform int tally;
uniform uint seed;
uniform float ignitionTemperature;
uniform float igniteChance; // per tick while hot
Expand Down Expand Up @@ -52,6 +58,7 @@ void main() {
if (imageLoad(gasMaskTex, n).r == 0u) heat = max(heat, imageLoad(temperatureTex, n).x);
}
}
uint remaining = 0u;
for (int i = 0; i < 4; ++i) {
ivec2 cell = gas * 2 + ivec2(i & 1, i >> 1);
uint material = imageLoad(materialTex, cell).r;
Expand All @@ -71,7 +78,9 @@ void main() {
next = 256u + fuel;
}
if (next != material) imageStore(materialTex, cell, uvec4(next));
if (isWood(next)) ++remaining;
}
if (tally != 0 && remaining > 0u) atomicAdd(woodCells, remaining);
return;
}
if (imageLoad(gasMaskTex, gas).r != 0u) {
Expand Down
83 changes: 78 additions & 5 deletions src/PowderApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ std::string WithGlfwError(std::string message) {
return message;
}

#ifdef _WIN32
// The executable has a console, but a double-clicked build closes it with the process, so a failed start also says why
// in a dialog.
void ShowErrorDialog(const std::string& message) {
const int size = MultiByteToWideChar(CP_UTF8, 0, message.c_str(), -1, nullptr, 0);
std::wstring text(static_cast<std::size_t>(std::max(size, 1)), L'\0');
MultiByteToWideChar(CP_UTF8, 0, message.c_str(), -1, text.data(), size);
MessageBoxW(nullptr, text.c_str(), L"Powder Game", MB_OK | MB_ICONERROR);
}
#endif

} // namespace

int PowderApp::Run() {
Expand All @@ -107,6 +118,17 @@ int PowderApp::Run() {

while (glfwWindowShouldClose(window_) == GLFW_FALSE) {
glfwPollEvents();
// Minimized: nothing is drawn or simulated, and the thread sleeps until the next event. The clocks restart
// on return, so the time spent hidden is not simulated as a backlog.
int framebuffer_width = 0;
int framebuffer_height = 0;
glfwGetFramebufferSize(window_, &framebuffer_width, &framebuffer_height);
if (glfwGetWindowAttrib(window_, GLFW_ICONIFIED) == GLFW_TRUE || framebuffer_width == 0 || framebuffer_height == 0) {
glfwWaitEvents();
previous_time = title_time = glfwGetTime();
title_frames = 0;
continue;
}
UpdateInput();
const double current_time = glfwGetTime();
RunInteractiveFrame(clock, current_time - previous_time);
Expand All @@ -131,6 +153,9 @@ int PowderApp::Run() {
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
Shutdown();
#ifdef _WIN32
if (!replay_mode_) ShowErrorDialog(error.what()); // replays stay headless and scriptable
#endif
return 1;
}
}
Expand All @@ -152,6 +177,7 @@ int PowderApp::RunInteractiveFrame(FixedStepClock& clock, double elapsed) {
}

void PowderApp::Initialize() {
replay_mode_ = std::getenv("POWDER_REPLAY") != nullptr; // before anything that can throw, so Run() knows
config_ = powder_config::LoadConfigFromEnvironment();

glfwSetErrorCallback(GlfwErrorCallback);
Expand All @@ -162,7 +188,6 @@ void PowderApp::Initialize() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
replay_mode_ = std::getenv("POWDER_REPLAY") != nullptr;
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

// Replays keep the reference 1280x900. Interactive windows take 85% of the work area's height and just enough
Expand Down Expand Up @@ -303,6 +328,8 @@ void PowderApp::Shutdown() {
DeleteProgram(init_state_program_);
DeleteBuffer(gas_state_.pressure_check);
DeleteBuffer(debug_state_.counters);
DiscardWoodTally();
DeleteBuffer(wood_tally_.buffer);
DeleteBuffer(phase_state_.cell_counts);
DeleteBuffer(phase_state_.gas_sources);
DeleteBuffer(phase_state_.sinks);
Expand Down Expand Up @@ -699,6 +726,10 @@ void PowderApp::CreateBuffers() {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, debug_state_.counters);
glBufferData(GL_SHADER_STORAGE_BUFFER, debug_counter_bytes, nullptr, GL_DYNAMIC_DRAW);

glGenBuffers(1, &wood_tally_.buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, wood_tally_.buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, 2 * sizeof(GLuint), nullptr, GL_DYNAMIC_DRAW);

glGenBuffers(1, &phase_state_.cell_counts);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, phase_state_.cell_counts);
glBufferData(GL_SHADER_STORAGE_BUFFER, 2 * water_count_bytes, nullptr, GL_DYNAMIC_DRAW);
Expand Down Expand Up @@ -843,6 +874,7 @@ void PowderApp::InitializeState() {
frame_index_ = 0U;
boundary_state_.static_dirty = true;
wood_present_ = false;
DiscardWoodTally();
oil_present_ = heat_present_ = false;
phase_state_.steam_ping = true;
// The gas solve's check schedule follows the last solve, so a stale count makes repeated replays diverge.
Expand Down Expand Up @@ -1481,7 +1513,10 @@ void PowderApp::RunSpawnFullRes(const BrushConfig& config, const DispatchRegion&
return;
}
if (config.replace_material) boundary_state_.static_dirty = true;
if (config.replace_material && config.material_value == kWoodMaterial) wood_present_ = heat_present_ = true;
if (config.replace_material && config.material_value == kWoodMaterial) {
wood_present_ = heat_present_ = true;
DiscardWoodTally(); // a count taken before this stroke would not include it
}

glUseProgram(spawn_full_program_);
glUniform2i(CachedUniform(spawn_full_program_, "gridSize"), kGridWidth, kGridHeight);
Expand Down Expand Up @@ -3160,9 +3195,41 @@ std::array<GLuint, 2> PowderApp::ReadPhaseSinks() const {
return sinks;
}

void PowderApp::DiscardWoodTally() {
if (wood_tally_.fence != nullptr) glDeleteSync(wood_tally_.fence);
wood_tally_.fence = nullptr;
}

// Wood catches from hot gas, burns down feeding the gas fire, and goes out in water (shaders/wood_burn.comp).
// Burning wood changes the static geometry with no readback, so every 15th tick (a quarter second) the distance field is
// rebuilt and the wood cells are counted. That count is read at the next sample, when its copy finished long ago and
// the read does not stall the GPU; when no wood is left the pass stops running, and the rebuild at the sample that
// counted zero was the last one that was needed.
void PowderApp::RunWoodPass() {
const bool sample = frame_index_ % 15U == 0U;
if (sample && wood_tally_.fence != nullptr) {
const GLenum waited = glClientWaitSync(wood_tally_.fence, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
if (waited == GL_WAIT_FAILED) throw std::runtime_error("Waiting for the wood count failed.");
GLuint remaining = 0U;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, wood_tally_.buffer);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint), sizeof(GLuint), &remaining);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
DiscardWoodTally();
if (remaining == 0U) {
wood_present_ = false;
return;
}
}
if (sample) {
const GLuint zero = 0U;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, wood_tally_.buffer);
glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
}
glUseProgram(wood_burn_program_);
glUniform1i(CachedUniform(wood_burn_program_, "tally"), sample ? 1 : 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, wood_tally_.buffer);
glUniform2i(CachedUniform(wood_burn_program_, "gasSize"), kGasWidth, kGasHeight);
glUniform1ui(CachedUniform(wood_burn_program_, "seed"), frame_index_ * 2654435761U);
glUniform1f(CachedUniform(wood_burn_program_, "ignitionTemperature"), 0.35F);
Expand All @@ -3179,9 +3246,15 @@ void PowderApp::RunWoodPass() {
DispatchGrid(kGasWidth, kGasHeight);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT);
}
// shortcut: burning wood changes the static geometry with no readback, so the distance field is rebuilt
// every quarter second while wood exists; track burnt-out cells if this shows in profiles.
if (frame_index_ % 15U == 0U) boundary_state_.static_dirty = true;
if (!sample) return;
boundary_state_.static_dirty = true;
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT);
glBindBuffer(GL_COPY_READ_BUFFER, wood_tally_.buffer);
glBindBuffer(GL_COPY_WRITE_BUFFER, wood_tally_.buffer);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, sizeof(GLuint), sizeof(GLuint));
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
wood_tally_.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}

void PowderApp::RunRenderFieldExtractionPass() {
Expand Down
9 changes: 8 additions & 1 deletion src/PowderApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,14 @@ class PowderApp {
// Lighting is presentation only. Off, composite.frag draws the plain albedo and no lantern, which is what the
// render-modes oracle and the HUD pixel tests compare against.
bool lighting_enabled_ = true;
// Wood was painted since the last reset: burning wood changes the static geometry (RunCouplingPass).
// Wood was painted since the last reset and some may remain: burning wood changes the static geometry
// (RunCouplingPass). RunWoodPass counts the wood cells (intact, burning or charred) into word 0 of the tally buffer
// every 15th tick and copies that to word 1 behind a fence; the next sample reads it, and a zero clears the flag.
bool wood_present_ = false;
struct WoodTally {
GLuint buffer = 0;
GLsync fence = nullptr;
} wood_tally_;
// Oil, or a heat source (fire or wood), was painted since the last reset: only then do the oil drift and the
// phase passes (RunPhasePass) run.
bool oil_present_ = false;
Expand Down Expand Up @@ -615,6 +621,7 @@ class PowderApp {
void RunFireHeatPass(float dt);
void RunCouplingPass(float dt);
void RunWoodPass();
void DiscardWoodTally();
void RunPhasePass(float dt);
GLuint CurrentSteam() const;
std::array<GLuint, 2> ReadPhaseSinks() const; // evaporated water and burned oil records since the last reset
Expand Down
Loading
Loading