Minor code tweaks

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:32 +02:00
1 親 f2b49cd6b8
コミット 7651ad0ef3
3ファイル変更
+46 -6

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

+22 -4
ファイルの表示
@@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
#endif
using System.Text;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Interop.Effects;
using ComputeSharp.D2D1.Shaders.Dispatching;
using ComputeSharp.D2D1.Shaders.Interop.Buffers;
@@ -26,6 +27,7 @@ public static unsafe class D2D1PixelShaderEffect
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
/// <param name="d2D1Factory1">A pointer to the <c>ID2D1Factory1</c> instance to use.</param>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
public static unsafe void RegisterForD2D1Factory1<T>(void* d2D1Factory1, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
@@ -40,12 +42,13 @@ public static unsafe class D2D1PixelShaderEffect
/// <typeparam name="TMapper">The type of <see cref="ID2D1TransformMapper{T}"/> implementation to register.</typeparam>
/// <param name="d2D1Factory1">A pointer to the <c>ID2D1Factory1</c> instance to use.</param>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
public static unsafe void RegisterForD2D1Factory1<T, TMapper>(void* d2D1Factory1, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper<T>, new()
{
RegisterForD2D1Factory1<T>(d2D1Factory1, static () => new TMapper(), out effectId);
RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId);
}
/// <summary>
@@ -55,6 +58,7 @@ public static unsafe class D2D1PixelShaderEffect
/// <param name="d2D1Factory1">A pointer to the <c>ID2D1Factory1</c> instance to use.</param>
/// <param name="mapperFactory">An optional factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for the transform.</param>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
public static unsafe void RegisterForD2D1Factory1<T>(void* d2D1Factory1, Func<ID2D1TransformMapper<T>>? mapperFactory, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
@@ -131,6 +135,8 @@ public static unsafe class D2D1PixelShaderEffect
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(Func{ID2D1TransformMapper{T}}?, out Guid)"/>.</remarks>
public static unsafe ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
@@ -144,6 +150,8 @@ public static unsafe class D2D1PixelShaderEffect
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
/// <typeparam name="TMapper">The type of <see cref="ID2D1TransformMapper{T}"/> implementation to register.</typeparam>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(Func{ID2D1TransformMapper{T}}?, out Guid)"/>.</remarks>
public static unsafe ReadOnlyMemory<byte> GetRegistrationBlob<T, TMapper>(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
@@ -158,6 +166,8 @@ public static unsafe class D2D1PixelShaderEffect
/// <typeparam name="T">The type of D2D1 pixel shader to get the registration blob for.</typeparam>
/// <param name="mapperFactory">An optional factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for the transform.</param>
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
/// <remarks>
/// <para>
/// This blob can be useful to marshal registration data across an app domain boundary without having assembly references cross this boundary as well.
@@ -267,14 +277,22 @@ public static unsafe class D2D1PixelShaderEffect
/// <typeparam name="T">The type of D2D1 pixel shader to create an effect for.</typeparam>
/// <param name="d2D1DeviceContext">A pointer to the <c>ID2D1DeviceContext</c> instance to use.</param>
/// <param name="d2D1Effect">A pointer to the resulting <c>ID2D1Effect*</c> pointer to produce.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if this method is called before initializing a shader effect for the shader type <typeparamref name="T"/>.
/// To do so, make sure to call either <see cref="RegisterForD2D1Factory1{T}(void*, out Guid)"/> (or an overload), or
/// to use <see cref="GetRegistrationBlob{T}(out Guid)"/> (or an overload), and use that blob to register an effect.
/// </exception>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1devicecontext-createeffect"/>.</remarks>
public static unsafe void CreateFromD2D1DeviceContext<T>(void* d2D1DeviceContext, void** d2D1Effect)
where T : unmanaged, ID2D1PixelShader
{
Guid shaderId = typeof(T).GUID;
if (!PixelShaderEffect.For<T>.TryGetId(out Guid id))
{
ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet.");
}
((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect(
effectId: &shaderId,
effectId: &id,
effect: (ID2D1Effect**)d2D1Effect).Assert();
}
@@ -285,7 +303,7 @@ public static unsafe class D2D1PixelShaderEffect
/// <param name="shader">The input D2D1 pixel shader to set the contant buffer for.</param>
/// <param name="d2D1Effect">A pointer to the <c>ID2D1Effect</c> instance to use.</param>
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1properties-setvalue(uint32_d2d1_property_type_constbyte_uint32)"/>.</remarks>
public static unsafe void SetConstantBuffer<T>(in T shader, void* d2D1Effect)
public static unsafe void SetConstantBufferForD2D1Effect<T>(in T shader, void* d2D1Effect)
where T : unmanaged, ID2D1PixelShader
{
D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect);
+22
ファイルの表示
@@ -131,6 +131,28 @@ internal unsafe partial struct PixelShaderEffect
/// </summary>
public static int NumberOfInputs => numberOfInputs;
/// <summary>
/// Tries to get the effect id, if it has been initialized.
/// </summary>
/// <param name="id">The resulting effect id.</param>
/// <returns>Whether or not the effect had been initialized.</returns>
public static bool TryGetId(out Guid id)
{
lock (typeof(For<T>))
{
if (isInitialized)
{
id = Id;
return true;
}
}
id = default;
return false;
}
/// <inheritdoc cref="FactoryDelegate"/>
private static int CreateEffect(IUnknown** effectImpl)
{
+2 -2
ファイルの表示
@@ -35,7 +35,7 @@ internal static class D2D1TestRunner
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<T>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get());
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
using ComPtr<IWICBitmap> wicBitmap = WICHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height);
using ComPtr<ID2D1Bitmap> d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), wicBitmap.Get(), d2D1Effect.Get());
@@ -76,7 +76,7 @@ internal static class D2D1TestRunner
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<T>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get());
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);