using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
///
/// A data loader for D2D1 pixel shaders dispatched via .
///
internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoader
{
///
/// The target buffer to write data to.
///
private readonly byte* buffer;
///
/// The length of the buffer pointed to by .
///
private readonly int length;
///
/// The number of written bytes.
///
private int writtenBytes;
///
/// Creates a new instance.
///
/// The target buffer to write data to.
/// The length of the buffer pointed to by .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D2D1ByteBufferDispatchDataLoader(byte* buffer, int length)
{
this.buffer = buffer;
this.length = length;
this.writtenBytes = 0;
}
///
/// Gets the number of written bytes, or 0 in case of failure.
///
/// The number of written bytes, of 0 in case of failure.
/// Whether or not the constant buffer was retrieved successfully.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetWrittenBytes(out int writtenBytes)
{
return (writtenBytes = this.writtenBytes) != 0;
}
///
void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
ReadOnlySpan constantBuffer = MemoryMarshal.Cast(data);
if (constantBuffer.TryCopyTo(new Span(this.buffer, this.length)))
{
this.writtenBytes = constantBuffer.Length;
}
}
}