Skip to content

Commit

Permalink
finish builder and utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoskey committed May 27, 2024
1 parent 2914aaf commit 08c2b68
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 48 deletions.
76 changes: 41 additions & 35 deletions crates/bevy_render_graph/src/std/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct BindGroupBuilder<'a, 'b: 'a, 'g: 'b> {

impl<'a, 'b: 'a, 'g: 'b> BindGroupBuilder<'a, 'b, 'g> {
pub fn new(
label: Label<'g>,
graph: &'a mut RenderGraphBuilder<'b, 'g>,
label: Label<'g>,
shader_stages: ShaderStages,
) -> Self {
Self {
Expand All @@ -47,6 +47,10 @@ impl<'a, 'b: 'a, 'g: 'b> BindGroupBuilder<'a, 'b, 'g> {
}
}

pub fn internal_graph(&mut self) -> &mut RenderGraphBuilder<'b, 'g> {
self.graph
}

pub fn set_shader_stages(&mut self, shader_stages: ShaderStages) -> &mut Self {
self.shader_stages = shader_stages;
self
Expand Down Expand Up @@ -220,17 +224,41 @@ impl<'a, 'b: 'a, 'g: 'b> BindGroupBuilder<'a, 'b, 'g> {
}

pub fn read_storage_buffer(&mut self, buffer: RenderHandle<'g, Buffer>) -> &mut Self {
self.graph.add_usages(buffer, BufferUsages::STORAGE);
self
self.storage_buffer(buffer, true)
}

pub fn write_storage_buffer(&mut self, buffer: RenderHandle<'g, Buffer>) -> &mut Self {
self.graph.add_usages(buffer, BufferUsages::STORAGE);
self
self.storage_buffer(buffer, false)
}

pub fn read_write_storage_buffer(&mut self, buffer: RenderHandle<'g, Buffer>) -> &mut Self {
self.storage_buffer(buffer, false)
}

pub fn storage_buffer(
&mut self,
buffer: RenderHandle<'g, Buffer>,
read_only: bool,
) -> &mut Self {
self.graph.add_usages(buffer, BufferUsages::STORAGE);
self.layout.push(BindGroupLayoutEntry {
binding: self.layout.len() as u32,
visibility: self.shader_stages,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
});
self.entries.push(RenderGraphBindGroupEntry {
binding: self.entries.len() as u32,
resource: RenderGraphBindingResource::Buffer(RenderGraphBufferBinding {
buffer,
offset: 0,
size: None,
}),
});
self
}

Expand All @@ -243,22 +271,6 @@ impl<'a, 'b: 'a, 'g: 'b> BindGroupBuilder<'a, 'b, 'g> {
});
bind_group
}

pub fn build_and_return_graph(
self,
) -> (
RenderHandle<'g, BindGroupLayout>,
RenderHandle<'g, BindGroup>,
&'a mut RenderGraphBuilder<'b, 'g>,
) {
let layout = self.graph.new_resource(self.layout);
let bind_group = self.graph.new_resource(RenderGraphBindGroupDescriptor {
label: self.label,
layout,
entries: self.entries,
});
(layout, bind_group, self.graph)
}
}

pub struct ComputePass<'a, 'b: 'a, 'g: 'b> {
Expand All @@ -272,15 +284,15 @@ pub struct ComputePass<'a, 'b: 'a, 'g: 'b> {

impl<'a, 'b: 'a, 'g: 'b> ComputePass<'a, 'b, 'g> {
pub fn new(
label: Label<'static>,
entry_point: Cow<'static, str>,
graph: &'a mut RenderGraphBuilder<'b, 'g>,
label: Label<'static>,
shader: Handle<Shader>,
entry_point: Cow<'static, str>,
) -> Self {
Self {
label: label.clone(),
entry_point,
bind_group: BindGroupBuilder::new(label, graph, ShaderStages::COMPUTE),
bind_group: BindGroupBuilder::new(graph, label, ShaderStages::COMPUTE),
shader,
dispatch_size: UVec3::ONE,
shader_defs: Vec::new(),
Expand All @@ -302,21 +314,15 @@ impl<'a, 'b: 'a, 'g: 'b> ComputePass<'a, 'b, 'g> {
self
}

//would infer texture format and dimension from metadata stored in graph.
pub fn write_storage_texture(&mut self, texture: RenderHandle<'g, TextureView>) -> &mut Self {
self.bind_group.write_storage_texture(texture);
self
}

// pub fn read_buffer(&mut self, buffer: RenderHandle<'g, Buffer>) -> &mut Self {
// self.bind_group.read_buffer(buffer);
// self
// }
//
// pub fn write_buffer(&mut self, buffer: RenderHandle<'g, Buffer>) -> &mut Self {
// self.bind_group.write_buffer(buffer);
// self
// }
pub fn define(&mut self, defines: &[ShaderDefVal]) -> &mut Self {
self.shader_defs.extend_from_slice(defines);
self
}

pub fn dispatch(&mut self, size: UVec3) -> &mut Self {
self.dispatch_size = size;
Expand All @@ -335,7 +341,7 @@ impl<'a, 'b: 'a, 'g: 'b> ComputePass<'a, 'b, 'g> {
self.dispatch(UVec3 { x, y, z })
}

pub fn build(self) {
pub fn build(&mut self) {
// let (layout, mut bind_group, graph) = self.bind_group.build_and_return_graph();
// let pipeline = graph.new_resource(RenderGraphComputePipelineDescriptor {
// label: self.label.clone(),
Expand Down
76 changes: 63 additions & 13 deletions crates/bevy_render_graph/src/std/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,60 @@ pub fn fullscreen_shader_vertex_state(graph: &RenderGraphBuilder) -> VertexState
pub fn fullscreen_pass<'g>(
graph: &mut RenderGraphBuilder<'_, 'g>,
shader: Handle<Shader>,
target: RenderHandle<'g, TextureView>,
mut target: RenderHandle<'g, TextureView>,
blend: Option<BlendState>,
bind_groups: &[RenderHandle<'g, BindGroup>],
) {
let format = graph
.meta(target)
.descriptor
.format
.unwrap_or_else(|| graph.meta(graph.meta(target).texture).format);
let pipeline = graph.new_resource(RenderGraphRenderPipelineDescriptor {
label: Some("blit_pipeline".into()),
layout: bind_groups
.iter()
.map(|bind_group| graph.meta(*bind_group).descriptor.layout)
.collect(),
push_constant_ranges: Vec::new(),
vertex: fullscreen_shader_vertex_state(graph),
primitive: Default::default(),
depth_stencil: Default::default(),
multisample: Default::default(),
fragment: Some(FragmentState {
shader,
shader_defs: Vec::new(),
entry_point: "blit_frag".into(),
targets: vec![Some(ColorTargetState {
format,
blend,
write_mask: ColorWrites::all(),
})],
}),
});

graph.add_node(
Some("blit_node".into()),
deps![&mut target],
move |ctx, cmds, _| {
let mut render_pass = cmds.begin_render_pass(&RenderPassDescriptor {
label: Some("blit_pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: ctx.get(target).deref(),
resolve_target: None,
ops: Operations {
load: LoadOp::Load,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
render_pass.set_pipeline(ctx.get(pipeline).deref());
render_pass.draw(0..3, 0..1);
},
);
}

pub mod blit {
Expand All @@ -75,47 +127,45 @@ pub mod blit {
RenderGraphBuilder,
},
deps,
std::BindGroupBuilder,
std::{BindGroupBuilder, SrcDst},
};

use super::fullscreen_shader_vertex_state;

pub fn one<'g>(
graph: &mut RenderGraphBuilder<'_, 'g>,
src: RenderHandle<'g, TextureView>,
dst: RenderHandle<'g, TextureView>,
src_dst: SrcDst<'g, TextureView>,
sampler: Option<RenderHandle<'g, Sampler>>,
blend: Option<BlendState>,
) {
let shader = graph
.world_resource::<AssetServer>()
.load("embedded://bevy_render_graph/std/blit.wgsl");
custom(graph, shader, src, dst, sampler, blend);
custom(graph, shader, src_dst, sampler, blend);
}

pub fn custom<'g>(
graph: &mut RenderGraphBuilder<'_, 'g>,
shader: Handle<Shader>,
src: RenderHandle<'g, TextureView>,
mut dst: RenderHandle<'g, TextureView>,
src_dst: SrcDst<'g, TextureView>,
sampler: Option<RenderHandle<'g, Sampler>>,
blend: Option<BlendState>,
) {
let sampler = sampler.unwrap_or_else(|| graph.new_resource(SamplerDescriptor::default()));
let bind_group = BindGroupBuilder::new(
Some("blit_bind_group".into()),
graph,
Some("blit_bind_group".into()),
ShaderStages::FRAGMENT,
)
.texture(src)
.texture(src_dst.src)
.sampler(sampler)
.build();

let format = graph
.meta(src)
.meta(src_dst.src)
.descriptor
.format
.unwrap_or_else(|| graph.meta(graph.meta(src).texture).format);
.unwrap_or_else(|| graph.meta(graph.meta(src_dst.src).texture).format);
let pipeline = graph.new_resource(RenderGraphRenderPipelineDescriptor {
label: Some("blit_pipeline".into()),
layout: vec![graph.meta(bind_group).descriptor.layout],
Expand All @@ -139,12 +189,12 @@ pub mod blit {

graph.add_node(
Some("blit_node".into()),
deps![&src, &mut dst],
deps![src_dst],
move |ctx, cmds, _| {
let mut render_pass = cmds.begin_render_pass(&RenderPassDescriptor {
label: Some("blit_pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: ctx.get(dst).deref(),
view: ctx.get(src_dst.dst).deref(),
resolve_target: None,
ops: Operations {
load: LoadOp::Load,
Expand Down

0 comments on commit 08c2b68

Please sign in to comment.