Simplified buffers type hierarchy
このコミットが含まれているのは:
1 親
94ed54ccc4
コミット
29eedddd65
3ファイル変更
+139
-162
検索条件に一致するファイルはありません
@@ -1,28 +1,108 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
|
||||
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory, that can be used from an HLSL shader
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public abstract class HlslBuffer<T> : Buffer<T> where T : unmanaged
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public unsafe abstract class HlslBuffer<T> : NativeObject
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslBuffer{T}"/> instance with the specified parameters
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
private protected HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType)
|
||||
: base(device, size, sizeInBytes, bufferType)
|
||||
private ComPtr<ID3D12Resource> d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
protected readonly int SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of each <typeparamref name="T"/> value contained in the buffer.
|
||||
/// </summary>
|
||||
protected readonly int ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
internal readonly int PaddedElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer type for the current <see cref="HlslBuffer{T}"/> instance.
|
||||
/// </summary>
|
||||
internal readonly BufferType BufferType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslBuffer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
|
||||
/// <param name="size">The number of items to store in the current buffer.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer.</param>
|
||||
/// <param name="bufferType">The buffer type for the current buffer.</param>
|
||||
internal HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes);
|
||||
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes;
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
BufferType = bufferType;
|
||||
PaddedElementSizeInBytes = sizeInBytes / size;
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (bufferType)
|
||||
{
|
||||
case BufferType.Constant: CreateConstantBufferView(); break;
|
||||
case BufferType.ReadOnly: CreateShaderResourceView(); break;
|
||||
case BufferType.ReadWrite: CreateUnorderedAccessView(); break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
|
||||
/// </summary>
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and returns an array
|
||||
/// </summary>
|
||||
@@ -111,5 +191,52 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a constant buffer.
|
||||
/// </summary>
|
||||
private void CreateConstantBufferView()
|
||||
{
|
||||
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
|
||||
|
||||
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
|
||||
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
|
||||
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateConstantBufferView(&d3D12ConstantBufferViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a readonly buffer.
|
||||
/// </summary>
|
||||
private void CreateShaderResourceView()
|
||||
{
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
|
||||
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
|
||||
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
d3D12ShaderResourceViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateShaderResourceView(D3D12Resource, &d3D12ShaderResourceViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a buffer than be both read and written to.
|
||||
/// </summary>
|
||||
private void CreateUnorderedAccessView()
|
||||
{
|
||||
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
|
||||
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
|
||||
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public unsafe class Buffer<T> : NativeObject
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12Resource> d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
|
||||
/// </summary>
|
||||
protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
protected readonly int SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of each <typeparamref name="T"/> value contained in the buffer.
|
||||
/// </summary>
|
||||
protected readonly int ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the current buffer.
|
||||
/// </summary>
|
||||
internal readonly int PaddedElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer type for the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
internal readonly BufferType BufferType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
|
||||
/// <param name="size">The number of items to store in the current buffer.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer.</param>
|
||||
/// <param name="bufferType">The buffer type for the current buffer.</param>
|
||||
internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes);
|
||||
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes;
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
BufferType = bufferType;
|
||||
PaddedElementSizeInBytes = sizeInBytes / size;
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (bufferType)
|
||||
{
|
||||
case BufferType.Constant: CreateConstantBufferView(); break;
|
||||
case BufferType.ReadOnly: CreateShaderResourceView(); break;
|
||||
case BufferType.ReadWrite: CreateUnorderedAccessView(); break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
|
||||
/// </summary>
|
||||
public GraphicsDevice GraphicsDevice { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
/// </summary>
|
||||
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a constant buffer.
|
||||
/// </summary>
|
||||
private void CreateConstantBufferView()
|
||||
{
|
||||
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
|
||||
|
||||
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
|
||||
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
|
||||
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateConstantBufferView(&d3D12ConstantBufferViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a readonly buffer.
|
||||
/// </summary>
|
||||
private void CreateShaderResourceView()
|
||||
{
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
|
||||
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
|
||||
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
d3D12ShaderResourceViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateShaderResourceView(D3D12Resource, &d3D12ShaderResourceViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a view for a buffer than be both read and written to.
|
||||
/// </summary>
|
||||
private void CreateUnorderedAccessView()
|
||||
{
|
||||
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
|
||||
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size;
|
||||
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
|
||||
|
||||
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using TerraFX.Interop;
|
||||
@@ -97,7 +97,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
|
||||
// Access Buffer<T>.D3D12GpuDescriptorHandle
|
||||
FieldInfo gpuDescriptorInfo = member.MemberType.GetField(
|
||||
nameof(Buffer<byte>.D3D12GpuDescriptorHandle),
|
||||
nameof(HlslBuffer<byte>.D3D12GpuDescriptorHandle),
|
||||
BindingFlags.NonPublic | BindingFlags.Instance)!;
|
||||
|
||||
il.EmitReadMember(gpuDescriptorInfo);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする