Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
gpu::Texture Class Reference

RAII wrapper around a wgpu::Texture with upload and aligned readback. More...

#include <gpu_utils.h>

+ Collaboration diagram for gpu::Texture:

Public Member Functions

 Texture (uint32_t width, uint32_t height, wgpu::TextureFormat format, wgpu::TextureUsage usage)
 Create a 2D texture.
 
void write (const void *data, size_t data_size, uint32_t bytes_per_pixel)
 Upload tightly-packed pixel data to the texture.
 
std::vector< uint8_t > read (uint32_t bytes_per_pixel)
 Blocking readback with automatic 256-byte row-alignment stripping.
 
wgpu::TextureView view () const
 Create a default texture view.
 
const wgpu::Texture & handle () const
 Underlying wgpu::Texture handle.
 
uint32_t width () const
 Width of the texture in pixels.
 
uint32_t height () const
 Height of the texture in pixels.
 
wgpu::TextureFormat format () const
 Texel format of the texture.
 
 Texture (const Texture &)=delete
 
Texture & operator= (const Texture &)=delete
 
 Texture (Texture &&other) noexcept
 Move constructor. Transfers ownership of the GPU texture.
 
Texture & operator= (Texture &&other) noexcept
 Move assignment. Destroys the current texture and takes ownership.
 
 ~Texture ()
 Destructor. Releases the GPU texture.
 

Private Attributes

wgpu::Texture texture_ {nullptr}
 
uint32_t width_ {0}
 
uint32_t height_ {0}
 
wgpu::TextureFormat format_ {}
 

Detailed Description

RAII wrapper around a wgpu::Texture with upload and aligned readback.

Handles the 256-byte row-alignment requirement for CopyTextureToBuffer automatically during readback.

Definition at line 257 of file gpu_utils.h.

Constructor & Destructor Documentation

◆ Texture() [1/2]

gpu::Texture::Texture ( uint32_t  width,
uint32_t  height,
wgpu::TextureFormat  format,
wgpu::TextureUsage  usage 
)
inline

Create a 2D texture.

Parameters
widthWidth in pixels.
heightHeight in pixels.
formatTexel format (e.g. RGBA8Unorm, RGBA32Float).
usageTexture usage flags.

Definition at line 264 of file gpu_utils.h.

265 : width_(width)
266 , height_(height)
267 , format_(format) {
268 wgpu::TextureDescriptor desc {};
269 desc.size = {width, height, 1};
270 desc.format = format;
271 desc.usage = usage;
272 texture_ = GPU::getClassInstance().get_device().CreateTexture(&desc);
273 }
uint32_t width() const
Width of the texture in pixels.
Definition gpu_utils.h:348
uint32_t height() const
Height of the texture in pixels.
Definition gpu_utils.h:352
wgpu::TextureFormat format() const
Texel format of the texture.
Definition gpu_utils.h:356

References format(), height(), and width().

+ Here is the call graph for this function:

◆ Texture() [2/2]

gpu::Texture::Texture ( Texture &&  other)
inlinenoexcept

Move constructor. Transfers ownership of the GPU texture.

Definition at line 365 of file gpu_utils.h.

366 : texture_(std::move(other.texture_))
367 , width_(other.width_)
368 , height_(other.height_)
369 , format_(other.format_) {
370 other.texture_ = nullptr;
371 other.width_ = 0;
372 other.height_ = 0;
373 }

◆ ~Texture()

gpu::Texture::~Texture ( )
inline

Destructor. Releases the GPU texture.

Definition at line 393 of file gpu_utils.h.

393 {
394 if (texture_) {
395 texture_.Destroy();
396 }
397 }

Member Function Documentation

◆ format()

wgpu::TextureFormat gpu::Texture::format ( ) const
inline

Texel format of the texture.

Definition at line 356 of file gpu_utils.h.

356 {
357 return format_;
358 }

Referenced by Texture().

+ Here is the caller graph for this function:

◆ handle()

const wgpu::Texture & gpu::Texture::handle ( ) const
inline

Underlying wgpu::Texture handle.

Definition at line 343 of file gpu_utils.h.

343 {
344 return texture_;
345 }

◆ height()

uint32_t gpu::Texture::height ( ) const
inline

Height of the texture in pixels.

Definition at line 352 of file gpu_utils.h.

352 {
353 return height_;
354 }

Referenced by Texture().

+ Here is the caller graph for this function:

◆ operator=()

Texture & gpu::Texture::operator= ( Texture &&  other)
inlinenoexcept

Move assignment. Destroys the current texture and takes ownership.

Definition at line 376 of file gpu_utils.h.

376 {
377 if (this != &other) {
378 if (texture_) {
379 texture_.Destroy();
380 }
381 texture_ = std::move(other.texture_);
382 width_ = other.width_;
383 height_ = other.height_;
384 format_ = other.format_;
385 other.texture_ = nullptr;
386 other.width_ = 0;
387 other.height_ = 0;
388 }
389 return *this;
390 }

◆ read()

std::vector< uint8_t > gpu::Texture::read ( uint32_t  bytes_per_pixel)
inline

Blocking readback with automatic 256-byte row-alignment stripping.

Returns tightly-packed pixel data with no padding between rows.

Parameters
bytes_per_pixelBytes per pixel in the texture format.
Returns
Tightly-packed pixel data.

Definition at line 296 of file gpu_utils.h.

296 {
297 uint32_t aligned_bpr = GPU::getAlignedBytesPerRow(width_, bytes_per_pixel);
298 uint32_t staging_size = aligned_bpr * height_;
299
300 // Create staging buffer
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);
305
306 // Encode copy
307 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
308
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);
317
318 wgpu::CommandBuffer commands = encoder.Finish();
319 GPU::getClassInstance().get_queue().Submit(1, &commands);
320
321 // Map and wait
322 const auto* mapped = static_cast<const uint8_t*>(map_and_wait(staging, staging_size));
323
324 // Strip row alignment padding
325 uint32_t tight_bpr = width_ * bytes_per_pixel;
326 std::vector<uint8_t> result(tight_bpr * height_);
327
328 for (uint32_t y = 0; y < height_; ++y) {
329 std::memcpy(result.data() + y * tight_bpr, mapped + y * aligned_bpr, tight_bpr);
330 }
331
332 staging.Unmap();
333 staging.Destroy();
334 return result;
335 }
const void * map_and_wait(wgpu::Buffer &buffer, size_t size)
Block until a MapAsync on buffer completes.
Definition gpu_utils.h:90

References gpu::map_and_wait().

+ Here is the call graph for this function:

◆ view()

wgpu::TextureView gpu::Texture::view ( ) const
inline

Create a default texture view.

Definition at line 338 of file gpu_utils.h.

338 {
339 return texture_.CreateView();
340 }

Referenced by gpu::BindEntry::texture().

+ Here is the caller graph for this function:

◆ width()

uint32_t gpu::Texture::width ( ) const
inline

Width of the texture in pixels.

Definition at line 348 of file gpu_utils.h.

348 {
349 return width_;
350 }

Referenced by Texture().

+ Here is the caller graph for this function:

◆ write()

void gpu::Texture::write ( const void *  data,
size_t  data_size,
uint32_t  bytes_per_pixel 
)
inline

Upload tightly-packed pixel data to the texture.

Parameters
dataPointer to pixel data.
data_sizeTotal size in bytes.
bytes_per_pixelBytes per pixel (e.g. 4 for RGBA8, 16 for RGBA32Float).

Definition at line 279 of file gpu_utils.h.

279 {
280 wgpu::TexelCopyTextureInfo dst {};
281 dst.texture = texture_;
282 wgpu::TexelCopyBufferLayout layout {};
283 layout.offset = 0;
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);
288 }

Member Data Documentation

◆ format_

wgpu::TextureFormat gpu::Texture::format_ {}
private

Definition at line 403 of file gpu_utils.h.

403{};

◆ height_

uint32_t gpu::Texture::height_ {0}
private

Definition at line 402 of file gpu_utils.h.

402{0};

◆ texture_

wgpu::Texture gpu::Texture::texture_ {nullptr}
private

Definition at line 400 of file gpu_utils.h.

400{nullptr};

◆ width_

uint32_t gpu::Texture::width_ {0}
private

Definition at line 401 of file gpu_utils.h.

401{0};

The documentation for this class was generated from the following file: