Use "cached", not "coherent" for staging buffers

This commit is contained in:
Stephen Seo 2024-03-26 12:21:40 +09:00
parent 2d47b7f892
commit 3a97f14199
2 changed files with 40 additions and 4 deletions

View file

@ -157,6 +157,34 @@ void dither::internal::vulkan_copy_buffer(VkDevice device,
vkFreeCommandBuffers(device, command_pool, 1, &command_buf); vkFreeCommandBuffers(device, command_pool, 1, &command_buf);
} }
void dither::internal::vulkan_flush_buffer(VkDevice device,
VkDeviceMemory memory) {
VkMappedMemoryRange range{};
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range.pNext = nullptr;
range.memory = memory;
range.offset = 0;
range.size = VK_WHOLE_SIZE;
if (vkFlushMappedMemoryRanges(device, 1, &range) != VK_SUCCESS) {
std::clog << "WARNING: vulkan_flush_buffer failed!\n";
}
}
void dither::internal::vulkan_invalidate_buffer(VkDevice device,
VkDeviceMemory memory) {
VkMappedMemoryRange range{};
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range.pNext = nullptr;
range.memory = memory;
range.offset = 0;
range.size = VK_WHOLE_SIZE;
if (vkInvalidateMappedMemoryRanges(device, 1, &range) != VK_SUCCESS) {
std::clog << "WARNING: vulkan_invalidate_buffer failed!\n";
}
}
std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl( std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
VkDevice device, VkPhysicalDevice phys_device, VkDevice device, VkPhysicalDevice phys_device,
VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue, VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue,
@ -178,7 +206,7 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
if (!internal::vulkan_create_buffer(device, phys_device, size * sizeof(int), if (!internal::vulkan_create_buffer(device, phys_device, size * sizeof(int),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
staging_pbp_buffer, staging_pbp_buffer,
staging_pbp_buffer_mem)) { staging_pbp_buffer_mem)) {
std::clog << "get_filter ERROR: Failed to create staging pbp buffer!\n"; std::clog << "get_filter ERROR: Failed to create staging pbp buffer!\n";
@ -207,7 +235,7 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
if (!internal::vulkan_create_buffer(device, phys_device, size * sizeof(int), if (!internal::vulkan_create_buffer(device, phys_device, size * sizeof(int),
VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
staging_filter_buffer, staging_filter_buffer,
staging_filter_buffer_mem)) { staging_filter_buffer_mem)) {
std::clog << "get_filter ERROR: Failed to create staging pbp buffer!\n"; std::clog << "get_filter ERROR: Failed to create staging pbp buffer!\n";
@ -234,6 +262,7 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
pipeline, pipeline_layout, descriptor_set, pipeline, pipeline_layout, descriptor_set,
filter_out_buf, size, &pbp, &reversed_pbp, filter_out_buf, size, &pbp, &reversed_pbp,
global_size, pbp_mapped_int, staging_pbp_buffer, global_size, pbp_mapped_int, staging_pbp_buffer,
staging_pbp_buffer_mem, staging_filter_buffer_mem,
staging_filter_buffer]() -> bool { staging_filter_buffer]() -> bool {
vkResetCommandBuffer(command_buffer, 0); vkResetCommandBuffer(command_buffer, 0);
@ -245,6 +274,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
} }
} }
vulkan_flush_buffer(device, staging_pbp_buffer_mem);
// Copy pbp buffer. // Copy pbp buffer.
vulkan_copy_buffer(device, command_pool, queue, staging_pbp_buffer, pbp_buf, vulkan_copy_buffer(device, command_pool, queue, staging_pbp_buffer, pbp_buf,
size * sizeof(int)); size * sizeof(int));
@ -292,6 +323,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
vulkan_copy_buffer(device, command_pool, queue, filter_out_buf, vulkan_copy_buffer(device, command_pool, queue, filter_out_buf,
staging_filter_buffer, size * sizeof(float)); staging_filter_buffer, size * sizeof(float));
vulkan_flush_buffer(device, staging_filter_buffer_mem);
return true; return true;
}; };

View file

@ -60,6 +60,9 @@ void vulkan_copy_buffer(VkDevice device, VkCommandPool command_pool,
VkQueue queue, VkBuffer src_buf, VkBuffer dst_buf, VkQueue queue, VkBuffer src_buf, VkBuffer dst_buf,
VkDeviceSize size); VkDeviceSize size);
void vulkan_flush_buffer(VkDevice device, VkDeviceMemory memory);
void vulkan_invalidate_buffer(VkDevice device, VkDeviceMemory memory);
std::vector<unsigned int> blue_noise_vulkan_impl( std::vector<unsigned int> blue_noise_vulkan_impl(
VkDevice device, VkPhysicalDevice phys_device, VkDevice device, VkPhysicalDevice phys_device,
VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue, VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue,