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