Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenGL surface via surfman #3151

Merged
merged 5 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]
zicklag marked this conversation as resolved.
Show resolved Hide resolved
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"]
zicklag marked this conversation as resolved.
Show resolved Hide resolved
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