yum-mirror/slang

Making it easier to work with shaders

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

bprbAdd Matrix overload for exp10() core lib function (#5620)4f6b928f5

master
3.9 KiB153 linesraw
1// TODO(JS):
2// It doesn't look like fxc, dxc, vk support double versions of many of the intrinsics, so they are disabled here.
3// Arguably we should implement simple intrinsics if missing in the core module.
4// More complicated functions (like say sin) can also be written, if not available on a target, but requires significant
5// care.
6
7// TODO(JS):
8// NOTE! war-double-host-callable category is a work around for issues with x86 & host-callable
9
10//TEST(compute, war-double-host-callable):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type
11//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -output-using-type
12//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 -output-using-type
13//DISABLE_TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -output-using-type -render-feature double
14//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -output-using-type
15
16//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0], stride=4):out,name outputBuffer
17RWStructuredBuffer<double> outputBuffer;
18
19typedef double Float;
20
21typedef matrix<Float, 2, 2> FloatMatrix;
22typedef matrix<int64_t, 2, 2> IntMatrix;
23typedef matrix<uint64_t, 2, 2> UIntMatrix;
24typedef vector<Float, 2> FloatVector;
25
26Float calcTotal(FloatVector v)
27{
28    return v.x + v.y;
29}
30
31Float calcTotal(FloatMatrix v) 
32{ 
33    return calcTotal(v[0]) + calcTotal(v[1]);
34}
35
36FloatMatrix makeFloatMatrix(Float f)
37{
38    return FloatMatrix(f);
39}
40
41IntMatrix makeIntMatrix(int v)
42{
43    IntMatrix m = { { v, v }, { v, v } };
44    return m;
45}
46
47void test1(inout FloatMatrix ft, inout FloatMatrix f, int idx)
48{
49    unmodified(f);
50
51    // fmod
52    ft += FloatMatrix(IntMatrix(((f % makeFloatMatrix(0.11f)) * makeFloatMatrix(100)) + makeFloatMatrix(0.5)));
53
54    ft += sin(f);
55
56    // Lets try some matrix/matrix
57    ft = f * ft;
58
59    // Lets try some vector matrix
60
61    {
62        FloatMatrix r = { mul(f[0], ft), mul(ft, f[1]) };
63        ft += r;
64    }
65
66    // Back to the transcendentals
67
68    ft += cos(f);
69    ft += tan(f);
70
71    ft += asin(f);
72    ft += acos(f);
73    ft += atan(f);
74
75    ft += atan2(f, makeFloatMatrix(2));
76    {
77        FloatMatrix sf, cf;
78        sincos(f, sf, cf);
79
80        ft += sf;
81        ft += cf;
82    }
83
84    ft += rcp(makeFloatMatrix(1.0) + f);
85    ft += FloatMatrix(sign(f - makeFloatMatrix(0.5)));
86
87    ft += saturate(f * makeFloatMatrix(4) - makeFloatMatrix(2.0));
88
89    ft += sqrt(f);
90    ft += rsqrt(makeFloatMatrix(1.0f) + f);
91
92    ft += exp2(f);
93    ft += exp(f);
94    ft += exp10(f);
95
96    ft += frac(f * makeFloatMatrix(3));
97    ft += ceil(f * makeFloatMatrix(5) - makeFloatMatrix(3));
98
99    ft += floor(f * makeFloatMatrix(10) - makeFloatMatrix(7));
100    ft += trunc(f * makeFloatMatrix(7));
101
102    ft += log(f + makeFloatMatrix(10.0));
103    ft += log2(f * makeFloatMatrix(3) + makeFloatMatrix(2));
104
105    {
106        float scalarVs[] = { 1, 10, 100, 1000 };
107        ft += FloatMatrix(IntMatrix(log10(makeFloatMatrix(Float(scalarVs[idx]))) + makeFloatMatrix(0.5f)));
108    }
109
110    ft += abs(f * makeFloatMatrix(4) - makeFloatMatrix(2.0f));
111
112    ft += min(makeFloatMatrix(0.5), f);
113    ft += max(f, makeFloatMatrix(0.75));
114}
115
116void test2(inout FloatMatrix ft, inout FloatMatrix f)
117{
118    unmodified(f);
119
120    ft += pow(makeFloatMatrix(0.5), f);
121
122    ft += smoothstep(makeFloatMatrix(0.2), makeFloatMatrix(0.7), f);
123    ft += lerp(makeFloatMatrix(-100), makeFloatMatrix(100), f);
124
125    ft += clamp(f, makeFloatMatrix(0.1), makeFloatMatrix(0.3));
126
127    ft += step(f, makeFloatMatrix(0.5));
128
129#if 0
130    IntMatrix vi = asint(makeFloatMatrix(idx)); 
131    ft += asfloat(vi);
132    
133    UIntMatrix vu = asuint(f);
134    ft += asfloat(vu);
135#endif
136}
137
138[numthreads(4, 1, 1)]
139void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
140{
141    int idx = int(dispatchThreadID.x);
142
143    Float scalarF = idx * (1.0f / (4.0f));
144
145    FloatMatrix ft = {}; 
146
147    FloatMatrix f = { { scalarF + 0.01, scalarF + 0.02}, { scalarF + 0.011, scalarF + 0.022}};
148
149    test1(ft, f, idx);
150    test2(ft, f);
151    
152    outputBuffer[idx] = calcTotal(ft);
153}