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

Code before:
        backend::descriptor_binding_t binding, Handle<HwTexture> th, SamplerParams params) {
    auto bindGroup = handleCast<WebGPUDescriptorSet>(dsh);
    auto texture = handleCast<WGPUTexture>(th);

    if (!bindGroup->getIsLocked()) {
        // Dawn will cache duplicate samplers, so we don't strictly need to maintain a cache.
        //  Making a cache might save us minor perf by reducing param translation
        auto sampler = makeSampler(params);
        // TODO making assumptions that size and offset mean the same thing here.
        wgpu::BindGroupEntry tEntry{ .binding = static_cast<uint32_t>(binding * 2),
            .textureView = texture->getTexView() };
        bindGroup->addEntry(tEntry.binding, std::move(tEntry));

        wgpu::BindGroupEntry sEntry{ .binding = static_cast<uint32_t>(binding * 2 + 1),
            .sampler = sampler };
        bindGroup->addEntry(sEntry.binding, std::move(sEntry));
    }
}

void WebGPUDriver::bindDescriptorSet(Handle<HwDescriptorSet> dsh,
        backend::descriptor_set_t setIndex, backend::DescriptorSetOffsetArray&& offsets) {
    assert_invariant(setIndex < MAX_DESCRIPTOR_SET_COUNT);
    const auto bindGroup = handleCast<WebGPUDescriptorSet>(dsh);
    const auto wbg = bindGroup->lockAndReturn(mDevice);

    mCurrentDescriptorSets[setIndex] = { wbg, bindGroup->countEntitiesWithDynamicOffsets(),
        std::move(offsets) };
}

void WebGPUDriver::setDebugTag(HandleBase::HandleId handleId, utils::CString tag) {
}
wgpu::Sampler WebGPUDriver::makeSampler(SamplerParams const& params) {
    wgpu::SamplerDescriptor desc{};

    desc.label = "TODO";
    desc.addressModeU = fWrapModeToWAddressMode(params.wrapS);
    desc.addressModeV = fWrapModeToWAddressMode(params.wrapR);
    desc.addressModeW = fWrapModeToWAddressMode(params.wrapT);
    if (params.compareMode == SamplerCompareMode::COMPARE_TO_TEXTURE) {
        switch (params.filterMag) {
            case SamplerMagFilter::NEAREST: {
                desc.magFilter = wgpu::FilterMode::Nearest;
                break;
            }
            case SamplerMagFilter::LINEAR: {
                desc.magFilter = wgpu::FilterMode::Linear;
                break;
            }
        }
        switch (params.filterMin) {
            case SamplerMinFilter::NEAREST: {

Review comment (suggestion):
nit: consider designated initializer:
```cpp
mCurrentDescriptorSets[setIndex] = {
    .bindGroup = wbg,
    .offsetCount = bindGroup->countEntitiesWithDynamicOffsets(),
    .offsets = std::move(offsets) };
```

Code after:
        backend::descriptor_binding_t binding, Handle<HwTexture> th, SamplerParams params) {
    auto bindGroup = handleCast<WebGPUDescriptorSet>(dsh);
    auto texture = handleCast<WGPUTexture>(th);

    if (!bindGroup->getIsLocked()) {
        // Dawn will cache duplicate samplers, so we don't strictly need to maintain a cache.
        //  Making a cache might save us minor perf by reducing param translation
        auto sampler = makeSampler(params);
        // TODO making assumptions that size and offset mean the same thing here.
        wgpu::BindGroupEntry tEntry{ .binding = static_cast<uint32_t>(binding * 2),
            .textureView = texture->getTexView() };
        bindGroup->addEntry(tEntry.binding, std::move(tEntry));

        wgpu::BindGroupEntry sEntry{ .binding = static_cast<uint32_t>(binding * 2 + 1),
            .sampler = sampler };
        bindGroup->addEntry(sEntry.binding, std::move(sEntry));
    }
}

void WebGPUDriver::bindDescriptorSet(Handle<HwDescriptorSet> dsh,
        backend::descriptor_set_t setIndex, backend::DescriptorSetOffsetArray&& offsets) {
    assert_invariant(setIndex < MAX_DESCRIPTOR_SET_COUNT);
    const auto bindGroup = handleCast<WebGPUDescriptorSet>(dsh);
    const auto wbg = bindGroup->lockAndReturn(mDevice);

    mCurrentDescriptorSets[setIndex] = { .bindGroup = wbg,
        .offsetCount = bindGroup->countEntitiesWithDynamicOffsets(),
        .offsets = std::move(offsets) };
}

void WebGPUDriver::setDebugTag(HandleBase::HandleId handleId, utils::CString tag) {
}
wgpu::Sampler WebGPUDriver::makeSampler(SamplerParams const& params) {
    wgpu::SamplerDescriptor desc{};

    desc.label = "TODO";
    desc.addressModeU = fWrapModeToWAddressMode(params.wrapS);
    desc.addressModeV = fWrapModeToWAddressMode(params.wrapR);
    desc.addressModeW = fWrapModeToWAddressMode(params.wrapT);
    if (params.compareMode == SamplerCompareMode::COMPARE_TO_TEXTURE) {
        switch (params.filterMag) {
            case SamplerMagFilter::NEAREST: {
                desc.magFilter = wgpu::FilterMode::Nearest;
                break;
            }
            case SamplerMagFilter::LINEAR: {
                desc.magFilter = wgpu::FilterMode::Linear;
                break;
            }
        }
        switch (params.filterMin) {