Skip to content

Commit

Permalink
Merge #3151
Browse files Browse the repository at this point in the history
3151: OpenGL surface via surfman r=kvark a=zicklag

Fixes/Discussion: gfx-rs/wgpu#450

PR checklist:
- [x] `make` succeeds (on *nix)
- [ ] `make reftests` succeeds
- [x] tested examples with the following backends:
  - GL ( excluding compute because it isn't supported )
- [x] `rustfmt` run on changed code


Co-authored-by: Zicklag <zicklag@katharostech.com>
  • Loading branch information
bors[bot] and zicklag authored Apr 14, 2020
2 parents 196fffe + 3c040be commit deeb135
Show file tree
Hide file tree
Showing 19 changed files with 730 additions and 300 deletions.
23 changes: 12 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
RUST_BACKTRACE:=1
EXCLUDES:=
FEATURES_GL:=
FEATURES_HAL:=
FEATURES_HAL2:=
FEATURES_HAL3:=

ifeq (,$(TARGET))
CHECK_TARGET_FLAG=
Expand All @@ -13,6 +13,7 @@ endif
ifeq ($(OS),Windows_NT)
EXCLUDES+= --exclude gfx-backend-metal
FEATURES_HAL=vulkan
FEATURES_GL=gl
ifeq ($(TARGET),x86_64-pc-windows-gnu)
# No d3d12 support on GNU windows ATM
# context: https://github.com/gfx-rs/gfx/pull/1417
Expand All @@ -21,7 +22,6 @@ ifeq ($(OS),Windows_NT)
else
FEATURES_HAL2=dx12
endif
FEATURES_HAL3=wgl
else
UNAME_S:=$(shell uname -s)
EXCLUDES+= --exclude gfx-backend-dx12
Expand All @@ -31,7 +31,9 @@ else
FEATURES_HAL=vulkan
endif
ifeq ($(TARGET),aarch64-apple-ios)
EXCLUDES+= --exclude gfx-backend-vulkan
EXCLUDES+= --exclude gfx-backend-vulkan --exclude gfx-backend-gl
else
FEATURES_GL=gl
endif
ifeq ($(UNAME_S),Darwin)
FEATURES_HAL=metal
Expand All @@ -44,17 +46,16 @@ endif
all: check test

help:
@echo "Supported backends: gl $(FEATURES_HAL) $(FEATURES_HAL2)"
@echo "Supported backends: $(FEATURES_GL) $(FEATURES_HAL) $(FEATURES_HAL2)"

check:
@echo "Note: excluding \`warden\` here, since it depends on serialization"
cargo check --all $(CHECK_TARGET_FLAG) $(EXCLUDES) --exclude gfx-warden
cd examples && cargo check $(CHECK_TARGET_FLAG) --features "gl"
cd examples && cargo check $(CHECK_TARGET_FLAG) --features "$(FEATURES_GL)"
cd examples && cargo check $(CHECK_TARGET_FLAG) --features "$(FEATURES_HAL)"
cd examples && cargo check $(CHECK_TARGET_FLAG) --features "$(FEATURES_HAL2)"
cd examples && cargo check $(CHECK_TARGET_FLAG) --features "$(FEATURES_HAL3)"
cd src/warden && cargo check $(CHECK_TARGET_FLAG) --no-default-features
cd src/warden && cargo check $(CHECK_TARGET_FLAG) --features "env_logger gl gl-ci $(FEATURES_HAL) $(FEATURES_HAL2)"
cd src/warden && cargo check $(CHECK_TARGET_FLAG) --features "env_logger $(FEATURES_GL) $(FEATURES_HAL) $(FEATURES_HAL2)"

test:
cargo test --all $(EXCLUDES)
Expand All @@ -63,20 +64,20 @@ doc:
cargo doc --all $(EXCLUDES)

reftests:
cd src/warden && cargo run --bin reftest --features "$(FEATURES_HAL) $(FEATURES_HAL2)" -- local #TODO: gl
cd src/warden && cargo run --bin reftest --features "$(FEATURES_GL) $(FEATURES_HAL) $(FEATURES_HAL2)" -- local

benches:
cd src/warden && cargo run --release --bin bench --features "$(FEATURES_HAL) $(FEATURES_HAL2)" -- blit
cd src/warden && cargo run --release --bin bench --features "$(FEATURES_GL) $(FEATURES_HAL) $(FEATURES_HAL2)" -- blit

reftests-ci:
cd src/warden && cargo test
cd src/warden && cargo run --features "gl-ci" -- ci
cd src/warden && cargo run --features "gl" -- ci

quad:
cd examples && cargo run --bin quad --features ${FEATURES_HAL}

quad-wasm:
cd examples && cargo +nightly build --target wasm32-unknown-unknown --features gl --bin quad && wasm-bindgen ../target/wasm32-unknown-unknown/debug/quad.wasm --out-dir ../examples/generated-wasm --web
cd examples && cargo +nightly build --features gl --target wasm32-unknown-unknown --bin quad && wasm-bindgen ../target/wasm32-unknown-unknown/debug/quad.wasm --out-dir ../examples/generated-wasm --web

shader-binaries:
ifeq ($(UNAME_S),Darwin)
Expand Down
11 changes: 3 additions & 8 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ edition = "2018"
[features]
default = []
metal = ["gfx-backend-metal"]
gl = ["gfx-backend-gl", "gfx-backend-gl/glutin"]
wgl = ["gfx-backend-gl", "gfx-backend-gl/wgl"]
gl = ["gfx-backend-gl"]
dx11 = ["gfx-backend-dx11"]
dx12 = ["gfx-backend-dx12"]
vulkan = ["gfx-backend-vulkan"]
Expand All @@ -28,21 +27,17 @@ name = "compute"
path = "compute/main.rs"

[dependencies]
env_logger = "0.6"
image = "0.21"
log = "0.4"
hal = { path = "../src/hal", version = "0.5", package = "gfx-hal" }
gfx-backend-gl = { path = "../src/backend/gl", version = "0.5", optional = true }
gfx-backend-empty = { path = "../src/backend/empty", version = "0.5" }
winit = { version = "0.21.0", features = ["web-sys"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = "0.6"
glsl-to-spirv = "0.1.4"

[dependencies.gfx-backend-gl]
path = "../src/backend/gl"
version = "0.5"
optional = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.55"
console_error_panic_hook = "0.1.6"
Expand Down
50 changes: 3 additions & 47 deletions examples/colour-uniform/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ const COLOR_RANGE: i::SubresourceRange = i::SubresourceRange {
layers: 0 .. 1,
};

trait SurfaceTrait {
#[cfg(feature = "gl")]
fn get_context_t(&self) -> &back::glutin::RawContext<back::glutin::PossiblyCurrent>;
}

impl SurfaceTrait for <back::Backend as Backend>::Surface {
#[cfg(feature = "gl")]
fn get_context_t(&self) -> &back::glutin::RawContext<back::glutin::PossiblyCurrent> {
self.context()
}
}

struct RendererState<B: Backend> {
uniform_desc_pool: Option<B::DescriptorPool>,
img_desc_pool: Option<B::DescriptorPool>,
Expand Down Expand Up @@ -362,10 +350,7 @@ impl<B: Backend> RendererState<B> {
}
}

fn draw(&mut self)
where
B::Surface: SurfaceTrait,
{
fn draw(&mut self) {
if self.recreate_swapchain {
self.recreate_swapchain();
self.recreate_swapchain = false;
Expand Down Expand Up @@ -592,7 +577,8 @@ impl<B: Backend> Drop for BackendState<B> {
feature = "vulkan",
feature = "dx11",
feature = "dx12",
feature = "metal"
feature = "metal",
feature = "gl",
))]
fn create_backend(
wb: winit::window::WindowBuilder,
Expand All @@ -615,34 +601,6 @@ fn create_backend(
}
}

#[cfg(feature = "gl")]
fn create_backend(
wb: winit::window::WindowBuilder,
event_loop: &winit::event_loop::EventLoop<()>,
) -> BackendState<back::Backend> {
let (context, window) = {
let builder =
back::config_context(back::glutin::ContextBuilder::new(), ColorFormat::SELF, None)
.with_vsync(true);
let windowed_context = builder.build_windowed(wb, event_loop).unwrap();
unsafe {
windowed_context
.make_current()
.expect("Unable to make context current")
.split()
}
};

let surface = back::Surface::from_context(context);
let mut adapters = surface.enumerate_adapters();
BackendState {
instance: None,
adapter: AdapterState::new(&mut adapters),
surface: ManuallyDrop::new(surface),
window,
}
}

struct AdapterState<B: Backend> {
adapter: Option<Adapter<B>>,
memory_types: Vec<MemoryType>,
Expand Down Expand Up @@ -1672,8 +1630,6 @@ fn main() {
*control_flow = winit::event_loop::ControlFlow::Exit
}
winit::event::WindowEvent::Resized(dims) => {
#[cfg(feature = "gl")]
renderer_state.backend.surface.get_context_t().resize(dims);
println!("RESIZE EVENT");
renderer_state.recreate_swapchain = true;
}
Expand Down
40 changes: 10 additions & 30 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
feature = "dx12",
feature = "metal",
feature = "gl",
feature = "wgl"
)),
allow(dead_code, unused_extern_crates, unused_imports)
)]
Expand All @@ -14,7 +13,7 @@
extern crate gfx_backend_dx11 as back;
#[cfg(feature = "dx12")]
extern crate gfx_backend_dx12 as back;
#[cfg(any(feature = "gl", feature = "wgl"))]
#[cfg(any(feature = "gl"))]
extern crate gfx_backend_gl as back;
#[cfg(feature = "metal")]
extern crate gfx_backend_metal as back;
Expand Down Expand Up @@ -91,11 +90,12 @@ const COLOR_RANGE: i::SubresourceRange = i::SubresourceRange {
feature = "dx12",
feature = "metal",
feature = "gl",
feature = "wgl"
))]
fn main() {
#[cfg(target_arch = "wasm32")]
console_log::init_with_level(log::Level::Debug).unwrap();

#[cfg(not(target_arch = "wasm32"))]
env_logger::init();

let event_loop = winit::event_loop::EventLoop::new();
Expand All @@ -111,38 +111,23 @@ fn main() {
.with_title("quad".to_string());

// instantiate backend
#[cfg(not(feature = "gl"))]
#[cfg(not(target_arch = "wasm32"))]
let (_window, instance, mut adapters, surface) = {
let window = wb.build(&event_loop).unwrap();
let instance =
back::Instance::create("gfx-rs quad", 1).expect("Failed to create an instance!");
let adapters = instance.enumerate_adapters();
let surface = unsafe {
instance
.create_surface(&window)
.expect("Failed to create a surface!")
};
let adapters = instance.enumerate_adapters();
// Return `window` so it is not dropped: dropping it invalidates `surface`.
(window, Some(instance), adapters, surface)
};
#[cfg(feature = "gl")]

#[cfg(target_arch = "wasm32")]
let (_window, instance, mut adapters, surface) = {
#[cfg(not(target_arch = "wasm32"))]
let (window, surface) = {
let builder =
back::config_context(back::glutin::ContextBuilder::new(), ColorFormat::SELF, None)
.with_vsync(true);
let windowed_context = builder.build_windowed(wb, &event_loop).unwrap();
let (context, window) = unsafe {
windowed_context
.make_current()
.expect("Unable to make context current")
.split()
};
let surface = back::Surface::from_context(context);
(window, surface)
};
#[cfg(target_arch = "wasm32")]
let (window, surface) = {
let window = wb.build(&event_loop).unwrap();
web_sys::window()
Expand All @@ -151,7 +136,8 @@ fn main() {
.unwrap()
.body()
.unwrap()
.append_child(&winit::platform::web::WindowExtWebSys::canvas(&window));
.append_child(&winit::platform::web::WindowExtWebSys::canvas(&window))
.unwrap();
let surface = back::Surface::from_raw_handle(&window);
(window, surface)
};
Expand Down Expand Up @@ -190,11 +176,6 @@ fn main() {
} => *control_flow = winit::event_loop::ControlFlow::Exit,
winit::event::WindowEvent::Resized(dims) => {
println!("resized to {:?}", dims);
#[cfg(all(feature = "gl", not(target_arch = "wasm32")))]
{
let context = renderer.surface.context();
context.resize(dims);
}
renderer.dimensions = window::Extent2D {
width: dims.width,
height: dims.height,
Expand Down Expand Up @@ -985,8 +966,7 @@ where
feature = "dx12",
feature = "metal",
feature = "gl",
feature = "wgl"
)))]
fn main() {
println!("You need to enable the native API feature (vulkan/metal/dx11/dx12/gl/wgl) in order to run the example");
println!("You need to enable the native API feature (vulkan/metal/dx11/dx12/gl) in order to run the example");
}
14 changes: 9 additions & 5 deletions src/backend/gl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ build = "build.rs"
name = "gfx_backend_gl"

[features]
default = []
wgl = []
default = ["wgl", "surfman", "surfman-x11"]
wgl = ["winapi"]
surfman-x11 = ["surfman/sm-x11"]

[dependencies]
arrayvec = "0.5"
Expand All @@ -32,10 +33,12 @@ parking_lot = "0.10"
spirv_cross = { version = "0.18", features = ["glsl"] }
lazy_static = "1"
raw-window-handle = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
glutin = { version = "0.23.0", optional = true }

[target.'cfg(all(unix, not(target_os = "ios")))'.dependencies]
# TODO: Update to released version when it comes out
surfman = { git = "https://github.com/servo/surfman.git", rev = "41ac1ee", features = ["sm-raw-window-handle"], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.6"
wasm-bindgen = "0.2.51"
Expand All @@ -61,7 +64,8 @@ features = [
]

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["libloaderapi", "windef", "wingdi", "winuser"] }
winapi = { version = "0.3", features = ["libloaderapi", "windef", "wingdi", "winuser"], optional = true }

[build-dependencies]
gl_generator = "0.11"
cfg_aliases = "0.1.0"
15 changes: 15 additions & 0 deletions src/backend/gl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ use std::fs::File;
use std::path::PathBuf;

fn main() {
// Setup cfg aliases
cfg_aliases::cfg_aliases! {
// Platforms
wasm: { target_arch = "wasm32" },
android: { target_os = "android" },
macos: { target_os = "macos" },
ios: { target_os = "ios" },
linux: { target_os = "linux" },
// Backends
surfman: { all(unix, feature = "surfman", not(ios)) },
wgl: { all(windows, feature = "wgl") },
glutin: { all(feature = "glutin", not(any(wasm, surfman))) },
dummy: { not(any(wasm, glutin, wgl, surfman)) },
}

let target = env::var("TARGET").unwrap();
let dest = PathBuf::from(&env::var("OUT_DIR").unwrap());

Expand Down
Loading

0 comments on commit deeb135

Please sign in to comment.