proper enums for texture units to improve readability and allow renderer type specifi... 76/30176/2
authorKimmo Hoikka <kimmo.hoikka@samsung.com>
Wed, 12 Nov 2014 11:53:16 +0000 (11:53 +0000)
committerKimmo Hoikka <kimmo.hoikka@samsung.com>
Wed, 12 Nov 2014 17:51:43 +0000 (17:51 +0000)
[Problem]
[Cause]
[Solution]

Change-Id: Ibdacf7b632f4922cb0a991b267d9723bfd0c71ae

dali/internal/render/gl-resources/bitmap-texture.cpp
dali/internal/render/gl-resources/compressed-bitmap-texture.cpp
dali/internal/render/gl-resources/frame-buffer-texture.cpp
dali/internal/render/gl-resources/native-frame-buffer-texture.cpp
dali/internal/render/gl-resources/native-texture.cpp
dali/internal/render/gl-resources/texture-units.h [new file with mode: 0644]
dali/internal/render/renderers/render-material.cpp
dali/internal/render/renderers/render-material.h
dali/internal/render/renderers/scene-graph-image-renderer.cpp
dali/internal/render/renderers/scene-graph-text-renderer.cpp
dali/internal/render/shaders/shader.cpp

index 75c44f1..d3d11cb 100644 (file)
 // CLASS HEADER
 #include <dali/internal/render/gl-resources/bitmap-texture.h>
 
-// EXTERNAL INCLUDES
-#include <math.h>
-#include <memory.h>
-
 // INTERNAL INCLUDES
-#include <dali/public-api/math/rect.h>
-#include <dali/public-api/math/math-utils.h>
 #include <dali/integration-api/debug.h>
 #include <dali/internal/render/common/vertex.h>
 #include <dali/internal/render/common/performance-monitor.h>
 #include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 
 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;
index ba5ff0d..7155704 100644 (file)
 // CLASS HEADER
 #include <dali/internal/render/gl-resources/compressed-bitmap-texture.h>
 
-// EXTERNAL INCLUDES
-#include <math.h>
-#include <memory.h>
-
 // INTERNAL INCLUDES
-#include <dali/public-api/math/rect.h>
-#include <dali/public-api/math/math-utils.h>
 #include <dali/integration-api/debug.h>
 #include <dali/internal/render/common/vertex.h>
 #include <dali/internal/render/common/performance-monitor.h>
 #include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 
 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;
index 0755310..37a7134 100644 (file)
@@ -20,6 +20,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/integration-api/debug.h>
 
 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
index e762dbf..acd2136 100644 (file)
@@ -20,6 +20,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/integration-api/debug.h>
 
 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
index 1c3f93a..239a915 100644 (file)
 // CLASS HEADER
 #include <dali/internal/render/gl-resources/native-texture.h>
 
-// EXTERNAL INCLUDES
-#include <math.h>
-#include <memory.h>
-
 // INTERNAL INCLUDES
-#include <dali/public-api/math/rect.h>
-#include <dali/public-api/math/math-utils.h>
 #include <dali/integration-api/debug.h>
 #include <dali/internal/render/common/vertex.h>
 #include <dali/internal/render/gl-resources/context.h>
 #include <dali/internal/render/gl-resources/texture.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 
 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 (file)
index 0000000..86fa36f
--- /dev/null
@@ -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 <dali/integration-api/gl-defines.h>
+
+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<unsigned int>( unit );
+}
+
+}//Internal
+
+}//Dali
+
+#endif // __DALI_INTERNAL_TEXTURE_UNITS_H__
index bbdebd5..e6114fa 100644 (file)
@@ -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 )
   {
index 8496912..02d8f98 100644 (file)
@@ -23,6 +23,7 @@
 #include <dali/internal/event/effects/shader-declarations.h>
 #include <dali/internal/render/shaders/custom-uniform.h>
 #include <dali/internal/render/gl-resources/texture-observer.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/internal/render/shaders/program.h>
 
 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
index 679127e..e35f5de 100644 (file)
@@ -28,6 +28,7 @@
 #include <dali/internal/render/gl-resources/gpu-buffer.h>
 #include <dali/internal/render/gl-resources/texture.h>
 #include <dali/internal/render/gl-resources/texture-cache.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/internal/render/renderers/scene-graph-renderer-debug.h>
 #include <dali/internal/render/shaders/program.h>
 #include <dali/internal/render/shaders/shader.h>
@@ -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 )
   {
index b223923..b988751 100644 (file)
 #include <dali/internal/common/text-vertex-2d.h>
 #include <dali/internal/event/text/font-impl.h>
 #include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/gl-resources/texture.h>
+#include <dali/internal/render/gl-resources/texture-cache.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/internal/render/common/culling-algorithms.h>
 #include <dali/internal/render/common/performance-monitor.h>
 #include <dali/internal/render/common/vertex.h>
 #include <dali/internal/render/renderers/scene-graph-renderer-debug.h>
 #include <dali/internal/render/shaders/program.h>
 #include <dali/internal/render/shaders/shader.h>
-#include <dali/internal/render/gl-resources/texture-cache.h>
-#include <dali/internal/render/gl-resources/texture.h>
 #include <dali/internal/update/controllers/scene-controller.h>
 
 #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 );
 
index a5af5b9..edf5750 100644 (file)
 #include <dali/internal/render/shaders/shader.h>
 
 // INTERNAL INCLUDES
-#include <dali/public-api/common/dali-common.h>
-#include <dali/public-api/common/stage.h>
-#include <dali/public-api/math/matrix.h>
-#include <dali/public-api/math/matrix3.h>
-#include <dali/public-api/math/vector4.h>
-#include <dali/internal/event/effects/shader-factory.h>
 #include <dali/internal/render/queue/render-queue.h>
 #include <dali/internal/render/common/render-debug.h>
 #include <dali/internal/render/common/post-process-resource-dispatcher.h>
 #include <dali/internal/render/gl-resources/texture.h>
 #include <dali/internal/render/gl-resources/texture-cache.h>
+#include <dali/internal/render/gl-resources/texture-units.h>
 #include <dali/internal/render/shaders/program.h>
 #include <dali/internal/render/shaders/uniform-meta.h>
 #include <dali/internal/common/image-sampler.h>
@@ -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 );
     }
   }