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 <atomic> 20 #include <cstdint> 21 #include <mutex> 22 23 #include <base/containers/refcnt_ptr.h> 24 #include <base/containers/string.h> 25 #include <base/containers/string_view.h> 26 #include <base/containers/unordered_map.h> 27 #include <base/math/vector.h> 28 #include <base/util/uid.h> 29 #include <render/datastore/intf_render_data_store_post_process.h> 30 #include <render/datastore/render_data_store_render_pods.h> 31 #include <render/namespace.h> 32 #include <render/shaders/common/render_post_process_structs_common.h> 33 34 RENDER_BEGIN_NAMESPACE() 35 class IRenderContext; 36 37 /** 38 * RenderDataStorePostProcess implementation. 39 */ 40 class RenderDataStorePostProcess final : public IRenderDataStorePostProcess { 41 public: 42 RenderDataStorePostProcess(const IRenderContext& renderContex, BASE_NS::string_view name); 43 ~RenderDataStorePostProcess() override = default; 44 45 // IRenderDataStore PreRender()46 void PreRender() override {} PostRender()47 void PostRender() override {} PreRenderBackend()48 void PreRenderBackend() override {} PostRenderBackend()49 void PostRenderBackend() override {} Clear()50 void Clear() override {} 51 GetFlags()52 uint32_t GetFlags() const override 53 { 54 return 0; 55 } 56 GetTypeName()57 BASE_NS::string_view GetTypeName() const override 58 { 59 return TYPE_NAME; 60 } 61 GetName()62 BASE_NS::string_view GetName() const override 63 { 64 return name_; 65 } 66 GetUid()67 const BASE_NS::Uid& GetUid() const override 68 { 69 return UID; 70 } 71 72 void Ref() override; 73 void Unref() override; 74 int32_t GetRefCount() override; 75 76 // IRenderDataStorePostProcess 77 void Create(BASE_NS::string_view name) override; 78 void Create(BASE_NS::string_view name, BASE_NS::string_view ppName, const RenderHandleReference& shader) override; 79 80 void Destroy(BASE_NS::string_view name) override; 81 void Destroy(BASE_NS::string_view name, BASE_NS::string_view ppName) override; 82 83 bool Contains(BASE_NS::string_view name) const override; 84 bool Contains(BASE_NS::string_view name, BASE_NS::string_view ppName) const override; 85 86 void Set(BASE_NS::string_view name, BASE_NS::string_view ppName, const PostProcess::Variables& vars) override; 87 void Set(BASE_NS::string_view name, BASE_NS::array_view<PostProcess::Variables> vars) override; 88 89 GlobalFactors GetGlobalFactors(BASE_NS::string_view name) const override; 90 BASE_NS::vector<PostProcess> Get(BASE_NS::string_view name) const override; 91 PostProcess Get(BASE_NS::string_view name, BASE_NS::string_view ppName) const override; 92 93 // for plugin / factory interface 94 static constexpr const char* const TYPE_NAME = "RenderDataStorePostProcess"; 95 96 static BASE_NS::refcnt_ptr<IRenderDataStore> Create(IRenderContext& renderContext, const char* name); 97 98 private: 99 const IRenderContext& renderContext_; 100 const BASE_NS::string name_; 101 102 struct PostProcessStack { 103 GlobalFactors globalFactors; 104 BASE_NS::vector<PostProcess> postProcesses; 105 }; 106 107 // must be locked when called 108 void CreateFromPod(BASE_NS::string_view name); 109 static void SetImpl(const PostProcess::Variables& vars, uint32_t ppIndex, PostProcessStack& ppStack); 110 static void SetGlobalFactorsImpl(const PostProcess::Variables& vars, uint32_t ppIndex, PostProcessStack& ppStack); 111 void GetShaderProperties(const RenderHandleReference& shader, PostProcess::Variables& vars); 112 void FillDefaultPostProcessData(const PostProcessConfiguration& ppConfig, PostProcessStack& ppStack); 113 114 BASE_NS::unordered_map<BASE_NS::string, PostProcessStack> allPostProcesses_; 115 116 mutable std::mutex mutex_; 117 118 std::atomic_int32_t refcnt_ { 0 }; 119 }; 120 121 /** 122 * PostProcessConversion helper. 123 */ 124 class PostProcessConversionHelper final { 125 public: GetFactorTonemap(const PostProcessConfiguration & input)126 static inline BASE_NS::Math::Vec4 GetFactorTonemap(const PostProcessConfiguration& input) 127 { 128 return { input.tonemapConfiguration.exposure, 0.0f, 0.0f, 129 static_cast<float>(input.tonemapConfiguration.tonemapType) }; 130 } 131 GetFactorVignette(const PostProcessConfiguration & input)132 static inline BASE_NS::Math::Vec4 GetFactorVignette(const PostProcessConfiguration& input) 133 { 134 return { input.vignetteConfiguration.coefficient, input.vignetteConfiguration.power, 0.0f, 0.0f }; 135 } 136 GetFactorDither(const PostProcessConfiguration & input)137 static inline BASE_NS::Math::Vec4 GetFactorDither(const PostProcessConfiguration& input) 138 { 139 return { input.ditherConfiguration.amountCoefficient, 0, 0, 140 static_cast<float>(input.ditherConfiguration.ditherType) }; 141 } 142 GetFactorColorConversion(const PostProcessConfiguration & input)143 static inline BASE_NS::Math::Vec4 GetFactorColorConversion(const PostProcessConfiguration& input) 144 { 145 return { 0.0f, 0.0f, 0.0f, static_cast<float>(input.colorConversionConfiguration.conversionFunctionType) }; 146 } 147 GetFactorFringe(const PostProcessConfiguration & input)148 static inline BASE_NS::Math::Vec4 GetFactorFringe(const PostProcessConfiguration& input) 149 { 150 return { input.colorFringeConfiguration.coefficient, input.colorFringeConfiguration.distanceCoefficient, 0.0f, 151 0.0f }; 152 } 153 GetFactorBlur(const PostProcessConfiguration & input)154 static inline BASE_NS::Math::Vec4 GetFactorBlur(const PostProcessConfiguration& input) 155 { 156 return { static_cast<float>(input.blurConfiguration.blurType), 157 static_cast<float>(input.blurConfiguration.blurQualityType), input.blurConfiguration.filterSize, 0 }; 158 } 159 GetFactorBloom(const PostProcessConfiguration & input)160 static inline BASE_NS::Math::Vec4 GetFactorBloom(const PostProcessConfiguration& input) 161 { 162 // the bloom global factor does not include scatter 163 return { input.bloomConfiguration.thresholdHard, input.bloomConfiguration.thresholdSoft, 164 input.bloomConfiguration.amountCoefficient, input.bloomConfiguration.dirtMaskCoefficient }; 165 } 166 GetFactorFxaa(const PostProcessConfiguration & input)167 static inline BASE_NS::Math::Vec4 GetFactorFxaa(const PostProcessConfiguration& input) 168 { 169 return { static_cast<float>(input.fxaaConfiguration.sharpness), 170 static_cast<float>(input.fxaaConfiguration.quality), 0.0f, 0.0f }; 171 } 172 GetFactorTaa(const PostProcessConfiguration & input)173 static inline BASE_NS::Math::Vec4 GetFactorTaa(const PostProcessConfiguration& input) 174 { 175 constexpr float alpha = 0.1f; 176 177 uint32_t varianceClippingBicubic = 0; 178 if (input.taaConfiguration.useBicubic) { 179 varianceClippingBicubic |= (1 << TAA_USE_BICUBIC_BIT); 180 } 181 if (input.taaConfiguration.useVarianceClipping) { 182 varianceClippingBicubic |= (1 << TAA_USE_VARIANCE_CLIPPING_BIT); 183 } 184 if (input.taaConfiguration.useyCoCG) { 185 varianceClippingBicubic |= (1 << TAA_USE_YCOCG_BIT); 186 } 187 if (input.taaConfiguration.ignoreBicubicEdges) { 188 varianceClippingBicubic |= (1 << TAA_IGNORE_EDGES_BIT); 189 } 190 191 const float* vcb = reinterpret_cast<float*>(&varianceClippingBicubic); 192 193 return { static_cast<float>(input.taaConfiguration.sharpness), 194 static_cast<float>(input.taaConfiguration.quality), *vcb, alpha }; 195 } 196 GetFactorDof(const PostProcessConfiguration & input)197 static inline BASE_NS::Math::Vec4 GetFactorDof(const PostProcessConfiguration& input) 198 { 199 const float focusStart = (input.dofConfiguration.focusPoint - (input.dofConfiguration.focusRange / 2)); 200 const float focusEnd = (input.dofConfiguration.focusPoint + (input.dofConfiguration.focusRange / 2)); 201 const float nearTransitionStart = (focusStart - input.dofConfiguration.nearTransitionRange); 202 const float farTransitionEnd = (focusEnd + input.dofConfiguration.farTransitionRange); 203 204 return { nearTransitionStart, focusStart, focusEnd, farTransitionEnd }; 205 } 206 GetFactorDof2(const PostProcessConfiguration & input)207 static inline BASE_NS::Math::Vec4 GetFactorDof2(const PostProcessConfiguration& input) 208 { 209 return { input.dofConfiguration.nearBlur, input.dofConfiguration.farBlur, input.dofConfiguration.nearPlane, 210 input.dofConfiguration.farPlane }; 211 } 212 GetFactorMotionBlur(const PostProcessConfiguration & input)213 static inline BASE_NS::Math::Vec4 GetFactorMotionBlur(const PostProcessConfiguration& input) 214 { 215 return { 216 static_cast<float>(input.motionBlurConfiguration.sharpness), 217 static_cast<float>(input.motionBlurConfiguration.quality), 218 input.motionBlurConfiguration.alpha, 219 input.motionBlurConfiguration.velocityCoefficient, 220 }; 221 } 222 }; 223 RENDER_END_NAMESPACE() 224 225 #endif // RENDER_DATA_STORE_RENDER_DATA_STORE_POST_PROCESS_H 226