using System; using System.Runtime.InteropServices; using System.Runtime.Versioning; using ComputeSharp; using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; [assembly: SupportedOSPlatform("windows6.1")] /// /// Native exports for the library. /// public static unsafe class Library { /// /// Registers the effect into a target ID2D1Factory1 instance. /// /// /// /// The HRESULT for the operation. [UnmanagedCallersOnly(EntryPoint = nameof(RegisterHelloWorldEffect))] public static int RegisterHelloWorldEffect(void* d2D1Factory1, Guid* effectId) { try { D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1, out *effectId); return 0; } catch (Exception e) { return Marshal.GetHRForException(e); } } /// /// Sets the constant buffer for the input ID2D1Effect (assumed to be ). /// /// /// /// The horizontal dispatch size for the current output. /// The vertical dispatch size for the current output. /// [UnmanagedCallersOnly(EntryPoint = nameof(SetHelloWorldEffectConstantBuffer))] public static int SetHelloWorldEffectConstantBuffer(void* d2D1Effect, float time, int dispatchWidth, int dispatchHeight) { try { D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect, new HelloWorld(time, new int2(dispatchWidth, dispatchHeight))); return 0; } catch (Exception e) { return Marshal.GetHRForException(e); } } } [D2DEffectDisplayName(nameof(HelloWorld))] [D2DEffectDescription("A hello world effect that displays a color gradient.")] [D2DEffectCategory("Render")] [D2DEffectAuthor("ComputeSharp.D2D1")] [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] internal readonly partial struct HelloWorld : ID2D1PixelShader { /// /// The current time since the start of the application. /// private readonly float time; /// /// The dispatch size for the current output. /// private readonly int2 dispatchSize; /// public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; float2 uv = xy / (float2)this.dispatchSize; float3 color = 0.5f + (0.5f * Hlsl.Cos(this.time + new float3(uv, uv.X) + new float3(0, 2, 4))); return new(color, 1f); } }