51 const char* src = R
"===(
53layout(local_size_x=1,local_size_y=1) in;
54layout(binding=1,std430) buffer BufferSrc { int vSrc[]; };
55layout(binding=2,std430) buffer BufferDst { int vDst[]; };
56uniform int uSrcPitch = 1;
57uniform int uDstPitch = 1;
58void main() { vDst[uDstPitch * gl_GlobalInvocationID.x + gl_GlobalInvocationID.y]
59 = vSrc[uSrcPitch * gl_GlobalInvocationID.x + gl_GlobalInvocationID.y]; }
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);
134 VkMemoryRequirements req;
135 vkGetBufferMemoryRequirements(device, buffer, &req);
136 this->allocatedSize = size;
140 VkMemoryGetWin32HandleInfoKHR info{};
141 info.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR;
142 info.memory = memory;
143 info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
145 auto vkGetMemoryWin32HandleKHR =
146 PFN_vkGetMemoryWin32HandleKHR(vkGetDeviceProcAddr(device,
"vkGetMemoryWin32HandleKHR"));
147 vkGetMemoryWin32HandleKHR(device, &info, &handle);
153 printf(
"Buffer allocated %d bytes\n", allocatedSize);
159 srcBufferSize = size;
160 if (src ==
nullptr || size <= 0)
return;
163 if (size > this->allocatedSize || size < (this->allocatedSize / 4)) {
165 this->allocateVkBuffer(size * 2);
172 if (copyCmd == VK_NULL_HANDLE) {
177 VkCommandBufferBeginInfo beginInfo{};
178 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
179 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
182 VkBufferCopy copyRegion{};
183 copyRegion.srcOffset = 0;
184 copyRegion.dstOffset = 0;
185 copyRegion.size = size;
186 vkCmdCopyBuffer(copyCmd, src, buffer, 1, ©Region);
200 int size = buffer.size() *
sizeof(
T);
204 int newSize = this->size;
207 if (
size < (this->size / 2))
210 if (
size > this->size)
211 newSize =
size * 1.5;
214 if(newSize != this->size) {
218 cuSafeCall(cudaGraphicsUnregisterResource(resource));
219 cuSafeCall(cudaGraphicsGLRegisterBuffer(&resource,
id, cudaGraphicsRegisterFlagsWriteDiscard));
224 cuSafeCall(cudaGraphicsMapResources(1, &resource));
225 cuSafeCall(cudaGraphicsResourceGetMappedPointer(&devicePtr, &size0, resource));
226 cuSafeCall(cudaMemcpy(devicePtr, buffer.begin(),
size, cudaMemcpyDeviceToDevice));
227 cuSafeCall(cudaGraphicsUnmapResources(1, &resource));
238 glDeleteMemoryObjectsEXT(1, &memoryObject);
239 glCreateMemoryObjectsEXT(1, &memoryObject);
241 glImportMemoryWin32HandleEXT(memoryObject, allocatedSize, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, handle);
248 if (tempBuffer != GL_INVALID_INDEX)
249 glDeleteBuffers(1, &tempBuffer);
250 glGenBuffers(1, &tempBuffer);
251 glNamedBufferStorageMemEXT(tempBuffer, allocatedSize, memoryObject, 0);
260 int src_pitch =
sizeof(
T) /
sizeof(
int);
261 int dst_pitch =
sizeof(
T) /
sizeof(
int);
265 copy->
proc(tempBuffer, this->
id, src_pitch, dst_pitch,
count());
273 return srcBufferSize /
sizeof(
T);
277 return buffer.size();