Adopt static abstracts in ComputeSharp.D2D1 (and WinUI)

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-18 19:21:17 +02:00
1 親 167041bee0
コミット 786efaf38f
24ファイル変更
+100 -151

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

+2 -5
ファイルの表示
@@ -1,5 +1,4 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
@@ -45,9 +44,7 @@ public readonly unsafe ref struct D2D1DrawInfoUpdateContext<T>
this.d2D1DrawInfoUpdateContext->GetConstantBuffer(pBuffer, constantBufferSize).Assert();
}
Unsafe.SkipInit(out T shader);
shader = shader.CreateFromConstantBuffer(buffer);
T shader = T.CreateFromConstantBuffer(buffer);
ArrayPool<byte>.Shared.Return(buffer);
@@ -62,6 +59,6 @@ public readonly unsafe ref struct D2D1DrawInfoUpdateContext<T>
{
D2D1DrawInfoUpdateContextConstantBufferLoader dataLoader = new(this.d2D1DrawInfoUpdateContext);
Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader);
T.LoadConstantBuffer(in shader, ref dataLoader);
}
}
+23 -51
ファイルの表示
@@ -20,9 +20,7 @@ public static class D2D1PixelShader
public static int GetConstantBufferSize<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.ConstantBufferSize;
return T.ConstantBufferSize;
}
/// <summary>
@@ -33,9 +31,7 @@ public static class D2D1PixelShader
public static int GetInputCount<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.InputCount;
return T.InputCount;
}
/// <summary>
@@ -46,9 +42,7 @@ public static class D2D1PixelShader
public static int GetResourceTextureCount<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.ResourceTextureCount;
return T.ResourceTextureCount;
}
/// <summary>
@@ -59,9 +53,7 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<D2D1PixelShaderInputType> GetInputTypes<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.InputTypes;
return T.InputTypes;
}
/// <summary>
@@ -72,9 +64,7 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<D2D1InputDescription> GetInputDescriptions<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.InputDescriptions;
return T.InputDescriptions;
}
/// <summary>
@@ -85,9 +75,7 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<D2D1ResourceTextureDescription> GetResourceTextureDescriptions<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.ResourceTextureDescriptions;
return T.ResourceTextureDescriptions;
}
/// <summary>
@@ -98,9 +86,7 @@ public static class D2D1PixelShader
public static D2D1PixelOptions GetPixelOptions<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.PixelOptions;
return T.PixelOptions;
}
/// <summary>
@@ -111,9 +97,7 @@ public static class D2D1PixelShader
public static D2D1BufferPrecision GetOutputBufferPrecision<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.BufferPrecision;
return T.BufferPrecision;
}
/// <summary>
@@ -124,9 +108,7 @@ public static class D2D1PixelShader
public static D2D1ChannelDepth GetOutputBufferChannelDepth<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.ChannelDepth;
return T.ChannelDepth;
}
/// <summary>
@@ -144,13 +126,11 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<byte> LoadBytecode<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
// This method is the one used by the built-in ID2D1Effect, so it has special optimization
// to help both performance and especially trimming. That is, we first check whether the
// HLSL code is already available, which should get completely inlined when using the
// generated code. This allows the entire fallback path below to be trimmed out.
if (shader.HlslBytecode is ReadOnlyMemory<byte> { Length: > 0 } hlslBytecode)
if (T.HlslBytecode is ReadOnlyMemory<byte> { Length: > 0 } hlslBytecode)
{
return hlslBytecode;
}
@@ -334,16 +314,12 @@ public static class D2D1PixelShader
out D2D1CompileOptions effectiveCompileOptions)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
string hlslSource = shader.HlslSource;
// Set the effective profile and option to the requested ones or the default values
effectiveShaderProfile = requestedShaderProfile ?? shader.ShaderProfile;
effectiveCompileOptions = requestedCompileOptions ?? shader.CompileOptions;
effectiveShaderProfile = requestedShaderProfile ?? T.ShaderProfile;
effectiveCompileOptions = requestedCompileOptions ?? T.CompileOptions;
// Compile the shader with the current settings
using ComPtr<ID3DBlob> dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions);
using ComPtr<ID3DBlob> dynamicBytecode = D3DCompiler.Compile(T.HlslSource, effectiveShaderProfile, effectiveCompileOptions);
byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer();
int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize();
@@ -367,15 +343,13 @@ public static class D2D1PixelShader
out D2D1CompileOptions effectiveCompileOptions)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
// Check whether there is precompiled bytecode we can use
if (shader.HlslBytecode is ReadOnlyMemory<byte> { Length: > 0 } hlslBytecode &&
(requestedShaderProfile is null || shader.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) &&
(requestedCompileOptions is null || shader.CompileOptions == requestedCompileOptions.GetValueOrDefault()))
if (T.HlslBytecode is ReadOnlyMemory<byte> { Length: > 0 } hlslBytecode &&
(requestedShaderProfile is null || T.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) &&
(requestedCompileOptions is null || T.CompileOptions == requestedCompileOptions.GetValueOrDefault()))
{
effectiveShaderProfile = shader.ShaderProfile;
effectiveCompileOptions = shader.CompileOptions;
effectiveShaderProfile = T.ShaderProfile;
effectiveCompileOptions = T.CompileOptions;
return hlslBytecode;
}
@@ -398,7 +372,7 @@ public static class D2D1PixelShader
{
D2D1ByteArrayConstantBufferLoader dataLoader = default;
Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader);
T.LoadConstantBuffer(in shader, ref dataLoader);
return dataLoader.GetResultingDispatchData();
}
@@ -437,7 +411,7 @@ public static class D2D1PixelShader
{
D2D1ByteBufferConstantBufferLoader dataLoader = new(buffer, span.Length);
Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader);
T.LoadConstantBuffer(in shader, ref dataLoader);
return dataLoader.TryGetWrittenBytes(out bytesWritten);
}
@@ -458,7 +432,7 @@ public static class D2D1PixelShader
D2D1DrawInfoConstantBufferLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo);
Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader);
T.LoadConstantBuffer(in shader, ref dataLoader);
}
/// <summary>
@@ -490,11 +464,9 @@ public static class D2D1PixelShader
public static bool TryCreateFromConstantBuffer<T>(ReadOnlySpan<byte> span, out T shader)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out shader);
if (span.Length >= shader.ConstantBufferSize)
if (span.Length >= T.ConstantBufferSize)
{
shader = shader.CreateFromConstantBuffer(span);
shader = T.CreateFromConstantBuffer(span);
return true;
}
+11 -22
ファイルの表示
@@ -1,6 +1,5 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Text;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.D2D1.Extensions;
@@ -26,9 +25,7 @@ public static unsafe class D2D1PixelShaderEffect
public static ref readonly Guid GetEffectId<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return ref shader.EffectId;
return ref T.EffectId;
}
/// <summary>
@@ -39,9 +36,7 @@ public static unsafe class D2D1PixelShaderEffect
public static string? GetEffectDisplayName<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.EffectDisplayName;
return T.EffectDisplayName;
}
/// <summary>
@@ -52,9 +47,7 @@ public static unsafe class D2D1PixelShaderEffect
public static string? GetEffectDescription<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.EffectDescription;
return T.EffectDescription;
}
/// <summary>
@@ -65,9 +58,7 @@ public static unsafe class D2D1PixelShaderEffect
public static string? GetEffectCategory<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.EffectCategory;
return T.EffectCategory;
}
/// <summary>
@@ -78,9 +69,7 @@ public static unsafe class D2D1PixelShaderEffect
public static string? GetEffectAuthor<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
return shader.EffectAuthor;
return T.EffectAuthor;
}
/// <summary>
@@ -194,7 +183,7 @@ public static unsafe class D2D1PixelShaderEffect
classId: pGuid,
propertyXml: (ushort*)pXml,
bindings: d2D1PropertyBinding,
bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + D2D1PixelShader.GetResourceTextureCount<T>()),
bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount),
effectFactory: PixelShaderEffect.Globals<T>.Instance.Factory).Assert();
}
@@ -249,8 +238,8 @@ public static unsafe class D2D1PixelShaderEffect
writer.Write(D2D1EffectRegistrationData.V1.BlobId);
// Effect id and number of inputs
writer.Write(GetEffectId<T>());
writer.Write(D2D1PixelShader.GetInputCount<T>());
writer.Write(T.EffectId);
writer.Write(T.InputCount);
// Retrieve the effect XML and include it into the registration blob
using (D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer<T>())
@@ -272,7 +261,7 @@ public static unsafe class D2D1PixelShaderEffect
}
// Bindings
writer.Write(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + D2D1PixelShader.GetResourceTextureCount<T>());
writer.Write(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount);
writer.Write("ConstantBuffer"u8);
writer.Write((byte)'\0');
writer.Write((nint)(delegate* unmanaged<IUnknown*, byte*, uint, uint*, HRESULT>)&PixelShaderEffect.GetConstantBufferImpl);
@@ -283,7 +272,7 @@ public static unsafe class D2D1PixelShaderEffect
writer.Write((nint)(delegate* unmanaged<IUnknown*, byte*, uint, HRESULT>)&PixelShaderEffect.SetTransformMapperImpl);
// Add all resource texture manager property bindings
for (int i = 0; i < D2D1PixelShader.GetResourceTextureCount<T>(); i++)
for (int i = 0; i < T.ResourceTextureCount; i++)
{
switch (i)
{
@@ -441,7 +430,7 @@ public static unsafe class D2D1PixelShaderEffect
D2D1EffectConstantBufferLoader dataLoader = new((ID2D1Effect*)d2D1Effect);
Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader);
T.LoadConstantBuffer(in shader, ref dataLoader);
}
/// <summary>
+1 -4
ファイルの表示
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.Win32;
@@ -27,8 +26,6 @@ public static class D2D1ReflectionServices
public static unsafe D2D1ShaderInfo GetShaderInfo<T>()
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
ReadOnlyMemory<byte> hlslBytecode = D2D1PixelShader.LoadBytecode<T>();
using ComPtr<ID3D11ShaderReflection> d3D11ShaderReflection = default;
@@ -52,7 +49,7 @@ public static class D2D1ReflectionServices
return new(
CompilerVersion: new string(d3D11ShaderDescription.Creator),
HlslSource: shader.HlslSource,
HlslSource: T.HlslSource,
HlslBytecode: hlslBytecode,
ConstantBufferCount: d3D11ShaderDescription.ConstantBuffers,
BoundResourceCount: d3D11ShaderDescription.BoundResources,
+10 -10
ファイルの表示
@@ -107,34 +107,34 @@ internal unsafe partial struct PixelShaderEffect
public static Globals<T> Instance { get; } = new();
/// <inheritdoc/>
public override ref readonly Guid EffectId => ref D2D1PixelShaderEffect.GetEffectId<T>();
public override ref readonly Guid EffectId => ref T.EffectId;
/// <inheritdoc/>
public override int ConstantBufferSize => D2D1PixelShader.GetConstantBufferSize<T>();
public override int ConstantBufferSize => T.ConstantBufferSize;
/// <inheritdoc/>
public override int InputCount => D2D1PixelShader.GetInputCount<T>();
public override int InputCount => T.InputCount;
/// <inheritdoc/>
public override int ResourceTextureCount => D2D1PixelShader.GetResourceTextureCount<T>();
public override int ResourceTextureCount => T.ResourceTextureCount;
/// <inheritdoc/>
public override ReadOnlyMemory<D2D1PixelShaderInputType> InputTypes => D2D1PixelShader.GetInputTypes<T>();
public override ReadOnlyMemory<D2D1PixelShaderInputType> InputTypes => T.InputTypes;
/// <inheritdoc/>
public override ReadOnlyMemory<D2D1InputDescription> InputDescriptions => D2D1PixelShader.GetInputDescriptions<T>();
public override ReadOnlyMemory<D2D1InputDescription> InputDescriptions => T.InputDescriptions;
/// <inheritdoc/>
public override ReadOnlyMemory<D2D1ResourceTextureDescription> ResourceTextureDescriptions => D2D1PixelShader.GetResourceTextureDescriptions<T>();
public override ReadOnlyMemory<D2D1ResourceTextureDescription> ResourceTextureDescriptions => T.ResourceTextureDescriptions;
/// <inheritdoc/>
public override D2D1PixelOptions PixelOptions => D2D1PixelShader.GetPixelOptions<T>();
public override D2D1PixelOptions PixelOptions => T.PixelOptions;
/// <inheritdoc/>
public override D2D1BufferPrecision BufferPrecision => D2D1PixelShader.GetOutputBufferPrecision<T>();
public override D2D1BufferPrecision BufferPrecision => T.BufferPrecision;
/// <inheritdoc/>
public override D2D1ChannelDepth ChannelDepth => D2D1PixelShader.GetOutputBufferChannelDepth<T>();
public override D2D1ChannelDepth ChannelDepth => T.ChannelDepth;
/// <inheritdoc/>
public override ReadOnlyMemory<byte> HlslBytecode
+6 -6
ファイルの表示
@@ -26,12 +26,12 @@ internal static unsafe class D2D1EffectXmlFactory
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
// Compute all necessary values first, outside of the lock
string? displayName = D2D1PixelShaderEffect.GetEffectDisplayName<T>();
string? description = D2D1PixelShaderEffect.GetEffectDescription<T>();
string? category = D2D1PixelShaderEffect.GetEffectCategory<T>();
string? author = D2D1PixelShaderEffect.GetEffectAuthor<T>();
int inputCount = D2D1PixelShader.GetInputCount<T>();
int resourceTextureCount = D2D1PixelShader.GetResourceTextureCount<T>();
string? displayName = T.EffectDisplayName;
string? description = T.EffectDescription;
string? category = T.EffectCategory;
string? author = T.EffectAuthor;
int inputCount = T.InputCount;
int resourceTextureCount = T.ResourceTextureCount;
StringBuilder builder = XmlBuilder;