Shader/ShaderWrite: Add optimize bool
This commit is contained in:
parent
525f24af2e
commit
61d082e61b
|
|
@ -101,6 +101,7 @@ int main()
|
||||||
|
|
||||||
Nz::ShaderWriter::States states;
|
Nz::ShaderWriter::States states;
|
||||||
states.enabledOptions = 0;
|
states.enabledOptions = 0;
|
||||||
|
states.optimize = true;
|
||||||
|
|
||||||
auto fragVertShader = device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, shaderSource, sizeof(shaderSource), states);
|
auto fragVertShader = device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, shaderSource, sizeof(shaderSource), states);
|
||||||
if (!fragVertShader)
|
if (!fragVertShader)
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,9 @@ namespace Nz::ShaderAst
|
||||||
private:
|
private:
|
||||||
std::optional<UInt64> m_enabledOptions;
|
std::optional<UInt64> m_enabledOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline StatementPtr Optimize(const StatementPtr& ast);
|
||||||
|
inline StatementPtr Optimize(const StatementPtr& ast, UInt64 enabledConditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Shader/Ast/AstOptimizer.inl>
|
#include <Nazara/Shader/Ast/AstOptimizer.inl>
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,19 @@
|
||||||
#include <Nazara/Shader/Ast/AstOptimizer.hpp>
|
#include <Nazara/Shader/Ast/AstOptimizer.hpp>
|
||||||
#include <Nazara/Shader/Debug.hpp>
|
#include <Nazara/Shader/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz::ShaderAst
|
||||||
{
|
{
|
||||||
|
inline StatementPtr Optimize(const StatementPtr& ast)
|
||||||
|
{
|
||||||
|
AstOptimizer optimize;
|
||||||
|
return optimize.Optimise(ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline StatementPtr Optimize(const StatementPtr& ast, UInt64 enabledConditions)
|
||||||
|
{
|
||||||
|
AstOptimizer optimize;
|
||||||
|
return optimize.Optimise(ast, enabledConditions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Shader/DebugOff.hpp>
|
#include <Nazara/Shader/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ namespace Nz
|
||||||
struct States
|
struct States
|
||||||
{
|
{
|
||||||
Nz::UInt64 enabledOptions = 0;
|
Nz::UInt64 enabledOptions = 0;
|
||||||
|
bool optimize = false;
|
||||||
bool sanitized = false;
|
bool sanitized = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <Nazara/Math/Algorithm.hpp>
|
#include <Nazara/Math/Algorithm.hpp>
|
||||||
#include <Nazara/Shader/ShaderBuilder.hpp>
|
#include <Nazara/Shader/ShaderBuilder.hpp>
|
||||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||||
|
#include <Nazara/Shader/Ast/AstOptimizer.hpp>
|
||||||
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
|
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
|
||||||
#include <Nazara/Shader/Ast/AstUtils.hpp>
|
#include <Nazara/Shader/Ast/AstUtils.hpp>
|
||||||
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
||||||
|
|
@ -122,10 +123,21 @@ namespace Nz
|
||||||
|
|
||||||
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader, options);
|
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader, options);
|
||||||
|
|
||||||
|
ShaderAst::StatementPtr* targetAstPtr = &sanitizedAst;
|
||||||
|
|
||||||
|
ShaderAst::StatementPtr optimizedAst;
|
||||||
|
if (states.optimize)
|
||||||
|
{
|
||||||
|
optimizedAst = ShaderAst::Optimize(*targetAstPtr);
|
||||||
|
targetAstPtr = &optimizedAst;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderAst::StatementPtr& targetAst = *targetAstPtr;
|
||||||
|
|
||||||
PreVisitor previsitor;
|
PreVisitor previsitor;
|
||||||
previsitor.enabledOptions = states.enabledOptions;
|
previsitor.enabledOptions = states.enabledOptions;
|
||||||
previsitor.selectedStage = shaderStage;
|
previsitor.selectedStage = shaderStage;
|
||||||
sanitizedAst->Visit(previsitor);
|
targetAst->Visit(previsitor);
|
||||||
|
|
||||||
if (!previsitor.entryPoint)
|
if (!previsitor.entryPoint)
|
||||||
throw std::runtime_error("missing entry point");
|
throw std::runtime_error("missing entry point");
|
||||||
|
|
@ -167,7 +179,7 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Append(const ShaderAst::IdentifierType& identifierType)
|
void GlslWriter::Append(const ShaderAst::IdentifierType& /*identifierType*/)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("unexpected identifier type");
|
throw std::runtime_error("unexpected identifier type");
|
||||||
}
|
}
|
||||||
|
|
@ -230,7 +242,7 @@ namespace Nz
|
||||||
Append(structDesc.name);
|
Append(structDesc.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Append(const ShaderAst::UniformType& uniformType)
|
void GlslWriter::Append(const ShaderAst::UniformType& /*uniformType*/)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("unexpected UniformType");
|
throw std::runtime_error("unexpected UniformType");
|
||||||
}
|
}
|
||||||
|
|
@ -734,7 +746,7 @@ namespace Nz
|
||||||
LeaveScope();
|
LeaveScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlslWriter::Visit(ShaderAst::DeclareOptionStatement& node)
|
void GlslWriter::Visit(ShaderAst::DeclareOptionStatement& /*node*/)
|
||||||
{
|
{
|
||||||
/* nothing to do */
|
/* nothing to do */
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
#include <Nazara/Shader/SpirvData.hpp>
|
#include <Nazara/Shader/SpirvData.hpp>
|
||||||
#include <Nazara/Shader/SpirvSection.hpp>
|
#include <Nazara/Shader/SpirvSection.hpp>
|
||||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||||
|
#include <Nazara/Shader/Ast/AstOptimizer.hpp>
|
||||||
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
|
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
|
||||||
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
||||||
#include <tsl/ordered_map.h>
|
#include <tsl/ordered_map.h>
|
||||||
|
|
@ -401,11 +402,23 @@ namespace Nz
|
||||||
|
|
||||||
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
|
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
|
||||||
{
|
{
|
||||||
|
ShaderAst::StatementPtr* targetAstPtr = &shader;
|
||||||
|
|
||||||
ShaderAst::StatementPtr sanitizedAst;
|
ShaderAst::StatementPtr sanitizedAst;
|
||||||
if (!states.sanitized)
|
if (!states.sanitized)
|
||||||
|
{
|
||||||
sanitizedAst = ShaderAst::Sanitize(shader);
|
sanitizedAst = ShaderAst::Sanitize(shader);
|
||||||
|
targetAstPtr = &sanitizedAst;
|
||||||
|
}
|
||||||
|
|
||||||
ShaderAst::StatementPtr& targetAst = (states.sanitized) ? shader : sanitizedAst;
|
ShaderAst::StatementPtr optimizedAst;
|
||||||
|
if (states.optimize)
|
||||||
|
{
|
||||||
|
optimizedAst = ShaderAst::Optimize(*targetAstPtr);
|
||||||
|
targetAstPtr = &optimizedAst;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderAst::StatementPtr& targetAst = *targetAstPtr;
|
||||||
|
|
||||||
m_context.states = &states;
|
m_context.states = &states;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue