Vulkan:设备在缓冲区复制过程中丢失
Vulkan: Device Lost during Buffer Copy
我很难提交缓冲区复制命令,这对我来说毫无意义。当我试图从暂存缓冲区复制到设备本地缓冲区时,验证层会出现"设备丢失"错误。真正令人困惑的是,只有当我以特定的方式调用复制函数时,它才会发生。
这是代码:
缓冲.cpp
#include "Buffer.h"
namespace vkr
{
Buffer::Buffer() : buffer(VK_NULL_HANDLE), size(0) {}
Buffer::Buffer(
const VmaAllocator & _allocator,
const vk::DeviceSize bufferSize,
const vk::BufferUsageFlags usage,
const VmaMemoryUsage memoryUsageFlags
) :
size(bufferSize)
{
vk::BufferCreateInfo bufferInfo = {};
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = vk::SharingMode::eExclusive;
VmaAllocationCreateInfo allocInfo = {};
allocInfo.usage = memoryUsageFlags;
auto vanillaBufferInfo = (VkBufferCreateInfo)bufferInfo;
vmaCreateBuffer(_allocator, &vanillaBufferInfo, &allocInfo, &buffer, &allocation, nullptr);
}
Buffer::Buffer(Buffer && otherBuffer)
{
buffer = otherBuffer.buffer;
allocation = otherBuffer.allocation;
size = otherBuffer.size;
otherBuffer.buffer = VK_NULL_HANDLE;
otherBuffer.allocation = VmaAllocation();
otherBuffer.size = 0;
}
Buffer & Buffer::operator=(Buffer && otherBuffer)
{
if (this != &otherBuffer) {
buffer = otherBuffer.buffer;
allocation = otherBuffer.allocation;
size = otherBuffer.size;
otherBuffer.buffer = VK_NULL_HANDLE;
otherBuffer.allocation = VmaAllocation();
otherBuffer.size = 0;
}
return *this;
}
Buffer::operator vk::Buffer() const
{
return vk::Buffer(buffer);
}
Buffer::operator VkBuffer() const
{
return buffer;
}
void Buffer::copyInto(const VmaAllocator & _allocator, const void* dataSrc)
{
void* mappedData;
vmaMapMemory(_allocator, allocation, &mappedData);
memcpy(mappedData, dataSrc, (size_t)size);
vmaUnmapMemory(_allocator, allocation);
}
void Buffer::copyInto(const VmaAllocator & _allocator, const void* dataSrc, const size_t newSize)
{
void* mappedData;
vmaMapMemory(_allocator, allocation, &mappedData);
memcpy(mappedData, dataSrc, newSize);
vmaUnmapMemory(_allocator, allocation);
}
void Buffer::destroy(const VmaAllocator & _allocator)
{
vmaDestroyBuffer(_allocator, buffer, allocation);
}
}
分级缓冲区:
#include "StagedBufferFactory.h"
namespace vkr
{
namespace StagedBufferFactory
{
vkr::Buffer create(
vk::Device & device,
vk::CommandPool & commandPool,
vk::Queue & graphicsQueue,
const vk::BufferUsageFlags usageFlags,
const vk::DeviceSize size,
const void * dataSrc,
const VmaAllocator & _allocator
)
{
// Staging buffer that we load the data onto that's visible to our CPU
vk::BufferUsageFlags stagingUsageFlags = vk::BufferUsageFlagBits::eTransferSrc;
vkr::Buffer stagingBuffer{ _allocator, size, stagingUsageFlags, VMA_MEMORY_USAGE_CPU_ONLY };
stagingBuffer.copyInto(_allocator, &dataSrc, (size_t)size);
// This is our buffer located on our GPU, inaccessible to our CPU
vk::BufferUsageFlags gpuBufferUsageFlags = vk::BufferUsageFlagBits::eTransferDst | usageFlags;
vkr::Buffer gpuBuffer{ _allocator, size, gpuBufferUsageFlags, VMA_MEMORY_USAGE_GPU_ONLY };
copyBuffer(device, commandPool, graphicsQueue, stagingBuffer, gpuBuffer);
stagingBuffer.destroy(_allocator);
return gpuBuffer;
}
void copyBuffer(
vk::Device & device,
vk::CommandPool & commandPool,
vk::Queue & graphicsQueue,
vkr::Buffer & stagingBuffer,
vkr::Buffer & gpuBuffer
)
{
vk::CommandBufferAllocateInfo allocInfo = {};
allocInfo.level = vk::CommandBufferLevel::ePrimary;
allocInfo.commandPool = commandPool;
allocInfo.commandBufferCount = 1;
vk::CommandBuffer commandBuffer;
device.allocateCommandBuffers(&allocInfo, &commandBuffer);
vk::CommandBufferBeginInfo beginInfo = {};
beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
commandBuffer.begin(&beginInfo);
vk::BufferCopy copyRegion = {};
copyRegion.srcOffset = 0;
copyRegion.dstOffset = 0;
copyRegion.size = stagingBuffer.size;
commandBuffer.copyBuffer(stagingBuffer.buffer, gpuBuffer.buffer, 1, ©Region);
commandBuffer.end();
vk::SubmitInfo submitInfo = {};
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
// Create fence to ensure that the command buffer has finished executing
vk::FenceCreateInfo fenceInfo{};
vk::Fence fence = device.createFence(fenceInfo);
// Submit to the queue
graphicsQueue.submit(1, &submitInfo, fence);
graphicsQueue.waitIdle();
//device.waitForFences({ fence }, true, 100000000000);
device.destroyFence(fence);
device.free(commandPool, 1, &commandBuffer);
}
}
}
缓冲区创建:
void HelloTriangleApplication::createIndexBuffer()
{
vk::DeviceSize bufferSize = sizeof(indices[0]) * indices.size();
//// Staging buffer that we load the data onto that's visible to our CPU
vk::BufferUsageFlags stagingUsageFlags = vk::BufferUsageFlagBits::eTransferSrc;
vkr::Buffer stagingBuffer{ vulkanAllocator, bufferSize, stagingUsageFlags, VMA_MEMORY_USAGE_CPU_ONLY };
stagingBuffer.copyInto(vulkanAllocator, indices.data());
//// This is our buffer located on our GPU, inaccessible to our CPU
vk::BufferUsageFlags indexUsageFlags = vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eIndexBuffer;
indexBuffer = vkr::Buffer{ vulkanAllocator, bufferSize, indexUsageFlags, VMA_MEMORY_USAGE_GPU_ONLY };
vkr::StagedBufferFactory::copyBuffer(device, commandPool, graphicsQueue, stagingBuffer, indexBuffer);
stagingBuffer.destroy(vulkanAllocator);
}
所以这个代码工作起来没有问题。。。但如果我尝试使用我的vkr::StagedBuffer::create
函数(据我所知,它调用了这个确切的代码(,程序就不起作用了。我得到一个黑色屏幕(而不是彩虹色的矩形(,偶尔验证层告诉我"设备丢失">
void HelloTriangleApplication::createIndexBuffer()
{
vk::DeviceSize bufferSize = sizeof(indices[0]) * indices.size();
indexBuffer = vkr::StagedBufferFactory::create(device, commandPool, graphicsQueue, vk::BufferUsageFlagBits::eIndexBuffer, bufferSize, indices.data(), vulkanAllocator);
}
我把头发都扯掉了,我绝对不明白为什么会出现这种情况。
vkr::StagedBufferFactory::create
代码中的语法错误造成的。我不小心取了传入的dataSrc
指针的一个指针,导致我试图用垃圾填充VkBuffer。
相关文章:
- 是否删除在对象构造过程中创建的对象
- 如何在鼠标挂钩过程中检测拖动
- clang 插件:在编译过程中修改 AST
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- 线程过程中的线程同步问题
- MySQL 连接器/C++.尝试在此过程中停止连接到数据库
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 在调用过程中删除 std::函数
- 动态矩阵特征分解过程中的误差
- cmake在构建过程中使用另一个工具检测标志
- 在模板参数推导过程中丢失限定符
- Vulkan:设备在缓冲区复制过程中丢失
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?
- Visual studio:在生成过程中复制内容,而不考虑 cpp 更改
- 如何设置Visual C++2010学习版以在生成过程中复制某些文件夹
- 复制构造函数是如何在从类类型到类类型的类型转换过程中引发的
- 在复制初始化过程中不会发生std::string的隐式构造
- 如果WinRT应用程序在文件复制过程中终止,会发生什么
- visual String使用c++中的指针进行复制,在复制过程中程序崩溃
- 如何在从c++到QML再到c++的传递过程中避免对象复制