Desktop: Ready runtime and render node for desktop (#2952)

* Desktop: Ready runtime and render node for desktop

* Address review comments
This commit is contained in:
Dennis Kobert
2025-07-29 01:23:35 +02:00
committed by GitHub
parent 6119dea58c
commit 2247dd9818
13 changed files with 177 additions and 102 deletions

View File

@@ -57,6 +57,15 @@ pub struct ImageTexture {
pub texture: (),
}
impl<'a> serde::Deserialize<'a> for ImageTexture {
fn deserialize<D>(_: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
unimplemented!("attempted to serialize a texture")
}
}
impl Hash for ImageTexture {
#[cfg(feature = "wgpu")]
fn hash<H: Hasher>(&self, state: &mut H) {

View File

@@ -4,7 +4,7 @@ use crate::wasm_application_io::WasmEditorApi;
use dyn_any::DynAny;
pub use dyn_any::StaticType;
pub use glam::{DAffine2, DVec2, IVec2, UVec2};
use graphene_application_io::SurfaceFrame;
use graphene_application_io::{ImageTexture, SurfaceFrame};
use graphene_brush::brush_cache::BrushCache;
use graphene_brush::brush_stroke::BrushStroke;
use graphene_core::raster::Image;
@@ -429,7 +429,12 @@ pub struct RenderOutput {
#[derive(Debug, Clone, Hash, PartialEq, dyn_any::DynAny, serde::Serialize, serde::Deserialize)]
pub enum RenderOutputType {
CanvasFrame(SurfaceFrame),
Svg { svg: String, image_data: Vec<(u64, Image<Color>)> },
#[serde(skip)]
Texture(ImageTexture),
Svg {
svg: String,
image_data: Vec<(u64, Image<Color>)>,
},
Image(Vec<u8>),
}

View File

@@ -137,7 +137,6 @@ impl WasmApplicationIo {
let wgpu_available = executor.is_some();
WGPU_AVAILABLE.store(wgpu_available as i8, Ordering::SeqCst);
// Always enable wgpu when running with Tauri
let mut io = Self {
#[cfg(target_arch = "wasm32")]
ids: AtomicU64::new(0),
@@ -149,6 +148,27 @@ impl WasmApplicationIo {
io.resources.insert("null".to_string(), Arc::from(include_bytes!("null.png").to_vec()));
io
}
#[cfg(all(not(target_arch = "wasm32"), feature = "wgpu"))]
pub fn new_with_context(context: wgpu_executor::Context) -> Self {
#[cfg(feature = "wgpu")]
let executor = WgpuExecutor::with_context(context);
#[cfg(not(feature = "wgpu"))]
let wgpu_available = false;
#[cfg(feature = "wgpu")]
let wgpu_available = executor.is_some();
WGPU_AVAILABLE.store(wgpu_available as i8, Ordering::SeqCst);
let mut io = Self {
gpu_executor: executor,
windows: Vec::new(),
resources: HashMap::new(),
};
io.resources.insert("null".to_string(), Arc::from(include_bytes!("null.png").to_vec()));
io
}
}

View File

@@ -170,10 +170,10 @@ async fn render_canvas(
render_config: RenderConfig,
data: impl GraphicElementRendered,
editor: &WasmEditorApi,
surface_handle: wgpu_executor::WgpuSurface,
surface_handle: Option<wgpu_executor::WgpuSurface>,
render_params: RenderParams,
) -> RenderOutputType {
use graphene_application_io::SurfaceFrame;
use graphene_application_io::{ImageTexture, SurfaceFrame};
let footprint = render_config.viewport;
let Some(exec) = editor.application_io.as_ref().unwrap().gpu_executor() else {
@@ -194,17 +194,26 @@ async fn render_canvas(
if !data.contains_artboard() && !render_config.hide_artboards {
background = Color::WHITE;
}
exec.render_vello_scene(&scene, &surface_handle, footprint.resolution, &context, background)
.await
.expect("Failed to render Vello scene");
if let Some(surface_handle) = surface_handle {
exec.render_vello_scene(&scene, &surface_handle, footprint.resolution, &context, background)
.await
.expect("Failed to render Vello scene");
let frame = SurfaceFrame {
surface_id: surface_handle.window_id,
resolution: render_config.viewport.resolution,
transform: glam::DAffine2::IDENTITY,
};
let frame = SurfaceFrame {
surface_id: surface_handle.window_id,
resolution: render_config.viewport.resolution,
transform: glam::DAffine2::IDENTITY,
};
RenderOutputType::CanvasFrame(frame)
RenderOutputType::CanvasFrame(frame)
} else {
let texture = exec
.render_vello_scene_to_texture(&scene, footprint.resolution, &context, background)
.await
.expect("Failed to render Vello scene");
RenderOutputType::Texture(ImageTexture { texture: Arc::new(texture) })
}
}
#[cfg(target_arch = "wasm32")]
@@ -316,12 +325,14 @@ async fn render<'a: 'n, T: 'n + GraphicElementRendered + WasmNotSend>(
let data = data.eval(ctx.clone()).await;
let editor_api = editor_api.eval(None).await;
#[cfg(all(feature = "vello", not(test)))]
let surface_handle = _surface_handle.eval(None).await;
#[cfg(all(feature = "vello", not(test), target_arch = "wasm32"))]
let _surface_handle = _surface_handle.eval(None).await;
#[cfg(not(target_arch = "wasm32"))]
let _surface_handle: Option<wgpu_executor::WgpuSurface> = None;
let use_vello = editor_api.editor_preferences.use_vello();
#[cfg(all(feature = "vello", not(test)))]
let use_vello = use_vello && surface_handle.is_some();
#[cfg(all(feature = "vello", not(test), target_arch = "wasm32"))]
let use_vello = use_vello && _surface_handle.is_some();
let mut metadata = RenderMetadata::default();
data.collect_metadata(&mut metadata, footprint, None);
@@ -333,7 +344,7 @@ async fn render<'a: 'n, T: 'n + GraphicElementRendered + WasmNotSend>(
if use_vello && editor_api.application_io.as_ref().unwrap().gpu_executor().is_some() {
#[cfg(all(feature = "vello", not(test)))]
return RenderOutput {
data: render_canvas(render_config, data, editor_api, surface_handle.unwrap(), render_params).await,
data: render_canvas(render_config, data, editor_api, _surface_handle, render_params).await,
metadata,
};
#[cfg(any(not(feature = "vello"), test))]

View File

@@ -33,7 +33,10 @@ impl Context {
.request_device(&wgpu::DeviceDescriptor {
label: None,
// #[cfg(not(feature = "passthrough"))]
#[cfg(target_arch = "wasm32")]
required_features: wgpu::Features::empty(),
#[cfg(not(target_arch = "wasm32"))]
required_features: wgpu::Features::PUSH_CONSTANTS,
// Currently disabled because not all backend support passthrough.
// TODO: reenable only when vulkan adapter is available
// #[cfg(feature = "passthrough")]
@@ -45,11 +48,6 @@ impl Context {
.await
.ok()?;
let info = adapter.get_info();
// skip this on LavaPipe temporarily
if info.vendor == 0x10005 {
return None;
}
Some(Self {
device: Arc::new(device),
queue: Arc::new(queue),

View File

@@ -141,6 +141,53 @@ impl WgpuExecutor {
Ok(())
}
pub async fn render_vello_scene_to_texture(&self, scene: &Scene, size: UVec2, context: &RenderContext, background: Color) -> Result<wgpu::Texture> {
let texture = self.context.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: size.x.max(1),
height: size.y.max(1),
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
usage: wgpu::TextureUsages::STORAGE_BINDING | wgpu::TextureUsages::TEXTURE_BINDING,
format: VELLO_SURFACE_FORMAT,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let [r, g, b, _] = background.to_rgba8_srgb();
let render_params = RenderParams {
// We are using an explicit opaque color here to eliminate the alpha premultiplication step
// which would be required to support a transparent webgpu canvas
base_color: vello::peniko::Color::from_rgba8(r, g, b, 0xff),
width: size.x,
height: size.y,
antialiasing_method: AaConfig::Msaa16,
};
{
let mut renderer = self.vello_renderer.lock().await;
for (image, texture) in context.resource_overrides.iter() {
let texture_view = wgpu::TexelCopyTextureInfoBase {
texture: texture.clone(),
mip_level: 0,
origin: Origin3d::ZERO,
aspect: TextureAspect::All,
};
renderer.override_image(image, Some(texture_view));
}
renderer.render_to_texture(&self.context.device, &self.context.queue, scene, &view, &render_params)?;
for (image, _) in context.resource_overrides.iter() {
renderer.override_image(image, None);
}
}
Ok(texture)
}
#[cfg(target_arch = "wasm32")]
pub fn create_surface(&self, canvas: graphene_application_io::WasmSurfaceHandle) -> Result<SurfaceHandle<Surface>> {
let surface = self.context.instance.create_surface(wgpu::SurfaceTarget::Canvas(canvas.surface))?;
@@ -182,6 +229,24 @@ impl WgpuExecutor {
.map_err(|e| anyhow::anyhow!("Failed to create Vello renderer: {:?}", e))
.ok()?;
Some(Self {
context,
vello_renderer: vello_renderer.into(),
})
}
pub fn with_context(context: Context) -> Option<Self> {
let vello_renderer = Renderer::new(
&context.device,
RendererOptions {
pipeline_cache: None,
use_cpu: false,
antialiasing_support: AaSupport::all(),
num_init_threads: std::num::NonZeroUsize::new(1),
},
)
.map_err(|e| anyhow::anyhow!("Failed to create Vello renderer: {:?}", e))
.ok()?;
Some(Self {
context,
vello_renderer: vello_renderer.into(),