Merge "Shader compilation tool for dali-toolkit" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bloom-view / bloom-view-impl.cpp
index 14f179b..d54dcd3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
@@ -23,7 +23,7 @@
 #include <iomanip>
 #include <dali/public-api/animation/constraint.h>
 #include <dali/public-api/animation/constraints.h>
-#include <dali/public-api/common/stage.h>
+#include <dali/devel-api/common/stage.h>
 #include <dali/public-api/object/property-map.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/object/type-registry-helper.h>
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
 #include <dali-toolkit/devel-api/controls/bloom-view/bloom-view.h>
+#include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
 #include <dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.h>
 #include <dali-toolkit/internal/controls/control/control-renderers.h>
+#include <dali-toolkit/internal/controls/control/control-data-impl.h>
 
 namespace Dali
 {
@@ -84,54 +86,6 @@ const char* const BLOOM_SATURATION_PROPERTY_NAME = "uBloomSaturation";
 const char* const IMAGE_INTENSITY_PROPERTY_NAME = "uImageIntensity";
 const char* const IMAGE_SATURATION_PROPERTY_NAME = "uImageSaturation";
 
-///////////////////////////////////////////////////////
-//
-// Bloom shaders
-//
-
-const char* const BLOOM_EXTRACT_FRAGMENT_SOURCE =
-  "varying mediump vec2 vTexCoord;\n"
-  "uniform sampler2D sTexture;\n"
-  "uniform lowp vec4 uColor;\n"
-  "uniform mediump float uBloomThreshold;\n"
-  "uniform mediump float uRecipOneMinusBloomThreshold;\n"
-  "void main()\n"
-  "{\n"
-  "  mediump vec4 col;\n"
-  "  col = texture2D(sTexture, vTexCoord);\n"
-  "  col = (col - uBloomThreshold) * uRecipOneMinusBloomThreshold;\n" // remove intensities lower than the thresold and remap intensities above the threshold to [0..1]
-  "  gl_FragColor = clamp(col, 0.0, 1.0);\n"
-  "}\n";
-
-const char* const COMPOSITE_FRAGMENT_SOURCE =
-  "precision mediump float;\n"
-  "varying mediump vec2 vTexCoord;\n"
-  "uniform sampler2D sTexture;\n"
-  "uniform sampler2D sEffect;\n"
-  "uniform lowp vec4 uColor;\n"
-  "uniform float uBloomIntensity;\n"
-  "uniform float uImageIntensity;\n"
-  "uniform float uBloomSaturation;\n"
-  "uniform float uImageSaturation;\n"
-
-  "vec4 ChangeSaturation(vec4 col, float sat)\n"
-  "{\n"
-  "  float grey = dot(col.rgb, vec3(0.3, 0.6, 0.1));\n"
-  "  return mix(vec4(grey, grey, grey, 1.0), col, sat);\n"
-  "}\n"
-
-  "void main()\n"
-  "{\n"
-  "  mediump vec4 image;\n"
-  "  mediump vec4 bloom;\n"
-  "  image = texture2D(sTexture, vTexCoord);\n"
-  "  bloom = texture2D(sEffect, vTexCoord);\n"
-  "  image = ChangeSaturation(image, uImageSaturation) * uImageIntensity;\n"
-  "  bloom = ChangeSaturation(bloom, uBloomSaturation) * uBloomIntensity;\n"
-  "  image *= 1.0 - clamp(bloom, 0.0, 1.0);\n" // darken base where bloom is strong, to prevent excessive burn-out of result
-  "  gl_FragColor = image + bloom;\n"
-  "}\n";
-
 } // namespace
 
 
@@ -222,40 +176,40 @@ Toolkit::BloomView BloomView::New(const unsigned int blurNumSamples, const float
 void BloomView::OnInitialize()
 {
   // root actor to parent all user added actors, needed to allow us to set that subtree as exclusive for our child render task
-  mChildrenRoot.SetParentOrigin( ParentOrigin::CENTER );
-  mInternalRoot.SetParentOrigin( ParentOrigin::CENTER );
+  mChildrenRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+  mInternalRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 
   //////////////////////////////////////////////////////
   // Create actors
 
   // Create an image view for rendering from the scene texture to the bloom texture
   mBloomExtractActor = Actor::New();
-  mBloomExtractActor.SetParentOrigin( ParentOrigin::CENTER );
+  mBloomExtractActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 
   // Create an image view for compositing the result (scene and bloom textures) to output
   mCompositeActor = Actor::New();
-  mCompositeActor.SetParentOrigin( ParentOrigin::CENTER );
+  mCompositeActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 
   // Create an image view for holding final result, i.e. the blurred image. This will get rendered to screen later, via default / user render task
   mTargetActor = Actor::New();
-  mTargetActor.SetParentOrigin( ParentOrigin::CENTER );
+  mTargetActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 
   // Create the Gaussian Blur object + render tasks
   // Note that we use mBloomExtractTarget as the source image and also re-use this as the gaussian blur final render target. This saves the gaussian blur code from creating it
   // render targets etc internally, so we make better use of resources
   // Note, this also internally creates the render tasks used by the Gaussian blur, this must occur after the bloom extraction and before the compositing
   mGaussianBlurView = Dali::Toolkit::GaussianBlurView::New(mBlurNumSamples, mBlurBellCurveWidth, mPixelFormat, mDownsampleWidthScale, mDownsampleHeightScale, true);
-  mGaussianBlurView.SetParentOrigin( ParentOrigin::CENTER );
+  mGaussianBlurView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 
 
   //////////////////////////////////////////////////////
   // Create cameras for the renders corresponding to the (potentially downsampled) render targets' size
   mRenderDownsampledCamera = CameraActor::New();
-  mRenderDownsampledCamera.SetParentOrigin(ParentOrigin::CENTER);
+  mRenderDownsampledCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
   mRenderDownsampledCamera.SetInvertYAxis( true );
 
   mRenderFullSizeCamera = CameraActor::New();
-  mRenderFullSizeCamera.SetParentOrigin(ParentOrigin::CENTER);
+  mRenderFullSizeCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
   mRenderFullSizeCamera.SetInvertYAxis( true );
 
 
@@ -272,14 +226,19 @@ void BloomView::OnInitialize()
 
   // bind properties for / set shader constants to defaults
   SetupProperties();
+
+  DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
+    return std::unique_ptr< Dali::Accessibility::Accessible >(
+      new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::ANIMATION ) );
+  } );
 }
 
 void BloomView::OnSizeSet(const Vector3& targetSize)
 {
   mTargetSize = Vector2(targetSize);
-  mChildrenRoot.SetSize(targetSize);
-  mCompositeActor.SetSize(targetSize);
-  mTargetActor.SetSize(targetSize);
+  mChildrenRoot.SetProperty( Actor::Property::SIZE, targetSize);
+  mCompositeActor.SetProperty( Actor::Property::SIZE, targetSize);
+  mTargetActor.SetProperty( Actor::Property::SIZE, targetSize);
 
   // Children render camera must move when GaussianBlurView object is
   // resized. This is since we cannot change render target size - so we need
@@ -289,7 +248,7 @@ void BloomView::OnSizeSet(const Vector3& targetSize)
   // this is the trade off for not being able to modify render target size
   // Change camera z position based on GaussianBlurView actor height
   float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f);
-  mRenderFullSizeCamera.SetZ( mTargetSize.height * cameraPosConstraintScale);
+  mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION_Z,  mTargetSize.height * cameraPosConstraintScale);
 
   // if we have already activated the blur, need to update render target sizes now to reflect the new size of this actor
   if(mActivated)
@@ -339,7 +298,7 @@ void BloomView::AllocateResources()
     mRenderDownsampledCamera.SetAspectRatio(mDownsampledWidth / mDownsampledHeight);
     mRenderDownsampledCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
 
-    mRenderDownsampledCamera.SetPosition(0.0f, 0.0f, ((mDownsampledHeight * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
+    mRenderDownsampledCamera.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, ( ( mDownsampledHeight * 0.5f ) / tanf( ARBITRARY_FIELD_OF_VIEW * 0.5f ) ) ) );
 
     // Create and place a camera for the children render, corresponding to its render target size
     mRenderFullSizeCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
@@ -348,14 +307,14 @@ void BloomView::AllocateResources()
     mRenderFullSizeCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
 
     float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f);
-    mRenderFullSizeCamera.SetPosition(0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale);
+    mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale ) );
 
     //////////////////////////////////////////////////////
     // Pass size change onto GaussianBlurView, so it matches
-    mGaussianBlurView.SetSize(mTargetSize);
+    mGaussianBlurView.SetProperty( Actor::Property::SIZE, mTargetSize );
     GetImpl(mGaussianBlurView).AllocateResources();
 
-    mGaussianBlurView.SetVisible( true );
+    mGaussianBlurView.SetProperty( Actor::Property::VISIBLE, true );
 
     //////////////////////////////////////////////////////
     // Create render targets
@@ -380,16 +339,16 @@ void BloomView::AllocateResources()
     //////////////////////////////////////////////////////
     // Point actors and render tasks at new render targets
 
-    Renderer bloomRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, BLOOM_EXTRACT_FRAGMENT_SOURCE );
+    Renderer bloomRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, SHADER_BLOOM_VIEW_EXTRACT_SHADER_FRAG );
     SetRendererTexture( bloomRenderer, mRenderTargetForRenderingChildren );
     mBloomExtractActor.AddRenderer( bloomRenderer );
-    mBloomExtractActor.SetSize( mDownsampledWidth, mDownsampledHeight ); // size needs to match render target
+    mBloomExtractActor.SetProperty( Actor::Property::SIZE, Vector2( mDownsampledWidth, mDownsampledHeight ) ); // size needs to match render target
 
     // set GaussianBlurView to blur our extracted bloom
     mGaussianBlurView.SetUserImageAndOutputRenderTarget( mBloomExtractTarget.GetColorTexture(), blurExtractTarget );
 
     // use the completed blur in the first buffer and composite with the original child actors render
-    Renderer compositeRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, COMPOSITE_FRAGMENT_SOURCE );
+    Renderer compositeRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, SHADER_BLOOM_VIEW_COMPOSITE_SHADER_FRAG );
     SetRendererTexture( compositeRenderer, mRenderTargetForRenderingChildren );
     TextureSet textureSet = compositeRenderer.GetTextures();
     textureSet.SetTexture( 0u, mRenderTargetForRenderingChildren.GetColorTexture() );
@@ -473,7 +432,7 @@ void BloomView::Deactivate()
   mTargetActor.RemoveRenderer( 0u );
   mCompositeActor.RemoveRenderer( 0u );
 
-  mGaussianBlurView.SetVisible( false );
+  mGaussianBlurView.SetProperty( Actor::Property::VISIBLE, false );
 
   mActivated = false;
 }