using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
#pragma warning disable CA1063, IDE0052
namespace ComputeSharp.Benchmark.Blas;
///
/// A that benchmarks the shader dispatch times.
///
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[MemoryDiagnoser]
public partial class DispatchingBenchmark : IDisposable
{
///
/// The test buffer.
///
private ReadWriteBuffer? buffer;
///
/// Initial setup for a benchmarking session.
///
[GlobalSetup]
public void Setup()
{
this.buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer(128);
}
///
[GlobalCleanup]
public void Dispose()
{
this.buffer!.Dispose();
}
///
/// Invokes a single shader, manually.
///
[Benchmark(Baseline = true)]
[BenchmarkCategory("SINGLE")]
public void Compute_Single()
{
GraphicsDevice.GetDefault().For(64, new TestShader(this.buffer!));
}
///
/// Invokes a single shader through a compute context.
///
[Benchmark]
[BenchmarkCategory("SINGLE")]
public void Compute_Single_WithContext()
{
using ComputeContext context = GraphicsDevice.GetDefault().CreateComputeContext();
context.For(64, new TestShader(this.buffer!));
}
///
/// Invokes a single shader through a compute context, asynchronously.
///
[Benchmark]
[BenchmarkCategory("SINGLE")]
public async Task Compute_Single_WithContext_Async()
{
await using ComputeContext context = GraphicsDevice.GetDefault().CreateComputeContext();
context.For(64, new TestShader(this.buffer!));
}
///
/// Invokes two empty compute shaders one by one.
///
[Benchmark(Baseline = true)]
[BenchmarkCategory("MULTIPLE")]
public void Compute_Multiple()
{
GraphicsDevice.GetDefault().For(64, new TestShader(this.buffer!));
GraphicsDevice.GetDefault().For(64, new TestShader(this.buffer!));
}
///
/// Invokes two empty compute shaders in a batch.
///
[Benchmark]
[BenchmarkCategory("MULTIPLE")]
public void Compute_Multiple_WithContext()
{
using ComputeContext context = GraphicsDevice.GetDefault().CreateComputeContext();
context.For(64, new TestShader(this.buffer!));
context.Barrier(this.buffer!);
context.For(64, new TestShader(this.buffer!));
}
///
/// Invokes two empty compute shaders in a batch, asynchronously.
///
[Benchmark]
[BenchmarkCategory("MULTIPLE")]
public async Task Compute_Multiple_WithContext_Async()
{
await using ComputeContext context = GraphicsDevice.GetDefault().CreateComputeContext();
context.For(64, new TestShader(this.buffer!));
context.Barrier(this.buffer!);
context.For(64, new TestShader(this.buffer!));
}
[AutoConstructor]
[EmbeddedBytecode(DispatchAxis.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct TestShader : IComputeShader
{
private readonly ReadWriteBuffer buffer;
///
public void Execute()
{
}
}
}