Merge pull request #16 from Sergio0694/feature_captured-scalars-buffers

Captured scalars buffers
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-14 15:10:18 +02:00
コミット 59c9fdc813
6ファイル変更
+114 -6

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

+50
ファイルの表示
@@ -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
ファイルの表示
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("ComputeSharp.Shaders")]
[assembly: InternalsVisibleTo("ComputeSharp.NetCore.Tests")]
+18 -1
ファイルの表示
@@ -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;
@@ -70,9 +72,24 @@ namespace ComputeSharp.Shaders
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();
}
}
}
}
+18 -4
ファイルの表示
@@ -63,6 +63,11 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
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>();
/// <summary>
@@ -107,6 +112,7 @@ namespace ComputeSharp.Shaders.Translation
List<DescriptorRange> descriptorRanges = new List<DescriptorRange>();
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((readWriteBuffersCount, (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((readOnlyBuffersCount, (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>
+26
ファイルの表示
@@ -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()
{
+1 -1
ファイルの表示
@@ -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]