Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bin/resources/Reflection.nzsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[nzsl_version("1.1")]
[author("SirLynix"), desc("Test module")]
[license("MIT")]
module Shader;

import * from DataStruct;

[layout(std140)]
struct Output
{
color: vec4[f32],
normal: vec3[f32],
roughness: f32,
metalness: f32
}
30 changes: 30 additions & 0 deletions bin/resources/Reflection.nzsl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"structs": [
{
"name": "Output",
"layout": "std140",
"members": [
{
"name": "color",
"type": "vec4[f32]",
"offset": 0
},
{
"name": "normal",
"type": "vec3[f32]",
"offset": 16
},
{
"name": "roughness",
"type": "f32",
"offset": 28
},
{
"name": "metalness",
"type": "f32",
"offset": 32
}
]
}
]
}
124 changes: 120 additions & 4 deletions src/ShaderCompiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <NZSL/Lang/Errors.hpp>
#include <NZSL/Lexer.hpp>
#include <NZSL/Parser.hpp>
#include <NZSL/Math/FieldOffsets.hpp>
#include <NZSL/SpirV/SpirvPrinter.hpp>
#include <NZSL/SpirvWriter.hpp>
#include <NZSL/Serializer.hpp>
Expand All @@ -30,6 +31,7 @@
#include <chrono>
#include <fstream>
#include <stdexcept>
#include <unordered_set>

namespace nzslc
{
Expand Down Expand Up @@ -201,6 +203,9 @@

if (m_options.count("compile") > 0)
Step("Compiling"sv, __LINE__, &Compiler::Compile);

if (m_options.count("reflect") > 0)
Step("Reflecting"sv, __LINE__, &Compiler::Reflect);

Check warning on line 208 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "line" member function of "std::source_location" instead of "__LINE__" macro.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyCivmQT7csWqFkn3&open=AZ5lyCivmQT7csWqFkn3&pullRequest=94
});
}

Expand Down Expand Up @@ -240,6 +245,9 @@
("p,partial", "Allow partial compilation")
("skip-unchanged", "After compilation, compare the output with the current output file and skip writing if the content is the same", cxxopts::value<bool>()->default_value("false"));

options.add_options("reflection")
("r,reflect", "Outputs informations about a struct as a json", cxxopts::value<std::vector<std::string>>());

options.add_options("glsl output")
("gl-es", "Generate GLSL ES instead of GLSL", cxxopts::value<bool>()->default_value("false"))
("gl-version", "OpenGL version (310 being 3.1)", cxxopts::value<std::uint32_t>(), "version")
Expand Down Expand Up @@ -690,12 +698,120 @@
throw std::runtime_error(fmt::format("{} has unknown extension \"{}\"", Nz::PathToString(m_inputFilePath.filename()), Nz::PathToString(extension)));
}

void Compiler::Reflect()

Check failure on line 701 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 28 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyIDENZwKksNFc5Yu&open=AZ5lyIDENZwKksNFc5Yu&pullRequest=94
{
m_outputHeader = false;

// if no output path has been provided, output in the same folder as the input file
std::filesystem::path outputFilePath = m_outputPath;
if (outputFilePath.empty())
outputFilePath = m_inputFilePath.parent_path();

Check warning on line 708 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L708

Added line #L708 was not covered by tests

outputFilePath /= m_inputFilePath.filename();
outputFilePath += Nz::Utf8Path(".json");

const std::vector<std::string>& reflectTypes = m_options["reflect"].as<std::vector<std::string>>();

std::unordered_set<std::string> remainingStructs(reflectTypes.begin(), reflectTypes.end());

Check warning on line 715 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the transparent equality "std::equal_to<>" and a custom transparent heterogeneous hasher with this associative string container.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyCivmQT7csWqFkn4&open=AZ5lyCivmQT7csWqFkn4&pullRequest=94

nlohmann::ordered_json structArray = nlohmann::ordered_json::array();

nzsl::Ast::ReflectVisitor::Callbacks callbacks;
callbacks.onStructDeclaration = [&](const nzsl::Ast::DeclareStructStatement& structDecl)

Check warning on line 720 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This lambda has 53 lines, which is greater than the 20 lines authorized. Split it into several lambdas or functions, or make it a named function.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyCivmQT7csWqFkn5&open=AZ5lyCivmQT7csWqFkn5&pullRequest=94
{
auto it = remainingStructs.find(structDecl.description.name);
if (it == remainingStructs.end())
return;

Check warning on line 724 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L724

Added line #L724 was not covered by tests

remainingStructs.erase(it);

nlohmann::ordered_json structDoc;
structDoc["name"] = structDecl.description.name;
if (!structDecl.description.tag.empty())
structDoc["tag"] = structDecl.description.tag;

Check warning on line 731 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L731

Added line #L731 was not covered by tests

nlohmann::ordered_json structMemberArray = nlohmann::ordered_json::array();

std::optional<nzsl::FieldOffsets> fieldOffsets;
if (structDecl.description.layout.IsResultingValue())
{
switch (structDecl.description.layout.GetResultingValue())
{
case nzsl::Ast::MemoryLayout::Scalar:
structDoc["layout"] = "scalar";
fieldOffsets.emplace(nzsl::StructLayout::Scalar);
break;

Check warning on line 743 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L740-L743

Added lines #L740 - L743 were not covered by tests

case nzsl::Ast::MemoryLayout::Std140:
structDoc["layout"] = "std140";
fieldOffsets.emplace(nzsl::StructLayout::Std140);
break;

case nzsl::Ast::MemoryLayout::Std430:
structDoc["layout"] = "std430";
fieldOffsets.emplace(nzsl::StructLayout::Std430);
break;

Check warning on line 753 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L750-L753

Added lines #L750 - L753 were not covered by tests
}
}
else if (structDecl.description.layout.IsExpression())
structDoc["layout"] = "unresolved";

Check warning on line 757 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L756-L757

Added lines #L756 - L757 were not covered by tests

for (const auto& member : structDecl.description.members)
{
nlohmann::ordered_json memberDoc;
memberDoc["name"] = member.name;

if (member.type.IsResultingValue())
{
memberDoc["type"] = nzsl::Ast::ToString(member.type.GetResultingValue());
if (fieldOffsets)
memberDoc["offset"] = nzsl::Ast::RegisterStructField(*fieldOffsets, member.type.GetResultingValue());
}
else if (member.type.IsExpression())
memberDoc["type"] = "unresolved";

Check warning on line 771 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L770-L771

Added lines #L770 - L771 were not covered by tests

if (member.locationIndex.IsResultingValue())
memberDoc["location"] = member.locationIndex.GetResultingValue();

Check warning on line 774 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L774

Added line #L774 was not covered by tests
else if (member.locationIndex.IsExpression())
memberDoc["location"] = "unresolved";

Check warning on line 776 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L776

Added line #L776 was not covered by tests

structMemberArray.push_back(std::move(memberDoc));
}

structDoc["members"] = std::move(structMemberArray);

structArray.push_back(std::move(structDoc));

// TODO: Stop visit if remainingStructs.empty()

Check warning on line 785 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyIDENZwKksNFc5Yt&open=AZ5lyIDENZwKksNFc5Yt&pullRequest=94
};

nzsl::Ast::ReflectVisitor reflectVisitor;
reflectVisitor.Reflect(*m_shaderModule, callbacks);

if (!remainingStructs.empty())
throw std::runtime_error(fmt::format("struct \"{}\" was not found", *remainingStructs.begin()));

Check warning on line 792 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define and throw a dedicated exception instead of using a generic one.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyIDENZwKksNFc5Yv&open=AZ5lyIDENZwKksNFc5Yv&pullRequest=94

Check warning on line 792 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L792

Added line #L792 was not covered by tests

nlohmann::ordered_json result;
result["structs"] = std::move(structArray);

if (m_skipOutput)
return;

Check warning on line 798 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L798

Added line #L798 was not covered by tests

if (m_outputToStdout)
{
OutputToStdout(result.dump(1, '\t'));
return;

Check warning on line 803 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

Codecov / codecov/patch

src/ShaderCompiler/Compiler.cpp#L802-L803

Added lines #L802 - L803 were not covered by tests
}

std::string output = result.dump(1, '\t');
OutputFile(std::move(outputFilePath), output.data(), output.size());
}

void Compiler::Resolve()
{
using namespace std::literals;

nzsl::Ast::TransformerContext context;
context.partialCompilation = m_options.count("partial") > 0;
m_transformerContext.partialCompilation = m_options.count("partial") > 0;

nzsl::Ast::ResolveTransformer::Options resolverOpt;

Expand Down Expand Up @@ -726,8 +842,8 @@
nzsl::Ast::ResolveTransformer resolver;
nzsl::Ast::ValidationTransformer validation;

Step("AST processing"sv, __LINE__, [&] { resolver.Transform(*m_shaderModule, context, resolverOpt); });
Step("AST validation"sv, __LINE__, [&] { validation.Transform(*m_shaderModule, context); });
Step("AST processing"sv, __LINE__, [&] { resolver.Transform(*m_shaderModule, m_transformerContext, resolverOpt); });

Check warning on line 845 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "line" member function of "std::source_location" instead of "__LINE__" macro.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyCivmQT7csWqFkn6&open=AZ5lyCivmQT7csWqFkn6&pullRequest=94
Step("AST validation"sv, __LINE__, [&] { validation.Transform(*m_shaderModule, m_transformerContext); });

Check warning on line 846 in src/ShaderCompiler/Compiler.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "line" member function of "std::source_location" instead of "__LINE__" macro.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ5lyCivmQT7csWqFkn7&open=AZ5lyCivmQT7csWqFkn7&pullRequest=94
}

template<typename F, typename... Args>
Expand Down
3 changes: 3 additions & 0 deletions src/ShaderCompiler/Compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <NZSL/Config.hpp>
#include <NZSL/Lang/Errors.hpp>
#include <NZSL/Ast/Module.hpp>
#include <NZSL/Ast/Transformations/TransformerContext.hpp>
#include <cxxopts.hpp>
#include <filesystem>
#include <type_traits>
Expand Down Expand Up @@ -64,6 +65,7 @@ namespace nzslc
void OutputFile(std::filesystem::path filePath, const void* data, std::size_t size, bool disallowHeader = false);
void OutputToStdout(std::string_view str);
void ReadInput();
void Reflect();
void Resolve();
template<typename F, typename... Args> auto Step(std::enable_if_t<!std::is_member_function_pointer_v<F>, std::string_view> stepName, std::size_t uniqueIndex, F&& func, Args&&... args) -> decltype(std::invoke(func, std::forward<Args>(args)...));
template<typename F, typename... Args> auto Step(std::enable_if_t<std::is_member_function_pointer_v<F>, std::string_view> stepName, std::size_t uniqueIndex, F&& func, Args&&... args) -> decltype(std::invoke(func, this, std::forward<Args>(args)...));
Expand All @@ -88,6 +90,7 @@ namespace nzslc
std::vector<StepTime> m_steps;
LogFormat m_logFormat;
nzsl::Ast::ModulePtr m_shaderModule;
nzsl::Ast::TransformerContext m_transformerContext;
cxxopts::ParseResult& m_options;
bool m_isProfiling;
bool m_outputHeader;
Expand Down
18 changes: 18 additions & 0 deletions tests/src/Tests/NzslcTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ TEST_CASE("Standalone compiler", "[NZSLC]")
// Generate the same shader a second time with --skip-unchanged and ensure file wasn't modified
ExecuteCommand("./nzslc --skip-unchanged --verbose --compile=spv --debug-level=regular -o test_files -m ../resources/modules/Color.nzslb -m ../resources/modules/Data/OutputStruct.nzslb -m ../resources/modules/Data/DataStruct.nzslb ../resources/Shader.nzslb", "Skipped file .+Shader.spv");
}

WHEN("Performing reflection")
{
REQUIRE(std::filesystem::exists("../resources/Reflection.nzsl"));

auto Cleanup = []
{
if (std::filesystem::is_directory("test_files"))
std::filesystem::remove_all("test_files");
};

Cleanup();

Nz::CallOnExit cleanupOnExit(std::move(Cleanup));

ExecuteCommand("./nzslc --verbose --reflect=Output --partial -o test_files ../resources/Reflection.nzsl");
CheckFileMatch("../resources/Reflection.nzsl.json", "test_files/Reflection.nzsl.json");
}
}
Loading