summaryrefslogtreecommitdiff
path: root/tests/autodiff-dstdlib/dstdlib-max.slang
diff options
context:
space:
mode:
authorwinmad <winmad.wlf@gmail.com>2022-11-14 16:43:55 -0800
committerGitHub <noreply@github.com>2022-11-14 16:43:55 -0800
commit25affe8e724fe4ee60a3b8ec2c494926930ba59f (patch)
tree39d2d3d209a99152e80bf40c395002697d2c3338 /tests/autodiff-dstdlib/dstdlib-max.slang
parent368ec3116ea0f10f44acbf76b5dc9e34d6ff3d32 (diff)
Adding some math functions and their derivatives (#2497)
Diffstat (limited to 'tests/autodiff-dstdlib/dstdlib-max.slang')
-rw-r--r--tests/autodiff-dstdlib/dstdlib-max.slang42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/autodiff-dstdlib/dstdlib-max.slang b/tests/autodiff-dstdlib/dstdlib-max.slang
new file mode 100644
index 000000000..d28b17e06
--- /dev/null
+++ b/tests/autodiff-dstdlib/dstdlib-max.slang
@@ -0,0 +1,42 @@
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+typedef DifferentialPair<float> dpfloat;
+typedef DifferentialPair<float2> dpfloat2;
+
+[ForwardDifferentiable]
+float diffMax(float x, float y)
+{
+ return max(x, y);
+}
+
+[ForwardDifferentiable]
+float2 diffMax2(float2 x, float2 y)
+{
+ return max(x, y);
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ {
+ dpfloat dpx = dpfloat(2.0, 1.0);
+ dpfloat dpy = dpfloat(5.0, -2.0);
+ dpfloat res = __fwd_diff(diffMax)(dpx, dpy);
+ outputBuffer[0] = res.p; // Expect: 5.000000
+ outputBuffer[1] = res.d; // Expect: -2.000000
+ }
+
+ {
+ dpfloat2 dpx = dpfloat2(float2(-3.0, 4.0), float2(-1.0, -1.0));
+ dpfloat2 dpy = dpfloat2(float2(1.0, 2.0), float2(2.0, 2.0));
+ dpfloat2 res = __fwd_diff(diffMax2)(dpx, dpy);
+ outputBuffer[2] = res.p[0]; // Expect: 1.000000
+ outputBuffer[3] = res.d[0]; // Expect: 2.000000
+ outputBuffer[4] = res.p[1]; // Expect: 4.000000
+ outputBuffer[5] = res.d[1]; // Expect: -1.000000
+ }
+}