2#include "internal/kmeans_gpu.h"
5#include "internal/cielab.h"
7#include "internal/Image.h"
8#include "internal/LABAPixel.h"
9#include "internal/PixelConverters.h"
10#include "internal/RGBAPixel.h"
28 uint32_t numCentroids;
32__attribute__((packed))
49__attribute__((packed))
65__attribute__((packed))
83template <
typename PixelT>
84void kMeansPlusPlusInitGpu(
86 const uint8_t color_space
91 size_t width = pixels.getWidth();
92 size_t height = pixels.getHeight();
93 size_t num_pixels = width * height;
94 uint32_t w =
static_cast<uint32_t
>(width);
95 uint32_t h =
static_cast<uint32_t
>(height);
97 std::vector<PixelT> centroids;
101 w, h, wgpu::TextureFormat::RGBA32Float,
102 wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst
105 std::vector<float> gpu_pixels;
106 gpu_pixels.reserve(num_pixels * 4);
107 for (
size_t i = 0; i < num_pixels; i++) {
108 PixelT p = pixels[i];
109 if constexpr (std::is_same_v<PixelT, ImageLib::LABAPixel<float>>) {
110 gpu_pixels.push_back(p.l / 255.0f);
111 gpu_pixels.push_back(p.a / 255.0f);
112 gpu_pixels.push_back(p.b / 255.0f);
113 gpu_pixels.push_back(p.alpha / 255.0f);
115 gpu_pixels.push_back(p.red / 255.0f);
116 gpu_pixels.push_back(p.green / 255.0f);
117 gpu_pixels.push_back(p.blue / 255.0f);
118 gpu_pixels.push_back(p.alpha / 255.0f);
121 inputTexture.write(gpu_pixels.data(), gpu_pixels.size() *
sizeof(float), 16);
124 std::vector<float> initial_dists(num_pixels, std::numeric_limits<float>::max());
127 wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst
129 minDistBuffer.write(initial_dists);
133 1, wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst
149 std::random_device rd;
150 std::mt19937 gen(rd());
153 std::uniform_int_distribution<> dis(0, num_pixels - 1);
154 int first_index = dis(gen);
155 centroids.push_back(pixels[first_index]);
158 for (
int i = 1; i < k; ++i) {
160 PixelT c = centroids.back();
162 if constexpr (std::is_same_v<PixelT, ImageLib::LABAPixel<float>>) {
164 c.l / 255.0f, c.a / 255.0f, c.b / 255.0f, 1.0f,
static_cast<uint32_t
>(width)
168 c.red / 255.0f, c.green / 255.0f, c.blue / 255.0f, 1.0f,
169 static_cast<uint32_t
>(width)
172 paramBuffer.write(¶ms, 1);
175 gpu::dispatch(pipeline, bindGroup, gpu::workgroup_count(w), gpu::workgroup_count(h));
178 std::vector<float> dists = minDistBuffer.read();
181 double sum_dist_sq = 0.0;
182 for (
size_t j = 0; j < num_pixels; ++j) {
183 sum_dist_sq += dists[j];
186 std::uniform_real_distribution<> dist_selector(0.0, sum_dist_sq);
187 double random_value = dist_selector(gen);
188 double current_sum = 0.0;
189 int selected_index = -1;
191 for (
size_t j = 0; j < num_pixels; ++j) {
192 current_sum += dists[j];
193 if (current_sum >= random_value) {
199 if (selected_index == -1)
200 selected_index = num_pixels - 1;
202 centroids.push_back(pixels[selected_index]);
204#if defined(__EMSCRIPTEN__)
205 emscripten_sleep(10);
209 std::copy(centroids.begin(), centroids.end(), out_centroids.begin());
211#if defined(__EMSCRIPTEN__)
212 emscripten_sleep(50);
232 const uint8_t* data, uint8_t* out_data, int32_t* out_labels,
const int32_t width,
233 const int32_t height,
const int32_t k,
const int32_t max_iter,
const uint8_t color_space
236 pixels.loadFromBuffer(data, width, height, ImageLib::RGBA_CONVERTER<float>);
237 const int32_t num_pixels {pixels.getSize()};
241 std::vector<int32_t> labels(num_pixels, -1);
245 if (color_space == gpu::COLOR_SPACE_CIELAB) {
246 for (
int i {0}; i < pixels.getSize(); ++i) {
247 rgb_to_lab<float, float>(pixels[i], lab[i]);
251 IMG2NUM_LOG_DEBUG(
"starting");
254 switch (color_space) {
255 case gpu::COLOR_SPACE_RGB:
256 kMeansPlusPlusInitGpu<ImageLib::RGBAPixel<float>>(pixels, centroids, k, color_space);
259 kMeansPlusPlusInitGpu<ImageLib::LABAPixel<float>>(lab, centroids_lab, k, color_space);
263 IMG2NUM_LOG_DEBUG(
"kmeans++ init done");
268 const uint32_t w =
static_cast<uint32_t
>(width);
269 const uint32_t h =
static_cast<uint32_t
>(height);
270 constexpr uint32_t bpp = 16;
274 w, h, wgpu::TextureFormat::RGBA32Float,
275 wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst
278 std::vector<float> pixels_;
279 pixels_.reserve(num_pixels * 4);
280 for (
int i = 0; i < num_pixels; i++) {
281 switch (color_space) {
282 case gpu::COLOR_SPACE_RGB: {
284 pixels_.push_back(p.red / 255.0f);
285 pixels_.push_back(p.green / 255.0f);
286 pixels_.push_back(p.blue / 255.0f);
287 pixels_.push_back(p.alpha / 255.0f);
292 pixels_.push_back(p.l / 255.0f);
293 pixels_.push_back(p.a / 255.0f);
294 pixels_.push_back(p.b / 255.0f);
295 pixels_.push_back(p.alpha / 255.0f);
300 inputTexture.write(pixels_.data(), pixels_.size() *
sizeof(
float), bpp);
304 static_cast<uint32_t
>(k), 1, wgpu::TextureFormat::RGBA32Float,
305 wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::StorageBinding |
306 wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc
309 std::vector<float> centroids_;
310 centroids_.reserve(k * 4);
311 switch (color_space) {
312 case gpu::COLOR_SPACE_RGB:
313 for (
int i = 0; i < k; i++) {
314 auto p = centroids[i];
315 centroids_.push_back(p.red / 255.0f);
316 centroids_.push_back(p.green / 255.0f);
317 centroids_.push_back(p.blue / 255.0f);
318 centroids_.push_back(p.alpha / 255.0f);
322 for (
int i = 0; i < k; i++) {
323 auto p = centroids_lab[i];
324 centroids_.push_back(p.l / 255.0f);
325 centroids_.push_back(p.a / 255.0f);
326 centroids_.push_back(p.b / 255.0f);
327 centroids_.push_back(p.alpha / 255.0f);
331 centroidTexture.write(centroids_.data(), centroids_.size() *
sizeof(
float), bpp);
335 w, h, wgpu::TextureFormat::RGBA32Uint,
336 wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::StorageBinding |
337 wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc
341 gpu::Buffer<Params> paramBuffer(1, wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst);
342 Params params = {
static_cast<uint32_t
>(num_pixels),
static_cast<uint32_t
>(k)};
343 paramBuffer.write(¶ms, 1);
346 std::vector<ClusterAccumulator> reset_centroids(k, {0, 0, 0, 0});
348 k, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst
350 accBuffer.write(reset_centroids);
353 wgpu::ComputePipeline pipeline1 =
380 IMG2NUM_LOG_DEBUG(
"start iterations");
384 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
385 for (int32_t iter {0}; iter < max_iter; ++iter) {
387 gpu::encode_pass(encoder, pipeline2, bindGroup2, gpu::workgroup_count(k, 256));
389 wgpu::CommandBuffer commands = encoder.Finish();
390 GPU::getClassInstance().get_queue().Submit(1, &commands);
391 IMG2NUM_LOG_DEBUG(
"done iterations");
398 IMG2NUM_LOG_DEBUG(
"read out");
399 std::vector<uint8_t> labelData = labelTexture.read(bpp);
401 IMG2NUM_LOG_DEBUG(
"mapping labels");
402 for (int32_t i = 0; i < num_pixels; ++i) {
404 std::memcpy(&r, &labelData[i * bpp],
sizeof(uint32_t));
405 labels[i] =
static_cast<int32_t
>(r);
409 std::vector<uint8_t> centroidData = centroidTexture.read(bpp);
410 const float* centroidFloats =
reinterpret_cast<const float*
>(centroidData.data());
412 IMG2NUM_LOG_DEBUG(
"mapping centroids");
413 for (
int i = 0; i < k; i++) {
414 float cr = centroidFloats[i * 4];
415 float cg = centroidFloats[i * 4 + 1];
416 float cb = centroidFloats[i * 4 + 2];
417 float ca = centroidFloats[i * 4 + 3];
418 switch (color_space) {
419 case gpu::COLOR_SPACE_RGB:
433 if (color_space == gpu::COLOR_SPACE_CIELAB) {
434 for (int32_t i {0}; i < k; ++i) {
435 lab_to_rgb<float, float>(centroids_lab[i], centroids[i]);
439 for (int32_t i = 0; i < num_pixels; ++i) {
440 const int32_t cluster = labels[i];
441 out_data[i * 4 + 0] =
static_cast<uint8_t
>(std::round(centroids[cluster].red));
442 out_data[i * 4 + 1] =
static_cast<uint8_t
>(std::round(centroids[cluster].green));
443 out_data[i * 4 + 2] =
static_cast<uint8_t
>(std::round(centroids[cluster].blue));
444 out_data[i * 4 + 3] = 255;
448 IMG2NUM_LOG_DEBUG(
"copying labels out");
449 std::memcpy(out_labels, labels.data(), labels.size() *
sizeof(int32_t));
451#if defined(__EMSCRIPTEN__)
452 emscripten_sleep(50);
RAII wrapper around a wgpu::Buffer with compile-time type safety.
RAII wrapper around a wgpu::Texture with upload and aligned readback.
Reusable RAII helpers for WebGPU/Dawn compute work.
static constexpr uint8_t COLOR_SPACE_CIELAB
Color space selector constants previously duplicated in each kernel.
void encode_pass(wgpu::CommandEncoder &encoder, const wgpu::ComputePipeline &pipeline, const wgpu::BindGroup &bind_group, uint32_t groups_x, uint32_t groups_y=1, uint32_t groups_z=1)
Encode a compute pass into an existing command encoder.
void dispatch(const wgpu::ComputePipeline &pipeline, const wgpu::BindGroup &bind_group, uint32_t groups_x, uint32_t groups_y=1, uint32_t groups_z=1)
Encode, finish, and submit a single compute dispatch.
uint32_t workgroup_count(uint32_t total_size, uint32_t workgroup_size=16)
Compute the number of workgroups needed to cover total_size with workgroups of workgroup_size.
wgpu::ComputePipeline cached_pipeline(const std::string &shader_id, const std::string &label)
Get or create a compute pipeline, caching by shader ID.
wgpu::BindGroup make_bind_group(const wgpu::ComputePipeline &pipeline, uint32_t group_index, const std::vector< BindEntry > &entries)
Build a wgpu::BindGroup from a pipeline layout and a list of entries.
Core image processing functions for img2num project.
static BindEntry buffer_entry(uint32_t binding, const Buffer< T > &buf)
Create a buffer entry (storage or uniform).
static BindEntry texture(uint32_t binding, const Texture &tex)
Create a texture-view entry.