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 number of threads to run on the X axis /// The number of threads to run on the Y axis /// The number of threads to run on the Z axis /// The input representing the compute shader to run public static void Run(GraphicsDevice device, int x, int y, int z, Action 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); // Render the loaded shader ShaderInfo shaderInfo = new ShaderInfo { FieldsList = shaderLoader.FieldsInfo, ThreadsX = x, ThreadsY = y, ThreadsZ = z, NumThreadsX = threadsX, NumThreadsY = threadsY, NumThreadsZ = threadsZ, 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.Buffers.Select((r, i) => (Resource: r, Index: i))) { commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); } // Dispatch and wait for completion commandList.Dispatch(groupsX, groupsY, groupsZ); commandList.Flush(true); } } }