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); }
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); }