-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not strictly necessary, but I do like having an explicit render graph, especially if it's something someone else ever wanted to tweak.
- Loading branch information
Showing
8 changed files
with
177 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod extract; | ||
pub mod lighting; | ||
pub mod sdf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mod node; | ||
mod pipeline; | ||
|
||
use bevy::{ | ||
asset::Handle, | ||
render::{render_graph::RenderLabel, render_resource::Shader}, | ||
}; | ||
|
||
pub use node::SdfNode; | ||
pub use pipeline::SdfPipeline; | ||
|
||
pub const SDF_SHADER: Handle<Shader> = | ||
Handle::weak_from_u128(231804371047309214783091483091843019281); | ||
|
||
#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)] | ||
pub struct SdfPass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use bevy::ecs::system::lifetimeless::Read; | ||
use bevy::prelude::*; | ||
use bevy::render::render_graph::ViewNode; | ||
|
||
use bevy::render::render_resource::{ | ||
BindGroupEntries, GpuArrayBuffer, Operations, PipelineCache, RenderPassColorAttachment, | ||
RenderPassDescriptor, | ||
}; | ||
use bevy::render::renderer::RenderDevice; | ||
use bevy::render::view::{ViewUniformOffset, ViewUniforms}; | ||
use smallvec::{smallvec, SmallVec}; | ||
|
||
use crate::render::extract::ExtractedLightOccluder2d; | ||
|
||
use crate::render::lighting::Lighting2dAuxiliaryTextures; | ||
|
||
use super::pipeline::SdfPipeline; | ||
|
||
const SDF_PASS: &str = "sdf_pass"; | ||
const SDF_BIND_GROUP: &str = "sdf_bind_group"; | ||
|
||
#[derive(Default)] | ||
pub struct SdfNode; | ||
|
||
impl ViewNode for SdfNode { | ||
type ViewQuery = (Read<ViewUniformOffset>, Read<Lighting2dAuxiliaryTextures>); | ||
|
||
fn run<'w>( | ||
&self, | ||
_graph: &mut bevy::render::render_graph::RenderGraphContext, | ||
render_context: &mut bevy::render::renderer::RenderContext<'w>, | ||
(view_offset, aux_textures): bevy::ecs::query::QueryItem<'w, Self::ViewQuery>, | ||
world: &'w World, | ||
) -> Result<(), bevy::render::render_graph::NodeRunError> { | ||
let sdf_pipeline = world.resource::<SdfPipeline>(); | ||
let pipeline_cache = world.resource::<PipelineCache>(); | ||
|
||
let (Some(pipeline), Some(view_uniform_binding), Some(light_occluders_binding)) = ( | ||
pipeline_cache.get_render_pipeline(sdf_pipeline.pipeline_id), | ||
world.resource::<ViewUniforms>().uniforms.binding(), | ||
world | ||
.resource::<GpuArrayBuffer<ExtractedLightOccluder2d>>() | ||
.binding(), | ||
) else { | ||
return Ok(()); | ||
}; | ||
|
||
let bind_group = render_context.render_device().create_bind_group( | ||
SDF_BIND_GROUP, | ||
&sdf_pipeline.layout, | ||
&BindGroupEntries::sequential((view_uniform_binding.clone(), light_occluders_binding)), | ||
); | ||
|
||
let mut sdf_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor { | ||
label: Some(SDF_PASS), | ||
color_attachments: &[Some(RenderPassColorAttachment { | ||
view: &aux_textures.sdf.default_view, | ||
resolve_target: None, | ||
ops: Operations::default(), | ||
})], | ||
..default() | ||
}); | ||
|
||
let mut dynamic_offsets: SmallVec<[u32; 3]> = smallvec![view_offset.offset]; | ||
|
||
// Storage buffers aren't available in WebGL2. We fall back to a | ||
// dynamic uniform buffer, and therefore need to provide the offset. | ||
// We're providing a value of 0 here as we're limiting the number of | ||
// point lights to only those that can reasonably fit in a single binding. | ||
if world | ||
.resource::<RenderDevice>() | ||
.limits() | ||
.max_storage_buffers_per_shader_stage | ||
== 0 | ||
{ | ||
dynamic_offsets.push(0); | ||
} | ||
|
||
sdf_pass.set_render_pipeline(pipeline); | ||
sdf_pass.set_bind_group(0, &bind_group, &dynamic_offsets); | ||
sdf_pass.draw(0..3, 0..1); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use bevy::core_pipeline::fullscreen_vertex_shader::fullscreen_shader_vertex_state; | ||
use bevy::prelude::*; | ||
use bevy::render::render_resource::binding_types::uniform_buffer; | ||
use bevy::render::render_resource::{ | ||
BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId, ColorTargetState, ColorWrites, | ||
FragmentState, GpuArrayBuffer, MultisampleState, PipelineCache, PrimitiveState, | ||
RenderPipelineDescriptor, ShaderStages, TextureFormat, | ||
}; | ||
use bevy::render::renderer::RenderDevice; | ||
use bevy::render::view::ViewUniform; | ||
|
||
use crate::render::extract::ExtractedLightOccluder2d; | ||
|
||
use super::SDF_SHADER; | ||
|
||
const SDF_PIPELINE: &str = "sdf_pipeline"; | ||
const SDF_BIND_GROUP_LAYOUT: &str = "sdf_bind_group_layout"; | ||
|
||
#[derive(Resource)] | ||
pub struct SdfPipeline { | ||
pub layout: BindGroupLayout, | ||
pub pipeline_id: CachedRenderPipelineId, | ||
} | ||
|
||
impl FromWorld for SdfPipeline { | ||
fn from_world(world: &mut World) -> Self { | ||
let render_device = world.resource::<RenderDevice>(); | ||
let pipeline_cache = world.resource::<PipelineCache>(); | ||
|
||
let layout = render_device.create_bind_group_layout( | ||
SDF_BIND_GROUP_LAYOUT, | ||
&BindGroupLayoutEntries::sequential( | ||
ShaderStages::FRAGMENT, | ||
( | ||
uniform_buffer::<ViewUniform>(true), | ||
GpuArrayBuffer::<ExtractedLightOccluder2d>::binding_layout(render_device), | ||
), | ||
), | ||
); | ||
|
||
let pipeline_id = pipeline_cache.queue_render_pipeline(RenderPipelineDescriptor { | ||
label: Some(SDF_PIPELINE.into()), | ||
layout: vec![layout.clone()], | ||
vertex: fullscreen_shader_vertex_state(), | ||
fragment: Some(FragmentState { | ||
shader: SDF_SHADER, | ||
shader_defs: vec![], | ||
entry_point: "fragment".into(), | ||
targets: vec![Some(ColorTargetState { | ||
format: TextureFormat::Rgba16Float, | ||
blend: None, | ||
write_mask: ColorWrites::ALL, | ||
})], | ||
}), | ||
primitive: PrimitiveState::default(), | ||
depth_stencil: None, | ||
multisample: MultisampleState::default(), | ||
push_constant_ranges: vec![], | ||
}); | ||
|
||
Self { | ||
layout, | ||
pipeline_id, | ||
} | ||
} | ||
} |