Shader/GlslWriter: Accepts mono-functions shader without specifying entry points

This commit is contained in:
Jérôme Leclercq 2021-05-24 21:08:18 +02:00
parent 7140e322c1
commit ac57b3fbf4
2 changed files with 17 additions and 18 deletions

View File

@ -58,7 +58,9 @@ namespace Nz
} }
else else
{ {
assert(!entryPoint); if (entryPoint)
throw std::runtime_error("multiple entry point functions found, this is not allowed in GLSL, please select one");
entryPoint = &node; entryPoint = &node;
} }
} }
@ -148,9 +150,16 @@ namespace Nz
targetAst->Visit(previsitor); targetAst->Visit(previsitor);
if (!previsitor.entryPoint) if (!previsitor.entryPoint)
throw std::runtime_error("missing entry point"); {
if (previsitor.forwardFunctionDeclarations.empty())
throw std::runtime_error("no function found");
state.entryFunc = previsitor.forwardFunctionDeclarations.front();
previsitor.forwardFunctionDeclarations.erase(previsitor.forwardFunctionDeclarations.begin());
}
else
state.entryFunc = previsitor.entryPoint; state.entryFunc = previsitor.entryPoint;
state.functionNames = std::move(previsitor.functionNames); state.functionNames = std::move(previsitor.functionNames);
AppendHeader(previsitor.forwardFunctionDeclarations); AppendHeader(previsitor.forwardFunctionDeclarations);

View File

@ -1,4 +1,5 @@
#include <Nazara/Core/File.hpp> #include <Nazara/Core/File.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Shader/GlslWriter.hpp> #include <Nazara/Shader/GlslWriter.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp> #include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/SpirvPrinter.hpp> #include <Nazara/Shader/SpirvPrinter.hpp>
@ -6,25 +7,14 @@
#include <Catch/catch.hpp> #include <Catch/catch.hpp>
#include <cctype> #include <cctype>
std::string_view Trim(std::string_view str)
{
while (!str.empty() && std::isspace(str.front()))
str.remove_prefix(1);
while (!str.empty() && std::isspace(str.back()))
str.remove_suffix(1);
return str;
}
void ExpectingGLSL(Nz::ShaderAst::StatementPtr& shader, std::string_view expectedOutput) void ExpectingGLSL(Nz::ShaderAst::StatementPtr& shader, std::string_view expectedOutput)
{ {
Nz::GlslWriter writer; Nz::GlslWriter writer;
std::string output = writer.Generate(shader); std::string output = writer.Generate(shader);
std::size_t funcOffset = output.find("void main()"); std::size_t funcOffset = output.find("void main()");
std::string_view subset = Trim(output).substr(funcOffset); std::string_view subset = Nz::Trim(output).substr(funcOffset);
expectedOutput = Trim(expectedOutput); expectedOutput = Nz::Trim(expectedOutput);
REQUIRE(subset == expectedOutput); REQUIRE(subset == expectedOutput);
} }
@ -42,8 +32,8 @@ void ExpectingSpirV(Nz::ShaderAst::StatementPtr& shader, std::string_view expect
std::string output = printer.Print(spirv.data(), spirv.size(), settings); std::string output = printer.Print(spirv.data(), spirv.size(), settings);
std::size_t funcOffset = output.find("OpFunction"); std::size_t funcOffset = output.find("OpFunction");
std::string_view subset = Trim(output).substr(funcOffset); std::string_view subset = Nz::Trim(output).substr(funcOffset);
expectedOutput = Trim(expectedOutput); expectedOutput = Nz::Trim(expectedOutput);
REQUIRE(subset == expectedOutput); REQUIRE(subset == expectedOutput);
} }