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 "device/device.h"
21 #include "util/log.h"
22
23 using namespace BASE_NS;
24
25 RENDER_BEGIN_NAMESPACE()
26 namespace {
CreateCpuDescriptorSetData(const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)27 CpuDescriptorSet CreateCpuDescriptorSetData(
28 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)
29 {
30 uint32_t dynamicOffsetCount = 0;
31 CpuDescriptorSet newSet;
32 newSet.bindings.reserve(descriptorSetLayoutBindings.size());
33 LowLevelDescriptorCounts descriptorCounts;
34 for (const auto& refBinding : descriptorSetLayoutBindings) {
35 // NOTE: sort from 0 to n
36 newSet.bindings.push_back({ refBinding, {} });
37 NodeContextDescriptorSetManager::IncreaseDescriptorSetCounts(refBinding, descriptorCounts, dynamicOffsetCount);
38 }
39 newSet.buffers.resize(descriptorCounts.bufferCount);
40 newSet.images.resize(descriptorCounts.imageCount);
41 newSet.samplers.resize(descriptorCounts.samplerCount);
42
43 newSet.dynamicOffsetDescriptors.resize(dynamicOffsetCount);
44 return newSet;
45 }
46 } // namespace
47
DescriptorSetManagerGles(Device & device)48 DescriptorSetManagerGles::DescriptorSetManagerGles(Device& device) : DescriptorSetManager(device) {}
49
BeginFrame()50 void DescriptorSetManagerGles::BeginFrame()
51 {
52 DescriptorSetManager::BeginFrame();
53 }
54
BeginBackendFrame()55 void DescriptorSetManagerGles::BeginBackendFrame()
56 {
57 // handle write locking
58 // handle possible destruction
59 for (const auto& descriptorSet : descriptorSets_) {
60 if (GlobalDescriptorSetBase* descriptorSetBase = descriptorSet.get(); descriptorSetBase) {
61 bool destroyDescriptorSets = true;
62 // if we have any descriptor sets in use we do not destroy the pool
63 for (auto& ref : descriptorSetBase->data) {
64 if (ref.renderHandleReference.GetRefCount() > 1) {
65 destroyDescriptorSets = false;
66 }
67 ref.frameWriteLocked = false;
68 }
69
70 if (destroyDescriptorSets) {
71 if (!descriptorSetBase->data.empty()) {
72 const RenderHandle handle = descriptorSetBase->data[0U].renderHandleReference.GetHandle();
73 // set handle (index location) to be available
74 availableHandles_.push_back(handle);
75 }
76 nameToIndex_.erase(descriptorSetBase->name);
77 *descriptorSetBase = {};
78 }
79 }
80 }
81 }
82
CreateDescriptorSets(const uint32_t arrayIndex,const uint32_t descriptorSetCount,const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)83 void DescriptorSetManagerGles::CreateDescriptorSets(const uint32_t arrayIndex, const uint32_t descriptorSetCount,
84 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)
85 {
86 PLUGIN_ASSERT((arrayIndex < descriptorSets_.size()) && (descriptorSets_[arrayIndex]));
87 PLUGIN_ASSERT(descriptorSets_[arrayIndex]->data.size() == descriptorSetCount);
88 if ((arrayIndex < descriptorSets_.size()) && (descriptorSets_[arrayIndex])) {
89 GlobalDescriptorSetBase* cpuData = descriptorSets_[arrayIndex].get();
90 for (uint32_t idx = 0; idx < descriptorSetCount; ++idx) {
91 const uint32_t additionalIndex = idx;
92 cpuData->data[additionalIndex].cpuDescriptorSet = CreateCpuDescriptorSetData(descriptorSetLayoutBindings);
93 }
94 }
95 }
96
UpdateDescriptorSetGpuHandle(const RenderHandle & handle)97 void DescriptorSetManagerGles::UpdateDescriptorSetGpuHandle(const RenderHandle& handle)
98 {
99 // vulkan runs the inherited method, gles does not need it
100 #if (RENDER_VALIDATION_ENABLED == 1)
101 const uint32_t arrayIndex = RenderHandleUtil::GetIndexPart(handle);
102 PLUGIN_ASSERT(
103 RenderHandleUtil::GetAdditionalData(handle) == NodeContextDescriptorSetManager::GLOBAL_DESCRIPTOR_BIT);
104
105 if (arrayIndex >= static_cast<uint32_t>(descriptorSets_.size())) {
106 PLUGIN_LOG_E("invalid handle in descriptor set management");
107 }
108 #endif
109 }
110
UpdateCpuDescriptorSetPlatform(const DescriptorSetLayoutBindingResources & bindingResources)111 void DescriptorSetManagerGles::UpdateCpuDescriptorSetPlatform(
112 const DescriptorSetLayoutBindingResources& bindingResources)
113 {
114 // no op
115 }
116
NodeContextDescriptorSetManagerGles(Device & device)117 NodeContextDescriptorSetManagerGles::NodeContextDescriptorSetManagerGles(Device& device)
118 : NodeContextDescriptorSetManager(device), device_ { device }
119 {
120 PLUGIN_UNUSED(device_);
121 }
122
123 NodeContextDescriptorSetManagerGles::~NodeContextDescriptorSetManagerGles() = default;
124
ResetAndReserve(const DescriptorCounts & descriptorCounts)125 void NodeContextDescriptorSetManagerGles::ResetAndReserve(const DescriptorCounts& descriptorCounts)
126 {
127 NodeContextDescriptorSetManager::ResetAndReserve(descriptorCounts);
128 }
129
BeginFrame()130 void NodeContextDescriptorSetManagerGles::BeginFrame()
131 {
132 NodeContextDescriptorSetManager::BeginFrame();
133
134 #if (RENDER_VALIDATION_ENABLED == 1)
135 oneFrameDescSetGeneration_ = (oneFrameDescSetGeneration_ + 1) % MAX_ONE_FRAME_GENERATION_IDX;
136 #endif
137 }
138
CreateDescriptorSet(const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)139 RenderHandle NodeContextDescriptorSetManagerGles::CreateDescriptorSet(
140 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)
141 {
142 RenderHandle clientHandle;
143 auto& cpuDescriptorSets = cpuDescriptorSets_[DESCRIPTOR_SET_INDEX_TYPE_STATIC];
144 #if (RENDER_VALIDATION_ENABLED == 1)
145 if (cpuDescriptorSets.size() >= maxSets_) {
146 PLUGIN_LOG_E("RENDER_VALIDATION: No more descriptor sets available");
147 }
148 #endif
149 if (cpuDescriptorSets.size() < maxSets_) {
150 const auto arrayIndex = (uint32_t)cpuDescriptorSets.size();
151 cpuDescriptorSets.push_back(CreateCpuDescriptorSetData(descriptorSetLayoutBindings));
152
153 // NOTE: can be used directly to index
154 clientHandle = RenderHandleUtil::CreateHandle(RenderHandleType::DESCRIPTOR_SET, arrayIndex, 0);
155 }
156 return clientHandle;
157 }
158
CreateOneFrameDescriptorSet(const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)159 RenderHandle NodeContextDescriptorSetManagerGles::CreateOneFrameDescriptorSet(
160 const array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings)
161 {
162 RenderHandle clientHandle;
163 auto& cpuDescriptorSets = cpuDescriptorSets_[DESCRIPTOR_SET_INDEX_TYPE_ONE_FRAME];
164
165 const auto arrayIndex = static_cast<uint32_t>(cpuDescriptorSets.size());
166 cpuDescriptorSets.push_back(CreateCpuDescriptorSetData(descriptorSetLayoutBindings));
167
168 // NOTE: can be used directly to index
169 clientHandle = RenderHandleUtil::CreateHandle(
170 RenderHandleType::DESCRIPTOR_SET, arrayIndex, oneFrameDescSetGeneration_, ONE_FRAME_DESC_SET_BIT);
171
172 return clientHandle;
173 }
174
UpdateDescriptorSetGpuHandle(const RenderHandle handle)175 void NodeContextDescriptorSetManagerGles::UpdateDescriptorSetGpuHandle(const RenderHandle handle)
176 {
177 #if (RENDER_VALIDATION_ENABLED == 1)
178 const uint32_t arrayIndex = RenderHandleUtil::GetIndexPart(handle);
179 const uint32_t oneFrameDescBit = RenderHandleUtil::GetAdditionalData(handle);
180 const uint32_t descSetIdx = (oneFrameDescBit & ONE_FRAME_DESC_SET_BIT) ? DESCRIPTOR_SET_INDEX_TYPE_ONE_FRAME
181 : DESCRIPTOR_SET_INDEX_TYPE_STATIC;
182 auto& cpuDescriptorSets = cpuDescriptorSets_[descSetIdx];
183 if (arrayIndex >= static_cast<uint32_t>(cpuDescriptorSets.size())) {
184 PLUGIN_LOG_E("invalid handle in descriptor set management");
185 }
186 if (oneFrameDescBit & ONE_FRAME_DESC_SET_BIT) {
187 const uint32_t generationIndex = RenderHandleUtil::GetGenerationIndexPart(handle);
188 if (generationIndex != oneFrameDescSetGeneration_) {
189 PLUGIN_LOG_E(
190 "RENDER_VALIDATION: invalid one frame descriptor set handle generation. One frame descriptor sets can "
191 "only be used once.");
192 }
193 }
194 #endif
195 }
196
UpdateCpuDescriptorSetPlatform(const DescriptorSetLayoutBindingResources & bindingResources)197 void NodeContextDescriptorSetManagerGles::UpdateCpuDescriptorSetPlatform(
198 const DescriptorSetLayoutBindingResources& bindingResources)
199 {
200 // no op
201 }
202
203 RENDER_END_NAMESPACE()
204