Skip to content

Commit

Permalink
enable debug extension on emscripten (#3519)
Browse files Browse the repository at this point in the history
* enable debug extension on emscripten

* update changelog

* extract emscripten stuff into a separate module

* use unmasked queries on w-u-u, and only enable extension on emscripten

* fix docs
  • Loading branch information
coderedart authored Feb 22, 2023
1 parent 30064ea commit af5f4d2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ By @teoxoy in [#3436](https://github.com/gfx-rs/wgpu/pull/3436)
#### GLES

- [gles] fix: Set FORCE_POINT_SIZE if it is vertex shader with mesh consist of point list. By @REASY in [3440](https://github.com/gfx-rs/wgpu/pull/3440)
- [gles] fix: enable `WEBGL_debug_renderer_info` before querying unmasked vendor/renderer to avoid crashing on emscripten in [#3519](https://github.com/gfx-rs/wgpu/pull/3519)

#### General

Expand Down
11 changes: 11 additions & 0 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,21 @@ impl super::Adapter {
let extensions = gl.supported_extensions();

let (vendor_const, renderer_const) = if extensions.contains("WEBGL_debug_renderer_info") {
// emscripten doesn't enable "WEBGL_debug_renderer_info" extension by default. so, we do it manually.
// See https://github.com/gfx-rs/wgpu/issues/3245 for context
#[cfg(target_os = "emscripten")]
if unsafe { super::emscripten::enable_extension("WEBGL_debug_renderer_info\0") } {
(GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)
} else {
(glow::VENDOR, glow::RENDERER)
}
// glow already enables WEBGL_debug_renderer_info on wasm32-unknown-unknown target by default.
#[cfg(not(target_os = "emscripten"))]
(GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)
} else {
(glow::VENDOR, glow::RENDERER)
};

let (vendor, renderer) = {
let vendor = unsafe { gl.get_parameter_string(vendor_const) };
let renderer = unsafe { gl.get_parameter_string(renderer_const) };
Expand Down
26 changes: 26 additions & 0 deletions wgpu-hal/src/gles/emscripten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern "C" {
/// returns 1 if success. 0 if failure. extension name must be null terminated
fn emscripten_webgl_enable_extension(
context: std::ffi::c_int,
extension: *const std::ffi::c_char,
) -> std::ffi::c_int;
fn emscripten_webgl_get_current_context() -> std::ffi::c_int;
}
/// Webgl requires extensions to be enabled before using them.
/// This function can be used to enable webgl extension on emscripten target.
///
/// returns true on success
///
/// # Safety:
///
/// - opengl context MUST BE current
/// - extension_name_null_terminated argument must be a valid string with null terminator.
/// - extension must be present. check `glow_context.supported_extensions()`
pub unsafe fn enable_extension(extension_name_null_terminated: &str) -> bool {
unsafe {
emscripten_webgl_enable_extension(
emscripten_webgl_get_current_context(),
extension_name_null_terminated.as_ptr() as _,
) == 1
}
}
2 changes: 2 additions & 0 deletions wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ To address this, we invalidate the vertex buffers based on:
///cbindgen:ignore
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
mod egl;
#[cfg(target_os = "emscripten")]
mod emscripten;
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
mod web;

Expand Down

0 comments on commit af5f4d2

Please sign in to comment.