use crate::effect_pass::EffectPass;
use crate::gpu::GpuContext;
use crate::hot_shader::HotEffectPass;
use crate::render_graph::{RenderContext, RenderNode};
pub struct EffectNode {
pub effect: EffectPass,
pub clear_color: Option<wgpu::Color>,
}
impl EffectNode {
pub fn new(effect: EffectPass) -> Self {
Self {
effect,
clear_color: Some(wgpu::Color::BLACK),
}
}
pub fn with_clear(mut self, color: wgpu::Color) -> Self {
self.clear_color = Some(color);
self
}
pub fn no_clear(mut self) -> Self {
self.clear_color = None;
self
}
}
impl RenderNode for EffectNode {
fn execute(
&self,
ctx: &mut RenderContext,
target: &wgpu::TextureView,
_input: Option<&wgpu::TextureView>,
) {
let load_op = match self.clear_color {
Some(color) => wgpu::LoadOp::Clear(color),
None => wgpu::LoadOp::Load,
};
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: load_op,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
if self.effect.uses_camera() {
self.effect
.render_with_camera(ctx.gpu, &mut render_pass, ctx.time, ctx.camera);
} else {
self.effect.render(ctx.gpu, &mut render_pass, ctx.time);
}
}
}
pub struct HotEffectNode {
pub effect: HotEffectPass,
pub clear_color: Option<wgpu::Color>,
}
impl HotEffectNode {
pub fn new(effect: HotEffectPass) -> Self {
Self {
effect,
clear_color: Some(wgpu::Color::BLACK),
}
}
pub fn with_clear(mut self, color: wgpu::Color) -> Self {
self.clear_color = Some(color);
self
}
pub fn no_clear(mut self) -> Self {
self.clear_color = None;
self
}
pub fn check_reload(&mut self, gpu: &GpuContext) {
self.effect.check_reload(gpu);
}
}
impl RenderNode for HotEffectNode {
fn execute(
&self,
ctx: &mut RenderContext,
target: &wgpu::TextureView,
_input: Option<&wgpu::TextureView>,
) {
let load_op = match self.clear_color {
Some(color) => wgpu::LoadOp::Clear(color),
None => wgpu::LoadOp::Load,
};
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: load_op,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
if self.effect.uses_camera() {
self.effect
.render_with_camera(ctx.gpu, &mut render_pass, ctx.time, ctx.camera);
} else {
self.effect.render(ctx.gpu, &mut render_pass, ctx.time);
}
}
fn check_hot_reload(&mut self, gpu: &GpuContext) {
self.effect.check_reload(gpu);
}
}