Skip to content

Commit 23c3a38

Browse files
committed
Create fake buffer for D3D11 UAV hidden counters. Closes baldurk#1593
1 parent af2c0d2 commit 23c3a38

File tree

9 files changed

+136
-10
lines changed

9 files changed

+136
-10
lines changed

qrenderdoc/Windows/PipelineState/D3D11PipelineStateViewer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,9 @@ void D3D11PipelineStateViewer::addResourceRow(const D3D11ViewTag &view,
702702
.arg(buf->length / r.elementByteSize);
703703
}
704704

705-
if(r.bufferFlags & (D3DBufferViewFlags::Append | D3DBufferViewFlags::Counter))
705+
if(r.counterResourceId != ResourceId())
706706
{
707-
typeName += tr(" (Count: %1)").arg(r.bufferStructCount);
707+
typeName += tr(" (%1: %2)").arg(ToQStr(r.counterResourceId)).arg(r.bufferStructCount);
708708
}
709709

710710
// get the buffer type, whether it's just a basic type or a complex struct

renderdoc/api/replay/d3d11_pipestate.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,20 @@ struct View
179179
bool operator==(const View &o) const
180180
{
181181
return viewResourceId == o.viewResourceId && resourceResourceId == o.resourceResourceId &&
182-
type == o.type && viewFormat == o.viewFormat && structured == o.structured &&
183-
bufferStructCount == o.bufferStructCount && elementByteSize == o.elementByteSize &&
184-
firstElement == o.firstElement && numElements == o.numElements &&
185-
bufferFlags == o.bufferFlags && firstMip == o.firstMip && numMips == o.numMips &&
186-
numSlices == o.numSlices && firstSlice == o.firstSlice;
182+
counterResourceId == o.counterResourceId && type == o.type && viewFormat == o.viewFormat &&
183+
structured == o.structured && bufferStructCount == o.bufferStructCount &&
184+
elementByteSize == o.elementByteSize && firstElement == o.firstElement &&
185+
numElements == o.numElements && bufferFlags == o.bufferFlags && firstMip == o.firstMip &&
186+
numMips == o.numMips && numSlices == o.numSlices && firstSlice == o.firstSlice;
187187
}
188188
bool operator<(const View &o) const
189189
{
190190
if(!(viewResourceId == o.viewResourceId))
191191
return viewResourceId < o.viewResourceId;
192192
if(!(resourceResourceId == o.resourceResourceId))
193193
return resourceResourceId < o.resourceResourceId;
194+
if(!(counterResourceId == o.counterResourceId))
195+
return counterResourceId < o.counterResourceId;
194196
if(!(type == o.type))
195197
return type < o.type;
196198
if(!(viewFormat == o.viewFormat))
@@ -223,6 +225,9 @@ struct View
223225
DOCUMENT("The :class:`ResourceId` of the underlying resource the view refers to.");
224226
ResourceId resourceResourceId;
225227

228+
DOCUMENT("The :class:`ResourceId` of the resource where the hidden buffer counter is stored.");
229+
ResourceId counterResourceId;
230+
226231
DOCUMENT("The :class:`TextureType` of the view type.");
227232
TextureType type;
228233

renderdoc/driver/d3d11/d3d11_debug.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ void D3D11DebugManager::RenderForPredicate()
422422
m_pImmediateContext->Draw(3, 0);
423423
}
424424

425+
ResourceId D3D11DebugManager::AddCounterUAVBuffer(ID3D11UnorderedAccessView *uav)
426+
{
427+
ResourceId ret = ResourceIDGen::GetNewUniqueID();
428+
m_CounterBufferToUAV[ret] = uav;
429+
m_UAVToCounterBuffer[uav] = ret;
430+
return ret;
431+
}
432+
425433
void D3D11Replay::GeneralMisc::Init(WrappedID3D11Device *device)
426434
{
427435
D3D11ShaderCache *shaderCache = device->GetShaderCache();

renderdoc/driver/d3d11/d3d11_debug.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ struct D3D11DebugManager
104104

105105
void RenderForPredicate();
106106

107+
ResourceId AddCounterUAVBuffer(ID3D11UnorderedAccessView *uav);
108+
ResourceId GetCounterBufferID(ID3D11UnorderedAccessView *uav)
109+
{
110+
auto it = m_UAVToCounterBuffer.find(uav);
111+
if(it != m_UAVToCounterBuffer.end())
112+
return it->second;
113+
return ResourceId();
114+
}
115+
ID3D11UnorderedAccessView *GetCounterBufferUAV(ResourceId id)
116+
{
117+
auto it = m_CounterBufferToUAV.find(id);
118+
if(it != m_CounterBufferToUAV.end())
119+
return it->second;
120+
return NULL;
121+
}
122+
void GetCounterBuffers(std::vector<ResourceId> &ret)
123+
{
124+
for(auto pair : m_CounterBufferToUAV)
125+
ret.push_back(pair.first);
126+
}
127+
107128
uint32_t GetStructCount(ID3D11UnorderedAccessView *uav);
108129
void GetBufferData(ID3D11Buffer *buff, uint64_t offset, uint64_t length, bytebuf &retData);
109130

@@ -158,6 +179,9 @@ struct D3D11DebugManager
158179

159180
std::list<CacheElem> m_ShaderItemCache;
160181

182+
std::map<ResourceId, ID3D11UnorderedAccessView *> m_CounterBufferToUAV;
183+
std::map<ID3D11UnorderedAccessView *, ResourceId> m_UAVToCounterBuffer;
184+
161185
WrappedID3D11Device *m_pDevice = NULL;
162186
WrappedID3D11DeviceContext *m_pImmediateContext = NULL;
163187

renderdoc/driver/d3d11/d3d11_device.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,32 @@ ReplayStatus WrappedID3D11Device::ReadLogInitialisation(RDCFile *rdc, bool store
12431243
SetupDrawcallPointers(m_Drawcalls, GetFrameRecord().drawcallList);
12441244
}
12451245

1246+
// propagate any UAV names onto counter buffers
1247+
{
1248+
std::vector<ResourceId> counterBuffers;
1249+
GetDebugManager()->GetCounterBuffers(counterBuffers);
1250+
1251+
for(ResourceId buffId : counterBuffers)
1252+
{
1253+
ID3D11UnorderedAccessView *uav = GetDebugManager()->GetCounterBufferUAV(buffId);
1254+
ResourceId uavId = GetResourceManager()->GetOriginalID(GetIDForResource(uav));
1255+
1256+
ResourceDescription &uavDesc = GetReplay()->GetResourceDesc(uavId);
1257+
ResourceDescription &bufDesc = GetReplay()->GetResourceDesc(buffId);
1258+
1259+
if(uavDesc.autogeneratedName)
1260+
{
1261+
uint64_t num;
1262+
memcpy(&num, &uavId, sizeof(uint64_t));
1263+
bufDesc.SetCustomName("UAV " + ToStr(num) + " counter");
1264+
}
1265+
else
1266+
{
1267+
bufDesc.SetCustomName(uavDesc.name + " counter");
1268+
}
1269+
}
1270+
}
1271+
12461272
#if ENABLED(RDOC_DEVEL)
12471273
for(auto it = chunkInfos.begin(); it != chunkInfos.end(); ++it)
12481274
{

renderdoc/driver/d3d11/d3d11_device3_wrap.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "d3d11_device.h"
2626
#include "d3d11_context.h"
27+
#include "d3d11_debug.h"
2728
#include "d3d11_resources.h"
2829

2930
///////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -650,6 +651,19 @@ bool WrappedID3D11Device::Serialise_CreateUnorderedAccessView1(
650651

651652
AddResource(pView, ResourceType::View, "Unordered Access View");
652653
DerivedResource(pResource, pView);
654+
655+
{
656+
D3D11_UNORDERED_ACCESS_VIEW_DESC desc = {};
657+
ret->GetDesc(&desc);
658+
659+
if(desc.ViewDimension == D3D11_UAV_DIMENSION_BUFFER &&
660+
(desc.Buffer.Flags & (D3D11_BUFFER_UAV_FLAG_APPEND | D3D11_BUFFER_UAV_FLAG_COUNTER)))
661+
{
662+
ResourceId counterBuffer = GetDebugManager()->AddCounterUAVBuffer(ret);
663+
AddResource(counterBuffer, ResourceType::Buffer, "UAV Counter");
664+
DerivedResource(ret, counterBuffer);
665+
}
666+
}
653667
}
654668

655669
return true;

renderdoc/driver/d3d11/d3d11_device_wrap.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,19 @@ bool WrappedID3D11Device::Serialise_CreateUnorderedAccessView(
969969

970970
AddResource(pView, ResourceType::View, "Unordered Access View");
971971
DerivedResource(pResource, pView);
972+
973+
{
974+
D3D11_UNORDERED_ACCESS_VIEW_DESC desc = {};
975+
ret->GetDesc(&desc);
976+
977+
if(desc.ViewDimension == D3D11_UAV_DIMENSION_BUFFER &&
978+
(desc.Buffer.Flags & (D3D11_BUFFER_UAV_FLAG_APPEND | D3D11_BUFFER_UAV_FLAG_COUNTER)))
979+
{
980+
ResourceId counterBuffer = GetDebugManager()->AddCounterUAVBuffer(ret);
981+
AddResource(counterBuffer, ResourceType::Buffer, "UAV Counter");
982+
DerivedResource(ret, counterBuffer);
983+
}
984+
}
972985
}
973986

974987
return true;

renderdoc/driver/d3d11/d3d11_replay.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ std::vector<ResourceId> D3D11Replay::GetBuffers()
613613
ret.push_back(it->first);
614614
}
615615

616+
GetDebugManager()->GetCounterBuffers(ret);
617+
616618
return ret;
617619
}
618620

@@ -621,6 +623,16 @@ BufferDescription D3D11Replay::GetBuffer(ResourceId id)
621623
BufferDescription ret = {};
622624
ret.resourceId = ResourceId();
623625

626+
if(GetDebugManager()->GetCounterBufferUAV(id))
627+
{
628+
// no original ID for this one
629+
ret.resourceId = id;
630+
ret.length = 4;
631+
ret.gpuAddress = 0;
632+
ret.creationFlags = BufferCategory::ReadWrite;
633+
return ret;
634+
}
635+
624636
auto it = WrappedID3D11Buffer::m_BufferList.find(id);
625637

626638
if(it == WrappedID3D11Buffer::m_BufferList.end())
@@ -952,10 +964,14 @@ void D3D11Replay::SavePipelineState(uint32_t eventId)
952964
view.elementByteSize =
953965
desc.Format == DXGI_FORMAT_UNKNOWN ? 1 : GetByteSize(1, 1, 1, desc.Format, 0);
954966

967+
view.counterResourceId = ResourceId();
968+
955969
if(desc.ViewDimension == D3D11_UAV_DIMENSION_BUFFER &&
956970
(desc.Buffer.Flags & (D3D11_BUFFER_UAV_FLAG_APPEND | D3D11_BUFFER_UAV_FLAG_COUNTER)))
957971
{
958972
view.bufferStructCount = GetDebugManager()->GetStructCount(rs->CSUAVs[s]);
973+
974+
view.counterResourceId = GetDebugManager()->GetCounterBufferID(rs->CSUAVs[s]);
959975
}
960976

961977
view.resourceResourceId = rm->GetOriginalID(GetIDForResource(res));
@@ -1250,6 +1266,8 @@ void D3D11Replay::SavePipelineState(uint32_t eventId)
12501266
(desc.Buffer.Flags & (D3D11_BUFFER_UAV_FLAG_APPEND | D3D11_BUFFER_UAV_FLAG_COUNTER)))
12511267
{
12521268
view.bufferStructCount = GetDebugManager()->GetStructCount(rs->OM.UAVs[s]);
1269+
1270+
view.counterResourceId = GetDebugManager()->GetCounterBufferID(rs->OM.UAVs[s]);
12531271
}
12541272

12551273
view.resourceResourceId = rm->GetOriginalID(GetIDForResource(res));
@@ -1585,6 +1603,9 @@ std::vector<uint32_t> D3D11Replay::GetPassEvents(uint32_t eventId)
15851603

15861604
ResourceId D3D11Replay::GetLiveID(ResourceId id)
15871605
{
1606+
ID3D11UnorderedAccessView *counterUAV = GetDebugManager()->GetCounterBufferUAV(id);
1607+
if(counterUAV)
1608+
return id;
15881609
if(!m_pDevice->GetResourceManager()->HasLiveResource(id))
15891610
return ResourceId();
15901611
return m_pDevice->GetResourceManager()->GetLiveID(id);
@@ -1911,6 +1932,20 @@ bool D3D11Replay::GetHistogram(ResourceId texid, const Subresource &sub, CompTyp
19111932

19121933
void D3D11Replay::GetBufferData(ResourceId buff, uint64_t offset, uint64_t length, bytebuf &retData)
19131934
{
1935+
ID3D11UnorderedAccessView *counterUAV = GetDebugManager()->GetCounterBufferUAV(buff);
1936+
if(counterUAV)
1937+
{
1938+
uint32_t count = GetDebugManager()->GetStructCount(counterUAV);
1939+
1940+
// copy the uint first
1941+
retData.resize(4U);
1942+
memcpy(retData.data(), &count, retData.size());
1943+
1944+
// remove offset bytes, up to 4
1945+
retData.erase(0, RDCMIN(4ULL, offset));
1946+
return;
1947+
}
1948+
19141949
auto it = WrappedID3D11Buffer::m_BufferList.find(buff);
19151950

19161951
if(it == WrappedID3D11Buffer::m_BufferList.end())

renderdoc/replay/renderdoc_serialise.inl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ void DoSerialise(SerialiserType &ser, D3D11Pipe::View &el)
10441044
{
10451045
SERIALISE_MEMBER(viewResourceId);
10461046
SERIALISE_MEMBER(resourceResourceId);
1047+
SERIALISE_MEMBER(counterResourceId);
10471048
SERIALISE_MEMBER(type);
10481049
SERIALISE_MEMBER(viewFormat);
10491050

@@ -1059,7 +1060,7 @@ void DoSerialise(SerialiserType &ser, D3D11Pipe::View &el)
10591060
SERIALISE_MEMBER(firstSlice);
10601061
SERIALISE_MEMBER(numSlices);
10611062

1062-
SIZE_CHECK(64);
1063+
SIZE_CHECK(72);
10631064
}
10641065

10651066
template <typename SerialiserType>
@@ -1193,7 +1194,7 @@ void DoSerialise(SerialiserType &ser, D3D11Pipe::OutputMerger &el)
11931194
SERIALISE_MEMBER(depthReadOnly);
11941195
SERIALISE_MEMBER(stencilReadOnly);
11951196

1196-
SIZE_CHECK(272);
1197+
SIZE_CHECK(280);
11971198
}
11981199

11991200
template <typename SerialiserType>
@@ -1225,7 +1226,7 @@ void DoSerialise(SerialiserType &ser, D3D11Pipe::State &el)
12251226

12261227
SERIALISE_MEMBER(predication);
12271228

1228-
SIZE_CHECK(2072);
1229+
SIZE_CHECK(2080);
12291230
}
12301231

12311232
#pragma endregion D3D11 pipeline state

0 commit comments

Comments
 (0)