Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DescriptorRange"/> struct.
 /// </summary>
 /// <param name="other">A <see cref="DescriptorRange1"/> to initialize from.</param>
 public DescriptorRange(DescriptorRange1 other)
 {
     RangeType          = other.RangeType;
     NumDescriptors     = other.NumDescriptors;
     BaseShaderRegister = other.BaseShaderRegister;
     RegisterSpace      = other.RegisterSpace;
     OffsetInDescriptorsFromTableStart = other.OffsetInDescriptorsFromTableStart;
 }
Exemplo n.º 2
0
        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);
        }