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.

fix(ww3d2): let heat haze read the scene it is drawn into

Olcay Seygand58b12557c54
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9f9cdb6e0..d3d68e71e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -484,6 +484,7 @@ found and fixed — EA's own, not port damage.**
- A crater is drawn whole or not at all. When the ground ran out of room for marks, the last one was drawn up to wherever the room ended and stopped there, a strip of it missing. There is room for four times as much ground under marks before that happens, and when it does, the oldest marks are the ones left out.
- Embers and sparks that trail an effect finish their run. When the effect that owned them ended first, they lost track of where it was, were drawn at the corner of the map and died early.
- Heat haze wobbles in both directions. The vertical part of the distortion was rolled, clipped against the top and bottom of the screen, and then never used - every shimmer pulled the picture along one diagonal.
+- The Microwave Tank's heat haze no longer wears a black cloud. The shimmer bends a copy of the picture behind it, and on the new renderer it read that copy while the picture was still being painted into it, which the graphics card answers with nothing at all: a dark blot, fading out towards its edges. It now reads the picture as it stood the moment before the shimmer was drawn.
- One tree in ten stood dead still in the breeze. The number that picks which way a tree sways was drawn from a range one short at the bottom, and the bottom of that range is the slot meaning "no sway at all".
- Police car lights flash at their own speed instead of your monitor's. On a fast screen with the frame cap off they were cycling five and a half times too quickly.
- Ten more places that wrote terrain, roads, bridges, trees, foundations and scorch marks into a buffer the graphics card had refused to hand over. Every one of them wrote to address zero instead, and a graphics device pulled out from under the game is all it takes; the frame is simply skipped now.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index 67804e5bf..797118929 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -189,6 +189,9 @@ DX11BackendClass::DX11BackendClass()
, DrawsIntoTargets(0)
, CurrentTarget(NULL)
, CurrentDepth(NULL)
+ , CurrentTargetResource(NULL)
+ , TargetCopy(NULL)
+ , TargetCopyView(NULL)
, DrawsMade(0)
, DrawsRefused(0)
, RefusedNoBuffer(0)
@@ -354,6 +357,12 @@ void DX11BackendClass::Shutdown()
UserBuffer = NULL;
UserBufferBytes = 0;
}
+ if (TargetCopy != NULL) {
+ TargetCopyView->Release();
+ TargetCopy->Release();
+ TargetCopyView = NULL;
+ TargetCopy = NULL;
+ }
Device = NULL;
}
@@ -777,6 +786,7 @@ void DX11BackendClass::Set_Render_Target(ID3D11RenderTargetView * target)
++TargetsBound;
Trace_Target("texture", width, height);
CurrentTarget = target;
+ CurrentTargetResource = resource;
CurrentDepth = Depth_For(width, height);
Device->Get_Context()->OMSetRenderTargets(1, &CurrentTarget, CurrentDepth);
Set_Viewport(0, 0, width, height);
@@ -1356,11 +1366,61 @@ void DX11BackendClass::Bind_State_Objects()
context->RSSetState(Rasterizer_State());
ID3D11SamplerState * samplers[DX11_BACKEND_TEXTURE_STAGES];
+ ID3D11ShaderResourceView * textures[DX11_BACKEND_TEXTURE_STAGES];
for (unsigned sampler = 0; sampler < DX11_BACKEND_TEXTURE_STAGES; ++sampler) {
samplers[sampler] = Sampler_State(sampler);
+ textures[sampler] = Readable_Texture(Textures[sampler]);
}
context->PSSetSamplers(0, DX11_BACKEND_TEXTURE_STAGES, samplers);
- context->PSSetShaderResources(0, DX11_BACKEND_TEXTURE_STAGES, Textures);
+ context->PSSetShaderResources(0, DX11_BACKEND_TEXTURE_STAGES, textures);
+}
+
+ID3D11ShaderResourceView * DX11BackendClass::Readable_Texture(ID3D11ShaderResourceView * texture)
+{
+ if (texture == NULL || CurrentTarget == NULL) {
+ return texture;
+ }
+
+ ID3D11Resource * resource = NULL;
+ texture->GetResource(&resource);
+ resource->Release();
+ if (resource != CurrentTargetResource) {
+ return texture;
+ }
+
+ // Set_Render_Target keeps only a target that is a two dimensional texture.
+ D3D11_TEXTURE2D_DESC description;
+ static_cast<ID3D11Texture2D *>(resource)->GetDesc(&description);
+ if (TargetCopy != NULL) {
+ D3D11_TEXTURE2D_DESC held;
+ TargetCopy->GetDesc(&held);
+ if (held.Width != description.Width || held.Height != description.Height
+ || held.Format != description.Format) {
+ TargetCopyView->Release();
+ TargetCopy->Release();
+ TargetCopyView = NULL;
+ TargetCopy = NULL;
+ }
+ }
+
+ if (TargetCopy == NULL) {
+ description.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+ description.MiscFlags = 0;
+ if (FAILED(Device->Get_Device()->CreateTexture2D(&description, NULL, &TargetCopy))) {
+ Note_Refusal("the device refused a copy of the render target a draw samples");
+ TargetCopy = NULL;
+ return texture;
+ }
+ if (FAILED(Device->Get_Device()->CreateShaderResourceView(TargetCopy, NULL, &TargetCopyView))) {
+ Note_Refusal("the device refused a view of the render target's copy");
+ TargetCopy->Release();
+ TargetCopy = NULL;
+ return texture;
+ }
+ }
+
+ Device->Get_Context()->CopyResource(TargetCopy, resource);
+ return TargetCopyView;
}
bool DX11BackendClass::Draw_Indexed_Triangles(unsigned index_count, unsigned start_index,
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index c90cd4d06..338c87684 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -417,6 +417,16 @@ private:
std::map<unsigned long long, ID3D11DepthStencilView *> TargetDepths;
ID3D11DepthStencilView * Depth_For(unsigned width, unsigned height);
+ // A draw that samples the target it is drawing into reads a copy of it taken just before the
+ // draw. Direct3D 9 handed such a draw whatever had been drawn so far; Direct3D 11 unbinds a
+ // resource that is bound both ways and the sample reads zero. Heat haze samples the scene while
+ // the scene is still going into that texture, and read as zero it put a black blob around every
+ // microwave tank.
+ ID3D11Resource * CurrentTargetResource;
+ ID3D11Texture2D * TargetCopy;
+ ID3D11ShaderResourceView * TargetCopyView;
+ ID3D11ShaderResourceView * Readable_Texture(ID3D11ShaderResourceView * texture);
+
std::map<std::string, ID3D11BlendState *> BlendStates;
std::map<std::string, ID3D11DepthStencilState *> DepthStencilStates;
std::map<std::string, ID3D11RasterizerState *> RasterizerStates;
diff --git a/GeneralsMD/Code/Tests/test_dx11backend.cpp b/GeneralsMD/Code/Tests/test_dx11backend.cpp
index 20ca9b15c..a4e749497 100644
--- a/GeneralsMD/Code/Tests/test_dx11backend.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11backend.cpp
@@ -546,6 +546,92 @@ TEST(dx11backend_the_alpha_test_cuts_the_pixels_the_comparison_rejects)
backend.Shutdown();
}
+// Heat haze samples the scene while the scene is still being drawn into that texture. The target is
+// cleared white and the quad multiplies what it samples by the vertex colour, so reading the scene
+// gives the vertex colour, the sample Direct3D 11 unbinds gives black, and a refused draw leaves white.
+TEST(dx11backend_a_draw_that_samples_its_own_target_reads_what_was_drawn)
+{
+ DX11DeviceClass device;
+ device.Request_Debug_Layer();
+ CHECK(device.Create_Offscreen());
+ ID3D11Device * d3d = device.Get_Device();
+
+ DX11BackendClass backend;
+ CHECK(backend.Initialise(&device));
+
+ D3D11_TEXTURE2D_DESC target_description;
+ memset(&target_description, 0, sizeof(target_description));
+ target_description.Width = TARGET_SIZE;
+ target_description.Height = TARGET_SIZE;
+ target_description.MipLevels = 1;
+ target_description.ArraySize = 1;
+ target_description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
+ target_description.SampleDesc.Count = 1;
+ target_description.Usage = D3D11_USAGE_DEFAULT;
+ target_description.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
+
+ ID3D11Texture2D * target = NULL;
+ CHECK(SUCCEEDED(d3d->CreateTexture2D(&target_description, NULL, &target)));
+
+ D3D11_TEXTURE2D_DESC staging_description = target_description;
+ staging_description.Usage = D3D11_USAGE_STAGING;
+ staging_description.BindFlags = 0;
+ staging_description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
+
+ ID3D11Texture2D * staging = NULL;
+ CHECK(SUCCEEDED(d3d->CreateTexture2D(&staging_description, NULL, &staging)));
+
+ ID3D11RenderTargetView * target_view = NULL;
+ CHECK(SUCCEEDED(d3d->CreateRenderTargetView(target, NULL, &target_view)));
+
+ ID3D11ShaderResourceView * target_texture = NULL;
+ CHECK(SUCCEEDED(d3d->CreateShaderResourceView(target, NULL, &target_texture)));
+
+ ID3D11Buffer * vertices = NULL;
+ CHECK(DX11Resource_Create_Vertex_Buffer(d3d, sizeof(QUAD_VERTICES), D3DPOOL_MANAGED, 0,
+ QUAD_VERTICES, &vertices));
+
+ ID3D11Buffer * indices = NULL;
+ CHECK(DX11Resource_Create_Index_Buffer(d3d, sizeof(QUAD_INDICES), D3DPOOL_MANAGED, 0,
+ QUAD_INDICES, &indices));
+
+ const float clear_colour[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ device.Get_Context()->ClearRenderTargetView(target_view, clear_colour);
+
+ backend.Set_Render_Target(target_view);
+ configure_unlit_pass_through(backend);
+ backend.Set_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+ backend.Set_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
+ backend.Set_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
+ backend.Set_Texture(0, target_texture);
+ backend.Set_Stream_Source(vertices, sizeof(BackendVertex), 0);
+ backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);
+
+ CHECK(backend.Draw_Indexed_Triangles(6, 0, 0));
+
+ device.Get_Context()->CopyResource(staging, target);
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ CHECK(SUCCEEDED(device.Get_Context()->Map(staging, 0, D3D11_MAP_READ, 0, &mapped)));
+ const unsigned char * centre = static_cast<const unsigned char *>(mapped.pData)
+ + mapped.RowPitch * (TARGET_SIZE / 2) + (TARGET_SIZE / 2) * 4;
+ if (centre[0] != EXPECTED_BLUE) {
+ printf(" centre pixel bgra %02x %02x %02x %02x\n",
+ centre[0], centre[1], centre[2], centre[3]);
+ }
+ CHECK_EQ(centre[0], EXPECTED_BLUE);
+ CHECK_EQ(centre[1], EXPECTED_GREEN);
+ CHECK_EQ(centre[2], EXPECTED_RED);
+ device.Get_Context()->Unmap(staging, 0);
+
+ indices->Release();
+ vertices->Release();
+ target_texture->Release();
+ target_view->Release();
+ staging->Release();
+ target->Release();
+ backend.Shutdown();
+}
+
// A draw with no stream bound is refused rather than attempted. D3D9 would have drawn nothing and
// said nothing; a count that goes up is the difference between a missing pass and a silent one.
TEST(dx11backend_a_draw_with_nothing_bound_is_counted_as_refused)