Skip to content
Merged
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
}
45 changes: 45 additions & 0 deletions bin/resources/Reflection.nzsl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"structs": [
{
"name": "Output",
"structIndex": 0,
"layout": "std140",
"members": [
{
"name": "color",
"type": {
"type": "vector",
"dims": 4,
"baseType": "f32"
},
"offset": 0
},
{
"name": "normal",
"type": {
"type": "vector",
"dims": 3,
"baseType": "f32"
},
"offset": 16
},
{
"name": "roughness",
"type": {
"type": "primitive",
"primitiveType": "f32"
},
"offset": 28
},
{
"name": "metalness",
"type": {
"type": "primitive",
"primitiveType": "f32"
},
"offset": 32
}
]
}
]
}
5 changes: 5 additions & 0 deletions include/NZSL/Ast/ExpressionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@
MethodType(const MethodType& methodType);
MethodType(MethodType&&) noexcept = default;

inline auto& ObjectType();

Check warning on line 161 in include/NZSL/Ast/ExpressionType.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move the "inline" specifier to the definition of the function.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AZ_VvsFd4MwfLen3sp2A&open=AZ_VvsFd4MwfLen3sp2A&pullRequest=94
inline const auto& ObjectType() const;

Check warning on line 162 in include/NZSL/Ast/ExpressionType.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move the "inline" specifier to the definition of the function.

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

template<typename T> void SetupObjectType(T&& value);

MethodType& operator=(const MethodType& methodType);
MethodType& operator=(MethodType&&) noexcept = default;

Expand Down
17 changes: 17 additions & 0 deletions include/NZSL/Ast/ExpressionType.inl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@
return !operator==(rhs);
}

inline auto& MethodType::ObjectType()
{
return objectType->type;
}

Check warning on line 166 in include/NZSL/Ast/ExpressionType.inl

View check run for this annotation

Codecov / codecov/patch

include/NZSL/Ast/ExpressionType.inl#L164-L166

Added lines #L164 - L166 were not covered by tests

inline const auto& MethodType::ObjectType() const
{
return objectType->type;
}

Check warning on line 171 in include/NZSL/Ast/ExpressionType.inl

View check run for this annotation

Codecov / codecov/patch

include/NZSL/Ast/ExpressionType.inl#L168-L171

Added lines #L168 - L171 were not covered by tests

template<typename T>
void MethodType::SetupObjectType(T&& value)
{
objectType = std::make_unique<ContainedType>();
objectType->type = std::forward<T>(value);
}


inline bool ModuleType::operator==(const ModuleType& rhs) const
{
Expand Down
4 changes: 4 additions & 0 deletions include/NZSL/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ namespace nzsl
static std::string_view ToString(Ast::BuiltinEntry builtinEntry);
static std::string_view ToString(Ast::DepthWriteMode depthWriteMode);
static std::string_view ToString(Ast::InterpolationQualifier interpolationQualifier);
static std::string_view ToString(Ast::IntrinsicType intrinsicType);
static std::string_view ToString(Ast::LoopUnroll loopUnroll);
static std::string_view ToString(Ast::MemoryLayout memoryLayout);
static std::string_view ToString(Ast::ModuleFeature moduleFeature);
static std::string_view ToString(Ast::TypeConstant typeConstant);
static std::string_view ToString(AccessPolicy accessPolicy);
static std::string_view ToString(ImageFormat imageFormat);
static std::string_view ToString(ImageType imageType);
static std::string_view ToString(ShaderStageType shaderStage);

private:
Expand Down
3 changes: 1 addition & 2 deletions src/NZSL/Ast/AstSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,7 @@ NAZARA_WARNING_GCC_DISABLE("-Wmaybe-uninitialized")
SizeT(methodIndex);

MethodType methodType;
methodType.objectType = std::make_unique<ContainedType>();
methodType.objectType->type = std::move(objectType);
methodType.SetupObjectType(std::move(objectType));
methodType.methodIndex = methodIndex;

type = std::move(methodType);
Expand Down
27 changes: 3 additions & 24 deletions src/NZSL/Ast/ExpressionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <NZSL/Ast/ExpressionType.hpp>
#include <NazaraUtils/Algorithm.hpp>
#include <NazaraUtils/TypeList.hpp>
#include <NZSL/Parser.hpp>
#include <NZSL/Ast/Cloner.hpp>
#include <NZSL/Ast/Compare.hpp>
#include <NZSL/Math/FieldOffsets.hpp>
Expand Down Expand Up @@ -450,18 +451,7 @@ namespace nzsl::Ast

std::string ToString(const SamplerType& type, const Stringifier& /*stringifier*/)
{
std::string_view dimensionStr;
switch (type.dim)
{
case ImageType::E1D: dimensionStr = "1D"; break;
case ImageType::E1D_Array: dimensionStr = "1DArray"; break;
case ImageType::E2D: dimensionStr = "2D"; break;
case ImageType::E2D_Array: dimensionStr = "2DArray"; break;
case ImageType::E3D: dimensionStr = "3D"; break;
case ImageType::Cubemap: dimensionStr = "Cube"; break;
}

return fmt::format("{}sampler{}[{}]", (type.depth) ? "depth_" : "", dimensionStr, ToString(type.sampledType));
return fmt::format("{}sampler{}[{}]", (type.depth) ? "depth_" : "", Parser::ToString(type.dim), ToString(type.sampledType));
}

std::string ToString(const StorageType& type, const Stringifier& stringifier)
Expand All @@ -479,18 +469,7 @@ namespace nzsl::Ast

std::string ToString(const TextureType& type, const Stringifier& /*stringifier*/)
{
std::string_view dimensionStr;
switch (type.dim)
{
case ImageType::E1D: dimensionStr = "1D"; break;
case ImageType::E1D_Array: dimensionStr = "1DArray"; break;
case ImageType::E2D: dimensionStr = "2D"; break;
case ImageType::E2D_Array: dimensionStr = "2DArray"; break;
case ImageType::E3D: dimensionStr = "3D"; break;
case ImageType::Cubemap: dimensionStr = "Cube"; break;
}

return fmt::format("texture{}[{}]", dimensionStr, ToString(type.baseType));
return fmt::format("texture{}[{}]", Parser::ToString(type.dim), ToString(type.baseType));
}

std::string ToString(const Type& type, const Stringifier& stringifier)
Expand Down
13 changes: 5 additions & 8 deletions src/NZSL/Ast/Transformations/ResolveTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,8 @@ namespace nzsl::Ast

for (const auto& [intrinsic, data] : LangData::s_intrinsicData)
{
if (!data.functionName.empty())
RegisterBuiltinIntrinsic(std::string(data.functionName), intrinsic);
if (!data.isMethod)
RegisterBuiltinIntrinsic(std::string(data.name), intrinsic);
}

// Constants
Expand Down Expand Up @@ -1404,8 +1404,7 @@ namespace nzsl::Ast
else
throw CompilerUnknownMethodError{ identifierEntry.sourceLocation, ToString(resolvedType, indexedExpr->sourceLocation), identifierEntry.identifier };

methodType.objectType = std::make_unique<ContainedType>();
methodType.objectType->type = resolvedType;
methodType.SetupObjectType(resolvedType);

// TODO: Add a MethodExpression?
auto identifierExpr = std::make_unique<AccessIdentifierExpression>();
Expand All @@ -1428,8 +1427,7 @@ namespace nzsl::Ast
else
throw CompilerUnknownMethodError{ identifierEntry.sourceLocation, ToString(resolvedType, indexedExpr->sourceLocation), identifierEntry.identifier };

methodType.objectType = std::make_unique<ContainedType>();
methodType.objectType->type = resolvedType;
methodType.SetupObjectType(resolvedType);

// TODO: Add a MethodExpression?
auto identifierExpr = std::make_unique<AccessIdentifierExpression>();
Expand All @@ -1452,8 +1450,7 @@ namespace nzsl::Ast

MethodType methodType;
methodType.methodIndex = 0; //< FIXME
methodType.objectType = std::make_unique<ContainedType>();
methodType.objectType->type = resolvedType;
methodType.SetupObjectType(resolvedType);

identifierExpr->cachedExpressionType = std::move(methodType);
indexedExpr = std::move(identifierExpr);
Expand Down
2 changes: 1 addition & 1 deletion src/NZSL/Ast/Transformations/ValidationTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ namespace nzsl::Ast
void ValidationTransformer::ValidateIntrinsicParameters(IntrinsicExpression& node, const T& intrinsicData)
{
if (node.parameters.size() != intrinsicData.nonConstraintParameterCount)
throw CompilerIntrinsicExpectedParameterCountError{ node.sourceLocation, Nz::SafeCast<std::uint32_t>(intrinsicData.nonConstraintParameterCount), intrinsicData.functionName, Nz::SafeCast<std::uint32_t>(node.parameters.size()) };
throw CompilerIntrinsicExpectedParameterCountError{ node.sourceLocation, Nz::SafeCast<std::uint32_t>(intrinsicData.nonConstraintParameterCount), intrinsicData.name, Nz::SafeCast<std::uint32_t>(node.parameters.size()) };

std::optional<std::size_t> unresolvedParameter;

Expand Down
12 changes: 6 additions & 6 deletions src/NZSL/Ast/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace nzsl::Ast
const auto& intrinsicData = intrinsicIt->second;

if (intrinsicExpr.parameters.size() != intrinsicData.nonConstraintParameterCount)
throw CompilerIntrinsicExpectedParameterCountError{ intrinsicExpr.sourceLocation, Nz::SafeCast<std::uint32_t>(intrinsicData.nonConstraintParameterCount), intrinsicData.functionName, Nz::SafeCast<std::uint32_t>(intrinsicExpr.parameters.size()) };
throw CompilerIntrinsicExpectedParameterCountError{ intrinsicExpr.sourceLocation, Nz::SafeCast<std::uint32_t>(intrinsicData.nonConstraintParameterCount), intrinsicData.name, Nz::SafeCast<std::uint32_t>(intrinsicExpr.parameters.size()) };

std::array<std::optional<ExpressionType>, 2> parameterTypes;
if (intrinsicData.returnType == ReturnType::Param0Type || intrinsicData.returnType == ReturnType::Param1Type)
Expand Down Expand Up @@ -270,7 +270,7 @@ namespace nzsl::Ast
return vecType;
}
else
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a primitive nor vector", intrinsicData.functionName) };
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a primitive nor vector", intrinsicData.name) };
}

case ReturnType::Param0SampledValue:
Expand All @@ -281,7 +281,7 @@ namespace nzsl::Ast

const ExpressionType& paramType = ResolveAlias(*expressionType);
if (!IsSamplerType(paramType))
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a sampler", intrinsicData.functionName) };
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a sampler", intrinsicData.name) };

const SamplerType& samplerType = std::get<SamplerType>(paramType);
if (samplerType.depth)
Expand All @@ -298,7 +298,7 @@ namespace nzsl::Ast

const ExpressionType& paramType = ResolveAlias(*expressionType);
if (!IsTextureType(paramType))
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a sampler", intrinsicData.functionName) };
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a sampler", intrinsicData.name) };

const TextureType& textureType = std::get<TextureType>(paramType);
return VectorType{ 4, textureType.baseType };
Expand All @@ -312,7 +312,7 @@ namespace nzsl::Ast

const ExpressionType& paramType = ResolveAlias(*expressionType);
if (!IsMatrixType(paramType))
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a matrix", intrinsicData.functionName) };
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a matrix", intrinsicData.name) };

MatrixType matrixType = std::get<MatrixType>(paramType);
std::swap(matrixType.columnCount, matrixType.rowCount);
Expand Down Expand Up @@ -346,7 +346,7 @@ namespace nzsl::Ast

const ExpressionType& paramType = ResolveAlias(*expressionType);
if (!IsVectorType(paramType))
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a vector", intrinsicData.functionName) };
throw AstInternalError{ intrinsicExpr.sourceLocation, fmt::format("intrinsic {} first parameter is not a vector", intrinsicData.name) };

const VectorType& vecType = std::get<VectorType>(paramType);
return vecType.type;
Expand Down
Loading
Loading