Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Kernel - Module.c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Kernel ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Data                                                                                                                                                       */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

static ETgMODULE_STATE                      s_enKernel_State = ETgMODULE_STATE__FREED;




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgKN_Module_Init ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_Module_Init( TgVOID )
{
    TgSINT32                            iIndex;

    /* Verify the state of the system */
    TgERROR( ETgMODULE_STATE__FREED == s_enKernel_State );
    s_enKernel_State = ETgMODULE_STATE__INITIALIZING;

    /* Job Manager Execution Queues */
    g_sJob_Stack__JiT = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__Render_High = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__Render_Normal = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__GPU = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__Client_High = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__Client_Normal = KTgJOB_QUEUE_ID__INVALID;
    g_sJob_Queue__Client_Low = KTgJOB_QUEUE_ID__INVALID;

#if !defined(TgCOMPILE_FORCE_ANSI)
    if (TgFAILED( tgKN_PM_Module_Init() ))
    {
        goto KN_PM_Init_Fail;
    };
/*# !defined(TgCOMPILE_FORCE_ANSI) */
#endif

    /* Initialize the Notification system */
    for (iIndex = 0; iIndex < KTgKN_MAX_NOTIFY_ENTRIES; ++iIndex)
    {
        g_aiKN_Notify_Next[iIndex] = iIndex + 1;
    };
    memset( g_aiKN_Notify_List, 0xFF, sizeof( g_aiKN_Notify_List ) );
    tgCM_UTM_SN_Init( &g_sKN_Notfiy_Lock.m_sLock );
    g_iKN_Notfiy_Free_Index = 0;

    /* File System */
    g_niKN_FS = 0;
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_FS_Mount__Free_Stack.m_sStack, g_asKN_FS_Mount, sizeof( STg2_KN_FS_Mount ), KTgKN_MAX_FILE_SYSTEM_MOUNT );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_File_Info__Free_Stack.m_sStack, g_asKN_File_Info, sizeof( STg2_KN_File_Info ), KTgKN_MAX_FILE_OPEN );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_File_Job__Free_Stack.m_sStack, g_asKN_File_Job, sizeof( STg2_KN_File_Job ), KTgKN_MAX_FILE_JOB );

    /* Register the FS-HD */
#if !defined(TgCOMPILE_FORCE_ANSI)
    {
        STg2_KN_File_System_DESC            sFS_DESC;

        sFS_DESC.m_niMax_Execute = KTgKN_FS_MAX_OS_COMMAND_PER_DEVICE;
        sFS_DESC.m_pfnFree_Space = tgKN_FS_HDD__Free_Space;
        sFS_DESC.m_pfnMount = tgKN_FS_HDD__Mount;
        sFS_DESC.m_pfnExecute_Job = tgKN_FS_HDD__Execute_Job;
        g_tiKN_FS_HDD = tgKN_Register_File_System( &sFS_DESC );
    }
/*# !defined(TgCOMPILE_FORCE_ANSI) */
#endif

#if defined(TgCOMPILE_GPU_SUPPORT)
    g_niKN_GPU_Adapter = 0;
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_GPU_Context_Device__Free_Stack.m_sStack, g_asKN_GPU_Context_Device, sizeof( STg2_KN_GPU_Context__Device ),
                                 KTgKN_GPU_MAX_DEVC_CONTEXT );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_GPU_Context_Output__Free_Stack.m_sStack, g_asKN_GPU_Context_Output, sizeof( STg2_KN_GPU_Context__Output ), KTgKN_GPU_MAX_OUTPUT );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_GPU_Context_Resource__Free_Stack.m_sStack, g_asKN_GPU_Context_Resource, sizeof( STg2_KN_GPU_Context__Resource ),
                                 KTgKN_GPU_MAX_RESOURCE_CONTEXT );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_GPU_Context_External__Free_Stack.m_sStack, g_asKN_GPU_Context_External, sizeof( STg2_KN_GPU_Context__External ),
                                 KTgKN_GPU_MAX_RESOURCE_CONTEXT );
    tgCM_UTM_AM_ST_Init_PreLoad( &g_sKN_GPU_Context__Free_Stack.m_sStack, g_asKN_GPU_Context, sizeof( STg2_KN_GPU_Context ), KTgKN_GPU_MAX_CONTEXT );

    if (TgFAILED( tgKN_GPU_PM_Module_Init() ))
    {
        goto KN_GPU_Init_Fail;
    };

    if (TgFAILED( tgKN_GPU_Shader_Lib_Init() ))
    {
        goto KN_GPU_Shader_Init_Fail;
    };

    if (TgFAILED( tgKN_GPU_TX_LIB_Init() ))
    {
        goto KN_GPU_Texture_Init_Fail;
    };
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

    /* Init Stats and Performance Markers */
#if TgS_STAT_KERNEL
#if defined(TgCOMPILE_GPU_SUPPORT)
    g_uiKN_Stats_Loaded_Mesh_Vertex = 0;
    g_uiKN_Stats_Loaded_Mesh_Face = 0;
    g_uiKN_Stats_Memory_Buffer = 0;
    g_uiKN_Stats_Memory_Mesh = 0;
    g_uiKN_Stats_Memory_Mesh_Collision = 0;
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif
/*# TgS_STAT_KERNEL */
#endif

    s_enKernel_State = ETgMODULE_STATE__INITIALIZED;
    return (KTgS_OK);

#if defined(TgCOMPILE_GPU_SUPPORT)
    KN_GPU_Texture_Init_Fail:   tgKN_GPU_Shader_Lib_Free();
    KN_GPU_Shader_Init_Fail:    tgKN_GPU_PM_Module_Free();
    KN_GPU_Init_Fail:           
#if !defined(TgCOMPILE_FORCE_ANSI)
                                tgKN_PM_Module_Free();
#endif
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

#if !defined(TgCOMPILE_FORCE_ANSI) || defined(TgCOMPILE_GPU_SUPPORT)
    KN_PM_Init_Fail:            s_enKernel_State = ETgMODULE_STATE__FREED;
                                return (KTgE_FAIL);
/*# !defined(TgCOMPILE_FORCE_ANSI) || defined(TgCOMPILE_GPU_SUPPORT) */
#endif
}


/* ---- tgKN_Module_Boot ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_Module_Boot( TgVOID )
{
    /* Verify the state of the system */
    TgERROR( ETgMODULE_STATE__INITIALIZED == s_enKernel_State );
    s_enKernel_State = ETgMODULE_STATE__BOOTING;

    g_sJob_Stack__JiT = tgJM_Init_Job_Queue();
    g_sJob_Queue__Render_High = tgJM_Init_Job_Queue();
    g_sJob_Queue__Render_Normal = tgJM_Init_Job_Queue();
    g_sJob_Queue__GPU = tgJM_Init_Job_Queue();
    g_sJob_Queue__Client_High = tgJM_Init_Job_Queue();
    g_sJob_Queue__Client_Normal = tgJM_Init_Job_Queue();
    g_sJob_Queue__Client_Low = tgJM_Init_Job_Queue();

#if !defined(TgCOMPILE_FORCE_ANSI)
    if (TgFAILED( tgKN_PM_Module_Boot() ))
    {
        goto KN_PM_Boot_Fail;
    };

    /* Create an instance of the FS-HD and create a mount point at the execution root */
    {
        TgCHAR szBuffer[KTgMAX_FILE_PATH];

        tgCM_Path_Query_Application_Data_Directory( szBuffer, KTgMAX_FILE_PATH );
        if (0 != szBuffer[0])
        {
            g_tiKN_FS_HDD__Mount_APP = tgKN_File_System_Mount( g_tiKN_FS_HDD, ETgFILE_IO_ACCESS__READ | ETgFILE_IO_ACCESS__WRITE | ETgFILE_IO_ACCESS__SHARED, szBuffer );
        };
    }
/*# !defined(TgCOMPILE_FORCE_ANSI) */
#endif

#if defined(TgCOMPILE_GPU_SUPPORT)
    if (TgFAILED( tgKN_GPU_PM_Module_Boot() ))
    {
        goto KN_GPU_Boot_Fail;
    };

    if (TgFAILED( tgKN_GPU_Shader_Lib_Boot() ))
    {
        goto KN_GPU_Shader_Boot_Fail;
    };

    if (TgFAILED( tgKN_GPU_TX_LIB_Boot() ))
    {
        goto KN_GPU_Texture_Boot_Fail;
    };
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

    tgCN_PrintF( KTgCN_CHANEL_INITIALIZE_MEMORY, TgT( "%-16.16s(%-32.32s): %-48.48s % 14d\n" ), TgT("Kernel"), TgT("Boot_Module"),
        TgT("Static Memory Size"), tgKN_Query_Fixed_Memory() );

    s_enKernel_State = ETgMODULE_STATE__BOOTED;
    return (KTgS_OK);

#if defined(TgCOMPILE_GPU_SUPPORT)
    KN_GPU_Texture_Boot_Fail:   tgKN_GPU_Shader_Lib_Stop();
    KN_GPU_Shader_Boot_Fail:    tgKN_GPU_PM_Module_Stop();
    KN_GPU_Boot_Fail:           
#if !defined(TgCOMPILE_FORCE_ANSI)
                                tgKN_PM_Module_Stop();
#endif
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

#if !defined(TgCOMPILE_FORCE_ANSI) || defined(TgCOMPILE_GPU_SUPPORT)
    KN_PM_Boot_Fail:            s_enKernel_State = ETgMODULE_STATE__INITIALIZED;
                                return (KTgE_FAIL);
/*# !defined(TgCOMPILE_FORCE_ANSI) || defined(TgCOMPILE_GPU_SUPPORT) */
#endif
}


/* ---- tgKN_Module_Stop ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_Module_Stop( TgVOID )
{
    if ((ETgMODULE_STATE__FREED == s_enKernel_State) || (ETgMODULE_STATE__INITIALIZED == s_enKernel_State))
    {
        return;
    };

    /* Verify the state of the system */
    TgERROR( ETgMODULE_STATE__BOOTED == s_enKernel_State );
    s_enKernel_State = ETgMODULE_STATE__STOPPING;

#if defined(TgCOMPILE_GPU_SUPPORT)
    tgKN_GPU_TX_LIB_Stop();
    tgKN_GPU_Shader_Lib_Stop();
    tgKN_GPU_PM_Module_Stop();
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

#if !defined(TgCOMPILE_FORCE_ANSI)
    tgKN_PM_Module_Stop();
#endif

    tgJM_Stop_Job_Queue( g_sJob_Queue__Client_Low, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__Client_Low );
    g_sJob_Queue__Client_Low = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Queue__Client_Normal, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__Client_Normal );
    g_sJob_Queue__Client_Normal = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Queue__Client_High, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__Client_High );
    g_sJob_Queue__Client_High = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Queue__GPU, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__GPU );
    g_sJob_Queue__GPU = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Queue__Render_Normal, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__Render_Normal );
    g_sJob_Queue__Render_Normal = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Queue__Render_High, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Queue__Render_High );
    g_sJob_Queue__Render_High = KTgJOB_QUEUE_ID__INVALID;

    tgJM_Stop_Job_Queue( g_sJob_Stack__JiT, TgTRUE );
    tgJM_Free_Job_Queue( g_sJob_Stack__JiT );
    g_sJob_Stack__JiT = KTgJOB_QUEUE_ID__INVALID;

    s_enKernel_State = ETgMODULE_STATE__STOPPED;
}


/* ---- tgKN_Module_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_Module_Free( TgVOID )
{
    if (ETgMODULE_STATE__FREED == s_enKernel_State)
    {
        return;
    };

    /* Verify the state of the system */
    TgERROR( ETgMODULE_STATE__STOPPED == s_enKernel_State || ETgMODULE_STATE__INITIALIZED == s_enKernel_State );
    s_enKernel_State = ETgMODULE_STATE__FREEING;

#if defined(TgCOMPILE_GPU_SUPPORT)
    tgKN_GPU_TX_LIB_Free();
    tgKN_GPU_Shader_Lib_Free();
    tgKN_GPU_PM_Module_Free();
/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif

#if !defined(TgCOMPILE_FORCE_ANSI)
    tgKN_PM_Module_Free();
#endif

    tgCM_UTM_SN_Free( &g_sKN_Notfiy_Lock.m_sLock );

    s_enKernel_State = ETgMODULE_STATE__FREED;
}


/* ---- tgKN_Module_Update ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_Module_Update( C_TgFLOAT32 fDT )
{
    TgRESULT                            tgResult = KTgS_OK;

    (void)fDT;

#if !defined(TgCOMPILE_FORCE_ANSI)
    tgResult |= tgKN_PM_Module_Update( fDT );
#endif
#if defined(TgCOMPILE_GPU_SUPPORT)
    tgResult |= tgKN_GPU_PM_Module_Update( fDT );
#endif

    return (tgResult);
}


/* ---- tgKN_Load_Config ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_Load_Config( TgVOID )
{

}


/* ---- tgKN_Save_Config ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_Save_Config( TgVOID )
{

}


/* ---- tgKN_Query_Init ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgKN_Query_Init( TgVOID )
{
    return (ETgMODULE_STATE__INITIALIZED <= s_enKernel_State && s_enKernel_State <= ETgMODULE_STATE__STOPPED ? TgTRUE : TgFALSE);
}


/* ---- tgKN_Query_Boot ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgKN_Query_Boot( TgVOID )
{
    return (ETgMODULE_STATE__BOOTED == s_enKernel_State ? TgTRUE : TgFALSE);
}


/* ---- tgKN_Stats ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgS_STAT_KERNEL
TgVOID tgKN_Stats( P_STg2_Output UNUSED_PARAM psOUT )
{
}
/*# TgS_STAT_KERNEL */
#endif