3b1482e3d25882f4c94ecc033a6eb4f382df3fc2
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.h
1 #ifndef DALI_INTERNAL_SHADER_H
2 #define DALI_INTERNAL_SHADER_H
3
4 /*
5  * Copyright (c) 2024 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
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/shader-data.h>            // ShaderPtr
23 #include <dali/internal/event/common/object-connector.h> // Dali::Internal::ObjectConnector
24 #include <dali/internal/event/common/object-impl.h>      // Dali::Internal::Object
25 #include <dali/public-api/common/dali-common.h>          // DALI_ASSERT_ALWAYS
26 #include <dali/public-api/common/intrusive-ptr.h>        // Dali::IntrusivePtr
27 #include <dali/public-api/rendering/shader.h>            // Dali::Shader
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace SceneGraph
34 {
35 class Shader;
36 }
37
38 class Shader;
39 using ShaderPtr = IntrusivePtr<Shader>;
40
41 /**
42  * Shader is an object that contains an array of structures of values that
43  * can be accessed as properties.
44  */
45 class Shader : public Object
46 {
47 public:
48   /**
49    * @copydoc Dali::Shader::New()
50    */
51   static ShaderPtr New(std::string_view          vertexShader,
52                        std::string_view          fragmentShader,
53                        Dali::Shader::Hint::Value hints,
54                        std::string_view          shaderName);
55
56   /**
57    * @copydoc Dali::Shader::New()
58    */
59   static ShaderPtr New(Dali::Property::Value shaderMap);
60
61   /**
62    * Retrieve the scene-graph shader added by this object.
63    * @return A pointer to the shader.
64    */
65   const SceneGraph::Shader& GetShaderSceneObject() const;
66
67 public: // Default property extensions from Object
68   /**
69    * @copydoc Dali::Internal::Object::SetDefaultProperty()
70    */
71   void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue) override;
72
73   /**
74    * @copydoc Dali::Internal::Object::GetDefaultProperty()
75    */
76   Property::Value GetDefaultProperty(Property::Index index) const override;
77
78   /**
79    * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
80    */
81   Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const override;
82
83 private: // implementation
84   /**
85    * Constructor
86    *
87    * @param[in] sceneObject the scene object
88    */
89   Shader(const SceneGraph::Shader* sceneObject);
90
91   /**
92    * @brief Update Shader Data
93    * If a ShaderData of the same renderPassTag is already exist, it is replaced,
94    * if not, new ShaderData is added.
95    * @param[in] vertexShader Vertex shader code for the effect.
96    * @param[in] fragmentShader Fragment Shader code for the effect.
97    * @param[in] renderPassTag render pass tag of this shader data
98    * @param[in] hints Hints to define the geometry of the rendered object
99    * @param[in] name The name of shader data.
100    */
101   void UpdateShaderData(std::string_view vertexShader, std::string_view fragmentShader, uint32_t renderPassTag, Dali::Shader::Hint::Value hints, std::string_view name);
102
103   /**
104    * @brief Sets shader data from shaderMap.
105    * The shaderMap should be Property::Map or Property::Array.
106    * @param[in] shaderMap shader property map.
107    */
108   void SetShaderProperty(const Dali::Property::Value& shaderMap);
109
110 protected:
111   /**
112    * A reference counted object may only be deleted by calling Unreference()
113    */
114   ~Shader() override;
115
116 private: // unimplemented methods
117   Shader()              = delete;
118   Shader(const Shader&) = delete;
119   Shader& operator=(const Shader&) = delete;
120
121 private:
122   std::vector<Internal::ShaderDataPtr> mShaderDataList;
123
124 public:
125   /**
126    * @copydoc Dali::Shader::GetShaderVersionPrefix()
127    */
128   static std::string GetShaderVersionPrefix();
129
130   /**
131    * @copydoc Dali::Shader::GetVertexShaderPrefix()
132    */
133   static std::string GetVertexShaderPrefix();
134
135   /**
136    * @copydoc Dali::Shader::GetFragmentShaderPrefix()
137    */
138   static std::string GetFragmentShaderPrefix();
139 };
140
141 } // namespace Internal
142
143 // Helpers for public-api forwarding methods
144 inline Internal::Shader& GetImplementation(Dali::Shader& handle)
145 {
146   DALI_ASSERT_ALWAYS(handle && "Shader handle is empty");
147
148   BaseObject& object = handle.GetBaseObject();
149
150   return static_cast<Internal::Shader&>(object);
151 }
152
153 inline const Internal::Shader& GetImplementation(const Dali::Shader& handle)
154 {
155   DALI_ASSERT_ALWAYS(handle && "Shader handle is empty");
156
157   const BaseObject& object = handle.GetBaseObject();
158
159   return static_cast<const Internal::Shader&>(object);
160 }
161
162 } // namespace Dali
163
164 #endif // DALI_INTERNAL_SHADER_H