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
3 changes: 2 additions & 1 deletion include/NZSL/Ast/Enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace nzsl::Ast

enum class IntrinsicType
{
// Next free index: 64
// Next free index: 65
Abs = 31,
ArcCos = 21,
ArcCosh = 22,
Expand Down Expand Up @@ -201,6 +201,7 @@ namespace nzsl::Ast
Round = 28,
RoundEven = 29,
TextureRead = 2,
TextureSampleExplicitLod = 64,
TextureSampleImplicitLod = 44,
TextureSampleImplicitLodDepthComp = 43,
TextureWrite = 45,
Expand Down
1 change: 1 addition & 0 deletions include/NZSL/SpirV/SpirvAstVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace nzsl
// Should be private but are referenced in SpirvGenData.hpp
void BuildArraySizeIntrinsic(const Ast::IntrinsicExpression& node);
void BuildSelectIntrinsic(const Ast::IntrinsicExpression& node);
void BuildTextureSampleExplicitLodIntrinsic(const Ast::IntrinsicExpression& node);
static SpirvGlslStd450Op SelectAbs(const Ast::IntrinsicExpression& node);
static SpirvGlslStd450Op SelectClamp(const Ast::IntrinsicExpression& node);
static SpirvGlslStd450Op SelectLerp(const Ast::IntrinsicExpression& node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ namespace nzsl::Ast
case IntrinsicType::FwidthFine:
case IntrinsicType::TextureRead:
case IntrinsicType::TextureWrite:
case IntrinsicType::TextureSampleExplicitLod:
case IntrinsicType::TextureSampleImplicitLod:
case IntrinsicType::TextureSampleImplicitLodDepthComp:
break;
Expand Down
7 changes: 7 additions & 0 deletions src/NZSL/Ast/Transformations/ResolveTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,8 @@
throw CompilerTextureUnexpectedAccessError{ sourceLocation, "<TODO>" };

AccessPolicy access = static_cast<AccessPolicy>(std::get<std::uint32_t>(accessValue));
if (LangData::s_accessPolicies.find(access) == LangData::s_accessPolicies.end())
throw CompilerTextureUnexpectedAccessError{ sourceLocation, std::to_string(Nz::SafeCast<std::uint32_t>(access)) };

std::optional<ImageFormat> formatOpt;
if (parameterCount >= 3)
Expand Down Expand Up @@ -932,6 +934,8 @@
throw CompilerStorageUnexpectedAccessError{ sourceLocation, "<TODO>" };

access = static_cast<AccessPolicy>(std::get<std::uint32_t>(accessValue));
if (LangData::s_accessPolicies.find(access) == LangData::s_accessPolicies.end())
throw CompilerStorageUnexpectedAccessError{ sourceLocation, std::to_string(Nz::SafeCast<std::uint32_t>(access)) };
}

StructType structType = std::get<StructType>(exprType);
Expand Down Expand Up @@ -1401,6 +1405,8 @@
methodType.methodIndex = 0;
else if (identifierEntry.identifier == "SampleDepthComp")
methodType.methodIndex = 1;
else if (identifierEntry.identifier == "SampleLevel")
methodType.methodIndex = 2;
else
throw CompilerUnknownMethodError{ identifierEntry.sourceLocation, ToString(resolvedType, indexedExpr->sourceLocation), identifierEntry.identifier };

Expand Down Expand Up @@ -2052,9 +2058,10 @@
{
IntrinsicType intrinsicType;
switch (methodType.methodIndex)
{

Check warning on line 2061 in src/NZSL/Ast/Transformations/ResolveTransformer.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce verbosity with "using enum" for "nzsl::Ast::IntrinsicType".

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AaAFxvKT63KyUSPFUxtU&open=AaAFxvKT63KyUSPFUxtU&pullRequest=101
case 0: intrinsicType = IntrinsicType::TextureSampleImplicitLod; break;
case 1: intrinsicType = IntrinsicType::TextureSampleImplicitLodDepthComp; break;
case 2: intrinsicType = IntrinsicType::TextureSampleExplicitLod; break;
default:
throw AstInvalidMethodIndexError{ callFuncExpr.sourceLocation, methodType.methodIndex, ToString(objectType, callFuncExpr.sourceLocation) };
}
Expand Down
1 change: 1 addition & 0 deletions src/NZSL/GlslWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@ namespace nzsl
case Ast::IntrinsicType::RoundEven: Append("roundEven"); break;
case Ast::IntrinsicType::Sign: Append("sign"); break;
case Ast::IntrinsicType::TextureRead: Append("imageLoad"); break;
case Ast::IntrinsicType::TextureSampleExplicitLod: Append("textureLod"); break;
case Ast::IntrinsicType::TextureSampleImplicitLod: Append("texture"); break;
case Ast::IntrinsicType::TextureWrite: Append("imageStore"); break;
case Ast::IntrinsicType::Trunc: Append("trunc"); break;
Expand Down
12 changes: 12 additions & 0 deletions src/NZSL/Lang/LangData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@

namespace nzsl::LangData
{
struct AccessPolicyData
{
std::string_view identifier;
};

constexpr auto s_accessPolicies = frozen::make_unordered_map<AccessPolicy, AccessPolicyData>({
{ AccessPolicy::ReadOnly, { "readonly" } },
{ AccessPolicy::ReadWrite, { "readwrite" } },
{ AccessPolicy::WriteOnly, { "writeonly" } },
});

struct AttributeData
{
std::string_view identifier;
Expand Down Expand Up @@ -281,6 +292,7 @@ namespace nzsl::LangData
{ Ast::IntrinsicType::Tan, Build("tan", false, ReturnType::Param0Type, Params<ParameterType::FValVec1632>{}) },
{ Ast::IntrinsicType::Tanh, Build("tanh", false, ReturnType::Param0Type, Params<ParameterType::FValVec1632>{}) },
{ Ast::IntrinsicType::TextureRead, Build("textureRead", true, ReturnType::Param0TextureValue, Params<ParameterType::Texture, ParameterType::TextureCoordinates>{}) },
{ Ast::IntrinsicType::TextureSampleExplicitLod, Build("textureSampleExplicitLod", true, ReturnType::Param0SampledValue, Params<ParameterType::Sampler, ParameterType::SampleCoordinates, ParameterType::F32>{}) },
{ Ast::IntrinsicType::TextureSampleImplicitLod, Build("textureSampleImplicitLod", true, ReturnType::Param0SampledValue, Params<ParameterType::Sampler, ParameterType::SampleCoordinates>{}, ShaderStageType::Fragment) },
{ Ast::IntrinsicType::TextureSampleImplicitLodDepthComp, Build("textureSampleImplicitLodDepthComp", true, ReturnType::Param0SampledValue, Params<ParameterType::Sampler, ParameterType::SampleCoordinates, ParameterType::F32>{}, ShaderStageType::Fragment) },
{ Ast::IntrinsicType::TextureWrite, Build("textureWrite", true, ReturnType::None, Params<ParameterType::Texture, ParameterType::TextureCoordinates, ParameterType::TextureData>{}) },
Expand Down
7 changes: 7 additions & 0 deletions src/NZSL/LangWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,13 @@ namespace nzsl
method = true;
break;

case Ast::IntrinsicType::TextureSampleExplicitLod:
assert(!node.parameters.empty());
Visit(node.parameters.front(), true);
Append(".SampleLevel");
method = true;
break;

case Ast::IntrinsicType::TextureSampleImplicitLodDepthComp:
assert(!node.parameters.empty());
Visit(node.parameters.front(), true);
Expand Down
28 changes: 28 additions & 0 deletions src/NZSL/SpirV/SpirvAstVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,34 @@
PushResultId(resultId);
}

void SpirvAstVisitor::BuildTextureSampleExplicitLodIntrinsic(const Ast::IntrinsicExpression& node)
{
if (node.parameters.size() != 3)
throw std::runtime_error("textureSampleExplicitLod intrinsic: unexpected parameter count");

Check warning on line 1397 in src/NZSL/SpirV/SpirvAstVisitor.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=AaAFxvNj63KyUSPFUxtV&open=AaAFxvNj63KyUSPFUxtV&pullRequest=101

Check warning on line 1397 in src/NZSL/SpirV/SpirvAstVisitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/NZSL/SpirV/SpirvAstVisitor.cpp#L1397

Added line #L1397 was not covered by tests

std::uint32_t resultTypeId = m_writer.GetTypeId(ResolveAlias(EnsureExpressionType(node)));

std::uint32_t samplerId = EvaluateExpression(*node.parameters[0]);
std::uint32_t coordinatesId = EvaluateExpression(*node.parameters[1]);
std::uint32_t lodId = EvaluateExpression(*node.parameters[2]);

HandleSourceLocation(node.sourceLocation);

std::uint32_t resultId = m_writer.AllocateResultId();

m_currentBlock->AppendVariadic(SpirvOp::OpImageSampleExplicitLod, [&](auto&& append)

Check failure on line 1409 in src/NZSL/SpirV/SpirvAstVisitor.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Explicitly capture the required scope variables.

See more on https://sonarcloud.io/project/issues?id=NazaraEngine_ShaderLang&issues=AaAFxvNj63KyUSPFUxtW&open=AaAFxvNj63KyUSPFUxtW&pullRequest=101
{
append(resultTypeId);
append(resultId);
append(samplerId);
append(coordinatesId);
append(SpirvImageOperands::Lod);
append(lodId);
});

PushResultId(resultId);
}

SpirvGlslStd450Op SpirvAstVisitor::SelectAbs(const Ast::IntrinsicExpression& node)
{
if (node.parameters.size() != 1)
Expand Down
1 change: 1 addition & 0 deletions src/NZSL/SpirV/SpirvGenData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ NAZARA_WARNING_CLANG_GCC_DISABLE("-Wmissing-field-initializers")
{ Ast::IntrinsicType::Tan, { SpirvGlslStd450Op::Tan } },
{ Ast::IntrinsicType::Tanh, { SpirvGlslStd450Op::Tanh } },
{ Ast::IntrinsicType::TextureRead, { SpirvOp::OpImageRead } },
{ Ast::IntrinsicType::TextureSampleExplicitLod, { &SpirvAstVisitor::BuildTextureSampleExplicitLodIntrinsic } },
{ Ast::IntrinsicType::TextureSampleImplicitLod, { SpirvOp::OpImageSampleImplicitLod } },
{ Ast::IntrinsicType::TextureSampleImplicitLodDepthComp, { SpirvOp::OpImageSampleDrefImplicitLod } },
{ Ast::IntrinsicType::TextureWrite, { SpirvOp::OpImageWrite } },
Expand Down
48 changes: 48 additions & 0 deletions tests/src/Tests/ComputeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,52 @@ layout(rgba8_snorm) uniform writeonly image2D tex_rgba8_snorm;
%6 = OpTypePointer StorageClass(UniformConstant) %5
%8 = OpTypeImage %1 Dim(Dim2D) 2 0 0 2 ImageFormat(Rgba8Snorm))", {}, {}, true);
}

SECTION("Explicit LOD sampling")
{
std::string_view nzslSource = R"(
[nzsl_version("1.1")]
module;

[auto_binding]
external
{
tex: sampler2D[f32],
output_tex: texture2D[f32, writeonly, rgba8]
}

struct Input
{
[builtin(global_invocation_indices)] indices: vec3[u32]
}

[entry(compute)]
[workgroup(8, 8, 1)]
fn main(input: Input)
{
let coords = vec2[i32](input.indices.xy);
let value = tex.SampleLevel(vec2[f32](0.5, 0.5), 0.0);
output_tex.Write(coords, value);
}
)";

nzsl::Ast::ModulePtr shaderModule = nzsl::Parse(nzslSource);
ResolveModule(*shaderModule);

nzsl::GlslWriter::Environment glslEnv;
glslEnv.glES = true;
glslEnv.glMajorVersion = 3;
glslEnv.glMinorVersion = 1;

ExpectGLSL(*shaderModule, R"(
vec4 value = textureLod(tex, vec2(0.5, 0.5), 0.0);
)", {}, glslEnv);

ExpectNZSL(*shaderModule, R"(
let value: vec4[f32] = tex.SampleLevel(vec2[f32](0.5, 0.5), 0.0);
)");

ExpectSPIRV(*shaderModule, R"(
OpImageSampleExplicitLod %27 %39 %40 ImageOperands(2) ImageOperands(26))", {}, {}, true);
}
}
26 changes: 26 additions & 0 deletions tests/src/Tests/ErrorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,32 @@ fn main()
}
)"), "(14,2 -> 18): CIdentifierAlreadyUsed error: identifier Viewer is already used");

CHECK_THROWS_WITH(Compile(R"(
[nzsl_version("1.1")]
module;

external
{
[binding(0)] tex: texture2D[f32, rgba8]
}

)"), "(7,20 -> 40): CTextureUnexpectedAccess error: texture type require readonly, readwrite or writeonly qualifier (got 38)");

CHECK_THROWS_WITH(Compile(R"(
[nzsl_version("1.1")]
module;

struct Foo
{
}

external
{
[binding(0)] foo: storage[Foo, rgba8]
}

)"), "(11,20 -> 38): CStorageUnexpectedAccess error: storage type access qualifiers must be readonly, readwrite or writeonly (got 38)");

}

/************************************************************************/
Expand Down
Loading