Commit 6eeaff164610a72a9809b705c60795fa15917253

Authored by Adeel Kazmi
Committed by Gerrit Code Review
2 parents 1c14a9a5 246e36c4

Merge "Added test for UniformBlocks" into devel/master

com.samsung.dali-demo.xml
... ... @@ -373,6 +373,9 @@
373 373 <ui-application appid="transitions.example" exec="/usr/apps/com.samsung.dali-demo/bin/transitions.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
374 374 <label>Shadow button</label>
375 375 </ui-application>
  376 + <ui-application appid="uniform-blocks.example" exec="/usr/apps/com.samsung.dali-demo/bin/uniform-blocks.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
  377 + <label>Uniform Blocks</label>
  378 + </ui-application>
376 379 <ui-application appid="video-view.example" exec="/usr/apps/com.samsung.dali-demo/bin/video-view.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
377 380 <label>Video View</label>
378 381 </ui-application>
... ...
examples/uniform-blocks/README.md 0 → 100644
  1 +This test is to validate the behaviour of UniformBlocks.
  2 +
  3 +It generates 200 actors with the same renderer. The frag shader
  4 +has a 1k color table in a uniform block, with a color index property
  5 +that is incremented for each actor that's created.
  6 +
  7 +On a 100ms timer, it removes the oldest actor and creates a new actor
  8 +with the next color index.
  9 +
  10 +On first touch, it changes the shader, and subtracts the currently indexed
  11 +color from the color in the vertex buffer.
  12 +
  13 +This shows that:
  14 + o Many uniform blocks are instantiated on a double-buffered UniformBuffer,
  15 + o Despite being a sparse shader, the full uniform block size is allocated,
  16 + o After touch, if everything is grey, then the color defined in the uniform
  17 + block is the same as the color defined in the vertex buffer.
  18 +
  19 +
  20 +
  21 +
  22 +
... ...
examples/uniform-blocks/shaders/uniform-block-alt.frag 0 → 100644
  1 +layout(std140) uniform FragmentBlock
  2 +{
  3 + lowp vec4 uColor;
  4 + mediump vec4 uColorArray[1024];
  5 + mediump int uColorIndex;
  6 +};
  7 +
  8 +void main()
  9 +{
  10 + // Test that the array color is the same as the actor color
  11 + fragColor = vec4(vec3(0.5, 0.5, 0.5) + vec3(uColorArray[uColorIndex].xyz-uColor.xyz)*0.5, 1.0);
  12 +}
... ...
examples/uniform-blocks/shaders/uniform-block.frag 0 → 100644
  1 +layout(std140) uniform FragmentBlock
  2 +{
  3 + lowp vec4 uColor;
  4 + mediump vec4 uColorArray[1024];
  5 + mediump int uColorIndex;
  6 +};
  7 +
  8 +void main()
  9 +{
  10 + fragColor = uColorArray[uColorIndex];
  11 +}
... ...
examples/uniform-blocks/shaders/uniform-block.vert 0 → 100644
  1 +INPUT vec2 aPosition;
  2 +
  3 +layout(std140) uniform VertexBlock
  4 +{
  5 + highp mat4 uMvpMatrix;
  6 + highp vec3 uSize;
  7 +};
  8 +
  9 +void main()
  10 +{
  11 + vec3 position = vec3(aPosition, 1.0) * uSize;
  12 + gl_Position = uMvpMatrix * vec4(position, 1);
  13 +}
... ...
examples/uniform-blocks/uniform-blocks-example.cpp 0 → 100644
  1 +/*
  2 + * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + *
  16 + */
  17 +
  18 +#include <dali-toolkit/dali-toolkit.h>
  19 +
  20 +#include "generated/uniform-block-vert.h"
  21 +#include "generated/uniform-block-frag.h"
  22 +#include "generated/uniform-block-alt-frag.h"
  23 +
  24 +using namespace Dali;
  25 +using Dali::Toolkit::TextLabel;
  26 +
  27 +
  28 +
  29 +/**
  30 + * This application tests that shaders with uniform blocks work as expected.
  31 + */
  32 +class UniformBlocksController : public ConnectionTracker
  33 +{
  34 +public:
  35 + UniformBlocksController(Application& application)
  36 + : mApplication(application)
  37 + {
  38 + // Connect to the Application's Init signal
  39 + mApplication.InitSignal().Connect(this, &UniformBlocksController::Create);
  40 + }
  41 +
  42 + ~UniformBlocksController() = default; // Nothing to do in destructor
  43 +
  44 + // The Init signal is received once (only) during the Application lifetime
  45 + void Create(Application& application)
  46 + {
  47 + // Get a handle to the window
  48 + Window window = application.GetWindow();
  49 + window.SetBackgroundColor(Color::WHITE);
  50 +
  51 + CreateShader(0);
  52 + CreateGeometry();
  53 + CreateRenderer();
  54 + mFirstActor = window.GetRootLayer().GetChildCount();
  55 +
  56 + for(int i=0; i<200; ++i)
  57 + {
  58 + AddActor(i);
  59 + }
  60 +
  61 + // Respond to a touch anywhere on the window
  62 + window.GetRootLayer().TouchedSignal().Connect(this, &UniformBlocksController::OnTouch);
  63 +
  64 + // Respond to key events
  65 + window.KeyEventSignal().Connect(this, &UniformBlocksController::OnKeyEvent);
  66 + mTimer = Timer::New(100);
  67 + mTimer.TickSignal().Connect(this, &UniformBlocksController::OnTick);
  68 + mTimer.Start();
  69 + }
  70 +
  71 + bool OnTick()
  72 + {
  73 + static int index=200;
  74 + Window window = mApplication.GetWindow();
  75 + Layer layer = window.GetRootLayer();
  76 + Actor child = layer.GetChildAt(mFirstActor);
  77 + UnparentAndReset(child);
  78 + AddActor(index);
  79 + index = (index+1)%1024;
  80 + return true;
  81 + }
  82 +
  83 + bool OnTouch(Actor actor, const TouchEvent& touch)
  84 + {
  85 + static int testNumber=0;
  86 + if(touch.GetState(0) == PointState::STARTED)
  87 + {
  88 + testNumber++;
  89 + if(testNumber >=2)
  90 + mApplication.Quit();
  91 +
  92 + CreateShader(testNumber);
  93 + mRenderer = Renderer::New(mGeometry, mShader);
  94 +
  95 + Actor parent = mApplication.GetWindow().GetRootLayer();
  96 + for(uint32_t i=0; i<parent.GetChildCount(); ++i)
  97 + {
  98 + parent.GetChildAt(i).RemoveRenderer(0);
  99 + parent.GetChildAt(i).AddRenderer(mRenderer);
  100 + }
  101 + }
  102 + return true;
  103 + }
  104 +
  105 + void OnKeyEvent(const KeyEvent& event)
  106 + {
  107 + if(event.GetState() == KeyEvent::DOWN)
  108 + {
  109 + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
  110 + {
  111 + mApplication.Quit();
  112 + }
  113 + }
  114 + }
  115 +
  116 + void AddActor(int n)
  117 + {
  118 + Actor actor = Actor::New();
  119 + actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
  120 + actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
  121 + actor.SetProperty(Actor::Property::POSITION, Vector3(Random::Range(-200.0f, 200.0f), Random::Range(-300.0f, 300.0f), 0.0f));;
  122 + actor.SetProperty( Actor::Property::SIZE, Vector2(32, 32) );
  123 + Vector4 color(Random::Range(0.1f,1.0f),Random::Range(0.1f,1.0f),Random::Range(0.1f,1.0f),1.0f);
  124 + actor.SetProperty( Actor::Property::COLOR, color);
  125 +
  126 + actor.AddRenderer(mRenderer);
  127 +
  128 + actor.RegisterProperty("uColorIndex", n);
  129 + char buffer[80];
  130 + sprintf(buffer, "uColorArray[%d]", n);
  131 + actor.RegisterProperty(buffer, color);
  132 +
  133 + Window window = mApplication.GetWindow();
  134 + window.Add(actor);
  135 + }
  136 +
  137 + void CreateShader(int testNumber)
  138 + {
  139 +
  140 +
  141 + // Create shaders
  142 + switch(testNumber)
  143 + {
  144 + case 0:
  145 + {
  146 + mShader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + std::string(SHADER_UNIFORM_BLOCK_VERT),
  147 + Dali::Shader::GetFragmentShaderPrefix() + std::string(SHADER_UNIFORM_BLOCK_FRAG));
  148 + break;
  149 + }
  150 + case 1:
  151 + {
  152 + mShader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + std::string(SHADER_UNIFORM_BLOCK_VERT),
  153 + Dali::Shader::GetFragmentShaderPrefix() + std::string(SHADER_UNIFORM_BLOCK_ALT_FRAG));
  154 + break;
  155 + }
  156 + }
  157 + }
  158 +
  159 + void CreateGeometry()
  160 + {
  161 + struct Vertex2D
  162 + {
  163 + Vertex2D(const Vector2& _co) :
  164 + co(_co){}
  165 + Dali::Vector2 co{};
  166 + };
  167 + const static Vector2 C(0.5f, 0.5f);
  168 + struct Quad2D
  169 + {
  170 + Vertex2D a0{Vector2(0.0f, 0.0f)-C};
  171 + Vertex2D a1{Vector2(1.0f, 0.0f)-C};
  172 + Vertex2D a2{Vector2(1.0f, 1.0f)-C};
  173 + Vertex2D a3{Vector2(0.0f, 0.0f)-C};
  174 + Vertex2D a4{Vector2(1.0f, 1.0f)-C};
  175 + Vertex2D a5{Vector2(0.0f, 1.0f)-C};
  176 + } QUAD;
  177 +
  178 + // Create geometry
  179 + Geometry geometry = Geometry::New();
  180 +
  181 + Property::Map attrMap{};
  182 + attrMap.Add("aPosition", Property::Type::VECTOR2);
  183 + VertexBuffer vb = VertexBuffer::New(attrMap);
  184 + vb.SetData( &QUAD, 6 );
  185 +
  186 + geometry.SetType( Geometry::Type::TRIANGLES);
  187 + geometry.AddVertexBuffer(vb);
  188 + mGeometry = geometry;
  189 + }
  190 +
  191 + void CreateRenderer()
  192 + {
  193 + mRenderer = Renderer::New( mGeometry, mShader);
  194 + }
  195 +
  196 +private:
  197 + Application& mApplication;
  198 +
  199 + Shader mShader;
  200 + Geometry mGeometry;
  201 + Renderer mRenderer;
  202 + uint32_t mFirstActor;
  203 + Timer mTimer;
  204 +};
  205 +
  206 +int DALI_EXPORT_API main(int argc, char** argv)
  207 +{
  208 + Application application = Application::New(&argc, &argv);
  209 + UniformBlocksController test(application);
  210 + application.MainLoop();
  211 + return 0;
  212 +}
... ...
resources/po/en_GB.po
... ... @@ -324,3 +324,6 @@ msgstr &quot;Web View&quot;
324 324  
325 325 msgid "DALI_DEMO_STR_TITLE_ANIMATED_VECTOR_IMAGES"
326 326 msgstr "Animated Vector Images"
  327 +
  328 +msgid "DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS"
  329 +msgstr "Uniform Blocks"
... ...
resources/po/en_US.po
... ... @@ -342,3 +342,6 @@ msgstr &quot;Text Bitmap Font&quot;
342 342  
343 343 msgid "DALI_DEMO_STR_TITLE_WAVES"
344 344 msgstr "Waves"
  345 +
  346 +msgid "DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS"
  347 +msgstr "Uniform Blocks"
... ...
shared/dali-demo-strings.h
... ... @@ -148,6 +148,7 @@ extern &quot;C&quot;
148 148 #define DALI_DEMO_STR_TITLE_TEXT_RENDERER dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_TEXT_RENDERER")
149 149 #define DALI_DEMO_STR_TITLE_TEXT_VISUAL dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_TEXT_VISUAL")
150 150 #define DALI_DEMO_STR_TITLE_TEXT_LABEL_BITMAP_FONT dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_TEXT_LABEL_BITMAP_FONT")
  151 +#define DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS")
151 152  
152 153 #else // !INTERNATIONALIZATION_ENABLED
153 154  
... ... @@ -265,6 +266,7 @@ extern &quot;C&quot;
265 266 #define DALI_DEMO_STR_TITLE_TEXT_RENDERER "Text Renderer"
266 267 #define DALI_DEMO_STR_TITLE_TEXT_VISUAL "Text Visual"
267 268 #define DALI_DEMO_STR_TITLE_TEXT_LABEL_BITMAP_FONT "Text Bitmap Font"
  269 +#define DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS "Uniform Blocks"
268 270 #endif
269 271  
270 272 #ifdef __cplusplus
... ...
tests-reel/dali-tests-reel.cpp
... ... @@ -59,6 +59,7 @@ int DALI_EXPORT_API main(int argc, char** argv)
59 59 demo.AddExample(Example("simple-text-renderer.example", DALI_DEMO_STR_TITLE_TEXT_RENDERER));
60 60 demo.AddExample(Example("simple-text-visual.example", DALI_DEMO_STR_TITLE_TEXT_VISUAL));
61 61 demo.AddExample(Example("simple-bitmap-font-text-label.example", DALI_DEMO_STR_TITLE_TEXT_LABEL_BITMAP_FONT));
  62 + demo.AddExample(Example("uniform-blocks.example", DALI_DEMO_STR_TITLE_UNIFORM_BLOCKS));
62 63  
63 64 demo.SortAlphabetically(true);
64 65  
... ...