Skip to content

Commit 8bcc2c8

Browse files
authored
Merge pull request Unity-Technologies#4 from NicoLeyman/dx12_small_textures_fix
Fixed intermediate texture resource alignment on DX12
2 parents 72c73e9 + b911f5d commit 8bcc2c8

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

PluginSource/source/RenderAPI_D3D12.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RenderAPI_D3D12 : public RenderAPI
3030
virtual void EndModifyVertexBuffer(void* bufferHandle);
3131

3232
private:
33+
UINT64 GetAlignedSize(int width, int height, int pixelSize, int rowPitch);
3334
ID3D12Resource* GetUploadResource(UINT64 size);
3435
void CreateResources();
3536
void ReleaseResources();
@@ -63,6 +64,23 @@ RenderAPI_D3D12::RenderAPI_D3D12()
6364
{
6465
}
6566

67+
UINT64 RenderAPI_D3D12::GetAlignedSize( int width, int height, int pixelSize, int rowPitch)
68+
{
69+
UINT64 size = width * height * pixelSize;
70+
71+
if (size < D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)
72+
{
73+
return D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
74+
}
75+
else if (width * pixelSize < rowPitch)
76+
{
77+
return rowPitch * height;
78+
}
79+
else
80+
{
81+
return size;
82+
}
83+
}
6684

6785
ID3D12Resource* RenderAPI_D3D12::GetUploadResource(UINT64 size)
6886
{
@@ -179,11 +197,12 @@ void* RenderAPI_D3D12::BeginModifyTexture(void* textureHandle, int textureWidth,
179197
s_D3D12CmdList->Reset(s_D3D12CmdAlloc, nullptr);
180198

181199
// Fill data
182-
const UINT64 kDataSize = textureWidth * textureHeight * 4;
200+
// Clamp to minimum rowPitch of RGBA32
201+
*outRowPitch = max(textureWidth * 4, 256);
202+
const UINT64 kDataSize = GetAlignedSize(textureWidth, textureHeight, 4, *outRowPitch);
183203
ID3D12Resource* upload = GetUploadResource(kDataSize);
184204
void* mapped = NULL;
185205
upload->Map(0, NULL, &mapped);
186-
*outRowPitch = textureWidth * 4;
187206
return mapped;
188207
}
189208

@@ -192,7 +211,7 @@ void RenderAPI_D3D12::EndModifyTexture(void* textureHandle, int textureWidth, in
192211
{
193212
ID3D12Device* device = s_D3D12->GetDevice();
194213

195-
const UINT64 kDataSize = textureWidth * textureHeight * 4;
214+
const UINT64 kDataSize = GetAlignedSize(textureWidth, textureHeight, 4, rowPitch);
196215
ID3D12Resource* upload = GetUploadResource(kDataSize);
197216
upload->Unmap(0, NULL);
198217

0 commit comments

Comments
 (0)