From eb462c360fc170d80bc22babc9aa51f2bab948a2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 17 Nov 2022 21:27:56 +0100 Subject: [PATCH] Add base D2D1TransformMapper type --- .../Shaders/Interop/D2D1TransformMapper{T}.cs | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs new file mode 100644 index 00000000..75e47988 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs @@ -0,0 +1,159 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; + +namespace ComputeSharp.D2D1.Interop; + +/// +/// Provides an ID2D1TransformMapper 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 ID2D1TransformMapper : IUnknown +/// { +/// HRESULT MapInputRectsToOutputRect( +/// [in] const ID2D1DrawInfoUpdateContex* 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 +/// ID2D1DrawInfoUpdateContex interface, that is passed to MapInputRectsToOutputRect. This interface is defined as follows: +/// +/// [uuid(430C5B40-AE16-485F-90E6-4FA4915144B6)] +/// interface ID2D1DrawInfoUpdateContex : IUnknown +/// { +/// HRESULT GetConstantBufferSize([out] UINT32 *size); +/// +/// HRESULT GetConstantBuffer( +/// [out] BYTE *buffer, +/// UINT32 bufferCount); +/// +/// HRESULT SetConstantBuffer( +/// [in] const BYTE *buffer, +/// UINT32 bufferCount); +/// } +/// +/// +/// +/// That is, ID2D1DrawInfoUpdateContex allows a custom transform (an ID2D1TransformMapper 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 class D2D1TransformMapper + where T : unmanaged, ID2D1PixelShader +{ + /// + /// + /// 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, Rectangle invalidInput, out Rectangle invalidOutput); +} \ No newline at end of file