Skip to content

Commit 8a9bc3e

Browse files
committed
EGL: Improved initialization.
1 parent 2e6ad92 commit 8a9bc3e

File tree

4 files changed

+121
-37
lines changed

4 files changed

+121
-37
lines changed

src/bgfx.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ namespace bgfx
14681468
return RenderFrame::NoContext;
14691469
}
14701470

1471-
const uint32_t g_uniformTypeSize[UniformType::Count+1] =
1471+
const uint32_t g_uniformTypeSize[] =
14721472
{
14731473
sizeof(int32_t),
14741474
0,
@@ -1477,6 +1477,7 @@ namespace bgfx
14771477
4*4*sizeof(float),
14781478
1,
14791479
};
1480+
static_assert(UniformType::Count+1 == BX_COUNTOF(g_uniformTypeSize), "Must match UniformType::Enum!");
14801481

14811482
void UniformBuffer::writeUniform(UniformType::Enum _type, uint16_t _loc, const void* _value, uint16_t _num)
14821483
{

src/glcontext_egl.cpp

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,8 @@ namespace bgfx { namespace gl
3535

3636
#if BGFX_USE_GL_DYNAMIC_LIB
3737

38-
typedef void (*EGLPROC)(void);
39-
40-
typedef EGLBoolean (EGLAPIENTRY* PGNEGLBINDAPIPROC)(EGLenum api);
41-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLCHOOSECONFIGPROC)(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs, EGLint config_size, EGLint* num_config);
42-
typedef EGLContext (EGLAPIENTRY* PFNEGLCREATECONTEXTPROC)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint* attrib_list);
43-
typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEPBUFFERSURFACEPROC)(EGLDisplay display, EGLConfig config, EGLint const* attrib_list);
44-
typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEWINDOWSURFACEPROC)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list);
45-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYCONTEXTPROC)(EGLDisplay dpy, EGLContext ctx);
46-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYSURFACEPROC)(EGLDisplay dpy, EGLSurface surface);
47-
typedef EGLContext (EGLAPIENTRY* PFNEGLGETCURRENTCONTEXTPROC)(void);
48-
typedef EGLSurface (EGLAPIENTRY* PFNEGLGETCURRENTSURFACEPROC)(EGLint readdraw);
49-
typedef EGLDisplay (EGLAPIENTRY* PFNEGLGETDISPLAYPROC)(EGLNativeDisplayType display_id);
50-
typedef EGLint (EGLAPIENTRY* PFNEGLGETERRORPROC)(void);
51-
typedef EGLPROC (EGLAPIENTRY* PFNEGLGETPROCADDRESSPROC)(const char* procname);
52-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLINITIALIZEPROC)(EGLDisplay dpy, EGLint* major, EGLint* minor);
53-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLMAKECURRENTPROC)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
54-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPBUFFERSPROC)(EGLDisplay dpy, EGLSurface surface);
55-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPINTERVALPROC)(EGLDisplay dpy, EGLint interval);
56-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLTERMINATEPROC)(EGLDisplay dpy);
57-
typedef const char* (EGLAPIENTRY* PGNEGLQUERYSTRINGPROC)(EGLDisplay dpy, EGLint name);
58-
typedef EGLBoolean (EGLAPIENTRY* PFNEGLGETCONFIGATTRIBPROC)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
59-
6038
#define EGL_IMPORT \
61-
EGL_IMPORT_FUNC(PGNEGLBINDAPIPROC, eglBindAPI); \
39+
EGL_IMPORT_FUNC(PFNEGLBINDAPIPROC, eglBindAPI); \
6240
EGL_IMPORT_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); \
6341
EGL_IMPORT_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); \
6442
EGL_IMPORT_FUNC(PFNEGLCREATEPBUFFERSURFACEPROC, eglCreatePbufferSurface); \
@@ -67,6 +45,7 @@ namespace bgfx { namespace gl
6745
EGL_IMPORT_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); \
6846
EGL_IMPORT_FUNC(PFNEGLGETCURRENTCONTEXTPROC, eglGetCurrentContext); \
6947
EGL_IMPORT_FUNC(PFNEGLGETCURRENTSURFACEPROC, eglGetCurrentSurface); \
48+
EGL_IMPORT_FUNC(PFNEGLGETPLATFORMDISPLAYPROC, eglGetPlatformDisplay); \
7049
EGL_IMPORT_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); \
7150
EGL_IMPORT_FUNC(PFNEGLGETERRORPROC, eglGetError); \
7251
EGL_IMPORT_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); \
@@ -76,7 +55,8 @@ namespace bgfx { namespace gl
7655
EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers); \
7756
EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
7857
EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate); \
79-
EGL_IMPORT_FUNC(PGNEGLQUERYSTRINGPROC, eglQueryString); \
58+
EGL_IMPORT_FUNC(PFNEGLQUERYSTRINGPROC, eglQueryString); \
59+
EGL_IMPORT_FUNC(PFNEGLGETCONFIGSPROC, eglGetConfigs); \
8060
EGL_IMPORT_FUNC(PFNEGLGETCONFIGATTRIBPROC, eglGetConfigAttrib); \
8161

8262
#define EGL_IMPORT_FUNC(_proto, _func) _proto _func
@@ -328,7 +308,101 @@ WL_EGL_IMPORT
328308
const bimg::ImageBlockInfo& colorBlockInfo = bimg::getBlockInfo(bimg::TextureFormat::Enum(_resolution.formatColor) );
329309
const bimg::ImageBlockInfo& depthStecilBlockInfo = bimg::getBlockInfo(bimg::TextureFormat::Enum(_resolution.formatDepthStencil) );
330310

331-
EGLint numConfig = 0;
311+
EGLint numConfigs = 0;
312+
EGLConfig configs[256];
313+
eglGetConfigs(m_display, configs, BX_COUNTOF(configs), &numConfigs);
314+
315+
BX_TRACE("");
316+
BX_TRACE("Number of EGL configs %d:", numConfigs);
317+
BX_TRACE("\t +--------------------------------------------------------------- Configuration number.");
318+
BX_TRACE("\t | Renderable type bits:");
319+
BX_TRACE("\t | +----------------------------------------------------------- - EGL_OPENGL_BIT");
320+
BX_TRACE("\t | |+---------------------------------------------------------- - EGL_OPENGL_ES2_BIT");
321+
BX_TRACE("\t | ||+--------------------------------------------------------- - EGL_OPENGL_ES3_BIT_KHR");
322+
BX_TRACE("\t | ||| Surface type bits:");
323+
BX_TRACE("\t | ||| +---------------------------------------------------- - EGL_SWAP_BEHAVIOR_PRESERVED_BIT");
324+
BX_TRACE("\t | ||| |+--------------------------------------------------- - EGL_MULTISAMPLE_RESOLVE_BOX_BIT");
325+
BX_TRACE("\t | ||| ||+-------------------------------------------------- - EGL_PBUFFER_BIT");
326+
BX_TRACE("\t | ||| |||+------------------------------------------------- - EGL_PIXMAP_BIT");
327+
BX_TRACE("\t | ||| ||||+------------------------------------------------ - EGL_WINDOW_BIT");
328+
BX_TRACE("\t | ||| ||||| ");
329+
BX_TRACE("\t | ||| ||||| +------------------------------------------- R size.");
330+
BX_TRACE("\t | ||| ||||| | +--------------------------------------- G size.");
331+
BX_TRACE("\t | ||| ||||| | | +----------------------------------- B size.");
332+
BX_TRACE("\t | ||| ||||| | | | +------------------------------- A size.");
333+
BX_TRACE("\t | ||| ||||| | | | | +--------------------------- Depth size.");
334+
BX_TRACE("\t | ||| ||||| | | | | | +----------------------- Stencil size.");
335+
BX_TRACE("\t | ||| ||||| | | | | | | ");
336+
BX_TRACE("\t | ||| ||||| | | | | | | +------------------- Min swap interval.");
337+
BX_TRACE("\t | ||| ||||| | | | | | | | +--------------- Max swap interval.");
338+
BX_TRACE("\t | ||| ||||| | | | | | | | | +----------- Samples");
339+
BX_TRACE("\t | ||| ||||| | | | | | | | | | +------- Sample buffers.");
340+
BX_TRACE("\t | ||| ||||| | | | | | | | | | | ");
341+
BX_TRACE("\t | ||| ||||| | | | | | | | | | | +-- Config ID.");
342+
BX_TRACE("\t | ||| ||||| | | | | | | | | | | | ");
343+
344+
EGLint maxSamples = 0;
345+
346+
for (EGLint ii = 0; ii < numConfigs; ++ii)
347+
{
348+
char buffer[1024];
349+
bx::StaticMemoryBlockWriter smbw(buffer, sizeof(buffer)-1);
350+
351+
int32_t total = 0;
352+
353+
total += bx::write(&smbw, bx::ErrorAssert{}, "%3d: ", ii);
354+
355+
EGLint renderableTypeBits;
356+
eglGetConfigAttrib(m_display, configs[ii], EGL_RENDERABLE_TYPE, &renderableTypeBits);
357+
358+
total += bx::write(&smbw, bx::ErrorAssert{}, "[%c%c%c]"
359+
, renderableTypeBits & EGL_OPENGL_BIT ? 'x' : ' '
360+
, renderableTypeBits & EGL_OPENGL_ES2_BIT ? 'x' : ' '
361+
, renderableTypeBits & EGL_OPENGL_ES3_BIT_KHR ? 'x' : ' '
362+
);
363+
364+
EGLint surfaceTypeBits;
365+
eglGetConfigAttrib(m_display, configs[ii], EGL_SURFACE_TYPE, &surfaceTypeBits);
366+
367+
total += bx::write(&smbw, bx::ErrorAssert{}, ", [%c%c%c%c%c]"
368+
, surfaceTypeBits & EGL_MULTISAMPLE_RESOLVE_BOX_BIT ? 'x' : ' '
369+
, surfaceTypeBits & EGL_PBUFFER_BIT ? 'x' : ' '
370+
, surfaceTypeBits & EGL_PIXMAP_BIT ? 'x' : ' '
371+
, surfaceTypeBits & EGL_SWAP_BEHAVIOR_PRESERVED_BIT ? 'x' : ' '
372+
, surfaceTypeBits & EGL_WINDOW_BIT ? 'x' : ' '
373+
);
374+
375+
EGLint samples;
376+
eglGetConfigAttrib(m_display, configs[ii], EGL_SAMPLES, &samples);
377+
maxSamples = bx::max(maxSamples, samples);
378+
379+
#define GET_CONFIG_ATTRIB(_attrib, _format) \
380+
{ \
381+
EGLint attribValue; \
382+
eglGetConfigAttrib(m_display, configs[ii], _attrib, &attribValue); \
383+
total += bx::write(&smbw, bx::ErrorAssert{}, _format, attribValue); \
384+
}
385+
386+
GET_CONFIG_ATTRIB(EGL_RED_SIZE, ", %2d");
387+
GET_CONFIG_ATTRIB(EGL_GREEN_SIZE, ", %2d");
388+
GET_CONFIG_ATTRIB(EGL_BLUE_SIZE, ", %2d");
389+
GET_CONFIG_ATTRIB(EGL_ALPHA_SIZE, ", %2d");
390+
GET_CONFIG_ATTRIB(EGL_DEPTH_SIZE, ", %2d");
391+
GET_CONFIG_ATTRIB(EGL_STENCIL_SIZE, ", %2d");
392+
393+
GET_CONFIG_ATTRIB(EGL_MIN_SWAP_INTERVAL, ", %2d");
394+
GET_CONFIG_ATTRIB(EGL_MAX_SWAP_INTERVAL, ", %2d");
395+
GET_CONFIG_ATTRIB(EGL_SAMPLES, ", %2d");
396+
GET_CONFIG_ATTRIB(EGL_SAMPLE_BUFFERS, ", %2d");
397+
398+
GET_CONFIG_ATTRIB(EGL_CONFIG_ID, ", %3d");
399+
400+
#undef GET_CONFIG_ATTRIB
401+
402+
buffer[total] = '\0';
403+
404+
BX_TRACE("\t%s", buffer);
405+
}
332406

333407
for (uint32_t retry = 0; retry < 2; ++retry)
334408
{
@@ -337,9 +411,9 @@ WL_EGL_IMPORT
337411

338412
attrs[numAttrs++] = EGL_RENDERABLE_TYPE;
339413
attrs[numAttrs++] = !!BGFX_CONFIG_RENDERER_OPENGL
340-
? EGL_OPENGL_BIT
341-
: (glVersion >= 30) ? EGL_OPENGL_ES3_BIT_KHR : EGL_OPENGL_ES2_BIT
342-
;
414+
? EGL_OPENGL_BIT
415+
: (glVersion >= 30) ? EGL_OPENGL_ES3_BIT_KHR : EGL_OPENGL_ES2_BIT
416+
;
343417

344418
attrs[numAttrs++] = EGL_SURFACE_TYPE;
345419
attrs[numAttrs++] = headless ? EGL_PBUFFER_BIT : EGL_WINDOW_BIT;
@@ -363,7 +437,7 @@ WL_EGL_IMPORT
363437
attrs[numAttrs++] = depthStecilBlockInfo.stencilBits;
364438

365439
attrs[numAttrs++] = EGL_SAMPLES;
366-
attrs[numAttrs++] = (EGLint)msaaSamples;
440+
attrs[numAttrs++] = (EGLint)bx::min(msaaSamples, maxSamples);
367441

368442
if (hasEglAndroidRecordable)
369443
{
@@ -378,10 +452,10 @@ WL_EGL_IMPORT
378452
, BX_COUNTOF(attrs)
379453
);
380454

381-
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
455+
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfigs);
382456

383457
if (!success
384-
|| 0 == numConfig)
458+
|| 0 == numConfigs)
385459
{
386460
msaaSamples = 0;
387461
continue;
@@ -390,7 +464,7 @@ WL_EGL_IMPORT
390464
break;
391465
}
392466

393-
BGFX_FATAL(0 != numConfig, Fatal::UnableToInitialize, "eglChooseConfig");
467+
BGFX_FATAL(0 != numConfigs, Fatal::UnableToInitialize, "eglChooseConfig");
394468

395469
m_msaaContext = 1 < msaaSamples;
396470

@@ -529,7 +603,7 @@ WL_EGL_IMPORT
529603
break;
530604
}
531605

532-
BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags);
606+
BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x). Retrying without it!", flags);
533607
}
534608

535609
BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
@@ -693,13 +767,17 @@ WL_EGL_IMPORT
693767

694768
# if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
695769
# if BX_PLATFORM_WINDOWS
696-
# define LIBRARY_NAME "libGL.dll"
770+
# if BGFX_CONFIG_RENDERER_OPENGL
771+
# define LIBRARY_NAME "libGL.dll"
772+
# else
773+
# define LIBRARY_NAME "libGLESv2.dll"
774+
# endif // BGFX_CONFIG_RENDERER_OPENGL
697775
# elif BX_PLATFORM_LINUX
698776
# if BGFX_CONFIG_RENDERER_OPENGL
699777
# define LIBRARY_NAME "libGL.so.1"
700778
# else
701779
# define LIBRARY_NAME "libGLESv2.so.2"
702-
# endif
780+
# endif // BGFX_CONFIG_RENDERER_OPENGL
703781
# endif
704782

705783
void* lib = bx::dlopen(LIBRARY_NAME);

src/glcontext_egl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#if BGFX_USE_EGL
1010

11+
#if BGFX_USE_GL_DYNAMIC_LIB
12+
# define EGL_EGL_PROTOTYPES 0
13+
#endif // BGFX_USE_GL_DYNAMIC_LIB
14+
1115
#include <EGL/egl.h>
1216
#include <EGL/eglext.h>
1317

src/renderer_gl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
|| BX_PLATFORM_LINUX \
1212
|| BX_PLATFORM_NX \
1313
|| BX_PLATFORM_RPI \
14-
) )
14+
) ) \
15+
|| (BGFX_CONFIG_RENDERER_OPENGLES && BX_PLATFORM_WINDOWS)
1516

1617
#define BGFX_USE_HTML5 (BGFX_CONFIG_RENDERER_OPENGLES && (0 \
1718
|| BX_PLATFORM_EMSCRIPTEN \

0 commit comments

Comments
 (0)