• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "texture.h"
16 
17 #include <scene/ext/intf_ecs_context.h>
18 #include <scene/ext/intf_render_resource.h>
19 #include <scene/interface/intf_scene.h>
20 
21 #include <3d/ecs/components/render_handle_component.h>
22 #include <render/device/intf_gpu_resource_manager.h>
23 
24 #include "entity_converting_value.h"
25 #include "resource/image.h"
26 #include "sampler.h"
27 
28 SCENE_BEGIN_NAMESPACE()
29 
30 namespace {
31 
32 struct SamplerConverter {
SamplerConverter__anon423caa680111::SamplerConverter33     SamplerConverter(const IInternalScene::Ptr& scene) : scene_(scene) {}
34 
35     using SourceType = ISampler::Ptr;
36     using TargetType = CORE_NS::EntityReference;
37 
ConvertToSource__anon423caa680111::SamplerConverter38     SourceType ConvertToSource(META_NS::IAny& any, const TargetType& v) const
39     {
40         auto p = META_NS::GetPointer<ISampler>(any);
41         if (auto scene = scene_.lock()) {
42             scene
43                 ->AddTask([&] {
44                     if (!p) {
45                         p = META_NS::GetObjectRegistry().Create<ISampler>(ClassId::Sampler);
46                         if (auto i = interface_cast<ISamplerInternal>(p)) {
47                             i->SetScene(scene);
48                         }
49                     }
50                     auto& ctx = scene->GetRenderContext();
51                     if (auto rhman = static_cast<CORE3D_NS::IRenderHandleComponentManager*>(
52                             scene->GetEcsContext().FindComponent<CORE3D_NS::RenderHandleComponent>())) {
53                         if (auto internal = interface_cast<ISamplerInternal>(p)) {
54                             internal->UpdateSamplerFromHandle(ctx, rhman->GetRenderHandleReference(v));
55                         }
56                     }
57                 })
58                 .Wait();
59         }
60         return p;
61     }
ConvertToTarget__anon423caa680111::SamplerConverter62     TargetType ConvertToTarget(const SourceType& v) const
63     {
64         TargetType res;
65         if (auto scene = scene_.lock(); scene && v) {
66             scene
67                 ->AddTask([&] {
68                     if (auto i = interface_cast<ISamplerInternal>(v)) {
69                         if (auto handle = i->GetHandleFromSampler(scene->GetRenderContext())) {
70                             res = scene->GetEcsContext().GetRenderHandleEntity(handle);
71                         }
72                     }
73                 })
74                 .Wait();
75         }
76         return res;
77     }
78 
79 private:
80     IInternalScene::WeakPtr scene_;
81 };
82 
83 } // namespace
84 
InitDynamicProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)85 bool Texture::InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path)
86 {
87     if (index_ == -1) {
88         CORE_LOG_E("Texture index is uninitialised");
89         return false;
90     }
91     BASE_NS::string cpath = "MaterialComponent.textures[" + BASE_NS::string(BASE_NS::to_string(index_)) + "]." + path;
92 
93     if (p->GetName() == "Image") {
94         auto ep = object_->CreateProperty(cpath).GetResult();
95         auto i = interface_cast<META_NS::IStackProperty>(p);
96         return ep && i &&
97                i->PushValue(
98                    META_NS::IValue::Ptr(new RenderResourceValue<IImage>(ep, { object_->GetScene(), ClassId::Image })));
99     }
100     if (p->GetName() == "Sampler") {
101         auto ep = object_->CreateProperty(cpath).GetResult();
102         auto i = interface_cast<META_NS::IStackProperty>(p);
103         return ep && i &&
104                i->PushValue(META_NS::IValue::Ptr(new ConvertingValue<SamplerConverter>(ep, { object_->GetScene() })));
105     }
106     return AttachEngineProperty(p, cpath);
107 }
108 
109 SCENE_END_NAMESPACE()
110