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 #include "gpu_resource_handle_util.h"
17 
18 #include <render/namespace.h>
19 #include <render/resource_handle.h>
20 
21 #include "util/log.h"
22 
23 RENDER_BEGIN_NAMESPACE()
24 
25 // Internal RendererDataHandleUtil API implementation
26 namespace RenderHandleUtil {
27 namespace {
28 #if (RENDER_VALIDATION_ENABLED == 1)
29 constexpr uint64_t HANDLE_ID_SIZE { RES_HANDLE_ID_MASK >> RES_HANDLE_ID_SHIFT };
30 #endif
31 } // namespace
32 
CreateGpuResourceHandle(const RenderHandleType type,const RenderHandleInfoFlags infoFlags,const uint32_t index,const uint32_t generationIndex)33 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
34     const uint32_t index, const uint32_t generationIndex)
35 {
36 #if (RENDER_VALIDATION_ENABLED == 1)
37     if (index >= HANDLE_ID_SIZE) {
38         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
39     }
40 #endif
41 
42     return { ((static_cast<uint64_t>(infoFlags) << RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
43              RES_HANDLE_ADDITIONAL_INFO_MASK) |
44              ((static_cast<uint64_t>(generationIndex) << RES_HANDLE_GENERATION_SHIFT) &
45              RES_HANDLE_GENERATION_MASK) |
46              (static_cast<uint64_t>((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
47              (static_cast<uint64_t>(type) & RENDER_HANDLE_TYPE_MASK) };
48 }
49 
CreateGpuResourceHandle(const RenderHandleType type,const RenderHandleInfoFlags infoFlags,const uint32_t index,const uint32_t generationIndex,const uint32_t hasNameId)50 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
51     const uint32_t index, const uint32_t generationIndex, const uint32_t hasNameId)
52 {
53 #if (RENDER_VALIDATION_ENABLED == 1)
54     if (index >= HANDLE_ID_SIZE) {
55         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
56     }
57 #endif
58     return { ((static_cast<uint64_t>(hasNameId) << RES_HANDLE_HAS_NAME_SHIFT) & RES_HANDLE_HAS_NAME_MASK) |
59              ((static_cast<uint64_t>(infoFlags) << RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
60              RES_HANDLE_ADDITIONAL_INFO_MASK) |
61              ((static_cast<uint64_t>(generationIndex) << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
62              (static_cast<uint64_t>((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
63              (static_cast<uint64_t>(type) & RENDER_HANDLE_TYPE_MASK) };
64 }
65 
CreateHandle(const RenderHandleType type,const uint32_t index)66 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index)
67 {
68     return CreateHandle(type, index, 0);
69 }
70 
CreateHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex)71 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
72 {
73 #if (RENDER_VALIDATION_ENABLED == 1)
74     if (index >= HANDLE_ID_SIZE) {
75         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
76     }
77 #endif
78     return { ((static_cast<uint64_t>(generationIndex) << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
79              (static_cast<uint64_t>((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
80              (static_cast<uint64_t>(type) & RENDER_HANDLE_TYPE_MASK) };
81 }
82 
CreateHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex,const uint32_t additionalData)83 RenderHandle CreateHandle(
84     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex, const uint32_t additionalData)
85 {
86     RenderHandle handle = CreateHandle(type, index, generationIndex);
87     handle.id |= ((static_cast<uint64_t>(additionalData) << RES_HANDLE_ADDITIONAL_INFO_SHIFT) &
88         RES_HANDLE_ADDITIONAL_INFO_MASK);
89     return handle;
90 }
91 
CreateEngineResourceHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex)92 EngineResourceHandle CreateEngineResourceHandle(
93     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
94 {
95 #if (RENDER_VALIDATION_ENABLED == 1)
96     if (index >= HANDLE_ID_SIZE) {
97         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
98     }
99 #endif
100     return { ((static_cast<uint64_t>(generationIndex) << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
101              (static_cast<uint64_t>((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
102              (static_cast<uint64_t>(type) & RENDER_HANDLE_TYPE_MASK) };
103 }
104 } // namespace RenderHandleUtil
105 RENDER_END_NAMESPACE()
106