matrix-syntax.hlsl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify %s
  2. // To test with the classic compiler, run
  3. // %sdxroot%\tools\x86\fxc.exe /T vs_5_1 matrix-syntax.hlsl
  4. matrix m;
  5. void abs_without_using_result() {
  6. matrix<float, 4, 4> mymatrix;
  7. abs(mymatrix); /* expected-warning {{ignoring return value of function declared with const attribute}} fxc-pass {{}} */
  8. matrix<float, 1, 4> mymatrix2;
  9. abs(mymatrix2); /* expected-warning {{ignoring return value of function declared with const attribute}} fxc-pass {{}} */
  10. }
  11. void abs_with_assignment() {
  12. matrix<float, 4, 4> mymatrix;
  13. matrix<float, 4, 4> absMatrix;
  14. absMatrix = abs(mymatrix);
  15. }
  16. matrix<float, 4, 4> abs_for_result(matrix<float, 4, 4> value) {
  17. return abs(value);
  18. }
  19. void fn_use_matrix(matrix<float, 4, 4> value) { }
  20. void abs_in_argument() {
  21. matrix<float, 4, 4> mymatrix;
  22. fn_use_matrix(abs(mymatrix));
  23. /*verify-ast
  24. CallExpr <col:5, col:32> 'void'
  25. |-ImplicitCastExpr <col:5> 'void (*)(matrix<float, 4, 4>)' <FunctionToPointerDecay>
  26. | `-DeclRefExpr <col:5> 'void (matrix<float, 4, 4>)' lvalue Function 'fn_use_matrix' 'void (matrix<float, 4, 4>)'
  27. `-CallExpr <col:19, col:31> 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  28. |-ImplicitCastExpr <col:19> 'matrix<float, 4, 4> (*)(matrix<float, 4, 4>)' <FunctionToPointerDecay>
  29. | `-DeclRefExpr <col:19> 'matrix<float, 4, 4> (matrix<float, 4, 4>)' lvalue Function 'abs' 'matrix<float, 4, 4> (matrix<float, 4, 4>)'
  30. `-ImplicitCastExpr <col:23> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' <LValueToRValue>
  31. `-DeclRefExpr <col:23> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' lvalue Var 'mymatrix' 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  32. */
  33. }
  34. void matrix_on_demand() {
  35. float4x4 thematrix;
  36. float4x4 anotherMatrix;
  37. bool2x1 boolMatrix;
  38. unsigned int4x2 unsignedMatrix;
  39. }
  40. void abs_on_demand() {
  41. float1x2 f12;
  42. float1x2 result = abs(f12);
  43. }
  44. void matrix_out_of_bounds() {
  45. matrix<float, 1, 8> matrix_oob_0; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3053: matrix dimensions must be between 1 and 4}}
  46. matrix<float, 0, 1> matrix_oob_1; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3053: matrix dimensions must be between 1 and 4}}
  47. matrix<float, -1, 1> matrix_oob_2; // expected-error {{invalid value, valid range is between 1 and 4 inclusive}} fxc-error {{X3053: matrix dimensions must be between 1 and 4}}
  48. }
  49. void matrix_unsigned() {
  50. unsigned int4x2 intMatrix;
  51. unsigned min16int4x3 min16Matrix; /* fxc-error {{X3085: unsigned can not be used with type}} */
  52. unsigned int64_t3x3 int64Matrix; /* fxc-error {{X3000: syntax error: unexpected token 'int64_t3x3'}} */
  53. unsigned uint3x4 uintMatrix; /* fxc-error {{X3085: unsigned can not be used with type}} */
  54. unsigned min16uint4x1 min16uintMatrix; /* fxc-error {{X3085: unsigned can not be used with type}} */
  55. unsigned uint64_t2x2 int64uintMatrix; /* fxc-error {{X3000: syntax error: unexpected token 'uint64_t2x2'}} */
  56. unsigned dword3x2 dwordvector; /* fxc-error {{X3000: syntax error: unexpected token 'dword3x2'}} */
  57. unsigned float2x3 floatMatrix; /* expected-error {{'float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  58. unsigned bool3x4 boolvector; /* expected-error {{'bool' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  59. unsigned half4x1 halfvector; /* expected-error {{'half' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  60. unsigned double1x2 doublevector; /* expected-error {{'double' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  61. unsigned min12int2x3 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}} */
  62. unsigned min16float3x4 min16floatvector; /* expected-error {{'min16float' cannot be signed or unsigned}} fxc-error {{X3085: unsigned can not be used with type}} */
  63. }
  64. void main() {
  65. // Multiple assignments in a chain.
  66. matrix<float, 4, 4> mymatrix;
  67. matrix<float, 4, 4> absMatrix = abs(mymatrix);
  68. matrix<float, 4, 4> absMatrix2 = abs(absMatrix);
  69. const matrix<float, 4, 4> myConstMatrix = mymatrix; /* expected-note {{variable 'myConstMatrix' declared const here}} expected-note {{variable 'myConstMatrix' declared const here}} fxc-pass {{}} */
  70. matrix<float, 2, 4> f24;
  71. float f;
  72. float2 f2;
  73. float3 f3;
  74. float4 f4;
  75. float farr2[2];
  76. // zero-based positions.
  77. f = mymatrix._m00;
  78. f2 = mymatrix._m00_m11;
  79. f4 = mymatrix._m00_m11_m00_m11;
  80. /*verify-ast
  81. BinaryOperator <col:5, col:19> 'float4':'vector<float, 4>' '='
  82. |-DeclRefExpr <col:5> 'float4':'vector<float, 4>' lvalue Var 'f4' 'float4':'vector<float, 4>'
  83. `-ExtMatrixElementExpr <col:10, col:19> 'vector<float, 4>':'vector<float, 4>' _m00_m11_m00_m11
  84. `-DeclRefExpr <col:10> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' lvalue Var 'mymatrix' 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  85. */
  86. //fxc error X3018 : invalid subscript '_m00_11'
  87. f2 = mymatrix._m00_11; // expected-error {{matrix subscript '_m00_11' mixes one-based and zero-based references}} fxc-error {{X3018: invalid subscript '_m00_11'}}
  88. //fxc error X3018: invalid subscript '_m00_m11_m00_m11_m00_m11_m00_m11'
  89. f24 = mymatrix._m00_m11_m00_m11_m00_m11_m00_m11; // expected-error {{more than four positions are referenced in '_m00_m11_m00_m11_m00_m11_m00_m11'}} fxc-error {{X3018: invalid subscript '_m00_m11_m00_m11_m00_m11_m00_m11'}}
  90. //fxc error X3017: cannot convert from 'float2' to 'float[2]'
  91. farr2 = mymatrix._m00_m01; // expected-error {{cannot implicitly convert from 'vector<float, 2>' to 'float [2]'}} fxc-error {{X3017: cannot convert from 'float2' to 'float[2]'}}
  92. //fxc error X3018: invalid subscript '_m04'
  93. f = mymatrix._m04; // expected-error {{the digit '4' is used in '_m04', but the syntax is for zero-based rows and columns}} fxc-error {{X3018: invalid subscript '_m04'}}
  94. f2 = mymatrix._m00_m01;
  95. //fxc error X3017: cannot implicitly convert from 'float2' to 'float3'
  96. f3 = mymatrix._m00_m01; // expected-error {{cannot convert from 'vector<float, 2>' to 'float3'}} fxc-error {{X3017: cannot implicitly convert from 'float2' to 'float3'}}
  97. //fxc warning X3206: implicit truncation of vector type
  98. f2 = mymatrix._m00_m01_m00; // expected-warning {{implicit truncation of vector type}} fxc-warning {{X3206: implicit truncation of vector type}}
  99. mymatrix._m00 = mymatrix._m01;
  100. mymatrix._m00_m11_m02_m13 = mymatrix._m10_m21_m10_m21;
  101. /*verify-ast
  102. BinaryOperator <col:5, col:42> 'vector<float, 4>':'vector<float, 4>' '='
  103. |-ExtMatrixElementExpr <col:5, col:14> 'vector<float, 4>':'vector<float, 4>' lvalue vectorcomponent _m00_m11_m02_m13
  104. | `-DeclRefExpr <col:5> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' lvalue Var 'mymatrix' 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  105. `-ExtMatrixElementExpr <col:33, col:42> 'vector<float, 4>':'vector<float, 4>' _m10_m21_m10_m21
  106. `-DeclRefExpr <col:33> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' lvalue Var 'mymatrix' 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  107. */
  108. //fxc error X3025: l-value specifies const object
  109. mymatrix._m00_m11_m00_m11 = mymatrix._m10_m21_m10_m21; // expected-error {{matrix is not assignable (contains duplicate components)}} fxc-error {{X3025: l-value specifies const object}}
  110. // one-based positions.
  111. //fxc error X3018: invalid subscript '_00'
  112. f = mymatrix._00; // expected-error {{the digit '0' is used in '_00', but the syntax is for one-based rows and columns}} fxc-error {{X3018: invalid subscript '_00'}}
  113. f = mymatrix._11;
  114. f2 = mymatrix._11_11;
  115. f4 = mymatrix._11_11_44_44;
  116. /*verify-ast
  117. BinaryOperator <col:5, col:19> 'float4':'vector<float, 4>' '='
  118. |-DeclRefExpr <col:5> 'float4':'vector<float, 4>' lvalue Var 'f4' 'float4':'vector<float, 4>'
  119. `-ExtMatrixElementExpr <col:10, col:19> 'vector<float, 4>':'vector<float, 4>' _11_11_44_44
  120. `-DeclRefExpr <col:10> 'matrix<float, 4, 4>':'matrix<float, 4, 4>' lvalue Var 'mymatrix' 'matrix<float, 4, 4>':'matrix<float, 4, 4>'
  121. */
  122. // member assignment using subscript syntax
  123. f = mymatrix[0][0];
  124. f = mymatrix[1][1];
  125. f2 = mymatrix[1].xx;
  126. f4 = mymatrix[2];
  127. f = mymatrix[0][4]; /* expected-error {{vector element index '4' is out of bounds}} fxc-pass {{}} */
  128. f = mymatrix[-1][3]; /* expected-error {{matrix row index '-1' is out of bounds}} fxc-pass {{}} */
  129. f4 = mymatrix[10]; /* expected-error {{matrix row index '10' is out of bounds}} fxc-pass {{}} */
  130. // accessing const member
  131. f = myConstMatrix[0][0];
  132. f = myConstMatrix[1][1];
  133. f2 = myConstMatrix[1].xx;
  134. f4 = myConstMatrix[2];
  135. myConstMatrix[0][0] = 3; /* expected-error {{cannot assign to variable 'myConstMatrix' with const-qualified type 'const matrix<float, 4, 4>'}} fxc-error {{X3025: l-value specifies const object}} */
  136. myConstMatrix[3] = float4(1,2,3,4); /* expected-error {{cannot assign to variable 'myConstMatrix' with const-qualified type 'const matrix<float, 4, 4>'}} fxc-error {{X3025: l-value specifies const object}} */
  137. }