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

Code before:
    struct StructureMipmapData {
        FrameGraphId<FrameGraphTexture> depth;
    };

    fg.addPass<StructureMipmapData>("StructureMipmap",
            [&](FrameGraph::Builder& builder, auto& data) {
                data.depth = builder.sample(depth);
                for (size_t i = 1; i < levelCount; i++) {
                    auto out = builder.createSubresource(data.depth, "Structure mip", {
                            .level = uint8_t(i)
                    });
                    out = builder.write(out, FrameGraphTexture::Usage::DEPTH_ATTACHMENT);
                    builder.declareRenderPass("Structure mip target", {
                            .attachments = { .depth = out }
                    });
                }
            },
            [=](FrameGraphResources const& resources, auto const& data, DriverApi& driver) {
                bindPostProcessDescriptorSet(driver);

                auto in = resources.getTexture(data.depth);
                auto& material = getPostProcessMaterial("mipmapDepth");
                FMaterial const* const ma = material.getMaterial(mEngine);
                auto pipeline = getPipelineState(ma);

                FMaterialInstance* const mi = mMaterialInstanceManager.getMaterialInstance(ma);

                // The first mip already exists, so we process n-1 lods
                for (size_t level = 0; level < levelCount - 1; level++) {
                    auto out = resources.getRenderPassInfo(level);

                    auto th = driver.createTextureView(in, level, 1);
                    mi->setParameter("depth", th, {
                        .filterMin = SamplerMinFilter::NEAREST_MIPMAP_NEAREST });
                    mi->commit(driver);
                    mi->use(driver);
                    renderFullScreenQuad(out, pipeline, driver);
                    driver.destroyTexture(th);
                }
            });

    return { depth, structurePass->picking };
}

// ------------------------------------------------------------------------------------------------

FrameGraphId<FrameGraphTexture> PostProcessManager::ssr(FrameGraph& fg,
        RenderPassBuilder const& passBuilder,
        FrameHistory const& frameHistory,
        CameraInfo const& cameraInfo,
        FrameGraphId<FrameGraphTexture> const structure,

Review comment (refactor):
For all these you could maybe have a helper on PostProcessManager so you don't have to repeat `mMaterialInstanceManager.` each time.


Code after:
                FrameGraphId<FrameGraphTexture> picking;
            };
            auto const& pickingRenderPass = fg.addPass<PickingRenderPassData>("Picking Render Pass",
                [&](FrameGraph::Builder& builder, auto& data) {
                    bool const isFL0 = mEngine.getDriverApi().getFeatureLevel() ==
                        FeatureLevel::FEATURE_LEVEL_0;

                    // TODO: Specify the precision for picking pass
                     width  = std::max(32u, uint32_t(std::ceil(float(width) * scale)));
                     height = std::max(32u, uint32_t(std::ceil(float(height) * scale)));
                    data.depth = builder.createTexture("Depth Buffer", {
                            .width = width, .height = height,
                            .format = isFL0 ? TextureFormat::DEPTH24 : TextureFormat::DEPTH32F });

                    data.depth = builder.write(data.depth,
                        FrameGraphTexture::Usage::DEPTH_ATTACHMENT);

                    data.picking = builder.createTexture("Picking Buffer", {
                            .width = width, .height = height,
                            .format = isFL0 ? TextureFormat::RGBA8 : TextureFormat::RG32F });

                    data.picking = builder.write(data.picking,
                        FrameGraphTexture::Usage::COLOR_ATTACHMENT);

                    builder.declareRenderPass("Picking Render Target", {
                            .attachments = {.color = { data.picking }, .depth = data.depth },
                            .clearFlags = TargetBufferFlags::COLOR0 | TargetBufferFlags::DEPTH
                        });
                },
                [=, passBuilder = passBuilder](FrameGraphResources const& resources,
                    auto const&, DriverApi& driver) mutable {
                        Variant pickingVariant(Variant::DEPTH_VARIANT);
                        pickingVariant.setPicking(true);

                        // bind the per-view descriptorSet that is used for the structure pass
                        getStructureDescriptorSet().bind(driver);

                        auto [target, params] = resources.getRenderPassInfo();
                        passBuilder.renderFlags(structureRenderFlags);
                        passBuilder.variant(pickingVariant);
                        passBuilder.commandTypeFlags(RenderPass::CommandTypeFlags::DEPTH);

                        RenderPass const pass{ passBuilder.build(mEngine, driver) };
                        driver.beginRenderPass(target, params);
                        pass.getExecutor().execute(mEngine, driver);
                        driver.endRenderPass();
                });

    return pickingRenderPass->picking;
}