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

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

    /**
     * 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.
     */
    void blit(wgpu::Queue const&, wgpu::CommandEncoder const&, BlitArgs const&);

private:

    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,
    // and if it's not found, it calls a `create...` method to create it and then stores it in the
    // cache. A `hash...` method is used to generate a key for the cache.

    [[nodiscard]] wgpu::RenderPipeline const& getOrCreateRenderPipeline(SamplerMagFilter,
            wgpu::TextureViewDimension sourceDimension, uint32_t sourceSampleCount,
            bool depthSource, wgpu::TextureFormat destinationTextureFormat);

    [[nodiscard]] wgpu::RenderPipeline createRenderPipeline(SamplerMagFilter,
            wgpu::TextureViewDimension sourceDimension, uint32_t sourceSampleCount,
            bool depthSource, wgpu::TextureFormat destinationTextureFormat);

    [[nodiscard]] static size_t hashRenderPipelineKey(SamplerMagFilter,
            wgpu::TextureViewDimension sourceDimension, uint32_t sourceSampleCount,
            bool depthSource, wgpu::TextureFormat destinationTextureFormat);

Review comment (suggestion):
I don't think these `@param` docs add anything useful beyond the types of arguments. I would suggest just the description line at most, e.g.:

```cpp
/**
  * Blits a region from a source texture to a destination texture.
  */
```

Less is more with documentation.

Code after:
  * 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,
    // and if it's not found, it calls a `create...` method to create it and then stores it in the
    // cache. A `hash...` method is used to generate a key for the cache.

    [[nodiscard]] wgpu::RenderPipeline const& getOrCreateRenderPipeline(SamplerMagFilter,
            wgpu::TextureViewDimension sourceDimension, uint32_t sourceSampleCount,
            bool depthSource, wgpu::TextureFormat destinationTextureFormat);

    [[nodiscard]] wgpu::RenderPipeline createRenderPipeline(SamplerMagFilter,
            wgpu::TextureViewDimension sourceDimension, uint32_t sourceSampleCount,
            bool depthSource, wgpu::TextureFormat destinationTextureFormat);