using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Buffers.Interop;
using ComputeSharp.Graphics.Buffers.Views;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
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.
[DebuggerTypeProxy(typeof(BufferDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ConstantBuffer : Buffer
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 length)
: base(device, length, (uint)GetPaddedSize(), ResourceType.Constant)
{
}
///
/// 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]");
///
/// Gets the right padded size for elements to store in the current instance.
///
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetPaddedSize()
{
return (Unsafe.SizeOf() + 15) & ~15;
}
///
public override unsafe void GetData(Span destination, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination));
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* destinationPointer = destination)
{
if (IsPaddingPresent)
{
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)destination.Length,
(uint)GetPaddedSize(),
destinationPointer);
}
else
{
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)destination.Length,
(uint)sizeof(T),
destinationPointer);
}
}
}
///
public override unsafe void SetData(ReadOnlySpan source, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source));
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* sourcePointer = source)
{
if (IsPaddingPresent)
{
MemoryHelper.Copy(
sourcePointer,
resource.Pointer,
(uint)offset,
(uint)source.Length,
(uint)GetPaddedSize());
}
else
{
MemoryHelper.Copy(
sourcePointer,
(uint)offset,
(uint)source.Length,
(uint)sizeof(T),
resource.Pointer);
}
}
}
///
public override unsafe void SetData(Buffer source)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
source.ThrowIfDisposed();
if (source is ConstantBuffer &&
source.GraphicsDevice == GraphicsDevice)
{
// Directly copy the input buffer, if possible
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(source);
}
///
public override string ToString()
{
return $"ComputeSharp.ConstantBuffer<{typeof(T)}>[{Length}]";
}
}
}