Skip to content

Commit

Permalink
Prefer OpenGL over OpenGL ES (#5482)
Browse files Browse the repository at this point in the history
* Prefer OpenGL over OpenGL ES

* Fix sRGB on egl

* Check if OpenGL is supported

* Add changelog entry

* Remove expected failure for OpenGL Non-ES,  add comment explaining FRAMEBUFFER_SRGB, add driver info to AdapterInfo

* Fix draw indexed

* CI host doesn't seem to support Rg8Snorm and Rgb9eUfloat clearing
  • Loading branch information
valaphee authored Apr 16, 2024
1 parent 2b0e3ed commit 0dc9dd6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Bottom level categories:
- Don't create a program for shader-clearing if that workaround isn't required. By @Dinnerbone in [#5348](https://github.com/gfx-rs/wgpu/pull/5348).
- Fix crash when holding multiple devices on wayland/surfaceless. By @ashdnazg in [#5351](https://github.com/gfx-rs/wgpu/pull/5351).
- Don't depend on bind group and bind group layout entry order in HAL. This caused incorrect severely incorrect command execution and, in some cases, crashes. By @ErichDonGubler in [#5421](https://github.com/gfx-rs/wgpu/pull/5421).
- OpenGL will now be preferred over OpenGL ES on EGL, making it consistent with WGL. By @valaphee in [#5482](https://github.com/gfx-rs/wgpu/pull/5482)
- Fix `first_instance` getting ignored in draw indexed when `ARB_shader_draw_parameters` feature is present and `base_vertex` is 0. By @valaphee in [#5482](https://github.com/gfx-rs/wgpu/pull/5482)
- Fill out `driver` and `driver_info`, with the OpenGL flavor and version, similar to Vulkan. By @valaphee in [#5482](https://github.com/gfx-rs/wgpu/pull/5482)

#### Vulkan

Expand Down
28 changes: 24 additions & 4 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl super::Adapter {
}
}

fn make_info(vendor_orig: String, renderer_orig: String) -> wgt::AdapterInfo {
fn make_info(vendor_orig: String, renderer_orig: String, version: String) -> wgt::AdapterInfo {
let vendor = vendor_orig.to_lowercase();
let renderer = renderer_orig.to_lowercase();

Expand Down Expand Up @@ -179,13 +179,33 @@ impl super::Adapter {
0
};

let driver;
let driver_info;
if version.starts_with("WebGL ") || version.starts_with("OpenGL ") {
let es_sig = " ES";
match version.find(es_sig) {
Some(pos) => {
driver = version[..pos + es_sig.len()].to_owned();
driver_info = version[pos + es_sig.len() + 1..].to_owned();
}
None => {
let pos = version.find(' ').unwrap();
driver = version[..pos].to_owned();
driver_info = version[pos + 1..].to_owned();
}
}
} else {
driver = "OpenGL".to_owned();
driver_info = version;
}

wgt::AdapterInfo {
name: renderer_orig,
vendor: vendor_id,
device: 0,
device_type: inferred_device_type,
driver: String::new(),
driver_info: String::new(),
driver,
driver_info,
backend: wgt::Backend::Gl,
}
}
Expand Down Expand Up @@ -825,7 +845,7 @@ impl super::Adapter {
max_msaa_samples: max_samples,
}),
},
info: Self::make_info(vendor, renderer),
info: Self::make_info(vendor, renderer, version),
features,
capabilities: crate::Capabilities {
limits,
Expand Down
26 changes: 25 additions & 1 deletion wgpu-hal/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,24 @@ impl Inner {
}

let (config, supports_native_window) = choose_config(&egl, display, srgb_kind)?;
egl.bind_api(khronos_egl::OPENGL_ES_API).unwrap();

let supports_opengl = if version >= (1, 4) {
let client_apis = egl
.query_string(Some(display), khronos_egl::CLIENT_APIS)
.unwrap()
.to_string_lossy();
client_apis
.split(' ')
.any(|client_api| client_api == "OpenGL")
} else {
false
};
egl.bind_api(if supports_opengl {
khronos_egl::OPENGL_API
} else {
khronos_egl::OPENGL_ES_API
})
.unwrap();

let needs_robustness = true;
let mut khr_context_flags = 0;
Expand Down Expand Up @@ -977,6 +994,7 @@ impl crate::Instance for Instance {
srgb_kind: inner.srgb_kind,
})
}

unsafe fn destroy_surface(&self, _surface: Surface) {}

unsafe fn enumerate_adapters(&self) -> Vec<crate::ExposedAdapter<super::Api>> {
Expand All @@ -993,6 +1011,12 @@ impl crate::Instance for Instance {
})
};

// In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions,
// as otherwise the user has to do the sRGB conversion.
if !matches!(inner.srgb_kind, SrgbFrameBufferKind::None) {
unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };
}

if self.flags.contains(wgt::InstanceFlags::DEBUG) && gl.supports_debug() {
log::debug!("Max label length: {}", unsafe {
gl.get_parameter_i32(glow::MAX_LABEL_LENGTH)
Expand Down
71 changes: 31 additions & 40 deletions wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,27 @@ impl super::Queue {
instance_count,
ref first_instance_location,
} => {
match base_vertex {
0 => {
unsafe {
gl.uniform_1_u32(first_instance_location.as_ref(), first_instance)
};
let supports_full_instancing = self
.shared
.private_caps
.contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);

if supports_full_instancing {
unsafe {
gl.draw_elements_instanced_base_vertex_base_instance(
topology,
index_count as i32,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
first_instance,
)
}
} else {
unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), first_instance) };

if base_vertex == 0 {
unsafe {
// Don't use `gl.draw_elements`/`gl.draw_elements_base_vertex` for `instance_count == 1`.
// Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `gl.draw_elements`/`gl.draw_elements_base_vertex`.
Expand All @@ -231,41 +246,17 @@ impl super::Queue {
instance_count as i32,
)
}
}
_ => {
let supports_full_instancing = self
.shared
.private_caps
.contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);

if supports_full_instancing {
unsafe {
gl.draw_elements_instanced_base_vertex_base_instance(
topology,
index_count as i32,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
first_instance,
)
}
} else {
unsafe {
gl.uniform_1_u32(first_instance_location.as_ref(), first_instance)
};

// If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature.
unsafe {
gl.draw_elements_instanced_base_vertex(
topology,
index_count as _,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
)
}
} else {
// If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature.
unsafe {
gl.draw_elements_instanced_base_vertex(
topology,
index_count as _,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions wgpu-hal/src/gles/wgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ impl crate::Instance for Instance {
.supported_extensions()
.contains("GL_ARB_framebuffer_sRGB");

// In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions,
// as otherwise the user has to do the sRGB conversion.
if srgb_capable {
unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };
}
Expand Down

0 comments on commit 0dc9dd6

Please sign in to comment.