From fcc6c2d8cf990cf2619444b0ae28b0cacbca3192 Mon Sep 17 00:00:00 2001 From: Kimmo Hoikka Date: Wed, 12 Nov 2014 11:53:16 +0000 Subject: [PATCH] proper enums for texture units to improve readability and allow renderer type specific units for less state changes [Problem] [Cause] [Solution] Change-Id: Ibdacf7b632f4922cb0a991b267d9723bfd0c71ae --- .../render/gl-resources/bitmap-texture.cpp | 15 +++---- .../gl-resources/compressed-bitmap-texture.cpp | 9 +--- .../render/gl-resources/frame-buffer-texture.cpp | 3 +- .../gl-resources/native-frame-buffer-texture.cpp | 3 +- .../render/gl-resources/native-texture.cpp | 9 +--- dali/internal/render/gl-resources/texture-units.h | 50 ++++++++++++++++++++++ dali/internal/render/renderers/render-material.cpp | 12 +++--- dali/internal/render/renderers/render-material.h | 3 +- .../renderers/scene-graph-image-renderer.cpp | 13 +++--- .../render/renderers/scene-graph-text-renderer.cpp | 18 +++++--- dali/internal/render/shaders/shader.cpp | 13 ++---- 11 files changed, 94 insertions(+), 54 deletions(-) create mode 100644 dali/internal/render/gl-resources/texture-units.h diff --git a/dali/internal/render/gl-resources/bitmap-texture.cpp b/dali/internal/render/gl-resources/bitmap-texture.cpp index 75c44f1..d3d11cb 100644 --- a/dali/internal/render/gl-resources/bitmap-texture.cpp +++ b/dali/internal/render/gl-resources/bitmap-texture.cpp @@ -18,17 +18,12 @@ // CLASS HEADER #include -// EXTERNAL INCLUDES -#include -#include - // INTERNAL INCLUDES -#include -#include #include #include #include #include +#include namespace Dali { @@ -90,7 +85,7 @@ void BitmapTexture::UploadBitmapArray( const BitmapUploadArray& bitmapArray ) CreateGlTexture(); } - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( 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 @@ -164,7 +159,7 @@ void BitmapTexture::AreaUpdated( const RectArea& updateArea, const unsigned char GLenum pixelDataType = GL_UNSIGNED_BYTE; Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time mContext.Bind2dTexture(mId); @@ -223,7 +218,7 @@ void BitmapTexture::AssignBitmap( bool generateTexture, const unsigned char* pix } DALI_ASSERT_DEBUG( mId != 0 ); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time mContext.Bind2dTexture(mId); Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat); @@ -334,7 +329,7 @@ void BitmapTexture::ClearAreas( const BitmapClearArray& areaArray, std::size_t b GLenum pixelDataType = GL_UNSIGNED_BYTE; Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time mContext.Bind2dTexture(mId); size_t numPixels = blockSize*blockSize; diff --git a/dali/internal/render/gl-resources/compressed-bitmap-texture.cpp b/dali/internal/render/gl-resources/compressed-bitmap-texture.cpp index ba5ff0d..7155704 100644 --- a/dali/internal/render/gl-resources/compressed-bitmap-texture.cpp +++ b/dali/internal/render/gl-resources/compressed-bitmap-texture.cpp @@ -18,17 +18,12 @@ // CLASS HEADER #include -// EXTERNAL INCLUDES -#include -#include - // INTERNAL INCLUDES -#include -#include #include #include #include #include +#include namespace Dali { @@ -86,7 +81,7 @@ void CompressedBitmapTexture::AssignBitmap( bool generateTexture, const unsigned } DALI_ASSERT_DEBUG( mId != 0 ); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture(TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD )); // bind in unused unit so rebind works the first time mContext.Bind2dTexture(mId); GLenum pixelFormat = GL_RGBA; diff --git a/dali/internal/render/gl-resources/frame-buffer-texture.cpp b/dali/internal/render/gl-resources/frame-buffer-texture.cpp index 0755310..37a7134 100644 --- a/dali/internal/render/gl-resources/frame-buffer-texture.cpp +++ b/dali/internal/render/gl-resources/frame-buffer-texture.cpp @@ -20,6 +20,7 @@ // INTERNAL INCLUDES #include +#include #include namespace Dali @@ -98,7 +99,7 @@ bool FrameBufferTexture::CreateGlTexture() DALI_LOG_TRACE_METHOD(Debug::Filter::gImage); mContext.GenTextures(1, &mId); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) ); // bind in unused unit so rebind works the first time mContext.Bind2dTexture(mId); // set texture parameters diff --git a/dali/internal/render/gl-resources/native-frame-buffer-texture.cpp b/dali/internal/render/gl-resources/native-frame-buffer-texture.cpp index e762dbf..acd2136 100644 --- a/dali/internal/render/gl-resources/native-frame-buffer-texture.cpp +++ b/dali/internal/render/gl-resources/native-frame-buffer-texture.cpp @@ -20,6 +20,7 @@ // INTERNAL INCLUDES #include +#include #include namespace Dali @@ -76,7 +77,7 @@ bool NativeFrameBufferTexture::CreateGlTexture() if( mNativeImage->GlExtensionCreate() ) { mContext.GenTextures(1, &mId); - mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( 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 diff --git a/dali/internal/render/gl-resources/native-texture.cpp b/dali/internal/render/gl-resources/native-texture.cpp index 1c3f93a..239a915 100644 --- a/dali/internal/render/gl-resources/native-texture.cpp +++ b/dali/internal/render/gl-resources/native-texture.cpp @@ -18,17 +18,12 @@ // CLASS HEADER #include -// EXTERNAL INCLUDES -#include -#include - // INTERNAL INCLUDES -#include -#include #include #include #include #include +#include namespace Dali { @@ -95,7 +90,7 @@ bool NativeTexture::CreateGlTexture() if( mNativeImage->GlExtensionCreate() ) { mContext.GenTextures( 1, &mId ); - mContext.ActiveTexture( GL_TEXTURE7 ); // bind in unused unit so rebind works the first time + mContext.ActiveTexture( TextureUnitAsGLenum( 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 diff --git a/dali/internal/render/gl-resources/texture-units.h b/dali/internal/render/gl-resources/texture-units.h new file mode 100644 index 0000000..86fa36f --- /dev/null +++ b/dali/internal/render/gl-resources/texture-units.h @@ -0,0 +1,50 @@ +#ifndef __DALI_INTERNAL_TEXTURE_UNITS_H__ +#define __DALI_INTERNAL_TEXTURE_UNITS_H__ + +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// INTERNAL INCLUDES +#include + +namespace Dali +{ +namespace Internal +{ + +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_MATERIAL_DIFFUSE, + TEXTURE_UNIT_MATERIAL_OPACITY, + TEXTURE_UNIT_MATERIAL_NORMAL_MAP, + TEXTURE_UNIT_SHADER, + TEXTURE_UNIT_UPLOAD, +}; + +inline unsigned int TextureUnitAsGLenum( TextureUnit unit ) +{ + // GL texture units are #defines in growing order to converting that to index + return GL_TEXTURE0 + static_cast( unit ); +} + +}//Internal + +}//Dali + +#endif // __DALI_INTERNAL_TEXTURE_UNITS_H__ diff --git a/dali/internal/render/renderers/render-material.cpp b/dali/internal/render/renderers/render-material.cpp index bbdebd5..e6114fa 100644 --- a/dali/internal/render/renderers/render-material.cpp +++ b/dali/internal/render/renderers/render-material.cpp @@ -227,16 +227,16 @@ void RenderMaterial::SetUniforms( RenderMaterialUniforms& uniforms, Program& pro uniforms.SetUniforms( *this, program, shaderType ); } -void RenderMaterial::BindTexture( Program& program, ResourceId id, Texture* texture, unsigned int textureUnit, Program::UniformType samplerIndex ) const +void RenderMaterial::BindTexture( Program& program, ResourceId id, Texture* texture, TextureUnit textureUnit, Program::UniformType samplerIndex ) const { DALI_ASSERT_DEBUG( NULL != mTextureCache ); if( texture != NULL ) { - mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, GL_TEXTURE0 + textureUnit ); + mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, TextureUnitAsGLenum( textureUnit ) ); // Set sampler uniforms for textures GLint samplerLoc = program.GetUniformLocation( samplerIndex ); - if( -1 != samplerLoc ) + if( Program::UNIFORM_UNKNOWN != samplerLoc ) { program.SetUniform1i( samplerLoc, textureUnit ); } @@ -281,7 +281,7 @@ void RenderMaterial::BindTextures( Program& program, unsigned int textureSampler } } - BindTexture( program, mDiffuseTextureId, mDiffuseTexture, 0, Program::UNIFORM_SAMPLER ); + BindTexture( program, mDiffuseTextureId, mDiffuseTexture, TEXTURE_UNIT_MATERIAL_DIFFUSE, Program::UNIFORM_SAMPLER ); if( mDiffuseTexture ) { @@ -289,14 +289,14 @@ void RenderMaterial::BindTextures( Program& program, unsigned int textureSampler } // GL_TEXTURE1 is used by shader effect texture - BindTexture( program, mOpacityTextureId, mOpacityTexture, 2, Program::UNIFORM_SAMPLER_OPACITY ); + BindTexture( program, mOpacityTextureId, mOpacityTexture, TEXTURE_UNIT_MATERIAL_OPACITY, Program::UNIFORM_SAMPLER_OPACITY ); if( mOpacityTexture ) { mOpacityTexture->ApplySampler( textureSampler ); } - BindTexture( program, mNormalMapTextureId, mNormalMapTexture, 3, Program::UNIFORM_SAMPLER_NORMAL_MAP ); + BindTexture( program, mNormalMapTextureId, mNormalMapTexture, TEXTURE_UNIT_MATERIAL_NORMAL_MAP, Program::UNIFORM_SAMPLER_NORMAL_MAP ); if( mNormalMapTexture ) { diff --git a/dali/internal/render/renderers/render-material.h b/dali/internal/render/renderers/render-material.h index 8496912..02d8f98 100644 --- a/dali/internal/render/renderers/render-material.h +++ b/dali/internal/render/renderers/render-material.h @@ -23,6 +23,7 @@ #include #include #include +#include #include namespace Dali @@ -183,7 +184,7 @@ private: * @param[in] textureUnit Texture unit ( should be a value between 0 and the max number of samplers - 1 ) * @param[in] samplerIndex The index of the uniform to bind the texture sampler */ - void BindTexture( Program& program, unsigned int textureId, Texture* texture, unsigned int textureUnit, Program::UniformType samplerIndex ) const; + void BindTexture( Program& program, unsigned int textureId, Texture* texture, TextureUnit textureUnit, Program::UniformType samplerIndex ) const; private: TextureCache* mTextureCache; ///< Texture cache of GL resources diff --git a/dali/internal/render/renderers/scene-graph-image-renderer.cpp b/dali/internal/render/renderers/scene-graph-image-renderer.cpp index 679127e..e35f5de 100644 --- a/dali/internal/render/renderers/scene-graph-image-renderer.cpp +++ b/dali/internal/render/renderers/scene-graph-image-renderer.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -265,7 +266,7 @@ void ImageRenderer::DoRender( BufferIndex bufferIndex, Program& program, const M DALI_ASSERT_DEBUG( mVertexBuffer ); - mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, GL_TEXTURE0 ); + mTextureCache->BindTexture( mTexture, mTextureId, GL_TEXTURE_2D, TextureUnitAsGLenum( TEXTURE_UNIT_IMAGE ) ); if( mTexture->GetTextureId() == 0 ) { @@ -274,18 +275,18 @@ void ImageRenderer::DoRender( BufferIndex bufferIndex, Program& program, const M mTexture->ApplySampler( mSamplerBitfield ); - // make sure the vertex is bound, this has to be done before - // we call VertexAttribPointer otherwise you get weird output on the display - mVertexBuffer->Bind(); - // Set sampler uniform GLint samplerLoc = program.GetUniformLocation( Program::UNIFORM_SAMPLER ); if( -1 != samplerLoc ) { // set the uniform - program.SetUniform1i( samplerLoc, 0 ); + program.SetUniform1i( samplerLoc, TEXTURE_UNIT_IMAGE ); } + // make sure the vertex is bound, this has to be done before + // we call VertexAttribPointer otherwise you get weird output on the display + mVertexBuffer->Bind(); + samplerLoc = program.GetUniformLocation( Program::UNIFORM_SAMPLER_RECT ); if( -1 != samplerLoc ) { diff --git a/dali/internal/render/renderers/scene-graph-text-renderer.cpp b/dali/internal/render/renderers/scene-graph-text-renderer.cpp index b223923..b988751 100644 --- a/dali/internal/render/renderers/scene-graph-text-renderer.cpp +++ b/dali/internal/render/renderers/scene-graph-text-renderer.cpp @@ -25,14 +25,15 @@ #include #include #include +#include +#include +#include #include #include #include #include #include #include -#include -#include #include #if defined(DEBUG_ENABLED) @@ -356,14 +357,22 @@ void TextRenderer::DoRender( BufferIndex bufferIndex, Program& program, const Ma 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 ) ); + if( mTexture->GetTextureId() == 0 ) + { + return; // early out if we haven't got a GL texture yet (e.g. due to context loss) + } + // Set sampler uniform const GLint samplerLoc = program.GetUniformLocation( Program::UNIFORM_SAMPLER ); if( Program::UNIFORM_UNKNOWN != samplerLoc ) { // set the uniform - program.SetUniform1i( samplerLoc, 0 ); + program.SetUniform1i( samplerLoc, TEXTURE_UNIT_TEXT ); } + mTexture->ApplySampler( mSamplerBitfield ); + const float SMOOTHING_ADJUSTMENT( 12.0f ); const float SMOOTHING_ADJUSTMENT_PIXEL_SIZE( 32.0f ); @@ -492,9 +501,6 @@ void TextRenderer::DoRender( BufferIndex bufferIndex, Program& program, const Ma const GLint positionLoc = program.GetAttribLocation(Program::ATTRIB_POSITION); const GLint texCoordLoc = program.GetAttribLocation(Program::ATTRIB_TEXCOORD); - mTexture->Bind(GL_TEXTURE_2D, GL_TEXTURE0); - mTexture->ApplySampler( mSamplerBitfield ); - mContext->EnableVertexAttributeArray( positionLoc ); mContext->EnableVertexAttributeArray( texCoordLoc ); diff --git a/dali/internal/render/shaders/shader.cpp b/dali/internal/render/shaders/shader.cpp index a5af5b9..edf5750 100644 --- a/dali/internal/render/shaders/shader.cpp +++ b/dali/internal/render/shaders/shader.cpp @@ -19,17 +19,12 @@ #include // INTERNAL INCLUDES -#include -#include -#include -#include -#include -#include #include #include #include #include #include +#include #include #include #include @@ -315,7 +310,7 @@ void Shader::SetUniforms( Context& context, if( mTexture ) { // got effect texture, bind it to texture unit 1 - mTextureCache->BindTexture( mTexture, mRenderTextureId, GL_TEXTURE_2D, GL_TEXTURE1 ); + mTextureCache->BindTexture( mTexture, mRenderTextureId, GL_TEXTURE_2D, TextureUnitAsGLenum( TEXTURE_UNIT_SHADER ) ); // Just apply the default sampling options for now mTexture->ApplySampler( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ); @@ -324,9 +319,9 @@ void Shader::SetUniforms( Context& context, const GLint loc = program.GetUniformLocation( Program::UNIFORM_EFFECT_SAMPLER ); if( Program::UNIFORM_UNKNOWN != loc ) { - DALI_PRINT_UNIFORM( debugStream, bufferIndex, "sEffect", 1 ); + DALI_PRINT_UNIFORM( debugStream, bufferIndex, "sEffect", TEXTURE_UNIT_SHADER ); // set the uniform - program.SetUniform1i( loc, 1 ); + program.SetUniform1i( loc, TEXTURE_UNIT_SHADER ); } } -- 2.7.4