diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 49386763..0fe38334 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; -using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; +using ComputeSharp.D2D1.Shaders.Interop.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER @@ -96,65 +96,9 @@ internal unsafe partial struct PixelShaderEffect // 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) + if (hresult == E.E_INVALIDARG && !effectContext->IsShaderNotSupported(@this->bytecode, @this->bytecodeSize)) { - 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; - } - - D3D_FEATURE_LEVEL d3DMinFeatureLevel; - - // Get the minimum feature level for the shader (in case of errors, just return the previous result again) - if (!Windows.SUCCEEDED(d3D11ShaderReflection.Get()->GetMinFeatureLevel(&d3DMinFeatureLevel))) - { - return E.E_INVALIDARG; - } - - D3D_FEATURE_LEVEL d3DMaxFeatureLevel; - - // Check whether the current context supports the minimum feature level - HRESULT hresultMaximumSupportedFeatureLevel = effectContext->GetMaximumSupportedFeatureLevel( - featureLevels: &d3DMinFeatureLevel, - featureLevelsCount: 1, - maximumSupportedFeatureLevel: &d3DMaxFeatureLevel); - - // If the context doesn't match the required feature level, return that as an error - if (hresultMaximumSupportedFeatureLevel == D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES) - { - return D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; - } - - // If the call failed for another reason, also just stop here and return the previous result - if (!Windows.SUCCEEDED(hresultMaximumSupportedFeatureLevel)) - { - return 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) - { - // 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; - } - } + hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; } // If loading the bytecode succeeded, set the transform node diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs index 39a188b0..d45990a8 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -8,7 +9,7 @@ namespace ComputeSharp.D2D1.Shaders.Interop.Extensions; /// /// Extensions for the type. /// -internal static class ID2D1EffectContextExtensions +internal static unsafe class ID2D1EffectContextExtensions { /// /// Gets an from an input object. @@ -37,7 +38,7 @@ internal static class ID2D1EffectContextExtensions /// be cast to , which can then be kept and used to synchronize later calls. /// /// - public static unsafe int GetD2D1Multithread(this ref ID2D1EffectContext effectContext, ID2D1Multithread** multithread) + public static int GetD2D1Multithread(this ref ID2D1EffectContext effectContext, ID2D1Multithread** multithread) { using ComPtr floodEffect = default; @@ -63,4 +64,75 @@ internal static class ID2D1EffectContextExtensions return hresult; } + + /// + /// Checks whether the input shader is not supported by the target instance. + /// + /// The input instance. + /// The shader bytecode. + /// The size of . + /// Whether the shader is not supported. + /// If any calls needed to check support fail, the method will also return . + public static bool IsShaderNotSupported(this ref ID2D1EffectContext effectContext, byte* bytecode, int bytecodeSize) + { + 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: bytecode, + SrcDataSize: (uint)bytecodeSize, + pInterface: Windows.__uuidof(), + ppReflector: d3D11ShaderReflection.GetVoidAddressOf()))) + { + return false; + } + + D3D_FEATURE_LEVEL d3DMinFeatureLevel; + + // Get the minimum feature level for the shader (in case of errors, just return the previous result again) + if (!Windows.SUCCEEDED(d3D11ShaderReflection.Get()->GetMinFeatureLevel(&d3DMinFeatureLevel))) + { + return false; + } + + D3D_FEATURE_LEVEL d3DMaxFeatureLevel; + + // Check whether the current context supports the minimum feature level + HRESULT hresultMaximumSupportedFeatureLevel = effectContext.GetMaximumSupportedFeatureLevel( + featureLevels: &d3DMinFeatureLevel, + featureLevelsCount: 1, + maximumSupportedFeatureLevel: &d3DMaxFeatureLevel); + + // If the context doesn't match the required feature level, return that as an error + if (hresultMaximumSupportedFeatureLevel == D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES) + { + return true; + } + + // If the call failed for another reason, also just stop here and return the previous result + if (!Windows.SUCCEEDED(hresultMaximumSupportedFeatureLevel)) + { + return false; + } + + 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 false; + } + + // If the context does not support double precision values, check whether the shader requested them + if (d2D1FeatureDataDoubles.doublePrecisionFloatShaderOps == 0) + { + // 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 true; + } + } + + return false; + } } \ No newline at end of file