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 "bloom.h"
17
18 #include <3d/ecs/components/post_process_component.h>
19 #include <render/device/intf_gpu_resource_manager.h>
20
21 #include "entity_converting_value.h"
22
23 META_TYPE(RENDER_NS::RenderHandle)
24
25 SCENE_BEGIN_NAMESPACE()
26 namespace {
27
28 struct RenderHandleImageConverter {
29 using SourceType = IImage::Ptr;
30 using TargetType = RENDER_NS::RenderHandle;
31
RenderHandleImageConverter__anona536a0280111::RenderHandleImageConverter32 RenderHandleImageConverter(IInternalScene::Ptr scene) : scene_(scene) {}
33
ConvertToSource__anona536a0280111::RenderHandleImageConverter34 SourceType ConvertToSource(META_NS::IAny& any, const TargetType& v) const
35 {
36 auto p = META_NS::GetPointer<IImage>(any);
37 if (auto scene = scene_.lock()) {
38 if (RENDER_NS::RenderHandleUtil::IsValid(v)) {
39 scene
40 ->AddTask([&] {
41 if (!p) {
42 p = interface_pointer_cast<IImage>(scene->CreateObject(ClassId::Image));
43 }
44 if (auto i = interface_cast<IRenderResource>(p)) {
45 i->SetRenderHandle(scene->GetRenderContext().GetDevice().GetGpuResourceManager().Get(v));
46 }
47 })
48 .Wait();
49 } else {
50 p = nullptr;
51 }
52 }
53 return p;
54 }
55
ConvertToTarget__anona536a0280111::RenderHandleImageConverter56 TargetType ConvertToTarget(const SourceType& v) const
57 {
58 TargetType handle;
59 if (auto scene = scene_.lock()) {
60 scene
61 ->AddTask([&] {
62 if (auto i = interface_cast<IRenderResource>(v)) {
63 handle = i->GetRenderHandle().GetHandle();
64 }
65 })
66 .Wait();
67 }
68 return handle;
69 }
70
71 IInternalScene::WeakPtr scene_;
72 };
73 } // namespace
74
InitDynamicProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)75 bool Bloom::InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path)
76 {
77 BASE_NS::string cpath = "PostProcessComponent.bloomConfiguration." + path;
78 if (p->GetName() == "DirtMaskImage") {
79 auto ep = object_->CreateProperty(cpath).GetResult();
80 auto i = interface_cast<META_NS::IStackProperty>(p);
81 return ep && i &&
82 i->PushValue(
83 META_NS::IValue::Ptr(new ConvertingValue<RenderHandleImageConverter>(ep, { object_->GetScene() })));
84 }
85 if (p->GetName() == "Enabled") {
86 auto i = interface_cast<META_NS::IStackProperty>(p);
87 return flags_ && i &&
88 i->PushValue(META_NS::IValue::Ptr(
89 new ConvertingValue<PPEffectEnabledConverter<CORE3D_NS::PostProcessComponent::BLOOM_BIT>>(
90 flags_, { flags_ })));
91 }
92 return AttachEngineProperty(p, cpath);
93 }
94
95 SCENE_END_NAMESPACE()