Skip to content

Commit

Permalink
Define Texture::Sample for float element types only. (#3403)
Browse files Browse the repository at this point in the history
* Define `Texture::Sample` for float element types only.

* Fixes #490.

* Fix checking of groupshared.

* Add test.

* Fix.

---------

Co-authored-by: Yong He <yhe@nvidia.com>
  • Loading branch information
csyonghe and Yong He authored Dec 13, 2023
1 parent 92f21de commit 6cbe215
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 48 deletions.
9 changes: 6 additions & 3 deletions source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -1091,12 +1091,15 @@ struct matrix : IArray<vector<T,C>>
__subscript(int index) -> vector<T,C> { __intrinsic_op($(kIROp_GetElement)) get; }
}

__intrinsic_op($(kIROp_Eql))
vector<bool, N> __vectorEql<T, let N:int>(vector<T, N> left, vector<T,N> right);

__generic<T:__BuiltinFloatingPointType, let N : int>
extension vector<T,N> : 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);
Expand Down
26 changes: 13 additions & 13 deletions source/slang/glsl.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -167,78 +167,78 @@ public vector<T,4> texelFetch<T:__BuiltinArithmeticType, let N : int> (Sampler2D
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (Sampler1D<vector<T,N>> sampler, float p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (Sampler1D<vector<T,N>> sampler, float p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (Sampler2D<vector<T,N>> sampler, float2 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (Sampler2D<vector<T,N>> sampler, float2 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (Sampler3D<vector<T,N>> sampler, float3 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (Sampler3D<vector<T,N>> sampler, float3 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (SamplerCube<vector<T,N>> sampler, float3 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (SamplerCube<vector<T,N>> sampler, float3 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public float texture<T:__BuiltinArithmeticType, let N : int> (Sampler1DShadow<vector<T,N>> sampler, float2 p)
public float texture<T:__BuiltinFloatingPointType, let N : int> (Sampler1DShadow<vector<T,N>> sampler, float2 p)
{
return sampler.SampleCmp(p.x, p.y);
}

[ForceInline]
public float texture<T:__BuiltinArithmeticType, let N : int> (Sampler2DShadow<vector<T,N>> sampler, float3 p)
public float texture<T:__BuiltinFloatingPointType, let N : int> (Sampler2DShadow<vector<T,N>> sampler, float3 p)
{
return sampler.SampleCmp(p.xy, p.z);
}

[ForceInline]
public float texture<T:__BuiltinArithmeticType, let N : int> (SamplerCubeShadow<vector<T,N>> sampler, float4 p)
public float texture<T:__BuiltinFloatingPointType, let N : int> (SamplerCubeShadow<vector<T,N>> sampler, float4 p)
{
return sampler.SampleCmp(p.xyz, p.w);
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (Sampler1DArray<vector<T,N>> sampler, float2 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (Sampler1DArray<vector<T,N>> sampler, float2 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (Sampler2DArray<vector<T,N>> sampler, float3 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (Sampler2DArray<vector<T,N>> sampler, float3 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public vector<T,4> texture<T:__BuiltinArithmeticType, let N : int> (SamplerCubeArray<vector<T,N>> sampler, float4 p, float bias = 0.0)
public vector<T,4> texture<T:__BuiltinFloatingPointType, let N : int> (SamplerCubeArray<vector<T,N>> sampler, float4 p, float bias = 0.0)
{
return __vectorReshape<4>(sampler.SampleBias(p, bias));
}

[ForceInline]
public float texture<T:__BuiltinArithmeticType, let N : int> (Sampler1DArrayShadow<vector<T,N>> sampler, float3 p)
public float texture<T:__BuiltinFloatingPointType, let N : int> (Sampler1DArrayShadow<vector<T,N>> sampler, float3 p)
{
return sampler.SampleCmp(p.xy, p.z);
}

[ForceInline]
public float texture<T:__BuiltinArithmeticType, let N : int> (Sampler2DArrayShadow<vector<T,N>> sampler, float4 p)
public float texture<T:__BuiltinFloatingPointType, let N : int> (Sampler2DArrayShadow<vector<T,N>> sampler, float4 p)
{
return sampler.SampleCmp(p.xyz, p.w);
}

__generic<T:__BuiltinArithmeticType, let N : int, shape:__ITextureShape, let sampleCount: int, let isArray:int, let format:int>
__generic<T:__BuiltinFloatingPointType, let N : int, shape:__ITextureShape, let sampleCount: int, let isArray:int, let format:int>
public vector<T,4> textureGrad(__TextureImpl<vector<T,N>, shape, isArray, 0, sampleCount, 0, 1, 1, format> sampler, vector<float, shape.dimensions+isArray> P, vector<float,shape.planeDimensions> dPdx, vector<float,shape.planeDimensions> dPdy)
{
return __vectorReshape<4>(sampler.SampleGrad(P, dPdx, dPdy));
Expand Down
69 changes: 39 additions & 30 deletions source/slang/hlsl.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,26 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
}).y;
}
}

__target_intrinsic(glsl, "texture($0, $1)")
float __glsl_texture(vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureOffset($0, $1, $2)")
float __glsl_texture_offset(vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLod($0, $1, 0)")
float __glsl_texture_level_zero(vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLodOffset($0, $1, 0, $2)")
float __glsl_texture_offset_level_zero(vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);
}

__generic<T:IFloat, Shape: __ITextureShape, let isArray:int, let isMS:int, let sampleCount:int, let isShadow:int, let format:int>
extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
{
[__readNone]
T Sample(vector<float, Shape.dimensions+isArray> location)
{
Expand Down Expand Up @@ -329,21 +348,6 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
}
}

__target_intrinsic(glsl, "texture($0, $1)")
float __glsl_texture(vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureOffset($0, $1, $2)")
float __glsl_texture_offset(vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLod($0, $1, 0)")
float __glsl_texture_level_zero(vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLodOffset($0, $1, 0, $2)")
float __glsl_texture_offset_level_zero(vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

[__readNone]
[ForceInline]
float SampleCmp(vector<float, Shape.dimensions+isArray> location, float compareValue)
Expand Down Expand Up @@ -586,7 +590,27 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,access,isShadow,0,forma
}).y;
}
}

__target_intrinsic(glsl, "texture($p, $2)")
float __glsl_texture(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureOffset($p, $2, $3)")
float __glsl_texture_offset(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLod($p, $2, 0)")
float __glsl_texture_level_zero(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> 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<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

}

__generic<T:IFloat, Shape: __ITextureShape, let isArray:int, let isMS:int, let sampleCount:int, let isShadow:int, let format:int>
extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,0,format>
{
[__readNone]
T Sample(SamplerState s, vector<float, Shape.dimensions+isArray> location)
{
Expand Down Expand Up @@ -726,21 +750,6 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,access,isShadow,0,forma
}
}

__target_intrinsic(glsl, "texture($p, $2)")
float __glsl_texture(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> value);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureOffset($p, $2, $3)")
float __glsl_texture_offset(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

__glsl_extension(GL_EXT_texture_shadow_lod)
__target_intrinsic(glsl, "textureLod($p, $2, 0)")
float __glsl_texture_level_zero(SamplerComparisonState s, vector<float, Shape.dimensions+isArray+1> 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<float, Shape.dimensions+isArray+1> value, constexpr vector<int, Shape.planeDimensions> offset);

[__readNone] [ForceInline]
float SampleCmp(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue)
{
Expand Down
8 changes: 6 additions & 2 deletions source/slang/slang-check-modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1081,12 +1081,16 @@ namespace Slang
case ASTNodeType::HLSLColumnMajorLayoutModifier:
case ASTNodeType::GLSLRowMajorLayoutModifier:
case ASTNodeType::HLSLEffectSharedModifier:
case ASTNodeType::HLSLGroupSharedModifier:
case ASTNodeType::HLSLVolatileModifier:
return as<VarDeclBase>(decl) || as<GLSLInterfaceBlockDecl>(decl);

case ASTNodeType::GLSLPrecisionModifier:
return as<VarDeclBase>(decl) || as<GLSLInterfaceBlockDecl>(decl) || as<CallableDecl>(decl);
case ASTNodeType::HLSLGroupSharedModifier:
// groupshared must be global or static.
if (!as<VarDeclBase>(decl))
return false;
return isGlobalDecl(decl) || isEffectivelyStatic(decl);
default:
return true;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/diagnostics/groupshared-local.slang
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 7 additions & 0 deletions tests/diagnostics/int-texture-sample.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):

void test(Texture2D<int4> itex, SamplerState s)
{
// CHECK: ([[# @LINE+1]]): error
itex.Sample(s, float2(0.0, 0.0));
}

0 comments on commit 6cbe215

Please sign in to comment.