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

Code before:
namespace filament::backend {

WebGPUSwapChain::WebGPUSwapChain(wgpu::Surface&& surface, wgpu::Extent2D const& extent,
        wgpu::Adapter const& adapter, wgpu::Device const& device, uint64_t flags)
    : mDevice(device),
      mSurface(surface),
      mNeedStencil((flags & SWAP_CHAIN_HAS_STENCIL_BUFFER) != 0),
      mDepthFormat(selectDepthFormat(device.HasFeature(wgpu::FeatureName::Depth32FloatStencil8),
              mNeedStencil)),
      mDepthTexture(createDepthTexture(device, extent, mDepthFormat)),
      mDepthTextureView(createDepthTextureView(mDepthTexture, mDepthFormat, mNeedStencil)),
      mType(SwapChainType::SURFACE),
      mHeadlessWidth(0),
      mHeadlessHeight(0) {

    wgpu::SurfaceCapabilities capabilities = {};
    if (!mSurface.GetCapabilities(adapter, &capabilities)) {
        FWGPU_LOGW << "Failed to get WebGPU surface capabilities" << utils::io::endl;
    } else {
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceCapabilitiesDetails(capabilities);
#endif
    }
    const bool useSRGBColorSpace = (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0;
    const bool needStencil = (flags & SWAP_CHAIN_HAS_STENCIL_BUFFER) != 0;
    initConfig(mConfig, device, capabilities, extent, useSRGBColorSpace);
    mDepthFormat = selectDepthFormat(device.HasFeature(wgpu::FeatureName::Depth32FloatStencil8),
            needStencil);
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
    printSurfaceConfiguration(mConfig, mDepthFormat);
#endif
    mSurface.Configure(&mConfig);
}

WebGPUSwapChain::WebGPUSwapChain(wgpu::Extent2D const& extent,
        wgpu::Adapter const& adapter, wgpu::Device const& device, uint64_t flags)
    : mType(SwapChainType::HEADLESS),
      mHeadlessWidth(extent.width),
      mHeadlessHeight(extent.height){

    const wgpu::TextureDescriptor textureDescriptor = {
        .usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc,
        .dimension = wgpu::TextureDimension::e2D,
        .size = { mHeadlessWidth, mHeadlessHeight, 1 },
        .format = wgpu::TextureFormat::RGBA8Unorm,
        .mipLevelCount = 1,
        .sampleCount = 1,
    };

     mRenderTargetTextures.fill(nullptr);
     mRenderTargetViews.fill(nullptr);

Review comment (suggestion):
nit: I recommend using a designated initializer even in these nested cases for robustness at least. Clarity is not much of an issue here because of the variable names, e.g. `= { .with=mHeadlessWidth, ...`

Code after:
    } else {
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceCapabilitiesDetails(capabilities);
#endif
    }
    const bool useSRGBColorSpace = (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0;

    initConfig(mConfig, device, capabilities, extent, useSRGBColorSpace);
    mDepthFormat = selectDepthFormat(device.HasFeature(wgpu::FeatureName::Depth32FloatStencil8),
            mNeedStencil);
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
    printSurfaceConfiguration(mConfig, mDepthFormat);
#endif
    mSurface.Configure(&mConfig);
}

WebGPUSwapChain::WebGPUSwapChain(wgpu::Extent2D const& extent,
        wgpu::Adapter const& adapter, wgpu::Device const& device, uint64_t flags)
    : mDevice{device},
      mNeedStencil{(flags & SWAP_CHAIN_HAS_STENCIL_BUFFER) != 0},
      mDepthFormat{selectDepthFormat(device.HasFeature(wgpu::FeatureName::Depth32FloatStencil8),
            mNeedStencil)},
      mDepthTexture{createDepthTexture(device, extent, mDepthFormat)},
      mDepthTextureView{createDepthTextureView(mDepthTexture, mDepthFormat, mNeedStencil)},
      mType{SwapChainType::HEADLESS},
      mHeadlessWidth{extent.width},
      mHeadlessHeight{extent.height}{

    mConfig.format = wgpu::TextureFormat::RGBA8Unorm;
    mConfig.device = device;
    mConfig.usage = wgpu::TextureUsage::RenderAttachment;
    mConfig.width = extent.width;
    mConfig.height = extent.height;
    mConfig.presentMode = wgpu::PresentMode::Fifo;
    mConfig.alphaMode = wgpu::CompositeAlphaMode::Auto;

    mDepthFormat = selectDepthFormat(device.HasFeature(wgpu::FeatureName::Depth32FloatStencil8),
                mNeedStencil);
    const wgpu::TextureDescriptor textureDescriptor = {
        .usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc,
        .dimension = wgpu::TextureDimension::e2D,
        .size = { .width = mHeadlessWidth, .height = mHeadlessHeight, .depthOrArrayLayers = 1 },
        .format = wgpu::TextureFormat::RGBA8Unorm,
        .mipLevelCount = 1,
        .sampleCount = 1,
    };

    const wgpu::TextureViewDescriptor viewDesc = {
        .format = textureDescriptor.format,
        .dimension = wgpu::TextureViewDimension::e2D,
        .baseMipLevel = 0,