From 6cbe215e58eeb8edc53d71e8f315e2fb55c0eeee Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 12 Dec 2023 16:29:51 -0800 Subject: Define `Texture::Sample` for float element types only. (#3403) * Define `Texture::Sample` for float element types only. * Fixes #490. * Fix checking of groupshared. * Add test. * Fix. --------- Co-authored-by: Yong He --- source/slang/core.meta.slang | 9 ++-- source/slang/glsl.meta.slang | 26 +++++------ source/slang/hlsl.meta.slang | 69 +++++++++++++++++------------- source/slang/slang-check-modifier.cpp | 8 +++- tests/diagnostics/groupshared-local.slang | 10 +++++ tests/diagnostics/int-texture-sample.slang | 7 +++ 6 files changed, 81 insertions(+), 48 deletions(-) create mode 100644 tests/diagnostics/groupshared-local.slang create mode 100644 tests/diagnostics/int-texture-sample.slang diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 3a19d345a..fbb18109e 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -1091,12 +1091,15 @@ struct matrix : IArray> __subscript(int index) -> vector { __intrinsic_op($(kIROp_GetElement)) get; } } +__intrinsic_op($(kIROp_Eql)) +vector __vectorEql(vector left, vector right); + __generic extension vector : IFloat { - [__unsafeForceInlineEarly] bool lessThan(This other) { return this < other; } - [__unsafeForceInlineEarly] bool lessThanOrEquals(This other) { return this <= other; } - [__unsafeForceInlineEarly] bool equals(This other) { return all(this == other); } + [__unsafeForceInlineEarly] bool lessThan(This other) { return this[0] < other[0]; } + [__unsafeForceInlineEarly] bool lessThanOrEquals(This other) { return this[0] <= other[0]; } + [__unsafeForceInlineEarly] bool equals(This other) { return all(__vectorEql(this, other)); } __intrinsic_op($(kIROp_Add)) This add(This other); __intrinsic_op($(kIROp_Sub)) This sub(This other); __intrinsic_op($(kIROp_Mul)) This mul(This other); diff --git a/source/slang/glsl.meta.slang b/source/slang/glsl.meta.slang index a43a7ba3c..3cab1232e 100644 --- a/source/slang/glsl.meta.slang +++ b/source/slang/glsl.meta.slang @@ -167,78 +167,78 @@ public vector texelFetch (Sampler2D } [ForceInline] -public vector texture (Sampler1D> sampler, float p, float bias = 0.0) +public vector texture (Sampler1D> sampler, float p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public vector texture (Sampler2D> sampler, float2 p, float bias = 0.0) +public vector texture (Sampler2D> sampler, float2 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public vector texture (Sampler3D> sampler, float3 p, float bias = 0.0) +public vector texture (Sampler3D> sampler, float3 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public vector texture (SamplerCube> sampler, float3 p, float bias = 0.0) +public vector texture (SamplerCube> sampler, float3 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public float texture (Sampler1DShadow> sampler, float2 p) +public float texture (Sampler1DShadow> sampler, float2 p) { return sampler.SampleCmp(p.x, p.y); } [ForceInline] -public float texture (Sampler2DShadow> sampler, float3 p) +public float texture (Sampler2DShadow> sampler, float3 p) { return sampler.SampleCmp(p.xy, p.z); } [ForceInline] -public float texture (SamplerCubeShadow> sampler, float4 p) +public float texture (SamplerCubeShadow> sampler, float4 p) { return sampler.SampleCmp(p.xyz, p.w); } [ForceInline] -public vector texture (Sampler1DArray> sampler, float2 p, float bias = 0.0) +public vector texture (Sampler1DArray> sampler, float2 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public vector texture (Sampler2DArray> sampler, float3 p, float bias = 0.0) +public vector texture (Sampler2DArray> sampler, float3 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public vector texture (SamplerCubeArray> sampler, float4 p, float bias = 0.0) +public vector texture (SamplerCubeArray> sampler, float4 p, float bias = 0.0) { return __vectorReshape<4>(sampler.SampleBias(p, bias)); } [ForceInline] -public float texture (Sampler1DArrayShadow> sampler, float3 p) +public float texture (Sampler1DArrayShadow> sampler, float3 p) { return sampler.SampleCmp(p.xy, p.z); } [ForceInline] -public float texture (Sampler2DArrayShadow> sampler, float4 p) +public float texture (Sampler2DArrayShadow> sampler, float4 p) { return sampler.SampleCmp(p.xyz, p.w); } -__generic +__generic public vector textureGrad(__TextureImpl, shape, isArray, 0, sampleCount, 0, 1, 1, format> sampler, vector P, vector dPdx, vector dPdy) { return __vectorReshape<4>(sampler.SampleGrad(P, dPdx, dPdy)); diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 5bb5ce038..477e0cfaf 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -214,7 +214,26 @@ extension __TextureImpl }).y; } } + + __target_intrinsic(glsl, "texture($0, $1)") + float __glsl_texture(vector value); + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureOffset($0, $1, $2)") + float __glsl_texture_offset(vector value, constexpr vector offset); + + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureLod($0, $1, 0)") + float __glsl_texture_level_zero(vector value); + + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureLodOffset($0, $1, 0, $2)") + float __glsl_texture_offset_level_zero(vector value, constexpr vector offset); +} + +__generic +extension __TextureImpl +{ [__readNone] T Sample(vector location) { @@ -329,21 +348,6 @@ extension __TextureImpl } } - __target_intrinsic(glsl, "texture($0, $1)") - float __glsl_texture(vector value); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureOffset($0, $1, $2)") - float __glsl_texture_offset(vector value, constexpr vector offset); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureLod($0, $1, 0)") - float __glsl_texture_level_zero(vector value); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureLodOffset($0, $1, 0, $2)") - float __glsl_texture_offset_level_zero(vector value, constexpr vector offset); - [__readNone] [ForceInline] float SampleCmp(vector location, float compareValue) @@ -586,7 +590,27 @@ extension __TextureImpl value); + + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureOffset($p, $2, $3)") + float __glsl_texture_offset(SamplerComparisonState s, vector value, constexpr vector offset); + + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureLod($p, $2, 0)") + float __glsl_texture_level_zero(SamplerComparisonState s, vector value); + + __glsl_extension(GL_EXT_texture_shadow_lod) + __target_intrinsic(glsl, "textureLodOffset($p, $2, 0, $3)") + float __glsl_texture_offset_level_zero(SamplerComparisonState s, vector value, constexpr vector offset); + +} +__generic +extension __TextureImpl +{ [__readNone] T Sample(SamplerState s, vector location) { @@ -726,21 +750,6 @@ extension __TextureImpl value); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureOffset($p, $2, $3)") - float __glsl_texture_offset(SamplerComparisonState s, vector value, constexpr vector offset); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureLod($p, $2, 0)") - float __glsl_texture_level_zero(SamplerComparisonState s, vector value); - - __glsl_extension(GL_EXT_texture_shadow_lod) - __target_intrinsic(glsl, "textureLodOffset($p, $2, 0, $3)") - float __glsl_texture_offset_level_zero(SamplerComparisonState s, vector value, constexpr vector offset); - [__readNone] [ForceInline] float SampleCmp(SamplerComparisonState s, vector location, float compareValue) { diff --git a/source/slang/slang-check-modifier.cpp b/source/slang/slang-check-modifier.cpp index 53d121b4b..a9a8c19da 100644 --- a/source/slang/slang-check-modifier.cpp +++ b/source/slang/slang-check-modifier.cpp @@ -966,9 +966,9 @@ namespace Slang case ASTNodeType::HLSLColumnMajorLayoutModifier: case ASTNodeType::GLSLRowMajorLayoutModifier: case ASTNodeType::HLSLEffectSharedModifier: - case ASTNodeType::HLSLGroupSharedModifier: case ASTNodeType::HLSLVolatileModifier: case ASTNodeType::GLSLPrecisionModifier: + case ASTNodeType::HLSLGroupSharedModifier: return modifierType; case ASTNodeType::HLSLStaticModifier: @@ -1081,12 +1081,16 @@ namespace Slang case ASTNodeType::HLSLColumnMajorLayoutModifier: case ASTNodeType::GLSLRowMajorLayoutModifier: case ASTNodeType::HLSLEffectSharedModifier: - case ASTNodeType::HLSLGroupSharedModifier: case ASTNodeType::HLSLVolatileModifier: return as(decl) || as(decl); case ASTNodeType::GLSLPrecisionModifier: return as(decl) || as(decl) || as(decl); + case ASTNodeType::HLSLGroupSharedModifier: + // groupshared must be global or static. + if (!as(decl)) + return false; + return isGlobalDecl(decl) || isEffectivelyStatic(decl); default: return true; } diff --git a/tests/diagnostics/groupshared-local.slang b/tests/diagnostics/groupshared-local.slang new file mode 100644 index 000000000..02198320e --- /dev/null +++ b/tests/diagnostics/groupshared-local.slang @@ -0,0 +1,10 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +// CHECK-NOT: ([[# @LINE+1]]): error 31201 +groupshared int b; + +int test() +{ + // CHECK: ([[# @LINE+1]]): error 31201 + groupshared int a; +} diff --git a/tests/diagnostics/int-texture-sample.slang b/tests/diagnostics/int-texture-sample.slang new file mode 100644 index 000000000..696e13a60 --- /dev/null +++ b/tests/diagnostics/int-texture-sample.slang @@ -0,0 +1,7 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +void test(Texture2D itex, SamplerState s) +{ + // CHECK: ([[# @LINE+1]]): error + itex.Sample(s, float2(0.0, 0.0)); +} -- cgit v1.2.3