Skip to content

Commit

Permalink
hal/gles: support externally initialized contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 4, 2022
1 parent 43f09fd commit 75b885b
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 178 deletions.
134 changes: 134 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ gles = ["naga/glsl-out", "glow", "egl", "libloading"]
dx12 = ["naga/hlsl-out", "native", "bit-set", "range-alloc", "winapi/d3d12", "winapi/d3d12shader", "winapi/d3d12sdklayers", "winapi/dxgi1_6"]
renderdoc = ["libloading", "renderdoc-sys"]

[[example]]
name = "halmark"

[[example]]
name = "raw-gles"
required-features = ["gles"]

[dependencies]
bitflags = "1.0"
parking_lot = "0.11"
Expand Down Expand Up @@ -86,4 +93,5 @@ features = ["wgsl-in"]

[dev-dependencies]
env_logger = "0.8"
winit = "0.26"
winit = "0.26" # for "halmark" example
glutin = "0.28" # for "gles" example
2 changes: 2 additions & 0 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This example shows basic usage of wgpu-hal by rendering
//! a ton of moving sprites, each with a separate texture and draw call.
extern crate wgpu_hal as hal;

use hal::{
Expand Down
118 changes: 118 additions & 0 deletions wgpu-hal/examples/raw-gles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! This example shows interop with raw GLES contexts -
//! the ability to hook up wgpu-hal to an existing context and draw into it.

extern crate wgpu_hal as hal;
use hal::{Adapter as _, CommandEncoder as _, Device as _, Queue as _};

fn main() {
env_logger::init();
println!("Initializing external GL context");

let event_loop = glutin::event_loop::EventLoop::new();
let window_builder = glutin::window::WindowBuilder::new();
let gl_context = unsafe {
glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (3, 0)))
.build_windowed(window_builder, &event_loop)
.unwrap()
.make_current()
.unwrap()
};
let inner_size = gl_context.window().inner_size();

println!("Hooking up to wgpu-hal");
let exposed = unsafe {
<hal::api::Gles as hal::Api>::Adapter::new_external(|name| {
gl_context.get_proc_address(name)
})
}
.expect("GL adapter can't be initialized");
let mut od = unsafe {
exposed
.adapter
.open(wgt::Features::empty(), &wgt::Limits::downlevel_defaults())
}
.unwrap();

let format = wgt::TextureFormat::Rgba8UnormSrgb;
let texture = <hal::api::Gles as hal::Api>::Texture::default_framebuffer(format);
let view = unsafe {
od.device
.create_texture_view(
&texture,
&hal::TextureViewDescriptor {
label: None,
format,
dimension: wgt::TextureViewDimension::D2,
usage: hal::TextureUses::COLOR_TARGET,
range: wgt::ImageSubresourceRange::default(),
},
)
.unwrap()
};

println!("Filling the screen");
let mut encoder = unsafe {
od.device
.create_command_encoder(&hal::CommandEncoderDescriptor {
label: None,
queue: &od.queue,
})
.unwrap()
};
let rp_desc = hal::RenderPassDescriptor {
label: None,
extent: wgt::Extent3d {
width: inner_size.width,
height: inner_size.height,
depth_or_array_layers: 1,
},
sample_count: 1,
color_attachments: &[hal::ColorAttachment {
target: hal::Attachment {
view: &view,
usage: hal::TextureUses::COLOR_TARGET,
},
resolve_target: None,
ops: hal::AttachmentOps::STORE,
clear_value: wgt::Color::BLUE,
}],
depth_stencil_attachment: None,
multiview: None,
};
unsafe {
encoder.begin_encoding(None).unwrap();
encoder.begin_render_pass(&rp_desc);
encoder.end_render_pass();
let cmd_buf = encoder.end_encoding().unwrap();
od.queue.submit(&[&cmd_buf], None).unwrap();
}

println!("Showing the window");
gl_context.swap_buffers().unwrap();

event_loop.run(move |event, _, control_flow| {
use glutin::{
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::ControlFlow,
};
*control_flow = ControlFlow::Wait;

match event {
Event::LoopDestroyed => return,
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => *control_flow = ControlFlow::Exit,
_ => (),
},
_ => (),
}
});
}
Loading

0 comments on commit 75b885b

Please sign in to comment.