Skip to content
Merged
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
11 changes: 7 additions & 4 deletions examples/wgpu_texture/src/demo_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
use crate::Color;
use anyrender_vello::wgpu_context::DeviceHandle;
use anyrender_vello::{CustomPaintCtx, CustomPaintSource, TextureHandle};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Instant;
use wgpu::{Device, Queue};
use wgpu::Instance;

pub struct DemoPaintSource {
state: DemoRendererState,
Expand All @@ -15,9 +16,9 @@ pub struct DemoPaintSource {
}

impl CustomPaintSource for DemoPaintSource {
fn resume(&mut self, device: &Device, queue: &Queue) {
fn resume(&mut self, _instance: &Instance, device_handle: &DeviceHandle) {
// TODO: work out what to do about width/height
let active_state = ActiveDemoRenderer::new(device, queue);
let active_state = ActiveDemoRenderer::new(device_handle);
self.state = DemoRendererState::Active(Box::new(active_state));
}

Expand Down Expand Up @@ -110,7 +111,9 @@ impl DemoPaintSource {
}

impl ActiveDemoRenderer {
pub(crate) fn new(device: &Device, queue: &Queue) -> Self {
pub(crate) fn new(device_handle: &DeviceHandle) -> Self {
let device = &device_handle.device;
let queue = &device_handle.queue;
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(include_str!(
Expand Down
5 changes: 3 additions & 2 deletions packages/anyrender_vello/src/custom_paint_source.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use crate::wgpu_context::DeviceHandle;
use peniko::Blob;
use vello::Renderer as VelloRenderer;
use vello::peniko::Image;
use wgpu::{Device, Queue, TexelCopyTextureInfoBase, Texture};
use wgpu::{Instance, TexelCopyTextureInfoBase, Texture};

pub trait CustomPaintSource: 'static {
fn resume(&mut self, device: &Device, queue: &Queue);
fn resume(&mut self, instance: &Instance, device_handle: &DeviceHandle);
fn suspend(&mut self);
fn render(
&mut self,
Expand Down
9 changes: 1 addition & 8 deletions packages/anyrender_vello/src/wgpu_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,11 @@ pub struct WGPUContext {
/// Q: could we drop the adapter here? wgpu docs say adapters do not need to be kept around...
#[derive(Clone, Debug)]
pub struct DeviceHandle {
adapter: Adapter,
pub adapter: Adapter,
pub device: Device,
pub queue: Queue,
}

impl DeviceHandle {
/// Returns the adapter associated with the device.
pub fn adapter(&self) -> &Adapter {
&self.adapter
}
}

impl WGPUContext {
pub fn new() -> Self {
Self::with_features_and_limits(None, None)
Expand Down
29 changes: 12 additions & 17 deletions packages/anyrender_vello/src/window_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
CustomPaintSource,
wgpu_context::{RenderSurface, WGPUContext},
wgpu_context::{DeviceHandle, RenderSurface, WGPUContext},
};
use anyrender::{WindowHandle, WindowRenderer};
use peniko::Color;
Expand All @@ -12,9 +12,7 @@ use std::sync::{
use vello::{
AaSupport, RenderParams, Renderer as VelloRenderer, RendererOptions, Scene as VelloScene,
};
use wgpu::{
CommandEncoderDescriptor, Device, Features, Limits, PresentMode, Queue, TextureViewDescriptor,
};
use wgpu::{CommandEncoderDescriptor, Features, Limits, PresentMode, TextureViewDescriptor};

use crate::{DEFAULT_THREADS, VelloScenePainter};

Expand All @@ -33,16 +31,11 @@ enum RenderState {
}

impl RenderState {
fn current_device_and_queue(&self) -> Option<(&Device, &Queue)> {
fn current_device_handle(&self) -> Option<&DeviceHandle> {
let RenderState::Active(state) = self else {
return None;
};

let device_handle = &state.surface.device_handle;
let device = &device_handle.device;
let queue = &device_handle.queue;

Some((device, queue))
Some(&state.surface.device_handle)
}
}

Expand Down Expand Up @@ -76,13 +69,14 @@ impl VelloWindowRenderer {
}
}

pub fn current_device_and_queue(&self) -> Option<(&Device, &Queue)> {
self.render_state.current_device_and_queue()
pub fn current_device_handle(&self) -> Option<&DeviceHandle> {
self.render_state.current_device_handle()
}

pub fn register_custom_paint_source(&mut self, mut source: Box<dyn CustomPaintSource>) -> u64 {
if let Some((device, queue)) = self.render_state.current_device_and_queue() {
source.resume(device, queue);
if let Some(device_handle) = self.render_state.current_device_handle() {
let instance = &self.wgpu_context.instance;
source.resume(instance, device_handle);
}
let id = PAINT_SOURCE_ID.fetch_add(1, atomic::Ordering::SeqCst);
self.custom_paint_sources.insert(id, source);
Expand Down Expand Up @@ -131,9 +125,10 @@ impl WindowRenderer for VelloWindowRenderer {

self.render_state = RenderState::Active(ActiveRenderState { renderer, surface });

let (device, queue) = self.render_state.current_device_and_queue().unwrap();
let device_handle = self.render_state.current_device_handle().unwrap();
let instance = &self.wgpu_context.instance;
for source in self.custom_paint_sources.values_mut() {
source.resume(device, queue)
source.resume(instance, device_handle)
}
}

Expand Down