Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace [AutoConstructor] with primary constructors in samples #671

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ public async Task Compute_Multiple_WithContext_Async()
context.For(64, new TestShader(this.buffer!));
}

[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct TestShader : IComputeShader
internal readonly partial struct TestShader(ReadWriteBuffer<float> buffer) : IComputeShader
{
private readonly ReadWriteBuffer<float> buffer;
private readonly ReadWriteBuffer<float> buffer = buffer;

/// <inheritdoc/>
public void Execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,133 +307,123 @@ protected override void OnFrameApply(ImageFrame<ImageSharpRgba32> source)
/// <summary>
/// Kernel for the vertical convolution pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct VerticalConvolutionProcessor : IComputeShader
internal readonly partial struct VerticalConvolutionProcessor(
IReadWriteNormalizedTexture2D<float4> source,
ReadWriteTexture2D<float4> reals,
ReadWriteTexture2D<float4> imaginaries,
ReadOnlyBuffer<Complex64> kernel) : IComputeShader
{
private readonly IReadWriteNormalizedTexture2D<float4> source;
private readonly ReadWriteTexture2D<float4> reals;
private readonly ReadWriteTexture2D<float4> imaginaries;
private readonly ReadOnlyBuffer<Complex64> kernel;

/// <inheritdoc/>
public void Execute()
{
float4 real = float4.Zero;
float4 imaginary = float4.Zero;
int maxY = this.source.Height;
int maxX = this.source.Width;
int kernelLength = this.kernel.Length;
int maxY = source.Height;
int maxX = source.Width;
int kernelLength = kernel.Length;
int radiusY = kernelLength >> 1;

for (int i = 0; i < kernelLength; i++)
{
int offsetY = Hlsl.Clamp(ThreadIds.Y + i - radiusY, 0, maxY);
int offsetX = Hlsl.Clamp(ThreadIds.X, 0, maxX);
float4 color = this.source[offsetX, offsetY];
Complex64 factors = this.kernel[i];
float4 color = source[offsetX, offsetY];
Complex64 factors = kernel[i];

real += factors.Real * color;
imaginary += factors.Imaginary * color;
}

this.reals[ThreadIds.XY] = real;
this.imaginaries[ThreadIds.XY] = imaginary;
reals[ThreadIds.XY] = real;
imaginaries[ThreadIds.XY] = imaginary;
}
}

/// <summary>
/// Kernel for the horizontal convolution pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader
internal readonly partial struct HorizontalConvolutionAndAccumulatePartialsProcessor(
float z,
float w,
ReadWriteTexture2D<float4> reals,
ReadWriteTexture2D<float4> imaginaries,
ReadWriteTexture2D<float4> target,
ReadOnlyBuffer<Complex64> kernel) : IComputeShader
{
private readonly float z;
private readonly float w;

private readonly ReadWriteTexture2D<float4> reals;
private readonly ReadWriteTexture2D<float4> imaginaries;
private readonly ReadWriteTexture2D<float4> target;
private readonly ReadOnlyBuffer<Complex64> kernel;

/// <inheritdoc/>
public void Execute()
{
int maxY = this.target.Height;
int maxX = this.target.Width;
int kernelLength = this.kernel.Length;
int maxY = target.Height;
int maxX = target.Width;
int kernelLength = kernel.Length;
int radiusX = kernelLength >> 1;
int offsetY = Hlsl.Clamp(ThreadIds.Y, 0, maxY);
ComplexVector4 result = default;

for (int i = 0; i < kernelLength; i++)
{
int offsetX = Hlsl.Clamp(ThreadIds.X + i - radiusX, 0, maxX);
float4 sourceReal = this.reals[offsetX, offsetY];
float4 sourceImaginary = this.imaginaries[offsetX, offsetY];
Complex64 factors = this.kernel[i];
float4 sourceReal = reals[offsetX, offsetY];
float4 sourceImaginary = imaginaries[offsetX, offsetY];
Complex64 factors = kernel[i];

result.Real += (Vector4)((factors.Real * sourceReal) - (factors.Imaginary * sourceImaginary));
result.Imaginary += (Vector4)((factors.Real * sourceImaginary) + (factors.Imaginary * sourceReal));
}

this.target[ThreadIds.XY] += (float4)result.WeightedSum(this.z, this.w);
target[ThreadIds.XY] += (float4)result.WeightedSum(z, w);
}
}

/// <summary>
/// Kernel for the gamma highlight pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct GammaHighlightProcessor : IComputeShader
internal readonly partial struct GammaHighlightProcessor(IReadWriteNormalizedTexture2D<float4> source) : IComputeShader
{
private readonly IReadWriteNormalizedTexture2D<float4> source;

/// <inheritdoc/>
public void Execute()
{
int width = this.source.Width;
int width = source.Width;

for (int i = 0; i < width; i++)
{
float4 v = this.source[i, ThreadIds.X];
float4 v = source[i, ThreadIds.X];

v.XYZ = v.XYZ * v.XYZ * v.XYZ;

this.source[i, ThreadIds.X] = v;
source[i, ThreadIds.X] = v;
}
}
}

/// <summary>
/// Kernel for the inverse gamma highlight pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct InverseGammaHighlightProcessor : IComputeShader
internal readonly partial struct InverseGammaHighlightProcessor(
ReadWriteTexture2D<float4> source,
IReadWriteNormalizedTexture2D<float4> target) : IComputeShader
{
private readonly ReadWriteTexture2D<float4> source;
private readonly IReadWriteNormalizedTexture2D<float4> target;

/// <inheritdoc/>
public void Execute()
{
int width = this.source.Width;
int width = source.Width;

for (int i = 0; i < width; i++)
{
float4 v = this.source[i, ThreadIds.X];
float4 v = source[i, ThreadIds.X];

v = Hlsl.Clamp(v, 0, float.MaxValue);
v.XYZ = Hlsl.Pow(v.XYZ, 1 / 3f);

this.target[i, ThreadIds.X] = v;
target[i, ThreadIds.X] = v;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,68 +117,64 @@ protected override void OnFrameApply(ImageFrame<ImageSharpRgba32> source)
/// <summary>
/// Kernel for the vertical convolution pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct VerticalConvolutionProcessor : IComputeShader
internal readonly partial struct VerticalConvolutionProcessor(
IReadWriteNormalizedTexture2D<float4> source,
IReadWriteNormalizedTexture2D<float4> target,
ReadOnlyBuffer<float> kernel) : IComputeShader
{
private readonly IReadWriteNormalizedTexture2D<float4> source;
private readonly IReadWriteNormalizedTexture2D<float4> target;
private readonly ReadOnlyBuffer<float> kernel;

/// <inheritdoc/>
public void Execute()
{
float4 result = float4.Zero;
int maxY = this.source.Height - 1;
int maxX = this.source.Width - 1;
int kernelLength = this.kernel.Length;
int maxY = source.Height - 1;
int maxX = source.Width - 1;
int kernelLength = kernel.Length;
int radiusY = kernelLength >> 1;

for (int i = 0; i < kernelLength; i++)
{
int offsetY = Hlsl.Clamp(ThreadIds.Y + i - radiusY, 0, maxY);
int offsetX = Hlsl.Clamp(ThreadIds.X, 0, maxX);
float4 color = this.source[offsetX, offsetY];
float4 color = source[offsetX, offsetY];

result += this.kernel[i] * color;
result += kernel[i] * color;
}

this.target[ThreadIds.XY] = result;
target[ThreadIds.XY] = result;
}
}

/// <summary>
/// Kernel for the horizontal convolution pass.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader
internal readonly partial struct HorizontalConvolutionProcessor(
IReadWriteNormalizedTexture2D<float4> source,
IReadWriteNormalizedTexture2D<float4> target,
ReadOnlyBuffer<float> kernel) : IComputeShader
{
private readonly IReadWriteNormalizedTexture2D<float4> source;
private readonly IReadWriteNormalizedTexture2D<float4> target;
private readonly ReadOnlyBuffer<float> kernel;

/// <inheritdoc/>
public void Execute()
{
float4 result = float4.Zero;
int maxY = this.source.Height - 1;
int maxX = this.source.Width - 1;
int kernelLength = this.kernel.Length;
int maxY = source.Height - 1;
int maxX = source.Width - 1;
int kernelLength = kernel.Length;
int radiusX = kernelLength >> 1;
int offsetY = Hlsl.Clamp(ThreadIds.Y, 0, maxY);

for (int i = 0; i < kernelLength; i++)
{
int offsetX = Hlsl.Clamp(ThreadIds.X + i - radiusX, 0, maxX);
float4 color = this.source[offsetX, offsetY];
float4 color = source[offsetX, offsetY];

result += this.kernel[i] * color;
result += kernel[i] * color;
}

this.target[ThreadIds.XY] = result;
target[ThreadIds.XY] = result;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions samples/ComputeSharp.Sample.FSharp.Shaders/MultiplyByTwo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ namespace ComputeSharp.Sample.FSharp.Shaders;
/// <summary>
/// A sample shader thaat just multiplies items in a buffer by two.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
public readonly partial struct MultiplyByTwo : IComputeShader
public readonly partial struct MultiplyByTwo(ReadWriteBuffer<float> buffer) : IComputeShader
{
private readonly ReadWriteBuffer<float> buffer;

/// <inheritdoc/>
public void Execute()
{
this.buffer[ThreadIds.X] *= 2;
buffer[ThreadIds.X] *= 2;
}
}
7 changes: 2 additions & 5 deletions samples/ComputeSharp.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
/// <summary>
/// The sample kernel that multiples all items by two.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MultiplyByTwo : IComputeShader
internal readonly partial struct MultiplyByTwo(ReadWriteBuffer<float> buffer) : IComputeShader
{
private readonly ReadWriteBuffer<float> buffer;

/// <inheritdoc/>
public void Execute()
{
this.buffer[ThreadIds.X] *= 2;
buffer[ThreadIds.X] *= 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1;
/// <para>Created by Benoit Marini.</para>
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
/// </summary>
/// <param name="time">The current time since the start of the application.</param>
/// <param name="dispatchSize">The dispatch size for the current output.</param>
[D2DInputCount(0)]
[D2DRequiresScenePosition]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DGeneratedPixelShaderDescriptor]
[AutoConstructor]
internal readonly partial struct ColorfulInfinity : ID2D1PixelShader
internal readonly partial struct ColorfulInfinity(float time, int2 dispatchSize) : ID2D1PixelShader
{
/// <summary>
/// The current time since the start of the application.
/// </summary>
private readonly float time;

/// <summary>
/// The dispatch size for the current output.
/// </summary>
private readonly int2 dispatchSize;

/// <summary>
/// The total number of layers for the final animation.
/// </summary>
Expand All @@ -40,7 +31,7 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1;
/// </summary>
private float4 Tex(float3 p)
{
float t = this.time + 78.0f;
float t = time + 78.0f;
float4 o = new(p.X, p.Y, p.Z, 3.0f * Hlsl.Sin(t * 0.1f));
float4 dec =
new float4(1.0f, 0.9f, 0.1f, 0.15f) +
Expand All @@ -58,9 +49,9 @@ private float4 Tex(float3 p)
public float4 Execute()
{
int2 xy = (int2)D2D.GetScenePosition().XY;
float2 uv = (xy - ((float2)this.dispatchSize * 0.5f)) / this.dispatchSize.Y;
float2 uv = (xy - ((float2)dispatchSize * 0.5f)) / dispatchSize.Y;
float3 col = 0;
float t = this.time * 0.3f;
float t = time * 0.3f;

for (float i = 0.0f; i <= 1.0f; i += 1.0f / NumberOfLayers)
{
Expand Down
Loading
Loading