Skip to content

Commit e9fc483

Browse files
committed
Add nzsla tests
1 parent 6abea6a commit e9fc483

File tree

14 files changed

+273
-87
lines changed

14 files changed

+273
-87
lines changed
375 Bytes
Binary file not shown.
3.33 KB
Binary file not shown.
436 Bytes
Binary file not shown.
816 Bytes
Binary file not shown.
1.17 KB
Binary file not shown.

include/NZSL/Archive.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace nzsl
2626

2727
enum class ArchiveEntryFlag
2828
{
29-
CompressedLZ4HC,
29+
CompressedLZ4HC = 0,
3030

3131
Max = CompressedLZ4HC
3232
};
@@ -37,7 +37,7 @@ namespace nzsl
3737

3838
enum class ArchiveEntryKind
3939
{
40-
BinaryShaderModule
40+
BinaryShaderModule = 0
4141
};
4242

4343
class NZSL_API Archive

src/NZSL/Archive.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ namespace nzsl
132132
std::string moduleName;
133133
std::uint32_t offset;
134134
std::uint32_t size;
135+
ArchiveEntryKind kind;
135136
ArchiveEntryFlags flags;
136137
};
137138

@@ -140,12 +141,17 @@ namespace nzsl
140141
{
141142
auto& data = entries.emplace_back();
142143
deserializer.Deserialize(data.moduleName);
143-
deserializer.Deserialize(data.offset);
144-
deserializer.Deserialize(data.size);
144+
145+
std::uint32_t kind;
146+
deserializer.Deserialize(kind);
147+
data.kind = static_cast<ArchiveEntryKind>(kind);
145148

146149
std::uint32_t flags;
147150
deserializer.Deserialize(flags);
148151
data.flags = ArchiveEntryFlags(Nz::SafeCast<ArchiveEntryFlags::BitField>(flags));
152+
153+
deserializer.Deserialize(data.offset);
154+
deserializer.Deserialize(data.size);
149155
}
150156

151157
Archive archive;
@@ -155,6 +161,7 @@ namespace nzsl
155161

156162
Archive::ModuleData module;
157163
module.name = std::move(entry.moduleName);
164+
module.kind = entry.kind;
158165
module.flags = entry.flags;
159166

160167
module.data.resize(entry.size);
@@ -178,9 +185,10 @@ namespace nzsl
178185
for (const auto& module : modules)
179186
{
180187
serializer.Serialize(module.name);
188+
serializer.Serialize(std::uint32_t(module.kind));
189+
serializer.Serialize(std::uint32_t(module.flags));
181190
moduleOffsets.push_back(serializer.Serialize(std::uint32_t(0))); // reserve space
182191
serializer.Serialize(Nz::SafeCast<std::uint32_t>(module.data.size()));
183-
serializer.Serialize(std::uint32_t(module.flags));
184192
}
185193

186194
auto offsetIt = moduleOffsets.begin();

src/ShaderArchiver/Archiver.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ namespace nzsla
4949
{
5050
m_outputPath = Nz::Utf8Path(outputPath);
5151

52-
if (!std::filesystem::is_directory(m_outputPath) && !std::filesystem::create_directories(m_outputPath))
53-
throw std::runtime_error(fmt::format("failed to create {} directory", Nz::PathToString(m_outputPath)));
52+
std::filesystem::path parentPath = m_outputPath.parent_path();
53+
if (!std::filesystem::is_directory(parentPath) && !std::filesystem::create_directories(parentPath))
54+
throw std::runtime_error(fmt::format("failed to create {} directory", Nz::PathToString(parentPath)));
5455
}
5556
}
5657
}
@@ -162,6 +163,7 @@ namespace nzsla
162163

163164
void Archiver::DoShow()
164165
{
166+
bool first = true;
165167
for (const std::filesystem::path& filePath : m_inputFiles)
166168
{
167169
if (filePath.extension() != Nz::Utf8Path(".nzsla"))
@@ -171,7 +173,13 @@ namespace nzsla
171173
nzsl::Deserializer deserializer(fileContent.data(), fileContent.size());
172174

173175
nzsl::Archive archive = nzsl::DeserializeArchive(deserializer);
174-
fmt::print("archive info for {}\n", Nz::PathToString(filePath));
176+
177+
if (!first)
178+
fmt::print("---\n");
179+
180+
first = false;
181+
182+
fmt::print("archive info for {}\n\n", Nz::PathToString(filePath));
175183

176184
const auto& modules = archive.GetModules();
177185
fmt::print("{} module(s) are stored in this archive:\n", modules.size());

tests/src/Tests/NzslaTests.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <Tests/ToolUtils.hpp>
2+
#include <NazaraUtils/Algorithm.hpp>
3+
#include <NazaraUtils/CallOnExit.hpp>
4+
#include <NazaraUtils/PathUtils.hpp>
5+
#include <NZSL/Config.hpp>
6+
#include <NZSL/FilesystemModuleResolver.hpp>
7+
#include <catch2/catch_test_macros.hpp>
8+
#include <fmt/format.h>
9+
#include <filesystem>
10+
11+
TEST_CASE("Standalone archiver", "[NZSLA]")
12+
{
13+
WHEN("Printing help")
14+
{
15+
ExecuteCommand("./nzsla -h", R"(Tool for managing NZSL shader archives)");
16+
}
17+
18+
WHEN("Printing version")
19+
{
20+
ExecuteCommand("./nzsla --version", fmt::format(R"(nzsla version \d\.\d\.\d using nzsl {}\.{}\.{})", NZSL_VERSION_MAJOR, NZSL_VERSION_MINOR, NZSL_VERSION_PATCH));
21+
}
22+
23+
WHEN("Compiling shader modules")
24+
{
25+
REQUIRE(std::filesystem::is_directory("../resources/modules/Archive"));
26+
27+
auto Cleanup = []
28+
{
29+
std::filesystem::remove_all("test_files");
30+
};
31+
32+
Cleanup();
33+
34+
Nz::CallOnExit cleanupOnExit(std::move(Cleanup));
35+
36+
// Archive each modules
37+
ExecuteCommand("./nzsla --archive -o test_files/test_archive.nzsla ../resources/modules/Archive/InstanceData.nzslb ../resources/modules/Archive/LightData.nzslb ../resources/modules/Archive/SkeletalData.nzslb ../resources/modules/Archive/SkinningData.nzslb ../resources/modules/Archive/ViewerData.nzslb");
38+
ExecuteCommand("./nzsla test_files/test_archive.nzsla", {}, R"(archive info for test_files/test_archive.nzsla
39+
40+
5 module(s) are stored in this archive:
41+
module name: Engine.InstanceData
42+
- kind: BinaryShaderModule
43+
- flags:
44+
- size: 375
45+
module name: Engine.LightData
46+
- kind: BinaryShaderModule
47+
- flags:
48+
- size: 3410
49+
module name: Engine.SkeletalData
50+
- kind: BinaryShaderModule
51+
- flags:
52+
- size: 436
53+
module name: Engine.SkinningData
54+
- kind: BinaryShaderModule
55+
- flags:
56+
- size: 816
57+
module name: Engine.ViewerData
58+
- kind: BinaryShaderModule
59+
- flags:
60+
- size: 1198)");
61+
62+
// Archive and compress
63+
ExecuteCommand("./nzsla --archive --compress -o test_files/test_archive.nzsla ../resources/modules/Archive/InstanceData.nzslb ../resources/modules/Archive/LightData.nzslb ../resources/modules/Archive/SkeletalData.nzslb ../resources/modules/Archive/SkinningData.nzslb ../resources/modules/Archive/ViewerData.nzslb");
64+
ExecuteCommand("./nzsla test_files/test_archive.nzsla", {}, R"(archive info for test_files/test_archive.nzsla
65+
66+
5 module(s) are stored in this archive:
67+
module name: Engine.InstanceData
68+
- kind: BinaryShaderModule
69+
- flags: CompressedLZ4HC
70+
- size: 211
71+
module name: Engine.LightData
72+
- kind: BinaryShaderModule
73+
- flags: CompressedLZ4HC
74+
- size: 1064
75+
module name: Engine.SkeletalData
76+
- kind: BinaryShaderModule
77+
- flags: CompressedLZ4HC
78+
- size: 267
79+
module name: Engine.SkinningData
80+
- kind: BinaryShaderModule
81+
- flags: CompressedLZ4HC
82+
- size: 333
83+
module name: Engine.ViewerData
84+
- kind: BinaryShaderModule
85+
- flags: CompressedLZ4HC
86+
- size: 459)");
87+
88+
// Register each module
89+
nzsl::FilesystemModuleResolver moduleResolver;
90+
moduleResolver.RegisterFile(Nz::Utf8Path("test_files/test_archive.nzsla"));
91+
92+
CHECK(moduleResolver.Resolve("Engine.InstanceData"));
93+
CHECK(moduleResolver.Resolve("Engine.LightData"));
94+
CHECK(moduleResolver.Resolve("Engine.SkeletalData"));
95+
CHECK(moduleResolver.Resolve("Engine.SkinningData"));
96+
CHECK(moduleResolver.Resolve("Engine.ViewerData"));
97+
CHECK_FALSE(moduleResolver.Resolve("NonExistent"));
98+
}
99+
}

tests/src/Tests/NzslcTests.cpp

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <Tests/ToolUtils.hpp>
12
#include <NazaraUtils/Algorithm.hpp>
23
#include <NazaraUtils/CallOnExit.hpp>
34
#include <NZSL/Config.hpp>
@@ -11,84 +12,6 @@
1112
#include <iostream>
1213
#include <string_view>
1314

14-
void CheckHeaderMatch(const std::filesystem::path& originalFilepath)
15-
{
16-
std::ifstream originalFile(originalFilepath, std::ios::in | std::ios::binary);
17-
REQUIRE(originalFile);
18-
19-
originalFile.seekg(0, std::ios::end);
20-
21-
std::streamsize length = originalFile.tellg();
22-
REQUIRE(length > 0);
23-
if (length == 0)
24-
return; //< ignore empty files
25-
26-
originalFile.seekg(0, std::ios::beg);
27-
28-
std::vector<char> originalContent(Nz::SafeCast<std::size_t>(length));
29-
REQUIRE(originalFile.read(&originalContent[0], length));
30-
31-
std::filesystem::path headerFilepath = originalFilepath;
32-
headerFilepath.concat(".h");
33-
34-
std::ifstream headerFile(headerFilepath, std::ios::in);
35-
REQUIRE(headerFile);
36-
37-
std::vector<char> content;
38-
39-
for (std::size_t i = 0; i < originalContent.size(); ++i)
40-
{
41-
std::uint8_t referenceValue = static_cast<std::uint8_t>(originalContent[i]);
42-
43-
unsigned int value;
44-
headerFile >> value;
45-
46-
if (value != referenceValue)
47-
REQUIRE(value == referenceValue);
48-
49-
char sep;
50-
headerFile >> sep;
51-
52-
if (sep != ',')
53-
REQUIRE(sep == ',');
54-
}
55-
56-
CHECK(headerFile.eof());
57-
}
58-
59-
void ExecuteCommand(const std::string& command, const std::string& pattern = {})
60-
{
61-
std::string output;
62-
auto ReadStdout = [&](const char* str, std::size_t size)
63-
{
64-
output.append(str, size);
65-
};
66-
67-
std::string errOutput;
68-
auto ReadStderr = [&](const char* str, std::size_t size)
69-
{
70-
errOutput.append(str, size);
71-
};
72-
73-
TinyProcessLib::Process compiler(command, {}, ReadStdout, ReadStderr);
74-
int exitCode = compiler.get_exit_status();
75-
if (exitCode != 0)
76-
{
77-
INFO("Command-line: " << command << "\nstdout: " << output << "\nstderr: " << errOutput);
78-
REQUIRE(exitCode == 0);
79-
}
80-
81-
if (!pattern.empty())
82-
{
83-
INFO("Full output: " << output);
84-
// matcher doesn't like multilines, keep only the first one
85-
if (std::size_t i = output.find_first_of("\r\n"); i != output.npos)
86-
output.resize(i);
87-
88-
CHECK_THAT(output, Catch::Matchers::Matches(pattern));
89-
}
90-
}
91-
9215
TEST_CASE("Standalone compiler", "[NZSLC]")
9316
{
9417
WHEN("Printing help")

tests/src/Tests/ShaderUtils.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <glslang/Public/ShaderLang.h>
1313
#include <spirv-tools/libspirv.hpp>
1414

15-
namespace
15+
namespace NAZARA_ANONYMOUS_NAMESPACE
1616
{
1717
// Use OpenGL default minimal values (from https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glGet.xhtml, https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGet.xhtml and https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/)
1818
const TBuiltInResource s_minResources = {
@@ -218,6 +218,8 @@ namespace
218218

219219
void ExpectGLSL(nzsl::ShaderStageType stageType, const nzsl::Ast::Module& shaderModule, std::string_view expectedOutput, const nzsl::ShaderWriter::States& options, const nzsl::GlslWriter::Environment& env, bool testShaderCompilation)
220220
{
221+
NAZARA_USE_ANONYMOUS_NAMESPACE
222+
221223
std::string expectedSource = SanitizeSource(expectedOutput);
222224

223225
std::string_view stageName;
@@ -319,6 +321,8 @@ void ExpectGLSL(const nzsl::Ast::Module& shaderModule, std::string_view expected
319321

320322
void ExpectNZSL(const nzsl::Ast::Module& shaderModule, std::string_view expectedOutput, const nzsl::ShaderWriter::States& options)
321323
{
324+
NAZARA_USE_ANONYMOUS_NAMESPACE
325+
322326
std::string source = SanitizeSource(expectedOutput);
323327

324328
SECTION("Generating NZSL")
@@ -349,6 +353,8 @@ void ExpectNZSL(const nzsl::Ast::Module& shaderModule, std::string_view expected
349353

350354
void ExpectSPIRV(const nzsl::Ast::Module& shaderModule, std::string_view expectedOutput, const nzsl::ShaderWriter::States& options, const nzsl::SpirvWriter::Environment& env, bool outputParameter)
351355
{
356+
NAZARA_USE_ANONYMOUS_NAMESPACE
357+
352358
std::string source = SanitizeSource(expectedOutput);
353359

354360
SECTION("Generating SPIR-V")

0 commit comments

Comments
 (0)