public void UpdateSetsDescriptorCopy()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 2) });

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0];
                    // It is valid to copy from self to self (without overlapping memory boundaries).
                    var descriptorCopy = new CopyDescriptorSet(set, 0, 0, set, 1, 0, 1);
                    pool.UpdateSets(descriptorCopies: new[] { descriptorCopy });
                }
        }
Example #2
0
        private void DrawInternal()
        {
            // Update descriptors
            var descriptorSets = stackalloc DescriptorSet[2];
            var setLayouts = stackalloc DescriptorSetLayout[2];
            setLayouts[0] = setLayouts[1] = descriptorSetLayout;

            var allocateInfo = new DescriptorSetAllocateInfo
            {
                StructureType = StructureType.DescriptorSetAllocateInfo,
                DescriptorPool = descriptorPool,
                DescriptorSetCount = 2,
                SetLayouts = new IntPtr(setLayouts),
            };
            device.AllocateDescriptorSets(ref allocateInfo, descriptorSets);

            var bufferInfo = new DescriptorBufferInfo
            {
                Buffer = uniformBuffer,
                Range = Vulkan.WholeSize
            };

            var write = new WriteDescriptorSet
            {
                StructureType = StructureType.WriteDescriptorSet,
                DescriptorCount = 1,
                DestinationSet = descriptorSets[0],
                DestinationBinding = 0,
                DescriptorType = DescriptorType.UniformBuffer,
                BufferInfo = new IntPtr(&bufferInfo)
            };

            var copy = new CopyDescriptorSet
            {
                StructureType = StructureType.CopyDescriptorSet,
                DescriptorCount = 1,
                SourceBinding = 0,
                DestinationBinding = 0,
                SourceSet = descriptorSets[0],
                DestinationSet = descriptorSets[1]
            };

            device.UpdateDescriptorSets(1, &write, 0, null);
            device.UpdateDescriptorSets(0, null, 1, &copy);

            // Post-present transition
            var memoryBarrier = new ImageMemoryBarrier
            {
                StructureType = StructureType.ImageMemoryBarrier,
                Image = backBuffers[currentBackBufferIndex],
                SubresourceRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout = ImageLayout.PresentSource,
                NewLayout = ImageLayout.ColorAttachmentOptimal,
                SourceAccessMask = AccessFlags.MemoryRead,
                DestinationAccessMask = AccessFlags.ColorAttachmentWrite
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.TopOfPipe, PipelineStageFlags.TopOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);

            // Clear render target
            var clearRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1);
            commandBuffer.ClearColorImage(backBuffers[currentBackBufferIndex], ImageLayout.TransferDestinationOptimal, new RawColor4(0, 0, 0, 1), 1, &clearRange);

            // Begin render pass
            var renderPassBeginInfo = new RenderPassBeginInfo
            {
                StructureType = StructureType.RenderPassBeginInfo,
                RenderPass = renderPass,
                Framebuffer = framebuffers[currentBackBufferIndex],
                RenderArea = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height),
            };
            commandBuffer.BeginRenderPass(ref renderPassBeginInfo, SubpassContents.Inline);

            // Bind pipeline
            commandBuffer.BindPipeline(PipelineBindPoint.Graphics, pipeline);

            // Bind descriptor sets
            commandBuffer.BindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, 1, descriptorSets + 1, 0, null);

            // Set viewport and scissor
            var viewport = new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height);
            commandBuffer.SetViewport(0, 1, &viewport);

            var scissor = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height);
            commandBuffer.SetScissor(0, 1, &scissor);

            // Bind vertex buffer
            var vertexBufferCopy = vertexBuffer;
            ulong offset = 0;
            commandBuffer.BindVertexBuffers(0, 1, &vertexBufferCopy, &offset);

            // Draw vertices
            commandBuffer.Draw(3, 1, 0, 0);

            // End render pass
            commandBuffer.EndRenderPass();

            // Pre-present transition
            memoryBarrier = new ImageMemoryBarrier
            {
                StructureType = StructureType.ImageMemoryBarrier,
                Image = backBuffers[currentBackBufferIndex],
                SubresourceRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout = ImageLayout.ColorAttachmentOptimal,
                NewLayout = ImageLayout.PresentSource,
                SourceAccessMask = AccessFlags.ColorAttachmentWrite,
                DestinationAccessMask = AccessFlags.MemoryRead
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.AllCommands, PipelineStageFlags.BottomOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }
Example #3
0
 public unsafe void UpdateDescriptorSets(uint descriptorWriteCount, WriteDescriptorSet* descriptorWrites, uint descriptorCopyCount, CopyDescriptorSet* descriptorCopies)
 {
     vkUpdateDescriptorSets(this, descriptorWriteCount, descriptorWrites, descriptorCopyCount, descriptorCopies);
 }
Example #4
0
        private void DrawInternal()
        {
            // Update descriptors
            var descriptorSets = stackalloc DescriptorSet[2];
            var setLayouts     = stackalloc DescriptorSetLayout[2];

            setLayouts[0] = setLayouts[1] = descriptorSetLayout;

            var allocateInfo = new DescriptorSetAllocateInfo
            {
                StructureType      = StructureType.DescriptorSetAllocateInfo,
                DescriptorPool     = descriptorPool,
                DescriptorSetCount = 2,
                SetLayouts         = new IntPtr(setLayouts),
            };

            device.AllocateDescriptorSets(ref allocateInfo, descriptorSets);

            var bufferInfo = new DescriptorBufferInfo
            {
                Buffer = uniformBuffer,
                Range  = Vulkan.WholeSize
            };

            var write = new WriteDescriptorSet
            {
                StructureType      = StructureType.WriteDescriptorSet,
                DescriptorCount    = 1,
                DestinationSet     = descriptorSets[0],
                DestinationBinding = 0,
                DescriptorType     = DescriptorType.UniformBuffer,
                BufferInfo         = new IntPtr(&bufferInfo)
            };

            var copy = new CopyDescriptorSet
            {
                StructureType      = StructureType.CopyDescriptorSet,
                DescriptorCount    = 1,
                SourceBinding      = 0,
                DestinationBinding = 0,
                SourceSet          = descriptorSets[0],
                DestinationSet     = descriptorSets[1]
            };

            device.UpdateDescriptorSets(1, &write, 0, null);
            device.UpdateDescriptorSets(0, null, 1, &copy);

            // Post-present transition
            var memoryBarrier = new ImageMemoryBarrier
            {
                StructureType         = StructureType.ImageMemoryBarrier,
                Image                 = backBuffers[currentBackBufferIndex],
                SubresourceRange      = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout             = ImageLayout.PresentSource,
                NewLayout             = ImageLayout.ColorAttachmentOptimal,
                SourceAccessMask      = AccessFlags.MemoryRead,
                DestinationAccessMask = AccessFlags.ColorAttachmentWrite
            };

            commandBuffer.PipelineBarrier(PipelineStageFlags.TopOfPipe, PipelineStageFlags.TopOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);

            // Clear render target
            var clearRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1);

            commandBuffer.ClearColorImage(backBuffers[currentBackBufferIndex], ImageLayout.TransferDestinationOptimal, new RawColor4(0, 0, 0, 1), 1, &clearRange);

            // Begin render pass
            var renderPassBeginInfo = new RenderPassBeginInfo
            {
                StructureType = StructureType.RenderPassBeginInfo,
                RenderPass    = renderPass,
                Framebuffer   = framebuffers[currentBackBufferIndex],
                RenderArea    = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height),
            };

            commandBuffer.BeginRenderPass(ref renderPassBeginInfo, SubpassContents.Inline);

            // Bind pipeline
            commandBuffer.BindPipeline(PipelineBindPoint.Graphics, pipeline);

            // Bind descriptor sets
            commandBuffer.BindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, 1, descriptorSets + 1, 0, null);

            // Set viewport and scissor
            var viewport = new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height);

            commandBuffer.SetViewport(0, 1, &viewport);

            var scissor = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height);

            commandBuffer.SetScissor(0, 1, &scissor);

            // Bind vertex buffer
            var   vertexBufferCopy = vertexBuffer;
            ulong offset           = 0;

            commandBuffer.BindVertexBuffers(0, 1, &vertexBufferCopy, &offset);

            // Draw vertices
            commandBuffer.Draw(3, 1, 0, 0);

            // End render pass
            commandBuffer.EndRenderPass();

            // Pre-present transition
            memoryBarrier = new ImageMemoryBarrier
            {
                StructureType         = StructureType.ImageMemoryBarrier,
                Image                 = backBuffers[currentBackBufferIndex],
                SubresourceRange      = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout             = ImageLayout.ColorAttachmentOptimal,
                NewLayout             = ImageLayout.PresentSource,
                SourceAccessMask      = AccessFlags.ColorAttachmentWrite,
                DestinationAccessMask = AccessFlags.MemoryRead
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.AllCommands, PipelineStageFlags.BottomOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }
Example #5
0
 internal static unsafe extern void vkUpdateDescriptorSets(Device device, uint descriptorWriteCount, WriteDescriptorSet* descriptorWrites, uint descriptorCopyCount, CopyDescriptorSet* descriptorCopies);