From c028c1e26d649dff790da825324cfb528af378c1 Mon Sep 17 00:00:00 2001 From: Xiangyin Ma Date: Thu, 11 Feb 2016 16:49:55 +0000 Subject: [PATCH] Ensure the use of ShaderEffect in ImageActor is backwards-compatible Change-Id: If517adfd7b1dca2478df14747bba18518e016824 --- dali/internal/event/actors/image-actor-impl.cpp | 61 ++++++++++++++++++------- dali/internal/event/actors/image-actor-impl.h | 13 ++++++ dali/internal/render/shader-source/image.txt | 11 +++-- dali/internal/render/shaders/program.cpp | 2 +- dali/public-api/shader-effects/shader-effect.h | 8 ++-- 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/dali/internal/event/actors/image-actor-impl.cpp b/dali/internal/event/actors/image-actor-impl.cpp index 035b23c..1a85477 100644 --- a/dali/internal/event/actors/image-actor-impl.cpp +++ b/dali/internal/event/actors/image-actor-impl.cpp @@ -59,14 +59,20 @@ TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::Actor ), Creat struct GridVertex { + GridVertex( float positionX, float positionY, const Vector2& size ) + : mPosition( positionX*size.x, positionY*size.y, 0.f ), + mTextureCoord( positionX+0.5f, positionY+0.5f ) + { + } + Vector3 mPosition; Vector2 mTextureCoord; }; -GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight ) +GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight, const Vector2& size ) { // Create vertices - std::vector< Vector2 > vertices; + std::vector< GridVertex > vertices; vertices.reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) ); for( unsigned int y = 0u; y < gridHeight + 1; ++y ) @@ -75,7 +81,7 @@ GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight ) for( unsigned int x = 0u; x < gridWidth + 1; ++x ) { float xPos = (float)x / gridWidth; - vertices.push_back( Vector2( xPos - 0.5f, yPos - 0.5f ) ); + vertices.push_back( GridVertex( xPos - 0.5f, yPos - 0.5f, size ) ); } } @@ -107,7 +113,8 @@ GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight ) Property::Map vertexFormat; - vertexFormat[ "aPosition" ] = Property::VECTOR2; + vertexFormat[ "aPosition" ] = Property::VECTOR3; + vertexFormat[ "aTexCoord" ] = Property::VECTOR2; PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New(); vertexPropertyBuffer->SetFormat( vertexFormat ); vertexPropertyBuffer->SetSize( vertices.size() ); @@ -137,16 +144,17 @@ GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight ) } const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n + attribute mediump vec3 aPosition;\n + attribute mediump vec2 aTexCoord;\n varying mediump vec2 vTexCoord;\n uniform mediump mat4 uMvpMatrix;\n uniform mediump vec3 uSize;\n - uniform mediump vec4 uTextureRect;\n + uniform mediump vec4 sTextureRect;\n \n void main()\n {\n - gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0);\n - vTexCoord = mix( uTextureRect.xy, uTextureRect.zw, aPosition + vec2(0.5));\n + gl_Position = uMvpMatrix * vec4(aPosition, 1.0);\n + vTexCoord = aTexCoord;\n }\n ); @@ -161,6 +169,8 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( }\n ); +const char * const TEXTURE_RECT_UNIFORM_NAME( "sTextureRect" ); + const size_t INVALID_TEXTURE_ID = (size_t)-1; const int INVALID_RENDERER_ID = -1; const uint16_t MAXIMUM_GRID_SIZE = 2048; @@ -176,7 +186,7 @@ ImageActorPtr ImageActor::New() //Create the renderer actor->mRenderer = Renderer::New(); - GeometryPtr quad = CreateGeometry( 1u, 1u ); + GeometryPtr quad = CreateGeometry( 1u, 1u, Vector2::ONE ); actor->mRenderer->SetGeometry( *quad ); ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE ); @@ -294,6 +304,7 @@ Vector4 ImageActor::GetNinePatchBorder() const ImageActor::ImageActor() : Actor( Actor::BASIC ), + mActorSize( Vector2::ZERO ), mGridSize( 1u, 1u ), mRendererIndex( INVALID_RENDERER_ID ), mTextureIndex( INVALID_TEXTURE_ID ), @@ -352,14 +363,11 @@ void ImageActor::UpdateGeometry() gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast(gridSize.height) ); } - if( gridWidth != mGridSize.GetWidth() || gridHeight != mGridSize.GetHeight() ) - { - mGridSize.SetWidth( gridWidth ); - mGridSize.SetHeight( gridHeight ); + mGridSize.SetWidth( gridWidth ); + mGridSize.SetHeight( gridHeight ); - GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight ); - mRenderer->SetGeometry( *geometry ); - } + GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight, mActorSize ); + mRenderer->SetGeometry( *geometry ); } void ImageActor::UpdateTexureRect() { @@ -379,7 +387,7 @@ void ImageActor::UpdateTexureRect() } Material* material = mRenderer->GetMaterial(); - material->RegisterProperty( "uTextureRect", textureRect ); + material->RegisterProperty( TEXTURE_RECT_UNIFORM_NAME, textureRect ); } unsigned int ImageActor::GetDefaultPropertyCount() const @@ -759,6 +767,25 @@ void ImageActor::EffectImageUpdated() } } +void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container ) +{ + if( mActorSize != size ) + { + mActorSize = size; + UpdateGeometry(); + } +} + +void ImageActor::OnSizeSet( const Vector3& targetSize ) +{ + Vector2 size( targetSize.x, targetSize.y ); + if( mActorSize != size ) + { + mActorSize = size; + UpdateGeometry(); + } +} + } // namespace Internal } // namespace Dali diff --git a/dali/internal/event/actors/image-actor-impl.h b/dali/internal/event/actors/image-actor-impl.h index a9e2f6f..be5fa46 100644 --- a/dali/internal/event/actors/image-actor-impl.h +++ b/dali/internal/event/actors/image-actor-impl.h @@ -222,6 +222,18 @@ public: */ virtual void RemoveShaderEffect(); +private: + + /** + * @copydoc Actor::OnRelayout + */ + virtual void OnRelayout( const Vector2& size, RelayoutContainer& container ); + + /** + * @copydoc Actor::OnSizeSet + */ + virtual void OnSizeSet( const Vector3& targetSize ); + public: // Default property extensions from Object /** @@ -319,6 +331,7 @@ private: PixelArea mPixelArea; ///< The pixel area of the image to render Vector4 mBlendColor; ///< The blend color for this ImageActor Vector4 mNinePatchBorder; ///< Nine-patch not supported, but this is used to store what is set so it can be returned for backwards compatibility. + Vector2 mActorSize; ///< The actor size Uint16Pair mGridSize; ///< The geometry grid size int mRendererIndex; ///< The index location of mRenderer size_t mTextureIndex; ///< The texture index for this ImageActor's texture diff --git a/dali/internal/render/shader-source/image.txt b/dali/internal/render/shader-source/image.txt index 3705da8..6ad19f8 100644 --- a/dali/internal/render/shader-source/image.txt +++ b/dali/internal/render/shader-source/image.txt @@ -1,6 +1,7 @@ -attribute mediump vec2 aPosition; +attribute mediump vec3 aPosition; +attribute mediump vec2 aTexCoord; uniform mediump mat4 uModelView; uniform mediump mat4 uProjection; @@ -14,12 +15,12 @@ uniform mediump vec3 uSize; varying mediump vec2 vTexCoord; -uniform mediump vec4 uTextureRect; +uniform mediump vec4 sTextureRect; void main() { - gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0); - vTexCoord = mix( uTextureRect.xy, uTextureRect.zw, aPosition + vec2(0.5) ); + gl_Position = uMvpMatrix * vec4(aPosition, 1.0); + vTexCoord = aTexCoord; } @@ -29,7 +30,7 @@ void main() uniform sampler2D sTexture; uniform sampler2D sEffect; -uniform mediump vec4 uTextureRect; +uniform mediump vec4 sTextureRect; uniform mediump vec4 uColor; varying mediump vec2 vTexCoord; diff --git a/dali/internal/render/shaders/program.cpp b/dali/internal/render/shaders/program.cpp index e1dbe8a..a2367f6 100644 --- a/dali/internal/render/shaders/program.cpp +++ b/dali/internal/render/shaders/program.cpp @@ -92,7 +92,7 @@ const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] = "uNormalMatrix", // UNIFORM_NORMAL_MATRIX "uColor", // UNIFORM_COLOR "sTexture", // UNIFORM_SAMPLER - "uTextureRect", // UNIFORM_SAMPLER_RECT + "sTextureRect", // UNIFORM_SAMPLER_RECT "sEffect", // UNIFORM_EFFECT_SAMPLER "uSize" // UNIFORM_SIZE }; diff --git a/dali/public-api/shader-effects/shader-effect.h b/dali/public-api/shader-effects/shader-effect.h index f810db4..22f2c46 100644 --- a/dali/public-api/shader-effects/shader-effect.h +++ b/dali/public-api/shader-effects/shader-effect.h @@ -48,8 +48,8 @@ namespace Dali * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER ( * void main() * { - * gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0); - * vTexCoord = mix( uTextureRect.xy, uTextureRect.zw, aPosition + vec2(0.5) ); + * gl_Position = uMvpMatrix * vec4(aPosition, 1.0); + * vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord) ); * } * ); * @SINCE_1_0.0 @@ -92,8 +92,8 @@ class ShaderEffect; *
  * void main()
  * {
- *     gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0);
- *     vTexCoord = mix( uTextureRect.xy, uTextureRect.zw, aPosition + vec2(0.5) );
+ *     gl_Position = uMvpMatrix * vec4(aPosition, 1.0);
+ *     vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord );
  * }
  * 
* For fragment shader the default part for images contains the following code: -- 2.7.4