using System; using System.Drawing; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using ComputeSharp.Win32; #pragma warning disable CA1033 namespace ComputeSharp.D2D1.Interop; /// /// Provides an ID2D1DrawTransformMapper implementation, which can be used to customize the draw transform logic in /// an effect created with and also shared across multiple instances of a given effect. /// /// The type of shader the transform will interact with. /// /// /// The built-in ID2D1EffectImpl /// implementation provided by (which makes it possible to register and create /// ID2D1Effect /// instances that can be used to run D2D1 pixel shaders) uses draw transform objects to allow customizing the transform logic externally. /// /// /// These managers are COM objects implementing the following interface: /// /// [uuid(02E6D48D-B892-4FBC-AA54-119203BAB802)] /// interface ID2D1DrawTransformMapper : IUnknown /// { /// HRESULT MapInputRectsToOutputRect( /// [in] const ID2D1DrawInfoUpdateContext* updateContext, /// [in] const RECT* inputRects, /// [in] const RECT* inputOpaqueSubRects, /// UINT32 inputRectCount, /// [out] RECT* outputRect, /// [out] RECT* outputOpaqueSubRect); /// /// HRESULT MapOutputRectToInputRects( /// [in] const RECT* outputRect, /// [out] RECT* inputRects, /// UINT32 inputRectsCount); /// /// HRESULT MapInvalidRect( /// UINT32 inputIndex, /// RECT invalidInputRect, /// [out] RECT* invalidOutputRect); /// }; /// /// /// /// These can be used to implement custom draw transform logic, assign it to an effect and also share these transforms over multiple effects. /// /// /// For details of how these work, MapInputRectsToOutputRect maps to /// ID2D1Transform::MapInputRectsToOutputRect, /// MapOutputRectToInputRects maps to ID2D1Transform::MapOutputRectToInputRects. /// and MapInvalidRect maps to ID2D1Transform::MapInvalidRect. /// /// /// The main difference between these APIs and the ones in ID2D1Transform is the fact that this interface is standalone (ie. it doesn't inherit from ID2D1TransformNode), /// and that it allows transform mappers to also read and update additional data tied to an effect instance (such as the shader constant buffer). This is done through the /// ID2D1DrawInfoUpdateContext interface, that is passed to MapInputRectsToOutputRect. This interface is defined as follows: /// /// [uuid(430C5B40-AE16-485F-90E6-4FA4915144B6)] /// interface ID2D1DrawInfoUpdateContext : IUnknown /// { /// HRESULT GetConstantBufferSize([out] UINT32 *size); /// /// HRESULT GetConstantBuffer( /// [out] BYTE *buffer, /// UINT32 bufferCount); /// /// HRESULT SetConstantBuffer( /// [in] const BYTE *buffer, /// UINT32 bufferCount); /// } /// /// /// /// That is, ID2D1DrawInfoUpdateContext allows a custom transform (an ID2D1DrawTransformMapper instance) to interact with the underlying ID2D1DrawInfo object /// that is owned by the effect being used, in a safe way. For instance, it allows a transform to read and update the constant buffer, which can be used to allow a transform to /// pass the exact dispatch area size to an input shader, without the consumer having to manually query that information beforehand (which might not be available either). /// /// /// This interface is implemented by ComputeSharp.D2D1, and it can be used through the APIs in , in several ways. /// That is, consumers can either implement a type inheriting from to implement their own fully customized transform /// mapping logic, or they can use the helper methods exposed by to easily retrieve ready to use transforms. /// /// /// A CCW (COM callable wrapper, see ) is also available for all /// of these APIs, implemented via the same type. That is, a given instance can expose its underlying CCW through the /// interface, and this can then be passed to an existing D2D1 effect instance. /// /// public abstract unsafe partial class D2D1DrawTransformMapper : ICustomQueryInterface, ID2D1DrawTransformMapperInterop where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { /// /// The object wrapped by the current instance. /// private ComPtr d2D1TransformMapperImpl; /// /// Creates a new instance. /// public D2D1DrawTransformMapper() { fixed (D2D1DrawTransformMapperImpl** d2D1TransformMapperImpl = this.d2D1TransformMapperImpl) { D2D1DrawTransformMapperImpl.Factory(this, d2D1TransformMapperImpl); } } /// /// Releases the underlying ID2D1DrawTransformMapper object. /// ~D2D1DrawTransformMapper() { this.d2D1TransformMapperImpl.Dispose(); } /// /// /// Allows a transform to state how it would map a a set of sample rectangles on its input to an output rectangle. /// /// /// This is effectively the inverse mapping of . /// /// /// The input value, which can be used to access and modify shader data. /// The input rectangles to be mapped to the output rectangle. This parameter is always equal to the input bounds. /// The input rectangles to be mapped to the opaque output rectangle. /// The output rectangle that maps to the corresponding input rectangle. /// The output rectangle that maps to the corresponding opaque input rectangle. /// /// /// Unlike and , this method is explicitly called by the renderer at /// a determined place in its rendering algorithm. The transform implementation may change its state based on the input rectangles /// and use this information to control its rendering information. This method is always called before the /// and methods. /// /// /// For more info, see . /// /// public abstract void MapInputsToOutput( D2D1DrawInfoUpdateContext drawInfoUpdateContext, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput); /// /// Allows a transform to state how it would map a rectangle requested on its output to a set of sample rectangles on its input. /// /// The output rectangle from which the inputs must be mapped. /// The corresponding set of inputs. The inputs will directly correspond to the transform inputs. /// /// /// The transform implementation must regard this method as purely functional. It can base the mapped input and output rectangles on its /// current state as specified by the encapsulating effect properties. However, it must not change its own state in response to this method /// being invoked. The Direct2D renderer implementation reserves the right to call this method at any time and in any sequence. /// /// /// For more info, see . /// /// public abstract void MapOutputToInputs(in Rectangle output, Span inputs); /// /// Sets the input rectangles for this rendering pass into the transform. /// /// The index of the input rectangle. /// The invalid input rectangle. /// The output rectangle to which the input rectangle must be mapped. /// /// /// The transform implementation must regard this method as purely functional. The transform implementation can base the mapped input rectangle on /// the transform implementation's current state as specified by the encapsulating effect properties. But the transform implementation can't change /// its own state in response to a call to . Direct2D can call this method at any time /// and in any sequence following a call to . /// /// /// For more info, see . /// /// public abstract void MapInvalidOutput(int inputIndex, in Rectangle invalidInput, out Rectangle invalidOutput); /// /// Gets the underlying object. /// /// The underlying object. [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void GetD2D1TransformMapper(ID2D1DrawTransformMapper** transformMapper) { bool lockTaken = false; this.d2D1TransformMapperImpl.Get()->SpinLock.Enter(ref lockTaken); // Whenever the CCW is requested from this object, we also make sure that the current instance is tracked // in the GC handle stored in the CCW. This could've been reset in case there were no other references to // the CCW (see comments in D2D1TransformMapperImpl about this), so here we're assigning a reference to the // current object again to ensure the returned CCW object will keep the instance alive if needed. try { this.d2D1TransformMapperImpl.Get()->EnsureTargetIsTracked(this); this.d2D1TransformMapperImpl.Get()->CopyToWithNoLock(transformMapper); } finally { this.d2D1TransformMapperImpl.Get()->SpinLock.Exit(); GC.KeepAlive(this); } } /// CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv) { fixed (Guid* pIid = &iid) fixed (IntPtr* pPpv = &ppv) { int hresult; bool lockTaken = false; this.d2D1TransformMapperImpl.Get()->SpinLock.Enter(ref lockTaken); try { this.d2D1TransformMapperImpl.Get()->EnsureTargetIsTracked(this); hresult = this.d2D1TransformMapperImpl.Get()->QueryInterfaceWithNoLock(pIid, (void**)pPpv); } finally { this.d2D1TransformMapperImpl.Get()->SpinLock.Exit(); GC.KeepAlive(this); } return hresult switch { S.S_OK => CustomQueryInterfaceResult.Handled, _ => CustomQueryInterfaceResult.Failed }; } } /// void ID2D1DrawTransformMapperInterop.MapInputsToOutput( ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { MapInputsToOutput( new D2D1DrawInfoUpdateContext(d2D1DrawInfoUpdateContext), inputs, opaqueInputs, out output, out opaqueOutput); } /// void ID2D1DrawTransformMapperInterop.MapOutputToInputs(in Rectangle output, Span inputs) { MapOutputToInputs(in output, inputs); } /// void ID2D1DrawTransformMapperInterop.MapInvalidOutput(int inputIndex, in Rectangle invalidInput, out Rectangle invalidOutput) { MapInvalidOutput(inputIndex, in invalidInput, out invalidOutput); } }