Implemented handling of captured buffers in shaders
このコミットが含まれているのは:
1 親
9e253144b4
コミット
08ed370fd8
4ファイル変更
+12
-41
検索条件に一致するファイルはありません
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
|
||||
namespace ComputeSharp.Shaders.Mappings
|
||||
{
|
||||
@@ -26,7 +27,7 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(Matrix4x4).FullName] = "float4x4",
|
||||
[typeof(ThreadIds).FullName] = "uint3",
|
||||
[typeof(RWBufferResource<>).FullName] = "RWBuffer"
|
||||
[typeof(Buffer2<>).FullName] = "RWBuffer"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -53,13 +54,13 @@ namespace ComputeSharp.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="Buffer2{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="Buffer2{T}"/> instance</returns>
|
||||
[Pure]
|
||||
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(RWBufferResource<>);
|
||||
public static bool IsBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(Buffer2<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given <see cref="Type"/> is an HLSL known type
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
@@ -115,16 +116,15 @@ namespace ComputeSharp.Shaders
|
||||
FieldInfoBase processedFieldInfo;
|
||||
|
||||
// Read write buffer
|
||||
if (HlslKnownTypes.IsReadWriteBufferType(fieldType))
|
||||
if (HlslKnownTypes.IsBufferType(fieldType) &&
|
||||
(HeapType)fieldType.GetProperty(nameof(Buffer2<byte>.HeapType)).GetValue(fieldValue) == HeapType.Default)
|
||||
{
|
||||
// Root parameter for a read write buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount);
|
||||
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++);
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
|
||||
namespace ComputeSharp.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,16 +20,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 Buffer2<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
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする