using System; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute; #endif namespace ComputeSharp.D2D1.Interop.Effects; /// /// A simple and implementation for a given pixel shader. /// internal unsafe partial struct PixelShaderEffect { /// /// The implementation for . /// private static class ID2D1EffectImplMethods { #if !NET6_0_OR_GREATER /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int InitializeDelegate(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int PrepareForRenderDelegate(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int SetGraphDelegate(PixelShaderEffect* @this, ID2D1TransformGraph* transformGraph); /// /// 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 InitializeDelegate InitializeWrapper = Initialize; /// /// A cached instance wrapping . /// public static readonly PrepareForRenderDelegate PrepareForRenderWrapper = PrepareForRender; /// /// A cached instance wrapping . /// public static readonly SetGraphDelegate SetGraphWrapper = SetGraph; #endif /// [UnmanagedCallersOnly] public static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) { return @this->QueryInterface(riid, ppvObject); } /// [UnmanagedCallersOnly] public static uint AddRef(PixelShaderEffect* @this) { return @this->AddRef(); } /// [UnmanagedCallersOnly] public static uint Release(PixelShaderEffect* @this) { return @this->Release(); } /// [UnmanagedCallersOnly] public static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph) { int hresult = effectContext->LoadPixelShader( shaderId: &@this->shaderId, shaderBuffer: @this->bytecode, shaderBufferCount: (uint)@this->bytecodeSize); // If E_INVALIDARG was returned, try to check whether double precision support was requested when not available. This // is only done to provide a more helpful error message to callers. If no error was returned, the behavior is the same. if (hresult == E.E_INVALIDARG) { D2D1_FEATURE_DATA_DOUBLES d2D1FeatureDataDoubles = default; // If the call failed, just do nothing and return the previous result if (!Windows.SUCCEEDED(effectContext->CheckFeatureSupport(D2D1_FEATURE.D2D1_FEATURE_DOUBLES, &d2D1FeatureDataDoubles, (uint)sizeof(D2D1_FEATURE_DATA_DOUBLES)))) { return E.E_INVALIDARG; } // If the context does not support double precision values, check whether the shader requested them if (d2D1FeatureDataDoubles.doublePrecisionFloatShaderOps == 0) { using ComPtr d3D11ShaderReflection = default; // Create the reflection instance, and in case of error just return the previous error like above if (!Windows.SUCCEEDED(DirectX.D3DReflect( pSrcData: @this->bytecode, SrcDataSize: (uint)@this->bytecodeSize, pInterface: Windows.__uuidof(), ppReflector: d3D11ShaderReflection.GetVoidAddressOf()))) { return E.E_INVALIDARG; } // If the shader requires double precision support, return a more descriptive error if ((d3D11ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0) { return D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; } } } // If loading the bytecode succeeded, set the transform node if (Windows.SUCCEEDED(hresult)) { hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform); } // If the transform node was set, also store the effect context if (Windows.SUCCEEDED(hresult)) { // Free the previous ID2D1EffectContext object, if present if (@this->d2D1EffectContext is not null) { _ = @this->d2D1EffectContext->Release(); } // Store the new ID2D1EffectContext object _ = effectContext->AddRef(); @this->d2D1EffectContext = effectContext; // Then also initialize all available resource texture managers foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(@this->resourceTextureDescriptions, @this->resourceTextureDescriptionCount)) { using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[resourceTextureDescription.Index]; using ComPtr resourceTextureManagerInternal = default; // Get the ID2D1ResourceTextureManagerInternal object (this is guaranteed to succeed here) _ = resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf()); // Initialize the resource texture manager with the effect context hresult = resourceTextureManagerInternal.Get()->SetEffectContext(effectContext); if (!Windows.SUCCEEDED(hresult)) { break; } } } return hresult; } /// [UnmanagedCallersOnly] public static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType) { int hresult = S.S_OK; // First, set the constant buffer, if available if (@this->constantBuffer is not null) { hresult = @this->d2D1DrawInfo->SetPixelShaderConstantBuffer( buffer: @this->constantBuffer, bufferCount: (uint)@this->constantBufferSize); } if (Windows.SUCCEEDED(hresult)) { foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(@this->resourceTextureDescriptions, @this->resourceTextureDescriptionCount)) { using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[resourceTextureDescription.Index]; using ComPtr resourceTextureManagerInternal = default; // Get the ID2D1ResourceTextureManagerInternal object (as above, this is guaranteed to succeed here) _ = resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf()); using ComPtr d2D1ResourceTexture = default; // Try to get the ID2D1ResourceTexture from the manager hresult = resourceTextureManagerInternal.Get()->GetResourceTexture(d2D1ResourceTexture.GetAddressOf()); if (!Windows.SUCCEEDED(hresult)) { break; } // Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use hresult = @this->d2D1DrawInfo->SetResourceTexture( textureIndex: (uint)resourceTextureDescription.Index, resourceTexture: d2D1ResourceTexture.Get()); if (!Windows.SUCCEEDED(hresult)) { break; } } } return hresult; } /// [UnmanagedCallersOnly] public static int SetGraph(PixelShaderEffect* @this, ID2D1TransformGraph* transformGraph) { return E.E_NOTIMPL; } } }