Skip to content

processingbase/ProcessingManager.hpp

Namespaces

Name
sgns
sgns::sgprocessing
Artifact and manifest binary serialization.

Classes

Name
struct sgns::sgprocessing::ExecutorRegistryEntry
Executor registry entry wrapping a processor factory and checkpoint support flag (D-20).
struct sgns::sgprocessing::ProcessOutput
Structured output from Process() — typed artifact records + execution manifest (Phase 08, ARTF-01/02/04).
class sgns::sgprocessing::ProcessingManager

Types

Name
using sgns::sgprocessing::ProcessingProcessor ProcessingProcessor

Functions

Name
OUTCOME_HPP_DECLARE_ERROR_2(sgns::sgprocessing , ProcessingManager::Error )

Types Documentation

using ProcessingProcessor

using sgns::sgprocessing::ProcessingProcessor = typedef sgns::sgprocessing::ProcessingProcessor;

Functions Documentation

function OUTCOME_HPP_DECLARE_ERROR_2

OUTCOME_HPP_DECLARE_ERROR_2(
    sgns::sgprocessing ,
    ProcessingManager::Error 
)

Source code

#ifndef PROCESSING_MANAGER_HPP_
#define PROCESSING_MANAGER_HPP_

#include <outcome/sgprocmgr-outcome.hpp>
#include <util/sgprocmgr-logger.hpp>
#include <SGNSProcMain.hpp>

#include <processors/processing_processor_mnn_image.hpp>
#include <processors/processing_processor_mnn_string.hpp>
#include <processors/processing_processor_mnn_volume.hpp>
#include <processors/processing_processor_mnn_texture1d.hpp>
#include <processors/processing_processor_mnn_mat2.hpp>
#include <processors/processing_processor_mnn_mat3.hpp>
#include <processors/processing_processor_mnn_mat4.hpp>
#include <processors/processing_processor_mnn_vec2.hpp>
#include <processors/processing_processor_mnn_vec3.hpp>
#include <processors/processing_processor_mnn_vec4.hpp>
#include <processors/processing_processor_mnn_tensor.hpp>
#include <processors/processing_processor_mnn_texturecube.hpp>
#include <processors/processing_processor_mnn_bool.hpp>
#include <processors/processing_processor_mnn_buffer.hpp>
#include <processors/processing_processor_mnn_float.hpp>
#include <processors/processing_processor_mnn_int.hpp>
#include <processors/processing_processor_render.hpp>
#ifdef SGPROC_HAS_MNN_LLM
// PROC-01 (Phase 04-sgprocessing-integration): only compiled in when the linked MNN
// static library was actually built with MNN_BUILD_LLM=ON (see
// src/processors/CMakeLists.txt's configure-time detection) -- MNN_Llm.hpp itself
// only forward-declares MNN::Transformer::Llm, so this include is always safe; the
// guard exists solely because src/processors/CMakeLists.txt conditionally excludes
// processing_processor_mnn_llm.cpp's definition of StartProcessing() in checkouts
// (like this one) where the vendored MNN wasn't built with LLM support -- without
// this guard, RegisterProcessorFactory's lambda below would reference an
// MNN_Llm vtable with no definition anywhere, breaking the link for every consumer
// of this shared submodule branch.
#include <processors/processing_processor_mnn_llm.hpp>
#endif
#include <capability/capability_validator.hpp>
#include <execution/execution_context.hpp>
#include <artifacts/artifact_types.hpp>
#include <artifacts/execution_manifest.hpp>
#include <boost/asio/io_context.hpp>
#include <iostream>
#include <Generators.hpp>

namespace sgns::sgprocessing
{
    // Move enum to namespace level
    using ProcessingProcessor = sgns::sgprocessing::ProcessingProcessor;

    struct ExecutorRegistryEntry
    {
        std::function<std::unique_ptr<ProcessingProcessor>()> factory;
        bool supports_checkpointing = false;
    };

    struct ProcessOutput
    {
        std::vector<Artifact> artifacts;       
        ExecutionManifest    manifest;         
        std::vector<uint8_t> combinedHash;     

        // Backward-compatible accessors — delegate to combinedHash so existing callers
        // that treat the Process() return as std::vector<uint8_t> continue to compile (D-10).
        size_t size()  const { return combinedHash.size(); }
        bool   empty() const { return combinedHash.empty(); }
        auto   begin() const { return combinedHash.begin(); }
        auto   end()   const { return combinedHash.end(); }
        auto   begin()       { return combinedHash.begin(); }
        auto   end()         { return combinedHash.end(); }
    };

    class ProcessingManager
    {
    public:
        ~ProcessingManager();
        enum class Error
        {
            PROCESS_INFO_MISSING     = 1,
            INVALID_JSON             = 2,
            INVALID_BLOCK_PARAMETERS = 3,
            NO_PROCESSOR             = 4,
            MISSING_INPUT            = 5,
            INPUT_UNAVAIL            = 6,
            SHADER_COMPILE_FAILED    = 7,
            SPIRV_VALIDATION_FAILED  = 8,
            PROCESSING_FAILED        = 9,
            MODEL_MISSING            = 10,
            MODEL_FORMAT_UNSUPPORTED = 11,
            RENDER_SHADER_MISSING    = 12,
            UNKNOWN_PASS_TYPE        = 13,
        };
        static outcome::result<std::shared_ptr<ProcessingManager>> Create( const std::string &jsondata );

        outcome::result<uint64_t>             ParseBlockSize() const;
        outcome::result<void>                 CheckProcessValidity();
        outcome::result<ProcessOutput> Process( std::shared_ptr<boost::asio::io_context> ioc,
                                                std::vector<std::vector<uint8_t>>       &chunkhashes,
                                                sgns::ModelNode                         &model,
                                                std::vector<std::string>                &output_locations );

        outcome::result<ProcessOutput> Process( std::shared_ptr<boost::asio::io_context> ioc,
                                                std::vector<std::vector<uint8_t>>       &chunkhashes,
                                                sgns::ModelNode                         &model,
                                                std::vector<std::string>                &output_locations,
                                                ExecutionContext                        &externalExecCtx );

        void CanExecute( const sgns::Pass                         &pass,
                         sgns::sgprocessing::CanExecuteCallback callback );

        void RegisterProcessorFactory( const int                                            &name,
                                       std::function<std::unique_ptr<ProcessingProcessor>()> factoryFunction )
        {
            m_processorFactories[name] = std::move( factoryFunction );
        }

        void RegisterPassProcessorFactory( PassType                                          type,
                                           std::function<std::unique_ptr<ProcessingProcessor>()> factoryFunction,
                                           bool                                              supportsCheckpointing = false )
        {
            m_passFactories[type] = { std::move( factoryFunction ), supportsCheckpointing };
        }

        sgns::SgnsProcessing GetProcessingData();

        outcome::result<size_t> GetInputIndex( const std::string &input ) const;

        float GetProgress() const
        {
            if ( m_processor )
            {
                return m_processor->GetProgress();
            }
            return 0.0f;
        }

        const ExecutionManifest &GetLastManifest() const
        {
            return m_lastManifest;
        }

        static bool IsProcessingValid( const std::string &jsondata );
        static bool IsProcessingModelValid( const std::string &jsondata );

        static outcome::result<sgns::ModelNode> GetModelNodeFromJson( const std::string &jsondata );

    private:
        ProcessingManager() = default;
        outcome::result<void> Init( const std::string &jsondata );
        outcome::result<
            std::shared_ptr<std::pair<std::shared_ptr<std::vector<char>>, std::shared_ptr<std::vector<char>>>>>
             GetCidForProc( std::shared_ptr<boost::asio::io_context> ioc, sgns::ModelNode &model );
        void GetSubCidForProc( std::shared_ptr<boost::asio::io_context> ioc,
                               std::string                              url,
                               std::shared_ptr<std::vector<char>>       results );

        outcome::result<ProcessOutput> ProcessInternal( std::shared_ptr<boost::asio::io_context> ioc,
                                                        std::vector<std::vector<uint8_t>>       &chunkhashes,
                                                        sgns::ModelNode                         &model,
                                                        std::vector<std::string>                &output_locations,
                                                        ExecutionContext                        &execCtx );

        bool SetProcessorByName( const int &name )
        {
            auto factoryFunction = m_processorFactories.find( name );
            if ( factoryFunction != m_processorFactories.end() )
            {
                m_processor = factoryFunction->second();
                return true;
            }
            std::cerr << "Unknown processor name: " << name << std::endl;
            return false;
        }

        bool SetProcessorByPassType( PassType type )
        {
            auto factoryFunction = m_passFactories.find( type );
            if ( factoryFunction != m_passFactories.end() )
            {
                m_processor = factoryFunction->second.factory();
                return true;
            }
            std::cerr << "Unknown pass type: " << static_cast<int>( type ) << std::endl;
            return false;
        }

        sgns::sgprocmanager::Logger          m_logger = sgns::sgprocmanager::createLogger( "SGProcessingManager" );
        sgns::SgnsProcessing                 processing_;
        std::unique_ptr<ProcessingProcessor> m_processor;
        ExecutionManifest                    m_lastManifest{};
        std::unordered_map<int, std::function<std::unique_ptr<ProcessingProcessor>()>>                    m_processorFactories;
        std::unordered_map<PassType, ExecutorRegistryEntry, PassTypeHash>                                m_passFactories;
        std::unordered_map<std::string, size_t>                                                           m_inputMap;
        std::unique_ptr<CapabilityValidator>                                                              m_capabilityValidator;
    };
}

OUTCOME_HPP_DECLARE_ERROR_2( sgns::sgprocessing, ProcessingManager::Error );

#endif

Updated on 2026-10-06 at 13:34:21 +0000