Convert shaders in dali-demo to use shader compilation tool
[platform/core/uifw/dali-demo.git] / examples / compressed-texture-formats / compressed-texture-formats-example.cpp
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 // EXTERNAL INCLUDES
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <dali/dali.h>
22
23 // INTERNAL INCLUDES
24 #include "shared/utility.h"
25 #include "generated/compressed-texture-formats-example-vert.h"
26 #include "generated/compressed-texture-formats-example-frag.h"
27
28 using namespace Dali;
29 using Dali::Toolkit::TextLabel;
30
31 namespace
32 {
33 const char* IMAGE_FILENAME_ETC                = DEMO_IMAGE_DIR "tx-etc1.ktx";
34 const char* IMAGE_FILENAME_ASTC_LINEAR        = DEMO_IMAGE_DIR "tx-astc-4x4-linear.ktx";
35 const char* IMAGE_FILENAME_ASTC_LINEAR_NATIVE = DEMO_IMAGE_DIR "tx-astc-4x4-linear-native.astc";
36
37 /**
38  * @brief Create a renderer to render an image and adds it to an actor
39  * @param[in] imagePath The path where the image file is located
40  * @param[in] actor The actor that will be used to render the image
41  * @param[in[ geometry The geometry to use
42  * @param[in] shader The shader to use
43  */
44 void AddImage(const char* imagePath, Actor& actor, Geometry& geometry, Shader& shader)
45 {
46   //Load the texture
47   Texture    texture    = DemoHelper::LoadTexture(imagePath);
48   TextureSet textureSet = TextureSet::New();
49   textureSet.SetTexture(0u, texture);
50
51   //Create the renderer
52   Renderer renderer = Renderer::New(geometry, shader);
53   renderer.SetTextures(textureSet);
54
55   //Set actor size and add the renderer
56   actor.SetProperty(Actor::Property::SIZE, Vector2(texture.GetWidth(), texture.GetHeight()));
57   actor.AddRenderer(renderer);
58 }
59
60 } // namespace
61 /**
62  * @brief This example shows 3 images, each of a different compressed texture type.
63  * If built and run on a OpenGL ES 3.1 compatable target, then all 3 images will display.
64  * Otherwise, the top image will display and the other 2 will appear as black squares.
65  */
66 class CompressedTextureFormatsController : public ConnectionTracker
67 {
68 public:
69   CompressedTextureFormatsController(Application& application)
70   : mApplication(application)
71   {
72     // Connect to the Application's Init signal
73     mApplication.InitSignal().Connect(this, &CompressedTextureFormatsController::Create);
74   }
75
76   ~CompressedTextureFormatsController()
77   {
78     // Nothing to do here;
79   }
80
81   // The Init signal is received once (only) during the Application lifetime
82   void Create(Application& application)
83   {
84     // Get a handle to the window
85     Window window = application.GetWindow();
86     window.SetBackgroundColor(Color::WHITE);
87
88     // Setup a TableView to hold a grid of images and labels.
89     Toolkit::TableView table = Toolkit::TableView::New(3u, 2u);
90     table.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
91     table.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
92     table.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
93     table.SetRelativeWidth(0u, 0.5f);
94     table.SetRelativeWidth(1u, 0.5f);
95     table.SetRelativeHeight(0u, 1.0f / 3.0f);
96     table.SetRelativeHeight(1u, 1.0f / 3.0f);
97     table.SetRelativeHeight(2u, 1.0f / 3.0f);
98
99     // Add text labels.
100     TextLabel textLabel = TextLabel::New("ETC1 (KTX):");
101     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
102     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
103     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
104     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
105     table.AddChild(textLabel, Toolkit::TableView::CellPosition(0u, 0u));
106     table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
107
108     textLabel = TextLabel::New("ASTC (KTX) 4x4 linear:");
109     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
110     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
111     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
112     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
113     table.AddChild(textLabel, Toolkit::TableView::CellPosition(1u, 0u));
114     table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
115
116     textLabel = TextLabel::New("ASTC (Native) 4x4 linear:");
117     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
118     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
119     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
120     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
121     table.AddChild(textLabel, Toolkit::TableView::CellPosition(2u, 0u));
122     table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
123
124     //Create the geometry and the shader renderers will use
125     Geometry geometry = DemoHelper::CreateTexturedQuad();
126     Shader   shader   = Shader::New(SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_VERT, SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_FRAG);
127
128     // Add images.
129     Actor actor = Actor::New();
130     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
131     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
132     AddImage(IMAGE_FILENAME_ETC, actor, geometry, shader);
133     table.AddChild(actor, Toolkit::TableView::CellPosition(0u, 1u));
134     table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
135
136     actor = Actor::New();
137     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
138     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
139     AddImage(IMAGE_FILENAME_ASTC_LINEAR, actor, geometry, shader);
140     table.AddChild(actor, Toolkit::TableView::CellPosition(1u, 1u));
141     table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
142
143     actor = Actor::New();
144     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
145     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
146     AddImage(IMAGE_FILENAME_ASTC_LINEAR_NATIVE, actor, geometry, shader);
147     table.AddChild(actor, Toolkit::TableView::CellPosition(2u, 1u));
148     table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
149
150     window.Add(table);
151
152     // Respond to touch and key signals
153     window.GetRootLayer().TouchedSignal().Connect(this, &CompressedTextureFormatsController::OnTouch);
154     window.KeyEventSignal().Connect(this, &CompressedTextureFormatsController::OnKeyEvent);
155   }
156
157   bool OnTouch(Actor actor, const TouchEvent& touch)
158   {
159     // quit the application
160     mApplication.Quit();
161     return true;
162   }
163
164   void OnKeyEvent(const KeyEvent& event)
165   {
166     if(event.GetState() == KeyEvent::DOWN)
167     {
168       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
169       {
170         mApplication.Quit();
171       }
172     }
173   }
174
175 private:
176   Application& mApplication;
177 };
178
179 int DALI_EXPORT_API main(int argc, char** argv)
180 {
181   Application                        application = Application::New(&argc, &argv);
182   CompressedTextureFormatsController test(application);
183   application.MainLoop();
184   return 0;
185 }