1 /*
2 * Copyright (c) 2022 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 <ComponentTools/component_query.h>
17
18 #include <3d/ecs/systems/intf_weather_system.h>
19 #include <3d/implementation_uids.h>
20 #include <3d/intf_graphics_context.h>
21 #include <base/containers/unordered_map.h>
22 #include <base/containers/vector.h>
23 #include <core/ecs/intf_ecs.h>
24 #include <render/datastore/intf_render_data_store_manager.h>
25 #include <render/device/gpu_resource_desc.h>
26
27 #include "render/datastore/render_data_store_weather.h"
28
29 RENDER_BEGIN_NAMESPACE()
30 class IRenderContext;
31 class IGpuResourceManager;
32 RENDER_END_NAMESPACE()
33
34 CORE3D_BEGIN_NAMESPACE()
35 class IRenderHandleComponentManager;
36 class INodeComponentManager;
37 class IMaterialComponentManager;
38 class IMeshComponentManager;
39 class IRenderMeshComponentManager;
40 class INameComponentManager;
41 class ITransformComponentManager;
42 class IRenderConfigurationComponentManager;
43 class IWaterRippleComponentManager;
44 class ICameraComponentManager;
45 class IEnvironmentComponentManager;
46 class IWeatherComponentManager;
47 CORE3D_END_NAMESPACE()
48
CORE3D_BEGIN_NAMESPACE()49 CORE3D_BEGIN_NAMESPACE()
50 class WeatherSystem final : public IWeatherSystem, private CORE_NS::IEcs::ComponentListener {
51 public:
52 /** Ripple plane object */
53 struct RipplePlaneResources {
54 CORE_NS::Entity entityID;
55 // gpu resources
56 CORE_NS::EntityReference rippleTextures[2];
57 CORE_NS::EntityReference rippleVelocityTextures[2];
58 CORE_NS::EntityReference rippleArgsBufferHandle;
59 };
60
61 WeatherSystem(CORE_NS::IEcs& ecs);
62 ~WeatherSystem();
63
64 // ISystem
65 BASE_NS::string_view GetName() const override;
66 BASE_NS::Uid GetUid() const override;
67
68 CORE_NS::IPropertyHandle* GetProperties() override;
69 const CORE_NS::IPropertyHandle* GetProperties() const override;
70 void SetProperties(const CORE_NS::IPropertyHandle&) override;
71
72 bool IsActive() const override;
73 void SetActive(bool state) override;
74
75 void Initialize() override;
76 void Uninitialize() override;
77 bool Update(bool frameRenderingQueued, uint64_t time, uint64_t delta) override;
78
79 const CORE_NS::IEcs& GetECS() const override;
80
81 // Listener
82 void OnComponentEvent(EventType type, const CORE_NS::IComponentManager& componentManager,
83 BASE_NS::array_view<const CORE_NS::Entity> entities) override;
84
85 private:
86 void ProcessWaterRipples();
87 void UpdateCloudMaterial(CORE_NS::Entity camEntity);
88
89 bool active_ { true };
90 CORE_NS::IEcs& ecs_;
91 RENDER_NS::IRenderContext* renderContext_;
92 CORE3D_NS::IGraphicsContext* graphicsContext_;
93 RENDER_NS::IGpuResourceManager* gpuResourceManager_;
94 RENDER_NS::IRenderDataStoreManager* renderDataStoreManager_;
95
96 CORE_NS::ComponentQuery query_;
97
98 CORE3D_NS::IMaterialComponentManager& materialManager_;
99 CORE3D_NS::IMeshComponentManager& meshManager_;
100 CORE3D_NS::IRenderMeshComponentManager& renderMeshManager_;
101 CORE3D_NS::IRenderHandleComponentManager& renderHandleManager_;
102 CORE3D_NS::ITransformComponentManager& transformationManager_;
103 CORE3D_NS::IRenderConfigurationComponentManager& renderConfigManager_;
104 CORE3D_NS::ICameraComponentManager& camManager_;
105 CORE3D_NS::IEnvironmentComponentManager& envManager_;
106 CORE3D_NS::IWeatherComponentManager& weatherManager_;
107
108 IWaterRippleComponentManager& rippleComponentManager_;
109 uint32_t weatherGeneration_ {};
110 BASE_NS::Math::Vec3 prevPlanePos_;
111 BASE_NS::vector<BASE_NS::pair<CORE_NS::Entity, BASE_NS::Math::UVec2>> newRippleToAdd_;
112 BASE_NS::unordered_map<CORE_NS::Entity, RipplePlaneResources> handleResources_;
113
114 BASE_NS::refcnt_ptr<RenderDataStoreWeather> dataStoreWeather_;
115
116 struct ObjectAndMaterial {
117 CORE_NS::Entity entity;
118 CORE_NS::Entity matEntity;
119 };
120
121 ObjectAndMaterial waterEffectsPlane;
122 CORE_NS::Entity GetMaterialEntity(const CORE_NS::Entity& entity);
123 RipplePlaneResources GetOrCreateSimulationResources(const CORE_NS::Entity& entity);
124
125 struct CloudData {
126 CORE_NS::Entity renderMeshEntity;
127 CORE_NS::EntityReference matEntity;
128 CORE_NS::EntityReference meshEntity;
129
130 // will be stored to material
131 CORE_NS::EntityReference cloudTexture;
132 CORE_NS::EntityReference cloudPrevTexture;
133
134 RENDER_NS::RenderHandleReference cloudTexHandle;
135 RENDER_NS::RenderHandleReference cloudPrevTexHandle;
136
137 RENDER_NS::GpuImageDesc gpuImageDesc {
138 RENDER_NS::ImageType::CORE_IMAGE_TYPE_2D,
139 RENDER_NS::ImageViewType::CORE_IMAGE_VIEW_TYPE_2D,
140 BASE_NS::Format::BASE_FORMAT_R16G16B16A16_SFLOAT,
141 RENDER_NS::ImageTiling::CORE_IMAGE_TILING_OPTIMAL,
142 RENDER_NS::ImageUsageFlagBits::CORE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
143 RENDER_NS::ImageUsageFlagBits::CORE_IMAGE_USAGE_SAMPLED_BIT,
144 RENDER_NS::MemoryPropertyFlagBits::CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
145 0U,
146 RENDER_NS::EngineImageCreationFlagBits::CORE_ENGINE_IMAGE_CREATION_DYNAMIC_BARRIERS,
147 1U,
148 1U,
149 1U,
150 1U,
151 1U,
152 RENDER_NS::SampleCountFlagBits::CORE_SAMPLE_COUNT_1_BIT,
153 {},
154 };
155
156 RenderDataStoreWeather::CloudRenderingType cloudRenderingType {
157 RenderDataStoreWeather::CloudRenderingType::REPROJECTED
158 };
159 };
160 CloudData cloudMatData_;
161 };
162 CORE3D_END_NAMESPACE()
163