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 { (((uint64_t)infoFlags << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK) |
43              (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
44              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
45              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
46 }
47 
CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags, const uint32_t index, const uint32_t generationIndex, const uint32_t hasNameId)48 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
49     const uint32_t index, const uint32_t generationIndex, const uint32_t hasNameId)
50 {
51 #if (RENDER_VALIDATION_ENABLED == 1)
52     if (index >= HANDLE_ID_SIZE) {
53         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
54     }
55 #endif
56     return { (((uint64_t)hasNameId << RES_HANDLE_HAS_NAME_SHIFT) & RES_HANDLE_HAS_NAME_MASK) |
57              (((uint64_t)infoFlags << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK) |
58              (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
59              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
60              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
61 }
62 
CreateHandle(const RenderHandleType type, const uint32_t index)63 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index)
64 {
65     return CreateHandle(type, index, 0);
66 }
67 
CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)68 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
69 {
70 #if (RENDER_VALIDATION_ENABLED == 1)
71     if (index >= HANDLE_ID_SIZE) {
72         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
73     }
74 #endif
75     return { (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
76              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
77              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
78 }
79 
CreateHandle( const RenderHandleType type, const uint32_t index, const uint32_t generationIndex, const uint32_t additionalData)80 RenderHandle CreateHandle(
81     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex, const uint32_t additionalData)
82 {
83     RenderHandle handle = CreateHandle(type, index, generationIndex);
84     handle.id |= (((uint64_t)additionalData << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK);
85     return handle;
86 }
87 
CreateEngineResourceHandle( const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)88 EngineResourceHandle CreateEngineResourceHandle(
89     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
90 {
91 #if (RENDER_VALIDATION_ENABLED == 1)
92     if (index >= HANDLE_ID_SIZE) {
93         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
94     }
95 #endif
96     return { (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
97              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
98              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
99 }
100 } // namespace RenderHandleUtil
101 RENDER_END_NAMESPACE()
102