1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_DATA_STORE_RENDER_DATA_STORE_POST_PROCESS_H
17 #define RENDER_DATA_STORE_RENDER_DATA_STORE_POST_PROCESS_H
18 
19 #include <cstdint>
20 #include <mutex>
21 
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <base/containers/unordered_map.h>
25 #include <base/math/vector.h>
26 #include <base/util/uid.h>
27 #include <render/datastore/intf_render_data_store_post_process.h>
28 #include <render/datastore/render_data_store_render_pods.h>
29 #include <render/namespace.h>
30 
31 RENDER_BEGIN_NAMESPACE()
32 class IRenderContext;
33 
34 /**
35  * RenderDataStorePostProcess implementation.
36  */
37 class RenderDataStorePostProcess final : public IRenderDataStorePostProcess {
38 public:
39     RenderDataStorePostProcess(const IRenderContext& renderContex, const BASE_NS::string_view name);
40     ~RenderDataStorePostProcess() override;
41 
CommitFrameData()42     void CommitFrameData() override {};
PreRender()43     void PreRender() override {};
PostRender()44     void PostRender() override {};
PreRenderBackend()45     void PreRenderBackend() override {};
PostRenderBackend()46     void PostRenderBackend() override {};
Clear()47     void Clear() override {};
GetFlags()48     uint32_t GetFlags() const override
49     {
50         return 0;
51     };
52 
53     void Create(const BASE_NS::string_view name) override;
54     void Create(const BASE_NS::string_view name, const BASE_NS::string_view ppName,
55         const RenderHandleReference& shader) override;
56 
57     void Destroy(const BASE_NS::string_view name) override;
58     void Destroy(const BASE_NS::string_view name, const BASE_NS::string_view ppName) override;
59 
60     bool Contains(const BASE_NS::string_view name) const override;
61     bool Contains(const BASE_NS::string_view name, const BASE_NS::string_view ppName) const override;
62 
63     void Set(const BASE_NS::string_view name, const BASE_NS::string_view ppName,
64         const PostProcess::Variables& vars) override;
65     void Set(const BASE_NS::string_view name, const BASE_NS::array_view<PostProcess::Variables> vars) override;
66 
67     GlobalFactors GetGlobalFactors(const BASE_NS::string_view name) const override;
68     BASE_NS::vector<PostProcess> Get(const BASE_NS::string_view name) const override;
69     PostProcess Get(const BASE_NS::string_view name, const BASE_NS::string_view ppName) const override;
70 
71     // for plugin / factory interface
72     static constexpr const char* const TYPE_NAME = "RenderDataStorePostProcess";
73 
74     static IRenderDataStore* Create(IRenderContext& renderContext, char const* name);
75     static void Destroy(IRenderDataStore* instance);
76 
GetTypeName()77     BASE_NS::string_view GetTypeName() const override
78     {
79         return TYPE_NAME;
80     }
81 
GetName()82     BASE_NS::string_view GetName() const override
83     {
84         return name_;
85     }
86 
GetUid()87     const BASE_NS::Uid& GetUid() const override
88     {
89         return UID;
90     }
91 
92 private:
93     const IRenderContext& renderContext_;
94     const BASE_NS::string name_;
95 
96     struct PostProcessStack {
97         GlobalFactors globalFactors;
98         BASE_NS::vector<PostProcess> postProcesses;
99     };
100 
101     // must be locked when called
102     void CreateFromPod(const BASE_NS::string_view name);
103     void SetImpl(const PostProcess::Variables& vars, const uint32_t ppIndex, PostProcessStack& ppStack);
104     void SetGlobalFactorsImpl(const PostProcess::Variables& vars, const uint32_t ppIndex, PostProcessStack& ppStack);
105     void GetShaderProperties(const RenderHandleReference& shader, PostProcess::Variables& vars);
106     void FillDefaultPostProcessData(const PostProcessConfiguration& ppConfig, PostProcessStack& ppStack);
107 
108     BASE_NS::unordered_map<BASE_NS::string, PostProcessStack> allPostProcesses_;
109 
110     mutable std::mutex mutex_;
111 };
112 
113 /**
114  * PostProcessConversion helper.
115  */
116 class PostProcessConversionHelper final {
117 public:
GetFactorTonemap(const PostProcessConfiguration & input)118     static inline BASE_NS::Math::Vec4 GetFactorTonemap(const PostProcessConfiguration& input)
119     {
120         return { input.tonemapConfiguration.exposure, 0.0f, 0.0f,
121             static_cast<float>(input.tonemapConfiguration.tonemapType) };
122     }
123 
GetFactorVignette(const PostProcessConfiguration & input)124     static inline BASE_NS::Math::Vec4 GetFactorVignette(const PostProcessConfiguration& input)
125     {
126         return { input.vignetteConfiguration.coefficient, input.vignetteConfiguration.power, 0.0f, 0.0f };
127     }
128 
GetFactorDither(const PostProcessConfiguration & input)129     static inline BASE_NS::Math::Vec4 GetFactorDither(const PostProcessConfiguration& input)
130     {
131         return { input.ditherConfiguration.amountCoefficient, 0, 0,
132             static_cast<float>(input.ditherConfiguration.ditherType) };
133     }
134 
GetFactorColorConversion(const PostProcessConfiguration & input)135     static inline BASE_NS::Math::Vec4 GetFactorColorConversion(const PostProcessConfiguration& input)
136     {
137         return { 0.0f, 0.0f, 0.0f, static_cast<float>(input.colorConversionConfiguration.conversionFunctionType) };
138     }
139 
GetFactorFringe(const PostProcessConfiguration & input)140     static inline BASE_NS::Math::Vec4 GetFactorFringe(const PostProcessConfiguration& input)
141     {
142         return { input.colorFringeConfiguration.coefficient, input.colorFringeConfiguration.distanceCoefficient, 0.0f,
143             0.0f };
144     }
145 
GetFactorBlur(const PostProcessConfiguration & input)146     static inline BASE_NS::Math::Vec4 GetFactorBlur(const PostProcessConfiguration& input)
147     {
148         return { static_cast<float>(input.blurConfiguration.blurType),
149             static_cast<float>(input.blurConfiguration.blurQualityType), input.blurConfiguration.filterSize, 0 };
150     }
151 
GetFactorBloom(const PostProcessConfiguration & input)152     static inline BASE_NS::Math::Vec4 GetFactorBloom(const PostProcessConfiguration& input)
153     {
154         return { input.bloomConfiguration.thresholdHard, input.bloomConfiguration.thresholdSoft,
155             input.bloomConfiguration.amountCoefficient, input.bloomConfiguration.dirtMaskCoefficient };
156     }
157 
GetFactorFxaa(const PostProcessConfiguration & input)158     static inline BASE_NS::Math::Vec4 GetFactorFxaa(const PostProcessConfiguration& input)
159     {
160         return { static_cast<float>(input.fxaaConfiguration.sharpness),
161             static_cast<float>(input.fxaaConfiguration.quality), 0.0f, 0.0f };
162     }
163 
GetFactorTaa(const PostProcessConfiguration & input)164     static inline BASE_NS::Math::Vec4 GetFactorTaa(const PostProcessConfiguration& input)
165     {
166         constexpr float alpha = 0.1f;
167         return { static_cast<float>(input.taaConfiguration.sharpness),
168             static_cast<float>(input.taaConfiguration.quality), 0.0f, alpha };
169     }
170 
GetFactorDof(const PostProcessConfiguration & input)171     static inline BASE_NS::Math::Vec4 GetFactorDof(const PostProcessConfiguration& input)
172     {
173         const float focusStart = (input.dofConfiguration.focusPoint - (input.dofConfiguration.focusRange / 2));
174         const float focusEnd = (input.dofConfiguration.focusPoint + (input.dofConfiguration.focusRange / 2));
175         const float nearTransitionStart = (focusStart - input.dofConfiguration.nearTransitionRange);
176         const float farTransitionEnd = (focusEnd + input.dofConfiguration.farTransitionRange);
177 
178         return { nearTransitionStart, focusStart, focusEnd, farTransitionEnd };
179     }
180 
GetFactorDof2(const PostProcessConfiguration & input)181     static inline BASE_NS::Math::Vec4 GetFactorDof2(const PostProcessConfiguration& input)
182     {
183         return { input.dofConfiguration.nearBlur, input.dofConfiguration.farBlur, input.dofConfiguration.nearPlane,
184             input.dofConfiguration.farPlane };
185     }
186 
GetFactorMotionBlur(const PostProcessConfiguration & input)187     static inline BASE_NS::Math::Vec4 GetFactorMotionBlur(const PostProcessConfiguration& input)
188     {
189         return {
190             static_cast<float>(input.motionBlurConfiguration.sharpness),
191             static_cast<float>(input.motionBlurConfiguration.quality),
192             input.motionBlurConfiguration.alpha,
193             input.motionBlurConfiguration.velocityCoefficient,
194         };
195     }
196 };
197 RENDER_END_NAMESPACE()
198 
199 #endif // RENDER_DATA_STORE_RENDER_DATA_STORE_POST_PROCESS_H
200