summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2020-02-06 18:16:36 -0500
committerGitHub <noreply@github.com>2020-02-06 15:16:36 -0800
commit7981da51debc66aa78cda72a4b0be3fc3a74d634 (patch)
treeb5b4372530c1413f1d23f207c1936d93e4049ac9
parentd3331fba6eaab44646010b556106da38925d43e0 (diff)
Float matrix intrinsic test/fixes (#1203)
* Fix CPP construct when matrix type. * Test intrinsics on float matrices. * Fix typo in _areNearlyEqual test. Increased default sensitivity. Added matrix-float test.
-rw-r--r--source/slang/slang-emit-cpp.cpp9
-rw-r--r--tests/hlsl-intrinsic/matrix-float.slang133
-rw-r--r--tests/hlsl-intrinsic/matrix-float.slang.expected.txt5
-rw-r--r--tools/slang-test/slang-test-main.cpp4
4 files changed, 149 insertions, 2 deletions
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp
index ae1798382..99d1fbf22 100644
--- a/source/slang/slang-emit-cpp.cpp
+++ b/source/slang/slang-emit-cpp.cpp
@@ -1145,11 +1145,19 @@ void CPPSourceEmitter::_emitConstructConvertDefinition(const UnownedStringSlice&
emitType(retType);
writer->emit("{ ");
+
IRType* dstElemType = _getElementType(retType);
//IRType* srcElemType = _getElementType(srcType);
TypeDimension dim = _getTypeDimension(srcType, false);
+ UnownedStringSlice rowTypeName;
+ if (dim.rowCount > 1)
+ {
+ IRType* rowType = m_typeSet.addVectorType(dstElemType, int(dim.colCount));
+ rowTypeName = _getTypeName(rowType);
+ }
+
for (int i = 0; i < dim.rowCount; ++i)
{
if (dim.rowCount > 1)
@@ -1158,6 +1166,7 @@ void CPPSourceEmitter::_emitConstructConvertDefinition(const UnownedStringSlice&
{
writer->emit(", \n");
}
+ writer->emit(rowTypeName);
writer->emit("{ ");
}
diff --git a/tests/hlsl-intrinsic/matrix-float.slang b/tests/hlsl-intrinsic/matrix-float.slang
new file mode 100644
index 000000000..34591d3d9
--- /dev/null
+++ b/tests/hlsl-intrinsic/matrix-float.slang
@@ -0,0 +1,133 @@
+// NOTE we can't test on VK/gl at the moment because we don't support intrinsics over matrices on that target currently
+
+//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -output-using-type
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 -use-dxil -output-using-type
+//DISABLE_TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -output-using-type
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-cuda -compute -output-using-type
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+typedef matrix<float, 2, 2> FloatMatrix;
+typedef matrix<int, 2, 2> IntMatrix;
+typedef matrix<uint, 2, 2> UIntMatrix;
+typedef vector<float, 2> FloatVector;
+
+float calcTotal(vector<float, 2> v)
+{
+ return v.x + v.y;
+}
+
+float calcTotal(FloatMatrix v)
+{
+ return calcTotal(v[0]) + calcTotal(v[1]);
+}
+
+FloatMatrix makeFloatMatrix(float f)
+{
+ FloatMatrix m = { { f, f }, { f, f } };
+ return m;
+}
+
+IntMatrix makeIntMatrix(int v)
+{
+ IntMatrix m = { { v, v }, { v, v } };
+ return m;
+}
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int idx = int(dispatchThreadID.x);
+
+ float scalarF = idx * (1.0f / (4.0f));
+
+ FloatMatrix ft = {};
+
+ FloatMatrix f = { { scalarF + 0.01, scalarF + 0.02f}, { scalarF + 0.011f, scalarF + 0.022f}};
+
+ // fmod
+ ft += FloatMatrix(IntMatrix(((f % makeFloatMatrix(0.11f)) * makeFloatMatrix(100)) + makeFloatMatrix(0.5)));
+
+ ft += sin(f);
+
+ // Lets try some matrix/matrix
+ ft = f * ft;
+
+ // Lets try some vector matrix
+
+ {
+ FloatMatrix r = {mul(f[0], ft), mul(ft, f[1])};
+ ft += r;
+ }
+
+ // Back to the transcendentals
+
+ ft += cos(f);
+ ft += tan(f);
+
+ ft += asin(f);
+ ft += acos(f);
+ ft += atan(f);
+
+ ft += atan2(f, makeFloatMatrix(2));
+
+#if 0
+ // This fails from DXC with a validation error(!)
+ {
+ FloatMatrix sf, cf;
+ sincos(f, sf, cf);
+
+ ft += sf;
+ ft += cf;
+ }
+#endif
+
+ ft += rcp(makeFloatMatrix(1.0) + f);
+ ft += FloatMatrix(sign(f - makeFloatMatrix(0.5)));
+
+ ft += saturate(f * makeFloatMatrix(4) - makeFloatMatrix(2.0));
+
+ ft += sqrt(f);
+ ft += rsqrt(makeFloatMatrix(1.0f) + f);
+
+ ft += exp2(f);
+ ft += exp(f);
+
+ ft += frac(f * makeFloatMatrix(3));
+ ft += ceil(f * makeFloatMatrix(5) - makeFloatMatrix(3));
+
+ ft += floor(f * makeFloatMatrix(10) - makeFloatMatrix(7));
+ ft += trunc(f * makeFloatMatrix(7));
+
+ ft += log(f + makeFloatMatrix(10.0));
+ ft += log2(f * makeFloatMatrix(3) + makeFloatMatrix(2));
+
+ {
+ float scalarVs[] = { 1, 10, 100, 1000 };
+ ft += FloatMatrix(IntMatrix(log10(makeFloatMatrix(scalarVs[idx])) + makeFloatMatrix(0.5f)));
+ }
+
+ ft += abs(f * makeFloatMatrix(4) - makeFloatMatrix(2.0f));
+
+ ft += min(makeFloatMatrix(0.5), f);
+ ft += max(f, makeFloatMatrix(0.75));
+
+ ft += pow(makeFloatMatrix(0.5), f);
+
+ ft += smoothstep(makeFloatMatrix(0.2), makeFloatMatrix(0.7), f);
+ ft += lerp(makeFloatMatrix(-100), makeFloatMatrix(100), f);
+
+ ft += clamp(f, makeFloatMatrix(0.1), makeFloatMatrix(0.3));
+
+ ft += step(f, makeFloatMatrix(0.5));
+
+ IntMatrix vi = asint(makeFloatMatrix(idx));
+ ft += asfloat(vi);
+
+ UIntMatrix vu = asuint(f);
+ ft += asfloat(vu);
+
+ outputBuffer[idx] = calcTotal(ft);
+} \ No newline at end of file
diff --git a/tests/hlsl-intrinsic/matrix-float.slang.expected.txt b/tests/hlsl-intrinsic/matrix-float.slang.expected.txt
new file mode 100644
index 000000000..10941c176
--- /dev/null
+++ b/tests/hlsl-intrinsic/matrix-float.slang.expected.txt
@@ -0,0 +1,5 @@
+type: float
+-367.570129
+-128.128876
+139.494034
+414.148346
diff --git a/tools/slang-test/slang-test-main.cpp b/tools/slang-test/slang-test-main.cpp
index e703ed26c..165f7a322 100644
--- a/tools/slang-test/slang-test-main.cpp
+++ b/tools/slang-test/slang-test-main.cpp
@@ -2026,7 +2026,7 @@ static bool _areNearlyEqual(double a, double b, double epsilon)
// https://en.wikipedia.org/wiki/Double_precision_floating-point_format
//
- const double minNormal = 2.2250738585072014e10-308;
+ const double minNormal = 2.2250738585072014e-308;
// Either a or b are very close to being zero, so doing relative comparison isn't really appropriate
if (a == 0.0 || b == 0.0 || (absA + absB < minNormal))
@@ -2058,7 +2058,7 @@ static void _calcLines(const UnownedStringSlice& slice, List<UnownedStringSlice>
}
}
-static SlangResult _compareWithType(const UnownedStringSlice& actual, const UnownedStringSlice& ref, double differenceThreshold = 0.001)
+static SlangResult _compareWithType(const UnownedStringSlice& actual, const UnownedStringSlice& ref, double differenceThreshold = 0.0001)
{
typedef slang::TypeReflection::ScalarType ScalarType;