From a8943485301e3ce4bb9df6d113b57abd146c4d81 Mon Sep 17 00:00:00 2001
From: Geoffrey McRae <geoff@hostfission.com>
Date: Wed, 28 Feb 2024 12:23:41 +1100
Subject: [PATCH] [host] d12: correct the pitch when HDR16 is in use

---
 host/platform/Windows/capture/D12/d12.c | 28 ++++++++++++-------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/host/platform/Windows/capture/D12/d12.c b/host/platform/Windows/capture/D12/d12.c
index 76ef9c9f9..317db73d2 100644
--- a/host/platform/Windows/capture/D12/d12.c
+++ b/host/platform/Windows/capture/D12/d12.c
@@ -522,8 +522,6 @@ static CaptureResult d12_waitFrame(unsigned frameBufferIndex,
       goto exit;
   }
 
-  D12FrameFormat dstFormat = this->dstFormat;
-
   // if the input format changed, reconfigure the effects
   if (srcFormat.desc.Width  == 0 ||
       srcFormat.desc.Width  != this->captureFormat.desc.Width  ||
@@ -531,7 +529,8 @@ static CaptureResult d12_waitFrame(unsigned frameBufferIndex,
       srcFormat.desc.Format != this->captureFormat.desc.Format ||
       srcFormat.colorSpace  != this->captureFormat.colorSpace)
   {
-    this->captureFormat = srcFormat;
+    D12FrameFormat dstFormat = this->dstFormat;
+    this->captureFormat      = srcFormat;
 
     D12Effect * effect;
     D12FrameFormat curFormat = srcFormat;
@@ -571,7 +570,7 @@ static CaptureResult d12_waitFrame(unsigned frameBufferIndex,
 
   D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout;
   ID3D12Device3_GetCopyableFootprints(*this->device,
-    &dstFormat.desc,
+    &this->dstFormat.desc,
     0       , // FirstSubresource
     1       , // NumSubresources
     0       , // BaseOffset,
@@ -581,20 +580,21 @@ static CaptureResult d12_waitFrame(unsigned frameBufferIndex,
     NULL);  // pTotalBytes
   this->pitch = layout.Footprint.RowPitch;
 
-  const unsigned int maxRows = maxFrameSize / layout.Footprint.RowPitch;
+  const unsigned maxRows = maxFrameSize / layout.Footprint.RowPitch;
+  const unsigned bpp     = this->dstFormat.format == CAPTURE_FMT_RGBA16F ? 8 : 4;
 
   frame->formatVer        = this->formatVer;
   frame->screenWidth      = srcFormat.width;
   frame->screenHeight     = srcFormat.height;
-  frame->dataWidth        = dstFormat.desc.Width;
-  frame->dataHeight       = min(maxRows, dstFormat.desc.Height);
-  frame->frameWidth       = dstFormat.width;
-  frame->frameHeight      = dstFormat.height;
-  frame->truncated        = maxRows < dstFormat.desc.Height;
-  frame->pitch            = layout.Footprint.RowPitch;
-  frame->stride           = layout.Footprint.RowPitch / 4;
-  frame->format           = dstFormat.format;
-  frame->hdr              = dstFormat.colorSpace ==
+  frame->dataWidth        = this->dstFormat.desc.Width;
+  frame->dataHeight       = min(maxRows, this->dstFormat.desc.Height);
+  frame->frameWidth       = this->dstFormat.width;
+  frame->frameHeight      = this->dstFormat.height;
+  frame->truncated        = maxRows < this->dstFormat.desc.Height;
+  frame->pitch            = this->pitch;
+  frame->stride           = this->pitch / bpp;
+  frame->format           = this->dstFormat.format;
+  frame->hdr              = this->dstFormat.colorSpace ==
     DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
   frame->hdrPQ            = false;
   frame->rotation         = desc.rotation;