Skip to content

Commit

Permalink
priotirize osmesa over glutin conext builder (#379)
Browse files Browse the repository at this point in the history
* priotirize osmesa over glutin conext builder

* run `cargo fmt`

* fix non linux code to match that of linux

* Add run headless example action

* fix

* fix?

* Hmm

* Test

* Fix?

---------

Co-authored-by: Asger Nyman Christiansen <[email protected]>
  • Loading branch information
adhami3310 and asny committed Jul 25, 2023
1 parent 177e9bb commit 4b7600c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,19 @@ jobs:
echo "::endgroup::"
fi
done
headless:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- run: sudo apt-get update && sudo apt-get install -y xvfb

- run: xvfb-run -a cargo run --example headless --features="headless"
4 changes: 3 additions & 1 deletion src/core/render_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ macro_rules! impl_render_target_core_extensions {
impl_render_target_core_extensions!(RenderTarget<'a>);
impl_render_target_core_extensions!(ColorTarget<'a>);
impl_render_target_core_extensions!(DepthTarget<'a>);
impl_render_target_core_extensions!(RenderTargetMultisample<C: TextureDataType, D: DepthTextureDataType>);
impl_render_target_core_extensions!(
RenderTargetMultisample<C: TextureDataType, D: DepthTextureDataType>
);
impl_render_target_core_extensions!(ColorTargetMultisample<C: TextureDataType>);
impl_render_target_core_extensions!(DepthTargetMultisample<D: DepthTextureDataType>);
4 changes: 3 additions & 1 deletion src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ macro_rules! impl_render_target_extensions {
impl_render_target_extensions!(RenderTarget<'a>);
impl_render_target_extensions!(ColorTarget<'a>);
impl_render_target_extensions!(DepthTarget<'a>);
impl_render_target_extensions!(RenderTargetMultisample<C: TextureDataType, D: DepthTextureDataType>);
impl_render_target_extensions!(
RenderTargetMultisample<C: TextureDataType, D: DepthTextureDataType>
);
impl_render_target_extensions!(ColorTargetMultisample<C: TextureDataType>);
impl_render_target_extensions!(DepthTargetMultisample<D: DepthTextureDataType>);

Expand Down
19 changes: 10 additions & 9 deletions src/window/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl HeadlessContext {
#[allow(unsafe_code)]
pub fn new() -> Result<Self, HeadlessError> {
let cb = ContextBuilder::new();
let (glutin_context, _el) = build_context(cb)?;
let glutin_context = build_context(cb)?;
let glutin_context = unsafe { glutin_context.make_current().map_err(|(_, e)| e)? };
let context = Context::from_gl_context(std::sync::Arc::new(unsafe {
crate::context::Context::from_loader_function(|s| {
Expand Down Expand Up @@ -88,29 +88,30 @@ fn build_context_osmesa<T1: ContextCurrentState>(
#[cfg(target_os = "linux")]
fn build_context<T1: ContextCurrentState>(
cb: ContextBuilder<T1>,
) -> Result<(glutin_029::Context<NotCurrent>, EventLoop<()>), CreationError> {
) -> Result<glutin_029::Context<NotCurrent>, CreationError> {
// On unix operating systems, you should always try for surfaceless first,
// and if that does not work, headless (pbuffers), and if that too fails,
// finally osmesa.
//
// If willing, you could attempt to use hidden windows instead of os mesa,
// but note that you must handle events for the window that come on the
// events loop.
let el = EventLoop::new();

/*
let err1 = match build_context_surfaceless(cb.clone(), &el) {
Ok(ctx) => return Ok((ctx, el)),
Err(err) => err,
};*/

let err2 = match build_context_headless(cb.clone(), &el) {
Ok(ctx) => return Ok((ctx, el)),
let _err3 = match build_context_osmesa(cb.clone()) {
Ok(ctx) => return Ok(ctx),
Err(err) => err,
};

let _err3 = match build_context_osmesa(cb) {
Ok(ctx) => return Ok((ctx, el)),
let el = EventLoop::new();

let err2 = match build_context_headless(cb, &el) {
Ok(ctx) => return Ok(ctx),
Err(err) => err,
};

Expand All @@ -120,7 +121,7 @@ fn build_context<T1: ContextCurrentState>(
#[cfg(not(target_os = "linux"))]
fn build_context<T1: ContextCurrentState>(
cb: ContextBuilder<T1>,
) -> Result<(glutin_029::Context<NotCurrent>, EventLoop<()>), CreationError> {
) -> Result<glutin_029::Context<NotCurrent>, CreationError> {
let el = EventLoop::new();
build_context_headless(cb.clone(), &el).map(|ctx| (ctx, el))
build_context_headless(cb.clone(), &el)
}

0 comments on commit 4b7600c

Please sign in to comment.