Vulkan深度图像绑定错误

时间:2017-02-15 05:55:24

标签: vulkan

您好我正在尝试绑定深度内存缓冲区,但是我收到一个错误,如下所示。我不知道为什么会出现这个错误。

深度格式为VK_FORMAT_D16_UNORM,用法为VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT。我在网上看到TILING不应该是线性的,但后来我得到了一个不同的错误。感谢!!!

创建和绑定图像的代码如下所示。

VkImageCreateInfo imageInfo = {};

// If the depth format is undefined, use fallback as 16-byte value
if (Depth.format == VK_FORMAT_UNDEFINED) {
    Depth.format = VK_FORMAT_D16_UNORM;
}

const VkFormat depthFormat = Depth.format;

VkFormatProperties props;
vkGetPhysicalDeviceFormatProperties(*deviceObj->gpu, depthFormat, &props);

if (props.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
    imageInfo.tiling = VK_IMAGE_TILING_LINEAR;
}
else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
    imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
}
else {
    std::cout << "Unsupported Depth Format, try other Depth formats.\n";
    exit(-1);
}

imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.pNext = NULL;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = depthFormat;
imageInfo.extent.width = width;
imageInfo.extent.height = height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.samples = NUM_SAMPLES;
imageInfo.queueFamilyIndexCount = 0;
imageInfo.pQueueFamilyIndices = NULL;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
imageInfo.flags = 0;

// User create image info and create the image objects
result = vkCreateImage(deviceObj->device, &imageInfo, NULL, &Depth.image);
assert(result == VK_SUCCESS);

// Get the image memory requirements
VkMemoryRequirements memRqrmnt;
vkGetImageMemoryRequirements(deviceObj->device, Depth.image, &memRqrmnt);

VkMemoryAllocateInfo memAlloc = {};
memAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memAlloc.pNext = NULL;
memAlloc.allocationSize = 0;
memAlloc.memoryTypeIndex = 0;
memAlloc.allocationSize = memRqrmnt.size;

// Determine the type of memory required with the help of memory properties
pass = deviceObj->memoryTypeFromProperties(memRqrmnt.memoryTypeBits, 0, /* No requirements */ &memAlloc.memoryTypeIndex);
assert(pass);

// Allocate the memory for image objects
result = vkAllocateMemory(deviceObj->device, &memAlloc, NULL, &Depth.mem);
assert(result == VK_SUCCESS);

// Bind the allocated memeory
result = vkBindImageMemory(deviceObj->device, Depth.image, Depth.mem, 0);
assert(result == VK_SUCCESS);

enter image description here

2 个答案:

答案 0 :(得分:3)

是的,深度使用图像可能支持 。 请参阅VkImageCreateInfo的规范和有效使用部分。 vkGetPhysicalDeviceFormatPropertiesvkGetPhysicalDeviceImageFormatProperties命令查询该功能。虽然深度格式是&#34; opaque&#34;,但没有太多理由使用线性平铺。

你似乎在做你的代码 但是错误通知您正在尝试使用给定Image不允许的内存类型。使用vkGetImageMemoryRequirements命令查询允许的内存类型。

可能你有一些错误(你正在使用0x1,这显然不是消息的0x84的一部分)。您可能希望重用Device Memory chapter of the specification中的示例代码。提供您的memoryTypeFromProperties实施以获得更具体的答案。

答案 1 :(得分:0)

我不小心将typeIndex设置为1而不是i,现在可以正常工作了。在我的辩护中,我整天都在进行排气,我的眼睛正在流血:)。谢谢你的帮助。

bool VulkanDevice::memoryTypeFromProperties(uint32_t typeBits, VkFlags      
requirementsMask, uint32_t *typeIndex)
{
    // Search memtypes to find first index with those properties
for (uint32_t i = 0; i < 32; i++) {
    if ((typeBits & 1) == 1) {
        // Type is available, does it match user properties?
        if ((memoryProperties.memoryTypes[i].propertyFlags & requirementsMask) == requirementsMask) {
            *typeIndex = i;// was set to 1 :(
            return true;
        }
    }
    typeBits >>= 1;
}
// No memory types matched, return failure
return false;
}