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): make Convert_Color/Clamp_Color hand back every register

Olcay Seygand5b04c3c2cbc
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
index 835aa0ba4..23ad9da6c 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
@@ -1011,6 +1011,18 @@ WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha
// such that 32 bit ingeger has AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB.
__asm
{
+ // This block is inlined wherever colors are converted, and VS2022 keeps live
+ // values in ANY of these registers across it -- SegLineRendererClass::Render
+ // held its point count in EAX and its transform reference in EBX, and both
+ // came back as color bytes (the fast_float_trunc lesson, one register wider).
+ // Hand back every register the way it was found; col carries the result out.
+ push eax
+ push ecx
+ push edx
+ push ebx
+ push esi
+ push edi
+
sub esp,20 // space for a, r, g and b float plus fpu rounding mode
// Store the fpu rounding mode
@@ -1068,6 +1080,13 @@ not_changed:
add esp,20
mov col,eax
+
+ pop edi
+ pop esi
+ pop ebx
+ pop edx
+ pop ecx
+ pop eax
}
return col;
}
@@ -1090,6 +1109,12 @@ WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
__asm
{
+ // Same contract as Convert_Color above: leave every register as found.
+ push edx
+ push ebx
+ push esi
+ push edi
+
mov esi,dword ptr color
mov edx,0x3f800000
@@ -1129,6 +1154,11 @@ WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
cmp edi,edx // if no less than 1.0 set to 1.0
cmovnb edi,edx
mov dword ptr[esi+12],edi
+
+ pop edi
+ pop esi
+ pop ebx
+ pop edx
}
}
diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cpp
index ef5c5324f..7905839ab 100644
--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp
+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp
@@ -1557,3 +1557,94 @@ TEST(w3d_shader_struct_is_the_size_the_file_format_expects)
CHECK_EQ(sizeof(W3dVectorStruct), 12u);
CHECK_EQ(sizeof(W3dQuaternionStruct), 16u);
}
+
+/* ------------------------------------------------------------------ */
+/* DX8Wrapper::Convert_Color / Clamp_Color register discipline.
+ *
+ * Both are WWINLINE __asm blocks in dx8wrapper.h, inlined into every
+ * renderer that converts a color. The Convert_Color block used EAX,
+ * EBX, ECX, EDX, ESI and EDI as scratch without handing them back;
+ * VS2022 keeps live values in those registers across the inlined block
+ * (SegLineRendererClass::Render held its point count in EAX and its
+ * transform reference in EBX), so the shell map died in Render with a
+ * color value where a pointer should have been. Same class of bug as
+ * fast_float_trunc and CRC::computeCRC, witnessed the same way: plant
+ * a sentinel, run the block, read the sentinel back. The witnesses
+ * cover the callee-saved registers only - the compiler is entitled to
+ * use EAX/ECX/EDX for its own glue between the two asm blocks. */
+
+#include "dx8wrapper.h"
+
+static void convertColorRegisterWitness(const Vector3 *rgb, float alpha, unsigned *colOut,
+ unsigned *ebxOut, unsigned *esiOut, unsigned *ediOut)
+{
+ __asm
+ {
+ mov ebx, 0x0BADF00D
+ mov esi, 0x0BADBEEF
+ mov edi, 0x0BADCAFE
+ }
+ *colOut = DX8Wrapper::Convert_Color(*rgb, alpha);
+ __asm
+ {
+ mov eax, ebxOut
+ mov dword ptr [eax], ebx
+ mov eax, esiOut
+ mov dword ptr [eax], esi
+ mov eax, ediOut
+ mov dword ptr [eax], edi
+ }
+}
+
+TEST(convert_color_leaves_the_callee_saved_registers_alone)
+{
+ Vector3 rgb(0.4f, 0.8f, 0.2f);
+ unsigned col = 0, ebxOut = 0, esiOut = 0, ediOut = 0;
+ convertColorRegisterWitness(&rgb, 1.0f, &col, &ebxOut, &esiOut, &ediOut);
+ CHECK_EQ(ebxOut, 0x0BADF00D);
+ CHECK_EQ(esiOut, 0x0BADBEEF);
+ CHECK_EQ(ediOut, 0x0BADCAFE);
+
+ /* ...and it still converts: truncate(x*255), packed AARRGGBB. */
+ unsigned expected = ((unsigned)(1.0f * 255.0f) << 24)
+ | ((unsigned)(0.4f * 255.0f) << 16)
+ | ((unsigned)(0.8f * 255.0f) << 8)
+ | (unsigned)(0.2f * 255.0f);
+ CHECK_EQ(col, expected);
+}
+
+static void clampColorRegisterWitness(Vector4 *color,
+ unsigned *ebxOut, unsigned *esiOut, unsigned *ediOut)
+{
+ __asm
+ {
+ mov ebx, 0x0BADF00D
+ mov esi, 0x0BADBEEF
+ mov edi, 0x0BADCAFE
+ }
+ DX8Wrapper::Clamp_Color(*color);
+ __asm
+ {
+ mov eax, ebxOut
+ mov dword ptr [eax], ebx
+ mov eax, esiOut
+ mov dword ptr [eax], esi
+ mov eax, ediOut
+ mov dword ptr [eax], edi
+ }
+}
+
+TEST(clamp_color_leaves_the_callee_saved_registers_alone)
+{
+ Vector4 color(-0.5f, 0.5f, 2.0f, 1.0f);
+ unsigned ebxOut = 0, esiOut = 0, ediOut = 0;
+ clampColorRegisterWitness(&color, &ebxOut, &esiOut, &ediOut);
+ CHECK_EQ(ebxOut, 0x0BADF00D);
+ CHECK_EQ(esiOut, 0x0BADBEEF);
+ CHECK_EQ(ediOut, 0x0BADCAFE);
+
+ CHECK_NEAR(color.X, 0.0f, 0.0001f);
+ CHECK_NEAR(color.Y, 0.5f, 0.0001f);
+ CHECK_NEAR(color.Z, 1.0f, 0.0001f);
+ CHECK_NEAR(color.W, 1.0f, 0.0001f);
+}