-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_execution_unit.cpp
194 lines (178 loc) · 6.97 KB
/
command_execution_unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include "command_execution_unit.hpp"
#include "logical_device.hpp"
namespace vkdd {
struct CommandBufferPool
{
vk::UniqueCommandPool m_commandPool;
std::vector<vk::UniqueCommandBuffer> m_commandBuffers;
uint32_t m_nextCommandBufferIndex;
};
struct QueueFamilyIndexData
{
vk::UniqueFence m_syncFence;
std::unordered_map<std::thread::id, CommandBufferPool> m_perThreadCommandBufferPools;
};
struct CommandBufferInfo
{
vk::CommandBufferSubmitInfo m_commandBufferInfo;
std::vector<vk::SemaphoreSubmitInfo> m_waitSemaphoreInfos;
std::vector<vk::SemaphoreSubmitInfo> m_signalSemaphoreInfos;
};
CommandExecutionUnit::CommandExecutionUnit(LogicalDevice& logicalDevice)
: m_logicalDevice(logicalDevice)
{
}
CommandExecutionUnit::~CommandExecutionUnit() {}
vk::Result CommandExecutionUnit::waitForIdle()
{
std::vector<vk::Fence> fences;
for(auto const& it : m_library)
{
fences.emplace_back(it.second.m_syncFence.get());
}
if(fences.empty())
{
return vk::Result::eSuccess;
}
vk::Result result = m_logicalDevice.vkDevice().waitForFences(fences, true, std::numeric_limits<uint64_t>::max());
m_logicalDevice.vkDevice().resetFences(fences);
return result;
}
void CommandExecutionUnit::waitForIdleAndReset()
{
this->waitForIdle();
for(auto const& queueIt : m_library)
{
for(auto const& threadIt : queueIt.second.m_perThreadCommandBufferPools)
{
m_logicalDevice.vkDevice().resetCommandPool(threadIt.second.m_commandPool.get());
}
}
}
std::vector<vk::CommandBuffer> CommandExecutionUnit::requestCommandBuffers(std::vector<uint32_t> queueFamilyIndices,
std::optional<DeviceMask> deviceMask)
{
std::vector<vk::CommandBuffer> cmdBuffers;
if(!queueFamilyIndices.empty())
{
std::lock_guard guard(m_mutex);
for(uint32_t queueFamilyIndex : queueFamilyIndices)
{
cmdBuffers.emplace_back(this->requestCommandBufferUnguarded(queueFamilyIndex, deviceMask));
}
}
return cmdBuffers;
}
vk::CommandBuffer CommandExecutionUnit::requestCommandBuffer(uint32_t queueFamilyIndex, std::optional<DeviceMask> deviceMask)
{
std::lock_guard guard(m_mutex);
return this->requestCommandBufferUnguarded(queueFamilyIndex, deviceMask);
}
vk::CommandBuffer CommandExecutionUnit::requestCommandBufferUnguarded(uint32_t queueFamilyIndex, std::optional<DeviceMask> deviceMask)
{
CommandBufferPool* cbp = nullptr;
if(auto findQueueIt = m_library.find(queueFamilyIndex); findQueueIt != m_library.end())
{
if(auto findThreadIt = findQueueIt->second.m_perThreadCommandBufferPools.find(std::this_thread::get_id());
findThreadIt != findQueueIt->second.m_perThreadCommandBufferPools.end())
{
cbp = &findThreadIt->second;
}
}
if(!cbp)
{
m_library[queueFamilyIndex].m_syncFence = m_logicalDevice.vkDevice().createFenceUnique({});
cbp = &(m_library[queueFamilyIndex].m_perThreadCommandBufferPools[std::this_thread::get_id()] = {
m_logicalDevice.vkDevice().createCommandPoolUnique({{}, queueFamilyIndex})});
}
if(cbp->m_commandBuffers.size() <= cbp->m_nextCommandBufferIndex)
{
vk::CommandBufferAllocateInfo graphicsCommandBufferAllocateInfo(cbp->m_commandPool.get(), vk::CommandBufferLevel::ePrimary, 1);
cbp->m_commandBuffers.emplace_back(
std::move(m_logicalDevice.vkDevice().allocateCommandBuffersUnique(graphicsCommandBufferAllocateInfo)[0]));
}
vk::CommandBuffer cmdBuffer = cbp->m_commandBuffers[cbp->m_nextCommandBufferIndex++].get();
m_commandBufferInfos[cmdBuffer] = {{cmdBuffer, deviceMask.value_or(DeviceMask())}, {}, {}};
m_submitOrder[queueFamilyIndex].emplace_back(cmdBuffer);
return cmdBuffer;
}
void CommandExecutionUnit::pushWaits(vk::CommandBuffer cmdBuffer, std::vector<vk::SemaphoreSubmitInfo> const& waitSemaphoreInfos)
{
if(!waitSemaphoreInfos.empty())
{
std::lock_guard guard(m_mutex);
auto findIt = m_commandBufferInfos.find(cmdBuffer);
if(findIt == m_commandBufferInfos.end())
{
LOGE("Unknown command buffer given.\n");
return;
}
findIt->second.m_waitSemaphoreInfos.insert(findIt->second.m_waitSemaphoreInfos.end(), waitSemaphoreInfos.begin(),
waitSemaphoreInfos.end());
}
}
void CommandExecutionUnit::pushWait(vk::CommandBuffer cmdBuffer, vk::SemaphoreSubmitInfo waitSemaphoreInfo)
{
this->pushWaits(cmdBuffer, {waitSemaphoreInfo});
}
void CommandExecutionUnit::pushSignals(vk::CommandBuffer cmdBuffer, std::vector<vk::SemaphoreSubmitInfo> const& signalSemaphoreInfos)
{
if(!signalSemaphoreInfos.empty())
{
std::lock_guard guard(m_mutex);
auto findIt = m_commandBufferInfos.find(cmdBuffer);
if(findIt == m_commandBufferInfos.end())
{
LOGE("Unknown command buffer given.\n");
return;
}
findIt->second.m_signalSemaphoreInfos.insert(findIt->second.m_signalSemaphoreInfos.end(),
signalSemaphoreInfos.begin(), signalSemaphoreInfos.end());
}
}
void CommandExecutionUnit::pushSignal(vk::CommandBuffer cmdBuffer, vk::SemaphoreSubmitInfo signalSemaphoreInfo)
{
this->pushSignals(cmdBuffer, {signalSemaphoreInfo});
}
void CommandExecutionUnit::submit()
{
for(auto const& it : m_submitOrder)
{
vk::Queue queue = m_logicalDevice.getQueue(it.first);
// for(uint32_t i = 0; i < it.second.size(); ++i)
// {
// CommandBufferInfo const& info = m_commandBufferInfos[it.second[i]];
// vk::SubmitInfo2 submit({}, info.m_waitSemaphoreInfos, info.m_commandBufferInfo, info.m_signalSemaphoreInfos);
// LOGI("%d\n", i);
// queue.submit2(submit, it.first == m_syncFenceQueueFamilyIndex && i == it.second.size() - 1 ? m_syncFence.get() : nullptr);
// }
std::vector<vk::SubmitInfo2> submits;
for(vk::CommandBuffer cmdBuffer : it.second)
{
CommandBufferInfo const& info = m_commandBufferInfos[cmdBuffer];
submits.emplace_back(vk::SubmitInfo2({}, info.m_waitSemaphoreInfos, info.m_commandBufferInfo, info.m_signalSemaphoreInfos));
}
queue.submit2(submits, m_library[it.first].m_syncFence.get());
}
m_submitOrder.clear();
m_commandBufferInfos.clear();
}
} // namespace vkdd