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

Code before:
    std::array<wgpu::TextureView, MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT> customColorViews{};
    uint32_t customColorViewCount = 0;
    wgpu::TextureView customDepthView = nullptr;
    wgpu::TextureFormat customDepthFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureFormat customStencilFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureView customStencilView = nullptr;

    mCurrentRenderTarget = renderTarget;
    if (renderTarget->isDefaultRenderTarget()) {
        assert_invariant(mSwapChain && mTextureView);
        defaultColorView = mTextureView;
        defaultDepthStencilView = mSwapChain->getDepthTextureView();
    } 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* hwTexture = handleCast<WGPUTexture>(colorInfos[i].handle);
                if (hwTexture) {
                    FILAMENT_CHECK_POSTCONDITION(colorInfos[i].layer < renderTarget->getLayerCount())
                    << "Color attachment " << i << " requests layer "
                    << colorInfos[i].layer << " but render target has only "
                    << renderTarget->getLayerCount() << ".";
                    const uint8_t mipLevel = colorInfos[i].level;
                    const uint32_t arrayLayer = colorInfos[i].layer;
                    customColorViews[customColorViewCount++] = hwTexture->getOrMakeTextureView(mipLevel, arrayLayer);
                }
            }
        }

        const auto& depthInfo = renderTarget->getDepthAttachmentInfo();
        if (depthInfo.handle) {
            FILAMENT_CHECK_POSTCONDITION(depthInfo.layer < renderTarget->getLayerCount())
            << "Depth attachment requests layer " << depthInfo.layer
            << "but render target has only " << renderTarget->getLayerCount() << ".";
            auto* hwTexture = handleCast<WGPUTexture>(depthInfo.handle);
            if (hwTexture) {
                const uint8_t depthMipLevel = depthInfo.level;
                const uint32_t depthArrayLayer = depthInfo.layer;
                customDepthView = hwTexture->getOrMakeTextureView(depthMipLevel,depthArrayLayer);
                customDepthFormat = hwTexture->getFormat();
            }
        }

        const auto& stencilInfo = renderTarget->getStencilAttachmentInfo();
        if (stencilInfo.handle) {
            // If depth and stencil use the same texture handle, this will re-cast but that's fine.
            auto* hwTexture = handleCast<WGPUTexture>(stencilInfo.handle);
            if (hwTexture) {
                FILAMENT_CHECK_POSTCONDITION(stencilInfo.layer < renderTarget->getLayerCount())
                        << "Stencil attachment requests layer " << stencilInfo.layer

Review comment (suggestion):
`getOrMakeTextureView` is kinda confusing since its only calling `makeTextureView`. Why dont we just overload `makeTextureView`

Code after:
void WebGPUDriver::compilePrograms(CompilerPriorityQueue priority,
        CallbackHandler* handler, CallbackHandler::Callback callback, void* user) {
    if (callback) {
        scheduleCallback(handler, user, callback);
    }
}

void WebGPUDriver::beginRenderPass(Handle<HwRenderTarget> rth, RenderPassParams const& params) {
    assert_invariant(mCommandEncoder);
    auto* renderTarget = handleCast<WGPURenderTarget>(rth);

    wgpu::TextureView defaultColorView = nullptr;
    wgpu::TextureView defaultDepthStencilView = nullptr;

    std::array<wgpu::TextureView, MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT> customColorViews{};
    uint32_t customColorViewCount = 0;
    wgpu::TextureView customDepthView = nullptr;
    wgpu::TextureFormat customDepthFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureFormat customStencilFormat = wgpu::TextureFormat::Undefined;
    wgpu::TextureView customStencilView = nullptr;

    mCurrentRenderTarget = renderTarget;
    if (renderTarget->isDefaultRenderTarget()) {
        assert_invariant(mSwapChain && mTextureView);
        defaultColorView = mTextureView;
        defaultDepthStencilView = mSwapChain->getDepthTextureView();
    } 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* hwTexture = handleCast<WGPUTexture>(colorInfos[i].handle);
                if (hwTexture) {
                    FILAMENT_CHECK_POSTCONDITION(
                            colorInfos[i].layer < renderTarget->getLayerCount())
                            << "Color attachment " << i << " requests layer " << colorInfos[i].layer
                            << " but render target has only " << renderTarget->getLayerCount()
                            << ".";
                    const uint8_t mipLevel = colorInfos[i].level;
                    const uint32_t arrayLayer = colorInfos[i].layer;
                    customColorViews[customColorViewCount++] =
                            hwTexture->getOrMakeTextureView(mipLevel, arrayLayer);
                }
            }
        }

        const auto& depthInfo = renderTarget->getDepthAttachmentInfo();
        if (depthInfo.handle) {
            FILAMENT_CHECK_POSTCONDITION(depthInfo.layer < renderTarget->getLayerCount())
                    << "Depth attachment requests layer " << depthInfo.layer
                    << "but render target has only " << renderTarget->getLayerCount() << ".";