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): send only changed rows of a repeated 16-bit d3d11 update

Olcay Seygan1e2b236b2b5c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dba07e070..05f946917 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ found and fixed — EA's own, not port damage.**
- The new command bar stopped eating half the frame. The clock and the frame counter in the top right corner sat inside the bar's own page, so every tick of them had the whole bar laid out again from scratch, 15ms a time and thirty times a second. The corner box is a page of its own now, the scoreboard counts again twice a second instead of every frame, and a click on the battlefield no longer lays the bar out twice. In the same four-player battle, almost half the frames used to miss 60 a second; now fewer than one in four hundred do, and the game draws more than twice as many frames. When the bar does have to be laid out again, on a hover, a click on it or the scoreboard's twice-a-second count, it takes 2ms where it took 14: the page engine was building an object for every word of the bar's style sheet and every space of its indenting, and reading the same style sheet from scratch each time. All ten pages were laid out both ways and every box and every word came out in the same place, size and colour.
- A screen full of fire and smoke is cheaper to draw. Thirty Inferno Cannons shelling twenty Crusaders used to send the graphics card 459 separate batches of see-through effects a frame, mostly the same texture again and again, and the game looked up the same seven card settings from scratch for every one of them. Matching effects go out together now, and settings that have not changed are not looked up again: 177 batches, and the same battle went from 11.6ms a frame to 8.6ms. It is at 6.7ms now, since the game stopped preparing everything it draws a second time for a copy of the picture nobody sees. Nothing on screen changes, the effects are drawn in exactly the order they were.
- The same battle with the particle limit turned up costs 7.6ms a frame where it cost 9.1ms, and the old renderer went from 9.3ms to 6.3ms. Both were keeping a count of every kind of draw the game makes, and both did it by writing out and reading back a description of each one, on every draw, for a report no player ever opens. They glance at one draw in sixteen now. The new renderer also stopped re-sending the graphics card settings it had just sent.
+- The new renderer stopped redoing work between two pieces of ground that look the same. The map's terrain goes out as hundreds of tiles a frame under the same camera and the same settings, and for every tile the game worked the camera's arithmetic out again, rebuilt its description of the surface and of each texture's filtering, and compared the lot with the tile before. It now notices nothing was changed and goes straight to drawing. The fog of war is sent to the card only where it moved since the last frame, instead of all of it every frame. Two brutal computer players on Twilight Flame at 1920x1080 went from 2.46ms a frame to 2.24ms, and every picture is the one it was.
- Raise the particle limit as far as you like and every puff gets drawn. Past about sixteen thousand smoke and fire sprites on screen the game quietly stopped drawing the rest: a test with 68,000 of them showed barely a fifth. A test with 101,000 now draws all of them at 8.3ms a frame, and in 1,200 frames not one fell below 60 a second. The first version that drew them all took 22.4ms; the rest came from sorting and copying them far less and from sharing the work across every core of the processor.
- Big fights no longer drag the picture down into the twenties. Every moving tank and soldier, and every puff of smoke, goes to the graphics card through one shared scratch area, and it was so small that a big fight on Bombardment Beach filled it about fifty times a frame. Each refill made the processor wait for the card, 8 to 12ms of every frame, while the card sat two thirds idle. The area is made full size once now and refills about seven times a frame, in 0.02ms. The same 16,000-frame battle, fast-forwarded, finishes in 226 seconds where it took between 263 and 325, and the seconds that fell under 50 frames a second went from 125 or more to 4.
- Every animation runs on a clock instead of counting frames.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp
index b3dc62e55..3ef7ae824 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp
@@ -47,6 +47,27 @@ static const GUID DX11_TEXTURE_TARGET =
// {2E2E9C24-1B2A-4C7E-9E2F-1D0B7E9A5C01}
static const GUID DX11_TEXTURE_DIRTY =
{ 0x2e2e9c24, 0x1b2a, 0x4c7e, { 0x9e, 0x2f, 0x1d, 0x0b, 0x7e, 0x9a, 0x5c, 0x01 } };
+// Which sixteen bit update last went into the copy, matched against LastUpdate below.
+// {2E2E9C25-1B2A-4C7E-9E2F-1D0B7E9A5C01}
+static const GUID DX11_TEXTURE_UPDATE =
+ { 0x2e2e9c25, 0x1b2a, 0x4c7e, { 0x9e, 0x2f, 0x1d, 0x0b, 0x7e, 0x9a, 0x5c, 0x01 } };
+
+// The source rows of the last sixteen bit update, so the next one into the same rectangle of the
+// same texture expands and sends only the rows that differ. The shroud copies its whole visible
+// rectangle every frame and most of its rows have not moved since the frame before; expanding all
+// of them anyway was 0.04ms of a 2.2ms frame. One entry, because the shroud is the only texture
+// updated every frame; anything else in between just takes the whole copy as before. The
+// generation is stamped on the D3D9 texture as well, so a texture made later at a freed one's
+// address does not inherit its rows.
+struct LastUpdateRows
+{
+ unsigned long long Generation;
+ D3D11_BOX Box;
+ D3DFORMAT Format;
+ std::vector<unsigned char> Rows;
+};
+static LastUpdateRows LastUpdate = { 0 };
+static unsigned long long NextUpdateGeneration = 1;
// Why the first texture that could not be copied could not be copied. A count of refusals says
// how much of the picture is missing its texture; this says what to fix.
@@ -471,13 +492,25 @@ bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,
}
ID3D11ShaderResourceView * view = DX11Texture_Mirror(device, context, texture);
- texture->Release();
+ unsigned long long generation = 0;
+ DWORD generation_size = sizeof(generation);
+ if (FAILED(texture->GetPrivateData(DX11_TEXTURE_UPDATE, &generation, &generation_size))) {
+ generation = 0;
+ }
+ D3DSURFACE_DESC destination_description;
+ // Only a default pool texture that is never drawn into: a managed one is also refreshed whole
+ // from its D3D9 copy behind this function's back, and a target is painted over by draws.
+ const bool drawn_into = FAILED(destination->GetDesc(&destination_description))
+ || (destination_description.Usage & D3DUSAGE_RENDERTARGET) != 0
+ || destination_description.Pool != D3DPOOL_DEFAULT;
if (view == NULL) {
+ texture->Release();
return false;
}
D3DSURFACE_DESC source_description;
if (FAILED(source->GetDesc(&source_description))) {
+ texture->Release();
return false;
}
@@ -490,12 +523,14 @@ bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,
? (unsigned)(source_rectangle->bottom - source_rectangle->top)
: source_description.Height;
if (width == 0 || height == 0) {
+ texture->Release();
return false;
}
D3DLOCKED_RECT locked;
if (FAILED(source->LockRect(&locked, source_rectangle, D3DLOCK_READONLY))) {
note_refusal("the surface being copied from would not lock");
+ texture->Release();
return false;
}
@@ -503,6 +538,7 @@ bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,
view->GetResource(&resource);
if (resource == NULL) {
source->UnlockRect();
+ texture->Release();
return false;
}
@@ -516,19 +552,73 @@ bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,
// The copy was expanded to eight bits a channel when it was made, so the update has to be
// expanded the same way or the rows land at the wrong stride in the wrong format.
- if (is_sixteen_bit_colour(source_description.Format)) {
+ if (is_sixteen_bit_colour(source_description.Format) && !drawn_into) {
+ const unsigned row_bytes = width * 2;
+ const unsigned char * rows = (const unsigned char *)locked.pBits;
+ const bool same_rectangle = generation != 0 && generation == LastUpdate.Generation
+ && memcmp(&box, &LastUpdate.Box, sizeof(box)) == 0
+ && LastUpdate.Format == source_description.Format
+ && LastUpdate.Rows.size() == (size_t)row_bytes * height;
+ if (!same_rectangle) {
+ LastUpdate.Rows.resize((size_t)row_bytes * height);
+ LastUpdate.Box = box;
+ LastUpdate.Format = source_description.Format;
+ }
+
+ // Each run of rows that differs from what the copy already holds goes up as one box; with
+ // a new rectangle every row differs and that is the whole copy, as it always was.
const unsigned pitch = width * 4;
- std::vector<unsigned char> expanded(pitch * height);
- expand_sixteen_bit(&expanded[0], pitch, (const unsigned char *)locked.pBits, locked.Pitch,
- width, height, source_description.Format);
- context->UpdateSubresource(resource, 0, &box, &expanded[0], pitch, 0);
+ std::vector<unsigned char> expanded;
+ unsigned row = 0;
+ while (row < height) {
+ unsigned char * held = &LastUpdate.Rows[(size_t)row * row_bytes];
+ if (same_rectangle && memcmp(held, rows + row * locked.Pitch, row_bytes) == 0) {
+ ++row;
+ continue;
+ }
+ unsigned end = row;
+ while (end < height) {
+ held = &LastUpdate.Rows[(size_t)end * row_bytes];
+ if (same_rectangle && memcmp(held, rows + end * locked.Pitch, row_bytes) == 0) {
+ break;
+ }
+ memcpy(held, rows + end * locked.Pitch, row_bytes);
+ ++end;
+ }
+ expanded.resize((size_t)pitch * (end - row));
+ expand_sixteen_bit(&expanded[0], pitch, rows + row * locked.Pitch, locked.Pitch,
+ width, end - row, source_description.Format);
+ D3D11_BOX run = box;
+ run.top = box.top + row;
+ run.bottom = box.top + end;
+ context->UpdateSubresource(resource, 0, &run, &expanded[0], pitch, 0);
+ row = end;
+ }
+
+ if (!same_rectangle) {
+ LastUpdate.Generation = NextUpdateGeneration++;
+ texture->SetPrivateData(DX11_TEXTURE_UPDATE, &LastUpdate.Generation,
+ sizeof(LastUpdate.Generation), 0);
+ }
}
else {
- context->UpdateSubresource(resource, 0, &box, locked.pBits, locked.Pitch, 0);
+ // Whatever this wrote over, the rows above no longer stand for it.
+ LastUpdate.Generation = 0;
+ if (is_sixteen_bit_colour(source_description.Format)) {
+ const unsigned pitch = width * 4;
+ std::vector<unsigned char> expanded(pitch * height);
+ expand_sixteen_bit(&expanded[0], pitch, (const unsigned char *)locked.pBits,
+ locked.Pitch, width, height, source_description.Format);
+ context->UpdateSubresource(resource, 0, &box, &expanded[0], pitch, 0);
+ }
+ else {
+ context->UpdateSubresource(resource, 0, &box, locked.pBits, locked.Pitch, 0);
+ }
}
resource->Release();
source->UnlockRect();
+ texture->Release();
return true;
}
diff --git a/GeneralsMD/Code/Tests/test_dx11texture.cpp b/GeneralsMD/Code/Tests/test_dx11texture.cpp
index 1a38dc551..f3b98b7cf 100644
--- a/GeneralsMD/Code/Tests/test_dx11texture.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11texture.cpp
@@ -320,3 +320,79 @@ TEST(dx11texture_an_alphaless_format_comes_out_opaque)
texture->Release();
}
+
+// The shroud's way in: a sixteen bit system memory surface copied over a default pool texture every
+// frame, most rows the same as the frame before. Only the rows that changed are sent again, so a
+// row wrongly taken for unchanged keeps the previous frame's value in the copy.
+static unsigned short shroud_pixel(unsigned row, unsigned column, unsigned frame)
+{
+ return (unsigned short)(0xf000 | ((row + frame) & 0xf) << 8 | (column & 0xf) << 4 | (frame & 0xf));
+}
+
+TEST(dx11texture_a_repeated_sixteen_bit_copy_carries_only_what_changed)
+{
+ Direct3D9Fixture d3d9;
+ if (!d3d9.Create()) {
+ printf("skip: no Direct3D 9 device on this machine\n");
+ return;
+ }
+
+ DX11DeviceClass d3d11;
+ CHECK(d3d11.Create_Offscreen());
+
+ IDirect3DTexture9 * texture = NULL;
+ IDirect3DSurface9 * source = NULL;
+ if (FAILED(d3d9.Get()->CreateTexture(TEXTURE_SIZE, TEXTURE_SIZE, ONE_LEVEL, 0, D3DFMT_A4R4G4B4,
+ D3DPOOL_DEFAULT, &texture, NULL))
+ || FAILED(d3d9.Get()->CreateOffscreenPlainSurface(TEXTURE_SIZE, TEXTURE_SIZE,
+ D3DFMT_A4R4G4B4, D3DPOOL_SYSTEMMEM, &source, NULL))) {
+ printf("skip: no A4R4G4B4 default pool texture on this machine\n");
+ if (texture != NULL) {
+ texture->Release();
+ }
+ return;
+ }
+ IDirect3DSurface9 * destination = NULL;
+ CHECK(SUCCEEDED(texture->GetSurfaceLevel(0, &destination)));
+
+ // Frame 0 writes every row, frame 1 changes rows 2 and 5 only, frame 2 changes nothing.
+ const unsigned frame_of_row[3][TEXTURE_SIZE] = {
+ { 0, 0, 0, 0, 0, 0, 0, 0 },
+ { 0, 0, 1, 0, 0, 1, 0, 0 },
+ { 0, 0, 1, 0, 0, 1, 0, 0 } };
+ const RECT whole = { 0, 0, (LONG)TEXTURE_SIZE, (LONG)TEXTURE_SIZE };
+ const POINT origin = { 0, 0 };
+ for (unsigned frame = 0; frame < 3; ++frame) {
+ D3DLOCKED_RECT locked;
+ CHECK(SUCCEEDED(source->LockRect(&locked, NULL, 0)));
+ for (unsigned row = 0; row < TEXTURE_SIZE; ++row) {
+ unsigned short * out = (unsigned short *)((unsigned char *)locked.pBits + row * locked.Pitch);
+ for (unsigned column = 0; column < TEXTURE_SIZE; ++column) {
+ out[column] = shroud_pixel(row, column, frame_of_row[frame][row]);
+ }
+ }
+ source->UnlockRect();
+ CHECK(DX11Texture_Update(d3d11.Get_Device(), d3d11.Get_Context(), destination, source,
+ &whole, &origin));
+
+ ID3D11ShaderResourceView * view =
+ DX11Texture_Mirror(d3d11.Get_Device(), d3d11.Get_Context(), texture);
+ CHECK(view != NULL);
+ unsigned char pixels[TEXTURE_SIZE * TEXTURE_SIZE * 4];
+ CHECK(read_back(d3d11, view, pixels, TEXTURE_SIZE * 4));
+ for (unsigned row = 0; row < TEXTURE_SIZE; ++row) {
+ for (unsigned column = 0; column < TEXTURE_SIZE; ++column) {
+ const unsigned short expected = shroud_pixel(row, column, frame_of_row[frame][row]);
+ const unsigned char * pixel = pixels + row * TEXTURE_SIZE * 4 + column * 4;
+ CHECK_EQ(pixel[0], (unsigned char)((expected & 0xf) * 17));
+ CHECK_EQ(pixel[1], (unsigned char)(((expected >> 4) & 0xf) * 17));
+ CHECK_EQ(pixel[2], (unsigned char)(((expected >> 8) & 0xf) * 17));
+ CHECK_EQ(pixel[3], 0xff);
+ }
+ }
+ }
+
+ destination->Release();
+ source->Release();
+ texture->Release();
+}