Example #1
0
            public Configure(GpuDevice device, Settings settings, GpuBindGroupLayout bindGroupLayout, GpuBindGroupLayout dynamicBindGroupLayout, GpuBindGroupLayout timeBindGroupLayout, GpuRenderPipeline pipeline, GpuRenderPipeline dynamicPipeline, GpuBuffer vertexBuffer, GpuTextureFormat swapChainFormat)
            {
                Device          = device;
                Settings        = settings;
                Pipeline        = pipeline;
                DynamicPipeline = dynamicPipeline;
                VertexBuffer    = vertexBuffer;
                SwapChainFormat = swapChainFormat;

                UniformBuffer = Device.CreateBuffer(new GpuBufferDescriptor(Settings.NumTriangles * AlignedUniformBytes + sizeof(float), GpuBufferUsageFlags.Uniform | GpuBufferUsageFlags.CopyDst));
                var uniformCpuBuffer = new Windows.Storage.Streams.Buffer(Settings.NumTriangles * AlignedUniformBytes)
                {
                    Length = Settings.NumTriangles * AlignedUniformBytes
                };

                using (var uniformCpuStream = uniformCpuBuffer.AsStream())
                    using (var uniformCpuWriter = new BinaryWriter(uniformCpuStream))
                    {
                        var rand = new Random();
                        for (var i = 0; i < Settings.NumTriangles; ++i)
                        {
                            uniformCpuWriter.Seek((int)(i * AlignedUniformBytes), SeekOrigin.Begin);
                            float scale = (float)(rand.NextDouble() * 0.2 + 0.2);
                            //scale = 5;
                            float offsetX      = (float)(0.9 * 2 * (rand.NextDouble() - 0.5));
                            float offsetY      = (float)(0.9 * 2 * (rand.NextDouble() - 0.5));
                            float scalar       = (float)(rand.NextDouble() * 1.5 + 0.5);
                            float scalarOffset = (float)(rand.NextDouble() * 10);
                            uniformCpuWriter.Write(scale);        //Scale
                            uniformCpuWriter.Write(offsetX);      //offsetX
                            uniformCpuWriter.Write(offsetY);      //offsetY
                            uniformCpuWriter.Write(scalar);       //scalar
                            uniformCpuWriter.Write(scalarOffset); //scalar offset
                        }
                    }
                BindGroups = new GpuBindGroup[Settings.NumTriangles];
                for (var i = 0; i < Settings.NumTriangles; ++i)
                {
                    BindGroups[i] = Device.CreateBindGroup(new GpuBindGroupDescriptor(bindGroupLayout, new GpuBindGroupEntry[]
                    {
                        new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, 6 * sizeof(float))
                        {
                            Offset = (UInt64)(i * AlignedUniformBytes)
                        })
                    }));
                }
                DynamicBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(dynamicBindGroupLayout, new GpuBindGroupEntry[]
                {
                    new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, 6 * sizeof(float)))
                }));


                TimeBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(timeBindGroupLayout, new GpuBindGroupEntry[]
                {
                    new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, sizeof(float))
                    {
                        Offset = TimeOffset
                    })
                }));
                Device.DefaultQueue.WriteBuffer(UniformBuffer, 0, uniformCpuBuffer);
                var renderBundleEncoder = Device.CreateRenderBundleEncoder(new GpuRenderBundleEncoderDescriptor(new GpuTextureFormat[] { SwapChainFormat }));

                RecordRenderPass(renderBundleEncoder);
                RenderBundle         = renderBundleEncoder.Finish();
                UniformTimeCpuBuffer = new Windows.Storage.Streams.Buffer(sizeof(float))
                {
                    Length = sizeof(float)
                };
            }