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

Code before:
      mMipmapGenerationStrategy{ src->mMipmapGenerationStrategy },
      mWebGPUFormat{ src->mWebGPUFormat },
      mAspect{ src->mAspect },
      mWebGPUUsage{ src->mWebGPUUsage },
      mViewUsage{ src->mViewUsage },
      mBlockWidth{ src->mBlockWidth },
      mBlockHeight{ src->mBlockHeight },
      mArrayLayerCount{ src->mArrayLayerCount },
      mTexture{ src->mTexture },
      mDefaultMipLevel{ baseLevel },
      mDefaultBaseArrayLayer{ src->mArrayLayerCount },
      mDefaultTextureView{ makeTextureView(mDefaultMipLevel, levelCount, 0, mDefaultBaseArrayLayer,
              toWebGPUTextureViewDimension(src->target)) },
      mMsaaSidecarTexture{ src->mMsaaSidecarTexture },
      mSwizzle{}{}

WebGPUTexture::WebGPUTexture(const WebGPUTexture* src,
        const wgpu::TextureComponentSwizzle nextSwizzle) noexcept
    : HwTexture{ src->target, src->levels, src->samples, src->width, src->height, src->depth,
              src->format, src->usage},
      mViewFormat{ src->mViewFormat },
      mMipmapGenerationStrategy{ src->mMipmapGenerationStrategy },
      mWebGPUFormat{ src->mWebGPUFormat },
      mAspect{ src->mAspect },
      mWebGPUUsage{ src->mWebGPUUsage },
      mViewUsage{ src->mViewUsage },
      mBlockWidth{ src->mBlockWidth },
      mBlockHeight{ src->mBlockHeight },
      mArrayLayerCount{ src->mArrayLayerCount },
      mTexture{ src->mTexture },
      mDefaultMipLevel{ src->mDefaultMipLevel },
      mDefaultBaseArrayLayer{ 0 },
      mMsaaSidecarTexture{src->mMsaaSidecarTexture},
      mSwizzle{ composeSwizzle(src->getSwizzle(), nextSwizzle) } {
    wgpu::TextureComponentSwizzleDescriptor swizzleDesc{};
    swizzleDesc.swizzle = mSwizzle;

    const wgpu::TextureViewDescriptor viewDesc{
        .nextInChain = &swizzleDesc,
        .label = "swizzled_texture_view",
        .format = mTexture.GetFormat(),
        .dimension = src->getViewDimension(),
    };
    mDefaultTextureView = mTexture.CreateView(&viewDesc);
    FILAMENT_CHECK_POSTCONDITION(mDefaultTextureView) << "Failed to create swizzled Texture view";
}

wgpu::Texture const& WebGPUTexture::getMsaaSidecarTexture(const uint8_t sampleCount) const {
    if (mMsaaSidecarTexture == nullptr) {
        return mMsaaSidecarTexture; // nullptr (no such sidecar)
    }

Review comment (suggestion):
I can see here that compose should work fine if the source sizzle is undefined (not a sizzle view) as well as if it is. A comment to that effect might help someone working with the code (or if `std::optional` or something is used that becomes more explicit).

Code after:
            .arrayLayerCount = mDefaultBaseArrayLayer,
        };
        mDefaultTextureView = mTexture.CreateView(&viewDesc);
        FILAMENT_CHECK_POSTCONDITION(mDefaultTextureView)
                << "Failed to create swizzled Texture view";
    }
}

WebGPUTexture::WebGPUTexture(const WebGPUTexture* src,
        const wgpu::TextureComponentSwizzle nextSwizzle) noexcept
    : HwTexture{ src->target, src->levels, src->samples, src->width, src->height, src->depth,
              src->format, src->usage},
      mViewFormat{ src->mViewFormat },
      mMipmapGenerationStrategy{ src->mMipmapGenerationStrategy },
      mWebGPUFormat{ src->mWebGPUFormat },
      mAspect{ src->mAspect },
      mWebGPUUsage{ src->mWebGPUUsage },
      mViewUsage{ src->mViewUsage },
      mBlockWidth{ src->mBlockWidth },
      mBlockHeight{ src->mBlockHeight },
      mArrayLayerCount{ src->mArrayLayerCount },
      mTexture{ src->mTexture },
      mDefaultMipLevel{ src->mDefaultMipLevel },
      mDefaultBaseArrayLayer{ 0 },
      mMsaaSidecarTexture{src->mMsaaSidecarTexture},
      mSwizzle{ composeSwizzle(src->getSwizzle(), nextSwizzle) } {
    wgpu::TextureComponentSwizzleDescriptor swizzleDesc{};
    swizzleDesc.swizzle = mSwizzle;

    const wgpu::TextureViewDescriptor viewDesc{
        .nextInChain = &swizzleDesc,
        .label = "swizzled_texture_view",
        .format = mTexture.GetFormat(),
        .dimension = src->getViewDimension(),
    };
    mDefaultTextureView = mTexture.CreateView(&viewDesc);
    FILAMENT_CHECK_POSTCONDITION(mDefaultTextureView) << "Failed to create swizzled Texture view";
}

bool WebGPUTexture::supportsMultipleMipLevelsViaStorageBinding(const wgpu::TextureFormat format) {
    return storageBindingCompatibleFormatForViewFormat(format) != wgpu::TextureFormat::Undefined;
}

wgpu::TextureView WebGPUTexture::makeAttachmentTextureView(const uint8_t mipLevel,
        const uint32_t arrayLayer, const uint32_t layerCount) {
    // TODO: there's an optimization to be made here to return mDefaultTextureView.
    //  Problem: mDefaultTextureView is a view of the entire texture,
    //  but this function (and its callers) expects a single-slice view.
    //  Returning the whole texture view for a single-slice request seems wrong.
    return makeTextureView(mipLevel, 1, arrayLayer, layerCount,
            layerCount > 1 ? wgpu::TextureViewDimension::e2DArray