Merge pull request #7 from Sergio0694/feature_auto-oob-skip

Added automatic bounds check
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-12 16:50:08 +02:00
コミット c872066e0c
7ファイル変更
+48 -104

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

-66
ファイルの表示
@@ -1,66 +0,0 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that indicates the number of threads to use to run a given compute shader
/// </summary>
[DebuggerDisplay("(X: {X}, Y: {Y}, Z: {Z})")]
public readonly struct NumThreads
{
/// <summary>
/// The X id of the current thread
/// </summary>
public readonly int X;
/// <summary>
/// The Y id of the current thread
/// </summary>
public readonly int Y;
/// <summary>
/// The Z id of the current thread
/// </summary>
public readonly int Z;
/// <summary>
/// Creates a new <see cref="NumThreads"/> instance with the specified parameters
/// </summary>
/// <param name="x">The number of threads in the X axis</param>
/// <param name="y">The number of threads in the Y axis</param>
public NumThreads(int x, int y)
{
X = x;
Y = y;
Z = 1;
}
/// <summary>
/// Creates a new <see cref="NumThreads"/> instance with the specified parameters
/// </summary>
/// <param name="x">The number of threads in the X axis</param>
/// <param name="y">The number of threads in the Y axis</param>
/// <param name="z">The number of threads in the Z axis</param>
public NumThreads(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Creates a new <see cref="NumThreads"/> instance from a value tuple
/// </summary>
/// <param name="xy">The tuple with the thread X and Y counts</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator NumThreads((int X, int Y) xy) => new NumThreads(xy.X, xy.Y);
/// <summary>
/// Creates a new <see cref="NumThreads"/> instance from a value tuple
/// </summary>
/// <param name="xy">The tuple with the thread X, Y and Z counts</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator NumThreads((int X, int Y, int Z) xyz) => new NumThreads(xyz.X, xyz.Y, xyz.Z);
}
}
+18 -3
ファイルの表示
@@ -16,20 +16,35 @@ namespace ComputeSharp.Shaders.Renderer.Models
public IReadOnlyList<FieldInfoBase> FieldsList { get; set; }
/// <summary>
/// Gets or sets the number of threads in the X group
/// Gets or sets the total number of threads to run on the X axis
/// </summary>
public int ThreadsX { get; set; }
/// <summary>
/// Gets or sets the number of threads in the Y group
/// Gets or sets the total number of threads to run on the Y axis
/// </summary>
public int ThreadsY { get; set; }
/// <summary>
/// Gets or sets the number of threads in the Z group
/// Gets or sets the total number of threads to run on the Z axis
/// </summary>
public int ThreadsZ { get; set; }
/// <summary>
/// Gets or sets the number of threads in the X group
/// </summary>
public int NumThreadsX { get; set; }
/// <summary>
/// Gets or sets the number of threads in the Y group
/// </summary>
public int NumThreadsY { get; set; }
/// <summary>
/// Gets or sets the number of threads in the Z group
/// </summary>
public int NumThreadsZ { get; set; }
/// <summary>
/// Gets or sets the name for the threads id variable used in the shader
/// </summary>
+6 -1
ファイルの表示
@@ -18,6 +18,11 @@ cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}})
{{/FieldsList}}
{{!Shader entry point}}
[Shader("compute")]
[NumThreads({{ThreadsX}}, {{ThreadsY}}, {{ThreadsZ}})]
[NumThreads({{NumThreadsX}}, {{NumThreadsY}}, {{NumThreadsZ}})]
void CSMain(uint3 {{ThreadsIdsVariableName}} : SV_DispatchThreadId)
{
if ({{ThreadsIdsVariableName}}.x < {{ThreadsX}} &&
{{ThreadsIdsVariableName}}.y < {{ThreadsY}} &&
{{ThreadsIdsVariableName}}.z < {{ThreadsZ}})
{{&ShaderBody}}
}
+20 -7
ファイルの表示
@@ -19,11 +19,21 @@ namespace ComputeSharp.Shaders
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="numThreads">The <see cref="NumThreads"/> value that indicates the number of threads to run in each thread group</param>
/// <param name="numGroups">The <see cref="NumThreads"/> value that indicates the number of thread groups to dispatch</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="z">The number of threads to run on the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void Run(GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action<ThreadIds> action)
public static void Run(GraphicsDevice device, int x, int y, int z, Action<ThreadIds> action)
{
// Calculate the optimized thread num and group values
int
threadsX = device.WavefrontSize,
threadsY = y > 1 ? device.WavefrontSize : 1,
threadsZ = z > 1 ? device.WavefrontSize : 1,
groupsX = (x - 1) / device.WavefrontSize + 1,
groupsY = (y - 1) / device.WavefrontSize + 1,
groupsZ = (z - 1) / device.WavefrontSize + 1;
// Load the input shader
ShaderLoader shaderLoader = ShaderLoader.Load(action);
@@ -31,9 +41,12 @@ namespace ComputeSharp.Shaders
ShaderInfo shaderInfo = new ShaderInfo
{
FieldsList = shaderLoader.FieldsInfo,
ThreadsX = numThreads.X,
ThreadsY = numThreads.Y,
ThreadsZ = numThreads.Z,
ThreadsX = x,
ThreadsY = y,
ThreadsZ = z,
NumThreadsX = threadsX,
NumThreadsY = threadsY,
NumThreadsZ = threadsZ,
ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName,
ShaderBody = shaderLoader.MethodBody
};
@@ -58,7 +71,7 @@ namespace ComputeSharp.Shaders
}
// Dispatch and wait for completion
commandList.Dispatch(numGroups.X, numGroups.Y, numGroups.Z);
commandList.Dispatch(groupsX, groupsY, groupsZ);
commandList.Flush(true);
}
}
+1
ファイルの表示
@@ -161,6 +161,7 @@ namespace ComputeSharp.Shaders.Translation
// Additional preprocessing
MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", ""));
MethodBody = MethodBody.TrimEnd('\n', '\r', ' ');
}
}
}
+3 -26
ファイルの表示
@@ -15,7 +15,7 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, Action<ThreadIds> action) => device.For(x, 1, 1, action);
public static void For(this GraphicsDevice device, int x, Action<ThreadIds> action) => ShaderRunner.Run(device, x, 1, 1, action);
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
@@ -24,7 +24,7 @@ namespace ComputeSharp
/// <param name="x">The number of threads to run on the X axis</param>
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, int y, Action<ThreadIds> action) => device.For(x, y, 1, action);
public static void For(this GraphicsDevice device, int x, int y, Action<ThreadIds> action) => ShaderRunner.Run(device, x, y, 1, action);
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
@@ -34,29 +34,6 @@ namespace ComputeSharp
/// <param name="y">The number of threads to run on the Y axis</param>
/// <param name="z">The number of threads to run on the Z axis</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, int x, int y, int z, Action<ThreadIds> action)
{
int
threadsX = device.WavefrontSize,
threadsY = y > 1 ? device.WavefrontSize : 1,
threadsZ = z > 1 ? device.WavefrontSize : 1,
groupsX = (x - 1) / device.WavefrontSize + 1,
groupsY = (y - 1) / device.WavefrontSize + 1,
groupsZ = (z - 1) / device.WavefrontSize + 1;
ShaderRunner.Run(device, (threadsX, threadsY, threadsZ), (groupsX, groupsY, groupsZ), action);
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader</param>
/// <param name="numThreads">The <see cref="NumThreads"/> value that indicates the number of threads to run in each thread group</param>
/// <param name="numGroups">The <see cref="NumThreads"/> value that indicates the number of thread groups to dispatch</param>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
public static void For(this GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action<ThreadIds> action)
{
ShaderRunner.Run(device, numThreads, numGroups, action);
}
public static void For(this GraphicsDevice device, int x, int y, int z, Action<ThreadIds> action) => ShaderRunner.Run(device, x, y, z, action);
}
}
-1
ファイルの表示
@@ -25,7 +25,6 @@ namespace DirectX12ComputeShaderSample
Action<ThreadIds> action = id =>
{
uint offset = id.X + id.Y * (uint)width;
if (offset > 99) return;
gpuBuffer[offset] *= 2;
};