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

Code before:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TNT_FILAMENT_BACKEND_WEBGPURENDERPASSMIPMAPGENERATOR_H
#define TNT_FILAMENT_BACKEND_WEBGPURENDERPASSMIPMAPGENERATOR_H

#include <utils/Panic.h>

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

#include <cstdint>
#include <string_view>

namespace filament::backend {

/**
 * A utility class for generating mipmaps for a texture using a series of render passes.
 * This approach is used for formats that do not support storage bindings, which are required
 * for the compute-based mipmap generation.
 */
class WebGPURenderPassMipmapGenerator final {
public:
    struct FormatCompatibility final {
        bool compatible{ false };
        std::string_view reason;
    };

    /**
     * Checks if a given texture format is compatible with render pass-based mipmap generation.
     * @param format The texture format.
     * @param dimension The texture dimension.
     * @param sampleCount The number of samples.
     * @return A FormatCompatibility struct indicating whether the format is compatible and a reason if not.
     */
    [[nodiscard]] static FormatCompatibility getCompatibilityFor(wgpu::TextureFormat,
            wgpu::TextureDimension, uint32_t sampleCount);

    explicit WebGPURenderPassMipmapGenerator(wgpu::Device const&);

    /**
     * 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 call, because texture updates
     * may otherwise (unintentionally) happen after draw commands encoded in the encoder.

Review comment (refactor):
I think the comment about compute-based mipmap generation can be removed, as it is not relevant for the purpose of this class, and honestly, I _really_ want to just remove both this, the compute-based mipmap generation, and the MsaaResolver  in favor of the swiss army knife blitter, which would simplify things.

Code after:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TNT_FILAMENT_BACKEND_WEBGPURENDERPASSMIPMAPGENERATOR_H
#define TNT_FILAMENT_BACKEND_WEBGPURENDERPASSMIPMAPGENERATOR_H

#include <utils/Panic.h>

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

#include <cstdint>
#include <string_view>

namespace filament::backend {

/**
 * A utility class for generating mipmaps for a texture using a series of render passes.
 */
class WebGPURenderPassMipmapGenerator final {
public:
    struct FormatCompatibility final {
        bool compatible{ false };
        std::string_view reason;
    };

    /**
     * Checks if a given texture format is compatible with render pass-based mipmap generation.
     * @return A FormatCompatibility struct indicating whether the format is compatible
     *         and a reason if not.
     */
    [[nodiscard]] static FormatCompatibility getCompatibilityFor(wgpu::TextureFormat,
            wgpu::TextureDimension, uint32_t sampleCount);

    explicit WebGPURenderPassMipmapGenerator(wgpu::Device const&);

    /**
     * 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 call, 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 generateMipmaps(wgpu::Queue const&, wgpu::Texture const&);