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