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 "texture.h"
17
18 #include <scene/ext/intf_ecs_context.h>
19 #include <scene/ext/intf_render_resource.h>
20
21 #include <3d/ecs/components/render_handle_component.h>
22 #include <render/device/intf_gpu_resource_manager.h>
23
24 #include "../bitmap.h"
25 #include "../entity_converting_value.h"
26
27 SCENE_BEGIN_NAMESPACE()
28 namespace {
29 // clang-format off
30 constexpr const char* const SAMPLERNAMES[] = {
31 "Unknown",
32 "CORE_DEFAULT_SAMPLER_NEAREST_REPEAT",
33 "CORE_DEFAULT_SAMPLER_NEAREST_CLAMP",
34 "CORE_DEFAULT_SAMPLER_LINEAR_REPEAT",
35 "CORE_DEFAULT_SAMPLER_LINEAR_CLAMP",
36 "CORE_DEFAULT_SAMPLER_LINEAR_MIPMAP_REPEAT",
37 "CORE_DEFAULT_SAMPLER_LINEAR_MIPMAP_CLAMP"
38 };
39 static_assert(sizeof(SAMPLERNAMES) / sizeof(*SAMPLERNAMES) == size_t(TextureSampler::END_OF_LIST));
40 // clang-format on
41
NameToTextureSampler(BASE_NS::string_view name)42 static TextureSampler NameToTextureSampler(BASE_NS::string_view name)
43 {
44 size_t i = 0;
45 for (; i != size_t(TextureSampler::END_OF_LIST) && name != SAMPLERNAMES[i]; ++i) {
46 }
47
48 TextureSampler s = TextureSampler::UNKNOWN;
49 if (i < size_t(TextureSampler::END_OF_LIST)) {
50 s = static_cast<TextureSampler>(i);
51 }
52 return s;
53 }
54
TextureSamplerToName(TextureSampler s)55 static BASE_NS::string TextureSamplerToName(TextureSampler s)
56 {
57 size_t i = static_cast<size_t>(s);
58 if (i < size_t(TextureSampler::END_OF_LIST)) {
59 return SAMPLERNAMES[i];
60 }
61 return "Unknown";
62 }
63
64 struct SamplerConverter {
65 using SourceType = TextureSampler;
66 using TargetType = CORE_NS::EntityReference;
67
SamplerConverter__anon57cd9d460111::SamplerConverter68 SamplerConverter(IInternalScene::Ptr scene) : scene_(scene) {}
69
ConvertToSource__anon57cd9d460111::SamplerConverter70 SourceType ConvertToSource(META_NS::IAny&, const TargetType& v) const
71 {
72 TextureSampler s = TextureSampler::UNKNOWN;
73 if (auto scene = scene_.lock()) {
74 auto f = scene->AddTask([&] {
75 BASE_NS::string name;
76 if (auto rhman = static_cast<CORE3D_NS::IRenderHandleComponentManager*>(
77 scene->GetEcsContext().FindComponent<CORE3D_NS::RenderHandleComponent>())) {
78 name = scene->GetRenderContext().GetDevice().GetGpuResourceManager().GetName(
79 rhman->GetRenderHandleReference(v));
80 }
81 return name;
82 });
83 s = NameToTextureSampler(f.GetResult());
84 }
85 return s;
86 }
ConvertToTarget__anon57cd9d460111::SamplerConverter87 TargetType ConvertToTarget(const SourceType& v) const
88 {
89 CORE_NS::EntityReference ent;
90 if (auto scene = scene_.lock()) {
91 auto name = TextureSamplerToName(v);
92 scene
93 ->AddTask([&] {
94 if (auto rhman = static_cast<CORE3D_NS::IRenderHandleComponentManager*>(
95 scene->GetEcsContext().FindComponent<CORE3D_NS::RenderHandleComponent>())) {
96 auto handle =
97 scene->GetRenderContext().GetDevice().GetGpuResourceManager().GetSamplerHandle(name);
98 ent = CORE3D_NS::GetOrCreateEntityReference(
99 scene->GetEcsContext().GetNativeEcs()->GetEntityManager(), *rhman, handle);
100 }
101 })
102 .Wait();
103 }
104 return ent;
105 }
106
107 IInternalScene::WeakPtr scene_;
108 };
109 } // namespace
110
Build(const META_NS::IMetadata::Ptr & d)111 bool Texture::Build(const META_NS::IMetadata::Ptr& d)
112 {
113 if (!Super::Build(d)) {
114 return false;
115 }
116
117 auto ip = d->GetProperty<size_t>("Index");
118 if (!ip) {
119 return false;
120 }
121 index_ = META_NS::GetValue(ip);
122
123 return true;
124 }
125
InitDynamicProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)126 bool Texture::InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path)
127 {
128 BASE_NS::string cpath = "MaterialComponent.textures[" + BASE_NS::string(BASE_NS::to_string(index_)) + "]." + path;
129
130 if (p->GetName() == "Image") {
131 auto ep = object_->CreateEngineProperty(cpath).GetResult();
132 auto i = interface_cast<META_NS::IStackProperty>(p);
133 return ep && i &&
134 i->PushValue(META_NS::IValue::Ptr(
135 new RenderResourceValue<IBitmap>(ep, { object_->GetScene(), ClassId::Bitmap })));
136 }
137 if (p->GetName() == "Sampler") {
138 auto ep = object_->CreateEngineProperty(cpath).GetResult();
139 auto i = interface_cast<META_NS::IStackProperty>(p);
140 return ep && i &&
141 i->PushValue(META_NS::IValue::Ptr(new ConvertingValue<SamplerConverter>(ep, { object_->GetScene() })));
142 }
143 return AttachEngineProperty(p, cpath);
144 }
145
146 SCENE_END_NAMESPACE()
147