Skip to content

Commit 4371d91

Browse files
TextureLoader: added WriteDDSToStream function
1 parent eb359ff commit 4371d91

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

TextureLoader/interface/TextureLoader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ bool DILIGENT_GLOBAL_FUNCTION(SaveTextureAsDDS)(const char* FilePath,
277277
const TextureDesc REF Desc,
278278
const TextureData REF TexData);
279279

280+
281+
/// Writes texture as DDS to a file stream.
282+
283+
/// \param [in] pFileStream - File stream.
284+
/// \param [in] Desc - Texture description.
285+
/// \param [in] TexData - Texture subresource data.
286+
/// \return true if the texture has been written successfully, and false otherwise.
287+
bool DILIGENT_GLOBAL_FUNCTION(WriteDDSToStream)(IFileStream* pFileStream,
288+
const TextureDesc REF Desc,
289+
const TextureData REF TexData);
290+
280291
#include "../../../DiligentCore/Primitives/interface/UndefGlobalFuncHelperMacros.h"
281292

282293
DILIGENT_END_NAMESPACE // namespace Diligent

TextureLoader/src/DDSLoader.cpp

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -48,7 +48,7 @@
4848
// clang-format off
4949

5050
#include "TextureLoaderImpl.hpp"
51-
#include "FileWrapper.hpp"
51+
#include "BasicFileStream.hpp"
5252
#include "GraphicsAccessories.hpp"
5353

5454
#include "dxgiformat.h"
@@ -831,15 +831,15 @@ static DXGI_FORMAT GetDXGIFormat(const DDS_PIXELFORMAT& ddpf)
831831

832832
//--------------------------------------------------------------------------------------
833833
static void FillInitData(
834-
_In_ Uint32 width,
835-
_In_ Uint32 height,
836-
_In_ Uint32 depth,
837-
_In_ Uint32 srcMipCount,
838-
_In_ Uint32 dstMipCount,
839-
_In_ Uint32 arraySize,
840-
_In_ DXGI_FORMAT format,
841-
_In_ size_t bitSize,
842-
_In_ const Uint8* bitData,
834+
_In_ Uint32 width,
835+
_In_ Uint32 height,
836+
_In_ Uint32 depth,
837+
_In_ Uint32 srcMipCount,
838+
_In_ Uint32 dstMipCount,
839+
_In_ Uint32 arraySize,
840+
_In_ DXGI_FORMAT format,
841+
_In_ size_t bitSize,
842+
_In_ const Uint8* bitData,
843843
_Out_ TextureSubResData* initData)
844844
{
845845
VERIFY_EXPR(bitData != nullptr && initData != nullptr);
@@ -1126,10 +1126,22 @@ void TextureLoaderImpl::LoadFromDDS(const TextureLoadInfo& TexLoadInfo, const Ui
11261126
}
11271127

11281128

1129-
bool SaveTextureAsDDS(const char* FilePath,
1129+
bool WriteDDSToStream(IFileStream* pFileStream,
11301130
const TextureDesc& Desc,
11311131
const TextureData& TexData)
11321132
{
1133+
if (pFileStream == nullptr)
1134+
{
1135+
DEV_ERROR("Output file stream must not be null");
1136+
return false;
1137+
}
1138+
1139+
if (!pFileStream->IsValid())
1140+
{
1141+
LOG_ERROR_MESSAGE("Output file stream is not valid");
1142+
return false;
1143+
}
1144+
11331145
const auto ArraySize = Desc.GetArraySize();
11341146
VERIFY(TexData.NumSubresources == Desc.MipLevels * ArraySize, "Incorrect number of subresources");
11351147
VERIFY_EXPR(TexData.pSubResources != nullptr);
@@ -1172,20 +1184,13 @@ bool SaveTextureAsDDS(const char* FilePath,
11721184
return false;
11731185
}
11741186

1175-
FileWrapper File{FilePath, EFileAccessMode::Overwrite};
1176-
if (!File)
1177-
{
1178-
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
1187+
if (!pFileStream->Write(&Magic, sizeof(Magic)))
11791188
return false;
1180-
}
11811189

1182-
if (!File->Write(&Magic, sizeof(Magic)))
1190+
if (!pFileStream->Write(&Header, sizeof(Header)))
11831191
return false;
11841192

1185-
if (!File->Write(&Header, sizeof(Header)))
1186-
return false;
1187-
1188-
if (!File->Write(&Header10, sizeof(Header10)))
1193+
if (!pFileStream->Write(&Header10, sizeof(Header10)))
11891194
return false;
11901195

11911196
const auto& FmtAttribs = GetTextureFormatAttribs(Desc.Format);
@@ -1202,7 +1207,7 @@ bool SaveTextureAsDDS(const char* FilePath,
12021207
for (Uint32 row = 0; row < MipProps.StorageHeight / FmtAttribs.BlockHeight; ++row)
12031208
{
12041209
const auto* pRowData = pData + Stride * row;
1205-
if (!File->Write(pRowData, StaticCast<size_t>(MipProps.RowSize)))
1210+
if (!pFileStream->Write(pRowData, StaticCast<size_t>(MipProps.RowSize)))
12061211
return false;
12071212
}
12081213
}
@@ -1211,6 +1216,20 @@ bool SaveTextureAsDDS(const char* FilePath,
12111216
return true;
12121217
}
12131218

1219+
bool SaveTextureAsDDS(const char* FilePath,
1220+
const TextureDesc& Desc,
1221+
const TextureData& TexData)
1222+
{
1223+
RefCntAutoPtr<BasicFileStream> pFileStream{BasicFileStream::Create(FilePath, EFileAccessMode::Overwrite)};
1224+
if (!pFileStream || !pFileStream->IsValid())
1225+
{
1226+
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
1227+
return false;
1228+
}
1229+
1230+
return WriteDDSToStream(pFileStream, Desc, TexData);
1231+
}
1232+
12141233
} // namespace Diligent
12151234

12161235
extern "C"

0 commit comments

Comments
 (0)