Repository: google/filament
File: filament/src/details/Material.cpp (C++)

Code before:
            assert_invariant(Variant::isValidDepthVariant(variant));
            mCachedPrograms[variant.key] = pDefaultMaterial->mCachedPrograms[variant.key];
        }
    }
}

void FMaterial::processDescriptorSets(FEngine& engine, MaterialParser const* const parser) {
    UTILS_UNUSED_IN_RELEASE bool success;


    success = parser->getDescriptorBindings(&mProgramDescriptorBindings);
    assert_invariant(success);

    std::array<backend::DescriptorSetLayout, 2> descriptorSetLayout;
    success = parser->getDescriptorSetLayout(&descriptorSetLayout);
    assert_invariant(success);

    // Setup Labels for debugging
    utils::CString namestr;
    utils::StaticString perViewStr = "_perView";
    utils::StaticString singleStr = "_single";

    parser->getName(&namestr);

    utils::CString singleLabel(namestr.c_str(), namestr.length() + singleStr.length());
    memccpy(singleLabel.c_str() + namestr.length(), singleStr.c_str(), '\0', singleStr.length());

    utils::CString perViewLabel(namestr.c_str(), namestr.length() + perViewStr.length());
    memccpy(perViewLabel.c_str() + namestr.length(), perViewStr.c_str(), '\0', perViewStr.length());

    descriptorSetLayout[0].label = singleLabel;
    descriptorSetLayout[1].label = perViewLabel;

    mDescriptorSetLayout = {
            engine.getDescriptorSetLayoutFactory(),
            engine.getDriverApi(), std::move(descriptorSetLayout[0]) };

    mPerViewDescriptorSetLayout = {
            engine.getDescriptorSetLayoutFactory(),
            engine.getDriverApi(), std::move(descriptorSetLayout[1]) };
}

descriptor_binding_t FMaterial::getSamplerBinding(
        std::string_view const& name) const {
    return mSamplerInterfaceBlock.getSamplerInfo(name)->binding;
}

template bool FMaterial::setConstant<int32_t>(uint32_t id, int32_t value) noexcept;
template bool FMaterial::setConstant<float>(uint32_t id, float value) noexcept;
template bool FMaterial::setConstant<bool>(uint32_t id, bool value) noexcept;


Review comment (suggestion):
We should really define a `utils::CString::append` method in terms of `utils::CString::insert`. That would make this cleaner.

Code after:
        assert_invariant(pDefaultMaterial);
        auto const allDepthVariants = VariantUtils::getDepthVariants();
        for (auto const variant: allDepthVariants) {
            assert_invariant(Variant::isValidDepthVariant(variant));
            mCachedPrograms[variant.key] = pDefaultMaterial->mCachedPrograms[variant.key];
        }
    }
}

void FMaterial::processDescriptorSets(FEngine& engine, MaterialParser const* const parser) {
    UTILS_UNUSED_IN_RELEASE bool success;

    success = parser->getDescriptorBindings(&mProgramDescriptorBindings);
    assert_invariant(success);

    backend::DescriptorSetLayout descriptorSetLayout;
    success = parser->getDescriptorSetLayout(&descriptorSetLayout);
    assert_invariant(success);
    auto perMatLabel = mName;
    perMatLabel.append("_perMat");
    descriptorSetLayout.label = std::move(perMatLabel);

    // get the PER_VIEW descriptor binding info
    auto perViewDescriptorSetLayout =
            descriptor_sets::getPerViewDescriptorSetLayout(mMaterialDomain, mVariantFilterMask,
            mIsVariantLit || mHasShadowMultiplier, mReflectionMode, mRefractionMode);
    auto perViewLabel = mName;
    perViewLabel.append("_perView");
    perViewDescriptorSetLayout.label = std::move(perViewLabel);

    // get the PER_RENDERABLE and PER_VIEW descriptor binding info
    for (auto&& [bindingPoint, descriptorSetLayout] : {
            std::pair{ DescriptorSetBindingPoints::PER_RENDERABLE,
                    descriptor_sets::getPerRenderableLayout() },
            std::pair{ DescriptorSetBindingPoints::PER_VIEW,
                    perViewDescriptorSetLayout }}) {
        Program::DescriptorBindingsInfo& descriptors = mProgramDescriptorBindings[+bindingPoint];
        descriptors.reserve(descriptorSetLayout.bindings.size());
        for (auto const& entry: descriptorSetLayout.bindings) {
            auto const& name = descriptor_sets::getDescriptorName(bindingPoint, entry.binding);
            descriptors.push_back({ name, entry.type, entry.binding });
        }
    }

    mDescriptorSetLayout = {
            engine.getDescriptorSetLayoutFactory(),
            engine.getDriverApi(), std::move(descriptorSetLayout) };

    mPerViewDescriptorSetLayout = {
            engine.getDescriptorSetLayoutFactory(),
            engine.getDriverApi(), perViewDescriptorSetLayout };