From 64be81e56f5c8743053d1e85ab72b5c8e0d243d8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 19 Oct 2022 00:46:01 +0200 Subject: [PATCH] Invert and improve logic for IsShaderSupported API --- ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 3 +- ...mpl.ID2D1ResourceTextureManagerInternal.cs | 2 +- .../ID2D1EffectContextExtensions.cs | 60 ++++++++++++------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 0fe38334..3dae4865 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -96,7 +96,8 @@ 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 && !effectContext->IsShaderNotSupported(@this->bytecode, @this->bytecodeSize)) + // If any error is detected while trying to check for shader support, ignore the value and propagate the current HRESULT. + if (hresult == E.E_INVALIDARG && effectContext->IsShaderSupported(@this->bytecode, @this->bytecodeSize) == S.S_FALSE) { hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs index 7472fb31..9f7cc023 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs @@ -113,7 +113,7 @@ partial struct D2D1ResourceTextureManagerImpl { using ComPtr d2D1Multithread = default; - int hresult = effectContext->GetD2D1Multithread(d2D1Multithread.GetAddressOf()); + HRESULT hresult = effectContext->GetD2D1Multithread(d2D1Multithread.GetAddressOf()); // If an ID2D1Multithread object is available, we can safely store the context. That // is, under the condition that the required multithread support is also available. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs index d45990a8..a9148353 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Extensions/ID2D1EffectContextExtensions.cs @@ -38,7 +38,7 @@ internal static unsafe class ID2D1EffectContextExtensions /// be cast to , which can then be kept and used to synchronize later calls. /// /// - public static int GetD2D1Multithread(this ref ID2D1EffectContext effectContext, ID2D1Multithread** multithread) + public static HRESULT GetD2D1Multithread(this ref ID2D1EffectContext effectContext, ID2D1Multithread** multithread) { using ComPtr floodEffect = default; @@ -66,73 +66,87 @@ internal static unsafe class ID2D1EffectContextExtensions } /// - /// Checks whether the input shader is not supported by the target instance. + /// Checks whether the input shader is supported by the target instance. /// /// The input instance. /// The shader bytecode. /// The size of . - /// Whether the shader is not supported. + /// + /// The for the operation: + /// + /// : the shader is supported. + /// : the shader is not supported. + /// Any other : an error occurred while checking the shader support. + /// + /// /// 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) + public static HRESULT IsShaderSupported(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( + // Create the reflection instance to extract the shader info + HRESULT hresult = DirectX.D3DReflect( pSrcData: bytecode, SrcDataSize: (uint)bytecodeSize, pInterface: Windows.__uuidof(), - ppReflector: d3D11ShaderReflection.GetVoidAddressOf()))) + ppReflector: d3D11ShaderReflection.GetVoidAddressOf()); + + if (!Windows.SUCCEEDED(hresult)) { - return false; + return hresult; } 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))) + // Get the minimum feature level for the shader + hresult = d3D11ShaderReflection.Get()->GetMinFeatureLevel(&d3DMinFeatureLevel); + + if (!Windows.SUCCEEDED(hresult)) { - return false; + return hresult; } D3D_FEATURE_LEVEL d3DMaxFeatureLevel; // Check whether the current context supports the minimum feature level - HRESULT hresultMaximumSupportedFeatureLevel = effectContext.GetMaximumSupportedFeatureLevel( + hresult = 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) + if (hresult == D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES) { - return true; + return S.S_FALSE; } - // If the call failed for another reason, also just stop here and return the previous result - if (!Windows.SUCCEEDED(hresultMaximumSupportedFeatureLevel)) + // If the call failed for any other reason, stop here like before + if (!Windows.SUCCEEDED(hresult)) { - return false; + return hresult; } 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)))) + // Check the feature support for the effect context in use + hresult = effectContext.CheckFeatureSupport(D2D1_FEATURE.D2D1_FEATURE_DOUBLES, &d2D1FeatureDataDoubles, (uint)sizeof(D2D1_FEATURE_DATA_DOUBLES)); + + if (!Windows.SUCCEEDED(hresult)) { - return false; + return hresult; } // 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 the shader requires double precision support, the shader is not supported if ((d3D11ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0) { - return true; + return S.S_FALSE; } } - return false; + // All calls succeeded and no unsupported feature was detected, so assume the shader is supported + return S.S_OK; } } \ No newline at end of file