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 "shader_type.h"
17
18 #include <scene/ext/intf_render_resource.h>
19 #include <scene/ext/util.h>
20 #include <scene/interface/resource/intf_render_resource_manager.h>
21
22 #include <core/image/intf_image_loader_manager.h>
23 #include <core/intf_engine.h>
24 #include <render/device/intf_gpu_resource_manager.h>
25
26 #include <meta/api/metadata_util.h>
27
28 #include "serialization/util.h"
29
SCENE_BEGIN_NAMESPACE()30 SCENE_BEGIN_NAMESPACE()
31
32 bool ShaderResourceType::Build(const META_NS::IMetadata::Ptr& d)
33 {
34 bool res = Super::Build(d);
35 if (res) {
36 auto context = GetInterfaceBuildArg<IRenderContext>(d, "RenderContext");
37 if (!context) {
38 CORE_LOG_E("Invalid arguments to construct ShaderResourceType");
39 return false;
40 }
41 context_ = context;
42 }
43 return res;
44 }
GetResourceName() const45 BASE_NS::string ShaderResourceType::GetResourceName() const
46 {
47 return "ShaderResource";
48 }
GetResourceType() const49 BASE_NS::Uid ShaderResourceType::GetResourceType() const
50 {
51 return ClassId::ShaderResource.Id().ToUid();
52 }
53
LoadResource(const StorageInfo & s) const54 CORE_NS::IResource::Ptr ShaderResourceType::LoadResource(const StorageInfo& s) const
55 {
56 CORE_NS::IResource::Ptr res;
57 if (auto rman = META_NS::GetObjectRegistry().Create<IRenderResourceManager>(
58 ClassId::RenderResourceManager, CreateRenderContextArg(context_.lock()))) {
59 res = interface_pointer_cast<CORE_NS::IResource>(rman->LoadShader(s.path).GetResult());
60 }
61 if (res && s.options) {
62 if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
63 META_NS::ClassId::ObjectResourceOptions)) {
64 opts->Load(*s.options, nullptr, nullptr);
65 auto in = interface_cast<META_NS::IMetadata>(opts);
66 auto out = interface_cast<META_NS::IMetadata>(res);
67 if (in && out) {
68 SerCopy(*in, *out);
69 }
70 }
71 }
72 return res;
73 }
SaveResource(const CORE_NS::IResource::ConstPtr & p,const StorageInfo & s) const74 bool ShaderResourceType::SaveResource(const CORE_NS::IResource::ConstPtr& p, const StorageInfo& s) const
75 {
76 if (s.options) {
77 if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
78 META_NS::ClassId::ObjectResourceOptions)) {
79 auto in = interface_cast<META_NS::IMetadata>(p);
80 auto out = interface_cast<META_NS::IMetadata>(opts);
81 if (auto i = interface_cast<META_NS::IDerivedFromResource>(p)) {
82 opts->SetBaseResource(i->GetResource());
83 }
84 if (in && out) {
85 SerCloneAllToDefaultIfSet(*in, *out);
86 opts->Save(*s.options, nullptr);
87 }
88 }
89 }
90 return true;
91 }
ReloadResource(const StorageInfo & s,const CORE_NS::IResource::Ptr & p) const92 bool ShaderResourceType::ReloadResource(const StorageInfo& s, const CORE_NS::IResource::Ptr& p) const
93 {
94 if (auto context = context_.lock()) {
95 if (auto dyn = interface_cast<META_NS::IDynamicResource>(p)) {
96 context
97 ->AddTask([&] {
98 auto& man = context->GetRenderer()->GetDevice().GetShaderManager();
99 man.ReloadShaderFile(s.path);
100 })
101 .Wait();
102 META_NS::Invoke<META_NS::IOnChanged>(dyn->OnResourceChanged());
103 return true;
104 }
105 }
106 return false;
107 }
108
109 SCENE_END_NAMESPACE()