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 "node_context_descriptor_set_manager_gles.h"
17
18 #include <render/namespace.h>
19
20 #include "util/log.h"
21
22 using namespace BASE_NS;
23
RENDER_BEGIN_NAMESPACE()24 RENDER_BEGIN_NAMESPACE()
25 NodeContextDescriptorSetManagerGLES::NodeContextDescriptorSetManagerGLES(Device& device)
26 : NodeContextDescriptorSetManager(), device_ { device }
27 {
28 PLUGIN_UNUSED(device_);
29 }
30
~NodeContextDescriptorSetManagerGLES()31 NodeContextDescriptorSetManagerGLES::~NodeContextDescriptorSetManagerGLES() {}
32
ResetAndReserve(const DescriptorCounts & descriptorCounts)33 void NodeContextDescriptorSetManagerGLES::ResetAndReserve(const DescriptorCounts& descriptorCounts)
34 {
35 NodeContextDescriptorSetManager::ResetAndReserve(descriptorCounts);
36 }
37
BeginFrame()38 void NodeContextDescriptorSetManagerGLES::BeginFrame()
39 {
40 NodeContextDescriptorSetManager::BeginFrame();
41
42 #if (RENDER_VALIDATION_ENABLED == 1)
43 oneFrameDescSetGeneration_ = (oneFrameDescSetGeneration_ + 1) % MAX_ONE_FRAME_GENERATION_IDX;
44 #endif
45 }
46
CreateDescriptorSet(const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings_)47 RenderHandle NodeContextDescriptorSetManagerGLES::CreateDescriptorSet(
48 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings_)
49 {
50 RenderHandle clientHandle;
51 auto& cpuDescriptorSets = cpuDescriptorSets_[DESCRIPTOR_SET_INDEX_TYPE_STATIC];
52 #if (RENDER_VALIDATION_ENABLED == 1)
53 if (cpuDescriptorSets.size() >= maxSets_) {
54 PLUGIN_LOG_E("RENDER_VALIDATION: No more descriptor sets available");
55 }
56 #endif
57 if (cpuDescriptorSets.size() < maxSets_) {
58 uint32_t dynamicOffsetCount = 0;
59 CpuDescriptorSet newSet;
60 newSet.bindings.reserve(descriptorSetLayoutBindings_.size());
61
62 uint32_t bufferCount = 0;
63 uint32_t imageCount = 0;
64 uint32_t samplerCount = 0;
65 for (const auto& refBinding : descriptorSetLayoutBindings_) {
66 // NOTE: sort from 0 to n
67 newSet.bindings.push_back({ refBinding, {} });
68 if (IsDynamicDescriptor(refBinding.descriptorType)) {
69 dynamicOffsetCount++;
70 }
71
72 const uint32_t descriptorCount = refBinding.descriptorCount;
73 if (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_SAMPLER) {
74 samplerCount += descriptorCount;
75 } else if (((refBinding.descriptorType >= CORE_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
76 (refBinding.descriptorType <= CORE_DESCRIPTOR_TYPE_STORAGE_IMAGE)) ||
77 (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
78 imageCount += descriptorCount;
79 } else if (((refBinding.descriptorType >= CORE_DESCRIPTOR_TYPE_UNIFORM_BUFFER) &&
80 (refBinding.descriptorType <= CORE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) ||
81 (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE)) {
82 bufferCount += descriptorCount;
83 } else {
84 PLUGIN_ASSERT_MSG(false, "descriptor type not found");
85 }
86 }
87 newSet.buffers.resize(bufferCount);
88 newSet.images.resize(imageCount);
89 newSet.samplers.resize(samplerCount);
90
91 const uint32_t arrayIndex = static_cast<uint32_t>(cpuDescriptorSets.size());
92 cpuDescriptorSets.push_back(move(newSet));
93 auto& currCpuDescriptorSet = cpuDescriptorSets[arrayIndex];
94 currCpuDescriptorSet.dynamicOffsetDescriptors.resize(dynamicOffsetCount);
95
96 // NOTE: can be used directly to index
97 clientHandle = RenderHandleUtil::CreateHandle(RenderHandleType::DESCRIPTOR_SET, arrayIndex, 0);
98 }
99 return clientHandle;
100 }
101
CreateOneFrameDescriptorSet(const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings_)102 RenderHandle NodeContextDescriptorSetManagerGLES::CreateOneFrameDescriptorSet(
103 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings_)
104 {
105 RenderHandle clientHandle;
106 auto& cpuDescriptorSets = cpuDescriptorSets_[DESCRIPTOR_SET_INDEX_TYPE_ONE_FRAME];
107
108 uint32_t dynamicOffsetCount = 0;
109 CpuDescriptorSet newSet;
110 newSet.bindings.reserve(descriptorSetLayoutBindings_.size());
111
112 uint32_t bufferCount = 0;
113 uint32_t imageCount = 0;
114 uint32_t samplerCount = 0;
115 for (const auto& refBinding : descriptorSetLayoutBindings_) {
116 // NOTE: sort from 0 to n
117 newSet.bindings.push_back({ refBinding, {} });
118 if (IsDynamicDescriptor(refBinding.descriptorType)) {
119 dynamicOffsetCount++;
120 }
121
122 const uint32_t descriptorCount = refBinding.descriptorCount;
123 if (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_SAMPLER) {
124 samplerCount += descriptorCount;
125 } else if (((refBinding.descriptorType >= CORE_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
126 (refBinding.descriptorType <= CORE_DESCRIPTOR_TYPE_STORAGE_IMAGE)) ||
127 (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
128 imageCount += descriptorCount;
129 } else if (((refBinding.descriptorType >= CORE_DESCRIPTOR_TYPE_UNIFORM_BUFFER) &&
130 (refBinding.descriptorType <= CORE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) ||
131 (refBinding.descriptorType == CORE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE)) {
132 bufferCount += descriptorCount;
133 } else {
134 PLUGIN_ASSERT_MSG(false, "RENDER_VALIDATION_ENABLED: descriptor type not found");
135 }
136 }
137 newSet.buffers.resize(bufferCount);
138 newSet.images.resize(imageCount);
139 newSet.samplers.resize(samplerCount);
140
141 const uint32_t arrayIndex = static_cast<uint32_t>(cpuDescriptorSets.size());
142 cpuDescriptorSets.push_back(move(newSet));
143
144 auto& currCpuDescriptorSet = cpuDescriptorSets[arrayIndex];
145 currCpuDescriptorSet.dynamicOffsetDescriptors.resize(dynamicOffsetCount);
146
147 // NOTE: can be used directly to index
148 clientHandle = RenderHandleUtil::CreateHandle(
149 RenderHandleType::DESCRIPTOR_SET, arrayIndex, oneFrameDescSetGeneration_, ONE_FRAME_DESC_SET_BIT);
150
151 return clientHandle;
152 }
153
UpdateDescriptorSetGpuHandle(const RenderHandle handle)154 void NodeContextDescriptorSetManagerGLES::UpdateDescriptorSetGpuHandle(const RenderHandle handle)
155 {
156 #if (RENDER_VALIDATION_ENABLED == 1)
157 const uint32_t arrayIndex = RenderHandleUtil::GetIndexPart(handle);
158 const uint32_t oneFrameDescBit = RenderHandleUtil::GetAdditionalData(handle);
159 const uint32_t descSetIdx = (oneFrameDescBit == ONE_FRAME_DESC_SET_BIT) ? DESCRIPTOR_SET_INDEX_TYPE_ONE_FRAME
160 : DESCRIPTOR_SET_INDEX_TYPE_STATIC;
161 auto& cpuDescriptorSets = cpuDescriptorSets_[descSetIdx];
162 if (arrayIndex >= static_cast<uint32_t>(cpuDescriptorSets.size())) {
163 PLUGIN_LOG_E("invalid handle in descriptor set management");
164 }
165 if (oneFrameDescBit == ONE_FRAME_DESC_SET_BIT) {
166 const uint32_t generationIndex = RenderHandleUtil::GetGenerationIndexPart(handle);
167 if (generationIndex != oneFrameDescSetGeneration_) {
168 PLUGIN_LOG_E(
169 "RENDER_VALIDATION: invalid one frame descriptor set handle generation. One frame descriptor sets can "
170 "only be used once.");
171 }
172 }
173 #endif
174 }
175
UpdateCpuDescriptorSetPlatform(const DescriptorSetLayoutBindingResources & bindingResources)176 void NodeContextDescriptorSetManagerGLES::UpdateCpuDescriptorSetPlatform(
177 const DescriptorSetLayoutBindingResources& bindingResources)
178 {
179 // no op
180 }
181 RENDER_END_NAMESPACE()
182