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_IRENDER_CONTEXT_H
17 #define API_RENDER_IRENDER_CONTEXT_H
18
19 #include <base/containers/string.h>
20 #include <base/containers/string_view.h>
21 #include <base/util/color.h>
22 #include <core/plugin/intf_class_factory.h>
23 #include <render/device/intf_device.h>
24 #include <render/namespace.h>
25 #include <render/resource_handle.h>
26
27 CORE_BEGIN_NAMESPACE()
28 class IEngine;
29 CORE_END_NAMESPACE()
30
31 RENDER_BEGIN_NAMESPACE()
32 class IRenderDataStoreManager;
33 class IRenderNodeGraphManager;
34 class IRenderer;
35 class IRenderUtil;
36
37 /**
38 * RenderResultCode
39 */
40 enum class RenderResultCode : uint32_t {
41 RENDER_SUCCESS = 0,
42 RENDER_ERROR = 1,
43 };
44
45 /**
46 * VersionInfo
47 */
48 struct VersionInfo {
49 /** Name, default is "no_name" */
50 BASE_NS::string name { "no_name" };
51 /** Major number of version */
52 uint32_t versionMajor { 0 };
53 /** Minor number of version */
54 uint32_t versionMinor { 0 };
55 /** Patch number of version */
56 uint32_t versionPatch { 0 };
57 };
58
59 /**
60 * RenderCreateInfo
61 */
62 struct RenderCreateInfo {
63 /** Create info flag bits to setup configuration flags */
64 enum CreateInfoFlagBits : uint32_t {
65 /** Request separate render frame backend (backend graphics API control) */
66 CREATE_INFO_SEPARATE_RENDER_FRAME_BACKEND_BIT = 0x00000002,
67 /** Request separate render frame present (backend graphics API presentation) */
68 CREATE_INFO_SEPARATE_RENDER_FRAME_PRESENT_BIT = 0x00000004,
69 /** Request ray-tracing support */
70 CREATE_INFO_RAY_TRACING_BIT = 0x00000008,
71 };
72 /** Container for render create info flag bits */
73 using CreateInfoFlags = uint32_t;
74 /** Thread pool create info */
75 struct ThreadPoolCreateInfo {
76 /** Coefficient to hardware registered number of cores
77 * With 1.0f and maxCount { ~0U }, the maximum number of core tasks are in use
78 */
79 float threadCountCoefficient { 0.5f };
80 /** Minimum count of threads in the thread pool.
81 * The renderer should have at least 4 threads to parallelize correctly.
82 */
83 uint32_t minCount { 4U };
84 /** Maximum count of threads in the thread pool.
85 * The renderer thread count should not exceed the expected task count.
86 */
87 uint32_t maxCount { ~0U };
88 };
89
90 /** Application version info */
91 VersionInfo applicationInfo;
92 /** Device create info */
93 DeviceCreateInfo deviceCreateInfo;
94 /** Creation flags */
95 CreateInfoFlags createFlags { 0U };
96 /** Threadpool create info */
97 ThreadPoolCreateInfo threadPoolCreateInfo;
98 /** Color space flags (defaults to linear) */
99 BASE_NS::ColorSpaceFlags colorSpaceFlags { 0U };
100 };
101
102 /**
103 * IRenderContext interface for accessing managers and renderer.
104 * Device must be created to access valid renderer, render node graph manager, and render data store manager.
105 */
106 class IRenderContext : public CORE_NS::IClassFactory {
107 public:
108 static constexpr auto UID = BASE_NS::Uid { "c8d8650b-efac-4e04-8e66-e12b35d2749e" };
109 using Ptr = BASE_NS::refcnt_ptr<IRenderContext>;
110
111 /** Init, create device for render use.
112 * @param createInfo Device creation info.
113 */
114 virtual RenderResultCode Init(const RenderCreateInfo& createInfo) = 0;
115
116 /** Get active device. */
117 virtual IDevice& GetDevice() const = 0;
118 /** Get rendererer */
119 virtual IRenderer& GetRenderer() const = 0;
120 /** Get render node graph manager */
121 virtual IRenderNodeGraphManager& GetRenderNodeGraphManager() const = 0;
122 /** Get render data store manager */
123 virtual IRenderDataStoreManager& GetRenderDataStoreManager() const = 0;
124
125 /** Get render utilities */
126 virtual IRenderUtil& GetRenderUtil() const = 0;
127
128 /** Get engine */
129 virtual CORE_NS::IEngine& GetEngine() const = 0;
130
131 /** Get version */
132 virtual BASE_NS::string_view GetVersion() = 0;
133
134 /** Writes current pipelines to disk if cache:// location exists. */
135 virtual void WritePipelineCache() const = 0;
136
137 /** Get create info */
138 virtual RenderCreateInfo GetCreateInfo() const = 0;
139
140 protected:
141 IRenderContext() = default;
142 virtual ~IRenderContext() = default;
143 };
144
GetName(const IRenderContext *)145 inline constexpr BASE_NS::string_view GetName(const IRenderContext*)
146 {
147 return "IRenderContext";
148 }
149 RENDER_END_NAMESPACE()
150
151 #endif // API_RENDER_IRENDER_CONTEXT_H
152