From 6fc7cd4e9d9865bdcbd89d1422e4aa154c65c8df Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:42:18 +0200 Subject: [PATCH] Updated mustached template to support automatic bounds check --- src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs | 6 +++--- .../Renderer/Templates/ShaderTemplate.mustache | 7 ++++++- src/ComputeSharp.Shaders/ShaderRunner.cs | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 598568ef..eb7bf1fe 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -18,17 +18,17 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets or sets the number of threads in the X group /// - public int ThreadsX { get; set; } + public int NumThreadsX { get; set; } /// /// Gets or sets the number of threads in the Y group /// - public int ThreadsY { get; set; } + public int NumThreadsY { get; set; } /// /// Gets or sets the number of threads in the Z group /// - public int ThreadsZ { get; set; } + public int NumThreadsZ { get; set; } /// /// Gets or sets the name for the threads id variable used in the shader diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 3ab5de4a..eee11e3e 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -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}} +} diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index e16a67c7..7886a08a 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -31,9 +31,9 @@ namespace ComputeSharp.Shaders ShaderInfo shaderInfo = new ShaderInfo { FieldsList = shaderLoader.FieldsInfo, - ThreadsX = numThreads.X, - ThreadsY = numThreads.Y, - ThreadsZ = numThreads.Z, + NumThreadsX = numThreads.X, + NumThreadsY = numThreads.Y, + NumThreadsZ = numThreads.Z, ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, ShaderBody = shaderLoader.MethodBody };