9ファイル変更
+131
-19
検索条件に一致するファイルはありません
@@ -26,7 +26,7 @@ More details available [here](https://www.nuget.org/packages/ComputeSharp/).
|
||||
|
||||
# Quick start
|
||||
|
||||
**ComputeSharp** exposes a `Gpu` class that acts as the entry point for all public APIs. It exposes the `Gpu.Default` property that let you access the main GPU device on the current machine, which can be used to allocate buffers and perform operations.
|
||||
**ComputeSharp** exposes a `Gpu` class that acts entry point for all public APIs. It exposes the `Gpu.Default` property that let you access the main GPU device on the current machine, which can be used to allocate buffers and perform operations.
|
||||
|
||||
The following sample shows how to allocate a writeable buffer, populate it with a compute shader, and read it back.
|
||||
|
||||
@@ -34,11 +34,8 @@ The following sample shows how to allocate a writeable buffer, populate it with
|
||||
// Allocate a writeable buffer on the GPU, with the contents of the array
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1000);
|
||||
|
||||
// Shader body
|
||||
Action<ThreadIds> action = id => buffer[id.X] = id.X;
|
||||
|
||||
// Run the shader
|
||||
Gpu.Default.For(1000, action);
|
||||
Gpu.Default.For(1000, id => buffer[id.X] = id.X);
|
||||
|
||||
// Get the data back
|
||||
float[] array = buffer.GetData();
|
||||
@@ -50,7 +47,7 @@ If the shader in C# is capturing some local variable, those will be automaticall
|
||||
|
||||
## Advanced usage
|
||||
|
||||
**ComputeSharp** lets you dispatch compute shaders over thread groups from 1 to 3 dimensions, includes supports for readonly buffers, and more. Here is a more advanced sample showcasing both these two features.
|
||||
**ComputeSharp** lets you dispatch compute shaders over thread groups from 1 to 3 dimensions, includes supports for readonly buffers, and more. The shader body can both be declared inline, as a separate `Action<ThreadIds>` or as a local method. Here is a more advanced sample showcasing all these features.
|
||||
|
||||
```C#
|
||||
int height = 10, width = 10;
|
||||
@@ -61,14 +58,14 @@ using ReadOnlyBuffer<float> xBuffer = Gpu.Default.AllocateConstantBuffer(x);
|
||||
using ReadWriteBuffer<float> yBuffer = Gpu.Default.AllocateReadWriteBuffer(y);
|
||||
|
||||
// Shader body
|
||||
Action<ThreadIds> action = id =>
|
||||
void Main(ThreadIds id)
|
||||
{
|
||||
uint offset = id.X + id.Y * (uint)width;
|
||||
yBuffer[offset] += xBuffer[offset];
|
||||
};
|
||||
}
|
||||
|
||||
// Run the shader
|
||||
Gpu.Default.For(width, height, action);
|
||||
Gpu.Default.For(width, height, Main);
|
||||
|
||||
// Get the data back and write it to the y array
|
||||
yBuffer.GetData(y);
|
||||
|
||||
@@ -39,6 +39,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
|
||||
public abstract void GetData(Span<T> span);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="HlslBuffer{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="array">The input <typeparamref name="T"/> array to read data from</param>
|
||||
public void SetData(T[] array) => SetData(array.AsSpan());
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extension methods for the <see cref="GraphicsDevice"/> type to allocate generic resources
|
||||
/// </summary>
|
||||
internal static class GraphicsResourceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified generic value
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="data">The input <see cref="object"/> to copy to the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance (as a <see cref="GraphicsResource"/>) with the input data</returns>
|
||||
[Pure]
|
||||
public static GraphicsResource AllocateReadOnlyBufferFromReflectedSingleValue(this GraphicsDevice device, object data)
|
||||
{
|
||||
// Create the generic constant buffer
|
||||
Type
|
||||
dataType = data.GetType(),
|
||||
bufferType = typeof(ReadOnlyBuffer<>).MakeGenericType(dataType);
|
||||
ConstructorInfo constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First();
|
||||
GraphicsResource buffer = (GraphicsResource)constructor.Invoke(new object[] { device, 1 });
|
||||
|
||||
// Create the array with the input value
|
||||
Type arrayType = dataType.MakeArrayType();
|
||||
ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance
|
||||
| BindingFlags.Public
|
||||
| BindingFlags.Instance
|
||||
| BindingFlags.OptionalParamBinding);
|
||||
Array array = (Array)arrayConstructors[0].Invoke(new object[] { 1 });
|
||||
|
||||
// Use the IList indexer to set the generic object
|
||||
IList ilist = array;
|
||||
ilist[0] = data;
|
||||
|
||||
// Set the input data to the new buffer
|
||||
MethodInfo setDataMethod = bufferType.GetMethod(nameof(HlslBuffer<byte>.SetData), new[] { arrayType });
|
||||
setDataMethod.Invoke(buffer, new object[] { array });
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.Shaders")]
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.NetCore.Tests")]
|
||||
@@ -7,7 +7,7 @@ static {{&FieldType}} {{FieldName}} = {{FieldValue}};
|
||||
{{#IsConstantBuffer}}
|
||||
cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}})
|
||||
{
|
||||
{{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}};
|
||||
{{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}};
|
||||
}
|
||||
{{/IsConstantBuffer}}
|
||||
{{!Read write buffer}}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Extensions;
|
||||
using ComputeSharp.Shaders.Renderer;
|
||||
using ComputeSharp.Shaders.Renderer.Models;
|
||||
using ComputeSharp.Shaders.Translation;
|
||||
@@ -65,14 +67,29 @@ namespace ComputeSharp.Shaders
|
||||
commandList.SetPipelineState(pipelineState);
|
||||
|
||||
// Load the captured buffers
|
||||
foreach (var buffer in shaderLoader.Buffers.Select((r, i) => (Resource: r, Index: i)))
|
||||
foreach (var buffer in shaderLoader.Buffers)
|
||||
{
|
||||
commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource);
|
||||
}
|
||||
|
||||
// Load the captured variables
|
||||
List<GraphicsResource> buffers = new List<GraphicsResource>();
|
||||
foreach (var variable in shaderLoader.CapturedConstantBufferValues)
|
||||
{
|
||||
GraphicsResource resource = device.AllocateReadOnlyBufferFromReflectedSingleValue(variable.Value);
|
||||
commandList.SetComputeRootDescriptorTable(variable.Index, resource);
|
||||
buffers.Add(resource);
|
||||
}
|
||||
|
||||
// Dispatch and wait for completion
|
||||
commandList.Dispatch(groupsX, groupsY, groupsZ);
|
||||
commandList.Flush();
|
||||
|
||||
// Free the allocated resources
|
||||
foreach (GraphicsResource resource in buffers)
|
||||
{
|
||||
resource.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,12 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// Gets the ordered collection of buffers used as fields in the current shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<GraphicsResource> Buffers { get; private set; }
|
||||
public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ordered collection of captured variables that need to be assigned to constant buffers
|
||||
/// </summary>
|
||||
public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues { get; private set; }
|
||||
|
||||
private readonly List<FieldInfoBase> _FieldsInfo = new List<FieldInfoBase>();
|
||||
|
||||
@@ -106,7 +111,8 @@ namespace ComputeSharp.Shaders.Translation
|
||||
if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body");
|
||||
|
||||
List<DescriptorRange> descriptorRanges = new List<DescriptorRange>();
|
||||
List<GraphicsResource> buffers = new List<GraphicsResource>();
|
||||
List<(int, GraphicsResource)> buffers = new List<(int, GraphicsResource)>();
|
||||
List<(int, object)> variables = new List<(int, object)>();
|
||||
int readWriteBuffersCount = 0;
|
||||
int readOnlyBuffersCount = 0;
|
||||
|
||||
@@ -125,7 +131,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
buffers.Add((GraphicsResource)fieldValue);
|
||||
buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue));
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType);
|
||||
processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++);
|
||||
@@ -137,16 +143,23 @@ namespace ComputeSharp.Shaders.Translation
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
buffers.Add((GraphicsResource)fieldValue);
|
||||
buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue));
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, false);
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(fieldType))
|
||||
{
|
||||
// Static scalar field
|
||||
// Read only buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, readOnlyBuffersCount);
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Register the captured value
|
||||
variables.Add((descriptorRanges.Count - 1, fieldValue));
|
||||
|
||||
// Constant buffer field
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType);
|
||||
processedFieldInfo = new StaticScalarFieldInfo(typeName, fieldName, fieldValue);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, true);
|
||||
}
|
||||
else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}");
|
||||
|
||||
@@ -155,6 +168,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
|
||||
RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray();
|
||||
Buffers = buffers;
|
||||
CapturedConstantBufferValues = variables;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -177,6 +191,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
// Additional preprocessing
|
||||
MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", ""));
|
||||
MethodBody = MethodBody.TrimEnd('\n', '\r', ' ');
|
||||
MethodBody = $" {MethodBody.Replace(Environment.NewLine, $"{Environment.NewLine} ")}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Extensions;
|
||||
using ComputeSharp.NetCore.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
@@ -10,6 +12,30 @@ namespace ComputeSharp.NetCore.Tests
|
||||
[TestCategory("Buffers")]
|
||||
public class BufferTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetDataFromSingleValueInt()
|
||||
{
|
||||
object value = 77;
|
||||
|
||||
using ReadOnlyBuffer<int> buffer = (ReadOnlyBuffer<int>)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value);
|
||||
int[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result.Length == 1);
|
||||
Assert.IsTrue((int)value == result[0]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetDataFromSingleValueFloat()
|
||||
{
|
||||
object value = 3.14f;
|
||||
|
||||
using ReadOnlyBuffer<float> buffer = (ReadOnlyBuffer<float>)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value);
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result.Length == 1);
|
||||
Assert.IsTrue(MathF.Abs(result[0] - (float)value) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetDataFromArray()
|
||||
{
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace ComputeSharp.NetCore.Tests
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue((int)result[0] == (int)value);
|
||||
Assert.IsTrue(MathF.Abs(result[0] - value) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする