119f7c4de8ac0fb986ba9be233648dfbd89028a6
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / material-definition.h
1 #ifndef DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H
2 #define DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H
3 /*
4  * Copyright (c) 2022 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-scene3d/public-api/api.h"
22 #include "dali-scene3d/public-api/loader/environment-definition.h"
23 #include "dali-scene3d/public-api/loader/index.h"
24 #include "dali-scene3d/public-api/loader/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 Scene3D
34 {
35 namespace Loader
36 {
37 /**
38  * @brief Helper enum for encoding and decoding sampler states.
39  */
40 struct DALI_SCENE3D_API SamplerFlags
41 {
42   using Type = uint8_t;
43
44   enum Values : Type
45   {
46     // Filter - 3 bits
47     FILTER_NEAREST        = 0,
48     FILTER_LINEAR         = NthBit(0),
49     FILTER_MIPMAP_NEAREST = NthBit(1),
50     FILTER_MIPMAP_LINEAR  = NthBit(2),
51
52     // Wrap - 2 bits
53     WRAP_REPEAT = 0,
54     WRAP_CLAMP  = NthBit(0),
55     WRAP_MIRROR = NthBit(1),
56
57     // Layout - apply shift, then mask
58     FILTER_MIN_BITS = 3,
59     FILTER_MIN_MASK = NthBit(FILTER_MIN_BITS) - 1,
60
61     FILTER_MAG_BITS  = 1,
62     FILTER_MAG_SHIFT = FILTER_MIN_BITS,
63     FILTER_MAG_MASK  = NthBit(FILTER_MAG_BITS) - 1,
64
65     WRAP_S_BITS  = 2,
66     WRAP_S_SHIFT = FILTER_MAG_SHIFT + FILTER_MAG_BITS,
67     WRAP_S_MASK  = NthBit(WRAP_S_BITS) - 1,
68
69     WRAP_T_BITS  = 2,
70     WRAP_T_SHIFT = WRAP_S_SHIFT + WRAP_S_BITS,
71     WRAP_T_MASK  = NthBit(WRAP_T_BITS) - 1,
72
73     // Diagnostics
74     MIPMAP_MASK = FILTER_MIPMAP_LINEAR | FILTER_MIPMAP_NEAREST,
75
76     // Default
77     DEFAULT = FILTER_LINEAR | (FILTER_LINEAR << FILTER_MAG_SHIFT) | (WRAP_REPEAT << WRAP_S_SHIFT) | (WRAP_REPEAT << WRAP_T_SHIFT), // LINEAR filters, REPEAT wraps
78   };
79
80   /**
81    * @return SamplerFlags bit pattern calculated from the given Dali Sampler settings.
82    */
83   static Type Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT);
84
85   /**
86    * @brief Decodes the minification filter patter of @a flags into the corresponding FilterMode.
87    */
88   static FilterMode::Type GetMinFilter(Type flags);
89
90   /**
91    * @brief Decodes the magnification filter patter of @a flags into the corresponding FilterMode.
92    */
93   static FilterMode::Type GetMagFilter(Type flags);
94
95   /**
96    * @brief Decodes the horizontal wrap pattern of @a flags into the corresponding WrapMode.
97    */
98   static WrapMode::Type GetWrapS(Type flags);
99
100   /**
101    * @brief Decodes the vertical wrap pattern of @a flags into the corresponding WrapMode.
102    */
103   static WrapMode::Type GetWrapT(Type flags);
104
105   /**
106    * @brief Creates a Sampler with the settings encoded in @a flags.
107    */
108   static Sampler MakeSampler(Type flags);
109 };
110
111 /**
112  * @brief Defines a texture from a combination of an image URI and its sampler definition.
113  */
114 struct DALI_SCENE3D_API TextureDefinition
115 {
116   std::string        mImageUri;
117   SamplerFlags::Type mSamplerFlags;
118
119   TextureDefinition(const std::string& imageUri = "", SamplerFlags::Type samplerFlags = SamplerFlags::DEFAULT);
120 };
121
122 /**
123  * @brief Defines a material with a number of texture stages, whether mipmappping
124  *  is enabled, and an index of an environment (usually of all environments in a
125  *  scene). Textures from the environment are added last when the DALi TextureSet
126  *  is being created.
127  */
128 struct DALI_SCENE3D_API MaterialDefinition
129 {
130   enum Flags : uint32_t
131   {
132     // Texture semantics
133     ALBEDO         = NthBit(0),
134     METALLIC       = NthBit(1),
135     ROUGHNESS      = NthBit(2),
136     NORMAL         = NthBit(3),
137     EMISSIVE       = NthBit(4),
138     OCCLUSION      = NthBit(5),
139     SPECULAR       = NthBit(6),
140     SPECULAR_COLOR = NthBit(7),
141     SUBSURFACE     = NthBit(8), // Note: dli-only
142
143     // Other binary options
144     TRANSPARENCY  = NthBit(20),
145     GLTF_CHANNELS = NthBit(21), // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#pbrmetallicroughnessmetallicroughnesstexture
146
147     // Alpha cutoff - reserved from the 24th bit
148     ALPHA_CUTOFF_BITS  = 8,
149     ALPHA_CUTOFF_SHIFT = sizeof(uint32_t) * 8 - ALPHA_CUTOFF_BITS,
150     ALPHA_CUTOFF_MASK  = (1 << ALPHA_CUTOFF_BITS) - 1,
151   };
152
153   /**
154    * @brief A(n image based) texture that's used in a material.
155    */
156   struct TextureStage
157   {
158     uint32_t          mSemantic;
159     TextureDefinition mTexture;
160   };
161
162   using Vector = std::vector<std::pair<MaterialDefinition, TextureSet>>;
163
164   struct RawData
165   {
166     struct TextureData
167     {
168       PixelData          mPixels;
169       SamplerFlags::Type mSamplerFlags;
170     };
171
172     std::vector<TextureData> mTextures;
173   };
174
175   MaterialDefinition() = default;
176
177   MaterialDefinition(const MaterialDefinition&) = delete;
178   MaterialDefinition& operator=(const MaterialDefinition&) = delete;
179
180   MaterialDefinition(MaterialDefinition&&) = default;
181   MaterialDefinition& operator=(MaterialDefinition&&) = default;
182
183   /**
184    * @brief Loads (or, in the case of solid color materials, creates) raw pixel data,
185    *  which is then returned.
186    * @note This may be called from any thread.
187    */
188   RawData LoadRaw(const std::string& imagesPath) const;
189
190   /**
191    * @brief Creates Textures from the pixel data in @a raw, gets the
192    *  the cube maps from the iEnvironment'th element of @a environments,
193    *  then creates a DALi TextureSet and returns it.
194    * @note This must be called from the event thread.
195    * @note The textures are added in the following order: 2D, cube maps.
196    */
197   TextureSet Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const;
198
199   /**
200    * @brief Checks if the given mask matches any of the textures defined.
201    */
202   bool CheckTextures(uint32_t flags) const;
203
204   /**
205    * @return The alpha test reference value.
206    * @note A value of 0.f means no alpha testing.
207    */
208   float GetAlphaCutoff() const
209   {
210     return ((mFlags >> ALPHA_CUTOFF_SHIFT) & ALPHA_CUTOFF_MASK) / 255.f;
211   }
212
213   /**
214    * @brief Encodes the alpha test reference @a value in flags.
215    * @note A value of 0.f means no alpha testing.
216    */
217   void SetAlphaCutoff(float value)
218   {
219     DALI_ASSERT_DEBUG(value >= 0.f && value <= 1.f);
220     mFlags |= static_cast<uint8_t>(std::round(value * 255.f)) << ALPHA_CUTOFF_SHIFT;
221   }
222
223 public: // DATA
224   uint32_t mFlags = 0x0;
225
226   Index   mEnvironmentIdx      = 0;
227   Vector4 mColor               = Color::WHITE;
228   float   mMetallic            = 1.f;
229   float   mRoughness           = 1.f;
230   Vector4 mBaseColorFactor     = Vector4::ONE;
231   float   mNormalScale         = 1.f;
232   float   mOcclusionStrength   = 1.f;
233   Vector3 mEmissiveFactor      = Vector3::ZERO;
234   float   mDielectricSpecular  = 0.04f;
235   float   mSpecularFactor      = 1.0f;
236   Vector3 mSpecularColorFactor = Vector3::ONE;
237
238   // For the glTF, each of albedo, metallicRoughness, normal textures are not essential.
239   bool mNeedAlbedoTexture            = true;
240   bool mNeedMetallicRoughnessTexture = true;
241   bool mNeedNormalTexture            = true;
242   bool mDoubleSided                  = false;
243
244   std::vector<TextureStage> mTextureStages;
245 };
246
247 } // namespace Loader
248 } // namespace Scene3D
249 } // namespace Dali
250
251 #endif //DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H