vector-syntax.hlsl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify %s
  2. float3 f3_ones = 1.0.xxx;
  3. float3 f3_ones_exp = 2.0e+2.rrr;
  4. vector v;
  5. vector<float, 1+1> v1p1;
  6. /*verify-ast
  7. VarDecl <col:1, col:20> col:20 v1p1 'vector<float, 1 + 1>':'vector<float, 2>'
  8. */
  9. static const int i = 1;
  10. vector<float, i+i> vfi;
  11. /*verify-ast
  12. VarDecl <col:1, col:20> col:20 vfi 'vector<float, i + i>':'vector<float, 2>'
  13. */
  14. static const int2 g_i2 = {1,2};
  15. /*verify-ast
  16. VarDecl <col:1, col:30> col:19 used g_i2 'const int2':'const vector<int, 2>' static cinit
  17. `-InitListExpr <col:26, col:30> 'const int2':'const vector<int, 2>'
  18. |-ImplicitCastExpr <col:27> 'int' <IntegralCast>
  19. | `-IntegerLiteral <col:27> 'literal int' 1
  20. `-ImplicitCastExpr <col:29> 'int' <IntegralCast>
  21. `-IntegerLiteral <col:29> 'literal int' 2
  22. */
  23. // Clang front-end currently doesn't support non-basic types in constant expressions, such as below:
  24. vector<float, g_i2.y> v_const_vec_expr2; /* expected-error {{non-type template argument of type 'int' is not an integral constant expression}} fxc-pass {{}} */
  25. vector<float, g_i2.x + g_i2.y> v_const_vec_expr3; /* expected-error {{non-type template argument of type 'int' is not an integral constant expression}} fxc-pass {{}} */
  26. void use_const_vec_expr() {
  27. float2 f2 = v_const_vec_expr2.xy;
  28. float3 f3 = v_const_vec_expr3.xyz;
  29. // These should fail (but don't in clang, since it couldn't construct the original vectors)
  30. float f_error2 = v_const_vec_expr2.z; /* fxc-error {{X3018: invalid subscript 'z'}} */
  31. float f_error3 = v_const_vec_expr3.w; /* fxc-error {{X3018: invalid subscript 'w'}} */
  32. }
  33. void abs_without_using_result() {
  34. vector<float, 4> myvector;
  35. abs(myvector); /* expected-warning {{ignoring return value of function declared with const attribute}} fxc-pass {{}} */
  36. vector<float, 4> myvector2;
  37. abs(myvector2); /* expected-warning {{ignoring return value of function declared with const attribute}} fxc-pass {{}} */
  38. }
  39. void abs_with_assignment() {
  40. vector<float, 4> myvector;
  41. vector<float, 4> absvector;
  42. absvector = abs(myvector);
  43. }
  44. vector<float, 4> abs_for_result(vector<float, 4> value) {
  45. return abs(value);
  46. }
  47. void fn_use_vector(vector<float, 4> value) { }
  48. void abs_in_argument() {
  49. vector<float, 4> myvector;
  50. fn_use_vector(abs(myvector));
  51. /*verify-ast
  52. CallExpr <col:5, col:32> 'void'
  53. |-ImplicitCastExpr <col:5> 'void (*)(vector<float, 4>)' <FunctionToPointerDecay>
  54. | `-DeclRefExpr <col:5> 'void (vector<float, 4>)' lvalue Function 'fn_use_vector' 'void (vector<float, 4>)'
  55. `-CallExpr <col:19, col:31> 'vector<float, 4>':'vector<float, 4>'
  56. |-ImplicitCastExpr <col:19> 'vector<float, 4> (*)(vector<float, 4>)' <FunctionToPointerDecay>
  57. | `-DeclRefExpr <col:19> 'vector<float, 4> (vector<float, 4>)' lvalue Function 'abs' 'vector<float, 4> (vector<float, 4>)'
  58. `-ImplicitCastExpr <col:23> 'vector<float, 4>':'vector<float, 4>' <LValueToRValue>
  59. `-DeclRefExpr <col:23> 'vector<float, 4>':'vector<float, 4>' lvalue Var 'myvector' 'vector<float, 4>':'vector<float, 4>'
  60. */
  61. }
  62. void vector_on_demand() {
  63. float4 thevector;
  64. float4 anothervector;
  65. bool2 boolvector;
  66. }
  67. void abs_on_demand() {
  68. float2 f2;
  69. float2 result = abs(f2);
  70. }
  71. void vector_out_of_bounds() {
  72. vector<float, 8> vector_oob_0; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3052: vector dimension must be between 1 and 4}}
  73. vector<float, 0> vector_oob_1; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3052: vector dimension must be between 1 and 4}}
  74. vector<float, -1> vector_oob_2; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3052: vector dimension must be between 1 and 4}}
  75. }
  76. void vector_unsigned() {
  77. unsigned int4 intvector;
  78. unsigned min16int4 min16vector; /* fxc-error {{X3085: unsigned can not be used with type}} */
  79. unsigned int64_t3 int64vector; /* */ /* fxc-error {{X3000: syntax error: unexpected token 'int64_t3'}} */
  80. unsigned uint3 uintvector; /* fxc-error {{X3085: unsigned can not be used with type}} */
  81. unsigned min16uint4 min16uintvector; /* fxc-error {{X3085: unsigned can not be used with type}} */
  82. unsigned uint64_t2 int64uintvector; /* */ /* fxc-error {{X3000: syntax error: unexpected token 'uint64_t2'}} */
  83. unsigned dword3 dwordvector; /* fxc-error {{X3000: syntax error: unexpected token 'dword3'}} */
  84. unsigned float2 floatvector; /* expected-error {{'float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  85. unsigned bool3 boolvector; /* expected-error {{'bool' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  86. unsigned half4 halfvector; /* expected-error {{'half' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  87. unsigned double1 doublevector; /* expected-error {{'double' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  88. unsigned min12int2 min12intvector; /* expected-error {{'min12int' cannot be signed or unsigned}} expected-warning {{min12int is promoted to min16int}} fxc-error {{X3085: unsigned can not be used with type}} */
  89. unsigned min16float3 min16floatvector; /* expected-error {{'min16float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  90. }
  91. float fn() {
  92. float4 myvar = float4(1,2,3,4);
  93. myvar.x = 1.0f;
  94. myvar.y = 1.0f;
  95. myvar.z = 1.0f;
  96. myvar.w = 1.0f;
  97. myvar[0] = 1.0f;
  98. myvar[1]= 1.0f;
  99. myvar[2] = 1.0f;
  100. myvar[3] = 1.0f;
  101. myvar[-10] = 1.0f; /* expected-error {{vector element index '-10' is out of bounds}} fxc-pass {{}} */
  102. myvar[4] = 1.0f; /* expected-error {{vector element index '4' is out of bounds}} fxc-pass {{}} */
  103. float3 myvar3 = float3(1,2,3);
  104. myvar3[3] = 1.0f; /* expected-error {{vector element index '3' is out of bounds}} fxc-pass {{}} */
  105. float2 myvar2 = float2(1,2);
  106. myvar2[2] = 1.0f; /* expected-error {{vector element index '2' is out of bounds}} fxc-pass {{}} */
  107. float1 myvar1 = float1(1);
  108. myvar1[1] = 1.0f; /* expected-error {{vector element index '1' is out of bounds}} fxc-pass {{}} */
  109. const float4 constMyVar = float4(1,2,3,4); /* expected-note {{variable 'constMyVar' declared const here}} expected-note {{variable 'constMyVar' declared const here}} fxc-pass {{}} */
  110. constMyVar = float4(1,1,1,1); /* expected-error {{cannot assign to variable 'constMyVar' with const-qualified type 'const float4'}} fxc-error {{X3025: l-value specifies const object}} */
  111. constMyVar[0] = 2.0f; /* expected-error {{cannot assign to variable 'constMyVar' with const-qualified type 'const float4'}} fxc-error {{X3025: l-value specifies const object}} */
  112. float4 myothervar;
  113. myothervar.rgba = myvar.xyzw;
  114. /*verify-ast
  115. BinaryOperator <col:5, col:29> 'vector<float, 4>':'vector<float, 4>' '='
  116. |-HLSLVectorElementExpr <col:5, col:16> 'vector<float, 4>':'vector<float, 4>' lvalue vectorcomponent rgba
  117. | `-DeclRefExpr <col:5> 'float4':'vector<float, 4>' lvalue Var 'myothervar' 'float4':'vector<float, 4>'
  118. `-ImplicitCastExpr <col:23, col:29> 'vector<float, 4>':'vector<float, 4>' <LValueToRValue>
  119. `-HLSLVectorElementExpr <col:23, col:29> 'vector<float, 4>':'vector<float, 4>' lvalue vectorcomponent xyzw
  120. `-DeclRefExpr <col:23> 'float4':'vector<float, 4>' lvalue Var 'myvar' 'float4':'vector<float, 4>'
  121. */
  122. float f;
  123. f.x = 1;
  124. uint u;
  125. u = f.x;
  126. uint3 u3;
  127. u3.xyz = f.xxx;
  128. /*verify-ast
  129. BinaryOperator <col:5, col:16> 'vector<uint, 3>':'vector<unsigned int, 3>' '='
  130. |-HLSLVectorElementExpr <col:5, col:8> 'vector<uint, 3>':'vector<unsigned int, 3>' lvalue vectorcomponent xyz
  131. | `-DeclRefExpr <col:5> 'uint3':'vector<unsigned int, 3>' lvalue Var 'u3' 'uint3':'vector<unsigned int, 3>'
  132. `-ImplicitCastExpr <col:14, col:16> 'vector<unsigned int, 3>' <HLSLCC_FloatingToIntegral>
  133. `-HLSLVectorElementExpr <col:14, col:16> 'vector<float, 3>':'vector<float, 3>' xxx
  134. `-ImplicitCastExpr <col:14> 'vector<float, 1>':'vector<float, 1>' lvalue <HLSLVectorSplat>
  135. `-DeclRefExpr <col:14> 'float' lvalue Var 'f' 'float'
  136. */
  137. u3 = (!u3).zyx;
  138. /*verify-ast
  139. BinaryOperator <col:5, col:16> 'uint3':'vector<unsigned int, 3>' '='
  140. |-DeclRefExpr <col:5> 'uint3':'vector<unsigned int, 3>' lvalue Var 'u3' 'uint3':'vector<unsigned int, 3>'
  141. `-ImplicitCastExpr <col:10, col:16> 'vector<unsigned int, 3>' <HLSLCC_IntegralCast>
  142. `-HLSLVectorElementExpr <col:10, col:16> 'vector<bool, 3>':'vector<bool, 3>' zyx
  143. `-ParenExpr <col:10, col:14> 'vector<bool, 3>':'vector<bool, 3>'
  144. `-UnaryOperator <col:11, col:12> 'vector<bool, 3>':'vector<bool, 3>' prefix '!'
  145. `-ImplicitCastExpr <col:12> 'vector<bool, 3>' <HLSLCC_IntegralToBoolean>
  146. `-ImplicitCastExpr <col:12> 'uint3':'vector<unsigned int, 3>' <LValueToRValue>
  147. `-DeclRefExpr <col:12> 'uint3':'vector<unsigned int, 3>' lvalue Var 'u3' 'uint3':'vector<unsigned int, 3>'
  148. */
  149. f.xx = 2; // expected-error {{vector is not assignable (contains duplicate components)}} fxc-error {{X3025: l-value specifies const object}}
  150. u3.x = u3.w; /* expected-error {{vector swizzle 'w' is out of bounds}} fxc-error {{X3018: invalid subscript 'w'}} */
  151. return f.x;
  152. }
  153. // float4 main(float4 param4 : FOO) : FOO {
  154. float4 plain(float4 param4 /* : FOO */) /*: FOO */{
  155. return fn();
  156. }