Skip to content

Commit 52360b5

Browse files
Implement generic pipeline caching
1 parent fae8c50 commit 52360b5

27 files changed

Lines changed: 580 additions & 430 deletions

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

desktop/src/render/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) struct RenderState {
2929
impl RenderState {
3030
pub(crate) fn new(window: &Window, context: WgpuContext, present_mode: Option<PresentMode>) -> Self {
3131
let size = window.surface_size();
32-
let surface = window.create_surface(context.instance.clone());
32+
let surface = window.create_surface(&context.instance);
3333

3434
let surface_caps = surface.get_capabilities(&context.adapter);
3535
let surface_format = surface_caps.formats.iter().find(|f| f.is_srgb()).copied().unwrap_or(surface_caps.formats[0]);

desktop/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Window {
8686
self.winit_window.request_redraw();
8787
}
8888

89-
pub(crate) fn create_surface(&self, instance: Arc<wgpu::Instance>) -> wgpu::Surface<'static> {
89+
pub(crate) fn create_surface(&self, instance: &wgpu::Instance) -> wgpu::Surface<'static> {
9090
instance.create_surface(self.winit_window.clone()).unwrap()
9191
}
9292

node-graph/graph-craft/src/application_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use graphene_application_io::ApplicationIo;
99
#[derive(Default)]
1010
pub struct PlatformApplicationIo {
1111
#[cfg(feature = "wgpu")]
12-
pub(crate) gpu_executor: Option<WgpuExecutor>,
12+
gpu_executor: Option<WgpuExecutor>,
1313
resources: Option<Box<dyn resource::LoadResource>>,
1414
}
1515

node-graph/graphene-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
127127

128128
// Get reference to wgpu executor and clone device handle
129129
let wgpu_executor_ref = application_io_arc.gpu_executor().unwrap();
130-
let device = wgpu_executor_ref.context.device.clone();
130+
let device = wgpu_executor_ref.context().device.clone();
131131

132132
let preferences = EditorPreferences {
133133
max_render_region_size: EditorPreferences::default().max_render_region_size,

node-graph/interpreted-executor/src/node_registry.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
200200
async_node!(graphene_core::context_modification::ContextModificationNode<_, _>, input: Context, fn_params: [Context => ListDyn, Context => graphene_std::ContextFeatures]),
201201
#[cfg(target_family = "wasm")]
202202
async_node!(graphene_core::context_modification::ContextModificationNode<_, _>, input: Context, fn_params: [Context => CanvasHandle, Context => graphene_std::ContextFeatures]),
203+
async_node!(graphene_core::context_modification::ContextModificationNode<_, _>, input: Context, fn_params: [Context => wgpu_executor::WgpuPipelineCache, Context => graphene_std::ContextFeatures]),
204+
async_node!(graphene_core::context_modification::ContextModificationNode<_, _>, input: Context, fn_params: [Context => &wgpu_executor::WgpuExecutor, Context => graphene_std::ContextFeatures]),
203205
// ==========
204206
// MEMO NODES
205207
// ==========
@@ -285,6 +287,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
285287
async_node!(graphene_core::memo::MemoizeNode<_, _>, input: Context, fn_params: [Context => graphene_std::transform::ScaleType]),
286288
async_node!(graphene_core::memo::MemoizeNode<_, _>, input: Context, fn_params: [Context => graphene_std::vector::misc::InterpolationDistribution]),
287289
async_node!(graphene_core::memo::MemoizeNode<_, _>, input: Context, fn_params: [Context => RenderIntermediate]),
290+
async_node!(graphene_core::memo::MemoizeNode<_, _>, input: Context, fn_params: [Context => wgpu_executor::WgpuPipelineCache]),
291+
async_node!(graphene_core::memo::MemoizeNode<_, _>, input: Context, fn_params: [Context => &wgpu_executor::WgpuExecutor]),
288292
];
289293
// =============
290294
// CONVERT NODES

node-graph/interpreted-executor/src/util.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use graph_craft::ProtoNodeIdentifier;
21
use graph_craft::application_io::PlatformEditorApi;
32
use graph_craft::concrete;
43
use graph_craft::document::value::TaggedValue;
@@ -8,7 +7,6 @@ use graphene_std::Context;
87
use graphene_std::ContextFeatures;
98
use graphene_std::uuid::NodeId;
109
use std::sync::Arc;
11-
use wgpu_executor::WgpuExecutor;
1210

1311
pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEditorApi>) -> NodeNetwork {
1412
let inner_network = DocumentNode {
@@ -40,7 +38,7 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
4038
},
4139
DocumentNode {
4240
call_argument: concrete!(Context),
43-
inputs: vec![NodeInput::scope("editor-api"), NodeInput::node(NodeId(0), 0)],
41+
inputs: vec![NodeInput::scope(graphene_std::platform_application_io::wgpu_executor::IDENTIFIER), NodeInput::node(NodeId(0), 0)],
4442
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_node::render::IDENTIFIER),
4543
context_features: graphene_std::ContextDependencies {
4644
extract: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
@@ -50,7 +48,11 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
5048
},
5149
DocumentNode {
5250
call_argument: concrete!(Context),
53-
inputs: vec![NodeInput::scope("editor-api"), NodeInput::node(NodeId(1), 0)],
51+
inputs: vec![
52+
NodeInput::scope(graphene_std::platform_application_io::wgpu_executor::IDENTIFIER),
53+
NodeInput::scope("editor-api"),
54+
NodeInput::node(NodeId(1), 0),
55+
],
5456
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_cache::render_output_cache::IDENTIFIER),
5557
context_features: graphene_std::ContextDependencies {
5658
extract: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
@@ -60,8 +62,8 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
6062
},
6163
DocumentNode {
6264
call_argument: concrete!(Context),
63-
inputs: vec![NodeInput::scope("editor-api"), NodeInput::node(NodeId(2), 0)],
64-
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::pixel_preview::pixel_preview::IDENTIFIER),
65+
inputs: vec![NodeInput::scope(graphene_std::render_pixel_preview::pixel_preview_pipeline::IDENTIFIER), NodeInput::node(NodeId(2), 0)],
66+
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_pixel_preview::render_pixel_preview::IDENTIFIER),
6567
context_features: graphene_std::ContextDependencies {
6668
extract: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
6769
inject: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
@@ -70,8 +72,11 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
7072
},
7173
DocumentNode {
7274
call_argument: concrete!(Context),
73-
inputs: vec![NodeInput::scope("editor-api"), NodeInput::node(NodeId(3), 0)],
74-
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_node::render_background::IDENTIFIER),
75+
inputs: vec![
76+
NodeInput::scope(graphene_std::render_background::background_compositor_pipeline::IDENTIFIER),
77+
NodeInput::node(NodeId(3), 0),
78+
],
79+
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_background::render_background::IDENTIFIER),
7580
context_features: graphene_std::ContextDependencies {
7681
extract: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
7782
inject: ContextFeatures::empty(),
@@ -100,7 +105,7 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
100105
};
101106

102107
// wrap the inner network in a scope
103-
let mut nodes = vec![
108+
let nodes = vec![
104109
inner_network,
105110
render_node,
106111
DocumentNode {
@@ -109,16 +114,7 @@ pub fn wrap_network_in_scope(network: NodeNetwork, editor_api: Arc<PlatformEdito
109114
..Default::default()
110115
},
111116
];
112-
let mut scope_injections = vec![("editor-api".to_string(), (NodeId(2), concrete!(&PlatformEditorApi)))];
113-
114-
if cfg!(feature = "gpu") {
115-
nodes.push(DocumentNode {
116-
implementation: DocumentNodeImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::ops::IntoNode<&WgpuExecutor>")),
117-
inputs: vec![NodeInput::node(NodeId(2), 0)],
118-
..Default::default()
119-
});
120-
scope_injections.push(("wgpu-executor".to_string(), (NodeId(3), concrete!(&WgpuExecutor))));
121-
}
117+
let scope_injections = vec![("editor-api".to_string(), (NodeId(2), concrete!(&PlatformEditorApi)))];
122118

123119
NodeNetwork {
124120
exports: vec![NodeInput::node(NodeId(1), 0)],

node-graph/libraries/canvas-utils/src/wasm.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl CanvasSurfaceHandle {
6262
if self.1.is_none() {
6363
let canvas = self.0.get().canvas.clone();
6464
let surface = executor
65-
.context
65+
.context()
6666
.instance
6767
.create_surface(wgpu::SurfaceTarget::Canvas(canvas))
6868
.expect("Failed to create surface from canvas");
@@ -86,21 +86,23 @@ impl Canvas for CanvasSurfaceHandle {
8686
#[cfg(feature = "wgpu")]
8787
impl CanvasSurface for CanvasSurfaceHandle {
8888
fn present(&mut self, image_texture: &ImageTexture, executor: &WgpuExecutor) {
89+
let context = executor.context();
90+
8991
let source_texture: &wgpu::Texture = image_texture.as_ref();
9092

9193
let surface = self.surface(executor);
9294

9395
// Blit the texture to the surface
94-
let mut encoder = executor.context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
96+
let mut encoder = context.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
9597
label: Some("Texture to Surface Blit"),
9698
});
9799

98100
let size = source_texture.size();
99101

100102
// Configure the surface at physical resolution (for HiDPI displays)
101-
let surface_caps = surface.get_capabilities(&executor.context.adapter);
103+
let surface_caps = surface.get_capabilities(&context.adapter);
102104
surface.configure(
103-
&executor.context.device,
105+
&context.device,
104106
&wgpu::SurfaceConfiguration {
105107
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
106108
format: wgpu::TextureFormat::Rgba8Unorm,
@@ -134,7 +136,7 @@ impl CanvasSurface for CanvasSurfaceHandle {
134136
source_texture.size(),
135137
);
136138

137-
executor.context.queue.submit([encoder.finish()]);
139+
context.queue.submit([encoder.finish()]);
138140
surface_texture.present();
139141
}
140142
}

node-graph/libraries/core-types/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ impl ProtoNodeIdentifier {
163163
}
164164
}
165165

166+
impl From<ProtoNodeIdentifier> for Cow<'static, str> {
167+
fn from(val: ProtoNodeIdentifier) -> Self {
168+
val.name
169+
}
170+
}
171+
166172
impl Display for ProtoNodeIdentifier {
167173
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
168174
f.debug_tuple("ProtoNodeIdentifier").field(&self.name).finish()

node-graph/libraries/wgpu-executor/src/context.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::sync::Arc;
21
use wgpu::{Adapter, Backends, Device, Features, Instance, Queue};
32

43
#[derive(Debug, Clone)]
54
pub struct Context {
6-
pub device: Arc<Device>,
7-
pub queue: Arc<Queue>,
8-
pub instance: Arc<Instance>,
9-
pub adapter: Arc<Adapter>,
5+
pub device: Device,
6+
pub queue: Queue,
7+
pub instance: Instance,
8+
pub adapter: Adapter,
109
}
1110

1211
impl Context {
@@ -58,12 +57,7 @@ impl ContextBuilder {
5857
let instance = self.build_instance();
5958
let adapter = self.request_adapter(&instance).await?;
6059
let (device, queue) = self.request_device(&adapter).await?;
61-
Some(Context {
62-
device: Arc::new(device),
63-
queue: Arc::new(queue),
64-
adapter: Arc::new(adapter),
65-
instance: Arc::new(instance),
66-
})
60+
Some(Context { device, queue, adapter, instance })
6761
}
6862
}
6963
impl ContextBuilder {
@@ -113,12 +107,7 @@ impl ContextBuilder {
113107
let adapter = if let Some(adapter) = selected_adapter { adapter } else { self.request_adapter(&instance).await? };
114108

115109
let (device, queue) = self.request_device(&adapter).await?;
116-
Some(Context {
117-
device: Arc::new(device),
118-
queue: Arc::new(queue),
119-
adapter: Arc::new(adapter),
120-
instance: Arc::new(instance),
121-
})
110+
Some(Context { device, queue, adapter, instance })
122111
}
123112
async fn select_adapter<S>(&self, instance: &Instance, select: S) -> Option<Adapter>
124113
where

0 commit comments

Comments
 (0)