Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Kernel - Resource [GPU] [Texture] [Image].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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#if defined(TgCOMPILE_GPU_SUPPORT)

#define T( A, ... ) A##TX_IMG##__VA_ARGS__
#include "TgS Kernel - Resource [GPU].c_inc"
#undef T


/* == Kernel ============================================================================================================================================================ */

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

/* ---- tgKN_GPU_TX_IMG_Inst__Init_Colour ------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgKN_GPU_RSI_ID tgKN_GPU_TX_IMG_Inst__Init_Colour( C_TgKN_GPU_CXT_DATA_ID tiCT, C_TgUINTXX uiHash, C_TgCOLOUR32 sCL0, CPCU_TgCHAR pszName )
{
    TgKN_GPU_RSI_ID                     tiTXI_IMG;
    STg2_KN_GPU_TX_Surface              sSF0;

    /* Invalid name or name already exists is an error condition for this function */
    if (KTgEMPTY_HASH == uiHash)
    {
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s |\n" ), TgT("Kernel"), TgT("tgKN_GPU_TXI_IMG__Init_Colour"),
                     TgT("Failed to create a default colour texture because name was empty.") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

    /* Create the texture as a 4x4.  Smaller textures do not work on the PS3 due to alignment restrictions */

    tiTXI_IMG = tgKN_GPU_TX_IMG_Inst__Create( tiCT, ETgKN_GPU_ALLOCATOR__VIDEO_MEMORY_WRITEONLY, uiHash, ETgKN_GPU_FORMAT__R8G8B8A8_UNORM, 1, 4, 4 );

    if (TgFALSE == tgKN_GPU_RSI_ID_Is_Valid( tiTXI_IMG ))
    {
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s |\n" ), TgT("Kernel"), TgT("tgKN_GPU_TXI_IMG__Init_Colour"),
                     TgT("Failed to create a default colour texture") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

#if TgS_DEBUG_KERNEL
    tgCN_PrintF( KTgCN_CHANEL_INITIALIZE | KTgCN_SEVERITY_7, TgT( "%-16.16s(%-32.32s): [Init] %-57.57s | id(% 4d) | % -9d BY\n" ), TgT("Kernel"),
                 TgT("tgKN_GPU_TXI_IMG__Init_Colour"), pszName ? pszName : TgT(""), tiTXI_IMG.m.iI, g_uiKN_Size_GPU_TX_IMG[tiTXI_IMG.m.iI] );
#endif

    /* Lock the texture so as to have a valid data pointer - and fill in the assigned colour into it */

    tgKN_GPU_PM_TX_IMG__Lock( &sSF0, tiTXI_IMG, 0 );

    {
        TgSINT32                            iY, iX;
        union
        {
            P_TgUINT08                          pui08;
            P_TgUINT32                          pui32;
        }                                   sMem;

        for (iY = 0; iY < sSF0.m_iH; ++iY)
        {
            for (iX = 0; iX < sSF0.m_iW; ++iX)
            {
                sMem.pui08 = sSF0.m_pData + sSF0.m_iPitch*iY;
                sMem.pui32[iX] = sCL0.m_uiColour;
            };
        };
    };

    tgKN_GPU_PM_TX_IMG__Unlock( tiTXI_IMG, 0 );

    return (tiTXI_IMG);
}


/* ---- tgKN_GPU_TX_IMG_Inst__Create ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgKN_GPU_RSI_ID tgKN_GPU_TX_IMG_Inst__Create(
    C_TgKN_GPU_CXT_DATA_ID tiCT, C_ETgKN_GPU_ALLOCATOR enAllocator, C_TgUINTXX uiHash, C_ETgKN_GPU_FORMAT enFMT, C_TgSINT32 niMIP, C_TgSINT32 iW, C_TgSINT32 iH )
{
    STg2_KN_GPU_TX_IMG_DESC             sTX_IMG;
    union
    {
        TgSIZE                              ui;
        P_STg2_KN_GPU_TX_IMG_DESC           ps;
    }                                   sDesc;
    TgKN_GPU_RS_ID                      tiTX_IMG;
    TgKN_GPU_RSI_ID                     tiTXI_IMG;

    TgCOMPILER_ASSERT( sizeof( TgSIZE ) == sizeof( P_STg2_KN_GPU_TX_IMG_DESC ), 0 );

    sTX_IMG.m_niMIP = niMIP;
    sTX_IMG.m_enFormat = enFMT;
    sTX_IMG.m_uiFlags = 0;
    sTX_IMG.m_uiHash = uiHash;
    sTX_IMG.m_iWidth = iW;
    sTX_IMG.m_iHeight = iH;

    sDesc.ps = &sTX_IMG;

    tiTX_IMG = tgKN_GPU_TX_IMG__Load( KTgKN_FILE_ID__INVALID, sDesc.ui, enAllocator, KTgEMPTY_HASH );

    if (TgTRUE != tgKN_GPU_RS_ID_Is_Valid( tiTX_IMG ))
    {
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s\n" ), TgT("Kernel"), TgT("tgKN_GPU_TXI_IMG__Create"), TgT("Failed to create texture") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

    tiTXI_IMG = tgKN_GPU_TX_IMG_Inst__Init( tiCT, tiTX_IMG );

    if (TgTRUE != tgKN_GPU_RSI_ID_Is_Valid( tiTXI_IMG ))
    {
        tgKN_GPU_TX_IMG__Release( tiTX_IMG );
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s\n" ), TgT("Kernel"), TgT("tgKN_GPU_TXI_IMG__Create"), TgT("Failed to create texture instance") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

    return (tiTXI_IMG);
}


/* ---- tgKN_GPU_RT_IMG__Create ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgKN_GPU_RSI_ID tgKN_GPU_RT_IMG__Create(
    C_TgKN_GPU_CXT_DATA_ID tiCT, C_ETgKN_GPU_ALLOCATOR enAllocator, C_TgUINTXX uiHash_RS, C_ETgKN_GPU_FORMAT enFMT, C_TgSINT32 iW, C_TgSINT32 iH )
{
    STg2_KN_GPU_TX_IMG_DESC             sTX_IMG;
    union
    {
        TgSIZE                              ui;
        P_STg2_KN_GPU_TX_IMG_DESC           ps;
    }                                   sDesc;
    TgKN_GPU_RS_ID                      tiTX_IMG;
    TgKN_GPU_RSI_ID                     tiTXI_IMG;

    TgCOMPILER_ASSERT( sizeof( TgSIZE ) == sizeof( P_STg2_KN_GPU_TX_IMG_DESC ), 0 );

    sTX_IMG.m_niMIP = 1;
    sTX_IMG.m_enFormat = enFMT;
    sTX_IMG.m_uiFlags = ETgKN_TX_FLAG__TARGET;
    sTX_IMG.m_uiHash = uiHash_RS;
    sTX_IMG.m_iWidth = iW;
    sTX_IMG.m_iHeight = iH;

    sDesc.ps = &sTX_IMG;

    tiTX_IMG = tgKN_GPU_TX_IMG__Load( KTgKN_FILE_ID__INVALID, sDesc.ui, enAllocator, KTgEMPTY_HASH );

    if (TgTRUE != tgKN_GPU_RS_ID_Is_Valid( tiTX_IMG ))
    {
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s\n" ), TgT("Kernel"), TgT("tgKN_GPU_RT_IMG__Create"), TgT("Failed to render target texture") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

    tiTXI_IMG = tgKN_GPU_TX_IMG_Inst__Init( tiCT, tiTX_IMG );

    if (TgTRUE != tgKN_GPU_RSI_ID_Is_Valid( tiTXI_IMG ))
    {
        tgKN_GPU_TX_IMG__Release( tiTX_IMG );
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT( "%-16.16s(%-32.32s): %-64.64s\n" ), TgT("Kernel"), TgT("tgKN_GPU_RT_IMG__Create"), TgT("Failed to instance render target") );
        return (KTgKN_GPU_RSI_ID__INVALID);
    };

    return (tiTXI_IMG);
}





/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Parent File Local Functions                                                                                                                                           */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgKN_GPU_TX_IMG__Load_BIN_Internal ------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgUINTXX tgKN_GPU_TX_IMG__Load_BIN_Internal(
    C_TgKN_FILE_ID tiFile, C_TgSIZE uiFile_Offset, C_ETgKN_GPU_ALLOCATOR enAllocator, PCU_STg2_KN_GPU_RS_LIB_DESC pLibRS, C_TgUINTXX uiHash_RS, C_TgSIZE uiIndex )
{
    TgBOOL                              bFileValid;
    union
    {
        TgSIZE                              ui;
        P_STg2_KN_GPU_TX_IMG_DESC           ps;
    }                                   sDesc;
    TgBOOL                              bResult;
    TgUINTXX                            uiHash;

    TgCOMPILER_ASSERT( sizeof( TgSIZE ) == sizeof( P_STg2_KN_GPU_TX_IMG_DESC ), 0 );

    TgPARAM_CHECK((TgTRUE == tgKN_FILE_ID_Is_Valid( tiFile )) ^ (KTgEMPTY_HASH == uiHash_RS) );
    TgPARAM_CHECK(pLibRS == &g_sKN_Lib_TX_IMG_DESC);
    TgPARAM_CHECK_INDEX(uiIndex, g_asKN_Lib_TX_IMG_Data);
    TgPARAM_CHECK(ETgKN_GPU_ALLOCATOR__INVALID != enAllocator);

    bFileValid = tgKN_FILE_ID_Is_Valid( tiFile );
    sDesc.ui = uiFile_Offset;

    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_niMIP = TgTRUE != bFileValid ? sDesc.ps->m_niMIP : -1;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_enFormat = TgTRUE != bFileValid ? sDesc.ps->m_enFormat : ETgKN_GPU_FORMAT__UNKNNOWN;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_uiLastTouch = 0;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_uiPriority = 0;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_enAllocator = enAllocator;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_uiFlags = TgTRUE != bFileValid ? sDesc.ps->m_uiFlags : 0;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_pHW = 0;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_sTX.m_pData = 0;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_iWidth = TgTRUE != bFileValid ? sDesc.ps->m_iWidth : -1;
    g_asKN_Lib_TX_IMG_Data[uiIndex].m_iHeight = TgTRUE != bFileValid ? sDesc.ps->m_iHeight : -1;

    uiHash = TgTRUE != bFileValid ? sDesc.ps->m_uiHash : uiHash_RS;

#if TgS_STAT_KERNEL || TgS_DEBUG_KERNEL
    g_uiKN_Size_GPU_TX_IMG[uiIndex] = 0;
#endif

    bResult = tgKN_GPU_PM_TX_IMG__Init( pLibRS, uiIndex, enAllocator, uiHash, tiFile, uiFile_Offset );

    /* Check for failure, and return the id to the usage pool before returning with an error state */
    if (TgFALSE == bResult)
    {
        tgCN_PrintF( KTgCN_CHANEL_WARNING, TgT( "%-16.16s(%-32.32s): [Warn] %-57.57s\n" ), TgT("Kernel"), TgT("tgKN_GPU_TX_IMG__Load_BIN_Internal"),
                     TgT("Failed to initialize texture image") );
        pLibRS->m_ptiRS[uiIndex] = KTgKN_GPU_RS_ID__INVALID;
        return (KTgEMPTY_HASH);
    };

#if TgS_DEBUG_KERNEL
    tgCN_PrintF( KTgCN_CHANEL_LOADING, TgT( "%-16.16s(%-32.32s): [Load] %-32.32s         | id(% 4d) | % -9d BY\n" ), TgT("Kernel"),
                 TgT("tgKN_GPU_TX_IMG__Load_BIN_Internal"), TgT(""), uiIndex, g_szKN_GPU_TX_IMG[uiIndex] );
#endif

#if TgS_STAT_KERNEL || TgS_DEBUG_KERNEL
    pLibRS->m_uiSize += g_uiKN_Size_GPU_TX_IMG[uiIndex];
#endif

    g_aiKN_Lib_TX_IMG_Ref[uiIndex] = 1;

    return (uiHash);
}


/*# defined(TgCOMPILE_GPU_SUPPORT) */
#endif