From 0ad252470e9514c13d82af6fd84eaff95eda409a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 7 May 2022 16:14:14 +0200 Subject: [PATCH] Update default draw transform mapper logic --- .../Extensions/RECTExtensions.cs | 23 ++++++ .../Shaders/Interop/D2D1PixelShaderEffect.cs | 6 +- .../Effects/PixelShaderEffect.For{T}.cs | 46 ++++++++---- ...lShaderEffect.ID2D1DrawTransformMethods.cs | 72 +++++++++++++++---- .../Interop/Effects/PixelShaderEffect.cs | 25 ++++--- .../Windows/shared/winerror/E.cs | 3 + 6 files changed, 135 insertions(+), 40 deletions(-) diff --git a/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs b/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs index 792b7374..b2118fc9 100644 --- a/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs @@ -75,4 +75,27 @@ internal static class RECTExtensions bottom = rectangle.Bottom }; } + + /// + /// Creates a with the area being the union of two other values. + /// + /// The first input instance to read. + /// The second input instance to read. + /// A with the area being the union of that of and . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RECT Union(this in RECT rect, in RECT other) + { + int left = Math.Min(rect.left, other.left); + int top = Math.Min(rect.top, other.top); + int right = Math.Max(rect.right, other.right); + int bottom = Math.Max(rect.bottom, other.bottom); + + return new() + { + left = left, + top = top, + right = right, + bottom = bottom + }; + } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 2f207577..3600b928 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -71,7 +71,7 @@ public static unsafe class D2D1PixelShaderEffect // Setup the input string StringBuilder effectInputsBuilder = new(); - for (int i = 0; i < PixelShaderEffect.For.NumberOfInputs; i++) + for (int i = 0; i < PixelShaderEffect.For.InputCount; i++) { effectInputsBuilder.Append(" @@ -230,7 +230,7 @@ public static unsafe class D2D1PixelShaderEffect "); // Add the input nodes - for (int i = 0; i < PixelShaderEffect.For.NumberOfInputs; i++) + for (int i = 0; i < PixelShaderEffect.For.InputCount; i++) { writer.WriteAsUtf8("inputRect = inputRects[0]; + for (uint i = 0; i < inputRectCount; i++) + { + if (@this->inputTypes[i] == D2D1PixelShaderInputType.Simple) + { + unionOfSimpleRects = (unionOfSimpleRects ?? default).Union(inputRects[i]); + } + } + + if (unionOfSimpleRects.HasValue) + { + *outputRect = unionOfSimpleRects.Value; + } + else + { + outputRect->MakeD2D1Infinite(); + } *outputOpaqueSubRect = default; } @@ -234,6 +264,11 @@ partial struct PixelShaderEffect { @this = (PixelShaderEffect*)&((void**)@this)[-1]; + if (inputIndex >= (uint)@this->inputCount) + { + return E.E_INVALIDARG; + } + if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper) { Rectangle invalidInput = invalidInputRect.ToRectangle(); @@ -253,8 +288,19 @@ partial struct PixelShaderEffect } else { - // Default mapping - *invalidOutputRect = @this->inputRect; + // 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->inputTypes[inputIndex]) + { + case D2D1PixelShaderInputType.Simple: + *invalidOutputRect = invalidInputRect; + break; + case D2D1PixelShaderInputType.Complex: + invalidOutputRect->MakeD2D1Infinite(); + break; + default: + return E.E_FAIL; + } } return S.S_OK; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 6b46dca5..8cbf824e 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -150,7 +150,16 @@ internal unsafe partial struct PixelShaderEffect /// /// The number of inputs for the shader. /// - private int numberOfInputs; + private int inputCount; + + /// + /// The buffer with the types of inputs for the shader. + /// + /// + /// This buffer is shared among effect instances, so it should not be released. It is + /// owned by , which will release it when the target type is unloaded. + /// + private D2D1PixelShaderInputType* inputTypes; /// /// The shader bytecode. @@ -167,11 +176,6 @@ internal unsafe partial struct PixelShaderEffect /// private GCHandle d2D1TransformMapperHandle; - /// - /// The current input rectangle. - /// - private RECT inputRect; - /// /// The instance currently in use. /// @@ -181,7 +185,8 @@ internal unsafe partial struct PixelShaderEffect /// The factory method for . /// /// The for the shader. - /// The number of inputs for the shader. + /// The number of inputs for the shader. + /// The buffer with the types of inputs for the shader. /// The shader bytecode. /// The size of . /// The instance to use for the effect. @@ -189,7 +194,8 @@ internal unsafe partial struct PixelShaderEffect /// This always returns 0. private static int Factory( Guid shaderId, - int numberOfInputs, + int inputCount, + D2D1PixelShaderInputType* inputTypes, byte* bytecode, int bytecodeSize, D2D1TransformMapper? d2D1TransformMapper, @@ -203,7 +209,8 @@ internal unsafe partial struct PixelShaderEffect @this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform; @this->referenceCount = 1; @this->shaderId = shaderId; - @this->numberOfInputs = numberOfInputs; + @this->inputCount = inputCount; + @this->inputTypes = inputTypes; @this->bytecode = bytecode; @this->bytecodeSize = bytecodeSize; @this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper); diff --git a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs index 83bdfaaa..e22e6bc8 100644 --- a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs +++ b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs @@ -18,5 +18,8 @@ namespace TerraFX.Interop.Windows [NativeTypeName("#define E_POINTER _HRESULT_TYPEDEF_(0x80004003L)")] public const int E_POINTER = unchecked((int)(0x80004003)); + + [NativeTypeName("#define E_FAIL _HRESULT_TYPEDEF_(0x80004005L)")] + public const int E_FAIL = unchecked((int)(0x80004005)); } } \ No newline at end of file