Vulkan:缓冲区复制期间设备丢失

时间:2018-07-24 02:41:50

标签: c++ vulkan

我对提交缓冲区复制命令感到非常困难,这对我来说毫无意义。当我尝试从登台缓冲区复制到设备本地缓冲区时,从验证层收到“设备丢失”错误。真正令人困惑的是,它仅在我以特定方式调用复制函数时才会发生。

代码如下:

Buffer.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, &copyRegion);

            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);
}

我要拔头发了,我绝对不明白为什么会出现这种情况。

1 个答案:

答案 0 :(得分:1)

正如弗兰克指出的,这都是由于我上面发布的vkr::StagedBufferFactory::create代码中的语法错误。我不小心采用了传入的dataSrc指针的指针,导致我试图用垃圾填充我的VkBuffer。