private void CreateBuffer(ulong size)
        {
            var createInfo = new SharpVulkan.BufferCreateInfo {
                StructureType = SharpVulkan.StructureType.BufferCreateInfo,
                Size          = size,
                SharingMode   = SharpVulkan.SharingMode.Exclusive,
                Usage         = SharpVulkan.BufferUsageFlags.TransferSource
            };

            buffer = context.Device.CreateBuffer(ref createInfo);
            memory = context.MemoryAllocator.Allocate(buffer,
                                                      SharpVulkan.MemoryPropertyFlags.HostVisible | SharpVulkan.MemoryPropertyFlags.HostCoherent);
            mappedMemory = context.MemoryAllocator.Map(memory);
            bufferSize   = size;
            bufferOffset = 0;
        }
Exemple #2
0
        private void FlushOrInvalidateMappedMemoryRange(MemoryAlloc alloc, ulong offset, ulong size, bool flush)
        {
            if (alloc.Allocator != this)
            {
                throw new ArgumentException(nameof(alloc));
            }
            if (alloc.Size < offset + size)
            {
                throw new ArgumentException();
            }
            if (size == 0)
            {
                return;
            }
            var memoryType   = alloc.Memory.Type;
            var hostCoherent = (memoryType.PropertyFlags & SharpVulkan.MemoryPropertyFlags.HostCoherent) != 0;

            if (hostCoherent)
            {
                return;
            }
            var nonCoherentAtomSize = Context.PhysicalDeviceLimits.NonCoherentAtomSize;
            var rangeStart          = GraphicsUtility.AlignDown(alloc.Offset + offset, nonCoherentAtomSize);
            var rangeEnd            = GraphicsUtility.AlignUp(alloc.Offset + offset + size, nonCoherentAtomSize);

            if (rangeEnd > alloc.Memory.Size)
            {
                rangeEnd = alloc.Memory.Size;
            }
            var range = new SharpVulkan.MappedMemoryRange {
                StructureType = SharpVulkan.StructureType.MappedMemoryRange,
                Memory        = alloc.Memory.Memory,
                Offset        = rangeStart,
                Size          = rangeEnd - rangeStart
            };

            if (flush)
            {
                Context.Device.FlushMappedMemoryRanges(1, &range);
            }
            else
            {
                Context.Device.InvalidateMappedMemoryRanges(1, &range);
            }
        }
Exemple #3
0
        private void CreateBuffer()
        {
            var alignedSliceSize = GraphicsUtility.AlignUp(sliceSize, sliceAlignment);
            var bufferSize       = sliceCount * alignedSliceSize;
            var createInfo       = new SharpVulkan.BufferCreateInfo {
                StructureType = SharpVulkan.StructureType.BufferCreateInfo,
                Size          = bufferSize,
                SharingMode   = SharpVulkan.SharingMode.Exclusive,
                Usage         = usage
            };

            buffer = context.Device.CreateBuffer(ref createInfo);
            memory = context.MemoryAllocator.Allocate(buffer, memoryPropertyFlags);
            sliceQueue.Clear();
            for (ulong i = 0; i < sliceCount; i++)
            {
                sliceQueue.Enqueue(new SliceEntry {
                    Offset = i * alignedSliceSize
                });
            }
            Generation++;
        }
Exemple #4
0
        public void Free(MemoryAlloc alloc)
        {
            if (alloc == null || alloc.Allocator == null)
            {
                return;
            }
            if (alloc.Allocator != this)
            {
                throw new ArgumentException(nameof(alloc));
            }
            var block = alloc.MemoryBlock;

            if (block != null)
            {
                block.Free(alloc.MemoryBlockNode);
            }
            else
            {
                FreeDeviceMemory(alloc.Memory);
            }
            alloc.Allocator = null;
        }
Exemple #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);
        }
Exemple #6
0
 public void InvalidateMappedMemoryRange(MemoryAlloc alloc, ulong offset, ulong size)
 {
     FlushOrInvalidateMappedMemoryRange(alloc, offset, size, flush: false);
 }