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 <3d/ecs/components/transform_component.h>
17 #include <3d/ecs/systems/intf_render_preprocessor_system.h>
18 #include <3d/ecs/systems/intf_render_system.h>
19 #include <3d/implementation_uids.h>
20 #include <core/ecs/intf_component_manager.h>
21 #include <core/ecs/intf_ecs.h>
22 #include <core/ecs/intf_system.h>
23 #include <core/intf_engine.h>
24 #include <core/io/intf_file_manager.h>
25 #include <core/plugin/intf_plugin.h>
26 #include <core/plugin/intf_plugin_decl.h>
27 #include <render/datastore/intf_render_data_store_manager.h>
28 #include <render/datastore/intf_render_data_store_pod.h>
29 #include <render/device/intf_shader_manager.h>
30 #include <render/implementation_uids.h>
31 #include <render/intf_plugin.h>
32 #include <render/intf_render_context.h>
33
34 #include "dotfield/ecs/components/dotfield_component.h"
35 #include "dotfield/ecs/systems/dotfield_system.h"
36 #include "dotfield/implementation_uids.h"
37 #include "dotfield/render/intf_render_data_store_default_dotfield.h"
38 #include "render/render_data_store_default_dotfield.h"
39 #include "render/render_node_dotfield_simulation.h"
40
41 // Rofs Data.
42 extern "C" const void* const DOTFIELD_BIN[];
43 extern "C" const uint64_t DOTFIELD_BIN_SIZE;
44
CORE_BEGIN_NAMESPACE()45 CORE_BEGIN_NAMESPACE()
46 #if defined(CORE_PLUGIN) && (CORE_PLUGIN == 1)
47 IPluginRegister* gPluginRegistry { nullptr };
GetPluginRegister()48 IPluginRegister& GetPluginRegister()
49 {
50 return *gPluginRegistry;
51 }
52 #endif
53 CORE_END_NAMESPACE()
54
55 namespace {
56 static constexpr RENDER_NS::IShaderManager::ShaderFilePathDesc SHADER_FILE_PATHS {
57 "dotfieldshaders://",
58 "dotfieldshaderstates://",
59 "dotfieldpipelinelayouts://",
60 "dotfieldvertexinputdeclarations://",
61 };
62 }
63
64 namespace Dotfield {
65 using CORE_NS::GetName;
66 using namespace BASE_NS;
67 using namespace CORE_NS;
68 using namespace RENDER_NS;
69 using namespace CORE3D_NS;
70
71 #ifdef __OHOS_PLATFORM__
GetVersionInfo()72 const char* GetVersionInfo()
73 {
74 return "GIT_TAG: GIT_REVISION: xxx GIT_BRANCH: dev";
75 }
76 #else
77 const char* GetVersionInfo();
78 #endif
79
80 constexpr string_view ROFS = "dotfieldrofs";
81 constexpr string_view SHADERS = "dotfieldshaders";
82 constexpr string_view VIDS = "dotfieldvertexinputdeclarations";
83 constexpr string_view PIDS = "dotfieldpipelinelayouts";
84 constexpr string_view SSTATES = "dotfieldshaderstates";
85
86 constexpr string_view SHADER_PATH = "dotfieldrofs://shaders/";
87 constexpr string_view VID_PATH = "dotfieldrofs://vertexinputdeclarations/";
88 constexpr string_view PID_PATH = "dotfieldrofs://pipelinelayouts/";
89 constexpr string_view SHADER_STATE_PATH = "dotfieldrofs://shaderstates/";
90
91 constexpr BASE_NS::Uid PLUGIN_DEPENDENCIES[] = { RENDER_NS::UID_RENDER_PLUGIN, CORE3D_NS::UID_3D_PLUGIN };
92
93 IComponentManager* IDotfieldComponentManagerInstance(IEcs&);
94 ISystem* IDotfieldSystemInstance(IEcs&);
95
96 void IDotfieldComponentManagerDestroy(IComponentManager*);
97 void IDotfieldSystemDestroy(ISystem*);
98
99 constexpr RenderDataStoreTypeInfo RenderDataDefaultDotfieldTypeInfo = {
100 { RenderDataStoreTypeInfo::UID },
101 RenderDataStoreDefaultDotfield::UID,
102 RenderDataStoreDefaultDotfield::TYPE_NAME,
103 RenderDataStoreDefaultDotfield::Create,
104 };
105
106 constexpr RenderDataStoreTypeInfo gRenderDataStores[] = {
107 RenderDataDefaultDotfieldTypeInfo,
108 };
109
110 constexpr RenderNodeTypeInfo RenderNodeDotfieldSimulationTypeInfo = {
111 { RenderNodeTypeInfo::UID },
112 RenderNodeDotfieldSimulation::UID,
113 RenderNodeDotfieldSimulation::TYPE_NAME,
114 RenderNodeDotfieldSimulation::Create,
115 RenderNodeDotfieldSimulation::Destroy,
116 RenderNodeDotfieldSimulation::BACKEND_FLAGS,
117 RenderNodeDotfieldSimulation::CLASS_TYPE,
118 // after
119 BASE_NS::Uid { "a25b27ea-a4ff-4b64-87c7-a7865cccfd92" },
120 // before
121 {},
122 };
123
124 constexpr RenderNodeTypeInfo gRenderNodes[] = {
125 RenderNodeDotfieldSimulationTypeInfo,
126 };
127
128 constexpr ComponentManagerTypeInfo DotfieldComponentTypeInfo = {
129 { ComponentManagerTypeInfo::UID },
130 IDotfieldComponentManager::UID,
131 GetName<IDotfieldComponentManager>().data(),
132 IDotfieldComponentManagerInstance,
133 IDotfieldComponentManagerDestroy,
134 };
135
136 constexpr ComponentManagerTypeInfo gComponentManagers[] = { DotfieldComponentTypeInfo };
137
138 constexpr Uid DotfieldSourceSystemRDeps[] = {
139 ITransformComponentManager::UID,
140 IDotfieldComponentManager::UID,
141 };
142
143 constexpr SystemTypeInfo gSystems[] = {
144 {
145 { SystemTypeInfo::UID },
146 IDotfieldSystem::UID,
147 GetName<IDotfieldSystem>().data(),
148 IDotfieldSystemInstance,
149 IDotfieldSystemDestroy,
150 {},
151 DotfieldSourceSystemRDeps,
152 {},
153 CORE3D_NS::IRenderPreprocessorSystem::UID,
154 },
155 };
156
Initialize(IRenderContext & context)157 PluginToken Initialize(IRenderContext& context)
158 {
159 auto& fm = context.GetEngine().GetFileManager();
160 // Create dotfieldrofs:// filesystem that points to embedded asset files.
161
162 auto rofs = fm.CreateROFilesystem(DOTFIELD_BIN, DOTFIELD_BIN_SIZE);
163
164 fm.RegisterFilesystem(ROFS, move(rofs));
165 // And register it under the needed proxy protocols.
166 fm.RegisterPath(SHADERS, SHADER_PATH, false);
167 fm.RegisterPath(VIDS, VID_PATH, false);
168 fm.RegisterPath(PIDS, PID_PATH, false);
169 fm.RegisterPath(SSTATES, SHADER_STATE_PATH, false);
170
171 auto& renderDataStoreManager = context.GetRenderDataStoreManager();
172 {
173 auto rsp = refcnt_ptr<IRenderDataStorePod>(
174 renderDataStoreManager.Create(IRenderDataStorePod::UID, "DotfieldPodStore"));
175 if (rsp) {
176 struct {
177 Math::Vec4 texSizeInvTexSize { 2.f, 2.f, 1.f / 2.f, 1.f / 2.f };
178 } config;
179 rsp->CreatePod("TexSizeConfig", "TexSizeShaderConfig", arrayviewU8(config));
180 }
181 }
182
183 context.GetDevice().GetShaderManager().LoadShaderFiles(SHADER_FILE_PATHS);
184 return &context;
185 }
186
Uninitialize(PluginToken token)187 void Uninitialize(PluginToken token)
188 {
189 auto context = static_cast<IRenderContext*>(token);
190 context->GetDevice().GetShaderManager().UnloadShaderFiles(SHADER_FILE_PATHS);
191 auto& fm = context->GetEngine().GetFileManager();
192 fm.UnregisterPath(SHADERS, SHADER_PATH);
193 fm.UnregisterPath(VIDS, VID_PATH);
194 fm.UnregisterPath(PIDS, PID_PATH);
195 fm.UnregisterFilesystem(ROFS);
196 }
197
InitializeEcs(IEcs &)198 PluginToken InitializeEcs(IEcs&)
199 {
200 return {};
201 }
202
UninitializeEcs(PluginToken)203 void UninitializeEcs(PluginToken) {}
204
205 static constexpr IRenderPlugin RENDER_PLUGIN(Initialize, Uninitialize);
206
207 static constexpr IEcsPlugin ECS_PLUGIN(InitializeEcs, UninitializeEcs);
208
RegisterInterfaces(IPluginRegister & pluginRegistry)209 PluginToken RegisterInterfaces(IPluginRegister& pluginRegistry)
210 {
211 #if defined(CORE_PLUGIN) && (CORE_PLUGIN == 1)
212 gPluginRegistry = &pluginRegistry;
213 #endif
214 for (const auto& info : gRenderDataStores) {
215 pluginRegistry.RegisterTypeInfo(info);
216 }
217 for (const auto& info : gRenderNodes) {
218 pluginRegistry.RegisterTypeInfo(info);
219 }
220 for (const auto& info : gComponentManagers) {
221 pluginRegistry.RegisterTypeInfo(info);
222 }
223 for (const auto& info : gSystems) {
224 pluginRegistry.RegisterTypeInfo(info);
225 }
226 pluginRegistry.RegisterTypeInfo(RENDER_PLUGIN);
227 pluginRegistry.RegisterTypeInfo(ECS_PLUGIN);
228 return &pluginRegistry;
229 }
230
UnregisterInterfaces(PluginToken token)231 void UnregisterInterfaces(PluginToken token)
232 {
233 auto& pluginRegistry = *static_cast<IPluginRegister*>(token);
234 for (const auto& info : gSystems) {
235 pluginRegistry.UnregisterTypeInfo(info);
236 }
237 for (const auto& info : gComponentManagers) {
238 pluginRegistry.UnregisterTypeInfo(info);
239 }
240 for (const auto& info : gRenderNodes) {
241 pluginRegistry.UnregisterTypeInfo(info);
242 }
243 for (const auto& info : gRenderDataStores) {
244 pluginRegistry.UnregisterTypeInfo(info);
245 }
246 pluginRegistry.UnregisterTypeInfo(ECS_PLUGIN);
247 pluginRegistry.UnregisterTypeInfo(RENDER_PLUGIN);
248 }
249
250 } // namespace Dotfield
251
252 extern "C" {
PLUGIN_DATA(DotfieldPlugin)253 PLUGIN_DATA(DotfieldPlugin) {
254 { CORE_NS::IPlugin::UID },
255 "DotfieldPlugin",
256 { Dotfield::UID_DOTFIELD_PLUGIN, Dotfield::GetVersionInfo },
257 Dotfield::RegisterInterfaces,
258 Dotfield::UnregisterInterfaces,
259 { Dotfield::PLUGIN_DEPENDENCIES },
260 };
261 }