Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Render Graph v1 #574

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ log = { workspace = true }
raw-window-handle = { workspace = true }
futures-intrusive = { workspace = true }
wgpu-profiler = { workspace = true, optional = true }
slotmap = { workspace = true }

[workspace.lints]
clippy.doc_markdown = "warn"
Expand All @@ -77,6 +78,7 @@ skrifa = "0.19.0"
peniko = "0.1.0"
futures-intrusive = "0.5.0"
raw-window-handle = "0.6.0"
slotmap = "1.0.7"

# NOTE: Make sure to keep this in sync with the version badge in README.md
wgpu = { version = "0.19.3" }
Expand Down
36 changes: 31 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod cpu_dispatch;
mod cpu_shader;
mod recording;
mod render;
mod render_graph;
mod scene;
mod shaders;
#[cfg(feature = "wgpu")]
Expand All @@ -100,6 +101,10 @@ pub use peniko;
/// 2D geometry, with a focus on curves.
pub use peniko::kurbo;

use render_graph::{
passes::{VelloCoarse, VelloFine},
RenderGraph, ResourceManager,
};
#[doc(hidden)]
pub use skrifa;

Expand Down Expand Up @@ -286,11 +291,32 @@ impl Renderer {
texture: &TextureView,
params: &RenderParams,
) -> Result<()> {
let (recording, target) = render::render_full(scene, &self.shaders, params);
let external_resources = [ExternalResource::Image(
*target.as_image().unwrap(),
texture,
)];
let mut render_graph = RenderGraph::new();

let mut resources = ResourceManager::new();
let out_image = ImageProxy::new(params.width, params.height, ImageFormat::Rgba8);
let out_image = resources.import_image(out_image);

let coarse = render_graph.insert_pass((), |()| VelloCoarse {});
let _fine = render_graph.insert_pass((coarse,), move |(coarse,)| VelloFine {
config_buf: coarse.config_buf,
tile_buf: coarse.tile_buf,
segments_buf: coarse.segments_buf,
ptcl_buf: coarse.ptcl_buf,
gradient_image: coarse.gradient_image,
info_bin_data_buf: coarse.info_bin_data_buf,
image_atlas: coarse.image_atlas,
out_image,
fine_workgroup_size: coarse.fine_workgroup_size,
});

let Some(recording) =
render_graph.process(resources, params, &self.shaders, scene.encoding(), false)
else {
panic!("Cyclic Render Graph");
};

let external_resources = [ExternalResource::Image(out_image.into(), texture)];
self.engine.run_recording(
device,
queue,
Expand Down
45 changes: 39 additions & 6 deletions src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ impl Recording {

pub fn dispatch<R>(&mut self, shader: ShaderId, wg_size: (u32, u32, u32), resources: R)
where
R: IntoIterator,
R::Item: Into<ResourceProxy>,
R: IntoResourceProxies,
{
let r = resources.into_iter().map(|r| r.into()).collect();
let r = resources.into_resource_proxies();
self.push(Command::Dispatch(shader, wg_size, r));
}

Expand All @@ -168,10 +167,9 @@ impl Recording {
offset: u64,
resources: R,
) where
R: IntoIterator,
R::Item: Into<ResourceProxy>,
R: IntoResourceProxies,
{
let r = resources.into_iter().map(|r| r.into()).collect();
let r = resources.into_resource_proxies();
self.push(Command::DispatchIndirect(shader, buf, offset, r));
}

Expand Down Expand Up @@ -206,6 +204,11 @@ impl Recording {
}
}

/// Moves all the commands of other into self, leaving other empty.
pub fn append(&mut self, other: &mut Recording) {
self.commands.append(&mut other.commands);
}

/// Returns a [`Vec`] containing all the [`Command`]s in order.
pub fn into_commands(self) -> Vec<Command> {
self.commands
Expand Down Expand Up @@ -277,3 +280,33 @@ impl From<ImageProxy> for ResourceProxy {
Self::Image(value)
}
}

pub trait IntoResourceProxies {
fn into_resource_proxies(self) -> Vec<ResourceProxy>;
}

macro_rules! impl_into_resource_proxies {
( $(($generic:ident, $index:tt))+ ) => {
impl<$($generic: Into<ResourceProxy>),+> IntoResourceProxies for ($($generic,)+) {
#[inline]
fn into_resource_proxies(self) -> Vec<ResourceProxy> {
vec![
$(
self.$index.into(),
)+
]
}
}
};
}

impl_into_resource_proxies!((A, 0));
impl_into_resource_proxies!((A, 0)(B, 1));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4)(F, 5));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4)(F, 5)(G, 6));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4)(F, 5)(G, 6)(H, 7));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4)(F, 5)(G, 6)(H, 7)(I, 8));
impl_into_resource_proxies!((A, 0)(B, 1)(C, 2)(D, 3)(E, 4)(F, 5)(G, 6)(H, 7)(I, 8)(J, 9));
Loading