OSX OpenGL设置代码中的竞争条件

时间:2016-12-24 19:32:04

标签: c++ macos cocoa opengl sdl

我试图在OSX上的一个单独的线程中进行OpenGL渲染。我使用SDL创建窗口,但我想手动编写OpenGL上下文创建代码。有时它工作正常(它应该在红色区域中显示绿色方块),但有时它只显示白色。

如果我只在一个线程中运行(下面有#define来打开和关闭它),一切正常。如果我插入一个停顿(一个for循环计数到1000万,另一个#define开关控制它)它工作正常,这让我相信我有一个竞争条件,我需要阻止渲染线程,直到操作系统是做了它做的任何事情。

不熟悉Cocoa或Objective-C,我该怎么做?或者我的问题是别的吗?

代码如下:

#include </Library/Frameworks/SDL2.framework/Headers/SDL.h>
#include </Library/Frameworks/SDL2.framework/Headers/SDL_syswm.h>
#include <OpenGL/GL3.h>
#include <array>
#import <Cocoa/Cocoa.h>
#include <OpenGL/CGLTypes.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLRenderers.h>
#include <thread>

namespace
{
    float const PositionData[] =
    {
        -0.5f,-0.5f,0,   0,0,
         0.5f,-0.5f,0,   0,0,
         0.5f, 0.5f,0,   0,0,
         0.5f, 0.5f,0,   0,0,
        -0.5f, 0.5f,0,   0,0,
        -0.5f,-0.5f,0,   0,0,
    };

    namespace buffer
    {
        enum type
        {
            VERTEX,
            TRANSFORM,
            MATERIAL,
            MAX
        };
    }//namespace buffer
}//namespace

#define RENDER_THREAD
#define BLOCK_RENDER_THREAD

int main() {
    Uint32 init_mode = SDL_INIT_VIDEO | SDL_INIT_TIMER;

#ifdef _DEBUG
    init_mode |= SDL_INIT_NOPARACHUTE;
#endif

    SDL_Init(init_mode);

    Uint32 window_mode = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE;

    SDL_Window* window;

    if (NULL == (window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, window_mode))) {
        SDL_Quit();
        return 1;
    }

    SDL_SysWMinfo wmi;
    SDL_VERSION(&wmi.version);

    if (!SDL_GetWindowWMInfo(window, &wmi) )
    {
        return 1;
    }

    std::atomic<bool> closing(false);

    auto PollEventQueue = [&closing]() {
        SDL_Event e;

        while (SDL_PollEvent(&e)) {
            switch (e.type)
            {
            case SDL_QUIT: {
                closing = true;
            } break;

            default: {
            } break;
            }
        }
    };

    NSWindow* native_window = wmi.info.cocoa.window;

    auto RenderThreadMain = [native_window, &closing, PollEventQueue]() {
#ifdef BLOCK_RENDER_THREAD
        for (int k = 0; k < 10000000; k++) {}
#endif

        NSOpenGLContext* context;
        @synchronized (native_window) {
            NSOpenGLPixelFormat *pixel_format = nullptr;

            NSOpenGLPixelFormatAttribute attributes[64] = {
                NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core,
                NSOpenGLPFAColorSize, 24,
                NSOpenGLPFAAlphaSize, 8,
                NSOpenGLPFADepthSize, 24,
                NSOpenGLPFAStencilSize, 8,

                NSOpenGLPFADoubleBuffer,
                NSOpenGLPFAAccelerated,
                NSOpenGLPFANoRecovery,

                0
            };

            NSOpenGLPixelFormatAttribute* the_end = std::find_if(std::begin(attributes), std::end(attributes), [](NSOpenGLPixelFormatAttribute attribute) {
                return attribute == 0;
            });

            if (true) {
                NSOpenGLPixelFormatAttribute multisample_attributes[] = {
                    NSOpenGLPFAMultisample,
                    NSOpenGLPFASampleBuffers, NSOpenGLPixelFormatAttribute(1),
                    NSOpenGLPFASamples, NSOpenGLPixelFormatAttribute(4),
                    0
                };

                // Copy it onto the attributes array
                int k = 0;
                while (multisample_attributes[k]) {
                    *(the_end++) = multisample_attributes[k++];
                }
            }

            NSView* native_view = [native_window contentView];
            NSRect native_rect = [native_view bounds];

            pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
            NSOpenGLView* gl_view = [[NSOpenGLView alloc] initWithFrame:native_rect pixelFormat:pixel_format];
            [pixel_format release];

            [gl_view setAutoresizingMask:
                (NSViewHeightSizable|NSViewWidthSizable|NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin)
            ];
            [native_view addSubview:gl_view];

            context = [gl_view openGLContext];

            GLint swap_interval = 1;
            [context setValues:&swap_interval forParameter:NSOpenGLCPSwapInterval];

            [context setView:[native_window contentView]];

            [context makeCurrentContext];
        }

        std::array<GLuint, buffer::MAX> BufferName;
        GLuint ProgramName;
        GLuint VertexArrayName;
        GLint UniformTransform;
        GLint UniformMaterial;

        const char* vertex_shader =
            "#version 150 core\n"
            "in vec3 Position;"
            "in vec2 UV;"
            "void main()"
            "{"
            "   gl_Position = vec4(Position, 1.0);"
            "}";
        const GLint vertex_shader_length = (GLint)strlen(vertex_shader);

        const char* fragment_shader =
            "#version 150 core\n"
            "out vec4 Color;"
            "void main()"
            "{"
            "   Color = vec4(0.0, 1.0, 0.0, 1.0);"
            "}";
        const GLint fragment_shader_length = (GLint)strlen(fragment_shader);

        GLuint fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource((GLuint)fragment_shader_id, 1, &fragment_shader, &fragment_shader_length);
        glCompileShader((GLuint)fragment_shader_id);

        int shader_compiled;
        glGetShaderiv((GLuint)fragment_shader_id, GL_COMPILE_STATUS, &shader_compiled);
        if (shader_compiled != GL_TRUE) {
            int log_length = 0;
            char log[1024];
            glGetShaderInfoLog((GLuint)fragment_shader_id, 1024, &log_length, log);
            printf("%s", log);
            return 1; // TODO: Error
        }

        GLuint vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource((GLuint)vertex_shader_id, 1, &vertex_shader, &vertex_shader_length);
        glCompileShader((GLuint)vertex_shader_id);

        glGetShaderiv((GLuint)vertex_shader_id, GL_COMPILE_STATUS, &shader_compiled);
        if (shader_compiled != GL_TRUE) {
            int log_length = 0;
            char log[1024];
            glGetShaderInfoLog((GLuint)vertex_shader_id, 1024, &log_length, log);
            printf("%s", log);
            return 1; // TODO: Error
        }

        ProgramName = glCreateProgram();
        glAttachShader(ProgramName, fragment_shader_id);
        glAttachShader(ProgramName, vertex_shader_id);

        glBindAttribLocation(ProgramName, 0, "Position");
        glLinkProgram(ProgramName);

        glGenBuffers(buffer::MAX, &BufferName[0]);

        glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(PositionData), PositionData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glGenVertexArrays(1, &VertexArrayName);
        glBindVertexArray(VertexArrayName);
            glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
            glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (const GLvoid *)(3*sizeof(float)));

            glEnableVertexAttribArray(0);
            glEnableVertexAttribArray(4);
        glBindVertexArray(0);

        while (!closing) {
#ifndef RENDER_THREAD
            PollEventQueue();
#endif

            glClearColor(1, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);

            glUseProgram(ProgramName);
            glBindVertexArray(VertexArrayName);
            glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 2);

            [context flushBuffer];
            [context update];
        }

        return 0;
    };

#ifdef RENDER_THREAD
    std::thread render_thread = std::thread(RenderThreadMain);

    while (!closing) {
        PollEventQueue();
    }

    render_thread.join();
#else
    RenderThreadMain();
#endif

    return 0;
}

编译:

clang++ test.mm -framework OpenGL -framework Cocoa -framework SDL2 -F/Library/Frameworks -std=c++14 -g

1 个答案:

答案 0 :(得分:2)

首先,您应该熟悉Apple的Thread Safety Summary Cocoa(尤其是AppKit)。在那里,您将了解到您不应该从辅助线程操纵视图层次结构。特别是,对-addSubview:的调用很糟糕。 (请注意,有关从那里描述的后台线程绘制的限制仅适用于传统绘图.OpenGL不要求您将焦点锁定在视图上。)

使用@synchronized(native_window)并不符合您的想法。它仅与在同一对象上明确使用@synchronized()的其他代码同步。 通常与只使用或在该窗口上操作的任何内容同步。我没有理由相信Cocoa中的任何东西在它的窗口上都有@synchronized(),所以你无需同步。

由于窗口和上下文的设置是一次性的,所以它可能只是在生成辅助线程之前在主线程上完成。

其次,您正在创建一个NSOpenGLView并从中获取上下文,但随后您将告诉上下文将自己与不同的视图(窗口的contentView)相关联。你为什么这样做? NSOpenGLView拥有该上下文,它应该与该视图保持关联。

最后,你正在使用的自动调整大小的掩码看起来很奇怪。由于您允许一切都变得灵活,因此GL视图将不会与窗口的内容视图保持同步。它会越来越慢地增长和缩小,它周围的边缘会增加并且会失去松弛。我猜你只想要NSViewHeightSizable|NSViewWidthSizable,以便视图一起调整,边距保持为0.