Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

perf(ww3d2): compile the modern water pixel programs unoptimised

Olcay Seygan7bd7e029c1ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4685e26e4..3bbd29260 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,7 @@ found and fixed — EA's own, not port damage.**
## Modern water
- Options has a Water Style box. Modern rolls the sea in waves whose crests catch the sun all the way into the shallows, and at the shore they turn along the bank and pile up foam. Boats push foam off their hulls. Classic stays the default, and either way a map keeps the water colour and depth its maker gave it.
-- Windows has it now on the default renderer, and the box switches it in the middle of a match. The Direct3D 9 renderer and macOS keep the Classic water and hide the box. Linux still draws one thing Windows does not, a second band of foam laid over the surf where waves break on a beach. The first Modern frame on a fresh install stops for about a minute while its two water shaders compile; every match after that loads them from the cache.
+- Windows has it now on the default renderer, and the box switches it in the middle of a match. The Direct3D 9 renderer and macOS keep the Classic water and hide the box. Linux still draws one thing Windows does not, a second band of foam laid over the surf where waves break on a beach. The first Modern frame on a fresh install stops for about ten seconds while its two water shaders compile; every match after that loads them from the cache.
## Volumetric hazard clouds
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index e813197a7..d1574feae 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -97,13 +97,15 @@ static unsigned read_program_file(const char * path,
return count;
}
-// FNV-1a over the profile and the source, which between them decide the bytecode. The generated
-// text is the key rather than the pipeline's state key, so a build that changes what a state
-// generates compiles again instead of drawing with the previous build's program.
-static unsigned long long program_hash(const char * profile, const std::string & source)
+// FNV-1a over the profile, the source and the compiler flags, which between them decide the
+// bytecode. The generated text is the key rather than the pipeline's state key, so a build that
+// changes what a state generates compiles again instead of drawing with the previous build's
+// program. Default flags add nothing, so every key from before the flags existed still matches.
+static unsigned long long program_hash(const char * profile, const std::string & source, unsigned flags)
{
const unsigned long long FNV_OFFSET = 14695981039346656037ULL;
const unsigned long long FNV_PRIME = 1099511628211ULL;
+ const unsigned BITS_PER_BYTE = 8;
unsigned long long hash = FNV_OFFSET;
for (const char * cursor = profile; *cursor != '\0'; ++cursor) {
@@ -112,6 +114,9 @@ static unsigned long long program_hash(const char * profile, const std::string &
for (size_t index = 0; index < source.size(); ++index) {
hash = (hash ^ static_cast<unsigned char>(source[index])) * FNV_PRIME;
}
+ for (unsigned remaining = flags; remaining != 0; remaining >>= BITS_PER_BYTE) {
+ hash = (hash ^ static_cast<unsigned char>(remaining)) * FNV_PRIME;
+ }
return hash;
}
@@ -1933,9 +1938,9 @@ void DX11BackendClass::Save_Shader_Cache() const
}
bool DX11BackendClass::Compile_Program(const std::string & source, const char * name,
- const char * profile, std::vector<unsigned char> & bytecode)
+ const char * profile, std::vector<unsigned char> & bytecode, unsigned flags)
{
- const unsigned long long hash = program_hash(profile, source);
+ const unsigned long long hash = program_hash(profile, source, flags);
std::map<unsigned long long, std::vector<unsigned char> >::const_iterator known
= CompiledPrograms.find(hash);
if (known != CompiledPrograms.end()) {
@@ -1950,7 +1955,7 @@ bool DX11BackendClass::Compile_Program(const std::string & source, const char *
ID3DBlob * code = NULL;
ID3DBlob * errors = NULL;
- if (FAILED(compiler(source.c_str(), source.size(), name, NULL, NULL, ENTRY_POINT, profile, 0, 0,
+ if (FAILED(compiler(source.c_str(), source.size(), name, NULL, NULL, ENTRY_POINT, profile, flags, 0,
&code, &errors))) {
Record_Compiler_Error(name, errors);
return false;
@@ -2440,10 +2445,12 @@ bool DX11BackendClass::Resolve(Pipeline & pipeline)
std::vector<unsigned char> vertex_code;
std::vector<unsigned char> pixel_code;
// The Modern water writes a dynamically indexed array, which shader model 4 refuses; every other
- // program keeps the profile its cached and shipped bytecode was compiled at.
- const char * const pixel_profile = EngineShader_Is_Modern_Water(PixelProgram) ? "ps_5_0" : PIXEL_PROFILE;
+ // program keeps the profile and flags its cached and shipped bytecode was compiled with.
+ const bool modern_water = EngineShader_Is_Modern_Water(PixelProgram);
+ const char * const pixel_profile = modern_water ? "ps_5_0" : PIXEL_PROFILE;
+ const unsigned pixel_flags = modern_water ? DX11_MODERN_WATER_COMPILE_FLAGS : 0;
if (!Compile_Program(vertex_hlsl, "ffvertex", VERTEX_PROFILE, vertex_code)
- || !Compile_Program(pixel_hlsl, "ffshader", pixel_profile, pixel_code)) {
+ || !Compile_Program(pixel_hlsl, "ffshader", pixel_profile, pixel_code, pixel_flags)) {
Refuse(key, REFUSED_NO_PROGRAM);
return false;
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index ed09007ef..2568ca13b 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -69,6 +69,13 @@ const unsigned DX11_BACKEND_TEXTURE_STAGES = 4;
// itself and has to reach one past it.
const unsigned DX11_BACKEND_STAGE_STATES = 33;
+// D3DCOMPILE_SKIP_OPTIMIZATION, which the Modern water's pixel programs compile with. The
+// optimiser spends 34 s on each of them (about 11 thousand instructions, mostly inlined copies of
+// the noise and hash helpers) and 4.5 s without it. The driver optimises the bytecode again
+// either way: the frame time stayed inside run-to-run noise and the picture did not change
+// (2026-10-23, docs/porting/windows-impact.md).
+const unsigned DX11_MODERN_WATER_COMPILE_FLAGS = 1u << 2;
+
class DX11BackendClass
{
public:
@@ -377,9 +384,10 @@ public:
const char * First_Compiler_Error() const { return CompilerError.c_str(); }
// A program for a pass of someone else's (the hazard clouds), compiled once and kept in the
- // shader cache with the generated ones, so a second run compiles nothing.
+ // shader cache with the generated ones, so a second run compiles nothing. The flags are
+ // D3DCompile's first flag word; zero is its default optimisation.
bool Compile_Program(const std::string & source, const char * name, const char * profile,
- std::vector<unsigned char> & bytecode);
+ std::vector<unsigned char> & bytecode, unsigned flags = 0);
private:
DX11BackendClass(const DX11BackendClass &);
diff --git a/GeneralsMD/Code/Tests/test_dx11backend.cpp b/GeneralsMD/Code/Tests/test_dx11backend.cpp
index 5beb77173..6ab771fee 100644
--- a/GeneralsMD/Code/Tests/test_dx11backend.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11backend.cpp
@@ -1226,28 +1226,29 @@ TEST(dx11backend_the_modern_water_programs_compile_at_shader_model_5)
PixelPipelineDescription pipeline;
memset(&pipeline, 0, sizeof(pipeline));
pipeline.FogEnabled = true;
- struct Program { std::string source; const char * profile; const char * name; };
+ struct Program { std::string source; const char * profile; const char * name; unsigned flags; };
std::vector<Program> programs;
for (const EngineShaderProgram water : { ENGINE_SHADER_WATER_TRAPEZOID, ENGINE_SHADER_WATER_RIVER }) {
- Program program = { std::string(), "ps_5_0", EngineShader_Name(water) };
+ Program program = { std::string(), "ps_5_0", EngineShader_Name(water), DX11_MODERN_WATER_COMPILE_FLAGS };
CHECK(EngineShader_Pixel_Program(water, pipeline, program.source));
programs.push_back(program);
}
- Program ocean = { std::string(), "vs_5_0", "water vertex" };
+ Program ocean = { std::string(), "vs_5_0", "water vertex", 0 };
CHECK(DX11Water_Ocean_Vertex_Program(water_vertex_description(), ocean.source));
programs.push_back(ocean);
for (unsigned pass = 0; pass < DX11_WATER_OCEAN_PASSES; ++pass) {
- const Program fft = { DX11Water_Ocean_Pass_Program(pass), "ps_5_0", "ocean pass" };
+ const Program fft = { DX11Water_Ocean_Pass_Program(pass), "ps_5_0", "ocean pass", 0 };
programs.push_back(fft);
}
- const Program fullscreen = { DX11Water_Ocean_Pass_Vertex_Program(), "vs_5_0", "ocean vertex" };
+ const Program fullscreen = { DX11Water_Ocean_Pass_Vertex_Program(), "vs_5_0", "ocean vertex", 0 };
programs.push_back(fullscreen);
for (const Program & program : programs) {
std::vector<unsigned char> code;
const bool vertex = program.profile[0] == 'v';
const unsigned started = GetTickCount();
- const bool compiled = backend.Compile_Program(program.source, program.name, program.profile, code);
+ const bool compiled = backend.Compile_Program(program.source, program.name, program.profile, code,
+ program.flags);
printf(" %s: %s in %u ms\n", program.name, compiled ? "compiled" : "refused",
GetTickCount() - started);
if (!compiled) {
diff --git a/docs/porting/windows-impact.md b/docs/porting/windows-impact.md
index 40a631bbe..66d7e8f04 100644
--- a/docs/porting/windows-impact.md
+++ b/docs/porting/windows-impact.md
@@ -539,9 +539,17 @@ behind the wrapper under Direct3D 11.
The two Modern pixel programs need shader model 5 (FXC refuses the dynamically indexed write that
`waterRefractionHit`'s neighbour mask used to make, and that line is now a multiply both compilers
-take). Each takes about 34 s to compile with d3dcompiler_47 at the backend's default flags, so the
-first Modern frame without a cache stalls for over a minute and `test_dx11backend` grew to 89 s.
-They are not in `dx11shaders.shipped`; recording them is open.
+take). At d3dcompiler_47's default optimisation each took 34 s (trapezoid 33.6, river 34.2), and
+`/O0` is no faster: the cost is the optimiser working through about 11 thousand instructions, most
+of them inlined copies of `oceanHash` (217) and `oceanNoise` (53). No loop attribute, table or
+precision flag moves it. They now compile with `D3DCOMPILE_SKIP_OPTIMIZATION`
+(`DX11_MODERN_WATER_COMPILE_FLAGS`), 6.0 and 5.5 s, and the flags join the cache key only when set,
+so no other program's key changed. `test_dx11backend` went from 89 s to 20 s. The driver optimises
+the bytecode again: over six interleaved warm runs of the shore view below the frame was 0.8 ms
+slower on average, inside the ±3 ms the same exe moved between runs, and the frame-600 picture
+against the optimised build's has 23 pixels past a difference of 24, against 162 between two runs
+of the optimised one. The water vertex program (0.6 s) and the FFT passes (32 ms at most) keep the
+default. The Modern programs are not in `dx11shaders.shipped`; recording them is open.
Measured on 2026-10-23, Release, 1280x720 windowed, `-noFPSLimit -msaa 0`, the same Bombardment
Beach skirmish: with the shore in view the frame is 4.44 ms mean (p99 10.25) on Classic and 17.63