Src/GPUPerfAPIGL/ASICInfo.cpp
//=========================================================================================================
/// Get the value of a counter by name in a given group
/// \param nGroup GroupID to find counter in
/// \param pCounterStr String name of counter
/// \param rValue Value of counter (returned)
/// \return false if counter not found, true otherwise
//=========================================================================================================
Get the value of a counter by name in a given group
\param nGroup GroupID to find counter in
\param pCounterStr String name of counter
\param rValue Value of counter (returned)
\return false if counter not found, true otherwise
Get the value of a counter by name in a given group \ param nGroup GroupID to find counter in \ param pCounterStr String name of counter \ param rValue Value of counter ( returned ) \ return false if counter not found true otherwise
bool GetCounterValue(GLint nGroup, const char* pCounterStr, GLuint& rValue)
{
    GLint nNumCounters;
    bool bResult = false;
    _oglGetPerfMonitorCountersAMD(nGroup, &nNumCounters, nullptr, 0, nullptr);
    if (nNumCounters > 0)
    {
        GLuint* pCounterList = new(std::nothrow) GLuint[nNumCounters];
        if (nullptr != pCounterList)
        {
            _oglGetPerfMonitorCountersAMD(nGroup, nullptr, nullptr, nNumCounters, pCounterList);
            for (int i = 0; i < nNumCounters; i++)
            {
                char counterStr[256];
                _oglGetPerfMonitorCounterStringAMD(nGroup, pCounterList[i], 255, nullptr, counterStr);
                if (!strcmp(pCounterStr, counterStr))
                {
                    GLuint monitor;
                    GLuint nResultSize;
                    _oglGenPerfMonitorsAMD(1, &monitor);
                    _oglSelectPerfMonitorCountersAMD(monitor, GL_TRUE, nGroup, 1, &pCounterList[i]);
                    _oglBeginPerfMonitorAMD(monitor);
                    _oglEndPerfMonitorAMD(monitor);
                    _oglGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_SIZE_AMD, 4, &nResultSize, nullptr);
                    assert((GLint)nResultSize == 3 * sizeof(GLuint));
                    if ((GLint)nResultSize == 3 * sizeof(GLuint))
                    {
                        GLubyte* pCounterData;
                        pCounterData = new(std::nothrow) GLubyte[nResultSize];
                        if (nullptr != pCounterData)
                        {
                            _oglGetPerfMonitorCounterDataAMD(monitor, GL_PERFMON_RESULT_AMD, nResultSize, (GLuint*)pCounterData, nullptr);
                            rValue = ((GLuint*)pCounterData)[2];
                            bResult = true;
                            delete[] pCounterData;
                        }
                    }
                    _oglSelectPerfMonitorCountersAMD(monitor, GL_FALSE, nGroup, 1, &pCounterList[i]);
                    bool deletePerfMonitor = true;
#if defined(_LINUX) && defined(X86)
                    unsigned int majorVer;
                    unsigned int minorVer;
                    unsigned int subMinorVer;
                    if (AMDTADLUtils::Instance()->GetDriverVersion(majorVer, minorVer, subMinorVer) && 15 > majorVer)
                    {
                        deletePerfMonitor = false;
                    }
#endif
                    if (deletePerfMonitor)
                    {
                        _oglDeletePerfMonitorsAMD(1, &monitor);
                    }
                    break;
                }
            }
            delete[] pCounterList;
        }
    }
    return bResult;
}
bool GetCounterValue ( GLint nGroup , const char * pCounterStr , GLuint & rValue ) { GLint nNumCounters ; bool bResult = false ; _oglGetPerfMonitorCountersAMD ( nGroup , & nNumCounters , nullptr , 0 , nullptr ) ; if ( nNumCounters > 0 ) { GLuint * pCounterList = new ( std :: nothrow ) GLuint [ nNumCounters ] ; if ( nullptr != pCounterList ) { _oglGetPerfMonitorCountersAMD ( nGroup , nullptr , nullptr , nNumCounters , pCounterList ) ; for ( int i = 0 ; i < nNumCounters ; i ++ ) { char counterStr [ 256 ] ; _oglGetPerfMonitorCounterStringAMD ( nGroup , pCounterList [ i ] , 255 , nullptr , counterStr ) ; if ( ! strcmp ( pCounterStr , counterStr ) ) { GLuint monitor ; GLuint nResultSize ; _oglGenPerfMonitorsAMD ( 1 , & monitor ) ; _oglSelectPerfMonitorCountersAMD ( monitor , GL_TRUE , nGroup , 1 , & pCounterList [ i ] ) ; _oglBeginPerfMonitorAMD ( monitor ) ; _oglEndPerfMonitorAMD ( monitor ) ; _oglGetPerfMonitorCounterDataAMD ( monitor , GL_PERFMON_RESULT_SIZE_AMD , 4 , & nResultSize , nullptr ) ; assert ( ( GLint ) nResultSize == 3 * sizeof ( GLuint ) ) ; if ( ( GLint ) nResultSize == 3 * sizeof ( GLuint ) ) { GLubyte * pCounterData ; pCounterData = new ( std :: nothrow ) GLubyte [ nResultSize ] ; if ( nullptr != pCounterData ) { _oglGetPerfMonitorCounterDataAMD ( monitor , GL_PERFMON_RESULT_AMD , nResultSize , ( GLuint * ) pCounterData , nullptr ) ; rValue = ( ( GLuint * ) pCounterData ) [ 2 ] ; bResult = true ; delete [ ] pCounterData ; } } _oglSelectPerfMonitorCountersAMD ( monitor , GL_FALSE , nGroup , 1 , & pCounterList [ i ] ) ; bool deletePerfMonitor = true ; #if defined ( _LINUX ) && defined ( X86 ) unsigned int majorVer ; unsigned int minorVer ; unsigned int subMinorVer ; if ( AMDTADLUtils :: Instance ( ) -> GetDriverVersion ( majorVer , minorVer , subMinorVer ) && 15 > majorVer ) { deletePerfMonitor = false ; } #endif if ( deletePerfMonitor ) { _oglDeletePerfMonitorsAMD ( 1 , & monitor ) ; } break ; } } delete [ ] pCounterList ; } } return bResult ; }
Get the value of a counter by name in a given group
\param nGroup GroupID to find counter in
\param pCounterStr String name of counter
\param rValue Value of counter (returned)
\return false if counter not found, true otherwise
Get the value of a counter by name in a given group \ param nGroup GroupID to find counter in \ param pCounterStr String name of counter \ param rValue Value of counter ( returned ) \ return false if counter not found true otherwise
// Start by getting the list of counters in the group // Get the list of counters in the group // Counter found, now create a monitor with it and get its value // need to begin / end the monitor so that the data is obtained // Get the counter result size // Result should just be 3 GLuint per counter // Get the counter results // Workaround a driver issue by not deleting the perf monitor. // The 32-bit Linux driver will crash when glDeletePerfMonitor // is called.  This is a HUGE hack and introduces an intentional // memory leak. // The driver EPR is # 410865, and it was fixed starting with 15.10