Renamed DDS to Texture in most places
Prepared SHTextureAsset for ease of load into buffer and compilation
Added Copy constructor for SHTextureAsset
This commit is contained in:
Xiao Qi 2022-09-27 13:02:12 +08:00
parent 1ea4689a89
commit 7487602151
8 changed files with 125 additions and 66 deletions

View File

@ -1,11 +0,0 @@
#pragma once
#include "tinyddsloader.h"
namespace SHADE
{
struct SHDDSAsset
{
tinyddsloader::DDSFile image;
};
}

View File

@ -0,0 +1,47 @@
#pragma once
#include "tinyddsloader.h"
#include "Graphics/MiddleEnd/Textures/SHTextureLibrary.h"
#include <memory>
namespace SHADE
{
class SHTextureAsset
{
public:
uint32_t numBytes;
uint32_t width;
uint32_t height;
SHTexture::TextureFormat format;
std::vector<uint32_t> mipOffsets;
SHTexture::PixelChannel const * pixelData;
SHTextureAsset()
: numBytes{ 0 },
width{ 0 },
height{ 0 },
format{ SHTexture::TextureFormat::eUndefined },
pixelData{ nullptr }
{}
SHTextureAsset(SHTextureAsset const& rhs)
: numBytes{ rhs.numBytes },
width{ rhs.width },
height{ rhs.height },
format{ rhs.format },
mipOffsets{ rhs.mipOffsets },
pixelData(rhs.pixelData)
{}
//SHTextureAsset(SHTextureAsset&& rhs)
// : numBytes{ rhs.numBytes },
// width{ rhs.width },
// height{ rhs.height },
// format{ rhs.format },
// mipOffsets{ rhs.mipOffsets },
// pixelData(std::move(rhs.pixelData))
//{}
};
}

View File

@ -1,38 +0,0 @@
#include "SHpch.h"
#include "SHDDSLoader.h"
namespace SHADE
{
std::string SHDDSLoader::TinyDDSResultToString(tinyddsloader::Result value)
{
switch (value)
{
case tinyddsloader::Result::ErrorFileOpen:
return "File open err";
case tinyddsloader::Result::ErrorRead:
return "File read err";
case tinyddsloader::Result::ErrorMagicWord:
return "File header magicword err";
case tinyddsloader::Result::ErrorSize:
return "File size err";
case tinyddsloader::Result::ErrorVerify:
return "Pixel format err";
case tinyddsloader::Result::ErrorNotSupported:
return "Unsupported format";
case tinyddsloader::Result::ErrorInvalidData:
return "Invalid data";
default:
return "Unknown";
}
}
void SHDDSLoader::LoadImageAsset(AssetPath path, SHDDSAsset& asset)
{
tinyddsloader::Result loadResult = tinyddsloader::Result::Success;
loadResult = asset.image.Load(path.string().c_str());
if (loadResult != tinyddsloader::Result::Success)
{
SHLOG_ERROR("Unable to load DDS file: {} at {}", TinyDDSResultToString(loadResult), path.string());
}
}
}

View File

@ -0,0 +1,60 @@
#include "SHpch.h"
#include "SHTextureLoader.h"
namespace SHADE
{
std::string SHTextureLoader::TinyDDSResultToString(tinyddsloader::Result value)
{
switch (value)
{
case tinyddsloader::Result::ErrorFileOpen:
return "File open err";
case tinyddsloader::Result::ErrorRead:
return "File read err";
case tinyddsloader::Result::ErrorMagicWord:
return "File header magic word err";
case tinyddsloader::Result::ErrorSize:
return "File size err";
case tinyddsloader::Result::ErrorVerify:
return "Pixel format err";
case tinyddsloader::Result::ErrorNotSupported:
return "Unsupported format";
case tinyddsloader::Result::ErrorInvalidData:
return "Invalid data";
default:
return "Unknown";
}
}
void SHTextureLoader::LoadImageAsset(AssetPath path, SHTextureAsset& asset)
{
tinyddsloader::Result loadResult = tinyddsloader::Result::Success;
tinyddsloader::DDSFile file;
loadResult = file.Load(path.string().c_str());
if (loadResult != tinyddsloader::Result::Success)
{
SHLOG_ERROR("Unable to load Texture file: {} at {}", TinyDDSResultToString(loadResult), path.string());
}
size_t totalBytes{ 0 };
std::vector<uint32_t> mipOff;
for (auto i{0}; i < file.GetMipCount(); ++i)
{
totalBytes += file.GetImageData(i, 0)->m_memSlicePitch;
mipOff.push_back(totalBytes);
}
SHTexture::PixelChannel* pixel = new SHTexture::PixelChannel[totalBytes];
std::memcpy(pixel, file.GetDDSData(), totalBytes);
//pixel = std::move(reinterpret_cast<SHTexture::PixelChannel const*>(file.GetDDSData()));
asset.numBytes = totalBytes;
asset.width = file.GetWidth();
asset.height = file.GetHeight();
asset.format = static_cast<vk::Format>(file.GetFormat());
asset.mipOffsets = std::move(mipOff);
asset.pixelData = std::move(pixel);
}
}

View File

@ -2,17 +2,16 @@
#define TINYDDSLOADER_IMPLEMENTATION
#include "../SHAssetMacros.h"
#include "../Asset Types/SHDDSAsset.h"
#include "../Asset Types/SHTextureAsset.h"
#include "tinyddsloader.h"
#include <vector>
namespace SHADE
{
class SHDDSLoader
class SHTextureLoader
{
private:
static std::string TinyDDSResultToString(tinyddsloader::Result value);
public:
static void LoadImageAsset(AssetPath paths, SHDDSAsset& image);
static void LoadImageAsset(AssetPath paths, SHTextureAsset& image);
};
}

View File

@ -69,7 +69,8 @@ enum class AssetType : uint8_t
#define SCENE_EXTENSION ".SHADE"
#define PREFAB_EXTENSION ".SHPrefab"
#define MATERIAL_EXTENSION ".SHMat"
#define TEXTURE_EXTENSION ".dds"
#define TEXTURE_EXTENSION ".shtex"
#define DDS_EXTENSION ".dds"
#define FBX_EXTENSION ".fbx"
#define GLTF_EXTENSION ".gltf"
#define MESH_EXTENSION ".shmesh"
@ -80,6 +81,7 @@ std::string const EXTENSIONS[] = {
MATERIAL_EXTENSION,
IMAGE_EXTENSION,
TEXTURE_EXTENSION,
DDS_EXTENSION,
MESH_EXTENSION,
SCRIPT_EXTENSION,
SCENE_EXTENSION,

View File

@ -15,7 +15,7 @@
#include "Filesystem/SHFileSystem.h"
#include "Libraries/SHMeshLoader.h"
#include "Libraries/SHDDSLoader.h"
#include "Libraries/SHTextureLoader.h"
namespace SHADE
{
@ -26,7 +26,7 @@ namespace SHADE
std::unordered_map<AssetID, SHAsset> SHAssetManager::assetRegistry;
std::unordered_map<AssetID, SHMeshAsset> SHAssetManager::meshCollection;
std::unordered_map<AssetID, SHDDSAsset> SHAssetManager::ddsCollection;
std::unordered_map<AssetID, SHTextureAsset> SHAssetManager::textureCollection;
/****************************************************************************
* \brief Static function to generate asset ID.
@ -211,7 +211,7 @@ namespace SHADE
}
);
}
else if (path.extension().string() == TEXTURE_EXTENSION)
else if (path.extension().string() == DDS_EXTENSION)
{
LoadDDS(
{
@ -236,10 +236,10 @@ namespace SHADE
return result;
}
std::vector<SHDDSAsset> SHAssetManager::GetAllDDS() noexcept
std::vector<SHTextureAsset> SHAssetManager::GetAllDDS() noexcept
{
std::vector<SHDDSAsset> result;
for (auto const& dds : ddsCollection)
std::vector<SHTextureAsset> result;
for (auto const& dds : textureCollection)
{
result.push_back(dds.second);
}
@ -305,11 +305,11 @@ namespace SHADE
void SHAssetManager::LoadDDS(SHAsset asset) noexcept
{
SHDDSAsset image;
SHTextureAsset image;
SHDDSLoader::LoadImageAsset(asset.path, image);
SHTextureLoader::LoadImageAsset(asset.path, image);
ddsCollection.emplace(GenerateAssetID(AssetType::DDS), image);
textureCollection.emplace(GenerateAssetID(AssetType::DDS), image);
}
/****************************************************************************

View File

@ -13,7 +13,7 @@
#include "SHAsset.h"
#include "Asset Types/SHMeshAsset.h"
#include "Asset Types/SHDDSAsset.h"
#include "Asset Types/SHTextureAsset.h"
#include "SH_API.h"
namespace SHADE
@ -73,7 +73,7 @@ namespace SHADE
//TODO: TEMPORARY FOR TESTING GLTF & DDS
static void LoadDataTemp(std::string path) noexcept;
static std::vector<SHMeshAsset> GetAllMeshes() noexcept;
static std::vector<SHDDSAsset> GetAllDDS() noexcept;
static std::vector<SHTextureAsset> GetAllDDS() noexcept;
private:
/****************************************************************************
@ -128,6 +128,6 @@ namespace SHADE
static std::unordered_map<AssetID, SHAsset> assetRegistry;
static std::unordered_map<AssetID, SHMeshAsset> meshCollection;
static std::unordered_map<AssetID, SHDDSAsset> ddsCollection;
static std::unordered_map<AssetID, SHTextureAsset> textureCollection;
};
}