0

I am trying to init Vulkan on Linux. I do pass SDL_WINDOW_VULKAN flag to SDL_CreateWindow and call to SDL_Vulkan_GetInstanceExtensions fails with error "The specified window isn't a Vulkan window".

I cannot find any specific info on what is the cause of this error, because in sources of SDL2 the cause of this error message is absence of SDL_WINDOW_VULKAN flag. See SDL_video.c#L4810 and SDL_video.c#L4830.

I have tried to create a Vulkan window with GLFW and it worked just fine.

I expect that Vulkan window should be possible to create with SDL2 as well.

Code to repro.

Makefile:

CC=gcc
CFLAGS= -c -g -Wall
LDLIBS= -lvulkan -lSDL2

all: prog

prog: app.o
    $(CC) $(LDLIBS) app.o -o app

app.o: app.c
    $(CC) $(CFLAGS) app.c

clean:
    rm -rf *.o

app.c:

#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <vulkan/vulkan.h>

int main()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    SDL_Window* win = SDL_CreateWindow("Hello World!", 100, 100, 620, 387, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN);
    if (win == NULL) {
        fprintf(stderr, "ERROR: Failed to create window. %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    SDL_Surface *surface = SDL_GetWindowSurface(win);
    if (!surface) {
        fprintf(stderr, "ERROR: Failed to get window surface. %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    unsigned int count = 0;
    bool ret = SDL_Vulkan_GetInstanceExtensions(win, &count, NULL);
    if (!ret) {
        fprintf(stderr, "ERROR: Failed to get instance extensions count. %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    SDL_Event event = {0};
    bool quit = false;
    while (!quit) {
        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
                break;
            }

            if (event.type == SDL_KEYDOWN) {
                switch(event.key.keysym.sym) {
                    case SDLK_ESCAPE:
                        quit = true;
                        break;
                    default:
                        break;
                }
            }
        }

        SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 255, 90, 120));
        SDL_UpdateWindowSurface(win);
    }

    SDL_DestroyWindow(win);
    SDL_Quit();

    return EXIT_SUCCESS;
}
vocasle
  • 799
  • 8
  • 11
  • SDL version? SDL distro binaries or self-built? Why are you calling `SDL_GetWindowSurface()` [if you intend to use Vulkan](https://github.com/libsdl-org/SDL/blob/release-2.26.5/include/SDL_video.h#L1287)? – genpfault May 04 '23 at 21:35
  • 1
    SDL Version: 2.26.5 Yep, it is distro binaries. Well, I have copied over an example code from [thenumb.at/cpp-course/sdl2/](https://thenumb.at/cpp-course/sdl2/01/01.html#rect) to reproduce a simple example and it looks like I have forgot to remove call to `SDL_GetWindowSurface`. Error has gone after I have removed call to `SDL_GetWindowSurface`. – vocasle May 04 '23 at 22:25
  • Cool! Glad it was something easy, feel free to self-answer :) – genpfault May 04 '23 at 23:10
  • 1
    Well, indeed this issue is resolved. Thank you a lot @genpfault. An interesting thing is that on Windows there were no issues. I was following [vulkan-tutorial.com](https://vulkan-tutorial.com/en/) but instead of GLFW I have chosen SDL2. And I have introduced call to `SDL_GetWindowSurface` [early on in main.cpp](https://github.com/vocasle/NEngine/commit/c16a6ac39241949013b04ee6289cb91132b06f26). – vocasle May 05 '23 at 01:27

1 Answers1

2

Turned out that the cause of "The specified window isn't a Vulkan window" error message on Linux is the call to SDL_GetWindowSurface before call to SDL_Vulkan_GetInstanceExtensions.

According to docs:

You may not combine this with 3D or the rendering API on this window.

vocasle
  • 799
  • 8
  • 11