/// <inheritdoc/> protected internal override void OnDestroyed() { if (ParentTexture != null || isNotOwningResources) { NativeImage = SharpVulkan.Image.Null; NativeMemory = DeviceMemory.Null; } if (!isNotOwningResources) { if (NativeMemory != DeviceMemory.Null) { GraphicsDevice.Collect(NativeMemory); NativeMemory = DeviceMemory.Null; SharedHandle = IntPtr.Zero; } if (NativeImage != SharpVulkan.Image.Null) { GraphicsDevice.Collect(NativeImage); NativeImage = SharpVulkan.Image.Null; } if (NativeBuffer != SharpVulkan.Buffer.Null) { GraphicsDevice.Collect(NativeBuffer); NativeBuffer = SharpVulkan.Buffer.Null; } if (NativeImageView != ImageView.Null) { GraphicsDevice.Collect(NativeImageView); NativeImageView = ImageView.Null; } if (NativeColorAttachmentView != ImageView.Null) { GraphicsDevice.Collect(NativeColorAttachmentView); NativeColorAttachmentView = ImageView.Null; } if (NativeDepthStencilView != ImageView.Null) { GraphicsDevice.Collect(NativeDepthStencilView); NativeDepthStencilView = ImageView.Null; } } base.OnDestroyed(); }
private void InitializeFromImpl(DataBox[] dataBoxes = null) { NativeFormat = VulkanConvertExtensions.ConvertPixelFormat(ViewFormat); HasStencil = IsStencilFormat(ViewFormat); NativeImageAspect = IsDepthStencil ? ImageAspectFlags.Depth : ImageAspectFlags.Color; if (HasStencil) { NativeImageAspect |= ImageAspectFlags.Stencil; } // For depth-stencil formats, automatically fall back to a supported one if (IsDepthStencil && HasStencil) { NativeFormat = GetFallbackDepthStencilFormat(GraphicsDevice, NativeFormat); } if (Usage == GraphicsResourceUsage.Staging) { if (NativeImage != SharpVulkan.Image.Null) { throw new InvalidOperationException(); } if (isNotOwningResources) { throw new InvalidOperationException(); } if (ParentTexture != null) { // Create only a view NativeBuffer = ParentTexture.NativeBuffer; NativeMemory = ParentTexture.NativeMemory; } else { CreateBuffer(); if (dataBoxes != null) { throw new InvalidOperationException(); } } } else { if (NativeImage != SharpVulkan.Image.Null) { throw new InvalidOperationException(); } NativeLayout = IsRenderTarget ? ImageLayout.ColorAttachmentOptimal : IsDepthStencil ? ImageLayout.DepthStencilAttachmentOptimal : IsShaderResource ? ImageLayout.ShaderReadOnlyOptimal : ImageLayout.General; if (NativeLayout == ImageLayout.TransferDestinationOptimal) { NativeAccessMask = AccessFlags.TransferRead; } if (NativeLayout == ImageLayout.ColorAttachmentOptimal) { NativeAccessMask = AccessFlags.ColorAttachmentWrite; } if (NativeLayout == ImageLayout.DepthStencilAttachmentOptimal) { NativeAccessMask = AccessFlags.DepthStencilAttachmentWrite; } if (NativeLayout == ImageLayout.ShaderReadOnlyOptimal) { NativeAccessMask = AccessFlags.ShaderRead | AccessFlags.InputAttachmentRead; } NativePipelineStageMask = IsRenderTarget ? PipelineStageFlags.ColorAttachmentOutput : IsDepthStencil ? PipelineStageFlags.ColorAttachmentOutput | PipelineStageFlags.EarlyFragmentTests | PipelineStageFlags.LateFragmentTests : IsShaderResource ? PipelineStageFlags.VertexInput | PipelineStageFlags.FragmentShader : PipelineStageFlags.None; if (ParentTexture != null) { // Create only a view NativeImage = ParentTexture.NativeImage; NativeMemory = ParentTexture.NativeMemory; } else { if (!isNotOwningResources) { CreateImage(); InitializeImage(dataBoxes); } } if (!isNotOwningResources) { NativeImageView = GetImageView(ViewType, ArraySlice, MipLevel); NativeColorAttachmentView = GetColorAttachmentView(ViewType, ArraySlice, MipLevel); NativeDepthStencilView = GetDepthStencilView(); } } }
internal void SetNativeHandles(SharpVulkan.Image image, ImageView attachmentView) { NativeImage = image; NativeColorAttachmentView = attachmentView; }
internal Texture InitializeFromPersistent(TextureDescription description, SharpVulkan.Image nativeImage) { NativeImage = nativeImage; return(InitializeFrom(description)); }
private unsafe void CreateImage() { // Create a new image var createInfo = new ImageCreateInfo { StructureType = StructureType.ImageCreateInfo, ArrayLayers = (uint)ArraySize, Extent = new Extent3D((uint)Width, (uint)Height, (uint)Depth), MipLevels = (uint)MipLevels, Samples = SampleCountFlags.Sample1, Format = NativeFormat, Flags = ImageCreateFlags.None, Tiling = ImageTiling.Optimal, InitialLayout = ImageLayout.Undefined }; switch (Dimension) { case TextureDimension.Texture1D: createInfo.ImageType = ImageType.Image1D; break; case TextureDimension.Texture2D: createInfo.ImageType = ImageType.Image2D; break; case TextureDimension.Texture3D: createInfo.ImageType = ImageType.Image3D; break; case TextureDimension.TextureCube: createInfo.ImageType = ImageType.Image2D; createInfo.Flags |= ImageCreateFlags.CubeCompatible; break; } // TODO VULKAN: Can we restrict more based on GraphicsResourceUsage? createInfo.Usage |= ImageUsageFlags.TransferSource | ImageUsageFlags.TransferDestination; if (IsRenderTarget) { createInfo.Usage |= ImageUsageFlags.ColorAttachment; } if (IsDepthStencil) { createInfo.Usage |= ImageUsageFlags.DepthStencilAttachment; } if (IsShaderResource) { createInfo.Usage |= ImageUsageFlags.Sampled; // TODO VULKAN: Input attachments } var memoryProperties = MemoryPropertyFlags.DeviceLocal; // Create native image // TODO: Multisampling, flags, usage, etc. NativeImage = GraphicsDevice.NativeDevice.CreateImage(ref createInfo); // Allocate and bind memory MemoryRequirements memoryRequirements; GraphicsDevice.NativeDevice.GetImageMemoryRequirements(NativeImage, out memoryRequirements); AllocateMemory(memoryProperties, memoryRequirements); if (NativeMemory != DeviceMemory.Null) { GraphicsDevice.NativeDevice.BindImageMemory(NativeImage, NativeMemory, 0); } }
private void InitializeFromImpl(DataBox[] dataBoxes = null) { NativeFormat = VulkanConvertExtensions.ConvertPixelFormat(ViewFormat); HasStencil = IsStencilFormat(ViewFormat); NativeImageAspect = IsDepthStencil ? ImageAspectFlags.Depth : ImageAspectFlags.Color; if (HasStencil) NativeImageAspect |= ImageAspectFlags.Stencil; // For depth-stencil formats, automatically fall back to a supported one if (IsDepthStencil && HasStencil) { NativeFormat = GetFallbackDepthStencilFormat(GraphicsDevice, NativeFormat); } if (Usage == GraphicsResourceUsage.Staging) { if (NativeImage != SharpVulkan.Image.Null) throw new InvalidOperationException(); if (isNotOwningResources) throw new InvalidOperationException(); if (ParentTexture != null) { // Create only a view NativeBuffer = ParentTexture.NativeBuffer; NativeMemory = ParentTexture.NativeMemory; } else { CreateBuffer(); if (dataBoxes != null) throw new InvalidOperationException(); } } else { if (NativeImage != SharpVulkan.Image.Null) throw new InvalidOperationException(); NativeLayout = IsRenderTarget ? ImageLayout.ColorAttachmentOptimal : IsDepthStencil ? ImageLayout.DepthStencilAttachmentOptimal : IsShaderResource ? ImageLayout.ShaderReadOnlyOptimal : ImageLayout.General; if (NativeLayout == ImageLayout.TransferDestinationOptimal) NativeAccessMask = AccessFlags.TransferRead; if (NativeLayout == ImageLayout.ColorAttachmentOptimal) NativeAccessMask = AccessFlags.ColorAttachmentWrite; if (NativeLayout == ImageLayout.DepthStencilAttachmentOptimal) NativeAccessMask = AccessFlags.DepthStencilAttachmentWrite; if (NativeLayout == ImageLayout.ShaderReadOnlyOptimal) NativeAccessMask = AccessFlags.ShaderRead | AccessFlags.InputAttachmentRead; NativePipelineStageMask = IsRenderTarget ? PipelineStageFlags.ColorAttachmentOutput : IsDepthStencil ? PipelineStageFlags.ColorAttachmentOutput | PipelineStageFlags.EarlyFragmentTests | PipelineStageFlags.LateFragmentTests : IsShaderResource ? PipelineStageFlags.VertexInput | PipelineStageFlags.FragmentShader : PipelineStageFlags.None; if (ParentTexture != null) { // Create only a view NativeImage = ParentTexture.NativeImage; NativeMemory = ParentTexture.NativeMemory; } else { if (!isNotOwningResources) { CreateImage(); InitializeImage(dataBoxes); } } if (!isNotOwningResources) { NativeImageView = GetImageView(ViewType, ArraySlice, MipLevel); NativeColorAttachmentView = GetColorAttachmentView(ViewType, ArraySlice, MipLevel); NativeDepthStencilView = GetDepthStencilView(); } } }
internal Texture InitializeFromPersistent(TextureDescription description, SharpVulkan.Image nativeImage) { NativeImage = nativeImage; return InitializeFrom(description); }
/// <inheritdoc/> protected internal override void OnDestroyed() { if (ParentTexture != null || isNotOwningResources) { NativeImage = SharpVulkan.Image.Null; NativeMemory = DeviceMemory.Null; } if (!isNotOwningResources) { if (NativeMemory != DeviceMemory.Null) { GraphicsDevice.Collect(NativeMemory); NativeMemory = DeviceMemory.Null; } if (NativeImage != SharpVulkan.Image.Null) { GraphicsDevice.Collect(NativeImage); NativeImage = SharpVulkan.Image.Null; } if (NativeBuffer != SharpVulkan.Buffer.Null) { GraphicsDevice.Collect(NativeBuffer); NativeBuffer = SharpVulkan.Buffer.Null; } if (NativeImageView != ImageView.Null) { GraphicsDevice.Collect(NativeImageView); NativeImageView = ImageView.Null; } if (NativeColorAttachmentView != ImageView.Null) { GraphicsDevice.Collect(NativeColorAttachmentView); NativeColorAttachmentView = ImageView.Null; } if (NativeDepthStencilView != ImageView.Null) { GraphicsDevice.Collect(NativeDepthStencilView); NativeDepthStencilView = ImageView.Null; } } base.OnDestroyed(); }
private unsafe void CreateImage() { // Create a new image var createInfo = new ImageCreateInfo { StructureType = StructureType.ImageCreateInfo, ArrayLayers = (uint)ArraySize, Extent = new Extent3D((uint)Width, (uint)Height, (uint)Depth), MipLevels = (uint)MipLevels, Samples = SampleCountFlags.Sample1, Format = NativeFormat, Flags = ImageCreateFlags.None, Tiling = ImageTiling.Optimal, InitialLayout = ImageLayout.Undefined }; switch (Dimension) { case TextureDimension.Texture1D: createInfo.ImageType = ImageType.Image1D; break; case TextureDimension.Texture2D: createInfo.ImageType = ImageType.Image2D; break; case TextureDimension.Texture3D: createInfo.ImageType = ImageType.Image3D; break; case TextureDimension.TextureCube: createInfo.ImageType = ImageType.Image2D; createInfo.Flags |= ImageCreateFlags.CubeCompatible; break; } // TODO VULKAN: Can we restrict more based on GraphicsResourceUsage? createInfo.Usage |= ImageUsageFlags.TransferSource | ImageUsageFlags.TransferDestination; if (IsRenderTarget) createInfo.Usage |= ImageUsageFlags.ColorAttachment; if (IsDepthStencil) createInfo.Usage |= ImageUsageFlags.DepthStencilAttachment; if (IsShaderResource) createInfo.Usage |= ImageUsageFlags.Sampled; // TODO VULKAN: Input attachments var memoryProperties = MemoryPropertyFlags.DeviceLocal; // Create native image // TODO: Multisampling, flags, usage, etc. NativeImage = GraphicsDevice.NativeDevice.CreateImage(ref createInfo); // Allocate and bind memory MemoryRequirements memoryRequirements; GraphicsDevice.NativeDevice.GetImageMemoryRequirements(NativeImage, out memoryRequirements); AllocateMemory(memoryProperties, memoryRequirements); if (NativeMemory != DeviceMemory.Null) { GraphicsDevice.NativeDevice.BindImageMemory(NativeImage, NativeMemory, 0); } }