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

Code before:
        .viewFormats = &depthFormat
    };
    wgpu::Texture depthTexture = device.CreateTexture(&descriptor);
    FILAMENT_CHECK_POSTCONDITION(depthTexture) << "Failed to create depth texture with width "
                                               << extent.width << " and height " << extent.height;
    return depthTexture;
}

[[nodiscard]] wgpu::TextureView createDepthTextureView(wgpu::Texture const& depthTexture,
        wgpu::TextureFormat depthFormat, bool needStencil) {
    wgpu::TextureViewDescriptor descriptor{
        .label = "depth_texture_view",
        .dimension = wgpu::TextureViewDimension::e2D,
        .baseMipLevel = 0,
        .mipLevelCount = 1,
        .baseArrayLayer = 0,
        .arrayLayerCount = 1,
        .aspect = wgpu::TextureAspect::DepthOnly,
        .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment
    };
    if (needStencil) {
        descriptor.aspect = wgpu::TextureAspect::All;
        descriptor.format = depthFormat;
    } else {
        descriptor.aspect = wgpu::TextureAspect::DepthOnly;
        if (depthFormat == wgpu::TextureFormat::Depth32FloatStencil8) {
            descriptor.format = wgpu::TextureFormat::Depth32Float;
        } else if (depthFormat == wgpu::TextureFormat::Depth24PlusStencil8) {
            descriptor.format = wgpu::TextureFormat::Depth24Plus;
        } else {
            descriptor.format = depthFormat;
        }
    }
    wgpu::TextureView depthTextureView = depthTexture.CreateView(&descriptor);
    FILAMENT_CHECK_POSTCONDITION(depthTextureView) << "Failed to create depth texture view";
    return depthTextureView;
}

}// namespace

WebGPUSwapChain::WebGPUSwapChain(wgpu::Surface&& surface, wgpu::Extent2D const& surfaceSize,
        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, surfaceSize, mDepthFormat)),
      mDepthTextureView(createDepthTextureView(mDepthTexture, mDepthFormat, mNeedStencil)) {
    wgpu::SurfaceCapabilities capabilities = {};
    if (!mSurface.GetCapabilities(adapter, &capabilities)) {

Review comment (bug):
is this really happening that `needStencil` is `false` and we are getting a depth format with stencil bits? I wonder if we should be panicking in this case; that feels like a Filament bug.

Code after:
        .viewFormats = &depthFormat
    };
    wgpu::Texture depthTexture = device.CreateTexture(&descriptor);
    FILAMENT_CHECK_POSTCONDITION(depthTexture) << "Failed to create depth texture with width "
                                               << extent.width << " and height " << extent.height;
    return depthTexture;
}

[[nodiscard]] wgpu::TextureView createDepthTextureView(wgpu::Texture const& depthTexture,
        wgpu::TextureFormat const& depthFormat, bool const needStencil) {
    wgpu::TextureViewDescriptor descriptor{
        .label = "depth_texture_view",
        .dimension = wgpu::TextureViewDimension::e2D,
        .baseMipLevel = 0,
        .mipLevelCount = 1,
        .baseArrayLayer = 0,
        .arrayLayerCount = 1,
        .aspect = wgpu::TextureAspect::DepthOnly,
        .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment
    };
    if (needStencil) {
        descriptor.aspect = wgpu::TextureAspect::All;
        descriptor.format = depthFormat;
    } else {
        descriptor.aspect = wgpu::TextureAspect::DepthOnly;
        if (depthFormat == wgpu::TextureFormat::Depth32FloatStencil8) {
            descriptor.format = wgpu::TextureFormat::Depth32Float;
        } else if (depthFormat == wgpu::TextureFormat::Depth24PlusStencil8) {
            descriptor.format = wgpu::TextureFormat::Depth24Plus;
        } else {
            descriptor.format = depthFormat;
        }
    }
    wgpu::TextureView depthTextureView = depthTexture.CreateView(&descriptor);
    FILAMENT_CHECK_POSTCONDITION(depthTextureView) << "Failed to create depth texture view";
    return depthTextureView;
}

}// namespace

WebGPUSwapChain::WebGPUSwapChain(wgpu::Surface&& surface, wgpu::Extent2D const& surfaceSize,
        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, surfaceSize, mDepthFormat)),
      mDepthTextureView(createDepthTextureView(mDepthTexture, mDepthFormat, mNeedStencil)) {
    wgpu::SurfaceCapabilities capabilities = {};
    if (!mSurface.GetCapabilities(adapter, &capabilities)) {