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
perf(w3ddevice): lock the radar textures once per pass, not once per pixel
Olcay Seygan3008df088b60
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 22f56a3aa..e7d34d4b1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1019,6 +1019,13 @@ the entire map that way, one dot after another. The picture is now kept in ordindot costs nothing, and handed over once a frame — only the rectangle that actually changed, andnothing at all on a frame where nothing moved.+**And the coloured dots on it.** The same picture carries a dot for every unit and building you can+see, and it is repainted several times a second. Each dot was handed to the graphics card and taken+back on its own, four times over, once per thing on the map -- so the busier the match got, the+bigger the hiccup, and it landed on a regular beat rather than spreading out. The whole minimap is+now handed over once and painted in one go. The map's terrain picture, redrawn whenever a bridge+comes down, works the same way instead of a hundred thousand separate trips.+**And scrolling across a big map.** Every frame the camera moves, the game sweeps the whole terrainlooking for its lowest point. It was reading that grid across the grain — every step landing in adifferent part of memory — and it is read along the grain now. Same answer, a fraction of the work,diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cppindex e0e8ac101..e3fa6e3dd 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp@@ -627,6 +627,12 @@ void W3DRadar::renderObjectList( const RadarObject *listHead, TextureClass *text// get surface for texture to render intoSurfaceClass *surface = texture->Get_Surface_Level();+ // this runs every radar refresh and draws four pixels per object. DrawPixel locks and+ // unlocks the texture for each one of them, so lock once for the whole list instead.+ Int surfacePitch;+ void *surfaceBits = surface->Lock( &surfacePitch );+ const UnsignedInt bytesPerPixel = surface->Get_Bytes_Per_Pixel();+// loop through all objects and drawICoord2D radarPoint;@@ -722,21 +728,22 @@ void W3DRadar::renderObjectList( const RadarObject *listHead, TextureClass *text// draw the blip, but make sure the points are legalif( legalRadarPoint( radarPoint.x, radarPoint.y ) )- surface->DrawPixel( radarPoint.x, radarPoint.y, c );+ surface->Draw_Pixel( radarPoint.x, radarPoint.y, c, bytesPerPixel, surfaceBits, surfacePitch );radarPoint.y++;if( legalRadarPoint( radarPoint.x, radarPoint.y ) )- surface->DrawPixel( radarPoint.x, radarPoint.y, c );+ surface->Draw_Pixel( radarPoint.x, radarPoint.y, c, bytesPerPixel, surfaceBits, surfacePitch );radarPoint.x++;if( legalRadarPoint( radarPoint.x, radarPoint.y ) )- surface->DrawPixel( radarPoint.x, radarPoint.y, c );+ surface->Draw_Pixel( radarPoint.x, radarPoint.y, c, bytesPerPixel, surfaceBits, surfacePitch );radarPoint.y--;if( legalRadarPoint( radarPoint.x, radarPoint.y ) )- surface->DrawPixel( radarPoint.x, radarPoint.y, c );+ surface->Draw_Pixel( radarPoint.x, radarPoint.y, c, bytesPerPixel, surfaceBits, surfacePitch );} // end for+ surface->Unlock();REF_PTR_RELEASE(surface);} // end renderObjectList@@ -1038,6 +1045,11 @@ void W3DRadar::buildTerrainTexture( TerrainLogic *terrain )surface = m_terrainTexture->Get_Surface_Level();DEBUG_ASSERTCRASH( surface, ("W3DRadar: Can't get surface for terrain texture\n") );+ // one lock for the whole texture - see the note below about what EA measured here+ Int surfacePitch;+ void *surfaceBits = surface->Lock( &surfacePitch );+ const UnsignedInt bytesPerPixel = surface->Get_Bytes_Per_Pixel();+// build the terrainRGBColor sampleColor;RGBColor color;@@ -1240,16 +1252,21 @@ void W3DRadar::buildTerrainTexture( TerrainLogic *terrain )// the sampling and interpolation algorithm for generating pretty looking terrain// and water for the radar is just, well, expensive.//- surface->DrawPixel( x, y, GameMakeColor( color.red * 255,- color.green * 255,- color.blue * 255,- 255 ) );+ // That was measured against a 2003 driver writing straight to an AGP surface. Here+ // every LockRect crosses d3d8to9 into the Direct3D 9 runtime, so the lock is hoisted+ // anyway - the sampling above is still the expensive half, and is untouched.+ surface->Draw_Pixel( x, y, GameMakeColor( color.red * 255,+ color.green * 255,+ color.blue * 255,+ 255 ),+ bytesPerPixel, surfaceBits, surfacePitch );} // end for x} // end for y// all done with the surface+ surface->Unlock();REF_PTR_RELEASE(surface);} // end buildTerrainTexturediff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cppindex 81b61ae49..25db8c05e 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp@@ -857,6 +857,49 @@ void SurfaceClass::DrawPixel(const unsigned int x,const unsigned int y, unsigned* 4/9/2001 hy : Created. ** 4/9/2001 hy : Created. **=============================================================================================*/+unsigned int SurfaceClass::Get_Bytes_Per_Pixel(void)+{+ SurfaceDescription sd;+ Get_Description(sd);++ return PixelSize(sd);+}++/*+** The loop bodies of DrawPixel and DrawHLine, against a surface the caller has already locked.+** bits and pitch are what Lock() handed back, so the origin is the surface origin and the row+** stride is the whole surface's - unlike DrawPixel, which locks the single pixel's rectangle and+** gets a pointer to that. The masks match DrawPixel's exactly: the assumption throughout is that+** whenever a pixel has alpha it is in the MSB.+*/+void SurfaceClass::Draw_Pixel(const unsigned int x, const unsigned int y, const unsigned int color,+ const unsigned int bytes_per_pixel, void *bits, const int pitch)+{+ unsigned char *row = (unsigned char *)bits + y * pitch;++ switch (bytes_per_pixel)+ {+ case 1:+ *(unsigned char *)(row + x) = (unsigned char)(color & 0xFF);+ break;+ case 2:+ *(unsigned short *)(row + x * 2) = (unsigned short)(color & 0xFFFF);+ break;+ case 4:+ *(unsigned int *)(row + x * 4) = color;+ break;+ }+}++void SurfaceClass::Draw_H_Line(const unsigned int y, const unsigned int x1, const unsigned int x2,+ const unsigned int color, const unsigned int bytes_per_pixel, void *bits, const int pitch)+{+ for (unsigned int x = x1; x <= x2; x++)+ {+ Draw_Pixel(x, y, color, bytes_per_pixel, bits, pitch);+ }+}+void SurfaceClass::DrawHLine(const unsigned int y,const unsigned int x1, const unsigned int x2, unsigned int color){SurfaceDescription sd;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.hindex 5eea506ee..0372ed5d5 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.h@@ -131,6 +131,17 @@ class SurfaceClass : public W3DMPO, public RefCountClassvoid DrawPixel(const unsigned int x,const unsigned int y, unsigned int color);+ // DrawPixel and DrawHLine each do a Get_Description, a LockRect of the one rectangle they+ // touch and an UnlockRect of their own, every call. In a loop over a whole texture that is+ // one driver round trip per pixel. Lock() the surface once, ask Get_Bytes_Per_Pixel() once,+ // and call these instead - they write the same bytes, with the same truncation.+ static void Draw_Pixel(const unsigned int x, const unsigned int y, const unsigned int color,+ const unsigned int bytes_per_pixel, void *bits, const int pitch);+ static void Draw_H_Line(const unsigned int y, const unsigned int x1, const unsigned int x2,+ const unsigned int color, const unsigned int bytes_per_pixel, void *bits, const int pitch);++ unsigned int Get_Bytes_Per_Pixel(void);+// get pixel function .. to be used infrequentlyvoid Get_Pixel(Vector3 &rgb, int x,int y);diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cppindex c3a142f04..b7bea5a02 100644--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp@@ -1699,3 +1699,68 @@ TEST(fontchars_returns_its_glyph_buffers_to_the_pool)CHECK_EQ(theW3DPoolFrees - frees, theW3DPoolAllocs - allocs);CHECK_EQ(theW3DPoolBadFrees, 0);}++// ---------------------------------------------------------------------------------------------+// SurfaceClass::DrawPixel takes a Get_Description, a one-pixel LockRect and an UnlockRect for+// every call. Draw_Pixel writes the same byte, with the same truncation, into a buffer the+// caller locked once. These pin the address arithmetic and the masks against DrawPixel's.+// ---------------------------------------------------------------------------------------------+#include "surfaceclass.h"++TEST(surface_draw_pixel_lands_where_the_pitch_says)+{+ const int pitch = 40; /* deliberately wider than 8 pixels * 4 bytes */+ unsigned char buf[ 40 * 4 ];+ memset( buf, 0, sizeof( buf ) );++ SurfaceClass::Draw_Pixel( 3, 2, 0xAABBCCDD, 4, buf, pitch );+ CHECK_EQ( *(unsigned int *)( buf + 2 * pitch + 3 * 4 ), 0xAABBCCDD );++ /* nothing outside that one pixel was touched */+ unsigned int written = 0;+ for( unsigned int i = 0; i < sizeof( buf ); ++i )+ if( buf[ i ] != 0 ) ++written;+ CHECK_EQ( written, 4 );+}++TEST(surface_draw_pixel_truncates_the_colour_the_way_drawpixel_did)+{+ unsigned char buf[ 64 ];++ /* one byte per pixel: the low byte of the colour, and the row stride is the pitch */+ memset( buf, 0, sizeof( buf ) );+ SurfaceClass::Draw_Pixel( 5, 1, 0x11223344, 1, buf, 16 );+ CHECK_EQ( buf[ 16 + 5 ], 0x44 );++ /* two bytes per pixel: the low half word */+ memset( buf, 0, sizeof( buf ) );+ SurfaceClass::Draw_Pixel( 5, 1, 0x11223344, 2, buf, 16 );+ CHECK_EQ( *(unsigned short *)( buf + 16 + 5 * 2 ), 0x3344 );++ /* an unsupported pixel size writes nothing at all, exactly as the switch used to */+ memset( buf, 0, sizeof( buf ) );+ SurfaceClass::Draw_Pixel( 5, 1, 0x11223344, 3, buf, 16 );+ unsigned int written = 0;+ for( unsigned int i = 0; i < sizeof( buf ); ++i )+ if( buf[ i ] != 0 ) ++written;+ CHECK_EQ( written, 0 );+}++TEST(surface_draw_h_line_fills_the_run_inclusive_at_both_ends)+{+ const int pitch = 32;+ unsigned char buf[ 32 * 3 ];+ memset( buf, 0, sizeof( buf ) );++ SurfaceClass::Draw_H_Line( 1, 2, 5, 0x000000FF, 4, buf, pitch );++ unsigned int *row = (unsigned int *)( buf + pitch );+ CHECK_EQ( row[ 1 ], 0 ); /* one before the run */+ for( unsigned int x = 2; x <= 5; ++x )+ CHECK_EQ( row[ x ], 0x000000FF );+ CHECK_EQ( row[ 6 ], 0 ); /* one past it */++ /* and no other row moved */+ CHECK_EQ( *(unsigned int *)buf, 0 );+ CHECK_EQ( *(unsigned int *)( buf + 2 * pitch ), 0 );+}