c - 从交换链呈现会导致段错误
问题描述
目前正在尝试创建一个显示交换链中图像的主循环:
void loop(struct Context *ctx, void(*loopfn)(struct Context*)) {
VkQueue queue;
vkGetDeviceQueue(ctx->objects.device, 0, 0, &queue);
while (!glfwWindowShouldClose(ctx->window)) {
VkResult fenceStatus = vkGetFenceStatus(ctx->objects.device, ctx->objects.fence);
if (fenceStatus == VK_SUCCESS) {
// Reset fence and command buffer
if (vkQueuePresentKHR(queue, &ctx->objects.presentInfo) != VK_SUCCESS) {
fprintf(stderr, "Failed to present image\n");
break;
}
loopfn(ctx);
} else if (fenceStatus == VK_ERROR_DEVICE_LOST) {
fprintf(stderr, "Device lost\n");
break;
}
}
}
void loopfn(struct Context *ctx) {
// Clear color image
}
int main() {
struct Context *ctx = init(500, 500, "Test");
if (!ctx)
return -1;
loop(ctx, loopfn);
deinit(ctx);
return 0;
}
但是,看起来我以不正确的方式使用了交换链扩展,这会转换为来自验证层的以下错误消息,然后是分段错误:
UNASSIGNED-CoreValidation-DrawState-SwapchainInvalidImage(ERROR / SPEC): msgNum: 0 - vkQueuePresentKHR: Swapchain image index too large (0). There are only 0 images in this swapchain.
Objects: 1
[0] 0x7, type: 1000001000, name: (null)
Validation(ERROR): msg_code: 0: [ UNASSIGNED-CoreValidation-DrawState-SwapchainInvalidImage ] Object: 0x7 (Type = 27) | vkQueuePresentKHR: Swapchain image index too large (0). There are only 0 images in this swapchain.
我的init
函数很长,我不知道把它放在这里是否很有用,但我实际上在做的(并且不起作用)是:
创建交换链如下:
VkSwapchainCreateInfoKHR swapchainInfo = { .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR , .pNext = NULL , .flags = 0x00000000 , .surface = ctx->objects.surface , .minImageCount = surfaceCapabilities.minImageCount , .imageFormat = pSurfaceFormats[0].format , .imageColorSpace = pSurfaceFormats[0].colorSpace , .imageExtent = surfaceCapabilities.currentExtent , .imageArrayLayers = 1 , .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT , .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE , .queueFamilyIndexCount = 1 , .pQueueFamilyIndices = NULL , .preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR , .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR , .presentMode = VK_PRESENT_MODE_MAILBOX_KHR , .clipped = VK_TRUE , .oldSwapchain = VK_NULL_HANDLE }; if (vkCreateSwapchainKHR(ctx->objects.device, &swapchainInfo, NULL, &ctx->objects.swapchain) != VK_SUCCESS) goto failure;
创建彩色图像和深度图像。这两个
VkImageCreateInfo
结构都有一个指向以下结构的指针作为它们的pNext
值:VkImageSwapchainCreateInfoKHR swapchainImageInfo = { .sType = VK_STRUCTURE_TYPE_IMAGE_SWAPCHAIN_CREATE_INFO_KHR , .pNext = NULL , .swapchain = ctx->objects.swapchain };
为两个图像分配内存,然后尝试将它们绑定到交换链,如下所示:
VkBindImageMemorySwapchainInfoKHR colorSwapchainBindInfo = { .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR , .pNext = NULL , .swapchain = ctx->objects.swapchain , .imageIndex = 0 }; VkBindImageMemoryInfo colorMemBindInfo = { .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO , .pNext = &colorSwapchainBindInfo , .image = ctx->objects.colorImage , .memory = ctx->objects.colorMemory , .memoryOffset = 0 }; // Pretty much the same thing for the depth image VkBindImageMemoryInfo memBindInfos[] = { colorMemBindInfo, depthMemBindInfo }; if (vkBindImageMemory2(ctx->objects.device, 2, memBindInfos) != VK_SUCCESS) goto failure;
执行两个所需的图像布局转换(从
VK_IMAGE_LAYOUT_UNDEFINED
到VK_IMAGE_LAYOUT_GENERAL
)- 创建两个所需的图像视图,一个帧缓冲区和一个
VkPresentInfoKHR
结构
我想知道我做错了什么以及如何解决它。随意要求任何额外的精度。
编辑:我使用的实例扩展是VK_KHR_surface
和VK_KHR_xcb_surface
.
uint32_t count;
const char **extensions = glfwGetRequiredInstanceExtensions(&count);
VkInstanceCreateInfo instanceInfo =
{ .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
, .pNext = NULL
, .flags = 0x00000000
, .pApplicationInfo = NULL
, .enabledLayerCount = 0
, .ppEnabledLayerNames = NULL
, .enabledExtensionCount = count
, .ppEnabledExtensionNames = extensions
};
我使用的唯一设备扩展是VK_KHR_swapchain
:
const char *deviceExtensions[] = { "VK_KHR_swapchain" };
VkDeviceCreateInfo deviceInfo =
{ .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
, .pNext = NULL
, .flags = 0x00000000
, .queueCreateInfoCount = 1
, .pQueueCreateInfos = &queueInfo
, .enabledLayerCount = 0
, .ppEnabledLayerNames = NULL
, .enabledExtensionCount = sizeof(deviceExtensions) / sizeof(deviceExtensions[0])
, .ppEnabledExtensionNames = deviceExtensions
, .pEnabledFeatures = NULL
};
解决方案
对,所以这是一个 XY 问题。似乎您正在使用不适合您的情况的 Vulkan 功能。即VkImageSwapchainCreateInfoKHR
和VkBindImageMemorySwapchainInfoKHR
。根据他们的描述,它们仅对 GPU 组有用。但是根据您的评论,您只想以旧的无聊方式使用 Swapchain。
Swapchain 需要以一种特殊的方式创建自己的镜像。这通常是对 Vulkan Swapchain 抽象的底层系统(例如 X、Wayland、GDI 或 DXGI)的限制。因此,您不会尝试自己创建这些图像(也不会尝试将内存绑定到它们),而是让vkCreateSwapchain
您为您完成。
BTW Vulkan 交换链对深度图像没有用处。都是彩色图像。
因此,当 Swapchain 是vkCreate
d 时,您将获得它为您创建的图像vkGetSwapchainImagesKHR
。用法类似于:
uint32_t count;
VkResult errco = vkGetSwapchainImagesKHR( dev, swpch, &count, nullptr );
if( errco ) throw "error";
std::vector<VkImage> swapchainImages( count );
errco = vkGetSwapchainImagesKHR( dev, swpch, &count, swapchainImages.data() );
if( errco ) throw "error";
注意:理论上你可能会得到比你要求的更多的图像vkCreateSwapchain
。
VkImage
这些图像与s 定期获得的一样好vkCreateImage
,除了两件事:
- 你不被允许
vkDestroyImage
;当你调用它们时,它们会被销毁vkDestroySwapchainKHR
。 - 它们已经绑定了内存(因此您不需要,也不允许将内存绑定到这些图像)。
这应该用于介绍,并解决这里的直接问题。
推荐阅读
- python - 在 python 中读取和解析类似 JSON 的字符串文件
- c# - 生成 ERR_HTTP2_PROTOCOL_ERROR 的 Azure 应用注册快速入门代码
- ruby-on-rails - Rails 多对多 API 查询
- python - 电报机器人 GetUpdate 返回 {"ok":true,"result":[]}
- json - 数据工厂 - 从 JSON 文件中使用破折号“-”从字段中检索值
- c++ - 访问者模式是否比受控使用 RTTI 更好?
- typescript - Webstorm+TypeScript 无法解析 CRA 项目中的 SCSS 模块(错误 TS2339)
- javascript - NodeJS API 不会删除或更新 mongodb 中的数据
- c# - 当 WPF 中的文本太长时,带有滑块的 TextBlock
- sql - SQL Server 错误操作数类型冲突:int 与日期不兼容