Exemple #1
0
        public static BufferWithMemory CreateBuffer(VkContext vulkan, Vk.DeviceSize size,
                                                    Vk.BufferUsageFlags usage, Vk.MemoryPropertyFlags memoryProps,
                                                    Vk.SharingMode sharingMode)
        {
            var bufferInfo = new Vk.BufferCreateInfo();

            bufferInfo.Size        = size;
            bufferInfo.Usage       = usage;
            bufferInfo.SharingMode = sharingMode;

            var container = new BufferWithMemory();

            container.Buffer = vulkan.Device.CreateBuffer(bufferInfo);

            var memoryReqs = vulkan.Device.GetBufferMemoryRequirements(container.Buffer);
            var allocInfo  = new Vk.MemoryAllocateInfo();

            allocInfo.AllocationSize  = memoryReqs.Size;
            allocInfo.MemoryTypeIndex = FindMemoryType(memoryReqs.MemoryTypeBits,
                                                       vulkan.PhysicalDevice, memoryProps);

            container.Memory = vulkan.Device.AllocateMemory(allocInfo);
            container.Bind(vulkan.Device, 0);

            return(container);
        }
Exemple #2
0
        private void createIndexBuffer()
        {
            var size          = Marshal.SizeOf(typeof(short)) * this.indices.Count;
            var transferUsage = Vk.BufferUsageFlags.TransferSrc;
            var indexUsage    = Vk.BufferUsageFlags.IndexBuffer
                                | Vk.BufferUsageFlags.TransferDst;
            var transferMemoryProps = Vk.MemoryPropertyFlags.DeviceLocal;
            var indexMemoryProps    = Vk.MemoryPropertyFlags.HostVisible
                                      | Vk.MemoryPropertyFlags.HostCoherent;
            var sharingMode = this.GetSharingMode();

            BufferWithMemory stagingBuffer;

            try {
                stagingBuffer = VkHelper.CreateBuffer(this, size, transferUsage,
                                                      transferMemoryProps, sharingMode);
            } catch (Vk.ResultException result) {
                throw new VkException("An error occurred while creating the staging buffer.", result);
            }

            IntPtr memory     = this.Device.MapMemory(stagingBuffer.Memory, 0, size);
            var    indexArray = this.indices.ToArray();

            MemoryManagement.ArrayToPtr <short>(indexArray, memory, false);
            this.Device.UnmapMemory(stagingBuffer.Memory);

            try {
                BufferWithMemory indexBuffer = VkHelper.CreateBuffer(this, size, indexUsage,
                                                                     indexMemoryProps, sharingMode);
                this.vkIndexBuffer       = indexBuffer.Buffer;
                this.vkIndexBufferMemory = indexBuffer.Memory;
            } catch (Vk.ResultException result) {
                throw new VkException("An error occurred while creating the index buffer.", result);
            }

            VkHelper.CopyBuffer(stagingBuffer.Buffer, this.vkIndexBuffer, size, this);

            stagingBuffer.Destroy(this.Device);
        }