Skip to content

Commit 7d06d2a

Browse files
ITextureUploader: add upload buffer auto-recycling
1 parent da5189b commit 7d06d2a

12 files changed

+392
-257
lines changed

Graphics/GraphicsTools/interface/GraphicsUtilities.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ DILIGENT_BEGIN_NAMESPACE(Diligent)
3939

4040
#include "../../../Primitives/interface/DefineRefMacro.h"
4141

42+
// clang-format off
4243
void DILIGENT_GLOBAL_FUNCTION(CreateUniformBuffer)(IRenderDevice* pDevice,
4344
Uint64 Size,
4445
const Char* Name,
4546
IBuffer** ppBuffer,
46-
USAGE Usage DEFAULT_VALUE(USAGE_DYNAMIC),
47-
BIND_FLAGS BindFlags DEFAULT_VALUE(BIND_UNIFORM_BUFFER),
48-
CPU_ACCESS_FLAGS CPUAccessFlags DEFAULT_VALUE(CPU_ACCESS_WRITE),
49-
void* pInitialData DEFAULT_VALUE(nullptr));
47+
USAGE Usage DEFAULT_VALUE(USAGE_DYNAMIC),
48+
BIND_FLAGS BindFlags DEFAULT_VALUE(BIND_UNIFORM_BUFFER),
49+
CPU_ACCESS_FLAGS CPUAccessFlags DEFAULT_VALUE(CPU_ACCESS_WRITE),
50+
void* pInitialData DEFAULT_VALUE(nullptr));
51+
// clang-format on
5052

5153
void DILIGENT_GLOBAL_FUNCTION(GenerateCheckerBoardPattern)(Uint32 Width,
5254
Uint32 Height,
@@ -261,6 +263,8 @@ struct GeometryPrimitiveBuffersCreateInfo
261263
};
262264
typedef struct GeometryPrimitiveBuffersCreateInfo GeometryPrimitiveBuffersCreateInfo;
263265

266+
// clang-format off
267+
264268
/// Creates vertex and index buffers for a geometry primitive (see Diligent::CreateGeometryPrimitive)
265269

266270
/// \param [in] pDevice - A pointer to the render device that will be used to create the buffers.
@@ -276,8 +280,8 @@ void DILIGENT_GLOBAL_FUNCTION(CreateGeometryPrimitiveBuffers)(IRenderDevice*
276280
const GeometryPrimitiveBuffersCreateInfo* pBufferCI,
277281
IBuffer** ppVertices,
278282
IBuffer** ppIndices,
279-
GeometryPrimitiveInfo* pInfo DEFAULT_VALUE(nullptr));
280-
283+
GeometryPrimitiveInfo* pInfo DEFAULT_VALUE(nullptr));
284+
// clang-format on
281285

282286
/// Returns the DirectX shader compiler interface associated with the specified render device.
283287

Graphics/GraphicsTools/interface/TextureUploader.hpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@
3333
namespace Diligent
3434
{
3535

36-
// clang-format off
3736

3837
/// Upload buffer description
3938
struct UploadBufferDesc
4039
{
41-
Uint32 Width = 0;
42-
Uint32 Height = 0;
43-
Uint32 Depth = 1;
44-
Uint32 MipLevels = 1;
45-
Uint32 ArraySize = 1;
46-
TEXTURE_FORMAT Format = TEX_FORMAT_UNKNOWN;
47-
48-
bool operator == (const UploadBufferDesc &rhs) const
40+
Uint32 Width = 0;
41+
Uint32 Height = 0;
42+
Uint32 Depth = 1;
43+
Uint32 MipLevels = 1;
44+
Uint32 ArraySize = 1;
45+
TEXTURE_FORMAT Format = TEX_FORMAT_UNKNOWN;
46+
47+
bool operator==(const UploadBufferDesc& rhs) const
4948
{
50-
return Width == rhs.Width &&
49+
return (Width == rhs.Width &&
5150
Height == rhs.Height &&
52-
Depth == rhs.Depth &&
53-
Format == rhs.Format;
51+
Depth == rhs.Depth &&
52+
MipLevels == rhs.MipLevels &&
53+
ArraySize == rhs.ArraySize &&
54+
Format == rhs.Format);
5455
}
5556
};
56-
// clang-format on
5757

5858
class IUploadBuffer : public IObject
5959
{
@@ -108,6 +108,8 @@ class ITextureUploader : public IObject
108108
IUploadBuffer** ppBuffer) = 0;
109109

110110

111+
// clang-format off
112+
111113
/// Schedules a GPU copy or executes the copy immediately.
112114

113115
/// \param [in] pContext - Pointer to the device context when the method is executed by
@@ -119,6 +121,8 @@ class ITextureUploader : public IObject
119121
/// \param [in] MipLevel - Destination mip level. When multiple mip levels are copied,
120122
/// the starting mip level.
121123
/// \param [in] pUploadBuffer - Upload buffer to copy data from.
124+
/// \param [in] AutoRecycle - Whether to recycle the upload buffer
125+
/// after the copy operation is complete.
122126
///
123127
/// When the method is called from a worker thread (pContext is null),
124128
/// it may enqueue a render-thread operation and block until the operation is
@@ -131,8 +135,9 @@ class ITextureUploader : public IObject
131135
ITexture* pDstTexture,
132136
Uint32 ArraySlice,
133137
Uint32 MipLevel,
134-
IUploadBuffer* pUploadBuffer) = 0;
135-
138+
IUploadBuffer* pUploadBuffer,
139+
bool AutoRecycle DEFAULT_VALUE(false)) = 0;
140+
// clang-format on
136141

137142
/// Recycles upload buffer to make it available for future operations.
138143

Graphics/GraphicsTools/interface/TextureUploaderBase.hpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,7 +42,7 @@ struct hash<Diligent::UploadBufferDesc>
4242
{
4343
size_t operator()(const Diligent::UploadBufferDesc& Desc) const
4444
{
45-
return Diligent::ComputeHash(Desc.Width, Desc.Height, Desc.Depth, static_cast<Diligent::Int32>(Desc.Format));
45+
return Diligent::ComputeHash(Desc.Width, Desc.Height, Desc.Depth, Desc.MipLevels, Desc.ArraySize, static_cast<Diligent::Int32>(Desc.Format));
4646
}
4747
};
4848

@@ -100,6 +100,47 @@ class TextureUploaderBase : public ObjectBase<ITextureUploader>
100100
m_pDevice{pDevice}
101101
{}
102102

103+
template <typename UploadBufferType>
104+
struct PendingOperation
105+
{
106+
enum class Type
107+
{
108+
Map,
109+
Copy
110+
} OpType;
111+
112+
bool AutoRecycle = false;
113+
114+
RefCntAutoPtr<UploadBufferType> pUploadBuffer;
115+
RefCntAutoPtr<ITexture> pDstTexture;
116+
117+
Uint32 DstSlice = 0;
118+
Uint32 DstMip = 0;
119+
120+
PendingOperation(Type Op, UploadBufferType* pBuff) :
121+
OpType{Op},
122+
pUploadBuffer{pBuff}
123+
{
124+
VERIFY_EXPR(OpType == Type::Map);
125+
}
126+
127+
PendingOperation(Type Op,
128+
UploadBufferType* pBuff,
129+
ITexture* pDstTex,
130+
Uint32 dstSlice,
131+
Uint32 dstMip,
132+
bool Recycle) :
133+
OpType{Op},
134+
pUploadBuffer{pBuff},
135+
pDstTexture{pDstTex},
136+
DstSlice{dstSlice},
137+
DstMip{dstMip},
138+
AutoRecycle{Recycle}
139+
{
140+
VERIFY_EXPR(OpType == Type::Copy);
141+
}
142+
};
143+
103144
protected:
104145
RefCntAutoPtr<IRenderDevice> m_pDevice;
105146
};

Graphics/GraphicsTools/interface/TextureUploaderD3D11.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -50,7 +50,8 @@ class TextureUploaderD3D11 : public TextureUploaderBase
5050
ITexture* pDstTexture,
5151
Uint32 ArraySlice,
5252
Uint32 MipLevel,
53-
IUploadBuffer* pUploadBuffer) override final;
53+
IUploadBuffer* pUploadBuffer,
54+
bool AutoRecycle) override final;
5455

5556
virtual void RecycleBuffer(IUploadBuffer* pUploadBuffer) override final;
5657

Graphics/GraphicsTools/interface/TextureUploaderD3D12_Vk.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -50,7 +50,8 @@ class TextureUploaderD3D12_Vk : public TextureUploaderBase
5050
ITexture* pDstTexture,
5151
Uint32 ArraySlice,
5252
Uint32 MipLevel,
53-
IUploadBuffer* pUploadBuffer) override final;
53+
IUploadBuffer* pUploadBuffer,
54+
bool AutoRecycle) override final;
5455

5556
virtual void RecycleBuffer(IUploadBuffer* pUploadBuffer) override final;
5657

Graphics/GraphicsTools/interface/TextureUploaderGL.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -50,7 +50,8 @@ class TextureUploaderGL : public TextureUploaderBase
5050
ITexture* pDstTexture,
5151
Uint32 ArraySlice,
5252
Uint32 MipLevel,
53-
IUploadBuffer* pUploadBuffer) override final;
53+
IUploadBuffer* pUploadBuffer,
54+
bool AutoRecycle) override final;
5455

5556
virtual void RecycleBuffer(IUploadBuffer* pUploadBuffer) override final;
5657

Graphics/GraphicsTools/interface/TextureUploaderWebGPU.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,8 @@ class TextureUploaderWebGPU : public TextureUploaderBase
4949
ITexture* pDstTexture,
5050
Uint32 ArraySlice,
5151
Uint32 MipLevel,
52-
IUploadBuffer* pUploadBuffer) override final;
52+
IUploadBuffer* pUploadBuffer,
53+
bool AutoRecycle) override final;
5354

5455
virtual void RecycleBuffer(IUploadBuffer* pUploadBuffer) override final;
5556

0 commit comments

Comments
 (0)