Skip to content

Commit 2b8ca29

Browse files
authored
Merge pull request #5 from NicoLeyman/dx12_small_textures_fix
Fixed crash on D3D12 with non power of 2 texture sizes.
2 parents 8bcc2c8 + cd54d6c commit 2b8ca29

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

PluginSource/source/RenderAPI_D3D12.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "RenderAPI.h"
22
#include "PlatformBase.h"
33

4+
#include <cmath>
5+
46
// Direct3D 12 implementation of RenderAPI.
57

68

@@ -30,6 +32,7 @@ class RenderAPI_D3D12 : public RenderAPI
3032
virtual void EndModifyVertexBuffer(void* bufferHandle);
3133

3234
private:
35+
UINT64 AlignPow2(UINT64 value);
3336
UINT64 GetAlignedSize(int width, int height, int pixelSize, int rowPitch);
3437
ID3D12Resource* GetUploadResource(UINT64 size);
3538
void CreateResources();
@@ -64,10 +67,18 @@ RenderAPI_D3D12::RenderAPI_D3D12()
6467
{
6568
}
6669

70+
UINT64 RenderAPI_D3D12::AlignPow2(UINT64 value)
71+
{
72+
UINT64 aligned = pow(2, (int)log2(value));
73+
return aligned >= value ? aligned : aligned * 2;
74+
}
75+
6776
UINT64 RenderAPI_D3D12::GetAlignedSize( int width, int height, int pixelSize, int rowPitch)
6877
{
6978
UINT64 size = width * height * pixelSize;
7079

80+
size = AlignPow2(size);
81+
7182
if (size < D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)
7283
{
7384
return D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
@@ -198,7 +209,7 @@ void* RenderAPI_D3D12::BeginModifyTexture(void* textureHandle, int textureWidth,
198209

199210
// Fill data
200211
// Clamp to minimum rowPitch of RGBA32
201-
*outRowPitch = max(textureWidth * 4, 256);
212+
*outRowPitch = max(AlignPow2(textureWidth * 4), 256);
202213
const UINT64 kDataSize = GetAlignedSize(textureWidth, textureHeight, 4, *outRowPitch);
203214
ID3D12Resource* upload = GetUploadResource(kDataSize);
204215
void* mapped = NULL;

0 commit comments

Comments
 (0)