EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
CMLogMessage("Failure to get EGL display");
return;
}
EGLint major, minor;
if (!eglInitialize(display, &major, &minor)) {
CMLogMessage("Failure to initialize EGL");
return;
}
// Step 2: Choose EGL configuration
EGLint attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8,
EGL_NONE
};
EGLConfig config;
EGLint numConfigs;
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs) || numConfigs == 0) {
CMLogMessage("Failure to choose EGL config");
eglTerminate(display);
return;
}
// Step 3: Create EGL context
EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 3, // OpenGL ES 3.0
EGL_NONE
};
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT) {
CMLogMessage("Failure to create EGL context");
eglTerminate(display);
return;
}
// Step 4: Create a pbuffer surface for offscreen rendering
EGLint pbufferAttribs[] = {
EGL_WIDTH, 1,
EGL_HEIGHT, 1,
EGL_NONE
};
EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttribs);
if (surface == EGL_NO_SURFACE) {
CMLogMessage("Failure to create EGL pbuffer surface");
eglDestroyContext(display, context);
eglTerminate(display);
return;
}
// Step 5: Make the context current
if (!eglMakeCurrent(display, surface, surface, context)) {
CMLogMessage("Failure to make EGL context current");
eglDestroySurface(display, surface);
eglDestroyContext(display, context);
eglTerminate(display);
return;
}
Hi, I encountered the same issue when using multi-threading with D3D11 backend. The cause was the same: multiple threads acquired the same EGLDisplay, and the underlying of EGLDisplay corresponded to the same StateManager (when switching threads, the current context did not correspond to the new thread, causing the issue). I tried commenting out the following code, which resolved the issue, but I'm unsure of the potential implications. Would this affect off-screen rendering and on-screen rendering differently?
Additionally, I'd like to ask: is the Vulkan backend thread-safe because it has been specially optimized at the lower level (or does not use a global StateManager/StateCache) to support multithreading?