VirtualBox

Ignore:
Timestamp:
May 6, 2014 5:21:16 PM (11 years ago)
Author:
vboxsync
Message:

crOpenGL: getAttribLocations

Location:
trunk/src/VBox/GuestHost/OpenGL/state_tracker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_glsl.c

    r50041 r51200  
    5858}
    5959
     60#ifdef IN_GUEST
     61static void crStateFreeProgramAttribsLocationCache(CRGLSLProgram* pProgram)
     62{
     63    if (pProgram->pAttribs) crFree(pProgram->pAttribs);
     64
     65    pProgram->pAttribs = NULL;
     66    pProgram->cAttribs = 0;
     67}
     68#endif
     69
    6070static void crStateFreeProgramAttribs(CRGLSLProgram* pProgram)
    6171{
     
    7787    if (pProgram->currentState.pAttribs)
    7888        crFree(pProgram->currentState.pAttribs);
     89
     90#ifdef IN_GUEST
     91    crStateFreeProgramAttribsLocationCache(pProgram);
     92
     93    pProgram->bAttribsSynced = GL_FALSE;
     94#endif
     95
    7996}
    8097
     
    361378    pProgram->pUniforms = NULL;
    362379    pProgram->cUniforms = 0;
     380
    363381#ifdef IN_GUEST
     382    pProgram->pAttribs = NULL;
     383    pProgram->cAttribs = 0;
     384
    364385    pProgram->bUniformsSynced = GL_FALSE;
     386    pProgram->bAttribsSynced = GL_FALSE;
    365387#endif
    366388
     
    600622        pProgram->activeState.pAttribs[i].name = crStrdup(pProgram->currentState.pAttribs[i].name);
    601623    }
     624
     625#ifdef IN_GUEST
     626    crStateFreeProgramAttribsLocationCache(pProgram);
     627#endif
    602628
    603629    crStateFreeProgramUniforms(pProgram);
     
    770796    if (!pProgram)
    771797    {
    772         crWarning("Unknown program %d", program);
     798        WARN(("Unknown program %d", program));
    773799        return GL_FALSE;
    774800    }
     
    777803    return pProgram->bUniformsSynced;
    778804#else
    779     crWarning("crStateIsProgramUniformsCached called on host side!!");
     805    WARN(("crStateIsProgramUniformsCached called on host side!!"));
     806    return GL_FALSE;
     807#endif
     808}
     809
     810DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramAttribsCached(GLuint program)
     811{
     812    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
     813
     814    if (!pProgram)
     815    {
     816        WARN(("Unknown program %d", program));
     817        return GL_FALSE;
     818    }
     819
     820#ifdef IN_GUEST
     821    return pProgram->bAttribsSynced;
     822#else
     823    WARN(("crStateIsProgramAttribsCached called on host side!!"));
    780824    return GL_FALSE;
    781825#endif
     
    856900
    857901    pProgram->bUniformsSynced = GL_TRUE;
     902
     903    CRASSERT((pCurrent-((char*)pData))==cbRead);
     904    CRASSERT(cbRead==cbData);
     905}
     906
     907DECLEXPORT(void) STATE_APIENTRY
     908crStateGLSLProgramCacheAttribs(GLuint program, GLsizei cbData, GLvoid *pData)
     909{
     910    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
     911    char *pCurrent = pData;
     912    GLsizei cbRead, cbName;
     913    GLuint i;
     914
     915    if (!pProgram)
     916    {
     917        WARN(("Unknown program %d", program));
     918        return;
     919    }
     920
     921    if (pProgram->bAttribsSynced)
     922    {
     923        WARN(("crStateGLSLProgramCacheAttribs: this shouldn't happen!"));
     924        crStateFreeProgramAttribsLocationCache(pProgram);
     925    }
     926
     927    if (cbData<sizeof(GLsizei))
     928    {
     929        WARN(("crStateGLSLProgramCacheAttribs: data too short"));
     930        return;
     931    }
     932
     933    pProgram->cAttribs = ((GLsizei*)pCurrent)[0];
     934    pCurrent += sizeof(GLsizei);
     935    cbRead = sizeof(GLsizei);
     936
     937    crDebug("crStateGLSLProgramCacheAttribs: %i active attribs", pProgram->cAttribs);
     938
     939    if (pProgram->cAttribs)
     940    {
     941        pProgram->pAttribs = crAlloc(pProgram->cAttribs*sizeof(CRGLSLAttrib));
     942        if (!pProgram->pAttribs)
     943        {
     944            WARN(("crStateGLSLProgramCacheAttribs: no memory"));
     945            pProgram->cAttribs = 0;
     946            return;
     947        }
     948    }
     949
     950    for (i=0; i<pProgram->cAttribs; ++i)
     951    {
     952        cbRead += sizeof(GLuint)+sizeof(GLsizei);
     953        if (cbRead>cbData)
     954        {
     955            crWarning("crStateGLSLProgramCacheAttribs: out of data reading attrib %i", i);
     956            return;
     957        }
     958        pProgram->pAttribs[i].index = ((GLint*)pCurrent)[0];
     959        pCurrent += sizeof(GLint);
     960        cbName = ((GLsizei*)pCurrent)[0];
     961        pCurrent += sizeof(GLsizei);
     962
     963        cbRead += cbName;
     964        if (cbRead>cbData)
     965        {
     966            crWarning("crStateGLSLProgramCacheAttribs: out of data reading attrib's name %i", i);
     967            return;
     968        }
     969
     970        pProgram->pAttribs[i].name = crStrndup(pCurrent, cbName);
     971        pCurrent += cbName;
     972
     973        crDebug("crStateGLSLProgramCacheAttribs: attribs[%i]=%d, %s", i, pProgram->pAttribs[i].index, pProgram->pAttribs[i].name);
     974    }
     975
     976    pProgram->bAttribsSynced = GL_TRUE;
    858977
    859978    CRASSERT((pCurrent-((char*)pData))==cbRead);
     
    9911110    CRASSERT((pCurrent-((char*)pData))==cbWritten);
    9921111}
     1112
     1113static GLboolean crStateGLSLProgramCacheOneAttrib(GLuint location, GLsizei cbName, GLchar *pName,
     1114                                                   char **pCurrent, GLsizei *pcbWritten, GLsizei maxcbData)
     1115{
     1116    *pcbWritten += sizeof(GLint)+sizeof(GLsizei)+cbName;
     1117    if (*pcbWritten>maxcbData)
     1118    {
     1119        WARN(("crStateGLSLProgramCacheOneAttrib: buffer too small"));
     1120        crFree(pName);
     1121        return GL_FALSE;
     1122    }
     1123
     1124    crDebug("crStateGLSLProgramCacheOneAttrib: attrib[%i]=%s.", location, pName);
     1125
     1126    ((GLint*)*pCurrent)[0] = location;
     1127    *pCurrent += sizeof(GLint);
     1128    ((GLsizei*)*pCurrent)[0] = cbName;
     1129    *pCurrent += sizeof(GLsizei);
     1130    crMemcpy(*pCurrent, pName, cbName);
     1131    *pCurrent += cbName;
     1132
     1133    return GL_TRUE;
     1134}
     1135
     1136DECLEXPORT(void) STATE_APIENTRY
     1137crStateGLSLProgramCacheAttribs(GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData)
     1138{
     1139    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
     1140    GLint maxAttribLen, activeAttribs=0, fakeAttribsCount, i, j;
     1141    char *pCurrent = pData;
     1142    GLsizei cbWritten;
     1143
     1144    if (!pProgram)
     1145    {
     1146        crWarning("Unknown program %d", program);
     1147        return;
     1148    }
     1149
     1150    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxAttribLen);
     1151    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTES, &activeAttribs);
     1152
     1153    *cbData = 0;
     1154
     1155    cbWritten = sizeof(GLsizei);
     1156    if (cbWritten>maxcbData)
     1157    {
     1158        crWarning("crStateGLSLProgramCacheAttribs: buffer too small");
     1159        return;
     1160    }
     1161    ((GLsizei*)pCurrent)[0] = activeAttribs;
     1162    fakeAttribsCount = activeAttribs;
     1163    pCurrent += sizeof(GLsizei);
     1164
     1165    crDebug("crStateGLSLProgramCacheAttribs: %i active attribs", activeAttribs);
     1166
     1167    if (activeAttribs>0)
     1168    {
     1169        /*+8 to make sure our array attribs with higher indices and [] will fit in as well*/
     1170        GLchar *name = (GLchar *) crAlloc(maxAttribLen+8);
     1171        GLenum type;
     1172        GLint size;
     1173        GLsizei cbName;
     1174        GLint location;
     1175
     1176        if (!name)
     1177        {
     1178            crWarning("crStateGLSLProgramCacheAttribs: no memory");
     1179            return;
     1180        }
     1181
     1182        for (i=0; i<activeAttribs; ++i)
     1183        {
     1184            diff_api.GetActiveAttrib(pProgram->hwid, i, maxAttribLen, &cbName, &size, &type, name);
     1185            location = diff_api.GetAttribLocation(pProgram->hwid, name);
     1186
     1187            if (!crStateGLSLProgramCacheOneAttrib(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
     1188                return;
     1189
     1190            /* Only one active attrib variable will be reported for a attrib array by glGetActiveAttrib,
     1191             * so we insert fake elements for other array elements.
     1192             */
     1193            if (size!=1)
     1194            {
     1195                char *pIndexStr = crStrchr(name, '[');
     1196                GLint firstIndex=1;
     1197                fakeAttribsCount += size;
     1198
     1199                crDebug("crStateGLSLProgramCacheAttribs: expanding array attrib, size=%i", size);
     1200
     1201                /*For array attribs it's valid to query location of 1st element as both attrib and attrib[0].
     1202                 *The name returned by glGetActiveAttrib is driver dependent,
     1203                 *atleast it's with [0] on win/ati and without [0] on linux/nvidia.
     1204                 */
     1205                if (!pIndexStr)
     1206                {
     1207                    pIndexStr = name+cbName;
     1208                    firstIndex=0;
     1209                }
     1210                else
     1211                {
     1212                    cbName = pIndexStr-name;
     1213                    if (!crStateGLSLProgramCacheOneAttrib(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
     1214                        return;
     1215                }
     1216
     1217                for (j=firstIndex; j<size; ++j)
     1218                {
     1219                    sprintf(pIndexStr, "[%i]", j);
     1220                    cbName = crStrlen(name);
     1221
     1222                    location = diff_api.GetAttribLocation(pProgram->hwid, name);
     1223
     1224                    if (!crStateGLSLProgramCacheOneAttrib(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
     1225                        return;
     1226                }
     1227            }
     1228        }
     1229
     1230        crFree(name);
     1231    }
     1232
     1233    if (fakeAttribsCount!=activeAttribs)
     1234    {
     1235        ((GLsizei*)pData)[0] = fakeAttribsCount;
     1236        crDebug("FakeCount %i", fakeAttribsCount);
     1237    }
     1238
     1239    *cbData = cbWritten;
     1240
     1241    CRASSERT((pCurrent-((char*)pData))==cbWritten);
     1242}
    9931243#endif
    9941244
     
    10241274#else
    10251275    crWarning("crStateGetUniformLocation called on host side!!");
     1276    return -1;
     1277#endif
     1278}
     1279
     1280DECLEXPORT(GLint) STATE_APIENTRY crStateGetAttribLocation(GLuint program, const char * name)
     1281{
     1282#ifdef IN_GUEST
     1283    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
     1284    GLint result=-1;
     1285    GLuint i;
     1286
     1287    if (!pProgram)
     1288    {
     1289        WARN(("Unknown program %d", program));
     1290        return -1;
     1291    }
     1292
     1293    if (!pProgram->bAttribsSynced)
     1294    {
     1295        WARN(("crStateGetAttribLocation called for uncached attribs"));
     1296        return -1;
     1297    }
     1298
     1299    for (i=0; i<pProgram->cAttribs; ++i)
     1300    {
     1301        if (!crStrcmp(name, pProgram->pAttribs[i].name))
     1302        {
     1303            result = pProgram->pAttribs[i].index;
     1304            break;
     1305        }
     1306    }
     1307
     1308    return result;
     1309#else
     1310    crWarning("crStateGetAttribLocation called on host side!!");
    10261311    return -1;
    10271312#endif
  • trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_special

    r44326 r51200  
    377377ValidateProgram
    378378BindAttribLocation
     379GetAttribLocation
    379380GetUniformLocation
    380381CopyTexImage2D
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette