SP3-282 Restructured loaders and compilers

Completed shader compiling and loading
This commit is contained in:
Xiao Qi 2022-10-27 12:54:58 +08:00
parent 5154b579d7
commit b5a1d5ea88
34 changed files with 212 additions and 91 deletions

View File

@ -0,0 +1,3 @@
Name: Cube.003
ID: 71245919
Type: 4

View File

@ -0,0 +1,3 @@
Name: Cube.012
ID: 80365422
Type: 4

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: RaccoonBag_Color_Ver4
ID: 58303057
Type: 3

View File

@ -0,0 +1,3 @@
Name: RaccoonPreTexturedVer1_Base9
ID: 64651793
Type: 3

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: Kirsch_CS
ID: 19931255
Type: 2

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: PureCopy_CS
ID: 29659779
Type: 2

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: TestCube_FS
ID: 18415057
Type: 2

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: TestCube_VS
ID: 29315909
Type: 2

Binary file not shown.

View File

@ -0,0 +1,3 @@
Name: TD_Checker_Base_Color
ID: 51995224
Type: 3

View File

@ -110,14 +110,7 @@ namespace Sandbox
SHComponentManager::CreateComponentSparseSet<SHRenderable>(); SHComponentManager::CreateComponentSparseSet<SHRenderable>();
SHComponentManager::CreateComponentSparseSet<SHCameraComponent>(); SHComponentManager::CreateComponentSparseSet<SHCameraComponent>();
//TODO: REMOVE AFTER PRESENTATION SHAssetManager::Load();
//SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf");
//SHAssetManager::LoadDataTemp("../../Assets/Cube.012.shmesh");
//SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds");
//SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds");
//SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.shtex");
//TODO: REMOVE AFTER PRESENTATION
auto id = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>(); auto id = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
auto id2 = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>(); auto id2 = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
@ -133,8 +126,6 @@ namespace Sandbox
SHSceneManager::InitSceneManager<SBTestScene>("TestScene"); SHSceneManager::InitSceneManager<SBTestScene>("TestScene");
SHFrameRateController::UpdateFRC(); SHFrameRateController::UpdateFRC();
SHAssetManager::Load();
} }
void SBApplication::Update(void) void SBApplication::Update(void)

View File

@ -54,6 +54,7 @@ namespace Sandbox
handles.emplace_back(SHResourceManager::LoadOrGet<SHMesh>(asset.id)); handles.emplace_back(SHResourceManager::LoadOrGet<SHMesh>(asset.id));
break; break;
case AssetType::TEXTURE: case AssetType::TEXTURE:
if (asset.name == "RaccoonPreTexturedVer1_Base9")
texHandles.emplace_back(SHResourceManager::LoadOrGet<SHTexture>(asset.id)); texHandles.emplace_back(SHResourceManager::LoadOrGet<SHTexture>(asset.id));
break; break;
} }

View File

@ -0,0 +1,61 @@
/*************************************************************************//**
* \file SHShaderAsset.cpp
* \author Brandon Mak
* \date 24 October 2022
* \brief
*
* Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
*****************************************************************************/
#include "SHpch.h"
#include "SHShaderAsset.h"
namespace SHADE
{
SHShaderAsset::SHShaderAsset() noexcept
: spirvBinary{},
shaderType{SH_SHADER_TYPE::VERTEX},
name{}
{
}
SHShaderAsset::SHShaderAsset(SHShaderAsset const& rhs) noexcept
: spirvBinary{rhs.spirvBinary},
shaderType{ rhs.shaderType },
name{rhs.name}
{
}
SHShaderAsset::SHShaderAsset(SHShaderAsset&& rhs) noexcept
: spirvBinary{ std::move(rhs.spirvBinary) },
shaderType{ std::move(rhs.shaderType) },
name{ std::move(rhs.name) }
{
}
SHShaderAsset& SHShaderAsset::operator=(SHShaderAsset&& rhs) noexcept
{
if (this == &rhs)
{
return *this;
}
spirvBinary = std::move(rhs.spirvBinary);
shaderType = std::move(rhs.shaderType);
name = std::move(rhs.name);
}
SHShaderAsset& SHShaderAsset::operator=(SHShaderAsset const& rhs) noexcept
{
if (this == &rhs)
{
return *this;
}
spirvBinary = rhs.spirvBinary;
shaderType = rhs.shaderType;
name = rhs.name;
}
}

View File

@ -13,6 +13,7 @@
#include "SHAssetData.h" #include "SHAssetData.h"
#include "SH_API.h" #include "SH_API.h"
#include <vector> #include <vector>
#include <string>
namespace SHADE namespace SHADE
{ {
@ -38,10 +39,7 @@ namespace SHADE
//! Name of the shader file (without parent path) //! Name of the shader file (without parent path)
std::string name; std::string name;
//! id of the shader SHShaderAsset() noexcept;
uint32_t id;
SHShaderAsset(void) noexcept;
SHShaderAsset(SHShaderAsset const& rhs) noexcept; SHShaderAsset(SHShaderAsset const& rhs) noexcept;
SHShaderAsset(SHShaderAsset&& rhs) noexcept; SHShaderAsset(SHShaderAsset&& rhs) noexcept;
SHShaderAsset& operator= (SHShaderAsset&& rhs) noexcept; SHShaderAsset& operator= (SHShaderAsset&& rhs) noexcept;

View File

@ -138,14 +138,14 @@ namespace SHADE
return; return;
} }
ExtractAnimations(*scene, anims); //ExtractAnimations(*scene, anims);
ProcessNode(*scene->mRootNode, *scene, meshes); ProcessNode(*scene->mRootNode, *scene, meshes);
aiImporter.FreeScene(); aiImporter.FreeScene();
} }
std::string SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept std::optional<AssetPath> SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept
{ {
std::string newPath{ path.parent_path().string() + '/' }; std::string newPath{ path.parent_path().string() + '/' };
newPath += asset.header.name + MESH_EXTENSION.data(); newPath += asset.header.name + MESH_EXTENSION.data();

View File

@ -35,6 +35,6 @@ namespace SHADE
public: public:
static void LoadFromFile(AssetPath path, MeshVectorRef meshes, AnimVectorRef anims) noexcept; static void LoadFromFile(AssetPath path, MeshVectorRef meshes, AnimVectorRef anims) noexcept;
static std::string CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept; static std::optional<AssetPath> CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept;
}; };
} }

View File

@ -21,7 +21,9 @@ namespace SHADE
{ {
std::string SHShaderSourceCompiler::CompileShaderSourceToBinary(AssetPath path, SHShaderAsset const& data) noexcept std::string SHShaderSourceCompiler::CompileShaderSourceToBinary(AssetPath path, SHShaderAsset const& data) noexcept
{ {
std::string newPath{ path.string() + SHADER_EXTENSION.data() }; std::string newPath{ path.string() };
newPath = newPath.substr(0, newPath.find_last_of('.'));
newPath += SHADER_EXTENSION.data();
std::ofstream file{ newPath, std::ios::binary | std::ios::out | std::ios::trunc }; std::ofstream file{ newPath, std::ios::binary | std::ios::out | std::ios::trunc };
@ -85,10 +87,13 @@ namespace SHADE
} }
auto result = new SHShaderAsset(); auto result = new SHShaderAsset();
result->spirvBinary.reserve(compileResult.end() - compileResult.begin()); result->spirvBinary.resize(compileResult.end() - compileResult.begin());
std::ranges::copy(compileResult.begin(), compileResult.end(), result->spirvBinary.data()); std::ranges::copy(compileResult.begin(), compileResult.end(), result->spirvBinary.data());
result->name = name;
result->shaderType = type;
return result; return result;
} }
@ -105,7 +110,7 @@ namespace SHADE
return SH_SHADER_TYPE::INAVLID_TYPE; return SH_SHADER_TYPE::INAVLID_TYPE;
} }
std::optional<std::string> SHShaderSourceCompiler::LoadAndCompileShader(AssetPath path) noexcept std::optional<AssetPath> SHShaderSourceCompiler::LoadAndCompileShader(AssetPath path) noexcept
{ {
auto type = GetShaderTypeFromFilename(path.filename().string()); auto type = GetShaderTypeFromFilename(path.filename().string());
@ -115,6 +120,8 @@ namespace SHADE
return {}; return {};
} }
path.make_preferred();
std::ifstream file{ path.string(), std::ios::in }; std::ifstream file{ path.string(), std::ios::in };
if (file.is_open()) if (file.is_open())
@ -140,7 +147,7 @@ namespace SHADE
return {}; return {};
} }
std::optional<std::string> SHShaderSourceCompiler::CompileShaderFromString std::optional<AssetPath> SHShaderSourceCompiler::CompileShaderFromString
(std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept (std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept
{ {
auto const data = CompileShaderSourceToMemory(string, path.filename().string(), type); auto const data = CompileShaderSourceToMemory(string, path.filename().string(), type);

View File

@ -24,8 +24,8 @@ namespace SHADE
static SH_SHADER_TYPE GetShaderTypeFromFilename(std::string name) noexcept; static SH_SHADER_TYPE GetShaderTypeFromFilename(std::string name) noexcept;
public: public:
static std::optional<std::string> LoadAndCompileShader(AssetPath path) noexcept; static std::optional<AssetPath> LoadAndCompileShader(AssetPath path) noexcept;
static std::optional<std::string> CompileShaderFromString static std::optional<AssetPath> CompileShaderFromString
(std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept; (std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept;
}; };
} }

View File

@ -107,63 +107,65 @@ namespace SHADE
asset.pixelData = std::move(pixel); asset.pixelData = std::move(pixel);
} }
void SHTextureCompiler::LoadImageAsset(AssetPath path, SHTextureAsset& image) std::string SHTextureCompiler::WriteToFile(SHTextureAsset const& asset, AssetPath path) noexcept
{ {
LoadTinyDDS(path, image); std::string newPath{ path.string() };
newPath = newPath.substr(0, newPath.find_last_of('.'));
newPath += TEXTURE_EXTENSION;
std::ofstream file{ newPath, std::ios::out | std::ios::binary };
if (!file.is_open())
{
SHLOG_ERROR("Unable to open file for writing texture file: {}", path.string());
}
constexpr auto intBytes{ sizeof(uint32_t) };
uint32_t const mipOffsetCount{ static_cast<uint32_t>(asset.mipOffsets.size()) };
file.write(
reinterpret_cast<char const*>(&asset.numBytes),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.width),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.height),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.format),
sizeof(SHTexture::TextureFormat)
);
file.write(
reinterpret_cast<char const*>(&mipOffsetCount),
intBytes
);
file.write(
reinterpret_cast<char const*>(asset.mipOffsets.data()),
intBytes * asset.mipOffsets.size()
);
file.write(
reinterpret_cast<char const*>(asset.pixelData),
asset.numBytes
);
file.close();
return newPath;
} }
std::string SHTextureCompiler::CompileTextureBinary(SHTextureAsset const& asset, AssetPath path)
std::optional<AssetPath> SHTextureCompiler::CompileTextureAsset(AssetPath path)
{ {
std::string newPath{ path.string() }; auto data = new SHTextureAsset();
newPath = newPath.substr(0, newPath.find_last_of('.')); LoadTinyDDS(path, *data);
newPath += TEXTURE_EXTENSION; return WriteToFile(*data, path);
std::ofstream file{ newPath, std::ios::out | std::ios::binary };
if (!file.is_open())
{
SHLOG_ERROR("Unable to open file for writing texture file: {}", path.string());
}
auto const intBytes{sizeof(uint32_t)};
uint32_t mipOffsetCount{ static_cast<uint32_t>(asset.mipOffsets.size()) };
file.write(
reinterpret_cast<char const*>(&asset.numBytes),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.width),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.height),
intBytes
);
file.write(
reinterpret_cast<char const*>(&asset.format),
sizeof(SHTexture::TextureFormat)
);
file.write(
reinterpret_cast<char const*>(&mipOffsetCount),
intBytes
);
file.write(
reinterpret_cast<char const*>(asset.mipOffsets.data()),
intBytes * asset.mipOffsets.size()
);
file.write(
reinterpret_cast<char const*>(asset.pixelData),
asset.numBytes
);
file.close();
return newPath;
} }
} }

View File

@ -25,8 +25,9 @@ namespace SHADE
static std::string TinyDDSResultToString(tinyddsloader::Result value); static std::string TinyDDSResultToString(tinyddsloader::Result value);
static vk::Format ddsLoaderToVkFormat(tinyddsloader::DDSFile::DXGIFormat format, bool isLinear); static vk::Format ddsLoaderToVkFormat(tinyddsloader::DDSFile::DXGIFormat format, bool isLinear);
static void LoadTinyDDS(AssetPath path, SHTextureAsset& asset) noexcept; static void LoadTinyDDS(AssetPath path, SHTextureAsset& asset) noexcept;
static std::string WriteToFile(SHTextureAsset const& asset, AssetPath path) noexcept;
public: public:
static void LoadImageAsset(AssetPath paths, SHTextureAsset& image); static std::optional<AssetPath> CompileTextureAsset(AssetPath path);
static std::string CompileTextureBinary(SHTextureAsset const& asset, AssetPath path);
}; };
} }

View File

@ -19,7 +19,9 @@ namespace SHADE
{ {
SHAssetData* SHShaderSourceLoader::Load(AssetPath path) SHAssetData* SHShaderSourceLoader::Load(AssetPath path)
{ {
SHShaderAsset* result = new SHShaderAsset(); auto result = new SHShaderAsset();
result->name = path.stem().stem().string();
std::ifstream file{ path.string(), std::ios::in | std::ios::binary }; std::ifstream file{ path.string(), std::ios::in | std::ios::binary };
if (!file.is_open()) if (!file.is_open())
@ -32,6 +34,9 @@ namespace SHADE
file.read(reinterpret_cast<char*>(&result->shaderType), sizeof(uint8_t)); file.read(reinterpret_cast<char*>(&result->shaderType), sizeof(uint8_t));
file.read(reinterpret_cast<char*>(&byteCount), sizeof(size_t)); file.read(reinterpret_cast<char*>(&byteCount), sizeof(size_t));
result->spirvBinary.resize(byteCount / sizeof(uint32_t));
file.read(reinterpret_cast<char*>(result->spirvBinary.data()), byteCount); file.read(reinterpret_cast<char*>(result->spirvBinary.data()), byteCount);
file.close(); file.close();

View File

@ -15,7 +15,7 @@
namespace SHADE namespace SHADE
{ {
struct SHShaderSourceLoader final : SHAssetLoader struct SHShaderSourceLoader : SHAssetLoader
{ {
SHAssetData* Load(AssetPath path) override; SHAssetData* Load(AssetPath path) override;
}; };

View File

@ -292,26 +292,48 @@ namespace SHADE
for (auto const& path : paths) for (auto const& path : paths)
{ {
SHAsset newAsset
{
.name = path.stem().string(),
.location = 0
};
auto const ext{ path.extension().string() }; auto const ext{ path.extension().string() };
if (ext == GLSL_EXTENSION.data()) if (ext == GLSL_EXTENSION.data())
{ {
SHShaderSourceCompiler::LoadAndCompileShader(path); newAsset.path = SHShaderSourceCompiler::LoadAndCompileShader(path).value();
newAsset.id = GenerateAssetID(AssetType::SHADER);
newAsset.type = AssetType::SHADER;
} }
else if (ext == DDS_EXTENSION.data()) else if (ext == DDS_EXTENSION.data())
{ {
auto image = new SHTextureAsset(); newAsset.path = SHTextureCompiler::CompileTextureAsset(path).value();
SHTextureCompiler::LoadImageAsset(path, *image); newAsset.id = GenerateAssetID(AssetType::TEXTURE);
newAsset.type = AssetType::TEXTURE;
} }
else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data()) else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data())
{ {
std::vector<SHMeshAsset*> meshes; std::vector<SHMeshAsset*> meshes;
std::vector<SHAnimationAsset*> anims; std::vector<SHAnimationAsset*> anims;
SHMeshCompiler::LoadFromFile(path, meshes, anims); SHMeshCompiler::LoadFromFile(path, meshes, anims);
for (auto const& mesh : meshes) for (auto const& mesh : meshes)
{ {
SHMeshCompiler::CompileMeshBinary(*mesh, path); SHAsset meshAsset{
.name = mesh->header.name,
.location = 0
};
meshAsset.path = SHMeshCompiler::CompileMeshBinary(*mesh, path).value();
meshAsset.id = GenerateAssetID(AssetType::MESH);
meshAsset.type = AssetType::MESH;
assetCollection.push_back(meshAsset);
SHAssetMetaHandler::WriteMetaData(meshAsset);
} }
continue;
} }
assetCollection.push_back(newAsset);
SHAssetMetaHandler::WriteMetaData(newAsset);
} }
} }
@ -328,6 +350,7 @@ namespace SHADE
****************************************************************************/ ****************************************************************************/
void SHAssetManager::Load() noexcept void SHAssetManager::Load() noexcept
{ {
//CompileAll();
InitLoaders(); InitLoaders();
BuildAssetCollection(); BuildAssetCollection();
//LoadAllData(); //LoadAllData();

View File

@ -174,11 +174,11 @@ namespace SHADE
gBufferSubpass->AddDepthOutput("Depth Buffer", SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL); gBufferSubpass->AddDepthOutput("Depth Buffer", SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL);
//// kirsch //// kirsch
//auto kirschShader = shaderModuleLibrary.GetShaderModule("KirschCs.glsl"); //auto kirschShader = shaderModuleLibrary.GetShaderModule("KirschCs");
//gBufferNode->AddNodeCompute(kirschShader, { "Scene Pre-Process", "Scene" }); //gBufferNode->AddNodeCompute(kirschShader, { "Scene Pre-Process", "Scene" });
// copy // copy
auto pureCopyShader = shaderModuleLibrary.GetShaderModule("PureCopyCs.glsl"); auto pureCopyShader = shaderModuleLibrary.GetBuiltInShaderModule("PureCopy_CS");
gBufferNode->AddNodeCompute(pureCopyShader, { "Scene Pre-Process", "Scene" }); gBufferNode->AddNodeCompute(pureCopyShader, { "Scene Pre-Process", "Scene" });
@ -196,8 +196,8 @@ namespace SHADE
worldRenderer->SetCameraDirector(cameraSystem->CreateDirector()); worldRenderer->SetCameraDirector(cameraSystem->CreateDirector());
auto cubeVS = shaderModuleLibrary.GetBuiltInShaderModule("TestCubeVs.glsl"); auto cubeVS = shaderModuleLibrary.GetBuiltInShaderModule("TestCube_VS");
auto cubeFS = shaderModuleLibrary.GetBuiltInShaderModule("TestCubeFs.glsl"); auto cubeFS = shaderModuleLibrary.GetBuiltInShaderModule("TestCube_FS");
defaultMaterial = AddMaterial(cubeVS, cubeFS, gBufferSubpass); defaultMaterial = AddMaterial(cubeVS, cubeFS, gBufferSubpass);

View File

@ -92,7 +92,7 @@ namespace SHADE
Handle<SHVkShaderModule> SHShaderModuleLibrary::GetBuiltInShaderModule(std::string shaderName) const noexcept Handle<SHVkShaderModule> SHShaderModuleLibrary::GetBuiltInShaderModule(std::string shaderName) const noexcept
{ {
if (builtInShaderModules.contains(shaderName)) if (builtInShaderModules.contains(shaderName))
return shaderModules.at(builtInShaderModules.at(shaderName)); return builtInShaderModules.at(shaderName);
else else
return {}; return {};
} }
@ -130,5 +130,10 @@ namespace SHADE
{ {
module.second->Reflect(); module.second->Reflect();
} }
for (auto& module : builtInShaderModules)
{
module.second->Reflect();
}
} }
} }