我正在尝试创建一个示例,并验证代码是否可以创建该示例。
但是,由于某种原因,代码编译失败,无法创建示例。我不知道我做错了什么。创建示例之前的所有操作都正常。但当我尝试验证示例创建时失败。我使用SDL2创建窗口。
这是我的cmakelist文件:
cmake_minimum_required(VERSION 3.16)
#COMPIER
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
#FLAGS
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -ggdb3")
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
#PROJECT
project(Vulkan_test LANGUAGES C)
enable_language(C)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
#Find packages installed in your system.
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(Vulkan REQUIRED vulkan)
#Include global directories
include_directories(
${SDL2_INCLUDE_DIRS}
${Vulkan_INCLUDE_DIRS}
)
#Executable Specific
add_executable(VULKAN_TEST
${CMAKE_CURRENT_SOURCE_DIR}/main.c
)
#Compile options
target_compile_options(VULKAN_TEST PUBLIC -O0 -ggdb3 -std=c2x -Wall -Wextra -Wmissing-prototypes -Wwrite-strings -Wpedantic)
#Libraries to link
target_link_libraries(
VULKAN_TEST
${SDL2_LIBRARIES}
${Vulkan_LIBRARIES}
)
这是我的main. h文件:
#pragma once
/* Required Standard Headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
/* SDL2 */
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
/* Vulkan */
#include <vulkan/vulkan.h>
#define rt_assert(expr, ...) ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
if (!(expr)) { \
fprintf(stderr,"At function [%s] in file [%s:%d], assert failed: [(%s)].\n", __func__, __FILE__, __LINE__, (#expr)); \
__VA_ARGS__; \
exit(EXIT_FAILURE); \
} \
}))
typedef struct _App {
SDL_Window *window;
int window_height;
int window_width;
const char* window_name;
SDL_Event sdl_event;
bool quit;
VkInstance inst;
uint32_t sdlExtCount;
const char** extNames;
} App;
/* Function Prototype */
void init_window(App *);
void init_vulkan(App *);
void main_loop(App *);
void cleanup(App *);
/* sub vulkan functions */
void init_vk_instance(App *);
这个文件是我的main. c
#include "main.h"
#define SDL_MAIN_HANDLED
#define App_init(X) App (X) = { \
.window = NULL, .window_height = 600, .window_width = 800, \
.window_name = "Vulkan Test", .quit = false, \
.inst = VK_NULL_HANDLE \
}
VkApplicationInfo appInfo = {0};
VkInstanceCreateInfo instInfo = {0};
void init_vk_instance(App *App) {
/* Vulkan VkApplicationInfo */
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = App->window_name;
appInfo.applicationVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
appInfo.pEngineName = App->window_name;
appInfo.engineVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
/* This is version I have installed from my linux package manager */
appInfo.apiVersion = VK_MAKE_API_VERSION(0,1,3,231);
appInfo.pNext = NULL;
/* Vulkan Instance info - VkInstanceCreateInfo */
instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instInfo.pApplicationInfo = &appInfo;
instInfo.enabledLayerCount = 0;
SDL_Vulkan_GetInstanceExtensions(App->window,&App->sdlExtCount,NULL);
App->extNames = malloc(App->sdlExtCount * sizeof(const char*));
SDL_Vulkan_GetInstanceExtensions(App->window,&App->sdlExtCount,App->extNames);
instInfo.enabledExtensionCount = App->sdlExtCount;
instInfo.ppEnabledExtensionNames = App->extNames;
for(uint32_t i = 0; i < App->sdlExtCount; i++) {
printf("%u : %s\n",i,App->extNames[i]);
}
rt_assert( (vkCreateInstance(&instInfo, NULL, &App->inst) == VK_SUCCESS), printf("Instance creation failed\n"), cleanup(App));
}
void init_vulkan(App *App) {
init_vk_instance(App);
}
void init_window(App *App) {
App->window = SDL_CreateWindow( App->window_name,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
App->window_width, App->window_height, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN |
SDL_WINDOW_RESIZABLE);
}
void cleanup(App *App) {
free(App->extNames);
SDL_DestroyWindow(App->window);
SDL_Quit();
}
void main_loop(App *App) {
while(!App->quit) {
while (SDL_PollEvent(&App->sdl_event)) {
if( App->sdl_event.type == SDL_QUIT ) App->quit = true;
}
}
}
int main ( void ) {
printf("Vulkan-Engine\n");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
App_init(test);
init_window(&test);
init_vulkan(&test);
main_loop(&test);
vkDestroyInstance(test.inst, NULL);
cleanup(&test);
return EXIT_SUCCESS;
}
这是我运行程序后得到的输出:
Vulkan-Engine
0 : VK_KHR_surface
1 : VK_KHR_xlib_surface
At function [init_vk_instance] in file [/home/Codes/C/Vulkan/main.c:35], assert failed: [((vkCreateInstance(&instInfo, NULL, &App->inst) == VK_SUCCESS))].
Instance creation failed
CMake编译期间的输出:
-- The C compiler identification is GNU 12.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
-- Checking for one of the modules 'sdl2'
-- Checking for one of the modules 'vulkan'
-- Configuring done
-- Generating done
-- Build files have been written to: /home/Codes/C/Vulkan/build
[ 50%] Building C object CMakeFiles/VULKAN_TEST.dir/main.c.o
[100%] Linking C executable /home/Codes/C/Vulkan/VULKAN_TEST
[100%] Built target VULKAN_TEST
这些是我通过包管理器安装在系统上的Vulkan相关包:
如图所示,vulkan API版本为1.3.231.1。我是否正确使用了VK_MAKE_API_VERSION(0,1,3,231)
?
1条答案
按热度按时间j5fpnvbx1#
我认为您缺少
KHR
宏例如,我使用了
windows 10
和MSYS2
这就是我所做的对我来说,它工作得很好,所以我的观察是,您需要找到一种方法来定义类似的宏,如
#define VK_USE_PLATFORM_WIN32_KHR
以下是
vulkan.h
中假定的列表扩展名这就是我在
MSYS2
中编译的方式