Exemple #1
0
        private static void CreateBuffer(ulong size, ResourceType type, out VkBuffer buffer, out MemoryAllocation memory)
        {
            var gd = Core.Instance !.Graphics;

            // Create buffer handle
            VkBufferCreateInfo bci = new(
                flags : VkBufferCreateFlags.NoFlags,
                size : size,
                usage : GetUsageFlags(type),
                sharingMode : VkSharingMode.Exclusive
                );
            VulkanHandle <VkBuffer> handle;

            gd.VkDevice.CreateBuffer(&bci, null, &handle)
            .Throw("Failed to create buffer handle");
            buffer = new(handle, gd.VkDevice);

            // Allocate and bind buffer memory
            VkMemoryRequirements memreq;

            buffer.GetBufferMemoryRequirements(&memreq);
            memory = gd.Memory.AllocateDevice(memreq) ??
                     throw new Exception("Failed to allocate memory for buffer");
            buffer.BindBufferMemory(memory.Handle, memory.Offset);
        }
Exemple #2
0
        internal bool CanDestroyImmediately = false; // Default to normal delayed-destroy
        #endregion                                   // Fields

        /// <summary>
        /// Create and map a new host buffer with the given size in bytes.
        /// </summary>
        /// <param name="size">The buffer size, in bytes.</param>
        public HostBuffer(ulong size)
            : base(ResourceType.HostBuffer)
        {
            DataSize = size;

            // Create buffer
            VkBufferCreateInfo bci = new(
                flags : VkBufferCreateFlags.NoFlags,
                size : size,
                usage : VkBufferUsageFlags.TransferDst | VkBufferUsageFlags.TransferSrc,
                sharingMode : VkSharingMode.Exclusive
                );
            VulkanHandle <VkBuffer> handle;

            Graphics.VkDevice.CreateBuffer(&bci, null, &handle).Throw("Failed to create host buffer");
            Buffer = new(handle, Graphics.VkDevice);

            // Allocate/bind memory
            VkMemoryRequirements memreq;

            Buffer.GetBufferMemoryRequirements(&memreq);
            Memory = Graphics.Memory.AllocateUpload(memreq) ??
                     throw new Exception("Failed to allocate host buffer memory");
            Buffer.BindBufferMemory(Memory.Handle, Memory.Offset);

            // Map memory
            Memory.Map();
            _stream = new((byte *)Memory.DataPtr, 0, (long)DataSize, FileAccess.Write);
        }
Exemple #3
0
        public UniformPushBuffer(ulong size)
        {
            var gd = Core.Instance !.Graphics;

            Alignment = gd.VkDeviceInfo.Properties.Limits.MinUniformBufferOffsetAlignment;

            // Calculate size requirements
            FrameSize      = MathHelper.RoundUp(size, Alignment);
            FrameAlignment = FrameSize + PADDING;
            TotalSize      = FrameAlignment * GraphicsDevice.MAX_PARALLEL_FRAMES;

            // Create the buffer
            VkBufferCreateInfo bci = new(
                flags : VkBufferCreateFlags.NoFlags,
                size : TotalSize,
                usage : VkBufferUsageFlags.UniformBuffer,
                sharingMode : VkSharingMode.Exclusive,
                queueFamilyIndexCount : 0,
                queueFamilyIndices : null
                );
            VulkanHandle <VkBuffer> handle;

            gd.VkDevice.CreateBuffer(&bci, null, &handle).Throw("Failed to create uniform push buffer");
            Handle = new(handle, gd.VkDevice);

            // Allocate and bind buffer memory
            VkMemoryRequirements memreq;

            Handle.GetBufferMemoryRequirements(&memreq);
            Memory = gd.Memory.AllocateDynamic(memreq) ??
                     throw new Exception("Failed to allocate uniform push buffer memory");
            Handle.BindBufferMemory(Memory.Handle, Memory.Offset)
            .Throw("Failed to bind uniform push buffer memory");

            // Map memory
            DataPtr = (byte *)Memory.Map();
        }