Skip to content

Commit

Permalink
D3D12 Pixel History state tracking.
Browse files Browse the repository at this point in the history
* For capture resources, use the actual resource state at the time of
the event rather than a hardcoded state.
* When hardcoded RT source state was used, on UAV events attempting to
transition a texture which was not created with the RTV flag from that
state caused a device removal.
* Includes a fix provided by Jake to ApplyBarriers that caused an
incorrect state to be returned in this usage.
  • Loading branch information
jristic authored and Zorro666 committed Feb 13, 2024
1 parent 9f4f0e6 commit dbfd128
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions renderdoc/driver/d3d12/d3d12_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ void D3D12ResourceManager::ApplyBarriers(BarrierSet &barriers,

auto it = states.find(id);
if(it == states.end())
return;
continue;

SubresourceStateVector &st = it->second;

Expand Down Expand Up @@ -726,7 +726,7 @@ void D3D12ResourceManager::ApplyBarriers(BarrierSet &barriers,

auto it = states.find(id);
if(it == states.end())
return;
continue;

SubresourceStateVector &st = it->second;

Expand Down
55 changes: 42 additions & 13 deletions renderdoc/driver/d3d12/d3d12_pixelhistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,30 @@ struct D3D12PixelHistoryCallback : public D3D12ActionCallback
return targetIndex;
}

D3D12_RESOURCE_STATES GetImageState(ID3D12Resource *target, uint32_t planeSlice,
D3D12_RESOURCE_STATES fallback)
{
RDCASSERTMSG("This function is not meant for use with the non-capture resources.",
target != m_CallbackInfo.colorImage && target != m_CallbackInfo.dsImage);

D3D12CommandData &cmdData = *m_pDevice->GetQueue()->GetCommandData();
SubresourceStateVector targetStates =
cmdData.m_BakedCmdListInfo[cmdData.m_LastCmdListID].GetState(m_pDevice, GetResID(target));

uint32_t baseMip = m_CallbackInfo.targetSubresource.mip;
uint32_t baseSlice = m_CallbackInfo.targetSubresource.slice;

uint32_t subresource =
D3D12CalcSubresource(baseMip, baseSlice, planeSlice, m_CallbackInfo.targetDesc.MipLevels,
m_CallbackInfo.targetDesc.DepthOrArraySize);

if(targetStates.size() > subresource && targetStates[subresource].IsStates())
{
return targetStates[subresource].ToStates();
}
return fallback;
}

WrappedID3D12Device *m_pDevice;
D3D12PixelHistoryShaderCache *m_ShaderCache;
D3D12PixelHistoryCallbackInfo m_CallbackInfo;
Expand Down Expand Up @@ -1093,18 +1117,20 @@ struct D3D12ColorAndStencilCallback : public D3D12PixelHistoryCallback
targetCopyParams.sample = m_CallbackInfo.targetSubresource.sample;
targetCopyParams.mip = m_CallbackInfo.targetSubresource.mip;
targetCopyParams.arraySlice = m_CallbackInfo.targetSubresource.slice;
targetCopyParams.srcImageState = D3D12_RESOURCE_STATE_RENDER_TARGET;
targetCopyParams.multisampled = (m_CallbackInfo.targetDesc.SampleDesc.Count != 1);
D3D12_RESOURCE_STATES fallback = D3D12_RESOURCE_STATE_RENDER_TARGET;
if(IsDepthFormat(m_CallbackInfo.targetDesc, m_CallbackInfo.compType))
{
targetCopyParams.srcImageState = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
fallback = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
targetCopyParams.srcImageFormat = GetDepthSRVFormat(m_CallbackInfo.targetDesc.Format, 0);
targetCopyParams.depthcopy = true;
targetCopyParams.copyFormat = GetDepthCopyFormat(m_CallbackInfo.targetDesc.Format);
offset += offsetof(struct D3D12PixelHistoryValue, depth);
}
targetCopyParams.srcImageState =
GetImageState(m_CallbackInfo.targetImage, targetCopyParams.planeSlice, fallback);

CopyImagePixel(cmd, targetCopyParams, offset);

Expand All @@ -1115,10 +1141,11 @@ struct D3D12ColorAndStencilCallback : public D3D12PixelHistoryCallback
GetDepthSRVFormat(m_CallbackInfo.targetImage->GetDesc().Format, 1);
stencilCopyParams.copyFormat = DXGI_FORMAT_R8_TYPELESS;
stencilCopyParams.planeSlice = 1;
fallback = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
stencilCopyParams.srcImageState =
m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
GetImageState(m_CallbackInfo.targetImage, stencilCopyParams.planeSlice, fallback);
CopyImagePixel(cmd, stencilCopyParams, offset + sizeof(float));
}

Expand All @@ -1143,20 +1170,22 @@ struct D3D12ColorAndStencilCallback : public D3D12PixelHistoryCallback
depthCopyParams.srcImageFormat = GetDepthSRVFormat(depthFormat, 0);
depthCopyParams.copyFormat = GetDepthCopyFormat(depthFormat);
depthCopyParams.depthcopy = true;
depthCopyParams.srcImageState = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
fallback = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
depthCopyParams.srcImageState = GetImageState(depthImage, depthCopyParams.planeSlice, fallback);
CopyImagePixel(cmd, depthCopyParams, offset + offsetof(struct D3D12PixelHistoryValue, depth));

if(IsDepthAndStencilFormat(depthFormat))
{
depthCopyParams.srcImageFormat = GetDepthSRVFormat(depthFormat, 1);
depthCopyParams.copyFormat = DXGI_FORMAT_R8_TYPELESS;
depthCopyParams.planeSlice = 1;
fallback = m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
depthCopyParams.srcImageState =
m_SavedState.dsv.GetDSV().Flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL
? D3D12_RESOURCE_STATE_DEPTH_READ
: D3D12_RESOURCE_STATE_DEPTH_WRITE;
GetImageState(depthImage, depthCopyParams.planeSlice, fallback);
CopyImagePixel(cmd, depthCopyParams,
offset + offsetof(struct D3D12PixelHistoryValue, stencil));
}
Expand Down

0 comments on commit dbfd128

Please sign in to comment.