MacOS OpenGL error in initializing offscreen context #58127
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#58127
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hi.
The first time when an offscreen OpenGL context is created with
GHOST_ContextCGL::createOffscreenContext()
,glClear(GL_COLOR_BUFFER_BIT)
is called whileglCheckFramebufferStatus(GL_FRAMEBUFFER)
isGL_FRAMEBUFFER_UNDEFINED
, resulting in an glError inGHOST_Context::initClearGL()
.When a new window with an (onscreen) OpenGLContext is created,
initClearGL()
is OK, becausemakeCurrentContext
did set the gl-context.Some possible (although maybe incorrect) fixes:
initClearGL()
when a window is created.initClearGL
andflushBuffer
) to another part in the initialization, afterGHOSTContextCGL::activateDrawingContext()
is called, so every operating system has the same initialization. This requires more code-refactoring, creating possible bugs at other operating systems, and I can only test MacOS.I think fix #1 is simple, solves at least a single glError and doesn't break anything. Not sure if
[m_openGLContext flushBuffer]
should also be disabled when creating an offscreen gl-context, but the flushing would be useless when nothing is done on this gl-context, right?Both possible fixes in #1 (version 1a or 1b) don't generate a glError.
Also fix #2 (remove both functions for on- and offscreen context) don't generate a glError.
OS: MacOS Mojave (10.14.1)
GPU: AMD Radeon RX 560
Added subscriber: @nielspl-2
#62184 was marked as duplicate of this issue
Hi. I looked at it again and it might be more problematic.
The class
NSOpenGLView
/NSOpenGLContext
is doing more gl-commands that keeps breaking it. With the help ofapitrace
I found out that it is somehow keeping a wrong GLPixelFormat cached somewhere.Not only is the unexpected attribute a problem, the other values were never defined anywhere in Blender so it is done by OSX.
When
[m_openGLContext flushBuffer]
is called, it internally callsCGLChoosePixelFormat()
.After disabling
flushBuffer
, the same would happen whenswapBuffers()
is called.On the more recent OSX version, all views are layer-backed views. (Also the end of subpixel antialiasing for fonts, which is noticeable on non-retina screens).
All NSView-classes automatically use the CALayer for drawing and NSView would handle the rest (Events etc.). So in reality, everything is offscreen-rendering and OSX can apply their own effects (like the bottle-minimizing-window-effect).
My suggestion would be to remove
NSOpenGLView
andNSOpenGLContext
completely, since Apple gave tips in 2011 on how to port your code fromNSOpenGLContext
toCGLContextObj
.(the old disabled, and new code in a single file)
https://github.com/ewmailing/GLEssentialsPortToCAOpenGLLayer/blob/master/Classes/Mac/NSGLView.m
Currently in Blender it is
CocoaOpenGLView -> NSOpenGLView -> NSView
.The new way would be
CocoaOpenGLView -> NSView
, whereCocoaOpenGLView
would still handle all events exactly as it is right now. But theNSView
's default layerCALayer
has to be aCAOpenGLLayer
.I have done some tests without success, but I think this is the way to go and keep trying.
To use apitrace MacOS 10.14.1 right now:
./apitrace trace /[PATH]/blender/_build/bin/Debug/blender.app/Contents/MacOS/blender
xCode tip:
Add a symbolic breakpoint in xCode like for example the
CGLChoosePixelFormat
. It also does a break when it was called somewhere internally, like in[m_openGLContext flushBuffer]
.Hi. I tried to create a "patch.diff", but it didn't work out very well (I'm not familiar with patching yet), so I will just upload 2 files in this post.
It contains changes in 2 files which won't affect the current build (AFAIK), but would be a start of the transition to remove NSOpenGLView/NSOpenGLContext.
diff #1:
blender/build_files/cmake/platform/platform_apple.cmake
only add:
-framework OpenGL
diff #2:
blender/intern/ghost/intern/GHOST_ContextCGL.mm
change:
static void makeAttribList(std::vector<NSOpenGLPixelFormatAttribute>& attribs, ...)
to
static void makeAttribList(std::vector<CGLPixelFormatAttribute>& attribs, ...)
and some changes to keep using the NSGLOpenGLContext as it is right now.
(I probably should have added
CGLReleasePixelFormat()
orCGLDestroyPixelFormat()
, but that isn't a big issue for now and it isn't documented very well either and those mem-leaks would be minimal, but must be noted. Releasing too much would definitely break things, and I don't know the retain cycle in this case.)platform_apple.cmake
GHOST_ContextCGL.mm
I would like to submit some bigger code-changes (as patches) in the future with the use of
CAOpenGLLayer
, so I was wondering if any developers are actively working on this part of MacOS?The code will be more C++ and less Objective-C++ with finer control over OpenGL calls and less fuzziness of MacOS's internal OpenGL handling.
Hi. Sorry for kind of spamming this topic.
The changes I'm making now will be a lot less than I previously thought were needed (hopefully).
CocoaOpenGLView
will stay a subclass ofNSOpenGLView
.The changes will mostly be removing
NSOpenGLContext
in favor ofCGLContext
.(Less Objective-C++ and less of Apple's "magic")
Makes sense, thanks for looking into this.
Hi. Here's a patch solves the current issue with the
FRAMEBUFFER_UNDEFINED
as mentioned in the opening post.The error was because no glViewport() was set before glClear(GL_COLOR_BUFFER_BIT) in ::initClearGL() was called.
When an on-screen context is created, the
[m_openGLView setOpenGLContext:]
will internally doglViewport
(and alsoglScissor
).NSOpenGLContext *prev_openGLContext
is changed because it is needed for off-screen contexts.(behavior is now more like to
prevHGLRC = ::wglGetCurrentContext();
inGHOST_ContextWGL.cpp
)NOTE: Only tested on non-retina screen.
fix-initializeDrawingContext.diff
This crashes in background mode for me, running
blender -b -E BLENDER_EEVEE -f 1
.Maybe clearing is not needed at all for an offscreen context though. We do that to show something in the window before the Blender UI is fully loaded, but for an offscreen context it does not matter.
In the error case at the end of the function, it's also restoring
prev_openGLContext
inm_openGLView
, but that's no longer correct if that's not what we're getting it from anymore. As far as I can tell the only place wheregoto error
happens is when we haven't modified the view though, so perhaps the code can be removed.Hi. I will upload a new patch later today (code cleanup and without the previous "fix").
I will also take a better look at your post and code and get back to you!
It's crashing here too. I'll take a closer look at it this weekend.
I'm putting the cleanup-patch here for a quick look, but it still needs some checking/revisions.
I also should check out 'virtual screens' because of:
[pixelFormat getValues:&actualSamples forAttribute:NSOpenGLPFASamples forVirtualScreen:0];
Some definitions are difficult to read: (snippets from docs ).
^^^ And this is just with 1 GPU and 1 Monitor. (next is multiple GPUs/monitors/screen-mirroring and hot-plugging eGPUs/monitors and GPU-switching, very complex)
relevant CGLPixelFormatAttributes:
kCGLPFAVirtualScreenCount
,kCGLPFARendererID
,kCGLPFADisplayMask
,kCGLPFASupportsAutomaticGraphicsSwitching
.Maybe a crazy idea now: Apple promotes their own Metal framework, but this can be mixed with OpenGL. Maybe the onscreen-buffer can be pushed into the Metal buffer and let them handle the rest. Lots of 'maybes', but maybe?
refactoring-ContextCGL.diff
A little debugging update:
Left-side image: with prev_openGLContext
Right-side image: without prev_openGLContext
It looks like it creates a new (unwanted and "unused") context. And also 2 unwanted. PixelFormats,
Since it's the first (on screen) context creation:
Just for reference:
Added subscriber: @intrigus-3
Changed status from 'Open' to: 'Resolved'
After
76019139f6
we are no longer clearing the buffer in offscreen contexts.