summaryrefslogtreecommitdiffstats
path: root/tests/wgsl/texture-sampler-less.slang
blob: 7048714b8e09ec67715ed19b8f78657c44be931b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// WGSL supports "textureSample()" only in fragment shader
//TEST:SIMPLE(filecheck=WGSL): -stage fragment -entry fragMain -target wgsl

// TODO: offset variants requires the offset value to be compile-time constant
// But the keyword, `constexpr`, doesn't seem to work for the combined-textures
#define TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET 0

//WGSL:@binding(0) {{.*}}t1D_f32v3{{.*}} : texture_1d<f32>;
//WGSL:@binding(1) {{.*}}t1D_f32v3{{.*}} : sampler;
//TEST_INPUT: TextureSampler1D(size=4, content = zero):name t1D_f32v3
Sampler1D<float3> t1D_f32v3;

//WGSL:@binding(2) {{.*}}t2D_f32v3{{.*}} : texture_2d<f32>;
//WGSL:@binding(3) {{.*}}t2D_f32v3{{.*}} : sampler;
//TEST_INPUT: TextureSampler2D(size=4, content = zero):name t2D_f32v3
Sampler2D<float3> t2D_f32v3;

//TEST_INPUT: TextureSampler3D(size=4, content = zero):name t3D_f32v3
Sampler3D<float3> t3D_f32v3;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name tCube_f32v3
SamplerCube<float3> tCube_f32v3;

//TEST_INPUT: TextureSampler1D(size=4, content = zero, arrayLength=2):name t1DArray_f32v3
Sampler1DArray<float3> t1DArray_f32v3;
//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name t2DArray_f32v3
Sampler2DArray<float3> t2DArray_f32v3;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name tCubeArray_f32v3
SamplerCubeArray<float3> tCubeArray_f32v3;

//TEST_INPUT: TextureSampler1D(size=4, content = zero):name t1D_f32v4
Sampler1D<float4> t1D_f32v4;
//TEST_INPUT: TextureSampler2D(size=4, content = zero):name t2D_f32v4
Sampler2D<float4> t2D_f32v4;
//TEST_INPUT: TextureSampler3D(size=4, content = zero):name t3D_f32v4
Sampler3D<float4> t3D_f32v4;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name tCube_f32v4
SamplerCube<float4> tCube_f32v4;

//TEST_INPUT: TextureSampler1D(size=4, content = zero, arrayLength=2):name t1DArray_f32v4
Sampler1DArray<float4> t1DArray_f32v4;
//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name t2DArray_f32v4
Sampler2DArray<float4> t2DArray_f32v4;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name tCubeArray_f32v4
SamplerCubeArray<float4> tCubeArray_f32v4;

//TEST_INPUT: TextureSampler2D(size=4, content = zero):name d2D
Sampler2DShadow d2D;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero):name dCube
SamplerCubeShadow dCube;
//TEST_INPUT: TextureSampler2D(size=4, content = zero, arrayLength=2):name d2DArray
Sampler2DArrayShadow d2DArray;
//TEST_INPUT: TextureSamplerCube(size=4, content = zero, arrayLength=2):name dCubeArray
SamplerCubeArrayShadow dCubeArray;

//TEST_INPUT: ubuffer(data=[0], stride=4):out,name outputBuffer
RWStructuredBuffer<int> outputBuffer;


bool TEST_texture<T>(
    Sampler1D<T> t1D,
    Sampler2D<T> t2D,
    Sampler3D<T> t3D,
    SamplerCube<T> tCube,
    Sampler1DArray<T> t1DArray,
    Sampler2DArray<T> t2DArray,
    SamplerCubeArray<T> tCubeArray
) where T:ITexelElement,IArithmetic
{
    // WGSL-LABEL: TEST_texture
    typealias Tvn = T;
    typealias Tv4 = vector<T.Element,4>;

    float u = 0;
    float u2 = 0.5;
    constexpr const float ddx = 0.0f;
    constexpr const float ddy = 0.0f;

    bool result = true
        // ===============================
        // float CalculateLevelOfDetail()
        // ===============================
        // WGSL doesn't have a way to calculate mip-map level for the given coordinate

        // ========================================
        // float CalculateLevelOfDetailUnclamped()
        // ========================================
        // WGSL doesn't have a way to calculate mip-map level for the given coordinate

        // ===========
        // T Sample()
        // https://www.w3.org/TR/WGSL/#texturesample
        // ===========

        // WGSL: textureSample({{\(*}}t1D
        && all(Tvn(T.Element(0)) == t1D.Sample(u))

        // WGSL: textureSample({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.Sample(float2(u, u)))

        // WGSL: textureSample({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.Sample(float3(u, u, u)))

        // WGSL: textureSample({{\(*}}tCube
        && all(Tvn(T.Element(0)) == tCube.Sample(normalize(float3(u, 1 - u, u))))

        // WGSL doesn't support textureSample for 1d_array and 3d_array; only 2d and cube

        // WGSL: textureSample({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.Sample(float3(u, u, 0)))

        // WGSL: textureSample({{\(*}}tCubeArray
        && all(Tvn(T.Element(0)) == tCubeArray.Sample(float4(normalize(float3(u, 1 - u, u)), 0)))

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // WGSL: textureSample({{\(*}}t1D
        && all(Tvn(T.Element(0)) == t1D.Sample(u, 1))

        // WGSL: textureSample({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.Sample(float2(u, u), int2(1, 1)))

        // WGSL: textureSample({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.Sample(float3(u, u, u), int3(1, 1, 1)))

        // WGSL doesn't support offset variant for cube and cube_array

        // WGSL: textureSample({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.Sample(float3(u, u, 0), int2(1, 1)))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // ===============
        // T SampleBias()
        // https://www.w3.org/TR/WGSL/#texturesamplebias
        // ===============

        // WGSL doesn't support Bias for 1D texture

        // WGSL: textureSampleBias({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleBias(float2(u, u), float(-1)))

        // WGSL: textureSampleBias({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleBias(float3(u, u, u), float(-1)))

        // WGSL: textureSampleBias({{\(*}}tCube
        && all(Tvn(T.Element(0)) == tCube.SampleBias(normalize(float3(u, 1 - u, u)), float(-1)))

        // WGSL: textureSampleBias({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleBias(float3(u, u, 0), float(-1)))

        // WGSL: textureSampleBias({{\(*}}tCubeArray
        && all(Tvn(T.Element(0)) == tCubeArray.SampleBias(float4(normalize(float3(u, 1 - u, u)), 0), float(-1)))

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureSampleBias({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleBias(float2(u, u), float(-1), int2(1, 1)))

        // W-GSL: textureSampleBias({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleBias(float3(u, u, u), float(-1), int3(1, 1, 1)))

        // W-GSL: textureSampleBias({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleBias(float3(u, u, 0), float(-1), int2(1, 1)))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // ===================================
        // T SampleLevel()
        // https://www.w3.org/TR/WGSL/#texturesamplelevel
        // ===================================

        // WGSL doesn't support textureSampleLevel for 1D texture

        // WGSL: textureSampleLevel({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleLevel(float2(u, u), 0))

        // WGSL: textureSampleLevel({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleLevel(float3(u, u, u), 0))

        // WGSL: textureSampleLevel({{\(*}}tCube
        && all(Tvn(T.Element(0)) == tCube.SampleLevel(normalize(float3(u, 1 - u, u)), 0))

        // WGSL: textureSampleLevel({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleLevel(float3(u, u, 0), 0))

        // WGSL: textureSampleLevel({{\(*}}tCubeArray
        && all(Tvn(T.Element(0)) == tCubeArray.SampleLevel(float4(normalize(float3(u, 1 - u, u)), 0), 0))

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureSampleLevel({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleLevel(float2(u, u), 0, int2(1, 1)))

        // W-GSL: textureSampleLevel({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleLevel(float3(u, u, u), 0, int3(1, 1, 1)))

        // W-GSL: textureSampleLevel({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleLevel(float3(u, u, 0), 0, int2(1, 1)))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // ==================
        // float SampleCmp()
        // https://www.w3.org/TR/WGSL/#texturesamplecompare
        // ==================

        // WGSL: textureSampleCompare({{\(*}}d2D
        && float(0) == d2D.SampleCmp(float2(u, u), 0)

        // WGSL: textureSampleCompare({{\(*}}d2DArray
        && float(0) == d2DArray.SampleCmp(float3(u, u, 0), 0)

        // WGSL: textureSampleCompare({{\(*}}dCube
        && float(0) == dCube.SampleCmp(normalize(float3(u, 1 - u, u)), 0)

        // WGSL: textureSampleCompare({{\(*}}dCubeArray
        && float(0) == dCubeArray.SampleCmp(float4(normalize(float3(u, 1 - u, u)), 0), 0)

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureSampleCompare({{\(*}}d2D
        && float(0) == d2D.SampleCmp(float2(u2, u), 0, int2(0, 0))

        // W-GSL: textureSampleCompare({{\(*}}d2DArray
        && float(0) == d2DArray.SampleCmp(float3(u2, u, u), 0, int2(0, 0))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // ===================================
        // float SampleCmpLevelZero()
        // https://www.w3.org/TR/WGSL/#texturesamplecomparelevel
        // ===================================

        // WGSL: textureSampleCompareLevel({{\(*}}d2D
        && float(0) == d2D.SampleCmpLevelZero(float2(u, u), 0)

        // WGSL: textureSampleCompareLevel({{\(*}}d2DArray
        && float(0) == d2DArray.SampleCmpLevelZero(float3(u, u, 0), 0)

        // WGSL: textureSampleCompareLevel({{\(*}}dCube
        && float(0) == dCube.SampleCmpLevelZero(normalize(float3(u, 1 - u, u)), 0)

        // WGSL: textureSampleCompareLevel({{\(*}}dCubeArray
        && float(0) == dCubeArray.SampleCmpLevelZero(float4(normalize(float3(u, 1-u, u)), 0), 0)

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureSampleCompareLevel({{\(*}}d2D
        && float(0) == d2D.SampleCmpLevelZero(float2(u2, u), 0, int2(0, 0))

        // W-GSL: textureSampleCompareLevel({{\(*}}d2DArray
        && float(0) == d2DArray.SampleCmpLevelZero(float3(u2, u, u), 0, int2(0, 0))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // ==================================
        // vector<T,4> Gather()
        // https://www.w3.org/TR/WGSL/#texturegather
        // ==================================

        // WGSL: textureGather({{.*}}t2D
        && all(Tv4(T.Element(0)) == t2D.Gather(float2(u, u)))

        // WGSL: textureGather({{.*}}tCube
        && all(Tv4(T.Element(0)) == tCube.Gather(normalize(float3(u, 1 - u, u))))

        // WGSL: textureGather({{.*}}t2DArray
        && all(Tv4(T.Element(0)) == t2DArray.Gather(float3(u, u, 0)))

        // WGSL: textureGather({{.*}}tCubeArray
        && all(Tv4(T.Element(0)) == tCubeArray.Gather(float4(normalize(float3(u, 1 - u, u)), 0)))

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureGather({{.*}}t2D
        && all(Tv4(T.Element(0)) == t2D.Gather(float2(u2, u), int2(0, 0)))

        // W-GSL: textureGather({{.*}}t2DArray
        && all(Tv4(T.Element(0)) == t2DArray.Gather(float3(u2, u, 0), int2(0, 0)))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET

        // =====================================
        // T SampleGrad()
        // https://www.w3.org/TR/WGSL/#texturesamplegrad
        // =====================================

        // WGSL doesn't support textureSampleGrad for 1D textures

        // WGSL: textureSampleGrad({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleGrad(float2(u, u), float2(ddx, ddx), float2(ddy, ddy)))

        // WGSL: textureSampleGrad({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleGrad(float3(u, u, u), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy)))

        // WGSL: textureSampleGrad({{\(*}}tCube
        && all(Tvn(T.Element(0)) == tCube.SampleGrad(normalize(float3(u, 1 - u, u)), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy)))

        // WGSL: textureSampleGrad({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleGrad(float3(u, u, 0.0f), float2(ddx, ddx), float2(ddy, ddy)))

        // WGSL: textureSampleGrad({{\(*}}tCubeArray
        && all(Tvn(T.Element(0)) == tCubeArray.SampleGrad(float4(normalize(float3(u, 1 - u, u)), 0), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy)))

#if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        // Offset variant

        // W-GSL: textureSampleGrad({{\(*}}t2D
        && all(Tvn(T.Element(0)) == t2D.SampleGrad(float2(u2, u), float2(ddx, ddx), float2(ddy, ddy), int2(0, 0)))

        // W-GSL: textureSampleGrad({{\(*}}t3D
        && all(Tvn(T.Element(0)) == t3D.SampleGrad(float3(u2, u, u), float3(ddx, ddx, ddx), float3(ddy, ddy, ddy), int3(0, 0, 0)))

        // W-GSL: textureSampleGrad({{\(*}}t2DArray
        && all(Tvn(T.Element(0)) == t2DArray.SampleGrad(float3(u2, u, 0.0f), float2(ddx, ddx), float2(ddy, ddy), int2(0, 0)))
#endif // #if TEST_WHEN_CONSTEXPR_WORKS_FOR_OFFSET
        ;

    return result;
}

void fragMain()
{
    bool result = true
        && TEST_texture<float3>(
            t1D_f32v3,
            t2D_f32v3,
            t3D_f32v3,
            tCube_f32v3,
            t1DArray_f32v3,
            t2DArray_f32v3,
            tCubeArray_f32v3)
        && TEST_texture<float4>(
            t1D_f32v4,
            t2D_f32v4,
            t3D_f32v4,
            tCube_f32v4,
            t1DArray_f32v4,
            t2DArray_f32v4,
            tCubeArray_f32v4)
        ;

    outputBuffer[0] = int(result);
}