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

Code before:
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);

    /**
     * Blits a region from a source texture to a destination texture.
     * @param queue The WebGPU queue to submit the blit command to.
     * @param encoder The command encoder to record the blit command into.
     * @param args The arguments for the blit operation.
     */

Review comment (suggestion):
Maybe "Offset of the region to blit to or from, in pixels". Or, better, we remove the comment and rename `origin` to `offsetInPixels`.

Code after:
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)
     */
    void copyByteByByte(wgpu::CommandEncoder const&, BlitArgs const&);

    void createSampler(SamplerMagFilter);

    // The following methods are used to create and cache WebGPU objects.
    // The pattern is to have a `getOrCreate...` method that looks up the object in a cache,