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

Code before:
            depthStencilSourceHandle = stencilInfo.handle;
            dsMipLevel = stencilInfo.level;
            dsArrayLayer = stencilInfo.layer;
        }

        if (depthStencilSourceHandle) {
            auto dsTexture = handleCast<WebGPUTexture>(depthStencilSourceHandle);
            if (dsTexture) {
                customDepthStencilView = dsTexture->getOrMakeTextureView(dsMipLevel, dsArrayLayer);
                customDepthStencilFormat = dsTexture->getViewFormat();

                if (any(renderTarget->getTargetFlags() & TargetBufferFlags::STENCIL) &&
                        !(customDepthStencilFormat == wgpu::TextureFormat::Depth24PlusStencil8 ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth32FloatStencil8 ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Stencil8)) {
                    FILAMENT_CHECK_POSTCONDITION(false)
                            << "Custom render target requested stencil, but the provided texture"
                               "format number"
                            << (uint32_t) customDepthStencilFormat
                            << " does not have a stencil aspect.";
                }
                if (any(renderTarget->getTargetFlags() & TargetBufferFlags::DEPTH) &&
                        !(customDepthStencilFormat == wgpu::TextureFormat::Depth16Unorm ||
                                customDepthStencilFormat == wgpu::TextureFormat::Depth32Float ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth24Plus ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth24PlusStencil8 ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth32FloatStencil8)) {
                    FILAMENT_CHECK_POSTCONDITION(false) << "Custom render target requested depth, "
                                                           "but the provided texture format number"
                                                        << (uint32_t) customDepthStencilFormat
                                                        << " does not have a depth aspect.";
                }
            }
        }
    }
    wgpu::RenderPassDescriptor renderPassDescriptor{};
    renderTarget->setUpRenderPassAttachments(renderPassDescriptor,
            params,
            defaultColorView,
            defaultDepthStencilView,
            customColorViews.data(),
            customColorViewCount,
            customDepthStencilView);

    mRenderPassEncoder = mCommandEncoder.BeginRenderPass(&renderPassDescriptor);


Review comment (suggestion):
from what I can tell, we are never actually checking that if we have both depth and stencil info that the underlying texture for each is the same.

Could we do something like?:
```cpp
if (depthInfo.handle && stencilInfo.handle) {
  auto depthTexture = handleCast<WebGPUTexture>(depthInfo.handle).getTexture();
  auto stencilTexture = handleCast<WebGPUTexture>(stencilInfo.handle)getTexture();
  FILAMENT_CHECK_POSTCONDITION(depthTexture.Get() == stencilTexture.Get()) ...
}
```

Code after:
        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) {
            depthStencilSourceHandle = depthInfo.handle;
            depthStencilMipLevel = depthInfo.level;
            depthStencilArrayLayer = depthInfo.layer;
        } else if (stencilInfo.handle) {
            depthStencilSourceHandle = stencilInfo.handle;
            depthStencilMipLevel = stencilInfo.level;
            depthStencilArrayLayer = stencilInfo.layer;
        }

        if (depthStencilSourceHandle) {
            auto dsTexture = handleCast<WebGPUTexture>(depthStencilSourceHandle);
            if (dsTexture) {
                customDepthStencilView = dsTexture->getOrMakeTextureView(depthStencilMipLevel, depthStencilArrayLayer);
                customDepthStencilFormat = dsTexture->getViewFormat();

                if (any(renderTarget->getTargetFlags() & TargetBufferFlags::STENCIL) &&
                        !(customDepthStencilFormat == wgpu::TextureFormat::Depth24PlusStencil8 ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth32FloatStencil8 ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Stencil8)) {
                    FILAMENT_CHECK_POSTCONDITION(false)
                            << "Custom render target requested stencil, but the provided texture"
                               "format number"
                            << (uint32_t) customDepthStencilFormat
                            << " does not have a stencil aspect.";
                }
                if (any(renderTarget->getTargetFlags() & TargetBufferFlags::DEPTH) &&
                        !(customDepthStencilFormat == wgpu::TextureFormat::Depth16Unorm ||
                                customDepthStencilFormat == wgpu::TextureFormat::Depth32Float ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth24Plus ||
                                customDepthStencilFormat ==
                                        wgpu::TextureFormat::Depth24PlusStencil8 ||
                                customDepthStencilFormat ==