Implement OpenGL Backend For Unix Platforms

This commit is contained in:
Zicklag 2020-08-29 14:58:28 -05:00
parent 7beb7c3411
commit f7f1ff699a
7 changed files with 665 additions and 90 deletions

678
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -25,8 +25,8 @@ arrayvec = "0.5"
bitflags = "1.0"
copyless = "0.1"
fxhash = "0.2"
hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx.git", rev = "c9fcd08956eeddac8f6d85b02794fc5e3d1717f5" }
gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx.git", rev = "c9fcd08956eeddac8f6d85b02794fc5e3d1717f5" }
hal = { package = "gfx-hal", version = "0.6" }
gfx-backend-empty = "0.6"
parking_lot = "0.11"
raw-window-handle = { version = "0.3", optional = true }
ron = { version = "0.6", optional = true }
@ -34,8 +34,8 @@ serde = { version = "1.0", features = ["serde_derive"], optional = true }
smallvec = "1"
tracing = { version = "0.1", default-features = false, features = ["std"] }
thiserror = "1"
gfx-descriptor = { git = "https://github.com/katharostech/gfx-extras.git", branch = "gl-final-showdown" }
gfx-memory = { git = "https://github.com/katharostech/gfx-extras.git", branch = "gl-final-showdown" }
gfx-descriptor = "0.2"
gfx-memory = "0.2"
[dependencies.naga]
version = "0.2"
@ -48,6 +48,9 @@ path = "../wgpu-types"
package = "wgpu-types"
version = "0.6"
[target.'cfg(all(unix, not(target_os = "ios")))'.dependencies]
gfx-backend-gl = { version = "0.6" }
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
gfx-backend-metal = { version = "0.6" }
gfx-backend-vulkan = { version = "0.6.4", optional = true }

View File

@ -6,13 +6,15 @@ fn main() {
// Setup cfg aliases
cfg_aliases::cfg_aliases! {
// Vendors/systems
apple: { any(target_os = "ios", target_os = "macos") },
ios: { target_os = "ios" },
macos: { target_os = "macos" },
apple: { any(ios, macos) },
// Backends
vulkan: { any(windows, all(unix, not(apple)), feature = "gfx-backend-vulkan") },
metal: { apple },
dx12: { windows },
dx11: { windows },
gl: { unix },
gl: { all(unix, not(ios)) },
}
}

View File

@ -1736,16 +1736,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
None
};
let actual_border = desc
let actual_border: hal::image::PackedColor = desc
.border_color
.map(|c| match c {
wgt::SamplerBorderColor::TransparentBlack => {
hal::image::BorderColor::TransparentBlack
[0., 0., 0., 0.].into()
}
wgt::SamplerBorderColor::OpaqueBlack => hal::image::BorderColor::OpaqueBlack,
wgt::SamplerBorderColor::OpaqueWhite => hal::image::BorderColor::OpaqueWhite,
wgt::SamplerBorderColor::OpaqueBlack => [0., 0., 0., 1.].into(),
wgt::SamplerBorderColor::OpaqueWhite => [1., 1., 1., 1.].into(),
})
.unwrap_or(hal::image::BorderColor::OpaqueBlack);
.unwrap_or([0., 0., 0., 0.].into());
let info = hal::image::SamplerDesc {
min_filter: conv::map_filter(desc.min_filter),

View File

@ -670,6 +670,8 @@ pub struct Hubs<F: GlobalIdentityHandlerFactory> {
dx12: Hub<backend::Dx12, F>,
#[cfg(dx11)]
dx11: Hub<backend::Dx11, F>,
#[cfg(gl)]
gl: Hub<backend::Gl, F>,
}
impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
@ -683,6 +685,8 @@ impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
dx12: Hub::new(factory),
#[cfg(dx11)]
dx11: Hub::new(factory),
#[cfg(gl)]
gl: Hub::new(factory),
}
}
}
@ -734,6 +738,10 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
{
self.hubs.dx11.clear(&mut *surface_guard);
}
#[cfg(gl)]
{
self.hubs.gl.clear(&mut *surface_guard);
}
// destroy surfaces
for element in surface_guard.map.drain(..) {
@ -795,6 +803,17 @@ impl GfxBackend for backend::Dx11 {
}
}
#[cfg(gl)]
impl GfxBackend for backend::Gl {
const VARIANT: Backend = Backend::Gl;
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.gl
}
fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface {
surface.gl.as_mut().unwrap()
}
}
#[cfg(test)]
fn _test_send_sync(global: &Global<IdentityManagerFactory>) {
fn test_internal<T: Send + Sync>(_: T) {}

View File

@ -38,6 +38,8 @@ pub struct Instance {
pub dx12: Option<gfx_backend_dx12::Instance>,
#[cfg(dx11)]
pub dx11: Option<gfx_backend_dx11::Instance>,
#[cfg(gl)]
pub gl: Option<gfx_backend_gl::Instance>,
}
impl Instance {
@ -59,6 +61,8 @@ impl Instance {
dx12: map((Backend::Dx12, gfx_backend_dx12::Instance::create)),
#[cfg(dx11)]
dx11: map((Backend::Dx11, gfx_backend_dx11::Instance::create)),
#[cfg(gl)]
gl: map((Backend::Gl, gfx_backend_gl::Instance::create)),
}
}
}
@ -81,6 +85,8 @@ impl Instance {
map((surface.dx12, &mut self.dx12)),
#[cfg(dx11)]
map((surface.dx11, &mut self.dx11)),
#[cfg(gl)]
map((surface.gl, &mut self.gl)),
}
}
}
@ -97,6 +103,8 @@ pub struct Surface {
pub dx12: Option<GfxSurface<backend::Dx12>>,
#[cfg(dx11)]
pub dx11: Option<GfxSurface<backend::Dx11>>,
#[cfg(gl)]
pub gl: Option<GfxSurface<backend::Gl>>,
}
#[derive(Debug)]
@ -341,7 +349,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
inst
.as_ref()
.and_then(|inst| inst.create_surface(handle).map_err(|e| {
eprintln!("Error: {:?}", e);
tracing::warn!("Error: {:?}", e);
}).ok())
};
@ -354,6 +362,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
dx12: map(&self.instance.dx12),
#[cfg(dx11)]
dx11: map(&self.instance.dx11),
#[cfg(gl)]
gl: map(&self.instance.gl),
}
}
};
@ -397,6 +407,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
map((&instance.dx12, Backend::Dx12, "Dx12", backend::Dx12::hub)),
#[cfg(dx11)]
map((&instance.dx11, Backend::Dx11, "Dx11", backend::Dx11::hub)),
#[cfg(gl)]
map((&instance.gl, Backend::Gl, "GL", backend::Gl::hub)),
}
adapters
@ -426,6 +438,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let mut id_metal = inputs.find(Backend::Metal);
let mut id_dx12 = inputs.find(Backend::Dx12);
let mut id_dx11 = inputs.find(Backend::Dx11);
let mut id_gl = inputs.find(Backend::Gl);
backends_map! {
let map = |(instance_backend, id_backend, surface_backend)| {
@ -477,6 +490,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
surface_dx11
}));
#[cfg(gl)]
let adapters_gl = map((&instance.gl, &id_gl, {
fn surface_gl(surf: &Surface) -> Option<&GfxSurface<backend::Gl>> {
surf.gl.as_ref()
}
surface_gl
}));
}
if device_types.is_empty() {
@ -532,6 +552,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
map(("Dx12", &mut id_dx12, adapters_dx12, backend::Dx12::hub)),
#[cfg(dx11)]
map(("Dx11", &mut id_dx11, adapters_dx11, backend::Dx11::hub)),
#[cfg(gl)]
map(("GL", &mut id_dx11, adapters_gl, backend::Gl::hub)),
}
let _ = (
@ -540,6 +562,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
id_metal.take(),
id_dx12.take(),
id_dx11.take(),
id_gl.take(),
);
tracing::warn!("Some adapters are present, but enumerating them failed!");
Err(RequestAdapterError::NotFound)

View File

@ -19,6 +19,8 @@ pub mod backend {
pub use gfx_backend_dx11::Backend as Dx11;
#[cfg(dx12)]
pub use gfx_backend_dx12::Backend as Dx12;
#[cfg(gl)]
pub use gfx_backend_gl::Backend as Gl;
#[cfg(metal)]
pub use gfx_backend_metal::Backend as Metal;
#[cfg(vulkan)]
@ -203,6 +205,8 @@ struct PrivateFeatures {
#[macro_export]
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {
// Note: For some reason the cfg aliases defined in build.rs don't succesfully apply in this
// macro so we must specify their equivalents manually
match $id.backend() {
#[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))]
wgt::Backend::Vulkan => $global.$method::<$crate::backend::Vulkan>( $($param),* ),
@ -212,6 +216,8 @@ macro_rules! gfx_select {
wgt::Backend::Dx12 => $global.$method::<$crate::backend::Dx12>( $($param),* ),
#[cfg(windows)]
wgt::Backend::Dx11 => $global.$method::<$crate::backend::Dx11>( $($param),* ),
#[cfg(all(unix, not(target_os = "ios")))]
wgt::Backend::Gl => $global.$method::<$crate::backend::Gl>( $($param),+ ),
_ => unreachable!()
}
};