diff options
| author | venkataram-nv <vedavamadath@nvidia.com> | 2024-07-01 16:01:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-01 16:01:21 -0700 |
| commit | fff79c311028cc8a5cff310a9c493b23af68eb0c (patch) | |
| tree | 3842c4bb724fc1c06b839f314728295a99cae4ca | |
| parent | 6262569c6305468afac8067112b61cc42780464c (diff) | |
Support HLSL `.sample` operators for MS textures (#4524)
* Add `.sample` operator for MS texture types
* Adding filecheck tests for `.sample`
| -rw-r--r-- | source/slang/hlsl.meta.slang | 33 | ||||
| -rw-r--r-- | tests/hlsl/texture-sample-operator.slang | 27 |
2 files changed, 60 insertions, 0 deletions
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 7c65aec5e..f50c63367 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -3202,6 +3202,7 @@ struct __TextureMip<T, Shape : __ITextureShape, let isArray : int, let isCombine get { return tex.Load(__makeVector(pos, mip)); } } } + struct __TextureMips<T, Shape : __ITextureShape, let isArray : int, let isCombined : int, let format : int> { __TextureImpl<T, Shape, isArray, 0 /*isMS*/, 0 /*sampleCount*/, 0 /*access*/, 0 /*isShadow*/, isCombined, format> tex; @@ -3222,6 +3223,38 @@ extension __TextureImpl<T, Shape, isArray, 0 /*isMS*/, 0 /*sampleCount*/, 0 /*ac } } +// Definitions to support the .sample[][] operator. +struct __TextureSample<T, Shape : __ITextureShape, let isArray : int, let isCombined : int, let format : int> +{ + __TextureImpl<T, Shape, isArray, 1 /*isMS*/, 0 /*sampleCount*/, 0 /*access*/, 0 /*isShadow*/, isCombined, format> tex; + int sample; + __subscript(vector<int, isArray + Shape.dimensions> pos)->T + { + [__unsafeForceInlineEarly] + get { return tex[pos, sample]; } + } +} + +struct __TextureSampleMS<T, Shape : __ITextureShape, let isArray : int, let isCombined : int, let format : int> +{ + __TextureImpl<T, Shape, isArray, 1 /*isMS*/, 0 /*sampleCount*/, 0 /*access*/, 0 /*isShadow*/, isCombined, format> tex; + __subscript(int sample)->__TextureSample<T, Shape, isArray, isCombined, format> + { + [__unsafeForceInlineEarly] + get { return { tex, sample }; } + } +} + +__generic<T, Shape : __ITextureShape, let isArray : int, let isCombined : int, let format : int> +extension __TextureImpl<T, Shape, isArray, 1 /*isMS*/, 0 /*sampleCount*/, 0 /*access*/, 0 /*isShadow*/, isCombined, format> +{ + property __TextureSampleMS<T, Shape, isArray, isCombined, format> sample + { + [__unsafeForceInlineEarly] + get { return { this }; } + } +} + // Texture type aliases. // T, Shape: __ITextureShape, let isArray:int, let isMS:int, let sampleCount:int, let access:int, let isShadow:int, let isCombined:int, let format:int ${{{{ diff --git a/tests/hlsl/texture-sample-operator.slang b/tests/hlsl/texture-sample-operator.slang new file mode 100644 index 000000000..175470367 --- /dev/null +++ b/tests/hlsl/texture-sample-operator.slang @@ -0,0 +1,27 @@ +//TEST:SIMPLE(filecheck=HLSL): -target hlsl -stage vertex +//TEST:SIMPLE(filecheck=GLSL): -target glsl -stage vertex +//TEST:SIMPLE(filecheck=METAL): -target metal -stage vertex +//TEST:SIMPLE(filecheck=SPIRV): -target spirv -stage vertex +//TEST:SIMPLE(filecheck=SPIRV): -target spirv-asm -stage vertex + +Texture2DMS <int3> t1; +Texture2DMSArray <float4> t2; + +float4 main() +{ + // HLSL: .sample + // GLSL: texelFetch + // SPIRV: OpImageFetch %v4int {{.*}} Sample {{.*}} + // METAL: .read + uint2 p1 = uint2(1, 2); + int3 a1 = t1.sample[7][p1]; + + // HLSL: .sample + // GLSL: texelFetch + // SPIRV: OpImageFetch %v4float {{.*}} Sample {{.*}} + // METAL: .read + uint p2 = uint(1); + float4 a2 = t2.sample[p2][uint3(1, 2, 3)]; + + return float4(float3(a1), 0) + a2; +} |
