Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Effect - Decal - Update.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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

#define T_NAME( A, ... ) A##Decal##__VA_ARGS__
#define T_TYPE( A, ... ) A##DECAL##__VA_ARGS__
#define T_TEXT( ... ) __VA_ARGS__ TgT("Decal")
#define T_EFFECT_FRAME 0
#define T_EFFECT_RENDER_PRIMITIVE_TYPE ETgRN_PRIMITIVE__TRIANGLE_LIST
#define T_EFFECT_PROCESS_COMMMAND_DEFAULT 0

#include "TgS Effect - Effect - Type.h_inc"


/* == Effect ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Shared Implementation                                                                                                                                                 */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

#include "TgS Effect - Effect - Update.i_inc"
#include "TgS Effect - Effect - Update.c_inc"




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

static TgBOOL                               tgFX__Update__Decal_Reserve_Cache_Buffer( P_STg2_FX__Decal );
static TgBOOL                               tgFX__Update__Decal_Commit_Cache_Buffer( P_STg2_FX__Decal, C_TgSINT32 );
static TgVOID                               tgFX__Update__Decal_Free_Cache_Buffer( P_STg2_FX__Decal );
static TgVOID                               tgFX__Update__Decal_Update__Write_Cache( P_STg2_FX__Decal, CP_STg2_CO_Request_F32_04 );



/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Private Functions                                                                                                                                                     */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgFX__File__Decal_Patch_File_Data ------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgFX__File__Decal_Patch_File_Data( P_STg2_FX_Decal__File_Data psData )
{
    TgUINTPTR                           uiData_Top;

    uiData_Top = (TgUINTPTR)psData;

    psData->m_sAnim.m_sData_Offset.m_uiColour += uiData_Top;
}




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

/* ---- tgFX__Update__Decal_Create_Command ------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgBOOL tgFX__Update__Decal_Create_Command( CP_STg2_FX_Instance psInst, C_TgFX_DATA_INST_ID tiData, CP_TgVOID UNUSED_PARAM pData1 )
{
    P_STg2_FX__Decal                    psDecal;
    CP_STg2_FX_Decal__File_Data         psFile_Data;

    psDecal = g_asFX__Decal + tiData.m.iI;
    psFile_Data = psDecal->m_psFile_Data;

    if (TgTRUE != tgFX__Update__Decal_Reserve_Cache_Buffer( psDecal ))
    {
        return (TgFALSE);
    };

    tgFX__Update__Set_Render_Data_From_Material_Hash( nullptr, &psDecal->m_tiMaterial, &psDecal->m_enVertex, psFile_Data->m_uiHash_Material );

    if (TgTRUE != tgRN_MATERIAL_ID_Is_Valid( psDecal->m_tiMaterial ))
    {
        return (TgFALSE);
    };

    psDecal->m_sExtend.m_vSize_Start = M_MAD_F32_04( MS_SET1_F32_04( tgCM_RAND_F32() ), psFile_Data->m_vSize_RNG[0], psFile_Data->m_vSize_MID[0] );
    psDecal->m_sExtend.m_vSize_Final = M_MAD_F32_04( MS_SET1_F32_04( tgCM_RAND_F32() ), psFile_Data->m_vSize_RNG[1], psFile_Data->m_vSize_MID[1] );

    psDecal->m_sExtend.m_vPos_W = psInst->m_vPos_W.m_mData;
    psDecal->m_sExtend.m_vScale = psInst->m_vScale.m_mData;
    psDecal->m_sExtend.m_vSpin = MS_SET1_F32_04( tgCM_RAND_F32()*psFile_Data->m_fSpin_RNG + psFile_Data->m_fSpin_MID );
    psDecal->m_sExtend.m_vSize = psDecal->m_sExtend.m_vSize_Start;
    psDecal->m_sExtend.m_vColour = tgFX_Evaluate_AnimData_F32_04( psFile_Data->m_sAnim.m_sParameter.m_psColour, MS_SET1_F32_04( 0.0F ) );
    tgRN_M_Calc_UV_Animation(
        &psDecal->m_sExtend.m_vUV_01_Constant, &psDecal->m_sExtend.m_vUV_01_Scale, psDecal->m_tiMaterial, KTgV_ZERO_F32_04.m_mData, KTgV_ZERO_F32_04.m_mData );

    if (psDecal->m_fTime_End__Start < 0.0F)
        g_aenFX__Decal__Shared__State[tiData.m.iI] = ETgFX_EFFECT_STATE__UPDATE__ACTIVE_INFINITE;
    else
        g_aenFX__Decal__Shared__State[tiData.m.iI] = ETgFX_EFFECT_STATE__UPDATE__ACTIVE_FIRST;

    return (TgTRUE);
}


/* ---- tgFX__Update__Decal_Update -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgVOID tgFX__Update__Decal_Update( TgVEC_M_F32_04 vdT, C_TgFLOAT32 fDT, C_ETgFX_UPDATE enUpdate )
{
    TgFX_DATA_INST_ID                   tiActive;
    P_STg2_FX__Decal                    psDecal;
    CP_STg2_FX_Decal__File_Data         psFile_Data;
    TgBOOL                              bRebuild_Cache;

    tiActive = T_NAME(s_atiFX__, __Update__Head)[enUpdate];

    while (TgFALSE != tgFX_DATA_INST_ID_Is_Valid( tiActive ))
    {
        tgFX__Update__Decal_Diag_Check_Effect( tiActive );

        psDecal = g_asFX__Decal + tiActive.m.iI;
        psFile_Data = psDecal->m_psFile_Data;
        bRebuild_Cache = ETgFX_EFFECT_STATE__UPDATE__ACTIVE_FIRST == g_aenFX__Decal__Shared__State[tiActive.m.iI] ? TgTRUE : TgFALSE;

        if (TgFALSE == tgFX__Update__Decal_Update_Life_Time( tiActive.m.iI, fDT, vdT ))
        {
            tiActive = psDecal->m_tiNext[ETgFX_NEXT__UPDATE];
            continue;
        };

        psDecal->m_sExtend.m_vColour = tgFX_Evaluate_AnimData_F32_04( psFile_Data->m_sAnim.m_sParameter.m_psColour, psDecal->m_vLive_Time );

        if (TgTRUE == bRebuild_Cache)
        {
            STg2_SE_CO_Request_F32_04           sRequest;
            TgVEC_F32_04                        vSize;

            vSize.m_mData = M_LEN_F32_04( psDecal->m_sExtend.m_vSize );

            tgSE_Init_Request( &sRequest );
            tgCO_REQ_Init_Collect_F32_04( &sRequest.m_sCollision_Request, ETgSPHERE, ETgCO_PURPOSE__DECAL );
            tgCO_REQ_Set_Include_Filter_F32_04( &sRequest.m_sCollision_Request, ETgCO_FILTER__RENDER );
            tgGM_SP_Init_F32_04( &sRequest.m_sCollision_Request.m_sPrimitive.m_sSP, (P_TgVEC_F32_04)&psDecal->m_sExtend.m_vPos_W, 2.0F * vSize.m.x );

            if (TgFAILED(tgSE_Collide( &sRequest, ETgSE_CHANNEL__SET )) || 0 == sRequest.m_sCollision_Request.m_niCollect_Last)
            {
                return;
            };

            tgFX__Update__Decal_Update__Write_Cache( psDecal, &sRequest.m_sCollision_Request );
        }

        tiActive = psDecal->m_tiNext[ETgFX_NEXT__UPDATE];
    };
}


/* ---- tgFX__Update__Decal_Process_Update_Command ---------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgFX__Update__Decal_Process_Update_Command( C_ETgFX_COMMAND enCommand, CP_TgVOID UNUSED_PARAM pData, C_TgFX_DATA_INST_ID tiData )
{
    if (ETgFX_COMMAND__CLIENT_TO_UPDATE__KILL == enCommand)
    {
        tgFX__Update__Decal_Free_Cache_Buffer( g_asFX__Decal + tiData.m.iI );
    };
}




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

/* ---- tgFX__Update__Decal_Reserve_Cache_Buffer ------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgBOOL tgFX__Update__Decal_Reserve_Cache_Buffer( P_STg2_FX__Decal psDecal )
{
    TgSINT32                            iPageSize;
    TgSINT32                            iMemory_Current;
    TgSINT32                            iMemory_New;
    TgSINT32                            iMax_Commit;

    iPageSize = (TgSINT32)tgMM_Page_Size();

    /* Determine if the decal max allocation fits into max memory limit for the module / system */
    iMax_Commit = 1 + (ETgFX_DECAL__VERTEX_MAX * (TgSINT32)sizeof( STg2_FX__Decal__Vertex ) / iPageSize);
    do
    {
        iMemory_Current = tgAM32_READ( &g_xiFX__Decal__Memory_Current );
        iMemory_New = iMemory_Current + iMax_Commit * iPageSize;
        if (iMemory_New > g_iFX_Decal_Memory_Max)
        {
            return (TgFALSE);
        };
    }
    while (iMemory_Current != tgAM32_XCMP( &g_xiFX__Decal__Memory_Current, iMemory_New, iMemory_Current ));

    /* Reserve enough memory for max number of vertices */
    psDecal->m_sExtend.m_psVert = TgRESERVE_VIRTUAL( (TgSIZE)(iMax_Commit * iPageSize) );
    if (nullptr == psDecal->m_sExtend.m_psVert)
    {
        do
        {
            iMemory_Current = tgAM32_READ( &g_xiFX__Decal__Memory_Current );
            iMemory_New = iMemory_Current - iMax_Commit * iPageSize;
        }
        while (iMemory_Current != tgAM32_XCMP( &g_xiFX__Decal__Memory_Current, iMemory_New, iMemory_Current ));
        return (TgFALSE);
    };

    /* Initialize the linked list of free particles and record the number of particles on committed memory pages */
    psDecal->m_sExtend.m_niVert = 0;
    psDecal->m_sExtend.m_niVert_Commit = 0;

    return (TgTRUE);
}


/* ---- tgFX__Update__Decal_Commit_Cache_Buffer ------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgBOOL tgFX__Update__Decal_Commit_Cache_Buffer( P_STg2_FX__Decal psDecal, C_TgSINT32 niVert )
{
    TgSINT32                            iPageSize;
    TgSINT32                            iMemory_Current;
    TgSINT32                            iMemory_New;
    TgSINT32                            niMin_Vert;
    TgSINT32                            iMax_Commit;
    TgSINT32                            iMin_Commit;

    iPageSize = (TgSINT32)tgMM_Page_Size();

    /* Determine if the emitter max allocation fits into max memory limit for the module / system */
    iMax_Commit = 1 + (ETgFX_DECAL__VERTEX_MAX * (TgSINT32)sizeof( STg2_FX__Decal__Vertex ) / iPageSize);
    niMin_Vert = tgCM_MAX_S32( 1, tgCM_MIN_S32( niVert, ETgFX_DECAL__VERTEX_MAX ) );
    iMin_Commit = 1 + (niMin_Vert * (TgSINT32)sizeof( STg2_FX__Decal__Vertex ) / iPageSize);

    /* Commit enough memory for one memory page, or one vertex */
    if (nullptr == TgCOMMIT_VIRTUAL( psDecal->m_sExtend.m_psVert, (TgSIZE)iMin_Commit ))
    {
        do
        {
            iMemory_Current = tgAM32_READ( &g_xiFX__Decal__Memory_Current );
            iMemory_New = iMemory_Current - iMax_Commit * iPageSize;
        }
        while (iMemory_Current != tgAM32_XCMP( &g_xiFX__Decal__Memory_Current, iMemory_New, iMemory_Current ));
        return (TgFALSE);
    };

    /* Initialize the linked list of free particles and record the number of particles on committed memory pages */
    psDecal->m_sExtend.m_niVert_Commit = iMin_Commit;

    return (TgTRUE);
}


/* ---- tgFX__Update__Decal_Free_Cache_Buffer ------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgVOID tgFX__Update__Decal_Free_Cache_Buffer( P_STg2_FX__Decal psDecal )
{
    TgSINT32                            iPageSize;
    TgSINT32                            iMemory_Current;
    TgSINT32                            iMemory_New;
    TgSINT32                            iMax_Commit;

    iPageSize = (TgSINT32)tgMM_Page_Size();

    iMax_Commit = 1 + (ETgFX_DECAL__VERTEX_MAX * (TgSINT32)sizeof( STg2_FX__Decal__Vertex ) / iPageSize);
    if (nullptr == psDecal->m_sExtend.m_psVert)
    {
        do
        {
            iMemory_Current = tgAM32_READ( &g_xiFX__Decal__Memory_Current );
            iMemory_New = iMemory_Current - iMax_Commit * iPageSize;
        }
        while (iMemory_Current != tgAM32_XCMP( &g_xiFX__Decal__Memory_Current, iMemory_New, iMemory_Current ));

        TgFREE_VIRTUAL( psDecal->m_sExtend.m_psVert );
    };

    psDecal->m_sExtend.m_psVert = nullptr;
    psDecal->m_sExtend.m_niVert = 0;
    psDecal->m_sExtend.m_niVert_Commit = 0;
}


/* ---- tgFX__Update__Decal_Update__Write_Cache ------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgVOID tgFX__Update__Decal_Update__Write_Cache( P_STg2_FX__Decal psDecal, CP_STg2_CO_Request_F32_04 psCO_Request )
{
    TgPLANE_F32_04                      asPN[ETgFRUSTUM_PLANE__MAX];
    TgVEC_F32_04                        vAA;
    TgVEC_F32_04                        vN0;
    TgVEC_F32_04                        vT0;
    TgVEC_F32_04                        vB0;
    TgVEC_M_F32_04                      vSize;
    TgVEC_M_F32_04                      vInvSizeX;
    TgVEC_M_F32_04                      vInvSizeY;
    TgVEC_M_F32_04                      vD0;
    TgVEC_M_F32_04                      vD1;
    TgVEC_M_F32_04                      vD2;
    TgVEC_F32_04                        vPlnEqN;
    TgVEC_F32_04                        qR0;
    TgSINT32                            iCollect;
    union
    {
        P_TgUINT08                          pui;
        P_STg2_CO_Clip_List_F32_04          ps;
    }                                   sClipList;

    psDecal->m_sExtend.m_niVert = 0;

    vN0.m_mData = psDecal->m_sExtend.m_vNormal;
    F_Init_Basis_From_Vector_F32_04( &vT0, &vB0, &vN0 );
    vAA.m_mData = M_SEL_F32_04( vN0.m_mData, psDecal->m_sExtend.m_vSpin, KTgV_FFF0.m_f32_v04.m_mData );
    qR0 = F_QT_Init_Axis_Angle_F32_04( &vAA );
    vT0.m_mData = M_QT_INV_TX_F32_04( vT0.m_mData, qR0.m_mData );
    vB0.m_mData = M_QT_INV_TX_F32_04( vB0.m_mData, qR0.m_mData );
    vSize = M_LEN_F32_04( psDecal->m_sExtend.m_vSize );
    vInvSizeX = M_DIV_F32_04( KTgV_ONE_F32_04.m_mData, M_SPX_F32_04( vSize ) );
    vInvSizeY = M_DIV_F32_04( KTgV_ONE_F32_04.m_mData, M_SPY_F32_04( vSize ) );

    /* Create Clip Planes */
    vD0 = M_DOT_F32_04( psDecal->m_sExtend.m_vPos_W, vN0.m_mData );
    vPlnEqN.m_mData = M_SEL_F32_04( M_NEG_F32_04( vN0.m_mData ), M_ADD_F32_04( vSize, vD0 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__NEAR, &vPlnEqN );
    vPlnEqN.m_mData = M_SEL_F32_04( vN0.m_mData, M_SUB_F32_04( vSize, vD0 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__FAR, &vPlnEqN );

    vD1 = M_DOT_F32_04( psDecal->m_sExtend.m_vPos_W, vT0.m_mData );
    vPlnEqN.m_mData = M_SEL_F32_04( vT0.m_mData, M_SUB_F32_04( vSize, vD1 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__LEFT, &vPlnEqN );
    vPlnEqN.m_mData = M_SEL_F32_04( M_NEG_F32_04( vT0.m_mData ), M_ADD_F32_04( vSize, vD1 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__RIGHT, &vPlnEqN );

    vD2 = M_DOT_F32_04( psDecal->m_sExtend.m_vPos_W, vB0.m_mData );
    vPlnEqN.m_mData = M_SEL_F32_04( M_NEG_F32_04( vB0.m_mData ), M_ADD_F32_04( vSize, vD2 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__TOP, &vPlnEqN );
    vPlnEqN.m_mData = M_SEL_F32_04( vB0.m_mData, M_SUB_F32_04( vSize, vD2 ), KTgV_FFF0.m_f32_v04.m_mData );
    tgGM_PN_Set_PlnEqN_F32_04( asPN + ETgFRUSTUM_PLANE__BOTTOM, &vPlnEqN );

    psDecal->m_vBA_Max_W = MS_SET1_F32_04( -KTgMAX_F32 );
    psDecal->m_vBA_Min_W = MS_SET1_F32_04( KTgMAX_F32 );

    TgALLOCA( TgUINT08, sizeof( STg2_CO_Clip_List_F32_04 ) + 8 * sizeof( TgVEC_F32_04 ), sClipList.pui );

    for (iCollect = 0; iCollect < psCO_Request->m_niCollect_Last; ++iCollect)
    {
        TgVEC_F32_04                        vDirTest;
        ETgFRUSTUM_PLANE                    enPlane;
        TgFLOAT32                           fNormal_Theshold;
        TgVEC_M_F32_04                      vNormal_Theshold;
        TgVEC_M_F32_04                      avUV_A[9];
        TgSINT32                            niStart;
        TgSINT32                            niVert;
        TgSINT32                            iVert;

        vDirTest.m_mData = M_DOT_F32_04( psCO_Request->m_psCollect[iCollect].m_vNormal.m_mData, vN0.m_mData );
        if (TgFALSE != tgCM_NR0_F32( vDirTest.m.x ))
        {
            continue;
        };

        /* Clip each triangle to the frustum planes */
        if (nullptr == sClipList.ps)
        {
            psDecal->m_sExtend.m_niVert = 0;
            break;
        };
        sClipList.ps->m_niPoint = 3;
        sClipList.ps->m_niMax = 9;
        sClipList.ps->m_avPoint[0] = psCO_Request->m_psCollect[iCollect].m_avPoint[0];
        sClipList.ps->m_avPoint[1] = psCO_Request->m_psCollect[iCollect].m_avPoint[1];
        sClipList.ps->m_avPoint[2] = psCO_Request->m_psCollect[iCollect].m_avPoint[2];

        for (enPlane = ETgFRUSTUM_PLANE__NEAR; enPlane < ETgFRUSTUM_PLANE__MAX; ++enPlane)
        {
            if (TgFAILED(tgCO_F_PN_Clip_F32_04( sClipList.ps, asPN + enPlane )))
            {
                break;
            };
        };

        if (sClipList.ps->m_niPoint < 3)
        {
            continue;
        };

        if (3 * (sClipList.ps->m_niPoint - 2) + psDecal->m_sExtend.m_niVert >= ETgFX_DECAL__VERTEX_MAX)
        {
            TgWARNING_MSGF( 0, TgT( "%-16.16s(%-32.32s): Decal exceeded max vertex count.\n" ), TgT("FX"), TgT("tgFX__Update__Decal_Update__Write_Cache") );
            psDecal->m_sExtend.m_niVert = 0;
            break;
        };

        if (TgTRUE != tgFX__Update__Decal_Commit_Cache_Buffer( psDecal, 3 * (sClipList.ps->m_niPoint - 2) + psDecal->m_sExtend.m_niVert ))
        {
            TgWARNING_MSGF( 0, TgT( "%-16.16s(%-32.32s): Failed to commit decal buffer.\n" ), TgT("FX"), TgT("tgFX__Update__Decal_Update__Write_Cache") );
            psDecal->m_sExtend.m_niVert = 0;
            break;
        };

        /* Calculate the UVs for each point and an alpha fade parameter based on normal projection */
        fNormal_Theshold = TgTRUE == tgCM_NR0_F32( g_fFX_Decal_Normal_Threshold ) ? 1.0F : 1.0F / g_fFX_Decal_Normal_Threshold;
        vNormal_Theshold = MS_SET1_F32_04( fNormal_Theshold );
        for (iVert = 0; iVert < sClipList.ps->m_niPoint; ++iVert)
        {
            TgVEC_M_F32_04                      vDiff;
            TgVEC_M_F32_04                      vUV_X;
            TgVEC_M_F32_04                      vUV_Y;
            TgVEC_M_F32_04                      vUV_XY;
            TgFLOAT32                           fA;

            vDiff = M_SUB_F32_04( sClipList.ps->m_avPoint[iVert].m_mData, psDecal->m_sExtend.m_vPos_W );
            vUV_X = M_MUL_F32_04( M_DOT_F32_04( vDiff, vT0.m_mData ), vInvSizeX );
            vUV_Y = M_MUL_F32_04( M_DOT_F32_04( vDiff, vB0.m_mData ), vInvSizeY );
            vUV_XY = M_SEL_F32_04( vUV_X, vUV_Y, KTgV_F000.m_f32_v04.m_mData );

            fA = tgCM_MIN_F32( 1.0F, fNormal_Theshold * tgPM_ABS_F32( F_DOT_F32_04( &psCO_Request->m_psCollect[iCollect].m_vNormal, &vN0 ) ) );
            avUV_A[iVert] = M_SEL_F32_04( vUV_XY, MS_SET1_F32_04( fA ), KTgV_FF00.m_f32_v04.m_mData );
        };

        psDecal->m_vBA_Max_W = M_MAX_F32_04( psDecal->m_vBA_Max_W, sClipList.ps->m_avPoint[0].m_mData );
        psDecal->m_vBA_Min_W = M_MIN_F32_04( psDecal->m_vBA_Min_W, sClipList.ps->m_avPoint[0].m_mData );
        psDecal->m_vBA_Max_W = M_MAX_F32_04( psDecal->m_vBA_Max_W, sClipList.ps->m_avPoint[1].m_mData );
        psDecal->m_vBA_Min_W = M_MIN_F32_04( psDecal->m_vBA_Min_W, sClipList.ps->m_avPoint[1].m_mData );

        /* Output the clipped points as a triangle fan and calculate the basis vectors */
        niStart = psDecal->m_sExtend.m_niVert;
        niVert = psDecal->m_sExtend.m_niVert;
        for (iVert = 1; iVert + 1 < sClipList.ps->m_niPoint; niVert += 3, ++iVert)
        {
            psDecal->m_sExtend.m_psVert[niVert + 0].m_vS0_W = sClipList.ps->m_avPoint[0].m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 0].m_vT0 = vT0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 0].m_vB0 = vB0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 0].m_vUV_A = avUV_A[0];
            psDecal->m_sExtend.m_psVert[niVert + 1].m_vS0_W = sClipList.ps->m_avPoint[iVert + 0].m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 1].m_vT0 = vT0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 1].m_vB0 = vB0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 1].m_vUV_A = avUV_A[iVert + 0];
            psDecal->m_sExtend.m_psVert[niVert + 2].m_vS0_W = sClipList.ps->m_avPoint[iVert + 1].m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 2].m_vT0 = vT0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 2].m_vB0 = vB0.m_mData;
            psDecal->m_sExtend.m_psVert[niVert + 2].m_vUV_A = avUV_A[iVert + 1];

            psDecal->m_vBA_Max_W = M_MAX_F32_04( psDecal->m_vBA_Max_W, sClipList.ps->m_avPoint[iVert + 1].m_mData );
            psDecal->m_vBA_Min_W = M_MIN_F32_04( psDecal->m_vBA_Min_W, sClipList.ps->m_avPoint[iVert + 1].m_mData );
        };
        TgERROR( niStart != niVert );
        psDecal->m_sExtend.m_niVert = niVert;
    };
}