Example #1
0
        public static unsafe void InsertBufferBarrier(
            VulkanRenderer gd,
            CommandBuffer commandBuffer,
            VkBuffer buffer,
            AccessFlags srcAccessMask,
            AccessFlags dstAccessMask,
            PipelineStageFlags srcStageMask,
            PipelineStageFlags dstStageMask,
            int offset,
            int size)
        {
            BufferMemoryBarrier memoryBarrier = new BufferMemoryBarrier()
            {
                SType               = StructureType.BufferMemoryBarrier,
                SrcAccessMask       = srcAccessMask,
                DstAccessMask       = dstAccessMask,
                SrcQueueFamilyIndex = Vk.QueueFamilyIgnored,
                DstQueueFamilyIndex = Vk.QueueFamilyIgnored,
                Buffer              = buffer,
                Offset              = (ulong)offset,
                Size = (ulong)size
            };

            gd.Api.CmdPipelineBarrier(
                commandBuffer,
                srcStageMask,
                dstStageMask,
                0,
                0,
                null,
                1,
                memoryBarrier,
                0,
                null);
        }
Example #2
0
        private void CopyFromOrToBuffer(
            CommandBuffer commandBuffer,
            VkBuffer buffer,
            Image image,
            int size,
            bool to,
            int x,
            int y,
            int width,
            int height)
        {
            var aspectFlags = Info.Format.ConvertAspectFlags();

            if (aspectFlags == (ImageAspectFlags.ImageAspectDepthBit | ImageAspectFlags.ImageAspectStencilBit))
            {
                aspectFlags = ImageAspectFlags.ImageAspectDepthBit;
            }

            var sl = new ImageSubresourceLayers(aspectFlags, (uint)FirstLevel, (uint)FirstLayer, 1);

            var extent = new Extent3D((uint)width, (uint)height, 1);

            var region = new BufferImageCopy(0, (uint)width, (uint)height, sl, new Offset3D(x, y, 0), extent);

            if (to)
            {
                _gd.Api.CmdCopyImageToBuffer(commandBuffer, image, ImageLayout.General, buffer, 1, region);
            }
            else
            {
                _gd.Api.CmdCopyBufferToImage(commandBuffer, buffer, image, ImageLayout.General, 1, region);
            }
        }
Example #3
0
 public BufferHolder(VulkanRenderer gd, Device device, VkBuffer buffer, MemoryAllocation allocation, int size)
 {
     _gd             = gd;
     _device         = device;
     _allocation     = allocation;
     _allocationAuto = new Auto <MemoryAllocation>(allocation);
     _waitable       = new MultiFenceHolder(size);
     _buffer         = new Auto <DisposableBuffer>(new DisposableBuffer(gd.Api, device, buffer), _waitable, _allocationAuto);
     _bufferHandle   = buffer.Handle;
     Size            = size;
     _map            = allocation.HostPointer;
 }
Example #4
0
        private void CopyFromOrToBuffer(
            CommandBuffer commandBuffer,
            VkBuffer buffer,
            Image image,
            int size,
            bool to,
            int dstLayer,
            int dstLevel,
            int x,
            int y,
            int width,
            int height)
        {
            var aspectFlags = Info.Format.ConvertAspectFlags();

            if (aspectFlags == (ImageAspectFlags.ImageAspectDepthBit | ImageAspectFlags.ImageAspectStencilBit))
            {
                aspectFlags = ImageAspectFlags.ImageAspectDepthBit;
            }

            var sl = new ImageSubresourceLayers(aspectFlags, (uint)(FirstLevel + dstLevel), (uint)(FirstLayer + dstLayer), 1);

            var extent = new Extent3D((uint)width, (uint)height, 1);

            int rowLengthAlignment = Info.BlockWidth;

            // We expect all data being written into the texture to have a stride aligned by 4.
            if (!to && Info.BytesPerPixel < 4)
            {
                rowLengthAlignment = 4 / Info.BytesPerPixel;
            }

            var region = new BufferImageCopy(
                0,
                (uint)AlignUpNpot(width, rowLengthAlignment),
                (uint)AlignUpNpot(height, Info.BlockHeight),
                sl,
                new Offset3D(x, y, 0),
                extent);

            if (to)
            {
                _gd.Api.CmdCopyImageToBuffer(commandBuffer, image, ImageLayout.General, buffer, 1, region);
            }
            else
            {
                _gd.Api.CmdCopyBufferToImage(commandBuffer, buffer, image, ImageLayout.General, 1, region);
            }
        }
        private unsafe void CreateHostBufferWithContent <T>(ReadOnlySpan <T> span, out Buffer buffer, out Allocation alloc) where T : unmanaged
        {
            BufferCreateInfo bufferInfo = new(
                usage : BufferUsageFlags.BufferUsageTransferSrcBit,
                size : (uint)Unsafe.SizeOf <T>() * (uint)span.Length);

            AllocationCreateInfo allocInfo = new(AllocationCreateFlags.Mapped, usage : MemoryUsage.CPU_Only);

            buffer = Allocator.CreateBuffer(in bufferInfo, in allocInfo, out alloc);

            if (!alloc.TryGetSpan(out Span <T> bufferSpan))
            {
                throw new InvalidOperationException("Unable to get Span<T> to mapped allocation.");
            }

            span.CopyTo(bufferSpan);
        }
        private unsafe void CreateUniformBuffer() //Simpler setup from the Vertex buffer because there is no staging or device copying
        {
            BufferCreateInfo bufferInfo = new BufferCreateInfo
            {
                SType       = StructureType.BufferCreateInfo,
                Size        = this.UniformBufferSize,
                Usage       = BufferUsageFlags.BufferUsageUniformBufferBit,
                SharingMode = SharingMode.Exclusive
            };

            // Allow this to be updated every frame
            var allocInfo = new AllocationCreateInfo(
                usage: MemoryUsage.CPU_To_GPU,
                requiredFlags: MemoryPropertyFlags.MemoryPropertyHostVisibleBit);

            // Binds buffer to allocation for you
            var buffer = this.Allocator.CreateBuffer(in bufferInfo, in allocInfo, out var allocation);

            // Camera/MVP Matrix calculation
            Camera.LookAt(new Vector3(2f, 2f, -5f), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            var radFov = MathF.PI / 180f * 45f;
            var aspect = (float)this.SwapchainExtent.Width / this.SwapchainExtent.Height;

            Camera.Perspective(radFov, aspect, 0.5f, 100f);

            Camera.UpdateMVP();

            allocation.Map();

            Matrix4x4 *ptr = (Matrix4x4 *)allocation.MappedData;

            ptr[0] = Camera.MVPMatrix;   // Camera Matrix
            ptr[1] = Matrix4x4.Identity; // Model Matrix

            allocation.Unmap();

            this.UniformBuffer     = buffer;
            this.UniformAllocation = allocation;
        }
Example #7
0
 public DisposableBuffer(Vk api, Device device, Silk.NET.Vulkan.Buffer buffer)
 {
     _api    = api;
     _device = device;
     Value   = buffer;
 }
Example #8
0
        public void InitializeBuffers(int setIndex, int baseBinding, int countPerUnit, DescriptorType type, VkBuffer dummyBuffer)
        {
            Span <DescriptorBufferInfo> infos = stackalloc DescriptorBufferInfo[countPerUnit];

            infos.Fill(new DescriptorBufferInfo()
            {
                Buffer = dummyBuffer,
                Range  = Vk.WholeSize
            });

            UpdateBuffers(setIndex, baseBinding, infos, type);
        }
Example #9
0
        private void CopyFromOrToBuffer(
            CommandBuffer commandBuffer,
            VkBuffer buffer,
            Image image,
            int size,
            bool to,
            int dstLayer,
            int dstLevel,
            int dstLayers,
            int dstLevels,
            bool singleSlice)
        {
            bool is3D   = Info.Target == Target.Texture3D;
            int  width  = Info.Width;
            int  height = Info.Height;
            int  depth  = is3D && !singleSlice ? Info.Depth : 1;
            int  layer  = is3D ? 0 : dstLayer;
            int  layers = dstLayers;
            int  levels = dstLevels;

            int offset = 0;

            for (int level = 0; level < levels; level++)
            {
                int mipSize = GetBufferDataLength(Info.GetMipSize(level));

                int endOffset = offset + mipSize;

                if ((uint)endOffset > (uint)size)
                {
                    break;
                }

                int rowLength = (Info.GetMipStride(level) / Info.BytesPerPixel) * Info.BlockWidth;

                var aspectFlags = Info.Format.ConvertAspectFlags();

                if (aspectFlags == (ImageAspectFlags.ImageAspectDepthBit | ImageAspectFlags.ImageAspectStencilBit))
                {
                    aspectFlags = ImageAspectFlags.ImageAspectDepthBit;
                }

                var sl = new ImageSubresourceLayers(
                    aspectFlags,
                    (uint)(FirstLevel + dstLevel + level),
                    (uint)(FirstLayer + layer),
                    (uint)layers);

                var extent = new Extent3D((uint)width, (uint)height, (uint)depth);

                int z = is3D ? dstLayer : 0;

                var region = new BufferImageCopy((ulong)offset, (uint)rowLength, (uint)height, sl, new Offset3D(0, 0, z), extent);

                if (to)
                {
                    _gd.Api.CmdCopyImageToBuffer(commandBuffer, image, ImageLayout.General, buffer, 1, region);
                }
                else
                {
                    _gd.Api.CmdCopyBufferToImage(commandBuffer, buffer, image, ImageLayout.General, 1, region);
                }

                offset += mipSize;

                width  = Math.Max(1, width >> 1);
                height = Math.Max(1, height >> 1);

                if (Info.Target == Target.Texture3D)
                {
                    depth = Math.Max(1, depth >> 1);
                }
            }
        }
        private unsafe void CreateDeviceLocalBuffer(BufferUsageFlags usage, uint size, out Buffer buffer, out Allocation alloc)
        {
            BufferCreateInfo bufferInfo = new(
                usage : usage | BufferUsageFlags.BufferUsageTransferDstBit,
                size : size);

            AllocationCreateInfo allocInfo = new(usage : MemoryUsage.GPU_Only);

            buffer = Allocator.CreateBuffer(in bufferInfo, in allocInfo, out alloc);
        }