From: Richard Huang Date: Thu, 21 Jan 2021 17:58:56 +0000 (+0000) Subject: Convert shaders in dali-demo to use shader compilation tool X-Git-Tag: dali_2.0.11~7 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-demo.git;a=commitdiff_plain;h=422d91612187d1a575b3ed00f7d33cb2d19e02ed Convert shaders in dali-demo to use shader compilation tool Change-Id: Ieee1546e6f20ad0f5fc29c239d7571e463bdcf6f --- diff --git a/build/tizen/examples/CMakeLists.txt b/build/tizen/examples/CMakeLists.txt index ed65080..6476710 100644 --- a/build/tizen/examples/CMakeLists.txt +++ b/build/tizen/examples/CMakeLists.txt @@ -21,6 +21,15 @@ IF (NOT "${ENABLE_SCENE_LOADER}" ) ENDIF() FOREACH(EXAMPLE ${SUBDIRS}) + # Generate source files for shaders + SET(SHADER_SOURCE_DIR "${EXAMPLES_SRC_DIR}/${EXAMPLE}/shaders/") + SET(SHADER_GENERATED_DIR "${EXAMPLES_SRC_DIR}/${EXAMPLE}/generated/") + SET(PARENT_CMAKE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../") + IF (EXISTS ${SHADER_SOURCE_DIR}) + EXECUTE_PROCESS( COMMAND bash -c "${PARENT_CMAKE_SOURCE_DIR}/shader-generator.sh ${SHADER_SOURCE_DIR} ${SHADER_GENERATED_DIR} --skip") + SET_PROPERTY( DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES + "${SHADER_GENERATED_DIR}" ) + ENDIF() FILE(GLOB SRCS "${EXAMPLES_SRC_DIR}/${EXAMPLE}/*.cpp") SET(SRCS ${SRCS} "${ROOT_SRC_DIR}/shared/resources-location.cpp") IF(SHARED) diff --git a/build/tizen/shader-generator.sh b/build/tizen/shader-generator.sh new file mode 100755 index 0000000..5426fd2 --- /dev/null +++ b/build/tizen/shader-generator.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +# Set Colors +Red='\033[0;31m' # Red +Yellow='\033[0;33m' # Yellow +Blue='\033[0;34m' # Blue +Clear='\033[0m' # Text Reset + +######################################################################################### + +Usage() +{ + echo -e "${Yellow}Usage: $(basename ${BASH_SOURCE[0]}) [indir] [outdir] [OPTIONS]" + echo + echo -e " ${Blue}Mandatory parameters:${Clear}" + echo -e " indir The input directory where the original shader files are located" + echo -e " outdir The output directory where the header files for the shaders should be generated" + echo -e " ${Red}NOTE: All the above parameters must be provided${Clear}" + echo + echo -e " ${Blue}Options:${Clear}" + echo -e " -s|--skip Skip the generation of header and cpp files that include all the generated shader header files" + echo -e " -h|--help Help" +} + +if [ "$1" = "-h" ] || [ "$1" = "--help" ] +then + Usage + exit 0 +elif [ "$#" -lt 2 ] +then + echo -e "${Red}ERROR:${Clear} Mandatory parameters are not provided" + echo + Usage + exit 1 +fi + +######################################################################################### + +indir=$1 +outdir=$2 +skip="" + +for option in $* +do + if [ "$option" = "-s" ] || [ "$option" = "--skip" ] + then + skip="--skip" + continue + elif [[ $option == -* ]] + then + echo -e "${Red}Invalid Option: ${Blue}$option${Clear}" + echo + Usage + exit 1 + fi +done + +######################################################################################### + +mkdir -p $outdir + +if [ ! -e $indir ] ; then + echo "Error: The folder \""$indir"\" does not exist!" + exit 1 +fi + +cd $indir +all_shaders=$(ls -1 *.{vert,frag,def} 2>/dev/null) +cd $OLDPWD + +# Generate one header file per shader which is defined as a const std::string_view +for name in $all_shaders ; do + echo "Generating header files for $name..." + varname=$(echo "SHADER_$name" | tr [a-z] [A-Z] | sed -e 's/-/_/g;s/\./_/g;') + + newname=$(echo ${name} | sed -e 's/\./-/;')".h" + echo Writing $newname + + shader_fullpath=$(echo ${indir})$name + + header_name="${varname}_GEN_H" + echo "const std::string_view" "$varname""{" > $outdir/$newname + cat $shader_fullpath | sed -e 's/^..*$/"&\\n"/' >> $outdir/$newname + echo "};" >> $outdir/$newname +done + +if [ "$skip" != "--skip" ]; then + # Generate one cpp file that includes all the previously generated string_views for shaders + echo "Generating cpp file..." + echo -e "#include \"../builtin-shader-extern-gen.h\"\n" > $outdir/builtin-shader-gen.cpp + + varnames= + for name in $all_shaders ; do + varname=$(echo "SHADER_$name" | tr [a-z] [A-Z] | sed -e 's/-/_/g;s/\./_/g;') + newname=$(echo ${name} | sed -e 's/\./-/;')".h" + varnames="${varnames} $varname" + echo "#include \"$newname\"" >> $outdir/builtin-shader-gen.cpp + done + + # Generate one header file that defines all the shader string_views as extern variables + echo "Generating extern header file ( for external use )..." + echo "#ifndef GRAPHICS_BUILTIN_SHADER_EXTERN_GEN_H" > $outdir/../builtin-shader-extern-gen.h + echo -e "#define GRAPHICS_BUILTIN_SHADER_EXTERN_GEN_H\n" >> $outdir/../builtin-shader-extern-gen.h + + echo "#include " >> $outdir/../builtin-shader-extern-gen.h + echo "" >> $outdir/../builtin-shader-extern-gen.h + + for name in $all_shaders ; do + varname=$(echo "SHADER_$name" | tr [a-z] [A-Z] | sed -e 's/-/_/g;s/\./_/g;') + newname=$(echo ${name} | sed -e 's/\./-/;')".h" + echo "extern const std::string_view $varname;" >> $outdir/../builtin-shader-extern-gen.h + done + cat >> $outdir/../builtin-shader-extern-gen.h << EOF + +#endif // GRAPHICS_BUILTIN_SHADER_EXTERN_GEN_H +EOF +fi + diff --git a/examples/animated-shapes/animated-shapes-example.cpp b/examples/animated-shapes/animated-shapes-example.cpp index 04c9bbd..508a75f 100644 --- a/examples/animated-shapes/animated-shapes-example.cpp +++ b/examples/animated-shapes/animated-shapes-example.cpp @@ -18,6 +18,8 @@ #include #include #include "shared/view.h" +#include "generated/animated-shapes-vert.h" +#include "generated/animated-shapes-frag.h" #include @@ -28,50 +30,16 @@ namespace { const char* APPLICATION_TITLE("Animated Shapes"); -// clang-format off -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER -( - attribute mediump vec3 aCoefficient; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uPosition[MAX_POINT_COUNT]; - varying lowp vec2 vCoefficient; - void main() - { - int vertexId = int(aCoefficient.z); - gl_Position = uMvpMatrix * vec4(uPosition[vertexId], 1.0); - - vCoefficient = aCoefficient.xy; - } -); - -// Fragment shader. -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER -( - uniform lowp vec4 uColor; - varying lowp vec2 vCoefficient; - void main() - { - lowp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y); - lowp float Cdx = dFdx(C); - lowp float Cdy = dFdy(C); - - lowp float distance = float(C / sqrt(Cdx*Cdx + Cdy*Cdy)); - lowp float alpha = 0.5 - distance; - gl_FragColor = vec4( uColor.rgb, uColor.a * alpha ); - } -); -// clang-format on - Shader CreateShader(unsigned int pointCount) { std::ostringstream vertexShader; vertexShader << "#define MAX_POINT_COUNT " << pointCount << "\n" - << VERTEX_SHADER; + << SHADER_ANIMATED_SHAPES_VERT; std::ostringstream fragmentShader; fragmentShader << "#extension GL_OES_standard_derivatives : enable " << "\n" - << FRAGMENT_SHADER; + << SHADER_ANIMATED_SHAPES_FRAG; Shader shader = Shader::New(vertexShader.str(), fragmentShader.str()); for(unsigned int i(0); i < pointCount; ++i) diff --git a/examples/animated-shapes/shaders/animated-shapes.frag b/examples/animated-shapes/shaders/animated-shapes.frag new file mode 100644 index 0000000..6127684 --- /dev/null +++ b/examples/animated-shapes/shaders/animated-shapes.frag @@ -0,0 +1,13 @@ +uniform lowp vec4 uColor; +varying lowp vec2 vCoefficient; + +void main() +{ + lowp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y); + lowp float Cdx = dFdx(C); + lowp float Cdy = dFdy(C); + + lowp float distance = float(C / sqrt(Cdx*Cdx + Cdy*Cdy)); + lowp float alpha = 0.5 - distance; + gl_FragColor = vec4( uColor.rgb, uColor.a * alpha ); +} diff --git a/examples/animated-shapes/shaders/animated-shapes.vert b/examples/animated-shapes/shaders/animated-shapes.vert new file mode 100644 index 0000000..06e6523 --- /dev/null +++ b/examples/animated-shapes/shaders/animated-shapes.vert @@ -0,0 +1,12 @@ +attribute mediump vec3 aCoefficient; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uPosition[MAX_POINT_COUNT]; +varying lowp vec2 vCoefficient; + +void main() +{ + int vertexId = int(aCoefficient.z); + gl_Position = uMvpMatrix * vec4(uPosition[vertexId], 1.0); + + vCoefficient = aCoefficient.xy; +} diff --git a/examples/benchmark/benchmark.cpp b/examples/benchmark/benchmark.cpp index c437dc2..4dc1e0a 100644 --- a/examples/benchmark/benchmark.cpp +++ b/examples/benchmark/benchmark.cpp @@ -20,6 +20,8 @@ // INTERNAL INCLUDES #include "shared/utility.h" +#include "generated/benchmark-vert.h" +#include "generated/benchmark-frag.h" using namespace Dali; using namespace Dali::Toolkit; @@ -149,33 +151,6 @@ struct VertexWithTexture Vector2 texCoord; }; -// clang-format off -const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexCoord;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - varying mediump vec2 vTexCoord;\n - void main()\n - {\n - vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n - gl_Position = uMvpMatrix * position;\n - vTexCoord = aTexCoord;\n - }\n -); - -const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n - }\n -); -// clang-format on - bool gUseMesh(false); bool gNinePatch(false); unsigned int gRowsPerPage(25); @@ -282,7 +257,7 @@ public: //Create all the renderers std::vector renderers(numImages); - Shader shader = Shader::New(VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE); + Shader shader = Shader::New(SHADER_BENCHMARK_VERT, SHADER_BENCHMARK_FRAG); Geometry geometry = DemoHelper::CreateTexturedQuad(); for(unsigned int i(0); i < numImages; ++i) { diff --git a/examples/benchmark/shaders/benchmark.frag b/examples/benchmark/shaders/benchmark.frag new file mode 100644 index 0000000..f9eba48 --- /dev/null +++ b/examples/benchmark/shaders/benchmark.frag @@ -0,0 +1,8 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} diff --git a/examples/benchmark/shaders/benchmark.vert b/examples/benchmark/shaders/benchmark.vert new file mode 100644 index 0000000..3cb2121 --- /dev/null +++ b/examples/benchmark/shaders/benchmark.vert @@ -0,0 +1,12 @@ +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; +} diff --git a/examples/bezier-curve/bezier-curve-example.cpp b/examples/bezier-curve/bezier-curve-example.cpp index b8765c3..d128218 100644 --- a/examples/bezier-curve/bezier-curve-example.cpp +++ b/examples/bezier-curve/bezier-curve-example.cpp @@ -19,6 +19,8 @@ #include #include #include "shared/view.h" +#include "generated/bezier-curve-vert.h" +#include "generated/bezier-curve-frag.h" #include @@ -42,28 +44,6 @@ const float ANIM_RIGHT_FACTOR(0.8f); const int AXIS_LABEL_POINT_SIZE(7); const float AXIS_LINE_SIZE(1.0f); -// clang-format off -const char* CURVE_VERTEX_SHADER = DALI_COMPOSE_SHADER - ( - attribute mediump vec2 aPosition; - uniform mediump mat4 uMvpMatrix; - uniform vec3 uSize; - void main() - { - gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0); - } - ); - -const char* CURVE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER - ( - uniform lowp vec4 uColor; - void main() - { - gl_FragColor = vec4(0.0,0.0,0.0,1.0); - } - ); -// clang-format on - inline float Clamp(float v, float min, float max) { if(v < min) return min; @@ -294,7 +274,7 @@ public: mCurve.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS); mCurve.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); - Shader shader = Shader::New(CURVE_VERTEX_SHADER, CURVE_FRAGMENT_SHADER); + Shader shader = Shader::New(SHADER_BEZIER_CURVE_VERT, SHADER_BEZIER_CURVE_FRAG); Property::Map curveVertexFormat; curveVertexFormat["aPosition"] = Property::VECTOR2; @@ -338,7 +318,7 @@ public: line.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS); line.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); - Shader shader = Shader::New(CURVE_VERTEX_SHADER, CURVE_FRAGMENT_SHADER); + Shader shader = Shader::New(SHADER_BEZIER_CURVE_VERT, SHADER_BEZIER_CURVE_FRAG); Geometry geometry = Geometry::New(); geometry.AddVertexBuffer(vertexBuffer); geometry.SetType(Geometry::LINE_STRIP); diff --git a/examples/bezier-curve/shaders/bezier-curve.frag b/examples/bezier-curve/shaders/bezier-curve.frag new file mode 100644 index 0000000..ef6b09e --- /dev/null +++ b/examples/bezier-curve/shaders/bezier-curve.frag @@ -0,0 +1,6 @@ +uniform lowp vec4 uColor; + +void main() +{ + gl_FragColor = vec4(0.0,0.0,0.0,1.0); +} diff --git a/examples/bezier-curve/shaders/bezier-curve.vert b/examples/bezier-curve/shaders/bezier-curve.vert new file mode 100644 index 0000000..a108a41 --- /dev/null +++ b/examples/bezier-curve/shaders/bezier-curve.vert @@ -0,0 +1,8 @@ +attribute mediump vec2 aPosition; +uniform mediump mat4 uMvpMatrix; +uniform vec3 uSize; + +void main() +{ + gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0); +} diff --git a/examples/color-transition/color-transition-controller.cpp b/examples/color-transition/color-transition-controller.cpp index d9cb208..8963740 100644 --- a/examples/color-transition/color-transition-controller.cpp +++ b/examples/color-transition/color-transition-controller.cpp @@ -18,6 +18,8 @@ #include "utils.h" #include "dali/dali.h" #include "dali-toolkit/dali-toolkit.h" +#include "generated/color-transition-controller-composite-vert.h" +#include "generated/color-transition-controller-composite-frag.h" using namespace Dali; using namespace Dali::Toolkit; @@ -27,65 +29,6 @@ namespace const Vector4 BG_COLOR = Vector4(0.f, 0.f, 0.f, 0.f); -const char* const COMPOSITE_VSH = DALI_COMPOSE_SHADER( -precision mediump float; - -// -uniform mat4 uMvpMatrix; -uniform vec3 uSize; -// - -uniform float uFlow; -uniform vec4 uUvTransform; // rotation, scale (initial, target)) - -attribute vec2 aPosition; - -varying vec2 vUv; -varying vec2 vUvFlow; - -void main() -{ - vec4 position = uMvpMatrix * vec4(aPosition * uSize.xy, 0., 1.); - - gl_Position = position; - - vec2 uv = position.xy / (position.ww * 2.); - vUv = uv + vec2(.5); - - float alpha = uFlow * .5 + .5; - vec2 uvRotationScale = mix(uUvTransform.xy, uUvTransform.zw, alpha); - float c = cos(uvRotationScale.x) * uvRotationScale.y; - float s = sin(uvRotationScale.x) * uvRotationScale.y; - vec4 uvMatrix = vec4(c, -s, s, c); - uv = vec2(dot(uvMatrix.xy, uv), dot(uvMatrix.zw, uv)); - - // N.B. +y is down which is well aligned with the inverted y of the off-screen render, - // however we need to flip the y of the uvs for the flow map. - vUvFlow = vec2(uv.x + .5, .5 - uv.y); -}); - -const char* const COMPOSITE_FSH = DALI_COMPOSE_SHADER( -precision mediump float; - -const float kStepsilon = 1e-2; - -uniform sampler2D sColor; -uniform sampler2D sFlowMap; - -uniform float uFlow; -uniform vec3 uRgb[2]; - -varying vec2 vUv; -varying vec2 vUvFlow; - -void main() -{ - vec4 colorAlpha = texture2D(sColor, vUv); - float flow = smoothstep(.5 - kStepsilon, .5 + kStepsilon, clamp(uFlow + texture2D(sFlowMap, vUvFlow).r, 0., 1.)); - - gl_FragColor = vec4(mix(colorAlpha.rgb, mix(uRgb[0], uRgb[1], flow), colorAlpha.a), 1.); -}); - } // nonamespace ColorTransitionController::ColorTransitionController(WeakHandle window, Actor content, RenderTaskList tasks, Vector3 initialColor) @@ -129,7 +72,7 @@ ColorTransitionController::ColorTransitionController(WeakHandle flowSampler.SetWrapMode(WrapMode::REPEAT, WrapMode::REPEAT); tsComposite.SetSampler(1, flowSampler); - auto shdComposite = Shader::New(COMPOSITE_VSH, COMPOSITE_FSH); + auto shdComposite = Shader::New(SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_VERT, SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_FRAG); auto compositeRenderer = CreateRenderer(tsComposite, geomComposite, shdComposite); composite.AddRenderer(compositeRenderer); diff --git a/examples/color-transition/shaders/color-transition-controller-composite.frag b/examples/color-transition/shaders/color-transition-controller-composite.frag new file mode 100644 index 0000000..49c79ad --- /dev/null +++ b/examples/color-transition/shaders/color-transition-controller-composite.frag @@ -0,0 +1,20 @@ +precision mediump float; + +const float kStepsilon = 1e-2; + +uniform sampler2D sColor; +uniform sampler2D sFlowMap; + +uniform float uFlow; +uniform vec3 uRgb[2]; + +varying vec2 vUv; +varying vec2 vUvFlow; + +void main() +{ + vec4 colorAlpha = texture2D(sColor, vUv); + float flow = smoothstep(.5 - kStepsilon, .5 + kStepsilon, clamp(uFlow + texture2D(sFlowMap, vUvFlow).r, 0., 1.)); + + gl_FragColor = vec4(mix(colorAlpha.rgb, mix(uRgb[0], uRgb[1], flow), colorAlpha.a), 1.); +} diff --git a/examples/color-transition/shaders/color-transition-controller-composite.vert b/examples/color-transition/shaders/color-transition-controller-composite.vert new file mode 100644 index 0000000..dc2b8b0 --- /dev/null +++ b/examples/color-transition/shaders/color-transition-controller-composite.vert @@ -0,0 +1,35 @@ +precision mediump float; + +// +uniform mat4 uMvpMatrix; +uniform vec3 uSize; +// + +uniform float uFlow; +uniform vec4 uUvTransform; // rotation, scale (initial, target)) + +attribute vec2 aPosition; + +varying vec2 vUv; +varying vec2 vUvFlow; + +void main() +{ + vec4 position = uMvpMatrix * vec4(aPosition * uSize.xy, 0., 1.); + + gl_Position = position; + + vec2 uv = position.xy / (position.ww * 2.); + vUv = uv + vec2(.5); + + float alpha = uFlow * .5 + .5; + vec2 uvRotationScale = mix(uUvTransform.xy, uUvTransform.zw, alpha); + float c = cos(uvRotationScale.x) * uvRotationScale.y; + float s = sin(uvRotationScale.x) * uvRotationScale.y; + vec4 uvMatrix = vec4(c, -s, s, c); + uv = vec2(dot(uvMatrix.xy, uv), dot(uvMatrix.zw, uv)); + + // N.B. +y is down which is well aligned with the inverted y of the off-screen render, + // however we need to flip the y of the uvs for the flow map. + vUvFlow = vec2(uv.x + .5, .5 - uv.y); +} diff --git a/examples/compressed-texture-formats/compressed-texture-formats-example.cpp b/examples/compressed-texture-formats/compressed-texture-formats-example.cpp index 1749a63..28e841b 100644 --- a/examples/compressed-texture-formats/compressed-texture-formats-example.cpp +++ b/examples/compressed-texture-formats/compressed-texture-formats-example.cpp @@ -22,6 +22,8 @@ // INTERNAL INCLUDES #include "shared/utility.h" +#include "generated/compressed-texture-formats-example-vert.h" +#include "generated/compressed-texture-formats-example-frag.h" using namespace Dali; using Dali::Toolkit::TextLabel; @@ -32,33 +34,6 @@ const char* IMAGE_FILENAME_ETC = DEMO_IMAGE_DIR "tx-etc1.ktx"; const char* IMAGE_FILENAME_ASTC_LINEAR = DEMO_IMAGE_DIR "tx-astc-4x4-linear.ktx"; const char* IMAGE_FILENAME_ASTC_LINEAR_NATIVE = DEMO_IMAGE_DIR "tx-astc-4x4-linear-native.astc"; -// clang-format off -static const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexCoord;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - varying mediump vec2 vTexCoord;\n - void main()\n - {\n - vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n - gl_Position = uMvpMatrix * position;\n - vTexCoord = aTexCoord;\n - }\n -); - -static const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n - }\n -); -// clang-format on - /** * @brief Create a renderer to render an image and adds it to an actor * @param[in] imagePath The path where the image file is located @@ -148,7 +123,7 @@ public: //Create the geometry and the shader renderers will use Geometry geometry = DemoHelper::CreateTexturedQuad(); - Shader shader = Shader::New(VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE); + Shader shader = Shader::New(SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_VERT, SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_FRAG); // Add images. Actor actor = Actor::New(); diff --git a/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.frag b/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.frag new file mode 100644 index 0000000..f9eba48 --- /dev/null +++ b/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.frag @@ -0,0 +1,8 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} diff --git a/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.vert b/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.vert new file mode 100644 index 0000000..3cb2121 --- /dev/null +++ b/examples/compressed-texture-formats/shaders/compressed-texture-formats-example.vert @@ -0,0 +1,12 @@ +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; +} diff --git a/examples/contact-cards/clipped-image.cpp b/examples/contact-cards/clipped-image.cpp index 0de8617..539c852 100644 --- a/examples/contact-cards/clipped-image.cpp +++ b/examples/contact-cards/clipped-image.cpp @@ -21,6 +21,10 @@ // EXTERNAL INCLUDES #include +// INTERNAL INCLUDES +#include "generated/clipped-image-vert.h" +#include "generated/clipped-image-frag.h" + namespace ClippedImage { using namespace Dali; @@ -31,39 +35,6 @@ namespace const char* const DELTA_PROPERTY_NAME("uDelta"); ///< Name of uniform used to mix the Circle and Quad geometries. /** - * @brief This vertex-shader mixes in the quad and circle geometry depending on the value of uDelta. - * - * uDelta is used to mix in the Circle and the Quad positions. - * If uDelta is 0.0f, then the circle position is adopted and if it is 1.0f, then the quad position is adopted. - */ -// clang-format off -const char * VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPositionCircle;\n - attribute mediump vec2 aPositionQuad;\n - uniform mediump float uDelta; - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - \n - void main()\n - {\n - mediump vec4 vertexPosition = vec4(mix(aPositionCircle,aPositionQuad,uDelta), 0.0, 1.0);\n - vertexPosition.xyz *= uSize;\n - gl_Position = uMvpMatrix * vertexPosition;\n - }\n -); - -/** - * @brief This fragment-shader does not output anything. It's for a control which is just going to clip as specified in the vertex shader. - */ -const char * FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - void main()\n - {\n - gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);\n - }\n -); -// clang-format on - -/** * @brief Creates the shader required for the clipped image * @return A reference to a static handle to a shader object (only created when first called). */ @@ -77,7 +48,7 @@ Shader& CreateShader() if(!shader) { - shader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + shader = Shader::New(SHADER_CLIPPED_IMAGE_VERT, SHADER_CLIPPED_IMAGE_FRAG); } return shader; diff --git a/examples/contact-cards/shaders/clipped-image.frag b/examples/contact-cards/shaders/clipped-image.frag new file mode 100644 index 0000000..bde9850 --- /dev/null +++ b/examples/contact-cards/shaders/clipped-image.frag @@ -0,0 +1,7 @@ +// This fragment-shader does not output anything. +// It's for a control which is just going to clip as specified in the vertex shader. + +void main() +{ + gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); +} diff --git a/examples/contact-cards/shaders/clipped-image.vert b/examples/contact-cards/shaders/clipped-image.vert new file mode 100644 index 0000000..cfe84bd --- /dev/null +++ b/examples/contact-cards/shaders/clipped-image.vert @@ -0,0 +1,17 @@ +// This vertex-shader mixes in the quad and circle geometry depending on the value of uDelta. +// +// uDelta is used to mix in the Circle and the Quad positions. +// If uDelta is 0.0f, then the circle position is adopted and if it is 1.0f, then the quad position is adopted. + +attribute mediump vec2 aPositionCircle; +attribute mediump vec2 aPositionQuad; +uniform mediump float uDelta; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; + +void main() +{ + mediump vec4 vertexPosition = vec4(mix(aPositionCircle,aPositionQuad,uDelta), 0.0, 1.0); + vertexPosition.xyz *= uSize; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/deferred-shading/deferred-shading.cpp b/examples/deferred-shading/deferred-shading.cpp index 02d6ef4..28cf835 100644 --- a/examples/deferred-shading/deferred-shading.cpp +++ b/examples/deferred-shading/deferred-shading.cpp @@ -21,6 +21,11 @@ #include "dali/public-api/actors/actor.h" #include "dali/public-api/rendering/renderer.h" +#include "generated/deferred-shading-mainpass-vert.h" +#include "generated/deferred-shading-mainpass-frag.h" +#include "generated/deferred-shading-prepass-vert.h" +#include "generated/deferred-shading-prepass-frag.h" + using namespace Dali; namespace @@ -38,181 +43,6 @@ namespace #define DEFINE_MAX_LIGHTS "const int kMaxLights = " QUOTE(MAX_LIGHTS) ";" -#define DEFINE(x) "#define " DALI_COMPOSE_SHADER(x) DALI_COMPOSE_SHADER(\n) - -// clang-format off - -//============================================================================= -// PRE-PASS -//============================================================================= -const char* const PREPASS_VSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float;) - DALI_COMPOSE_SHADER( - -// DALI uniforms -uniform mat4 uMvpMatrix; -uniform mat3 uNormalMatrix; -uniform vec3 uSize; - -uniform vec3 uDepth_InvDepth_Near;\n) - DEFINE(DEPTH uDepth_InvDepth_Near.x) - DEFINE(INV_DEPTH uDepth_InvDepth_Near.y) - DEFINE(NEAR uDepth_InvDepth_Near.z) - DALI_COMPOSE_SHADER( - -in vec3 aPosition; -in vec3 aNormal; - -out vec4 vPosition; -out vec3 vNormal; - -vec4 Map(vec4 v) // projection space -> texture -{ - return vec4(v.xyz / (2.f * v.w) + vec3(.5f), (v.w - NEAR) * INV_DEPTH); -} - -void main() -{ - vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); - vPosition = Map(position); - gl_Position = position; - - vNormal = normalize(uNormalMatrix * aNormal); -}); - -//============================================================================= -const char* const PREPASS_FSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float; - -// DALI uniform -uniform vec4 uColor; - -in vec4 vPosition; -in vec3 vNormal; - -// These are our outputs. -layout(location = 0) out vec3 oNormal; -layout(location = 1) out vec4 oPosition; -layout(location = 2) out vec3 oColor; - -void main() -{ - oColor = uColor.rgb; - oPosition = vPosition; - oNormal = normalize(vNormal) * .5f + .5f; -}); - -//============================================================================= -// MAIN (LIGHTING) PASS -//============================================================================= -const char* const MAINPASS_VSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float; - -// DALI uniforms -uniform mat4 uMvpMatrix; -uniform vec3 uSize; - -in vec3 aPosition; -in vec2 aTexCoord; - -out vec2 vUv; - -void main() -{ - vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); - vUv = aTexCoord; - - gl_Position = position; -}); - -//============================================================================= -const char* const MAINPASS_FSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float;\n) - DEFINE_MAX_LIGHTS - DALI_COMPOSE_SHADER( - -const float kAttenuationConst = .05f; -const float kAttenuationLinear = .1f; -const float kAttenuationQuadratic = .15f; - -// G-buffer -uniform sampler2D uTextureNormal; -uniform sampler2D uTexturePosition; -uniform sampler2D uTextureColor; - -uniform mat4 uInvProjection; - -uniform vec3 uDepth_InvDepth_Near;\n) - DEFINE(DEPTH uDepth_InvDepth_Near.x) - DEFINE(INV_DEPTH uDepth_InvDepth_Near.y) - DEFINE(NEAR uDepth_InvDepth_Near.z) - DALI_COMPOSE_SHADER( - -// Light source uniforms -struct Light -{ - vec3 position; // view space - float radius; - vec3 color; -}; - -uniform Light uLights[kMaxLights]; - -in vec2 vUv; - -out vec4 oColor; - -vec4 Unmap(vec4 m) // texture -> projection -{ - m.w = m.w * DEPTH + NEAR; - m.xyz = (m.xyz - vec3(.5)) * (2.f * m.w); - return m; -} - -vec3 CalculateLighting(vec3 pos, vec3 normal) -{ - vec3 viewDir = normalize(pos); - vec3 viewDirRefl = -reflect(viewDir, normal); - - vec3 light = vec3(0.04f); // fake ambient term - for (int i = 0; i < kMaxLights; ++i) - { - vec3 rel = pos - uLights[i].position; - float distance = length(rel); - rel /= distance; - - float a = uLights[i].radius / (kAttenuationConst + kAttenuationLinear * distance + - kAttenuationQuadratic * distance * distance); // attenuation - - float l = max(0.f, dot(normal, rel)); // lambertian - float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f); // specular - - light += (uLights[i].color * (l + s)) * a; - } - - return light; -} - -void main() -{ - vec3 normSample = texture(uTextureNormal, vUv).xyz; - if (dot(normSample, normSample) == 0.f) - { - discard; // if we didn't write this texel, don't bother lighting it. - } - - vec3 normal = normalize(normSample - .5f); - - vec4 posSample = texture(uTexturePosition, vUv); - vec3 pos = (uInvProjection * Unmap(posSample)).xyz; - - vec3 color = texture(uTextureColor, vUv).rgb; - vec3 finalColor = color * CalculateLighting(pos, normal); - - oColor = vec4(finalColor, 1.f); -}); -// clang-format on - //============================================================================= // PRNG for floats. struct FloatRand @@ -542,7 +372,7 @@ private: Geometry mesh = CreateOctahedron(false); // Create main actors - Shader preShader = Shader::New(PREPASS_VSH, PREPASS_FSH); + Shader preShader = Shader::New(SHADER_DEFERRED_SHADING_PREPASS_VERT, SHADER_DEFERRED_SHADING_PREPASS_FRAG); TextureSet noTexturesThanks = TextureSet::New(); Renderer meshRenderer = CreateRenderer(noTexturesThanks, mesh, preShader, OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); meshRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK); @@ -631,7 +461,7 @@ private: finalImageTextures.SetSampler(1, sampler); finalImageTextures.SetSampler(2, sampler); - Shader shdMain = Shader::New(MAINPASS_VSH, MAINPASS_FSH); + Shader shdMain = Shader::New(SHADER_DEFERRED_SHADING_MAINPASS_VERT, SHADER_DEFERRED_SHADING_MAINPASS_FRAG); Geometry finalImageGeom = CreateTexturedQuadGeometry(true); Renderer finalImageRenderer = CreateRenderer(finalImageTextures, finalImageGeom, shdMain); RegisterDepthProperties(depth, zNear, finalImageRenderer); diff --git a/examples/deferred-shading/shaders/deferred-shading-mainpass.frag b/examples/deferred-shading/shaders/deferred-shading-mainpass.frag new file mode 100644 index 0000000..0056861 --- /dev/null +++ b/examples/deferred-shading/shaders/deferred-shading-mainpass.frag @@ -0,0 +1,84 @@ +#version 300 es + +precision mediump float; + +const int kMaxLights = 32; + +const float kAttenuationConst = .05f; +const float kAttenuationLinear = .1f; +const float kAttenuationQuadratic = .15f; + +// G-buffer +uniform sampler2D uTextureNormal; +uniform sampler2D uTexturePosition; +uniform sampler2D uTextureColor; + +uniform mat4 uInvProjection; +uniform vec3 uDepth_InvDepth_Near; + +#define DEPTH uDepth_InvDepth_Near.x +#define INV_DEPTH uDepth_InvDepth_Near.y +#define NEAR uDepth_InvDepth_Near.z + +// Light source uniforms +struct Light +{ + vec3 position; // view space + float radius; + vec3 color; +}; + +uniform Light uLights[kMaxLights]; + +in vec2 vUv; +out vec4 oColor; + +vec4 Unmap(vec4 m) // texture -> projection +{ + m.w = m.w * DEPTH + NEAR; + m.xyz = (m.xyz - vec3(.5)) * (2.f * m.w); + return m; +} + +vec3 CalculateLighting(vec3 pos, vec3 normal) +{ + vec3 viewDir = normalize(pos); + vec3 viewDirRefl = -reflect(viewDir, normal); + + vec3 light = vec3(0.04f); // fake ambient term + for (int i = 0; i < kMaxLights; ++i) + { + vec3 rel = pos - uLights[i].position; + float distance = length(rel); + rel /= distance; + + float a = uLights[i].radius / (kAttenuationConst + kAttenuationLinear * distance + + kAttenuationQuadratic * distance * distance); // attenuation + + float l = max(0.f, dot(normal, rel)); // lambertian + float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f); // specular + + light += (uLights[i].color * (l + s)) * a; + } + + return light; +} + +void main() +{ + vec3 normSample = texture(uTextureNormal, vUv).xyz; + if (dot(normSample, normSample) == 0.f) + { + discard; // if we didn't write this texel, don't bother lighting it. + } + + vec3 normal = normalize(normSample - .5f); + + vec4 posSample = texture(uTexturePosition, vUv); + vec3 pos = (uInvProjection * Unmap(posSample)).xyz; + + vec3 color = texture(uTextureColor, vUv).rgb; + vec3 finalColor = color * CalculateLighting(pos, normal); + + oColor = vec4(finalColor, 1.f); +} diff --git a/examples/deferred-shading/shaders/deferred-shading-mainpass.vert b/examples/deferred-shading/shaders/deferred-shading-mainpass.vert new file mode 100644 index 0000000..57544d9 --- /dev/null +++ b/examples/deferred-shading/shaders/deferred-shading-mainpass.vert @@ -0,0 +1,19 @@ +#version 300 es +precision mediump float; + +// DALI uniforms +uniform mat4 uMvpMatrix; +uniform vec3 uSize; + +in vec3 aPosition; +in vec2 aTexCoord; + +out vec2 vUv; + +void main() +{ + vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); + vUv = aTexCoord; + + gl_Position = position; +} diff --git a/examples/deferred-shading/shaders/deferred-shading-prepass.frag b/examples/deferred-shading/shaders/deferred-shading-prepass.frag new file mode 100644 index 0000000..82f7c3b --- /dev/null +++ b/examples/deferred-shading/shaders/deferred-shading-prepass.frag @@ -0,0 +1,20 @@ +#version 300 es +precision mediump float; + +// DALI uniform +uniform vec4 uColor; + +in vec4 vPosition; +in vec3 vNormal; + +// These are our outputs. +layout(location = 0) out vec3 oNormal; +layout(location = 1) out vec4 oPosition; +layout(location = 2) out vec3 oColor; + +void main() +{ + oColor = uColor.rgb; + oPosition = vPosition; + oNormal = normalize(vNormal) * .5f + .5f; +} diff --git a/examples/deferred-shading/shaders/deferred-shading-prepass.vert b/examples/deferred-shading/shaders/deferred-shading-prepass.vert new file mode 100644 index 0000000..cb61ce9 --- /dev/null +++ b/examples/deferred-shading/shaders/deferred-shading-prepass.vert @@ -0,0 +1,34 @@ +#version 300 es + +precision mediump float; + +// DALI uniforms +uniform mat4 uMvpMatrix; +uniform mat3 uNormalMatrix; +uniform vec3 uSize; + +uniform vec3 uDepth_InvDepth_Near; + +#define DEPTH uDepth_InvDepth_Near.x +#define INV_DEPTH uDepth_InvDepth_Near.y +#define NEAR uDepth_InvDepth_Near.z + +in vec3 aPosition; +in vec3 aNormal; + +out vec4 vPosition; +out vec3 vNormal; + +vec4 Map(vec4 v) // projection space -> texture +{ + return vec4(v.xyz / (2.f * v.w) + vec3(.5f), (v.w - NEAR) * INV_DEPTH); +} + +void main() +{ + vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); + vPosition = Map(position); + gl_Position = position; + + vNormal = normalize(uNormalMatrix * aNormal); +} diff --git a/examples/fpp-game/game-renderer.cpp b/examples/fpp-game/game-renderer.cpp index 7e54d01..c8a24da 100644 --- a/examples/fpp-game/game-renderer.cpp +++ b/examples/fpp-game/game-renderer.cpp @@ -19,37 +19,10 @@ #include "game-model.h" #include "game-texture.h" -#include - -namespace -{ -// clang-format off +#include "generated/game-renderer-vert.h" +#include "generated/game-renderer-frag.h" -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute highp vec3 aPosition;\n - attribute highp vec3 aNormal;\n - attribute highp vec2 aTexCoord;\n - uniform highp mat4 uMvpMatrix;\n - varying highp vec2 vTexCoord;\n - void main()\n - {\n - gl_Position = uMvpMatrix * vec4(aPosition, 1.0 );\n - vTexCoord = aTexCoord;\n - vTexCoord.y = 1.0 - vTexCoord.y;\n - }\n -) - ; -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - uniform sampler2D sTexture;\n - varying highp vec2 vTexCoord;\n - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(1.2, 1.2, 1.2, 1.0);\n - }\n -); -// clang-format on - -} // namespace +#include GameRenderer::GameRenderer() : mModel(NULL), @@ -77,7 +50,7 @@ void GameRenderer::Setup() { if(!mRenderer && mModel) { - Dali::Shader shader = Dali::Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + Dali::Shader shader = Dali::Shader::New(SHADER_GAME_RENDERER_VERT, SHADER_GAME_RENDERER_FRAG); mRenderer = Dali::Renderer::New(mModel->GetGeometry(), shader); mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_WRITE_MODE, Dali::DepthWriteMode::ON); mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_FUNCTION, Dali::DepthFunction::LESS_EQUAL); diff --git a/examples/fpp-game/shaders/game-renderer.frag b/examples/fpp-game/shaders/game-renderer.frag new file mode 100644 index 0000000..e98712c --- /dev/null +++ b/examples/fpp-game/shaders/game-renderer.frag @@ -0,0 +1,7 @@ +uniform sampler2D sTexture; +varying highp vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(1.2, 1.2, 1.2, 1.0); +} diff --git a/examples/fpp-game/shaders/game-renderer.vert b/examples/fpp-game/shaders/game-renderer.vert new file mode 100644 index 0000000..239ec2b --- /dev/null +++ b/examples/fpp-game/shaders/game-renderer.vert @@ -0,0 +1,12 @@ +attribute highp vec3 aPosition; +attribute highp vec3 aNormal; +attribute highp vec2 aTexCoord; +uniform highp mat4 uMvpMatrix; +varying highp vec2 vTexCoord; + +void main() +{ + gl_Position = uMvpMatrix * vec4(aPosition, 1.0 ); + vTexCoord = aTexCoord; + vTexCoord.y = 1.0 - vTexCoord.y; +} diff --git a/examples/image-view-url/image-view-url-example.cpp b/examples/image-view-url/image-view-url-example.cpp index 9036e38..c721d7e 100644 --- a/examples/image-view-url/image-view-url-example.cpp +++ b/examples/image-view-url/image-view-url-example.cpp @@ -19,6 +19,7 @@ #include #include "shared/view.h" +#include "generated/image-view-url-frag.h" using namespace Dali; @@ -31,22 +32,6 @@ const char* const TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png"); const char* const BUTTON_ICON(DEMO_IMAGE_DIR "icon-change.png"); const char* const BUTTON_ICON_SELECTED(DEMO_IMAGE_DIR "icon-change-selected.png"); -const char* FILTER_FRAGMENT_SOURCE = - { - "precision highp float;\n" - "varying mediump vec2 vTexCoord;\n" - "uniform sampler2D sTexture;\n" - "uniform mediump float uDelta;\n" - "void main()\n" - "{\n" - " vec4 color = vec4(0.0);\n" - " vec2 texCoord = vTexCoord * 2. - 1.;\n" - " mat2 rotation = mat2(cos(uDelta), -sin(uDelta), sin(uDelta), cos(uDelta));" - " texCoord = (rotation * texCoord) * .5 + .5;\n" - " color += texture2D( sTexture, texCoord );\n" - " gl_FragColor = color;\n" - "}\n"}; - const char* DELTA_UNIFORM_NAME = "uDelta"; const Vector2 TARGET_SIZE(800.f, 800.f); @@ -119,7 +104,7 @@ private: mActorForInput.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); mActorForInput.SetProperty(Actor::Property::SIZE, TARGET_SIZE); Property::Map customShader; - customShader[Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FILTER_FRAGMENT_SOURCE; + customShader[Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = SHADER_IMAGE_VIEW_URL_FRAG.data(); Property::Map visualMap; visualMap.Insert(Toolkit::Visual::Property::SHADER, customShader); mActorForInput.SetProperty(Toolkit::ImageView::Property::IMAGE, visualMap); diff --git a/examples/image-view-url/shaders/image-view-url.frag b/examples/image-view-url/shaders/image-view-url.frag new file mode 100644 index 0000000..4b07ce4 --- /dev/null +++ b/examples/image-view-url/shaders/image-view-url.frag @@ -0,0 +1,14 @@ +precision highp float; +varying mediump vec2 vTexCoord; +uniform sampler2D sTexture; +uniform mediump float uDelta; + +void main() +{ + vec4 color = vec4(0.0); + vec2 texCoord = vTexCoord * 2. - 1.; + mat2 rotation = mat2(cos(uDelta), -sin(uDelta), sin(uDelta), cos(uDelta)); + texCoord = (rotation * texCoord) * .5 + .5; + color += texture2D( sTexture, texCoord ); + gl_FragColor = color; +} \ No newline at end of file diff --git a/examples/line-mesh/line-mesh-example.cpp b/examples/line-mesh/line-mesh-example.cpp index 3ef3d51..1bc3589 100644 --- a/examples/line-mesh/line-mesh-example.cpp +++ b/examples/line-mesh/line-mesh-example.cpp @@ -22,6 +22,8 @@ // INTERNAL INCLUDES #include "shared/view.h" +#include "generated/line-mesh-vert.h" +#include "generated/line-mesh-frag.h" #include @@ -29,36 +31,6 @@ using namespace Dali; namespace { -#define MAKE_SHADER(A) #A - -const char* VERTEX_SHADER = MAKE_SHADER( - attribute mediump vec2 aPosition1; - attribute mediump vec2 aPosition2; - attribute lowp vec3 aColor; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform mediump float uMorphAmount; - - varying lowp vec3 vColor; - - void main() { - mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount); - mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0); - vColor = aColor; - vertexPosition.xyz *= uSize; - vertexPosition = uMvpMatrix * vertexPosition; - gl_Position = vertexPosition; - }); - -const char* FRAGMENT_SHADER = MAKE_SHADER( - uniform lowp vec4 uColor; - uniform sampler2D sTexture; - - varying lowp vec3 vColor; - - void main() { - gl_FragColor = uColor * vec4(vColor, 1.0); - }); const unsigned short INDEX_LINES[] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0}; const unsigned short INDEX_LOOP[] = {0, 1, 2, 3, 4}; @@ -177,7 +149,7 @@ public: mMeshActor.Reset(); } - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_LINE_MESH_VERT, SHADER_LINE_MESH_FRAG); mGeometry = CreateGeometry(); mRenderer = Renderer::New(mGeometry, mShader); diff --git a/examples/line-mesh/shaders/line-mesh.frag b/examples/line-mesh/shaders/line-mesh.frag new file mode 100644 index 0000000..c9eeb01 --- /dev/null +++ b/examples/line-mesh/shaders/line-mesh.frag @@ -0,0 +1,9 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; + +varying lowp vec3 vColor; + +void main() +{ + gl_FragColor = uColor * vec4(vColor, 1.0); +} diff --git a/examples/line-mesh/shaders/line-mesh.vert b/examples/line-mesh/shaders/line-mesh.vert new file mode 100644 index 0000000..e9ff779 --- /dev/null +++ b/examples/line-mesh/shaders/line-mesh.vert @@ -0,0 +1,18 @@ +attribute mediump vec2 aPosition1; +attribute mediump vec2 aPosition2; +attribute lowp vec3 aColor; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform mediump float uMorphAmount; + +varying lowp vec3 vColor; + +void main() +{ + mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount); + mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0); + vColor = aColor; + vertexPosition.xyz *= uSize; + vertexPosition = uMvpMatrix * vertexPosition; + gl_Position = vertexPosition; +} diff --git a/examples/mesh-morph/mesh-morph-example.cpp b/examples/mesh-morph/mesh-morph-example.cpp index aa9d57d..852449c 100644 --- a/examples/mesh-morph/mesh-morph-example.cpp +++ b/examples/mesh-morph/mesh-morph-example.cpp @@ -21,37 +21,13 @@ // INTERNAL INCLUDES #include "shared/view.h" +#include "generated/mesh-morph-vert.h" +#include "generated/mesh-morph-frag.h" using namespace Dali; namespace { -#define MAKE_SHADER(A) #A - -const char* VERTEX_SHADER = MAKE_SHADER( - attribute mediump vec2 aInitPos; - attribute mediump vec2 aFinalPos; - attribute mediump vec3 aColor; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform mediump float uDelta; - uniform lowp vec4 uColor; - varying lowp vec4 vColor; - - void main() { - mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0); - vertexPosition.xyz *= uSize; - vertexPosition = uMvpMatrix * vertexPosition; - gl_Position = vertexPosition; - vColor = vec4(aColor, 0.) * uColor; - }); - -const char* FRAGMENT_SHADER = MAKE_SHADER( - varying lowp vec4 vColor; - - void main() { - gl_FragColor = vColor; - }); Geometry CreateGeometry() { @@ -288,7 +264,7 @@ public: // The Init signal is received once (only) during the Application lifetime - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_MESH_MORPH_VERT, SHADER_MESH_MORPH_FRAG); mGeometry = CreateGeometry(); mRenderer = Renderer::New(mGeometry, mShader); diff --git a/examples/mesh-morph/shaders/mesh-morph.frag b/examples/mesh-morph/shaders/mesh-morph.frag new file mode 100644 index 0000000..93f7a01 --- /dev/null +++ b/examples/mesh-morph/shaders/mesh-morph.frag @@ -0,0 +1,6 @@ +varying lowp vec4 vColor; + +void main() +{ + gl_FragColor = vColor; +} \ No newline at end of file diff --git a/examples/mesh-morph/shaders/mesh-morph.vert b/examples/mesh-morph/shaders/mesh-morph.vert new file mode 100644 index 0000000..766c88c --- /dev/null +++ b/examples/mesh-morph/shaders/mesh-morph.vert @@ -0,0 +1,17 @@ +attribute mediump vec2 aInitPos; +attribute mediump vec2 aFinalPos; +attribute mediump vec3 aColor; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform mediump float uDelta; +uniform lowp vec4 uColor; +varying lowp vec4 vColor; + +void main() +{ + mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0); + vertexPosition.xyz *= uSize; + vertexPosition = uMvpMatrix * vertexPosition; + gl_Position = vertexPosition; + vColor = vec4(aColor, 0.) * uColor; +} diff --git a/examples/metaball-explosion/metaball-explosion-example.cpp b/examples/metaball-explosion/metaball-explosion-example.cpp index 0c65e00..ae39e8d 100644 --- a/examples/metaball-explosion/metaball-explosion-example.cpp +++ b/examples/metaball-explosion/metaball-explosion-example.cpp @@ -28,6 +28,9 @@ // INTERNAL INCLUDES #include "shared/utility.h" // DemoHelper::LoadTexture +#include "generated/metaball-vert.h" +#include "generated/metaball-frag.h" +#include "generated/metaball-refraction-frag.h" using namespace Dali; @@ -40,127 +43,6 @@ const char* const BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-2.jpg"); constexpr uint32_t METABALL_NUMBER = 6; /** - * Vertex shader code for metaball - */ -// clang-format off -const char* const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER ( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexture;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - uniform lowp vec4 uColor;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - vTexCoord = aTexture;\n - mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n - gl_Position = uMvpMatrix * vertexPosition;\n - }\n -); - -/** - * Fragment shader code for metaball - */ -const char* const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER ( - precision mediump float;\n - varying vec2 vTexCoord;\n - uniform vec2 uPositionMetaball;\n - uniform vec2 uPositionVar;\n - uniform vec2 uGravityVector;\n - uniform float uRadius;\n - uniform float uRadiusVar;\n - void main()\n - {\n - vec2 adjustedCoords = vTexCoord * 2.0 - 1.0;\n - vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar;\n - \n - float finalRadius = uRadius + uRadiusVar;\n - vec2 distanceVec = adjustedCoords - finalMetaballPosition;\n - float result = dot(distanceVec, distanceVec);\n - float color = inversesqrt(result) * finalRadius;\n - \n - gl_FragColor = vec4(color,color,color,1.0);\n - }\n -); - -/** - * Fragment shader code for metaball and background composition with refraction effect - */ -const char* const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( - precision highp float;\n - varying vec2 vTexCoord;\n - uniform sampler2D sTexture;\n - uniform sampler2D sEffect;\n - uniform vec2 uPositionMetaball;\n - void main()\n - {\n - vec2 zoomCoords;\n - vec3 normal = vec3(0.0,0.0,1.0);\n - vec2 fakePos = vec2(0.0,0.0);\n - vec3 color = vec3(1.0, 1.0, 1.0); - float ambient = 0.2; - \n - vec4 metaColor = texture2D(sEffect, vTexCoord);\n - \n - vec2 adjustedCoords = vTexCoord.xy * vec2(2.0) - vec2(1.0);\n - fakePos = adjustedCoords.xy - vec2(uPositionMetaball.x, -uPositionMetaball.y); - float len = length(fakePos) + 0.01;\n - vec3 colorPos = vec3(0,0,1); - \n - if (metaColor.r > 0.85)\n - {\n - zoomCoords = ((vTexCoord - 0.5) * 0.9);\n - zoomCoords = zoomCoords + 0.5;\n - \n - float interpNormal = mix(0.7, 1.0, (metaColor.r - 0.85) * 4.);\n - normal.xyz = vec3(fakePos.x * (1.0 - interpNormal) / len, fakePos.y * (1.0 - interpNormal) / len, interpNormal);\n - normal.xyz = normalize(normal.xyz);\n - color = vec3(0.65, 1.0, 0);\n - colorPos = vec3(fakePos.x,fakePos.y,0); - }\n - else if (metaColor.r > 0.75)\n - {\n - float interpolation = mix(0.9, 1.15, (0.85 - metaColor.r) * 10.0);\n - zoomCoords = ((vTexCoord - 0.5) * interpolation);\n - zoomCoords = zoomCoords + 0.5;\n - \n - float interpNormal = mix(0.7, 0.0, (0.85 - metaColor.r) * 10.0);\n - normal.xyz = vec3(fakePos.x * (1.0 - interpNormal) / len, fakePos.y * (1.0 - interpNormal) / len, interpNormal);\n - normal.xyz = normalize(normal.xyz);\n - color = vec3(0.65, 1.0, 0);\n - colorPos = vec3(fakePos.x,fakePos.y,0); - }\n - else\n - {\n - zoomCoords = vTexCoord;\n - normal = vec3(0,0,0);\n - ambient = 0.5;\n - }\n - \n - vec3 lightPosition = vec3(-750.0,-1000.0,2000.0);\n - vec3 vertex = vec3(adjustedCoords.x,adjustedCoords.y,0.0);\n - \n - vec3 vecToLight = normalize( lightPosition - vertex );\n - \n - float lightDiffuse = dot( vecToLight, normal );\n - lightDiffuse = max(0.0,lightDiffuse);\n - lightDiffuse = lightDiffuse * 0.5 + 0.5; - \n - vec3 vertexToEye = vec3(0,0,1) - vertex;\n - vertexToEye = normalize(vertexToEye); - vec3 lightReflect = normalize(reflect(-vecToLight, normal));\n - float specularFactor = max(0.0,dot(vertexToEye, lightReflect));\n - specularFactor = pow(specularFactor, 32.0) * 0.7; - \n - vec4 texColor = texture2D(sTexture, zoomCoords);\n - gl_FragColor.rgb = texColor.rgb * ambient + color.rgb * texColor.rgb * lightDiffuse + vec3(specularFactor);\n - gl_FragColor.a = 1.0; - }\n - ); -// clang-format on - -/** * Metadata for each ball */ struct MetaballInfo @@ -429,7 +311,7 @@ Geometry MetaballExplosionController::CreateGeometry(bool aspectMappedTexture) void MetaballExplosionController::CreateMetaballActors() { // Create the shader for the metaballs, tell DALi that shader modifies geometry so we dont need to set a meaningless size - Shader shader = Shader::New(METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER, Shader::Hint::MODIFIES_GEOMETRY); + Shader shader = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_FRAG, Shader::Hint::MODIFIES_GEOMETRY); Geometry metaballGeom = CreateGeometry(); // Reuse same renderer for each actor @@ -493,7 +375,7 @@ void MetaballExplosionController::CreateMetaballImage() void MetaballExplosionController::CreateComposition() { //Create new shader - Shader shader = Shader::New(METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER); + Shader shader = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_REFRACTION_FRAG); // Create new texture set auto textureSet = TextureSet::New(); diff --git a/examples/metaball-explosion/shaders/metaball-refraction.frag b/examples/metaball-explosion/shaders/metaball-refraction.frag new file mode 100644 index 0000000..ad67df6 --- /dev/null +++ b/examples/metaball-explosion/shaders/metaball-refraction.frag @@ -0,0 +1,71 @@ +//Fragment shader code for metaball and background composition with refraction effect + +precision highp float; +varying vec2 vTexCoord; +uniform sampler2D sTexture; +uniform sampler2D sEffect; +uniform vec2 uPositionMetaball; +void main() +{ + vec2 zoomCoords; + vec3 normal = vec3(0.0,0.0,1.0); + vec2 fakePos = vec2(0.0,0.0); + vec3 color = vec3(1.0, 1.0, 1.0); + float ambient = 0.2; + + vec4 metaColor = texture2D(sEffect, vTexCoord); + + vec2 adjustedCoords = vTexCoord.xy * vec2(2.0) - vec2(1.0); + fakePos = adjustedCoords.xy - vec2(uPositionMetaball.x, -uPositionMetaball.y); + float len = length(fakePos) + 0.01; + vec3 colorPos = vec3(0,0,1); + + if (metaColor.r > 0.85) + { + zoomCoords = ((vTexCoord - 0.5) * 0.9); + zoomCoords = zoomCoords + 0.5; + + float interpNormal = mix(0.7, 1.0, (metaColor.r - 0.85) * 4.); + normal.xyz = vec3(fakePos.x * (1.0 - interpNormal) / len, fakePos.y * (1.0 - interpNormal) / len, interpNormal); + normal.xyz = normalize(normal.xyz); + color = vec3(0.65, 1.0, 0); + colorPos = vec3(fakePos.x,fakePos.y,0); + } + else if (metaColor.r > 0.75) + { + float interpolation = mix(0.9, 1.15, (0.85 - metaColor.r) * 10.0); + zoomCoords = ((vTexCoord - 0.5) * interpolation); + zoomCoords = zoomCoords + 0.5; + + float interpNormal = mix(0.7, 0.0, (0.85 - metaColor.r) * 10.0); + normal.xyz = vec3(fakePos.x * (1.0 - interpNormal) / len, fakePos.y * (1.0 - interpNormal) / len, interpNormal); + normal.xyz = normalize(normal.xyz); + color = vec3(0.65, 1.0, 0); + colorPos = vec3(fakePos.x,fakePos.y,0); + } + else + { + zoomCoords = vTexCoord; + normal = vec3(0,0,0); + ambient = 0.5; + } + + vec3 lightPosition = vec3(-750.0,-1000.0,2000.0); + vec3 vertex = vec3(adjustedCoords.x,adjustedCoords.y,0.0); + + vec3 vecToLight = normalize( lightPosition - vertex ); + + float lightDiffuse = dot( vecToLight, normal ); + lightDiffuse = max(0.0,lightDiffuse); + lightDiffuse = lightDiffuse * 0.5 + 0.5; + + vec3 vertexToEye = vec3(0,0,1) - vertex; + vertexToEye = normalize(vertexToEye); + vec3 lightReflect = normalize(reflect(-vecToLight, normal)); + float specularFactor = max(0.0,dot(vertexToEye, lightReflect)); + specularFactor = pow(specularFactor, 32.0) * 0.7; + + vec4 texColor = texture2D(sTexture, zoomCoords); + gl_FragColor.rgb = texColor.rgb * ambient + color.rgb * texColor.rgb * lightDiffuse + vec3(specularFactor); + gl_FragColor.a = 1.0; +} diff --git a/examples/metaball-explosion/shaders/metaball.frag b/examples/metaball-explosion/shaders/metaball.frag new file mode 100644 index 0000000..8411a18 --- /dev/null +++ b/examples/metaball-explosion/shaders/metaball.frag @@ -0,0 +1,22 @@ +// Fragment shader code for metaball + +precision mediump float; +varying vec2 vTexCoord; +uniform vec2 uPositionMetaball; +uniform vec2 uPositionVar; +uniform vec2 uGravityVector; +uniform float uRadius; +uniform float uRadiusVar; + +void main() +{ + vec2 adjustedCoords = vTexCoord * 2.0 - 1.0; + vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar; + + float finalRadius = uRadius + uRadiusVar; + vec2 distanceVec = adjustedCoords - finalMetaballPosition; + float result = dot(distanceVec, distanceVec); + float color = inversesqrt(result) * finalRadius; + + gl_FragColor = vec4(color,color,color,1.0); +} diff --git a/examples/metaball-explosion/shaders/metaball.vert b/examples/metaball-explosion/shaders/metaball.vert new file mode 100644 index 0000000..911e77a --- /dev/null +++ b/examples/metaball-explosion/shaders/metaball.vert @@ -0,0 +1,15 @@ +//Vertex shader code for metaball + +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexture; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform lowp vec4 uColor; +varying mediump vec2 vTexCoord; + +void main() +{ + vTexCoord = aTexture; + mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0); + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/metaball-refrac/metaball-refrac-example.cpp b/examples/metaball-refrac/metaball-refrac-example.cpp index 1d1c046..a25e5c3 100644 --- a/examples/metaball-refrac/metaball-refrac-example.cpp +++ b/examples/metaball-refrac/metaball-refrac-example.cpp @@ -27,6 +27,10 @@ // INTERNAL INCLUDES #include "shared/utility.h" // DemoHelper::LoadTexture +#include "generated/metaball-vert.h" +#include "generated/metaball-frag.h" +#include "generated/metaball-refraction-frag.h" +#include "generated/fragment-frag.h" using namespace Dali; @@ -39,117 +43,6 @@ const float GRAVITY_Y(-0.09); // number of metaballs constexpr uint32_t METABALL_NUMBER = 6; -// clang-format off - -/** - * Vertex shader for metaballs - */ -const char* const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER ( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexture;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - uniform lowp vec4 uColor;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n - vertexPosition = uMvpMatrix * vertexPosition;\n - gl_Position = vertexPosition;\n - vTexCoord = aTexture;\n - }\n -); - -/** - * Fragment shader for metaballs - */ -const char* const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER ( - precision mediump float;\n - varying vec2 vTexCoord;\n - uniform vec2 uPositionMetaball;\n - uniform vec2 uPositionVar;\n - uniform vec2 uGravityVector;\n - uniform float uRadius;\n - uniform float uRadiusVar;\n - uniform float uAspect;\n - void main()\n - {\n - vec2 adjustedCoords = vTexCoord * 2.0 - 1.0;\n - vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar;\n - - float distance = (adjustedCoords.x - finalMetaballPosition.x) * (adjustedCoords.x - finalMetaballPosition.x) + - (adjustedCoords.y - finalMetaballPosition.y) * (adjustedCoords.y - finalMetaballPosition.y);\n - float finalRadius = uRadius + uRadiusVar;\n - float color = finalRadius / sqrt( distance );\n - vec2 bordercolor = vec2(0.0,0.0);\n - if (vTexCoord.x < 0.1)\n - {\n - bordercolor.x = (0.1 - vTexCoord.x) * 0.8;\n - }\n - if (vTexCoord.x > 0.9)\n - {\n - bordercolor.x = (vTexCoord.x - 0.9) * 0.8;\n - }\n - if (vTexCoord.y < 0.1)\n - {\n - bordercolor.y = (0.1 - vTexCoord.y) * 0.8;\n - }\n - if (vTexCoord.y > (0.9 * uAspect))\n - {\n - bordercolor.y = (vTexCoord.y - (0.9 * uAspect)) * 0.8;\n - }\n - float border = (bordercolor.x + bordercolor.y) * 0.5;\n - gl_FragColor = vec4(color + border,color + border,color + border,1.0);\n - }\n -); - -/** - * Fragment shader code for metaball and background composition with refraction effect - */ -const char* const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( - precision mediump float;\n - varying vec2 vTexCoord;\n - uniform sampler2D sTexture;\n - uniform sampler2D sEffect;\n - void main()\n - {\n - vec4 metaColor = texture2D(sEffect, vTexCoord);\n - vec2 zoomCoords;\n - float bright = 1.0;\n - if (metaColor.r > 0.85)\n - {\n - zoomCoords = ((vTexCoord - 0.5) * 0.95) + 0.5;\n - }\n - else if (metaColor.r > 0.78)\n - {\n - float interpolation = mix(0.95, 1.05, (0.85 - metaColor.r) * 50.0);\n - zoomCoords = ((vTexCoord - 0.5) * interpolation) + 0.5;\n - bright = 1.2;\n - }\n - else\n - {\n - zoomCoords = vTexCoord;\n - }\n - - gl_FragColor = texture2D(sTexture, zoomCoords) * bright;\n - }\n - ); - -/** - * Fragment shader code when there's no effect - */ -const char* const FRAG_SHADER = DALI_COMPOSE_SHADER ( - precision mediump float;\n - varying vec2 vTexCoord;\n - uniform sampler2D sTexture;\n - void main()\n - {\n - gl_FragColor = texture2D(sTexture, vTexCoord);\n - }\n -); -// clang-format on - /** * Metadata for each ball */ @@ -405,7 +298,7 @@ void MetaballRefracController::CreateMetaballActors() const float aspect = mScreenSize.y / mScreenSize.x; // Create the renderer for the metaballs - Shader shader = Shader::New(METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER, Shader::Hint::MODIFIES_GEOMETRY); + Shader shader = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_FRAG, Shader::Hint::MODIFIES_GEOMETRY); Geometry metaballGeometry = CreateGeometry(); Renderer renderer = Renderer::New(metaballGeometry, shader); renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON); @@ -471,7 +364,7 @@ void MetaballRefracController::CreateMetaballImage() void MetaballRefracController::CreateComposition() { // Create Refraction shader and renderer - mShaderRefraction = Shader::New(METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER); + mShaderRefraction = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_REFRACTION_FRAG); // Create new texture set mTextureSetRefraction = TextureSet::New(); @@ -479,7 +372,7 @@ void MetaballRefracController::CreateComposition() mTextureSetRefraction.SetTexture(1u, mMetaballFBO.GetColorTexture()); // Create normal shader - mShaderNormal = Shader::New(METABALL_VERTEX_SHADER, FRAG_SHADER); + mShaderNormal = Shader::New(SHADER_METABALL_VERT, SHADER_FRAGMENT_FRAG); // Create new texture set mTextureSetNormal = TextureSet::New(); diff --git a/examples/metaball-refrac/shaders/fragment.frag b/examples/metaball-refrac/shaders/fragment.frag new file mode 100644 index 0000000..1878397 --- /dev/null +++ b/examples/metaball-refrac/shaders/fragment.frag @@ -0,0 +1,10 @@ +// Fragment shader code when there's no effect + +precision mediump float; +varying vec2 vTexCoord; +uniform sampler2D sTexture; + +void main() +{ + gl_FragColor = texture2D(sTexture, vTexCoord); +} diff --git a/examples/metaball-refrac/shaders/metaball-refraction.frag b/examples/metaball-refrac/shaders/metaball-refraction.frag new file mode 100644 index 0000000..e84f3ea --- /dev/null +++ b/examples/metaball-refrac/shaders/metaball-refraction.frag @@ -0,0 +1,29 @@ +// Fragment shader code for metaball and background composition with refraction effect + +precision mediump float; +varying vec2 vTexCoord; +uniform sampler2D sTexture; +uniform sampler2D sEffect; + +void main() +{ + vec4 metaColor = texture2D(sEffect, vTexCoord); + vec2 zoomCoords; + float bright = 1.0; + if (metaColor.r > 0.85) + { + zoomCoords = ((vTexCoord - 0.5) * 0.95) + 0.5; + } + else if (metaColor.r > 0.78) + { + float interpolation = mix(0.95, 1.05, (0.85 - metaColor.r) * 50.0); + zoomCoords = ((vTexCoord - 0.5) * interpolation) + 0.5; + bright = 1.2; + } + else + { + zoomCoords = vTexCoord; + } + + gl_FragColor = texture2D(sTexture, zoomCoords) * bright; +} diff --git a/examples/metaball-refrac/shaders/metaball.frag b/examples/metaball-refrac/shaders/metaball.frag new file mode 100644 index 0000000..ed87767 --- /dev/null +++ b/examples/metaball-refrac/shaders/metaball.frag @@ -0,0 +1,40 @@ +// Fragment shader for metaballs + +precision mediump float; +varying vec2 vTexCoord; +uniform vec2 uPositionMetaball; +uniform vec2 uPositionVar; +uniform vec2 uGravityVector; +uniform float uRadius; +uniform float uRadiusVar; +uniform float uAspect; + +void main() +{ + vec2 adjustedCoords = vTexCoord * 2.0 - 1.0; + vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar; + + float distance = (adjustedCoords.x - finalMetaballPosition.x) * (adjustedCoords.x - finalMetaballPosition.x) + + (adjustedCoords.y - finalMetaballPosition.y) * (adjustedCoords.y - finalMetaballPosition.y); + float finalRadius = uRadius + uRadiusVar; + float color = finalRadius / sqrt( distance ); + vec2 bordercolor = vec2(0.0,0.0); + if (vTexCoord.x < 0.1) + { + bordercolor.x = (0.1 - vTexCoord.x) * 0.8; + } + if (vTexCoord.x > 0.9) + { + bordercolor.x = (vTexCoord.x - 0.9) * 0.8; + } + if (vTexCoord.y < 0.1) + { + bordercolor.y = (0.1 - vTexCoord.y) * 0.8; + } + if (vTexCoord.y > (0.9 * uAspect)) + { + bordercolor.y = (vTexCoord.y - (0.9 * uAspect)) * 0.8; + } + float border = (bordercolor.x + bordercolor.y) * 0.5; + gl_FragColor = vec4(color + border,color + border,color + border,1.0); +} diff --git a/examples/metaball-refrac/shaders/metaball.vert b/examples/metaball-refrac/shaders/metaball.vert new file mode 100644 index 0000000..370d870 --- /dev/null +++ b/examples/metaball-refrac/shaders/metaball.vert @@ -0,0 +1,16 @@ +// Vertex shader for metaballs + +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexture; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform lowp vec4 uColor; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0); + vertexPosition = uMvpMatrix * vertexPosition; + gl_Position = vertexPosition; + vTexCoord = aTexture; +} diff --git a/examples/native-image-source/native-image-source-example.cpp b/examples/native-image-source/native-image-source-example.cpp index f08c142..dad40d3 100644 --- a/examples/native-image-source/native-image-source-example.cpp +++ b/examples/native-image-source/native-image-source-example.cpp @@ -22,6 +22,8 @@ // INTERNAL INCLUDES #include "shared/utility.h" +#include "generated/native-image-source-texture-vert.h" +#include "generated/native-image-source-texture-frag.h" using namespace Dali; using namespace Toolkit; @@ -43,33 +45,6 @@ Shader CreateShader(NativeImageInterface& nativeImage) { static const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D"; - // clang-format off - static const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexCoord;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - varying mediump vec2 vTexCoord;\n - void main()\n - {\n - vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n - gl_Position = uMvpMatrix * position;\n - vTexCoord = aTexCoord;\n - }\n - ); - - static const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n - }\n - ); - // clang-format on - std::string fragmentShader; //Get custom fragment shader prefix @@ -77,11 +52,11 @@ Shader CreateShader(NativeImageInterface& nativeImage) if(fragmentPrefix) { fragmentShader = fragmentPrefix; - fragmentShader += FRAGMENT_SHADER_TEXTURE; + fragmentShader += SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_FRAG.data(); } else { - fragmentShader = FRAGMENT_SHADER_TEXTURE; + fragmentShader = SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_FRAG.data(); } //Get custom sampler type name @@ -91,7 +66,7 @@ Shader CreateShader(NativeImageInterface& nativeImage) fragmentShader.replace(fragmentShader.find(DEFAULT_SAMPLER_TYPENAME), strlen(DEFAULT_SAMPLER_TYPENAME), customSamplerTypename); } - return Shader::New(VERTEX_SHADER_TEXTURE, fragmentShader); + return Shader::New(SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_VERT, fragmentShader); } } // namespace diff --git a/examples/native-image-source/shaders/native-image-source-texture.frag b/examples/native-image-source/shaders/native-image-source-texture.frag new file mode 100644 index 0000000..f9eba48 --- /dev/null +++ b/examples/native-image-source/shaders/native-image-source-texture.frag @@ -0,0 +1,8 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} diff --git a/examples/native-image-source/shaders/native-image-source-texture.vert b/examples/native-image-source/shaders/native-image-source-texture.vert new file mode 100644 index 0000000..3cb2121 --- /dev/null +++ b/examples/native-image-source/shaders/native-image-source-texture.vert @@ -0,0 +1,12 @@ +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; +} diff --git a/examples/particles/particle-view.cpp b/examples/particles/particle-view.cpp index 36db679..966a1e4 100644 --- a/examples/particles/particle-view.cpp +++ b/examples/particles/particle-view.cpp @@ -18,6 +18,11 @@ #include "utils.h" #include "dali/public-api/animation/constraints.h" +#include "generated/particle-view-vert.h" +#include "generated/particle-view-frag.h" +#include "generated/particle-view-simple-vert.h" +#include "generated/particle-view-simple-frag.h" + //#define ENABLE_DEBUG_VOLUME #define USE_GLSL_VERSION(version) "#version " #version "\n" @@ -29,195 +34,6 @@ namespace const uint32_t POPULATION_GRANULARITY = 128; -///@brief Shader for billboarded particles, where the vertices of the particles -/// are supplied as vec3 position (particle position) + vec2 sub-position. -const char* const PARTICLES_VSH = USE_GLSL_VERSION(300 es) -DALI_COMPOSE_SHADER( - precision lowp float; - uniform mat4 uModelView; // DALi - uniform mat4 uProjection; // DALi - uniform vec3 uSize; // DALi - uniform vec4 uColor; // DALi - - uniform vec3 uSecondaryColor; - uniform vec2 uDepthRange; // x is zNear, y is 1.f / (zFar - zNear) - uniform float uTwinkleFrequency; - uniform float uTwinkleSizeScale; - uniform float uTwinkleOpacityWeight; - uniform float uTime; - uniform float uFocalLength; - uniform float uAperture; - uniform float uPopulation; - - struct Scatter - { - float radiusSqr; - float amount; - vec3 ray; - }; - - const int SCATTER_VARS = 6; // Must match ParticleView::mScatterProps' size. - uniform Scatter uScatter[SCATTER_VARS]; - - const int POPULATION_GRANULARITY = 128; - uniform float uOrderLookUp[POPULATION_GRANULARITY]; - - in vec3 aPosition; - in float aSeed; - in vec4 aPath; - in vec2 aSubPosition; - in float aSize; - - flat out float vDepth; - flat out float vFocalDistance; - out vec2 vUvUnit; - flat out float vOpacity; - flat out vec3 vColor; // ignore alpha - - float bezier(vec3 control, float alpha) - { - return mix(mix(control.x, control.y, alpha), mix(control.y, control.z, alpha), alpha); - } - - void main() { - // Get random order from the look-up table, based on particle ID. - int particleId = gl_VertexID / 6; - float order = uOrderLookUp[particleId & (POPULATION_GRANULARITY - 1)]; - - // Get twinkle scalar - float twinkle = sin(uTime * floor(uTwinkleFrequency * aSeed) + fract(aSeed * 1.17137)); - - // Add Motion - float s = sin(uTime + aSeed) * .5f + .5f; // different phase for all - // NOTE: you'd think that taking the bezier() calls apart would save 4 mix() calls, since - // the mix()es (of xy / yz / zw / wx) are all calculated twice. It turns out that the MALI - // compiler is already doing this; leaving it as is for readability. - float bx0 = bezier(aPath.xyz, s); - float bx1 = bezier(aPath.zwx, s); - float by0 = bezier(aPath.yzw, s); - float by1 = bezier(aPath.wxy, s); - vec3 motion = vec3(mix(bx0, bx1, s), mix(by0, by1, s), 0.f); - - // Model to view position - vec3 position3 = aPosition * uSize + motion; - - vec4 position = uModelView * vec4(position3, 1.f); - - // Add scatter - calculated in view space, using view ray - vec3 normalizedPos = position.xyz / uSize; - for (int i = 0; i < SCATTER_VARS; ++i) - { - vec2 scatterDist = (normalizedPos - uScatter[i].ray * dot(uScatter[i].ray, normalizedPos)).xy; - - // NOTE: replacing the division with a multiplication (by inverse) oddly results in more instructions (MALI). - float scatter = max(0.f, uScatter[i].radiusSqr - dot(scatterDist, scatterDist)) * - uScatter[i].amount / aSize; - position.xy += scatter * normalize(scatterDist) * uSize.xy; - } - - // Calculate normalised depth and distance from focal plane - float depth = (position.z - uDepthRange.x) * uDepthRange.y; - vDepth = depth; - - float focalDist = (uFocalLength - depth) * uAperture; - focalDist *= focalDist; - vFocalDistance = max(focalDist, 1e-6f); // NOTE: was clamp(..., 1.f); side effect: out of focus particles get squashed at higher aperture values. - - // Calculate expiring scale - for size and opacity. - float expiringScale = smoothstep(order + 1.f, order, uPopulation); - - // Calculate billboard position and size - vec2 subPosition = aSubPosition * aSize * - (1.f + twinkle * aSeed * uTwinkleSizeScale) * - expiringScale; - - // Insist on hacking the size? Do it here... - float sizeHack = depth + .5f; - // NOTE: sizeHack *= sizeHack looked slightly better. - subPosition *= sizeHack; - - vec3 subPositionView = vec3(subPosition, 0.); - - // Add billboards to view position. - position += vec4(subPositionView, 0.f); - - // subPosition doubles as normalized (-1..1) UV. - vUvUnit = aSubPosition; - - // Vary opacity (actor alpha) by time as well as expiring scale. - vOpacity = uColor.a * expiringScale * - (1.0f + aSeed + twinkle * uTwinkleOpacityWeight) / (2.0f + uTwinkleOpacityWeight); - - // Randomize RGB using seed. - vec3 mixColor = vec3(fract(aSeed), fract(aSeed * 16.f), fract(aSeed * 256.f)); - vColor = mix(uColor.rgb, uSecondaryColor, mixColor); - - gl_Position = uProjection * position; - }); - -///@brief Fragment shader for particles, which simulates depth of field -/// using a combination of procedural texturing, alpha testing and alpha -/// blending. -const char* const PARTICLES_FSH = USE_GLSL_VERSION(300 es) -DALI_COMPOSE_SHADER( - precision lowp float; - uniform float uAlphaTestRefValue; - uniform vec2 uFadeRange; // near, far - in vec2 vUvUnit; - flat in float vDepth; - flat in float vFocalDistance; - flat in float vOpacity; - flat in vec3 vColor; - out vec4 oFragColor; - - const float REF_VALUE_THRESHOLD = 1. / 64.; - - void main() { - // Softened disc pattern from normalized UVs - float value = 1.f - dot(vUvUnit, vUvUnit); - - // Decrease area of particles 'in-focus'. - float refValue = (1.f - vFocalDistance) * .5f; - float threshold = REF_VALUE_THRESHOLD * (1.f + vDepth); - float alpha = pow(value, vFocalDistance) * smoothstep(refValue - threshold, refValue + threshold, value); - if (alpha < uAlphaTestRefValue) - { - discard; - } - - // Apply opacity - alpha *= vOpacity; - alpha *= alpha; - - // Fade particles out as they get close to the near and far clipping planes - alpha *= smoothstep(.0f, uFadeRange.x, vDepth) * smoothstep(1.f, uFadeRange.y, vDepth); - - oFragColor = vec4(vColor, alpha); - }); - -///@brief Shader for simple textured geometry. -const char* const SIMPLE_VSH = USE_GLSL_VERSION(300 es) -DALI_COMPOSE_SHADER( - precision mediump float; - uniform mat4 uMvpMatrix;//by DALi - uniform vec3 uSize; // by DALi - in vec3 aPosition; - void main() { - gl_Position = uMvpMatrix * vec4(aPosition * uSize, 1.f); - }); - -///@brief Shader for an unlit, unfogged, textured mesh. -const char* const SIMPLE_FSH = USE_GLSL_VERSION(300 es) -DALI_COMPOSE_SHADER( - precision mediump float; - uniform vec4 uColor; - out vec4 oFragColor; - - void main() { - oFragColor = uColor; - }); - - uint32_t GetSkipValue(uint32_t count, uint32_t prime) { uint32_t skip = 0; @@ -243,7 +59,7 @@ ParticleView::ParticleView(const ParticleField& field, Dali::Actor world, Dali:: } // create shader - Shader particleShader = Shader::New(PARTICLES_VSH, PARTICLES_FSH, Shader::Hint::MODIFIES_GEOMETRY); + Shader particleShader = Shader::New(SHADER_PARTICLE_VIEW_VERT, SHADER_PARTICLE_VIEW_FRAG, Shader::Hint::MODIFIES_GEOMETRY); float zNear = camera.GetNearClippingPlane(); float zFar = camera.GetFarClippingPlane(); @@ -318,7 +134,7 @@ ParticleView::ParticleView(const ParticleField& field, Dali::Actor world, Dali:: #ifdef ENABLE_DEBUG_VOLUME Geometry cubeGeom = CreateCuboidWireframeGeometry(); - renderer = CreateRenderer(renderer.GetTextures(), cubeGeom, Shader::New(SIMPLE_VSH, SIMPLE_FSH)); + renderer = CreateRenderer(renderer.GetTextures(), cubeGeom, Shader::New(SHADER_PARTICLE_VIEW_SIMPLE_VERT, SHADER_PARTICLE_VIEW_SIMPLE_FRAG)); masterParticles.AddRenderer(renderer); #endif diff --git a/examples/particles/shaders/particle-view-simple.frag b/examples/particles/shaders/particle-view-simple.frag new file mode 100644 index 0000000..e2611af --- /dev/null +++ b/examples/particles/shaders/particle-view-simple.frag @@ -0,0 +1,12 @@ +// Shader for an unlit, unfogged, textured mesh. + +#version 300 es + +precision mediump float; +uniform vec4 uColor; +out vec4 oFragColor; + +void main() +{ + oFragColor = uColor; +} diff --git a/examples/particles/shaders/particle-view-simple.vert b/examples/particles/shaders/particle-view-simple.vert new file mode 100644 index 0000000..674ad4d --- /dev/null +++ b/examples/particles/shaders/particle-view-simple.vert @@ -0,0 +1,13 @@ +// Shader for simple textured geometry. + +#version 300 es + +precision mediump float; +uniform mat4 uMvpMatrix;//by DALi +uniform vec3 uSize; // by DALi +in vec3 aPosition; + +void main() +{ + gl_Position = uMvpMatrix * vec4(aPosition * uSize, 1.f); +} diff --git a/examples/particles/shaders/particle-view.frag b/examples/particles/shaders/particle-view.frag new file mode 100644 index 0000000..c539513 --- /dev/null +++ b/examples/particles/shaders/particle-view.frag @@ -0,0 +1,40 @@ +// Fragment shader for particles, which simulates depth of field using +// a combination of procedural texturing, alpha testing and alpha blending. + +#version 300 es + +precision lowp float; +uniform float uAlphaTestRefValue; +uniform vec2 uFadeRange; // near, far +in vec2 vUvUnit; +flat in float vDepth; +flat in float vFocalDistance; +flat in float vOpacity; +flat in vec3 vColor; +out vec4 oFragColor; + +const float REF_VALUE_THRESHOLD = 1. / 64.; + +void main() +{ + // Softened disc pattern from normalized UVs + float value = 1.f - dot(vUvUnit, vUvUnit); + + // Decrease area of particles 'in-focus'. + float refValue = (1.f - vFocalDistance) * .5f; + float threshold = REF_VALUE_THRESHOLD * (1.f + vDepth); + float alpha = pow(value, vFocalDistance) * smoothstep(refValue - threshold, refValue + threshold, value); + if (alpha < uAlphaTestRefValue) + { + discard; + } + + // Apply opacity + alpha *= vOpacity; + alpha *= alpha; + + // Fade particles out as they get close to the near and far clipping planes + alpha *= smoothstep(.0f, uFadeRange.x, vDepth) * smoothstep(1.f, uFadeRange.y, vDepth); + + oFragColor = vec4(vColor, alpha); +} diff --git a/examples/particles/shaders/particle-view.vert b/examples/particles/shaders/particle-view.vert new file mode 100644 index 0000000..6d61044 --- /dev/null +++ b/examples/particles/shaders/particle-view.vert @@ -0,0 +1,126 @@ +// Shader for billboarded particles, where the vertices of the particles +// are supplied as vec3 position (particle position) + vec2 sub-position. + +#version 300 es + +precision lowp float; +uniform mat4 uModelView; // DALi +uniform mat4 uProjection; // DALi +uniform vec3 uSize; // DALi +uniform vec4 uColor; // DALi + +uniform vec3 uSecondaryColor; +uniform vec2 uDepthRange; // x is zNear, y is 1.f / (zFar - zNear) +uniform float uTwinkleFrequency; +uniform float uTwinkleSizeScale; +uniform float uTwinkleOpacityWeight; +uniform float uTime; +uniform float uFocalLength; +uniform float uAperture; +uniform float uPopulation; + +struct Scatter +{ + float radiusSqr; + float amount; + vec3 ray; +}; + +const int SCATTER_VARS = 6; // Must match ParticleView::mScatterProps' size. +uniform Scatter uScatter[SCATTER_VARS]; + +const int POPULATION_GRANULARITY = 128; +uniform float uOrderLookUp[POPULATION_GRANULARITY]; + +in vec3 aPosition; +in float aSeed; +in vec4 aPath; +in vec2 aSubPosition; +in float aSize; + +flat out float vDepth; +flat out float vFocalDistance; +out vec2 vUvUnit; +flat out float vOpacity; +flat out vec3 vColor; // ignore alpha + +float bezier(vec3 control, float alpha) +{ + return mix(mix(control.x, control.y, alpha), mix(control.y, control.z, alpha), alpha); +} + +void main() { + // Get random order from the look-up table, based on particle ID. + int particleId = gl_VertexID / 6; + float order = uOrderLookUp[particleId & (POPULATION_GRANULARITY - 1)]; + + // Get twinkle scalar + float twinkle = sin(uTime * floor(uTwinkleFrequency * aSeed) + fract(aSeed * 1.17137)); + + // Add Motion + float s = sin(uTime + aSeed) * .5f + .5f; // different phase for all + // NOTE: you'd think that taking the bezier() calls apart would save 4 mix() calls, since + // the mix()es (of xy / yz / zw / wx) are all calculated twice. It turns out that the MALI + // compiler is already doing this; leaving it as is for readability. + float bx0 = bezier(aPath.xyz, s); + float bx1 = bezier(aPath.zwx, s); + float by0 = bezier(aPath.yzw, s); + float by1 = bezier(aPath.wxy, s); + vec3 motion = vec3(mix(bx0, bx1, s), mix(by0, by1, s), 0.f); + + // Model to view position + vec3 position3 = aPosition * uSize + motion; + + vec4 position = uModelView * vec4(position3, 1.f); + + // Add scatter - calculated in view space, using view ray + vec3 normalizedPos = position.xyz / uSize; + for (int i = 0; i < SCATTER_VARS; ++i) + { + vec2 scatterDist = (normalizedPos - uScatter[i].ray * dot(uScatter[i].ray, normalizedPos)).xy; + + // NOTE: replacing the division with a multiplication (by inverse) oddly results in more instructions (MALI). + float scatter = max(0.f, uScatter[i].radiusSqr - dot(scatterDist, scatterDist)) * + uScatter[i].amount / aSize; + position.xy += scatter * normalize(scatterDist) * uSize.xy; + } + + // Calculate normalised depth and distance from focal plane + float depth = (position.z - uDepthRange.x) * uDepthRange.y; + vDepth = depth; + + float focalDist = (uFocalLength - depth) * uAperture; + focalDist *= focalDist; + vFocalDistance = max(focalDist, 1e-6f); // NOTE: was clamp(..., 1.f); side effect: out of focus particles get squashed at higher aperture values. + + // Calculate expiring scale - for size and opacity. + float expiringScale = smoothstep(order + 1.f, order, uPopulation); + + // Calculate billboard position and size + vec2 subPosition = aSubPosition * aSize * + (1.f + twinkle * aSeed * uTwinkleSizeScale) * + expiringScale; + + // Insist on hacking the size? Do it here... + float sizeHack = depth + .5f; + // NOTE: sizeHack *= sizeHack looked slightly better. + subPosition *= sizeHack; + + vec3 subPositionView = vec3(subPosition, 0.); + + // Add billboards to view position. + position += vec4(subPositionView, 0.f); + + // subPosition doubles as normalized (-1..1) UV. + vUvUnit = aSubPosition; + + // Vary opacity (actor alpha) by time as well as expiring scale. + vOpacity = uColor.a * expiringScale * + (1.0f + aSeed + twinkle * uTwinkleOpacityWeight) / (2.0f + uTwinkleOpacityWeight); + + // Randomize RGB using seed. + vec3 mixColor = vec3(fract(aSeed), fract(aSeed * 16.f), fract(aSeed * 256.f)); + vColor = mix(uColor.rgb, uSecondaryColor, mixColor); + + gl_Position = uProjection * position; +} diff --git a/examples/perf-scroll/perf-scroll.cpp b/examples/perf-scroll/perf-scroll.cpp index 798194a..dc6cdc8 100644 --- a/examples/perf-scroll/perf-scroll.cpp +++ b/examples/perf-scroll/perf-scroll.cpp @@ -18,6 +18,8 @@ #include #include #include "shared/utility.h" +#include "generated/perf-scroll-vert.h" +#include "generated/perf-scroll-frag.h" using namespace Dali; using namespace Dali::Toolkit; @@ -147,35 +149,6 @@ struct VertexWithTexture Vector2 texCoord; }; -// clang-format off - -const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - attribute mediump vec2 aPosition;\n - attribute mediump vec2 aTexCoord;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump vec3 uSize;\n - varying mediump vec2 vTexCoord;\n - void main()\n - {\n - vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n - gl_Position = uMvpMatrix * position;\n - vTexCoord = aTexCoord;\n - }\n -); - -const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture;\n - varying mediump vec2 vTexCoord;\n - - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n - }\n -); - -// clang-format on - bool gUseMesh(false); bool gNinePatch(false); unsigned int gRowsPerPage(15); @@ -294,7 +267,7 @@ public: //Create all the renderers std::vector renderers(numImages); - Shader shader = Shader::New(VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE); + Shader shader = Shader::New(SHADER_PERF_SCROLL_VERT, SHADER_PERF_SCROLL_FRAG); Geometry geometry = DemoHelper::CreateTexturedQuad(); for(unsigned int i(0); i < numImages; ++i) { diff --git a/examples/perf-scroll/shaders/perf-scroll.frag b/examples/perf-scroll/shaders/perf-scroll.frag new file mode 100644 index 0000000..f9eba48 --- /dev/null +++ b/examples/perf-scroll/shaders/perf-scroll.frag @@ -0,0 +1,8 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} diff --git a/examples/perf-scroll/shaders/perf-scroll.vert b/examples/perf-scroll/shaders/perf-scroll.vert new file mode 100644 index 0000000..3cb2121 --- /dev/null +++ b/examples/perf-scroll/shaders/perf-scroll.vert @@ -0,0 +1,12 @@ +attribute mediump vec2 aPosition; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +varying mediump vec2 vTexCoord; + +void main() +{ + vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0); + gl_Position = uMvpMatrix * position; + vTexCoord = aTexCoord; +} diff --git a/examples/point-mesh/point-mesh-example.cpp b/examples/point-mesh/point-mesh-example.cpp index 37fb023..1e66ab5 100644 --- a/examples/point-mesh/point-mesh-example.cpp +++ b/examples/point-mesh/point-mesh-example.cpp @@ -21,6 +21,8 @@ // INTERNAL INCLUDES #include "shared/utility.h" #include "shared/view.h" +#include "generated/point-mesh-vert.h" +#include "generated/point-mesh-frag.h" using namespace Dali; @@ -29,49 +31,6 @@ namespace const char* MATERIAL_SAMPLE(DEMO_IMAGE_DIR "gallery-small-48.jpg"); const char* MATERIAL_SAMPLE2(DEMO_IMAGE_DIR "gallery-medium-19.jpg"); -#define MAKE_SHADER(A) #A - -const char* VERTEX_SHADER = MAKE_SHADER( - attribute mediump vec2 aPosition; - attribute highp float aHue; - varying mediump vec2 vTexCoord; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform mediump float uPointSize; - uniform lowp vec4 uFadeColor; - varying mediump vec3 vVertexColor; - varying mediump float vHue; - - vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); - } - - void main() { - mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); - vertexPosition.xyz *= (uSize - uPointSize); - vertexPosition = uMvpMatrix * vertexPosition; - vVertexColor = hsv2rgb(vec3(aHue, 0.7, 1.0)); - vHue = aHue; - gl_PointSize = uPointSize; - gl_Position = vertexPosition; - }); - -const char* FRAGMENT_SHADER = MAKE_SHADER( - varying mediump vec3 vVertexColor; - varying mediump float vHue; - uniform lowp vec4 uColor; - uniform sampler2D sTexture1; - uniform sampler2D sTexture2; - uniform lowp vec4 uFadeColor; - - void main() { - mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord); - mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord); - gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1 * vHue) + (texCol2 * (1.0 - vHue))); - }); - Geometry CreateGeometry() { // Create vertices @@ -150,7 +109,7 @@ public: Texture texture0 = DemoHelper::LoadTexture(MATERIAL_SAMPLE); Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2); - Shader shader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + Shader shader = Shader::New(SHADER_POINT_MESH_VERT, SHADER_POINT_MESH_FRAG); TextureSet textureSet = TextureSet::New(); textureSet.SetTexture(0u, texture0); diff --git a/examples/point-mesh/shaders/point-mesh.frag b/examples/point-mesh/shaders/point-mesh.frag new file mode 100644 index 0000000..51be239 --- /dev/null +++ b/examples/point-mesh/shaders/point-mesh.frag @@ -0,0 +1,13 @@ +varying mediump vec3 vVertexColor; +varying mediump float vHue; +uniform lowp vec4 uColor; +uniform sampler2D sTexture1; +uniform sampler2D sTexture2; +uniform lowp vec4 uFadeColor; + +void main() +{ + mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord); + mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord); + gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1 * vHue) + (texCol2 * (1.0 - vHue))); +} diff --git a/examples/point-mesh/shaders/point-mesh.vert b/examples/point-mesh/shaders/point-mesh.vert new file mode 100644 index 0000000..6afbdb4 --- /dev/null +++ b/examples/point-mesh/shaders/point-mesh.vert @@ -0,0 +1,27 @@ +attribute mediump vec2 aPosition; +attribute highp float aHue; +varying mediump vec2 vTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform mediump float uPointSize; +uniform lowp vec4 uFadeColor; +varying mediump vec3 vVertexColor; +varying mediump float vHue; + +vec3 hsv2rgb(vec3 c) +{ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= (uSize - uPointSize); + vertexPosition = uMvpMatrix * vertexPosition; + vVertexColor = hsv2rgb(vec3(aHue, 0.7, 1.0)); + vHue = aHue; + gl_PointSize = uPointSize; + gl_Position = vertexPosition; +} diff --git a/examples/reflection-demo/reflection-example.cpp b/examples/reflection-demo/reflection-example.cpp index 6a44fda..f7f50ae 100644 --- a/examples/reflection-demo/reflection-example.cpp +++ b/examples/reflection-demo/reflection-example.cpp @@ -22,146 +22,17 @@ #include #include "gltf-scene.h" +#include "generated/reflection-vert.h" +#include "generated/reflection-frag.h" +#include "generated/reflection-simple-frag.h" +#include "generated/reflection-textured-frag.h" +#include "generated/reflection-plasma-frag.h" +#include "generated/reflection-tex-frag.h" using namespace Dali; namespace { -// clang-format off - -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute mediump vec3 aPosition;\n - attribute mediump vec3 aNormal;\n - attribute mediump vec2 aTexCoord;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump mat3 uNormalMatrix;\n - uniform mediump vec3 uSize;\n - \n - varying mediump vec2 vTexCoord; \n - varying mediump vec3 vNormal; \n - varying mediump vec3 vPosition; \n - void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vertexPosition.xyz *= uSize;\n - vTexCoord = aTexCoord;\n - vNormal = normalize(uNormalMatrix * aNormal);\n - vPosition = aPosition; \n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture; \n - varying mediump vec3 vNormal;\n - varying mediump vec3 vPosition; \n - varying mediump vec2 vTexCoord; \n - \n - void main()\n -{\n - gl_FragColor = texture2D(sTexture, vTexCoord) * 50.0;\n -}\n -); - -const char* FRAGMENT_SIMPLE_SHADER = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture; \n - varying mediump vec3 vNormal;\n - varying mediump vec3 vPosition; \n - varying mediump vec2 vTexCoord; \n - \n - void main()\n -{\n - gl_FragColor = texture2D(sTexture, vTexCoord) * 2.0;\n -}\n -); - -const char* TEXTURED_FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture; \n - uniform mediump vec2 uScreenSize;\n - - uniform mediump vec3 eyePos;\n - uniform mediump vec3 lightDir;\n - - varying mediump vec3 vNormal;\n - varying mediump vec3 vPosition; \n - varying mediump vec2 vTexCoord; \n - \n - void main()\n -{\n - mediump vec3 n = normalize( vNormal );\n - mediump vec3 l = normalize( lightDir );\n - mediump vec3 e = normalize( eyePos );\n - mediump float intensity = max(dot(n,l), 0.0);\n - gl_FragColor = texture2D(sTexture, vTexCoord) * intensity;\n -}\n -); - -const char* PLASMA_FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - precision mediump float;\n - uniform sampler2D sTexture; \n - - uniform float uTime; - uniform float uKFactor; - uniform mediump vec3 eyePos;\n - uniform mediump vec3 lightDir;\n - varying mediump vec3 vNormal;\n - varying mediump vec3 vPosition; \n - varying mediump vec2 vTexCoord; \n - \n - void main()\n -{\n - mediump vec3 n = normalize( vNormal );\n - mediump vec3 l = normalize( lightDir );\n - mediump vec3 e = normalize( eyePos );\n - mediump float intensity = max(dot(n,l), 0.0);\n -\n - const mediump float PI = 3.1415926535897932384626433832795;\n - mediump float v = 0.0;\n - mediump vec2 c = vTexCoord * uKFactor - uKFactor/2.0;\n - v += sin((c.x+uTime));\n - v += sin((c.y+uTime)/2.0);\n - v += sin((c.x+c.y+uTime)/2.0);\n - c += uKFactor/2.0 * vec2(sin(uTime/3.0), cos(uTime/2.0));\n - v += sin(sqrt(c.x*c.x+c.y*c.y+1.0)+uTime);\n - v = v/2.0;\n - mediump vec3 col = vec3(1, sin(PI*v), cos(PI*v));\n - gl_FragColor = (texture2D(sTexture, vTexCoord)) * (((col.r+col.g+col.b)/3.0)+1.0+intensity);\n -}\n -); - -const char* TEX_FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - uniform lowp vec4 uColor;\n - uniform sampler2D sTexture0; \n - uniform sampler2D sTexture1; \n - uniform mediump vec3 eyePos;\n - uniform mediump vec3 lightDir;\n - uniform mediump vec2 uScreenSize;\n - varying mediump vec3 vNormal;\n - varying mediump vec3 vPosition; \n - varying mediump vec2 vTexCoord; \n - \n - - mediump float rand(mediump vec2 co){\n - return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n - }\n - \n - void main()\n -{\n - mediump vec2 tx = (gl_FragCoord.xy / uScreenSize.xy);\n - mediump vec3 n = normalize( vNormal );\n - mediump vec3 l = normalize( lightDir );\n - mediump vec3 e = normalize( eyePos );\n - mediump float factor = gl_FragCoord.y / uScreenSize.y;\n - mediump float intensity = max(dot(n,l), 0.0);\n - mediump vec2 uv = tx;\n - gl_FragColor = ((texture2D(sTexture0, vTexCoord) * factor ) + \n - (texture2D(sTexture1, uv))) * intensity;\n -}\n -); -// clang-format on struct Model { @@ -335,11 +206,11 @@ void CreateModelsFromGLTF(glTF* gltf, ModelContainer& models) // change shader to use texture if material indicates that if(mesh->material != 0xffffffff && gltf->GetMaterials()[mesh->material].pbrMetallicRoughness.enabled) { - models.emplace_back(CreateModel(*gltf, mesh, VERTEX_SHADER, TEXTURED_FRAGMENT_SHADER)); + models.emplace_back(CreateModel(*gltf, mesh, SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_TEXTURED_FRAG.data())); } else { - models.emplace_back(CreateModel(*gltf, mesh, VERTEX_SHADER, FRAGMENT_SHADER)); + models.emplace_back(CreateModel(*gltf, mesh, SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_FRAG.data())); } } } @@ -500,13 +371,13 @@ private: auto planeActor = mLayer3D.FindChildByName("Plane"); auto solarActor = mLayer3D.FindChildByName("solar_root"); auto backgroundActor = mLayer3D.FindChildByName("background"); - ReplaceShader(backgroundActor, VERTEX_SHADER, FRAGMENT_SIMPLE_SHADER); + ReplaceShader(backgroundActor, SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_SIMPLE_FRAG.data()); mCenterActor = mLayer3D.FindChildByName("center"); mCenterHorizActor = mLayer3D.FindChildByName("center2"); // Prepare Sun auto sun = mLayer3D.FindChildByName("sun"); - ReplaceShader(sun, VERTEX_SHADER, PLASMA_FRAGMENT_SHADER); + ReplaceShader(sun, SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_PLASMA_FRAG.data()); mSunTimeUniformIndex = sun.RegisterProperty("uTime", 0.0f); mSunKFactorUniformIndex = sun.RegisterProperty("uKFactor", 0.0f); @@ -515,7 +386,7 @@ private: // Milkyway auto milkyway = mLayer3D.FindChildByName("milkyway"); - ReplaceShader(milkyway, VERTEX_SHADER, FRAGMENT_SHADER); + ReplaceShader(milkyway, SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_FRAG.data()); auto renderTaskSourceActor = mLayer3D.FindChildByName("RenderTaskSource"); @@ -544,7 +415,7 @@ private: /** * Change shader to textured */ - Shader texShader = CreateShader(VERTEX_SHADER, TEX_FRAGMENT_SHADER); + Shader texShader = CreateShader(SHADER_REFLECTION_VERT.data(), SHADER_REFLECTION_TEX_FRAG.data()); planeActor.RegisterProperty("uScreenSize", Vector2(windowWidth, windowHeight)); auto renderer = planeActor.GetRendererAt(0); auto textureSet = renderer.GetTextures(); diff --git a/examples/reflection-demo/shaders/reflection-plasma.frag b/examples/reflection-demo/shaders/reflection-plasma.frag new file mode 100644 index 0000000..47b0055 --- /dev/null +++ b/examples/reflection-demo/shaders/reflection-plasma.frag @@ -0,0 +1,30 @@ +precision mediump float; +uniform sampler2D sTexture; + +uniform float uTime; +uniform float uKFactor; +uniform mediump vec3 eyePos; +uniform mediump vec3 lightDir; +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec3 n = normalize( vNormal ); + mediump vec3 l = normalize( lightDir ); + mediump vec3 e = normalize( eyePos ); + mediump float intensity = max(dot(n,l), 0.0); + + const mediump float PI = 3.1415926535897932384626433832795; + mediump float v = 0.0; + mediump vec2 c = vTexCoord * uKFactor - uKFactor/2.0; + v += sin((c.x+uTime)); + v += sin((c.y+uTime)/2.0); + v += sin((c.x+c.y+uTime)/2.0); + c += uKFactor/2.0 * vec2(sin(uTime/3.0), cos(uTime/2.0)); + v += sin(sqrt(c.x*c.x+c.y*c.y+1.0)+uTime); + v = v/2.0; + mediump vec3 col = vec3(1, sin(PI*v), cos(PI*v)); + gl_FragColor = (texture2D(sTexture, vTexCoord)) * (((col.r+col.g+col.b)/3.0)+1.0+intensity); +} diff --git a/examples/reflection-demo/shaders/reflection-simple.frag b/examples/reflection-demo/shaders/reflection-simple.frag new file mode 100644 index 0000000..1c813ae --- /dev/null +++ b/examples/reflection-demo/shaders/reflection-simple.frag @@ -0,0 +1,10 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D(sTexture, vTexCoord) * 2.0; +} diff --git a/examples/reflection-demo/shaders/reflection-tex.frag b/examples/reflection-demo/shaders/reflection-tex.frag new file mode 100644 index 0000000..8f792cc --- /dev/null +++ b/examples/reflection-demo/shaders/reflection-tex.frag @@ -0,0 +1,27 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture0; +uniform sampler2D sTexture1; +uniform mediump vec3 eyePos; +uniform mediump vec3 lightDir; +uniform mediump vec2 uScreenSize; +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; +varying mediump vec2 vTexCoord; + +mediump float rand(mediump vec2 co) +{ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} + +void main() +{ + mediump vec2 tx = (gl_FragCoord.xy / uScreenSize.xy); + mediump vec3 n = normalize( vNormal ); + mediump vec3 l = normalize( lightDir ); + mediump vec3 e = normalize( eyePos ); + mediump float factor = gl_FragCoord.y / uScreenSize.y; + mediump float intensity = max(dot(n,l), 0.0); + mediump vec2 uv = tx; + gl_FragColor = ((texture2D(sTexture0, vTexCoord) * factor ) + + (texture2D(sTexture1, uv))) * intensity; +} diff --git a/examples/reflection-demo/shaders/reflection-textured.frag b/examples/reflection-demo/shaders/reflection-textured.frag new file mode 100644 index 0000000..8eb6061 --- /dev/null +++ b/examples/reflection-demo/shaders/reflection-textured.frag @@ -0,0 +1,19 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +uniform mediump vec2 uScreenSize; + +uniform mediump vec3 eyePos; +uniform mediump vec3 lightDir; + +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec3 n = normalize( vNormal ); + mediump vec3 l = normalize( lightDir ); + mediump vec3 e = normalize( eyePos ); + mediump float intensity = max(dot(n,l), 0.0); + gl_FragColor = texture2D(sTexture, vTexCoord) * intensity; +} diff --git a/examples/reflection-demo/shaders/reflection.frag b/examples/reflection-demo/shaders/reflection.frag new file mode 100644 index 0000000..55060a8 --- /dev/null +++ b/examples/reflection-demo/shaders/reflection.frag @@ -0,0 +1,10 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D(sTexture, vTexCoord) * 50.0; +} diff --git a/examples/reflection-demo/shaders/reflection.vert b/examples/reflection-demo/shaders/reflection.vert new file mode 100644 index 0000000..872da7d --- /dev/null +++ b/examples/reflection-demo/shaders/reflection.vert @@ -0,0 +1,20 @@ +attribute mediump vec3 aPosition; +attribute mediump vec3 aNormal; +attribute mediump vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump mat3 uNormalMatrix; +uniform mediump vec3 uSize; + +varying mediump vec2 vTexCoord; +varying mediump vec3 vNormal; +varying mediump vec3 vPosition; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vertexPosition.xyz *= uSize; + vTexCoord = aTexCoord; + vNormal = normalize(uNormalMatrix * aNormal); + vPosition = aPosition; + gl_Position = uMvpMatrix * vertexPosition; +} \ No newline at end of file diff --git a/examples/refraction-effect/refraction-effect-example.cpp b/examples/refraction-effect/refraction-effect-example.cpp index 7587ffe..cbea48b 100644 --- a/examples/refraction-effect/refraction-effect-example.cpp +++ b/examples/refraction-effect/refraction-effect-example.cpp @@ -28,6 +28,10 @@ // INTERNAL INCLUDES #include "shared/utility.h" #include "shared/view.h" +#include "generated/refraction-effect-flat-vert.h" +#include "generated/refraction-effect-flat-frag.h" +#include "generated/refraction-effect-refraction-vert.h" +#include "generated/refraction-effect-refraction-frag.h" using namespace Dali; @@ -94,117 +98,6 @@ struct Vertex } }; -/************************************************************************************************ - *** The shader source is used when the MeshActor is not touched*** - ************************************************************************************************/ -// clang-format off -const char* VERTEX_SHADER_FLAT = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n -attribute mediump vec3 aNormal;\n -attribute highp vec2 aTexCoord;\n -uniform mediump mat4 uMvpMatrix;\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n - vTexCoord = aTexCoord.xy;\n -}\n -); - -const char* FRAGMENT_SHADER_FLAT = DALI_COMPOSE_SHADER( -uniform lowp vec4 uColor;\n -uniform sampler2D sTexture;\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n -}\n -); - -/************************************************************ - ** Custom refraction effect shader*************************** - ************************************************************/ -const char* VERTEX_SHADER_REFRACTION = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n -attribute mediump vec3 aNormal;\n -attribute highp vec2 aTexCoord;\n -uniform mediump mat4 uMvpMatrix;\n -varying mediump vec4 vVertex;\n -varying mediump vec3 vNormal;\n -varying mediump vec2 vTexCoord;\n -varying mediump vec2 vTextureOffset;\n -void main()\n -{\n - gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n - vTexCoord = aTexCoord.xy;\n - - vNormal = aNormal;\n - vVertex = vec4( aPosition, 1.0 );\n - float length = max(0.01, length(aNormal.xy)) * 40.0;\n - vTextureOffset = aNormal.xy / length;\n -}\n -); - -const char* FRAGMENT_SHADER_REFRACTION = DALI_COMPOSE_SHADER( -precision mediump float;\n -uniform mediump float uEffectStrength;\n -uniform mediump vec3 uLightPosition;\n -uniform mediump vec2 uLightXYOffset;\n -uniform mediump vec2 uLightSpinOffset;\n -uniform mediump float uLightIntensity;\n -uniform lowp vec4 uColor;\n -uniform sampler2D sTexture;\n -varying mediump vec4 vVertex;\n -varying mediump vec3 vNormal;\n -varying mediump vec2 vTexCoord;\n -varying mediump vec2 vTextureOffset;\n - -vec3 rgb2hsl(vec3 rgb)\n -{\n - float epsilon = 1.0e-10;\n - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n - vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));\n - vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r));\n - \n - // RGB -> HCV - float value = Q.x;\n - float chroma = Q.x - min(Q.w, Q.y);\n - float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon));\n - // HCV -> HSL - float lightness = value - chroma*0.5;\n - return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness );\n -}\n - -vec3 hsl2rgb( vec3 hsl )\n -{\n - // pure hue->RGB - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n - vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www);\n - vec3 RGB = clamp(p - K.xxx, 0.0, 1.0);\n - \n - float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y;\n - return ( RGB - 0.5 ) * chroma + hsl.z;\n -}\n - -void main()\n -{\n - vec3 normal = normalize( vNormal);\n - - vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0);\n - mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 );\n - mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0);\n - - spotEffect = spotEffect * uEffectStrength;\n - mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity ) * spotEffect;\n - - lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect );\n - vec3 lightedColor = hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) );\n - - gl_FragColor = vec4( lightedColor, color.a ) * uColor;\n -}\n -); -// clang-format on - } // namespace /*************************************************/ @@ -281,7 +174,7 @@ private: DemoHelper::DEFAULT_MODE_SWITCH_PADDING); // shader used when the screen is not touched, render a flat surface - mShaderFlat = Shader::New(VERTEX_SHADER_FLAT, FRAGMENT_SHADER_FLAT); + mShaderFlat = Shader::New(SHADER_REFRACTION_EFFECT_FLAT_VERT, SHADER_REFRACTION_EFFECT_FLAT_FRAG); mGeometry = CreateGeometry(MESH_FILES[mCurrentMeshId]); Texture texture = DemoHelper::LoadWindowFillingTexture(window.GetSize(), TEXTURE_IMAGES[mCurrentTextureId]); @@ -301,7 +194,7 @@ private: mContent.TouchedSignal().Connect(this, &RefractionEffectExample::OnTouch); // shader used when the finger is touching the screen. render refraction effect - mShaderRefraction = Shader::New(VERTEX_SHADER_REFRACTION, FRAGMENT_SHADER_REFRACTION); + mShaderRefraction = Shader::New(SHADER_REFRACTION_EFFECT_REFRACTION_VERT, SHADER_REFRACTION_EFFECT_REFRACTION_FRAG); // register uniforms mLightXYOffsetIndex = mMeshActor.RegisterProperty("uLightXYOffset", Vector2::ZERO); diff --git a/examples/refraction-effect/shaders/refraction-effect-flat.frag b/examples/refraction-effect/shaders/refraction-effect-flat.frag new file mode 100644 index 0000000..f9eba48 --- /dev/null +++ b/examples/refraction-effect/shaders/refraction-effect-flat.frag @@ -0,0 +1,8 @@ +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor; +} diff --git a/examples/refraction-effect/shaders/refraction-effect-flat.vert b/examples/refraction-effect/shaders/refraction-effect-flat.vert new file mode 100644 index 0000000..fc35d09 --- /dev/null +++ b/examples/refraction-effect/shaders/refraction-effect-flat.vert @@ -0,0 +1,13 @@ +// The shader source is used when the MeshActor is not touched + +attribute mediump vec3 aPosition; +attribute mediump vec3 aNormal; +attribute highp vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +varying mediump vec2 vTexCoord; + +void main() +{ + gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 ); + vTexCoord = aTexCoord.xy; +} diff --git a/examples/refraction-effect/shaders/refraction-effect-refraction.frag b/examples/refraction-effect/shaders/refraction-effect-refraction.frag new file mode 100644 index 0000000..8fcf92d --- /dev/null +++ b/examples/refraction-effect/shaders/refraction-effect-refraction.frag @@ -0,0 +1,56 @@ +precision mediump float; +uniform mediump float uEffectStrength; +uniform mediump vec3 uLightPosition; +uniform mediump vec2 uLightXYOffset; +uniform mediump vec2 uLightSpinOffset; +uniform mediump float uLightIntensity; +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +varying mediump vec4 vVertex; +varying mediump vec3 vNormal; +varying mediump vec2 vTexCoord; +varying mediump vec2 vTextureOffset; + +vec3 rgb2hsl(vec3 rgb) +{ + float epsilon = 1.0e-10; + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g)); + vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r)); + + // RGB -> HCV + float value = Q.x; + float chroma = Q.x - min(Q.w, Q.y); + float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon)); + // HCV -> HSL + float lightness = value - chroma*0.5; + return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness ); +} + +vec3 hsl2rgb( vec3 hsl ) +{ + // pure hue->RGB + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www); + vec3 RGB = clamp(p - K.xxx, 0.0, 1.0); + + float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y; + return ( RGB - 0.5 ) * chroma + hsl.z; +} + +void main() +{ + vec3 normal = normalize( vNormal); + + vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0); + mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 ); + mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0); + + spotEffect = spotEffect * uEffectStrength; + mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity ) * spotEffect; + + lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect ); + vec3 lightedColor = hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) ); + + gl_FragColor = vec4( lightedColor, color.a ) * uColor; +} diff --git a/examples/refraction-effect/shaders/refraction-effect-refraction.vert b/examples/refraction-effect/shaders/refraction-effect-refraction.vert new file mode 100644 index 0000000..0061123 --- /dev/null +++ b/examples/refraction-effect/shaders/refraction-effect-refraction.vert @@ -0,0 +1,19 @@ +attribute mediump vec3 aPosition; +attribute mediump vec3 aNormal; +attribute highp vec2 aTexCoord; +uniform mediump mat4 uMvpMatrix; +varying mediump vec4 vVertex; +varying mediump vec3 vNormal; +varying mediump vec2 vTexCoord; +varying mediump vec2 vTextureOffset; + +void main() +{ + gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 ); + vTexCoord = aTexCoord.xy; + + vNormal = aNormal; + vVertex = vec4( aPosition, 1.0 ); + float length = max(0.01, length(aNormal.xy)) * 40.0; + vTextureOffset = aNormal.xy / length; +} diff --git a/examples/renderer-stencil/renderer-stencil-example.cpp b/examples/renderer-stencil/renderer-stencil-example.cpp index f324ea7..2290639 100644 --- a/examples/renderer-stencil/renderer-stencil-example.cpp +++ b/examples/renderer-stencil/renderer-stencil-example.cpp @@ -16,12 +16,16 @@ */ // EXTERNAL INCLUDES +#include #include // INTERNAL INCLUDES -#include "renderer-stencil-shaders.h" #include "shared/utility.h" #include "shared/view.h" +#include "generated/render-stencil-vert.h" +#include "generated/render-stencil-frag.h" +#include "generated/render-stencil-textured-vert.h" +#include "generated/render-stencil-textured-frag.h" using namespace Dali; @@ -57,6 +61,14 @@ const Vector4 REFLECTION_COLOR(0.6f, 0.6f, 0.6f, 0.6f); ///< Note that alpha is // We need to control the draw order as we are controlling both the stencil and depth buffer per renderer. const int DEPTH_INDEX_GRANULARITY(10000); ///< This value is the gap in depth-index in-between each renderer. +// Shader uniforms: +const char* const COLOR_UNIFORM_NAME("uColor"); +const char* const OBJECT_DIMENSIONS_UNIFORM_NAME("uObjectDimensions"); +const char* const LIGHT_POSITION_UNIFORM_NAME = "uLightPosition"; +const char* const POSITION("aPosition"); +const char* const NORMAL("aNormal"); +const char* const TEXTURE("aTexCoord"); + } // Anonymous namespace /** @@ -477,11 +489,11 @@ private: if(textured) { - shader = Shader::New(VERTEX_SHADER_TEXTURED, FRAGMENT_SHADER_TEXTURED); + shader = Shader::New(SHADER_RENDER_STENCIL_TEXTURED_VERT, SHADER_RENDER_STENCIL_TEXTURED_FRAG); } else { - shader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + shader = Shader::New(SHADER_RENDER_STENCIL_VERT, SHADER_RENDER_STENCIL_FRAG); } // Here we modify the light position based on half the window size as a pre-calculation step. diff --git a/examples/renderer-stencil/renderer-stencil-shaders.h b/examples/renderer-stencil/renderer-stencil-shaders.h deleted file mode 100644 index 68c4dc1..0000000 --- a/examples/renderer-stencil/renderer-stencil-shaders.h +++ /dev/null @@ -1,128 +0,0 @@ -#ifndef DALI_DEMO_RENDERER_STENCIL_SHADERS_H -#define DALI_DEMO_RENDERER_STENCIL_SHADERS_H - -/* - * 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. - * 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. - */ - -// EXTERNAL INCLUDES -#include - -// Shader uniforms: -const char* const COLOR_UNIFORM_NAME("uColor"); -const char* const OBJECT_DIMENSIONS_UNIFORM_NAME("uObjectDimensions"); -const char* const LIGHT_POSITION_UNIFORM_NAME = "uLightPosition"; -const char* const POSITION("aPosition"); -const char* const NORMAL("aNormal"); -const char* const TEXTURE("aTexCoord"); - -// clang-format off - -// Shader for basic, per-vertex lighting (vertex): -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute mediump vec3 aPosition; - attribute highp vec3 aNormal; - attribute highp vec2 aTexCoord; - - varying mediump vec2 vTexCoord; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform mediump vec3 uObjectDimensions; - varying mediump vec3 vIllumination; - uniform mediump mat4 uModelView; - uniform mediump mat4 uViewMatrix; - uniform mediump mat3 uNormalMatrix; - uniform mediump vec3 uLightPosition; - - void main() - { - mediump vec4 vertexPosition = vec4( aPosition * uObjectDimensions, 1.0 ); - vertexPosition = uMvpMatrix * vertexPosition; - - vec4 mvVertexPosition = uModelView * vertexPosition; - - vec3 vectorToLight = normalize( mat3( uViewMatrix ) * uLightPosition - mvVertexPosition.xyz ); - - vec3 normal = uNormalMatrix * aNormal; - float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 ); - vIllumination = vec3( lightDiffuse * 0.5 + 0.5 ); - - gl_Position = vertexPosition; - } -); - -// Fragment shader. -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - varying mediump vec2 vTexCoord; - varying mediump vec3 vIllumination; - uniform lowp vec4 uColor; - uniform sampler2D sTexture; - - void main() - { - gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a ); - } -); - -// Shader for basic, per-vertex lighting with texture (vertex): -const char* VERTEX_SHADER_TEXTURED = DALI_COMPOSE_SHADER( - attribute mediump vec3 aPosition; - attribute highp vec3 aNormal; - attribute highp vec2 aTexCoord; - - varying mediump vec2 vTexCoord; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform mediump vec3 uObjectDimensions; - varying mediump vec3 vIllumination; - uniform mediump mat4 uModelView; - uniform mediump mat4 uViewMatrix; - uniform mediump mat3 uNormalMatrix; - uniform mediump vec3 uLightPosition; - - void main() - { - mediump vec4 vertexPosition = vec4( aPosition * uObjectDimensions, 1.0 ); - vertexPosition = uMvpMatrix * vertexPosition; - - vec4 mvVertexPosition = uModelView * vertexPosition; - - vec3 vectorToLight = normalize( mat3( uViewMatrix ) * uLightPosition - mvVertexPosition.xyz ); - - vec3 normal = uNormalMatrix * aNormal; - float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 ); - vIllumination = vec3( lightDiffuse * 0.5 + 0.5 ); - - vTexCoord = aTexCoord; - gl_Position = vertexPosition; - } -); - -// Fragment shader. -const char* FRAGMENT_SHADER_TEXTURED = DALI_COMPOSE_SHADER( - varying mediump vec2 vTexCoord; - varying mediump vec3 vIllumination; - uniform lowp vec4 uColor; - uniform sampler2D sTexture; - - void main() - { - gl_FragColor = vec4( texture2D( sTexture, vTexCoord ).rgb * vIllumination.rgb * uColor.rgb, uColor.a ); - } -); - -// clang-format on - -#endif // DALI_DEMO_RENDERER_STENCIL_SHADERS_H diff --git a/examples/renderer-stencil/shaders/render-stencil-textured.frag b/examples/renderer-stencil/shaders/render-stencil-textured.frag new file mode 100644 index 0000000..27c63e5 --- /dev/null +++ b/examples/renderer-stencil/shaders/render-stencil-textured.frag @@ -0,0 +1,9 @@ +varying mediump vec2 vTexCoord; +varying mediump vec3 vIllumination; +uniform lowp vec4 uColor; +uniform sampler2D sTexture; + +void main() +{ + gl_FragColor = vec4( texture2D( sTexture, vTexCoord ).rgb * vIllumination.rgb * uColor.rgb, uColor.a ); +} diff --git a/examples/renderer-stencil/shaders/render-stencil-textured.vert b/examples/renderer-stencil/shaders/render-stencil-textured.vert new file mode 100644 index 0000000..7dd5918 --- /dev/null +++ b/examples/renderer-stencil/shaders/render-stencil-textured.vert @@ -0,0 +1,32 @@ +// Shader for basic, per-vertex lighting with texture (vertex): + +attribute mediump vec3 aPosition; +attribute highp vec3 aNormal; +attribute highp vec2 aTexCoord; + +varying mediump vec2 vTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform mediump vec3 uObjectDimensions; +varying mediump vec3 vIllumination; +uniform mediump mat4 uModelView; +uniform mediump mat4 uViewMatrix; +uniform mediump mat3 uNormalMatrix; +uniform mediump vec3 uLightPosition; + +void main() +{ + mediump vec4 vertexPosition = vec4( aPosition * uObjectDimensions, 1.0 ); + vertexPosition = uMvpMatrix * vertexPosition; + + vec4 mvVertexPosition = uModelView * vertexPosition; + + vec3 vectorToLight = normalize( mat3( uViewMatrix ) * uLightPosition - mvVertexPosition.xyz ); + + vec3 normal = uNormalMatrix * aNormal; + float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 ); + vIllumination = vec3( lightDiffuse * 0.5 + 0.5 ); + + vTexCoord = aTexCoord; + gl_Position = vertexPosition; +} diff --git a/examples/renderer-stencil/shaders/render-stencil.frag b/examples/renderer-stencil/shaders/render-stencil.frag new file mode 100644 index 0000000..12dd2a9 --- /dev/null +++ b/examples/renderer-stencil/shaders/render-stencil.frag @@ -0,0 +1,9 @@ +varying mediump vec2 vTexCoord; +varying mediump vec3 vIllumination; +uniform lowp vec4 uColor; +uniform sampler2D sTexture; + +void main() +{ + gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a ); +} diff --git a/examples/renderer-stencil/shaders/render-stencil.vert b/examples/renderer-stencil/shaders/render-stencil.vert new file mode 100644 index 0000000..06270a9 --- /dev/null +++ b/examples/renderer-stencil/shaders/render-stencil.vert @@ -0,0 +1,31 @@ +// Shader for basic, per-vertex lighting (vertex): + +attribute mediump vec3 aPosition; +attribute highp vec3 aNormal; +attribute highp vec2 aTexCoord; + +varying mediump vec2 vTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform mediump vec3 uObjectDimensions; +varying mediump vec3 vIllumination; +uniform mediump mat4 uModelView; +uniform mediump mat4 uViewMatrix; +uniform mediump mat3 uNormalMatrix; +uniform mediump vec3 uLightPosition; + +void main() +{ + mediump vec4 vertexPosition = vec4( aPosition * uObjectDimensions, 1.0 ); + vertexPosition = uMvpMatrix * vertexPosition; + + vec4 mvVertexPosition = uModelView * vertexPosition; + + vec3 vectorToLight = normalize( mat3( uViewMatrix ) * uLightPosition - mvVertexPosition.xyz ); + + vec3 normal = uNormalMatrix * aNormal; + float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 ); + vIllumination = vec3( lightDiffuse * 0.5 + 0.5 ); + + gl_Position = vertexPosition; +} diff --git a/examples/rendering-basic-light/rendering-basic-light-example.cpp b/examples/rendering-basic-light/rendering-basic-light-example.cpp index 6d92c0d..85588c6 100644 --- a/examples/rendering-basic-light/rendering-basic-light-example.cpp +++ b/examples/rendering-basic-light/rendering-basic-light-example.cpp @@ -18,6 +18,9 @@ #include #include +#include "generated/rendering-basic-light-vert.h" +#include "generated/rendering-basic-light-frag.h" + using namespace Dali; using namespace Toolkit; @@ -66,75 +69,6 @@ Material material[] = int MaterialID = 0; -// clang-format off - -/* - * Vertex shader - */ -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -attribute mediump vec3 aNormal;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -uniform mediump mat4 uModelView;\n // DALi shader builtin -uniform mediump mat3 uNormalMatrix;\n // DALi shader builtin -\n -varying mediump vec3 vNormal;\n -varying mediump vec3 vFragPos;\n -\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vertexPosition.xyz *= uSize;\n - vFragPos = vec3(uModelView * vertexPosition);\n - vNormal = uNormalMatrix * aNormal;\n - \n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader - */ -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( -varying mediump vec3 vNormal;\n -varying mediump vec3 vFragPos;\n -uniform mediump vec3 viewPos;\n // custom uniform -\n -struct Material {\n - mediump vec3 ambient;\n - mediump vec3 diffuse;\n - mediump vec3 specular;\n - mediump float shininess;\n -};\n -struct Light {\n - mediump vec3 position;\n - mediump vec3 color;\n -};\n -uniform Material material;\n // custom uniform -uniform Light light;\n // custom uniform -\n -void main()\n -{\n -\n // Ambient - mediump vec3 ambient = material.ambient * light.color;\n -\n // Diffuse - mediump vec3 norm = normalize(vNormal);\n - mediump vec3 lightDir = normalize(light.position - vFragPos);\n - mediump float diff = max(dot(norm, lightDir), 0.0);\n - mediump vec3 diffuse = material.diffuse * diff * light.color;\n - \n -\n // Specular - mediump vec3 viewDir = normalize(viewPos - vFragPos);\n - mediump vec3 reflectDir = reflect(-lightDir, norm); \n - mediump float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n - mediump vec3 specular = material.specular * spec * light.color; \n - mediump vec3 result = (ambient + diffuse + specular);\n - gl_FragColor = vec4(result, 1.0);\n -}\n -); -// clang-format on - } // namespace // This example shows per-pixel lighting of materials with different ambient, diffuse, specular and shininess parameters @@ -323,14 +257,14 @@ public: } /** - * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * Creates a shader using inlined variable SHADER_RENDERING_BASIC_LIGHT_VERT and SHADER_RENDERING_BASIC_LIGHT_FRAG * * Shaders are very basic and all they do is transforming vertices and interpolating * input per-vertex color. */ void CreateCubeShader() { - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_RENDERING_BASIC_LIGHT_VERT, SHADER_RENDERING_BASIC_LIGHT_FRAG); float scale = 120.0f; mShader.RegisterProperty("light.position", Vector3(1.2 * scale, scale, 2.0 * scale)); diff --git a/examples/rendering-basic-light/shaders/rendering-basic-light.frag b/examples/rendering-basic-light/shaders/rendering-basic-light.frag new file mode 100644 index 0000000..6f56153 --- /dev/null +++ b/examples/rendering-basic-light/shaders/rendering-basic-light.frag @@ -0,0 +1,39 @@ +varying mediump vec3 vNormal; +varying mediump vec3 vFragPos; +uniform mediump vec3 viewPos; // custom uniform + +struct Material +{ + mediump vec3 ambient; + mediump vec3 diffuse; + mediump vec3 specular; + mediump float shininess; +}; + +struct Light +{ + mediump vec3 position; + mediump vec3 color; +}; + +uniform Material material; // custom uniform +uniform Light light; // custom uniform + +void main() +{ + // Ambient + mediump vec3 ambient = material.ambient * light.color; + // Diffuse + mediump vec3 norm = normalize(vNormal); + mediump vec3 lightDir = normalize(light.position - vFragPos); + mediump float diff = max(dot(norm, lightDir), 0.0); + mediump vec3 diffuse = material.diffuse * diff * light.color; + + // Specular + mediump vec3 viewDir = normalize(viewPos - vFragPos); + mediump vec3 reflectDir = reflect(-lightDir, norm); + mediump float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + mediump vec3 specular = material.specular * spec * light.color; + mediump vec3 result = (ambient + diffuse + specular); + gl_FragColor = vec4(result, 1.0); +} diff --git a/examples/rendering-basic-light/shaders/rendering-basic-light.vert b/examples/rendering-basic-light/shaders/rendering-basic-light.vert new file mode 100644 index 0000000..bc5c85a --- /dev/null +++ b/examples/rendering-basic-light/shaders/rendering-basic-light.vert @@ -0,0 +1,19 @@ +attribute mediump vec3 aPosition; // DALi shader builtin +attribute mediump vec3 aNormal; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin +uniform mediump mat4 uModelView; // DALi shader builtin +uniform mediump mat3 uNormalMatrix; // DALi shader builtin + +varying mediump vec3 vNormal; +varying mediump vec3 vFragPos; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vertexPosition.xyz *= uSize; + vFragPos = vec3(uModelView * vertexPosition); + vNormal = uNormalMatrix * aNormal; + + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-basic-pbr/model-skybox.cpp b/examples/rendering-basic-pbr/model-skybox.cpp index 31cc654..03597f7 100644 --- a/examples/rendering-basic-pbr/model-skybox.cpp +++ b/examples/rendering-basic-pbr/model-skybox.cpp @@ -25,47 +25,8 @@ // INTERNAL INCLUDES #include "obj-loader.h" - -namespace -{ -// clang-format off - -/* - * Vertex shader for a skybox - */ -const char* VERTEX_SHADER_SKYBOX = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -\n -varying mediump vec3 vTexCoord;\n -void main()\n -{\n - vTexCoord = aPosition;\n - - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vec4 clipSpacePosition = uMvpMatrix * vertexPosition;\n - gl_Position = clipSpacePosition.xyww;\n // Writes 1.0, the maximum depth value, into the depth buffer. - // This is an optimization to avoid running the fragment shader - // for the pixels hidden by the scene's objects. -}\n -); - -/* - * Fragment shader for a skybox - */ -const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER( -uniform samplerCube uSkyBoxTexture;\n -\n -varying mediump vec3 vTexCoord;\n -void main()\n -{\n - mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0);\n - gl_FragColor = texColor;\n -}\n -); -// clang-format on - -} // namespace +#include "generated/skybox-vert.h" +#include "generated/skybox-frag.h" ModelSkybox::ModelSkybox() { @@ -78,7 +39,7 @@ ModelSkybox::~ModelSkybox() void ModelSkybox::Init(const Vector3& size) { Geometry geometry = CreateGeometry(); - Shader shader = Shader::New(VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX); + Shader shader = Shader::New(SHADER_SKYBOX_VERT, SHADER_SKYBOX_FRAG); Renderer renderer = Renderer::New(geometry, shader); diff --git a/examples/rendering-basic-pbr/shaders/skybox.frag b/examples/rendering-basic-pbr/shaders/skybox.frag new file mode 100644 index 0000000..b28f895 --- /dev/null +++ b/examples/rendering-basic-pbr/shaders/skybox.frag @@ -0,0 +1,8 @@ +uniform samplerCube uSkyBoxTexture; +varying mediump vec3 vTexCoord; + +void main() +{ + mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0); + gl_FragColor = texColor; +} diff --git a/examples/rendering-basic-pbr/shaders/skybox.vert b/examples/rendering-basic-pbr/shaders/skybox.vert new file mode 100644 index 0000000..9e9f433 --- /dev/null +++ b/examples/rendering-basic-pbr/shaders/skybox.vert @@ -0,0 +1,14 @@ +attribute mediump vec3 aPosition; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin + +varying mediump vec3 vTexCoord; +void main() +{ + vTexCoord = aPosition; + + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vec4 clipSpacePosition = uMvpMatrix * vertexPosition; + gl_Position = clipSpacePosition.xyww; // Writes 1.0, the maximum depth value, into the depth buffer. + // This is an optimization to avoid running the fragment shader + // for the pixels hidden by the scene's objects. +} diff --git a/examples/rendering-cube/rendering-cube.cpp b/examples/rendering-cube/rendering-cube.cpp index 756e841..08cf1c7 100644 --- a/examples/rendering-cube/rendering-cube.cpp +++ b/examples/rendering-cube/rendering-cube.cpp @@ -18,48 +18,12 @@ #include #include +#include "generated/rendering-cube-vert.h" +#include "generated/rendering-cube-frag.h" + using namespace Dali; using namespace Toolkit; -namespace -{ -// clang-format off - -/* - * Vertex shader - */ -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -attribute mediump vec3 aColor;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -varying mediump vec4 vColor;\n -\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vertexPosition.xyz *= uSize;\n - vColor = vec4( aColor, 1.0 );\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader - */ -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( -varying mediump vec4 vColor;\n -\n -void main()\n -{\n - gl_FragColor = vColor;\n -}\n -); -// clang-format on - -} // namespace - // This example shows how to create a cube with colors on each side // class DrawCubeController : public ConnectionTracker @@ -204,14 +168,14 @@ public: } /** - * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * Creates a shader using inlined variable SHADER_RENDERING_CUBE_VERT and SHADER_RENDERING_CUBE_FRAG * * Shaders are very basic and all they do is transforming vertices and interpolating * input per-vertex color. */ void CreateCubeShader() { - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_RENDERING_CUBE_VERT, SHADER_RENDERING_CUBE_FRAG); } /** diff --git a/examples/rendering-cube/shaders/rendering-cube.frag b/examples/rendering-cube/shaders/rendering-cube.frag new file mode 100644 index 0000000..1f8954a --- /dev/null +++ b/examples/rendering-cube/shaders/rendering-cube.frag @@ -0,0 +1,6 @@ +varying mediump vec4 vColor; + +void main() +{ + gl_FragColor = vColor; +} diff --git a/examples/rendering-cube/shaders/rendering-cube.vert b/examples/rendering-cube/shaders/rendering-cube.vert new file mode 100644 index 0000000..d4a0b5c --- /dev/null +++ b/examples/rendering-cube/shaders/rendering-cube.vert @@ -0,0 +1,14 @@ +attribute mediump vec3 aPosition; // DALi shader builtin +attribute mediump vec3 aColor; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +varying mediump vec4 vColor; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vertexPosition.xyz *= uSize; + vColor = vec4( aColor, 1.0 ); + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-line/rendering-line.cpp b/examples/rendering-line/rendering-line.cpp index 8925047..ad20fc9 100644 --- a/examples/rendering-line/rendering-line.cpp +++ b/examples/rendering-line/rendering-line.cpp @@ -18,44 +18,12 @@ #include #include +#include "generated/rendering-line-vert.h" +#include "generated/rendering-line-frag.h" + using namespace Dali; using namespace Toolkit; -namespace -{ -// clang-format off - -/* - * Vertex shader - */ -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( -attribute mediump vec2 aPosition;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n - vertexPosition.xyz *= uSize;\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader - */ -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( -uniform mediump vec4 uColor;\n -\n -void main()\n -{\n - gl_FragColor = uColor;\n -}\n -); -// clang-format on - -} // namespace - // This example shows how to draw a line in actor's color // class DrawLineController : public ConnectionTracker @@ -143,13 +111,13 @@ public: } /** - * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * Creates a shader using inlined variable SHADER_RENDERING_LINE_VERT and SHADER_RENDERING_LINE_FRAG * * Shaders are very basic and all they do is transforming vertices and applying actor's colour. */ void CreateLineShader() { - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_RENDERING_LINE_VERT, SHADER_RENDERING_LINE_FRAG); } /** diff --git a/examples/rendering-line/shaders/rendering-line.frag b/examples/rendering-line/shaders/rendering-line.frag new file mode 100644 index 0000000..b8d9d3a --- /dev/null +++ b/examples/rendering-line/shaders/rendering-line.frag @@ -0,0 +1,6 @@ +uniform mediump vec4 uColor; + +void main() +{ + gl_FragColor = uColor; +} diff --git a/examples/rendering-line/shaders/rendering-line.vert b/examples/rendering-line/shaders/rendering-line.vert new file mode 100644 index 0000000..53f4a4f --- /dev/null +++ b/examples/rendering-line/shaders/rendering-line.vert @@ -0,0 +1,10 @@ +attribute mediump vec2 aPosition; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= uSize; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-radial-progress/radial-progress.cpp b/examples/rendering-radial-progress/radial-progress.cpp index 675b690..9bd55cd 100644 --- a/examples/rendering-radial-progress/radial-progress.cpp +++ b/examples/rendering-radial-progress/radial-progress.cpp @@ -18,6 +18,11 @@ #include #include +#include "generated/radial-progress-basic-vert.h" +#include "generated/radial-progress-basic-frag.h" +#include "generated/radial-progress-textured-vert.h" +#include "generated/radial-progress-textured-frag.h" + using namespace Dali; namespace // unnamed namespace @@ -30,77 +35,6 @@ const int NUMBER_OF_SIDES(64); // number of sides of the polygon used as a const float INITIAL_DELAY(2.0f); // initial delay before showing the circle const float PROGRESS_DURATION(0.5f); // number of seconds to fully show the circle -// clang-format off - -/* - * Vertex shader for textured quad - */ -const char* VERTEX_SHADER_TEXTURED = DALI_COMPOSE_SHADER( -attribute mediump vec2 aPosition;\n -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n - vertexPosition.xyz *= uSize;\n - vTexCoord = vec2(1.0, 1.0)*(aPosition + vec2(0.5) );\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shaderfor textured quad - */ -const char* FRAGMENT_SHADER_TEXTURED = DALI_COMPOSE_SHADER( -uniform sampler2D uTexture;\n -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 texColor = texture2D( uTexture, vTexCoord );\n - gl_FragColor = texColor;\n -}\n -); - -/* - * Vertex shader for polygon - */ -const char* VERTEX_SHADER_BASIC = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -uniform mediump float uProgress;\n -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n -\n - float index = aPosition.z;\n - if( uProgress < index )\n - {\n - vertexPosition = vec4(0.0, 0.0, 0.0, 1.0);\n - }\n -\n - vertexPosition.xyz *= uSize;\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader for polygon - */ -const char* FRAGMENT_SHADER_BASIC = DALI_COMPOSE_SHADER( - -void main()\n -{\n - gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );\n -}\n -); -// clang-format on - } // unnamed namespace // This example shows how to render a radial progress indicator @@ -195,7 +129,7 @@ public: geometry.AddVertexBuffer(vertexBuffer); geometry.SetType(Geometry::TRIANGLE_FAN); - Shader shader = Shader::New(VERTEX_SHADER_BASIC, FRAGMENT_SHADER_BASIC); + Shader shader = Shader::New(SHADER_RADIAL_PROGRESS_BASIC_VERT, SHADER_RADIAL_PROGRESS_BASIC_FRAG); Renderer renderer = Renderer::New(geometry, shader); // Setting stencil data. We don't want to render to the color buffer so @@ -233,7 +167,7 @@ public: { // Create shader & geometry needed by Renderer - Shader shader = Shader::New(VERTEX_SHADER_TEXTURED, FRAGMENT_SHADER_TEXTURED); + Shader shader = Shader::New(SHADER_RADIAL_PROGRESS_TEXTURED_VERT, SHADER_RADIAL_PROGRESS_TEXTURED_FRAG); Property::Map vertexFormat; vertexFormat["aPosition"] = Property::VECTOR2; diff --git a/examples/rendering-radial-progress/shaders/radial-progress-basic.frag b/examples/rendering-radial-progress/shaders/radial-progress-basic.frag new file mode 100644 index 0000000..d8d6226 --- /dev/null +++ b/examples/rendering-radial-progress/shaders/radial-progress-basic.frag @@ -0,0 +1,6 @@ +// Fragment shader for polygon + +void main() +{ + gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ); +} diff --git a/examples/rendering-radial-progress/shaders/radial-progress-basic.vert b/examples/rendering-radial-progress/shaders/radial-progress-basic.vert new file mode 100644 index 0000000..9700c41 --- /dev/null +++ b/examples/rendering-radial-progress/shaders/radial-progress-basic.vert @@ -0,0 +1,22 @@ +// Vertex shader for polygon + +attribute mediump vec3 aPosition; +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin +uniform mediump float uProgress; + +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0); + + float index = aPosition.z; + if( uProgress < index ) + { + vertexPosition = vec4(0.0, 0.0, 0.0, 1.0); + } + + vertexPosition.xyz *= uSize; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-radial-progress/shaders/radial-progress-textured.frag b/examples/rendering-radial-progress/shaders/radial-progress-textured.frag new file mode 100644 index 0000000..4017c03 --- /dev/null +++ b/examples/rendering-radial-progress/shaders/radial-progress-textured.frag @@ -0,0 +1,10 @@ +// Fragment shaderfor textured quad + +uniform sampler2D uTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 texColor = texture2D( uTexture, vTexCoord ); + gl_FragColor = texColor; +} diff --git a/examples/rendering-radial-progress/shaders/radial-progress-textured.vert b/examples/rendering-radial-progress/shaders/radial-progress-textured.vert new file mode 100644 index 0000000..78aca62 --- /dev/null +++ b/examples/rendering-radial-progress/shaders/radial-progress-textured.vert @@ -0,0 +1,15 @@ +// Vertex shader for textured quad + +attribute mediump vec2 aPosition; +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= uSize; + vTexCoord = vec2(1.0, 1.0)*(aPosition + vec2(0.5) ); + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-skybox/rendering-skybox.cpp b/examples/rendering-skybox/rendering-skybox.cpp index d2d0f29..bab1a0e 100644 --- a/examples/rendering-skybox/rendering-skybox.cpp +++ b/examples/rendering-skybox/rendering-skybox.cpp @@ -19,82 +19,16 @@ #include #include "look-camera.h" +#include "generated/rendering-skybox-vert.h" +#include "generated/rendering-skybox-frag.h" +#include "generated/rendering-skybox-cube-vert.h" +#include "generated/rendering-skybox-cube-frag.h" using namespace Dali; using namespace Toolkit; namespace { -// clang-format off -/* - * Vertex shader for a textured cube - */ -const char* VERTEX_SHADER_CUBE = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -attribute mediump vec2 aTexCoord;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vertexPosition.xyz *= uSize;\n - vTexCoord = aTexCoord;\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader for a textured cube - */ -const char* FRAGMENT_SHADER_CUBE = DALI_COMPOSE_SHADER( -uniform sampler2D uTexture;\n -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 texColor = texture2D( uTexture, vTexCoord );\n - gl_FragColor = texColor;\n -}\n -); - -/* - * Vertex shader for a skybox - */ -const char* VERTEX_SHADER_SKYBOX = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -\n -varying mediump vec3 vTexCoord;\n -void main()\n -{\n - vTexCoord.x = aPosition.x;\n - vTexCoord.y = -aPosition.y;\n // convert to GL coords - vTexCoord.z = aPosition.z;\n - - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vec4 clipSpacePosition = uMvpMatrix * vertexPosition;\n - gl_Position = clipSpacePosition.xyww;\n // Writes 1.0, the maximum depth value, into the depth buffer. - // This is an optimization to avoid running the fragment shader - // for the pixels hidden by the scene's objects. -}\n -); - -/* - * Fragment shader for a skybox - */ -const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER( -uniform samplerCube uSkyBoxTexture;\n -\n -varying mediump vec3 vTexCoord;\n -void main()\n -{\n - mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord );\n - gl_FragColor = texColor;\n -}\n -); -// clang-format on const float CAMERA_DEFAULT_FOV(60.0f); const float CAMERA_DEFAULT_NEAR(0.1f); @@ -215,8 +149,8 @@ public: */ void CreateShaders() { - mShaderCube = Shader::New(VERTEX_SHADER_CUBE, FRAGMENT_SHADER_CUBE); - mShaderSkybox = Shader::New(VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX); + mShaderCube = Shader::New(SHADER_RENDERING_SKYBOX_CUBE_VERT, SHADER_RENDERING_SKYBOX_CUBE_FRAG); + mShaderSkybox = Shader::New(SHADER_RENDERING_SKYBOX_CUBE_VERT, SHADER_RENDERING_SKYBOX_CUBE_FRAG); } /** diff --git a/examples/rendering-skybox/shaders/rendering-skybox-cube.frag b/examples/rendering-skybox/shaders/rendering-skybox-cube.frag new file mode 100644 index 0000000..b8a29f5 --- /dev/null +++ b/examples/rendering-skybox/shaders/rendering-skybox-cube.frag @@ -0,0 +1,11 @@ +// Fragment shader for a textured cube + +uniform sampler2D uTexture; + +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 texColor = texture2D( uTexture, vTexCoord ); + gl_FragColor = texColor; +} diff --git a/examples/rendering-skybox/shaders/rendering-skybox-cube.vert b/examples/rendering-skybox/shaders/rendering-skybox-cube.vert new file mode 100644 index 0000000..621f98d --- /dev/null +++ b/examples/rendering-skybox/shaders/rendering-skybox-cube.vert @@ -0,0 +1,16 @@ +// Vertex shader for a textured cube + +attribute mediump vec3 aPosition; // DALi shader builtin +attribute mediump vec2 aTexCoord; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vertexPosition.xyz *= uSize; + vTexCoord = aTexCoord; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-skybox/shaders/rendering-skybox.frag b/examples/rendering-skybox/shaders/rendering-skybox.frag new file mode 100644 index 0000000..c755645 --- /dev/null +++ b/examples/rendering-skybox/shaders/rendering-skybox.frag @@ -0,0 +1,11 @@ +// Fragment shader for a skybox + +uniform samplerCube uSkyBoxTexture; + +varying mediump vec3 vTexCoord; + +void main() +{ + mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord ); + gl_FragColor = texColor; +} diff --git a/examples/rendering-skybox/shaders/rendering-skybox.vert b/examples/rendering-skybox/shaders/rendering-skybox.vert new file mode 100644 index 0000000..c2ce43a --- /dev/null +++ b/examples/rendering-skybox/shaders/rendering-skybox.vert @@ -0,0 +1,19 @@ +// Vertex shader for a skybox + +attribute mediump vec3 aPosition; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin + +varying mediump vec3 vTexCoord; + +void main() +{ + vTexCoord.x = aPosition.x; + vTexCoord.y = -aPosition.y; // convert to GL coords + vTexCoord.z = aPosition.z; + + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vec4 clipSpacePosition = uMvpMatrix * vertexPosition; + gl_Position = clipSpacePosition.xyww; // Writes 1.0, the maximum depth value, into the depth buffer. + // This is an optimization to avoid running the fragment shader + // for the pixels hidden by the scene's objects. +} diff --git a/examples/rendering-textured-cube/rendering-textured-cube.cpp b/examples/rendering-textured-cube/rendering-textured-cube.cpp index e3df9de..646e296 100644 --- a/examples/rendering-textured-cube/rendering-textured-cube.cpp +++ b/examples/rendering-textured-cube/rendering-textured-cube.cpp @@ -18,46 +18,14 @@ #include #include +#include "generated/rendering-textured-cube-vert.h" +#include "generated/rendering-textured-cube-frag.h" + using namespace Dali; using namespace Toolkit; namespace { -// clang-format off - -/* - * Vertex shader - */ -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( -attribute mediump vec3 aPosition;\n // DALi shader builtin -attribute mediump vec2 aTexCoord;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n - vertexPosition.xyz *= uSize;\n - vTexCoord = aTexCoord;\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader - */ -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( -uniform sampler2D uTexture;\n -\n -varying mediump vec2 vTexCoord;\n -void main()\n -{\n - mediump vec4 texColor = texture2D( uTexture, vTexCoord );\n - gl_FragColor = texColor;\n -}\n -); -// clang-format on const char* TEXTURE_URL = DEMO_IMAGE_DIR "wood.png"; @@ -204,14 +172,14 @@ public: } /** - * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * Creates a shader using SHADER_RENDERING_TEXTURED_CUBE_VERT and SHADER_RENDERING_TEXTURED_CUBE_FRAG * * Shaders are very basic and all they do is transforming vertices and sampling * a texture. */ void CreateCubeShader() { - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_RENDERING_TEXTURED_CUBE_VERT, SHADER_RENDERING_TEXTURED_CUBE_FRAG); } /** diff --git a/examples/rendering-textured-cube/shaders/rendering-textured-cube.frag b/examples/rendering-textured-cube/shaders/rendering-textured-cube.frag new file mode 100644 index 0000000..43fb27f --- /dev/null +++ b/examples/rendering-textured-cube/shaders/rendering-textured-cube.frag @@ -0,0 +1,8 @@ +uniform sampler2D uTexture; +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 texColor = texture2D( uTexture, vTexCoord ); + gl_FragColor = texColor; +} diff --git a/examples/rendering-textured-cube/shaders/rendering-textured-cube.vert b/examples/rendering-textured-cube/shaders/rendering-textured-cube.vert new file mode 100644 index 0000000..36c0e60 --- /dev/null +++ b/examples/rendering-textured-cube/shaders/rendering-textured-cube.vert @@ -0,0 +1,14 @@ +attribute mediump vec3 aPosition; // DALi shader builtin +attribute mediump vec2 aTexCoord; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +varying mediump vec2 vTexCoord; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 1.0); + vertexPosition.xyz *= uSize; + vTexCoord = aTexCoord; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/rendering-triangle/rendering-triangle.cpp b/examples/rendering-triangle/rendering-triangle.cpp index 1b5ef12..cecce9a 100644 --- a/examples/rendering-triangle/rendering-triangle.cpp +++ b/examples/rendering-triangle/rendering-triangle.cpp @@ -18,44 +18,12 @@ #include #include +#include "generated/rendering-triangle-vert.h" +#include "generated/rendering-triangle-frag.h" + using namespace Dali; using namespace Toolkit; -namespace -{ -// clang-format off - -/* - * Vertex shader - */ -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( -attribute mediump vec2 aPosition;\n // DALi shader builtin -uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin -uniform mediump vec3 uSize;\n // DALi shader builtin -\n -void main()\n -{\n - mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n - vertexPosition.xyz *= uSize;\n - gl_Position = uMvpMatrix * vertexPosition;\n -}\n -); - -/* - * Fragment shader - */ -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( -uniform mediump vec4 uColor;\n -\n -void main()\n -{\n - gl_FragColor = uColor;\n -}\n -); -// clang-format on - -} // namespace - // This example shows how to draw a triangle in actor's color // class DrawTriangleController : public ConnectionTracker @@ -150,7 +118,7 @@ public: */ void CreateTriangleShader() { - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_RENDERING_TRIANGLE_VERT, SHADER_RENDERING_TRIANGLE_FRAG); } /** diff --git a/examples/rendering-triangle/shaders/rendering-triangle.frag b/examples/rendering-triangle/shaders/rendering-triangle.frag new file mode 100644 index 0000000..b8d9d3a --- /dev/null +++ b/examples/rendering-triangle/shaders/rendering-triangle.frag @@ -0,0 +1,6 @@ +uniform mediump vec4 uColor; + +void main() +{ + gl_FragColor = uColor; +} diff --git a/examples/rendering-triangle/shaders/rendering-triangle.vert b/examples/rendering-triangle/shaders/rendering-triangle.vert new file mode 100644 index 0000000..53f4a4f --- /dev/null +++ b/examples/rendering-triangle/shaders/rendering-triangle.vert @@ -0,0 +1,10 @@ +attribute mediump vec2 aPosition; // DALi shader builtin +uniform mediump mat4 uMvpMatrix; // DALi shader builtin +uniform mediump vec3 uSize; // DALi shader builtin + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= uSize; + gl_Position = uMvpMatrix * vertexPosition; +} diff --git a/examples/simple-text-renderer/shaders/simple-text-renderer.frag b/examples/simple-text-renderer/shaders/simple-text-renderer.frag new file mode 100644 index 0000000..f7fe862 --- /dev/null +++ b/examples/simple-text-renderer/shaders/simple-text-renderer.frag @@ -0,0 +1,15 @@ +#version 300 es + +precision mediump float; + +in vec2 vUV; +out vec4 FragColor; + +uniform sampler2D sAlbedo; +uniform vec4 uColor; + +void main() +{ + vec4 color = texture(sAlbedo, vUV); + FragColor = vec4(color.rgb, uColor.a * color.a); +} diff --git a/examples/simple-text-renderer/shaders/simple-text-renderer.vert b/examples/simple-text-renderer/shaders/simple-text-renderer.vert new file mode 100644 index 0000000..3cc55bf --- /dev/null +++ b/examples/simple-text-renderer/shaders/simple-text-renderer.vert @@ -0,0 +1,20 @@ +#version 300 es + +precision mediump float; + +in vec2 aPosition; +in vec2 aTexCoord; + +out vec2 vUV; + +uniform vec3 uSize; +uniform mat4 uMvpMatrix; + +void main() +{ + vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= uSize; + gl_Position = uMvpMatrix * vertexPosition; + + vUV = aTexCoord; +} diff --git a/examples/simple-text-renderer/simple-text-renderer-example.cpp b/examples/simple-text-renderer/simple-text-renderer-example.cpp index 23654a0..c9c0017 100644 --- a/examples/simple-text-renderer/simple-text-renderer-example.cpp +++ b/examples/simple-text-renderer/simple-text-renderer-example.cpp @@ -26,6 +26,10 @@ #include #include +// INTERNAL INCLUDES +#include "generated/simple-text-renderer-vert.h" +#include "generated/simple-text-renderer-frag.h" + using namespace std; using namespace Dali; using namespace Dali::Toolkit; @@ -35,44 +39,6 @@ namespace const std::string IMAGE1 = DEMO_IMAGE_DIR "application-icon-1.png"; const std::string IMAGE2 = DEMO_IMAGE_DIR "application-icon-6.png"; -#define MAKE_SHADER(A) #A - -const std::string VERSION_3_ES = "#version 300 es\n"; - -const char* VERTEX_SHADER = MAKE_SHADER( - precision mediump float; - - in vec2 aPosition; - in vec2 aTexCoord; - - out vec2 vUV; - - uniform vec3 uSize; - uniform mat4 uMvpMatrix; - - void main() { - vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); - vertexPosition.xyz *= uSize; - gl_Position = uMvpMatrix * vertexPosition; - - vUV = aTexCoord; - }); - -const char* FRAGMENT_SHADER = MAKE_SHADER( - precision mediump float; - - in vec2 vUV; - - out vec4 FragColor; - - uniform sampler2D sAlbedo; - uniform vec4 uColor; - - void main() { - vec4 color = texture(sAlbedo, vUV); - FragColor = vec4(color.rgb, uColor.a * color.a); - }); - Renderer CreateRenderer() { // Create the geometry. @@ -100,7 +66,7 @@ Renderer CreateRenderer() geometry.SetType(Geometry::TRIANGLE_STRIP); // Create the shader - Shader shader = Shader::New(VERSION_3_ES + VERTEX_SHADER, VERSION_3_ES + FRAGMENT_SHADER); + Shader shader = Shader::New(SHADER_SIMPLE_TEXT_RENDERER_VERT, SHADER_SIMPLE_TEXT_RENDERER_FRAG); // Create the renderer diff --git a/examples/sparkle/shaders/sparkle-effect.frag b/examples/sparkle/shaders/sparkle-effect.frag new file mode 100644 index 0000000..006a7d2 --- /dev/null +++ b/examples/sparkle/shaders/sparkle-effect.frag @@ -0,0 +1,11 @@ +precision highp float; +uniform sampler2D sTexture; +varying vec2 vTexCoord; + +varying lowp vec4 vColor; + +void main() +{ + gl_FragColor = vColor; + gl_FragColor.a *= texture2D(sTexture, vTexCoord).a; +} diff --git a/examples/sparkle/shaders/sparkle-effect.vert b/examples/sparkle/shaders/sparkle-effect.vert new file mode 100644 index 0000000..02c33df --- /dev/null +++ b/examples/sparkle/shaders/sparkle-effect.vert @@ -0,0 +1,93 @@ +precision highp float; + +attribute vec2 aTexCoord; +uniform mat4 uMvpMatrix; +varying vec2 vTexCoord; + +attribute vec2 aParticlePath0; +attribute vec2 aParticlePath1; +attribute vec2 aParticlePath2; +attribute vec2 aParticlePath3; +attribute vec2 aParticlePath4; +attribute vec2 aParticlePath5; + +uniform float uPercentage; +uniform float uPercentageMarked; +uniform vec3 uParticleColors[NUM_COLOR]; +uniform float uOpacity[NUM_PARTICLE]; +uniform vec2 uTapIndices; +uniform float uTapOffset[MAXIMUM_ANIMATION_COUNT]; +uniform vec2 uTapPoint[MAXIMUM_ANIMATION_COUNT]; +uniform float uAcceleration; +uniform float uRadius; +uniform float uScale; +uniform float uBreak; + +varying lowp vec4 vColor; + +void main() +{ + // we store the particle index inside texCoord attribute + float idx = abs(aTexCoord.y)-1.0; + + // early out if the particle is invisible + if(uOpacity[int(idx)]<1e-5) + { + gl_Position = vec4(0.0); + vColor = vec4(0.0); + return; + } + + // As the movement along the b-curve has nonuniform speed with a uniform increasing parameter 'uPercentage' + // we give different particles the different 'percentage' to make them looks more random + float increment = idx / float(NUM_PARTICLE)*5.0; + float percentage = mod(uPercentage +uAcceleration+increment, 1.0); + + vec2 p0; vec2 p1; vec2 p2; vec2 p3; + // calculate the particle position by using the cubic b-curve equation + if(percentage<0.5) // particle on the first b-curve + { + p0 = aParticlePath0; + p1 = aParticlePath1; + p2 = aParticlePath2; + p3 = aParticlePath3; + } + else + { + p0 = aParticlePath3; + p1 = aParticlePath4; + p2 = aParticlePath5; + p3 = aParticlePath0; + } + float t = mod( percentage*2.0, 1.0); + vec2 position = (1.0-t)*(1.0-t)*(1.0-t)*p0 + 3.0*(1.0-t)*(1.0-t)*t*p1+3.0*(1.0-t)*t*t*p2 + t*t*t*p3; + + vec2 referencePoint = mix(p0,p3,0.5); + float maxAnimationCount = float(MAXIMUM_ANIMATION_COUNT); + + for( float i=uTapIndices.x; i #include +#include "generated/sparkle-effect-vert.h" +#include "generated/sparkle-effect-frag.h" + using namespace Dali; using Dali::Toolkit::ImageView; @@ -230,126 +233,14 @@ struct Vertex */ Shader New() { - // clang-format off - std::string vertexShader = DALI_COMPOSE_SHADER( - precision highp float;\n - \n - attribute vec2 aTexCoord;\n - uniform mat4 uMvpMatrix;\n - varying vec2 vTexCoord;\n - \n - attribute vec2 aParticlePath0;\n - attribute vec2 aParticlePath1;\n - attribute vec2 aParticlePath2;\n - attribute vec2 aParticlePath3;\n - attribute vec2 aParticlePath4;\n - attribute vec2 aParticlePath5;\n - \n - uniform float uPercentage;\n - uniform float uPercentageMarked;\n - uniform vec3 uParticleColors[NUM_COLOR];\n - uniform float uOpacity[NUM_PARTICLE];\n - uniform vec2 uTapIndices; - uniform float uTapOffset[MAXIMUM_ANIMATION_COUNT];\n - uniform vec2 uTapPoint[MAXIMUM_ANIMATION_COUNT];\n - uniform float uAcceleration;\n - uniform float uRadius;\n - uniform float uScale;\n - uniform float uBreak;\n - \n - varying lowp vec4 vColor;\n - \n - void main()\n - {\n - // we store the particle index inside texCoord attribute - float idx = abs(aTexCoord.y)-1.0;\n - \n - // early out if the particle is invisible - if(uOpacity[int(idx)]<1e-5)\n - {\n - gl_Position = vec4(0.0);\n - vColor = vec4(0.0);\n - return;\n - }\n - \n - // As the movement along the b-curve has nonuniform speed with a uniform increasing parameter 'uPercentage' - // we give different particles the different 'percentage' to make them looks more random - float increment = idx / float(NUM_PARTICLE)*5.0; - float percentage = mod(uPercentage +uAcceleration+increment, 1.0); - \n - vec2 p0; vec2 p1; vec2 p2; vec2 p3; - // calculate the particle position by using the cubic b-curve equation - if(percentage<0.5)\n // particle on the first b-curve - {\n - p0 = aParticlePath0;\n - p1 = aParticlePath1;\n - p2 = aParticlePath2;\n - p3 = aParticlePath3;\n - }\n - else\n - {\n - p0 = aParticlePath3;\n - p1 = aParticlePath4;\n - p2 = aParticlePath5;\n - p3 = aParticlePath0;\n - }\n - float t = mod( percentage*2.0, 1.0);\n - vec2 position = (1.0-t)*(1.0-t)*(1.0-t)*p0 + 3.0*(1.0-t)*(1.0-t)*t*p1+3.0*(1.0-t)*t*t*p2 + t*t*t*p3;\n - \n - vec2 referencePoint = mix(p0,p3,0.5);\n - float maxAnimationCount = float(MAXIMUM_ANIMATION_COUNT);\n - \n - for( float i=uTapIndices.x; i #include +#include "generated/image-channel-control-frag.h" + #include using namespace Dali; // Needed for macros @@ -29,21 +31,6 @@ namespace Internal { namespace { -// clang-format off - -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - varying mediump vec2 vTexCoord;\n - uniform sampler2D sTexture;\n - uniform mediump vec4 uColor;\n - uniform mediump vec3 mixColor;\n - uniform mediump vec3 uChannels;\n - \n - void main()\n - {\n - gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(mixColor,1.0) * uColor * vec4(uChannels, 1.0) ;\n - }\n -); -// clang-format on Dali::BaseHandle Create() { @@ -95,7 +82,7 @@ void ImageChannelControl::SetImage(const std::string& url) Property::Map properties; Property::Map shader; - shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER; + shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = SHADER_IMAGE_CHANNEL_CONTROL_FRAG.data(); properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE; properties[Dali::Toolkit::Visual::Property::SHADER] = shader; properties[Dali::Toolkit::ImageVisual::Property::URL] = url; diff --git a/examples/styling/shaders/image-channel-control.frag b/examples/styling/shaders/image-channel-control.frag new file mode 100644 index 0000000..4a8b905 --- /dev/null +++ b/examples/styling/shaders/image-channel-control.frag @@ -0,0 +1,10 @@ +varying mediump vec2 vTexCoord; +uniform sampler2D sTexture; +uniform mediump vec4 uColor; +uniform mediump vec3 mixColor; +uniform mediump vec3 uChannels; + +void main() +{ + gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(mixColor,1.0) * uColor * vec4(uChannels, 1.0) ; +} diff --git a/examples/textured-mesh/shaders/textured-mesh.frag b/examples/textured-mesh/shaders/textured-mesh.frag new file mode 100644 index 0000000..1edd66e --- /dev/null +++ b/examples/textured-mesh/shaders/textured-mesh.frag @@ -0,0 +1,9 @@ +varying mediump vec2 vTexCoord; +uniform lowp vec4 uColor; +uniform sampler2D sTexture; +uniform lowp vec4 uFadeColor; + +void main() +{ + gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * uFadeColor; +} diff --git a/examples/textured-mesh/shaders/textured-mesh.vert b/examples/textured-mesh/shaders/textured-mesh.vert new file mode 100644 index 0000000..1193150 --- /dev/null +++ b/examples/textured-mesh/shaders/textured-mesh.vert @@ -0,0 +1,15 @@ +attribute mediump vec2 aPosition; +attribute highp vec2 aTexCoord; +varying mediump vec2 vTexCoord; +uniform mediump mat4 uMvpMatrix; +uniform mediump vec3 uSize; +uniform lowp vec4 uFadeColor; + +void main() +{ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); + vertexPosition.xyz *= uSize; + vertexPosition = uMvpMatrix * vertexPosition; + vTexCoord = aTexCoord; + gl_Position = vertexPosition; +} diff --git a/examples/textured-mesh/textured-mesh-example.cpp b/examples/textured-mesh/textured-mesh-example.cpp index 9927620..b638a36 100644 --- a/examples/textured-mesh/textured-mesh-example.cpp +++ b/examples/textured-mesh/textured-mesh-example.cpp @@ -21,6 +21,8 @@ // INTERNAL INCLUDES #include "shared/utility.h" #include "shared/view.h" +#include "generated/textured-mesh-vert.h" +#include "generated/textured-mesh-frag.h" using namespace Dali; @@ -29,34 +31,6 @@ namespace const char* MATERIAL_SAMPLE(DEMO_IMAGE_DIR "gallery-small-48.jpg"); const char* MATERIAL_SAMPLE2(DEMO_IMAGE_DIR "gallery-medium-19.jpg"); -#define MAKE_SHADER(A) #A - -const char* VERTEX_SHADER = MAKE_SHADER( - attribute mediump vec2 aPosition; - attribute highp vec2 aTexCoord; - varying mediump vec2 vTexCoord; - uniform mediump mat4 uMvpMatrix; - uniform mediump vec3 uSize; - uniform lowp vec4 uFadeColor; - - void main() { - mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0); - vertexPosition.xyz *= uSize; - vertexPosition = uMvpMatrix * vertexPosition; - vTexCoord = aTexCoord; - gl_Position = vertexPosition; - }); - -const char* FRAGMENT_SHADER = MAKE_SHADER( - varying mediump vec2 vTexCoord; - uniform lowp vec4 uColor; - uniform sampler2D sTexture; - uniform lowp vec4 uFadeColor; - - void main() { - gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * uFadeColor; - }); - /** * Sinusoidal curve starting at zero with 2 cycles */ @@ -107,7 +81,7 @@ public: Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE); Texture texture2 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2); - mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER); + mShader = Shader::New(SHADER_TEXTURED_MESH_VERT, SHADER_TEXTURED_MESH_FRAG); mTextureSet1 = TextureSet::New(); mTextureSet1.SetTexture(0u, texture1); diff --git a/examples/waves/shaders/waves.frag b/examples/waves/shaders/waves.frag new file mode 100644 index 0000000..8a496a1 --- /dev/null +++ b/examples/waves/shaders/waves.frag @@ -0,0 +1,51 @@ +precision highp float; + +uniform vec4 uColor; // DALi +uniform sampler2D uNormalMap; // DALi + +uniform vec3 uInvLightDir; +uniform vec3 uLightColorSqr; +uniform vec3 uAmbientColor; + +uniform float uNormalMapWeight; +uniform float uSpecularity; + +varying vec2 vUv; +varying vec3 vNormal; +varying vec3 vViewPos; +varying float vHeight; + +float Rand(vec2 co) +{ + return fract(sin(dot(co.xy, vec2(12.98981, 78.2331))) * 43758.5453); +} + +float Sum(vec3 v) +{ + return v.x + v.y + v.z; +} + +void main() +{ + vec3 viewPos = normalize(vViewPos); + vec2 uv2 = vUv + vViewPos.xy / vViewPos.z * vHeight + vec2(.5, 0.); + + vec3 perturbNormal = texture2D(uNormalMap, vUv).rgb * 2. - 1.; + vec3 perturbNormal2 = texture2D(uNormalMap, uv2).rgb * 2. - 1.; + vec3 normal = normalize(vNormal + perturbNormal * uNormalMapWeight); + vec3 normal2 = normalize(vNormal + perturbNormal2 * uNormalMapWeight); + + vec3 color = uAmbientColor; + float d = max(0., dot(normal, -uInvLightDir)); + color += uColor.rgb * d; + + vec3 reflected = reflect(uInvLightDir, normal); + d = max(0., dot(reflected, viewPos)); + color += pow(d, uSpecularity) * uLightColorSqr; + + reflected = reflect(uInvLightDir, normal2); + d = max(0., dot(reflected, viewPos)); + color += pow(d, uSpecularity) * uLightColorSqr; + + gl_FragColor = vec4(color, 1.); +} diff --git a/examples/waves/shaders/waves.vert b/examples/waves/shaders/waves.vert new file mode 100644 index 0000000..ac9af2f --- /dev/null +++ b/examples/waves/shaders/waves.vert @@ -0,0 +1,99 @@ +#define FMA(a, b, c) ((a) * (b) + (c)) // fused multiply-add + +precision highp float; + +const float kTile = 1.; + +const float kPi = 3.1415926535; +const float kEpsilon = 1. / 32.; + +// DALI uniforms +uniform vec3 uSize; +uniform mat4 uModelView; +uniform mat4 uProjection; +uniform mat3 uNormalMatrix; + +// our uniforms +uniform float uTime; +uniform vec2 uScrollScale; +uniform float uWaveRate; +uniform float uWaveAmplitude; +uniform float uParallaxAmount; + +attribute vec2 aPosition; +attribute vec2 aTexCoord; + +varying vec2 vUv; +varying vec3 vViewPos; +varying vec3 vNormal; +varying float vHeight; + +float CubicHermite(float B, float C, float t) +{ + float dCB = (C - B) * .5; + float A = B - dCB; + float D = B + dCB; + vec3 p = vec3(D + .5 * (((B - C) * 3.) - A), A - 2.5 * B + 2. * C - D, + .5 * (C - A)); + return FMA(FMA(FMA(p.x, t, p.y), t, p.z), t, B); +} + +float Hash(float n) +{ + return fract(sin(n) * 43751.5453123); +} + +float HeightAtTile(vec2 pos) +{ + float rate = Hash(Hash(pos.x) * Hash(pos.y)); + + return (sin(uTime * rate * uWaveRate) * .5 + .5) * uWaveAmplitude; +} + +float CalculateHeight(vec2 position) +{ + vec2 tile = floor(position); + position = fract(position); + + vec2 cp = vec2( + CubicHermite( + HeightAtTile(tile + vec2( kTile * -0.5, kTile * -0.5)), + HeightAtTile(tile + vec2( kTile * +0.5, kTile * -0.5)), + position.x), + CubicHermite( + HeightAtTile(tile + vec2( kTile * -0.5, kTile * +0.5)), + HeightAtTile(tile + vec2( kTile * +0.5, kTile * +0.5)), + position.x) + ); + + return CubicHermite(cp.x, cp.y, position.y); +} + +vec3 CalculateNormal(vec2 position) +{ + vec3 normal = vec3( + CalculateHeight(vec2(position.x - kEpsilon, position.y)) - + CalculateHeight(vec2(position.x + kEpsilon, position.y)), + .25, + CalculateHeight(vec2(position.x, position.y - kEpsilon)) - + CalculateHeight(vec2(position.x, position.y + kEpsilon)) + ); + return normal; +} + +void main() +{ + vUv = aTexCoord; + + vec2 scrollPosition = aPosition * uScrollScale + vec2(0., uTime * -kPi); + vNormal = uNormalMatrix * CalculateNormal(scrollPosition); + + float h = CalculateHeight(scrollPosition); + vHeight = h * uParallaxAmount; + vec3 position = vec3(aPosition.x, h, aPosition.y); + + vec4 viewPosition = uModelView * vec4(position * uSize, 1.); + vViewPos = -viewPosition.xyz; + + gl_Position = uProjection * viewPosition; +} diff --git a/examples/waves/waves-example.cpp b/examples/waves/waves-example.cpp index 9689772..a4d2995 100644 --- a/examples/waves/waves-example.cpp +++ b/examples/waves/waves-example.cpp @@ -32,165 +32,14 @@ #include #include +#include "generated/waves-vert.h" +#include "generated/waves-frag.h" + using namespace Dali; namespace { -constexpr std::string_view WAVES_VSH = - "#define FMA(a, b, c) ((a) * (b) + (c))\n" // fused multiply-add -DALI_COMPOSE_SHADER( - precision highp float; - - const float kTile = 1.; - - const float kPi = 3.1415926535; - const float kEpsilon = 1. / 32.; - - // DALI uniforms - uniform vec3 uSize; - uniform mat4 uModelView; - uniform mat4 uProjection; - uniform mat3 uNormalMatrix; - - // our uniforms - uniform float uTime; - uniform vec2 uScrollScale; - uniform float uWaveRate; - uniform float uWaveAmplitude; - uniform float uParallaxAmount; - - attribute vec2 aPosition; - attribute vec2 aTexCoord; - - varying vec2 vUv; - varying vec3 vViewPos; - varying vec3 vNormal; - varying float vHeight; - - float CubicHermite(float B, float C, float t) - { - float dCB = (C - B) * .5; - float A = B - dCB; - float D = B + dCB; - vec3 p = vec3(D + .5 * (((B - C) * 3.) - A), A - 2.5 * B + 2. * C - D, - .5 * (C - A)); - return FMA(FMA(FMA(p.x, t, p.y), t, p.z), t, B); - } - - float Hash(float n) - { - return fract(sin(n) * 43751.5453123); - } - - float HeightAtTile(vec2 pos) - { - float rate = Hash(Hash(pos.x) * Hash(pos.y)); - - return (sin(uTime * rate * uWaveRate) * .5 + .5) * uWaveAmplitude; - } - - float CalculateHeight(vec2 position) - { - vec2 tile = floor(position); - position = fract(position); - - vec2 cp = vec2( - CubicHermite( - HeightAtTile(tile + vec2( kTile * -0.5, kTile * -0.5)), - HeightAtTile(tile + vec2( kTile * +0.5, kTile * -0.5)), - position.x), - CubicHermite( - HeightAtTile(tile + vec2( kTile * -0.5, kTile * +0.5)), - HeightAtTile(tile + vec2( kTile * +0.5, kTile * +0.5)), - position.x) - ); - - return CubicHermite(cp.x, cp.y, position.y); - } - - vec3 CalculateNormal(vec2 position) - { - vec3 normal = vec3( - CalculateHeight(vec2(position.x - kEpsilon, position.y)) - - CalculateHeight(vec2(position.x + kEpsilon, position.y)), - .25, - CalculateHeight(vec2(position.x, position.y - kEpsilon)) - - CalculateHeight(vec2(position.x, position.y + kEpsilon)) - ); - return normal; - } - - void main() - { - vUv = aTexCoord; - - vec2 scrollPosition = aPosition * uScrollScale + vec2(0., uTime * -kPi); - vNormal = uNormalMatrix * CalculateNormal(scrollPosition); - - float h = CalculateHeight(scrollPosition); - vHeight = h * uParallaxAmount; - vec3 position = vec3(aPosition.x, h, aPosition.y); - - vec4 viewPosition = uModelView * vec4(position * uSize, 1.); - vViewPos = -viewPosition.xyz; - - gl_Position = uProjection * viewPosition; - }); - -constexpr std::string_view WAVES_FSH = DALI_COMPOSE_SHADER( - precision highp float; - - uniform vec4 uColor; // DALi - uniform sampler2D uNormalMap; // DALi - - uniform vec3 uInvLightDir; - uniform vec3 uLightColorSqr; - uniform vec3 uAmbientColor; - - uniform float uNormalMapWeight; - uniform float uSpecularity; - - varying vec2 vUv; - varying vec3 vNormal; - varying vec3 vViewPos; - varying float vHeight; - - float Rand(vec2 co) - { - return fract(sin(dot(co.xy, vec2(12.98981, 78.2331))) * 43758.5453); - } - - float Sum(vec3 v) - { - return v.x + v.y + v.z; - } - - void main() - { - vec3 viewPos = normalize(vViewPos); - vec2 uv2 = vUv + vViewPos.xy / vViewPos.z * vHeight + vec2(.5, 0.); - - vec3 perturbNormal = texture2D(uNormalMap, vUv).rgb * 2. - 1.; - vec3 perturbNormal2 = texture2D(uNormalMap, uv2).rgb * 2. - 1.; - vec3 normal = normalize(vNormal + perturbNormal * uNormalMapWeight); - vec3 normal2 = normalize(vNormal + perturbNormal2 * uNormalMapWeight); - - vec3 color = uAmbientColor; - float d = max(0., dot(normal, -uInvLightDir)); - color += uColor.rgb * d; - - vec3 reflected = reflect(uInvLightDir, normal); - d = max(0., dot(reflected, viewPos)); - color += pow(d, uSpecularity) * uLightColorSqr; - - reflected = reflect(uInvLightDir, normal2); - d = max(0., dot(reflected, viewPos)); - color += pow(d, uSpecularity) * uLightColorSqr; - - gl_FragColor = vec4(color, 1.); - }); - const float TIME_STEP = 0.0952664626; const std::string UNIFORM_LIGHT_COLOR_SQR = "uLightColorSqr"; @@ -423,7 +272,7 @@ private: specularity = mWaveShader.GetProperty(mUSpecularity).Get(); } - Shader shader = Shader::New(WAVES_VSH.data(), WAVES_FSH.data(), Shader::Hint::MODIFIES_GEOMETRY); + Shader shader = Shader::New(SHADER_WAVES_VERT, SHADER_WAVES_FRAG, Shader::Hint::MODIFIES_GEOMETRY); mULightColorSqr = shader.RegisterProperty(UNIFORM_LIGHT_COLOR_SQR, lightColorSqr); mUAmbientColor = shader.RegisterProperty(UNIFORM_AMBIENT_COLOR, ambientColor); mUInvLightDir = shader.RegisterProperty(UNIFORM_INV_LIGHT_DIR, invLightDir);