• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
32 PLUGIN_STATIC_ASSERT((RES_HANDLE_ADDITIONAL_INFO_MASK >> RES_HANDLE_ADDITIONAL_INFO_SHIFT) >
33                      RenderHandleInfoFlagBits::CORE_RESOURCE_HANDLE_DYNAMIC_ADDITIONAL_STATE);
34 PLUGIN_STATIC_ASSERT(RES_HANDLE_GENERATION_MASK == RENDER_HANDLE_GENERATION_MASK);
35 } // namespace
36 
CreateGpuResourceHandle(const RenderHandleType type,const RenderHandleInfoFlags infoFlags,const uint32_t index,const uint32_t generationIndex)37 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
38     const uint32_t index, const uint32_t generationIndex)
39 {
40 #if (RENDER_VALIDATION_ENABLED == 1)
41     if (index >= HANDLE_ID_SIZE) {
42         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
43     }
44 #endif
45 
46     return { (((uint64_t)infoFlags << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK) |
47              (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
48              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
49              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
50 }
51 
CreateGpuResourceHandle(const RenderHandleType type,const RenderHandleInfoFlags infoFlags,const uint32_t index,const uint32_t generationIndex,const uint32_t hasNameId)52 RenderHandle CreateGpuResourceHandle(const RenderHandleType type, const RenderHandleInfoFlags infoFlags,
53     const uint32_t index, const uint32_t generationIndex, const uint32_t hasNameId)
54 {
55 #if (RENDER_VALIDATION_ENABLED == 1)
56     if (index >= HANDLE_ID_SIZE) {
57         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
58     }
59 #endif
60     return { (((uint64_t)hasNameId << RES_HANDLE_HAS_NAME_SHIFT) & RES_HANDLE_HAS_NAME_MASK) |
61              (((uint64_t)infoFlags << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK) |
62              (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
63              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
64              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
65 }
66 
CreateHandle(const RenderHandleType type,const uint32_t index)67 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index)
68 {
69     return CreateHandle(type, index, 0);
70 }
71 
CreateHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex)72 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
73 {
74 #if (RENDER_VALIDATION_ENABLED == 1)
75     if (index >= HANDLE_ID_SIZE) {
76         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
77     }
78 #endif
79     return { (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
80              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
81              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
82 }
83 
CreateHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex,const uint32_t additionalData)84 RenderHandle CreateHandle(
85     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex, const uint32_t additionalData)
86 {
87     RenderHandle handle = CreateHandle(type, index, generationIndex);
88     handle.id |= (((uint64_t)additionalData << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK);
89     return handle;
90 }
91 
CreateHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex,const uint32_t additionalData,const uint32_t additionalIndex)92 RenderHandle CreateHandle(const RenderHandleType type, const uint32_t index, const uint32_t generationIndex,
93     const uint32_t additionalData, const uint32_t additionalIndex)
94 {
95     RenderHandle handle = CreateHandle(type, index, generationIndex);
96     handle.id |= (((uint64_t)additionalData << RES_HANDLE_ADDITIONAL_INFO_SHIFT) & RES_HANDLE_ADDITIONAL_INFO_MASK);
97     handle.id |= (((uint64_t)additionalIndex << RES_HANDLE_ADDITIONAL_INDEX_SHIFT) & RES_HANDLE_ADDITIONAL_INDEX_MASK);
98     return handle;
99 }
100 
CreateEngineResourceHandle(const RenderHandleType type,const uint32_t index,const uint32_t generationIndex)101 EngineResourceHandle CreateEngineResourceHandle(
102     const RenderHandleType type, const uint32_t index, const uint32_t generationIndex)
103 {
104 #if (RENDER_VALIDATION_ENABLED == 1)
105     if (index >= HANDLE_ID_SIZE) {
106         PLUGIN_LOG_E("index (%u), exceeds max index (%u)", index, static_cast<uint32_t>(HANDLE_ID_SIZE));
107     }
108 #endif
109     return { (((uint64_t)generationIndex << RES_HANDLE_GENERATION_SHIFT) & RES_HANDLE_GENERATION_MASK) |
110              ((uint64_t)((index << RES_HANDLE_ID_SHIFT) & RES_HANDLE_ID_MASK)) |
111              ((uint64_t)(type)&RENDER_HANDLE_TYPE_MASK) };
112 }
113 } // namespace RenderHandleUtil
114 RENDER_END_NAMESPACE()
115