Compare commits

...

3 commits

Author SHA1 Message Date
Stephen Seo 6aaef83ca9 Cache vulkan buffers, remove staging buffers
At this point, runtime is almost the same as the OpenCL implementation.
2024-03-26 13:18:24 +09:00
Stephen Seo 3a97f14199 Use "cached", not "coherent" for staging buffers 2024-03-26 12:21:40 +09:00
Stephen Seo 2d47b7f892 Attempt to optimize vulkan compute 2024-03-25 16:53:42 +09:00
2 changed files with 152 additions and 112 deletions

View file

@ -157,12 +157,40 @@ void dither::internal::vulkan_copy_buffer(VkDevice device,
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(
VkDevice device, VkPhysicalDevice phys_device,
VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue,
VkBuffer pbp_buf, VkPipeline pipeline, VkPipelineLayout pipeline_layout,
VkDescriptorSet descriptor_set, VkBuffer filter_out_buf, const int width,
const int height) {
VkDevice device, VkCommandBuffer command_buffer, VkQueue queue,
VkPipeline pipeline, VkPipelineLayout pipeline_layout,
VkDescriptorSet descriptor_set, float *filter_out_mapped, int *pbp_mapped,
VkDeviceMemory filter_out_buf_mem, VkDeviceMemory pbp_buf_mem,
const int width, const int height) {
const int size = width * height;
const int pixel_count = size * 4 / 10;
const int local_size = 256;
@ -170,59 +198,24 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
(std::size_t)std::ceil((float)size / (float)local_size);
std::vector<bool> pbp = random_noise(size, pixel_count);
std::vector<int> pbp_i(pbp.size());
std::vector<float> filter(size);
bool reversed_pbp = false;
const auto get_filter = [device, phys_device, command_buffer, command_pool,
queue, pbp_buf, pipeline, pipeline_layout,
descriptor_set, filter_out_buf, size, &pbp, &pbp_i,
&reversed_pbp, global_size, &filter]() -> bool {
for (unsigned int i = 0; i < pbp.size(); ++i) {
if (reversed_pbp) {
pbp_i[i] = pbp[i] ? 0 : 1;
} else {
pbp_i[i] = pbp[i] ? 1 : 0;
}
}
const auto get_filter = [device, command_buffer, queue, pipeline,
pipeline_layout, descriptor_set, &pbp, &reversed_pbp,
global_size, filter_out_buf_mem, pbp_buf_mem,
pbp_mapped]() -> bool {
vkResetCommandBuffer(command_buffer, 0);
// Copy pbp buffer.
{
VkBuffer staging_buffer;
VkDeviceMemory staging_buffer_mem;
if (!internal::vulkan_create_buffer(
device, phys_device, size * sizeof(int),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
staging_buffer, staging_buffer_mem)) {
std::clog << "get_filter ERROR: Failed to create staging buffer!\n";
return false;
for (unsigned int i = 0; i < pbp.size(); ++i) {
if (reversed_pbp) {
pbp_mapped[i] = pbp[i] ? 0 : 1;
} else {
pbp_mapped[i] = pbp[i] ? 1 : 0;
}
utility::Cleanup cleanup_staging_buf(
[device](void *ptr) {
vkDestroyBuffer(device, *((VkBuffer *)ptr), nullptr);
},
&staging_buffer);
utility::Cleanup cleanup_staging_buf_mem(
[device](void *ptr) {
vkFreeMemory(device, *((VkDeviceMemory *)ptr), nullptr);
},
&staging_buffer_mem);
void *data_ptr;
vkMapMemory(device, staging_buffer_mem, 0, size * sizeof(int), 0,
&data_ptr);
std::memcpy(data_ptr, pbp_i.data(), size * sizeof(int));
vkUnmapMemory(device, staging_buffer_mem);
vulkan_copy_buffer(device, command_pool, queue, staging_buffer, pbp_buf,
size * sizeof(int));
}
vulkan_flush_buffer(device, pbp_buf_mem);
VkCommandBufferBeginInfo begin_info{};
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@ -262,40 +255,7 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
return false;
}
// Copy back filter_out buffer.
{
VkBuffer staging_buffer;
VkDeviceMemory staging_buffer_mem;
if (!internal::vulkan_create_buffer(
device, phys_device, size * sizeof(float),
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
staging_buffer, staging_buffer_mem)) {
std::clog << "get_filter ERROR: Failed to create staging buffer!\n";
return false;
}
utility::Cleanup cleanup_staging_buf(
[device](void *ptr) {
vkDestroyBuffer(device, *((VkBuffer *)ptr), nullptr);
},
&staging_buffer);
utility::Cleanup cleanup_staging_buf_mem(
[device](void *ptr) {
vkFreeMemory(device, *((VkDeviceMemory *)ptr), nullptr);
},
&staging_buffer_mem);
vulkan_copy_buffer(device, command_pool, queue, filter_out_buf,
staging_buffer, size * sizeof(float));
void *data_ptr;
vkMapMemory(device, staging_buffer_mem, 0, size * sizeof(float), 0,
&data_ptr);
std::memcpy(filter.data(), data_ptr, size * sizeof(float));
vkUnmapMemory(device, staging_buffer_mem);
}
vulkan_flush_buffer(device, filter_out_buf_mem);
return true;
};
@ -322,7 +282,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
std::cerr << "Vulkan: Failed to execute get_filter at start!\n";
} else {
#ifndef NDEBUG
internal::write_filter(filter, width, "filter_out_start.pgm");
internal::write_filter(vulkan_buf_to_vec(filter_out_mapped, size), width,
"filter_out_start.pgm");
#endif
}
@ -340,7 +301,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
}
int min, max;
std::tie(min, max) = internal::filter_minmax(filter, pbp);
std::tie(min, max) =
internal::filter_minmax_raw_array(filter_out_mapped, size, pbp);
pbp[max] = false;
@ -351,7 +313,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
// get second buffer's min
int second_min;
std::tie(second_min, std::ignore) = internal::filter_minmax(filter, pbp);
std::tie(second_min, std::ignore) =
internal::filter_minmax_raw_array(filter_out_mapped, size, pbp);
if (second_min == max) {
pbp[max] = true;
@ -383,7 +346,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
std::cerr << "Vulkan: Failed to execute do_filter (at end)\n";
} else {
#ifndef NDEBUG
internal::write_filter(filter, width, "filter_out_final.pgm");
internal::write_filter(vulkan_buf_to_vec(filter_out_mapped, size), width,
"filter_out_final.pgm");
FILE *blue_noise_image = fopen("blue_noise.pbm", "w");
fprintf(blue_noise_image, "P1\n%d %d\n", width, height);
for (int y = 0; y < height; ++y) {
@ -418,7 +382,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
std::cout << i << ' ';
#endif
get_filter();
std::tie(std::ignore, max) = internal::filter_minmax(filter, pbp);
std::tie(std::ignore, max) =
internal::filter_minmax_raw_array(filter_out_mapped, size, pbp);
pbp.at(max) = false;
dither_array.at(max) = i;
#ifndef NDEBUG
@ -441,7 +406,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
std::cout << i << ' ';
#endif
get_filter();
std::tie(min, std::ignore) = internal::filter_minmax(filter, pbp);
std::tie(min, std::ignore) =
internal::filter_minmax_raw_array(filter_out_mapped, size, pbp);
pbp.at(min) = true;
dither_array.at(min) = i;
#ifndef NDEBUG
@ -457,7 +423,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
image::Bl min_pixels = internal::rangeToBl(dither_array, width);
min_pixels.writeToFile(image::file_type::PNG, true, "da_mid_pixels.png");
get_filter();
internal::write_filter(filter, width, "filter_mid.pgm");
internal::write_filter(vulkan_buf_to_vec(filter_out_mapped, size), width,
"filter_mid.pgm");
image::Bl pbp_image = toBl(pbp, width);
pbp_image.writeToFile(image::file_type::PNG, true, "debug_pbp_mid.png");
}
@ -469,7 +436,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
std::cout << i << ' ';
#endif
get_filter();
std::tie(std::ignore, max) = internal::filter_minmax(filter, pbp);
std::tie(std::ignore, max) =
internal::filter_minmax_raw_array(filter_out_mapped, size, pbp);
pbp.at(max) = true;
dither_array.at(max) = i;
#ifndef NDEBUG
@ -485,7 +453,8 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
#ifndef NDEBUG
{
get_filter();
internal::write_filter(filter, width, "filter_after.pgm");
internal::write_filter(vulkan_buf_to_vec(filter_out_mapped, size), width,
"filter_after.pgm");
image::Bl pbp_image = toBl(pbp, width);
pbp_image.writeToFile(image::file_type::PNG, true, "debug_pbp_after.png");
}
@ -494,6 +463,15 @@ std::vector<unsigned int> dither::internal::blue_noise_vulkan_impl(
return dither_array;
}
std::vector<float> dither::internal::vulkan_buf_to_vec(float *mapped,
unsigned int size) {
std::vector<float> v(size);
std::memcpy(v.data(), mapped, size * sizeof(float));
return v;
}
#endif // DITHERING_VULKAN_ENABLED == 1
#include "image.hpp"
@ -964,11 +942,13 @@ image::Bl dither::blue_noise(int width, int height, int threads,
VkBuffer filter_out_buf;
VkDeviceMemory filter_out_buf_mem;
if (!internal::vulkan_create_buffer(device, phys_device, filter_out_size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
filter_out_buf, filter_out_buf_mem)) {
if (!internal::vulkan_create_buffer(
device, phys_device, filter_out_size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
filter_out_buf, filter_out_buf_mem)) {
std::clog << "WARNING: Failed to create filter_out buffer!\n";
goto ENDOF_VULKAN;
}
@ -982,14 +962,24 @@ image::Bl dither::blue_noise(int width, int height, int threads,
vkFreeMemory(device, *((VkDeviceMemory *)ptr), nullptr);
},
&filter_out_buf_mem);
void *filter_out_mapped;
vkMapMemory(device, filter_out_buf_mem, 0, filter_out_size, 0,
&filter_out_mapped);
utility::Cleanup cleanup_filter_out_mapped(
[device](void *ptr) {
vkUnmapMemory(device, *((VkDeviceMemory *)ptr));
},
&filter_out_buf_mem);
float *filter_out_mapped_float = (float *)filter_out_mapped;
VkBuffer pbp_buf;
VkDeviceMemory pbp_buf_mem;
if (!internal::vulkan_create_buffer(device, phys_device, pbp_size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
pbp_buf, pbp_buf_mem)) {
if (!internal::vulkan_create_buffer(
device, phys_device, pbp_size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
pbp_buf, pbp_buf_mem)) {
std::clog << "WARNING: Failed to create pbp buffer!\n";
goto ENDOF_VULKAN;
}
@ -1003,6 +993,14 @@ image::Bl dither::blue_noise(int width, int height, int threads,
vkFreeMemory(device, *((VkDeviceMemory *)ptr), nullptr);
},
&pbp_buf_mem);
void *pbp_mapped;
vkMapMemory(device, pbp_buf_mem, 0, pbp_size, 0, &pbp_mapped);
utility::Cleanup cleanup_pbp_mapped(
[device](void *ptr) {
vkUnmapMemory(device, *((VkDeviceMemory *)ptr));
},
&pbp_buf_mem);
int *pbp_mapped_int = (int *)pbp_mapped;
VkBuffer other_buf;
VkDeviceMemory other_buf_mem;
@ -1179,9 +1177,10 @@ image::Bl dither::blue_noise(int width, int height, int threads,
}
auto result = dither::internal::blue_noise_vulkan_impl(
device, phys_device, command_buffer, command_pool, compute_queue,
pbp_buf, compute_pipeline, compute_pipeline_layout,
compute_descriptor_set, filter_out_buf, width, height);
device, command_buffer, compute_queue, compute_pipeline,
compute_pipeline_layout, compute_descriptor_set,
filter_out_mapped_float, pbp_mapped_int, filter_out_buf_mem,
pbp_buf_mem, width, height);
if (!result.empty()) {
return internal::rangeToBl(result, width);
}

View file

@ -60,12 +60,17 @@ void vulkan_copy_buffer(VkDevice device, VkCommandPool command_pool,
VkQueue queue, VkBuffer src_buf, VkBuffer dst_buf,
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(
VkDevice device, VkPhysicalDevice phys_device,
VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue,
VkBuffer pbp_buf, VkPipeline pipeline, VkPipelineLayout pipeline_layout,
VkDescriptorSet descriptor_set, VkBuffer filter_out_buf, const int width,
const int height);
VkDevice device, VkCommandBuffer command_buffer, VkQueue queue,
VkPipeline pipeline, VkPipelineLayout pipeline_layout,
VkDescriptorSet descriptor_set, float *filter_out_mapped, int *pbp_mapped,
VkDeviceMemory filter_out_buf_mem, VkDeviceMemory pbp_buf_mem,
const int width, const int height);
std::vector<float> vulkan_buf_to_vec(float *mapped, unsigned int size);
#endif
#if DITHERING_OPENCL_ENABLED == 1
@ -300,6 +305,42 @@ inline std::pair<int, int> filter_minmax(const std::vector<float> &filter,
return {min_index, max_index};
}
inline std::pair<int, int> filter_minmax_raw_array(const float *const filter,
unsigned int size,
std::vector<bool> pbp) {
// ensure minority pixel is "true"
unsigned int count = 0;
for (bool value : pbp) {
if (value) {
++count;
}
}
if (count * 2 >= pbp.size()) {
// std::cout << "MINMAX flip\n"; // DEBUG
for (unsigned int i = 0; i < pbp.size(); ++i) {
pbp[i] = !pbp[i];
}
}
float min = std::numeric_limits<float>::infinity();
float max = -std::numeric_limits<float>::infinity();
int min_index = -1;
int max_index = -1;
for (unsigned int i = 0; i < size; ++i) {
if (!pbp[i] && filter[i] < min) {
min_index = i;
min = filter[i];
}
if (pbp[i] && filter[i] > max) {
max_index = i;
max = filter[i];
}
}
return {min_index, max_index};
}
inline std::pair<int, int> filter_abs_minmax(const std::vector<float> &filter) {
float min = std::numeric_limits<float>::infinity();
float max = -std::numeric_limits<float>::infinity();