Convert more shaders in dali-toolkit and dali-scene-loader to use shader compilation...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / mesh / mesh-visual.cpp
index 4d6a4cd..f34f66c 100644 (file)
@@ -30,6 +30,7 @@
 #include <dali-toolkit/public-api/visuals/visual-properties.h>
 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
+#include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
 
 namespace Dali
 {
@@ -94,253 +95,6 @@ DALI_ENUM_TO_STRING_TABLE_END( SHADING_MODE )
 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
 
-//Shaders
-//If a shader requires certain textures, they must be listed in order,
-//as detailed in the TextureIndex enum documentation.
-
-//A basic shader that doesn't use textures at all.
-const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER(
-  attribute highp vec3 aPosition;\n
-  attribute highp vec3 aNormal;\n
-  varying mediump vec3 vIllumination;\n
-  uniform mediump vec3 uSize;\n
-  uniform mediump mat4 uMvpMatrix;\n
-  uniform mediump mat4 uModelView;\n
-  uniform mediump mat4 uViewMatrix;\n
-  uniform mediump mat3 uNormalMatrix;
-  uniform mediump mat4 uObjectMatrix;\n
-  uniform mediump vec3 lightPosition;\n
-  uniform mediump vec2 uStageOffset;\n
-
-  //Visual size and offset
-  uniform mediump vec2 offset;\n
-  uniform mediump vec2 size;\n
-  uniform mediump vec4 offsetSizeMode;\n
-  uniform mediump vec2 origin;\n
-  uniform mediump vec2 anchorPoint;\n
-
-  vec4 ComputeVertexPosition()\n
-  {\n
-    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
-    float scaleFactor = min( visualSize.x, visualSize.y );\n
-    vec3 originFlipY =  vec3(origin.x, -origin.y, 0.0);
-    vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
-    vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
-    return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
-  }\n
-
-  void main()\n
-  {\n
-    vec4 normalisedVertexPosition = ComputeVertexPosition();\n
-    vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
-    vertexPosition = uMvpMatrix * vertexPosition;\n
-
-    //Illumination in Model-View space - Transform attributes and uniforms\n
-    vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
-    vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
-
-    vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
-    mvLightPosition = uViewMatrix * mvLightPosition;\n
-    vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
-
-    float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
-    vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
-
-    gl_Position = vertexPosition;\n
-  }\n
-);
-
-//Fragment shader corresponding to the texture-less shader.
-const char* SIMPLE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  precision mediump float;\n
-  varying mediump vec3 vIllumination;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform lowp float preMultipliedAlpha;\n
-
-  void main()\n
-  {\n
-    gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a ) * vec4( mixColor, 1.0 );\n
-  }\n
-);
-
-//Diffuse and specular illumination shader with albedo texture. Texture is index 0.
-const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
-  attribute highp vec3 aPosition;\n
-  attribute highp vec2 aTexCoord;\n
-  attribute highp vec3 aNormal;\n
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec3 vIllumination;\n
-  varying mediump float vSpecular;\n
-  uniform mediump vec3 uSize;\n
-  uniform mediump mat4 uMvpMatrix;\n
-  uniform mediump mat4 uModelView;
-  uniform mediump mat4 uViewMatrix;\n
-  uniform mediump mat3 uNormalMatrix;
-  uniform mediump mat4 uObjectMatrix;\n
-  uniform mediump vec3 lightPosition;\n
-  uniform mediump vec2 uStageOffset;\n
-
-  //Visual size and offset
-  uniform mediump vec2 offset;\n
-  uniform mediump vec2 size;\n
-  uniform mediump vec4 offsetSizeMode;\n
-  uniform mediump vec2 origin;\n
-  uniform mediump vec2 anchorPoint;\n
-
-  vec4 ComputeVertexPosition()\n
-  {\n
-    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
-    float scaleFactor = min( visualSize.x, visualSize.y );\n
-    vec3 originFlipY =  vec3(origin.x, -origin.y, 0.0);
-    vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
-    vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
-    return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
-  }\n
-
-  void main()
-  {\n
-    vec4 normalisedVertexPosition = ComputeVertexPosition();\n
-    vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
-    vertexPosition = uMvpMatrix * vertexPosition;\n
-
-    //Illumination in Model-View space - Transform attributes and uniforms\n
-    vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
-    vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
-
-    vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
-    mvLightPosition = uViewMatrix * mvLightPosition;\n
-    vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
-
-    vec3 viewDirection = normalize( -mvVertexPosition.xyz );
-
-    float lightDiffuse = dot( vectorToLight, normal );\n
-    lightDiffuse = max( 0.0,lightDiffuse );\n
-    vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
-
-    vec3 reflectDirection = reflect( -vectorToLight, normal );
-    vSpecular = pow( max( dot( reflectDirection, viewDirection ), 0.0 ), 4.0 );
-
-    vTexCoord = aTexCoord;\n
-    gl_Position = vertexPosition;\n
-  }\n
-);
-
-//Fragment shader corresponding to the diffuse and specular illumination shader with albedo texture
-const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  precision mediump float;\n
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec3 vIllumination;\n
-  varying mediump float vSpecular;\n
-  uniform sampler2D sDiffuse;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform lowp float preMultipliedAlpha;\n
-
-  void main()\n
-  {\n
-    vec4 texture = texture2D( sDiffuse, vTexCoord );\n
-    vec4 visualMixColor = vec4( mixColor, 1.0 );\n
-    gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb * visualMixColor.rgb + vSpecular * 0.3, texture.a * uColor.a * visualMixColor.a );\n
-  }\n
-);
-
-//Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader.
-//Diffuse (albedo) texture is index 0, normal is 1, gloss is 2. They must be declared in this order.
-const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER(
-  attribute highp vec3 aPosition;\n
-  attribute highp vec2 aTexCoord;\n
-  attribute highp vec3 aNormal;\n
-  attribute highp vec3 aTangent;\n
-  attribute highp vec3 aBiNormal;\n
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec3 vLightDirection;\n
-  varying mediump vec3 vHalfVector;\n
-  uniform mediump vec3 uSize;\n
-  uniform mediump mat4 uMvpMatrix;\n
-  uniform mediump mat4 uModelView;
-  uniform mediump mat4 uViewMatrix;\n
-  uniform mediump mat3 uNormalMatrix;
-  uniform mediump mat4 uObjectMatrix;\n
-  uniform mediump vec3 lightPosition;\n
-  uniform mediump vec2 uStageOffset;\n
-
-  //Visual size and offset
-  uniform mediump vec2 offset;\n
-  uniform mediump vec2 size;\n
-  uniform mediump vec4 offsetSizeMode;\n
-  uniform mediump vec2 origin;\n
-  uniform mediump vec2 anchorPoint;\n
-
-  vec4 ComputeVertexPosition()\n
-  {\n
-    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
-    float scaleFactor = min( visualSize.x, visualSize.y );\n
-    vec3 originFlipY =  vec3(origin.x, -origin.y, 0.0);
-    vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
-    vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
-    return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
-  }\n
-
-  void main()
-  {\n
-    vec4 normalisedVertexPosition = ComputeVertexPosition();\n
-    vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
-    vertexPosition = uMvpMatrix * vertexPosition;\n
-
-    vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
-
-    vec3 tangent = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aTangent );
-    vec3 binormal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aBiNormal );
-    vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );
-
-    vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
-    mvLightPosition = uViewMatrix * mvLightPosition;\n
-    vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
-    vLightDirection.x = dot( vectorToLight, tangent );
-    vLightDirection.y = dot( vectorToLight, binormal );
-    vLightDirection.z = dot( vectorToLight, normal );
-
-    vec3 viewDirection = normalize( -mvVertexPosition.xyz );
-    vec3 halfVector = normalize( viewDirection + vectorToLight );
-    vHalfVector.x = dot( halfVector, tangent );
-    vHalfVector.y = dot( halfVector, binormal );
-    vHalfVector.z = dot( halfVector, normal );
-
-    vTexCoord = aTexCoord;\n
-    gl_Position = vertexPosition;\n
-  }\n
-);
-
-//Fragment shader corresponding to the shader that uses all textures (diffuse, normal and gloss maps)
-const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  precision mediump float;\n
-  varying mediump vec2 vTexCoord;\n
-  varying mediump vec3 vLightDirection;\n
-  varying mediump vec3 vHalfVector;\n
-  uniform sampler2D sDiffuse;\n
-  uniform sampler2D sNormal;\n
-  uniform sampler2D sGloss;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform lowp float preMultipliedAlpha;\n
-
-  void main()\n
-  {\n
-    vec4 texture = texture2D( sDiffuse, vTexCoord );\n
-    vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
-    vec4 glossMap = texture2D( sGloss, vTexCoord );\n
-    vec4 visualMixColor = vec4( mixColor, 1.0 );\n
-
-    float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n
-    lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
-
-    float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 )  ;
-
-    gl_FragColor = vec4( texture.rgb * uColor.rgb * visualMixColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a * visualMixColor.a );\n
-  }\n
-);
-
 } // unnamed namespace
 
 MeshVisualPtr MeshVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
@@ -564,7 +318,7 @@ void MeshVisual::InitializeRenderer()
 void MeshVisual::SupplyEmptyGeometry()
 {
   mGeometry = Geometry::New();
-  mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
+  mShader = Shader::New( SHADER_MESH_VISUAL_SIMPLE_SHADER_VERT, SHADER_MESH_VISUAL_SIMPLE_SHADER_FRAG );
   mImpl->mRenderer = Renderer::New( mGeometry, mShader );
 
   DALI_LOG_ERROR( "Initialisation error in mesh visual.\n" );
@@ -588,15 +342,15 @@ void MeshVisual::CreateShader()
 {
   if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
   {
-    mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER );
+    mShader = Shader::New( SHADER_MESH_VISUAL_NORMAL_MAP_SHADER_VERT, SHADER_MESH_VISUAL_NORMAL_MAP_SHADER_FRAG );
   }
   else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING )
   {
-    mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+    mShader = Shader::New( SHADER_MESH_VISUAL_SHADER_VERT, SHADER_MESH_VISUAL_SHADER_FRAG );
   }
   else //Textureless
   {
-    mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
+    mShader = Shader::New( SHADER_MESH_VISUAL_SIMPLE_SHADER_VERT, SHADER_MESH_VISUAL_SIMPLE_SHADER_FRAG );
   }
 
   UpdateShaderUniforms();