Skip to content

Commit

Permalink
TextureLoader: added WriteDDSToStream function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 4, 2024
1 parent eb359ff commit 4371d91
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
11 changes: 11 additions & 0 deletions TextureLoader/interface/TextureLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ bool DILIGENT_GLOBAL_FUNCTION(SaveTextureAsDDS)(const char* FilePath,
const TextureDesc REF Desc,
const TextureData REF TexData);


/// Writes texture as DDS to a file stream.

/// \param [in] pFileStream - File stream.
/// \param [in] Desc - Texture description.
/// \param [in] TexData - Texture subresource data.
/// \return true if the texture has been written successfully, and false otherwise.
bool DILIGENT_GLOBAL_FUNCTION(WriteDDSToStream)(IFileStream* pFileStream,
const TextureDesc REF Desc,
const TextureData REF TexData);

#include "../../../DiligentCore/Primitives/interface/UndefGlobalFuncHelperMacros.h"

DILIGENT_END_NAMESPACE // namespace Diligent
65 changes: 42 additions & 23 deletions TextureLoader/src/DDSLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -48,7 +48,7 @@
// clang-format off

#include "TextureLoaderImpl.hpp"
#include "FileWrapper.hpp"
#include "BasicFileStream.hpp"
#include "GraphicsAccessories.hpp"

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

//--------------------------------------------------------------------------------------
static void FillInitData(
_In_ Uint32 width,
_In_ Uint32 height,
_In_ Uint32 depth,
_In_ Uint32 srcMipCount,
_In_ Uint32 dstMipCount,
_In_ Uint32 arraySize,
_In_ DXGI_FORMAT format,
_In_ size_t bitSize,
_In_ const Uint8* bitData,
_In_ Uint32 width,
_In_ Uint32 height,
_In_ Uint32 depth,
_In_ Uint32 srcMipCount,
_In_ Uint32 dstMipCount,
_In_ Uint32 arraySize,
_In_ DXGI_FORMAT format,
_In_ size_t bitSize,
_In_ const Uint8* bitData,
_Out_ TextureSubResData* initData)
{
VERIFY_EXPR(bitData != nullptr && initData != nullptr);
Expand Down Expand Up @@ -1126,10 +1126,22 @@ void TextureLoaderImpl::LoadFromDDS(const TextureLoadInfo& TexLoadInfo, const Ui
}


bool SaveTextureAsDDS(const char* FilePath,
bool WriteDDSToStream(IFileStream* pFileStream,
const TextureDesc& Desc,
const TextureData& TexData)
{
if (pFileStream == nullptr)
{
DEV_ERROR("Output file stream must not be null");
return false;
}

if (!pFileStream->IsValid())
{
LOG_ERROR_MESSAGE("Output file stream is not valid");
return false;
}

const auto ArraySize = Desc.GetArraySize();
VERIFY(TexData.NumSubresources == Desc.MipLevels * ArraySize, "Incorrect number of subresources");
VERIFY_EXPR(TexData.pSubResources != nullptr);
Expand Down Expand Up @@ -1172,20 +1184,13 @@ bool SaveTextureAsDDS(const char* FilePath,
return false;
}

FileWrapper File{FilePath, EFileAccessMode::Overwrite};
if (!File)
{
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
if (!pFileStream->Write(&Magic, sizeof(Magic)))
return false;
}

if (!File->Write(&Magic, sizeof(Magic)))
if (!pFileStream->Write(&Header, sizeof(Header)))
return false;

if (!File->Write(&Header, sizeof(Header)))
return false;

if (!File->Write(&Header10, sizeof(Header10)))
if (!pFileStream->Write(&Header10, sizeof(Header10)))
return false;

const auto& FmtAttribs = GetTextureFormatAttribs(Desc.Format);
Expand All @@ -1202,7 +1207,7 @@ bool SaveTextureAsDDS(const char* FilePath,
for (Uint32 row = 0; row < MipProps.StorageHeight / FmtAttribs.BlockHeight; ++row)
{
const auto* pRowData = pData + Stride * row;
if (!File->Write(pRowData, StaticCast<size_t>(MipProps.RowSize)))
if (!pFileStream->Write(pRowData, StaticCast<size_t>(MipProps.RowSize)))
return false;
}
}
Expand All @@ -1211,6 +1216,20 @@ bool SaveTextureAsDDS(const char* FilePath,
return true;
}

bool SaveTextureAsDDS(const char* FilePath,
const TextureDesc& Desc,
const TextureData& TexData)
{
RefCntAutoPtr<BasicFileStream> pFileStream{BasicFileStream::Create(FilePath, EFileAccessMode::Overwrite)};
if (!pFileStream || !pFileStream->IsValid())
{
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
return false;
}

return WriteDDSToStream(pFileStream, Desc, TexData);
}

} // namespace Diligent

extern "C"
Expand Down

0 comments on commit 4371d91

Please sign in to comment.