Skip to content

Commit 8530da8

Browse files
vkorytskobaldurk
authored andcommitted
Fix dataSize calculation in WriteToSubresource
Corrects width, height, and depth calculation in WriteToSubresource for the specified subresource when storing provided source data blob. Using base resource dimensions could have led to source-data read access violation. Also updates corresponding test to validate subresource modification.
1 parent 7a2af10 commit 8530da8

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

renderdoc/driver/d3d12/d3d12_device.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,10 +2338,12 @@ bool WrappedID3D12Device::Serialise_WriteToSubresource(SerialiserType &ser, ID3D
23382338
}
23392339
else
23402340
{
2341-
UINT width = UINT(desc.Width);
2342-
UINT height = desc.Height;
2341+
UINT width = RDCMAX(1U, UINT(desc.Width) >> Subresource);
2342+
UINT height = RDCMAX(1U, desc.Height >> Subresource);
23432343
// only 3D textures have a depth, array slices are separate subresources.
2344-
UINT depth = (desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? desc.DepthOrArraySize : 1);
2344+
UINT depth = (desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D
2345+
? RDCMAX(1U, UINT(desc.DepthOrArraySize) >> Subresource)
2346+
: 1);
23452347

23462348
// if we have a box, use its dimensions
23472349
if(pDstBox)

util/test/demos/d3d12/d3d12_write_subresource.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ float4 main(v2f IN) : SV_Target0
6969
D3D12_STATIC_SAMPLER_DESC staticSamp = {};
7070
staticSamp.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
7171
staticSamp.AddressU = staticSamp.AddressV = staticSamp.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
72+
// LOD is clamped to 0 since input SRV has multiple mips for subresource-modification
73+
// validation, but only the first one is considered as output result.
74+
staticSamp.MinLOD = staticSamp.MaxLOD = 0;
7275
staticSamp.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
7376

7477
ID3D12RootSignaturePtr sig = MakeSig(
@@ -86,11 +89,15 @@ float4 main(v2f IN) : SV_Target0
8689
heap.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
8790
heap.Type = D3D12_HEAP_TYPE_CUSTOM;
8891

89-
uint32_t *texData = new uint32_t[2048 * 2084];
92+
const size_t width = 2048, height = 2048;
93+
const UINT subresourceIdx = 8;
9094

91-
ID3D12ResourcePtr tex = MakeTexture(DXGI_FORMAT_R8G8B8A8_UNORM, 2048, 2048)
95+
uint32_t *baseData = new uint32_t[width * height];
96+
uint32_t *subresourceData = new uint32_t[(width * height) >> (2 * subresourceIdx)];
97+
98+
ID3D12ResourcePtr tex = MakeTexture(DXGI_FORMAT_R8G8B8A8_UNORM, width, height)
9299
.CustomHeap(heap)
93-
.Mips(1)
100+
.Mips(12)
94101
.InitialState(D3D12_RESOURCE_STATE_COMMON);
95102

96103
D3D12_GPU_DESCRIPTOR_HANDLE view =
@@ -102,15 +109,36 @@ float4 main(v2f IN) : SV_Target0
102109

103110
GPUSync();
104111

105-
tex->Map(0, NULL, NULL);
112+
{
113+
const size_t rowPitch = width * sizeof(uint32_t), depthPitch = rowPitch * height;
114+
115+
tex->Map(0, NULL, NULL);
116+
117+
memset(baseData, 0x00, depthPitch);
118+
tex->WriteToSubresource(0, NULL, baseData, rowPitch, depthPitch);
119+
120+
D3D12_BOX box = {400, 400, 0, 1600, 1600, 1};
121+
memset(baseData, 0xff, depthPitch);
122+
tex->WriteToSubresource(0, &box, baseData, rowPitch, depthPitch);
123+
124+
tex->Unmap(0, NULL);
125+
}
126+
127+
{
128+
const size_t rowPitch = (width >> subresourceIdx) * sizeof(uint32_t),
129+
depthPitch = rowPitch * (height >> subresourceIdx);
130+
131+
tex->Map(subresourceIdx, NULL, NULL);
132+
133+
memset(subresourceData, 0x00, depthPitch);
134+
tex->WriteToSubresource(subresourceIdx, NULL, subresourceData, rowPitch, depthPitch);
106135

107-
memset(texData, 0, 2048 * 2048 * sizeof(uint32_t));
108-
tex->WriteToSubresource(0, NULL, texData, 1024 * 4, 1024 * 1024);
109-
D3D12_BOX box = {400, 400, 0, 1600, 1600, 1};
110-
memset(texData, 0xff, 2048 * 2048 * sizeof(uint32_t));
111-
tex->WriteToSubresource(0, &box, texData, 1024 * 4, 1024 * 1024);
136+
D3D12_BOX box = {1, 1, 0, 7, 7, 1};
137+
memset(subresourceData, 0xff, depthPitch);
138+
tex->WriteToSubresource(subresourceIdx, &box, subresourceData, rowPitch, depthPitch);
112139

113-
tex->Unmap(0, NULL);
140+
tex->Unmap(subresourceIdx, NULL);
141+
}
114142

115143
GPUSync();
116144

0 commit comments

Comments
 (0)