Exemplo n.º 1
0
 private void GetImageMemoryRequirements(
     SharpVulkan.Image image,
     out SharpVulkan.MemoryRequirements requirements,
     out bool prefersDedicatedAllocation,
     out bool requiresDedicatedAllocation)
 {
     if (Context.SupportsDedicatedAllocation)
     {
         var requirementsInfo = new SharpVulkan.Ext.ImageMemoryRequirementsInfo2 {
             StructureType = SharpVulkan.Ext.StructureType.ImageMemoryRequirementsInfo2,
             Image         = image
         };
         var dedicatedRequirements = new SharpVulkan.Ext.MemoryDedicatedRequirements {
             StructureType = SharpVulkan.Ext.StructureType.MemoryDedicatedRequirements
         };
         var requirements2 = new SharpVulkan.Ext.MemoryRequirements2 {
             StructureType = SharpVulkan.Ext.StructureType.MemoryRequirements2,
             Next          = new IntPtr(&dedicatedRequirements)
         };
         Context.VKExt.GetImageMemoryRequirements2(Context.Device, ref requirementsInfo, ref requirements2);
         requirements = requirements2.MemoryRequirements;
         prefersDedicatedAllocation  = dedicatedRequirements.PrefersDedicatedAllocation;
         requiresDedicatedAllocation = dedicatedRequirements.RequiresDedicatedAllocation;
     }
     else
     {
         Context.Device.GetImageMemoryRequirements(image, out requirements);
         prefersDedicatedAllocation  = false;
         requiresDedicatedAllocation = false;
     }
 }
Exemplo n.º 2
0
        private void CreateDepthStencilBuffer()
        {
            var formats = new[] {
                SharpVulkan.Format.D32SFloatS8UInt,
                SharpVulkan.Format.D24UNormS8UInt,
                SharpVulkan.Format.D16UNormS8UInt
            };

            depthStencilFormat = formats.First(format => {
                context.PhysicalDevice.GetFormatProperties(format, out var formatProperties);
                return((formatProperties.OptimalTilingFeatures & SharpVulkan.FormatFeatureFlags.DepthStencilAttachment) != 0);
            });
            var tiling     = SharpVulkan.ImageTiling.Optimal;
            var createInfo = new SharpVulkan.ImageCreateInfo {
                StructureType = SharpVulkan.StructureType.ImageCreateInfo,
                ImageType     = SharpVulkan.ImageType.Image2D,
                Usage         = SharpVulkan.ImageUsageFlags.DepthStencilAttachment,
                Format        = depthStencilFormat,
                Extent        = new SharpVulkan.Extent3D((uint)width, (uint)height, 1),
                MipLevels     = 1,
                ArrayLayers   = 1,
                Samples       = SharpVulkan.SampleCountFlags.Sample1,
                SharingMode   = SharpVulkan.SharingMode.Exclusive,
                Tiling        = tiling,
                InitialLayout = SharpVulkan.ImageLayout.Undefined
            };

            depthStencilBuffer = context.Device.CreateImage(ref createInfo);
            depthStencilMemory = context.MemoryAllocator.Allocate(depthStencilBuffer, SharpVulkan.MemoryPropertyFlags.DeviceLocal, tiling);
            var viewCreateInfo = new SharpVulkan.ImageViewCreateInfo {
                StructureType    = SharpVulkan.StructureType.ImageViewCreateInfo,
                ViewType         = SharpVulkan.ImageViewType.Image2D,
                Image            = depthStencilBuffer,
                Format           = depthStencilFormat,
                Components       = SharpVulkan.ComponentMapping.Identity,
                SubresourceRange = new SharpVulkan.ImageSubresourceRange(
                    SharpVulkan.ImageAspectFlags.Depth |
                    SharpVulkan.ImageAspectFlags.Stencil)
            };

            depthStencilView = context.Device.CreateImageView(ref viewCreateInfo);
            var memoryBarrier = new SharpVulkan.ImageMemoryBarrier {
                StructureType         = SharpVulkan.StructureType.ImageMemoryBarrier,
                Image                 = depthStencilBuffer,
                OldLayout             = SharpVulkan.ImageLayout.Undefined,
                NewLayout             = SharpVulkan.ImageLayout.DepthStencilAttachmentOptimal,
                SourceAccessMask      = SharpVulkan.AccessFlags.None,
                DestinationAccessMask = SharpVulkan.AccessFlags.DepthStencilAttachmentRead | SharpVulkan.AccessFlags.DepthStencilAttachmentWrite,
                SubresourceRange      = new SharpVulkan.ImageSubresourceRange(
                    SharpVulkan.ImageAspectFlags.Depth |
                    SharpVulkan.ImageAspectFlags.Stencil)
            };

            context.EndRenderPass();
            context.EnsureCommandBuffer();
            context.CommandBuffer.PipelineBarrier(SharpVulkan.PipelineStageFlags.TopOfPipe,
                                                  SharpVulkan.PipelineStageFlags.EarlyFragmentTests | SharpVulkan.PipelineStageFlags.LateFragmentTests,
                                                  SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }
Exemplo n.º 3
0
 public virtual void Dispose()
 {
     if (image != SharpVulkan.Image.Null)
     {
         context.Release(imageView);
         context.Release(image);
         context.Release(memory);
         image = SharpVulkan.Image.Null;
     }
     Disposed = true;
 }
Exemplo n.º 4
0
        public MemoryAlloc Allocate(SharpVulkan.Image image, SharpVulkan.MemoryPropertyFlags propertyFlags, SharpVulkan.ImageTiling tiling)
        {
            GetImageMemoryRequirements(image,
                                       out var requirements,
                                       out var prefersDedicated,
                                       out bool requiresDedicated);
            var dedicatedAllocateInfo = new SharpVulkan.Ext.MemoryDedicatedAllocateInfo {
                StructureType = SharpVulkan.Ext.StructureType.MemoryDedicatedAllocateInfo,
                Image         = image
            };
            var alloc = Allocate(
                requirements, &dedicatedAllocateInfo, prefersDedicated, requiresDedicated,
                propertyFlags, tiling == SharpVulkan.ImageTiling.Linear);

            Context.Device.BindImageMemory(image, alloc.Memory.Memory, alloc.Offset);
            return(alloc);
        }
Exemplo n.º 5
0
        private void Create(bool renderTarget)
        {
            var vkFormat = VulkanHelper.GetVKFormat(format);
            var usage    =
                SharpVulkan.ImageUsageFlags.TransferSource |
                SharpVulkan.ImageUsageFlags.TransferDestination |
                SharpVulkan.ImageUsageFlags.Sampled;

            if (renderTarget)
            {
                usage |= SharpVulkan.ImageUsageFlags.ColorAttachment;
            }
            var tiling          = SharpVulkan.ImageTiling.Optimal;
            var imageCreateInfo = new SharpVulkan.ImageCreateInfo {
                StructureType = SharpVulkan.StructureType.ImageCreateInfo,
                ImageType     = SharpVulkan.ImageType.Image2D,
                Usage         = usage,
                Format        = vkFormat,
                Extent        = new SharpVulkan.Extent3D((uint)width, (uint)height, 1),
                MipLevels     = (uint)levelCount,
                ArrayLayers   = 1,
                Samples       = SharpVulkan.SampleCountFlags.Sample1,
                SharingMode   = SharpVulkan.SharingMode.Exclusive,
                InitialLayout = SharpVulkan.ImageLayout.Undefined,
                Tiling        = tiling
            };

            image = context.Device.CreateImage(ref imageCreateInfo);
            var memoryPropertyFlags = SharpVulkan.MemoryPropertyFlags.DeviceLocal;

            if (IsPvrtc1Format(Format))
            {
                memoryPropertyFlags |= SharpVulkan.MemoryPropertyFlags.HostVisible | SharpVulkan.MemoryPropertyFlags.HostCoherent;
            }
            memory = context.MemoryAllocator.Allocate(image, memoryPropertyFlags, tiling);
            var viewCreateInfo = new SharpVulkan.ImageViewCreateInfo {
                StructureType    = SharpVulkan.StructureType.ImageViewCreateInfo,
                ViewType         = SharpVulkan.ImageViewType.Image2D,
                Image            = image,
                Format           = vkFormat,
                Components       = SharpVulkan.ComponentMapping.Identity,
                SubresourceRange = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color)
            };

            imageView = context.Device.CreateImageView(ref viewCreateInfo);
            var memoryBarrier = new SharpVulkan.ImageMemoryBarrier {
                StructureType         = SharpVulkan.StructureType.ImageMemoryBarrier,
                Image                 = image,
                OldLayout             = SharpVulkan.ImageLayout.Undefined,
                NewLayout             = SharpVulkan.ImageLayout.ShaderReadOnlyOptimal,
                SourceAccessMask      = SharpVulkan.AccessFlags.None,
                DestinationAccessMask = SharpVulkan.AccessFlags.ShaderRead,
                SubresourceRange      = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color)
            };

            context.EndRenderPass();
            context.EnsureCommandBuffer();
            context.CommandBuffer.PipelineBarrier(
                SharpVulkan.PipelineStageFlags.TopOfPipe, SharpVulkan.PipelineStageFlags.VertexShader | SharpVulkan.PipelineStageFlags.FragmentShader,
                SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }