yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaimplement dot products for 1 vectors (#8599)b4023f715

master
2.2 KiB77 linesraw
1//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -output-using-type
2//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-dx12 -output-using-type
3//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -output-using-type
4//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-mtl -output-using-type
5//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda -output-using-type
6//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-wgsl -output-using-type
7
8// Test for dot product with 1-element vectors called from a generic function
9
10// CHECK: 20
11
12//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
13RWStructuredBuffer<int> outputBuffer;
14
15// Generic function that computes dot product for N-sized float vectors
16__generic<let N : int>
17float genericDotFloat(vector<float, N> a, vector<float, N> b)
18{
19    return dot(a, b);
20}
21
22// Generic function that computes dot product for N-sized int vectors
23__generic<let N : int>
24int genericDotInt(vector<int, N> a, vector<int, N> b)
25{
26    return dot(a, b);
27}
28
29// Generic function for testing with different N values
30__generic<let N : int>
31float testFloatDot(float value)
32{
33    vector<float, N> vec1;
34    vector<float, N> vec2;
35    
36    // Initialize all components to the same value
37    for (int i = 0; i < N; i++)
38    {
39        vec1[i] = value;
40        vec2[i] = value;
41    }
42    
43    return genericDotFloat(vec1, vec2);
44}
45
46// Generic function for testing integer dot products
47__generic<let N : int>
48int testIntDot(int value)
49{
50    vector<int, N> vec1;
51    vector<int, N> vec2;
52    
53    // Initialize all components to the same value
54    for (int i = 0; i < N; i++)
55    {
56        vec1[i] = value;
57        vec2[i] = value;
58    }
59    
60    return genericDotInt(vec1, vec2);
61}
62
63[numthreads(1, 1, 1)]
64void computeMain()
65{
66    // Test with N=1 (single element vectors) - this is the main test case
67    float floatResult1 = testFloatDot<1>(3.0);  // 3.0 * 3.0 = 9.0
68    int intResult1 = testIntDot<1>(3);          // 3 * 3 = 9
69    
70    // Test with N=2 to ensure generic function works for other sizes
71    float floatResult2 = testFloatDot<2>(1.0);  // (1.0*1.0 + 1.0*1.0) = 2.0
72    
73    // Sum all results: 9 + 9 + 2 = 20
74    int result = int(floatResult1) + intResult1 + int(floatResult2);
75    
76    outputBuffer[0] = result;
77}