2#include "internal/kmeans_gpu.h"
5#include "internal/cielab.h"
6#include "internal/gpu.h"
7#include "internal/Image.h"
8#include "internal/LABAPixel.h"
9#include "internal/log.h"
10#include "internal/PixelConverters.h"
11#include "internal/RGBAPixel.h"
27static constexpr uint8_t COLOR_SPACE_OPTION_CIELAB {0};
28static constexpr uint8_t COLOR_SPACE_OPTION_RGB {1};
35 uint32_t numCentroids;
39__attribute__((packed))
56__attribute__((packed))
72__attribute__((packed))
80template <
typename PixelT>
81void kMeansPlusPlusInitGpu(
83 const uint8_t color_space
88 size_t width = pixels.getWidth();
89 size_t height = pixels.getHeight();
90 size_t num_pixels = width * height;
92 std::vector<PixelT> centroids;
97 wgpu::TextureDescriptor texDesc = {};
98 texDesc.size = {
static_cast<uint32_t
>(width),
static_cast<uint32_t
>(height), 1};
99 texDesc.format = wgpu::TextureFormat::RGBA32Float;
100 texDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
101 texDesc.label =
"inputTextureInit";
102 wgpu::Texture inputTexture = GPU::getClassInstance().get_device().CreateTexture(&texDesc);
105 std::vector<float> gpu_pixels;
106 gpu_pixels.reserve(num_pixels * 4);
108 for (
int i = 0; i < num_pixels; i++) {
109 PixelT p = pixels[i];
110 if constexpr (std::is_same_v<PixelT, ImageLib::LABAPixel<float>>) {
111 gpu_pixels.push_back(p.l / 255.0f);
112 gpu_pixels.push_back(p.a / 255.0f);
113 gpu_pixels.push_back(p.b / 255.0f);
114 gpu_pixels.push_back(p.alpha / 255.0f);
116 gpu_pixels.push_back(p.red / 255.0f);
117 gpu_pixels.push_back(p.green / 255.0f);
118 gpu_pixels.push_back(p.blue / 255.0f);
119 gpu_pixels.push_back(p.alpha / 255.0f);
123 wgpu::TexelCopyTextureInfo texDst = {};
124 texDst.texture = inputTexture;
125 wgpu::TexelCopyBufferLayout texLayout = {};
126 texLayout.bytesPerRow = width * 16;
127 texLayout.rowsPerImage = height;
128 GPU::getClassInstance().get_queue().WriteTexture(
129 &texDst, gpu_pixels.data(), gpu_pixels.size() * 4, &texLayout, &texDesc.size
134 std::vector<float> initial_dists(num_pixels, std::numeric_limits<float>::max());
136 wgpu::BufferDescriptor distDesc = {};
137 distDesc.size = num_pixels *
sizeof(float);
139 wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;
140 wgpu::Buffer minDistBuffer = GPU::getClassInstance().get_device().CreateBuffer(&distDesc);
141 GPU::getClassInstance().get_queue().WriteBuffer(
142 minDistBuffer, 0, initial_dists.data(), distDesc.size
147 wgpu::BufferDescriptor uniDesc = {};
149 uniDesc.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
150 wgpu::Buffer paramBuffer = GPU::getClassInstance().get_device().CreateBuffer(&uniDesc);
153 wgpu::BufferDescriptor readDesc = {};
154 readDesc.size = num_pixels *
sizeof(float);
155 readDesc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
156 wgpu::Buffer readBuffer = GPU::getClassInstance().get_device().CreateBuffer(&readDesc);
159 wgpu::ComputePipeline pipeline =
160 GPU::getClassInstance().createPipeline(
"dist_shader",
"updateDistShader");
163 wgpu::BindGroupEntry entries[3];
164 entries[0].binding = 0;
165 entries[0].textureView = inputTexture.CreateView();
166 entries[1].binding = 1;
167 entries[1].buffer = minDistBuffer;
168 entries[1].size = distDesc.size;
169 entries[2].binding = 2;
170 entries[2].buffer = paramBuffer;
171 entries[2].size = uniDesc.size;
173 wgpu::BindGroupDescriptor bgDesc = {};
174 bgDesc.layout = pipeline.GetBindGroupLayout(0);
175 bgDesc.entryCount = 3;
176 bgDesc.entries = entries;
177 wgpu::BindGroup bindGroup = GPU::getClassInstance().get_device().CreateBindGroup(&bgDesc);
181 std::random_device rd;
182 std::mt19937 gen(rd());
185 std::uniform_int_distribution<> dis(0, num_pixels - 1);
186 int first_index = dis(gen);
187 centroids.push_back(pixels[first_index]);
191 bool* done =
new bool(
false);
193 for (
int i = 1; i < k; ++i) {
196 PixelT c = centroids.back();
198 if constexpr (std::is_same_v<PixelT, ImageLib::LABAPixel<float>>) {
200 c.l / 255.0f, c.a / 255.0f, c.b / 255.0f, 1.0f,
static_cast<uint32_t
>(width)
204 c.red / 255.0f, c.green / 255.0f, c.blue / 255.0f, 1.0f,
205 static_cast<uint32_t
>(width)
209 GPU::getClassInstance().get_queue().WriteBuffer(
214 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
215 wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
216 pass.SetPipeline(pipeline);
217 pass.SetBindGroup(0, bindGroup);
218 pass.DispatchWorkgroups((width + 15) / 16, (height + 15) / 16, 1);
222 encoder.CopyBufferToBuffer(minDistBuffer, 0, readBuffer, 0, readDesc.size);
223 wgpu::CommandBuffer commands = encoder.Finish();
224 GPU::getClassInstance().get_queue().Submit(1, &commands);
229 wgpu::MapMode::Read, 0, readDesc.size, wgpu::CallbackMode::AllowProcessEvents,
230 [](wgpu::MapAsyncStatus status, wgpu::StringView msg,
void* userdata) {
231 bool* flag = static_cast<bool*>(userdata);
232 bool success = false;
233 if (status == wgpu::MapAsyncStatus::Success) {
246 GPU::getClassInstance().get_instance().ProcessEvents();
247#if defined(__EMSCRIPTEN__)
248 emscripten_sleep(10);
252 const float* dists = (
const float*)readBuffer.GetConstMappedRange();
254 double sum_dist_sq = 0.0;
258 for (
size_t j = 0; j < num_pixels; ++j) {
259 sum_dist_sq += dists[j];
263 std::uniform_real_distribution<> dist_selector(0.0, sum_dist_sq);
264 double random_value = dist_selector(gen);
265 double current_sum = 0.0;
266 int selected_index = -1;
268 for (
size_t j = 0; j < num_pixels; ++j) {
269 current_sum += dists[j];
270 if (current_sum >= random_value) {
276 if (selected_index == -1)
277 selected_index = num_pixels - 1;
280 centroids.push_back(pixels[selected_index]);
282#if defined(__EMSCRIPTEN__)
283 emscripten_sleep(10);
287 std::copy(centroids.begin(), centroids.end(), out_centroids.begin());
291 inputTexture.Destroy();
292 readBuffer.Destroy();
293 minDistBuffer.Destroy();
294 paramBuffer.Destroy();
297#if defined(__EMSCRIPTEN__)
298 emscripten_sleep(50);
307 const int32_t height,
const int32_t k, wgpu::Texture& inputTexture, wgpu::Texture& labelTexture,
308 wgpu::Texture& centroidTexture, wgpu::TextureDescriptor& labelDesc,
309 wgpu::TextureDescriptor& centroidDesc, wgpu::ComputePipeline& pipeline1,
310 wgpu::ComputePipeline& pipeline2, wgpu::BindGroup& bindGroup1, wgpu::BindGroup& bindGroup2,
311 const uint8_t color_space
313 int bytesPerPixel {16};
314 const int32_t num_pixels {pixels.getSize()};
316 wgpu::TextureDescriptor texDesc = {};
317 texDesc.size = {
static_cast<uint32_t
>(width),
static_cast<uint32_t
>(height), 1};
318 texDesc.format = wgpu::TextureFormat::RGBA32Float;
319 texDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
320 texDesc.label =
"inputTexture";
321 inputTexture = GPU::getClassInstance().get_device().CreateTexture(&texDesc);
323 wgpu::TexelCopyTextureInfo dst = {};
324 dst.texture = inputTexture;
325 wgpu::TexelCopyBufferLayout layout = {};
327 layout.bytesPerRow = width * bytesPerPixel;
328 layout.rowsPerImage = height;
330 std::vector<float> pixels_;
331 for (
int i = 0; i < num_pixels; i++) {
332 switch (color_space) {
333 case COLOR_SPACE_OPTION_RGB: {
335 pixels_.push_back(p.red / 255.0f);
336 pixels_.push_back(p.green / 255.0f);
337 pixels_.push_back(p.blue / 255.0f);
338 pixels_.push_back(p.alpha / 255.0f);
341 case COLOR_SPACE_OPTION_CIELAB: {
343 pixels_.push_back(p.l / 255.0f);
344 pixels_.push_back(p.a / 255.0f);
345 pixels_.push_back(p.b / 255.0f);
346 pixels_.push_back(p.alpha / 255.0f);
352 GPU::getClassInstance().get_queue().WriteTexture(
353 &dst, pixels_.data(), pixels_.size() *
sizeof(
float), &layout, &texDesc.size
357 centroidDesc.size = {
static_cast<uint32_t
>(k), 1, 1};
358 centroidDesc.format = wgpu::TextureFormat::RGBA32Float;
359 centroidDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::StorageBinding |
360 wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc;
361 centroidDesc.label =
"centroidTexture";
362 centroidTexture = GPU::getClassInstance().get_device().CreateTexture(¢roidDesc);
364 wgpu::TexelCopyTextureInfo cdst = {};
365 cdst.texture = centroidTexture;
366 wgpu::TexelCopyBufferLayout clayout = {};
368 clayout.bytesPerRow = k * bytesPerPixel;
369 clayout.rowsPerImage = 1;
371 std::vector<float> centroids_;
372 switch (color_space) {
373 case COLOR_SPACE_OPTION_RGB: {
374 for (
int i = 0; i < k; i++) {
375 auto p = centroids[i];
376 centroids_.push_back(p.red / 255.0f);
377 centroids_.push_back(p.green / 255.0f);
378 centroids_.push_back(p.blue / 255.0f);
379 centroids_.push_back(p.alpha / 255.0f);
383 case COLOR_SPACE_OPTION_CIELAB: {
384 for (
int i = 0; i < k; i++) {
385 auto p = centroids_lab[i];
386 centroids_.push_back(p.l / 255.0f);
387 centroids_.push_back(p.a / 255.0f);
388 centroids_.push_back(p.b / 255.0f);
389 centroids_.push_back(p.alpha / 255.0f);
395 GPU::getClassInstance().get_queue().WriteTexture(
396 &cdst, centroids_.data(), centroids_.size() *
sizeof(
float), &clayout, ¢roidDesc.size
400 labelDesc.size = {
static_cast<uint32_t
>(width),
static_cast<uint32_t
>(height), 1};
401 labelDesc.format = wgpu::TextureFormat::RGBA32Uint;
402 labelDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::StorageBinding |
403 wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc;
404 labelDesc.label =
"labelTexture";
405 labelTexture = GPU::getClassInstance().get_device().CreateTexture(&labelDesc);
408 Params params = {
static_cast<uint32_t
>(num_pixels),
static_cast<uint32_t
>(k)};
409 wgpu::BufferDescriptor bufDesc = {};
410 bufDesc.size =
sizeof(
Params);
411 bufDesc.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
412 wgpu::Buffer paramBuffer = GPU::getClassInstance().get_device().CreateBuffer(&bufDesc);
413 GPU::getClassInstance().get_queue().WriteBuffer(paramBuffer, 0, ¶ms,
sizeof(
Params));
416 std::vector<ClusterAccumulator> reset_centroids(k, {0, 0, 0, 0});
417 wgpu::BufferDescriptor accDesc = {};
419 accDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst;
420 wgpu::Buffer accBuffer = GPU::getClassInstance().get_device().CreateBuffer(&accDesc);
421 GPU::getClassInstance().get_queue().WriteBuffer(
422 accBuffer, 0, reset_centroids.data(), accDesc.size
427 GPU::getClassInstance().createPipeline(
"assign_update_shader",
"assignUpdateShader");
428 pipeline2 = GPU::getClassInstance().createPipeline(
"resolve_shader",
"resolveShader");
431 wgpu::BindGroupDescriptor bindGroupDesc1 = {};
432 bindGroupDesc1.layout = pipeline1.GetBindGroupLayout(0);
433 wgpu::BindGroupEntry entries1[5];
435 entries1[0].binding = 0;
436 entries1[0].textureView = inputTexture.CreateView();
438 entries1[1].binding = 1;
439 entries1[1].textureView = centroidTexture.CreateView();
441 entries1[2].binding = 2;
442 entries1[2].textureView = labelTexture.CreateView();
444 entries1[3].binding = 3;
445 entries1[3].buffer = paramBuffer;
446 entries1[3].size =
sizeof(
Params);
448 entries1[4].binding = 4;
449 entries1[4].buffer = accBuffer;
452 bindGroupDesc1.entryCount = 5;
453 bindGroupDesc1.entries = entries1;
454 bindGroup1 = GPU::getClassInstance().get_device().CreateBindGroup(&bindGroupDesc1);
456 wgpu::BindGroupDescriptor bindGroupDesc2 = {};
457 bindGroupDesc2.layout = pipeline2.GetBindGroupLayout(0);
458 wgpu::BindGroupEntry entries2[2];
459 entries2[0].binding = 0;
460 entries2[0].buffer = accBuffer;
461 entries2[0].size = accDesc.size;
462 entries2[1].binding = 1;
463 entries2[1].textureView = centroidTexture.CreateView();
464 bindGroupDesc2.entryCount = 2;
465 bindGroupDesc2.entries = entries2;
466 bindGroup2 = GPU::getClassInstance().get_device().CreateBindGroup(&bindGroupDesc2);
470 const uint8_t* data, uint8_t* out_data, int32_t* out_labels,
const int32_t width,
471 const int32_t height,
const int32_t k,
const int32_t max_iter,
const uint8_t color_space
474 pixels.loadFromBuffer(data, width, height, ImageLib::RGBA_CONVERTER<float>);
475 const int32_t num_pixels {pixels.getSize()};
482 std::vector<int32_t> labels(num_pixels, -1);
486 if (color_space == COLOR_SPACE_OPTION_CIELAB) {
487 for (
int i {0}; i < pixels.getSize(); ++i) {
488 rgb_to_lab<float, float>(pixels[i], lab[i]);
492 IMG2NUM_LOG_INFO(
"starting");
495 switch (color_space) {
496 case COLOR_SPACE_OPTION_RGB: {
497 kMeansPlusPlusInitGpu<ImageLib::RGBAPixel<float>>(pixels, centroids, k, color_space);
500 case COLOR_SPACE_OPTION_CIELAB: {
501 kMeansPlusPlusInitGpu<ImageLib::LABAPixel<float>>(lab, centroids_lab, k, color_space);
506 IMG2NUM_LOG_INFO(
"kmeans++ init done");
509 int bytesPerPixel {16};
514 wgpu::ComputePipeline pipeline1;
515 wgpu::ComputePipeline pipeline2;
516 wgpu::BindGroup bindGroup1;
517 wgpu::BindGroup bindGroup2;
518 wgpu::Texture inputTexture;
519 wgpu::Texture labelTexture;
520 wgpu::Texture centroidTexture;
521 wgpu::TextureDescriptor labelDesc = {};
522 wgpu::TextureDescriptor centroidDesc = {};
526 pixels, lab, centroids, centroids_lab, width, height, k, inputTexture, labelTexture,
527 centroidTexture, labelDesc, centroidDesc, pipeline1, pipeline2, bindGroup1, bindGroup2,
531 uint32_t wgX = (width + 15) / 16;
532 uint32_t wgY = (height + 15) / 16;
535 uint32_t bytesPerRowLabels =
536 GPU::getAlignedBytesPerRow(width,
static_cast<uint32_t
>(bytesPerPixel));
537 wgpu::BufferDescriptor readLabelsDesc = {};
538 readLabelsDesc.size = bytesPerRowLabels * height;
539 readLabelsDesc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
540 wgpu::Buffer readLabelsBuffer =
541 GPU::getClassInstance().get_device().CreateBuffer(&readLabelsDesc);
544 uint32_t bytesPerRowCentroids =
545 GPU::getAlignedBytesPerRow(width,
static_cast<uint32_t
>(bytesPerPixel));
546 wgpu::BufferDescriptor readCentroidsDesc = {};
547 readCentroidsDesc.size = bytesPerRowCentroids;
548 readCentroidsDesc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
549 wgpu::Buffer readCentroidsBuffer =
550 GPU::getClassInstance().get_device().CreateBuffer(&readCentroidsDesc);
553 IMG2NUM_LOG_INFO(
"start iterations");
554 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
555 for (int32_t iter {0}; iter < max_iter; ++iter) {
556 wgpu::ComputePassEncoder pass1 = encoder.BeginComputePass();
557 pass1.SetPipeline(pipeline1);
558 pass1.SetBindGroup(0, bindGroup1);
559 pass1.DispatchWorkgroups(wgX, wgY);
562 wgpu::ComputePassEncoder pass2 = encoder.BeginComputePass();
563 pass2.SetPipeline(pipeline2);
564 pass2.SetBindGroup(0, bindGroup2);
565 pass2.DispatchWorkgroups((k + 255) / 256, 1);
572 wgpu::TexelCopyTextureInfo srcLabels = {};
573 srcLabels.texture = labelTexture;
574 wgpu::TexelCopyBufferInfo dstLabels = {};
575 dstLabels.buffer = readLabelsBuffer;
576 dstLabels.layout.bytesPerRow = bytesPerRowLabels;
577 dstLabels.layout.rowsPerImage = height;
578 encoder.CopyTextureToBuffer(&srcLabels, &dstLabels, &labelDesc.size);
581 wgpu::TexelCopyTextureInfo srcCentroids = {};
582 srcCentroids.texture = centroidTexture;
583 wgpu::TexelCopyBufferInfo dstCentroids = {};
584 dstCentroids.buffer = readCentroidsBuffer;
585 dstCentroids.layout.bytesPerRow = bytesPerRowCentroids;
586 dstCentroids.layout.rowsPerImage = 1;
587 encoder.CopyTextureToBuffer(&srcCentroids, &dstCentroids, ¢roidDesc.size);
589 wgpu::CommandBuffer commands = encoder.Finish();
590 GPU::getClassInstance().get_queue().Submit(1, &commands);
591 IMG2NUM_LOG_INFO(
"done iterations");
594 bool* done1 =
new bool(
false);
595 bool* done2 =
new bool(
false);
598 readLabelsBuffer.MapAsync(
599 wgpu::MapMode::Read, 0, readLabelsDesc.size, wgpu::CallbackMode::AllowProcessEvents,
600 [](wgpu::MapAsyncStatus status, wgpu::StringView msg,
void* userdata) {
601 bool* flag = static_cast<bool*>(userdata);
602 bool success = false;
603 if (status == wgpu::MapAsyncStatus::Success) {
611 IMG2NUM_LOG_INFO(
"read out");
614 GPU::getClassInstance().get_instance().ProcessEvents();
615#if defined(__EMSCRIPTEN__)
616 emscripten_sleep(10);
620 IMG2NUM_LOG_INFO(
"mapping labels");
621 const uint8_t* mappedData = (
const uint8_t*)readLabelsBuffer.GetConstMappedRange();
624 for (
size_t y = 0; y < height; ++y) {
625 const uint8_t* rowPtr = mappedData + (y * bytesPerRowLabels);
626 for (
size_t x = 0; x < width; ++x) {
627 const uint8_t* pixelPtr = rowPtr + (x * bytesPerPixel);
629 std::memcpy(&r, pixelPtr,
sizeof(uint32_t));
631 size_t dstIndex = y * width + x;
632 labels[dstIndex] =
static_cast<int32_t
>(r);
636 readLabelsBuffer.Unmap();
639 readCentroidsBuffer.MapAsync(
640 wgpu::MapMode::Read, 0, readCentroidsDesc.size, wgpu::CallbackMode::AllowProcessEvents,
641 [](wgpu::MapAsyncStatus status, wgpu::StringView msg,
void* userdata) {
642 bool* flag =
static_cast<bool*
>(userdata);
643 bool success =
false;
644 if (status == wgpu::MapAsyncStatus::Success) {
653 GPU::getClassInstance().get_instance().ProcessEvents();
654#if defined(__EMSCRIPTEN__)
655 emscripten_sleep(10);
659 IMG2NUM_LOG_INFO(
"mapping centroids");
660 const float* mappedDataFloat = (
const float*)readCentroidsBuffer.GetConstMappedRange();
663 for (
int i = 0; i < k; i++) {
665 const float* centroidPtr = mappedDataFloat + (i * 4);
667 float r = *(centroidPtr);
668 float g = *(centroidPtr + 1);
669 float b = *(centroidPtr + 2);
670 float a = *(centroidPtr + 3);
671 switch (color_space) {
672 case COLOR_SPACE_OPTION_RGB: {
676 case COLOR_SPACE_OPTION_CIELAB: {
684 readCentroidsBuffer.Unmap();
687 if (color_space == COLOR_SPACE_OPTION_CIELAB) {
688 for (int32_t i {0}; i < k; ++i) {
689 lab_to_rgb<float, float>(centroids_lab[i], centroids[i]);
693 for (int32_t i = 0; i < num_pixels; ++i) {
694 const int32_t cluster = labels[i];
695 out_data[i * 4 + 0] =
static_cast<uint8_t
>(centroids[cluster].red);
696 out_data[i * 4 + 1] =
static_cast<uint8_t
>(centroids[cluster].green);
697 out_data[i * 4 + 2] =
static_cast<uint8_t
>(centroids[cluster].blue);
698 out_data[i * 4 + 3] = 255;
702 IMG2NUM_LOG_INFO(
"copying labels out");
703 std::memcpy(out_labels, labels.data(), labels.size() *
sizeof(int32_t));
706 inputTexture.Destroy();
708 labelTexture.Destroy();
710 centroidTexture.Destroy();
711 readLabelsBuffer.Destroy();
712 readCentroidsBuffer.Destroy();
717 labels.shrink_to_fit();
718#if defined(__EMSCRIPTEN__)
719 emscripten_sleep(50);
Core image processing functions for img2num project.