Changed handling of uniforms of arrays of structs
[platform/core/uifw/dali-core.git] / dali / internal / update / common / uniform-map.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H
2 #define DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <cstdint> // uint32_t
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/common/hash.h>
26 #include <dali/devel-api/common/owner-container.h>
27 #include <dali/internal/common/const-string.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 class PropertyInputImpl;
34
35 namespace SceneGraph
36 {
37 /**
38  * The uniform map is used to map a uniform name to a property value.
39  */
40 class UniformPropertyMapping
41 {
42 public:
43   using Hash = unsigned long;
44
45   /**
46    * Constructor
47    */
48   UniformPropertyMapping(ConstString theUniformName, const PropertyInputImpl* thePropertyPtr)
49   : propertyPtr(thePropertyPtr),
50     uniformName(theUniformName),
51     uniformNameHash(0u),
52     uniformNameHashNoArray(0u),
53     arrayIndex(0u)
54   {
55     // Look for array index closing bracket
56     auto nameStringView = theUniformName.GetStringView();
57     auto pos            = nameStringView.rfind("]");
58
59     // If found, extract the array index and store it, if it's an element in an array of basic types.
60     if(pos != std::string::npos)
61     {
62       auto pos0 = theUniformName.GetStringView().rfind("[", pos);
63       if(pos == nameStringView.length() - 1) // if element is in struct, don't set array index.
64       {
65         arrayIndex = atoi(theUniformName.GetCString() + pos0 + 1);
66       }
67       // Calculate hash from name without array index
68       uniformNameHashNoArray = Dali::CalculateHash(theUniformName.GetStringView().substr(0, pos0).data(), '[');
69     }
70     uniformName     = theUniformName;
71     uniformNameHash = Dali::CalculateHash(theUniformName.GetCString());
72   }
73
74   UniformPropertyMapping() = default;
75
76   const PropertyInputImpl* propertyPtr{nullptr};
77   ConstString              uniformName{};
78   Hash                     uniformNameHash{0u};
79   Hash                     uniformNameHashNoArray{0u};
80   int32_t                  arrayIndex{0u};
81 };
82
83 /**
84  * The UniformMap class is used to map uniform names to property values. It is available
85  * in all of the classes responsible for rendering:
86  * Actor, Renderer, Geometry, TextureSet, Shader.
87  *
88  * It can be observed for changes to the mapping table.
89  */
90 class UniformMap
91 {
92 public:
93   using SizeType = uint32_t;
94
95   class Observer
96   {
97   public:
98     /**
99      * Inform observer that uniform mappings have been changed
100      * @param mappings
101      */
102     virtual void UniformMappingsChanged(const UniformMap& mappings) = 0;
103
104   protected:
105     /**
106      * Virtual destructor, no deletion through this interface
107      */
108     virtual ~Observer() = default;
109   };
110
111   /**
112    * Add an observer that watches for changes in the mappings
113    */
114   void AddObserver(Observer& observer);
115
116   /**
117    * Remove an observer
118    */
119   void RemoveObserver(Observer& observer);
120
121   /**
122    * Add a map to the mappings table.
123    */
124   void Add(UniformPropertyMapping map);
125
126   /**
127    * Remove a map from the mappings table
128    */
129   void Remove(ConstString uniformName);
130
131   /**
132    * Find a property given the uniform name.
133    * @return The address of the property if it's in the map, or NULL otherwise.
134    */
135   const PropertyInputImpl* Find(ConstString uniformName);
136
137   /**
138    * Get the count of uniforms in the map
139    * @return The number of uniform mappings
140    */
141   SizeType Count() const;
142
143   /**
144    * @pre index must be in the range 0 :: Count()-1
145    * @param[in] index The index of the element to fetch
146    * @return reference to the element in the map
147    */
148   const UniformPropertyMapping& operator[](SizeType index) const;
149
150 private:
151   /**
152    * Helper to call the observers when the mappings have changed
153    */
154   void MappingChanged();
155
156 private:
157   using UniformMapContainer = Dali::Vector<UniformPropertyMapping>;
158   using UniformMapIter      = UniformMapContainer::Iterator;
159   using Observers           = Dali::Vector<Observer*>;
160   using ObserversIter       = Observers::Iterator;
161
162   UniformMapContainer mUniformMaps; // Owner container of uniform maps
163
164   Observers mObservers;
165 };
166
167 } // namespace SceneGraph
168 } // namespace Internal
169 } // namespace Dali
170
171 #endif // DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H