using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Graphics.Resources.Interop;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using Microsoft.Toolkit.Diagnostics;
using FX = TerraFX.Interop.Windows;
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
{
///
/// The alignment boundary for elements in a constant buffer.
///
private const int ElementAlignment = FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT;
///
/// Creates a new instance with the specified parameters.
///
/// The associated with the current instance.
/// The number of items to store in the current buffer.
/// The allocation mode to use for the new resource.
internal ConstantBuffer(GraphicsDevice device, int length, AllocationMode allocationMode)
: base(device, length, (uint)GetPaddedSize(), ResourceType.Constant, allocationMode)
{
}
///
/// 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 unsafe int GetPaddedSize()
{
return AlignmentHelper.Pad(sizeof(T), ElementAlignment);
}
///
internal override unsafe void CopyTo(ref T destination, int size, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* destinationPointer = &destination)
{
if (IsPaddingPresent)
{
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)size,
(uint)GetPaddedSize(),
destinationPointer);
}
else
{
MemoryHelper.Copy(
resource.Pointer,
(uint)offset,
(uint)size,
(uint)sizeof(T),
destinationPointer);
}
}
}
///
internal override unsafe void CopyFrom(ref T source, int size, int offset)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
Guard.IsInRange(offset, 0, Length, nameof(offset));
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* sourcePointer = &source)
{
if (IsPaddingPresent)
{
MemoryHelper.Copy(
sourcePointer,
resource.Pointer,
(uint)offset,
(uint)size,
(uint)GetPaddedSize(),
isPaddingInSourceToo: false);
}
else
{
MemoryHelper.Copy(
sourcePointer,
(uint)offset,
(uint)size,
(uint)sizeof(T),
resource.Pointer);
}
}
}
///
public override unsafe void CopyFrom(Buffer source)
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
if (source is ConstantBuffer buffer)
{
using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
if (IsPaddingPresent)
{
MemoryHelper.Copy(
sourceMap.Pointer,
destinationMap.Pointer,
0,
(uint)source.Length,
(uint)GetPaddedSize(),
isPaddingInSourceToo: true);
}
else
{
MemoryHelper.Copy(
sourceMap.Pointer,
0,
(uint)source.Length,
(uint)sizeof(T),
destinationMap.Pointer);
}
}
else CopyFromWithCpuBuffer(source);
}
///
public override string ToString()
{
return $"ComputeSharp.ConstantBuffer<{typeof(T)}>[{Length}]";
}
}
}