Skip to content

Commit cc82d34

Browse files
bors[bot]kvark
andauthored
Merge #3470
3470: [gl] replace Surfman by EGL r=grovesNL a=kvark Fixes #3468 This is a BIG change. It re-architects WSI of the GL backend to unconditionally use EGL (via `khronos-egl` crate atm, but we can change that). This means the backend is *only* supported on Linux/BSD/Android, it's no longer supported on Apple and Microsoft platforms. One of the consequences - we throw Surfman away. There was too much complication and too little benefit from it anyway. Another consequence - we are locked to GLES, given that I experienced issues trying to get a desktop GL context via EGL. We can revisit this, but really I think going with GLES 3.1 makes total sense for the backend, unconditionally. if the target platform is powerful above what GLES 3.1 offers, it should just support Vulkan. Most importantly, it solves GL context sharing, so presentation is much more robust. However, it doesn't solve the extra copy - #3469. For this, we'll need to follow-up by directly using platform extensions, such as [EGL_WL_create_wayland_buffer_from_image](https://www.khronos.org/registry/EGL/extensions/WL/EGL_WL_create_wayland_buffer_from_image.txt). PR checklist: - [x] `make` succeeds (on *nix) - [x] `make reftests` succeeds - [x] tested examples with the following backends: Linux Co-authored-by: Dzmitry Malyshau <[email protected]>
2 parents fa32352 + d8fe67c commit cc82d34

File tree

18 files changed

+365
-641
lines changed

18 files changed

+365
-641
lines changed

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ path = "../src/backend/vulkan"
5656
version = "0.6"
5757
optional = true
5858

59-
[target.'cfg(unix)'.dependencies.gfx-backend-gl]
59+
[target.'cfg(all(unix, not(any(target_os = "ios", target_os = "macos"))))'.dependencies.gfx-backend-gl]
6060
path = "../src/backend/gl"
6161
version = "0.6"
6262
features = ["x11"]

examples/compute/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![cfg_attr(
22
not(any(
33
feature = "vulkan",
4+
feature = "gl",
45
feature = "dx11",
56
feature = "dx12",
6-
feature = "metal"
7+
feature = "metal",
78
)),
89
allow(dead_code, unused_extern_crates, unused_imports)
910
)]
@@ -12,6 +13,8 @@
1213
extern crate gfx_backend_dx11 as back;
1314
#[cfg(feature = "dx12")]
1415
extern crate gfx_backend_dx12 as back;
16+
#[cfg(feature = "gl")]
17+
extern crate gfx_backend_gl as back;
1518
#[cfg(feature = "metal")]
1619
extern crate gfx_backend_metal as back;
1720
#[cfg(feature = "vulkan")]
@@ -23,9 +26,10 @@ use hal::{adapter::MemoryType, buffer, command, memory, pool, prelude::*, pso};
2326

2427
#[cfg(any(
2528
feature = "vulkan",
29+
feature = "gl",
2630
feature = "dx11",
2731
feature = "dx12",
28-
feature = "metal"
32+
feature = "metal",
2933
))]
3034
fn main() {
3135
env_logger::init();
@@ -288,6 +292,7 @@ unsafe fn create_buffer<B: hal::Backend>(
288292

289293
#[cfg(not(any(
290294
feature = "vulkan",
295+
feature = "gl",
291296
feature = "dx11",
292297
feature = "dx12",
293298
feature = "metal"

examples/mesh-shading/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
extern crate gfx_backend_dx11 as back;
1414
#[cfg(feature = "dx12")]
1515
extern crate gfx_backend_dx12 as back;
16-
#[cfg(all(unix, feature = "gl"))]
16+
#[cfg(feature = "gl")]
1717
extern crate gfx_backend_gl as back;
1818
#[cfg(feature = "metal")]
1919
extern crate gfx_backend_metal as back;

examples/quad/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate gfx_backend_dx12 as back;
1010
feature = "gl",
1111
)))]
1212
extern crate gfx_backend_empty as back;
13-
#[cfg(all(unix, feature = "gl"))]
13+
#[cfg(feature = "gl")]
1414
extern crate gfx_backend_gl as back;
1515
#[cfg(feature = "metal")]
1616
extern crate gfx_backend_metal as back;

src/backend/gl/Cargo.toml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ readme = "README.md"
1111
documentation = "https://docs.rs/gfx-backend-gl"
1212
workspace = "../../.."
1313
edition = "2018"
14-
build = "build.rs"
1514

1615
[lib]
1716
name = "gfx_backend_gl"
1817

1918
[features]
20-
default = ["surfman"]
21-
x11 = ["surfman/sm-x11"]
19+
default = []
2220

2321
[dependencies]
2422
arrayvec = "0.5"
@@ -32,9 +30,10 @@ parking_lot = "0.11"
3230
spirv_cross = { version = "0.22", features = ["glsl"] }
3331
lazy_static = "1"
3432
raw-window-handle = "0.3"
33+
x11 = { version = "2", features = ["xlib"], optional = true }
3534

36-
[target.'cfg(all(unix, not(target_os = "ios")))'.dependencies]
37-
surfman = { version = "0.3", features = ["sm-raw-window-handle"], optional = true }
35+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
36+
egl = { package = "khronos-egl", version = "2.1.0" }
3837

3938
[target.'cfg(target_arch = "wasm32")'.dependencies]
4039
js-sys = "0.3.6"
@@ -59,6 +58,3 @@ features = [
5958
"WebGlTexture",
6059
"Window",
6160
]
62-
63-
[build-dependencies]
64-
cfg_aliases = "0.1.0"

src/backend/gl/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[OpenGL](https://www.khronos.org/opengl/) backend for gfx.
44

5+
Can only be used on non-Apple Unix systems. The WSI is hard-coded to EGL.
6+
57
## Normalized Coordinates
68

79
Render | Depth | Texture

src/backend/gl/build.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/backend/gl/src/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ impl d::Device<B> for Device {
17521752
}
17531753
}
17541754

1755-
#[cfg(wasm)]
1755+
#[cfg(target_arch = "wasm32")]
17561756
unsafe fn wait_for_fences<I>(
17571757
&self,
17581758
fences: I,

src/backend/gl/src/info.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,23 @@ pub enum Requirement<'a> {
254254
Ext(&'a str),
255255
}
256256

257+
const IS_WEBGL: bool = cfg!(target_arch = "wasm32");
258+
257259
impl Info {
258260
fn get(gl: &GlContainer) -> Info {
259261
let platform_name = PlatformName::get(gl);
260262
let version = Version::parse(get_string(gl, glow::VERSION).unwrap_or_default()).unwrap();
261-
#[cfg(not(wasm))]
262-
let shading_language =
263+
let shading_language = if IS_WEBGL {
264+
Version::new_embedded(3, 0, String::from(""))
265+
} else {
263266
Version::parse(get_string(gl, glow::SHADING_LANGUAGE_VERSION).unwrap_or_default())
264-
.unwrap();
265-
#[cfg(wasm)]
266-
let shading_language = Version::new_embedded(3, 0, String::from(""));
267+
.unwrap()
268+
};
267269
// TODO: Use separate path for WebGL extensions in `glow` somehow
268270
// Perhaps automatic fallback for NUM_EXTENSIONS to EXTENSIONS on native
269-
#[cfg(wasm)]
270-
let extensions = HashSet::new();
271-
#[cfg(not(wasm))]
272-
let extensions = if version >= Version::new(3, 0, None, String::from("")) {
271+
let extensions = if IS_WEBGL {
272+
HashSet::new()
273+
} else if version >= Version::new(3, 0, None, String::from("")) {
273274
let num_exts = get_usize(gl, glow::NUM_EXTENSIONS).unwrap();
274275
(0..num_exts)
275276
.map(|i| unsafe { gl.get_parameter_indexed_string(glow::EXTENSIONS, i as u32) })
@@ -327,8 +328,6 @@ impl Info {
327328
}
328329
}
329330

330-
const IS_WEBGL: bool = cfg!(wasm);
331-
332331
/// Load the information pertaining to the driver and the corresponding device
333332
/// capabilities.
334333
pub(crate) fn query_all(

src/backend/gl/src/lib.rs

Lines changed: 13 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ extern crate bitflags;
88
#[macro_use]
99
extern crate log;
1010

11-
#[cfg(surfman)]
12-
use parking_lot::RwLock;
13-
1411
use std::{
1512
cell::Cell,
1613
fmt,
@@ -35,19 +32,11 @@ mod state;
3532
mod window;
3633

3734
// Web implementation
38-
#[cfg(wasm)]
39-
pub use window::web::Surface;
40-
41-
// Surfman implementation
42-
#[cfg(surfman)]
43-
pub use crate::window::surfman::{Instance, Surface, Swapchain};
44-
// Helps windows detect discrete GPUs
45-
#[cfg(surfman)]
46-
surfman::declare_surfman!();
35+
#[cfg(target_arch = "wasm32")]
36+
pub use window::web::{Surface, Swapchain};
4737

48-
// Catch-all dummy implementation
49-
#[cfg(dummy)]
50-
pub use window::dummy::{Instance, Surface, Swapchain};
38+
#[cfg(not(target_arch = "wasm32"))]
39+
pub use crate::window::egl::{Instance, Surface, Swapchain};
5140

5241
pub use glow::Context as GlContext;
5342
use glow::HasContext;
@@ -56,51 +45,19 @@ type ColorSlot = u8;
5645

5746
pub(crate) struct GlContainer {
5847
context: GlContext,
59-
60-
#[cfg(surfman)]
61-
surfman_device: Starc<RwLock<surfman::Device>>,
62-
63-
#[cfg(surfman)]
64-
surfman_context: Starc<RwLock<surfman::Context>>,
6548
}
6649

6750
impl GlContainer {
68-
#[cfg(not(wasm))]
69-
fn make_current(&self) {
70-
#[cfg(surfman)]
71-
self.surfman_device
72-
.write()
73-
.make_context_current(&self.surfman_context.read())
74-
.expect("TODO");
75-
}
76-
77-
#[cfg(any(glutin, wgl))]
78-
fn from_fn_proc<F>(fn_proc: F) -> GlContainer
51+
#[cfg(not(target_arch = "wasm32"))]
52+
unsafe fn from_fn_proc<F>(fn_proc: F) -> GlContainer
7953
where
8054
F: FnMut(&str) -> *const std::os::raw::c_void,
8155
{
8256
let context = glow::Context::from_loader_function(fn_proc);
8357
GlContainer { context }
8458
}
8559

86-
#[cfg(surfman)]
87-
fn from_fn_proc<F>(
88-
fn_proc: F,
89-
surfman_device: Starc<RwLock<surfman::Device>>,
90-
surfman_context: Starc<RwLock<surfman::Context>>,
91-
) -> GlContainer
92-
where
93-
F: FnMut(&str) -> *const std::os::raw::c_void,
94-
{
95-
let context = unsafe { glow::Context::from_loader_function(fn_proc) };
96-
GlContainer {
97-
context,
98-
surfman_device,
99-
surfman_context,
100-
}
101-
}
102-
103-
#[cfg(wasm)]
60+
#[cfg(target_arch = "wasm32")]
10461
fn from_canvas(canvas: &web_sys::HtmlCanvasElement) -> GlContainer {
10562
let context = {
10663
use wasm_bindgen::JsCast;
@@ -136,31 +93,18 @@ impl GlContainer {
13693
impl Deref for GlContainer {
13794
type Target = GlContext;
13895
fn deref(&self) -> &GlContext {
139-
#[cfg(not(wasm))]
140-
self.make_current();
14196
&self.context
14297
}
14398
}
14499

145-
#[cfg(surfman)]
146-
impl Drop for GlContainer {
147-
fn drop(&mut self) {
148-
// Contexts must be manually destroyed to prevent a panic
149-
self.surfman_device
150-
.read()
151-
.destroy_context(&mut self.surfman_context.write())
152-
.expect("TODO");
153-
}
154-
}
155-
156100
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
157101
pub enum Backend {}
158102

159103
impl hal::Backend for Backend {
160-
#[cfg(not(wasm))]
104+
#[cfg(not(target_arch = "wasm32"))]
161105
type Instance = Instance;
162106

163-
#[cfg(wasm)]
107+
#[cfg(target_arch = "wasm32")]
164108
type Instance = Surface;
165109

166110
type PhysicalDevice = PhysicalDevice;
@@ -223,7 +167,6 @@ impl Error {
223167
}
224168
}
225169

226-
#[cfg(not(wasm))]
227170
fn debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, message: &str) {
228171
let source_str = match source {
229172
glow::DEBUG_SOURCE_API => "API",
@@ -280,14 +223,6 @@ enum MemoryUsage {
280223
/// Internal struct of shared data between the physical and logical device.
281224
struct Share {
282225
context: GlContainer,
283-
284-
/// Context associated with an instance.
285-
///
286-
/// Parenting context for all device contexts shared with it.
287-
/// Used for querying basic information and spawning shared contexts.
288-
#[allow(unused)]
289-
instance_context: DeviceContext,
290-
291226
info: Info,
292227
supported_features: hal::Features,
293228
legacy_features: info::LegacyFeatures,
@@ -438,12 +373,8 @@ unsafe impl<T: ?Sized> Sync for Wstarc<T> {}
438373
#[derive(Debug)]
439374
pub struct PhysicalDevice(Starc<Share>);
440375

441-
#[cfg(not(wgl))]
442-
type DeviceContext = ();
443-
444376
impl PhysicalDevice {
445-
#[allow(unused)]
446-
fn new_adapter(instance_context: DeviceContext, gl: GlContainer) -> adapter::Adapter<Backend> {
377+
fn new_adapter(gl: GlContainer) -> adapter::Adapter<Backend> {
447378
// query information
448379
let (info, supported_features, legacy_features, hints, limits, private_caps) =
449380
info::query_all(&gl);
@@ -520,7 +451,6 @@ impl PhysicalDevice {
520451
// create the shared context
521452
let share = Share {
522453
context: gl,
523-
instance_context,
524454
info,
525455
supported_features,
526456
legacy_features,
@@ -634,12 +564,9 @@ impl adapter::PhysicalDevice<Backend> for PhysicalDevice {
634564
// initialize permanent states
635565
let gl = &self.0.context;
636566

637-
#[cfg(not(wasm))]
638-
{
639-
if cfg!(debug_assertions) && gl.supports_debug() {
640-
gl.enable(glow::DEBUG_OUTPUT);
641-
gl.debug_message_callback(debug_message_callback);
642-
}
567+
if cfg!(debug_assertions) && !cfg!(target_arch = "wasm32") && gl.supports_debug() {
568+
gl.enable(glow::DEBUG_OUTPUT);
569+
gl.debug_message_callback(debug_message_callback);
643570
}
644571

645572
if self

0 commit comments

Comments
 (0)