CreateGlTexture();
}
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
mContext.Bind2dTexture(mId);
mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
GLenum pixelDataType = GL_UNSIGNED_BYTE;
Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
mContext.Bind2dTexture(mId);
}
DALI_ASSERT_DEBUG( mId != 0 );
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
mContext.Bind2dTexture(mId);
Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
GLenum pixelDataType = GL_UNSIGNED_BYTE;
Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
mContext.Bind2dTexture(mId);
size_t numPixels = blockSize*blockSize;
}
DALI_ASSERT_DEBUG( mId != 0 );
- mContext.ActiveTexture(TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD )); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );
mContext.Bind2dTexture(mId);
GLenum pixelFormat = GL_RGBA;
// INTERNAL INCLUDES
#include <dali/public-api/common/constants.h>
+#include <dali/public-api/common/compile-time-assert.h>
#include <dali/internal/render/shaders/program.h>
#include <dali/integration-api/platform-abstraction.h>
#include <dali/internal/render/common/render-manager.h>
namespace // unnamed namespace
{
+DALI_COMPILE_TIME_ASSERT( TEXTURE_UNIT_LAST <= Context::MAX_TEXTURE_UNITS );
+
/**
* GL error strings
*/
delete hashProgram.second;
}
-const unsigned int UNINITIALIZED_TEXTURE_UNIT = std::numeric_limits<unsigned int>::max();// GL_MAX_TEXTURE_UNITS can't be used because it's depreciated in gles2
-
} // unnamed namespace
#ifdef DEBUG_ENABLED
mBoundArrayBufferId(0),
mBoundElementArrayBufferId(0),
mBoundTransformFeedbackBufferId(0),
- mActiveTextureUnit( UNINITIALIZED_TEXTURE_UNIT ),
+ mActiveTextureUnit( TEXTURE_UNIT_LAST ),
mUsingDefaultBlendColor(true),
mBlendFuncSeparateSrcRGB(GL_ONE),
mBlendFuncSeparateDstRGB(GL_ZERO),
mGlAbstraction.BindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, mBoundTransformFeedbackBufferId);
#endif
- mActiveTextureUnit = UNINITIALIZED_TEXTURE_UNIT;
+ mActiveTextureUnit = TEXTURE_UNIT_LAST;
mUsingDefaultBlendColor = true;
mGlAbstraction.BlendColor( 0.0f, 0.0f, 0.0f, 0.0f );
mGlAbstraction.FrontFace(GL_CCW);
mGlAbstraction.CullFace(GL_BACK);
- // rebind texture units
+ // rebind texture units to 0
for( unsigned int i=0; i < MAX_TEXTURE_UNITS; ++i )
{
mBound2dTextureId[ i ] = 0;
// set active texture
mGlAbstraction.ActiveTexture( GL_TEXTURE0 + i );
- // bind the previous texture
mGlAbstraction.BindTexture(GL_TEXTURE_2D, mBound2dTextureId[ i ] );
}
// INTERNAL INCLUDES
#include <dali/integration-api/gl-defines.h>
+#include <dali/public-api/actors/renderable-actor.h>
#include <dali/public-api/common/map-wrapper.h>
#include <dali/public-api/common/dali-vector.h>
-#include <dali/public-api/actors/renderable-actor.h>
-#include <dali/integration-api/debug.h>
-#include <dali/integration-api/gl-abstraction.h>
-#include <dali/internal/render/common/performance-monitor.h>
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/math/rect.h>
#include <dali/public-api/math/vector4.h>
+#include <dali/integration-api/debug.h>
+#include <dali/integration-api/gl-abstraction.h>
+#include <dali/internal/render/common/performance-monitor.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
namespace Dali
{
*/
static const unsigned int MAX_ATTRIBUTE_CACHE_SIZE = 8;
- static const unsigned int MAX_TEXTURE_UNITS = 8; // for GLES 2.0 its 8, which is more than DALi uses anyways
+ static const unsigned int MAX_TEXTURE_UNITS = 8; // for GLES 2.0 8 is guaranteed, which is more than DALi uses anyways
/**
* Creates the Dali Context object.
/**
* Wrapper for OpenGL ES 2.0 glActiveTexture()
*/
- void ActiveTexture(GLenum textureUnit)
+ void ActiveTexture( TextureUnit textureUnit )
{
- // GL texture units are #defines in growing order to converting that to index
- unsigned int unit = textureUnit - GL_TEXTURE0;
-
- if ( unit != mActiveTextureUnit )
+ if ( textureUnit != mActiveTextureUnit )
{
- mActiveTextureUnit = unit;
+ mActiveTextureUnit = textureUnit;
LOG_GL("ActiveTexture %x\n", textureUnit);
- CHECK_GL( *this, mGlAbstraction.ActiveTexture(textureUnit) );
+ CHECK_GL( *this, mGlAbstraction.ActiveTexture(TextureUnitAsGLenum(textureUnit)) );
}
}
}
/**
- * The wrapper for OpenGL ES 2.0 glBindTexture() has been replaced by Bind2dTexture and BindCubeMapTexture.
+ * Helper to bind texture for rendering. If given texture is
+ * already bound in the given textureunit, this method does nothing.
+ * Otherwise changes the active texture unit and binds the texture.
+ * Note! after this call active texture unit may not necessarily be the one
+ * passed in as argument so you cannot change texture unit state!!
+ * @param textureunit to bind to
+ * @param texture to bind
*/
+ void BindTextureForUnit( TextureUnit textureunit, GLuint texture )
+ {
+ if( mBound2dTextureId[ textureunit ] != texture )
+ {
+ ActiveTexture( textureunit );
+ Bind2dTexture( texture );
+ }
+ }
/**
* Wrapper for OpenGL ES 2.0 glBindTexture(GL_TEXTURE_2D)
GLuint mBoundTransformFeedbackBufferId; ///< The ID passed to glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER)
// glBindTexture() state
- unsigned int mActiveTextureUnit;
+ TextureUnit mActiveTextureUnit;
GLuint mBound2dTextureId[ MAX_TEXTURE_UNITS ]; ///< The ID passed to glBindTexture(GL_TEXTURE_2D)
// glBlendColor() state
bool FrameBufferTexture::Prepare()
{
// bind texture
- Bind(GL_TEXTURE_2D, GL_TEXTURE0);
+ Bind( GL_TEXTURE_2D, TEXTURE_UNIT_FRAMEBUFFER );
if( 0 != mId )
{
DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
mContext.GenTextures(1, &mId);
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD ); // bind in unused unit so rebind works the first time
mContext.Bind2dTexture(mId);
// set texture parameters
if( mNativeImage->GlExtensionCreate() )
{
mContext.GenTextures(1, &mId);
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD ); // bind in unused unit so rebind works the first time
mContext.Bind2dTexture(mId);
mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
// on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
}
-bool NativeTexture::Bind(GLenum target, GLenum textureunit )
+bool NativeTexture::Bind(GLenum target, TextureUnit textureunit )
{
bool created = false;
if( mNativeImage->GlExtensionCreate() )
{
mContext.GenTextures( 1, &mId );
- mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time
+ mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD ); // bind in unused unit so rebind works the first time
mContext.Bind2dTexture( mId );
mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
/**
* @copydoc Texture::Bind
*/
- virtual bool Bind(GLenum target, GLenum textureunit = GL_TEXTURE0);
+ virtual bool Bind( GLenum target, TextureUnit textureunit );
/**
* @copydoc Texture::IsFullyOpaque
}
}
-void TextureCache::BindTexture( Texture *texture, ResourceId id, GLenum target, GLenum textureunit )
+void TextureCache::BindTexture( Texture *texture, ResourceId id, GLenum target, TextureUnit textureunit )
{
bool created = texture->Bind(target, textureunit);
if( created && texture->UpdateOnCreate() ) // i.e. the pixel data was sent to GL
#include <dali/internal/update/common/scene-graph-buffers.h>
#include <dali/internal/render/common/texture-cache-dispatcher.h>
#include <dali/internal/render/gl-resources/texture-declarations.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
namespace Dali
{
* @param[in] texture pointer to the texture
* @param[in] id Resource id of texture
* @param[in] target (e.g. GL_TEXTURE_2D)
- * @param[in] textureunit ( e.g.: GL_TEXTURE0 )
+ * @param[in] textureunit to use
*/
- void BindTexture( Texture* texture, ResourceId id, GLenum target, GLenum textureunit );
+ void BindTexture( Texture* texture, ResourceId id, GLenum target, TextureUnit textureunit );
/**
* Get the texture associated with the resource ID
enum TextureUnit
{
TEXTURE_UNIT_IMAGE = 0,
- TEXTURE_UNIT_TEXT = 0, // for now use same texture unit as text to avoid too many ActiveTexture Calls
+ TEXTURE_UNIT_TEXT,
TEXTURE_UNIT_MATERIAL_DIFFUSE,
TEXTURE_UNIT_MATERIAL_OPACITY,
TEXTURE_UNIT_MATERIAL_NORMAL_MAP,
TEXTURE_UNIT_SHADER,
TEXTURE_UNIT_UPLOAD,
+ TEXTURE_UNIT_FRAMEBUFFER,
+ TEXTURE_UNIT_LAST
};
inline unsigned int TextureUnitAsGLenum( TextureUnit unit )
return false;
}
-bool Texture::Bind(GLenum target, GLenum textureunit )
+bool Texture::Bind(GLenum target, TextureUnit textureunit )
{
// This is the only supported type at the moment
DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
}
// Bind the texture id
- mContext.ActiveTexture(textureunit);
- mContext.Bind2dTexture(mId);
+ mContext.BindTextureForUnit(textureunit, mId );
return created;
}
}
-void Texture::ApplyTextureParameter( GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
+void Texture::ApplyTextureParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
{
GLint newFilterModeGL = FilterModeToGL( newFilterMode, daliDefault, systemDefault );
GLint currentFilterModeGL = FilterModeToGL( currentFilterMode, daliDefault, systemDefault );
if( newFilterModeGL != currentFilterModeGL )
{
+ mContext.ActiveTexture( unit );
mContext.TexParameteri( GL_TEXTURE_2D, filterType, newFilterModeGL );
}
}
-void Texture::ApplySampler( unsigned int samplerBitfield )
+void Texture::ApplySampler( TextureUnit unit, unsigned int samplerBitfield )
{
if( mSamplerBitfield != samplerBitfield && mId != 0 )
{
- ApplyTextureParameter( GL_TEXTURE_MIN_FILTER,
+ ApplyTextureParameter( unit,
+ GL_TEXTURE_MIN_FILTER,
ImageSampler::GetMinifyFilterMode( mSamplerBitfield ),
ImageSampler::GetMinifyFilterMode( samplerBitfield ),
DALI_MINIFY_DEFAULT,
SYSTEM_MINIFY_DEFAULT );
- ApplyTextureParameter( GL_TEXTURE_MAG_FILTER,
+ ApplyTextureParameter( unit,
+ GL_TEXTURE_MAG_FILTER,
ImageSampler::GetMagnifyFilterMode( mSamplerBitfield ),
ImageSampler::GetMagnifyFilterMode( samplerBitfield ),
DALI_MAGNIFY_DEFAULT,
#include <dali/internal/render/common/uv-rect.h>
#include <dali/integration-api/gl-abstraction.h>
#include <dali/internal/render/gl-resources/gl-resource-owner.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
#include <dali/public-api/images/image.h>
#include <dali/public-api/images/pixel.h>
#include <dali/public-api/images/native-image.h>
* @return True if the opengl texture was created, false if there was already a texture
* or no texture could be created yet ( e.g. no bitmap data after context loss )
*/
- virtual bool Bind(GLenum target, GLenum textureunit);
+ virtual bool Bind(GLenum target, TextureUnit textureunit);
/**
* Returns GL texture ID
/**
* @brief Apply the given sampler to the texture.
*
+ * @param[in] texture unit to use
* @param[in] samplerBitfield A bitfield with packed sampler options.
*/
- void ApplySampler( unsigned int samplerBitfield );
+ void ApplySampler( TextureUnit unit, unsigned int samplerBitfield );
protected:
/**
* @brief Apply the given texture parameters.
*
+ * @param[in] texture unit to use
* @param[in] filterType Minification or magnification.
* @param[in] currentFilterMode The current filter mode.
* @param[in] newFilterMode The new filter mode.
* @param[in] daliDefault The default dali filter mode for the given filterType.
* @param[in] systemDefault The default system filter mode for the given filterType.
*/
- void ApplyTextureParameter( GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault );
+ void ApplyTextureParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault );
protected:
if( texture != NULL )
{
- mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, TextureUnitAsGLenum( textureUnit ) );
+ mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, textureUnit );
// Set sampler uniforms for textures
GLint samplerLoc = program.GetUniformLocation( samplerIndex );
if( Program::UNIFORM_UNKNOWN != samplerLoc )
if( mDiffuseTexture )
{
- mDiffuseTexture->ApplySampler( textureSampler );
+ mDiffuseTexture->ApplySampler( TEXTURE_UNIT_MATERIAL_DIFFUSE, textureSampler );
}
- // GL_TEXTURE1 is used by shader effect texture
BindTexture( program, mOpacityTextureId, mOpacityTexture, TEXTURE_UNIT_MATERIAL_OPACITY, Program::UNIFORM_SAMPLER_OPACITY );
if( mOpacityTexture )
{
- mOpacityTexture->ApplySampler( textureSampler );
+ mOpacityTexture->ApplySampler( TEXTURE_UNIT_MATERIAL_OPACITY, textureSampler );
}
BindTexture( program, mNormalMapTextureId, mNormalMapTexture, TEXTURE_UNIT_MATERIAL_NORMAL_MAP, Program::UNIFORM_SAMPLER_NORMAL_MAP );
if( mNormalMapTexture )
{
- mNormalMapTexture->ApplySampler( textureSampler );
+ mNormalMapTexture->ApplySampler( TEXTURE_UNIT_MATERIAL_NORMAL_MAP, textureSampler );
}
}
DALI_ASSERT_DEBUG( mVertexBuffer );
- mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, TextureUnitAsGLenum( TEXTURE_UNIT_IMAGE ) );
+ mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, TEXTURE_UNIT_IMAGE );
if( mTexture->GetTextureId() == 0 )
{
return; // early out if we haven't got a GL texture yet (e.g. due to context loss)
}
- mTexture->ApplySampler( mSamplerBitfield );
+ mTexture->ApplySampler( TEXTURE_UNIT_IMAGE, mSamplerBitfield );
// Set sampler uniform
GLint samplerLoc = program.GetUniformLocation( Program::UNIFORM_SAMPLER );
DALI_LOG_INFO( gTextFilter, Debug::General, "TextRenderer::DoRender(this: %p) textureId:%d\n", this, mTextureId );
- mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, TextureUnitAsGLenum( TEXTURE_UNIT_TEXT ) );
+ mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, TEXTURE_UNIT_TEXT );
if( mTexture->GetTextureId() == 0 )
{
return; // early out if we haven't got a GL texture yet (e.g. due to context loss)
program.SetUniform1i( samplerLoc, TEXTURE_UNIT_TEXT );
}
- mTexture->ApplySampler( mSamplerBitfield );
+ mTexture->ApplySampler( TEXTURE_UNIT_TEXT, mSamplerBitfield );
const float SMOOTHING_ADJUSTMENT( 12.0f );
const float SMOOTHING_ADJUSTMENT_PIXEL_SIZE( 32.0f );
if( mTexture )
{
// got effect texture, bind it to texture unit 1
- mTextureCache->BindTexture( mTexture, mRenderTextureId, GL_TEXTURE_2D, TextureUnitAsGLenum( TEXTURE_UNIT_SHADER ) );
+ mTextureCache->BindTexture( mTexture, mRenderTextureId, GL_TEXTURE_2D, TEXTURE_UNIT_SHADER);
// Just apply the default sampling options for now
- mTexture->ApplySampler( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) );
+ mTexture->ApplySampler( TEXTURE_UNIT_SHADER, ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) );
// get effect sampler uniform
const GLint loc = program.GetUniformLocation( Program::UNIFORM_EFFECT_SAMPLER );