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

Code before:

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 surfaceSize = mPlatform.getSurfaceExtent(mNativeWindow);
    mTextureView = mSwapChain->getCurrentSurfaceTextureView(surfaceSize);
    assert_invariant(mTextureView);

    assert_invariant(mDefaultRenderTarget);

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

    newTargetFlags = filament::backend::TargetBufferFlags::NONE;

    //Assuming Color and Depth are always present.
    newTargetFlags |= filament::backend::TargetBufferFlags::COLOR;
    if (depthFormat != wgpu::TextureFormat::Undefined) {
        newTargetFlags |= filament::backend::TargetBufferFlags::DEPTH;

        if (depthFormat == wgpu::TextureFormat::Depth24PlusStencil8 ||
                depthFormat == wgpu::TextureFormat::Depth32FloatStencil8) {
            newTargetFlags |= filament::backend::TargetBufferFlags::STENCIL;
        }
    }
    mDefaultRenderTarget->setTargetFlags(newTargetFlags);

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

void WebGPUDriver::commit(Handle<HwSwapChain> sch) {
    wgpu::CommandBufferDescriptor commandBufferDescriptor{
        .label = "command_buffer",
    };
    mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
    assert_invariant(mCommandBuffer);
    mCommandEncoder = nullptr;

Review comment (suggestion):
this looks unnecessary, line before already has `TargetBufferFlags newTargetFlags = filament::backend::TargetBufferFlags::NONE;` 

Code after:

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 surfaceSize = mPlatform.getSurfaceExtent(mNativeWindow);
    mTextureView = mSwapChain->getCurrentSurfaceTextureView(surfaceSize);
    assert_invariant(mTextureView);

    assert_invariant(mDefaultRenderTarget);

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

    //Assuming Color and Depth are always present.
    newTargetFlags |= filament::backend::TargetBufferFlags::COLOR;
    if (depthFormat != wgpu::TextureFormat::Undefined) {
        newTargetFlags |= filament::backend::TargetBufferFlags::DEPTH;

        if (depthFormat == wgpu::TextureFormat::Depth24PlusStencil8 ||
                depthFormat == wgpu::TextureFormat::Depth32FloatStencil8) {
            newTargetFlags |= filament::backend::TargetBufferFlags::STENCIL;
        }
    }
    mDefaultRenderTarget->setTargetFlags(newTargetFlags);

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

void WebGPUDriver::commit(Handle<HwSwapChain> sch) {
    wgpu::CommandBufferDescriptor commandBufferDescriptor{
        .label = "command_buffer",
    };
    mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
    assert_invariant(mCommandBuffer);
    mCommandEncoder = nullptr;
    mQueue.Submit(1, &mCommandBuffer);