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(w3ddevice): read film frames a pass late, join panes by runs

Olcay Seygan3c87ad71c67f
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
index 14df1bbe5..f95ef0aab 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
@@ -267,6 +267,18 @@ Int ObserverCamera_paneLayout( Int count, Real *rays, Int width, Int height );
Real ObserverCamera_angleForShare( Real share, Int width, Int height );
/// the pane pixel x, y falls in, the rays meeting at origin x, y (pixels, y down)
Int ObserverCamera_paneOf( Real x, Real y, Real originX, Real originY, const Real *rays, Int count );
+/// a stretch of row y from column x0 up to, not including, x1 that lies in one pane
+struct ObserverPaneRun
+{
+ Int y;
+ Int x0;
+ Int x1;
+ Int pane;
+};
+/// a width by height picture as runs, row by row and left to right, every pixel in the pane
+/// ObserverCamera_paneOf gives its centre
+void ObserverCamera_paneRuns( Int width, Int height, Real originX, Real originY, const Real *rays, Int count,
+ std::vector< ObserverPaneRun > &runs );
/// how far the rays' meeting point has to move, away from pane 0, before pane 0 is the whole of a
/// width by height picture: the panes come in from there and go back out to it
Real ObserverCamera_paneExit( const Real *rays, Int count, Int width, Int height );
@@ -409,7 +421,7 @@ public:
/// rectangles from pane 0 whichever pane they lie over. Cleared at the start of each of its draws
void clearBroadcast( void ) { m_broadcast.clear(); }
void addBroadcast( const IRegion2D &region ) { m_broadcast.push_back( region ); }
- Bool isBroadcast( Int x, Int y ) const;
+ const std::vector< IRegion2D > &getBroadcast( void ) const { return m_broadcast; }
/// the rows the score bar takes at the top of the picture, which every pane's circle stays below
void setBroadcastTop( Real rows ) { m_broadcastTop = rows; }
/// a pane's circle round its subject where that is drawn this frame, the panes part way in or out
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
index fe4110250..c29be2b51 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
@@ -622,6 +622,84 @@ Int ObserverCamera_paneOf( Real x, Real y, Real originX, Real originY, const Rea
return pane;
}
+//-------------------------------------------------------------------------------------------------
+/* paneOf's float angle strays less than two hundredths of a degree from the true one, even on an 8K
+ picture, for a pixel at least PANE_RUN_CLEARANCE pixels from the meeting point. A stretch of a row whose true
+ angle stays more than PANE_RUN_MARGIN degrees off every ray and off the turn's ends all the way along
+ can therefore take one pixel's pane for all of it. A stretch that cannot is halved, and one no longer
+ than PANE_RUN_SHORTEST is asked pixel by pixel. */
+static const double PANE_RUN_MARGIN = 0.05;
+static const double PANE_RUN_CLEARANCE = 2.0;
+static const Int PANE_RUN_SHORTEST = 4;
+
+static double paneRunAngle( double x, double towardsTop, double originX )
+{
+ const double angle = atan2( towardsTop, x - originX ) * 180.0 / 3.14159265358979323846;
+ return angle < 0.0 ? angle + 360.0 : angle;
+}
+
+static Bool paneRunIsOnePane( Int first, Int last, double towardsTop, double originX, const Real *rays, Int count )
+{
+ const double left = first + 0.5;
+ const double right = last + 0.5;
+ if( fabs( towardsTop ) < PANE_RUN_CLEARANCE && right > originX - PANE_RUN_CLEARANCE
+ && left < originX + PANE_RUN_CLEARANCE )
+ return FALSE;
+ // along a row the angle only ever turns one way, so its ends bound it
+ const double leftAngle = paneRunAngle( left, towardsTop, originX );
+ const double rightAngle = paneRunAngle( right, towardsTop, originX );
+ const double lowest = min( leftAngle, rightAngle ) - PANE_RUN_MARGIN;
+ const double highest = max( leftAngle, rightAngle ) + PANE_RUN_MARGIN;
+ if( lowest <= 0.0 || highest >= 360.0 )
+ return FALSE;
+ for( Int ray = 0; ray < count; ray++ )
+ {
+ if( rays[ ray ] >= lowest && rays[ ray ] <= highest )
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static void addPaneRun( std::vector< ObserverPaneRun > &runs, Int y, Int x0, Int x1, Int pane )
+{
+ if( !runs.empty() && runs.back().y == y && runs.back().x1 == x0 && runs.back().pane == pane )
+ {
+ runs.back().x1 = x1;
+ return;
+ }
+ const ObserverPaneRun run = { y, x0, x1, pane };
+ runs.push_back( run );
+}
+
+static void addPaneRuns( std::vector< ObserverPaneRun > &runs, Int y, Int first, Int last, Real originX, Real originY,
+ const Real *rays, Int count )
+{
+ const Real centreY = (Real)y + 0.5f;
+ const double towardsTop = (double)originY - centreY;
+ if( paneRunIsOnePane( first, last, towardsTop, originX, rays, count ) )
+ {
+ addPaneRun( runs, y, first, last + 1, ObserverCamera_paneOf( (Real)first + 0.5f, centreY, originX, originY, rays, count ) );
+ return;
+ }
+ if( last - first < PANE_RUN_SHORTEST )
+ {
+ for( Int x = first; x <= last; x++ )
+ addPaneRun( runs, y, x, x + 1, ObserverCamera_paneOf( (Real)x + 0.5f, centreY, originX, originY, rays, count ) );
+ return;
+ }
+ const Int middle = ( first + last ) / 2;
+ addPaneRuns( runs, y, first, middle, originX, originY, rays, count );
+ addPaneRuns( runs, y, middle + 1, last, originX, originY, rays, count );
+}
+
+void ObserverCamera_paneRuns( Int width, Int height, Real originX, Real originY, const Real *rays, Int count,
+ std::vector< ObserverPaneRun > &runs )
+{
+ runs.clear();
+ for( Int y = 0; y < height; y++ )
+ addPaneRuns( runs, y, 0, width - 1, originX, originY, rays, count );
+}
+
//-------------------------------------------------------------------------------------------------
Coord2D ObserverCamera_paneExitDirection( const Real *rays )
{
@@ -2549,18 +2627,6 @@ Coord2D ObserverCamera::getFramedRadarMiddle( Real radarDiagonal ) const
return middle;
}
-//-------------------------------------------------------------------------------------------------
-Bool ObserverCamera::isBroadcast( Int x, Int y ) const
-{
- for( size_t index = 0; index < m_broadcast.size(); index++ )
- {
- const IRegion2D &region = m_broadcast[ index ];
- if( x >= region.lo.x && x < region.hi.x && y >= region.lo.y && y < region.hi.y )
- return TRUE;
- }
- return FALSE;
-}
-
//-------------------------------------------------------------------------------------------------
/** Where each pane's subject is drawn, the way update and stepPaneCameras put it: pane 0's moves from
* the middle of the screen to its circle as the panes come in, every other pane's circle slides
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 531f66513..50c945baa 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -37,6 +37,7 @@ static void drawFramerateBar(void);
// SYSTEM INCLUDES ////////////////////////////////////////////////////////////
#include <stdlib.h>
+#include <algorithm>
#include "Lib/Clock.h"
#if defined(_WIN32)
#include <windows.h>
@@ -3824,20 +3825,22 @@ static void CreateBMPFile(char *pszFile, char *image, Int width, Int height)
fclose(fp);
}
-// A system-memory copy of the back buffer (32-bit, not multisampled), NULL when that is
-// not possible. Taken at the end of draw(), before Present: the front-buffer path is a
-// desktop capture, and a window presented through a DXGI flip swap chain is one that
-// desktop captures do not see - the old code saved black.
-static IDirect3DSurface9 *captureBackBuffer(void)
+// The back buffer (32-bit, not multisampled) copied into *copy, a system-memory surface kept from
+// call to call and made again when the size or the format changes. The copy may still be on its
+// way when this returns: locking *copy is what waits for it. FALSE when the back buffer could not
+// be read. Taken at the end of draw(), before Present: the front-buffer path is a desktop
+// capture, and a window presented through a DXGI flip swap chain is one that desktop captures do
+// not see - the old code saved black.
+static Bool copyBackBuffer(IDirect3DSurface9 **copy)
{
IDirect3DDevice9 *dev = DX8Wrapper::_Get_D3D_Device();
IDirect3DSurface9 *bb = NULL;
if (dev == NULL || Render_Failed(dev->GetBackBuffer(PRIMARY_SWAP_CHAIN, 0, D3DBACKBUFFER_TYPE_MONO, &bb)) || bb == NULL)
- return NULL;
+ return FALSE;
D3DSURFACE_DESC desc;
bb->GetDesc(&desc);
- IDirect3DSurface9 *copy = NULL;
+ Bool copied = FALSE;
if (desc.Format == D3DFMT_X8R8G8B8 || desc.Format == D3DFMT_A8R8G8B8)
{
// GetRenderTargetData refuses a multisampled source, and with -msaa or the options menu's
@@ -3856,22 +3859,41 @@ static IDirect3DSurface9 *captureBackBuffer(void)
source = resolved;
}
- if (Render_Succeeded(source->GetDesc(&desc)) && desc.MultiSampleType == D3DMULTISAMPLE_NONE
- && Render_Succeeded(dev->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
- D3DPOOL_SYSTEMMEM, &copy, NULL)) && copy != NULL
- && Render_Failed(dev->GetRenderTargetData(source, copy)))
+ if (Render_Succeeded(source->GetDesc(&desc)) && desc.MultiSampleType == D3DMULTISAMPLE_NONE)
{
- copy->Release();
- copy = NULL;
+ if (*copy != NULL)
+ {
+ D3DSURFACE_DESC held;
+ (*copy)->GetDesc(&held);
+ if (held.Width != desc.Width || held.Height != desc.Height || held.Format != desc.Format)
+ {
+ (*copy)->Release();
+ *copy = NULL;
+ }
+ }
+ if (*copy == NULL && Render_Failed(dev->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
+ D3DPOOL_SYSTEMMEM, copy, NULL)))
+ *copy = NULL;
+ copied = *copy != NULL && Render_Succeeded(dev->GetRenderTargetData(source, *copy));
}
if (resolved != NULL)
resolved->Release();
}
bb->Release();
- if (copy == NULL)
- DEBUG_LOG(("takeScreenShot - the back buffer could not be read, falling back to a desktop capture\n"));
- return copy;
+ return copied;
+}
+
+// A system-memory copy of the back buffer, NULL when that is not possible.
+static IDirect3DSurface9 *captureBackBuffer(void)
+{
+ IDirect3DSurface9 *copy = NULL;
+ if (copyBackBuffer(&copy))
+ return copy;
+ if (copy != NULL)
+ copy->Release();
+ DEBUG_LOG(("takeScreenShot - the back buffer could not be read, falling back to a desktop capture\n"));
+ return NULL;
}
///Save Screen Capture to a file - deferred to the end of the next rendered frame
@@ -4063,14 +4085,44 @@ static UnsignedInt s_videoLastFrame = 0;
static Int s_videoFramesWritten = 0;
static Int s_videoFramesMissed = 0;
static char s_videoDirectory[_MAX_PATH];
-/* -directorrecord's panes past the first, each drawn before pane 0 and kept here until the frame everybody
- sees is read and the pane's wedge is taken from it */
-static char *s_videoHeld[OBSERVER_MOST_PANES] = { NULL };
-static Int s_videoHeldWidth[OBSERVER_MOST_PANES] = { 0 };
-static Int s_videoHeldHeight[OBSERVER_MOST_PANES] = { 0 };
-static UnsignedInt s_videoHeldFrame[OBSERVER_MOST_PANES] = { 0 };
-static UnsignedByte *s_videoPaneMap = NULL;
-static Int s_videoPaneMapSize = 0;
+/* Every picture of the recording is copied off the GPU into a slot of the record its frame is drawn
+ into, and read back a frame later while the next frame is drawn into the other record: reading the
+ copy as soon as it is asked for waits for the GPU to finish the frame. -directorrecord's panes past
+ the first are drawn before pane 0 on the same logic frame and have slots of their own; the record
+ keeps where the panes lay when pane 0 was drawn, which is what its picture shows. */
+enum { VIDEO_RECORDS = 2 };
+enum { VIDEO_PIXEL_BYTES = 4 }; // blue, green, red and a byte nothing reads
+static_assert(VIDEO_RECORDS * OBSERVER_MOST_PANES <= DX11_FRAME_COPY_SLOTS,
+ "every pane of both records has a Direct3D 11 slot");
+struct VideoRecord
+{
+ Bool pending; ///< pane 0's copy is made and the frame is still to be written
+ UnsignedInt frame;
+ Bool paneCopied[OBSERVER_MOST_PANES];
+ UnsignedInt paneFrame[OBSERVER_MOST_PANES];
+ Int paneCount;
+ Real paneRays[OBSERVER_MOST_PANES];
+ Coord2D paneOrigin;
+ IRegion2D radarFrame;
+ std::vector<IRegion2D> broadcast;
+};
+static VideoRecord s_videoRecords[VIDEO_RECORDS];
+static Int s_videoDrawingRecord = 0;
+static IDirect3DSurface9 *s_videoSystemCopies[VIDEO_RECORDS * OBSERVER_MOST_PANES] = { NULL };
+static std::vector<UnsignedByte> s_videoJoined;
+/* the panes' runs are worked out again only when the picture or where its panes lie changes, which is
+ only while they slide in or out */
+struct VideoPaneShape
+{
+ Int width;
+ Int height;
+ Int count;
+ Coord2D origin;
+ Real rays[OBSERVER_MOST_PANES];
+};
+static VideoPaneShape s_videoPaneRunsShape;
+static std::vector<ObserverPaneRun> s_videoPaneRuns;
+static std::vector<IRegion2D> s_videoRowKept;
#if defined(_WIN32)
/* -directorrecord writes no frames to disk: they go down a pipe into ffmpeg as they are drawn, which a
whole match of 1080p bitmaps would need tens of gigabytes for. With no ffmpeg it records nothing
@@ -4130,22 +4182,36 @@ static void deleteVideoFrames(void)
}
}
+static void writeVideoRecord(Int recordIndex);
+
+static void releaseFrameCopies(void)
+{
+ Direct3D11_Release_Frame_Copies();
+ for (Int slot = 0; slot < VIDEO_RECORDS * OBSERVER_MOST_PANES; ++slot)
+ {
+ if (s_videoSystemCopies[slot] != NULL)
+ s_videoSystemCopies[slot]->Release();
+ s_videoSystemCopies[slot] = NULL;
+ }
+}
+
static void finishVideo(void)
{
if (!s_videoStarted || s_videoFinished)
+ {
+ // a recording that never started, or stopped for want of ffmpeg, can still hold copies
+ releaseFrameCopies();
return;
+ }
s_videoFinished = TRUE;
+ // the last frame drawn is still waiting on its copy
+ writeVideoRecord(1 - s_videoDrawingRecord);
DEBUG_LOG(("VIDEO: %d frames written to %s, %d logic frames went by without a picture\n",
s_videoFramesWritten, s_videoDirectory, s_videoFramesMissed));
- for (Int pane = 0; pane < OBSERVER_MOST_PANES; ++pane)
- {
- delete [] s_videoHeld[pane];
- s_videoHeld[pane] = NULL;
- }
- delete [] s_videoPaneMap;
- s_videoPaneMap = NULL;
- s_videoPaneMapSize = 0;
+ releaseFrameCopies();
+ std::vector<UnsignedByte>().swap(s_videoJoined);
+ std::vector<ObserverPaneRun>().swap(s_videoPaneRuns);
#if defined(_WIN32)
// the end of the pipe is the end of the stream: ffmpeg finishes the movie and exits
@@ -4267,7 +4333,7 @@ static Bool openVideoPipe(Int width, Int height)
buildVideoMoviePath(moviePath, ARRAY_SIZE(moviePath));
char commandLine[4 * _MAX_PATH];
snprintf(commandLine, ARRAY_SIZE(commandLine),
- "\"%s\" -y -loglevel error -f rawvideo -pix_fmt bgr24 -s %dx%d -framerate %d -i - "
+ "\"%s\" -y -loglevel error -f rawvideo -pix_fmt bgr0 -s %dx%d -framerate %d -i - "
"-vf pad=ceil(iw/2)*2:ceil(ih/2)*2 -c:v libx264 -pix_fmt yuv420p -crf 18 \"%s\"",
encoderPath, width, height, LOGICFRAMES_PER_SECOND, moviePath);
@@ -4298,53 +4364,151 @@ static Bool openVideoPipe(Int width, Int height)
}
#endif
+static Int videoCopySlot(Int recordIndex, Int pane)
+{
+ return recordIndex * OBSERVER_MOST_PANES + pane;
+}
+
+/** Start copying the frame being drawn off the GPU into slot, without waiting for it. FALSE when there
+ * was no picture to copy, which is what a device that has gone away gives. */
+static Bool queueFrameCopy(Int slot)
+{
+ if (Direct3D11_Present_Is_Enabled())
+ return Direct3D11_Queue_Frame_Copy((unsigned)slot);
+ return copyBackBuffer(&s_videoSystemCopies[slot]);
+}
+
+/** slot's copy, VIDEO_PIXEL_BYTES a pixel, top row first, until unmapFrameCopy; NULL when it cannot be read. */
+static const UnsignedByte *mapFrameCopy(Int slot, Int *width, Int *height, Int *pitch)
+{
+ if (Direct3D11_Present_Is_Enabled())
+ {
+ unsigned mappedWidth = 0;
+ unsigned mappedHeight = 0;
+ unsigned mappedPitch = 0;
+ const unsigned char *pixels = Direct3D11_Map_Frame_Copy((unsigned)slot, mappedWidth, mappedHeight, mappedPitch);
+ *width = (Int)mappedWidth;
+ *height = (Int)mappedHeight;
+ *pitch = (Int)mappedPitch;
+ return pixels;
+ }
+ IDirect3DSurface9 *copy = s_videoSystemCopies[slot];
+ D3DLOCKED_RECT locked;
+ if (copy == NULL || Render_Failed(copy->LockRect(&locked, NULL, D3DLOCK_READONLY)))
+ return NULL;
+ D3DSURFACE_DESC desc;
+ copy->GetDesc(&desc);
+ *width = (Int)desc.Width;
+ *height = (Int)desc.Height;
+ *pitch = (Int)locked.Pitch;
+ return (const UnsignedByte *)locked.pBits;
+}
+
+static void unmapFrameCopy(Int slot)
+{
+ if (Direct3D11_Present_Is_Enabled())
+ Direct3D11_Unmap_Frame_Copy((unsigned)slot);
+ else
+ s_videoSystemCopies[slot]->UnlockRect();
+}
+
+static bool keptLeftOf(const IRegion2D &first, const IRegion2D &second)
+{
+ return first.lo.x < second.lo.x;
+}
+
+/** The rectangles that stay pane 0's on row y, the radar's frame and the broadcast's, left to right. */
+static void collectRowKept(const VideoRecord &record, Int y)
+{
+ s_videoRowKept.clear();
+ for (size_t index = 0; index <= record.broadcast.size(); ++index)
+ {
+ const IRegion2D &region = index < record.broadcast.size() ? record.broadcast[index] : record.radarFrame;
+ if (y >= region.lo.y && y < region.hi.y && region.lo.x < region.hi.x)
+ s_videoRowKept.push_back(region);
+ }
+ std::sort(s_videoRowKept.begin(), s_videoRowKept.end(), keptLeftOf);
+}
+
+static void copyVideoSpan(UnsignedByte *joined, Int width, const UnsignedByte *pane, Int panePitch, Int y, Int x0, Int x1)
+{
+ if (x1 > x0)
+ memcpy(joined + ((size_t)y * width + x0) * VIDEO_PIXEL_BYTES, pane + (size_t)y * panePitch + (size_t)x0 * VIDEO_PIXEL_BYTES,
+ (size_t)(x1 - x0) * VIDEO_PIXEL_BYTES);
+}
+
/** -directorrecord's panes joined into pane 0's picture: each pixel is taken from the pane whose wedge
- * it lies in. A pane's camera already slid its picture with the rays' meeting point, so the copy is
- * pixel for pixel; moving the pixels instead read past a picture's edge and smeared it. Every pane
- * draws the lines and the radar's frame, so a seam needs nothing of its own. Pane 0 keeps the framed
- * radar and the broadcast's score bar and labels, which only its draw has. A pane with no picture
- * of this frame and size stays pane 0's. */
-static void joinVideoPanes(char *rows, Int width, Int height, UnsignedInt frame)
+ * it lies in, a row's stretch of one pane at a time. A pane's camera already slid its picture with the
+ * rays' meeting point, so the copy is pixel for pixel; moving the pixels instead read past a picture's
+ * edge and smeared it. Every pane draws the lines and the radar's frame, so a seam needs nothing of its
+ * own. Pane 0 keeps the framed radar and the broadcast's score bar and labels, which only its draw has.
+ * A pane with no picture of this frame and size stays pane 0's. */
+static void joinVideoPanes(const VideoRecord &record, Int recordIndex, UnsignedByte *joined, Int width, Int height)
{
- const Int count = TheObserverCamera.getDrawnPaneCount();
- const Real *rays = TheObserverCamera.getPaneRays();
- const Coord2D origin = TheObserverCamera.getPaneOrigin();
- const IRegion2D &radar = TheObserverCamera.getRadarFrame();
+ VideoPaneShape shape = {};
+ shape.width = width;
+ shape.height = height;
+ shape.count = record.paneCount;
+ shape.origin = record.paneOrigin;
+ memcpy(shape.rays, record.paneRays, sizeof(Real) * record.paneCount);
+ if (memcmp(&shape, &s_videoPaneRunsShape, sizeof(shape)) != 0)
+ {
+ s_videoPaneRunsShape = shape;
+ ObserverCamera_paneRuns(width, height, shape.origin.x, shape.origin.y, shape.rays, shape.count, s_videoPaneRuns);
+ }
- if (s_videoPaneMapSize != width * height)
+ const UnsignedByte *panes[OBSERVER_MOST_PANES] = { NULL };
+ Int panePitches[OBSERVER_MOST_PANES] = { 0 };
+ for (Int pane = 1; pane < record.paneCount; ++pane)
{
- delete [] s_videoPaneMap;
- s_videoPaneMapSize = width * height;
- s_videoPaneMap = NEW UnsignedByte[s_videoPaneMapSize];
+ if (!record.paneCopied[pane] || record.paneFrame[pane] != record.frame)
+ continue;
+ Int paneWidth = 0;
+ Int paneHeight = 0;
+ panes[pane] = mapFrameCopy(videoCopySlot(recordIndex, pane), &paneWidth, &paneHeight, &panePitches[pane]);
+ if (panes[pane] != NULL && (paneWidth != width || paneHeight != height))
+ {
+ unmapFrameCopy(videoCopySlot(recordIndex, pane));
+ panes[pane] = NULL;
+ }
}
- for (Int y = 0; y < height; ++y)
- for (Int x = 0; x < width; ++x)
- s_videoPaneMap[y * width + x] = (UnsignedByte)ObserverCamera_paneOf((Real)x + 0.5f, (Real)y + 0.5f,
- origin.x, origin.y, rays, count);
- for (Int y = 0; y < height; ++y)
+ Int keptRow = -1;
+ for (size_t index = 0; index < s_videoPaneRuns.size(); ++index)
{
- for (Int x = 0; x < width; ++x)
+ const ObserverPaneRun &run = s_videoPaneRuns[index];
+ const UnsignedByte *pane = panes[run.pane];
+ if (pane == NULL)
+ continue;
+ if (run.y != keptRow)
{
- const Int pane = s_videoPaneMap[y * width + x];
- if (pane == 0)
- continue;
- if (s_videoHeld[pane] == NULL || s_videoHeldFrame[pane] != frame
- || s_videoHeldWidth[pane] != width || s_videoHeldHeight[pane] != height)
- continue;
- if (x >= radar.lo.x && x < radar.hi.x && y >= radar.lo.y && y < radar.hi.y)
- continue;
- if (TheObserverCamera.isBroadcast(x, y))
+ keptRow = run.y;
+ collectRowKept(record, run.y);
+ }
+ Int start = run.x0;
+ for (size_t kept = 0; kept < s_videoRowKept.size() && start < run.x1; ++kept)
+ {
+ const IRegion2D &region = s_videoRowKept[kept];
+ if (region.hi.x <= start)
continue;
- const Int at = (y * width + x) * 3;
- memcpy(rows + at, s_videoHeld[pane] + at, 3);
+ if (region.lo.x >= run.x1)
+ break;
+ copyVideoSpan(joined, width, pane, panePitches[run.pane], run.y, start, region.lo.x);
+ start = region.hi.x;
}
+ copyVideoSpan(joined, width, pane, panePitches[run.pane], run.y, start, run.x1);
+ }
+
+ for (Int pane = 1; pane < record.paneCount; ++pane)
+ {
+ if (panes[pane] != NULL)
+ unmapFrameCopy(videoCopySlot(recordIndex, pane));
}
}
-/** One picture of the recording, into ffmpeg's pipe under -directorrecord, as the next numbered .bmp
- * otherwise. */
-static Bool writeVideoFrame(char *rows, Int width, Int height)
+/** One picture of the recording, VIDEO_PIXEL_BYTES a pixel with no gap between rows, into ffmpeg's pipe
+ * under -directorrecord, as the next numbered .bmp otherwise. */
+static Bool writeVideoFrame(const UnsignedByte *pixels, Int width, Int height, UnsignedInt frame)
{
#if defined(_WIN32)
if (TheGlobalData->m_directorRecord && !s_videoPipeTried)
@@ -4357,12 +4521,12 @@ static Bool writeVideoFrame(char *rows, Int width, Int height)
// ffmpeg was told one size; a picture of another would shear every frame after it
if (width != s_videoPipeWidth || height != s_videoPipeHeight)
return FALSE;
- const DWORD bytes = (DWORD)(3 * width * height);
+ const DWORD bytes = (DWORD)(VIDEO_PIXEL_BYTES * width * height);
DWORD written = 0;
- if (WriteFile(s_videoPipe, rows, bytes, &written, NULL) && written == bytes)
+ if (WriteFile(s_videoPipe, pixels, bytes, &written, NULL) && written == bytes)
return TRUE;
DEBUG_LOG(("VIDEO: %s stopped taking frames (error %u) at logic frame %u\n", VIDEO_ENCODER,
- (unsigned)GetLastError(), TheGameLogic->getFrame()));
+ (unsigned)GetLastError(), frame));
return FALSE;
}
#endif
@@ -4381,10 +4545,53 @@ static Bool writeVideoFrame(char *rows, Int width, Int height)
}
char pathname[_MAX_PATH];
buildVideoFramePath(pathname, ARRAY_SIZE(pathname), s_videoFramesWritten);
+ char *rows = NEW char[3 * width * height];
+ for (Int pixel = 0; pixel < width * height; ++pixel)
+ memcpy(rows + pixel * 3, pixels + pixel * VIDEO_PIXEL_BYTES, 3);
CreateBMPFile(pathname, rows, width, height);
+ delete [] rows;
return TRUE;
}
+/** The frame held in a record, pane 0's picture with the other panes' wedges taken into it, written out
+ * once. */
+static void writeVideoRecord(Int recordIndex)
+{
+ VideoRecord &record = s_videoRecords[recordIndex];
+ if (!record.pending)
+ return;
+ record.pending = FALSE;
+
+ const Int slot = videoCopySlot(recordIndex, 0);
+ Int width = 0;
+ Int height = 0;
+ Int pitch = 0;
+ const UnsignedByte *picture = mapFrameCopy(slot, &width, &height, &pitch);
+ if (picture == NULL)
+ {
+ ++s_videoFramesMissed;
+ return;
+ }
+
+ const Int rowBytes = width * VIDEO_PIXEL_BYTES;
+ const UnsignedByte *pixels = picture;
+ if (record.paneCount >= 2 || pitch != rowBytes)
+ {
+ s_videoJoined.resize((size_t)rowBytes * height);
+ for (Int y = 0; y < height; ++y)
+ memcpy(&s_videoJoined[(size_t)y * rowBytes], picture + (size_t)y * pitch, rowBytes);
+ if (record.paneCount >= 2)
+ joinVideoPanes(record, recordIndex, &s_videoJoined[0], width, height);
+ pixels = &s_videoJoined[0];
+ }
+
+ if (writeVideoFrame(pixels, width, height, record.frame))
+ ++s_videoFramesWritten;
+ else
+ ++s_videoFramesMissed;
+ unmapFrameCopy(slot);
+}
+
static void captureVideoFrame(void)
{
if (TheGlobalData->m_videoEndFrame <= 0 || s_videoFinished || TheGameLogic == NULL
@@ -4405,9 +4612,9 @@ static void captureVideoFrame(void)
if (TheObserverCamera.isDrawingSecond())
{
const Int pane = TheObserverCamera.getDrawingPane();
- delete [] s_videoHeld[pane];
- s_videoHeld[pane] = captureFrameRows(&s_videoHeldWidth[pane], &s_videoHeldHeight[pane]);
- s_videoHeldFrame[pane] = frame;
+ VideoRecord &drawing = s_videoRecords[s_videoDrawingRecord];
+ drawing.paneCopied[pane] = queueFrameCopy(videoCopySlot(s_videoDrawingRecord, pane));
+ drawing.paneFrame[pane] = frame;
return;
}
@@ -4444,23 +4651,24 @@ static void captureVideoFrame(void)
}
s_videoLastFrame = frame;
- Int width = 0;
- Int height = 0;
- char *rows = captureFrameRows(&width, &height);
- if (rows == NULL)
+ const Int drawingIndex = s_videoDrawingRecord;
+ VideoRecord &drawing = s_videoRecords[drawingIndex];
+ if (!queueFrameCopy(videoCopySlot(drawingIndex, 0)))
{
++s_videoFramesMissed;
return;
}
-
- if (TheObserverCamera.getDrawnPaneCount() >= 2)
- joinVideoPanes(rows, width, height, frame);
-
- if (writeVideoFrame(rows, width, height))
- ++s_videoFramesWritten;
- else
- ++s_videoFramesMissed;
- delete [] rows;
+ drawing.pending = TRUE;
+ drawing.frame = frame;
+ drawing.paneCount = TheObserverCamera.getDrawnPaneCount();
+ memcpy(drawing.paneRays, TheObserverCamera.getPaneRays(), sizeof(drawing.paneRays));
+ drawing.paneOrigin = TheObserverCamera.getPaneOrigin();
+ drawing.radarFrame = TheObserverCamera.getRadarFrame();
+ drawing.broadcast = TheObserverCamera.getBroadcast();
+
+ // the frame before this one is long drawn, so reading its copies waits on nothing
+ s_videoDrawingRecord = 1 - drawingIndex;
+ writeVideoRecord(s_videoDrawingRecord);
}
/** Start/Stop campturing an AVI movie*/
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index 29e404aa2..b154971d2 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -123,6 +123,7 @@ void Direct3D11_Release()
Post.Shutdown();
Device.Set_Scene_View(NULL);
Backend.Shutdown();
+ Direct3D11_Release_Frame_Copies();
Device.Release();
Active = false;
}
@@ -528,6 +529,22 @@ void Direct3D11_Mirror_Clear(bool colour, bool depth, float red, float green, fl
}
}
+// A texture the CPU can read that the back buffer copies into whole.
+static D3D11_TEXTURE2D_DESC staging_description_of(ID3D11Resource * back_buffer)
+{
+ D3D11_TEXTURE2D_DESC description;
+ ((ID3D11Texture2D *)back_buffer)->GetDesc(&description);
+ description.Usage = D3D11_USAGE_STAGING;
+ description.BindFlags = 0;
+ description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
+ description.MiscFlags = 0;
+ // A multisampled back buffer cannot be copied into a single-sample staging texture, and this
+ // swap chain never asks for one, so the copy is the whole of it.
+ description.SampleDesc.Count = 1;
+ description.SampleDesc.Quality = 0;
+ return description;
+}
+
unsigned char * Direct3D11_Capture_Back_Buffer(unsigned & width, unsigned & height,
unsigned & pitch)
{
@@ -546,17 +563,7 @@ unsigned char * Direct3D11_Capture_Back_Buffer(unsigned & width, unsigned & heig
ID3D11Resource * back_buffer = NULL;
Device.Get_Back_Buffer_View()->GetResource(&back_buffer);
- D3D11_TEXTURE2D_DESC description;
- ((ID3D11Texture2D *)back_buffer)->GetDesc(&description);
- description.Usage = D3D11_USAGE_STAGING;
- description.BindFlags = 0;
- description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
- description.MiscFlags = 0;
- // A multisampled back buffer cannot be copied into a single-sample staging texture, and this
- // swap chain never asks for one, so the copy below is the whole of it.
- description.SampleDesc.Count = 1;
- description.SampleDesc.Quality = 0;
-
+ const D3D11_TEXTURE2D_DESC description = staging_description_of(back_buffer);
ID3D11Texture2D * staging = NULL;
if (FAILED(Device.Get_Device()->CreateTexture2D(&description, NULL, &staging))) {
back_buffer->Release();
@@ -589,6 +596,77 @@ void Direct3D11_Release_Capture(unsigned char * pixels)
delete [] pixels;
}
+static ID3D11Texture2D * FrameCopies[DX11_FRAME_COPY_SLOTS];
+
+bool Direct3D11_Queue_Frame_Copy(unsigned slot)
+{
+ if (!Active || Device.Get_Back_Buffer_View() == NULL) {
+ return false;
+ }
+
+ // what the player sees, the way a screenshot takes it
+ Direct3D11_Finish_Frame();
+
+ ID3D11Resource * back_buffer = NULL;
+ Device.Get_Back_Buffer_View()->GetResource(&back_buffer);
+ const D3D11_TEXTURE2D_DESC description = staging_description_of(back_buffer);
+
+ ID3D11Texture2D *& copy = FrameCopies[slot];
+ if (copy != NULL) {
+ D3D11_TEXTURE2D_DESC held;
+ copy->GetDesc(&held);
+ if (held.Width != description.Width || held.Height != description.Height
+ || held.Format != description.Format) {
+ copy->Release();
+ copy = NULL;
+ }
+ }
+ if (copy == NULL && FAILED(Device.Get_Device()->CreateTexture2D(&description, NULL, &copy))) {
+ copy = NULL;
+ back_buffer->Release();
+ return false;
+ }
+
+ Device.Get_Context()->CopyResource(copy, back_buffer);
+ back_buffer->Release();
+ return true;
+}
+
+const unsigned char * Direct3D11_Map_Frame_Copy(unsigned slot, unsigned & width, unsigned & height,
+ unsigned & pitch)
+{
+ width = 0;
+ height = 0;
+ pitch = 0;
+ ID3D11Texture2D * copy = FrameCopies[slot];
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ if (!Active || copy == NULL || FAILED(Device.Get_Context()->Map(copy, 0, D3D11_MAP_READ, 0, &mapped))) {
+ return NULL;
+ }
+
+ D3D11_TEXTURE2D_DESC description;
+ copy->GetDesc(&description);
+ width = description.Width;
+ height = description.Height;
+ pitch = mapped.RowPitch;
+ return (const unsigned char *)mapped.pData;
+}
+
+void Direct3D11_Unmap_Frame_Copy(unsigned slot)
+{
+ Device.Get_Context()->Unmap(FrameCopies[slot], 0);
+}
+
+void Direct3D11_Release_Frame_Copies()
+{
+ for (unsigned slot = 0; slot < DX11_FRAME_COPY_SLOTS; ++slot) {
+ if (FrameCopies[slot] != NULL) {
+ FrameCopies[slot]->Release();
+ FrameCopies[slot] = NULL;
+ }
+ }
+}
+
void Direct3D11_End_Scene(bool flip_frames)
{
if (Active && flip_frames && PresentRequested) {
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index 40311be71..7a3b307ff 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -256,6 +256,17 @@ unsigned char * Direct3D11_Capture_Back_Buffer(unsigned & width, unsigned & heig
unsigned & pitch);
void Direct3D11_Release_Capture(unsigned char * pixels);
+// The same back buffer for a recording, without waiting for it. Queue copies it into one of
+// DX11_FRAME_COPY_SLOTS staging textures kept from call to call; Map hands that copy back a frame
+// later, by when the GPU has long made it, and is null for a slot that holds nothing. A Map is
+// followed by an Unmap of the same slot before the slot is queued again.
+enum { DX11_FRAME_COPY_SLOTS = 16 };
+bool Direct3D11_Queue_Frame_Copy(unsigned slot);
+const unsigned char * Direct3D11_Map_Frame_Copy(unsigned slot, unsigned & width, unsigned & height,
+ unsigned & pitch);
+void Direct3D11_Unmap_Frame_Copy(unsigned slot);
+void Direct3D11_Release_Frame_Copies();
+
// The buffers and the vertex format a draw is about to read, mirrored as DX8Wrapper binds them.
// A null twin unbinds, which is what a stream with no buffer means. Index buffers in this engine
// are 16-bit without exception, so the format is not a parameter.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
index 5d347ad0c..910f785d8 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
@@ -109,6 +109,17 @@ unsigned char * Direct3D11_Capture_Back_Buffer(unsigned & width, unsigned & heig
}
void Direct3D11_Release_Capture(unsigned char * pixels) { delete [] pixels; }
+bool Direct3D11_Queue_Frame_Copy(unsigned) { return false; }
+const unsigned char * Direct3D11_Map_Frame_Copy(unsigned, unsigned & width, unsigned & height, unsigned & pitch)
+{
+ width = 0;
+ height = 0;
+ pitch = 0;
+ return NULL;
+}
+void Direct3D11_Unmap_Frame_Copy(unsigned) {}
+void Direct3D11_Release_Frame_Copies() {}
+
void Direct3D11_Mirror_Stream_Source(DX11BufferTwinClass *, unsigned, unsigned) {}
void Direct3D11_Mirror_Indices(DX11BufferTwinClass *) {}
void Direct3D11_Mirror_Vertex_Format(unsigned) {}
diff --git a/GeneralsMD/Code/Tests/test_observer_camera.inc b/GeneralsMD/Code/Tests/test_observer_camera.inc
index 85b6ea704..4fc47be5e 100644
--- a/GeneralsMD/Code/Tests/test_observer_camera.inc
+++ b/GeneralsMD/Code/Tests/test_observer_camera.inc
@@ -505,6 +505,57 @@ TEST(a_pixel_lies_in_the_wedge_counter_clockwise_of_its_ray)
CHECK_EQ( ObserverCamera_paneOf( 1200.0f, 360.0f, 640.0f, 360.0f, rays, 4 ), 3 );
}
+TEST(the_pane_runs_put_every_pixel_where_pane_of_puts_it)
+{
+ // the meeting point in the middle, on a pixel's centre row and column, sliding out past a corner,
+ // and on an odd picture, for every layout
+ struct Picture { Int width; Int height; Real originX; Real originY; };
+ const Picture pictures[] = {
+ { 1280, 720, 640.0f, 360.0f },
+ { 1280, 720, 640.5f, 360.5f },
+ { 1280, 720, 1503.37f, -211.81f },
+ { 333, 217, 101.25f, 160.75f },
+ };
+ std::vector< ObserverPaneRun > runs;
+ for( Int picture = 0; picture < (Int)ARRAY_SIZE( pictures ); picture++ )
+ {
+ const Picture &shape = pictures[ picture ];
+ for( Int count = 2; count <= OBSERVER_MOST_PANES; count++ )
+ {
+ Real rays[ OBSERVER_MOST_PANES ];
+ ObserverCamera_paneLayout( count, rays, shape.width, shape.height );
+ ObserverCamera_paneRuns( shape.width, shape.height, shape.originX, shape.originY, rays, count, runs );
+ Int wrong = 0;
+ Int covered = 0;
+ Int nextY = 0;
+ Int nextX = 0;
+ for( size_t index = 0; index < runs.size(); index++ )
+ {
+ const ObserverPaneRun &run = runs[ index ];
+ if( run.x0 == 0 && run.y == nextY + 1 && nextX == shape.width )
+ {
+ nextY = run.y;
+ nextX = 0;
+ }
+ CHECK( run.y == nextY && run.x0 == nextX && run.x1 > run.x0 );
+ nextX = run.x1;
+ for( Int x = run.x0; x < run.x1; x++ )
+ {
+ const Int pane = ObserverCamera_paneOf( (Real)x + 0.5f, (Real)run.y + 0.5f, shape.originX, shape.originY,
+ rays, count );
+ if( pane != run.pane )
+ wrong++;
+ covered++;
+ }
+ }
+ CHECK_EQ( wrong, 0 );
+ CHECK_EQ( covered, shape.width * shape.height );
+ // a run is a whole stretch of one pane, not a pixel at a time
+ CHECK( (Int)runs.size() < shape.height * ( count + 2 ) );
+ }
+ }
+}
+
TEST(pane_zero_fills_the_screen_once_the_meeting_point_is_out_far_enough)
{
Real rays[ OBSERVER_MOST_PANES ];