NSOpenGLView:openGLContext 在 DisplayLink 回调中返回 nil

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

我遇到了上下文消失的问题。我有一个简单的 NSOpenGLView 子类。我在 initWithFrame 中定义像素格式,并随后验证我是否具有有效的上下文。我在 awakeFromNib 中做了一些额外的 gl 设置,并验证我是否仍然具有有效的上下文。我使用 DisplayLink 进行主要渲染。在显示链接回调中,[self openGLContext]返回nil。在调试器中,我可以看到 _openGLContext 实例变量仍然有效 - 但访问器返回 nil。我错过了什么?

------编辑:包含代码-------------

MyGLView.h

@interface MyGLView : NSOpenGLView

- (void)displayLinkDraw;

@end

MyGLView.m

- (id)initWithFrame:(NSRect)frameRect
{
    NSOpenGLPixelFormat        *windowedPixelFormat;
    NSOpenGLPixelFormatAttribute    attribs[] = {
        NSOpenGLPFAWindow,
        NSOpenGLPFAColorSize, 32,
        NSOpenGLPFAAccelerated,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFASingleRenderer,
        0 };

    windowedPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
    self = [super initWithFrame:frameRect pixelFormat:windowedPixelFormat];

    // set synch to VBL to eliminate tearing
    GLint    vblSynch = 1;
    [[self openGLContext] setValues:&vblSynch forParameter:NSOpenGLCPSwapInterval];

    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns valid context here. ***
    return self;
}

- (void)awakeFromNib
{
    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns valid context here. ***

    [self.openGLContext makeCurrentContext];

    glDisable(GL_DEPTH_TEST);

    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);

    glEnableVertexAttribArray(ATTRIB_TEXCOORD);
    glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);

    glGenFramebuffers(1, &_frameBufferID);
    glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);

    glGenRenderbuffers(1, &_colorBufferID);
    glBindRenderbuffer(GL_RENDERBUFFER, _colorBufferID);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, self.frame.size.width, self.frame.size.height);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorBufferID);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

- (void)displayLinkDraw
{
    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns nil here??? ***
    [context makeCurrentContext];

    CGLLockContext([context CGLContextObj]);

    glClearColor(0.9f, 0.9f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    // ...

    [context flushBuffer];

    CGLUnlockContext([context CGLContextObj]);
}

MyViewController.m

static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext)
{
    MyViewController* self = (__bridge MyViewController *)displayLinkContext;
    MyGLView* glView = self->_myGLView;

    [glView displayLinkDraw];

    return kCVReturnSuccess;
}
macos opengl
1个回答
0
投票

Apple 的 GLEssentials 示例应用程序在

CVDisplayLink
子类中具有功能正常的
NSOpenGLView
循环。 Apple 还有关于显示链接设置的技术问答

我已经在我自己的基本 OpenGL 动画中成功地使用了这种方法。 你的

CVDisplayLink
设置代码应该看起来像这样(我的代码是从我的
NSOpenGLView
子类运行的,你的似乎是从视图控制器设置的)。
如果您没有使用您设置的上下文和像素格式调用
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext()
,则可能选择了无法访问该上下文的显示链接。

// Create a display link capable of being used with all active displays
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);

// Set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void *)(self));

// Set the display link for the current renderer
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);

// Activate the display link
CVDisplayLinkStart(displayLink);
© www.soinside.com 2019 - 2024. All rights reserved.