Updated all header files to new format
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / material-definition.h
1 #ifndef DALI_SCENE_LOADER_MATERIAL_DEFINITION_H
2 #define DALI_SCENE_LOADER_MATERIAL_DEFINITION_H
3 /*
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include "dali-scene-loader/public-api/api.h"
22 #include "dali-scene-loader/public-api/environment-definition.h"
23 #include "dali-scene-loader/public-api/index.h"
24 #include "dali-scene-loader/public-api/utils.h"
25
26 // EXTERNAL INCLUDES
27 #include <cmath>
28 #include "dali/public-api/common/vector-wrapper.h"
29 #include "dali/public-api/math/vector4.h"
30
31 namespace Dali
32 {
33 namespace SceneLoader
34 {
35 /**
36  * @brief Helper enum for encoding and decoding sampler states.
37  */
38 struct DALI_SCENE_LOADER_API SamplerFlags
39 {
40   using Type = uint8_t;
41
42   enum Values : Type
43   {
44     // Filter - 3 bits
45     FILTER_NEAREST        = 0,
46     FILTER_LINEAR         = NthBit(0),
47     FILTER_MIPMAP_NEAREST = NthBit(1),
48     FILTER_MIPMAP_LINEAR  = NthBit(2),
49
50     // Wrap - 2 bits
51     WRAP_REPEAT = 0,
52     WRAP_CLAMP  = NthBit(0),
53     WRAP_MIRROR = NthBit(1),
54
55     // Layout - apply shift, then mask
56     FILTER_MIN_BITS = 3,
57     FILTER_MIN_MASK = NthBit(FILTER_MIN_BITS) - 1,
58
59     FILTER_MAG_BITS  = 1,
60     FILTER_MAG_SHIFT = FILTER_MIN_BITS,
61     FILTER_MAG_MASK  = NthBit(FILTER_MAG_BITS) - 1,
62
63     WRAP_S_BITS  = 2,
64     WRAP_S_SHIFT = FILTER_MAG_SHIFT + FILTER_MAG_BITS,
65     WRAP_S_MASK  = NthBit(WRAP_S_BITS) - 1,
66
67     WRAP_T_BITS  = 2,
68     WRAP_T_SHIFT = WRAP_S_SHIFT + WRAP_S_BITS,
69     WRAP_T_MASK  = NthBit(WRAP_T_BITS) - 1,
70
71     // Diagnostics
72     MIPMAP_MASK = FILTER_MIPMAP_LINEAR | FILTER_MIPMAP_NEAREST,
73
74     // Default
75     DEFAULT = FILTER_LINEAR | (FILTER_LINEAR << FILTER_MAG_SHIFT) | (WRAP_REPEAT << WRAP_S_SHIFT) | (WRAP_REPEAT << WRAP_T_SHIFT), // LINEAR filters, REPEAT wraps
76   };
77
78   /**
79    * @return SamplerFlags bit pattern calculated from the given Dali Sampler settings.
80    */
81   static Type Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT);
82
83   /**
84    * @brief Decodes the minification filter patter of @a flags into the corresponding FilterMode.
85    */
86   static FilterMode::Type GetMinFilter(Type flags);
87
88   /**
89    * @brief Decodes the magnification filter patter of @a flags into the corresponding FilterMode.
90    */
91   static FilterMode::Type GetMagFilter(Type flags);
92
93   /**
94    * @brief Decodes the horizontal wrap pattern of @a flags into the corresponding WrapMode.
95    */
96   static WrapMode::Type GetWrapS(Type flags);
97
98   /**
99    * @brief Decodes the vertical wrap pattern of @a flags into the corresponding WrapMode.
100    */
101   static WrapMode::Type GetWrapT(Type flags);
102
103   /**
104    * @brief Creates a Sampler with the settings encoded in @a flags.
105    */
106   static Sampler MakeSampler(Type flags);
107 };
108
109 /**
110  * @brief Defines a texture from a combination of an image URI and its sampler definition.
111  */
112 struct DALI_SCENE_LOADER_API TextureDefinition
113 {
114   std::string        mImageUri;
115   SamplerFlags::Type mSamplerFlags;
116
117   TextureDefinition(const std::string& imageUri = "", SamplerFlags::Type samplerFlags = SamplerFlags::DEFAULT);
118 };
119
120 /**
121  * @brief Defines a material with a number of texture stages, whether mipmappping
122  *  is enabled, and an index of an environment (usually of all environments in a
123  *  scene). Textures from the environment are added last when the DALi TextureSet
124  *  is being created.
125  */
126 struct DALI_SCENE_LOADER_API MaterialDefinition
127 {
128   enum Flags : uint32_t
129   {
130     // Texture semantics
131     ALBEDO     = NthBit(0),
132     METALLIC   = NthBit(1),
133     ROUGHNESS  = NthBit(2),
134     NORMAL     = NthBit(3),
135     EMISSIVE   = NthBit(4), // TODO: support
136     OCCLUSION  = NthBit(5), // TODO: support
137     SUBSURFACE = NthBit(6), // Note: dli-only
138
139     // Other binary options
140     TRANSPARENCY  = NthBit(20),
141     GLTF_CHANNELS = NthBit(21), // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#pbrmetallicroughnessmetallicroughnesstexture
142
143     // Alpha cutoff - reserved from the 24th bit
144     ALPHA_CUTOFF_BITS  = 8,
145     ALPHA_CUTOFF_SHIFT = sizeof(uint32_t) * 8 - ALPHA_CUTOFF_BITS,
146     ALPHA_CUTOFF_MASK  = (1 << ALPHA_CUTOFF_BITS) - 1,
147   };
148
149   /**
150    * @brief A(n image based) texture that's used in a material.
151    */
152   struct TextureStage
153   {
154     uint32_t          mSemantic;
155     TextureDefinition mTexture;
156   };
157
158   using Vector = std::vector<std::pair<MaterialDefinition, TextureSet>>;
159
160   struct RawData
161   {
162     struct TextureData
163     {
164       PixelData          mPixels;
165       SamplerFlags::Type mSamplerFlags;
166     };
167
168     std::vector<TextureData> mTextures;
169   };
170
171   MaterialDefinition() = default;
172
173   MaterialDefinition(const MaterialDefinition&) = delete;
174   MaterialDefinition& operator=(const MaterialDefinition&) = delete;
175
176   MaterialDefinition(MaterialDefinition&&) = default;
177   MaterialDefinition& operator=(MaterialDefinition&&) = default;
178
179   /**
180    * @brief Loads (or, in the case of solid color materials, creates) raw pixel data,
181    *  which is then returned.
182    * @note This may be called from any thread.
183    */
184   RawData LoadRaw(const std::string& imagesPath) const;
185
186   /**
187    * @brief Creates Textures from the pixel data in @a raw, gets the
188    *  the cube maps from the iEnvironment'th element of @a environments,
189    *  then creates a DALi TextureSet and returns it.
190    * @note This must be called from the event thread.
191    * @note The textures are added in the following order: 2D, cube maps.
192    */
193   TextureSet Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const;
194
195   /**
196    * @brief Checks if the given mask matches any of the textures defined.
197    */
198   bool CheckTextures(uint32_t flags) const;
199
200   /**
201    * @return The alpha test reference value.
202    * @note A value of 0.f means no alpha testing.
203    */
204   float GetAlphaCutoff() const
205   {
206     return ((mFlags >> ALPHA_CUTOFF_SHIFT) & ALPHA_CUTOFF_MASK) / 255.f;
207   }
208
209   /**
210    * @brief Encodes the alpha test reference @a value in flags.
211    * @note A value of 0.f means no alpha testing.
212    */
213   void SetAlphaCutoff(float value)
214   {
215     DALI_ASSERT_DEBUG(value >= 0.f && value <= 1.f);
216     mFlags |= static_cast<uint8_t>(std::round(value * 255.f)) << ALPHA_CUTOFF_SHIFT;
217   }
218
219 public: // DATA
220   uint32_t mFlags = 0x0;
221
222   Index                     mEnvironmentIdx = 0;
223   Vector4                   mColor          = Color::WHITE;
224   float                     mMetallic       = 1.f;
225   float                     mRoughness      = 1.f;
226   std::vector<TextureStage> mTextureStages;
227 };
228
229 } // namespace SceneLoader
230 } // namespace Dali
231
232 #endif //DALI_SCENE_LOADER_MATERIAL_DEFINITION_H