Skip to content

Commit

Permalink
Wrap surface creation with configuration into a shared method
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 17, 2024
1 parent 99df34e commit 7853e7d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 78 deletions.
88 changes: 42 additions & 46 deletions blade-graphics/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,36 +260,64 @@ impl super::Context {
})
}

pub fn create_surface<
I: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle,
>(
pub fn create_surface<I: raw_window_handle::HasWindowHandle>(
&self,
window: I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
use raw_window_handle::RawDisplayHandle as Rdh;
use raw_window_handle::RawWindowHandle as Rwh;

let library = match window.display_handle().unwrap().as_raw() {
Rdh::Xlib(_) => Some(find_x_library().unwrap()),
Rdh::Xcb(_) => Some(find_x_library().unwrap()),
Rdh::Wayland(_) => Some(find_wayland_library().unwrap()),
let window_handle = window.window_handle().unwrap().as_raw();
let library = match window_handle {
Rwh::Xlib(_) => Some(find_x_library().unwrap()),
Rwh::Xcb(_) => Some(find_x_library().unwrap()),
Rwh::Wayland(_) => Some(find_wayland_library().unwrap()),
_ => None,
};

let mut surface = unsafe {
Ok(unsafe {
let guard = self.lock();
super::Surface {
platform: PlatformSurface {
library,
window_handle: window.window_handle().unwrap().as_raw(),
window_handle,
swapchain: Mutex::new(None),
},
renderbuf: guard.create_renderbuffer().unwrap(),
framebuf: guard.create_framebuffer().unwrap(),
}
};
self.reconfigure_surface(&mut surface, config);
Ok(surface)
})
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
use raw_window_handle::RawWindowHandle as Rwh;

let inner = self.platform.inner.lock().unwrap();
let mut swapchain = surface.platform.swapchain.lock().unwrap();
if let Some(s) = swapchain.take() {
inner
.egl
.instance
.destroy_surface(inner.egl.display, s.surface)
.unwrap();
}
if let Rwh::Wayland(handle) = surface.platform.window_handle {
unsafe {
let wl_egl_window_destroy: libloading::Symbol<WlEglWindowDestroyFun> = surface
.platform
.library
.as_ref()
.unwrap()
.get(b"wl_egl_window_destroy")
.unwrap();
wl_egl_window_destroy(handle.surface.as_ptr());
}
}
inner.egl.make_current();
unsafe {
inner.glow.delete_renderbuffer(surface.renderbuf);
inner.glow.delete_framebuffer(surface.framebuf);
}
inner.egl.unmake_current();
}

pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
Expand Down Expand Up @@ -449,38 +477,6 @@ impl super::Context {
inner.egl.unmake_current();
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
use raw_window_handle::RawWindowHandle as Rwh;

let inner = self.platform.inner.lock().unwrap();
let mut swapchain = surface.platform.swapchain.lock().unwrap();
if let Some(s) = swapchain.take() {
inner
.egl
.instance
.destroy_surface(inner.egl.display, s.surface)
.unwrap();
}
if let Rwh::Wayland(handle) = surface.platform.window_handle {
unsafe {
let wl_egl_window_destroy: libloading::Symbol<WlEglWindowDestroyFun> = surface
.platform
.library
.as_ref()
.unwrap()
.get(b"wl_egl_window_destroy")
.unwrap();
wl_egl_window_destroy(handle.surface.as_ptr());
}
}
inner.egl.make_current();
unsafe {
inner.glow.delete_renderbuffer(surface.renderbuf);
inner.glow.delete_framebuffer(surface.framebuf);
}
inner.egl.unmake_current();
}

pub(super) fn lock(&self) -> ContextLock {
let inner = self.platform.inner.lock().unwrap();
inner.egl.make_current();
Expand Down
11 changes: 4 additions & 7 deletions blade-graphics/src/gles/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ impl super::Context {
pub fn create_surface<I>(
&self,
_window: &I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
let platform = PlatformSurface {
info: crate::SurfaceInfo {
Expand All @@ -110,17 +109,17 @@ impl super::Context {
},
extent: crate::Extent::default(),
};
let mut surface = unsafe {
Ok(unsafe {
super::Surface {
platform,
renderbuf: self.platform.glow.create_renderbuffer().unwrap(),
framebuf: self.platform.glow.create_framebuffer().unwrap(),
}
};
self.reconfigure_surface(&mut surface, config);
Ok(surface)
})
}

pub fn destroy_surface(&self, _surface: &mut super::Surface) {}

pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
//TODO: create WebGL context here
let format_desc = super::describe_texture_format(surface.platform.info.format);
Expand All @@ -146,8 +145,6 @@ impl super::Context {
surface.platform.extent = config.size;
}

pub fn destroy_surface(&self, _surface: &mut super::Surface) {}

/// Obtain a lock to the EGL context and get handle to the [`glow::Context`] that can be used to
/// do rendering.
pub(super) fn lock(&self) -> &glow::Context {
Expand Down
14 changes: 14 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ pub struct DeviceInformation {
pub driver_info: String,
}

impl Context {
pub fn create_surface_configured<
I: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle,
>(
&self,
window: &I,
config: SurfaceConfig,
) -> Result<Surface, NotSupportedError> {
let mut surface = self.create_surface(window)?;
self.reconfigure_surface(&mut surface, config);
Ok(surface)
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Memory {
/// Device-local memory. Fast for GPU operations.
Expand Down
25 changes: 10 additions & 15 deletions blade-graphics/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ impl super::Surface {
}

impl super::Context {
pub fn create_surface<
I: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle,
>(
pub fn create_surface<I: raw_window_handle::HasWindowHandle>(
&self,
window: &I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
let mut surface = match window.window_handle().unwrap().as_raw() {
Ok(match window.window_handle().unwrap().as_raw() {
#[cfg(target_os = "ios")]
raw_window_handle::RawWindowHandle::UiKit(handle) => unsafe {
super::Surface::from_view(handle.ui_view.as_ptr() as *mut _)
Expand All @@ -99,9 +96,14 @@ impl super::Context {
super::Surface::from_view(handle.ns_view.as_ptr() as *mut _)
},
_ => return Err(crate::NotSupportedError::PlatformNotSupported),
};
self.reconfigure_surface(&mut surface, config);
Ok(surface)
})
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
unsafe {
let () = msg_send![surface.view, release];
}
surface.view = ptr::null_mut();
}

pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
Expand Down Expand Up @@ -141,11 +143,4 @@ impl super::Context {
let () = msg_send![surface.render_layer, setDisplaySyncEnabled: vsync];
}
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
unsafe {
let () = msg_send![surface.view, release];
}
surface.view = ptr::null_mut();
}
}
7 changes: 2 additions & 5 deletions blade-graphics/src/vulkan/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl super::Context {
>(
&self,
window: &I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
let khr_swapchain = self
.device
Expand Down Expand Up @@ -119,7 +118,7 @@ impl super::Context {
.unwrap()
};

let mut this = super::Surface {
Ok(super::Surface {
device: khr_swapchain,
raw,
frames: Vec::new(),
Expand All @@ -131,9 +130,7 @@ impl super::Context {
target_size: [0; 2],
},
_full_screen_exclusive: fullscreen_exclusive_ext.full_screen_exclusive_supported != 0,
};
self.reconfigure_surface(&mut this, config);
Ok(this)
})
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
Expand Down
2 changes: 1 addition & 1 deletion examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Example {
let window_size = window.inner_size();

let surface = context
.create_surface(window, Self::make_surface_config(window_size))
.create_surface_configured(window, Self::make_surface_config(window_size))
.unwrap();

let global_layout = <Params as gpu::ShaderData>::layout();
Expand Down
4 changes: 3 additions & 1 deletion examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ impl Example {
display_sync: gpu::DisplaySync::Block,
..Default::default()
};
let surface = context.create_surface(window, surface_config).unwrap();
let surface = context
.create_surface_configured(window, surface_config)
.unwrap();
let surface_info = surface.info();

let gui_painter = blade_egui::GuiPainter::new(surface_info, &context);
Expand Down
4 changes: 3 additions & 1 deletion examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl Example {
transparent: true,
..Default::default()
};
let surface = context.create_surface(window, surface_config).unwrap();
let surface = context
.create_surface_configured(window, surface_config)
.unwrap();

let target = context.create_texture(gpu::TextureDesc {
name: "main",
Expand Down
4 changes: 3 additions & 1 deletion examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ impl Example {

let surface_config = Self::make_surface_config(window.inner_size());
let surface_size = surface_config.size;
let surface = context.create_surface(window, surface_config).unwrap();
let surface = context
.create_surface_configured(window, surface_config)
.unwrap();
let surface_info = surface.info();

let num_workers = num_cpus::get_physical().max((num_cpus::get() * 3 + 2) / 4);
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ impl Engine {

let surface_config = Self::make_surface_config(window.inner_size());
let surface_size = surface_config.size;
let gpu_surface = gpu_context.create_surface(window, surface_config).unwrap();
let gpu_surface = gpu_context
.create_surface_configured(window, surface_config)
.unwrap();
let surface_info = gpu_surface.info();

let num_workers = num_cpus::get_physical().max((num_cpus::get() * 3 + 2) / 4);
Expand Down

0 comments on commit 7853e7d

Please sign in to comment.