尝试加载 OpenGL 4.1,但在 MacOS 上只能获取 OpenGL 2.1

问题描述 投票:0回答:1

我正在尝试在 Macbook Pro 14(M2 Pro CPU、Macos Sonoma 14.5)中设置基于 OpenGL 的项目。我基本上已经设法让一切正常工作,但无论我做什么,我都无法让程序使用 OpenGL 4.1,而是使用 2.1 加载。

为此,我使用 SDL3、Glad 和 Zig,但我只是加载相同的库,就好像我使用 C 或 C++ 一样。这是我的初始化代码:

pub fn initialize_sdl() !SDLComponents {
    c.SDL_Log("Initializing SDL");

    // Very important to set these before initializing SDL
    _ = c.SDL_GL_LoadLibrary(null);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_ACCELERATED_VISUAL, 1);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, c.GL_TRUE);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_PROFILE_MASK, c.SDL_GL_CONTEXT_PROFILE_CORE);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_MINOR_VERSION, 1);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_DOUBLEBUFFER, 1);
    _ = c.SDL_GL_SetAttribute(c.SDL_GL_DEPTH_SIZE, 24);

    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
        c.SDL_LogError(c.SDL_LOG_CATEGORY_RENDER, "Unable to initialize SDL: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    }

    const window: *c.SDL_Window = c.SDL_CreateWindow("Powder3D", 400, 400, c.SDL_WINDOW_OPENGL | c.SDL_WINDOW_RESIZABLE) orelse {
        c.SDL_LogError(c.SDL_LOG_CATEGORY_RENDER, "Unable to create window: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    };

    const open_gl_context: c.SDL_GLContext = c.SDL_GL_CreateContext(window) orelse {
        c.SDL_LogError(c.SDL_LOG_CATEGORY_RENDER, "Unable to create OpenGL context: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    };

    if (c.gladLoadGLLoader(sdlGLGetProcAddress) == 0) {
        c.SDL_LogError(c.SDL_LOG_CATEGORY_RENDER, "Unable to load OpenGL functions with glad");
        return error.GLADInitializationFailed;
    }

    const versionStringPtr = c.glGetString(c.GL_VERSION) orelse {
        c.SDL_LogError(c.SDL_LOG_CATEGORY_RENDER, "Could not get GL version");
        return error.OPENGLInitializationFailed;
    };

    const versionString = std.mem.span(versionStringPtr);
    std.debug.print("OpenGL version: {s}\n", .{versionString});

    return .{ .window = window, .context = open_gl_context };
}

报告的 open gl 版本(代码将其打印在底部)输出“2.1 Metal - 88.1”。浏览网络后,大多数人都通过设置

SDL_GL_CONTEXT_PROFILE_MASK
SDL_GL_CONTEXT_PROFILE_CORE
值来解决此问题,但这对我来说并不起作用。

macos opengl sdl zig glad
1个回答
0
投票

SDL_GL_SetAttribute()
调用需要在
SDL_Init(SDL_INIT_VIDEO)
之后但在
SDL_CreateWindow()
之前进行才有效:

/**
 * Set an OpenGL window attribute before window creation.
 *
 * This function sets the OpenGL attribute `attr` to `value`. The requested
 * attributes should be set before creating an OpenGL window. You should use
 * SDL_GL_GetAttribute() to check the values after creating the OpenGL
 * context, since the values obtained can differ from the requested ones.
 *
 * \param attr an SDL_GLattr enum value specifying the OpenGL attribute to set
 * \param value the desired value for the attribute
 * \returns 0 on success or a negative error code on failure; call
 *          SDL_GetError() for more information.
 *
 * \since This function is available since SDL 2.0.0.
 *
 * \sa SDL_GL_GetAttribute
 * \sa SDL_GL_ResetAttributes
 */
extern DECLSPEC int SDLCALL SDL_GL_SetAttribute(SDL_GLattr attr, int value);
© www.soinside.com 2019 - 2024. All rights reserved.