diff --git a/SHADE_Engine/src/Assets/Asset Types/SHDDSAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHDDSAsset.h deleted file mode 100644 index 30f22c4e..00000000 --- a/SHADE_Engine/src/Assets/Asset Types/SHDDSAsset.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "tinyddsloader.h" - -namespace SHADE -{ - struct SHDDSAsset - { - tinyddsloader::DDSFile image; - }; -} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h new file mode 100644 index 00000000..12c9cf22 --- /dev/null +++ b/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h @@ -0,0 +1,47 @@ +#pragma once + +#include "tinyddsloader.h" + +#include "Graphics/MiddleEnd/Textures/SHTextureLibrary.h" + +#include + +namespace SHADE +{ + class SHTextureAsset + { + public: + uint32_t numBytes; + uint32_t width; + uint32_t height; + SHTexture::TextureFormat format; + std::vector 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)) + //{} + }; +} diff --git a/SHADE_Engine/src/Assets/Libraries/SHDDSLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHDDSLoader.cpp deleted file mode 100644 index 32eab9a9..00000000 --- a/SHADE_Engine/src/Assets/Libraries/SHDDSLoader.cpp +++ /dev/null @@ -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()); - } - } -} diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp new file mode 100644 index 00000000..ea10aef5 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp @@ -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 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(file.GetDDSData())); + + asset.numBytes = totalBytes; + asset.width = file.GetWidth(); + asset.height = file.GetHeight(); + asset.format = static_cast(file.GetFormat()); + asset.mipOffsets = std::move(mipOff); + asset.pixelData = std::move(pixel); + } +} diff --git a/SHADE_Engine/src/Assets/Libraries/SHDDSLoader.h b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h similarity index 61% rename from SHADE_Engine/src/Assets/Libraries/SHDDSLoader.h rename to SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h index e2bd734a..393e1f02 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHDDSLoader.h +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h @@ -2,17 +2,16 @@ #define TINYDDSLOADER_IMPLEMENTATION #include "../SHAssetMacros.h" -#include "../Asset Types/SHDDSAsset.h" +#include "../Asset Types/SHTextureAsset.h" #include "tinyddsloader.h" -#include 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); }; } diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index c6d6bec1..8c462af7 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -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, diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index f2475216..6ffeda4a 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -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 SHAssetManager::assetRegistry; std::unordered_map SHAssetManager::meshCollection; - std::unordered_map SHAssetManager::ddsCollection; + std::unordered_map 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 SHAssetManager::GetAllDDS() noexcept + std::vector SHAssetManager::GetAllDDS() noexcept { - std::vector result; - for (auto const& dds : ddsCollection) + std::vector 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); } /**************************************************************************** diff --git a/SHADE_Engine/src/Assets/SHAssetManager.h b/SHADE_Engine/src/Assets/SHAssetManager.h index bf6e0dc5..0da41cc1 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.h +++ b/SHADE_Engine/src/Assets/SHAssetManager.h @@ -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 GetAllMeshes() noexcept; - static std::vector GetAllDDS() noexcept; + static std::vector GetAllDDS() noexcept; private: /**************************************************************************** @@ -128,6 +128,6 @@ namespace SHADE static std::unordered_map assetRegistry; static std::unordered_map meshCollection; - static std::unordered_map ddsCollection; + static std::unordered_map textureCollection; }; }