123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /**
- * @file llgltexture.cpp
- * @brief OpenGL texture implementation
- *
- * $LicenseInfo:firstyear=2012&license=viewergpl$
- *
- * Copyright (c) 2012, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "llgltexture.h"
- #include "llimagegl.h"
- // Made this a global variable to cover differences between grids (SL or
- // OpenSim). HB
- S32 gMaxImageSizeDefault = 1024;
- LLGLTexture::LLGLTexture(bool usemipmaps)
- {
- init();
- mUseMipMaps = usemipmaps;
- }
- LLGLTexture::LLGLTexture(U32 width, U32 height, U8 components, bool usemipmaps)
- {
- init();
- mFullWidth = width;
- mFullHeight = height;
- mUseMipMaps = usemipmaps;
- mComponents = components;
- setTexelsPerImage();
- }
- LLGLTexture::LLGLTexture(const LLImageRaw* rawp, bool usemipmaps)
- {
- init();
- mUseMipMaps = usemipmaps;
- // Create an empty image of the specified size and width
- mImageGLp = new LLImageGL(rawp, usemipmaps);
- mImageGLp->setOwner(this);
- mFullWidth = mImageGLp->getWidth();
- mFullHeight = mImageGLp->getHeight();
- mComponents = mImageGLp->getComponents();
- setTexelsPerImage();
- }
- LLGLTexture::~LLGLTexture()
- {
- cleanup();
- mImageGLp = NULL;
- }
- void LLGLTexture::init()
- {
- mBoostLevel = BOOST_NONE;
- mFullWidth = 0;
- mFullHeight = 0;
- mTexelsPerImage = 0;
- mUseMipMaps = false;
- mComponents = 0;
- mTextureState = NO_DELETE;
- mDontDiscard = false;
- mNeedsGLTexture = false;
- mIsMegaTexture = false;
- }
- void LLGLTexture::cleanup()
- {
- if (mImageGLp)
- {
- mImageGLp->cleanup();
- }
- }
- // virtual
- void LLGLTexture::dump()
- {
- if (mImageGLp)
- {
- mImageGLp->dump();
- }
- }
- void LLGLTexture::setBoostLevel(U32 level)
- {
- // Do not downgrade UI textures, ever ! HB
- if (mBoostLevel == BOOST_UI)
- {
- return;
- }
- if (level == BOOST_UI)
- {
- mBoostLevel = level;
- // UI textures must always be kept in memory for the whole duration of
- // the viewer session. HB
- mTextureState = ALWAYS_KEEP;
- // Also, never allow to discard UI textures. HB
- mDontDiscard = true;
- return;
- }
- mBoostLevel = level;
- #if LL_IMPLICIT_SETNODELETE
- if (level != BOOST_NONE && level != BOOST_ALM && level != BOOST_SELECTED)
- {
- mTextureState = NO_DELETE;
- }
- #else
- // Make map textures no-delete, always.
- if (level == BOOST_MAP)
- {
- mTextureState = NO_DELETE;
- }
- #endif
- }
- void LLGLTexture::generateGLTexture()
- {
- if (mImageGLp.isNull())
- {
- mImageGLp = new LLImageGL(mFullWidth, mFullHeight, mComponents,
- mUseMipMaps);
- mImageGLp->setOwner(this);
- }
- }
- LLImageGL* LLGLTexture::getGLImage() const
- {
- llassert(mImageGLp.notNull());
- return mImageGLp;
- }
- bool LLGLTexture::createGLTexture()
- {
- if (mImageGLp.isNull())
- {
- generateGLTexture();
- }
- return mImageGLp->createGLTexture();
- }
- bool LLGLTexture::createGLTexture(S32 discard_level, const LLImageRaw* rawimg,
- S32 usename, bool to_create, bool defer_copy,
- U32* tex_name)
- {
- if (mImageGLp.isNull())
- {
- llwarns << "NULL GL image for GL texture 0x" << std::hex
- << (intptr_t)this << std::dec << llendl;
- llassert(false);
- return false;
- }
- bool ret = mImageGLp->createGLTexture(discard_level, rawimg, usename,
- to_create, defer_copy, tex_name);
- if (ret)
- {
- mFullWidth = mImageGLp->getCurrentWidth();
- mFullHeight = mImageGLp->getCurrentHeight();
- mComponents = mImageGLp->getComponents();
- setTexelsPerImage();
- }
- return ret;
- }
- void LLGLTexture::setExplicitFormat(S32 internal_format, U32 primary_format,
- U32 type_format, bool swap_bytes)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setExplicitFormat(internal_format, primary_format, type_format,
- swap_bytes);
- }
- void LLGLTexture::setAddressMode(LLTexUnit::eTextureAddressMode mode)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setAddressMode(mode);
- }
- void LLGLTexture::setFilteringOption(LLTexUnit::eTextureFilterOptions option)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setFilteringOption(option);
- }
- //virtual
- S32 LLGLTexture::getWidth(S32 discard_level) const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getWidth(discard_level);
- }
- //virtual
- S32 LLGLTexture::getHeight(S32 discard_level) const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getHeight(discard_level);
- }
- S32 LLGLTexture::getMaxDiscardLevel() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getMaxDiscardLevel();
- }
- S32 LLGLTexture::getDiscardLevel() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getDiscardLevel();
- }
- S8 LLGLTexture::getComponents() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getComponents();
- }
- U32 LLGLTexture::getTexName() const
- {
- return mImageGLp.notNull() ? mImageGLp->getTexName() : 0;
- }
- bool LLGLTexture::hasGLTexture() const
- {
- return mImageGLp.notNull() && mImageGLp->getHasGLTexture();
- }
- bool LLGLTexture::getBoundRecently() const
- {
- return mImageGLp.notNull() && mImageGLp->getBoundRecently();
- }
- LLTexUnit::eTextureType LLGLTexture::getTarget() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getTarget();
- }
- bool LLGLTexture::setSubImage(const LLImageRaw* rawimg, S32 x_pos, S32 y_pos,
- S32 width, S32 height, U32 use_name)
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->setSubImage(rawimg, x_pos, y_pos, width, height, 0,
- use_name);
- }
- bool LLGLTexture::setSubImage(const U8* datap, S32 data_width, S32 data_height,
- S32 x_pos, S32 y_pos, S32 width, S32 height,
- U32 use_name)
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->setSubImage(datap, data_width, data_height, x_pos, y_pos,
- width, height, 0, use_name);
- }
- void LLGLTexture::setGLTextureCreated (bool initialized)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setGLTextureCreated (initialized);
- }
- #if 0 // Not used
- void LLGLTexture::setTexName(U32 name)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setTexName(name);
- }
- void LLGLTexture::setTarget(U32 target, LLTexUnit::eTextureType bind_target)
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->setTarget(target, bind_target);
- }
- #endif
- LLTexUnit::eTextureAddressMode LLGLTexture::getAddressMode() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getAddressMode();
- }
- S32 LLGLTexture::getTextureMemory() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->mTextureMemory;
- }
- U32 LLGLTexture::getPrimaryFormat() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getPrimaryFormat();
- }
- bool LLGLTexture::getIsAlphaMask() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getIsAlphaMask();
- }
- bool LLGLTexture::getMask(const LLVector2 &tc)
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getMask(tc);
- }
- F32 LLGLTexture::getTimePassedSinceLastBound()
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->getTimePassedSinceLastBound();
- }
- bool LLGLTexture::isJustBound() const
- {
- llassert_always(mImageGLp.notNull());
- return mImageGLp->isJustBound();
- }
- void LLGLTexture::forceUpdateBindStats() const
- {
- llassert_always(mImageGLp.notNull());
- mImageGLp->forceUpdateBindStats();
- }
- void LLGLTexture::destroyGLTexture()
- {
- if (mImageGLp.notNull() && mImageGLp->getHasGLTexture())
- {
- mImageGLp->destroyGLTexture();
- }
- mTextureState = DELETED;
- }
- void LLGLTexture::setTexelsPerImage()
- {
- S32 fullwidth = llmin(mFullWidth, (S32)gMaxImageSizeDefault);
- S32 fullheight = llmin(mFullHeight, (S32)gMaxImageSizeDefault);
- mTexelsPerImage = fullwidth * fullheight;
- }
- // Returns the minimum discard level to apply to this image, depending on its
- // dimensions and the gMaxImageSizeDefault limit. HB
- S32 LLGLTexture::getMinDiscardLevel() const
- {
- S32 max_size = llmax(mFullWidth, mFullHeight);
- if (max_size <= gMaxImageSizeDefault)
- {
- return 0;
- }
- if (max_size > 2 * gMaxImageSizeDefault)
- {
- // E.g. 4K image and 1K max size. HB
- return 2;
- }
- // E.g. 2K image and 1K max size. HB
- return 1;
- }
|