示例#1
0
        private static async Task ExecuteOnGpu(GraphicsDevice device, int index, TestShader shader)
        {
            ShaderGeneratorContext context = new ShaderGeneratorContext(device);

            context.Visit(shader);

            PipelineState pipelineState = await context.CreateComputePipelineStateAsync();

            DescriptorSet?descriptorSet = context.CreateShaderResourceViewDescriptorSet();

            using (CommandList commandList = new CommandList(device, CommandListType.Compute))
            {
                commandList.SetPipelineState(pipelineState);

                if (descriptorSet != null)
                {
                    commandList.SetComputeRootDescriptorTable(0, descriptorSet);
                }

                commandList.Dispatch(1, 1, 1);
                await commandList.FlushAsync();
            }

            float sum = shader.DestinationBuffer.Resource.GetArray <float>().Sum();

            Console.WriteLine($"Origin: GPU, Thread: {index}, sum: {sum}.");
        }
示例#2
0
        public static async Task RunAsync(GraphicsDevice device)
        {
            int count    = 100;
            int sumCount = 10;

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < count; i++)
            {
                int index = i;

                Console.WriteLine($"Scheduling task {index}");

                tasks.Add(Task.Run(async() =>
                {
                    TestShader cpuShader = CreateTestShader(device, sumCount);
                    await ExecuteOnCpu(device, index, cpuShader);

                    TestShader gpuShader = CreateTestShader(device, sumCount);
                    await ExecuteOnGpu(device, index, gpuShader);
                }));
            }

            Console.WriteLine("Awaiting tasks...");
            Console.WriteLine($"Task count: {tasks.Count}");

            await Task.WhenAll(tasks);

            Console.WriteLine("DONE!");
        }
示例#3
0
        private static TestShader CreateTestShader(GraphicsDevice device, int sumCount)
        {
            GraphicsResource buffer = GraphicsResource.CreateBuffer <float>(device, sumCount, ResourceFlags.AllowUnorderedAccess);
            WriteableStructuredBuffer <float> bufferView = new WriteableStructuredBuffer <float>(UnorderedAccessView.FromBuffer <float>(buffer));

            TestShader shader = new TestShader(bufferView, Sigmoid);

            return(shader);
        }
示例#4
0
        public static async Task RunAsync(GraphicsDevice device)
        {
            int count    = 100;
            int sumCount = 10;

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < count; i++)
            {
                int index = i;

                Console.WriteLine($"Scheduling task {index}");

                tasks.Add(Task.Run(async() =>
                {
                    GraphicsBuffer <float> buffer = GraphicsBuffer.Create <float>(device, sumCount, ResourceFlags.AllowUnorderedAccess);
                    TestShader shader             = new TestShader(buffer, Sigmoid);

                    ShaderGeneratorContext context = new ShaderGeneratorContext(device);
                    context.Visit(shader);

                    ShaderGeneratorResult result = new ShaderGenerator(shader).GenerateShader();

                    PipelineState pipelineState = await context.CreateComputePipelineStateAsync();
                    DescriptorSet?descriptorSet = context.CreateShaderResourceViewDescriptorSet();

                    using (CommandList commandList = new CommandList(device, CommandListType.Compute))
                    {
                        commandList.SetPipelineState(pipelineState);

                        if (descriptorSet != null)
                        {
                            commandList.SetComputeRootDescriptorTable(0, descriptorSet);
                        }

                        commandList.Dispatch(1, 1, 1);
                        await commandList.FlushAsync();
                    }

                    float sum = buffer.GetData().Sum();

                    Console.WriteLine($"Thread: {index}, sum: {sum}.");
                }));
            }

            Console.WriteLine("Awaiting tasks...");
            Console.WriteLine($"Task count: {tasks.Count}");

            await Task.WhenAll(tasks);

            Console.WriteLine("DONE!");
        }
示例#5
0
        private static Task ExecuteOnCpu(GraphicsDevice device, int index, TestShader shader)
        {
            for (int x = 0; x < 10; x++)
            {
                shader.Execute(new CSInput {
                    DispatchThreadId = new Int3 {
                        X = x
                    }
                });
            }

            float sum = shader.DestinationBuffer.Resource.GetArray <float>().Sum();

            Console.WriteLine($"Origin: CPU, Thread: {index}, sum: {sum}.");

            return(Task.CompletedTask);
        }