4#ifndef IMG2NUM_GPU_UTILS_H
5#define IMG2NUM_GPU_UTILS_H
19#if defined(__EMSCRIPTEN__)
20#include <emscripten/emscripten.h>
23#include "internal/gpu.h"
24#include "internal/log.h"
29#include <initializer_list>
34#include <unordered_map>
36#include <webgpu/webgpu_cpp.h>
46static constexpr uint8_t COLOR_SPACE_RGB {1};
61inline wgpu::ComputePipeline
66 thread_local std::unordered_map<std::string, wgpu::ComputePipeline> cache;
68 auto it = cache.find(shader_id);
69 if (it != cache.end()) {
72 wgpu::ComputePipeline pipeline = GPU::getClassInstance().createPipeline(shader_id, label);
73 cache[shader_id] = pipeline;
90inline const void*
map_and_wait(wgpu::Buffer& buffer,
size_t size) {
98 wgpu::Future future = buffer.MapAsync(
99 wgpu::MapMode::Read, 0, size, GPU::wait_mode(),
100 [](wgpu::MapAsyncStatus status, wgpu::StringView ,
void* userdata) {
101 auto* s =
static_cast<MapState*
>(userdata);
103 s->success = (status == wgpu::MapAsyncStatus::Success);
105 static_cast<void*
>(&state)
108 GPU::getClassInstance().wait(future, state.done);
110 if (!state.success) {
111 throw std::runtime_error(
"gpu::map_and_wait: MapAsync failed");
114 const void* mapped = buffer.GetConstMappedRange(0, size);
116 throw std::runtime_error(
"gpu::map_and_wait: GetConstMappedRange returned null");
132 static_assert(std::is_trivially_copyable_v<T>,
"Buffer<T>: T must be trivially copyable");
138 Buffer(
size_t count, wgpu::BufferUsage usage)
140 , byte_size_(count * sizeof(T)) {
141 wgpu::BufferDescriptor desc {};
142 desc.size = byte_size_;
144 buffer_ = GPU::getClassInstance().get_device().CreateBuffer(&desc);
151 void write(
const T* data,
size_t count,
size_t offset_elements = 0) {
152 GPU::getClassInstance().get_queue().WriteBuffer(
153 buffer_, offset_elements *
sizeof(T), data, count *
sizeof(T)
158 void write(
const std::vector<T>& data) {
159 write(data.data(), data.size());
168 wgpu::BufferDescriptor staging_desc {};
169 staging_desc.size = byte_size_;
170 staging_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
171 wgpu::Buffer staging = GPU::getClassInstance().get_device().CreateBuffer(&staging_desc);
174 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
175 encoder.CopyBufferToBuffer(buffer_, 0, staging, 0, byte_size_);
176 wgpu::CommandBuffer commands = encoder.Finish();
177 GPU::getClassInstance().get_queue().Submit(1, &commands);
183 std::vector<T> result(count_);
184 std::memcpy(result.data(), mapped, byte_size_);
212 : buffer_(std::move(other.buffer_))
213 , count_(other.count_)
214 , byte_size_(other.byte_size_) {
215 other.buffer_ =
nullptr;
217 other.byte_size_ = 0;
222 if (
this != &other) {
226 buffer_ = std::move(other.buffer_);
227 count_ = other.count_;
228 byte_size_ = other.byte_size_;
229 other.buffer_ =
nullptr;
231 other.byte_size_ = 0;
244 wgpu::Buffer buffer_ {
nullptr};
246 size_t byte_size_ {0};
268 wgpu::TextureDescriptor desc {};
272 texture_ = GPU::getClassInstance().get_device().CreateTexture(&desc);
279 void write(
const void* data,
size_t data_size, uint32_t bytes_per_pixel) {
280 wgpu::TexelCopyTextureInfo dst {};
281 dst.texture = texture_;
282 wgpu::TexelCopyBufferLayout layout {};
284 layout.bytesPerRow = width_ * bytes_per_pixel;
285 layout.rowsPerImage = height_;
286 wgpu::Extent3D extent {width_, height_, 1};
287 GPU::getClassInstance().get_queue().WriteTexture(&dst, data, data_size, &layout, &extent);
296 std::vector<uint8_t>
read(uint32_t bytes_per_pixel) {
297 uint32_t aligned_bpr = GPU::getAlignedBytesPerRow(width_, bytes_per_pixel);
298 uint32_t staging_size = aligned_bpr * height_;
301 wgpu::BufferDescriptor staging_desc {};
302 staging_desc.size = staging_size;
303 staging_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
304 wgpu::Buffer staging = GPU::getClassInstance().get_device().CreateBuffer(&staging_desc);
307 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
309 wgpu::TexelCopyTextureInfo src {};
310 src.texture = texture_;
311 wgpu::TexelCopyBufferInfo dst {};
312 dst.buffer = staging;
313 dst.layout.bytesPerRow = aligned_bpr;
314 dst.layout.rowsPerImage = height_;
315 wgpu::Extent3D extent {width_, height_, 1};
316 encoder.CopyTextureToBuffer(&src, &dst, &extent);
318 wgpu::CommandBuffer commands = encoder.Finish();
319 GPU::getClassInstance().get_queue().Submit(1, &commands);
322 const auto* mapped =
static_cast<const uint8_t*
>(
map_and_wait(staging, staging_size));
325 uint32_t tight_bpr = width_ * bytes_per_pixel;
326 std::vector<uint8_t> result(tight_bpr * height_);
328 for (uint32_t y = 0; y < height_; ++y) {
329 std::memcpy(result.data() + y * tight_bpr, mapped + y * aligned_bpr, tight_bpr);
338 wgpu::TextureView
view()
const {
339 return texture_.CreateView();
366 : texture_(std::move(other.texture_))
367 , width_(other.width_)
368 , height_(other.height_)
369 , format_(other.format_) {
370 other.texture_ =
nullptr;
377 if (
this != &other) {
381 texture_ = std::move(other.texture_);
382 width_ = other.width_;
383 height_ = other.height_;
384 format_ = other.format_;
385 other.texture_ =
nullptr;
400 wgpu::Texture texture_ {
nullptr};
402 uint32_t height_ {0};
403 wgpu::TextureFormat format_ {};
413 wgpu::TextureView texture_view {};
414 wgpu::Buffer buffer {};
415 uint64_t buffer_size {0};
416 uint64_t buffer_offset {0};
422 e.texture_view = tex.
view();
430 e.texture_view = std::move(tv);
447 e.buffer = std::move(buf);
448 e.buffer_size = size;
460 const wgpu::ComputePipeline& pipeline, uint32_t group_index,
461 const std::vector<BindEntry>& entries
463 std::vector<wgpu::BindGroupEntry> wgpu_entries;
464 wgpu_entries.reserve(entries.size());
466 for (
const auto& e : entries) {
467 wgpu::BindGroupEntry wge {};
468 wge.binding = e.binding;
469 if (e.texture_view) {
470 wge.textureView = e.texture_view;
473 wge.buffer = e.buffer;
474 wge.size = e.buffer_size;
475 wge.offset = e.buffer_offset;
477 wgpu_entries.push_back(wge);
480 wgpu::BindGroupDescriptor desc {};
481 desc.layout = pipeline.GetBindGroupLayout(group_index);
482 desc.entryCount =
static_cast<uint32_t
>(wgpu_entries.size());
483 desc.entries = wgpu_entries.data();
485 return GPU::getClassInstance().get_device().CreateBindGroup(&desc);
503 wgpu::CommandEncoder& encoder,
const wgpu::ComputePipeline& pipeline,
504 const wgpu::BindGroup& bind_group, uint32_t groups_x, uint32_t groups_y = 1,
505 uint32_t groups_z = 1
507 wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
508 pass.SetPipeline(pipeline);
509 pass.SetBindGroup(0, bind_group);
510 pass.DispatchWorkgroups(groups_x, groups_y, groups_z);
524 const wgpu::ComputePipeline& pipeline,
const wgpu::BindGroup& bind_group, uint32_t groups_x,
525 uint32_t groups_y = 1, uint32_t groups_z = 1
527 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
528 encode_pass(encoder, pipeline, bind_group, groups_x, groups_y, groups_z);
529 wgpu::CommandBuffer commands = encoder.Finish();
530 GPU::getClassInstance().get_queue().Submit(1, &commands);
542 return (total_size + workgroup_size - 1) / workgroup_size;
RAII wrapper around a wgpu::Buffer with compile-time type safety.
size_t size() const
Element count.
Buffer(Buffer &&other) noexcept
Move constructor. Transfers ownership of the GPU buffer.
void write(const T *data, size_t count, size_t offset_elements=0)
Upload data from CPU to GPU.
Buffer(size_t count, wgpu::BufferUsage usage)
Create a GPU buffer holding count elements of T.
Buffer & operator=(Buffer &&other) noexcept
Move assignment. Destroys the current buffer and takes ownership.
void write(const std::vector< T > &data)
Upload an entire vector to GPU.
~Buffer()
Destructor. Releases the GPU buffer.
size_t byte_size() const
Size in bytes.
const wgpu::Buffer & handle() const
Returns the underlying wgpu::Buffer handle.
std::vector< T > read()
Blocking readback: GPU → CPU.
RAII wrapper around a wgpu::Texture with upload and aligned readback.
std::vector< uint8_t > read(uint32_t bytes_per_pixel)
Blocking readback with automatic 256-byte row-alignment stripping.
uint32_t width() const
Width of the texture in pixels.
Texture & operator=(Texture &&other) noexcept
Move assignment. Destroys the current texture and takes ownership.
void write(const void *data, size_t data_size, uint32_t bytes_per_pixel)
Upload tightly-packed pixel data to the texture.
wgpu::TextureView view() const
Create a default texture view.
~Texture()
Destructor. Releases the GPU texture.
uint32_t height() const
Height of the texture in pixels.
Texture(uint32_t width, uint32_t height, wgpu::TextureFormat format, wgpu::TextureUsage usage)
Create a 2D texture.
Texture(Texture &&other) noexcept
Move constructor. Transfers ownership of the GPU texture.
wgpu::TextureFormat format() const
Texel format of the texture.
const wgpu::Texture & handle() const
Underlying wgpu::Texture handle.
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.
const void * map_and_wait(wgpu::Buffer &buffer, size_t size)
Block until a MapAsync on buffer completes.
A single entry for bind-group construction.
static BindEntry texture_raw(uint32_t binding, wgpu::TextureView tv)
Create a texture-view entry from a raw wgpu::TextureView.
static BindEntry buffer_raw(uint32_t binding, wgpu::Buffer buf, uint64_t size)
Create a buffer entry from a raw wgpu::Buffer and size.
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.