Repository: google/filament
File: filament/backend/src/webgpu/WebGPUBlitter.h (C/C++)

Code before:
#include <backend/DriverEnums.h>

#include <tsl/robin_map.h>
#include <webgpu/webgpu_cpp.h>

#include <cstdint>

namespace filament::backend {

/**
 * A helper class for blitting textures.
 * This class provides a `blit` method that can copy a region from a source texture to a destination
 * texture. It handles different texture formats, and can perform filtering (nearest or linear).
 * It can also be used to resolve MSAA textures.
 *
 * The blitter maintains a cache of pipelines, pipeline layouts, and shader modules to avoid
 * creating them on every blit operation.
 */
class WebGPUBlitter final {
public:
    /**
     * A struct that contains the arguments for a blit operation.
     */
    struct BlitArgs final {
        /**
         * A struct that describes a texture attachment for a blit operation.
         */
        struct Attachment final {
            // The WebGPU texture to use for the attachment.
            wgpu::Texture const& texture;
            // The texture aspect to use (e.g., color, depth, stencil).
            wgpu::TextureAspect aspect{ wgpu::TextureAspect::Undefined };
            // The origin of the region to blit, in pixels.
            wgpu::Origin2D origin{};
            // The extent of the region to blit, in pixels.
            wgpu::Extent2D extent{};
            // The mip level to use.
            uint32_t mipLevel{ 0 };
            // The array layer or depth slice to use.
            uint32_t layerOrDepth{ 0 };
        };

        // The source attachment for the blit operation.
        Attachment source;
        // The destination attachment for the blit operation.
        Attachment destination;
        // The magnification filter to use when blitting.
        SamplerMagFilter filter;
    };

    explicit WebGPUBlitter(wgpu::Device const& device);

Review comment (suggestion):
I think the comment should be removed, as it doesn't help. If anything, maybe a rename for clarity, such as "TextureAttachment" or "TextureInfo". _("attachment" actually might potentially lead to confusion a little, because typically attachments refer to outputs of GPU pipeline passes, which is not the case for the source and not necessarily the case for the destination)_.

Code after:
#include <backend/DriverEnums.h>

#include <tsl/robin_map.h>
#include <webgpu/webgpu_cpp.h>

#include <cstdint>

namespace filament::backend {

/**
  * A blit essentially writes pixels to a region of one texture given a region of another texture.
  * Such a process can be a byte-by-byte copy when possible, while other scenarios involve various
  * transformations, such as pixel format conversion, image scaling, filtering/sampling
  * (such as down sampling), or even multi-sample resolves. Some blits can be faster than others,
  * depending on what is needed; for example, a byte-by-byte copy is generally faster than other
  * operations.
  *
  * This class is responsible for performing blits throughout the lifecycle of its owner/caller,
  * e.g. the WebGPU backend/driver, where it can leverage state to optimize such blit calls over time.
  */
class WebGPUBlitter final {
public:
    struct BlitArgs final {
        struct Attachment final {
            wgpu::Texture const& texture;
            wgpu::TextureAspect aspect{ wgpu::TextureAspect::Undefined };
            wgpu::Origin2D origin{};
            wgpu::Extent2D extent{};
            uint32_t mipLevel{ 0 };
            uint32_t layerOrDepth{ 0 };
        };

        Attachment source;
        Attachment destination;
        SamplerMagFilter filter;
    };

    explicit WebGPUBlitter(wgpu::Device const& device);

    /**
     * IMPORTANT NOTE: when reusing a command encoder and/or textures make sure to flush/submit
     * pending commands (draws, etc.) to the GPU prior to calling this blit, because texture updates
     * may otherwise (unintentionally) happen after draw commands encoded in the encoder.
     * Submitting any commands up to this point ensures the calls happen in the expected
     * sequence.
     */
    void blit(wgpu::Queue const&, wgpu::CommandEncoder const&, BlitArgs const&);

private:
    /**
     * ONLY should be called if canDoDirectCopy(...) is true (see it in WebGPUBlitter.cpp)