Newer
Older
David Parks
committed
if (mState)
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
{
mWasEnabled = sStateMap[state];
llassert(mWasEnabled == glIsEnabled(state));
setEnabled(enabled);
stop_glerror();
}
}
void LLGLState::setEnabled(S32 enabled)
{
if (!mState)
{
return;
}
if (enabled == CURRENT_STATE)
{
enabled = sStateMap[mState] == GL_TRUE ? TRUE : FALSE;
}
else if (enabled == TRUE && sStateMap[mState] != GL_TRUE)
{
gGL.flush();
glEnable(mState);
sStateMap[mState] = GL_TRUE;
}
else if (enabled == FALSE && sStateMap[mState] != GL_FALSE)
{
gGL.flush();
glDisable(mState);
sStateMap[mState] = GL_FALSE;
}
mIsEnabled = enabled;
}
LLGLState::~LLGLState()
{
stop_glerror();
if (mState)
{
if (gDebugGL)
{
if (!gDebugSession)
{
llassert_always(sStateMap[mState] == glIsEnabled(mState));
}
else
{
if (sStateMap[mState] != glIsEnabled(mState))
{
ll_fail("GL enabled state does not match expected");
}
}
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
}
if (mIsEnabled != mWasEnabled)
{
gGL.flush();
if (mWasEnabled)
{
glEnable(mState);
sStateMap[mState] = GL_TRUE;
}
else
{
glDisable(mState);
sStateMap[mState] = GL_FALSE;
}
}
}
stop_glerror();
}
////////////////////////////////////////////////////////////////////////////////
void LLGLManager::initGLStates()
{
//gl states moved to classes in llglstates.h
LLGLState::initClass();
}
////////////////////////////////////////////////////////////////////////////////
void parse_gl_version( S32* major, S32* minor, S32* release, std::string* vendor_specific, std::string* version_string )
{
// GL_VERSION returns a null-terminated string with the format:
// <major>.<minor>[.<release>] [<vendor specific>]
const char* version = (const char*) glGetString(GL_VERSION);
*major = 0;
*minor = 0;
*release = 0;
vendor_specific->assign("");
if( !version )
{
return;
}
version_string->assign(version);
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
std::string ver_copy( version );
S32 len = (S32)strlen( version ); /* Flawfinder: ignore */
S32 i = 0;
S32 start;
// Find the major version
start = i;
for( ; i < len; i++ )
{
if( '.' == version[i] )
{
break;
}
}
std::string major_str = ver_copy.substr(start,i-start);
LLStringUtil::convertToS32(major_str, *major);
if( '.' == version[i] )
{
i++;
}
// Find the minor version
start = i;
for( ; i < len; i++ )
{
if( ('.' == version[i]) || isspace(version[i]) )
{
break;
}
}
std::string minor_str = ver_copy.substr(start,i-start);
LLStringUtil::convertToS32(minor_str, *minor);
// Find the release number (optional)
if( '.' == version[i] )
{
i++;
start = i;
for( ; i < len; i++ )
{
if( isspace(version[i]) )
{
break;
}
}
std::string release_str = ver_copy.substr(start,i-start);
LLStringUtil::convertToS32(release_str, *release);
}
// Skip over any white space
while( version[i] && isspace( version[i] ) )
{
i++;
}
// Copy the vendor-specific string (optional)
if( version[i] )
{
vendor_specific->assign( version + i );
}
}
David Parks
committed
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
void parse_glsl_version(S32& major, S32& minor)
{
// GL_SHADING_LANGUAGE_VERSION returns a null-terminated string with the format:
// <major>.<minor>[.<release>] [<vendor specific>]
const char* version = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
major = 0;
minor = 0;
if( !version )
{
return;
}
std::string ver_copy( version );
S32 len = (S32)strlen( version ); /* Flawfinder: ignore */
S32 i = 0;
S32 start;
// Find the major version
start = i;
for( ; i < len; i++ )
{
if( '.' == version[i] )
{
break;
}
}
std::string major_str = ver_copy.substr(start,i-start);
LLStringUtil::convertToS32(major_str, major);
if( '.' == version[i] )
{
i++;
}
// Find the minor version
start = i;
for( ; i < len; i++ )
{
if( ('.' == version[i]) || isspace(version[i]) )
{
break;
}
}
std::string minor_str = ver_copy.substr(start,i-start);
LLStringUtil::convertToS32(minor_str, minor);
}
LLGLUserClipPlane::LLGLUserClipPlane(const LLPlane& p, const glh::matrix4f& modelview, const glh::matrix4f& projection, bool apply)
mApply = apply;
if (mApply)
{
mModelview = modelview;
mProjection = projection;
setPlane(p[0], p[1], p[2], p[3]);
}
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
}
void LLGLUserClipPlane::setPlane(F32 a, F32 b, F32 c, F32 d)
{
glh::matrix4f& P = mProjection;
glh::matrix4f& M = mModelview;
glh::matrix4f invtrans_MVP = (P * M).inverse().transpose();
glh::vec4f oplane(a,b,c,d);
glh::vec4f cplane;
invtrans_MVP.mult_matrix_vec(oplane, cplane);
cplane /= fabs(cplane[2]); // normalize such that depth is not scaled
cplane[3] -= 1;
if(cplane[2] < 0)
cplane *= -1;
glh::matrix4f suffix;
suffix.set_row(2, cplane);
glh::matrix4f newP = suffix * P;
gGL.matrixMode(LLRender::MM_PROJECTION);
gGL.pushMatrix();
gGL.loadMatrix(newP.m);
gGLObliqueProjectionInverse = LLMatrix4(newP.inverse().transpose().m);
gGL.matrixMode(LLRender::MM_MODELVIEW);
}
LLGLUserClipPlane::~LLGLUserClipPlane()
{
if (mApply)
{
gGL.matrixMode(LLRender::MM_PROJECTION);
gGL.popMatrix();
gGL.matrixMode(LLRender::MM_MODELVIEW);
}
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
}
LLGLNamePool::LLGLNamePool()
{
}
LLGLNamePool::~LLGLNamePool()
{
}
void LLGLNamePool::upkeep()
{
std::sort(mNameList.begin(), mNameList.end(), CompareUsed());
}
void LLGLNamePool::cleanup()
{
for (name_list_t::iterator iter = mNameList.begin(); iter != mNameList.end(); ++iter)
{
releaseName(iter->name);
}
mNameList.clear();
}
GLuint LLGLNamePool::allocate()
{
for (name_list_t::iterator iter = mNameList.begin(); iter != mNameList.end(); ++iter)
{
if (!iter->used)
{
iter->used = TRUE;
return iter->name;
}
}
NameEntry entry;
entry.name = allocateName();
entry.used = TRUE;
mNameList.push_back(entry);
return entry.name;
#else
return allocateName();
#endif
}
void LLGLNamePool::release(GLuint name)
{
for (name_list_t::iterator iter = mNameList.begin(); iter != mNameList.end(); ++iter)
{
if (iter->name == name)
{
if (iter->used)
{
iter->used = FALSE;
return;
}
else
{
llerrs << "Attempted to release a pooled name that is not in use!" << llendl;
}
llerrs << "Attempted to release a non pooled name!" << llendl;
#else
releaseName(name);
#endif
}
//static
void LLGLNamePool::upkeepPools()
{
Richard Nelson
committed
for (tracker_t::instance_iter iter = beginInstances(); iter != endInstances(); ++iter)
LLGLNamePool & pool = *iter;
pool.upkeep();
}
}
//static
void LLGLNamePool::cleanupPools()
{
Richard Nelson
committed
for (tracker_t::instance_iter iter = beginInstances(); iter != endInstances(); ++iter)
LLGLNamePool & pool = *iter;
pool.cleanup();
}
}
LLGLDepthTest::LLGLDepthTest(GLboolean depth_enabled, GLboolean write_enabled, GLenum depth_func)
: mPrevDepthEnabled(sDepthEnabled), mPrevDepthFunc(sDepthFunc), mPrevWriteEnabled(sWriteEnabled)
{
stop_glerror();
checkState();
if (!depth_enabled)
{ // always disable depth writes if depth testing is disabled
// GL spec defines this as a requirement, but some implementations allow depth writes with testing disabled
// The proper way to write to depth buffer with testing disabled is to enable testing and use a depth_func of GL_ALWAYS
write_enabled = FALSE;
}
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
if (depth_enabled != sDepthEnabled)
{
gGL.flush();
if (depth_enabled) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
sDepthEnabled = depth_enabled;
}
if (depth_func != sDepthFunc)
{
gGL.flush();
glDepthFunc(depth_func);
sDepthFunc = depth_func;
}
if (write_enabled != sWriteEnabled)
{
gGL.flush();
glDepthMask(write_enabled);
sWriteEnabled = write_enabled;
}
}
LLGLDepthTest::~LLGLDepthTest()
{
checkState();
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
if (sDepthEnabled != mPrevDepthEnabled )
{
gGL.flush();
if (mPrevDepthEnabled) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
sDepthEnabled = mPrevDepthEnabled;
}
if (sDepthFunc != mPrevDepthFunc)
{
gGL.flush();
glDepthFunc(mPrevDepthFunc);
sDepthFunc = mPrevDepthFunc;
}
if (sWriteEnabled != mPrevWriteEnabled )
{
gGL.flush();
glDepthMask(mPrevWriteEnabled);
sWriteEnabled = mPrevWriteEnabled;
}
}
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
void LLGLDepthTest::checkState()
{
if (gDebugGL)
{
GLint func = 0;
GLboolean mask = FALSE;
glGetIntegerv(GL_DEPTH_FUNC, &func);
glGetBooleanv(GL_DEPTH_WRITEMASK, &mask);
if (glIsEnabled(GL_DEPTH_TEST) != sDepthEnabled ||
sWriteEnabled != mask ||
sDepthFunc != func)
{
if (gDebugSession)
{
gFailLog << "Unexpected depth testing state." << std::endl;
}
else
{
LL_GL_ERRS << "Unexpected depth testing state." << LL_ENDL;
}
}
}
}
David Parks
committed
LLGLSquashToFarClip::LLGLSquashToFarClip(glh::matrix4f P, U32 layer)
David Parks
committed
F32 depth = 0.99999f - 0.0001f * layer;
David Parks
committed
P.element(2, i) = P.element(3, i) * depth;
gGL.matrixMode(LLRender::MM_PROJECTION);
gGL.pushMatrix();
gGL.loadMatrix(P.m);
gGL.matrixMode(LLRender::MM_MODELVIEW);
Merov Linden
committed
LLGLSquashToFarClip::~LLGLSquashToFarClip()
gGL.matrixMode(LLRender::MM_PROJECTION);
gGL.popMatrix();
gGL.matrixMode(LLRender::MM_MODELVIEW);
David Parks
committed
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
LLGLSyncFence::LLGLSyncFence()
{
#ifdef GL_ARB_sync
mSync = 0;
#endif
}
LLGLSyncFence::~LLGLSyncFence()
{
#ifdef GL_ARB_sync
if (mSync)
{
glDeleteSync(mSync);
}
#endif
}
void LLGLSyncFence::placeFence()
{
#ifdef GL_ARB_sync
if (mSync)
{
glDeleteSync(mSync);
}
mSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
#endif
}
bool LLGLSyncFence::isCompleted()
{
bool ret = true;
#ifdef GL_ARB_sync
if (mSync)
{
GLenum status = glClientWaitSync(mSync, 0, 1);
if (status == GL_TIMEOUT_EXPIRED)
{
ret = false;
}
}
#endif
return ret;
}
void LLGLSyncFence::wait()
{
#ifdef GL_ARB_sync
if (mSync)
{
while (glClientWaitSync(mSync, 0, FENCE_WAIT_TIME_NANOSECONDS) == GL_TIMEOUT_EXPIRED)
{ //track the number of times we've waited here
static S32 waits = 0;
waits++;
}
}
#endif
}