Skip to content

Commit 58a9a83

Browse files
Texture Loader: added option to use custom memory allocator
1 parent 45260db commit 58a9a83

File tree

4 files changed

+54
-50
lines changed

4 files changed

+54
-50
lines changed

TextureLoader/include/TextureLoaderImpl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class TextureLoaderImpl final : public ObjectBase<ITextureLoader>
8585
const std::string m_Name;
8686
TextureDesc m_TexDesc;
8787

88-
std::vector<TextureSubResData> m_SubResources;
89-
std::vector<std::vector<Uint8>> m_Mips;
88+
std::vector<TextureSubResData> m_SubResources;
89+
std::vector<RefCntAutoPtr<IDataBlob>> m_Mips;
9090
};
9191

9292
} // namespace Diligent

TextureLoader/interface/TextureLoader.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
DILIGENT_BEGIN_NAMESPACE(Diligent)
3636

3737
struct Image;
38+
struct IMemoryAllocator;
3839

3940
// clang-format off
4041

@@ -82,34 +83,34 @@ DILIGENT_TYPED_ENUM(TEXTURE_LOAD_COMPRESS_MODE, Uint8)
8283
struct TextureLoadInfo
8384
{
8485
/// Texture name passed over to the texture creation method
85-
const Char* Name DEFAULT_VALUE(nullptr);
86+
const Char* Name DEFAULT_INITIALIZER(nullptr);
8687

8788
/// Usage
88-
USAGE Usage DEFAULT_VALUE(USAGE_IMMUTABLE);
89+
USAGE Usage DEFAULT_INITIALIZER(USAGE_IMMUTABLE);
8990

9091
/// Bind flags
91-
BIND_FLAGS BindFlags DEFAULT_VALUE(BIND_SHADER_RESOURCE);
92+
BIND_FLAGS BindFlags DEFAULT_INITIALIZER(BIND_SHADER_RESOURCE);
9293

9394
/// Number of mip levels
94-
Uint32 MipLevels DEFAULT_VALUE(0);
95+
Uint32 MipLevels DEFAULT_INITIALIZER(0);
9596

9697
/// CPU access flags
97-
CPU_ACCESS_FLAGS CPUAccessFlags DEFAULT_VALUE(CPU_ACCESS_NONE);
98+
CPU_ACCESS_FLAGS CPUAccessFlags DEFAULT_INITIALIZER(CPU_ACCESS_NONE);
9899

99100
/// Flag indicating if this texture uses sRGB gamma encoding
100-
Bool IsSRGB DEFAULT_VALUE(False);
101+
Bool IsSRGB DEFAULT_INITIALIZER(False);
101102

102103
/// Flag indicating that the procedure should generate lower mip levels
103-
Bool GenerateMips DEFAULT_VALUE(True);
104+
Bool GenerateMips DEFAULT_INITIALIZER(True);
104105

105106
/// Flag indicating that the image should be flipped vertically
106-
Bool FlipVertically DEFAULT_VALUE(False);
107+
Bool FlipVertically DEFAULT_INITIALIZER(False);
107108

108109
/// Flag indicating that RGB channels should be premultiplied by alpha
109-
Bool PermultiplyAlpha DEFAULT_VALUE(False);
110+
Bool PermultiplyAlpha DEFAULT_INITIALIZER(False);
110111

111112
/// Texture format
112-
TEXTURE_FORMAT Format DEFAULT_VALUE(TEX_FORMAT_UNKNOWN);
113+
TEXTURE_FORMAT Format DEFAULT_INITIALIZER(TEX_FORMAT_UNKNOWN);
113114

114115
/// Alpha cut-off value used to remap alpha channel when generating mip
115116
/// levels as follows:
@@ -118,13 +119,13 @@ struct TextureLoadInfo
118119
///
119120
/// \note This value must be in 0 to 1 range and is only
120121
/// allowed for 4-channel 8-bit textures.
121-
float AlphaCutoff DEFAULT_VALUE(0);
122+
float AlphaCutoff DEFAULT_INITIALIZER(0);
122123

123124
/// Coarse mip filter type, see Diligent::TEXTURE_LOAD_MIP_FILTER.
124-
TEXTURE_LOAD_MIP_FILTER MipFilter DEFAULT_VALUE(TEXTURE_LOAD_MIP_FILTER_DEFAULT);
125+
TEXTURE_LOAD_MIP_FILTER MipFilter DEFAULT_INITIALIZER(TEXTURE_LOAD_MIP_FILTER_DEFAULT);
125126

126127
/// Texture compression mode, see Diligent::TEXTURE_LOAD_COMPRESS_MODE.
127-
TEXTURE_LOAD_COMPRESS_MODE CompressMode DEFAULT_VALUE(TEXTURE_LOAD_COMPRESS_MODE_NONE);
128+
TEXTURE_LOAD_COMPRESS_MODE CompressMode DEFAULT_INITIALIZER(TEXTURE_LOAD_COMPRESS_MODE_NONE);
128129

129130
/// Texture component swizzle.
130131
///
@@ -143,6 +144,9 @@ struct TextureLoadInfo
143144
/// be clipped to the specified dimension.
144145
Uint32 UniformImageClipDim DEFAULT_INITIALIZER(0);
145146

147+
/// An optional memory allocator to allocate memory for the texture.
148+
struct IMemoryAllocator* pAllocator DEFAULT_INITIALIZER(nullptr);
149+
146150
#if DILIGENT_CPP_INTERFACE
147151
explicit TextureLoadInfo(const Char* _Name,
148152
USAGE _Usage = TextureLoadInfo{}.Usage,

TextureLoader/src/DDSLoader.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ struct TexFormatToDXGIFormatMap
502502
{
503503
for (int DXGIFmt = int{DXGI_FORMAT_UNKNOWN} + 1; DXGIFmt < int{DXGI_FORMAT_COUNT}; ++DXGIFmt)
504504
{
505-
auto TexFmt = DXGIFormatToTexFormat(static_cast<DXGI_FORMAT>(DXGIFmt));
505+
TEXTURE_FORMAT TexFmt = DXGIFormatToTexFormat(static_cast<DXGI_FORMAT>(DXGIFmt));
506506
if (TexFmt != TEX_FORMAT_UNKNOWN)
507507
FmtMap[TexFmt] = static_cast<DXGI_FORMAT>(DXGIFmt);
508508
}
@@ -852,9 +852,9 @@ static void FillInitData(
852852
{
853853
for (size_t mip = 0; mip < srcMipCount; mip++)
854854
{
855-
const auto w = std::max(width >> mip, 1u);
856-
const auto h = std::max(height >> mip, 1u);
857-
const auto d = std::max(depth >> mip, 1u);
855+
const Uint32 w = std::max(width >> mip, 1u);
856+
const Uint32 h = std::max(height >> mip, 1u);
857+
const Uint32 d = std::max(depth >> mip, 1u);
858858

859859
size_t NumBytes = 0;
860860
size_t RowBytes = 0;
@@ -940,7 +940,7 @@ void TextureLoaderImpl::LoadFromDDS(const TextureLoadInfo& TexLoadInfo, const Ui
940940
LOG_ERROR_AND_THROW("Invalid dds magic number (", dwMagicNumber, "). ", DDS_MAGIC, " is expected.");
941941
}
942942

943-
const auto* header = reinterpret_cast<const DDS_HEADER*>(pData + sizeof(Uint32));
943+
const DDS_HEADER* header = reinterpret_cast<const DDS_HEADER*>(pData + sizeof(Uint32));
944944

945945
// Verify header to validate DDS file
946946
if (header->size != sizeof(DDS_HEADER) ||
@@ -974,15 +974,15 @@ void TextureLoaderImpl::LoadFromDDS(const TextureLoadInfo& TexLoadInfo, const Ui
974974
Uint32 d3d11ResDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
975975
DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN;
976976

977-
const auto SrcMipCount = std::max(header->mipMapCount, 1u);
978-
m_TexDesc.MipLevels = SrcMipCount;
977+
const Uint32 SrcMipCount = std::max(header->mipMapCount, 1u);
978+
m_TexDesc.MipLevels = SrcMipCount;
979979
if (TexLoadInfo.MipLevels > 0)
980980
m_TexDesc.MipLevels = std::min(m_TexDesc.MipLevels, TexLoadInfo.MipLevels);
981981

982982
if ((header->ddspf.flags & DDS_FOURCC) &&
983983
(MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
984984
{
985-
auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>((const char*)header + sizeof(DDS_HEADER));
985+
const DDS_HEADER_DXT10* d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>((const char*)header + sizeof(DDS_HEADER));
986986

987987
ArraySize = d3d10ext->arraySize;
988988
if (ArraySize == 0)
@@ -1142,7 +1142,7 @@ bool WriteDDSToStream(IFileStream* pFileStream,
11421142
return false;
11431143
}
11441144

1145-
const auto ArraySize = Desc.GetArraySize();
1145+
const Uint32 ArraySize = Desc.GetArraySize();
11461146
VERIFY(TexData.NumSubresources == Desc.MipLevels * ArraySize, "Incorrect number of subresources");
11471147
VERIFY_EXPR(TexData.pSubResources != nullptr);
11481148

@@ -1193,20 +1193,20 @@ bool WriteDDSToStream(IFileStream* pFileStream,
11931193
if (!pFileStream->Write(&Header10, sizeof(Header10)))
11941194
return false;
11951195

1196-
const auto& FmtAttribs = GetTextureFormatAttribs(Desc.Format);
1196+
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(Desc.Format);
11971197
for (Uint32 Slice = 0; Slice < ArraySize; ++Slice)
11981198
{
11991199
for (Uint32 Mip = 0; Mip < Desc.MipLevels; ++Mip)
12001200
{
1201-
const auto& MipProps = GetMipLevelProperties(Desc, Mip);
1202-
const auto& SubRes = TexData.pSubResources[Slice * Desc.MipLevels + Mip];
1201+
const MipLevelProperties MipProps = GetMipLevelProperties(Desc, Mip);
1202+
const TextureSubResData& SubRes = TexData.pSubResources[Slice * Desc.MipLevels + Mip];
12031203
VERIFY_EXPR(SubRes.pData != nullptr);
1204-
const auto* pData = reinterpret_cast<const Uint8*>(SubRes.pData);
1205-
const auto Stride = SubRes.Stride;
1204+
const Uint8* pData = static_cast<const Uint8*>(SubRes.pData);
1205+
const Uint64 Stride = SubRes.Stride;
12061206
VERIFY(Stride >= MipProps.RowSize, "Row stride is too small");
12071207
for (Uint32 row = 0; row < MipProps.StorageHeight / FmtAttribs.BlockHeight; ++row)
12081208
{
1209-
const auto* pRowData = pData + Stride * row;
1209+
const void* pRowData = pData + Stride * row;
12101210
if (!pFileStream->Write(pRowData, StaticCast<size_t>(MipProps.RowSize)))
12111211
return false;
12121212
}

TextureLoader/src/TextureLoaderImpl.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ TextureLoaderImpl::TextureLoaderImpl(IReferenceCounters* pRefCounters,
134134
m_Name{TexLoadInfo.Name != nullptr ? TexLoadInfo.Name : ""},
135135
m_TexDesc{TexDescFromTexLoadInfo(TexLoadInfo, m_Name)}
136136
{
137-
const auto ImgFileFormat = Image::GetFileFormat(pData, DataSize);
137+
const IMAGE_FILE_FORMAT ImgFileFormat = Image::GetFileFormat(pData, DataSize);
138138
if (ImgFileFormat == IMAGE_FILE_FORMAT_UNKNOWN)
139139
{
140140
LOG_ERROR_AND_THROW("Unable to derive image format.");
@@ -151,7 +151,7 @@ TextureLoaderImpl::TextureLoaderImpl(IReferenceCounters* pRefCounters,
151151
ImgLoadInfo.Format = ImgFileFormat;
152152
if (!m_pDataBlob)
153153
{
154-
m_pDataBlob = DataBlobImpl::Create(DataSize, pData);
154+
m_pDataBlob = DataBlobImpl::Create(TexLoadInfo.pAllocator, DataSize, pData);
155155
}
156156
ImgLoadInfo.IsSRGB = TexLoadInfo.IsSRGB;
157157
ImgLoadInfo.PermultiplyAlpha = TexLoadInfo.PermultiplyAlpha;
@@ -250,10 +250,10 @@ void TextureLoaderImpl::LoadFromImage(Image* pImage, const TextureLoadInfo& TexL
250250
TexLoadInfo.FlipVertically ||
251251
SwizzleRequired)
252252
{
253-
auto DstStride = ImgDesc.Width * NumComponents * TexFmtDesc.ComponentSize;
254-
DstStride = AlignUp(DstStride, Uint32{4});
255-
m_Mips[0].resize(size_t{DstStride} * size_t{ImgDesc.Height});
256-
m_SubResources[0].pData = m_Mips[0].data();
253+
Uint32 DstStride = ImgDesc.Width * NumComponents * TexFmtDesc.ComponentSize;
254+
DstStride = AlignUp(DstStride, Uint32{4});
255+
m_Mips[0] = DataBlobImpl::Create(TexLoadInfo.pAllocator, size_t{DstStride} * size_t{ImgDesc.Height});
256+
m_SubResources[0].pData = m_Mips[0]->GetDataPtr();
257257
m_SubResources[0].Stride = DstStride;
258258

259259
CopyPixelsAttribs CopyAttribs;
@@ -263,7 +263,7 @@ void TextureLoaderImpl::LoadFromImage(Image* pImage, const TextureLoadInfo& TexL
263263
CopyAttribs.pSrcPixels = pImage->GetData()->GetConstDataPtr();
264264
CopyAttribs.SrcStride = ImgDesc.RowStride;
265265
CopyAttribs.SrcCompCount = ImgDesc.NumComponents;
266-
CopyAttribs.pDstPixels = m_Mips[0].data();
266+
CopyAttribs.pDstPixels = m_Mips[0]->GetDataPtr();
267267
CopyAttribs.DstComponentSize = TexFmtDesc.ComponentSize;
268268
CopyAttribs.DstStride = DstStride;
269269
CopyAttribs.DstCompCount = NumComponents;
@@ -321,13 +321,13 @@ void TextureLoaderImpl::LoadFromImage(Image* pImage, const TextureLoadInfo& TexL
321321
RowSize = AlignUp(RowSize, Uint64{4});
322322
MipSize = RowSize * MipLevelProps.LogicalHeight;
323323
}
324-
m_Mips[m].resize(StaticCast<size_t>(MipSize));
325-
m_SubResources[m].pData = m_Mips[m].data();
324+
m_Mips[m] = DataBlobImpl::Create(TexLoadInfo.pAllocator, StaticCast<size_t>(MipSize));
325+
m_SubResources[m].pData = m_Mips[m]->GetDataPtr();
326326
m_SubResources[m].Stride = RowSize;
327327

328328
if (TexLoadInfo.GenerateMips)
329329
{
330-
auto FinerMipProps = GetMipLevelProperties(m_TexDesc, m - 1);
330+
MipLevelProperties FinerMipProps = GetMipLevelProperties(m_TexDesc, m - 1);
331331
if (TexLoadInfo.GenerateMips)
332332
{
333333
ComputeMipLevelAttribs Attribs;
@@ -336,7 +336,7 @@ void TextureLoaderImpl::LoadFromImage(Image* pImage, const TextureLoadInfo& TexL
336336
Attribs.FineMipHeight = FinerMipProps.LogicalHeight;
337337
Attribs.pFineMipData = m_SubResources[m - 1].pData;
338338
Attribs.FineMipStride = StaticCast<size_t>(m_SubResources[m - 1].Stride);
339-
Attribs.pCoarseMipData = m_Mips[m].data();
339+
Attribs.pCoarseMipData = m_Mips[m]->GetDataPtr();
340340
Attribs.CoarseMipStride = StaticCast<size_t>(m_SubResources[m].Stride);
341341
Attribs.AlphaCutoff = TexLoadInfo.AlphaCutoff;
342342
static_assert(MIP_FILTER_TYPE_DEFAULT == static_cast<MIP_FILTER_TYPE>(TEXTURE_LOAD_MIP_FILTER_DEFAULT), "Inconsistent enum values");
@@ -385,20 +385,20 @@ void TextureLoaderImpl::CompressSubresources(Uint32 NumComponents, Uint32 NumSrc
385385
m_TexDesc.Format = CompressedFormat;
386386
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(CompressedFormat);
387387

388-
std::vector<std::vector<Uint8>> CompressedMips(m_SubResources.size());
388+
std::vector<RefCntAutoPtr<IDataBlob>> CompressedMips(m_SubResources.size());
389389
for (Uint32 slice = 0; slice < m_TexDesc.GetArraySize(); ++slice)
390390
{
391391
for (Uint32 mip = 0; mip < m_TexDesc.MipLevels; ++mip)
392392
{
393-
const Uint32 SubResIndex = slice * m_TexDesc.MipLevels + mip;
394-
TextureSubResData& SubResData = m_SubResources[SubResIndex];
395-
std::vector<Uint8>& CompressedMip = CompressedMips[SubResIndex];
393+
const Uint32 SubResIndex = slice * m_TexDesc.MipLevels + mip;
394+
TextureSubResData& SubResData = m_SubResources[SubResIndex];
395+
RefCntAutoPtr<IDataBlob>& CompressedMip = CompressedMips[SubResIndex];
396396

397397
const MipLevelProperties CompressedMipProps = GetMipLevelProperties(m_TexDesc, mip);
398398
const Uint32 MaxCol = CompressedMipProps.LogicalWidth - 1;
399399
const Uint32 MaxRow = CompressedMipProps.LogicalHeight - 1;
400400
const size_t CompressedStride = static_cast<size_t>(CompressedMipProps.RowSize);
401-
CompressedMip.resize(CompressedStride * CompressedMipProps.StorageHeight);
401+
CompressedMip = DataBlobImpl::Create(TexLoadInfo.pAllocator, CompressedStride * CompressedMipProps.StorageHeight);
402402

403403
for (Uint32 row = 0; row < CompressedMipProps.StorageHeight; row += FmtAttribs.BlockHeight)
404404
{
@@ -431,7 +431,7 @@ void TextureLoaderImpl::CompressSubresources(Uint32 NumComponents, Uint32 NumSrc
431431
return reinterpret_cast<const unsigned char*>(BlockData.data());
432432
};
433433

434-
Uint8* pDst = &CompressedMip[(col / FmtAttribs.BlockWidth) * FmtAttribs.ComponentSize + CompressedStride * (row / FmtAttribs.BlockHeight)];
434+
Uint8* pDst = CompressedMip->GetDataPtr<Uint8>() + (col / FmtAttribs.BlockWidth) * FmtAttribs.ComponentSize + CompressedStride * (row / FmtAttribs.BlockHeight);
435435
if (NumComponents == 1)
436436
{
437437
std::array<Uint8, 16> BlockData8;
@@ -456,7 +456,7 @@ void TextureLoaderImpl::CompressSubresources(Uint32 NumComponents, Uint32 NumSrc
456456
}
457457
}
458458

459-
SubResData.pData = CompressedMip.data();
459+
SubResData.pData = CompressedMip->GetDataPtr();
460460
SubResData.Stride = CompressedStride;
461461
}
462462
}
@@ -477,7 +477,7 @@ void CreateTextureLoaderFromFile(const char* FilePath,
477477
if (!File)
478478
LOG_ERROR_AND_THROW("Failed to open file '", FilePath, "'.");
479479

480-
auto pFileData = DataBlobImpl::Create();
480+
RefCntAutoPtr<DataBlobImpl> pFileData = DataBlobImpl::Create(TexLoadInfo.pAllocator);
481481
File->Read(pFileData);
482482

483483
RefCntAutoPtr<ITextureLoader> pTexLoader{
@@ -504,7 +504,7 @@ void CreateTextureLoaderFromMemory(const void* pData,
504504
RefCntAutoPtr<IDataBlob> pDataCopy;
505505
if (MakeDataCopy)
506506
{
507-
pDataCopy = DataBlobImpl::Create(Size, pData);
507+
pDataCopy = DataBlobImpl::Create(TexLoadInfo.pAllocator, Size, pData);
508508
pData = pDataCopy->GetConstDataPtr();
509509
}
510510
RefCntAutoPtr<ITextureLoader> pTexLoader{MakeNewRCObj<TextureLoaderImpl>()(TexLoadInfo, reinterpret_cast<const Uint8*>(pData), Size, std::move(pDataCopy))};

0 commit comments

Comments
 (0)