Merge pull request #466 from Sergio0694/dev/fix-device-change

Fix PixelShaderEffect<T>.ConstantBuffer accessors crashing for empty types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-01-11 16:36:41 -08:00
コミット 999d3e2762
4ファイル変更
+56 -11

検索条件に一致するファイルはありません

@@ -57,4 +57,24 @@ internal static unsafe class ID2D1DeviceContextExtensions
return dpiX;
}
/// <summary>
/// Gets an <see cref="ID2D1Factory1"/> object from a given <see cref="ID2D1DeviceContext"/>.
/// </summary>
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
/// <param name="d2D1Factory1">The resulting <see cref="ID2D1Factory1"/> object.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
/// <remarks>
/// The <see cref="Interop.D2D1PixelShaderEffect"/> APIs specifically need an <see cref="ID2D1Factory1"/> (as <see cref="ID2D1Factory1.RegisterEffectFromString"/> is used).
/// </remarks>
public static HRESULT GetFactory1(this ref ID2D1DeviceContext deviceContext, ID2D1Factory1** d2D1Factory1)
{
using ComPtr<ID2D1Factory> d2D1Factory = default;
// Get the underlying ID2D1Factory from the input context, which can be used to register effects
deviceContext.GetFactory(d2D1Factory.GetAddressOf());
// QueryInterface for the ID2D1Factory1 object (which should always be present)
return d2D1Factory.CopyTo(d2D1Factory1);
}
}
+30
ファイルの表示
@@ -29,6 +29,15 @@ internal static unsafe class ID2D1EffectExtensions
where T : unmanaged, ID2D1PixelShader
{
int constantBufferSize = D2D1PixelShader.GetConstantBufferSize<T>();
// Special cases the constant buffer type being empty. If that's the case, there is no
// need to try to retrieve the constant buffer from the underlying effect anyway, and
// most importantly, the ID2D1EffectImpl from ComputeSharp.D2D1 will return E_INVALIDARG.
if (constantBufferSize == 0)
{
return default;
}
byte[] buffer = ArrayPool<byte>.Shared.Rent(constantBufferSize);
fixed (byte* p = buffer)
@@ -48,6 +57,27 @@ internal static unsafe class ID2D1EffectExtensions
return shader;
}
/// <summary>
/// Sets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <typeparam name="T">The type of shader being used.</typeparam>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="value">The constant buffer value to set.</param>
public static void SetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect, in T value)
where T : unmanaged, ID2D1PixelShader
{
// Same as above, simply do nothing for empty types. This case is still handled to
// avoid crashing when someone's just setting an empty constant buffer, which is
// unnecessary but still valid (eg. a stateless shader). This could be the case if
// someone was eg. binding or using some other automated system to set constant buffer.
if (D2D1PixelShader.GetConstantBufferSize<T>() == 0)
{
return;
}
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect((ID2D1Effect*)Unsafe.AsPointer(ref d2D1Effect), in value);
}
/// <summary>
/// Gets the <see cref="D2D1TransformMapper{T}"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
+5 -10
ファイルの表示
@@ -224,16 +224,6 @@ unsafe partial class PixelShaderEffect<T>
/// <returns>Whether the effect was realized correctly.</returns>
private bool Realize(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
{
using ComPtr<ID2D1Factory> d2D1Factory = default;
// Get the underlying ID2D1Factory from the input context, so we can register and create the effect
deviceContext->GetFactory(d2D1Factory.GetAddressOf());
using ComPtr<ID2D1Factory1> d2D1Factory1 = default;
// D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used)
d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert();
fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect)
{
Guid effectId = PixelShaderEffect.For<T>.Instance.Id;
@@ -248,6 +238,11 @@ unsafe partial class PixelShaderEffect<T>
if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED ||
hresult == E.E_NOTFOUND)
{
using ComPtr<ID2D1Factory1> d2D1Factory1 = default;
// Get the ID2D1Factory1 object to register the effect (required by D2D1PixelShaderEffect)
deviceContext->GetFactory1(d2D1Factory1.GetAddressOf()).Assert();
// Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before)
D2D1PixelShaderEffect.RegisterForD2D1Factory1<T>(d2D1Factory1.Get(), out _);
+1 -1
ファイルの表示
@@ -40,7 +40,7 @@ unsafe partial class PixelShaderEffect<T>
// If the effect is realized, set the property on the underlying ID2D1Effect object
if (this.d2D1Effect.Get() is not null)
{
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.d2D1Effect.Get(), in value);
this.d2D1Effect.Get()->SetConstantBuffer(in value);
}
else
{