Пример #1
0
 internal Texture InitializeWithoutResources(TextureDescription description)
 {
     isNotOwningResources = true;
     return(InitializeFrom(description));
 }
Пример #2
0
 /// <summary>
 /// Creates a new 2D <see cref="Texture" />.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
 /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
 /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
 /// <param name="usage">The usage.</param>
 /// <returns>A new instance of 2D <see cref="Texture" /> class.</returns>
 public static Texture New2D(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default, TextureOptions options = TextureOptions.None)
 {
     return(new Texture(device).InitializeFrom(TextureDescription.New2D(width, height, mipCount, format, textureFlags, arraySize, usage, MultisampleCount.None, options)));
 }
Пример #3
0
 internal Texture InitializeFromPersistent(TextureDescription description, SharpVulkan.Image nativeImage)
 {
     NativeImage = nativeImage;
     return(InitializeFrom(description));
 }
Пример #4
0
 /// <summary>
 /// Creates a new 1D <see cref="Texture" /> with a single level of mipmap.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice" />.</param>
 /// <param name="width">The width.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="dataPtr">Data ptr</param>
 /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
 /// <param name="usage">The usage.</param>
 /// <returns>A new instance of 1D <see cref="Texture" /> class.</returns>
 /// <remarks>The first dimension of mipMapTextures describes the number of array (Texture Array), second dimension is the mipmap, the third is the texture data for a particular mipmap.</remarks>
 public static Texture New1D(GraphicsDevice device, int width, PixelFormat format, IntPtr dataPtr, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable)
 {
     return(New(device, TextureDescription.New1D(width, format, textureFlags, usage), new[] { new DataBox(dataPtr, 0, 0), }));
 }
Пример #5
0
        private unsafe void CreateBackBuffers()
        {
            // Create the texture object
            var backBufferDescription = new TextureDescription
            {
                ArraySize        = 1,
                Dimension        = TextureDimension.Texture2D,
                Height           = Description.BackBufferHeight,
                Width            = Description.BackBufferWidth,
                Depth            = 1,
                Flags            = TextureFlags.RenderTarget,
                Format           = Description.BackBufferFormat,
                MipLevels        = 1,
                MultisampleCount = MultisampleCount.None,
                Usage            = GraphicsResourceUsage.Default
            };

            backbuffer.InitializeWithoutResources(backBufferDescription);

            var createInfo = new ImageViewCreateInfo
            {
                StructureType    = StructureType.ImageViewCreateInfo,
                SubresourceRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                Format           = backbuffer.NativeFormat,
            };

            // We initialize swapchain images to PresentSource, since we swap them out while in this layout.
            backbuffer.NativeAccessMask = AccessFlags.MemoryRead;
            backbuffer.NativeLayout     = ImageLayout.PresentSource;

            var imageMemoryBarrier = new ImageMemoryBarrier
            {
                StructureType         = StructureType.ImageMemoryBarrier,
                SubresourceRange      = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout             = ImageLayout.Undefined,
                NewLayout             = ImageLayout.PresentSource,
                SourceAccessMask      = AccessFlags.None,
                DestinationAccessMask = AccessFlags.MemoryRead
            };

            var commandBuffer = GraphicsDevice.NativeCopyCommandBuffer;
            var beginInfo     = new CommandBufferBeginInfo {
                StructureType = StructureType.CommandBufferBeginInfo
            };

            commandBuffer.Begin(ref beginInfo);

            var buffers = GraphicsDevice.NativeDevice.GetSwapchainImages(swapChain);

            swapchainImages = new SwapChainImageInfo[buffers.Length];

            for (int i = 0; i < buffers.Length; i++)
            {
                // Create image views
                swapchainImages[i].NativeImage = createInfo.Image = buffers[i];
                swapchainImages[i].NativeColorAttachmentView = GraphicsDevice.NativeDevice.CreateImageView(ref createInfo);

                // Transition to default layout
                imageMemoryBarrier.Image = buffers[i];
                commandBuffer.PipelineBarrier(PipelineStageFlags.AllCommands, PipelineStageFlags.AllCommands, DependencyFlags.None, 0, null, 0, null, 1, &imageMemoryBarrier);
            }

            // Close and submit
            commandBuffer.End();

            var submitInfo = new SubmitInfo
            {
                StructureType      = StructureType.SubmitInfo,
                CommandBufferCount = 1,
                CommandBuffers     = new IntPtr(&commandBuffer),
            };

            GraphicsDevice.NativeCommandQueue.Submit(1, &submitInfo, Fence.Null);
            GraphicsDevice.NativeCommandQueue.WaitIdle();
            commandBuffer.Reset(CommandBufferResetFlags.None);

            // Get next image
            currentBufferIndex = GraphicsDevice.NativeDevice.AcquireNextImage(swapChain, ulong.MaxValue, GraphicsDevice.GetNextPresentSemaphore(), Fence.Null);

            // Apply the first swap chain image to the texture
            backbuffer.SetNativeHandles(swapchainImages[currentBufferIndex].NativeImage, swapchainImages[currentBufferIndex].NativeColorAttachmentView);
        }
Пример #6
0
 /// <summary>
 /// Creates a new 1D <see cref="Texture"/>.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="width">The width.</param>
 /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="usage">The usage.</param>
 /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
 /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
 /// <returns>
 /// A new instance of 1D <see cref="Texture"/> class.
 /// </returns>
 public static Texture New1D(GraphicsDevice device, int width, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
 {
     return(New(device, TextureDescription.New1D(width, mipCount, format, textureFlags, arraySize, usage)));
 }
Пример #7
0
 /// <summary>
 /// Creates a new 1D <see cref="Texture" /> with a single level of mipmap.
 /// </summary>
 /// <typeparam name="T">Type of the initial data to upload to the texture</typeparam>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="width">The width.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="usage">The usage.</param>
 /// <param name="textureData">Texture data. Size of must be equal to sizeof(Format) * width </param>
 /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
 /// <returns>A new instance of <see cref="Texture" /> class.</returns>
 /// <remarks>
 /// The first dimension of mipMapTextures describes the number of array (Texture Array), second dimension is the mipmap, the third is the texture data for a particular mipmap.
 /// </remarks>
 public unsafe static Texture New1D <T>(GraphicsDevice device, int width, PixelFormat format, T[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Immutable) where T : struct
 {
     return(New(device, TextureDescription.New1D(width, format, textureFlags, usage), new[] { GetDataBox(format, width, 1, 1, textureData, (IntPtr)Interop.Fixed(textureData)) }));
 }
Пример #8
0
        private unsafe void CreateBackBuffers()
        {
            backbuffer.OnDestroyed();

            // Create the texture object
            var backBufferDescription = new TextureDescription
            {
                ArraySize        = 1,
                Dimension        = TextureDimension.Texture2D,
                Height           = Description.BackBufferHeight,
                Width            = Description.BackBufferWidth,
                Depth            = 1,
                Flags            = TextureFlags.RenderTarget,
                Format           = Description.BackBufferFormat,
                MipLevels        = 1,
                MultisampleCount = MultisampleCount.None,
                Usage            = GraphicsResourceUsage.Default
            };

            backbuffer.InitializeWithoutResources(backBufferDescription);

            var createInfo = new VkImageViewCreateInfo
            {
                sType            = VkStructureType.ImageViewCreateInfo,
                subresourceRange = new VkImageSubresourceRange(VkImageAspectFlags.Color, 0, 1, 0, 1),
                format           = backbuffer.NativeFormat,
                viewType         = VkImageViewType.Image2D,
            };

            // We initialize swapchain images to PresentSource, since we swap them out while in this layout.
            backbuffer.NativeAccessMask = VkAccessFlags.MemoryRead;
            backbuffer.NativeLayout     = VkImageLayout.PresentSrcKHR;

            var imageMemoryBarrier = new VkImageMemoryBarrier
            {
                sType            = VkStructureType.ImageMemoryBarrier,
                subresourceRange = new VkImageSubresourceRange(VkImageAspectFlags.Color, 0, 1, 0, 1),
                oldLayout        = VkImageLayout.Undefined,
                newLayout        = VkImageLayout.PresentSrcKHR,
                srcAccessMask    = VkAccessFlags.None,
                dstAccessMask    = VkAccessFlags.MemoryRead
            };

            var commandBuffer = GraphicsDevice.NativeCopyCommandBuffer;
            var beginInfo     = new VkCommandBufferBeginInfo {
                sType = VkStructureType.CommandBufferBeginInfo
            };

            vkBeginCommandBuffer(commandBuffer, &beginInfo);

            var buffers = vkGetSwapchainImagesKHR(GraphicsDevice.NativeDevice, swapChain);

            swapchainImages = new SwapChainImageInfo[buffers.Length];

            for (int i = 0; i < buffers.Length; i++)
            {
                // Create image views
                swapchainImages[i].NativeImage = createInfo.image = buffers[i];
                vkCreateImageView(GraphicsDevice.NativeDevice, &createInfo, null, out swapchainImages[i].NativeColorAttachmentView);

                // Transition to default layout
                imageMemoryBarrier.image = buffers[i];
                vkCmdPipelineBarrier(commandBuffer, VkPipelineStageFlags.AllCommands, VkPipelineStageFlags.AllCommands, VkDependencyFlags.None, 0, null, 0, null, 1, &imageMemoryBarrier);
            }

            // Close and submit
            vkEndCommandBuffer(commandBuffer);

            var submitInfo = new VkSubmitInfo
            {
                sType = VkStructureType.SubmitInfo,
                commandBufferCount = 1,
                pCommandBuffers    = &commandBuffer,
            };

            vkQueueSubmit(GraphicsDevice.NativeCommandQueue, 1, &submitInfo, VkFence.Null);
            vkQueueWaitIdle(GraphicsDevice.NativeCommandQueue);
            vkResetCommandBuffer(commandBuffer, VkCommandBufferResetFlags.None);

            vkAcquireNextImageKHR(GraphicsDevice.NativeDevice, swapChain, ulong.MaxValue, VkSemaphore.Null, VkFence.Null, out currentBufferIndex);

            // Apply the first swap chain image to the texture
            backbuffer.SetNativeHandles(swapchainImages[currentBufferIndex].NativeImage, swapchainImages[currentBufferIndex].NativeColorAttachmentView);
        }
Пример #9
0
 /// <summary>
 /// Creates a texture for output.
 /// </summary>
 /// <param name="description">The description.</param>
 /// <param name="viewFormat">The pixel format seen by the shader</param>
 /// <returns>Texture.</returns>
 protected virtual Texture CreateTexture(TextureDescription description, PixelFormat viewFormat)
 {
     return(Texture.New(GraphicsDevice, description));
 }
Пример #10
0
 /// <summary>
 /// Creates a new 3D <see cref="Texture"/>.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="depth">The depth.</param>
 /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="usage">The usage.</param>
 /// <param name="textureFlags">true if the texture needs to support unordered read write.</param>
 /// <returns>
 /// A new instance of 3D <see cref="Texture"/> class.
 /// </returns>
 public static Texture New3D(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
 {
     return(new Texture(device).InitializeFrom(TextureDescription.New3D(width, height, depth, mipCount, format, textureFlags, usage)));
 }
Пример #11
0
 /// <summary>
 /// Creates a new Cube <see cref="Texture" />.
 /// </summary>
 /// <param name="device">The <see cref="GraphicsDevice" />.</param>
 /// <param name="size">The size (in pixels) of the top-level faces of the cube texture.</param>
 /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int &gt;=1 for a particular mipmap count.</param>
 /// <param name="format">Describes the format to use.</param>
 /// <param name="textureFlags">The texture flags.</param>
 /// <param name="usage">The usage.</param>
 /// <returns>A new instance of 2D <see cref="Texture" /> class.</returns>
 public static Texture NewCube(GraphicsDevice device, int size, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default)
 {
     return(new Texture(device).InitializeFrom(TextureDescription.NewCube(size, mipCount, format, textureFlags, usage)));
 }
Пример #12
0
 /// <summary>
 /// Gets a texture for the specified description.
 /// </summary>
 /// <param name="description">The description.</param>
 /// <returns>A texture</returns>
 public Texture GetTemporaryTexture(TextureDescription description)
 {
     return(GetTemporaryResource(textureCache, description, createTextureDelegate, getTextureDefinitionDelegate, PixelFormat.None));
 }