• 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 API_RENDER_DEVICE_IDEVICE_H
17 #define API_RENDER_DEVICE_IDEVICE_H
18 
19 #include <cstdint>
20 
21 #include <render/device/pipeline_state_desc.h>
22 #include <render/namespace.h>
23 
24 RENDER_BEGIN_NAMESPACE()
25 class IGpuResourceManager;
26 class IShaderManager;
27 
28 struct BackendExtra {};
29 struct RenderBackendRecordingState {};
30 
31 /** Device configuration flag bits */
32 enum DeviceConfigurationFlagBits {
33     /** Enable pipeline caching */
34     CORE_DEVICE_CONFIGURATION_PIPELINE_CACHE_BIT = 1 << 0,
35 };
36 
37 /** Swapchain flag bits */
38 enum SwapchainFlagBits {
39     /** Color buffer */
40     CORE_SWAPCHAIN_COLOR_BUFFER_BIT = 0x00000001,
41     /** Depth buffer */
42     CORE_SWAPCHAIN_DEPTH_BUFFER_BIT = 0x00000002,
43 };
44 /** Container for swapchain flag bits */
45 using SwapchainFlags = uint32_t;
46 
47 /** Swapchain create info */
48 struct SwapchainCreateInfo {
49     /** Surface handle */
50     uint64_t surfaceHandle { 0 };
51     /** Vertical sync enabled */
52     bool vsync { true };
53     /** Prefer sRBG format */
54     bool preferSrgbFormat { true };
55     /** Swapchain flags */
56     SwapchainFlags swapchainFlags { SwapchainFlagBits::CORE_SWAPCHAIN_COLOR_BUFFER_BIT };
57 };
58 
59 struct DeviceConfiguration {
60     /** Maps to buffering count e.g. coherent gpu buffers and command buffers (vulkan)
61      * default value is 3, supported values depends from the backend.
62      * Should always be bigger or equal to swapchain count. Waits earlier in the frame.
63      */
64     uint32_t bufferingCount { 3u };
65     /** Maps to desired swapchain count default value is 3,
66      * and supported values depend from the backend.
67      */
68     uint32_t swapchainImageCount { 3u };
69 
70     /**
71      * Integrated devices (mobile gpus, integrated desktop chips) expose all memory pools with host visible flags.
72      * If all pools have requiredIntegratedMemoryFlags then this optimization will be enabled and
73      * staging buffers are not used for this data. The data is directly mapped once for writing.
74      * Set to zero (0u) to disable all staging optimizations.
75      */
76     uint32_t requiredIntegratedMemoryFlags { CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
77                                              CORE_MEMORY_PROPERTY_HOST_VISIBLE_BIT };
78 
79     /** Additional options for device creation.
80      */
81     uint32_t configurationFlags { CORE_DEVICE_CONFIGURATION_PIPELINE_CACHE_BIT };
82 };
83 
84 /** Device backend type */
85 enum class DeviceBackendType {
86     /** Vulkan backend */
87     VULKAN,
88     /** GLES backend */
89     OPENGLES,
90     /** OpenGL backend */
91     OPENGL
92 };
93 
94 /** @ingroup group_gfx_idevice */
95 /** Device create info */
96 struct DeviceCreateInfo {
97     /** Backend type to be created */
98     DeviceBackendType backendType { DeviceBackendType::VULKAN };
99     /** Device configuration */
100     DeviceConfiguration deviceConfiguration;
101     /** Backend configuration pointer */
102     const BackendExtra* backendConfiguration { nullptr };
103 };
104 
105 /** @ingroup group_gfx_ilowleveldevice */
106 /** ILowLevelDevice, to provide low-level access to platform device and resources. */
107 class ILowLevelDevice {
108 public:
109     /** Returns backend type */
110     virtual DeviceBackendType GetBackendType() const = 0;
111 
112 protected:
113     ILowLevelDevice() = default;
114     virtual ~ILowLevelDevice() = default;
115 };
116 
117 /** @ingroup group_gfx_idevice */
118 struct DevicePlatformData {};
119 
120 /** @ingroup group_gfx_idevice */
121 /** IDevice */
122 class IDevice {
123 public:
124     /** Create a swapchain for graphics API use.
125      *  @param swapchainCreateInfo Swapchain creation info.
126      */
127     virtual void CreateSwapchain(const SwapchainCreateInfo& swapchainCreateInfo) = 0;
128 
129     /** Destroys swapchain
130      */
131     virtual void DestroySwapchain() = 0;
132 
133     /** Returns backend type (vulkan, gles, etc) */
134     virtual DeviceBackendType GetBackendType() const = 0;
135 
136     /** Returns device platform data which needs to be cast for correct device. */
137     virtual const DevicePlatformData& GetPlatformData() const = 0;
138 
139     /** Get GPU resource manager interface. */
140     virtual IGpuResourceManager& GetGpuResourceManager() const = 0;
141     /** Get shader manager interface. */
142     virtual IShaderManager& GetShaderManager() const = 0;
143 
144     /** Returns supported flags for given format. */
145     virtual FormatProperties GetFormatProperties(const BASE_NS::Format format) const = 0;
146 
147     /** Returns acceleration structure build sizes. Only a single geometry data should be valid.
148      *  @param geometryInfo Build geometry info for size calculations.
149      *  @param triangles Build geometry info for size calculations.
150      *  @param aabb Build geometry info for size calculations.
151      *  @param instances Build geometry info for size calculations.
152      */
153     virtual AccelerationStructureBuildSizes GetAccelerationStructureBuildSizes(
154         const AccelerationStructureBuildGeometryInfo& geometry,
155         BASE_NS::array_view<const AccelerationStructureGeometryTrianglesInfo> triangles,
156         BASE_NS::array_view<const AccelerationStructureGeometryAabbsInfo> aabbs,
157         BASE_NS::array_view<const AccelerationStructureGeometryInstancesInfo> instances) const = 0;
158 
159     /** Get frame count. */
160     virtual uint64_t GetFrameCount() const = 0;
161 
162     /** Wait for the GPU to become idle.
163      *  Needs to be called from render thread where renderFrame is called.
164      *
165      *  This is a hazardous method and should only be called with extreme caution.
166      *  Do not call this method per frame.
167      */
168     virtual void WaitForIdle() = 0;
169 
170     /** Get low level device interface. Needs to be cast to correct ILowLevelDeviceXX based on backend type. */
171     virtual ILowLevelDevice& GetLowLevelDevice() const = 0;
172 
173 protected:
174     IDevice() = default;
175     virtual ~IDevice() = default;
176 };
177 
178 /** @ingroup group_gfx_idevice */
179 struct GpuBufferPlatformData {};
180 struct GpuImagePlatformData {};
181 struct GpuSamplerPlatformData {};
182 struct GpuAccelerationStructurePlatformData {};
183 RENDER_END_NAMESPACE()
184 
185 #endif // API_RENDER_DEVICE_IDEVICE_H
186