From d890f785508c1aa95c532cfd44e0f6c868e68c02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:00:46 +0200 Subject: [PATCH] Added ShaderRunner class --- src/ComputeSharp.Shaders/ShaderRunner.cs | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/ComputeSharp.Shaders/ShaderRunner.cs diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs new file mode 100644 index 00000000..e16a67c7 --- /dev/null +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -0,0 +1,65 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics; +using ComputeSharp.Shaders.Renderer; +using ComputeSharp.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Translation; +using SharpDX.Direct3D12; +using CommandList = ComputeSharp.Graphics.CommandList; +using PipelineState = ComputeSharp.Graphics.PipelineState; + +namespace ComputeSharp.Shaders +{ + /// + /// A responsible for performing all the necessary operations to compile and run a compute shader + /// + public static class ShaderRunner + { + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The value that indicates the number of threads to run in each thread group + /// The value that indicates the number of thread groups to dispatch + /// The input representing the compute shader to run + public static void Run(GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action action) + { + // Load the input shader + ShaderLoader shaderLoader = ShaderLoader.Load(action); + + // Render the loaded shader + ShaderInfo shaderInfo = new ShaderInfo + { + FieldsList = shaderLoader.FieldsInfo, + ThreadsX = numThreads.X, + ThreadsY = numThreads.Y, + ThreadsZ = numThreads.Z, + ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, + ShaderBody = shaderLoader.MethodBody + }; + string shaderSource = ShaderRenderer.Instance.Render(shaderInfo); + + // Compile the loaded shader to HLSL bytecode + ShaderBytecode shaderBytecode = ShaderCompiler.Instance.CompileShader(shaderSource); + + // Create the root signature for the pipeline and the pipeline state + RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, shaderLoader.RootParameters); + RootSignature rootSignature = device.CreateRootSignature(rootSignatureDescription); + PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); + + // Create the commands list + using CommandList commandList = new CommandList(device, CommandListType.Compute); + commandList.SetPipelineState(pipelineState); + + // Load the captured buffers + foreach (var buffer in shaderLoader.ReadWriteBuffers.Select((r, i) => (Resource: r, Index: i))) + { + commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); + } + + // Dispatch and wait for completion + commandList.Dispatch(numGroups.X, numGroups.Y, numGroups.Z); + commandList.Flush(true); + } + } +}