From b4023f715885ada9a2777ea3b0d6d9739860b39b Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Sat, 11 Oct 2025 01:42:43 +0900 Subject: implement dot products for 1 vectors (#8599) Closes https://github.com/shader-slang/slang/issues/8378 --- tests/compute/dot1-generic.slang | 77 ++++++++++++++++++++++++++++++++++++++++ tests/compute/dot1.slang | 32 +++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tests/compute/dot1-generic.slang create mode 100644 tests/compute/dot1.slang (limited to 'tests') 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 outputBuffer; + +// Generic function that computes dot product for N-sized float vectors +__generic +float genericDotFloat(vector a, vector b) +{ + return dot(a, b); +} + +// Generic function that computes dot product for N-sized int vectors +__generic +int genericDotInt(vector a, vector b) +{ + return dot(a, b); +} + +// Generic function for testing with different N values +__generic +float testFloatDot(float value) +{ + vector vec1; + vector 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 +int testIntDot(int value) +{ + vector vec1; + vector 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 outputBuffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // Float dot product with 1-element vectors + vector floatVec1 = vector(2.0); + vector floatVec2 = vector(2.0); + float floatDot = dot(floatVec1, floatVec2); // 2.0 * 2.0 = 4.0 + + // Int dot product with 1-element vectors + vector intVec1 = vector(2); + vector intVec2 = vector(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; +} -- cgit v1.2.3