Skip to content

Commit

Permalink
Moved Blob class to separate cpp/h files in order to use it outside t…
Browse files Browse the repository at this point in the history
…exture converter, removed unused projects
  • Loading branch information
drzhn committed May 5, 2023
1 parent b5c990b commit ff4bf53
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 1,337 deletions.
34 changes: 2 additions & 32 deletions DirectXTex/DirectXTex.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include <DirectXMath.h>

#include "Blob.h"

#ifdef _WIN32
#if defined(NTDDI_WIN10_FE) || defined(__MINGW32__)
#include <ocidl.h>
Expand Down Expand Up @@ -400,38 +402,6 @@ namespace DirectX
uint8_t* m_memory;
};

//---------------------------------------------------------------------------------
// Memory blob (allocated buffer pointer is always 16-byte aligned)
class Blob
{
public:
Blob() noexcept : m_buffer(nullptr), m_size(0) {}
Blob(Blob&& moveFrom) noexcept : m_buffer(nullptr), m_size(0) { *this = std::move(moveFrom); }
~Blob() { Release(); }

Blob& __cdecl operator= (Blob&& moveFrom) noexcept;

Blob(const Blob&) = delete;
Blob& operator=(const Blob&) = delete;

HRESULT __cdecl Initialize(_In_ size_t size) noexcept;

void __cdecl Release() noexcept;

void *__cdecl GetBufferPointer() const noexcept { return m_buffer; }
size_t __cdecl GetBufferSize() const noexcept { return m_size; }

HRESULT __cdecl Resize(size_t size) noexcept;
// Reallocate for a new size

HRESULT __cdecl Trim(size_t size) noexcept;
// Shorten size without reallocation

private:
void* m_buffer;
size_t m_size;
};

//---------------------------------------------------------------------------------
// Image I/O

Expand Down
89 changes: 0 additions & 89 deletions DirectXTex/DirectXTexUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,92 +1508,3 @@ size_t TexMetadata::ComputeIndex(size_t mip, size_t item, size_t slice) const no
return size_t(-1);
}
}


//=====================================================================================
// Blob - Bitmap image container
//=====================================================================================

Blob& Blob::operator= (Blob&& moveFrom) noexcept
{
if (this != &moveFrom)
{
Release();

m_buffer = moveFrom.m_buffer;
m_size = moveFrom.m_size;

moveFrom.m_buffer = nullptr;
moveFrom.m_size = 0;
}
return *this;
}

void Blob::Release() noexcept
{
if (m_buffer)
{
_aligned_free(m_buffer);
m_buffer = nullptr;
}

m_size = 0;
}

_Use_decl_annotations_
HRESULT Blob::Initialize(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

Release();

m_buffer = _aligned_malloc(size, 16);
if (!m_buffer)
{
Release();
return E_OUTOFMEMORY;
}

m_size = size;

return S_OK;
}

HRESULT Blob::Trim(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

if (!m_buffer)
return E_UNEXPECTED;

if (size > m_size)
return E_INVALIDARG;

m_size = size;

return S_OK;
}

HRESULT Blob::Resize(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

if (!m_buffer || !m_size)
return E_UNEXPECTED;

void *tbuffer = _aligned_malloc(size, 16);
if (!tbuffer)
return E_OUTOFMEMORY;

memcpy(tbuffer, m_buffer, std::min(m_size, size));

Release();

m_buffer = tbuffer;
m_size = size;

return S_OK;
}
6 changes: 3 additions & 3 deletions DirectXTex/DirectXTex_Desktop_2022_Win10.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>DirectXTexP.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<ProgramDataBaseFileName>$(IntDir)$(TargetName).pdb</ProgramDataBaseFileName>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
Expand Down Expand Up @@ -306,7 +306,7 @@
</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>DirectXTexP.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<ProgramDataBaseFileName>$(IntDir)$(TargetName).pdb</ProgramDataBaseFileName>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
Expand Down Expand Up @@ -389,7 +389,7 @@
</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>DirectXTexP.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)Shaders\Compiled;$(SolutionDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<ProgramDataBaseFileName>$(IntDir)$(TargetName).pdb</ProgramDataBaseFileName>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
Expand Down
93 changes: 93 additions & 0 deletions Texconv/Blob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//=====================================================================================
// Blob - Bitmap image container
//=====================================================================================
#include "Blob.h"

#include <cstring>

#include "Winerror.h"


Blob& Blob::operator= (Blob&& moveFrom) noexcept
{
if (this != &moveFrom)
{
Release();

m_buffer = moveFrom.m_buffer;
m_size = moveFrom.m_size;

moveFrom.m_buffer = nullptr;
moveFrom.m_size = 0;
}
return *this;
}

void Blob::Release() noexcept
{
if (m_buffer)
{
_aligned_free(m_buffer);
m_buffer = nullptr;
}

m_size = 0;
}

_Use_decl_annotations_
HRESULT Blob::Initialize(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

Release();

m_buffer = _aligned_malloc(size, 16);
if (!m_buffer)
{
Release();
return E_OUTOFMEMORY;
}

m_size = size;

return S_OK;
}

HRESULT Blob::Trim(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

if (!m_buffer)
return E_UNEXPECTED;

if (size > m_size)
return E_INVALIDARG;

m_size = size;

return S_OK;
}

HRESULT Blob::Resize(size_t size) noexcept
{
if (!size)
return E_INVALIDARG;

if (!m_buffer || !m_size)
return E_UNEXPECTED;

void *tbuffer = _aligned_malloc(size, 16);
if (!tbuffer)
return E_OUTOFMEMORY;

memcpy(tbuffer, m_buffer, std::min(m_size, size));

Release();

m_buffer = tbuffer;
m_size = size;

return S_OK;
}
Loading

0 comments on commit ff4bf53

Please sign in to comment.