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

Code before:
            static_cast<float>(params.viewport.height),
            params.depthRange.near,
            params.depthRange.far);
}

void WebGPUDriver::endRenderPass(int /* dummy */) {
    mRenderPassEncoder.End();
    mRenderPassEncoder = nullptr;
}

void WebGPUDriver::nextSubpass(int) {
    //todo
}

void WebGPUDriver::makeCurrent(Handle<HwSwapChain> drawSch, Handle<HwSwapChain> readSch) {
    ASSERT_PRECONDITION_NON_FATAL(drawSch == readSch,
            "WebGPU driver does not support distinct draw/read swap chains.");
    auto swapChain = handleCast<WebGPUSwapChain>(drawSch);
    mSwapChain = swapChain;
    assert_invariant(mSwapChain);

    wgpu::Extent2D extent{};
    if (!mSwapChain->isHeadless()) {
        extent = mPlatform.getSurfaceExtent(mNativeWindow);
    }
    mTextureView = mSwapChain->getNextTextureView(extent);

    assert_invariant(mTextureView);

    assert_invariant(mDefaultRenderTarget);

    wgpu::TextureFormat depthFormat = mSwapChain->getDepthFormat();
    TargetBufferFlags newTargetFlags = filament::backend::TargetBufferFlags::NONE;

    //Assuming Color always present in default render target.
    newTargetFlags |= filament::backend::TargetBufferFlags::COLOR;
    if (depthFormat != wgpu::TextureFormat::Undefined) {
        if (hasDepth(depthFormat)) {
            newTargetFlags |= filament::backend::TargetBufferFlags::DEPTH;
        }
        if (hasStencil(depthFormat)) {
            newTargetFlags |= filament::backend::TargetBufferFlags::STENCIL;
        }
    }
    mDefaultRenderTarget->setTargetFlags(newTargetFlags);

    if (!mCommandEncoder) {
        wgpu::CommandEncoderDescriptor commandEncoderDescriptor = { .label = "frame_command_encoder" };
        mCommandEncoder = mDevice.CreateCommandEncoder(&commandEncoderDescriptor);
    }
    assert_invariant(mCommandEncoder);

Review comment (suggestion):
This is confusing as well. Why would we NEED to send an extent at this point. Now, we are sending an empty (0,0) extent. Seems like at this point, we should either have an overload of getNextTextureView that does not take an extent. I think the reason there was a separate function before was to make clear when NOT sending an extent was OK. The key point has to do with resizing.

A suggestion:
```cpp
if (mSwapChain->isHeadless()) {
 mTextureView = mSwapChain->getNextTextureView();
} else {
  mTextureView = mSwapChain->getNextTextureView(mPlatform.getSurfaceExtent(mNativeWindow));
}
```
and/or maybe we do something with the naming of the functions for clarity (which was done before).

Code after:
                    << "Default render target requested stencil, but swap chain's depth format "
                    << (uint32_t) defaultDepthStencilFormat << " does not have a stencil aspect.";
        }

        if (any(renderTarget->getTargetFlags() & TargetBufferFlags::DEPTH) &&
                !(hasDepth(defaultDepthStencilFormat))) {
            FILAMENT_CHECK_POSTCONDITION(false)
                    << "Default render target requested depth, but swap chain's depth format "
                    << (uint32_t) defaultDepthStencilFormat << " does not have a depth aspect.";
        }
    } else {
        // Resolve views for custom render target
        const auto& colorInfos = renderTarget->getColorAttachmentInfos();
        for (int i = 0; i < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; ++i) {
            if (colorInfos[i].handle) {
                auto colorTexture = handleCast<WebGPUTexture>(colorInfos[i].handle);
                if (colorTexture) {
                    const uint8_t mipLevel = colorInfos[i].level;
                    const uint32_t arrayLayer = colorInfos[i].layer;
                    customColorViews[customColorViewCount] =
                            colorTexture->makeAttachmentTextureView(mipLevel, arrayLayer, renderTarget->getLayerCount());
                    if (msaaSidecarsRequired) {
                        const wgpu::TextureView msaaSidecarView{
                            colorTexture->makeMsaaSidecarTextureViewIfTextureSidecarExists(
                                    renderTarget->getSamples(), mipLevel, arrayLayer)
                        };
                        FILAMENT_CHECK_POSTCONDITION(msaaSidecarView)
                                << "Could not get a required MSAA sidecar texture view for color "
                                << customColorViewCount << "?";
                        customColorMsaaSidecarViews[customColorViewCount] = msaaSidecarView;
                    }
                    customColorViewCount++;
                }
            }
        }
        const auto& depthInfo = renderTarget->getDepthAttachmentInfo();
        const auto& stencilInfo = renderTarget->getStencilAttachmentInfo();

        Handle<HwTexture> depthStencilSourceHandle = {};
        uint8_t depthStencilMipLevel = 0;
        uint32_t depthStencilArrayLayer = 0;

        if (depthInfo.handle && stencilInfo.handle) {
            auto const depthTexture{handleCast<WebGPUTexture>(depthInfo.handle)->getTexture()};
            auto const stencilTexture{handleCast<WebGPUTexture>(stencilInfo.handle)->getTexture()};
            FILAMENT_CHECK_POSTCONDITION(depthTexture.Get() == stencilTexture.Get())
                    << "Filament must reference the same resource if both depth and stencil "
                       "textures are present";
        }

        if (depthInfo.handle) {