yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
d10732742
master
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type 2//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 4 5//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer 6RWStructuredBuffer<float> outputBuffer; 7 8typedef DifferentialPair<float> dpfloat; 9 10interface IFoo 11{ 12 // Since IFoo is not inheriting from IDifferentiable, 13 // The `this` parameter should be considered as `no_diff` when `getVal` 14 // is called through this interface. 15 [ForwardDifferentiable] 16 float getVal(float y); 17} 18 19struct A : IDifferentiable, IFoo 20{ 21 float x; 22 23 // This `getVal` implementation will have `this` parameter treated as 24 // differentiable. In order for this method to satisfy the `IFoo.getVal` 25 // requirement, we need to synthesize a method with `[NoDiffThis]` attribute 26 // that calls this. 27 [ForwardDifferentiable] 28 float getVal(float y){ return x * x + y * y; } 29} 30 31[ForwardDifferentiable] 32static float f<T:IFoo>(T obj, float y) 33{ 34 return obj.getVal(y); 35} 36 37[ForwardDifferentiable] 38static float f2(IFoo obj, float y) 39{ 40 return obj.getVal(y); 41} 42 43[ForwardDifferentiable] 44float f3(A obj, float y) 45{ 46 return obj.getVal(y); 47} 48 49[numthreads(1, 1, 1)] 50void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 51{ 52 A a; 53 a.x = 2.0; 54 A.Differential ad; 55 ad.x = 1.0; 56 57 let rs = __fwd_diff(f)(a, dpfloat(3.0, 1.0)); 58 outputBuffer[0] = rs.p; // Expect: 13.0 59 outputBuffer[1] = rs.d; // Expect: 6.0 60 61 let rs2 = __fwd_diff(f2)(a, dpfloat(3.0, 1.0)); 62 outputBuffer[2] = rs2.p; // Expect: 13.0 63 outputBuffer[3] = rs2.d; // Expect: 6.0 64 65 // By calling A.getVal directly, we will invoke the implementation 66 // that differentiates the `this` argument. 67 let rs3 = __fwd_diff(f3)(DifferentialPair<A>(a, ad), dpfloat(3.0, 1.0)); 68 outputBuffer[4] = rs3.p; // Expect: 13.0 69 outputBuffer[5] = rs3.d; // Expect: 10.0 70}