Skip to content

Commit

Permalink
fix: missing allocator type parameter for command buffer in graph record
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAPenguin0 committed Apr 19, 2023
1 parent ad9e8aa commit 3d0ae3d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 32 deletions.
6 changes: 4 additions & 2 deletions examples/03_raytracing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use phobos::pipeline::raytracing::RayTracingPipelineBuilder;
use phobos::prelude::*;
use phobos::util::align::align;

use crate::example_runner::{Context, create_shader, ExampleApp, ExampleRunner, load_spirv_file, WindowContext};
use crate::example_runner::{Context, create_shader, ExampleApp, ExampleRunner, load_spirv_file, save_dotfile, WindowContext};

#[path = "../example_runner/lib.rs"]
mod example_runner;
Expand Down Expand Up @@ -307,7 +307,7 @@ impl ExampleApp for RaytracingSample {
float32: [0.0, 0.0, 0.0, 0.0],
}),
)?
.sample_image(rt_pass.output(&rt_image).unwrap(), PipelineStage::RAY_TRACING_SHADER_KHR)
.sample_image(rt_pass.output(&rt_image).unwrap(), PipelineStage::FRAGMENT_SHADER)
.execute_fn(|cmd, ifc, bindings, _| {
let vertices: Vec<f32> =
vec![-1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0];
Expand All @@ -329,6 +329,8 @@ impl ExampleApp for RaytracingSample {
.add_pass(present)?
.build()?;

save_dotfile(graph.task_graph(), "graph.svg");

let mut bindings = PhysicalResourceBindings::new();
bindings.bind_image("swapchain", ifc.swapchain_image.as_ref().unwrap());
bindings.bind_image("rt_out", &self.attachment_view);
Expand Down
32 changes: 31 additions & 1 deletion examples/example_runner/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::fs;
use std::fs::File;
use std::io::Read;
use std::io::{Read, Write};
use std::path::Path;

use anyhow::{bail, Result};
use futures::executor::block_on;
use layout::backends::svg::SVGWriter;
use layout::gv;
use layout::gv::GraphBuilder;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder};
use winit::window::{Window, WindowBuilder};
Expand All @@ -21,11 +24,38 @@ pub fn load_spirv_file(path: &Path) -> Vec<u32> {
Vec::from(binary)
}

#[allow(dead_code)]
pub fn create_shader(path: &str, stage: vk::ShaderStageFlags) -> ShaderCreateInfo {
let code = load_spirv_file(Path::new(path));
ShaderCreateInfo::from_spirv(stage, code)
}

#[allow(dead_code)]
pub fn save_dotfile<G>(graph: &G, path: &str)
where
G: GraphViz, {
let dot = graph.dot().unwrap();
let dot = format!("{}", dot);
let mut parser = gv::DotParser::new(&dot);
match parser.process() {
Ok(g) => {
let mut svg = SVGWriter::new();
let mut builder = GraphBuilder::new();
builder.visit_graph(&g);
let mut vg = builder.get();
vg.do_it(false, false, false, &mut svg);
let svg = svg.finalize();
let mut f = File::create(Path::new(path)).unwrap();
f.write(&svg.as_bytes()).unwrap();
}
Err(e) => {
parser.print_error();
println!("dot render error: {}", e);
}
}
}


#[derive(Debug)]
pub struct WindowContext {
pub event_loop: EventLoop<()>,
Expand Down
18 changes: 9 additions & 9 deletions src/graph/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,31 @@ use crate::graph::resource::{AttachmentType, ResourceUsage};
use crate::pipeline::PipelineStage;

/// The returned value from a pass callback function.
pub type PassFnResult<'q, D> = Result<IncompleteCommandBuffer<'q, D>>;
pub type PassFnResult<'q, D, A> = Result<IncompleteCommandBuffer<'q, D, A>>;

pub trait PassExecutor<D: ExecutionDomain, U, A: Allocator> {
fn execute<'q>(
&mut self,
cmd: IncompleteCommandBuffer<'q, D>,
cmd: IncompleteCommandBuffer<'q, D, A>,
ifc: &mut InFlightContext<A>,
bindings: &PhysicalResourceBindings,
user_data: &mut U,
) -> PassFnResult<'q, D>;
) -> PassFnResult<'q, D, A>;
}

impl<D, U, A, F> PassExecutor<D, U, A> for F
where
D: ExecutionDomain,
A: Allocator,
F: for<'q> FnMut(IncompleteCommandBuffer<'q, D>, &mut InFlightContext<A>, &PhysicalResourceBindings, &mut U) -> PassFnResult<'q, D>,
F: for<'q> FnMut(IncompleteCommandBuffer<'q, D, A>, &mut InFlightContext<A>, &PhysicalResourceBindings, &mut U) -> PassFnResult<'q, D, A>,
{
fn execute<'q>(
&mut self,
cmd: IncompleteCommandBuffer<'q, D>,
cmd: IncompleteCommandBuffer<'q, D, A>,
ifc: &mut InFlightContext<A>,
bindings: &PhysicalResourceBindings,
user_data: &mut U,
) -> PassFnResult<'q, D> {
) -> PassFnResult<'q, D, A> {
self.call_mut((cmd, ifc, bindings, user_data))
}
}
Expand All @@ -129,11 +129,11 @@ impl EmptyPassExecutor {
impl<D: ExecutionDomain, U, A: Allocator> PassExecutor<D, U, A> for EmptyPassExecutor {
fn execute<'q>(
&mut self,
cmd: IncompleteCommandBuffer<'q, D>,
cmd: IncompleteCommandBuffer<'q, D, A>,
_ifc: &mut InFlightContext<A>,
_bindings: &PhysicalResourceBindings,
_user_data: &mut U,
) -> PassFnResult<'q, D> {
) -> PassFnResult<'q, D, A> {
Ok(cmd)
}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ impl<'cb, D: ExecutionDomain, U, A: Allocator> PassBuilder<'cb, D, U, A> {

pub fn execute_fn<F>(mut self, exec: F) -> Self
where
F: for<'q> FnMut(IncompleteCommandBuffer<'q, D>, &mut InFlightContext<A>, &PhysicalResourceBindings, &mut U) -> PassFnResult<'q, D> + 'cb, {
F: for<'q> FnMut(IncompleteCommandBuffer<'q, D, A>, &mut InFlightContext<A>, &PhysicalResourceBindings, &mut U) -> PassFnResult<'q, D, A> + 'cb, {
self.inner.execute = Box::new(exec);
self
}
Expand Down
40 changes: 20 additions & 20 deletions src/graph/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub trait RecordGraphToCommandBuffer<D: ExecutionDomain, U, A: Allocator> {
/// - This function can error if a virtual resource used in the graph is lacking an physical binding.
fn record<'q>(
&mut self,
cmd: IncompleteCommandBuffer<'q, D>,
cmd: IncompleteCommandBuffer<'q, D, A>,
bindings: &PhysicalResourceBindings,
ifc: &mut InFlightContext<A>,
debug: Option<Arc<DebugMessenger>>,
user_data: &mut U,
) -> Result<IncompleteCommandBuffer<'q, D>>
) -> Result<IncompleteCommandBuffer<'q, D, A>>
where
Self: Sized;
}
Expand Down Expand Up @@ -194,8 +194,8 @@ fn render_area<D: ExecutionDomain, U, A: Allocator>(pass: &PassNode<PassResource
fn annotate_pass<'q, D: ExecutionDomain, U, A: Allocator>(
pass: &PassNode<PassResource, D, U, A>,
debug: &Arc<DebugMessenger>,
cmd: IncompleteCommandBuffer<'q, D>,
) -> Result<IncompleteCommandBuffer<'q, D>> {
cmd: IncompleteCommandBuffer<'q, D, A>,
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
let name = CString::new(pass.identifier.clone())?;
let label = vk::DebugUtilsLabelEXT {
s_type: vk::StructureType::DEBUG_UTILS_LABEL_EXT,
Expand All @@ -207,18 +207,18 @@ fn annotate_pass<'q, D: ExecutionDomain, U, A: Allocator>(
}

#[cfg(not(feature = "debug-markers"))]
fn annotate_pass<D: ExecutionDomain>(_: &PassNode<PassResource, D>, _: &Arc<DebugMessenger>, cmd: IncompleteCommandBuffer<D>) -> Result<IncompleteCommandBuffer<D>> {
fn annotate_pass<D: ExecutionDomain, A: Allocator>(_: &PassNode<PassResource, D>, _: &Arc<DebugMessenger>, cmd: IncompleteCommandBuffer<D, A>) -> Result<IncompleteCommandBuffer<D, A>> {
Ok(cmd)
}

fn record_pass<'q, D: ExecutionDomain, U, A: Allocator>(
pass: &mut PassNode<'_, PassResource, D, U, A>,
bindings: &PhysicalResourceBindings,
ifc: &mut InFlightContext<A>,
mut cmd: IncompleteCommandBuffer<'q, D>,
mut cmd: IncompleteCommandBuffer<'q, D, A>,
debug: Option<Arc<DebugMessenger>>,
user_data: &mut U,
) -> Result<IncompleteCommandBuffer<'q, D>> {
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
if let Some(debug) = debug.clone() {
cmd = annotate_pass(pass, &debug, cmd)?;
}
Expand Down Expand Up @@ -251,12 +251,12 @@ fn record_pass<'q, D: ExecutionDomain, U, A: Allocator>(
Ok(cmd)
}

fn record_image_barrier<'q, D: ExecutionDomain>(
fn record_image_barrier<'q, D: ExecutionDomain, A: Allocator>(
barrier: &PassResourceBarrier,
image: &ImageView,
dst_resource: &PassResource,
cmd: IncompleteCommandBuffer<'q, D>,
) -> Result<IncompleteCommandBuffer<'q, D>> {
cmd: IncompleteCommandBuffer<'q, D, A>,
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
// Image layouts:
// barrier.resource has information on srcLayout
// dst_resource(barrier) has information on dstLayout
Expand Down Expand Up @@ -291,12 +291,12 @@ fn record_image_barrier<'q, D: ExecutionDomain>(
Ok(cmd.pipeline_barrier_2(&dependency))
}

fn record_buffer_barrier<'q, D: ExecutionDomain>(
fn record_buffer_barrier<'q, D: ExecutionDomain, A: Allocator>(
barrier: &PassResourceBarrier,
_buffer: &BufferView,
_dst_resource: &PassResource,
cmd: IncompleteCommandBuffer<'q, D>,
) -> Result<IncompleteCommandBuffer<'q, D>> {
cmd: IncompleteCommandBuffer<'q, D, A>,
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
// Since every driver implements buffer barriers as global memory barriers, we will do the same.
let vk_barrier = vk::MemoryBarrier2 {
s_type: vk::StructureType::MEMORY_BARRIER_2,
Expand All @@ -322,12 +322,12 @@ fn record_buffer_barrier<'q, D: ExecutionDomain>(
Ok(cmd.pipeline_barrier_2(&dependency))
}

fn record_barrier<'q, D: ExecutionDomain>(
fn record_barrier<'q, D: ExecutionDomain, A: Allocator>(
barrier: &PassResourceBarrier,
dst_resource: &PassResource,
bindings: &PhysicalResourceBindings,
cmd: IncompleteCommandBuffer<'q, D>,
) -> Result<IncompleteCommandBuffer<'q, D>> {
cmd: IncompleteCommandBuffer<'q, D, A>,
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
let physical_resource = bindings.resolve(&barrier.resource.resource);
let Some(resource) = physical_resource else { return Err(anyhow::Error::from(Error::NoResourceBound(barrier.resource.uid().clone()))) };
match resource {
Expand All @@ -341,10 +341,10 @@ fn record_node<'q, D: ExecutionDomain, U, A: Allocator>(
node: NodeIndex,
bindings: &PhysicalResourceBindings,
ifc: &mut InFlightContext<A>,
cmd: IncompleteCommandBuffer<'q, D>,
cmd: IncompleteCommandBuffer<'q, D, A>,
debug: Option<Arc<DebugMessenger>>,
user_data: &mut U,
) -> Result<IncompleteCommandBuffer<'q, D>> {
) -> Result<IncompleteCommandBuffer<'q, D, A>> {
let graph = &mut graph.graph.graph;
let dst_resource_res = PassGraph::barrier_dst_resource(graph, node).cloned();
let weight = graph.node_weight_mut(node).unwrap();
Expand All @@ -363,12 +363,12 @@ fn record_node<'q, D: ExecutionDomain, U, A: Allocator>(
impl<'cb, D: ExecutionDomain, U, A: Allocator> RecordGraphToCommandBuffer<D, U, A> for BuiltPassGraph<'cb, D, U, A> {
fn record<'q>(
&mut self,
mut cmd: IncompleteCommandBuffer<'q, D>,
mut cmd: IncompleteCommandBuffer<'q, D, A>,
bindings: &PhysicalResourceBindings,
ifc: &mut InFlightContext<A>,
debug: Option<Arc<DebugMessenger>>,
user_data: &mut U,
) -> Result<IncompleteCommandBuffer<'q, D>>
) -> Result<IncompleteCommandBuffer<'q, D, A>>
where
Self: Sized, {
let mut active = HashSet::new();
Expand Down

0 comments on commit 3d0ae3d

Please sign in to comment.