Dump images from GfxImageLoadDef

This commit is contained in:
Jan
2021-04-28 00:05:56 +02:00
parent 687d1185a3
commit 12ac569bfa
14 changed files with 549 additions and 41 deletions

View File

@ -0,0 +1,103 @@
#include "Dx12TextureLoader.h"
Dx12TextureLoader::Dx12TextureLoader(MemoryManager* memoryManager)
: m_memory_manager(memoryManager),
m_format(DXGI_FORMAT_UNKNOWN),
m_type(TextureType::T_2D),
m_has_mip_maps(false),
m_width(1u),
m_height(1u),
m_depth(1u)
{
}
const ImageFormat* Dx12TextureLoader::GetFormatForDx12Format() const
{
for (auto i : ImageFormat::ALL_FORMATS)
{
if (i->GetDxgiFormat() == m_format)
return i;
}
return nullptr;
}
Dx12TextureLoader& Dx12TextureLoader::Format(const DXGI_FORMAT format)
{
m_format = format;
return *this;
}
Dx12TextureLoader& Dx12TextureLoader::Type(const TextureType textureType)
{
m_type = textureType;
return *this;
}
Dx12TextureLoader& Dx12TextureLoader::HasMipMaps(const bool hasMipMaps)
{
m_has_mip_maps = hasMipMaps;
return *this;
}
Dx12TextureLoader& Dx12TextureLoader::Width(const size_t width)
{
m_width = width;
return *this;
}
Dx12TextureLoader& Dx12TextureLoader::Height(const size_t height)
{
m_height = height;
return *this;
}
Dx12TextureLoader& Dx12TextureLoader::Depth(const size_t depth)
{
m_depth = depth;
return *this;
}
Texture* Dx12TextureLoader::LoadTexture(const void* data)
{
const auto* format = GetFormatForDx12Format();
if (format == nullptr)
return nullptr;
Texture* texture;
switch (m_type)
{
case TextureType::T_2D:
texture = m_memory_manager->Create<Texture2D>(format, m_width, m_height, m_has_mip_maps);
break;
case TextureType::T_3D:
texture = m_memory_manager->Create<Texture3D>(format, m_width, m_height, m_depth, m_has_mip_maps);
break;
case TextureType::T_CUBE:
texture = m_memory_manager->Create<TextureCube>(format, m_width, m_width, m_has_mip_maps);
break;
default:
return nullptr;
}
texture->Allocate();
const auto mipMapCount = m_has_mip_maps ? texture->GetMipMapCount() : 1;
const auto faceCount = m_type == TextureType::T_CUBE ? 6 : 1;
const void* currentDataOffset = data;
for (auto currentMipLevel = 0; currentMipLevel < mipMapCount; currentMipLevel++)
{
for (auto currentFace = 0; currentFace < faceCount; currentFace++)
{
const auto mipSize = texture->GetSizeOfMipLevel(currentMipLevel);
memcpy(texture->GetBufferForMipLevel(currentMipLevel, currentFace), currentDataOffset, mipSize);
currentDataOffset = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(currentDataOffset) + mipSize);
}
}
return texture;
}

View File

@ -0,0 +1,35 @@
#pragma once
#include <unordered_map>
#include "Utils/ClassUtils.h"
#include "Image/DxgiFormat.h"
#include "Utils/MemoryManager.h"
#include "Image/Texture.h"
class Dx12TextureLoader
{
static std::unordered_map<ImageFormatId, ImageFormatId> m_conversion_table;
MemoryManager* m_memory_manager;
DXGI_FORMAT m_format;
TextureType m_type;
bool m_has_mip_maps;
size_t m_width;
size_t m_height;
size_t m_depth;
_NODISCARD const ImageFormat* GetFormatForDx12Format() const;
public:
explicit Dx12TextureLoader(MemoryManager* memoryManager);
Dx12TextureLoader& Format(DXGI_FORMAT format);
Dx12TextureLoader& Type(TextureType textureType);
Dx12TextureLoader& HasMipMaps(bool hasMipMaps);
Dx12TextureLoader& Width(size_t width);
Dx12TextureLoader& Height(size_t height);
Dx12TextureLoader& Depth(size_t depth);
Texture* LoadTexture(const void* data);
};

View File

@ -0,0 +1,103 @@
#include "Dx9TextureLoader.h"
Dx9TextureLoader::Dx9TextureLoader(MemoryManager* memoryManager)
: m_memory_manager(memoryManager),
m_format(D3DFMT_UNKNOWN),
m_type(TextureType::T_2D),
m_has_mip_maps(false),
m_width(1u),
m_height(1u),
m_depth(1u)
{
}
const ImageFormat* Dx9TextureLoader::GetFormatForDx9Format() const
{
for (auto i : ImageFormat::ALL_FORMATS)
{
if (i->GetD3DFormat() == m_format)
return i;
}
return nullptr;
}
Dx9TextureLoader& Dx9TextureLoader::Format(const D3DFORMAT format)
{
m_format = format;
return *this;
}
Dx9TextureLoader& Dx9TextureLoader::Type(const TextureType textureType)
{
m_type = textureType;
return *this;
}
Dx9TextureLoader& Dx9TextureLoader::HasMipMaps(const bool hasMipMaps)
{
m_has_mip_maps = hasMipMaps;
return *this;
}
Dx9TextureLoader& Dx9TextureLoader::Width(const size_t width)
{
m_width = width;
return *this;
}
Dx9TextureLoader& Dx9TextureLoader::Height(const size_t height)
{
m_height = height;
return *this;
}
Dx9TextureLoader& Dx9TextureLoader::Depth(const size_t depth)
{
m_depth = depth;
return *this;
}
Texture* Dx9TextureLoader::LoadTexture(const void* data)
{
const auto* format = GetFormatForDx9Format();
if (format == nullptr)
return nullptr;
Texture* texture;
switch (m_type)
{
case TextureType::T_2D:
texture = m_memory_manager->Create<Texture2D>(format, m_width, m_height, m_has_mip_maps);
break;
case TextureType::T_3D:
texture = m_memory_manager->Create<Texture3D>(format, m_width, m_height, m_depth, m_has_mip_maps);
break;
case TextureType::T_CUBE:
texture = m_memory_manager->Create<TextureCube>(format, m_width, m_width, m_has_mip_maps);
break;
default:
return nullptr;
}
texture->Allocate();
const auto mipMapCount = m_has_mip_maps ? texture->GetMipMapCount() : 1;
const auto faceCount = m_type == TextureType::T_CUBE ? 6 : 1;
const void* currentDataOffset = data;
for (auto currentMipLevel = 0; currentMipLevel < mipMapCount; currentMipLevel++)
{
for (auto currentFace = 0; currentFace < faceCount; currentFace++)
{
const auto mipSize = texture->GetSizeOfMipLevel(currentMipLevel);
memcpy(texture->GetBufferForMipLevel(currentMipLevel, currentFace), currentDataOffset, mipSize);
currentDataOffset = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(currentDataOffset) + mipSize);
}
}
return texture;
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <unordered_map>
#include "Utils/ClassUtils.h"
#include "Image/D3DFormat.h"
#include "Utils/MemoryManager.h"
#include "Image/Texture.h"
class Dx9TextureLoader
{
MemoryManager* m_memory_manager;
D3DFORMAT m_format;
TextureType m_type;
bool m_has_mip_maps;
size_t m_width;
size_t m_height;
size_t m_depth;
_NODISCARD const ImageFormat* GetFormatForDx9Format() const;
public:
explicit Dx9TextureLoader(MemoryManager* memoryManager);
Dx9TextureLoader& Format(D3DFORMAT format);
Dx9TextureLoader& Type(TextureType textureType);
Dx9TextureLoader& HasMipMaps(bool hasMipMaps);
Dx9TextureLoader& Width(size_t width);
Dx9TextureLoader& Height(size_t height);
Dx9TextureLoader& Depth(size_t depth);
Texture* LoadTexture(const void* data);
};