diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
index 893d04a4..a87af065 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
@@ -110,7 +110,16 @@ partial struct PixelShaderEffect
{
@this = (PixelShaderEffect*)&((void**)@this)[-1];
- return (uint)@this->GetGlobals().InputCount;
+ 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;
+ }
}
///
@@ -119,47 +128,54 @@ partial struct PixelShaderEffect
{
@this = (PixelShaderEffect*)&((void**)@this)[-1];
- if (inputRectsCount != @this->GetGlobals().InputCount)
+ try
{
- return E.E_INVALIDARG;
- }
-
- if (@this->d2D1TransformMapper is not null)
- {
- // Forward to the current ID2D1TransformMapper instance
- HRESULT hresult = @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount);
-
- if (!Windows.SUCCEEDED(hresult))
+ if (inputRectsCount != @this->GetGlobals().InputCount)
{
- return hresult;
+ return E.E_INVALIDARG;
}
- }
- 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++)
+ if (@this->d2D1TransformMapper is not null)
{
- switch (inputTypes[(int)i])
+ // Forward to the current ID2D1TransformMapper instance
+ HRESULT hresult = @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount);
+
+ if (!Windows.SUCCEEDED(hresult))
{
- case D2D1PixelShaderInputType.Simple:
- inputRects[i] = *outputRect;
- break;
- case D2D1PixelShaderInputType.Complex:
- inputRects[i].MakeD2D1Infinite();
- break;
- default:
- return E.E_FAIL;
+ return hresult;
}
}
- }
+ else
+ {
+ ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span;
- return S.S_OK;
+ // 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);
+ }
}
///
@@ -168,93 +184,100 @@ partial struct PixelShaderEffect
{
@this = (PixelShaderEffect*)&((void**)@this)[-1];
- if (inputRectCount != @this->GetGlobals().InputCount)
+ try
{
- return E.E_INVALIDARG;
- }
-
- if (@this->d2D1TransformMapper is not null)
- {
- using ComPtr d2D1DrawInfoUpdateContext = default;
-
- // Create an ID2D1DrawInfoUpdateContext instance
- HRESULT hresult = D2D1DrawInfoUpdateContextImpl.Factory(
- drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(),
- constantBuffer: @this->constantBuffer,
- constantBufferSize: @this->GetGlobals().ConstantBufferSize,
- d2D1DrawInfo: @this->d2D1DrawInfo);
-
- if (!Windows.SUCCEEDED(hresult))
+ if (inputRectCount != @this->GetGlobals().InputCount)
{
- return hresult;
+ return E.E_INVALIDARG;
}
- // Forward the call to the input ID2D1TransformMapper instance
- 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();
-
- if (!Windows.SUCCEEDED(hresult))
+ if (@this->d2D1TransformMapper is not null)
{
- return hresult;
- }
- }
- 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();
+ using ComPtr d2D1DrawInfoUpdateContext = default;
- *outputOpaqueSubRect = default;
- }
- else
- {
- ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span;
+ // Create an ID2D1DrawInfoUpdateContext instance
+ HRESULT hresult = D2D1DrawInfoUpdateContextImpl.Factory(
+ drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(),
+ constantBuffer: @this->constantBuffer,
+ constantBufferSize: @this->GetGlobals().ConstantBufferSize,
+ d2D1DrawInfo: @this->d2D1DrawInfo);
- // 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 (!Windows.SUCCEEDED(hresult))
{
- if (isUnionOfSimpleRectsEmpty)
- {
- unionOfSimpleRects = inputRects[i];
- isUnionOfSimpleRectsEmpty = false;
- }
- else
- {
- unionOfSimpleRects = unionOfSimpleRects.Union(inputRects[i]);
- }
+ return hresult;
+ }
+
+ // Forward the call to the input ID2D1TransformMapper instance
+ 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();
+
+ if (!Windows.SUCCEEDED(hresult))
+ {
+ return hresult;
}
}
-
- if (isUnionOfSimpleRectsEmpty)
+ 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
{
- *outputRect = unionOfSimpleRects;
+ 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;
}
- *outputOpaqueSubRect = default;
+ return S.S_OK;
+ }
+ catch (Exception e)
+ {
+ return Marshal.GetHRForException(e);
}
-
- return S.S_OK;
}
///
@@ -263,39 +286,46 @@ partial struct PixelShaderEffect
{
@this = (PixelShaderEffect*)&((void**)@this)[-1];
- if (inputIndex >= (uint)@this->GetGlobals().InputCount)
+ try
{
- return E.E_INVALIDARG;
- }
-
- if (@this->d2D1TransformMapper is not null)
- {
- // Forward to the current ID2D1TransformMapper instance
- HRESULT hresult = @this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect);
-
- if (!Windows.SUCCEEDED(hresult))
+ if (inputIndex >= (uint)@this->GetGlobals().InputCount)
{
- return hresult;
+ return E.E_INVALIDARG;
}
- }
- 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;
+ if (@this->d2D1TransformMapper is not null)
+ {
+ // Forward to the current ID2D1TransformMapper instance
+ HRESULT hresult = @this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect);
+
+ if (!Windows.SUCCEEDED(hresult))
+ {
+ return hresult;
+ }
+ }
+ 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);
+ }
}
///
@@ -304,58 +334,65 @@ partial struct PixelShaderEffect
{
@this = (PixelShaderEffect*)&((void**)@this)[-1];
- // Free the previous ID2D1DrawInfo object, if present
- ComPtr.Release(@this->d2D1DrawInfo);
-
- // Store the new ID2D1DrawInfo object
- _ = drawInfo->AddRef();
-
- @this->d2D1DrawInfo = drawInfo;
-
- Guid shaderId = @this->GetGlobals().EffectId;
- D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions;
-
- // Set the pixel shader for the effect
- HRESULT hresult = drawInfo->SetPixelShader(&shaderId, (D2D1_PIXEL_OPTIONS)pixelOptions);
-
- if (hresult != S.S_OK)
+ try
{
- return hresult;
- }
+ // Free the previous ID2D1DrawInfo object, if present
+ ComPtr.Release(@this->d2D1DrawInfo);
- // 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;
+ // Store the new ID2D1DrawInfo object
+ _ = drawInfo->AddRef();
- hresult = drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription);
+ @this->d2D1DrawInfo = drawInfo;
+
+ Guid shaderId = @this->GetGlobals().EffectId;
+ D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions;
+
+ // Set the pixel shader for the effect
+ HRESULT hresult = drawInfo->SetPixelShader(&shaderId, (D2D1_PIXEL_OPTIONS)pixelOptions);
if (hresult != S.S_OK)
{
return hresult;
}
- }
- 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)
- {
- hresult = drawInfo->SetOutputBuffer(
- (D2D1_BUFFER_PRECISION)bufferPrecision,
- (D2D1_CHANNEL_DEPTH)channelDepth);
-
- if (hresult != S.S_OK)
+ // If any input descriptions are present, set them
+ foreach (ref readonly D2D1InputDescription inputDescription in @this->GetGlobals().InputDescriptions.Span)
{
- return hresult;
- }
- }
+ D2D1_INPUT_DESCRIPTION d2D1InputDescription;
+ d2D1InputDescription.filter = (D2D1_FILTER)inputDescription.Filter;
+ d2D1InputDescription.levelOfDetailCount = (uint)inputDescription.LevelOfDetailCount;
- return S.S_OK;
+ hresult = drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription);
+
+ if (hresult != S.S_OK)
+ {
+ return hresult;
+ }
+ }
+
+ 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)
+ {
+ hresult = drawInfo->SetOutputBuffer(
+ (D2D1_BUFFER_PRECISION)bufferPrecision,
+ (D2D1_CHANNEL_DEPTH)channelDepth);
+
+ if (hresult != S.S_OK)
+ {
+ return hresult;
+ }
+ }
+
+ return S.S_OK;
+ }
+ catch (Exception e)
+ {
+ return Marshal.GetHRForException(e);
+ }
}
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
index 5f1d26bf..fea6cd51 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
@@ -86,121 +86,135 @@ internal unsafe partial struct PixelShaderEffect
[UnmanagedCallersOnly]
public static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph)
{
- ReadOnlySpan bytecode = @this->GetGlobals().HlslBytecode.Span;
- int hresult;
-
- fixed (Guid* pShaderId = &@this->GetGlobals().EffectId)
- fixed (byte* pBytecode = bytecode)
+ try
{
- hresult = effectContext->LoadPixelShader(
- shaderId: pShaderId,
- shaderBuffer: pBytecode,
- shaderBufferCount: (uint)bytecode.Length);
+ ReadOnlySpan bytecode = @this->GetGlobals().HlslBytecode.Span;
+ int hresult;
- // 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 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(pBytecode, bytecode.Length) == S.S_FALSE)
+ fixed (Guid* pShaderId = &@this->GetGlobals().EffectId)
+ fixed (byte* pBytecode = bytecode)
{
- hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES;
+ hresult = effectContext->LoadPixelShader(
+ shaderId: pShaderId,
+ shaderBuffer: pBytecode,
+ shaderBufferCount: (uint)bytecode.Length);
+
+ // 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 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(pBytecode, bytecode.Length) == S.S_FALSE)
+ {
+ hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES;
+ }
}
- }
- // If loading the bytecode succeeded, set the transform node
- if (Windows.SUCCEEDED(hresult))
+ // 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
+ ComPtr.Release(@this->d2D1EffectContext);
+
+ // Store the new ID2D1EffectContext object
+ _ = effectContext->AddRef();
+
+ @this->d2D1EffectContext = effectContext;
+ }
+
+ return hresult;
+ }
+ catch (Exception e)
{
- hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform);
+ return Marshal.GetHRForException(e);
}
-
- // If the transform node was set, also store the effect context
- if (Windows.SUCCEEDED(hresult))
- {
- // Free the previous ID2D1EffectContext object, if present
- ComPtr.Release(@this->d2D1EffectContext);
-
- // Store the new ID2D1EffectContext object
- _ = effectContext->AddRef();
-
- @this->d2D1EffectContext = effectContext;
- }
-
- return hresult;
}
///
[UnmanagedCallersOnly]
public static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType)
{
- int hresult = S.S_OK;
-
- // Validate the constant buffer
- if (@this->GetGlobals().ConstantBufferSize > 0 &&
- @this->constantBuffer is null)
+ try
{
- return E.E_NOT_VALID_STATE;
- }
+ 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->GetGlobals().ConstantBufferSize);
- }
-
- if (Windows.SUCCEEDED(hresult))
- {
- ReadOnlySpan resourceTextureDescriptions = @this->GetGlobals().ResourceTextureDescriptions.Span;
-
- for (int i = 0; i < resourceTextureDescriptions.Length; i++)
+ // Validate the constant buffer
+ if (@this->GetGlobals().ConstantBufferSize > 0 &&
+ @this->constantBuffer is null)
{
- using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i];
+ return E.E_NOT_VALID_STATE;
+ }
- // If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture
- if (resourceTextureManager.Get() is null)
+ // First, set the constant buffer, if available
+ if (@this->constantBuffer is not null)
+ {
+ hresult = @this->d2D1DrawInfo->SetPixelShaderConstantBuffer(
+ buffer: @this->constantBuffer,
+ bufferCount: (uint)@this->GetGlobals().ConstantBufferSize);
+ }
+
+ if (Windows.SUCCEEDED(hresult))
+ {
+ ReadOnlySpan resourceTextureDescriptions = @this->GetGlobals().ResourceTextureDescriptions.Span;
+
+ for (int i = 0; i < resourceTextureDescriptions.Length; i++)
{
- hresult = E.E_NOT_VALID_STATE;
+ using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i];
- break;
- }
+ // If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture
+ if (resourceTextureManager.Get() is null)
+ {
+ hresult = E.E_NOT_VALID_STATE;
- using ComPtr resourceTextureManagerInternal = default;
+ break;
+ }
- // Get the ID2D1ResourceTextureManagerInternal object
- hresult = resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf());
+ using ComPtr resourceTextureManagerInternal = default;
- // This cast should always succeed, as when an input resource texture managers is set it's
- // also checked for ID2D1ResourceTextureManagerInternal, but still validate for good measure.
- if (!Windows.SUCCEEDED(hresult))
- {
- break;
- }
+ // Get the ID2D1ResourceTextureManagerInternal object
+ hresult = resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf());
- using ComPtr d2D1ResourceTexture = default;
+ // This cast should always succeed, as when an input resource texture managers is set it's
+ // also checked for ID2D1ResourceTextureManagerInternal, but still validate for good measure.
+ if (!Windows.SUCCEEDED(hresult))
+ {
+ break;
+ }
- // Try to get the ID2D1ResourceTexture from the manager
- hresult = resourceTextureManagerInternal.Get()->GetResourceTexture(d2D1ResourceTexture.GetAddressOf());
+ using ComPtr d2D1ResourceTexture = default;
- if (!Windows.SUCCEEDED(hresult))
- {
- break;
- }
+ // Try to get the ID2D1ResourceTexture from the manager
+ hresult = resourceTextureManagerInternal.Get()->GetResourceTexture(d2D1ResourceTexture.GetAddressOf());
- ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i];
+ 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());
+ ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i];
- 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;
+ return hresult;
+ }
+ catch (Exception e)
+ {
+ return Marshal.GetHRForException(e);
+ }
}
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
index 25dc7e52..862e24dd 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
@@ -115,46 +115,53 @@ unsafe partial struct PixelShaderEffect
{
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
- // This is a call likely from ID2D1Effect::GetValueSize, to query the property size
- if (data is null &&
- dataSize == 0 &&
- actualSize is not null)
+ try
{
- *actualSize = (uint)@this->GetGlobals().ConstantBufferSize;
+ // This is a call likely from ID2D1Effect::GetValueSize, to query the property size
+ if (data is null &&
+ dataSize == 0 &&
+ actualSize is not null)
+ {
+ *actualSize = (uint)@this->GetGlobals().ConstantBufferSize;
- return S.S_FALSE;
+ return S.S_FALSE;
+ }
+
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
+
+ if (dataSize < @this->GetGlobals().ConstantBufferSize)
+ {
+ return E.E_NOT_SUFFICIENT_BUFFER;
+ }
+
+ if (@this->constantBuffer is null)
+ {
+ return E.E_NOT_VALID_STATE;
+ }
+
+ uint writtenBytes = 0;
+
+ if (@this->GetGlobals().ConstantBufferSize > 0)
+ {
+ Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, @this->GetGlobals().ConstantBufferSize);
+
+ writtenBytes = (uint)@this->GetGlobals().ConstantBufferSize;
+ }
+
+ if (actualSize is not null)
+ {
+ *actualSize = writtenBytes;
+ }
+
+ return S.S_OK;
}
-
- if (data is null)
+ catch (Exception e)
{
- return E.E_POINTER;
+ return Marshal.GetHRForException(e);
}
-
- if (dataSize < @this->GetGlobals().ConstantBufferSize)
- {
- return E.E_NOT_SUFFICIENT_BUFFER;
- }
-
- if (@this->constantBuffer is null)
- {
- return E.E_NOT_VALID_STATE;
- }
-
- uint writtenBytes = 0;
-
- if (@this->GetGlobals().ConstantBufferSize > 0)
- {
- Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, @this->GetGlobals().ConstantBufferSize);
-
- writtenBytes = (uint)@this->GetGlobals().ConstantBufferSize;
- }
-
- if (actualSize is not null)
- {
- *actualSize = writtenBytes;
- }
-
- return S.S_OK;
}
///
@@ -163,31 +170,38 @@ unsafe partial struct PixelShaderEffect
{
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
- if (data is null)
+ try
{
- return E.E_POINTER;
- }
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
- if (dataSize != (uint)@this->GetGlobals().ConstantBufferSize)
+ if (dataSize != (uint)@this->GetGlobals().ConstantBufferSize)
+ {
+ return E.E_INVALIDARG;
+ }
+
+ // Reuse the existing buffer if there is one, otherwise allocate a new one
+ if (@this->constantBuffer is not null)
+ {
+ Buffer.MemoryCopy(data, @this->constantBuffer, dataSize, dataSize);
+ }
+ else
+ {
+ void* buffer = NativeMemory.Alloc(dataSize);
+
+ Buffer.MemoryCopy(data, buffer, dataSize, dataSize);
+
+ @this->constantBuffer = (byte*)buffer;
+ }
+
+ return S.S_OK;
+ }
+ catch (Exception e)
{
- return E.E_INVALIDARG;
+ return Marshal.GetHRForException(e);
}
-
- // Reuse the existing buffer if there is one, otherwise allocate a new one
- if (@this->constantBuffer is not null)
- {
- Buffer.MemoryCopy(data, @this->constantBuffer, dataSize, dataSize);
- }
- else
- {
- void* buffer = NativeMemory.Alloc(dataSize);
-
- Buffer.MemoryCopy(data, buffer, dataSize, dataSize);
-
- @this->constantBuffer = (byte*)buffer;
- }
-
- return S.S_OK;
}
///
@@ -196,36 +210,43 @@ unsafe partial struct PixelShaderEffect
{
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
- if (data is null)
+ try
{
- return E.E_POINTER;
- }
-
- if (dataSize < sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
-
- if (@this->d2D1TransformMapper is null)
- {
- *(void**)data = null;
- }
- else
- {
- HRESULT hresult = ((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof(), (void**)data);
-
- if (!Windows.SUCCEEDED(hresult))
+ if (data is null)
{
- return hresult;
+ return E.E_POINTER;
}
- }
- if (actualSize is not null)
+ if (dataSize < sizeof(void*))
+ {
+ return E.E_INVALIDARG;
+ }
+
+ if (@this->d2D1TransformMapper is null)
+ {
+ *(void**)data = null;
+ }
+ else
+ {
+ HRESULT hresult = ((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof(), (void**)data);
+
+ if (!Windows.SUCCEEDED(hresult))
+ {
+ return hresult;
+ }
+ }
+
+ if (actualSize is not null)
+ {
+ *actualSize = (uint)sizeof(void*);
+ }
+
+ return S.S_OK;
+ }
+ catch (Exception e)
{
- *actualSize = (uint)sizeof(void*);
+ return Marshal.GetHRForException(e);
}
-
- return S.S_OK;
}
///
@@ -234,41 +255,48 @@ unsafe partial struct PixelShaderEffect
{
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
- if (data is null)
+ try
{
- return E.E_POINTER;
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
+
+ void* value = *(void**)data;
+
+ if (value is null)
+ {
+ return E.E_POINTER;
+ }
+
+ if (dataSize != (uint)sizeof(void*))
+ {
+ return E.E_INVALIDARG;
+ }
+
+ using ComPtr unknown = (IUnknown*)value;
+ using ComPtr transformMapper = default;
+
+ // Check that the input object implements ID2D1TransformMapper
+ int result = unknown.CopyTo(transformMapper.GetAddressOf());
+
+ if (result != S.S_OK)
+ {
+ return result;
+ }
+
+ // If there's already an existing manager, release it
+ ComPtr.Release(@this->d2D1TransformMapper);
+
+ // Store the transform mapper manager into the effect
+ @this->d2D1TransformMapper = transformMapper.Detach();
+
+ return S.S_OK;
}
-
- void* value = *(void**)data;
-
- if (value is null)
+ catch (Exception e)
{
- return E.E_POINTER;
+ return Marshal.GetHRForException(e);
}
-
- if (dataSize != (uint)sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
-
- using ComPtr unknown = (IUnknown*)value;
- using ComPtr transformMapper = default;
-
- // Check that the input object implements ID2D1TransformMapper
- int result = unknown.CopyTo(transformMapper.GetAddressOf());
-
- if (result != S.S_OK)
- {
- return result;
- }
-
- // If there's already an existing manager, release it
- ComPtr.Release(@this->d2D1TransformMapper);
-
- // Store the transform mapper manager into the effect
- @this->d2D1TransformMapper = transformMapper.Detach();
-
- return S.S_OK;
}
///
@@ -281,36 +309,43 @@ unsafe partial struct PixelShaderEffect
/// The for the operation.
private int GetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize, uint* actualSize)
{
- if (index >= GetGlobals().ResourceTextureCount)
+ try
{
- return E.E_INVALIDARG;
- }
+ if (index >= GetGlobals().ResourceTextureCount)
+ {
+ return E.E_INVALIDARG;
+ }
- if (data is null)
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
+
+ if (dataSize < sizeof(void*))
+ {
+ return E.E_INVALIDARG;
+ }
+
+ using ComPtr resourceTextureManager = this.resourceTextureManagerBuffer[index];
+
+ HRESULT hresult = resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data);
+
+ if (!Windows.SUCCEEDED(hresult))
+ {
+ return hresult;
+ }
+
+ if (actualSize is not null)
+ {
+ *actualSize = (uint)sizeof(void*);
+ }
+
+ return S.S_OK;
+ }
+ catch (Exception e)
{
- return E.E_POINTER;
+ return Marshal.GetHRForException(e);
}
-
- if (dataSize < sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
-
- using ComPtr resourceTextureManager = this.resourceTextureManagerBuffer[index];
-
- HRESULT hresult = resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data);
-
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
-
- if (actualSize is not null)
- {
- *actualSize = (uint)sizeof(void*);
- }
-
- return S.S_OK;
}
///
@@ -322,75 +357,82 @@ unsafe partial struct PixelShaderEffect
/// The for the operation.
private int SetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize)
{
- if (index >= GetGlobals().ResourceTextureCount)
+ try
{
- return E.E_INVALIDARG;
- }
+ if (index >= GetGlobals().ResourceTextureCount)
+ {
+ return E.E_INVALIDARG;
+ }
- if (data is null)
- {
- return E.E_POINTER;
- }
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
- void* value = *(void**)data;
+ void* value = *(void**)data;
- if (value is null)
- {
- return E.E_POINTER;
- }
+ if (value is null)
+ {
+ return E.E_POINTER;
+ }
- if (dataSize != (uint)sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
+ if (dataSize != (uint)sizeof(void*))
+ {
+ return E.E_INVALIDARG;
+ }
- using ComPtr unknown = (IUnknown*)value;
- using ComPtr resourceTextureManager = default;
+ using ComPtr unknown = (IUnknown*)value;
+ using ComPtr resourceTextureManager = default;
- // Check that the input object implements ID2D1ResourceTextureManager
- int result = unknown.CopyTo(resourceTextureManager.GetAddressOf());
+ // Check that the input object implements ID2D1ResourceTextureManager
+ int result = unknown.CopyTo(resourceTextureManager.GetAddressOf());
- if (result != S.S_OK)
- {
- return result;
- }
-
- using ComPtr resourceTextureManagerInternal = default;
-
- // Then, also check that it implements ID2D1ResourceTextureManagerInternal
- result = unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf());
-
- if (result != S.S_OK)
- {
- return result;
- }
-
- // Initialize the resource texture manager, if an effect context is available
- if (this.d2D1EffectContext is not null)
- {
- uint dimensions = (uint)GetGlobals().ResourceTextureDescriptions.Span[index].Dimensions;
-
- result = resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions);
-
- // ID2D1ResourceTextureManager::Initialize should generally return either S_OK for first
- // initialization, S_FALSE if an ID2D1EffectContext is already present (which is allowed, to
- // enable sharing resource texture managers across different source textures and effects), or
- // E_INVALIDARG if the manager has already been initialized through the public interface,
- // and the stored dimensions for that don't match the ones for this resource texture index.
- if (!Windows.SUCCEEDED(result))
+ if (result != S.S_OK)
{
return result;
}
+
+ using ComPtr resourceTextureManagerInternal = default;
+
+ // Then, also check that it implements ID2D1ResourceTextureManagerInternal
+ result = unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf());
+
+ if (result != S.S_OK)
+ {
+ return result;
+ }
+
+ // Initialize the resource texture manager, if an effect context is available
+ if (this.d2D1EffectContext is not null)
+ {
+ uint dimensions = (uint)GetGlobals().ResourceTextureDescriptions.Span[index].Dimensions;
+
+ result = resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions);
+
+ // ID2D1ResourceTextureManager::Initialize should generally return either S_OK for first
+ // initialization, S_FALSE if an ID2D1EffectContext is already present (which is allowed, to
+ // enable sharing resource texture managers across different source textures and effects), or
+ // E_INVALIDARG if the manager has already been initialized through the public interface,
+ // and the stored dimensions for that don't match the ones for this resource texture index.
+ if (!Windows.SUCCEEDED(result))
+ {
+ return result;
+ }
+ }
+
+ ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index];
+
+ // If there's already an existing manager at this index, release it
+ ComPtr.Release(currentResourceTextureManager);
+
+ // Store the resource texture manager into the buffer
+ currentResourceTextureManager = resourceTextureManager.Detach();
+
+ return S.S_OK;
+ }
+ catch (Exception e)
+ {
+ return Marshal.GetHRForException(e);
}
-
- ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index];
-
- // If there's already an existing manager at this index, release it
- ComPtr.Release(currentResourceTextureManager);
-
- // Store the resource texture manager into the buffer
- currentResourceTextureManager = resourceTextureManager.Detach();
-
- return S.S_OK;
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
index 8aa37e86..fb3a2acf 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
@@ -233,13 +233,7 @@ internal unsafe partial struct PixelShaderEffect
if (referenceCount == 0)
{
- // Retrieve the number of resource textures before freeing the globals handle
- int resourceTextureDescriptionCount = GetGlobals().ResourceTextureCount;
-
- if (this.globalsHandle.IsAllocated)
- {
- this.globalsHandle.Free();
- }
+ this.globalsHandle.Free();
NativeMemory.Free(this.constantBuffer);
@@ -247,8 +241,10 @@ internal unsafe partial struct PixelShaderEffect
ComPtr.Release(this.d2D1DrawInfo);
ComPtr.Release(this.d2D1EffectContext);
- // Retrieve all possible resource texture managers in use and release the ones that had been assigned (from one of the property bindings)
- for (int i = 0; i < resourceTextureDescriptionCount; i++)
+ // Retrieve all possible resource texture managers in use and release the ones that had been
+ // assigned (from one of the property bindings). We just hardcode 16 here and dont access
+ // the globals, as technically invoking APIs on it might throw an exception.
+ for (int i = 0; i < 16; i++)
{
ComPtr.Release(this.resourceTextureManagerBuffer[i]);
}