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 #include "postprocess.h"
17
18 #include <scene/ext/intf_render_resource.h>
19
20 #include <render/device/intf_gpu_resource_manager.h>
21
22 #include <meta/api/make_callback.h>
23
24 #include "../converting_value.h"
25
26 SCENE_BEGIN_NAMESPACE()
27 namespace {
28 struct TonemapConverter {
TonemapConverter__anon50ad2d320111::TonemapConverter29 TonemapConverter(META_NS::Property<uint32_t> flags) : flags_(flags) {}
30
31 using SourceType = ITonemap::Ptr;
32 using TargetType = RENDER_NS::TonemapConfiguration;
33
ConvertToSource__anon50ad2d320111::TonemapConverter34 SourceType ConvertToSource(META_NS::IAny& any, const TargetType& v) const
35 {
36 auto p = META_NS::GetPointer<ITonemap>(any);
37 if (!p) {
38 p = META_NS::GetObjectRegistry().Create<ITonemap>(ClassId::Tonemap);
39 }
40 if (p) {
41 p->Enabled()->SetValue(flags_->GetValue() & CORE3D_NS::PostProcessComponent::TONEMAP_BIT);
42 p->Type()->SetValue(static_cast<TonemapType>(v.tonemapType));
43 p->Exposure()->SetValue(v.exposure);
44 }
45 return p;
46 }
ConvertToTarget__anon50ad2d320111::TonemapConverter47 TargetType ConvertToTarget(const SourceType& v) const
48 {
49 TargetType res {};
50 if (v && v->Enabled()->GetValue()) {
51 flags_->SetValue(flags_->GetValue() | CORE3D_NS::PostProcessComponent::TONEMAP_BIT);
52 } else {
53 flags_->SetValue(flags_->GetValue() & ~CORE3D_NS::PostProcessComponent::TONEMAP_BIT);
54 }
55 if (v) {
56 res.tonemapType = static_cast<RENDER_NS::TonemapConfiguration::TonemapType>(v->Type()->GetValue());
57 res.exposure = v->Exposure()->GetValue();
58 }
59 return res;
60 }
61
62 private:
63 META_NS::Property<uint32_t> flags_;
64 };
65
66 struct BloomConverter {
BloomConverter__anon50ad2d320111::BloomConverter67 BloomConverter(const IInternalScene::Ptr& scene, META_NS::Property<uint32_t> flags) : scene_(scene), flags_(flags)
68 {}
69
70 using SourceType = IBloom::Ptr;
71 using TargetType = RENDER_NS::BloomConfiguration;
ConvertToSource__anon50ad2d320111::BloomConverter72 SourceType ConvertToSource(META_NS::IAny& any, const TargetType& v) const
73 {
74 auto p = META_NS::GetPointer<IBloom>(any);
75 if (!p) {
76 p = META_NS::GetObjectRegistry().Create<IBloom>(ClassId::Bloom);
77 }
78 if (p) {
79 p->Enabled()->SetValue(flags_->GetValue() & CORE3D_NS::PostProcessComponent::BLOOM_BIT);
80 p->Type()->SetValue(static_cast<BloomType>(v.bloomType));
81 p->Quality()->SetValue(static_cast<EffectQualityType>(v.bloomQualityType));
82 p->ThresholdHard()->SetValue(v.thresholdHard);
83 p->ThresholdSoft()->SetValue(v.thresholdSoft);
84 p->AmountCoefficient()->SetValue(v.amountCoefficient);
85 p->DirtMaskCoefficient()->SetValue(v.dirtMaskCoefficient);
86 p->Scatter()->SetValue(v.scatter);
87 p->ScaleFactor()->SetValue(v.scaleFactor);
88
89 if (RENDER_NS::RenderHandleUtil::IsValid(v.dirtMaskImage)) {
90 if (auto scene = scene_.lock()) {
91 scene
92 ->AddTask([&] {
93 auto& resources = scene->GetRenderContext().GetDevice().GetGpuResourceManager();
94 if (auto handle = resources.Get(v.dirtMaskImage)) {
95 if (auto bitm = META_NS::GetObjectRegistry().Create<IBitmap>(ClassId::Bitmap)) {
96 if (auto i = interface_cast<IRenderResource>(bitm)) {
97 i->SetRenderHandle(scene, handle);
98 }
99 p->DirtMaskImage()->SetValue(bitm);
100 }
101 }
102 })
103 .Wait();
104 }
105 }
106 p->UseCompute()->SetValue(v.useCompute);
107 }
108 return p;
109 }
ConvertToTarget__anon50ad2d320111::BloomConverter110 TargetType ConvertToTarget(const SourceType& p) const
111 {
112 TargetType v {};
113 if (p && p->Enabled()->GetValue()) {
114 flags_->SetValue(flags_->GetValue() | CORE3D_NS::PostProcessComponent::BLOOM_BIT);
115 } else {
116 flags_->SetValue(flags_->GetValue() & ~CORE3D_NS::PostProcessComponent::BLOOM_BIT);
117 }
118 if (p) {
119 v.bloomType = static_cast<RENDER_NS::BloomConfiguration::BloomType>(p->Type()->GetValue());
120 v.bloomQualityType = static_cast<RENDER_NS::BloomConfiguration::BloomQualityType>(p->Quality()->GetValue());
121 v.thresholdHard = p->ThresholdHard()->GetValue();
122 v.thresholdSoft = p->ThresholdSoft()->GetValue();
123 v.amountCoefficient = p->AmountCoefficient()->GetValue();
124 v.dirtMaskCoefficient = p->DirtMaskCoefficient()->GetValue();
125 if (auto image = interface_cast<IRenderResource>(p->DirtMaskImage()->GetValue())) {
126 v.dirtMaskImage = image->GetRenderHandle().GetHandle();
127 }
128 v.useCompute = p->UseCompute()->GetValue();
129 v.scatter = p->Scatter()->GetValue();
130 v.scaleFactor = p->ScaleFactor()->GetValue();
131 }
132 return v;
133 }
134
135 private:
136 IInternalScene::WeakPtr scene_;
137 META_NS::Property<uint32_t> flags_;
138 };
139 } // namespace
140
CreateEntity(const IInternalScene::Ptr & scene)141 CORE_NS::Entity PostProcess::CreateEntity(const IInternalScene::Ptr& scene)
142 {
143 auto& ecs = scene->GetEcsContext();
144 auto pp = ecs.FindComponent<CORE3D_NS::PostProcessComponent>();
145 if (!pp) {
146 return CORE_NS::Entity {};
147 }
148 auto ent = ecs.GetNativeEcs()->GetEntityManager().Create();
149 pp->Create(ent);
150 return ent;
151 }
SetEcsObject(const IEcsObject::Ptr & obj)152 bool PostProcess::SetEcsObject(const IEcsObject::Ptr& obj)
153 {
154 auto p = META_NS::GetObjectRegistry().Create<IInternalPostProcess>(ClassId::PostProcessComponent);
155 if (auto acc = interface_cast<IEcsObjectAccess>(p)) {
156 if (acc->SetEcsObject(obj)) {
157 pp_ = p;
158 Init(obj);
159 }
160 }
161 return pp_ != nullptr;
162 }
GetEcsObject() const163 IEcsObject::Ptr PostProcess::GetEcsObject() const
164 {
165 auto acc = interface_cast<IEcsObjectAccess>(pp_);
166 return acc ? acc->GetEcsObject() : nullptr;
167 }
Init(const IEcsObject::Ptr & eobj)168 void PostProcess::Init(const IEcsObject::Ptr& eobj)
169 {
170 Tonemap()->PushValue(
171 META_NS::IValue::Ptr(new ConvertingValue<TonemapConverter>(pp_->Tonemap(), { pp_->EnableFlags() })));
172 Bloom()->PushValue(META_NS::IValue::Ptr(
173 new ConvertingValue<BloomConverter>(pp_->Bloom(), { eobj->GetScene(), pp_->EnableFlags() })));
174 }
175
176 SCENE_END_NAMESPACE()
177