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 API_RENDER_DEVICE_IDEVICE_H 17 #define API_RENDER_DEVICE_IDEVICE_H 18 19 #include <cstdint> 20 21 #include <render/device/gpu_resource_desc.h> 22 #include <render/device/pipeline_state_desc.h> 23 #include <render/namespace.h> 24 25 RENDER_BEGIN_NAMESPACE() 26 class IGpuResourceManager; 27 class IShaderManager; 28 29 struct BackendExtra {}; 30 struct RenderBackendRecordingState {}; 31 32 /** Device configuration flag bits */ 33 enum DeviceConfigurationFlagBits { 34 /** Enable pipeline caching */ 35 CORE_DEVICE_CONFIGURATION_PIPELINE_CACHE_BIT = 1 << 0, 36 }; 37 38 /** Swapchain flag bits */ 39 enum SwapchainFlagBits { 40 /** Color buffer */ 41 CORE_SWAPCHAIN_COLOR_BUFFER_BIT = 0x00000001, 42 /** Depth buffer */ 43 CORE_SWAPCHAIN_DEPTH_BUFFER_BIT = 0x00000002, 44 /** Enable v-sync */ 45 CORE_SWAPCHAIN_VSYNC_BIT = 0x00000004, 46 /** Hint to prefer sRGB format */ 47 CORE_SWAPCHAIN_SRGB_BIT = 0x00000008, 48 /** Hint to prefer high dynamic range format */ 49 CORE_SWAPCHAIN_HDR_BIT = 0x00000010, 50 }; 51 /** Container for swapchain flag bits */ 52 using SwapchainFlags = uint32_t; 53 54 /** Swapchain create info */ 55 struct SwapchainCreateInfo { 56 /** Surface handle */ 57 uint64_t surfaceHandle { 0 }; 58 /** Swapchain flags */ 59 SwapchainFlags swapchainFlags { SwapchainFlagBits::CORE_SWAPCHAIN_COLOR_BUFFER_BIT | 60 SwapchainFlagBits::CORE_SWAPCHAIN_VSYNC_BIT | 61 SwapchainFlagBits::CORE_SWAPCHAIN_SRGB_BIT }; 62 /** Needed image usage flags for swapchain color image. Checked against supported */ 63 ImageUsageFlags imageUsageFlags { ImageUsageFlagBits::CORE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT }; 64 65 struct SwapchainWindow { 66 /** Platform window pointer */ 67 uintptr_t window { 0 }; 68 /** Platform instance (hInstance, connection, display) */ 69 uintptr_t instance { 0 }; 70 }; 71 /** Window handles. Creates surface automatically. 72 * NOTE: do not create surface for surfaceHandle if the window and instance is provided 73 */ 74 SwapchainWindow window; 75 }; 76 77 struct DeviceConfiguration { 78 /** Maps to buffering count e.g. coherent gpu buffers and command buffers (vulkan) 79 * default value is 3, supported values depends from the backend. 80 * Should always be bigger or equal to swapchain count. Waits earlier in the frame. 81 */ 82 uint32_t bufferingCount { 3u }; 83 /** Maps to desired swapchain count default value is 3, 84 * and supported values depend from the backend. 85 */ 86 uint32_t swapchainImageCount { 3u }; 87 88 /** 89 * Integrated devices (mobile gpus, integrated desktop chips) expose all memory pools with host visible flags. 90 * If all pools have requiredIntegratedMemoryFlags then this optimization will be enabled and 91 * staging buffers are not used for this data. The data is directly mapped once for writing. 92 * Set to zero (0u) to disable all staging optimizations. 93 */ 94 uint32_t requiredIntegratedMemoryFlags { CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | 95 CORE_MEMORY_PROPERTY_HOST_VISIBLE_BIT }; 96 97 /** Additional options for device creation. 98 */ 99 uint32_t configurationFlags { CORE_DEVICE_CONFIGURATION_PIPELINE_CACHE_BIT }; 100 }; 101 102 /** Device backend type */ 103 enum class DeviceBackendType { 104 /** Vulkan backend */ 105 VULKAN, 106 /** GLES backend */ 107 OPENGLES, 108 /** OpenGL backend */ 109 OPENGL 110 }; 111 112 /** @ingroup group_idevice */ 113 /** Device create info */ 114 struct DeviceCreateInfo { 115 /** Backend type to be created */ 116 DeviceBackendType backendType { DeviceBackendType::VULKAN }; 117 /** Device configuration */ 118 DeviceConfiguration deviceConfiguration; 119 /** Backend configuration pointer */ 120 const BackendExtra* backendConfiguration { nullptr }; 121 }; 122 123 /** Common device properties for various features */ 124 struct CommonDeviceProperties { 125 /** Fragment shading rate properties */ 126 FragmentShadingRateProperties fragmentShadingRateProperties; 127 }; 128 129 /** @ingroup group_gfx_ilowleveldevice */ 130 /** ILowLevelDevice, to provide low-level access to platform device and resources. */ 131 class ILowLevelDevice { 132 public: 133 /** Returns backend type */ 134 virtual DeviceBackendType GetBackendType() const = 0; 135 136 protected: 137 ILowLevelDevice() = default; 138 virtual ~ILowLevelDevice() = default; 139 }; 140 141 /** @ingroup group_idevice */ 142 struct DevicePlatformData {}; 143 144 /** Settings which can be changed after device has been created. */ 145 struct BackendConfig {}; 146 147 /** @ingroup group_idevice 148 * IDevice interface for accessing device. 149 * Not internally synchronized and therefore Create And Destroy -methods 150 * need to be called from the rendering thread. 151 * NOTE: Even though CreateSwapchainHandle returns a reference counted image handle, 152 * one needs to explicitly destroy the swapchain with DestroySwapchain(handle) 153 */ 154 class IDevice { 155 public: 156 /** Create a swapchain for graphics API use. 157 * @param swapchainCreateInfo Swapchain creation info. 158 */ 159 virtual void CreateSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) = 0; 160 161 /** Destroys swapchain 162 */ 163 virtual void DestroySwapchain() = 0; 164 165 /** Create a swapchain for graphics API use. Prefer using this method. 166 * Replace handle to re-use the same shallow handle throughout the Render. 167 * Add global unique image name to get global access to the resource. 168 * @param swapchainCreateInfo Swapchain creation info. 169 * @param replacedHandle Handle to be used as a shallow render handle for swapchain images. 170 * @param name Optional unique global name for the image. 171 */ 172 virtual RenderHandleReference CreateSwapchainHandle(const SwapchainCreateInfo& swapchainCreateInfo, 173 const RenderHandleReference& replacedHandle, const BASE_NS::string_view name) = 0; 174 175 /** Create a swapchain for graphics API use. 176 * @param swapchainCreateInfo Swapchain creation info. 177 */ 178 virtual RenderHandleReference CreateSwapchainHandle(const SwapchainCreateInfo& swapchainCreateInfo) = 0; 179 180 /** Destroys swapchain 181 */ 182 virtual void DestroySwapchain(const RenderHandleReference& handle) = 0; 183 184 /** Returns backend type (vulkan, gles, etc) */ 185 virtual DeviceBackendType GetBackendType() const = 0; 186 187 /** Returns device platform data which needs to be cast for correct device. */ 188 virtual const DevicePlatformData& GetPlatformData() const = 0; 189 190 /** Get GPU resource manager interface. */ 191 virtual IGpuResourceManager& GetGpuResourceManager() const = 0; 192 /** Get shader manager interface. */ 193 virtual IShaderManager& GetShaderManager() const = 0; 194 195 /** Returns supported flags for given format. */ 196 virtual FormatProperties GetFormatProperties(const BASE_NS::Format format) const = 0; 197 198 /** Returns common device properties */ 199 virtual const CommonDeviceProperties& GetCommonDeviceProperties() const = 0; 200 201 /** Returns acceleration structure build sizes. Only a single geometry data should be valid. 202 * @param geometryInfo Build geometry info for size calculations. 203 * @param triangles Build geometry info for size calculations. 204 * @param aabb Build geometry info for size calculations. 205 * @param instances Build geometry info for size calculations. 206 */ 207 virtual AsBuildSizes GetAccelerationStructureBuildSizes(const AsBuildGeometryInfo& geometry, 208 BASE_NS::array_view<const AsGeometryTrianglesInfo> triangles, 209 BASE_NS::array_view<const AsGeometryAabbsInfo> aabbs, 210 BASE_NS::array_view<const AsGeometryInstancesInfo> instances) const = 0; 211 212 /** Get frame count. */ 213 virtual uint64_t GetFrameCount() const = 0; 214 215 /** Wait for the GPU to become idle. 216 * Needs to be called from render thread where renderFrame is called. 217 * 218 * This is a hazardous method and should only be called with extreme caution. 219 * Do not call this method per frame. 220 */ 221 virtual void WaitForIdle() = 0; 222 223 /** Get low level device interface. Needs to be cast to correct ILowLevelDeviceXX based on backend type. */ 224 virtual ILowLevelDevice& GetLowLevelDevice() const = 0; 225 226 /** Update backend configuration. 227 * @param config Backend type specific settings. 228 */ 229 virtual void SetBackendConfig(const BackendConfig& config) = 0; 230 231 /** Get device configuration. 232 * The device configuration is updated after creation, i.e. the config might not match the device creation struct. 233 * @return Returns device configuration. 234 */ 235 virtual DeviceConfiguration GetDeviceConfiguration() const = 0; 236 237 protected: 238 IDevice() = default; 239 virtual ~IDevice() = default; 240 }; 241 242 /** @ingroup group_idevice */ 243 struct GpuBufferPlatformData {}; 244 struct GpuImagePlatformData {}; 245 struct GpuSamplerPlatformData {}; 246 struct GpuAccelerationStructurePlatformData {}; 247 RENDER_END_NAMESPACE() 248 249 #endif // API_RENDER_DEVICE_IDEVICE_H 250