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 missing direct3d 11 programs off the render frame

Olcay Seygan6b721960d5f9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd04e4094..34f8b533a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ found and fixed — EA's own, not port damage.**
- Windows draws them too, on the default renderer, and so do Linux and macOS. The Direct3D 9 renderer and the Classic graphics setting keep the original puddles whatever the box says, and turning heat effects off takes the shimmer off a nuclear field as well. A cloud that only drifts redraws its smoke twenty times a second rather than every frame, and the second match of a session no longer stalls on the first toxin shell while the clouds' shaders are built again.
- The first toxin cloud of a fresh install no longer freezes the game for three and a half seconds: the clouds' shaders come with the game now, and are made ready when a match starts or the box is switched rather than when the first field opens. A toxin field also no longer comes back at full strength for ten seconds after it has run out; it fades with the hazard.
- A fresh install's first Scud Storm landing, Particle Cannon, nuke and general's air strikes no longer catch a picture for 110 to 130 milliseconds each, whichever cloud style is on. The explosion and lit-smoke shaders those strikes draw with come with the game now, where each one used to be built the first time its blast showed up; a superweapon battle on Twilight Flame from a fresh install now builds nothing at all.
+- A blast the game has no shader for yet, a mod's or a combination nobody recorded, no longer stops the picture while one is built. The shader is built beside the game, and the blast's pieces that need it appear when it is ready, about a sixth of a second later at worst; before, the whole frame waited 110 to 130 milliseconds for each one. With a shader file missing twenty-two of them, a superweapon battle drew on with no pause and the longest any blast waited was 153 milliseconds. The interface, the fog of war and anything drawn into a picture of its own still get their shaders at once, on the match's first frame.
## A rank ladder for every army
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 6d03200c8..3e440e558 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -554,9 +554,14 @@ W3DDisplay::~W3DDisplay()
}
unsigned programsShipped = 0;
unsigned programsHeld = 0;
- Direct3D11_Program_Statistics( programsShipped, programsHeld );
- DEBUG_LOG(("-dx11 programs: %u shipped with the game, %u held at the end\n",
- programsShipped, programsHeld));
+ unsigned programsOffFrame = 0;
+ double longestOffFrame = 0.0;
+ unsigned long long drawsWaited = 0;
+ Direct3D11_Program_Statistics( programsShipped, programsHeld, programsOffFrame, longestOffFrame,
+ drawsWaited );
+ DEBUG_LOG(("-dx11 programs: %u shipped with the game, %u held at the end; %u compiled off the "
+ "frame (longest %.0f ms), %I64u draws left out while they compiled\n",
+ programsShipped, programsHeld, programsOffFrame, longestOffFrame, drawsWaited));
unsigned long long noBuffer = 0;
unsigned long long noStage = 0;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index be68f1715..ac93b5c24 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -23,6 +23,11 @@
#include "dx11resource.h"
#include <d3dcommon.h>
+#include <condition_variable>
+#include <deque>
+#include <mutex>
+#include <set>
+#include <thread>
#include <stdio.h>
#include <string.h>
#include <windows.h>
@@ -219,6 +224,11 @@ DX11BackendClass::DX11BackendClass()
, PipelinesBuilt(0)
, FrameBuildMilliseconds(0.0)
, FrameBuildCount(0)
+ , Worker(NULL)
+ , CompileOffFrame(false)
+ , ProgramsCompiledOffFrame(0)
+ , LongestOffFrameMilliseconds(0.0)
+ , DrawsWaitedForProgram(0)
, ShaderCacheChanged(false)
, ShippedPrograms(0)
, LastShaderCacheSave(0)
@@ -528,6 +538,8 @@ void DX11BackendClass::Release_Cached()
void DX11BackendClass::Shutdown()
{
+ // Before the save, so what the worker finished goes into the file.
+ Stop_Compile_Worker();
if (ShaderCacheChanged) {
Save_Shader_Cache();
ShaderCacheChanged = false;
@@ -1948,6 +1960,8 @@ void DX11BackendClass::Load_Shipped_Programs(const char * path)
void DX11BackendClass::Save_Shader_Cache_If_Due()
{
+ // Once a frame, so a program whose effect was gone before it landed still reaches the file.
+ Take_Off_Frame_Programs();
if (!ShaderCacheChanged) {
return;
}
@@ -2024,6 +2038,180 @@ bool DX11BackendClass::Compile_Program(const std::string & source, const char *
return true;
}
+// Jobs waiting at once. A frame meets a handful of new programs at most; past this a program
+// compiles on the spot, as before.
+static const size_t COMPILE_QUEUE_LIMIT = 64;
+
+// Two threads, because one left the last of a match's first eight new programs waiting 0.9 s.
+static const unsigned COMPILE_THREADS = 2;
+
+// Threads that run D3DCompile, which d3dcompiler_47 allows from any thread. They never see the
+// device, the context or the backend's maps: jobs go in and bytecode comes out under one lock, and
+// the render thread moves the bytecode into CompiledPrograms.
+struct DX11BackendClass::CompileWorker
+{
+ struct Job
+ {
+ unsigned long long Hash;
+ std::string Source;
+ std::string Name;
+ std::string Profile;
+ unsigned Flags;
+ double Queued;
+ };
+ struct Done
+ {
+ unsigned long long Hash;
+ bool Compiled;
+ std::vector<unsigned char> Bytecode;
+ double Milliseconds;
+ };
+
+ explicit CompileWorker(D3DCompileFunction compiler)
+ : Compiler(compiler), Stopping(false)
+ {
+ for (unsigned index = 0; index < COMPILE_THREADS; ++index) {
+ Threads[index] = std::thread(&CompileWorker::Run, this);
+ }
+ }
+
+ void Run()
+ {
+ for (;;) {
+ Job job;
+ {
+ std::unique_lock<std::mutex> hold(Lock);
+ Wake.wait(hold, [this] { return Stopping || !Jobs.empty(); });
+ if (Stopping) {
+ return;
+ }
+ job = std::move(Jobs.front());
+ Jobs.pop_front();
+ }
+ Done done;
+ done.Hash = job.Hash;
+ ID3DBlob * code = NULL;
+ ID3DBlob * errors = NULL;
+ done.Compiled = SUCCEEDED(Compiler(job.Source.c_str(), job.Source.size(), job.Name.c_str(), NULL,
+ NULL, ENTRY_POINT, job.Profile.c_str(), job.Flags, 0, &code, &errors));
+ if (done.Compiled) {
+ const unsigned char * bytes = static_cast<const unsigned char *>(code->GetBufferPointer());
+ done.Bytecode.assign(bytes, bytes + code->GetBufferSize());
+ code->Release();
+ }
+ if (errors != NULL) {
+ errors->Release();
+ }
+ done.Milliseconds = DX11Resource_Milliseconds_Now() - job.Queued;
+ std::lock_guard<std::mutex> hold(Lock);
+ Finished.push_back(std::move(done));
+ }
+ }
+
+ D3DCompileFunction Compiler;
+ std::mutex Lock;
+ std::condition_variable Wake;
+ std::deque<Job> Jobs;
+ std::vector<Done> Finished;
+ std::set<unsigned long long> Queued; // queued, running or finished and not yet taken
+ bool Stopping;
+ std::thread Threads[COMPILE_THREADS];
+};
+
+bool DX11BackendClass::Compiles_Off_Frame() const
+{
+ if (!CompileOffFrame || VertexProgram != ENGINE_SHADER_NONE || PixelProgram != ENGINE_SHADER_NONE
+ || CurrentTarget != NULL || ShadowMapBound || (VertexFormat & D3DFVF_XYZRHW) != 0) {
+ return false;
+ }
+ // A perspective projection has nought in its corner; Render2D's identity and an orthographic
+ // overlay have one, and those are the interface. Camera-space particles keep the perspective.
+ if (Projection[15] != 0.0f) {
+ return false;
+ }
+ if (RenderStates.Get_Render_State(D3DRS_ALPHABLENDENABLE) != FALSE) {
+ const DWORD source = RenderStates.Get_Render_State(D3DRS_SRCBLEND);
+ const DWORD destination = RenderStates.Get_Render_State(D3DRS_DESTBLEND);
+ if (source == D3DBLEND_DESTCOLOR || source == D3DBLEND_ZERO || destination == D3DBLEND_SRCCOLOR) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool DX11BackendClass::Queue_Off_Frame(unsigned long long hash, const std::string & source,
+ const char * name, const char * profile, unsigned flags)
+{
+ if (Worker == NULL) {
+ D3DCompileFunction compiler = compiler_function();
+ if (compiler == NULL) {
+ return false;
+ }
+ Worker = new CompileWorker(compiler);
+ }
+ std::lock_guard<std::mutex> hold(Worker->Lock);
+ if (Worker->Queued.count(hash) != 0) {
+ return true;
+ }
+ if (Worker->Jobs.size() >= COMPILE_QUEUE_LIMIT) {
+ return false;
+ }
+ CompileWorker::Job job = { hash, source, name, profile, flags, DX11Resource_Milliseconds_Now() };
+ Worker->Jobs.push_back(std::move(job));
+ Worker->Queued.insert(hash);
+ Worker->Wake.notify_one();
+ return true;
+}
+
+void DX11BackendClass::Take_Off_Frame_Programs()
+{
+ if (Worker == NULL) {
+ return;
+ }
+ std::vector<CompileWorker::Done> finished;
+ {
+ std::lock_guard<std::mutex> hold(Worker->Lock);
+ finished.swap(Worker->Finished);
+ for (size_t index = 0; index < finished.size(); ++index) {
+ Worker->Queued.erase(finished[index].Hash);
+ }
+ }
+ for (size_t index = 0; index < finished.size(); ++index) {
+ CompileWorker::Done & done = finished[index];
+ if (!done.Compiled) {
+ FailedOffFrame[done.Hash] = true;
+ continue;
+ }
+ CompiledPrograms[done.Hash].swap(done.Bytecode);
+ ShaderCacheChanged = true;
+ ++ProgramsCompiledOffFrame;
+ if (done.Milliseconds > LongestOffFrameMilliseconds) {
+ LongestOffFrameMilliseconds = done.Milliseconds;
+ }
+ }
+}
+
+void DX11BackendClass::Stop_Compile_Worker()
+{
+ if (Worker == NULL) {
+ return;
+ }
+ {
+ std::lock_guard<std::mutex> hold(Worker->Lock);
+ Worker->Stopping = true;
+ }
+ Worker->Wake.notify_all();
+ // At most the compile each thread is in the middle of; the jobs behind them are dropped and
+ // queued again by the next draw that wants them.
+ for (unsigned index = 0; index < COMPILE_THREADS; ++index) {
+ Worker->Threads[index].join();
+ }
+ Take_Off_Frame_Programs();
+ delete Worker;
+ Worker = NULL;
+ PendingPipelines.clear();
+}
+
bool DX11BackendClass::Compile_Pixel_Program(const std::string & source, const char * name,
std::vector<unsigned char> & bytecode)
{
@@ -2469,6 +2657,20 @@ bool DX11BackendClass::Resolve(Pipeline & pipeline)
Refuse(key, static_cast<RefusalReason>(refused->second));
return false;
}
+ // The same key drawn where it may not wait (into a texture, say) compiles on the spot below.
+ const bool off_frame = Compiles_Off_Frame();
+ if (off_frame && !PendingPipelines.empty()) {
+ std::map<std::string, std::pair<unsigned long long, unsigned long long> >::iterator pending
+ = PendingPipelines.find(key);
+ if (pending != PendingPipelines.end()) {
+ Take_Off_Frame_Programs();
+ if (!Program_Settled(pending->second.first) || !Program_Settled(pending->second.second)) {
+ ++DrawsWaitedForProgram;
+ return false;
+ }
+ PendingPipelines.erase(pending);
+ }
+ }
const double build_started = DX11Resource_Milliseconds_Now();
std::string vertex_hlsl;
@@ -2507,6 +2709,21 @@ bool DX11BackendClass::Resolve(Pipeline & pipeline)
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;
+ // A scene draw of a generated program neither cache holds waits for the worker (the header has
+ // which draws do not). A full queue compiles here, as before.
+ if (off_frame) {
+ const unsigned long long vertex_hash = program_hash(VERTEX_PROFILE, vertex_hlsl, 0);
+ const unsigned long long pixel_hash = program_hash(pixel_profile, pixel_hlsl, pixel_flags);
+ const bool vertex_missing = !Program_Settled(vertex_hash);
+ const bool pixel_missing = !Program_Settled(pixel_hash);
+ if ((vertex_missing || pixel_missing)
+ && (!vertex_missing || Queue_Off_Frame(vertex_hash, vertex_hlsl, "ffvertex", VERTEX_PROFILE, 0))
+ && (!pixel_missing || Queue_Off_Frame(pixel_hash, pixel_hlsl, "ffshader", pixel_profile, pixel_flags))) {
+ PendingPipelines[key] = std::make_pair(vertex_hash, pixel_hash);
+ ++DrawsWaitedForProgram;
+ return false;
+ }
+ }
if (!Compile_Program(vertex_hlsl, "ffvertex", VERTEX_PROFILE, vertex_code)
|| !Compile_Program(pixel_hlsl, "ffshader", pixel_profile, pixel_code, pixel_flags)) {
Refuse(key, REFUSED_NO_PROGRAM);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index a7fa57b0b..850df4839 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -178,8 +178,8 @@ public:
// Keep every compiled program in this file across runs: read now, written while the game runs
// (Save_Shader_Cache_If_Due) and at Shutdown when a program was compiled that the file did not
- // hold. A program compiled mid-match costs 25 to 60ms on the frame that first needs it, which is
- // the stutter a new explosion brought.
+ // hold. A program compiled on the frame that first needs it costs that frame 100 to 130ms of
+ // FXC, the stutter a new explosion brought; Set_Compile_Off_Frame moves most of them to a worker.
void Set_Shader_Cache_Path(const char * path);
// The programs that ship with the game, compiled by Microsoft's d3dcompiler_47 when the file was
// recorded (Tools/record-dx11-shaders.ps1): read after the user's cache, so for the same source a
@@ -406,6 +406,24 @@ public:
bool Compile_Program(const std::string & source, const char * name, const char * profile,
std::vector<unsigned char> & bytecode, unsigned flags = 0);
+ // A generated fixed-function program missing from both caches is compiled on a worker thread
+ // rather than on the frame that first meets it, which cost that frame 100 to 130ms of FXC: the
+ // draws that want it are refused until it lands, about a tenth of a second, and the frame goes
+ // on without them. Only a scene draw through a perspective camera takes this road. These still
+ // compile on the spot: a draw into a texture or into the sun's map (drawn once, or a shadow
+ // lost), a pre-transformed one or one under an identity or orthographic projection (the
+ // interface, Render2D's quads, the filters), a multiplicative pass (the terrain's shroud pass:
+ // left out, it would show shrouded ground) and an engine shader (terrain, water, trees). Roads
+ // and bridges are fixed-function and may wait. Off unless asked for, so a test that draws and
+ // reads back sees its draw.
+ void Set_Compile_Off_Frame(bool enabled) { CompileOffFrame = enabled; }
+ // Programs the worker compiled, the longest one took from queue to landing, and the draws
+ // refused while their program was being compiled.
+ void Compile_Statistics(unsigned & compiled_off_frame, double & longest_milliseconds,
+ unsigned long long & draws_waited) const
+ { compiled_off_frame = ProgramsCompiledOffFrame; longest_milliseconds = LongestOffFrameMilliseconds;
+ draws_waited = DrawsWaitedForProgram; }
+
private:
DX11BackendClass(const DX11BackendClass &);
DX11BackendClass & operator=(const DX11BackendClass &);
@@ -1015,6 +1033,30 @@ private:
// Bytecode by the hash of the profile and the source it was compiled from. Two pipelines that
// generate the same vertex program share one compile, and so do two runs through the file.
std::map<unsigned long long, std::vector<unsigned char> > CompiledPrograms;
+
+ // The worker that compiles off the frame (Set_Compile_Off_Frame), made with its first job and
+ // joined in Shutdown. It runs D3DCompile and nothing else: the device and the context stay on
+ // this thread, so a device reset leaves it nothing to touch.
+ struct CompileWorker;
+ CompileWorker * Worker;
+ bool CompileOffFrame;
+ // Pipelines waiting on the worker, by key, with the hashes of their two programs.
+ std::map<std::string, std::pair<unsigned long long, unsigned long long> > PendingPipelines;
+ // Programs the worker could not compile: the pipeline then compiles on the spot, which records
+ // the compiler's error and refuses it the usual way.
+ std::map<unsigned long long, bool> FailedOffFrame;
+ unsigned ProgramsCompiledOffFrame;
+ double LongestOffFrameMilliseconds;
+ unsigned long long DrawsWaitedForProgram;
+ // Whether the draw being resolved may wait for the worker (Set_Compile_Off_Frame has which may not).
+ bool Compiles_Off_Frame() const;
+ bool Program_Settled(unsigned long long hash) const
+ { return CompiledPrograms.count(hash) != 0 || FailedOffFrame.count(hash) != 0; }
+ bool Queue_Off_Frame(unsigned long long hash, const std::string & source, const char * name,
+ const char * profile, unsigned flags);
+ void Take_Off_Frame_Programs();
+ void Stop_Compile_Worker();
+
std::string ShaderCachePath;
bool ShaderCacheChanged;
unsigned ShippedPrograms;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index 5f70187ce..953080e0b 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -376,6 +376,7 @@ bool Direct3D11_Create(HWND window, unsigned width, unsigned height)
cache_path += SHADER_CACHE_FILE;
Backend.Set_Shader_Cache_Path(cache_path.c_str());
Backend.Load_Shipped_Programs(SHIPPED_SHADER_FILE);
+ Backend.Set_Compile_Off_Frame(true);
// The chain is the one thing here that is allowed to fail without taking the backend with it:
// a machine whose compiler refuses the passes still gets the frame, just not the effect.
@@ -1259,10 +1260,12 @@ void Direct3D11_Statistics(unsigned & pipelines_built, unsigned long long & draw
}
}
-void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held)
+void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held, unsigned & off_frame,
+ double & longest_milliseconds, unsigned long long & draws_waited)
{
shipped = Active ? Backend.Shipped_Program_Count() : 0;
held = Active ? Backend.Compiled_Program_Count() : 0;
+ Backend.Compile_Statistics(off_frame, longest_milliseconds, draws_waited);
}
void Direct3D11_Take_Frame_Cost(double & pipeline_milliseconds, unsigned & pipelines,
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index c45330fba..221676cad 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -377,7 +377,10 @@ void Direct3D11_Statistics(unsigned & pipelines_built, unsigned long long & draw
// How many compiled programs came with the game (dx11shaders.shipped) and how many the backend holds now,
// shipped, cached and compiled together: a run whose held count exceeds the shipped one compiled something.
-void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held);
+// Of those compiled, how many the worker built off the frame, the longest one from queue to landing, and
+// the draws left out while they were built.
+void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held, unsigned & off_frame,
+ double & longest_milliseconds, unsigned long long & draws_waited);
// What the frame since the last call spent building pipelines and copying textures, and how many
// of each, how many times the soft particles copied the scene's depth, and how many constant blocks
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
index 2cb841824..a6e7ffe1a 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
@@ -174,10 +174,14 @@ void Direct3D11_Statistics(unsigned & pipelines_built, unsigned long long & draw
draws_refused = 0;
}
-void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held)
+void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held, unsigned & off_frame,
+ double & longest_milliseconds, unsigned long long & draws_waited)
{
shipped = 0;
held = 0;
+ off_frame = 0;
+ longest_milliseconds = 0.0;
+ draws_waited = 0;
}
void Direct3D11_Take_Frame_Cost(double & pipeline_milliseconds, unsigned & pipelines,
double & texture_milliseconds, unsigned & textures, unsigned & depth_copies,
diff --git a/GeneralsMD/Code/Tests/test_dx11backend.cpp b/GeneralsMD/Code/Tests/test_dx11backend.cpp
index 6ab771fee..8a1350c2a 100644
--- a/GeneralsMD/Code/Tests/test_dx11backend.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11backend.cpp
@@ -36,6 +36,8 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
+#include <chrono>
+#include <thread>
#include <vector>
static const unsigned TARGET_SIZE = 64;
@@ -372,6 +374,117 @@ TEST(dx11backend_the_same_state_resolves_to_one_pipeline)
backend.Shutdown();
}
+// The pass-through state under a perspective projection, the only kind of scene draw that may wait
+// for the worker: an identity projection is how Render2D draws the interface.
+static void configure_perspective_pass_through(DX11BackendClass & backend)
+{
+ configure_unlit_pass_through(backend);
+ float perspective[16];
+ set_identity(perspective);
+ perspective[11] = 1.0f;
+ perspective[15] = 0.0f;
+ backend.Set_Transform(D3DTS_PROJECTION, perspective);
+}
+
+// With the compile off the frame, a state neither cache holds leaves its draws out until the
+// worker has its two programs: the second draw of the same state queues nothing more, and the
+// draw after they land is made from them with no compile of its own.
+TEST(dx11backend_a_missing_program_compiles_off_the_frame)
+{
+ DX11DeviceClass device;
+ CHECK(device.Create_Offscreen());
+
+ DX11BackendClass backend;
+ CHECK(backend.Initialise(&device));
+ backend.Set_Compile_Off_Frame(true);
+
+ ID3D11Buffer * vertices = NULL;
+ CHECK(DX11Resource_Create_Vertex_Buffer(device.Get_Device(), sizeof(QUAD_VERTICES),
+ D3DPOOL_MANAGED, 0, QUAD_VERTICES, &vertices));
+
+ ID3D11Buffer * indices = NULL;
+ CHECK(DX11Resource_Create_Index_Buffer(device.Get_Device(), sizeof(QUAD_INDICES),
+ D3DPOOL_MANAGED, 0, QUAD_INDICES, &indices));
+
+ backend.Set_Viewport(0, 0, TARGET_SIZE, TARGET_SIZE);
+ configure_perspective_pass_through(backend);
+ backend.Set_Stream_Source(vertices, sizeof(BackendVertex), 0);
+ backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);
+
+ CHECK(!backend.Draw_Indexed_Triangles(6, 0, 0));
+ CHECK(!backend.Draw_Indexed_Triangles(6, 0, 0));
+
+ // FXC takes about a tenth of a second for these two; twenty seconds is a hung worker.
+ const unsigned WAIT_STEPS = 2000;
+ bool drawn = false;
+ for (unsigned step = 0; step < WAIT_STEPS && !drawn; ++step) {
+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
+ drawn = backend.Draw_Indexed_Triangles(6, 0, 0);
+ }
+ CHECK(drawn);
+
+ unsigned off_frame = 0;
+ double longest = 0.0;
+ unsigned long long waited = 0;
+ backend.Compile_Statistics(off_frame, longest, waited);
+ CHECK_EQ(off_frame, 2u);
+ CHECK(waited >= 2ull);
+ CHECK(longest > 0.0);
+
+ unsigned pipelines = 0;
+ unsigned long long made = 0;
+ unsigned long long refused = 0;
+ backend.Statistics(pipelines, made, refused);
+ CHECK_EQ(pipelines, 1u);
+ CHECK_EQ(made, 1ull);
+
+ indices->Release();
+ vertices->Release();
+ backend.Shutdown();
+}
+
+// A key still waiting on the worker is drawn at once by a draw that may not wait: the same state
+// under Render2D's identity projection compiles on the spot rather than leaving the interface out.
+TEST(dx11backend_a_draw_that_may_not_wait_compiles_a_pending_key_on_the_spot)
+{
+ DX11DeviceClass device;
+ CHECK(device.Create_Offscreen());
+
+ DX11BackendClass backend;
+ CHECK(backend.Initialise(&device));
+ backend.Set_Compile_Off_Frame(true);
+
+ ID3D11Buffer * vertices = NULL;
+ CHECK(DX11Resource_Create_Vertex_Buffer(device.Get_Device(), sizeof(QUAD_VERTICES),
+ D3DPOOL_MANAGED, 0, QUAD_VERTICES, &vertices));
+
+ ID3D11Buffer * indices = NULL;
+ CHECK(DX11Resource_Create_Index_Buffer(device.Get_Device(), sizeof(QUAD_INDICES),
+ D3DPOOL_MANAGED, 0, QUAD_INDICES, &indices));
+
+ backend.Set_Viewport(0, 0, TARGET_SIZE, TARGET_SIZE);
+ configure_perspective_pass_through(backend);
+ backend.Set_Stream_Source(vertices, sizeof(BackendVertex), 0);
+ backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);
+
+ CHECK(!backend.Draw_Indexed_Triangles(6, 0, 0));
+ float identity[16];
+ set_identity(identity);
+ backend.Set_Transform(D3DTS_PROJECTION, identity);
+ CHECK(backend.Draw_Indexed_Triangles(6, 0, 0));
+
+ unsigned pipelines = 0;
+ unsigned long long made = 0;
+ unsigned long long refused = 0;
+ backend.Statistics(pipelines, made, refused);
+ CHECK_EQ(pipelines, 1u);
+ CHECK_EQ(made, 1ull);
+
+ indices->Release();
+ vertices->Release();
+ backend.Shutdown();
+}
+
// The vertex block goes into a ring of 4096 one-kilobyte entries, so 5000 draws with a world matrix
// each wrap it once, and the draw after them reads an entry written NO_OVERWRITE past the discard.
// Every one of the 5000 puts the quad off the target; the last puts it back over the whole of it.