BeRo1985 / pasvulkan

Vulkan header generator, OOP-style API wrapper, framework and prospective Vulkan-based game engine for Object Pascal
zlib License
193 stars 31 forks source link

Question about TpvVulkanBuffer #31

Closed johnhutch111 closed 1 year ago

johnhutch111 commented 1 year ago

Can a TpvVulkanBuffer keep its memory mapped (persistent mapping) rather than mapping and unmapping during an update? I want to use a TpvVulkanBuffer as a Uniform buffer being updated each frame. It is recommended that the buffer memory should stay mapped. Is it sensible to keep memory mapped??

BeRo1985 commented 1 year ago

Yes, it is possible:

TpvVulkanBuffer.Create(pvApplication.VulkanDevice,
                       SizeOf(TMyUniforms),
                       TVkBufferUsageFlags(VK_BUFFER_USAGE_TRANSFER_DST_BIT) or TVkBufferUsageFlags(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT),
                       TVkSharingMode(VK_SHARING_MODE_EXCLUSIVE),
                       [],
                       TVkMemoryPropertyFlags(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) or TVkMemoryPropertyFlags(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT),
                       TVkMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
                       0,
                       0,
                       0,
                       0,
                       0,
                       0,
                       [TpvVulkanBufferFlag.PersistentMapped]); // <= this

Then it is persistent mapped behind the scenes, where the PasVulkan memory manager API will handle it automatically then, so that you can still use the Upload function then, or even just the Update function, when it is ensured, that it is persistent mapped.

And for staging uploads/downloads, you can also look into pvApplication.VulkanDevice.MemoryStaging, since TpvVulkanDeviceMemoryStaging is using a pre-allocated staging buffer for all staging memory transfers for non-memory-mappable destinations (upload) or sources (download).

johnhutch111 commented 1 year ago

Hey thanks. Got it.