Newer
Older
/**
* @file llrender.cpp
* @brief LLRender implementation
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llrender.h"
#include "llvertexbuffer.h"
#include "llcubemap.h"
#include "llglslshader.h"
#include "llimagegl.h"
#include "llrendertarget.h"
LLRender gGL;
// Handy copies of last good GL matrices
F64 gGLModelView[16];
F64 gGLLastModelView[16];
F64 gGLProjection[16];
S32 gGLViewport[4];
Palmer Truelson
committed
U32 LLRender::sUICalls = 0;
U32 LLRender::sUIVerts = 0;
David Parks
committed
static const U32 LL_NUM_TEXTURE_LAYERS = 32;
David Parks
committed
static const U32 LL_NUM_LIGHT_UNITS = 8;
static GLenum sGLTextureType[] =
{
GL_TEXTURE_2D,
GL_TEXTURE_RECTANGLE_ARB,
David Parks
committed
GL_TEXTURE_CUBE_MAP_ARB,
GL_TEXTURE_2D_MULTISAMPLE
};
static GLint sGLAddressMode[] =
{
GL_REPEAT,
GL_MIRRORED_REPEAT,
GL_CLAMP_TO_EDGE
};
static GLenum sGLCompareFunc[] =
{
GL_NEVER,
GL_ALWAYS,
GL_LESS,
GL_LEQUAL,
GL_EQUAL,
GL_NOTEQUAL,
GL_GEQUAL,
GL_GREATER
};
const U32 immediate_mask = LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_COLOR | LLVertexBuffer::MAP_TEXCOORD0;
static GLenum sGLBlendFactor[] =
{
GL_ONE,
GL_ZERO,
GL_DST_COLOR,
GL_SRC_COLOR,
GL_ONE_MINUS_DST_COLOR,
GL_ONE_MINUS_SRC_COLOR,
GL_DST_ALPHA,
GL_SRC_ALPHA,
GL_ONE_MINUS_DST_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_ZERO // 'BF_UNDEF'
LLTexUnit::LLTexUnit(S32 index)
: mCurrTexType(TT_NONE), mCurrBlendType(TB_MULT),
mCurrColorOp(TBO_MULT), mCurrAlphaOp(TBO_MULT),
mCurrColorSrc1(TBS_TEX_COLOR), mCurrColorSrc2(TBS_PREV_COLOR),
mCurrAlphaSrc1(TBS_TEX_ALPHA), mCurrAlphaSrc2(TBS_PREV_ALPHA),
mCurrColorScale(1), mCurrAlphaScale(1), mCurrTexture(0),
mHasMipMaps(false)
llassert_always(index < (S32)LL_NUM_TEXTURE_LAYERS);
mIndex = index;
}
//static
U32 LLTexUnit::getInternalType(eTextureType type)
return sGLTextureType[type];
void LLTexUnit::refreshState(void)
// We set dirty to true so that the tex unit knows to ignore caching
// and we reset the cached tex unit state
gGL.flush();
glActiveTextureARB(GL_TEXTURE0_ARB + mIndex);
//
// Per apple spec, don't call glEnable/glDisable when index exceeds max texture units
// http://www.mailinglistarchive.com/html/mac-opengl@lists.apple.com/2008-07/msg00653.html
//
bool enableDisable = (mIndex < gGLManager.mNumTextureUnits) && mCurrTexType != LLTexUnit::TT_MULTISAMPLE_TEXTURE;
if (mCurrTexType != TT_NONE)
if (enableDisable)
{
glEnable(sGLTextureType[mCurrTexType]);
}
glBindTexture(sGLTextureType[mCurrTexType], mCurrTexture);
if (enableDisable)
{
glDisable(GL_TEXTURE_2D);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
if (mCurrBlendType != TB_COMBINE)
{
setTextureBlendType(mCurrBlendType);
}
else
{
setTextureCombiner(mCurrColorOp, mCurrColorSrc1, mCurrColorSrc2, false);
setTextureCombiner(mCurrAlphaOp, mCurrAlphaSrc1, mCurrAlphaSrc2, true);
}
}
void LLTexUnit::activate(void)
{
if (mIndex < 0) return;
if ((S32)gGL.mCurrTextureUnitIndex != mIndex || gGL.mDirty)
David Parks
committed
gGL.flush();
glActiveTextureARB(GL_TEXTURE0_ARB + mIndex);
gGL.mCurrTextureUnitIndex = mIndex;
}
}
void LLTexUnit::enable(eTextureType type)
if (mIndex < 0) return;
if ( (mCurrTexType != type || gGL.mDirty) && (type != TT_NONE) )
{
activate();
if (mCurrTexType != TT_NONE && !gGL.mDirty)
{
disable(); // Force a disable of a previous texture type if it's enabled.
}
mCurrTexType = type;
gGL.flush();
if (type != LLTexUnit::TT_MULTISAMPLE_TEXTURE &&
{
glEnable(sGLTextureType[type]);
}
}
}
void LLTexUnit::disable(void)
{
if (mIndex < 0) return;
if (mCurrTexType != TT_NONE)
activate();
unbind(mCurrTexType);
gGL.flush();
if (mCurrTexType != LLTexUnit::TT_MULTISAMPLE_TEXTURE &&
{
glDisable(sGLTextureType[mCurrTexType]);
}
mCurrTexType = TT_NONE;
bool LLTexUnit::bind(LLTexture* texture, bool for_rendering, bool forceBind)
stop_glerror();
if (mIndex < 0) return false;
gGL.flush();
LLImageGL* gl_tex = NULL ;
if (texture == NULL || !(gl_tex = texture->getGLTexture()))
llwarns << "NULL LLTexUnit::bind texture" << llendl;
return false;
if (!gl_tex->getTexName()) //if texture does not exist
//if deleted, will re-generate it immediately
texture->forceImmediateUpdate() ;
gl_tex->forceUpdateBindStats() ;
return texture->bindDefaultImage(mIndex);
//in audit, replace the selected texture by the default one.
if(gAuditTexture && for_rendering && LLImageGL::sCurTexPickSize > 0)
{
if(texture->getWidth() * texture->getHeight() == LLImageGL::sCurTexPickSize)
{
gl_tex->updateBindStats(gl_tex->mTextureMemory);
return bind(LLImageGL::sHighlightTexturep.get());
}
}
if ((mCurrTexture != gl_tex->getTexName()) || forceBind)
{
activate();
enable(gl_tex->getTarget());
mCurrTexture = gl_tex->getTexName();
glBindTexture(sGLTextureType[gl_tex->getTarget()], mCurrTexture);
if(gl_tex->updateBindStats(gl_tex->mTextureMemory))
{
texture->setActive() ;
texture->updateBindStatsForTester() ;
}
mHasMipMaps = gl_tex->mHasMipMaps;
if (gl_tex->mTexOptionsDirty)
{
gl_tex->mTexOptionsDirty = false;
setTextureAddressMode(gl_tex->mAddressMode);
setTextureFilteringOption(gl_tex->mFilterOption);
}
}
return true;
}
bool LLTexUnit::bind(LLImageGL* texture, bool for_rendering, bool forceBind)
{
stop_glerror();
if (mIndex < 0) return false;
if(!texture)
{
llwarns << "NULL LLTexUnit::bind texture" << llendl;
return false;
}
if(!texture->getTexName())
{
if(LLImageGL::sDefaultGLTexture && LLImageGL::sDefaultGLTexture->getTexName())
{
return bind(LLImageGL::sDefaultGLTexture) ;
}
return false ;
}
if ((mCurrTexture != texture->getTexName()) || forceBind)
Palmer Truelson
committed
gGL.flush();
activate();
enable(texture->getTarget());
mCurrTexture = texture->getTexName();
glBindTexture(sGLTextureType[texture->getTarget()], mCurrTexture);
texture->updateBindStats(texture->mTextureMemory);
mHasMipMaps = texture->mHasMipMaps;
if (texture->mTexOptionsDirty)
{
texture->mTexOptionsDirty = false;
setTextureAddressMode(texture->mAddressMode);
setTextureFilteringOption(texture->mFilterOption);
}
}
bool LLTexUnit::bind(LLCubeMap* cubeMap)
if (mIndex < 0) return false;
gGL.flush();
if (cubeMap == NULL)
{
llwarns << "NULL LLTexUnit::bind cubemap" << llendl;
return false;
}
if (mCurrTexture != cubeMap->mImages[0]->getTexName())
{
if (gGLManager.mHasCubeMap && LLCubeMap::sUseCubeMaps)
{
activate();
enable(LLTexUnit::TT_CUBE_MAP);
mCurrTexture = cubeMap->mImages[0]->getTexName();
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, mCurrTexture);
mHasMipMaps = cubeMap->mImages[0]->mHasMipMaps;
cubeMap->mImages[0]->updateBindStats(cubeMap->mImages[0]->mTextureMemory);
if (cubeMap->mImages[0]->mTexOptionsDirty)
{
cubeMap->mImages[0]->mTexOptionsDirty = false;
setTextureAddressMode(cubeMap->mImages[0]->mAddressMode);
setTextureFilteringOption(cubeMap->mImages[0]->mFilterOption);
}
return true;
}
else
{
llwarns << "Using cube map without extension!" << llendl;
return false;
// LLRenderTarget is unavailible on the mapserver since it uses FBOs.
#if !LL_MESA_HEADLESS
bool LLTexUnit::bind(LLRenderTarget* renderTarget, bool bindDepth)
{
if (mIndex < 0) return false;
gGL.flush();
if (bindDepth)
{
if (renderTarget->hasStencil())
{
llerrs << "Cannot bind a render buffer for sampling. Allocate render target without a stencil buffer if sampling of depth buffer is required." << llendl;
}
bindManual(renderTarget->getUsage(), renderTarget->getDepth());
}
else
{
bindManual(renderTarget->getUsage(), renderTarget->getTexture());
}
return true;
}
#endif // LL_MESA_HEADLESS
bool LLTexUnit::bindManual(eTextureType type, U32 texture, bool hasMips)
if (mIndex < 0)
{
return false;
}
if(mCurrTexture != texture)
{
gGL.flush();
activate();
enable(type);
mCurrTexture = texture;
glBindTexture(sGLTextureType[type], texture);
mHasMipMaps = hasMips;
}
return true;
}
void LLTexUnit::unbind(eTextureType type)
{
stop_glerror();
if (mIndex < 0) return;
// Disabled caching of binding state.
if (mCurrTexType == type)
{
gGL.flush();
activate();
mCurrTexture = 0;
glBindTexture(sGLTextureType[type], 0);
David Parks
committed
stop_glerror();
}
}
void LLTexUnit::setTextureAddressMode(eTextureAddressMode mode)
{
if (mIndex < 0 || mCurrTexture == 0) return;
gGL.flush();
activate();
glTexParameteri (sGLTextureType[mCurrTexType], GL_TEXTURE_WRAP_S, sGLAddressMode[mode]);
glTexParameteri (sGLTextureType[mCurrTexType], GL_TEXTURE_WRAP_T, sGLAddressMode[mode]);
if (mCurrTexType == TT_CUBE_MAP)
glTexParameteri (GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, sGLAddressMode[mode]);
}
}
void LLTexUnit::setTextureFilteringOption(LLTexUnit::eTextureFilterOptions option)
{
David Parks
committed
if (mIndex < 0 || mCurrTexture == 0 || mCurrTexType == LLTexUnit::TT_MULTISAMPLE_TEXTURE) return;
gGL.flush();
if (option == TFO_POINT)
{
glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else
{
glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
if (option >= TFO_TRILINEAR && mHasMipMaps)
{
glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else if (option >= TFO_BILINEAR)
{
glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else
{
glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
if (gGLManager.mHasAnisotropic)
{
if (LLImageGL::sGlobalUseAnisotropic && option == TFO_ANISOTROPIC)
{
if (gGL.mMaxAnisotropy < 1.f)
{
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gGL.mMaxAnisotropy);
Xiaohong Bao
committed
llinfos << "gGL.mMaxAnisotropy: " << gGL.mMaxAnisotropy << llendl ;
gGL.mMaxAnisotropy = llmax(1.f, gGL.mMaxAnisotropy) ;
}
glTexParameterf(sGLTextureType[mCurrTexType], GL_TEXTURE_MAX_ANISOTROPY_EXT, gGL.mMaxAnisotropy);
}
else
glTexParameterf(sGLTextureType[mCurrTexType], GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.f);
}
void LLTexUnit::setTextureBlendType(eTextureBlendType type)
{
if (mIndex < 0) return;
// Do nothing if it's already correctly set.
if (mCurrBlendType == type && !gGL.mDirty)
{
return;
}
Palmer Truelson
committed
gGL.flush();
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
activate();
mCurrBlendType = type;
S32 scale_amount = 1;
switch (type)
{
case TB_REPLACE:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
break;
case TB_ADD:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
break;
case TB_MULT:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
break;
case TB_MULT_X2:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
scale_amount = 2;
break;
case TB_ALPHA_BLEND:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
break;
case TB_COMBINE:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
break;
default:
llerrs << "Unknown Texture Blend Type: " << type << llendl;
break;
}
setColorScale(scale_amount);
setAlphaScale(1);
}
GLint LLTexUnit::getTextureSource(eTextureBlendSrc src)
{
switch(src)
{
// All four cases should return the same value.
case TBS_PREV_COLOR:
case TBS_PREV_ALPHA:
case TBS_ONE_MINUS_PREV_COLOR:
case TBS_ONE_MINUS_PREV_ALPHA:
return GL_PREVIOUS_ARB;
// All four cases should return the same value.
case TBS_TEX_COLOR:
case TBS_TEX_ALPHA:
case TBS_ONE_MINUS_TEX_COLOR:
case TBS_ONE_MINUS_TEX_ALPHA:
return GL_TEXTURE;
// All four cases should return the same value.
case TBS_VERT_COLOR:
case TBS_VERT_ALPHA:
case TBS_ONE_MINUS_VERT_COLOR:
case TBS_ONE_MINUS_VERT_ALPHA:
return GL_PRIMARY_COLOR_ARB;
// All four cases should return the same value.
case TBS_CONST_COLOR:
case TBS_CONST_ALPHA:
case TBS_ONE_MINUS_CONST_COLOR:
case TBS_ONE_MINUS_CONST_ALPHA:
return GL_CONSTANT_ARB;
default:
llwarns << "Unknown eTextureBlendSrc: " << src << ". Using Vertex Color instead." << llendl;
return GL_PRIMARY_COLOR_ARB;
}
}
GLint LLTexUnit::getTextureSourceType(eTextureBlendSrc src, bool isAlpha)
{
switch(src)
{
// All four cases should return the same value.
case TBS_PREV_COLOR:
case TBS_TEX_COLOR:
case TBS_VERT_COLOR:
case TBS_CONST_COLOR:
return (isAlpha) ? GL_SRC_ALPHA: GL_SRC_COLOR;
// All four cases should return the same value.
case TBS_PREV_ALPHA:
case TBS_TEX_ALPHA:
case TBS_VERT_ALPHA:
case TBS_CONST_ALPHA:
return GL_SRC_ALPHA;
// All four cases should return the same value.
case TBS_ONE_MINUS_PREV_COLOR:
case TBS_ONE_MINUS_TEX_COLOR:
case TBS_ONE_MINUS_VERT_COLOR:
case TBS_ONE_MINUS_CONST_COLOR:
return (isAlpha) ? GL_ONE_MINUS_SRC_ALPHA : GL_ONE_MINUS_SRC_COLOR;
// All four cases should return the same value.
case TBS_ONE_MINUS_PREV_ALPHA:
case TBS_ONE_MINUS_TEX_ALPHA:
case TBS_ONE_MINUS_VERT_ALPHA:
case TBS_ONE_MINUS_CONST_ALPHA:
return GL_ONE_MINUS_SRC_ALPHA;
default:
llwarns << "Unknown eTextureBlendSrc: " << src << ". Using Source Color or Alpha instead." << llendl;
return (isAlpha) ? GL_SRC_ALPHA: GL_SRC_COLOR;
}
}
void LLTexUnit::setTextureCombiner(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2, bool isAlpha)
{
if (mIndex < 0) return;
if (mCurrBlendType != TB_COMBINE || gGL.mDirty)
{
mCurrBlendType = TB_COMBINE;
David Parks
committed
gGL.flush();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
}
// We want an early out, because this function does a LOT of stuff.
if ( ( (isAlpha && (mCurrAlphaOp == op) && (mCurrAlphaSrc1 == src1) && (mCurrAlphaSrc2 == src2))
|| (!isAlpha && (mCurrColorOp == op) && (mCurrColorSrc1 == src1) && (mCurrColorSrc2 == src2)) ) && !gGL.mDirty)
{
return;
}
David Parks
committed
gGL.flush();
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Get the gl source enums according to the eTextureBlendSrc sources passed in
GLint source1 = getTextureSource(src1);
GLint source2 = getTextureSource(src2);
// Get the gl operand enums according to the eTextureBlendSrc sources passed in
GLint operand1 = getTextureSourceType(src1, isAlpha);
GLint operand2 = getTextureSourceType(src2, isAlpha);
// Default the scale amount to 1
S32 scale_amount = 1;
GLenum comb_enum, src0_enum, src1_enum, src2_enum, operand0_enum, operand1_enum, operand2_enum;
if (isAlpha)
{
// Set enums to ALPHA ones
comb_enum = GL_COMBINE_ALPHA_ARB;
src0_enum = GL_SOURCE0_ALPHA_ARB;
src1_enum = GL_SOURCE1_ALPHA_ARB;
src2_enum = GL_SOURCE2_ALPHA_ARB;
operand0_enum = GL_OPERAND0_ALPHA_ARB;
operand1_enum = GL_OPERAND1_ALPHA_ARB;
operand2_enum = GL_OPERAND2_ALPHA_ARB;
// cache current combiner
mCurrAlphaOp = op;
mCurrAlphaSrc1 = src1;
mCurrAlphaSrc2 = src2;
}
else
{
// Set enums to RGB ones
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
comb_enum = GL_COMBINE_RGB_ARB;
src0_enum = GL_SOURCE0_RGB_ARB;
src1_enum = GL_SOURCE1_RGB_ARB;
src2_enum = GL_SOURCE2_RGB_ARB;
operand0_enum = GL_OPERAND0_RGB_ARB;
operand1_enum = GL_OPERAND1_RGB_ARB;
operand2_enum = GL_OPERAND2_RGB_ARB;
// cache current combiner
mCurrColorOp = op;
mCurrColorSrc1 = src1;
mCurrColorSrc2 = src2;
}
switch(op)
{
case TBO_REPLACE:
// Slightly special syntax (no second sources), just set all and return.
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, src0_enum, source1);
glTexEnvi(GL_TEXTURE_ENV, operand0_enum, operand1);
(isAlpha) ? setAlphaScale(1) : setColorScale(1);
return;
case TBO_MULT:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_MODULATE);
break;
case TBO_MULT_X2:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_MODULATE);
scale_amount = 2;
break;
case TBO_MULT_X4:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_MODULATE);
scale_amount = 4;
break;
case TBO_ADD:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_ADD);
break;
case TBO_ADD_SIGNED:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_ADD_SIGNED_ARB);
break;
case TBO_SUBTRACT:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_SUBTRACT_ARB);
break;
case TBO_LERP_VERT_ALPHA:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, src2_enum, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, operand2_enum, GL_SRC_ALPHA);
break;
case TBO_LERP_TEX_ALPHA:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, src2_enum, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, operand2_enum, GL_SRC_ALPHA);
break;
case TBO_LERP_PREV_ALPHA:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, src2_enum, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV, operand2_enum, GL_SRC_ALPHA);
break;
case TBO_LERP_CONST_ALPHA:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, src2_enum, GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV, operand2_enum, GL_SRC_ALPHA);
break;
case TBO_LERP_VERT_COLOR:
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, src2_enum, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, operand2_enum, (isAlpha) ? GL_SRC_ALPHA : GL_SRC_COLOR);
break;
default:
llwarns << "Unknown eTextureBlendOp: " << op << ". Setting op to replace." << llendl;
// Slightly special syntax (no second sources), just set all and return.
glTexEnvi(GL_TEXTURE_ENV, comb_enum, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, src0_enum, source1);
glTexEnvi(GL_TEXTURE_ENV, operand0_enum, operand1);
(isAlpha) ? setAlphaScale(1) : setColorScale(1);
return;
}
// Set sources, operands, and scale accordingly
glTexEnvi(GL_TEXTURE_ENV, src0_enum, source1);
glTexEnvi(GL_TEXTURE_ENV, operand0_enum, operand1);
glTexEnvi(GL_TEXTURE_ENV, src1_enum, source2);
glTexEnvi(GL_TEXTURE_ENV, operand1_enum, operand2);
(isAlpha) ? setAlphaScale(scale_amount) : setColorScale(scale_amount);
}
void LLTexUnit::setColorScale(S32 scale)
{
if (mCurrColorScale != scale || gGL.mDirty)
{
mCurrColorScale = scale;
gGL.flush();
glTexEnvi( GL_TEXTURE_ENV, GL_RGB_SCALE, scale );
}
}
void LLTexUnit::setAlphaScale(S32 scale)
{
if (mCurrAlphaScale != scale || gGL.mDirty)
{
mCurrAlphaScale = scale;
gGL.flush();
glTexEnvi( GL_TEXTURE_ENV, GL_ALPHA_SCALE, scale );
}
}
// Useful for debugging that you've manually assigned a texture operation to the correct
// texture unit based on the currently set active texture in opengl.
void LLTexUnit::debugTextureUnit(void)
{
if (mIndex < 0) return;
GLint activeTexture;
glGetIntegerv(GL_ACTIVE_TEXTURE_ARB, &activeTexture);
if ((GL_TEXTURE0_ARB + mIndex) != activeTexture)
{
U32 set_unit = (activeTexture - GL_TEXTURE0_ARB);
llwarns << "Incorrect Texture Unit! Expected: " << set_unit << " Actual: " << mIndex << llendl;
}
}
David Parks
committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
LLLightState::LLLightState(S32 index)
: mIndex(index),
mEnabled(false),
mConstantAtten(1.f),
mLinearAtten(0.f),
mQuadraticAtten(0.f),
mSpotExponent(0.f),
mSpotCutoff(180.f)
{
if (mIndex == 0)
{
mDiffuse.set(1,1,1,1);
mSpecular.set(1,1,1,1);
}
mAmbient.set(0,0,0,1);
mPosition.set(0,0,1,0);
mSpotDirection.set(0,0,-1);
}
void LLLightState::enable()
{
if (!mEnabled)
{
glEnable(GL_LIGHT0+mIndex);
mEnabled = true;
}
}
void LLLightState::disable()
{
if (mEnabled)
{
glDisable(GL_LIGHT0+mIndex);
mEnabled = false;
}
}
void LLLightState::setDiffuse(const LLColor4& diffuse)
{
if (mDiffuse != diffuse)
{
mDiffuse = diffuse;
glLightfv(GL_LIGHT0+mIndex, GL_DIFFUSE, mDiffuse.mV);
}
}
void LLLightState::setAmbient(const LLColor4& ambient)
{
if (mAmbient != ambient)
{
mAmbient = ambient;
glLightfv(GL_LIGHT0+mIndex, GL_AMBIENT, mAmbient.mV);
}
}
void LLLightState::setSpecular(const LLColor4& specular)
{
if (mSpecular != specular)
{
mSpecular = specular;
glLightfv(GL_LIGHT0+mIndex, GL_SPECULAR, mSpecular.mV);
}
}
void LLLightState::setPosition(const LLVector4& position)
{
//always set position because modelview matrix may have changed
mPosition = position;
glLightfv(GL_LIGHT0+mIndex, GL_POSITION, mPosition.mV);
}
void LLLightState::setConstantAttenuation(const F32& atten)
{
if (mConstantAtten != atten)
{
mConstantAtten = atten;
glLightf(GL_LIGHT0+mIndex, GL_CONSTANT_ATTENUATION, atten);
}
}
void LLLightState::setLinearAttenuation(const F32& atten)
{
if (mLinearAtten != atten)
{
mLinearAtten = atten;
glLightf(GL_LIGHT0+mIndex, GL_LINEAR_ATTENUATION, atten);
}
}
void LLLightState::setQuadraticAttenuation(const F32& atten)
{
if (mQuadraticAtten != atten)
{
mQuadraticAtten = atten;
glLightf(GL_LIGHT0+mIndex, GL_QUADRATIC_ATTENUATION, atten);
}
}
void LLLightState::setSpotExponent(const F32& exponent)
{
if (mSpotExponent != exponent)
{
mSpotExponent = exponent;
glLightf(GL_LIGHT0+mIndex, GL_SPOT_EXPONENT, exponent);
}
}
void LLLightState::setSpotCutoff(const F32& cutoff)
{
if (mSpotCutoff != cutoff)
{
mSpotCutoff = cutoff;
glLightf(GL_LIGHT0+mIndex, GL_SPOT_CUTOFF, cutoff);
}
}
void LLLightState::setSpotDirection(const LLVector3& direction)
{
//always set direction because modelview matrix may have changed
mSpotDirection = direction;
glLightfv(GL_LIGHT0+mIndex, GL_SPOT_DIRECTION, direction.mV);
}
LLRender::LLRender()
: mDirty(false),
mCount(0),
mMode(LLRender::TRIANGLES),
mCurrTextureUnitIndex(0),
mMaxAnisotropy(0.f)
{
mBuffer = new LLVertexBuffer(immediate_mask, 0);
mBuffer->allocateBuffer(4096, 0, TRUE);
mBuffer->getVertexStrider(mVerticesp);
mBuffer->getTexCoord0Strider(mTexcoordsp);
mBuffer->getColorStrider(mColorsp);
mTexUnits.reserve(LL_NUM_TEXTURE_LAYERS);
for (U32 i = 0; i < LL_NUM_TEXTURE_LAYERS; i++)
{
mTexUnits.push_back(new LLTexUnit(i));
}
mDummyTexUnit = new LLTexUnit(-1);
David Parks
committed
for (U32 i = 0; i < LL_NUM_LIGHT_UNITS; ++i)
{
mLightState.push_back(new LLLightState(i));
}
for (U32 i = 0; i < 4; i++)
{
mCurrColorMask[i] = true;
}
mCurrAlphaFunc = CF_DEFAULT;
mCurrAlphaFuncVal = 0.01f;
mCurrBlendColorSFactor = BF_UNDEF;
mCurrBlendAlphaSFactor = BF_UNDEF;
mCurrBlendColorDFactor = BF_UNDEF;
mCurrBlendAlphaDFactor = BF_UNDEF;
}
LLRender::~LLRender()
{
shutdown();
}
void LLRender::shutdown()
{
for (U32 i = 0; i < mTexUnits.size(); i++)
{
delete mTexUnits[i];
}
delete mDummyTexUnit;
mDummyTexUnit = NULL;
David Parks
committed
for (U32 i = 0; i < mLightState.size(); ++i)
{
delete mLightState[i];
}
mLightState.clear();
}
void LLRender::refreshState(void)
{
mDirty = true;
U32 active_unit = mCurrTextureUnitIndex;
for (U32 i = 0; i < mTexUnits.size(); i++)
{
mTexUnits[i]->refreshState();
}
mTexUnits[active_unit]->activate();
setColorMask(mCurrColorMask[0], mCurrColorMask[1], mCurrColorMask[2], mCurrColorMask[3]);
setAlphaRejectSettings(mCurrAlphaFunc, mCurrAlphaFuncVal);
mDirty = false;
}
void LLRender::translatef(const GLfloat& x, const GLfloat& y, const GLfloat& z)
{
flush();
glTranslatef(x,y,z);
}
void LLRender::scalef(const GLfloat& x, const GLfloat& y, const GLfloat& z)
{
flush();
glScalef(x,y,z);
}
void LLRender::pushMatrix()
{
flush();
glPushMatrix();
}