Repository: google/filament
File: filament/backend/src/webgpu/WebGPUDriver.cpp (C++)

Code before:
    FILAMENT_CHECK_PRECONDITION(sourceViewport.left >= 0 && sourceViewport.bottom >= 0 &&
                                destinationViewport.left >= 0 && destinationViewport.bottom >= 0)
            << "Source and destination viewports must be positive.";

    // We always blit from/to the COLOR0 attachment.
    Handle<HwTexture> sourceTextureHandle = srcTarget->getColorAttachmentInfos()[0].handle;
    Handle<HwTexture> destinationTextureHandle = dstTarget->getColorAttachmentInfos()[0].handle;

    if (UTILS_UNLIKELY(!sourceTextureHandle || !destinationTextureHandle)) {
        FWGPU_LOGE << "blitDEPRECATED could not find a valid color attachment to blit.";
        return;
    }

    // WebGPU's texture coordinates are top-left, while Filament's are bottom-left.
    // We need to flip the y-coordinate for both source and destination.
    auto const srcTexture = handleCast<WebGPUTexture>(sourceTextureHandle);
    const uint32_t srcTextureHeight = srcTexture->height;
    const uint32_t flippedSourceY =
            srcTextureHeight - sourceViewport.bottom - sourceViewport.height;

    auto const dstTexture = handleCast<WebGPUTexture>(destinationTextureHandle);
    const uint32_t dstTextureHeight = dstTexture->height;
    const uint32_t flippedDestinationY =
            dstTextureHeight - destinationViewport.bottom - destinationViewport.height;

    const wgpu::Origin2D sourceOrigin = { (uint32_t) sourceViewport.left, flippedSourceY };
    const wgpu::Origin2D destinationOrigin = { (uint32_t) destinationViewport.left,
        flippedDestinationY };

    const wgpu::Extent2D sourceSize = { (uint32_t) sourceViewport.width,
        (uint32_t) sourceViewport.height };
    const wgpu::Extent2D destinationSize = { (uint32_t) destinationViewport.width,
        (uint32_t) destinationViewport.height };

    // The internal blit() function doesn't support separate source/destination sizes,
    // so we call the blitter directly.
    bool reusedCommandEncoder{ true };
    if (mCommandEncoder) {
        flush();
    } else {
        reusedCommandEncoder = false;
        const wgpu::CommandEncoderDescriptor desc{ .label = "blit_deprecated_command" };
        mCommandEncoder = mDevice.CreateCommandEncoder(&desc);
    }

    const WebGPUBlitter::BlitArgs blitArgs{
        .source = { .texture = srcTexture->getTexture(), .aspect = srcTexture->getAspect(),
                    .origin = sourceOrigin, .extent = sourceSize, .mipLevel = 0, .layerOrDepth = 0, },
        .destination = { .texture = dstTexture->getTexture(), .aspect = dstTexture->getAspect(),
                         .origin = destinationOrigin, .extent = destinationSize, .mipLevel = 0, .layerOrDepth = 0, },
        .filter = filter,

Review comment (suggestion):
please avoid C-style casts, using something like `static_cast<uint32_t>(...` instead.

Code after:
    FILAMENT_CHECK_PRECONDITION(sourceViewport.left >= 0 && sourceViewport.bottom >= 0 &&
                                destinationViewport.left >= 0 && destinationViewport.bottom >= 0)
            << "Source and destination viewports must be positive.";

    // We always blit from/to the COLOR0 attachment.
    auto sourceAttachment {sourceTarget->getColorAttachmentInfos()[0]};
    Handle<HwTexture> sourceTextureHandle{ sourceAttachment.handle };
    auto destinationAttachment {destinationTarget->getColorAttachmentInfos()[0]};
    Handle<HwTexture> destinationTextureHandle{
        destinationAttachment.handle
    };

    if (UTILS_UNLIKELY(!sourceTextureHandle || !destinationTextureHandle)) {
        FWGPU_LOGE << "blitDEPRECATED could not find a valid color attachment to blit.";
        return;
    }

    // WebGPU's texture coordinates are top-left, while Filament's are bottom-left.
    // We need to flip the y-coordinate for both source and destination.
    auto const sourceTexture{ handleCast<WebGPUTexture>(sourceTextureHandle) };
    auto const destinationTexture{ handleCast<WebGPUTexture>(destinationTextureHandle) };

    const uint32_t sourceTextureHeight{ sourceTexture->height };
    const uint32_t flippedSourceY{ sourceTextureHeight - sourceViewport.bottom -
                                   sourceViewport.height };

    const uint32_t destinationTextureHeight{ destinationTexture->height };
    const uint32_t flippedDestinationY{ destinationTextureHeight - destinationViewport.bottom -
                                        destinationViewport.height };

    const wgpu::Origin2D sourceOrigin{ static_cast<uint32_t>(sourceViewport.left), flippedSourceY };
    const wgpu::Origin2D destinationOrigin{ static_cast<uint32_t>(destinationViewport.left),
        flippedDestinationY };

    const wgpu::Extent2D sourceSize{ static_cast<uint32_t>(sourceViewport.width),
        static_cast<uint32_t>(sourceViewport.height) };
    const wgpu::Extent2D destinationSize{ static_cast<uint32_t>(destinationViewport.width),
        static_cast<uint32_t>(destinationViewport.height) };
    
    bool reusedCommandEncoder{ true };
    if (mCommandEncoder) {
        flush();
    } else {
        reusedCommandEncoder = false;
        const wgpu::CommandEncoderDescriptor desc{ .label = "blit_deprecated_command" };
        mCommandEncoder = mDevice.CreateCommandEncoder(&desc);
    }

    const WebGPUBlitter::BlitArgs blitArgs{
        .source = { .texture = sourceTexture->getTexture(),
                    .aspect = sourceTexture->getAspect(),