Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS (DX12) Kernel - System [Enumeration].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 Functions                                                                                                                                                  */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

static P_STg2_KN_GPU_DXGI_Adapter           tgKN_GPU_PM_Enumerate__Process_Adapter( P_DXGI_ADAPTER, C_TgBOOL );
static TgRESULT                             tgKN_GPU_PM_Enumerate__Adapter_Output( PCU_STg2_KN_GPU_DXGI_Adapter );
static TgVOID                               tgKN_GPU_PM_Enumerate__Adapter_Mode( PCU_STg2_KN_GPU_DXGI_Adapter, PCU_STg2_KN_GPU_DXGI_Output );
static TgVOID                               tgKN_GPU_PM_Enumerate__Populate_Module( TgVOID );

static TgSINT32 CDECL                       tgKN_GPU_PM_Quick_Sort__DXGI_MODE_DESC( CPC_TgVOID, CPC_TgVOID );




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

/* ---- tgKN_GPU_PM_Enumerate ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_GPU_PM_Enumerate( TgVOID )
{
    TgSINT32                            iAdapter;
    TgBOOL                              bLoop;
    P_DXGI_ADAPTER                      psDXGI_Adapter;
    P_STg2_KN_GPU_DXGI_Adapter          psAdapter;

    TgCRITICAL(nullptr != g_pDXGIFactory_5);
    TgERROR(TgTRUE == tgKN_Query_Boot());

    g_niKN_GPU_Adapter = 0;

    for (iAdapter = 0, bLoop = TgTRUE; TgTRUE == bLoop && g_niKN_GPU_Adapter < KTgKN_GPU_MAX_ADAPTER; ++iAdapter)
    {

        if (FAILED(IDXGIFactory5_EnumAdapters( g_pDXGIFactory_5, iAdapter, &psDXGI_Adapter )))
        {
            /* #REVIEW: Does not work currently */
            //if (FAILED(IDXGIFactory5_EnumWarpAdapter( g_pDXGIFactory_5, &IID_IDXGIAdapter, &psDXGI_Adapter )))
            //{
            //    break;
            //};
            //bLoop = TgFALSE;
            break;
        };

        psAdapter = tgKN_GPU_PM_Enumerate__Process_Adapter( psDXGI_Adapter, TgTRUE == bLoop ? TgFALSE : TgTRUE );
        if (nullptr == psAdapter)
        {
            continue;
        };

        psAdapter->m_iOrdinal = iAdapter;
    };

    tgKN_GPU_PM_Enumerate__Populate_Module();

    return (0 != g_niKN_GPU_Adapter ? KTgS_OK : KTgE_FAIL);
}


/* ---- tgKN_GPU_PM_Query_Mode_List ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSINT32 tgKN_GPU_PM_Query_Mode_List( P_STg2_KN_GPU_Mode psMode, C_TgSINT32 niMode, C_TgSINT32 iAdapter, C_TgSINT32 iOutput, C_ETgKN_GPU_TARGET enFormat )
{
    DXGI_FORMAT                         enDXGI_Format;
    TgSINT32                            niRet;
    TgSINT32                            iMode;

    TgPARAM_CHECK_INDEX( iAdapter, g_asKN_GPU_DXGI_Adapter );
    TgPARAM_CHECK_INDEX( iOutput, g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput );

    enDXGI_Format = KTgKN_DX_TARGET_LIST[enFormat];
    niRet = 0;
    if (nullptr == psMode)
    {
        for (iMode = 0; iMode < g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_niMode; ++iMode)
        {
            if (g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].Format != enDXGI_Format)
                continue;
            ++niRet;
        };
    }
    else
    {
        for (iMode = 0; iMode < g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_niMode; ++iMode)
        {
            if (g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].Format != enDXGI_Format)
                continue;
            if (niRet < niMode)
            {
                psMode[niRet].m_uiWidth = g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].Width;
                psMode[niRet].m_uiHeight = g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].Height;
                psMode[niRet].m_uiRefresh_Rate  = g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].RefreshRate.Numerator;
                psMode[niRet].m_uiRefresh_Rate /= g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput->m_psMode[iMode].RefreshRate.Denominator;
                psMode[niRet].m_enFormat = enFormat;
            };
            ++niRet;
        };
    };

    return (niRet);
}




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

/* ---- tgKN_GPU_PM_Enumerate__Process_Adapter -------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_STg2_KN_GPU_DXGI_Adapter tgKN_GPU_PM_Enumerate__Process_Adapter( P_DXGI_ADAPTER psDXGI_Adapter, C_TgBOOL bSoftware )
{
    P_STg2_KN_GPU_DXGI_Adapter          psAdapter;
    P_DXGI_ADAPTER3                     psDXGI_Adapter_3;
    TgSINT32                            iFormat;

    /* IDXGIAdapter3 adds memory management functionality that will be needed */
    if (FAILED(IDXGIAdapter1_QueryInterface( psDXGI_Adapter, &IID_IDXGIAdapter3, (PP_TgVOID)&psDXGI_Adapter_3 )))
    {
        TgVERIFY(0 == IDXGIAdapter_Release( psDXGI_Adapter ));
        return (nullptr);
    };
    TgVERIFY(1 == IDXGIAdapter_Release( psDXGI_Adapter ));
    psDXGI_Adapter = nullptr;

    psAdapter = g_asKN_GPU_DXGI_Adapter + g_niKN_GPU_Adapter;
    TgCRITICAL(0 != psAdapter);

    psAdapter->m_pDXGI_Adapter_3 = psDXGI_Adapter_3;
    TgVERIFY(SUCCEEDED(IDXGIAdapter3_GetDesc2( psDXGI_Adapter_3, &psAdapter->m_sAdapter )));
    psAdapter->m_psDevice1 = nullptr;

    if ((TgTRUE != bSoftware) && (psAdapter->m_sAdapter.Flags & DXGI_ADAPTER_FLAG_SOFTWARE))
    {
        tgKN_GPU_DXGI_Free_Adapter( psAdapter );
        return (nullptr);
    };

    /* Attempt to acquire a 12.x feature level first (if not installed we get an INVALIDARG fail immediately) */
    psAdapter->m_psDevice1 = tgKN_GPU_DXGI_Device__Create( &psAdapter->m_enFeature_Level, psAdapter );
    if (nullptr == psAdapter->m_psDevice1)
    {
        tgKN_GPU_DXGI_Free_Adapter( psAdapter );
        return (nullptr);
    };

    /* Check for multi-sample support */
    memset( psAdapter->m_aiMS, 0xFF, sizeof( psAdapter->m_aiMS ) );
    for (iFormat = 0; iFormat < ETgKN_GPU_TARGET__MAX; ++iFormat)
    {
        TgSINT32                            iMS;
        D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS sMS;

        sMS.Format = KTgKN_DX_TARGET_LIST[iFormat];
        sMS.Flags = D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE;

        for (iMS = 0; iMS < KTgKN_GPU_MS__MAX && iMS < D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; ++iMS)
        {
            sMS.SampleCount = (UINT)(iMS + 1);
            sMS.NumQualityLevels = 0;

            if (FAILED(ID3D12Device_CheckFeatureSupport(
                psAdapter->m_psDevice1, D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &sMS, sizeof( D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS ) ) ))
            {
                continue;
            };

            if (sMS.NumQualityLevels > 0)
            {
                psAdapter->m_aiMS[iFormat][iMS] = (TgSINT32)sMS.NumQualityLevels;
            };
        };
    };

    psAdapter->m_niOutput = 0;

    if (TgFAILED(tgKN_GPU_PM_Enumerate__Adapter_Output( psAdapter )))
    {
        // This would be really screwed up since we are not filtering any of the devices or modes but just in case ...
        tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT("%-16.16s(%-48.48s): Failed to find any device and/or modes with adapter.\n"), TgT("Kernel"), TgT("GPU Enumerate") );
        tgKN_GPU_DXGI_Free_Adapter( psAdapter );
        return (nullptr);
    };

    ++g_niKN_GPU_Adapter;

    return (psAdapter);
}


/* ---- tgKN_GPU_PM_Enumerate__Adapter_Output --------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgRESULT tgKN_GPU_PM_Enumerate__Adapter_Output( PCU_STg2_KN_GPU_DXGI_Adapter psAdapter )
{
    TgSINT32                            iOutput;
    DXGI_MODE_DESC1                     sSrcDesc, sMatchDesc;
    P_STg2_KN_GPU_DXGI_Output           psOutput;
    TgSINT32                            iFormat;

    memset( &sSrcDesc, 0, sizeof( sSrcDesc ) );
    memset( &sMatchDesc, 0, sizeof( sMatchDesc ) );

    psAdapter->m_niOutput = 0;
    for (iOutput = 0; psAdapter->m_niOutput < KTgKN_GPU_MAX_OUTPUT; ++iOutput)
    {
        IDXGIOutput                         *pDXGI_Output = nullptr;

        psOutput = psAdapter->m_asOutput + psAdapter->m_niOutput;

        if (FAILED(IDXGIAdapter3_EnumOutputs( psAdapter->m_pDXGI_Adapter_3, iOutput, &pDXGI_Output )))
            break;

        if (FAILED(IDXGIOutput_QueryInterface( pDXGI_Output, &IID_IDXGIOutput5, (PP_TgVOID)&psOutput->m_pDXGI_Output_5 ) ))
        {
            TgVERIFY(0 == IDXGIOutput_Release( pDXGI_Output ));
            continue;
        };

        TgVERIFY(SUCCEEDED(IDXGIOutput1_GetDesc( pDXGI_Output, &psOutput->m_sDXGI_Output )));

        tgKN_GPU_PM_Enumerate__Adapter_Mode( psAdapter, psOutput );
        if (0 >= psOutput->m_niMode)
        {
            /* This would be really screwed up since we are not filtering any of the devices or modes but just in case ... */
            tgCN_PrintF( KTgCN_CHANEL_ERROR, TgT("%-16.16s(%-48.48s): Failed to find any modes for an adapter output.\n"), TgT("Kernel"),
                         TgT("tgKN_GPU_PM_Enumerate__Adapter_Output") );
            TgVERIFY(0 == IDXGIOutput_Release( pDXGI_Output ));
            continue;
        };
        TgERROR(0 < psOutput->m_niMode);

        psOutput->m_iOrdinal = iOutput;

        for (iFormat = 0; iFormat < ETgKN_GPU_TARGET__MAX; ++iFormat)
        {
            if (0 >= psOutput->m_niMode_Target_Format[iFormat])
                continue;

            sSrcDesc.Format = KTgKN_DX_TARGET_LIST[iFormat];

            TgVERIFY(SUCCEEDED(IDXGIOutput5_FindClosestMatchingMode1( psOutput->m_pDXGI_Output_5, &sSrcDesc, &sMatchDesc, (IUnknown*)psAdapter->m_psDevice1 )));
            if (sSrcDesc.Format != sMatchDesc.Format)
                psOutput->m_niMode_Target_Format[iFormat] = 0;
        };

        ++psAdapter->m_niOutput;
    };

    return (0 < psAdapter->m_niOutput ? KTgS_OK : KTgE_FAIL);
}


/* ---- tgKN_GPU_PM_Enumerate__Adapter_Mode ----------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgVOID tgKN_GPU_PM_Enumerate__Adapter_Mode( PCU_STg2_KN_GPU_DXGI_Adapter psAdapter, PCU_STg2_KN_GPU_DXGI_Output psOutput )
{
    UINT                                nuiModeFormat[ETgKN_GPU_TARGET__MAX];
    TgSINT32                            niMode, niModeMax;
    TgSINT32                            iIndex, iFormat;
    TgSINT32                            iRet;
    P_DXGI_MODE_DESC1                   psModes;
    TgSINT32                            iIndex_Unique;

    TgPARAM_CHECK((nullptr != psAdapter) && (nullptr != psOutput));

    niModeMax = 2048;
    niMode = 0;
    iFormat = 0;

    psModes = (P_DXGI_MODE_DESC1)TgMALLOC_TEMP(sizeof(DXGI_MODE_DESC1)*(TgSIZE)niModeMax);
    TgCRITICAL(0 != psModes);

    while (iFormat < 4) /* #HACK: ETgKN_GPU_TARGET__MAX */
    {
        iRet = IDXGIOutput5_GetDisplayModeList1(
            psOutput->m_pDXGI_Output_5, KTgKN_DX_TARGET_LIST[iFormat], DXGI_ENUM_MODES_SCALING, nuiModeFormat + iFormat, psModes + niMode );

        if (DXGI_ERROR_NOT_FOUND == iRet)
        {
            TgFREE_TEMP( psModes );
            psOutput->m_psMode = 0;
            psOutput->m_niMode = 0;
            return;
        };

        /* Make a single fake display mode for remote sessions */
        if ((DXGI_ERROR_NOT_CURRENTLY_AVAILABLE == iRet) && (DXGI_FORMAT_R8G8B8A8_UNORM == KTgKN_DX_TARGET_LIST[iFormat]))
        {
            if (niMode == niModeMax)
            {
                // Not particularly a clean way to deal with the issue but it will work.
                iRet = DXGI_ERROR_MORE_DATA;
            }
            else
            {
                if( 0 != g_pfnGetSystemMetrics( 0x1000 ) ) // SM_REMOTESESSION
                {
                    DEVMODE                             sDevMode;

                    sDevMode.dmSize = sizeof( DEVMODE );
                    if (g_pfnEnumDisplaySettings( nullptr, ENUM_CURRENT_SETTINGS, &sDevMode ))
                    {
                        psModes[niMode].Width = sDevMode.dmPelsWidth;
                        psModes[niMode].Height = sDevMode.dmPelsHeight;
                        psModes[niMode].RefreshRate.Numerator = 60;
                        psModes[niMode].RefreshRate.Denominator = 1;
                        psModes[niMode].Format = DXGI_FORMAT_R8G8B8A8_UNORM;
                        psModes[niMode].ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
                        psModes[niMode].Scaling = DXGI_MODE_SCALING_CENTERED;
                        psModes[niMode].Stereo = 0;

                        nuiModeFormat[iFormat] = 1;
                    };
                };
            };
        };

        /* Keep increasing the max pool until we have enough entries.  Should only require one attempt. */
        if (DXGI_ERROR_MORE_DATA == iRet)
        {
            P_DXGI_MODE_DESC1                   psModesNew;

            niModeMax += 512;

            psModesNew = (DXGI_MODE_DESC1*)TgMALLOC_TEMP(sizeof(DXGI_MODE_DESC1)*(TgSIZE)niModeMax);
            TgCRITICAL(0 != psModesNew);

            memcpy( psModesNew, psModes, sizeof(DXGI_MODE_DESC1)*(TgSIZE)niMode );

            TgFREE_TEMP( psModes );
            psModes = psModesNew;
            continue;
        };

        TgCRITICAL(nuiModeFormat[iFormat] < (TgUINT32)KTgMAX_S32);
        TgCRITICAL(niMode < KTgMAX_S32 - (TgSINT32)nuiModeFormat[iFormat]);
        niMode += (TgSINT32)nuiModeFormat[iFormat];
        ++iFormat;
    };

    /* Sort the modes for the selection routines and remove duplicates */
    qsort( psModes, (TgSIZE)niMode, sizeof(DXGI_MODE_DESC1), tgKN_GPU_PM_Quick_Sort__DXGI_MODE_DESC );
    for (iIndex_Unique = 0, iIndex = 1; iIndex < niMode; ++iIndex)
    {
        if (0 != tgKN_GPU_PM_Quick_Sort__DXGI_MODE_DESC( psModes + iIndex_Unique, psModes + iIndex ))
        {
            ++iIndex_Unique;
            if (iIndex_Unique != iIndex)
            {
                psModes[iIndex_Unique] = psModes[iIndex];
            };
            continue;
        };
    };
    niMode = iIndex_Unique + 1;

    psOutput->m_psMode = (DXGI_MODE_DESC1*)TgMALLOC_POOL(sizeof(DXGI_MODE_DESC1)*(TgSIZE)niMode);
    memcpy( psOutput->m_psMode, psModes, sizeof(DXGI_MODE_DESC1)*(TgSIZE)niMode );
    TgFREE_TEMP( psModes );

    for (iFormat = 0; iFormat < ETgKN_GPU_TARGET__MAX; ++iFormat)
    {
        psOutput->m_niMode_Target_Format[iFormat] = (TgSINT32)nuiModeFormat[iFormat];
    };
    psOutput->m_niMode = niMode;
}


/* ---- tgKN_GPU_PM_Enumerate__Populate_Module -------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_GPU_PM_Enumerate__Populate_Module( TgVOID )
{
    TgSINT32                            iAdapter;

    for (iAdapter = 0; iAdapter < g_niKN_GPU_Adapter; ++iAdapter)
    {
        P_STg2_KN_GPU_DXGI_Output           psDXGI_Output;
        P_STg2_KN_GPU_Output                psOutput;
        TgSINT32                            iOutput;

        tgCN_PrintF( KTgCN_CHANEL_INITIALIZE | KTgCN_SEVERITY_9, TgT( "%-16.16s(%-48.48s): Adapter: %d\n" ), TgT( "Kernel" ), TgT( "tgKN_GPU_PM_Enumerate" ), iAdapter );

    #if TgS_STAT_KERNEL || TgS_DEBUG_KERNEL
        tgKN_GPU_DXGI_Output__Adapter( g_asKN_GPU_DXGI_Adapter + iAdapter );
    #endif

        tgSZ_FromWideChar( g_asKN_GPU_Adapter[iAdapter].m_szAdapter, TgARRAY_COUNT( g_asKN_GPU_Adapter[iAdapter].m_szAdapter ),
                           g_asKN_GPU_DXGI_Adapter[iAdapter].m_sAdapter.Description, TgARRAY_COUNT( g_asKN_GPU_DXGI_Adapter[iAdapter].m_sAdapter.Description ) );
        g_asKN_GPU_Adapter[iAdapter].m_niOutput = 0;

        psDXGI_Output = g_asKN_GPU_DXGI_Adapter[iAdapter].m_asOutput;
        psOutput = g_asKN_GPU_Adapter[iAdapter].m_asOutput;
        for (iOutput = 0; iOutput < g_asKN_GPU_DXGI_Adapter[iAdapter].m_niOutput; ++iOutput)
        {
            TgSINT32                            iFormat;

            tgSZ_FromWideChar( psOutput->m_szName, TgARRAY_COUNT( psOutput->m_szName ), psDXGI_Output->m_sDXGI_Output.DeviceName,
                               TgARRAY_COUNT( psDXGI_Output->m_sDXGI_Output.DeviceName ) );
            psOutput->m_uiFlags = 0;
            for (iFormat = 0; iFormat < ETgKN_GPU_TARGET__MAX; ++iFormat)
            {
                tgBF_Set_Flag_U64( &g_asKN_GPU_Adapter[iAdapter].m_asOutput[g_asKN_GPU_Adapter[iAdapter].m_niOutput].m_uiFlags,
                                   (TgUINT32)(ETgKN_GPU_ADAPTER_OUTPUT_FLAGS__TARGET__START + iFormat),
                                   0 != psDXGI_Output->m_niMode_Target_Format[iFormat] ? TgTRUE : TgFALSE );

            };
            ++psDXGI_Output;
            ++psOutput;
            ++g_asKN_GPU_Adapter[iAdapter].m_niOutput;
        };

        TgERROR( g_asKN_GPU_DXGI_Adapter[iAdapter].m_niOutput == g_asKN_GPU_Adapter[iAdapter].m_niOutput );
    };
}


/* ---- tgKN_GPU_PM_Quick_Sort__DXGI_MODE_DESC -------------------------------------------------------------------------------------------------------------------------- */
/* NOTE: Mode selection routines depend on this specific ordering of the modes.                                                                                           */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgSINT32 CDECL tgKN_GPU_PM_Quick_Sort__DXGI_MODE_DESC( CPC_TgVOID arg1, CPC_TgVOID arg2 )
{
    const DXGI_MODE_DESC1               *pdm1 = (const DXGI_MODE_DESC1*)arg1;
    const DXGI_MODE_DESC1               *pdm2 = (const DXGI_MODE_DESC1*)arg2;

    if (pdm1->Width > pdm2->Width) return 1;
    if (pdm1->Width < pdm2->Width) return -1;
    if (pdm1->Height > pdm2->Height) return 1;
    if (pdm1->Height < pdm2->Height) return -1;
    if (pdm1->Format > pdm2->Format) return 1;
    if (pdm1->Format < pdm2->Format) return -1;
    if (pdm1->RefreshRate.Numerator > pdm2->RefreshRate.Numerator) return 1;
    if (pdm1->RefreshRate.Numerator < pdm2->RefreshRate.Numerator) return -1;
    return 0;
}