public async Task <PipelineState> CreateGraphicsPipelineStateAsync() { if (MaterialDescriptor is null) { throw new InvalidOperationException("The current material descriptor cannot be null when creating a pipeline state."); } InputElementDescription[] inputElements = new[] { new InputElementDescription("Position", 0, (Format)PixelFormat.R32G32B32_Float, 0), new InputElementDescription("Normal", 0, (Format)PixelFormat.R32G32B32_Float, 1), new InputElementDescription("Tangent", 0, (Format)PixelFormat.R32G32B32A32_Float, 2), new InputElementDescription("TexCoord", 0, (Format)PixelFormat.R32G32_Float, 3) }; CompiledShader compiledShader = new CompiledShader(); string shaderCachePath = Path.Combine("Log", "ShaderCache"); string filePath = Path.Combine(shaderCachePath, $"Shader_{MaterialDescriptor.Id}"); if (!await Content.ExistsAsync(filePath)) { ShaderGenerator shaderGenerator = new ShaderGenerator(MaterialDescriptor.Attributes); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); CompiledShaderAsset shaderAsset = new CompiledShaderAsset(); foreach (var entryPoint in result.EntryPoints) { compiledShader.Shaders[entryPoint.Key] = ShaderCompiler.Compile(GetShaderStage(entryPoint.Key), result.ShaderSource, entryPoint.Value); shaderAsset.ShaderSources[entryPoint.Key] = Path.Combine(shaderCachePath, $"{entryPoint.Key}_{MaterialDescriptor.Id}.cso"); using Stream stream = await Content.FileProvider.OpenStreamAsync(shaderAsset.ShaderSources[entryPoint.Key], FileMode.Create, FileAccess.ReadWrite); await stream.WriteAsync(compiledShader.Shaders[entryPoint.Key], 0, compiledShader.Shaders[entryPoint.Key].Length); } await Content.SaveAsync(filePath, shaderAsset); } else { compiledShader = await Content.LoadAsync <CompiledShader>(filePath); } ID3D12RootSignature rootSignature = CreateRootSignature(); return(new PipelineState(GraphicsDevice, inputElements, rootSignature, compiledShader.Shaders["vertex"], compiledShader.Shaders["pixel"], compiledShader.Shaders.ContainsKey("geometry") ? compiledShader.Shaders["geometry"] : null, compiledShader.Shaders.ContainsKey("hull") ? compiledShader.Shaders["hull"] : null, compiledShader.Shaders.ContainsKey("domain") ? compiledShader.Shaders["domain"] : null)); }
public async Task <PipelineState> CreateGraphicsPipelineStateAsync() { if (MaterialDescriptor is null) { throw new InvalidOperationException("The current material descriptor cannot be null when creating a pipeline state."); } InputElementDescription[] inputElements = new[] { new InputElementDescription("Position", 0, (Format)PixelFormat.R32G32B32_Float, 0), new InputElementDescription("Normal", 0, (Format)PixelFormat.R32G32B32_Float, 1), new InputElementDescription("Tangent", 0, (Format)PixelFormat.R32G32B32A32_Float, 2), new InputElementDescription("TexCoord", 0, (Format)PixelFormat.R32G32_Float, 3) }; CompiledShader compiledShader = new CompiledShader(); string fileName = $"Shader_{MaterialDescriptor.Id}"; if (!await Content.ExistsAsync(fileName)) { ShaderGenerator shaderGenerator = new ShaderGenerator(MaterialDescriptor.Attributes); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); CompiledShaderAsset shaderAsset = new CompiledShaderAsset(); foreach (var entryPoint in result.EntryPoints) { compiledShader.Shaders[entryPoint.Key] = ShaderCompiler.Compile(GetShaderStage(entryPoint.Key), result.ShaderSource, entryPoint.Value); shaderAsset.ShaderSources[entryPoint.Key] = $"{entryPoint.Key}_{MaterialDescriptor.Id}.cso"; await FileIO.WriteBytesAsync(await Content.RootFolder !.CreateFileAsync(shaderAsset.ShaderSources[entryPoint.Key], CreationCollisionOption.ReplaceExisting), compiledShader.Shaders[entryPoint.Key]); } await Content.SaveAsync(fileName, shaderAsset); } else { compiledShader = await Content.LoadAsync <CompiledShader>(fileName); } ID3D12RootSignature rootSignature = CreateRootSignature(); return(new PipelineState(GraphicsDevice, inputElements, rootSignature, compiledShader.Shaders["vertex"], compiledShader.Shaders["pixel"], compiledShader.Shaders.ContainsKey("geometry") ? compiledShader.Shaders["geometry"] : null, compiledShader.Shaders.ContainsKey("hull") ? compiledShader.Shaders["hull"] : null, compiledShader.Shaders.ContainsKey("domain") ? compiledShader.Shaders["domain"] : null)); }
private static async Task ExecuteOnGpu(GraphicsDevice device, StructuredBuffer <float> sourceBufferView, WriteableStructuredBuffer <float> destinationBufferView) { bool generateWithDelegate = false; DescriptorSet descriptorSet = new DescriptorSet(device, 2); descriptorSet.AddResourceViews(destinationBufferView); descriptorSet.AddResourceViews(sourceBufferView); // Generate computer shader ShaderGenerator shaderGenerator = generateWithDelegate ? CreateShaderGeneratorWithDelegate(sourceBufferView, destinationBufferView) : CreateShaderGeneratorWithClass(); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); // Compile shader byte[] shaderBytecode = ShaderCompiler.Compile(ShaderStage.ComputeShader, result.ShaderSource, result.EntryPoints["compute"]); DescriptorRange[] descriptorRanges = new DescriptorRange[] { new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, 0), new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, 0) }; RootParameter rootParameter = new RootParameter(new RootDescriptorTable(descriptorRanges), ShaderVisibility.All); RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, new[] { rootParameter }); RootSignature rootSignature = new RootSignature(device, rootSignatureDescription); PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); // Execute computer shader using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); commandList.SetComputeRootDescriptorTable(0, descriptorSet); commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } }
public virtual Task <CompiledShader> CreateShaderAsync() { if (Shader is null) { throw new InvalidOperationException(); } CompiledShader compiledShader = new CompiledShader(); ShaderGenerator shaderGenerator = new ShaderGenerator(Shader, Settings); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); foreach (var entryPoint in result.EntryPoints) { compiledShader.Shaders[entryPoint.Key] = ShaderCompiler.Compile(GetShaderStage(entryPoint.Key), result.ShaderSource, entryPoint.Value); } return(Task.FromResult(compiledShader)); }
public override async Task <CompiledShader> CreateShaderAsync() { if (MaterialDescriptor is null) { throw new InvalidOperationException("The current material descriptor cannot be null when creating a pipeline state."); } if (Shader is null) { throw new InvalidOperationException(); } CompiledShader compiledShader = new CompiledShader(); string shaderCachePath = Path.Combine("Log", "ShaderCache"); string filePath = Path.Combine(shaderCachePath, $"Shader_{MaterialDescriptor.Id}"); if (!await Content.ExistsAsync(filePath)) { ShaderGenerator shaderGenerator = new ShaderGenerator(Shader, Settings); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); CompiledShaderAsset shaderAsset = new CompiledShaderAsset(); foreach (var entryPoint in result.EntryPoints) { compiledShader.Shaders[entryPoint.Key] = ShaderCompiler.Compile(GetShaderStage(entryPoint.Key), result.ShaderSource, entryPoint.Value); shaderAsset.ShaderSources[entryPoint.Key] = Path.Combine(shaderCachePath, $"{entryPoint.Key}_{MaterialDescriptor.Id}.cso"); using Stream stream = await Content.FileProvider.OpenStreamAsync(shaderAsset.ShaderSources[entryPoint.Key], FileMode.Create, FileAccess.ReadWrite); await stream.WriteAsync(compiledShader.Shaders[entryPoint.Key], 0, compiledShader.Shaders[entryPoint.Key].Length); } await Content.SaveAsync(filePath, shaderAsset); } else { compiledShader = await Content.LoadAsync <CompiledShader>(filePath); } return(compiledShader); }
private static async Task Main() { // Create graphics device using GraphicsDevice device = new GraphicsDevice(FeatureLevel.Level11_0); // Create 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; } float[] outputArray = new float[width * height]; using GraphicsBuffer <float> sourceBuffer = GraphicsBuffer.ShaderResource.New(device, array.AsSpan()); using GraphicsBuffer <float> destinationBuffer = GraphicsBuffer.UnorderedAccess.New <float>(device, array.Length * 2); GraphicsBuffer <float> slicedDestinationBuffer = destinationBuffer.Slice(20, 60); slicedDestinationBuffer = slicedDestinationBuffer.Slice(10, 50); DescriptorSet descriptorSet = new DescriptorSet(device, 2); descriptorSet.AddUnorderedAccessViews(slicedDestinationBuffer); descriptorSet.AddShaderResourceViews(sourceBuffer); // Generate computer shader bool generateWithDelegate = true; ShaderGenerator shaderGenerator = generateWithDelegate ? CreateShaderGeneratorWithDelegate(sourceBuffer, destinationBuffer) : CreateShaderGeneratorWithClass(); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); // Copmile shader byte[] shaderBytecode = ShaderCompiler.Compile(DxcShaderStage.ComputeShader, result.ShaderSource, result.EntryPoints["compute"]); DescriptorRange1[] descriptorRanges = new DescriptorRange1[] { new DescriptorRange1(DescriptorRangeType.UnorderedAccessView, 1, 0), new DescriptorRange1(DescriptorRangeType.ShaderResourceView, 1, 0) }; RootParameter1 rootParameter = new RootParameter1(new RootDescriptorTable1(descriptorRanges), ShaderVisibility.All); var rootSignatureDescription = new VersionedRootSignatureDescription(new RootSignatureDescription1(RootSignatureFlags.None, new[] { rootParameter })); var rootSignature = device.CreateRootSignature(rootSignatureDescription); PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); // Execute computer shader using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); commandList.SetComputeRootDescriptorTable(0, descriptorSet); commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } // Print matrix Console.WriteLine("Before:"); PrintMatrix(array, width, height); destinationBuffer.GetData(outputArray.AsSpan()); Console.WriteLine(); Console.WriteLine("After:"); PrintMatrix(outputArray, width, height); }