Repository navigation
Expand file tree
/
Copy pathvksc_command_pool.cpp
More file actions
121 lines (102 loc) · 4.03 KB
/
Copy pathvksc_command_pool.cpp
File metadata and controls
121 lines (102 loc) · 4.03 KB
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
/*
* Copyright (c) 2024-2026 The Khronos Group Inc.
* Copyright (c) 2024-2026 RasterGrid Kft.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "vksc_command_pool.h"
#include "vksc_device.h"
#include "vksc_command_buffer.h"
namespace vksc {
CommandPool::CommandPool(VkCommandPool command_pool, Device& device, VkDeviceSize reserved_size, uint32_t reserved_count)
: handle_(command_pool),
device_(device),
logger_(device.Log(), VK_OBJECT_TYPE_COMMAND_POOL, command_pool),
reserved_memory_size_(reserved_size),
max_command_buffer_count_(reserved_count) {
command_buffers_.reserve(reserved_count);
}
CommandPool::~CommandPool() {
for (auto command_buffer : command_buffers_) {
CommandBuffer::FromHandle(command_buffer)->Free();
}
}
bool CommandPool::operator==(const VkCommandPool& rhs) const { return this->handle_ == rhs; }
void CommandPool::GetMemoryConsumption(VkCommandPoolMemoryConsumption* pConsumption) const {
std::unique_lock lock(mutex_);
pConsumption->commandPoolReservedSize = reserved_memory_size_;
pConsumption->commandPoolAllocated = allocated_memory_size_;
}
VkResult CommandPool::AllocateMemory(VkDeviceSize size) {
std::unique_lock lock(mutex_);
if (allocated_memory_size_ + size <= reserved_memory_size_) {
allocated_memory_size_ += size;
return VK_SUCCESS;
} else {
Log().Error("VKSC-EMU-CommandPool-OutOfReservedMemory", "Ran out of the reserved memory for the command pool (%p)",
handle_);
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
}
icd::ObjectReservation<CommandPool, VkCommandBuffer> CommandPool::ReserveCommandBuffers(uint32_t count,
const VkCommandBuffer* buffers) {
return {*this, count};
}
VkResult CommandPool::FreeMemory(VkDeviceSize size) {
std::unique_lock lock(mutex_);
if (size <= allocated_memory_size_) {
allocated_memory_size_ -= size;
return VK_SUCCESS;
} else {
GetDevice().ReportFault(VK_FAULT_LEVEL_CRITICAL, VK_FAULT_TYPE_INVALID_API_USAGE);
Log().Fatal("VKSC-EMU-CommandPool-Underflow",
"Command buffer memory release resulted in command pool (%p) memory underflow", handle_);
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
}
VkResult CommandPool::FreeCommandBuffers(uint32_t count, const VkCommandBuffer* buffers) {
std::unique_lock lock(mutex_);
for (uint32_t i = 0; i < count; ++i) {
if (buffers[i] == VK_NULL_HANDLE) {
continue;
}
auto it = command_buffers_.find(buffers[i]);
if (it == command_buffers_.cend()) {
GetDevice().ReportFault(VK_FAULT_LEVEL_CRITICAL, VK_FAULT_TYPE_INVALID_API_USAGE);
Log().Fatal("VKSC-EMU-CommandPool-UnknownCommandBuffer",
"Command pool (%p) was requested to free command buffer buffer (%p) which wasn't recognized by it.",
handle_, buffers[i]);
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
command_buffers_.erase(it);
}
return VK_SUCCESS;
}
VkResult CommandPool::ResetCommandPool(VkCommandPoolResetFlags) {
std::unique_lock lock(mutex_);
VkResult result = VK_SUCCESS;
for (auto& command_buffer : command_buffers_) {
CommandBuffer::FromHandle(command_buffer)->FreeMemory();
if (CommandBuffer::FromHandle(command_buffer)->GetStatus() != VK_SUCCESS) {
result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
}
return result;
}
uint32_t CommandPool::ReserveInternal(uint32_t count) {
mutex_.lock();
if (command_buffers_.size() + count <= max_command_buffer_count_) {
return count;
} else {
mutex_.unlock();
return 0;
}
}
void CommandPool::CancelInternal(uint32_t count) { mutex_.unlock(); }
void CommandPool::CommitInternal(VkCommandBuffer* handles, uint32_t count) {
for (uint32_t i = 0; i < count; ++i) {
command_buffers_.insert(handles[i]);
}
mutex_.unlock();
}
} // namespace vksc