Rename kernel using fns/vars with word "grayscale"

This commit is contained in:
Stephen Seo 2021-11-27 12:33:26 +09:00
parent 5ad6f9d035
commit b14f0a01b2
3 changed files with 62 additions and 46 deletions

View file

@ -9,7 +9,7 @@
#include <png.h> #include <png.h>
const char *Image::opencl_kernel_ = nullptr; const char *Image::opencl_grayscale_kernel_ = nullptr;
Image::Image() : data_(), width_(0), height_(0), is_grayscale_(true) {} Image::Image() : data_(), width_(0), height_(0), is_grayscale_(true) {}
@ -692,31 +692,36 @@ std::unique_ptr<Image> Image::ToGrayscale() const {
return grayscale_image; return grayscale_image;
} }
std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) { std::unique_ptr<Image> Image::ToGrayscaleDitheredWithBlueNoise(
Image *blue_noise) {
if (!blue_noise->IsGrayscale()) { if (!blue_noise->IsGrayscale()) {
std::cout << "ERROR ToDitheredWithBlueNoise: blue_noise is not grayscale" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: blue_noise is not grayscale"
<< std::endl;
return {}; return {};
} }
auto grayscale_image = ToGrayscale(); auto grayscale_image = ToGrayscale();
if (!grayscale_image) { if (!grayscale_image) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to get grayscale Image" std::cout << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to get "
"grayscale Image"
<< std::endl; << std::endl;
return {}; return {};
} }
auto opencl_handle = GetOpenCLHandle(); auto opencl_handle = GetOpenCLHandle();
if (!opencl_handle) { if (!opencl_handle) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to get OpenCLHandle" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to get OpenCLHandle"
<< std::endl;
return {}; return {};
} }
// set up kernel and buffers // set up kernel and buffers
auto kid = auto kid = opencl_handle->CreateKernelFromSource(
opencl_handle->CreateKernelFromSource(GetDitheringKernel(), "Dither"); GetGrayscaleDitheringKernel(), "Dither");
if (kid == 0) { if (kid == 0) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to create OpenCL Kernel" std::cout << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to create "
"OpenCL Kernel"
<< std::endl; << std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
@ -726,8 +731,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
kid, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, kid, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
grayscale_image->data_.size(), grayscale_image->data_.data()); grayscale_image->data_.size(), grayscale_image->data_.data());
if (input_buffer_id == 0) { if (input_buffer_id == 0) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set input buffer" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set input buffer"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -735,8 +741,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
auto output_buffer_id = opencl_handle->CreateKernelBuffer( auto output_buffer_id = opencl_handle->CreateKernelBuffer(
kid, CL_MEM_WRITE_ONLY, grayscale_image->data_.size(), nullptr); kid, CL_MEM_WRITE_ONLY, grayscale_image->data_.size(), nullptr);
if (output_buffer_id == 0) { if (output_buffer_id == 0) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set output buffer" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set output buffer"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -745,61 +752,68 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
kid, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, blue_noise->data_.size(), kid, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, blue_noise->data_.size(),
(void *)blue_noise->data_.data()); (void *)blue_noise->data_.data());
if (blue_noise_buffer_id == 0) { if (blue_noise_buffer_id == 0) {
std::cout std::cout << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set "
<< "ERROR ToDitheredWithBlueNoise: Failed to set blue-noise buffer" "blue-noise buffer"
<< std::endl; << std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
// assign buffers/data to kernel parameters // assign buffers/data to kernel parameters
if (!opencl_handle->AssignKernelBuffer(kid, 0, input_buffer_id)) { if (!opencl_handle->AssignKernelBuffer(kid, 0, input_buffer_id)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 0" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 0"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
if (!opencl_handle->AssignKernelBuffer(kid, 1, blue_noise_buffer_id)) { if (!opencl_handle->AssignKernelBuffer(kid, 1, blue_noise_buffer_id)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 1" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 1"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
if (!opencl_handle->AssignKernelBuffer(kid, 2, output_buffer_id)) { if (!opencl_handle->AssignKernelBuffer(kid, 2, output_buffer_id)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 2" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 2"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
unsigned int width = grayscale_image->GetWidth(); unsigned int width = grayscale_image->GetWidth();
if (!opencl_handle->AssignKernelArgument(kid, 3, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 3, sizeof(unsigned int),
&width)) { &width)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 3" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 3"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
unsigned int height = grayscale_image->GetHeight(); unsigned int height = grayscale_image->GetHeight();
if (!opencl_handle->AssignKernelArgument(kid, 4, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 4, sizeof(unsigned int),
&height)) { &height)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 4" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 4"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
unsigned int blue_noise_width = blue_noise->GetWidth(); unsigned int blue_noise_width = blue_noise->GetWidth();
if (!opencl_handle->AssignKernelArgument(kid, 5, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 5, sizeof(unsigned int),
&blue_noise_width)) { &blue_noise_width)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 5" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 5"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
unsigned int blue_noise_height = blue_noise->GetHeight(); unsigned int blue_noise_height = blue_noise->GetHeight();
if (!opencl_handle->AssignKernelArgument(kid, 6, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 6, sizeof(unsigned int),
&blue_noise_height)) { &blue_noise_height)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 6" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 6"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -808,8 +822,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
std::rand() % (blue_noise_width * blue_noise_height); std::rand() % (blue_noise_width * blue_noise_height);
if (!opencl_handle->AssignKernelArgument(kid, 7, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 7, sizeof(unsigned int),
&blue_noise_offset)) { &blue_noise_offset)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 7" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to set parameter 7"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -838,8 +853,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
if (!opencl_handle->ExecuteKernel2D(kid, width, height, work_group_size_0, if (!opencl_handle->ExecuteKernel2D(kid, width, height, work_group_size_0,
work_group_size_1, true)) { work_group_size_1, true)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to execute Kernel" std::cout
<< std::endl; << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to execute Kernel"
<< std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -847,9 +863,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
if (!opencl_handle->GetBufferData(kid, output_buffer_id, if (!opencl_handle->GetBufferData(kid, output_buffer_id,
grayscale_image->GetSize(), grayscale_image->GetSize(),
grayscale_image->data_.data())) { grayscale_image->data_.data())) {
std::cout std::cout << "ERROR ToGrayscaleDitheredWithBlueNoise: Failed to get output "
<< "ERROR ToDitheredWithBlueNoise: Failed to get output buffer data" "buffer data"
<< std::endl; << std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
@ -858,9 +874,9 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
return grayscale_image; return grayscale_image;
} }
const char *Image::GetDitheringKernel() { const char *Image::GetGrayscaleDitheringKernel() {
if (opencl_kernel_ == nullptr) { if (opencl_grayscale_kernel_ == nullptr) {
opencl_kernel_ = opencl_grayscale_kernel_ =
"unsigned int BN_INDEX(\n" "unsigned int BN_INDEX(\n"
"unsigned int x,\n" "unsigned int x,\n"
"unsigned int y,\n" "unsigned int y,\n"
@ -891,7 +907,7 @@ const char *Image::GetDitheringKernel() {
"}\n"; "}\n";
} }
return opencl_kernel_; return opencl_grayscale_kernel_;
} }
OpenCLHandle::Ptr Image::GetOpenCLHandle() { OpenCLHandle::Ptr Image::GetOpenCLHandle() {

View file

@ -103,16 +103,16 @@ class Image {
* *
* \return A std::unique_ptr holding an Image on success, empty otherwise. * \return A std::unique_ptr holding an Image on success, empty otherwise.
*/ */
std::unique_ptr<Image> ToDitheredWithBlueNoise(Image *blue_noise); std::unique_ptr<Image> ToGrayscaleDitheredWithBlueNoise(Image *blue_noise);
/// Returns the Dithering Kernel function as a C string /// Returns the Dithering Kernel function as a C string
static const char *GetDitheringKernel(); static const char *GetGrayscaleDitheringKernel();
/// Returns the OpenCLHandle::Ptr instance /// Returns the OpenCLHandle::Ptr instance
OpenCLHandle::Ptr GetOpenCLHandle(); OpenCLHandle::Ptr GetOpenCLHandle();
private: private:
static const char *opencl_kernel_; static const char *opencl_grayscale_kernel_;
OpenCLHandle::Ptr opencl_handle_; OpenCLHandle::Ptr opencl_handle_;
/// Internally holds rgba /// Internally holds rgba
std::vector<uint8_t> data_; std::vector<uint8_t> data_;

View file

@ -18,7 +18,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
auto output = input.ToDitheredWithBlueNoise(&bluenoise); auto output = input.ToGrayscaleDitheredWithBlueNoise(&bluenoise);
if (!output || !output->IsValid()) { if (!output || !output->IsValid()) {
std::cout << "ERROR: output Image is invalid" << std::endl; std::cout << "ERROR: output Image is invalid" << std::endl;
return 1; return 1;