diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs
index 0e617bdf..1533c5cc 100644
--- a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs
+++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs
@@ -69,6 +69,22 @@ internal static class ArgumentOutOfRangeExceptionExtensions
}
}
+ ///
+ /// Throws an if is greater than or equal to .
+ ///
+ /// Dummy value to invoke the extension upon (always pass .
+ /// The argument to validate as less or equal than .
+ /// The value to compare with .
+ /// The name of the parameter with which corresponds.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void ThrowIfGreaterThanOrEqual(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
+ {
+ if (value >= other)
+ {
+ Throw(parameterName, value);
+ }
+ }
+
///
/// Throws an if is less than .
///
@@ -118,6 +134,22 @@ internal static class ArgumentOutOfRangeExceptionExtensions
}
}
+ ///
+ /// Throws an if is not equal to a given other value.
+ ///
+ /// Dummy value to invoke the extension upon (always pass .
+ /// The argument to validate.
+ /// The value to compare against.
+ /// The name of the parameter with which corresponds.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void ThrowIfNotEqual(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
+ {
+ if (value != other)
+ {
+ Throw(parameterName, value);
+ }
+ }
+
///
/// Throws an if is not in the specified range.
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
index a87af065..cc0035b3 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
@@ -130,20 +130,12 @@ partial struct PixelShaderEffect
try
{
- if (inputRectsCount != @this->GetGlobals().InputCount)
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectsCount, @this->GetGlobals().InputCount, nameof(inputRectsCount));
if (@this->d2D1TransformMapper is not null)
{
// Forward to the current ID2D1TransformMapper instance
- HRESULT hresult = @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount);
-
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
+ @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert();
}
else
{
@@ -186,29 +178,21 @@ partial struct PixelShaderEffect
try
{
- if (inputRectCount != @this->GetGlobals().InputCount)
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectCount, @this->GetGlobals().InputCount, nameof(inputRectCount));
if (@this->d2D1TransformMapper is not null)
{
using ComPtr d2D1DrawInfoUpdateContext = default;
// Create an ID2D1DrawInfoUpdateContext instance
- HRESULT hresult = D2D1DrawInfoUpdateContextImpl.Factory(
+ D2D1DrawInfoUpdateContextImpl.Factory(
drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(),
constantBuffer: @this->constantBuffer,
constantBufferSize: @this->GetGlobals().ConstantBufferSize,
- d2D1DrawInfo: @this->d2D1DrawInfo);
-
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
+ d2D1DrawInfo: @this->d2D1DrawInfo).Assert();
// Forward the call to the input ID2D1TransformMapper instance
- hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect(
+ HRESULT hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect(
updateContext: (ID2D1DrawInfoUpdateContext*)d2D1DrawInfoUpdateContext.Get(),
inputRects: inputRects,
inputOpaqueSubRects: inputOpaqueSubRects,
@@ -217,12 +201,10 @@ partial struct PixelShaderEffect
outputOpaqueSubRect: outputOpaqueSubRect);
// Regardless of the operation result, always invalidate the context
- _ = d2D1DrawInfoUpdateContext.Get()->Close();
+ d2D1DrawInfoUpdateContext.Get()->Close().Assert();
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
+ // Now we can validate that the call was in fact successful
+ hresult.Assert();
}
else if (inputRectCount == 0)
{
@@ -288,20 +270,12 @@ partial struct PixelShaderEffect
try
{
- if (inputIndex >= (uint)@this->GetGlobals().InputCount)
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual((int)inputIndex, @this->GetGlobals().InputCount, nameof(inputIndex));
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;
- }
+ @this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert();
}
else
{
@@ -348,12 +322,7 @@ partial struct PixelShaderEffect
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;
- }
+ drawInfo->SetPixelShader(&shaderId, (D2D1_PIXEL_OPTIONS)pixelOptions).Assert();
// If any input descriptions are present, set them
foreach (ref readonly D2D1InputDescription inputDescription in @this->GetGlobals().InputDescriptions.Span)
@@ -362,12 +331,7 @@ partial struct PixelShaderEffect
d2D1InputDescription.filter = (D2D1_FILTER)inputDescription.Filter;
d2D1InputDescription.levelOfDetailCount = (uint)inputDescription.LevelOfDetailCount;
- hresult = drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription);
-
- if (hresult != S.S_OK)
- {
- return hresult;
- }
+ drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription).Assert();
}
D2D1BufferPrecision bufferPrecision = @this->GetGlobals().BufferPrecision;
@@ -377,14 +341,9 @@ partial struct PixelShaderEffect
if (bufferPrecision != D2D1BufferPrecision.Unknown ||
channelDepth != D2D1ChannelDepth.Default)
{
- hresult = drawInfo->SetOutputBuffer(
+ drawInfo->SetOutputBuffer(
(D2D1_BUFFER_PRECISION)bufferPrecision,
- (D2D1_CHANNEL_DEPTH)channelDepth);
-
- if (hresult != S.S_OK)
- {
- return hresult;
- }
+ (D2D1_CHANNEL_DEPTH)channelDepth).Assert();
}
return S.S_OK;
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
index fea6cd51..f13cdbb2 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
@@ -1,5 +1,6 @@
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;
@@ -89,12 +90,11 @@ internal unsafe partial struct PixelShaderEffect
try
{
ReadOnlySpan bytecode = @this->GetGlobals().HlslBytecode.Span;
- int hresult;
fixed (Guid* pShaderId = &@this->GetGlobals().EffectId)
fixed (byte* pBytecode = bytecode)
{
- hresult = effectContext->LoadPixelShader(
+ HRESULT hresult = effectContext->LoadPixelShader(
shaderId: pShaderId,
shaderBuffer: pBytecode,
shaderBufferCount: (uint)bytecode.Length);
@@ -106,27 +106,23 @@ internal unsafe partial struct PixelShaderEffect
{
hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES;
}
+
+ // Finally, assert that we did load the pixel shader correctly
+ hresult.Assert();
}
// If loading the bytecode succeeded, set the transform node
- if (Windows.SUCCEEDED(hresult))
- {
- hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform);
- }
+ transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert();
- // 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);
+ // Free the previous ID2D1EffectContext object, if present
+ ComPtr.Release(@this->d2D1EffectContext);
- // Store the new ID2D1EffectContext object
- _ = effectContext->AddRef();
+ // Store the new ID2D1EffectContext object
+ _ = effectContext->AddRef();
- @this->d2D1EffectContext = effectContext;
- }
+ @this->d2D1EffectContext = effectContext;
- return hresult;
+ return S.S_OK;
}
catch (Exception e)
{
@@ -140,9 +136,10 @@ internal unsafe partial struct PixelShaderEffect
{
try
{
- int hresult = S.S_OK;
-
- // Validate the constant buffer
+ // Validate the constant buffer. We either must have a stateless shader, in which case
+ // the constant buffer might either be null or empty (both cases are allowed), or we
+ // must have a shader with some state and a constant buffer being set. In that case
+ // the code setting the constant buffer will have already validated its length.
if (@this->GetGlobals().ConstantBufferSize > 0 &&
@this->constantBuffer is null)
{
@@ -152,64 +149,44 @@ internal unsafe partial struct PixelShaderEffect
// First, set the constant buffer, if available
if (@this->constantBuffer is not null)
{
- hresult = @this->d2D1DrawInfo->SetPixelShaderConstantBuffer(
+ @this->d2D1DrawInfo->SetPixelShaderConstantBuffer(
buffer: @this->constantBuffer,
- bufferCount: (uint)@this->GetGlobals().ConstantBufferSize);
+ bufferCount: (uint)@this->GetGlobals().ConstantBufferSize).Assert();
}
- if (Windows.SUCCEEDED(hresult))
+ ReadOnlySpan resourceTextureDescriptions = @this->GetGlobals().ResourceTextureDescriptions.Span;
+
+ for (int i = 0; i < resourceTextureDescriptions.Length; i++)
{
- ReadOnlySpan resourceTextureDescriptions = @this->GetGlobals().ResourceTextureDescriptions.Span;
+ using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i];
- for (int i = 0; i < resourceTextureDescriptions.Length; i++)
+ // If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture
+ if (resourceTextureManager.Get() is null)
{
- using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i];
-
- // 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;
-
- break;
- }
-
- using ComPtr resourceTextureManagerInternal = default;
-
- // Get the ID2D1ResourceTextureManagerInternal object
- hresult = resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf());
-
- // 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;
- }
-
- using ComPtr d2D1ResourceTexture = default;
-
- // Try to get the ID2D1ResourceTexture from the manager
- hresult = resourceTextureManagerInternal.Get()->GetResourceTexture(d2D1ResourceTexture.GetAddressOf());
-
- if (!Windows.SUCCEEDED(hresult))
- {
- break;
- }
-
- ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i];
-
- // 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 E.E_NOT_VALID_STATE;
}
+
+ using ComPtr resourceTextureManagerInternal = default;
+
+ // Get the ID2D1ResourceTextureManagerInternal object. 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.
+ resourceTextureManager.CopyTo(resourceTextureManagerInternal.GetAddressOf()).Assert();
+
+ using ComPtr d2D1ResourceTexture = default;
+
+ // Try to get the ID2D1ResourceTexture from the manager
+ resourceTextureManagerInternal.Get()->GetResourceTexture(d2D1ResourceTexture.GetAddressOf()).Assert();
+
+ ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i];
+
+ // Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use
+ @this->d2D1DrawInfo->SetResourceTexture(
+ textureIndex: (uint)resourceTextureDescription.Index,
+ resourceTexture: d2D1ResourceTexture.Get()).Assert();
}
- return hresult;
+ return S.S_OK;
}
catch (Exception e)
{
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
index 862e24dd..6e124bd3 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
@@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using TerraFX.Interop.DirectX;
@@ -117,7 +118,8 @@ unsafe partial struct PixelShaderEffect
try
{
- // This is a call likely from ID2D1Effect::GetValueSize, to query the property size
+ // This is a call likely from ID2D1Effect::GetValueSize, to query the property size.
+ // In that case we explicitly allow these inputs, and return the size of the buffer.
if (data is null &&
dataSize == 0 &&
actualSize is not null)
@@ -127,10 +129,7 @@ unsafe partial struct PixelShaderEffect
return S.S_FALSE;
}
- if (data is null)
- {
- return E.E_POINTER;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
if (dataSize < @this->GetGlobals().ConstantBufferSize)
{
@@ -172,15 +171,8 @@ unsafe partial struct PixelShaderEffect
try
{
- if (data is null)
- {
- return E.E_POINTER;
- }
-
- if (dataSize != (uint)@this->GetGlobals().ConstantBufferSize)
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
+ default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)dataSize, @this->GetGlobals().ConstantBufferSize, nameof(dataSize));
// Reuse the existing buffer if there is one, otherwise allocate a new one
if (@this->constantBuffer is not null)
@@ -212,15 +204,8 @@ unsafe partial struct PixelShaderEffect
try
{
- if (data is null)
- {
- return E.E_POINTER;
- }
-
- if (dataSize < sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
+ default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize));
if (@this->d2D1TransformMapper is null)
{
@@ -228,12 +213,7 @@ unsafe partial struct PixelShaderEffect
}
else
{
- HRESULT hresult = ((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof(), (void**)data);
-
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
+ ((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof(), (void**)data).Assert();
}
if (actualSize is not null)
@@ -257,33 +237,18 @@ unsafe partial struct PixelShaderEffect
try
{
- if (data is null)
- {
- return E.E_POINTER;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
+ default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize));
void* value = *(void**)data;
- if (value is null)
- {
- return E.E_POINTER;
- }
-
- if (dataSize != (uint)sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentNullException).ThrowIfNull(value);
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;
- }
+ unknown.CopyTo(transformMapper.GetAddressOf()).Assert();
// If there's already an existing manager, release it
ComPtr.Release(@this->d2D1TransformMapper);
@@ -311,29 +276,13 @@ unsafe partial struct PixelShaderEffect
{
try
{
- if (index >= GetGlobals().ResourceTextureCount)
- {
- return E.E_INVALIDARG;
- }
-
- if (data is null)
- {
- return E.E_POINTER;
- }
-
- if (dataSize < sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
+ default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize));
+ default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual(index, GetGlobals().ResourceTextureCount);
using ComPtr resourceTextureManager = this.resourceTextureManagerBuffer[index];
- HRESULT hresult = resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data);
-
- if (!Windows.SUCCEEDED(hresult))
- {
- return hresult;
- }
+ resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data).Assert();
if (actualSize is not null)
{
@@ -359,65 +308,36 @@ unsafe partial struct PixelShaderEffect
{
try
{
- if (index >= GetGlobals().ResourceTextureCount)
- {
- return E.E_INVALIDARG;
- }
-
- if (data is null)
- {
- return E.E_POINTER;
- }
+ default(ArgumentNullException).ThrowIfNull(data);
+ default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize));
+ default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual(index, GetGlobals().ResourceTextureCount);
void* value = *(void**)data;
- if (value is null)
- {
- return E.E_POINTER;
- }
-
- if (dataSize != (uint)sizeof(void*))
- {
- return E.E_INVALIDARG;
- }
+ default(ArgumentNullException).ThrowIfNull(value);
using ComPtr unknown = (IUnknown*)value;
using ComPtr resourceTextureManager = default;
// Check that the input object implements ID2D1ResourceTextureManager
- int result = unknown.CopyTo(resourceTextureManager.GetAddressOf());
-
- if (result != S.S_OK)
- {
- return result;
- }
+ unknown.CopyTo(resourceTextureManager.GetAddressOf()).Assert();
using ComPtr resourceTextureManagerInternal = default;
// Then, also check that it implements ID2D1ResourceTextureManagerInternal
- result = unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf());
-
- if (result != S.S_OK)
- {
- return result;
- }
+ unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf()).Assert();
// 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;
- }
+ resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions).Assert();
}
ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index];