summaryrefslogtreecommitdiffstats
path: root/tests/compute
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2025-10-11 01:42:43 +0900
committerGitHub <noreply@github.com>2025-10-10 16:42:43 +0000
commitb4023f715885ada9a2777ea3b0d6d9739860b39b (patch)
tree6bceec79ba7747e6826603f3893f89e4697975a4 /tests/compute
parent48afbf9a1075fcf541b2c196c6313aeda57e9637 (diff)
implement dot products for 1 vectors (#8599)
Closes https://github.com/shader-slang/slang/issues/8378
Diffstat (limited to 'tests/compute')
-rw-r--r--tests/compute/dot1-generic.slang77
-rw-r--r--tests/compute/dot1.slang32
2 files changed, 109 insertions, 0 deletions
diff --git a/tests/compute/dot1-generic.slang b/tests/compute/dot1-generic.slang
new file mode 100644
index 000000000..aed2c9a39
--- /dev/null
+++ b/tests/compute/dot1-generic.slang
@@ -0,0 +1,77 @@
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-dx12 -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-mtl -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-wgsl -output-using-type
+
+// Test for dot product with 1-element vectors called from a generic function
+
+// CHECK: 20
+
+//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+// Generic function that computes dot product for N-sized float vectors
+__generic<let N : int>
+float genericDotFloat(vector<float, N> a, vector<float, N> b)
+{
+ return dot(a, b);
+}
+
+// Generic function that computes dot product for N-sized int vectors
+__generic<let N : int>
+int genericDotInt(vector<int, N> a, vector<int, N> b)
+{
+ return dot(a, b);
+}
+
+// Generic function for testing with different N values
+__generic<let N : int>
+float testFloatDot(float value)
+{
+ vector<float, N> vec1;
+ vector<float, N> vec2;
+
+ // Initialize all components to the same value
+ for (int i = 0; i < N; i++)
+ {
+ vec1[i] = value;
+ vec2[i] = value;
+ }
+
+ return genericDotFloat(vec1, vec2);
+}
+
+// Generic function for testing integer dot products
+__generic<let N : int>
+int testIntDot(int value)
+{
+ vector<int, N> vec1;
+ vector<int, N> vec2;
+
+ // Initialize all components to the same value
+ for (int i = 0; i < N; i++)
+ {
+ vec1[i] = value;
+ vec2[i] = value;
+ }
+
+ return genericDotInt(vec1, vec2);
+}
+
+[numthreads(1, 1, 1)]
+void computeMain()
+{
+ // Test with N=1 (single element vectors) - this is the main test case
+ float floatResult1 = testFloatDot<1>(3.0); // 3.0 * 3.0 = 9.0
+ int intResult1 = testIntDot<1>(3); // 3 * 3 = 9
+
+ // Test with N=2 to ensure generic function works for other sizes
+ float floatResult2 = testFloatDot<2>(1.0); // (1.0*1.0 + 1.0*1.0) = 2.0
+
+ // Sum all results: 9 + 9 + 2 = 20
+ int result = int(floatResult1) + intResult1 + int(floatResult2);
+
+ outputBuffer[0] = result;
+}
diff --git a/tests/compute/dot1.slang b/tests/compute/dot1.slang
new file mode 100644
index 000000000..d6022318d
--- /dev/null
+++ b/tests/compute/dot1.slang
@@ -0,0 +1,32 @@
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-dx12 -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-mtl -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda -output-using-type
+//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-wgsl -output-using-type
+
+// Test for dot product with 1-element vectors (float and int)
+
+// CHECK: 8
+
+//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(1, 1, 1)]
+void computeMain()
+{
+ // Float dot product with 1-element vectors
+ vector<float, 1> floatVec1 = vector<float, 1>(2.0);
+ vector<float, 1> floatVec2 = vector<float, 1>(2.0);
+ float floatDot = dot(floatVec1, floatVec2); // 2.0 * 2.0 = 4.0
+
+ // Int dot product with 1-element vectors
+ vector<int, 1> intVec1 = vector<int, 1>(2);
+ vector<int, 1> intVec2 = vector<int, 1>(2);
+ int intDot = dot(intVec1, intVec2); // 2 * 2 = 4
+
+ // Add them together and convert to int
+ int result = int(floatDot) + intDot; // 4 + 4 = 8
+
+ outputBuffer[0] = result;
+}