diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
index 58934467..593091a6 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
@@ -44,6 +44,11 @@ internal unsafe partial struct PixelShaderEffect
///
private static Guid shaderId;
+ ///
+ /// The size of the constant buffer for the shader.
+ ///
+ private static int constantBufferSize;
+
///
/// The number of inputs for the shader.
///
@@ -129,6 +134,7 @@ internal unsafe partial struct PixelShaderEffect
{
// Load all shader properties
Guid shaderId = typeof(T).GUID;
+ int constantBufferSize = D2D1PixelShader.GetConstantBufferSize();
D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision();
D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth();
D2D1PixelOptions pixelOptions = D2D1PixelShader.GetPixelOptions();
@@ -165,6 +171,7 @@ internal unsafe partial struct PixelShaderEffect
// Set the shared state and mark the type as initialized
For.shaderId = shaderId;
+ For.constantBufferSize = constantBufferSize;
For.inputCount = inputCount;
For.inputTypes = inputTypes;
For.inputDescriptionCount = inputDescriptionCount;
@@ -251,6 +258,7 @@ internal unsafe partial struct PixelShaderEffect
return PixelShaderEffect.Factory(
shaderId,
+ constantBufferSize,
inputCount,
inputTypes,
inputDescriptionCount,
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
index 4b9995ff..8ea1cfc6 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs
@@ -200,6 +200,7 @@ partial struct PixelShaderEffect
opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle();
}
+ // TODO: shouldn't there be a check for the buffer being null?
ReadOnlySpan buffer = new(@this->constantBuffer, @this->constantBufferSize);
Rectangle output;
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
index 132b0e5d..3314fe22 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
@@ -69,20 +69,32 @@ unsafe partial struct PixelShaderEffect
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
private static int GetConstantBufferImpl(IUnknown* effect, byte* data, uint dataSize, uint* actualSize)
{
+ if (data is null || actualSize is null)
+ {
+ return E.E_POINTER;
+ }
+
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
+ if (dataSize < @this->constantBufferSize)
+ {
+ return E.E_NOT_SUFFICIENT_BUFFER;
+ }
+
+ if (@this->constantBuffer is null)
+ {
+ return E.E_NOT_VALID_STATE;
+ }
+
if (@this->constantBufferSize == 0)
{
*actualSize = 0;
}
else
{
- // TODO: handle case with buffer not large enough
- int bytesToCopy = Math.Min((int)dataSize, @this->constantBufferSize);
+ Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, @this->constantBufferSize);
- Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, bytesToCopy);
-
- *actualSize = (uint)bytesToCopy;
+ *actualSize = (uint)@this->constantBufferSize;
}
return S.S_OK;
@@ -92,8 +104,18 @@ unsafe partial struct PixelShaderEffect
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
private static int SetConstantBufferImpl(IUnknown* effect, byte* data, uint dataSize)
{
+ if (data is null)
+ {
+ return E.E_POINTER;
+ }
+
PixelShaderEffect* @this = (PixelShaderEffect*)effect;
+ if (dataSize != (uint)@this->constantBufferSize)
+ {
+ return E.E_INVALIDARG;
+ }
+
if (@this->constantBuffer is not null)
{
NativeMemory.Free(@this->constantBuffer);
@@ -104,7 +126,6 @@ unsafe partial struct PixelShaderEffect
Buffer.MemoryCopy(data, buffer, dataSize, dataSize);
@this->constantBuffer = (byte*)buffer;
- @this->constantBufferSize = (int)dataSize;
return S.S_OK;
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
index b31caa95..249f0544 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
@@ -114,21 +114,21 @@ internal unsafe partial struct PixelShaderEffect
///
private volatile int referenceCount;
+ ///
+ /// The for the shader.
+ ///
+ private Guid shaderId;
+
///
/// The constant buffer data, if set.
///
private byte* constantBuffer;
///
- /// The size of , if set.
+ /// The size of .
///
private int constantBufferSize;
- ///
- /// The for the shader.
- ///
- private Guid shaderId;
-
///
/// The number of inputs for the shader.
///
@@ -219,6 +219,7 @@ internal unsafe partial struct PixelShaderEffect
/// The factory method for .
///
/// The for the shader.
+ /// The size of the constant buffer for the shader.
/// The number of inputs for the shader.
/// The buffer with the types of inputs for the shader.
/// The number of available input descriptions.
@@ -235,6 +236,7 @@ internal unsafe partial struct PixelShaderEffect
/// This always returns 0.
private static int Factory(
Guid shaderId,
+ int constantBufferSize,
int inputCount,
D2D1PixelShaderInputType* inputTypes,
int inputDescriptionCount,
@@ -268,6 +270,8 @@ internal unsafe partial struct PixelShaderEffect
@this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform;
@this->referenceCount = 1;
@this->shaderId = shaderId;
+ @this->constantBuffer = null;
+ @this->constantBufferSize = constantBufferSize;
@this->inputCount = inputCount;
@this->inputTypes = inputTypes;
@this->inputDescriptionCount = inputDescriptionCount;
diff --git a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs
index 6190c538..e8227e77 100644
--- a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs
+++ b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs
@@ -10,6 +10,9 @@ namespace TerraFX.Interop.Windows
[NativeTypeName("#define E_NOT_VALID_STATE HRESULT_FROM_WIN32(ERROR_INVALID_STATE)")]
public const int E_NOT_VALID_STATE = -2147019873;
+ [NativeTypeName("#define E_NOT_SUFFICIENT_BUFFER HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)")]
+ public const int E_NOT_SUFFICIENT_BUFFER = -2147024774;
+
[NativeTypeName("#define E_NOTIMPL _HRESULT_TYPEDEF_(0x80004001L)")]
public const int E_NOTIMPL = unchecked((int)(0x80004001));