using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Helpers;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
namespace ComputeSharp
{
///
/// A representing a typed read write buffer stored on GPU memory
///
/// The type of items stored on the buffer
public sealed class ConstantBuffer : HlslBuffer
where T : unmanaged
{
///
/// Creates a new instance with the specified parameters
///
/// The associated with the current instance
/// The number of items to store in the current buffer
internal ConstantBuffer(GraphicsDevice device, int size)
: base(device, size, size * GetPaddedSize(), BufferType.Constant)
{
}
///
/// Gets the right padded size for elements to store in the current instance
///
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetPaddedSize()
{
int size = Unsafe.SizeOf();
return (size + 15) & ~15;
}
///
/// Gets a single value from the current constant buffer
///
/// The index of the value to get
/// This API can only be used from a compute shader, and will always throw if used anywhere else
public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer)}[int]");
///
public override unsafe void GetData(Span span, int offset, int count)
{
using MappedResource resource = MapResource();
if (IsPaddingPresent)
{
ref T spanRef = ref MemoryMarshal.GetReference(span);
ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer);
// Move to the initial ref
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
for (var i = 0; i < count; i++)
{
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef);
}
}
else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
}
///
public override unsafe void SetData(ReadOnlySpan span, int offset, int count)
{
using MappedResource resource = MapResource();
if (IsPaddingPresent)
{
ref T spanRef = ref MemoryMarshal.GetReference(span);
ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer);
// Move to the initial offset
resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize());
for (var i = 0; i < count; i++)
{
ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize());
Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i);
}
}
else MemoryHelper.Copy(span, resource.Pointer, offset, count);
}
///
public override unsafe void SetData(HlslBuffer buffer)
{
if (buffer is ConstantBuffer)
{
// Directly copy the input buffer, if possible
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(buffer);
}
}
}