-
Notifications
You must be signed in to change notification settings - Fork 1
/
logical_device.cpp
495 lines (458 loc) · 23.8 KB
/
logical_device.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* 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 "logical_device.hpp"
#include "command_execution_unit.hpp"
#include "logical_display.hpp"
#include "canvas_region_render_thread.hpp"
#include "vulkan_memory_object_uploader.hpp"
#include <math.h>
#include <unordered_set>
#include "_autogen/donut.vert.h"
#include "_autogen/donut.frag.h"
namespace vkdd {
struct DeallocationContainer
{
FrameIndex m_frameIndex;
VulkanMemoryPool::Allocation m_rawAllocation;
BufferAllocation m_bufferAllocation;
ImageAllocation m_imageAllocation;
bool operator<(DeallocationContainer const& other) const { return m_frameIndex < other.m_frameIndex; }
};
LogicalDevice::LogicalDevice(vk::Instance instance, uint32_t devGroupIdx)
: m_instance(instance)
, m_devGroupIdx(devGroupIdx)
, m_frameIndex(0)
{
std::vector<vk::PhysicalDeviceGroupProperties> devGroups = m_instance.enumeratePhysicalDeviceGroups();
assert(m_devGroupIdx < devGroups.size());
vk::PhysicalDeviceGroupProperties devGroup = devGroups[m_devGroupIdx];
for(uint32_t i = 0; i < devGroup.physicalDeviceCount; ++i)
{
m_physicalDevices.emplace_back(devGroup.physicalDevices[i]);
}
m_perSubDeviceMemPools = std::vector<MemPoolCollection>(m_physicalDevices.size());
}
LogicalDevice::~LogicalDevice() {}
LogicalDisplay* LogicalDevice::enableDisplay(class Scene const& scene, vk::DisplayKHR display, CanvasRegion displayRegionOnCanvas)
{
for(UniqueLogicalDisplay const& logicalDisplay : m_logicalDisplays)
{
if(logicalDisplay->getDisplay() == display)
{
LOGE("Tried to enable display twice.\n");
return nullptr;
}
}
// vk_ddisplay
// first find all physical device indices which report the desired display
std::vector<DeviceIndex> deviceIndices;
for(DeviceIndex devIdx = 0; devIdx < m_physicalDevices.size(); ++devIdx)
{
vk::PhysicalDevice dev = m_physicalDevices[devIdx];
std::vector<vk::DisplayPropertiesKHR> displays = dev.getDisplayPropertiesKHR();
for(vk::DisplayPropertiesKHR dispProps : displays)
{
if(dispProps.display == display)
{
deviceIndices.emplace_back(devIdx);
break;
}
}
}
if(deviceIndices.empty())
{
LOGE("Given display is not connected to any physical device of this device group.");
return nullptr;
}
// vk_ddisplay
// create the logical display and provide the sub device indices
UniqueLogicalDisplay logicalDisplay = std::make_unique<LogicalDisplay>(*this, display, displayRegionOnCanvas);
if(!logicalDisplay->init(scene, deviceIndices))
{
LOGE("Initialization of logical display failed.\n");
return nullptr;
}
return m_logicalDisplays.emplace_back(std::move(logicalDisplay)).get();
}
void LogicalDevice::createDonutPipeline()
{
m_donutVert = m_device->createShaderModuleUnique({{}, sizeof(donut_vert), donut_vert});
m_donutFrag = m_device->createShaderModuleUnique({{}, sizeof(donut_frag), donut_frag});
vk::PipelineCacheCreateInfo pipelineCacheCreateInfo({});
m_donutPipelineCache = m_device->createPipelineCacheUnique(pipelineCacheCreateInfo);
vk::PipelineShaderStageCreateInfo donutVertStage({}, vk::ShaderStageFlagBits::eVertex, m_donutVert.get(), "main");
vk::PipelineShaderStageCreateInfo donutFragStage({}, vk::ShaderStageFlagBits::eFragment, m_donutFrag.get(), "main");
std::vector<vk::PipelineShaderStageCreateInfo> stages = {donutVertStage, donutFragStage};
vk::VertexInputBindingDescription perVertexBindingDesc(0, sizeof(DefaultVertex), vk::VertexInputRate::eVertex);
vk::VertexInputBindingDescription perInstanceBindingDesc(1, sizeof(DefaultInstance), vk::VertexInputRate::eInstance);
std::vector<vk::VertexInputBindingDescription> vertexInputBindingDescs = {perVertexBindingDesc, perInstanceBindingDesc};
vk::VertexInputAttributeDescription vertexPosDesc(0, 0, vk::Format::eR32G32B32Sfloat, 0 * sizeof(float));
vk::VertexInputAttributeDescription vertexNormalDesc(1, 0, vk::Format::eR32G32B32Sfloat, 3 * sizeof(float));
vk::VertexInputAttributeDescription vertexTexDesc(2, 0, vk::Format::eR32G32Sfloat, 6 * sizeof(float));
vk::VertexInputAttributeDescription instanceModel0Desc(3, 1, vk::Format::eR32G32B32A32Sfloat, 0 * sizeof(float));
vk::VertexInputAttributeDescription instanceModel1Desc(4, 1, vk::Format::eR32G32B32A32Sfloat, 4 * sizeof(float));
vk::VertexInputAttributeDescription instanceModel2Desc(5, 1, vk::Format::eR32G32B32A32Sfloat, 8 * sizeof(float));
vk::VertexInputAttributeDescription instanceModel3Desc(6, 1, vk::Format::eR32G32B32A32Sfloat, 12 * sizeof(float));
vk::VertexInputAttributeDescription instanceInvModel0Desc(7, 1, vk::Format::eR32G32B32A32Sfloat, 16 * sizeof(float));
vk::VertexInputAttributeDescription instanceInvModel1Desc(8, 1, vk::Format::eR32G32B32A32Sfloat, 20 * sizeof(float));
vk::VertexInputAttributeDescription instanceInvModel2Desc(9, 1, vk::Format::eR32G32B32A32Sfloat, 24 * sizeof(float));
vk::VertexInputAttributeDescription instanceInvModel3Desc(10, 1, vk::Format::eR32G32B32A32Sfloat, 28 * sizeof(float));
vk::VertexInputAttributeDescription instanceColorGradientIdxDesc(11, 1, vk::Format::eR32Uint, 32 * sizeof(float));
vk::VertexInputAttributeDescription instanceShellHeightDesc(12, 1, vk::Format::eR32Sfloat, 33 * sizeof(float));
vk::VertexInputAttributeDescription instanceExtrusionDesc(13, 1, vk::Format::eR32Sfloat, 34 * sizeof(float));
std::vector<vk::VertexInputAttributeDescription> vertexInputAttributeDescs = {
vertexPosDesc, vertexNormalDesc, vertexTexDesc, instanceModel0Desc,
instanceModel1Desc, instanceModel2Desc, instanceModel3Desc, instanceInvModel0Desc,
instanceInvModel1Desc, instanceInvModel2Desc, instanceInvModel3Desc, instanceColorGradientIdxDesc,
instanceShellHeightDesc, instanceExtrusionDesc};
vk::PipelineVertexInputStateCreateInfo vertexInputState({}, vertexInputBindingDescs, vertexInputAttributeDescs);
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState({}, vk::PrimitiveTopology::eTriangleStrip, true);
vk::PipelineViewportStateCreateInfo viewportState({}, 1, nullptr, 1, nullptr);
vk::PipelineRasterizationStateCreateInfo rasterizationState({}, false, false, vk::PolygonMode::eFill,
vk::CullModeFlagBits::eNone, vk::FrontFace::eCounterClockwise);
rasterizationState.setLineWidth(1.0f);
vk::PipelineMultisampleStateCreateInfo multisampleState({}, vk::SampleCountFlagBits::e1);
vk::PipelineDepthStencilStateCreateInfo depthStencilState({}, true, true, vk::CompareOp::eLess, false, false);
vk::PipelineColorBlendAttachmentState attachment;
attachment.setColorWriteMask(vk::FlagTraits<vk::ColorComponentFlagBits>::allFlags);
vk::PipelineColorBlendStateCreateInfo colorBlendState({}, false, vk::LogicOp::eClear, attachment);
std::vector<vk::DynamicState> dynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor};
vk::PipelineDynamicStateCreateInfo dynamicState({}, dynamicStates);
vk::PushConstantRange pushConstantRange(vk::ShaderStageFlagBits::eVertex, 0, sizeof(GlobalData));
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo({}, {}, pushConstantRange);
m_donutPipelineLayout = m_device->createPipelineLayoutUnique(pipelineLayoutCreateInfo);
vk::GraphicsPipelineCreateInfo donutPipelineCreateInfo({}, stages, &vertexInputState, &inputAssemblyState, {},
&viewportState, &rasterizationState, &multisampleState,
&depthStencilState, &colorBlendState, &dynamicState,
m_donutPipelineLayout.get(), m_donutRenderPass.get(), 0);
auto donutPipelineResult = m_device->createGraphicsPipelineUnique(m_donutPipelineCache.get(), donutPipelineCreateInfo);
assert(donutPipelineResult.result == vk::Result::eSuccess);
m_donutPipeline = std::move(donutPipelineResult.value);
}
TriangleMesh* LogicalDevice::getDonutTriangleMesh(DeviceIndex deviceIndex, uint32_t baseNumTesselations)
{
std::lock_guard guard(m_donutTriMeshesMtx);
auto findIt = m_donutTriMeshes[deviceIndex].find(baseNumTesselations);
if(findIt != m_donutTriMeshes[deviceIndex].end())
{
return findIt->second.get();
}
TriangleMesh* triMesh = m_donutTriMeshes[deviceIndex]
.emplace(baseNumTesselations, std::make_unique<TriangleMesh>(*this, deviceIndex))
.first->second.get();
triMesh->buildTorus(baseNumTesselations, 2 * baseNumTesselations);
return triMesh;
}
MemTypeIndex LogicalDevice::getMemoryTypeIndex(DeviceIndex deviceIndex, uint32_t memoryTypeBits, vk::MemoryPropertyFlags memPropFlags)
{
vk::PhysicalDeviceMemoryProperties memProps = m_physicalDevices[deviceIndex].getMemoryProperties();
for(uint32_t i = 0; i < memProps.memoryTypeCount; ++i)
{
if((memoryTypeBits & (1 << i)) && (memProps.memoryTypes[i].propertyFlags & memPropFlags) == memPropFlags)
{
return i;
}
}
LOGE("Failed to allocate device memory.\n");
return (uint32_t)-1;
}
VulkanMemoryPool::Allocation LogicalDevice::allocateHostVisibleDeviceMemory(vk::MemoryRequirements memReqs,
void const* initialData,
size_t initialDataSize)
{
vk::MemoryPropertyFlags memPropFlags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent;
VulkanMemoryPool::Allocation allocation = this->allocateDeviceMemory({}, memReqs, memPropFlags);
if(initialData != nullptr && initialDataSize != 0)
{
void* mapped = m_device->mapMemory(allocation.devMem(), allocation.devMemOffset(), initialDataSize);
memcpy(mapped, initialData, initialDataSize);
m_device->unmapMemory(allocation.devMem());
}
return std::move(allocation);
}
VulkanMemoryPool::Allocation LogicalDevice::allocateStagingMemory(size_t size, size_t alignment)
{
return m_stagingMemPool->alloc(size, alignment);
}
VulkanMemoryPool::Allocation LogicalDevice::allocateDeviceMemory(OptionalDeviceIndex deviceIndex,
vk::MemoryRequirements memReqs,
vk::MemoryPropertyFlags memPropFlags)
{
MemTypeIndex memTypeIdx = this->getMemoryTypeIndex(deviceIndex.value_or(0), memReqs.memoryTypeBits, memPropFlags);
return this->getMemPool(deviceIndex, memTypeIdx)->alloc(memReqs.size, memReqs.alignment);
}
BufferAllocation LogicalDevice::allocateBuffer(OptionalDeviceIndex deviceIndex, vk::BufferCreateInfo createInfo, vk::MemoryPropertyFlags memPropFlags)
{
vk::UniqueBuffer buffer = m_device->createBufferUnique(createInfo);
vk::MemoryRequirements2 memReqs = m_device->getBufferMemoryRequirements(buffer.get());
VulkanMemoryPool::Allocation allocation = this->allocateDeviceMemory(deviceIndex, memReqs.memoryRequirements, memPropFlags);
m_device->bindBufferMemory(buffer.get(), allocation.devMem(), allocation.devMemOffset());
return {std::move(buffer), std::move(allocation)};
}
ImageAllocation LogicalDevice::allocateImage(OptionalDeviceIndex deviceIndex, vk::ImageCreateInfo createInfo, vk::MemoryPropertyFlags memPropFlags)
{
vk::UniqueImage image = m_device->createImageUnique(createInfo);
vk::MemoryRequirements2 memReqs = m_device->getImageMemoryRequirements(image.get());
VulkanMemoryPool::Allocation allocation = this->allocateDeviceMemory(deviceIndex, memReqs.memoryRequirements, memPropFlags);
m_device->bindImageMemory(image.get(), allocation.devMem(), allocation.devMemOffset());
return {std::move(image), std::move(allocation)};
}
BufferAllocation LogicalDevice::allocateStagingBuffer(vk::BufferCreateInfo createInfo)
{
vk::UniqueBuffer buffer = m_device->createBufferUnique(createInfo);
vk::MemoryRequirements memReqs = m_device->getBufferMemoryRequirements(buffer.get());
VulkanMemoryPool::Allocation allocation = this->allocateStagingMemory(memReqs.size, memReqs.alignment);
m_device->bindBufferMemory(buffer.get(), allocation.devMem(), allocation.devMemOffset());
return {std::move(buffer), std::move(allocation)};
}
void LogicalDevice::scheduleForDeallocation(VulkanMemoryPool::Allocation allocation, uint32_t numFramesToKeepAlive)
{
this->scheduleForDeallocation({m_frameIndex + numFramesToKeepAlive, std::move(allocation)});
}
void LogicalDevice::scheduleForDeallocation(BufferAllocation allocation, uint32_t numFramesToKeepAlive)
{
this->scheduleForDeallocation({m_frameIndex + numFramesToKeepAlive, {}, std::move(allocation)});
}
void LogicalDevice::scheduleForDeallocation(ImageAllocation allocation, uint32_t numFramesToKeepAlive)
{
this->scheduleForDeallocation({m_frameIndex + numFramesToKeepAlive, {}, {}, std::move(allocation)});
}
void LogicalDevice::scheduleForDeallocation(DeallocationContainer deallocation)
{
std::lock_guard guard(m_deallocationQueueMtx);
auto lbIt = std::lower_bound(m_deallocationQueue.begin(), m_deallocationQueue.end(), deallocation);
m_deallocationQueue.insert(lbIt, std::move(deallocation));
}
std::optional<uint32_t> LogicalDevice::getQueueFamilyIndex(vk::QueueFlags flags, std::unordered_set<uint32_t> excludeQueueFamilyIndices)
{
std::unordered_set<uint32_t> candidates;
for(vk::PhysicalDevice physicalDevice : m_physicalDevices)
{
std::vector<vk::QueueFamilyProperties> queueFamilyProps = physicalDevice.getQueueFamilyProperties();
for(uint32_t i = 0; i < queueFamilyProps.size(); ++i)
{
if(excludeQueueFamilyIndices.find(i) == excludeQueueFamilyIndices.end())
{
bool desiredFlagsOnAllDevices = true;
for(vk::PhysicalDevice otherPhysicalDevice : m_physicalDevices)
{
std::vector<vk::QueueFamilyProperties> otherQueueFamilyProps = physicalDevice.getQueueFamilyProperties();
desiredFlagsOnAllDevices &= i < otherQueueFamilyProps.size() && (otherQueueFamilyProps[i].queueFlags & flags);
}
if(desiredFlagsOnAllDevices)
{
candidates.insert(i);
}
}
}
}
if(candidates.empty())
{
LOGE("No common queue family index found.\n");
return {};
}
return *candidates.begin();
}
vk::Queue LogicalDevice::getQueue(uint32_t queueFamilyIndex) const
{
auto findIt = m_queues.find(queueFamilyIndex);
return findIt == m_queues.end() ? nullptr : findIt->second;
}
bool LogicalDevice::start()
{
// starting a logical device will create all its resources, including its vkDevice, queues, memory pools, and render
// pass. at last the logical displays and their render contexts are started as well
std::optional<uint32_t> graphicsQueueFamilyIndex = this->getQueueFamilyIndex(vk::QueueFlagBits::eGraphics, {});
if(!graphicsQueueFamilyIndex.has_value())
{
LOGE("No graphics queue family index.\n");
return false;
}
std::optional<uint32_t> transferQueueFamilyIndex =
this->getQueueFamilyIndex(vk::QueueFlagBits::eTransfer, {graphicsQueueFamilyIndex.value()});
if(!transferQueueFamilyIndex.has_value())
{
LOGE("No dedicated transfer queue family index.\n");
return false;
}
std::optional<uint32_t> framebufferTransferQueueFamilyIndex =
this->getQueueFamilyIndex(vk::QueueFlagBits::eTransfer,
{graphicsQueueFamilyIndex.value(), transferQueueFamilyIndex.value()});
if(!framebufferTransferQueueFamilyIndex.has_value())
{
LOGE("No two dedicated transfer queue family indices.\n");
return false;
}
m_graphicsQueueFamilyIndex = graphicsQueueFamilyIndex.value();
m_transferQueueFamilyIndex = transferQueueFamilyIndex.value();
m_framebufferTransferQueueFamilyIndex = framebufferTransferQueueFamilyIndex.value();
LOGI("Queue family indices - graphics: %d, transfer: %d, fb transfer: %d.\n", m_graphicsQueueFamilyIndex,
m_transferQueueFamilyIndex, m_framebufferTransferQueueFamilyIndex);
std::vector<float> queuePriorities = {1.0f};
std::vector<vk::DeviceQueueCreateInfo> devQueueCreateInfos = {
{{}, m_graphicsQueueFamilyIndex, queuePriorities},
{{}, m_transferQueueFamilyIndex, queuePriorities},
{{}, m_framebufferTransferQueueFamilyIndex, queuePriorities},
};
std::vector<char const*> enabledExtensions = {"VK_KHR_swapchain", "VK_NV_acquire_winrt_display"};
vk::PhysicalDeviceSynchronization2Features synchronization2Features(true);
vk::PhysicalDeviceTimelineSemaphoreFeatures timelineSemaphoreFeatures(true, &synchronization2Features);
vk::DeviceGroupDeviceCreateInfo devGroupDevCreateInfo(m_physicalDevices, &timelineSemaphoreFeatures);
vk::DeviceCreateInfo devCreateInfo({}, devQueueCreateInfos, {}, enabledExtensions, nullptr, &devGroupDevCreateInfo);
m_device = m_physicalDevices.front().createDeviceUnique(devCreateInfo);
m_queues[m_graphicsQueueFamilyIndex] = m_device->getQueue(m_graphicsQueueFamilyIndex, 0);
m_queues[m_transferQueueFamilyIndex] = m_device->getQueue(m_transferQueueFamilyIndex, 0);
m_queues[m_framebufferTransferQueueFamilyIndex] = m_device->getQueue(m_framebufferTransferQueueFamilyIndex, 0);
for(UniqueCommandExecutionUnit& cmdExecUnit : m_cmdExecUnits)
{
cmdExecUnit = std::make_unique<CommandExecutionUnit>(*this);
}
m_uploader = std::make_unique<VulkanMemoryObjectUploader>(*this);
MemTypeIndex stagingMemTypeIdx =
this->getMemoryTypeIndex(0, ~0, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible);
m_stagingMemPool = std::make_unique<VulkanMemoryPool>(m_device.get(), DeviceMask(), stagingMemTypeIdx, true);
std::vector<vk::SurfaceFormatKHR> commonSurfaceFormats;
if(!m_logicalDisplays.empty())
{
m_logicalDisplays.front()->querySurfaceFormats(commonSurfaceFormats);
}
for(auto const& logicalDisplay : m_logicalDisplays)
{
std::vector<vk::SurfaceFormatKHR> surfaceFormats;
logicalDisplay->querySurfaceFormats(surfaceFormats);
commonSurfaceFormats.erase(std::remove_if(commonSurfaceFormats.begin(), commonSurfaceFormats.end(),
[&](auto const& fmt) {
return std::find(surfaceFormats.begin(), surfaceFormats.end(), fmt)
== surfaceFormats.end();
}),
commonSurfaceFormats.end());
}
if(commonSurfaceFormats.empty())
{
LOGE("No common surface format for shared display.\n");
return false;
}
vk::Format preferredFormat = vk::Format::eB8G8R8A8Unorm;
vk::ColorSpaceKHR preferredColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
vk::SurfaceFormatKHR surfFormat = commonSurfaceFormats.front();
for(vk::SurfaceFormatKHR fmt : commonSurfaceFormats)
{
if(fmt.format == preferredFormat && fmt.colorSpace == preferredColorSpace)
{
surfFormat = fmt;
break;
}
}
vk::AttachmentDescription colorAttachment({}, surfFormat.format, vk::SampleCountFlagBits::e1, vk::AttachmentLoadOp::eClear);
colorAttachment.setInitialLayout(vk::ImageLayout::eColorAttachmentOptimal);
colorAttachment.setFinalLayout(vk::ImageLayout::eColorAttachmentOptimal);
vk::AttachmentDescription depthStencilAttachment({}, vk::Format::eD24UnormS8Uint, vk::SampleCountFlagBits::e1,
vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eStore,
vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eStore);
depthStencilAttachment.setInitialLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal);
depthStencilAttachment.setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal);
std::vector<vk::AttachmentDescription> attachments = {colorAttachment, depthStencilAttachment};
vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal);
vk::AttachmentReference depthStencilAttachmentRef(1, vk::ImageLayout::eDepthStencilAttachmentOptimal);
vk::SubpassDescription subpass({}, vk::PipelineBindPoint::eGraphics, {}, colorAttachmentRef, {}, &depthStencilAttachmentRef);
vk::RenderPassCreateInfo renderPassCreateInfo({}, attachments, subpass);
m_donutRenderPass = m_device->createRenderPassUnique(renderPassCreateInfo);
this->createDonutPipeline();
for(uint32_t i = 0; i < m_logicalDisplays.size(); ++i)
{
if(!m_logicalDisplays[i]->start(surfFormat, m_donutRenderPass.get()))
{
LOGI("Starting of rendering on display %d failed.\n", i);
}
}
return true;
}
VulkanMemoryPool* LogicalDevice::getMemPool(OptionalDeviceIndex deviceIndex, MemTypeIndex memTypeIdx)
{
std::lock_guard guard(m_memPoolsMtx);
MemPoolCollection& memPoolCollection = deviceIndex.has_value() ? m_perSubDeviceMemPools[deviceIndex.value()] : m_globalMemPools;
if(auto findIt = memPoolCollection.find(memTypeIdx); findIt != memPoolCollection.end())
{
return findIt->second.get();
}
DeviceMask deviceMask = deviceIndex.has_value() ? DeviceMask::ofSingleDevice(deviceIndex.value()) : DeviceMask();
return memPoolCollection
.emplace(memTypeIdx, std::make_unique<VulkanMemoryPool>(m_device.get(), deviceMask, memTypeIdx, false))
.first->second.get();
}
void LogicalDevice::render()
{
CommandExecutionUnit& cmdExecUnit = *m_cmdExecUnits[m_frameIndex % NUM_QUEUED_FRAMES];
cmdExecUnit.waitForIdleAndReset();
m_uploader->prepare(cmdExecUnit);
for(auto const& logicalDisplay : m_logicalDisplays)
{
logicalDisplay->renderFrameAsync(cmdExecUnit);
}
std::vector<vk::Semaphore> waitSems;
std::vector<vk::SwapchainKHR> swapchains;
std::vector<uint32_t> imageIndices;
for(auto const& logicalDisplay : m_logicalDisplays)
{
std::optional<LogicalDisplay::PresentData> presentData = logicalDisplay->finishFrameRendering(cmdExecUnit);
if(presentData.has_value())
{
waitSems.emplace_back(presentData.value().m_waitSem);
swapchains.emplace_back(presentData.value().m_swapchain);
imageIndices.emplace_back(presentData.value().m_imageIndex);
}
}
m_uploader->finish();
cmdExecUnit.submit();
// vk_ddisplay
// all displays of this logical device can be presented at once
if(!swapchains.empty())
{
vk::PresentInfoKHR presentInfo(waitSems, swapchains, imageIndices);
vk::Result presentResult = this->getQueue(m_graphicsQueueFamilyIndex).presentKHR(presentInfo);
if(presentResult != vk::Result::eSuccess)
{
LOGW("presentKHR() failed.\n");
}
}
auto it = m_deallocationQueue.begin();
while(it != m_deallocationQueue.end() && it->m_frameIndex < m_frameIndex)
{
++it;
}
m_deallocationQueue.erase(m_deallocationQueue.begin(), it);
++m_frameIndex;
}
void LogicalDevice::interrupt()
{
m_cmdExecUnits[(m_frameIndex + NUM_QUEUED_FRAMES - 1) % NUM_QUEUED_FRAMES]->waitForIdle();
for(auto const& logicalDisplay : m_logicalDisplays)
{
logicalDisplay->interrupt();
}
m_deallocationQueue.clear();
}
void LogicalDevice::join()
{
for(auto const& logicalDisplay : m_logicalDisplays)
{
logicalDisplay->join();
}
}
} // namespace vkdd