• 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_GPU_RESOURCE_HANDLE_UTIL_H
17 #define DEVICE_GPU_RESOURCE_HANDLE_UTIL_H
18 
19 #include <cstdint>
20 
21 #include <base/namespace.h>
22 #include <base/util/compile_time_hashes.h>
23 #include <render/namespace.h>
24 #include <render/resource_handle.h>
25 
RENDER_BEGIN_NAMESPACE()26 RENDER_BEGIN_NAMESPACE()
27 constexpr const uint64_t GPU_RESOURCE_HANDLE_ID_MASK { INVALID_RESOURCE_HANDLE };
28 
29 struct EngineResourceHandle {
30     uint64_t id { GPU_RESOURCE_HANDLE_ID_MASK };
31 };
32 
33 inline bool operator==(EngineResourceHandle const& lhs, EngineResourceHandle const& rhs)
34 {
35     return (lhs.id & GPU_RESOURCE_HANDLE_ID_MASK) == (rhs.id & GPU_RESOURCE_HANDLE_ID_MASK);
36 }
37 
38 // NOTE: RenderHandleType valid max is currently 15
39 
40 enum RenderHandleInfoFlagBits {
41     CORE_RESOURCE_HANDLE_DYNAMIC_TRACK = 0x00000001,
42     CORE_RESOURCE_HANDLE_RESET_ON_FRAME_BORDERS = 0x00000002,
43     CORE_RESOURCE_HANDLE_DEPTH_IMAGE = 0x00000004,
44     CORE_RESOURCE_HANDLE_IMMEDIATELY_CREATED = 0x00000008,
45     CORE_RESOURCE_HANDLE_DEFERRED_DESTROY = 0x00000010,
46     CORE_RESOURCE_HANDLE_MAP_OUTSIDE_RENDERER = 0x00000020,
47     CORE_RESOURCE_HANDLE_PLATFORM_CONVERSION = 0x00000040, // e.g. hwBuffer ycbcr conversion / oes
48     CORE_RESOURCE_HANDLE_ACCELERATION_STRUCTURE = 0x00000080,
49 };
50 using RenderHandleInfoFlags = uint32_t;
51 
52 namespace RenderHandleUtil {
53 static constexpr uint64_t RES_ID_MASK { GPU_RESOURCE_HANDLE_ID_MASK };
54 static constexpr uint64_t RES_HANDLE_ID_MASK { 0x00FFfff0 };
55 static constexpr uint64_t RES_HANDLE_GENERATION_MASK { 0xFF000000 };
56 static constexpr uint64_t RES_HANDLE_HAS_NAME_MASK { 0x1000000000000 };
57 static constexpr uint64_t RES_HANDLE_ADDITIONAL_INFO_MASK { 0xffff00000000 };
58 
59 static constexpr uint64_t RES_HANDLE_ID_SHIFT { 4 };
60 static constexpr uint64_t RES_HANDLE_TYPE_SHIFT { 0 };
61 static constexpr uint64_t RES_HANDLE_GENERATION_SHIFT { 24 };
62 static constexpr uint64_t RES_HANDLE_HAS_NAME_SHIFT { 48 };
63 static constexpr uint64_t RES_HANDLE_ADDITIONAL_INFO_SHIFT { 32 };
64 
65 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
66     const uint32_t index, const uint32_t generationIndex);
67 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
68     const uint32_t index, const uint32_t generationIndex, const uint32_t hasNameId);
69 
70 // only related to GPU resources
IsDynamicResource(const RenderHandle handle)71 inline bool IsDynamicResource(const RenderHandle handle)
72 {
73     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
74            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_DYNAMIC_TRACK;
75 }
IsResetOnFrameBorders(const RenderHandle handle)76 inline bool IsResetOnFrameBorders(const RenderHandle handle)
77 {
78     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
79            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_RESET_ON_FRAME_BORDERS;
80 }
IsDepthImage(const RenderHandle handle)81 inline bool IsDepthImage(const RenderHandle handle)
82 {
83     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
84            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_DEPTH_IMAGE;
85 }
IsDeferredDestroy(const RenderHandle handle)86 inline bool IsDeferredDestroy(const RenderHandle handle)
87 {
88     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
89            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_DEFERRED_DESTROY;
90 }
IsMappableOutsideRenderer(const RenderHandle handle)91 inline bool IsMappableOutsideRenderer(const RenderHandle handle)
92 {
93     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
94            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_MAP_OUTSIDE_RENDERER;
95 }
IsImmediatelyCreated(const RenderHandle handle)96 inline bool IsImmediatelyCreated(const RenderHandle handle)
97 {
98     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
99            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_IMMEDIATELY_CREATED;
100 }
IsPlatformConversionResource(const RenderHandle handle)101 inline bool IsPlatformConversionResource(const RenderHandle handle)
102 {
103     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
104            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_PLATFORM_CONVERSION;
105 }
GetHasNamePart(const RenderHandle handle)106 inline uint32_t GetHasNamePart(const RenderHandle handle)
107 {
108     return (handle.id & RES_HANDLE_HAS_NAME_MASK) >> RES_HANDLE_HAS_NAME_SHIFT;
109 }
IsGpuBuffer(const RenderHandle & handle)110 inline bool IsGpuBuffer(const RenderHandle& handle)
111 {
112     return RenderHandleUtil::GetHandleType(handle) == RenderHandleType::GPU_BUFFER;
113 }
IsGpuImage(const RenderHandle & handle)114 inline bool IsGpuImage(const RenderHandle& handle)
115 {
116     return RenderHandleUtil::GetHandleType(handle) == RenderHandleType::GPU_IMAGE;
117 }
IsGpuSampler(const RenderHandle & handle)118 inline bool IsGpuSampler(const RenderHandle& handle)
119 {
120     return RenderHandleUtil::GetHandleType(handle) == RenderHandleType::GPU_SAMPLER;
121 }
IsGpuAccelerationStructure(const RenderHandle & handle)122 inline bool IsGpuAccelerationStructure(const RenderHandle& handle)
123 {
124     return ((handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
125            RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_ACCELERATION_STRUCTURE;
126 }
127 
128 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index);
129 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex);
130 RenderHandle CreateHandle(
131     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex, const uint32_t additionalData);
132 
GetIndexPart(const RenderHandle handle)133 inline uint32_t GetIndexPart(const RenderHandle handle)
134 {
135     return (handle.id & RES_HANDLE_ID_MASK) >> RES_HANDLE_ID_SHIFT;
136 }
GetIndexPart(const uint64_t handleId)137 inline uint32_t GetIndexPart(const uint64_t handleId)
138 {
139     return (handleId & RES_HANDLE_ID_MASK) >> RES_HANDLE_ID_SHIFT;
140 }
GetGenerationIndexPart(const RenderHandle handle)141 inline uint32_t GetGenerationIndexPart(const RenderHandle handle)
142 {
143     return (handle.id & RES_HANDLE_GENERATION_MASK) >> RES_HANDLE_GENERATION_SHIFT;
144 }
GetGenerationIndexPart(const uint64_t handleId)145 inline uint32_t GetGenerationIndexPart(const uint64_t handleId)
146 {
147     return (handleId & RES_HANDLE_GENERATION_MASK) >> RES_HANDLE_GENERATION_SHIFT;
148 }
149 
GetAdditionalData(const RenderHandle handle)150 inline uint32_t GetAdditionalData(const RenderHandle handle)
151 {
152     return (handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT;
153 }
154 // 0xF (1 << descriptorSetIndex)
GetPipelineLayoutDescriptorSetMask(const RenderHandle handle)155 inline uint32_t GetPipelineLayoutDescriptorSetMask(const RenderHandle handle)
156 {
157     return (handle.id & RES_HANDLE_ADDITIONAL_INFO_MASK) >> RES_HANDLE_ADDITIONAL_INFO_SHIFT;
158 }
159 
IsValid(const EngineResourceHandle & handle)160 inline bool IsValid(const EngineResourceHandle& handle)
161 {
162     return handle.id != INVALID_RESOURCE_HANDLE;
163 }
GetHandleType(const EngineResourceHandle & handle)164 inline RenderHandleType GetHandleType(const EngineResourceHandle& handle)
165 {
166     return GetHandleType(RenderHandle { handle.id });
167 }
GetIndexPart(const EngineResourceHandle & handle)168 inline uint32_t GetIndexPart(const EngineResourceHandle& handle)
169 {
170     return GetIndexPart(RenderHandle { handle.id });
171 }
GetGenerationIndexPart(const EngineResourceHandle & handle)172 inline uint32_t GetGenerationIndexPart(const EngineResourceHandle& handle)
173 {
174     return GetGenerationIndexPart(RenderHandle { handle.id });
175 }
176 EngineResourceHandle CreateEngineResourceHandle(
177     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex);
178 } // namespace RenderHandleUtil
179 RENDER_END_NAMESPACE()
180 
181 BASE_BEGIN_NAMESPACE()
182 template<typename T>
183 uint64_t hash(const T& b);
184 
185 template<>
hash(const RENDER_NS::RenderHandle & handle)186 inline uint64_t hash(const RENDER_NS::RenderHandle& handle)
187 {
188     return handle.id;
189 }
190 BASE_END_NAMESPACE()
191 
192 #endif // DEVICE_GPU_RESOURCE_HANDLE_UTIL_H
193