diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.cpp b/SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.cpp similarity index 74% rename from SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.cpp rename to SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.cpp index d9bf9ddc..8a5769d0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.cpp @@ -6,27 +6,6 @@ namespace SHADE { - /***************************************************************************/ - /*! - - \brief - Initializes the directory to take assets from. TODO: Only temporary until - the resource manager is implemented. - - \param directory - - \return - - */ - /***************************************************************************/ - void SHShaderSourceLibrary::Init (std::string directory) noexcept - { - shaderDirectory = directory; - if (shaderDirectory.back() != '/') - { - shaderDirectory += '/'; - } - } /***************************************************************************/ /*! @@ -147,15 +126,6 @@ namespace SHADE /***************************************************************************/ bool SHShaderSourceLibrary::LoadShader (uint32_t id, std::string glslFile, SH_SHADER_TYPE type, bool checkSpirvOutdated/* = true*/, bool recompileAnyway /*= false*/) noexcept { - //if (sourceLibrary.contains(id)) - //{ - // SHLOG_ERROR("Shader with ID passed in already exists. Use a different ID"); - // return false; - //} - - std::string fullGLSLPath = shaderDirectory + glslFile; - auto path = std::filesystem::path(fullGLSLPath); - if (path.extension() != ".glsl") { SHLOG_ERROR("Shader is not GLSL file, failed to load shader. "); @@ -164,16 +134,16 @@ namespace SHADE std::string spirvFilepath = path.replace_extension("spv").string(); - SHShaderData newShaderData{}; + SHShaderAsset newShaderData{}; newShaderData.shaderType = type; // spirv file std::ifstream spirvFile(spirvFilepath, std::ios::ate | std::ios::binary); // If we disable spirv validation, file is not checked - if (!recompileAnyway && - spirvFile.is_open() && - (checkSpirvOutdated ? (std::filesystem::last_write_time(spirvFilepath) > std::filesystem::last_write_time(fullGLSLPath)) : true)) + if (!recompileAnyway && + spirvFile.is_open() && + (checkSpirvOutdated ? (std::filesystem::last_write_time(spirvFilepath) > std::filesystem::last_write_time(fullGLSLPath)) : true)) { // Get file size of binary uint32_t fileSize = static_cast(spirvFile.tellg()); @@ -210,58 +180,42 @@ namespace SHADE newShaderData.name = glslFile; newShaderData.id = id; - - sourceLibrary.emplace_back(std::move (newShaderData)); + + sourceLibrary.emplace_back(std::move(newShaderData)); return true; } /***************************************************************************/ /*! - - \brief - Gets the entire source library. - - \return - The container of binary data. - - */ - /***************************************************************************/ - std::vector const& SHShaderSourceLibrary::GetSourceLibrary(void) const noexcept - { - return sourceLibrary; - } - /***************************************************************************/ - /*! - \brief - Move ctor for shader data. - + Move ctor for shader data. + \param rhs The other shader data - + */ /***************************************************************************/ - SHShaderData::SHShaderData(SHShaderData&& rhs) noexcept - : spirvBinary{ std::move (rhs.spirvBinary)} - , shaderType{std::move (rhs.shaderType)} - , name{ std::move (rhs.name)} - , id {std::move (rhs.id)} + SHShaderAsset::SHShaderAsset(SHShaderAsset&& rhs) noexcept + : spirvBinary{ std::move(rhs.spirvBinary) } + , shaderType{ std::move(rhs.shaderType) } + , name{ std::move(rhs.name) } + , id{ std::move(rhs.id) } { } /***************************************************************************/ /*! - + \brief Default ctor for shader data. Does nothing. - + */ /***************************************************************************/ - SHShaderData::SHShaderData(void) noexcept + SHShaderAsset::SHShaderAsset(void) noexcept : spirvBinary{} - , shaderType{SH_SHADER_TYPE::VERTEX} + , shaderType{ SH_SHADER_TYPE::VERTEX } , name{ } , id{ } @@ -269,16 +223,16 @@ namespace SHADE } - SHShaderData::SHShaderData(SHShaderData const& rhs) noexcept - : spirvBinary{rhs.spirvBinary} - , shaderType{ rhs.shaderType} - , name{rhs.name } - , id{rhs.id } + SHShaderAsset::SHShaderAsset(SHShaderAsset const& rhs) noexcept + : spirvBinary{ rhs.spirvBinary } + , shaderType{ rhs.shaderType } + , name{ rhs.name } + , id{ rhs.id } { } - SHShaderData& SHShaderData::operator=(SHShaderData const& rhs) noexcept + SHShaderAsset& SHShaderAsset::operator=(SHShaderAsset const& rhs) noexcept { if (this == &rhs) return *this; @@ -292,7 +246,7 @@ namespace SHADE return *this; } - SHShaderData& SHShaderData::operator=(SHShaderData&& rhs) noexcept + SHShaderAsset& SHShaderAsset::operator=(SHShaderAsset&& rhs) noexcept { if (this == &rhs) return *this; diff --git a/SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.h b/SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.h new file mode 100644 index 00000000..cb4a3453 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHShaderSourceLibrary.h @@ -0,0 +1,25 @@ +#ifndef SH_SHADER_SOURCE_LIBRARY_H +#define SH_SHADER_SOURCE_LIBRARY_H + +#include "Graphics/MiddleEnd/Shaders/SHShaderType.h" +#include "shaderc/shaderc.hpp" + +namespace SHADE +{ + class SHShaderSourceLibrary + { + private: + /*-----------------------------------------------------------------------*/ + /* PRIVATE MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + std::vector CompileToBinary(std::string const& glslSource, char const* const spirvFilename, SH_SHADER_TYPE type, shaderc_optimization_level opLevel = shaderc_optimization_level_zero); + + // TODO: Delete after file IO is implemented + std::string GetStringFromFile(char const* filePath) noexcept; + + public: + bool LoadShader (uint32_t id, std::string glslFile, SH_SHADER_TYPE type, bool checkSpirvOutdated = true, bool recompileAnyway = false) noexcept; + }; +} + +#endif diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index 0fdfa04e..cb8e1331 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -56,6 +56,7 @@ enum class AssetType : AssetTypeMeta DDS, MAX_COUNT }; +constexpr size_t TYPE_COUNT{ static_cast(AssetType::MAX_COUNT) }; //Directory #ifdef _PUBLISH @@ -70,7 +71,8 @@ constexpr std::string_view META_EXTENSION {".shmeta"}; constexpr std::string_view IMAGE_EXTENSION {".png"}; constexpr std::string_view AUDIO_EXTENSION {".ogg"}; constexpr std::string_view AUDIO_WAV_EXTENSION {".wav"}; -constexpr std::string_view SHADER_EXTENSION {".glsl"}; +constexpr std::string_view SHADER_EXTENSION{ ".glsl" }; +constexpr std::string_view SHADER_SPRV_EXTENSION {".sprv"}; constexpr std::string_view SCRIPT_EXTENSION {".cs"}; constexpr std::string_view SCENE_EXTENSION {".SHADE"}; constexpr std::string_view PREFAB_EXTENSION {".SHPrefab"}; @@ -84,6 +86,7 @@ constexpr std::string_view MESH_EXTENSION {".shmesh"}; constexpr std::string_view EXTENSIONS[] = { AUDIO_EXTENSION, SHADER_EXTENSION, + SHADER_SPRV_EXTENSION, MATERIAL_EXTENSION, IMAGE_EXTENSION, TEXTURE_EXTENSION, @@ -97,7 +100,18 @@ constexpr std::string_view EXTENSIONS[] = { GLTF_EXTENSION }; -constexpr size_t TYPE_COUNT {static_cast(AssetType::MAX_COUNT) }; +// SHADER IDENTIFIERS +constexpr std::string_view VERTEX_SHADER{ "_VS" }; +constexpr std::string_view FRAGMENT_SHADER{ "_FS" }; +constexpr std::string_view COMPUTER_SHADER{ "_CS" }; + +constexpr std::string_view SHADER_IDENTIFIERS[] = { + VERTEX_SHADER, + FRAGMENT_SHADER, + COMPUTER_SHADER +}; + +constexpr size_t SHADER_TYPE_MAX_COUNT{ 3 }; // Error flags constexpr std::string_view FILE_NOT_FOUND_ERR {"FILE NOT FOUND"}; diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index b7d7268f..9198666c 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -17,6 +17,7 @@ #include "Libraries/SHAssimpLibrary.h" #include "Libraries/SHMeshLoader.h" #include "Libraries/SHTextureLoader.h" +#include "Libraries/SHShaderSourceLoader.h" #include "Libraries/SHMeshCompiler.h" #include "Libraries/SHTextureCompiler.h" @@ -206,6 +207,33 @@ namespace SHADE } + std::vector SHAssetManager::GetAllDataOfType(AssetType type) noexcept + { + auto const toRetrieve = GetAllRecordOfType(type); + std::vector result; + result.reserve(toRetrieve.size()); + for (auto const& get : toRetrieve) + { + result.push_back(LoadData(get)); + } + + return result; + } + + std::vector SHAssetManager::GetAllRecordOfType(AssetType type) noexcept + { + std::vector result; + for (auto const& asset : assetCollection) + { + if (asset.type == type) + { + result.push_back(asset); + } + } + + return result; + } + bool SHAssetManager::IsRecognised(char const* ext) noexcept { for (auto const& e : EXTENSIONS) diff --git a/SHADE_Engine/src/Assets/SHAssetManager.h b/SHADE_Engine/src/Assets/SHAssetManager.h index 9ee7ab92..fedebfba 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.h +++ b/SHADE_Engine/src/Assets/SHAssetManager.h @@ -77,6 +77,9 @@ namespace SHADE template static std::enable_if_t, T const* const> GetData(AssetID id) noexcept; + + static std::vector GetAllDataOfType(AssetType type) noexcept; + static std::vector GetAllRecordOfType(AssetType type) noexcept; private: /**************************************************************************** * \brief Load resource data into memory diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 870325ac..35bf2c5b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -25,7 +25,6 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/System/SHSystemRoutine.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Graphics/RenderGraph/SHRenderGraph.h" -#include "Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.h" #include "Graphics/MiddleEnd/Shaders/SHShaderModuleLibrary.h" #include "SHMeshLibrary.h" #include "Graphics/MiddleEnd/Materials/SHMaterialInstanceCache.h" @@ -338,9 +337,7 @@ namespace SHADE // Temp Cameras Handle worldCamera; Handle screenCamera; - - // TODO: Temporary only until resource library from Xiao Qi is implemented - SHShaderSourceLibrary shaderSourceLibrary; + SHShaderModuleLibrary shaderModuleLibrary; // Temp Materials diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.h deleted file mode 100644 index bb346111..00000000 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef SH_SHADER_SOURCE_LIBRARY_H -#define SH_SHADER_SOURCE_LIBRARY_H - -#include -#include "SHShaderType.h" -#include "shaderc/shaderc.hpp" - -namespace SHADE -{ - struct SHShaderData - { - /*-----------------------------------------------------------------------*/ - /* MEMBER VARIABLES */ - /*-----------------------------------------------------------------------*/ - //! container storing the spirv binary - std::vector spirvBinary; - - //! For the compilation of the shader. Vulkan backend will use it too - SH_SHADER_TYPE shaderType; - - //! Name of the shader file (without parent path) - std::string name; - - //! id of the shader - uint32_t id; - - SHShaderData(void) noexcept; - SHShaderData(SHShaderData const& rhs) noexcept; - SHShaderData(SHShaderData&& rhs) noexcept; - SHShaderData& operator= (SHShaderData&& rhs) noexcept; - SHShaderData& operator= (SHShaderData const& rhs) noexcept; - }; - - // TODO: This class is purely temporary and will be converted/changed when XQ implements his resource manager - class SHShaderSourceLibrary - { - private: - /*-----------------------------------------------------------------------*/ - /* PRIVATE MEMBER VARIABLES */ - /*-----------------------------------------------------------------------*/ - //! Stores all the source data. Take note that the source here is GLSL source and NOT binary data. - //! Binary data gets passed to the backend to convert to spirv. - std::vector sourceLibrary; - - //! The directory where the shaders are located. - std::string shaderDirectory; - - /*-----------------------------------------------------------------------*/ - /* PRIVATE MEMBER FUNCTIONS */ - /*-----------------------------------------------------------------------*/ - std::vector CompileToBinary(std::string const& glslSource, char const* const spirvFilename, SH_SHADER_TYPE type, shaderc_optimization_level opLevel = shaderc_optimization_level_zero); - - // TODO: Delete after file IO is implemented - std::string GetStringFromFile(char const* filePath) noexcept; - - public: - /*-----------------------------------------------------------------------*/ - /* PUBLIC MEMBER FUNCTIONS */ - /*-----------------------------------------------------------------------*/ - void Init (std::string directory) noexcept; - bool LoadShader (uint32_t id, std::string glslFile, SH_SHADER_TYPE type, bool checkSpirvOutdated = true, bool recompileAnyway = false) noexcept; - - /*-----------------------------------------------------------------------*/ - /* SETTERS AND GETTERS */ - /*-----------------------------------------------------------------------*/ - std::vector const& GetSourceLibrary(void) const noexcept; - }; -} - -#endif