Merge pull request #581 from Sergio0694/dev/additional-numeric-properties

Add new numeric properties to ID2D1Shader
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-09-30 16:15:16 +02:00
コミット 2dcde8748e
7ファイル変更
+121 -92

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

@@ -1,27 +0,0 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>InputCount</c> property.
/// </summary>
private static partial class InputCount
{
/// <summary>
/// Writes the <c>InputCount</c> property.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => {info.InputTypes.Length};");
}
}
}
@@ -0,0 +1,51 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the available numeric properties.
/// </summary>
private static partial class NumericProperties
{
/// <summary>
/// Writes the <c>ConstantBufferSize</c> property.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteConstantBufferSizeSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ConstantBufferSize => {info.ConstantBufferSizeInBytes};");
}
/// <summary>
/// Writes the <c>InputCount</c> property.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteInputCountSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => {info.InputTypes.Length};");
}
/// <summary>
/// Writes the <c>ResourceTextureCount</c> property.
/// </summary>
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteResourceTextureCountSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(typeof(ID2D1ShaderGenerator));
writer.WriteLine($"readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ResourceTextureCount => {info.ResourceTextureDescriptions.Length};");
}
}
}
+3 -1
ファイルの表示
@@ -201,7 +201,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
declaredMembers.Add(EffectMetadata.WriteEffectDescriptionSyntax);
declaredMembers.Add(EffectMetadata.WriteEffectCategorySyntax);
declaredMembers.Add(EffectMetadata.WriteEffectAuthorSyntax);
declaredMembers.Add(InputCount.WriteSyntax);
declaredMembers.Add(NumericProperties.WriteConstantBufferSizeSyntax);
declaredMembers.Add(NumericProperties.WriteInputCountSyntax);
declaredMembers.Add(NumericProperties.WriteResourceTextureCountSyntax);
declaredMembers.Add(InputTypes.WriteSyntax);
declaredMembers.Add(InputDescriptions.WriteSyntax);
declaredMembers.Add(ResourceTextureDescriptions.WriteSyntax);
+14
ファイルの表示
@@ -67,6 +67,13 @@ public interface ID2D1Shader
[Obsolete("This method is not intended to be used directly by user code")]
D2D1PixelOptions PixelOptions { get; }
/// <summary>
/// Gets the size in bytes of the constant buffer for the current shader.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
int ConstantBufferSize { get; }
/// <summary>
/// Gets the number of inputs for the current shader.
/// </summary>
@@ -74,6 +81,13 @@ public interface ID2D1Shader
[Obsolete("This method is not intended to be used directly by user code")]
int InputCount { get; }
/// <summary>
/// Gets the number of resource textures for the current shader.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
int ResourceTextureCount { get; }
/// <summary>
/// Gets the input types for the current shader.
/// </summary>
+16 -1
ファイルの表示
@@ -295,6 +295,19 @@ public static class D2D1PixelShader
return shader.InputCount;
}
/// <summary>
/// Gets the number of resource textures for a D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to get the resource texture count for.</typeparam>
/// <returns>The number of resource texture for the D2D1 pixel shader of type <typeparamref name="T"/>.</returns>
public static int GetResourceTextureCount<T>()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return shader.ResourceTextureCount;
}
/// <summary>
/// Gets the input types for a D2D1 pixel shader.
/// </summary>
@@ -387,7 +400,9 @@ public static class D2D1PixelShader
public static int GetConstantBufferSize<T>()
where T : unmanaged, ID2D1PixelShader
{
return D2D1BufferSizeDispatchDataLoader.For<T>.ConstantBufferSize;
Unsafe.SkipInit(out T shader);
return shader.ConstantBufferSize;
}
/// <summary>
-63
ファイルの表示
@@ -1,63 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.__Internals;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A data loader for D2D1 pixel shaders dispatched via <see cref="ID2D1DrawInfo"/>, to just retrieve the constant buffer size.
/// </summary>
internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoader
{
/// <summary>
/// The size of the constant buffer.
/// </summary>
private int size;
/// <summary>
/// Gets the size of the constant buffer.
/// </summary>
/// <returns>The size of the constant buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetConstantBufferSize()
{
return this.size;
}
/// <inheritdoc/>
void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan<byte> data)
{
this.size = data.Length;
}
/// <summary>
/// A helper class caching computed constant buffer sizes for generic shaders.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
public static class For<T>
where T : unmanaged, ID2D1PixelShader
{
/// <summary>
/// Gets the constant buffer size for a shader of type <typeparamref name="T"/>.
/// </summary>
public static int ConstantBufferSize { get; } = GetConstantBufferSize();
/// <summary>
/// Calculates the constant buffer size for a shader of type <typeparamref name="T"/>.
/// </summary>
/// <returns>The constant buffer size for a shader of type <typeparamref name="T"/>.</returns>
private static int GetConstantBufferSize()
{
D2D1BufferSizeDispatchDataLoader dataLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadDispatchData(ref dataLoader);
return dataLoader.GetConstantBufferSize();
}
}
}
+37
ファイルの表示
@@ -235,6 +235,43 @@ public partial class D2D1PixelShaderTests
Assert.AreEqual(span[3].Dimensions, 2);
}
[TestMethod]
public unsafe void GetResourceTextureCount_IsCorrect()
{
Assert.AreEqual(0, D2D1PixelShader.GetResourceTextureCount<ShaderWithoutResourceTextures>());
Assert.AreEqual(1, D2D1PixelShader.GetResourceTextureCount<ShaderWithJustOneResourceTextures>());
Assert.AreEqual(4, D2D1PixelShader.GetResourceTextureCount<ShaderWithResourceTextures>());
}
[D2DInputCount(4)]
[D2DInputSimple(0)]
[D2DInputSimple(2)]
[D2DInputComplex(1)]
[D2DInputComplex(3)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[AutoConstructor]
partial struct ShaderWithoutResourceTextures : ID2D1PixelShader
{
public Float4 Execute()
{
return 0;
}
}
[D2DInputCount(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[AutoConstructor]
partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader
{
[D2DResourceTextureIndex(0)]
D2D1ResourceTexture1D<float> myTexture;
public Float4 Execute()
{
return this.myTexture.Sample(0);
}
}
[D2DInputCount(4)]
[D2DInputSimple(0)]
[D2DInputSimple(2)]