Add shader compiler (nzslc) and use it

This commit is contained in:
SirLynix
2022-04-06 09:04:09 +02:00
parent 262c82b9e1
commit 8784ec9b47
9 changed files with 331 additions and 21 deletions

View File

@@ -6,6 +6,8 @@
#include <Nazara/Graphics/GuillotineTextureAtlas.hpp>
#include <Nazara/Graphics/MaterialPipeline.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <Nazara/Shader/Ast/AstSerializer.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <Nazara/Utility/Font.hpp>
#include <array>
#include <stdexcept>
@@ -16,31 +18,31 @@ namespace Nz
namespace
{
const UInt8 r_textureBlitShader[] = {
#include <Nazara/Graphics/Resources/Shaders/TextureBlit.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/TextureBlit.nzslb.h>
};
const UInt8 r_basicMaterialShader[] = {
#include <Nazara/Graphics/Resources/Shaders/BasicMaterial.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/BasicMaterial.nzslb.h>
};
const UInt8 r_depthMaterialShader[] = {
#include <Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/DepthMaterial.nzslb.h>
};
const UInt8 r_phongMaterialShader[] = {
#include <Nazara/Graphics/Resources/Shaders/PhongMaterial.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/PhongMaterial.nzslb.h>
};
const UInt8 r_instanceDataModule[] = {
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/InstanceData.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/InstanceData.nzslb.h>
};
const UInt8 r_lightDataModule[] = {
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/LightData.nzslb.h>
};
const UInt8 r_viewerDataModule[] = {
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl.h>
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzslb.h>
};
}
@@ -259,7 +261,7 @@ namespace Nz
template<std::size_t N>
void Graphics::RegisterEmbedShaderModule(const UInt8(&content)[N])
{
m_shaderModuleResolver->RegisterModule(std::string_view(reinterpret_cast<const char*>(content), N));
m_shaderModuleResolver->RegisterModule(ShaderAst::UnserializeShader(content, N));
}
void Graphics::SelectDepthStencilFormats()

View File

@@ -4,8 +4,10 @@
#include <Nazara/Shader/FilesystemModuleResolver.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <Nazara/Shader/Ast/AstSerializer.hpp>
#include <efsw/efsw.h>
#include <cassert>
#include <Nazara/Shader/Debug.hpp>
@@ -29,9 +31,25 @@ namespace Nz
ShaderAst::ModulePtr module;
try
{
module = ShaderLang::ParseFromFile(realPath);
if (!module)
return;
std::string ext = realPath.extension().generic_u8string();
if (ext == CompiledModuleExtension)
{
Nz::File file(realPath);
if (!file.Open(Nz::OpenMode::ReadOnly | Nz::OpenMode::Text))
throw std::runtime_error("failed to open " + realPath.generic_u8string());
std::size_t length = static_cast<std::size_t>(file.GetSize());
std::vector<Nz::UInt8> content(length);
if (length > 0 && file.Read(&content[0], length) != length)
throw std::runtime_error("failed to read " + realPath.generic_u8string());
module = ShaderAst::UnserializeShader(content.data(), content.size());
}
else if (ext == ModuleExtension)
module = ShaderLang::ParseFromFile(realPath);
else
throw std::runtime_error("unknown extension " + ext);
}
catch (const std::exception& e)
{
@@ -39,6 +57,9 @@ namespace Nz
return;
}
if (!module)
return;
std::string moduleName = module->metadata->moduleName;
RegisterModule(std::move(module));
@@ -108,7 +129,7 @@ namespace Nz
for (const auto& entry : std::filesystem::recursive_directory_iterator(realPath))
{
if (entry.is_regular_file() && StringEqual(entry.path().extension().generic_u8string(), ModuleExtension, Nz::CaseIndependent{}))
if (entry.is_regular_file() && CheckExtension(entry.path().generic_u8string()))
{
try
{
@@ -133,7 +154,7 @@ namespace Nz
void FilesystemModuleResolver::OnFileAdded(std::string_view directory, std::string_view filename)
{
if (!EndsWith(filename, ModuleExtension))
if (!CheckExtension(filename))
return;
RegisterModule(std::filesystem::path(directory) / filename);
@@ -141,7 +162,7 @@ namespace Nz
void FilesystemModuleResolver::OnFileRemoved(std::string_view directory, std::string_view filename)
{
if (!EndsWith(filename, ModuleExtension))
if (!CheckExtension(filename))
return;
std::filesystem::path canonicalPath = std::filesystem::canonical(std::filesystem::path(directory) / filename);
@@ -156,7 +177,7 @@ namespace Nz
void FilesystemModuleResolver::OnFileMoved(std::string_view directory, std::string_view filename, std::string_view oldFilename)
{
if (oldFilename.empty() || !EndsWith(oldFilename, ModuleExtension))
if (oldFilename.empty() || !CheckExtension(oldFilename))
return;
std::filesystem::path canonicalPath = std::filesystem::canonical(std::filesystem::path(directory) / oldFilename);
@@ -174,9 +195,14 @@ namespace Nz
void FilesystemModuleResolver::OnFileUpdated(std::string_view directory, std::string_view filename)
{
if (!EndsWith(filename, ModuleExtension))
if (!CheckExtension(filename))
return;
RegisterModule(std::filesystem::path(directory) / filename);
}
bool FilesystemModuleResolver::CheckExtension(std::string_view filename)
{
return EndsWith(filename, ModuleExtension, Nz::CaseIndependent{}) || EndsWith(filename, CompiledModuleExtension, Nz::CaseIndependent{});
}
}