【问题标题】:Vulkan depth image binding errorVulkan 深度图像绑定错误
【发布时间】:2017-02-16 11:47:16
【问题描述】:

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

深度格式为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);

【问题讨论】:

    标签: vulkan


    【解决方案1】:

    是的,深度使用图像可能不支持线性平铺。 请参阅VkImageCreateInfo 的规范和有效用法部分。该能力通过vkGetPhysicalDeviceFormatPropertiesvkGetPhysicalDeviceImageFormatProperties 命令查询。虽然深度格式是“opaque”,所以没有太多理由使用线性平铺。

    您似乎在代码中执行此操作。
    但是该错误会通知您您正在尝试使用给定图像不允许的内存类型。使用vkGetImageMemoryRequirements命令查询允许使用的内存类型。

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

    【讨论】:

    • 您好,感谢您的帮助...对于 memoryTypeFromProperties,我得到 memRqrmntTypeBits 为 132。而 132 是 VK_FORMAT_BC1_RGB_SRGB_BLOCK = 132。我得到不受支持的深度格式,请尝试其他深度格式。
    【解决方案2】:

    我不小心将 typeIndex 设置为 1 而不是 i,它现在可以工作了。在我的辩护中,我整天都在进行 vulkan 编码,我的眼睛在流血:)。感谢您的帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2019-08-06
      • 2021-07-28
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多