Compare commits

..

No commits in common. "6aaef83ca94999c37bb75c22f267c9d8da985dc5" and "47ba03337b630366a6ec1b6077fedee6ca9cbc8b" have entirely different histories.

2 changed files with 110 additions and 150 deletions

View file

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

View file

@ -60,17 +60,12 @@ 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, VkCommandBuffer command_buffer, VkQueue queue, VkDevice device, VkPhysicalDevice phys_device,
VkPipeline pipeline, VkPipelineLayout pipeline_layout, VkCommandBuffer command_buffer, VkCommandPool command_pool, VkQueue queue,
VkDescriptorSet descriptor_set, float *filter_out_mapped, int *pbp_mapped, VkBuffer pbp_buf, VkPipeline pipeline, VkPipelineLayout pipeline_layout,
VkDeviceMemory filter_out_buf_mem, VkDeviceMemory pbp_buf_mem, VkDescriptorSet descriptor_set, VkBuffer filter_out_buf, const int width,
const int width, const int height); const int height);
std::vector<float> vulkan_buf_to_vec(float *mapped, unsigned int size);
#endif #endif
#if DITHERING_OPENCL_ENABLED == 1 #if DITHERING_OPENCL_ENABLED == 1
@ -305,42 +300,6 @@ inline std::pair<int, int> filter_minmax(const std::vector<float> &filter,
return {min_index, max_index}; 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) { inline std::pair<int, int> filter_abs_minmax(const std::vector<float> &filter) {
float min = std::numeric_limits<float>::infinity(); float min = std::numeric_limits<float>::infinity();
float max = -std::numeric_limits<float>::infinity(); float max = -std::numeric_limits<float>::infinity();