Skip to content

Commit 594476c

Browse files
authored
Check Opengl version is 3.3+ before creating a GL context over a GL ES context (#5996)
* Retry with GLES if creating a GL context fails * Cleaner GL context creation retry
1 parent 8c7c5c4 commit 594476c

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Bottom level categories:
4949

5050
#### General
5151

52+
- If GL context creation fails retry with GLES. By @Rapdorian in [#5996](https://github.com/gfx-rs/wgpu/pull/5996)
5253
- Fix profiling with `tracy`. By @waywardmonkeys in [#5988](https://github.com/gfx-rs/wgpu/pull/5988)
5354
- As a workaround for [issue #4905](https://github.com/gfx-rs/wgpu/issues/4905), `wgpu-core` is undocumented unless `--cfg wgpu_core_doc` feature is enabled. By @kpreid in [#5987](https://github.com/gfx-rs/wgpu/pull/5987)
5455
- Bump MSRV for `d3d12`/`naga`/`wgpu-core`/`wgpu-hal`/`wgpu-types`' to 1.76. By @wumpf in [#6003](https://github.com/gfx-rs/wgpu/pull/6003)

wgpu-hal/src/gles/egl.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -550,26 +550,25 @@ impl Inner {
550550
let supports_khr_context = display_extensions.contains("EGL_KHR_create_context");
551551

552552
let mut context_attributes = vec![];
553-
if supports_opengl {
554-
context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);
555-
context_attributes.push(3);
556-
context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);
557-
context_attributes.push(3);
558-
if force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {
559-
log::warn!("Ignoring specified GLES minor version as OpenGL is used");
560-
}
561-
} else {
562-
context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);
563-
context_attributes.push(3); // Request GLES 3.0 or higher
564-
if force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {
565-
context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);
566-
context_attributes.push(match force_gles_minor_version {
567-
wgt::Gles3MinorVersion::Automatic => unreachable!(),
568-
wgt::Gles3MinorVersion::Version0 => 0,
569-
wgt::Gles3MinorVersion::Version1 => 1,
570-
wgt::Gles3MinorVersion::Version2 => 2,
571-
});
572-
}
553+
let mut gl_context_attributes = vec![];
554+
let mut gles_context_attributes = vec![];
555+
gl_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);
556+
gl_context_attributes.push(3);
557+
gl_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);
558+
gl_context_attributes.push(3);
559+
if supports_opengl && force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {
560+
log::warn!("Ignoring specified GLES minor version as OpenGL is used");
561+
}
562+
gles_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);
563+
gles_context_attributes.push(3); // Request GLES 3.0 or higher
564+
if force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {
565+
gles_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);
566+
gles_context_attributes.push(match force_gles_minor_version {
567+
wgt::Gles3MinorVersion::Automatic => unreachable!(),
568+
wgt::Gles3MinorVersion::Version0 => 0,
569+
wgt::Gles3MinorVersion::Version1 => 1,
570+
wgt::Gles3MinorVersion::Version2 => 2,
571+
});
573572
}
574573
if flags.contains(wgt::InstanceFlags::DEBUG) {
575574
if version >= (1, 5) {
@@ -606,15 +605,31 @@ impl Inner {
606605
context_attributes.push(khr_context_flags);
607606
}
608607
context_attributes.push(khronos_egl::NONE);
609-
let context = match egl.create_context(display, config, None, &context_attributes) {
610-
Ok(context) => context,
611-
Err(e) => {
612-
return Err(crate::InstanceError::with_source(
613-
String::from("unable to create GLES 3.x context"),
614-
e,
615-
));
616-
}
617-
};
608+
609+
gl_context_attributes.extend(&context_attributes);
610+
gles_context_attributes.extend(&context_attributes);
611+
612+
let context = if supports_opengl {
613+
egl.create_context(display, config, None, &gl_context_attributes)
614+
.or_else(|_| {
615+
egl.bind_api(khronos_egl::OPENGL_ES_API).unwrap();
616+
egl.create_context(display, config, None, &gles_context_attributes)
617+
})
618+
.map_err(|e| {
619+
crate::InstanceError::with_source(
620+
String::from("unable to create OpenGL or GLES 3.x context"),
621+
e,
622+
)
623+
})
624+
} else {
625+
egl.create_context(display, config, None, &gles_context_attributes)
626+
.map_err(|e| {
627+
crate::InstanceError::with_source(
628+
String::from("unable to create GLES 3.x context"),
629+
e,
630+
)
631+
})
632+
}?;
618633

619634
// Testing if context can be binded without surface
620635
// and creating dummy pbuffer surface if not.

0 commit comments

Comments
 (0)