From f98b27592b2482dc89adc055073a3ee015f2424e Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Wed, 24 Apr 2024 14:03:15 -0400 Subject: Initial Commit --- src/resources.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/resources.rs (limited to 'src/resources.rs') diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..d900f18 --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,79 @@ +use bevy::{ + core_pipeline::fullscreen_vertex_shader::fullscreen_shader_vertex_state, + prelude::*, + render::{ + render_resource::{ + binding_types::{sampler, texture_2d}, + BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId, ColorTargetState, + ColorWrites, FragmentState, MultisampleState, PipelineCache, PrimitiveState, + RenderPipelineDescriptor, Sampler, SamplerBindingType, SamplerDescriptor, ShaderStages, + TextureFormat, TextureSampleType, + }, + renderer::RenderDevice, + texture::BevyDefault, + }, +}; + +#[derive(Resource)] +pub struct DitherPostProcessPipeline { + pub layout: BindGroupLayout, + pub screen_sampler: Sampler, + pub threshold_map_sampler: Sampler, + pub pipeline_id: CachedRenderPipelineId, +} + +impl FromWorld for DitherPostProcessPipeline { + fn from_world(world: &mut World) -> Self { + let render_device = world.resource::(); + + let layout = render_device.create_bind_group_layout( + "dither_post_process_bind_group_layout", + &BindGroupLayoutEntries::sequential( + ShaderStages::FRAGMENT, + ( + texture_2d(TextureSampleType::Float { filterable: true }), + sampler(SamplerBindingType::Filtering), + texture_2d(TextureSampleType::Float { filterable: true }), + sampler(SamplerBindingType::Filtering), + ), + ), + ); + + let screen_sampler = render_device.create_sampler(&SamplerDescriptor::default()); + let threshold_map_sampler = render_device.create_sampler(&SamplerDescriptor::default()); + + let shader = world.resource::().load::( + "embedded://grex_dither_post_process/../assets/shaders/dither_post_process.wgsl", + ); + + let pipeline_id = + world + .resource_mut::() + .queue_render_pipeline(RenderPipelineDescriptor { + label: Some("dither_post_process_render_pipeline".into()), + layout: vec![layout.clone()], + push_constant_ranges: vec![], + vertex: fullscreen_shader_vertex_state(), + primitive: PrimitiveState::default(), + depth_stencil: None, + multisample: MultisampleState::default(), + fragment: Some(FragmentState { + shader, + shader_defs: vec![], + entry_point: "fragment".into(), + targets: vec![Some(ColorTargetState { + format: TextureFormat::bevy_default(), + blend: None, + write_mask: ColorWrites::ALL, + })], + }), + }); + + Self { + layout, + screen_sampler, + threshold_map_sampler, + pipeline_id, + } + } +} -- cgit v1.2.3