summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-07-17 09:26:53 -0700
committerTim Foley <tfoley@nvidia.com>2017-07-17 09:30:24 -0700
commitf881f52f6c34dbc2aa0a0701d6600467fae5a5ee (patch)
tree9fc05221b73181c906e37a62b0fd0da3552660fe
parentfdab35e93082d6da4f9dbb4b6ec7b4c6dbce831c (diff)
Add explicit operator overloads for scalar/matrix cases
Fixes #103 - Previously I was relying on scalar-to-vector promotion to pick the right type in these cases, but I hadn't implemented scalar-to-matrix promotion (I should...) - Rather than relying on promotion behavior, this change goes ahead and adds explicit overloads. I think this is probably a better decision in the long term, since one might want to support these cases for operators, while warning (or erroring) on the more general cases of implicit conversion. - This covers matrix/scalar, scalar/matrix, vector/scalar, and scalar/vector cases
-rw-r--r--source/slang/slang-stdlib.cpp21
-rw-r--r--tests/bugs/gh-103.slang19
-rw-r--r--tests/bugs/gh-103.slang.expected49
3 files changed, 89 insertions, 0 deletions
diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp
index 3450e72d7..d266c0560 100644
--- a/source/slang/slang-stdlib.cpp
+++ b/source/slang/slang-stdlib.cpp
@@ -1833,6 +1833,27 @@ namespace Slang
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << "matrix<" << leftType << ",N,M> left, matrix<" << rightType << ",N,M> right);\n";
break;
}
+
+ // We are going to go ahead and explicitly define combined
+ // operations for the scalar-op-vector, etc. cases, rather
+ // than rely on promotion rules.
+
+ // scalar-vector and scalar-matrix
+ if (!(op.flags & ASSIGNMENT))
+ {
+ sb << "__generic<let N : int> ";
+ sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(" << leftQual << leftType << " left, vector<" << rightType << ",N> right);\n";
+
+ sb << "__generic<let N : int, let M : int> ";
+ sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << leftType << " left, matrix<" << rightType << ",N,M> right);\n";
+ }
+
+ // vector-scalar and matrix-scalar
+ sb << "__generic<let N : int> ";
+ sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(" << leftQual << "vector<" << leftType << ",N> left, " << rightType << " right);\n";
+
+ sb << "__generic<let N : int, let M : int> ";
+ sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << "matrix<" << leftType << ",N,M> left, " << rightType << " right);\n";
}
}
diff --git a/tests/bugs/gh-103.slang b/tests/bugs/gh-103.slang
new file mode 100644
index 000000000..2b10c2b3f
--- /dev/null
+++ b/tests/bugs/gh-103.slang
@@ -0,0 +1,19 @@
+//TEST:COMPARE_HLSL: -profile ps_4_0 -entry main
+
+// Ensure that matrix-times-scalar works
+
+float4x4 doIt(float4x4 a, float b)
+{
+ return a * b;
+}
+
+cbuffer C
+{
+ float4x4 a;
+ float b;
+};
+
+float4 main() : SV_Target
+{
+ return doIt(a, b)[0];
+}
diff --git a/tests/bugs/gh-103.slang.expected b/tests/bugs/gh-103.slang.expected
new file mode 100644
index 000000000..acf3026d9
--- /dev/null
+++ b/tests/bugs/gh-103.slang.expected
@@ -0,0 +1,49 @@
+result code = 0
+standard error = {
+}
+standard output = {
+//
+// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
+//
+//
+// Buffer Definitions:
+//
+// cbuffer C
+// {
+//
+// float4x4 a; // Offset: 0 Size: 64
+// float b; // Offset: 64 Size: 4
+//
+// }
+//
+//
+// Resource Bindings:
+//
+// Name Type Format Dim Slot Elements
+// ------------------------------ ---------- ------- ----------- ---- --------
+// C cbuffer NA NA 0 1
+//
+//
+//
+// Input signature:
+//
+// Name Index Mask Register SysValue Format Used
+// -------------------- ----- ------ -------- -------- ------- ------
+// no Input
+//
+// Output signature:
+//
+// Name Index Mask Register SysValue Format Used
+// -------------------- ----- ------ -------- -------- ------- ------
+// SV_Target 0 xyzw 0 TARGET float xyzw
+//
+ps_4_0
+dcl_constantbuffer cb0[5], immediateIndexed
+dcl_output o0.xyzw
+mul o0.x, cb0[0].x, cb0[4].x
+mul o0.y, cb0[1].x, cb0[4].x
+mul o0.z, cb0[2].x, cb0[4].x
+mul o0.w, cb0[3].x, cb0[4].x
+ret
+// Approximately 5 instruction slots used
+}