vkd3d-shader/hlsl: Use a block in hlsl_normalize_binary_exprs().
[vkd3d.git] / tests / hlsl / ternary.shader_test
blobfd1b3efd320d6dc327738075ce2e89f50a364fac
1 [require]
2 % The ternary operator works differently in sm6. See sm6-ternary.shader_test.
3 shader model < 6.0
6 [pixel shader]
7 uniform float4 x;
9 float4 main() : sv_target
11     return x.x ? x : x - 1;
14 [test]
15 uniform 0 float4 2.0 3.0 4.0 5.0
16 draw quad
17 probe (0, 0) rgba (2.0, 3.0, 4.0, 5.0)
18 uniform 0 float4 0.0 10.0 11.0 12.0
19 draw quad
20 probe (0, 0) rgba (-1.0, 9.0, 10.0, 11.0)
23 [pixel shader]
24 uniform float4 x;
26 float4 main() : sv_target
28     float4 ret;
30     ret.x = x.x ? x.x : 1;
31     ret.y = x.y ? 2 : x.y;
32     ret.z = ret.w = 0.0;
33     return ret;
36 [test]
37 uniform 0 float4 1.1 3.0 4.0 5.0
38 draw quad
39 probe (0, 0) rgba (1.1, 2.0, 0.0, 0.0)
42 [pixel shader]
43 float4 f;
45 float4 main() : sv_target
47     float f1 = 0.1, f2 = 0.2, f3;
48     f3 = f.x ? (f1 = 0.5) + 0.2 : (f2 = 0.6);
49     return float4(f1, f2, f3, 0.0);
52 [test]
53 uniform 0 float4 1.0 0.0 0.0 0.0
54 draw quad
55 probe (0, 0) rgba (0.5, 0.6, 0.7, 0.0)
58 [pixel shader]
59 float4 x, y, z;
61 float4 main() : sv_target
63     return x ? y : z;
66 [test]
67 uniform 0 float4 0.0 1.0 0.0 -3.0
68 uniform 4 float4 1.0 2.0 3.0 4.0
69 uniform 8 float4 5.0 6.0 7.0 8.0
70 draw quad
71 probe (0, 0) rgba (5.0, 2.0, 7.0, 4.0)
74 % The usual type conversion is applied to the first and second expression, as
75 % long as they are numeric.
77 [pixel shader fail]
79 uniform float2x4 a;
80 uniform float3x2 b;
82 float4 main() : sv_target
84     0 ? a : b;
85     return 0;
89 [pixel shader]
91 static float3x3 a = {0, 1, 2, 3, 4, 5, 6, 7, 8};
92 static float2x2 b = {.1, .2, .3, .4};
94 float4 main() : sv_target
96     return float4(1 ? a : b);
99 [test]
100 draw quad
101 probe (0, 0) rgba (0.0, 1.0, 3.0, 4.0)
104 [pixel shader]
106 static float3 a = {0, 1, 2};
107 static float4 b = {5, 6, 7, 8};
109 float4 main() : sv_target
111     return float4(0 ? a : b, 1.0);
114 [test]
115 draw quad
116 probe (0, 0) rgba (5.0, 6.0, 7.0, 1.0)
119 % More restrictions are placed on the type of the first (condition) operand,
120 % relative to the common type computed from the other two (result). Either:
121 % * the class and dimensions must match exactly;
122 % * the condition operand is scalar;
123 % * the result operands are scalar;
124 % * one is a typeN and the other is a type1xN
126 [pixel shader fail]
128 uniform float4 cond;
129 uniform float2x2 a, b;
131 float4 main() : sv_target
133     return float4(cond ? a : b);
137 [pixel shader fail]
139 uniform float2x2 cond;
140 uniform float4 a, b;
142 float4 main() : sv_target
144     return float4(cond ? a : b);
148 [pixel shader]
150 static float2x2 cond = {1, 0, 0, 1};
151 static float2x2 a = {1, 2, 3, 4};
152 static float2x2 b = {5, 6, 7, 8};
154 float4 main() : sv_target
156     return float4(cond ? a : b);
159 [test]
160 draw quad
161 probe (0, 0) rgba (1.0, 6.0, 7.0, 4.0)
164 [pixel shader fail]
166 uniform float3 cond;
167 uniform float4 a, b;
169 float4 main() : sv_target
171     (cond ? a : b);
172     return 0;
176 [pixel shader fail]
178 uniform float4 cond;
179 uniform float4x1 a, b;
181 float4 main() : sv_target
183     (cond ? a : b);
184     return 0;
188 % As may be expected, this yields the type of the arguments, not the conditional.
190 [pixel shader]
192 static float4 cond = {1, 0, 0, 1};
193 static float1x4 a = {1, 2, 3, 4};
194 static float1x4 b = {5, 6, 7, 8};
196 float4 main() : sv_target
198     return (cond ? a : b)[0];
201 [test]
202 draw quad
203 probe (0, 0) rgba (1.0, 6.0, 7.0, 4.0)
206 [pixel shader]
208 static float1x4 cond = {1, 0, 0, 1};
209 static float4 a = {1, 2, 3, 4};
210 static float4 b = {5, 6, 7, 8};
212 float4 main() : sv_target
214     return (cond ? a : b)[0];
217 [test]
218 draw quad
219 probe (0, 0) rgba (1.0, 1.0, 1.0, 1.0)
222 [pixel shader fail]
224 uniform float1x4 cond;
225 uniform float4 a, b;
227 float4 main() : sv_target
229     return (cond ? a : b)[0][0];
233 [pixel shader]
235 static float1 cond = {0};
236 static float1x1 a = {2};
237 static float1x1 b = {3};
239 float4 main() : sv_target
241     return (cond ? a : b)[0][0];
244 [test]
245 draw quad
246 probe (0, 0) rgba (3.0, 3.0, 3.0, 3.0)
249 [pixel shader]
251 static float1x1 cond = {1};
252 static float1 a = {2};
253 static float1 b = {3};
255 float4 main() : sv_target
257     return (cond ? a : b)[0];
260 [test]
261 draw quad
262 probe (0, 0) rgba (2.0, 2.0, 2.0, 2.0)
265 [pixel shader fail]
267 static float1x1 cond = {1};
268 static float1 a = {2};
269 static float1 b = {3};
271 float4 main() : sv_target
273     return (cond ? a : b)[0][0];
277 [pixel shader]
279 uniform float cond;
280 uniform float4 a, b;
282 float4 main() : sv_target
284     return float4(cond ? a : b);
287 [test]
288 uniform 0 float4 1.0 0.0 0.0 0.0
289 uniform 4 float4 1.0 2.0 3.0 4.0
290 uniform 8 float4 5.0 6.0 7.0 8.0
291 draw quad
292 probe (0, 0) rgba (1.0, 2.0, 3.0, 4.0)
295 [pixel shader]
297 // "scalar" can mean any 1-component numeric type.
298 static float1x1 cond = {0};
299 static float4 a = {1, 2, 3, 4};
300 static float4 b = {5, 6, 7, 8};
302 float4 main() : sv_target
304     return float4(cond ? a : b);
307 [test]
308 draw quad
309 probe (0, 0) rgba (5.0, 6.0, 7.0, 8.0)
312 [pixel shader]
314 uniform float4 cond;
315 uniform float4 a, b;
317 float4 main() : sv_target
319     return float4(cond ? a.x : b.x);
322 [test]
323 uniform 0 float4 1.0 0.0 1.0 0.0
324 uniform 4 float4 1.0 2.0 3.0 4.0
325 uniform 8 float4 5.0 6.0 7.0 8.0
326 draw quad
327 probe (0, 0) rgba (1.0, 5.0, 1.0, 5.0)
330 [pixel shader]
332 // "scalar" can mean any 1-component numeric type.
333 static float4 cond = {1, 0, 0, 1};
334 static float1x1 a = {2};
335 static float1x1 b = {3};
337 float4 main() : sv_target
339     return float4(cond ? a : b);
342 [test]
343 draw quad
344 probe (0, 0) rgba (2.0, 3.0, 3.0, 2.0)
347 % Objects can be used, but their types have to be identical.
349 [pixel shader]
350 Texture2D t;
352 float4 main() : sv_target
354     Texture2D tmp = 0 ? t : t;
355     return 0;
359 [pixel shader fail todo]
360 Texture2D t;
361 float4 f;
363 float4 main() : sv_target
365     f ? t : t;
366     return 0;
370 [pixel shader fail]
371 Texture2D t;
372 Texture3D u;
374 float4 main() : sv_target
376     0 ? t : u;
377     return 0;
381 % Of course objects cannot be used as the condition.
383 [pixel shader fail]
384 Texture2D t;
386 float4 main() : sv_target
388     t ? 0 : 1;
389     return 0;