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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
test(ww3d2): probe the coordinate a stage generates for itself
Olcay Seygan5417589f556a
diff --git a/GeneralsMD/Code/Tests/test_ffvertexd3d9.cpp b/GeneralsMD/Code/Tests/test_ffvertexd3d9.cppindex 3409aa42b..adaed0fd0 100644--- a/GeneralsMD/Code/Tests/test_ffvertexd3d9.cpp+++ b/GeneralsMD/Code/Tests/test_ffvertexd3d9.cpp@@ -468,3 +468,179 @@ TEST(ffvertexd3d9_the_building_pipeline_lights_a_quad_like_the_fixed_function_docompare(scene);}++// The coordinate a stage generates for itself, read back as a colour.+//+// Nineteen of the sixty-five texture stages a Flash Effect frame sets do not read a coordinate out+// of the vertex at all: they ask for the camera space position, or the camera space normal, and put+// it through a texture matrix. That is the scrolling noise the terrain and the models are+// multiplied by, and until now nothing checked it - the lighting tests above use one pass-through+// stage, and a wrong coordinate there would look like a texture, not like an error.+//+// The probe is a strip 256 texels wide whose red channel is its own index, sampled with a point+// filter and clamped. Whatever coordinate reaches the sampler comes back as a number: red 89 means+// u landed in the 90th texel and nowhere else. The matrix maps the camera space z to u, so both+// paths are asked the same question about the same axis.+static const unsigned PROBE_WIDTH = 256;++// u = 0.5 * z + 0.1 written for D3D9's row vector convention, so the third row scales z into u and+// the fourth row carries the offset. With z at 0.5 across the quad, u is 0.35 and the texel is 89.+static void set_probe_matrix(D3DMATRIX & matrix)+{+ memset(&matrix, 0, sizeof(matrix));+ matrix._31 = 0.5f;+ matrix._41 = 0.1f;+ matrix._42 = 0.25f;+ matrix._44 = 1.0f;+}++static IDirect3DTexture9 * make_probe(IDirect3DDevice9 * device)+{+ IDirect3DTexture9 * texture = NULL;+ if (FAILED(device->CreateTexture(PROBE_WIDTH, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,+ &texture, NULL))) {+ return NULL;+ }+ D3DLOCKED_RECT locked;+ if (FAILED(texture->LockRect(0, &locked, NULL, 0))) {+ texture->Release();+ return NULL;+ }+ unsigned char * row = static_cast<unsigned char *>(locked.pBits);+ for (unsigned texel = 0; texel < PROBE_WIDTH; ++texel) {+ row[texel * 4 + 0] = 64;+ row[texel * 4 + 1] = static_cast<unsigned char>(255 - texel);+ row[texel * 4 + 2] = static_cast<unsigned char>(texel);+ row[texel * 4 + 3] = 255;+ }+ texture->UnlockRect(0);+ return texture;+}++// Everything the probe shares with itself on both paths: the texture selected rather than the+// vertex colour, no filtering to blur which texel was chosen, and no lighting to colour it.+static void configure_probe(IDirect3DDevice9 * device)+{+ device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);+ device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);+ device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);+ device->SetRenderState(D3DRS_SPECULARENABLE, FALSE);+ device->SetRenderState(D3DRS_COLORVERTEX, FALSE);+ device->SetRenderState(D3DRS_FOGENABLE, FALSE);+ device->SetRenderState(D3DRS_LIGHTING, FALSE);+ device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);+ device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);+ device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);+ device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);+ device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);+ device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);+ device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);+ device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);+ device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);+ device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);+ device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);+}++static VertexPipelineDescription describe_probe(DWORD generation)+{+ VertexPipelineDescription description;+ memset(&description, 0, sizeof(description));+ description.FVF = QUAD_FVF;+ description.LightingEnabled = false;+ description.SpecularEnabled = false;+ description.ColourVertexEnabled = false;+ description.DiffuseMaterialSource = D3DMCS_MATERIAL;+ description.AmbientMaterialSource = D3DMCS_MATERIAL;+ description.EmissiveMaterialSource = D3DMCS_MATERIAL;+ description.SpecularMaterialSource = D3DMCS_MATERIAL;+ description.LightCount = 0;+ description.StageCount = 1;+ description.Stages[0].TextureCoordinateIndex = generation;+ description.Stages[0].TextureTransformFlags = D3DTTFF_COUNT2;+ description.FogEnabled = false;+ return description;+}++static void compare_probe(DWORD generation, const char * what)+{+ D3D9OffscreenDevice fixture;+ if (!fixture.Create(TARGET_SIZE)) {+ printf(" no Direct3D 9 device on this machine - skipped\n");+ return;+ }+ D3DCompileFunction compile = load_compiler();+ if (compile == NULL) {+ printf(" no %s on this machine - skipped\n", COMPILER_MODULE);+ return;+ }+ IDirect3DDevice9 * device = fixture.Get_Device();++ IDirect3DTexture9 * probe = make_probe(device);+ CHECK(probe != NULL);+ if (probe == NULL) {+ return;+ }++ D3DMATRIX identity;+ D3D9Test_Set_Identity(identity);+ D3DMATRIX texture_matrix;+ set_probe_matrix(texture_matrix);++ clear_target(device);+ configure_probe(device);+ device->SetTexture(0, probe);+ device->SetTransform(D3DTS_WORLD, &identity);+ device->SetTransform(D3DTS_VIEW, &identity);+ device->SetTransform(D3DTS_PROJECTION, &identity);+ device->SetTransform(D3DTS_TEXTURE0, &texture_matrix);+ device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, generation);+ device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);+ device->SetVertexShader(NULL);+ device->SetFVF(QUAD_FVF);+ CHECK(draw_quad(device));++ unsigned char fixed_function[3] = { 0, 0, 0 };+ CHECK(fixture.Read_Pixel(TARGET_SIZE / 2, TARGET_SIZE / 2, fixed_function));++ std::string hlsl;+ CHECK(VertexShader_Generate(describe_probe(generation), VERTEX_SHADER_TARGET_D3D9, hlsl));+ IDirect3DVertexShader9 * shader = build_shader(device, compile, hlsl);+ CHECK(shader != NULL);+ if (shader == NULL) {+ probe->Release();+ return;+ }++ clear_target(device);+ configure_probe(device);+ device->SetTexture(0, probe);+ set_matrix(device, VERTEX_REGISTER_WORLD_VIEW_PROJECTION, identity);+ set_matrix(device, VERTEX_REGISTER_WORLD_VIEW, identity);+ set_matrix(device, VERTEX_REGISTER_NORMAL_TRANSFORM, identity);+ set_matrix(device, VERTEX_REGISTER_TEXTURE_MATRICES, texture_matrix);+ device->SetFVF(QUAD_FVF);+ device->SetVertexShader(shader);+ CHECK(draw_quad(device));+ device->SetVertexShader(NULL);+ shader->Release();++ unsigned char generated[3] = { 0, 0, 0 };+ CHECK(fixture.Read_Pixel(TARGET_SIZE / 2, TARGET_SIZE / 2, generated));+ probe->Release();++ printf(" %s: fixed function texel %u, generated texel %u\n",+ what, fixed_function[2], generated[2]);++ const int difference = static_cast<int>(generated[2]) - static_cast<int>(fixed_function[2]);+ CHECK(difference >= -1 && difference <= 1);+}++TEST(ffvertexd3d9_a_camera_space_coordinate_goes_through_the_texture_matrix_the_same_way)+{+ compare_probe(D3DTSS_TCI_CAMERASPACEPOSITION, "camera space position");+}++TEST(ffvertexd3d9_a_camera_space_normal_goes_through_the_texture_matrix_the_same_way)+{+ compare_probe(D3DTSS_TCI_CAMERASPACENORMAL, "camera space normal");+}diff --git a/GeneralsMD/Code/Tests/test_pixelcentre.cpp b/GeneralsMD/Code/Tests/test_pixelcentre.cppindex 08130a22c..7a0c23544 100644--- a/GeneralsMD/Code/Tests/test_pixelcentre.cpp+++ b/GeneralsMD/Code/Tests/test_pixelcentre.cpp@@ -94,7 +94,14 @@ static const unsigned short EDGE_INDICES[6] = { 0, 1, 2, 0, 2, 3 };// left to full on the right, so an alpha test cuts it somewhere in the middle and the column it cuts// at is the threshold read back in pixels. Eight of the sixty-six pipelines a Flash Effect frame// builds carry an alpha test, and all eight are the greater-or-equal one.-static const DWORD ALPHA_TEST_REFERENCE = 128;+static DWORD AlphaTestReference = 128;++// One knife-edge reference proves the rounding and nothing else. A sweep proves the rule, which+// matters because what is left of the difference between the two frames sits on thin alpha tested+// foliage: a palm trunk a pixel wide is exactly where a threshold half a level out shows.+static const DWORD ALPHA_REFERENCES[] = { 1, 16, 32, 64, 96, 128, 160, 192, 224, 254 };+static const unsigned ALPHA_REFERENCE_COUNT =+ sizeof(ALPHA_REFERENCES) / sizeof(ALPHA_REFERENCES[0]);static const EdgeVertex ALPHA_QUAD[4] = {{ { -1.0f, 1.0f, 0.5f }, { 0.0f, 0.0f, 1.0f }, 0x00ff0000, { 0.0f, 0.0f }, { 0.0f, 0.0f } },@@ -159,7 +166,7 @@ static bool direct3d9_draw(const EdgeVertex quad[4], bool alpha_test, TargetLinedevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);device->SetRenderState(D3DRS_ALPHATESTENABLE, alpha_test ? TRUE : FALSE);device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);- device->SetRenderState(D3DRS_ALPHAREF, ALPHA_TEST_REFERENCE);+ device->SetRenderState(D3DRS_ALPHAREF, AlphaTestReference);device->SetVertexShader(NULL);device->SetFVF(EDGE_FVF);@@ -278,7 +285,7 @@ static bool direct3d11_draw(const EdgeVertex quad[4], bool alpha_test, TargetLinconfigure_unlit(backend);backend.Set_Render_State(D3DRS_ALPHATESTENABLE, alpha_test ? TRUE : FALSE);backend.Set_Render_State(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);- backend.Set_Render_State(D3DRS_ALPHAREF, ALPHA_TEST_REFERENCE);+ backend.Set_Render_State(D3DRS_ALPHAREF, AlphaTestReference);backend.Set_Stream_Source(vertices, sizeof(EdgeVertex), 0);backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);@@ -353,17 +360,22 @@ TEST(pixelcentre_the_two_runtimes_cut_the_same_pixels)TargetLine eleven_row;TargetLine eleven_column;- if (!direct3d9_draw(ALPHA_QUAD, true, nine_row, nine_column)- || !direct3d11_draw(ALPHA_QUAD, true, eleven_row, eleven_column)) {- printf(" no Direct3D 9 or Direct3D 11 device on this machine - skipped\n");- return;- }+ for (unsigned index = 0; index < ALPHA_REFERENCE_COUNT; ++index) {+ AlphaTestReference = ALPHA_REFERENCES[index];- const int nine_cut = first_covered(nine_row);- const int eleven_cut = first_covered(eleven_row);+ if (!direct3d9_draw(ALPHA_QUAD, true, nine_row, nine_column)+ || !direct3d11_draw(ALPHA_QUAD, true, eleven_row, eleven_column)) {+ printf(" no Direct3D 9 or Direct3D 11 device on this machine - skipped\n");+ return;+ }- printf(" alpha reference %u: Direct3D 9 keeps from column %d, Direct3D 11 from %d\n",- ALPHA_TEST_REFERENCE, nine_cut, eleven_cut);+ const int nine_cut = first_covered(nine_row);+ const int eleven_cut = first_covered(eleven_row);- CHECK_EQ(eleven_cut, nine_cut);+ if (eleven_cut != nine_cut) {+ printf(" alpha reference %u: Direct3D 9 keeps from column %d, Direct3D 11 from %d\n",+ AlphaTestReference, nine_cut, eleven_cut);+ }+ CHECK_EQ(eleven_cut, nine_cut);+ }}diff --git a/dx11-check.ps1 b/dx11-check.ps1index c39a5e037..219f4e154 100644--- a/dx11-check.ps1+++ b/dx11-check.ps1@@ -26,7 +26,11 @@# -BackendNoise takes the repeated pair through the Direct3D 11 backend instead, which answers a# different question: the printed floor is Direct3D 9 against itself, and a backend with a# repeatability of its own would be measured against a floor that cannot see it.-param([double]$Margin = 1.0, [string]$Map = '', [switch]$BackendNoise)+#+# -Extra passes further switches to all three shots, which is how a pass is taken out of both sides+# at once: -Extra -nofx, -Extra -noshadowvolumes, -Extra -noshroud. A difference that collapses when+# one pass is gone belongs to that pass, and one that does not is somewhere else.+param([double]$Margin = 1.0, [string]$Map = '', [switch]$BackendNoise, [string[]]$Extra = @())Add-Type -AssemblyName System.Drawing$run = Join-Path $PSScriptRoot "GeneralsMD\Run"@@ -53,7 +57,7 @@ function Shoot($c, $tag, $extra) {$arguments = @('-win','-xres','1280','-yres','720','-quickstart','-noshellmap','-multiInstance','-msaa','0','-map',"`"Maps\$($c.map)\$($c.map).map`"",'-autoskirmish','4','-aidiff','easy','-seed','5','-maxframes',($c.f+80),'-screenshot',$c.f,'-camera',$c.x,$c.y,- '-logPrefix',"dx11chk_$tag`_") + $extra+ '-logPrefix',"dx11chk_$tag`_") + $extra + $Extratry {$process = Start-Process (Join-Path $run 'generals.exe') -ArgumentList $arguments -WorkingDirectory $run -PassThru$null = $process.WaitForExit(900000)