using System; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Interop.Effects; /// partial struct PixelShaderEffect { /// /// The implementation for . /// private static unsafe class ID2D1DrawTransformMethods { #if !NET6_0_OR_GREATER /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate uint GetInputCountDelegate(PixelShaderEffect* @this); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int MapOutputRectToInputRectsDelegate(PixelShaderEffect* @this, RECT* outputRect, RECT* inputRects, uint inputRectsCount); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int MapInputRectsToOutputRectDelegate(PixelShaderEffect* @this, RECT* inputRects, RECT* inputOpaqueSubRects, uint inputRectCount, RECT* outputRect, RECT* outputOpaqueSubRect); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int MapInvalidRectDelegate(PixelShaderEffect* @this, uint inputIndex, RECT invalidInputRect, RECT* invalidOutputRect); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int SetDrawInfoDelegate(PixelShaderEffect* @this, ID2D1DrawInfo* drawInfo); /// /// A cached instance wrapping . /// public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface; /// /// A cached instance wrapping . /// public static readonly AddRefDelegate AddRefWrapper = AddRef; /// /// A cached instance wrapping . /// public static readonly ReleaseDelegate ReleaseWrapper = Release; /// /// A cached instance wrapping . /// public static readonly GetInputCountDelegate GetInputCountWrapper = GetInputCount; /// /// A cached instance wrapping . /// public static readonly MapOutputRectToInputRectsDelegate MapOutputRectToInputRectsWrapper = MapOutputRectToInputRects; /// /// A cached instance wrapping . /// public static readonly MapInputRectsToOutputRectDelegate MapInputRectsToOutputRectWrapper = MapInputRectsToOutputRect; /// /// A cached instance wrapping . /// public static readonly MapInvalidRectDelegate MapInvalidRectWrapper = MapInvalidRect; /// /// A cached instance wrapping . /// public static readonly SetDrawInfoDelegate SetDrawInfoWrapper = SetDrawInfo; #endif /// [UnmanagedCallersOnly] public static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; return @this->QueryInterface(riid, ppvObject); } /// [UnmanagedCallersOnly] public static uint AddRef(PixelShaderEffect* @this) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; return @this->AddRef(); } /// [UnmanagedCallersOnly] public static uint Release(PixelShaderEffect* @this) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; return @this->Release(); } /// [UnmanagedCallersOnly] public static uint GetInputCount(PixelShaderEffect* @this) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; try { return (uint)@this->GetGlobals().InputCount; } catch { // GetInputCount doesn't respect COM, so it does not return an HRESULT. // The only thing we can do in this case is just to return 0 instead. return 0; } } /// [UnmanagedCallersOnly] public static int MapOutputRectToInputRects(PixelShaderEffect* @this, RECT* outputRect, RECT* inputRects, uint inputRectsCount) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; try { default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectsCount, @this->GetGlobals().InputCount, nameof(inputRectsCount)); if (@this->d2D1TransformMapper is not null) { // Forward to the current ID2D1TransformMapper instance @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert(); } else { ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span; // If no custom transform is used, apply the default mapping. In this case, the loop will // automatically handle cases where no inputs are defined. If inputs are present instead, // the default mapping will set the input rect for a simple input to be the same as the // output rect, and the input rect for a complex rect to be infinite. In both cases, D2D // will handle clipping the inputs to the actual output rect area, if needed. for (uint i = 0; i < inputRectsCount; i++) { switch (inputTypes[(int)i]) { case D2D1PixelShaderInputType.Simple: inputRects[i] = *outputRect; break; case D2D1PixelShaderInputType.Complex: inputRects[i].MakeD2D1Infinite(); break; default: return E.E_FAIL; } } } return S.S_OK; } catch (Exception e) { return Marshal.GetHRForException(e); } } /// [UnmanagedCallersOnly] public static int MapInputRectsToOutputRect(PixelShaderEffect* @this, RECT* inputRects, RECT* inputOpaqueSubRects, uint inputRectCount, RECT* outputRect, RECT* outputOpaqueSubRect) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; try { default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectCount, @this->GetGlobals().InputCount, nameof(inputRectCount)); if (@this->d2D1TransformMapper is not null) { using ComPtr d2D1DrawInfoUpdateContext = default; // Create an ID2D1DrawInfoUpdateContext instance D2D1DrawInfoUpdateContextImpl.Factory( drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(), constantBuffer: @this->constantBuffer, constantBufferSize: @this->GetGlobals().ConstantBufferSize, d2D1DrawInfo: @this->d2D1DrawInfo).Assert(); // Forward the call to the input ID2D1TransformMapper instance HRESULT hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect( updateContext: (ID2D1DrawInfoUpdateContext*)d2D1DrawInfoUpdateContext.Get(), inputRects: inputRects, inputOpaqueSubRects: inputOpaqueSubRects, inputRectCount: inputRectCount, outputRect: outputRect, outputOpaqueSubRect: outputOpaqueSubRect); // Regardless of the operation result, always invalidate the context d2D1DrawInfoUpdateContext.Get()->Close().Assert(); // Now we can validate that the call was in fact successful hresult.Assert(); } else if (inputRectCount == 0) { // If there are no inputs, make the output rectangle infinite. This is useful // to make output-only effects work fine by default, without needing a custom // transform. In this case, the target area will be the output node anyway. outputRect->MakeD2D1Infinite(); *outputOpaqueSubRect = default; } else { ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span; // If at least one input is present and no custom mapper is available, apply the default // mapping. In this case, the output rect should be the union of the input rects for all // the simple shader inputs, or infinite if there are no simple inputs. The input rects // for complex inputs, if present, are not needed in this scenario, so they're ignored. RECT unionOfSimpleRects = default; bool isUnionOfSimpleRectsEmpty = true; for (uint i = 0; i < inputRectCount; i++) { if (inputTypes[(int)i] == D2D1PixelShaderInputType.Simple) { if (isUnionOfSimpleRectsEmpty) { unionOfSimpleRects = inputRects[i]; isUnionOfSimpleRectsEmpty = false; } else { unionOfSimpleRects = unionOfSimpleRects.Union(inputRects[i]); } } } if (isUnionOfSimpleRectsEmpty) { outputRect->MakeD2D1Infinite(); } else { *outputRect = unionOfSimpleRects; } *outputOpaqueSubRect = default; } return S.S_OK; } catch (Exception e) { return Marshal.GetHRForException(e); } } /// [UnmanagedCallersOnly] public static int MapInvalidRect(PixelShaderEffect* @this, uint inputIndex, RECT invalidInputRect, RECT* invalidOutputRect) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; try { default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual((int)inputIndex, @this->GetGlobals().InputCount, nameof(inputIndex)); if (@this->d2D1TransformMapper is not null) { // Forward to the current ID2D1TransformMapper instance @this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert(); } else { // The default mapping in this scenario just needs to set the invalid output rect for simple inputs to // be the invalid input rect, and to set the invalid output rect for complex inputs to an infinite rect. switch (@this->GetGlobals().InputTypes.Span[(int)inputIndex]) { case D2D1PixelShaderInputType.Simple: *invalidOutputRect = invalidInputRect; break; case D2D1PixelShaderInputType.Complex: invalidOutputRect->MakeD2D1Infinite(); break; default: return E.E_FAIL; } } return S.S_OK; } catch (Exception e) { return Marshal.GetHRForException(e); } } /// [UnmanagedCallersOnly] public static int SetDrawInfo(PixelShaderEffect* @this, ID2D1DrawInfo* drawInfo) { @this = (PixelShaderEffect*)&((void**)@this)[-1]; try { // Store the new ID2D1DrawInfo object ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo); Guid shaderId = @this->GetGlobals().EffectId; D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions; // Set the pixel shader for the effect drawInfo->SetPixelShader(&shaderId, (D2D1_PIXEL_OPTIONS)pixelOptions).Assert(); // If any input descriptions are present, set them foreach (ref readonly D2D1InputDescription inputDescription in @this->GetGlobals().InputDescriptions.Span) { D2D1_INPUT_DESCRIPTION d2D1InputDescription; d2D1InputDescription.filter = (D2D1_FILTER)inputDescription.Filter; d2D1InputDescription.levelOfDetailCount = (uint)inputDescription.LevelOfDetailCount; drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription).Assert(); } D2D1BufferPrecision bufferPrecision = @this->GetGlobals().BufferPrecision; D2D1ChannelDepth channelDepth = @this->GetGlobals().ChannelDepth; // If a custom buffer precision or channel depth is requested, set it if (bufferPrecision != D2D1BufferPrecision.Unknown || channelDepth != D2D1ChannelDepth.Default) { drawInfo->SetOutputBuffer( (D2D1_BUFFER_PRECISION)bufferPrecision, (D2D1_CHANNEL_DEPTH)channelDepth).Assert(); } return S.S_OK; } catch (Exception e) { return Marshal.GetHRForException(e); } } } }