Moved original shader source library

Added functions to asset manager to get all by type
This commit is contained in:
Xiao Qi 2022-10-24 04:17:51 +08:00
parent 254fc4e641
commit 8a01065641
7 changed files with 98 additions and 147 deletions

View File

@ -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<uint32_t>(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<SHShaderData> 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;

View File

@ -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<uint32_t> 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

View File

@ -56,6 +56,7 @@ enum class AssetType : AssetTypeMeta
DDS,
MAX_COUNT
};
constexpr size_t TYPE_COUNT{ static_cast<size_t>(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<size_t>(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"};

View File

@ -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<SHAssetData const*> SHAssetManager::GetAllDataOfType(AssetType type) noexcept
{
auto const toRetrieve = GetAllRecordOfType(type);
std::vector<SHAssetData const*> result;
result.reserve(toRetrieve.size());
for (auto const& get : toRetrieve)
{
result.push_back(LoadData(get));
}
return result;
}
std::vector<SHAsset> SHAssetManager::GetAllRecordOfType(AssetType type) noexcept
{
std::vector<SHAsset> 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)

View File

@ -77,6 +77,9 @@ namespace SHADE
template<typename T>
static std::enable_if_t<std::is_base_of_v<SHAssetData, T>, T const* const> GetData(AssetID id) noexcept;
static std::vector<SHAssetData const*> GetAllDataOfType(AssetType type) noexcept;
static std::vector<SHAsset> GetAllRecordOfType(AssetType type) noexcept;
private:
/****************************************************************************
* \brief Load resource data into memory

View File

@ -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<SHCamera> worldCamera;
Handle<SHCamera> screenCamera;
// TODO: Temporary only until resource library from Xiao Qi is implemented
SHShaderSourceLibrary shaderSourceLibrary;
SHShaderModuleLibrary shaderModuleLibrary;
// Temp Materials

View File

@ -1,70 +0,0 @@
#ifndef SH_SHADER_SOURCE_LIBRARY_H
#define SH_SHADER_SOURCE_LIBRARY_H
#include <map>
#include "SHShaderType.h"
#include "shaderc/shaderc.hpp"
namespace SHADE
{
struct SHShaderData
{
/*-----------------------------------------------------------------------*/
/* MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
//! container storing the spirv binary
std::vector<uint32_t> 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<SHShaderData> sourceLibrary;
//! The directory where the shaders are located.
std::string shaderDirectory;
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/
std::vector<uint32_t> 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<SHShaderData> const& GetSourceLibrary(void) const noexcept;
};
}
#endif