• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef DEVICE_DEVICE_H
17 #define DEVICE_DEVICE_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <mutex>
22 
23 #include <base/containers/string.h>
24 #include <base/containers/unique_ptr.h>
25 #include <base/containers/unordered_map.h>
26 #include <base/containers/vector.h>
27 #include <base/util/color.h>
28 #include <base/util/formats.h>
29 #include <core/namespace.h>
30 #include <core/threading/intf_thread_pool.h>
31 #include <render/device/intf_device.h>
32 #include <render/device/pipeline_state_desc.h>
33 #include <render/namespace.h>
34 #include <render/resource_handle.h>
35 
36 #include "device/shader_manager.h"
37 #include "device/swapchain.h"
38 
39 RENDER_BEGIN_NAMESPACE()
40 class RenderContext;
41 class GpuResourceManager;
42 class DescriptorSetManager;
43 
44 class ShaderModule;
45 class ComputePipelineStateObject;
46 class GpuBuffer;
47 class GpuComputeProgram;
48 class GpuImage;
49 class GpuSampler;
50 class GpuResourceManager;
51 class GpuComputeProgram;
52 class GpuShaderProgram;
53 class GraphicsPipelineStateObject;
54 class PlatformGpuMemoryAllocator;
55 class RenderBackend;
56 class RenderFrameSync;
57 class NodeContextDescriptorSetManager;
58 class NodeContextPoolManager;
59 class ShaderModule;
60 class Swapchain;
61 class GpuSemaphore;
62 struct BackendSpecificBufferDesc;
63 struct BackendSpecificImageDesc;
64 struct GpuAccelerationStructureDesc;
65 struct GpuBufferDesc;
66 struct GpuComputeProgramCreateData;
67 struct GpuImageDesc;
68 struct GpuImagePlatformData;
69 struct GpuSamplerDesc;
70 struct GpuShaderProgramCreateData;
71 struct PipelineLayout;
72 struct ShaderModuleCreateInfo;
73 struct SwapchainCreateInfo;
74 
75 struct LowLevelRenderPassData {};
76 struct LowLevelPipelineLayoutData {};
77 
78 struct DeviceFormatSupportConstants {
79     static constexpr uint32_t ALL_FLAGS_SUPPORTED { 0xFFFFu };
80     static constexpr uint32_t LINEAR_FORMAT_MAX_IDX { BASE_NS::Format::BASE_FORMAT_ASTC_12x12_SRGB_BLOCK };
81     static constexpr uint32_t LINEAR_FORMAT_MAX_COUNT { LINEAR_FORMAT_MAX_IDX + 1u };
82     static constexpr uint32_t ADDITIONAL_FORMAT_START_NUMBER { BASE_NS::Format::BASE_FORMAT_G8B8G8R8_422_UNORM };
83     static constexpr uint32_t ADDITIONAL_FORMAT_END_NUMBER { BASE_NS::Format::BASE_FORMAT_G8_B8_R8_3PLANE_444_UNORM };
84     static constexpr uint32_t ADDITIONAL_FORMAT_MAX_COUNT {
85         (ADDITIONAL_FORMAT_END_NUMBER - ADDITIONAL_FORMAT_START_NUMBER) + 1u
86     };
87     static constexpr uint32_t ADDITIONAL_FORMAT_BASE_IDX { LINEAR_FORMAT_MAX_COUNT };
88 };
89 
90 class Device : public IDevice {
91 public:
92     explicit Device(RenderContext& renderContext);
93 
94     Device(const Device&) = delete;
95     Device& operator=(const Device&) = delete;
96 
97     virtual PlatformGpuMemoryAllocator* GetPlatformGpuMemoryAllocator() = 0;
98 
99     // Activate context
100     virtual void Activate() = 0;
101     // Deactivate context
102     virtual void Deactivate() = 0;
103 
104     // Allow parallel render node processing  (on GLES checks that coherent mapping allowed)
105     virtual bool AllowThreadedProcessing() const = 0;
106 
107     // Generally set once to true when device is created.
108     // Set to false e.g. when device is lost.
109     // Set and Get uses atomics if needed by the platform.
110     void SetDeviceStatus(bool status);
111     bool GetDeviceStatus() const;
112 
113     // (re-)create swapchain
114     void CreateSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) override;
115     RenderHandleReference CreateSwapchainHandle(const SwapchainCreateInfo& swapchainCreateInfo,
116         const RenderHandleReference& replacedHandle, BASE_NS::string_view name) override;
117     RenderHandleReference CreateSwapchainHandle(const SwapchainCreateInfo& swapchainCreateInfo) override;
118     void DestroySwapchain() override;
119     void DestroySwapchain(const RenderHandleReference& handle) override;
120     // device specific preparation
121     virtual BASE_NS::unique_ptr<Swapchain> CreateDeviceSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) = 0;
122     virtual void DestroyDeviceSwapchain() = 0;
123 
124     RenderHandleReference CreateSwapchainImpl(const SwapchainCreateInfo& swapchainCreateInfo,
125         const RenderHandleReference& replacedHandle, BASE_NS::string_view name);
126     void DestroySwapchainImpl(const RenderHandleReference& handle);
127 
128     SurfaceTransformFlags GetSurfaceTransformFlags(const RenderHandle& handle) const;
129 
130     // Lock/Unlock access for this frame's GPU resources (for safety reasons)
131     void SetLockResourceBackendAccess(bool value);
132     // Set render backend running
133     void SetRenderBackendRunning(bool value);
134     bool GetLockResourceBackendAccess() const;
135 
136     // Set to true when RenderFrame called
137     // Set to false when coming out of RenderFrame
138     void SetRenderFrameRunning(bool value);
139     bool GetRenderFrameRunning() const;
140 
141     // Marks the beginning of a frame rendering. Expected to at least increment frame counter.
142     void FrameStart();
143     void FrameEnd();
144 
145     IGpuResourceManager& GetGpuResourceManager() const override;
146     IShaderManager& GetShaderManager() const override;
147 
148     DescriptorSetManager& GetDescriptorSetManager() const;
149 
150     void SetBackendConfig(const BackendConfig& config) override;
151 
152     uint64_t GetFrameCount() const override;
153 
154     const CommonDeviceProperties& GetCommonDeviceProperties() const override;
155 
156     DeviceConfiguration GetDeviceConfiguration() const override;
157 
158     bool HasSwapchain() const;
159     // get swapchain with shallow
160     const Swapchain* GetSwapchain(RenderHandle handle) const;
161 
162     struct SwapchainData {
163         static constexpr uint32_t MAX_IMAGE_VIEW_COUNT { 5U };
164 
165         RenderHandle remappableSwapchainImage {};
166         RenderHandle imageViews[MAX_IMAGE_VIEW_COUNT];
167         uint32_t imageViewCount { 0U };
168     };
169 
170     struct InternalSwapchainData {
171         static constexpr uint32_t MAX_IMAGE_VIEW_COUNT { 5U };
172 
173         uint64_t surfaceHandle { 0 };
174         uintptr_t window { 0 };
175 
176         BASE_NS::string globalName;
177         BASE_NS::string name;
178         RenderHandleReference remappableSwapchainImage {};
179         RenderHandleReference additionalDepthBufferHandle {};
180         RenderHandle remappableAdditionalSwapchainImage {}; // Not owned
181         RenderHandleReference imageViews[MAX_IMAGE_VIEW_COUNT];
182         uint32_t imageViewCount { 0U };
183 
184         BASE_NS::unique_ptr<Swapchain> swapchain;
185     };
186     SwapchainData GetSwapchainData(RenderHandle handle) const;
187 
188     // Number of command buffers to allocate for each node
189     uint32_t GetCommandBufferingCount() const;
190     // Get ANDed memory property flags from all memory pools
191     MemoryPropertyFlags GetSharedMemoryPropertyFlags() const;
192 
193     BASE_NS::Format GetFormatOrFallback(BASE_NS::Format inputFormat) const;
194     BASE_NS::Format GetColorSpaceFormat(BASE_NS::Format inputFormat, BASE_NS::ColorSpaceFlags colorSpaceFlags) const;
195     BASE_NS::ColorSpaceFlags GetColorSpaceFlags() const;
196 
197     // Platform specific creation
198 
199     virtual BASE_NS::unique_ptr<GpuBuffer> CreateGpuBuffer(const GpuBufferDesc& desc) = 0;
200     virtual BASE_NS::unique_ptr<GpuBuffer> CreateGpuBuffer(const GpuAccelerationStructureDesc& desc) = 0;
201     virtual BASE_NS::unique_ptr<GpuBuffer> CreateGpuBuffer(const BackendSpecificBufferDesc& desc) = 0;
202 
203     // Create gpu image resources
204     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImage(const GpuImageDesc& desc) = 0;
205     /** Creates a engine GpuImage resource with platform resources.
206      * Does not own the platform resources nor does not destroy them.
207      */
208     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImageView(
209         const GpuImageDesc& desc, const GpuImagePlatformData& platformData) = 0;
210     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImageView(
211         const GpuImageDesc& desc, const BackendSpecificImageDesc& platformData) = 0;
212     virtual BASE_NS::vector<BASE_NS::unique_ptr<GpuImage>> CreateGpuImageViews(const Swapchain& platformData) = 0;
213 
214     virtual BASE_NS::unique_ptr<GpuSampler> CreateGpuSampler(const GpuSamplerDesc& desc) = 0;
215 
216     virtual BASE_NS::unique_ptr<RenderFrameSync> CreateRenderFrameSync() = 0;
217     virtual BASE_NS::unique_ptr<RenderBackend> CreateRenderBackend(
218         GpuResourceManager& gpuResourceMgr, CORE_NS::ITaskQueue* queue) = 0;
219 
220     virtual BASE_NS::unique_ptr<ShaderModule> CreateShaderModule(const ShaderModuleCreateInfo& data) = 0;
221     virtual BASE_NS::unique_ptr<ShaderModule> CreateComputeShaderModule(const ShaderModuleCreateInfo& data) = 0;
222     virtual BASE_NS::unique_ptr<GpuShaderProgram> CreateGpuShaderProgram(GpuShaderProgramCreateData const& data) = 0;
223     virtual BASE_NS::unique_ptr<GpuComputeProgram> CreateGpuComputeProgram(GpuComputeProgramCreateData const& data) = 0;
224 
225     virtual BASE_NS::unique_ptr<NodeContextDescriptorSetManager> CreateNodeContextDescriptorSetManager() = 0;
226     virtual BASE_NS::unique_ptr<NodeContextPoolManager> CreateNodeContextPoolManager(
227         class GpuResourceManager& gpuResourceMgr, const GpuQueue& gpuQueue) = 0;
228 
229     virtual BASE_NS::unique_ptr<GraphicsPipelineStateObject> CreateGraphicsPipelineStateObject(
230         const GpuShaderProgram& gpuProgram, const GraphicsState& graphicsState, const PipelineLayout& pipelineLayout,
231         const VertexInputDeclarationView& vertexInputDeclarationView,
232         const ShaderSpecializationConstantDataView& shaderSpecializationConstantDataView,
233         BASE_NS::array_view<const DynamicStateEnum> dynamicStates, const RenderPassDesc& renderPassDesc,
234         const BASE_NS::array_view<const RenderPassSubpassDesc>& renderPassSubpassDesc, uint32_t subpassIndex,
235         const LowLevelRenderPassData* renderPassData, const LowLevelPipelineLayoutData* pipelineLayoutData) = 0;
236 
237     virtual BASE_NS::unique_ptr<ComputePipelineStateObject> CreateComputePipelineStateObject(
238         const GpuComputeProgram& gpuProgram, const PipelineLayout& pipelineLayout,
239         const ShaderSpecializationConstantDataView& shaderSpecializationConstantDataView,
240         const LowLevelPipelineLayoutData* pipelineLayoutData) = 0;
241 
242     virtual BASE_NS::unique_ptr<GpuSemaphore> CreateGpuSemaphore() = 0;
243     virtual BASE_NS::unique_ptr<GpuSemaphore> CreateGpuSemaphoreView(uint64_t handle) = 0;
244 
245     // returns a valid queue from the same family or default queue
246     virtual GpuQueue GetValidGpuQueue(const GpuQueue& gpuQueue) const = 0;
247     virtual uint32_t GetGpuQueueCount() const = 0;
248 
249     /** Initialize cache for accelerating pipeline state object creation. Caching is not used unless this function is
250      * called.
251      * @param initialData Optional cache content returned by GetPipelineCache.
252      */
253     virtual void InitializePipelineCache(BASE_NS::array_view<const uint8_t> initialData) = 0;
254     virtual BASE_NS::vector<uint8_t> GetPipelineCache() const = 0;
255 
256 protected:
257     RenderContext& renderContext_;
258     DeviceConfiguration deviceConfiguration_;
259 
260     BASE_NS::unique_ptr<GpuResourceManager> gpuResourceMgr_;
261     BASE_NS::unique_ptr<ShaderManager> shaderMgr_;
262     BASE_NS::unique_ptr<DescriptorSetManager> globalDescriptorSetMgr_;
263 
264     // multi-swapchain, the built-in default is only with swapchain_ and swapchainData_
265     BASE_NS::vector<InternalSwapchainData> swapchains_;
266     RenderHandleReference defaultSwapchainHandle_;
267 
268     std::atomic<bool> deviceStatus_ { false };
269     uint64_t frameCount_ { 0 };
270 
271     bool isRenderbackendRunning_ { false };
272     bool isBackendResourceAccessLocked_ { false };
273     bool isRenderFrameRunning_ { false };
274 
275     MemoryPropertyFlags deviceSharedMemoryPropertyFlags_ { 0 };
276     CommonDeviceProperties commonDeviceProperties_;
277 
278     BASE_NS::unordered_map<BASE_NS::Format, BASE_NS::Format> colorSpaceLinearFormats_;
279     BASE_NS::unordered_map<BASE_NS::Format, BASE_NS::Format> colorSpaceSrgbAsLinearFormats_;
280 };
281 
282 // Plaform specific helper
283 GpuImageDesc GetImageDescFromHwBufferDesc(uintptr_t platformHwBuffer);
284 GpuBufferDesc GetBufferDescFromHwBufferDesc(uintptr_t platformHwBuffer);
285 RENDER_END_NAMESPACE()
286 
287 #endif // DEVICE_DEVICE_H
288