66 VkBuffer src = data.buffer();
67 int size = data.size() *
sizeof(
T);
71 if (this->
width != data.nx() ||
72 this->height != data.ny()) {
74 this->
width = data.nx();
80 vkDestroyBuffer(device, buffer,
nullptr);
81 vkFreeMemory(device, memory,
nullptr);
83 VkExternalMemoryHandleTypeFlags
type;
86 type = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
88 type = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
93 VkBufferCreateInfo bufferInfo{};
94 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
95 bufferInfo.size = size;
96 bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
97 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
99 VkExternalMemoryBufferCreateInfo externalInfo{};
100 externalInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO;
101 externalInfo.handleTypes =
type;
102 bufferInfo.pNext = &externalInfo;
104 if (vkCreateBuffer(device, &bufferInfo,
nullptr, &buffer) != VK_SUCCESS) {
105 throw std::runtime_error(
"failed to create buffer!");
111 VkMemoryRequirements memRequirements;
112 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
114 VkMemoryAllocateInfo allocInfo{};
115 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
116 allocInfo.allocationSize = memRequirements.size;
117 allocInfo.memoryTypeIndex = ctx->getMemoryType(memRequirements.memoryTypeBits,
118 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
121 VkExportMemoryAllocateInfo memoryHandleEx{};
122 memoryHandleEx.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
123 memoryHandleEx.handleTypes =
type;
124 allocInfo.pNext = &memoryHandleEx;
126 if (vkAllocateMemory(device, &allocInfo,
nullptr, &memory) != VK_SUCCESS) {
127 throw std::runtime_error(
"failed to allocate buffer memory!");
131 vkBindBufferMemory(device, buffer, memory, 0);
136 VkMemoryRequirements req;
137 vkGetBufferMemoryRequirements(device, buffer, &req);
141 if (copyCmd == VK_NULL_HANDLE) {
142 copyCmd = ctx->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
146 VkCommandBufferBeginInfo beginInfo{};
147 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
148 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
151 VkBufferCopy copyRegion{};
152 copyRegion.srcOffset = 0;
153 copyRegion.dstOffset = 0;
154 copyRegion.size = size;
155 vkCmdCopyBuffer(copyCmd, src, buffer, 1, ©Region);
158 ctx->flushCommandBuffer(copyCmd, ctx->transferQueue,
false);
167 VkCommandBufferBeginInfo beginInfo{};
168 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
169 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
172 VkBufferCopy copyRegion{};
173 copyRegion.srcOffset = 0;
174 copyRegion.dstOffset = 0;
175 copyRegion.size = size;
176 vkCmdCopyBuffer(copyCmd, buffer, wtf.buffer(), 1, ©Region);
179 ctx->flushCommandBuffer(copyCmd, ctx->transferQueue,
false);
186 VkMemoryGetWin32HandleInfoKHR info{};
187 info.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR;
188 info.memory = memory;
189 info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
191 auto vkGetMemoryWin32HandleKHR =
192 PFN_vkGetMemoryWin32HandleKHR(vkGetDeviceProcAddr(device,
"vkGetMemoryWin32HandleKHR"));
193 vkGetMemoryWin32HandleKHR(device, &info, &handle);