-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBuffer.cpp
More file actions
92 lines (74 loc) · 2.8 KB
/
Copy pathCommandBuffer.cpp
File metadata and controls
92 lines (74 loc) · 2.8 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
#include "CommandBuffer.h"
#include <new>
#include "CommandPool.h"
#include "Device.h"
#include "Internal/Error.h"
extern "C" {
GPU_API VkResult GPU_CALL gpuCommandBufferCreate(
GPUCommandPool commandPool,
VkCommandBufferLevel level,
GPUCommandBuffer* commandBuffer) {
gpu::internal::clearLastError();
if (commandPool == nullptr || commandBuffer == nullptr) {
gpu::internal::setLastError("Command pool and command buffer output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*commandBuffer = nullptr;
VkCommandBufferAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.commandPool = commandPool->handle;
allocateInfo.level = level;
allocateInfo.commandBufferCount = 1;
VkCommandBuffer handle = VK_NULL_HANDLE;
const VkResult result = gpuAllocateCommandBuffers(commandPool->device->handle, &allocateInfo, &handle);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to allocate command buffer.");
return result;
}
GPUCommandBuffer wrapper = new (std::nothrow) GPUCommandBuffer_T{
commandPool,
commandPool->deviceHandle,
commandPool->handle,
handle,
level,
nullptr,
nullptr};
if (wrapper == nullptr) {
gpuFreeCommandBuffers(commandPool->device->handle, commandPool->handle, 1, &handle);
gpu::internal::setLastError("Failed to allocate CommandBuffer.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
wrapper->next = commandPool->firstCommandBuffer;
if (wrapper->next != nullptr) {
wrapper->next->previous = wrapper;
}
commandPool->firstCommandBuffer = wrapper;
*commandBuffer = wrapper;
return VK_SUCCESS;
}
GPU_API void GPU_CALL gpuCommandBufferDestroy(GPUCommandBuffer commandBuffer) {
if (commandBuffer == nullptr) {
return;
}
if (commandBuffer->previous != nullptr) {
commandBuffer->previous->next = commandBuffer->next;
} else if (commandBuffer->commandPool != nullptr) {
commandBuffer->commandPool->firstCommandBuffer = commandBuffer->next;
}
if (commandBuffer->next != nullptr) {
commandBuffer->next->previous = commandBuffer->previous;
}
gpuFreeCommandBuffers(
commandBuffer->deviceHandle,
commandBuffer->commandPoolHandle,
1,
&commandBuffer->handle);
delete commandBuffer;
}
GPU_API VkCommandBuffer GPU_CALL gpuCommandBufferGetVkHandle(GPUCommandBuffer commandBuffer) {
return commandBuffer != nullptr ? commandBuffer->handle : VK_NULL_HANDLE;
}
GPU_API GPUCommandPool GPU_CALL gpuCommandBufferGetCommandPool(GPUCommandBuffer commandBuffer) {
return commandBuffer != nullptr ? commandBuffer->commandPool : nullptr;
}
} // extern "C"