Merge pull request #6 from Sergio0694/feature_integrated-buffers

Integrated buffers
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-12 13:50:06 +02:00
コミット fcb5aad911
44ファイル変更
+366 -433

検索条件に一致するファイルはありません

@@ -1,7 +1,7 @@
using System;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics.Buffers.Abstract
namespace ComputeSharp.Graphics.Buffers.Abstract
{
/// <summary>
/// An abstract <see langword="class"/> that represents a graphics resource object
@@ -35,12 +35,12 @@ namespace DirectX12GameEngine.Graphics.Buffers.Abstract
/// <summary>
/// Gets the <see cref="CpuDescriptorHandle"/> instance for the current resource
/// </summary>
protected internal CpuDescriptorHandle NativeCpuDescriptorHandle { get; protected set; }
protected internal CpuDescriptorHandle? NativeCpuDescriptorHandle { get; protected set; }
/// <summary>
/// Gets the <see cref="GpuDescriptorHandle"/> instance for the current resource
/// </summary>
protected internal GpuDescriptorHandle NativeGpuDescriptorHandle { get; protected set; }
protected internal GpuDescriptorHandle? NativeGpuDescriptorHandle { get; protected set; }
/// <summary>
/// Maps the current resource to a specified subresource
+175
ファイルの表示
@@ -0,0 +1,175 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Helpers;
using SharpDX.Direct3D12;
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 class Buffer<T> : GraphicsResource where T : unmanaged
{
/// <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="heapType">The heap type for the current buffer</param>
protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device)
{
Size = size;
ElementSizeInBytes = Unsafe.SizeOf<T>();
SizeInBytes = Size * ElementSizeInBytes;
HeapType = heapType;
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags);
ResourceStates resourceStates = heapType switch
{
HeapType.Upload => ResourceStates.GenericRead,
HeapType.Readback => ResourceStates.CopyDestination,
_ => ResourceStates.Common
};
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates);
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch
{
HeapType.Default => CreateUnorderedAccessView(),
HeapType.Upload => CreateConstantBufferView(),
_ => default
};
}
/// <summary>
/// Creates the descriptors for a constant buffer
/// </summary>
/// <returns>The CPU and GPU handles for a constant buffer</returns>
[Pure]
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView()
{
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
int constantBufferSize = (SizeInBytes + 255) & ~255;
ConstantBufferViewDescription description = new ConstantBufferViewDescription
{
BufferLocation = NativeResource!.GPUVirtualAddress,
SizeInBytes = constantBufferSize
};
GraphicsDevice.NativeDevice.CreateConstantBufferView(description, cpuHandle);
return (cpuHandle, gpuHandle);
}
/// <summary>
/// Creates the descriptors for a read write buffer
/// </summary>
/// <returns>The CPU and GPU handles for a read write buffer</returns>
[Pure]
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView()
{
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
UnorderedAccessViewDescription description = new UnorderedAccessViewDescription
{
Format = SharpDX.DXGI.Format.R32_Float,
Dimension = UnorderedAccessViewDimension.Buffer,
Buffer = { ElementCount = Size }
};
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
return (cpuHandle, gpuHandle);
}
/// <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 the size in bytes of the current buffer
/// </summary>
public int SizeInBytes { get; }
/// <summary>
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
/// </summary>
public int ElementSizeInBytes { get; }
/// <summary>
/// Gets the heap type being targeted by the current buffer
/// </summary>
public HeapType HeapType { get; }
/// <summary>
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and returns an array
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
[Pure]
public T[] GetData()
{
T[] data = new T[Size];
GetData(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
public void GetData(Span<T> span)
{
// Create a temporary readback buffer if the current buffer type is read write (can't be read from directly)
if (HeapType == HeapType.Default)
{
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Readback);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
copyCommandList.Flush(true);
readbackBaffer.GetData(span);
}
else
{
// Map and read the memory back directly
Map(0);
MemoryHelper.Copy(MappedResource, span);
Unmap(0);
}
}
/// <summary>
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="Buffer{T}"/> instance
/// </summary>
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
public void SetData(Span<T> span)
{
// Create a temporary upload buffer if the current buffer type is read write (can't be written to directly)
if (HeapType == HeapType.Default)
{
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Upload);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
uploadBuffer.SetData(span);
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
copyCommandList.Flush(true);
}
else
{
// Map and copy the memory directly
Map(0);
MemoryHelper.Copy(span, MappedResource);
Unmap(0);
}
}
}
}
+60
ファイルの表示
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Graphics.Buffers.Extensions
{
/// <summary>
/// A <see langword="class"/> with extension methods for the <see cref="GraphicsDevice"/> type to allocate buffers
/// </summary>
public static class BufferExtensions
{
/// <summary>
/// Allocates a new read write buffer with the specified parameters
/// </summary>
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
/// <param name="size">The size of the buffer to allocate</param>
/// <returns>A zeroed <see cref="ReadWriteBuffer{T}"/> instance of size <paramref name="size"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, int size) where T : unmanaged
{
return new ReadWriteBuffer<T>(device, size);
}
/// <summary>
/// Allocates a new read write buffer with the specified parameters
/// </summary>
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
{
ReadWriteBuffer<T> buffer = new ReadWriteBuffer<T>(device, span.Length);
buffer.SetData(span);
return buffer;
}
/// <summary>
/// Allocates a new constant buffer with the specified parameters
/// </summary>
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
/// <param name="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
{
ReadOnlyBuffer<T> buffer = new ReadOnlyBuffer<T>(device, span.Length);
buffer.SetData(span);
return buffer;
}
}
}
+26
ファイルの表示
@@ -0,0 +1,26 @@
using System;
using SharpDX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers
{
/// <summary>
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
public sealed class ReadOnlyBuffer<T> : Buffer<T> where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyBuffer{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>
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { }
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
/// </summary>
/// <param name="i">The index of the value to get</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
}
}
+30
ファイルの表示
@@ -0,0 +1,30 @@
using System;
using SharpDX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers
{
/// <summary>
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
public sealed class ReadWriteBuffer<T> : Buffer<T> where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteBuffer{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>
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { }
/// <summary>
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
/// </summary>
/// <param name="i">The index of the value to get or set</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
public T this[uint i]
{
get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
}
}
}
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
internal sealed class CommandAllocatorPool : IDisposable
{
@@ -1,8 +1,8 @@
using System;
using DirectX12GameEngine.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Abstract;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
public sealed class CommandList : IDisposable
{
@@ -110,7 +110,8 @@ namespace DirectX12GameEngine.Graphics
public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GraphicsResource resource)
{
SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle);
if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor");
SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value);
}
public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor)
@@ -130,7 +131,8 @@ namespace DirectX12GameEngine.Graphics
public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource)
{
SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle);
if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor");
SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value);
}
public void SetComputeRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor)
@@ -1,6 +1,6 @@
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
public sealed class CompiledCommandList
{
@@ -8,11 +8,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.18362.2002-preview" />
<PackageReference Include="SharpDX.Direct3D12" Version="4.2.1-beta0-gab36f12303" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DirectX12GameEngine.Core\DirectX12GameEngine.Core.csproj" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Numerics.Vectors" Version="4.6.0-preview5.19224.8" />
<PackageReference Include="System.Runtime.InteropServices.WindowsRuntime" Version="4.3.0" />
</ItemGroup>
</Project>
@@ -1,7 +1,7 @@
using System;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
internal sealed class DescriptorAllocator : IDisposable
{
@@ -3,7 +3,7 @@ using System.Threading;
using SharpDX.Direct3D;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
public sealed class GraphicsDevice : IDisposable
{
@@ -1,4 +1,4 @@
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
public enum GraphicsHeapType
{
@@ -1,7 +1,7 @@
using System;
using System.Runtime.CompilerServices;
namespace DirectX12GameEngine.Core.Helpers
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A <see langword="class"/> with some helper methods to copy data between memory areas
@@ -1,6 +1,6 @@
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics
namespace ComputeSharp.Graphics
{
/// <summary>
/// A <see langword="class"/> representing a custom pipeline state for a compute operation
@@ -18,8 +18,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DirectX12GameEngine.Core\DirectX12GameEngine.Core.csproj" />
<ProjectReference Include="..\DirectX12GameEngine.Graphics\DirectX12GameEngine.Graphics.csproj" />
<ProjectReference Include="..\ComputeSharp.Graphics\ComputeSharp.Graphics.csproj" />
</ItemGroup>
<ItemGroup>
@@ -36,11 +35,7 @@
</ItemGroup>
<ItemGroup>
<None Remove="Assets\ShaderTemplate.mustache" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\ShaderTemplate.mustache">
<Content Include="Renderer\Templates\ShaderTemplate.mustache">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.CodeAnalysis;
namespace DirectX12GameEngine.Shaders.Mappings
namespace ComputeSharp.Shaders.Mappings
{
/// <summary>
/// A <see langword="class"/> that contains and maps known HLSL method names to common .NET method
@@ -30,9 +30,9 @@ namespace DirectX12GameEngine.Shaders.Mappings
["System.MathF.PI"] = "3.14159274f",
// ThreadIds
["DirectX12GameEngine.Shaders.Primitives.ThreadIds.X"] = ".x",
["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Y"] = ".y",
["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Z"] = ".z",
["ComputeSharp.ThreadIds.X"] = ".x",
["ComputeSharp.ThreadIds.Y"] = ".y",
["ComputeSharp.ThreadIds.Z"] = ".z",
// Vector3
["System.Numerics.Vector3.X"] = ".x",
@@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Text.RegularExpressions;
using DirectX12GameEngine.Shaders.Primitives;
using ComputeSharp.Graphics.Buffers;
namespace DirectX12GameEngine.Shaders.Mappings
namespace ComputeSharp.Shaders.Mappings
{
/// <summary>
/// A <see langword="class"/> that contains and maps known HLSL type names to common .NET types
@@ -27,7 +27,7 @@ namespace DirectX12GameEngine.Shaders.Mappings
[typeof(Vector4).FullName] = "float4",
[typeof(Matrix4x4).FullName] = "float4x4",
[typeof(ThreadIds).FullName] = "uint3",
[typeof(RWBufferResource<>).FullName] = "RWBuffer"
[typeof(ReadWriteBuffer<>).FullName] = "RWBuffer"
};
/// <summary>
@@ -54,13 +54,13 @@ namespace DirectX12GameEngine.Shaders.Mappings
type == typeof(Matrix4x4);
/// <summary>
/// Checks whether or not the input type is a <see cref="RWBufferResource{T}"/> value
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> value
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="RWBufferResource{T}"/> instance</returns>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadWriteBuffer{T}"/> instance</returns>
[Pure]
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(RWBufferResource<>);
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
/// <summary>
/// Checks whether or not a given <see cref="Type"/> is an HLSL known type
@@ -12,7 +12,7 @@ using ICSharpCode.Decompiler.Metadata;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace DirectX12GameEngine.Shaders
namespace ComputeSharp.Shaders
{
/// <summary>
/// A <see langword="class"/> that is able to decompile arbitrary methods
@@ -1,4 +1,4 @@
namespace DirectX12GameEngine.Shaders.Primitives
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that indicates the ids of a given GPU thread running a compute shader
@@ -1,4 +1,4 @@
namespace DirectX12GameEngine.Shaders.Renderer.Models.Abstract
namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract
{
/// <summary>
/// A <see langword="class"/> that contains info on a shader field
@@ -1,6 +1,6 @@
using DirectX12GameEngine.Shaders.Renderer.Models.Abstract;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields
namespace ComputeSharp.Shaders.Renderer.Models.Fields
{
/// <summary>
/// A <see langword="class"/> that contains info on a constant buffer field
@@ -1,6 +1,6 @@
using DirectX12GameEngine.Shaders.Renderer.Models.Abstract;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields
namespace ComputeSharp.Shaders.Renderer.Models.Fields
{
/// <summary>
/// A <see langword="class"/> that contains info on a read write buffer field
@@ -1,6 +1,6 @@
using DirectX12GameEngine.Shaders.Renderer.Models.Abstract;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields
namespace ComputeSharp.Shaders.Renderer.Models.Fields
{
/// <summary>
/// A <see langword="class"/> that contains info on a static scalar field
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using DirectX12GameEngine.Shaders.Renderer.Models.Abstract;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
#pragma warning disable CS8618 // Non-nullable field is uninitialized
namespace DirectX12GameEngine.Shaders.Renderer.Models
namespace ComputeSharp.Shaders.Renderer.Models
{
/// <summary>
/// A <see langword="class"/> that contains info to render a shader source to compile
@@ -2,9 +2,9 @@
using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
using DirectX12GameEngine.Shaders.Renderer.Models;
using ComputeSharp.Shaders.Renderer.Models;
namespace DirectX12GameEngine.Shaders.Renderer
namespace ComputeSharp.Shaders.Renderer
{
/// <summary>
/// A <see langword="class"/> that uses the mustache templating system to render HLSL shaders
@@ -24,7 +24,7 @@ namespace DirectX12GameEngine.Shaders.Renderer
string
assemblyPath = Assembly.GetExecutingAssembly().Location,
assemblyDirectory = Path.GetDirectoryName(assemblyPath) ?? throw new InvalidOperationException("Can't find the assembly directory"),
templatePath = Path.Combine(assemblyDirectory, "Assets", "ShaderTemplate.mustache");
templatePath = Path.Combine(assemblyDirectory, "Renderer", "Templates", "ShaderTemplate.mustache");
Template = File.ReadAllText(templatePath);
}
差分なしでファイル名を変更。
@@ -3,7 +3,7 @@ using System.Diagnostics.Contracts;
using DotNetDxc;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Shaders
namespace ComputeSharp.Shaders
{
/// <summary>
/// A <see langword="class"/> that uses the <see cref="HlslDxcLib"/> APIs to compile compute shaders
@@ -4,18 +4,17 @@ using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using DirectX12GameEngine.Graphics.Buffers.Abstract;
using DirectX12GameEngine.Shaders.Mappings;
using DirectX12GameEngine.Shaders.Primitives;
using DirectX12GameEngine.Shaders.Renderer.Models.Abstract;
using DirectX12GameEngine.Shaders.Renderer.Models.Fields;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Shaders.Mappings;
using ComputeSharp.Shaders.Renderer.Models.Fields;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SharpDX.Direct3D12;
#pragma warning disable CS8618 // Non-nullable field is uninitialized
namespace DirectX12GameEngine.Shaders
namespace ComputeSharp.Shaders
{
/// <summary>
/// A <see langword="class"/> responsible for loading and processing <see cref="Action{T}"/> instances
@@ -123,9 +122,7 @@ namespace DirectX12GameEngine.Shaders
descriptorRanges.Add(range);
// Reference to the underlying buffer
object readWriteBuffer = fieldInfo.GetValue(ShaderInstance);
GraphicsResource resource = (GraphicsResource)fieldType.GetProperty(nameof(RWBufferResource<byte>.Buffer)).GetValue(readWriteBuffer);
readWriteBuffers.Add(resource);
readWriteBuffers.Add((GraphicsResource)fieldValue);
string typeName = HlslKnownTypes.GetMappedName(fieldType);
processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++);
@@ -4,7 +4,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace DirectX12GameEngine.Shaders
namespace ComputeSharp.Shaders
{
public class ShaderSyntaxRewriter : CSharpSyntaxRewriter
{
@@ -1,10 +1,10 @@
using System.Linq;
using DirectX12GameEngine.Shaders.Mappings;
using ComputeSharp.Shaders.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace DirectX12GameEngine.Shaders
namespace ComputeSharp.Shaders
{
public static class SyntaxNodeExtensions
{
+4 -4
ファイルの表示
@@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29201.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Graphics", "DirectX12GameEngine.Graphics\DirectX12GameEngine.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Shaders", "DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "ComputeSharp.Shaders\ComputeSharp.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Core", "DirectX12GameEngine.Core\DirectX12GameEngine.Core.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
+2 -2
ファイルの表示
@@ -6,8 +6,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DirectX12GameEngine.Graphics\DirectX12GameEngine.Graphics.csproj" />
<ProjectReference Include="..\DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj" />
<ProjectReference Include="..\ComputeSharp.Graphics\ComputeSharp.Graphics.csproj" />
<ProjectReference Include="..\ComputeSharp.Shaders\ComputeSharp.Shaders.csproj" />
</ItemGroup>
</Project>
+7 -7
ファイルの表示
@@ -1,13 +1,13 @@
using System;
using System.Linq;
using DirectX12GameEngine.Graphics;
using DirectX12GameEngine.Shaders;
using DirectX12GameEngine.Shaders.Primitives;
using DirectX12GameEngine.Shaders.Renderer;
using DirectX12GameEngine.Shaders.Renderer.Models;
using ComputeSharp;
using ComputeSharp.Graphics;
using ComputeSharp.Shaders;
using ComputeSharp.Shaders.Renderer;
using ComputeSharp.Shaders.Renderer.Models;
using SharpDX.Direct3D12;
using CommandList = DirectX12GameEngine.Graphics.CommandList;
using PipelineState = DirectX12GameEngine.Graphics.PipelineState;
using CommandList = ComputeSharp.Graphics.CommandList;
using PipelineState = ComputeSharp.Graphics.PipelineState;
using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode;
namespace DirectX12ComputeShaderSample
+6 -11
ファイルの表示
@@ -1,9 +1,8 @@
using DirectX12GameEngine.Shaders;
using System;
using System;
using System.Linq;
using DirectX12GameEngine.Graphics.Buffers;
using DirectX12GameEngine.Shaders.Primitives;
using Buffer = DirectX12GameEngine.Graphics.Buffers.Buffer;
using ComputeSharp;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.Graphics.Buffers.Extensions;
namespace DirectX12ComputeShaderSample
{
@@ -20,16 +19,12 @@ namespace DirectX12ComputeShaderSample
array[i] = i + 1;
}
using Buffer<float> gpuBuffer = Buffer.UnorderedAccess.New(Gpu.Default, array.AsSpan());
// Variables for closure
uint size = (uint)width;
var data = gpuBuffer.GetGpuResource();
using ReadWriteBuffer<float> gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan());
// Shader body
Action<ThreadIds> action = id =>
{
data[id.X + id.Y * size] *= 2;
gpuBuffer[id.X + id.Y * (uint)width] *= 2;
};
// Run the shader
-18
ファイルの表示
@@ -1,18 +0,0 @@
namespace DirectX12GameEngine.Graphics
{
public struct BufferDescription
{
public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, GraphicsHeapType heapType)
{
SizeInBytes = sizeInBytes;
Flags = bufferFlags;
HeapType = heapType;
}
public int SizeInBytes;
public BufferFlags Flags;
public GraphicsHeapType HeapType;
}
}
-19
ファイルの表示
@@ -1,19 +0,0 @@
using System;
namespace DirectX12GameEngine.Graphics
{
[Flags]
public enum BufferFlags
{
None = 0,
ConstantBuffer = 1,
IndexBuffer = 2,
VertexBuffer = 4,
RenderTarget = 8,
ShaderResource = 16,
UnorderedAccess = 32,
StructuredBuffer = 64,
RawBuffer = 512,
ArgumentBuffer = 1024
}
}
-25
ファイルの表示
@@ -1,25 +0,0 @@
using System;
namespace DirectX12GameEngine.Graphics.Buffers
{
public partial class Buffer
{
public static class Constant
{
public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Upload)
{
return Buffer.New(device, size, BufferFlags.ConstantBuffer, heapType);
}
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged
{
return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType);
}
public static unsafe Buffer<T> New<T>(GraphicsDevice device, Span<T> data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged
{
return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType);
}
}
}
}
-25
ファイルの表示
@@ -1,25 +0,0 @@
using System;
namespace DirectX12GameEngine.Graphics.Buffers
{
public partial class Buffer
{
public static class UnorderedAccess
{
public static unsafe Buffers.Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return Buffers.Buffer.New(device, size, BufferFlags.UnorderedAccess, heapType);
}
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType);
}
public static unsafe Buffer<T> New<T>(GraphicsDevice device, Span<T> data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType);
}
}
}
}
-232
ファイルの表示
@@ -1,232 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using DirectX12GameEngine.Core.Helpers;
using DirectX12GameEngine.Graphics.Buffers.Abstract;
using SharpDX.Direct3D12;
namespace DirectX12GameEngine.Graphics.Buffers
{
public partial class Buffer : GraphicsResource
{
protected Buffer(GraphicsDevice device) : base(device)
{
}
public BufferDescription Description { get; private set; }
public int SizeInBytes => Description.SizeInBytes;
public BufferFlags Flags => Description.Flags;
public GraphicsHeapType Usage => Description.HeapType;
public int ElementCount { get; protected set; }
public T[] GetData<T>() where T : unmanaged
{
T[] data = new T[SizeInBytes / Unsafe.SizeOf<T>()];
GetData(data.AsSpan());
return data;
}
public unsafe void GetData<T>(ref T data, int offsetInBytes = 0) where T : unmanaged
{
fixed (T* pointer = &data)
{
GetData(new Span<T>(pointer, 1), offsetInBytes);
}
}
public void GetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage == GraphicsHeapType.Default)
{
using Buffer<T> readbackBaffer = New<T>(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf<T>());
copyCommandList.Flush(true);
readbackBaffer.GetData(data);
}
else
{
Map(0);
MemoryHelper.Copy(MappedResource + offsetInBytes, data);
Unmap(0);
}
}
public unsafe void SetData<T>(in T data, int offsetInBytes = 0) where T : unmanaged
{
fixed (T* pointer = &data)
{
SetData(new Span<T>(pointer, 1), offsetInBytes);
}
}
public void SetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
{
if (Usage == GraphicsHeapType.Default)
{
using Buffer<T> uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf<T>());
copyCommandList.Flush(true);
}
else
{
Map(0);
MemoryHelper.Copy(data, MappedResource + offsetInBytes);
Unmap(0);
}
}
public Buffers.Buffer ToReadback()
{
BufferDescription description = Description;
description.HeapType = GraphicsHeapType.Readback;
description.Flags = BufferFlags.None;
return New(GraphicsDevice, description);
}
public static Buffers.Buffer New(GraphicsDevice device, BufferDescription description)
{
return new Buffers.Buffer(device).InitializeFrom(description);
}
public static Buffers.Buffer New(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return New(device, new BufferDescription(size, bufferFlags, heapType));
}
public static Buffer<T> New<T>(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
int bufferSize = elementCount * Unsafe.SizeOf<T>();
return new Buffer<T>(device, bufferSize, bufferFlags, heapType);
}
public static unsafe Buffer<T> New<T>(GraphicsDevice device, in T data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
fixed (T* pointer = &data)
{
return New(device, new Span<T>(pointer, 1), bufferFlags, heapType);
}
}
public static Buffer<T> New<T>(GraphicsDevice device, Span<T> data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
Buffer<T> buffer = New<T>(device, data.Length, bufferFlags, heapType);
buffer.SetData(data);
return buffer;
}
protected internal Buffers.Buffer InitializeFrom(BufferDescription description)
{
ResourceStates resourceStates = ResourceStates.Common;
if (description.HeapType == GraphicsHeapType.Upload)
{
resourceStates = ResourceStates.GenericRead;
}
else if (description.HeapType == GraphicsHeapType.Readback)
{
resourceStates = ResourceStates.CopyDestination;
}
NativeResource ??= GraphicsDevice.NativeDevice.CreateCommittedResource(
new HeapProperties((HeapType)description.HeapType), HeapFlags.None,
ConvertToNativeDescription(description), resourceStates);
Description = description;
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = description.Flags switch
{
BufferFlags.ConstantBuffer => CreateConstantBufferView(),
BufferFlags.ShaderResource => CreateShaderResourceView(),
BufferFlags.UnorderedAccess => CreateUnorderedAccessView(),
_ => default
};
return this;
}
private static ResourceDescription ConvertToNativeDescription(BufferDescription description)
{
int size = description.SizeInBytes;
ResourceFlags flags = ResourceFlags.None;
if (description.Flags.HasFlag(BufferFlags.UnorderedAccess))
{
flags |= ResourceFlags.AllowUnorderedAccess;
//flags |= ResourceFlags.AllowSimultaneousAccess;
}
return ResourceDescription.Buffer(size, flags);
}
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView()
{
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
int constantBufferSize = (SizeInBytes + 255) & ~255;
ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription
{
BufferLocation = NativeResource.GPUVirtualAddress,
SizeInBytes = constantBufferSize
};
GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle);
return (cpuHandle, gpuHandle);
}
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView()
{
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle);
return (cpuHandle, gpuHandle);
}
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView()
{
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
var description = new UnorderedAccessViewDescription
{
Format = SharpDX.DXGI.Format.R32_Float,
Dimension = UnorderedAccessViewDimension.Buffer,
Buffer =
{
ElementCount = ElementCount,
}
};
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
return (cpuHandle, gpuHandle);
}
}
public class Buffer<T> : Buffers.Buffer where T : unmanaged
{
protected internal Buffer(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) : base(device)
{
ElementSize = Unsafe.SizeOf<T>();
ElementCount = size / ElementSize;
InitializeFrom(new BufferDescription(size, bufferFlags, heapType));
}
public int ElementSize { get; }
public T[] GetData() => GetData<T>();
}
}
-28
ファイルの表示
@@ -1,28 +0,0 @@
using System;
using System.Numerics;
using DirectX12GameEngine.Graphics.Buffers;
using Buffer = System.Buffer;
namespace DirectX12GameEngine.Shaders
{
public abstract class ShaderResource
{
}
public sealed class RWBufferResource<T> : ShaderResource where T : unmanaged
{
internal RWBufferResource(Buffer<T> buffer) => Buffer = buffer;
public Buffer<T> Buffer { get; }
public T this[uint index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
public static class RWBufferResourceExtensions
{
public static RWBufferResource<T> GetGpuResource<T>(this Buffer<T> buffer) where T : unmanaged
{
return new RWBufferResource<T>(buffer);
}
}
}