Shader: Add module resolver + use modules for engine shaders
This commit is contained in:
71
tests/Engine/Core/VirtualDirectoryTest.cpp
Normal file
71
tests/Engine/Core/VirtualDirectoryTest.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <Nazara/Core/VirtualDirectory.hpp>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <random>
|
||||
|
||||
TEST_CASE("VirtualDirectory", "[Core][VirtualDirectory]")
|
||||
{
|
||||
std::shared_ptr<Nz::VirtualDirectory> virtualDir = std::make_shared<Nz::VirtualDirectory>();
|
||||
|
||||
WHEN("Iterating it, it only has . and ..")
|
||||
{
|
||||
bool failed = false;
|
||||
virtualDir->Foreach([&](const std::string& name, const Nz::VirtualDirectory::Entry& /*entry*/)
|
||||
{
|
||||
if (name != "." && name != "..")
|
||||
failed = true;
|
||||
}, true);
|
||||
|
||||
CHECK_FALSE(failed);
|
||||
}
|
||||
AND_WHEN("Iterating it including dots, we get them")
|
||||
{
|
||||
bool failed = false;
|
||||
virtualDir->Foreach([&](const std::string& name, const Nz::VirtualDirectory::Entry& /*entry*/)
|
||||
{
|
||||
failed = true;
|
||||
});
|
||||
|
||||
CHECK_FALSE(failed);
|
||||
}
|
||||
AND_WHEN("We try to retrieve a file, it fails")
|
||||
{
|
||||
Nz::VirtualDirectory::Entry entry;
|
||||
CHECK_FALSE(virtualDir->GetEntry("File.bin", &entry));
|
||||
CHECK_FALSE(virtualDir->GetEntry("Foo/File.bin", &entry));
|
||||
CHECK_FALSE(virtualDir->GetEntry("Foo/Bar/File.bin", &entry));
|
||||
|
||||
virtualDir->StoreDirectory("Foo/Bar", std::make_shared<Nz::VirtualDirectory>());
|
||||
|
||||
CHECK(virtualDir->GetEntry("Foo", &entry));
|
||||
CHECK(std::holds_alternative<Nz::VirtualDirectory::VirtualDirectoryEntry>(entry));
|
||||
CHECK(virtualDir->GetEntry("Foo/Bar", &entry));
|
||||
CHECK(std::holds_alternative<Nz::VirtualDirectory::VirtualDirectoryEntry>(entry));
|
||||
CHECK_FALSE(virtualDir->GetEntry("Foo/Bar/File.bin", &entry));
|
||||
}
|
||||
|
||||
WHEN("Storing a file")
|
||||
{
|
||||
std::mt19937 randGen(std::random_device{}());
|
||||
std::vector<Nz::UInt8> randomData;
|
||||
for (std::size_t i = 0; i < 1024; ++i)
|
||||
{
|
||||
unsigned int data = randGen();
|
||||
randomData.push_back((data & 0x000000FF) >> 0);
|
||||
randomData.push_back((data & 0x0000FF00) >> 8);
|
||||
randomData.push_back((data & 0x00FF0000) >> 16);
|
||||
randomData.push_back((data & 0xFF000000) >> 24);
|
||||
}
|
||||
|
||||
virtualDir->StoreFile("File.bin", randomData);
|
||||
|
||||
WHEN("We retrieve it")
|
||||
{
|
||||
Nz::VirtualDirectory::Entry entry;
|
||||
REQUIRE(virtualDir->GetEntry("File.bin", &entry));
|
||||
|
||||
REQUIRE(std::holds_alternative<Nz::VirtualDirectory::FileContentEntry>(entry));
|
||||
const auto& contentEntry = std::get<Nz::VirtualDirectory::FileContentEntry>(entry);
|
||||
CHECK(std::equal(randomData.begin(), randomData.end(), contentEntry.data->begin(), contentEntry.data->end()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <Engine/Shader/ShaderUtils.hpp>
|
||||
#include <Nazara/Core/File.hpp>
|
||||
#include <Nazara/Core/StringExt.hpp>
|
||||
#include <Nazara/Shader/DirectoryModuleResolver.hpp>
|
||||
#include <Nazara/Shader/ShaderBuilder.hpp>
|
||||
#include <Nazara/Shader/ShaderLangParser.hpp>
|
||||
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
|
||||
@@ -29,8 +30,6 @@ struct OutputData
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::ModulePtr importedModule = Nz::ShaderLang::Parse(importedSource);
|
||||
|
||||
std::string_view shaderSource = R"(
|
||||
[nzsl_version("1.0")]
|
||||
module;
|
||||
@@ -48,14 +47,11 @@ fn main(input: InputData) -> OutputData
|
||||
|
||||
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(shaderSource);
|
||||
|
||||
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
|
||||
sanitizeOpt.moduleCallback = [&](const std::vector<std::string>& modulePath) -> Nz::ShaderAst::ModulePtr
|
||||
{
|
||||
REQUIRE(modulePath.size() == 1);
|
||||
REQUIRE(modulePath[0] == "SimpleModule");
|
||||
auto directoryModuleResolver = std::make_shared<Nz::DirectoryModuleResolver>();
|
||||
directoryModuleResolver->RegisterModuleFile("SimpleModule", importedSource.data(), importedSource.size());
|
||||
|
||||
return importedModule;
|
||||
};
|
||||
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
|
||||
sanitizeOpt.moduleResolver = directoryModuleResolver;
|
||||
|
||||
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, sanitizeOpt));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user