Skip to content
Draft
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
10 changes: 8 additions & 2 deletions crates/gpui_wgpu/src/wgpu_renderer/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,15 @@ impl WgpuHeadlessRenderer {
pub fn new_with_requirements(
requirements: Option<&crate::WgpuDeviceRequirements>,
) -> anyhow::Result<Self> {
let context = WgpuContext::new_headless(requirements)?;
Self::with_context(&WgpuContext::new_headless(requirements)?)
}

/// Like [`Self::new`], on an existing context's device and queue instead
/// of a new device, so a suite that opens many windows creates one. Each
/// renderer still gets its own sprite atlas and render target.
pub fn with_context(context: &WgpuContext) -> anyhow::Result<Self> {
let renderer = WgpuRenderer::new_headless(
&context,
context,
Size {
width: DevicePixels(1),
height: DevicePixels(1),
Expand Down
52 changes: 51 additions & 1 deletion crates/gpui_wgpu/tests/headless_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use gpui::{
Quad, RenderImageParams, RenderSvgParams, ScaledFilter, ScaledPixels, Scene, ShaderBool,
Shadow, Size, Underline, checkerboard, linear_color_stop, linear_gradient, solid_background,
};
use gpui_ce_wgpu::WgpuHeadlessRenderer;
use gpui_ce_wgpu::{WgpuContext, WgpuHeadlessRenderer};
use smallvec::smallvec;
use std::borrow::Cow;

Expand Down Expand Up @@ -58,6 +58,56 @@ fn the_headless_context_handle_lends_the_renderers_own_device() {
assert!(!handle.device_lost());
}

#[test]
fn renderers_on_one_context_share_its_device_and_render_alike() {
let context = WgpuContext::new_headless(None).expect("headless context");
let mut first = WgpuHeadlessRenderer::with_context(&context).expect("first renderer");
let mut second = WgpuHeadlessRenderer::with_context(&context).expect("second renderer");
let mut own_device = WgpuHeadlessRenderer::new().expect("renderer with its own device");

let first_handle = first.gpu_context_info().expect("a live device");
let second_handle = second.gpu_context_info().expect("a live device");
assert!(first_handle.is_same_device(&second_handle));
assert!(std::ptr::eq(second_handle.device(), &*context.device));
assert!(std::ptr::eq(second_handle.queue(), &*context.queue));
assert!(!first_handle.is_same_device(&own_device.gpu_context_info().expect("a live device")));

let scene = triangle_scene();
let expected = own_device
.render_scene_to_image(&scene, TARGET)
.expect("frame on its own device");
for renderer in [&mut first, &mut second] {
let image = renderer
.render_scene_to_image(&scene, TARGET)
.expect("frame on the shared device");
assert!(
image == expected,
"a shared device renders a different frame"
);
}
}

fn triangle_scene() -> Scene {
let mut scene = Scene::default();
scene.insert_primitive(Quad {
bounds: bounds(0.0, 0.0, 310.0, 100.0),
content_mask: full_mask(),
background: solid_background(gpui::black()),
..Default::default()
});
let mut builder = gpui::PathBuilder::fill();
builder.move_to(gpui::point(gpui::px(20.0), gpui::px(20.0)));
builder.line_to(gpui::point(gpui::px(80.0), gpui::px(20.0)));
builder.line_to(gpui::point(gpui::px(50.0), gpui::px(80.0)));
builder.close();
let mut path = builder.build().expect("triangle").scale(1.0);
path.content_mask = full_mask();
path.color = solid_background(gpui::white());
scene.insert_primitive(path);
scene.finish();
scene
}

fn bounds(x: f32, y: f32, w: f32, h: f32) -> Bounds<ScaledPixels> {
Bounds {
origin: Point {
Expand Down
Loading