Beispiel #1
0
        public void Dispose()
        {
            ThrowIfDisposed();

            buffer.Dispose();
            memory.Free();
            disposed = true;
        }
Beispiel #2
0
 // ALWAYS call base.Dispose(disposing)
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         VkBuffer.Dispose();
         VkMemory?.Dispose();
     }
 }
Beispiel #3
0
 public void Dispose()
 {
     Fence?.Dispose();
     Memory.Dispose();
     Buffer.Dispose();
     if (WriteUsingStagingBuffer)
     {
         StagingMemory?.Dispose();
         StagingBuffer?.Dispose();
         StagingCommandBuffer?.Dispose();
     }
 }
Beispiel #4
0
        internal static VKBuffer Storage <T>(Context ctx, T[] data) where T : struct
        {
            long size = data.Length * Interop.SizeOf <T>();

            // Create a staging buffer that is writable by host.
            VulkanCore.Buffer  stagingBuffer = ctx.Device.CreateBuffer(new BufferCreateInfo(size, BufferUsages.TransferSrc));
            MemoryRequirements stagingReq    = stagingBuffer.GetMemoryRequirements();
            int stagingMemoryTypeIndex       = ctx.MemoryProperties.MemoryTypes.IndexOf(
                stagingReq.MemoryTypeBits,
                MemoryProperties.HostVisible | MemoryProperties.HostCoherent);
            DeviceMemory stagingMemory = ctx.Device.AllocateMemory(new MemoryAllocateInfo(stagingReq.Size, stagingMemoryTypeIndex));
            IntPtr       vertexPtr     = stagingMemory.Map(0, stagingReq.Size);

            Interop.Write(vertexPtr, data);
            stagingMemory.Unmap();
            stagingBuffer.BindMemory(stagingMemory);

            // Create a device local buffer where the data will be copied.
            VulkanCore.Buffer  buffer = ctx.Device.CreateBuffer(new BufferCreateInfo(size, BufferUsages.VertexBuffer | BufferUsages.StorageBuffer | BufferUsages.TransferDst));
            MemoryRequirements req    = buffer.GetMemoryRequirements();
            int memoryTypeIndex       = ctx.MemoryProperties.MemoryTypes.IndexOf(
                req.MemoryTypeBits,
                MemoryProperties.DeviceLocal);
            DeviceMemory memory = ctx.Device.AllocateMemory(new MemoryAllocateInfo(req.Size, memoryTypeIndex));

            buffer.BindMemory(memory);

            // Copy the data from staging buffers to device local buffers.
            CommandBuffer cmdBuffer = ctx.GraphicsCommandPool.AllocateBuffers(new CommandBufferAllocateInfo(CommandBufferLevel.Primary, 1))[0];

            cmdBuffer.Begin(new CommandBufferBeginInfo(CommandBufferUsages.OneTimeSubmit));
            cmdBuffer.CmdCopyBuffer(stagingBuffer, buffer, new BufferCopy(size));
            cmdBuffer.End();

            // Submit.
            Fence fence = ctx.Device.CreateFence();

            ctx.GraphicsQueue.Submit(new SubmitInfo(commandBuffers: new[] { cmdBuffer }), fence);
            fence.Wait();

            // Cleanup.
            fence.Dispose();
            cmdBuffer.Dispose();
            stagingBuffer.Dispose();
            stagingMemory.Dispose();

            return(new VKBuffer(ctx, buffer, memory, null, data.Length, size));
        }
 public void Cleanup()
 {
     /*
      * Clean up all Vulkan Resources.
      */
     bufferMemory.Dispose();
     buffer.Dispose();
     computeShaderModule.Dispose();
     descriptorPool.Dispose();
     descriptorSetLayout.Dispose();
     pipelineLayout.Dispose();
     foreach (Pipeline pipeline in pipelines)
     {
         pipeline.Dispose();
     }
     commandPool.Dispose();
     device.Dispose();
     instance.Dispose();
 }