• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
22 #include <base/containers/string.h>
23 #include <base/containers/unique_ptr.h>
24 #include <base/containers/vector.h>
25 #include <base/util/formats.h>
26 #include <core/threading/intf_thread_pool.h>
27 #include <render/device/intf_device.h>
28 #include <render/device/pipeline_state_desc.h>
29 #include <render/namespace.h>
30 #include <render/resource_handle.h>
31 
32 #include "util/log.h"
33 
34 RENDER_BEGIN_NAMESPACE()
35 class RenderContext;
36 class ShaderManager;
37 class GpuResourceManager;
38 
39 class ShaderModule;
40 class ComputePipelineStateObject;
41 class GpuAccelerationStructure;
42 class GpuBuffer;
43 class GpuComputeProgram;
44 class GpuImage;
45 class GpuSampler;
46 class GpuResourceManager;
47 class GpuComputeProgram;
48 class GpuShaderProgram;
49 class GraphicsPipelineStateObject;
50 class PlatformGpuMemoryAllocator;
51 class RenderBackend;
52 class RenderFrameSync;
53 class NodeContextDescriptorSetManager;
54 class NodeContextPoolManager;
55 class ShaderModule;
56 class Swapchain;
57 struct BackendSpecificImageDesc;
58 struct GpuAccelerationStructureDesc;
59 struct GpuBufferDesc;
60 struct GpuComputeProgramCreateData;
61 struct GpuImageDesc;
62 struct GpuImagePlatformData;
63 struct GpuSamplerDesc;
64 struct GpuShaderProgramCreateData;
65 struct PipelineLayout;
66 struct ShaderModuleCreateInfo;
67 struct SwapchainCreateInfo;
68 
69 struct LowLevelRenderPassData {};
70 struct LowLevelPipelineLayoutData {};
71 
72 struct DeviceFormatSupportConstants {
73     PLUGIN_STATIC_ASSERT(BASE_NS::Format::BASE_FORMAT_ASTC_12x12_SRGB_BLOCK == 184u);
74     PLUGIN_STATIC_ASSERT(BASE_NS::Format::BASE_FORMAT_G8B8G8R8_422_UNORM == 1000156000u);
75     static constexpr uint32_t ALL_FLAGS_SUPPORTED { 0xFFFFu };
76     static constexpr uint32_t LINEAR_FORMAT_MAX_IDX { BASE_NS::Format::BASE_FORMAT_ASTC_12x12_SRGB_BLOCK };
77     static constexpr uint32_t LINEAR_FORMAT_MAX_COUNT { LINEAR_FORMAT_MAX_IDX + 1u };
78     static constexpr uint32_t ADDITIONAL_FORMAT_START_NUMBER { BASE_NS::Format::BASE_FORMAT_G8B8G8R8_422_UNORM };
79     static constexpr uint32_t ADDITIONAL_FORMAT_END_NUMBER { BASE_NS::Format::BASE_FORMAT_G8_B8_R8_3PLANE_444_UNORM };
80     static constexpr uint32_t ADDITIONAL_FORMAT_MAX_COUNT {
81         (ADDITIONAL_FORMAT_END_NUMBER - ADDITIONAL_FORMAT_START_NUMBER) + 1u
82     };
83     static constexpr uint32_t ADDITIONAL_FORMAT_BASE_IDX { LINEAR_FORMAT_MAX_COUNT };
84 };
85 
86 class Device : public IDevice {
87 public:
88     Device(RenderContext& renderContext, const DeviceCreateInfo& deviceCreateInfo);
89 
90     Device(const Device&) = delete;
91     Device& operator=(const Device&) = delete;
92 
93     virtual PlatformGpuMemoryAllocator* GetPlatformGpuMemoryAllocator() = 0;
94 
95     // Activate context
96     virtual void Activate() = 0;
97     // Deactivate context
98     virtual void Deactivate() = 0;
99 
100     // Allow parallel render node processing  (on GLES checks that coherent mapping allowed)
101     virtual bool AllowThreadedProcessing() const = 0;
102 
103     // Generally set once to true when device is created.
104     // Set to false e.g. when device is lost.
105     // Set and Get uses atomics if needed by the platform.
106     void SetDeviceStatus(const bool status);
107     bool GetDeviceStatus() const;
108 
109     // (re-)create swapchain
110     void CreateSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) override;
111     void DestroySwapchain() override;
112     // device specific preparation
113     virtual void CreateDeviceSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) = 0;
114     virtual void DestroyDeviceSwapchain() = 0;
115 
116     // Lock/Unlock access for this frame's GPU resources (for safety reasons)
117     void SetLockResourceBackendAccess(const bool value);
118     // Set render backend running
119     void SetRenderBackendRunning(const bool value);
120     bool GetLockResourceBackendAccess() const;
121 
122     // Marks the beginning of a frame rendering. Expected to at least increment frame counter.
123     void FrameStart();
124     void FrameEnd();
125 
126     IGpuResourceManager& GetGpuResourceManager() const override;
127     IShaderManager& GetShaderManager() const override;
128 
129     uint64_t GetFrameCount() const override;
130 
131     bool HasSwapchain() const;
132     const Swapchain* GetSwapchain() const;
133     // set backbuffer (swapchain) client handle
134     void SetBackbufferHandle(const RenderHandle handle);
135     // get backbuffer client handle
136     RenderHandle GetBackbufferHandle() const;
137 
138     DeviceConfiguration GetDeviceConfiguration() const;
139     // Number of command buffers to allocate for each node
140     uint32_t GetCommandBufferingCount() const;
141     // Get ANDed memory property flags from all memory pools
142     MemoryPropertyFlags GetSharedMemoryPropertyFlags() const;
143 
144     // Platform specific creation
145 
146     virtual BASE_NS::unique_ptr<GpuBuffer> CreateGpuBuffer(const GpuBufferDesc& desc) = 0;
147 
148     // Create gpu image resources
149     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImage(const GpuImageDesc& desc) = 0;
150     /** Creates a engine GpuImage resource with platform resources.
151      * Does not own the platform resources nor does not destroy them.
152      */
153     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImageView(
154         const GpuImageDesc& desc, const GpuImagePlatformData& platformData) = 0;
155     virtual BASE_NS::unique_ptr<GpuImage> CreateGpuImageView(
156         const GpuImageDesc& desc, const BackendSpecificImageDesc& platformData) = 0;
157     virtual BASE_NS::vector<BASE_NS::unique_ptr<GpuImage>> CreateGpuImageViews(const Swapchain& platformData) = 0;
158 
159     virtual BASE_NS::unique_ptr<GpuSampler> CreateGpuSampler(const GpuSamplerDesc& desc) = 0;
160 
161     virtual BASE_NS::unique_ptr<GpuAccelerationStructure> CreateGpuAccelerationStructure(
162         const GpuAccelerationStructureDesc& desc) = 0;
163 
164     virtual BASE_NS::unique_ptr<RenderFrameSync> CreateRenderFrameSync() = 0;
165     virtual BASE_NS::unique_ptr<RenderBackend> CreateRenderBackend(
166         GpuResourceManager& gpuResourceMgr, const CORE_NS::IParallelTaskQueue::Ptr& queue) = 0;
167 
168     virtual BASE_NS::unique_ptr<ShaderModule> CreateShaderModule(const ShaderModuleCreateInfo& data) = 0;
169     virtual BASE_NS::unique_ptr<ShaderModule> CreateComputeShaderModule(const ShaderModuleCreateInfo& data) = 0;
170     virtual BASE_NS::unique_ptr<GpuShaderProgram> CreateGpuShaderProgram(GpuShaderProgramCreateData const& data) = 0;
171     virtual BASE_NS::unique_ptr<GpuComputeProgram> CreateGpuComputeProgram(GpuComputeProgramCreateData const& data) = 0;
172 
173     virtual BASE_NS::unique_ptr<NodeContextDescriptorSetManager> CreateNodeContextDescriptorSetManager() = 0;
174     virtual BASE_NS::unique_ptr<NodeContextPoolManager> CreateNodeContextPoolManager(
175         class GpuResourceManager& gpuResourceMgr, const GpuQueue& gpuQueue) = 0;
176 
177     virtual BASE_NS::unique_ptr<GraphicsPipelineStateObject> CreateGraphicsPipelineStateObject(
178         const GpuShaderProgram& gpuProgram, const GraphicsState& graphicsState, const PipelineLayout& pipelineLayout,
179         const VertexInputDeclarationView& vertexInputDeclarationView,
180         const ShaderSpecializationConstantDataView& shaderSpecializationConstantDataView,
181         const DynamicStateFlags dynamicStateFlags, const RenderPassDesc& renderPassDesc,
182         const BASE_NS::array_view<const RenderPassSubpassDesc>& renderPassSubpassDesc, const uint32_t subpassIndex,
183         const LowLevelRenderPassData* renderPassData, const LowLevelPipelineLayoutData* pipelineLayoutData) = 0;
184 
185     virtual BASE_NS::unique_ptr<ComputePipelineStateObject> CreateComputePipelineStateObject(
186         const GpuComputeProgram& gpuProgram, const PipelineLayout& pipelineLayout,
187         const ShaderSpecializationConstantDataView& shaderSpecializationConstantDataView,
188         const LowLevelPipelineLayoutData* pipelineLayoutData) = 0;
189 
190     // returns a valid queue from the same family or default queue
191     virtual GpuQueue GetValidGpuQueue(const GpuQueue& gpuQueue) const = 0;
192     virtual uint32_t GetGpuQueueCount() const = 0;
193 
194     /** Initialize cache for accelerating pipeline state object creation. Caching is not used unless this function is
195      * called.
196      * @param initialData Optional cache content returned by GetPipelineCache.
197      */
198     virtual void InitializePipelineCache(BASE_NS::array_view<const uint8_t> initialData) = 0;
199     virtual BASE_NS::vector<uint8_t> GetPipelineCache() const = 0;
200 
201 protected:
202     RenderContext& renderContext_;
203     DeviceConfiguration deviceConfiguration_;
204 
205     BASE_NS::unique_ptr<GpuResourceManager> gpuResourceMgr_;
206     BASE_NS::unique_ptr<ShaderManager> shaderMgr_;
207 
208     BASE_NS::unique_ptr<Swapchain> swapchain_;
209     RenderHandle backbufferHandle_;
210 
211     std::atomic<bool> deviceStatus_ { false };
212     uint64_t frameCount_ { 0u };
213 
214     bool isRenderbackendRunning_ { false };
215     bool isBackendResourceAccessLocked_ { false };
216 
217     MemoryPropertyFlags deviceSharedMemoryPropertyFlags_ { 0 };
218 
219     BASE_NS::vector<RenderHandleReference> defaultGpuResources_;
220 };
221 
222 RENDER_END_NAMESPACE()
223 
224 #endif // DEVICE_DEVICE_H
225