diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 28de6038..00bfe9cb 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -11,57 +11,17 @@ using PipelineState = DirectX12GameEngine.Graphics.PipelineState; namespace DirectX12ComputeShaderSample { - public class MyComputeShader - { - [ConstantBufferResource] public uint Width; - - [ShaderResource] public RWBufferResource Data; - - [ShaderMethod] - [Shader("compute")] - [NumThreads(5, 5, 1)] - public void CSMain([SystemDispatchThreadIdSemantic] UInt3 id) - { - Data[id.X + id.Y * Width] *= 2; - } - } - public class Program { private static void Main() { - // Generate computer shader - - MyComputeShader myComputeShader = new MyComputeShader(); - ShaderGenerator shaderGenerator = new ShaderGenerator(myComputeShader); - ShaderGenerationResult result = shaderGenerator.GenerateShader(); - - byte[] shaderBytecode = ShaderCompiler.CompileShader(result.ShaderSource, SharpDX.D3DCompiler.ShaderVersion.ComputeShader, result.ComputeShader); - - // Create graphics device and pipeline state - + // Create the graphics device using GraphicsDevice device = new GraphicsDevice(true); - SharpDX.Direct3D12.RootParameter[] rootParameters = new SharpDX.Direct3D12.RootParameter[] - { - new SharpDX.Direct3D12.RootParameter(SharpDX.Direct3D12.ShaderVisibility.All, - new SharpDX.Direct3D12.RootConstants(0, 0, 1)), - new SharpDX.Direct3D12.RootParameter(SharpDX.Direct3D12.ShaderVisibility.All, - new SharpDX.Direct3D12.DescriptorRange(SharpDX.Direct3D12.DescriptorRangeType.UnorderedAccessView, 1, 0)) - }; - - var rootSignatureDescription = new SharpDX.Direct3D12.RootSignatureDescription(SharpDX.Direct3D12.RootSignatureFlags.None, rootParameters); - var rootSignature = device.CreateRootSignature(rootSignatureDescription); - - PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); - - // Create graphics buffer - + // Create the graphics buffer int width = 10; int height = 10; - float[] array = new float[width * height]; - for (int i = 0; i < array.Length; i++) { array[i] = i + 1; @@ -69,6 +29,37 @@ namespace DirectX12ComputeShaderSample using Buffer gpuBuffer = Buffer.UnorderedAccess.New(device, array.AsSpan()); + // Variables for closure + var data = new RWBufferResource(); + uint Width = (uint)width; + + // Shader body + Action action = id => + { + data[id.X + id.Y * Width] *= 2; + }; + + // Generate computer shader + ShaderGenerator shaderGenerator = new ShaderGenerator(action); + ShaderGenerationResult result = shaderGenerator.GenerateShaderForLambda(); + + byte[] shaderBytecode = ShaderCompiler.CompileShader(result.ShaderSource, SharpDX.D3DCompiler.ShaderVersion.ComputeShader, result.ComputeShader); + + // Create the pipeline state + + RootParameter[] rootParameters = + { + new RootParameter(ShaderVisibility.All, new RootConstants(0, 0, 1)), + new RootParameter(ShaderVisibility.All, new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, 0)) + }; + + var rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, rootParameters); + var rootSignature = device.CreateRootSignature(rootSignatureDescription); + + PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); + + + // Execute computer shader using (CommandList commandList = new CommandList(device, CommandListType.Compute))