bevy_pixel_art_shader 0.2.3

Pixel art material extension for Bevy — toon shading, CIELAB palette quantization, and Bayer dithering on top of PBR lighting.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use bevy::{
    asset::{embedded_asset, load_embedded_asset},
    core_pipeline::{
        FullscreenShader,
        core_3d::{
            DEPTH_TEXTURE_SAMPLING_SUPPORTED,
            graph::{Core3d, Node3d},
        },
        prepass::{DepthPrepass, ViewPrepassTextures},
    },
    ecs::query::QueryState,
    prelude::*,
    render::{
        Extract, Render, RenderApp, RenderSystems,
        extract_component::{
            ComponentUniforms, DynamicUniformIndex, ExtractComponent, ExtractComponentPlugin,
            UniformComponentPlugin,
        },
        render_asset::RenderAssets,
        render_graph::{
            Node, NodeRunError, RenderGraphContext, RenderGraphExt, RenderLabel,
        },
        render_resource::{
            binding_types::{sampler, texture_2d, texture_depth_2d, uniform_buffer},
            *,
        },
        renderer::{RenderContext, RenderDevice},
        sync_world::RenderEntity,
        texture::GpuImage,
        view::ViewTarget,
    },
};

// ──────────────────────────────────────────────
//  Public components
// ──────────────────────────────────────────────

/// Marker for the low-res pixel art camera. Synced to the render world
/// so the compositor node can query its prepass textures.
#[derive(Component, Clone, Default, Reflect)]
#[reflect(Component)]
pub struct LowResPixelArtCamera;

/// Attach to the full-res camera to enable depth-aware compositing.
/// Automatically requires `DepthPrepass` on the same entity.
#[derive(Component, Clone, Reflect)]
#[reflect(Component)]
#[require(DepthPrepass)]
pub struct PixelArtCompositor {
    pub lowres_image: Handle<Image>,
    /// Depth bias for the lowres vs fullres comparison.
    /// Compensates for precision mismatch between the two depth buffers.
    pub depth_bias: f32,
}

// ──────────────────────────────────────────────
//  GPU uniform
// ──────────────────────────────────────────────

#[derive(Component, Clone, Copy, ShaderType)]
pub struct CompositorUniform {
    pub depth_bias: f32,
}

impl ExtractComponent for CompositorUniform {
    type QueryData = &'static PixelArtCompositor;
    type QueryFilter = ();
    type Out = Self;

    fn extract_component(
        compositor: bevy::ecs::query::QueryItem<'_, '_, Self::QueryData>,
    ) -> Option<Self::Out> {
        Some(CompositorUniform {
            depth_bias: compositor.depth_bias,
        })
    }
}

// ──────────────────────────────────────────────
//  Plugin
// ──────────────────────────────────────────────

pub struct PixelArtCompositorPlugin;

impl Plugin for PixelArtCompositorPlugin {
    fn build(&self, app: &mut App) {
        embedded_asset!(app, "compositor.wgsl");

        app.register_type::<PixelArtCompositor>();
        app.register_type::<LowResPixelArtCamera>();
        app.add_plugins((
            ExtractComponentPlugin::<CompositorUniform>::default(),
            UniformComponentPlugin::<CompositorUniform>::default(),
        ));

        let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
            return;
        };

        render_app
            .init_resource::<SpecializedRenderPipelines<CompositorPipeline>>()
            .add_systems(ExtractSchedule, extract_compositor)
            .add_systems(
                Render,
                prepare_compositor_pipelines.in_set(RenderSystems::Prepare),
            )
            .add_render_graph_node::<CompositorNode>(Core3d, CompositorLabel)
            .add_render_graph_edges(
                Core3d,
                (Node3d::PostProcessing, CompositorLabel, Node3d::Fxaa),
            );
    }

    fn finish(&self, app: &mut App) {
        app.sub_app_mut(RenderApp)
            .init_resource::<CompositorPipeline>();
    }
}

// ──────────────────────────────────────────────
//  Render-world types
// ──────────────────────────────────────────────

/// Extracted each frame from `PixelArtCompositor`.
#[derive(Component, Clone)]
pub struct ExtractedCompositor {
    pub lowres_image: Handle<Image>,
}

/// Per-view cached pipeline id.
#[derive(Component, Clone, Copy)]
pub struct CompositorPipelineId(CachedRenderPipelineId);

#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)]
pub struct CompositorLabel;

// ──────────────────────────────────────────────
//  Pipeline resource
// ──────────────────────────────────────────────

#[derive(Resource)]
pub struct CompositorPipeline {
    pub shader: Handle<Shader>,
    pub nearest_sampler: Sampler,
    pub layout: BindGroupLayoutDescriptor,
    pub fullscreen_shader: FullscreenShader,
}

impl FromWorld for CompositorPipeline {
    fn from_world(world: &mut World) -> Self {
        let shader = load_embedded_asset!(world, "compositor.wgsl");

        let layout = BindGroupLayoutDescriptor::new(
            "pixel_art_compositor: bind_group_layout",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::FRAGMENT,
                (
                    // 0: fullres color
                    texture_2d(TextureSampleType::Float { filterable: true }),
                    // 1: fullres depth
                    texture_depth_2d(),
                    // 2: lowres color
                    texture_2d(TextureSampleType::Float { filterable: true }),
                    // 3: lowres depth
                    texture_depth_2d(),
                    // 4: nearest sampler
                    sampler(SamplerBindingType::NonFiltering),
                    // 5: compositor uniform
                    uniform_buffer::<CompositorUniform>(true),
                ),
            ),
        );

        let render_device = world.resource::<RenderDevice>();
        let nearest_sampler = render_device.create_sampler(&SamplerDescriptor {
            label: Some("pixel_art_compositor nearest sampler"),
            mag_filter: FilterMode::Nearest,
            min_filter: FilterMode::Nearest,
            ..default()
        });

        Self {
            shader,
            nearest_sampler,
            layout,
            fullscreen_shader: world.resource::<FullscreenShader>().clone(),
        }
    }
}

// ──────────────────────────────────────────────
//  Specialization
// ──────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct CompositorKey {
    pub hdr: bool,
}

impl SpecializedRenderPipeline for CompositorPipeline {
    type Key = CompositorKey;

    fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
        let format = if key.hdr {
            ViewTarget::TEXTURE_FORMAT_HDR
        } else {
            TextureFormat::bevy_default()
        };

        RenderPipelineDescriptor {
            label: Some("pixel_art_compositor: pipeline".into()),
            layout: vec![self.layout.clone()],
            vertex: self.fullscreen_shader.to_vertex_state(),
            fragment: Some(FragmentState {
                shader: self.shader.clone(),
                shader_defs: vec![],
                entry_point: Some("fragment".into()),
                targets: vec![Some(ColorTargetState {
                    format,
                    blend: None,
                    write_mask: ColorWrites::ALL,
                })],
            }),
            primitive: default(),
            depth_stencil: None,
            multisample: default(),
            push_constant_ranges: vec![],
            zero_initialize_workgroup_memory: false,
        }
    }
}

// ──────────────────────────────────────────────
//  Extract system
// ──────────────────────────────────────────────

pub fn extract_compositor(
    mut commands: Commands,
    compositor_query: Extract<Query<(RenderEntity, &PixelArtCompositor)>>,
    lowres_query: Extract<Query<RenderEntity, With<LowResPixelArtCamera>>>,
) {
    if !DEPTH_TEXTURE_SAMPLING_SUPPORTED {
        info_once!(
            "Disable pixel art compositor on this platform because depth textures aren't supported"
        );
        return;
    }

    for (entity, compositor) in compositor_query.iter() {
        commands
            .get_entity(entity)
            .expect("Compositor entity wasn't synced.")
            .insert(ExtractedCompositor {
                lowres_image: compositor.lowres_image.clone(),
            });
    }

    for entity in lowres_query.iter() {
        commands
            .get_entity(entity)
            .expect("LowRes camera entity wasn't synced.")
            .insert(LowResPixelArtCamera);
    }
}

// ──────────────────────────────────────────────
//  Prepare system
// ──────────────────────────────────────────────

pub fn prepare_compositor_pipelines(
    mut commands: Commands,
    pipeline_cache: Res<PipelineCache>,
    mut pipelines: ResMut<SpecializedRenderPipelines<CompositorPipeline>>,
    compositor_pipeline: Res<CompositorPipeline>,
    query: Query<(Entity, &ViewTarget), With<ExtractedCompositor>>,
) {
    for (entity, view_target) in &query {
        let hdr = view_target.is_hdr();
        let id = pipelines.specialize(
            &pipeline_cache,
            &compositor_pipeline,
            CompositorKey { hdr },
        );
        commands.entity(entity).insert(CompositorPipelineId(id));
    }
}

// ──────────────────────────────────────────────
//  Render node
// ──────────────────────────────────────────────

pub struct CompositorNode {
    view_query: QueryState<(
        &'static ViewTarget,
        &'static ViewPrepassTextures,
        &'static ExtractedCompositor,
        &'static CompositorPipelineId,
        &'static DynamicUniformIndex<CompositorUniform>,
    )>,
    lowres_query: QueryState<&'static ViewPrepassTextures, With<LowResPixelArtCamera>>,
}

impl FromWorld for CompositorNode {
    fn from_world(world: &mut World) -> Self {
        Self {
            view_query: QueryState::new(world),
            lowres_query: QueryState::new(world),
        }
    }
}

impl Node for CompositorNode {
    fn update(&mut self, world: &mut World) {
        self.view_query.update_archetypes(world);
        self.lowres_query.update_archetypes(world);
    }

    fn run(
        &self,
        graph: &mut RenderGraphContext,
        render_context: &mut RenderContext,
        world: &World,
    ) -> Result<(), NodeRunError> {
        let view_entity = graph.view_entity();

        let Ok((view_target, fullres_prepass, extracted, pipeline_id, uniform_index)) =
            self.view_query.get_manual(world, view_entity)
        else {
            return Ok(());
        };

        let compositor_pipeline = world.resource::<CompositorPipeline>();
        let pipeline_cache = world.resource::<PipelineCache>();

        let Some(pipeline) = pipeline_cache.get_render_pipeline(pipeline_id.0) else {
            return Ok(());
        };

        // Full-res depth
        let Some(fullres_depth) = &fullres_prepass.depth else {
            return Ok(());
        };

        // Low-res camera prepass textures
        let Some(lowres_prepass) = self.lowres_query.iter_manual(world).next() else {
            return Ok(());
        };
        let Some(lowres_depth) = &lowres_prepass.depth else {
            return Ok(());
        };

        // Low-res color image (the render-to-texture target)
        let Some(lowres_image) = world
            .resource::<RenderAssets<GpuImage>>()
            .get(&extracted.lowres_image)
        else {
            return Ok(());
        };

        // Compositor uniform buffer
        let Some(uniform_binding) = world
            .resource::<ComponentUniforms<CompositorUniform>>()
            .uniforms()
            .binding()
        else {
            return Ok(());
        };

        let post_process = view_target.post_process_write();

        let bind_group = render_context.render_device().create_bind_group(
            "pixel_art_compositor_bind_group",
            &pipeline_cache
                .get_bind_group_layout(&compositor_pipeline.layout),
            &BindGroupEntries::sequential((
                // 0: fullres color (current camera output)
                post_process.source,
                // 1: fullres depth
                &fullres_depth.texture.default_view,
                // 2: lowres color (pixel art render target)
                &lowres_image.texture_view,
                // 3: lowres depth
                &lowres_depth.texture.default_view,
                // 4: nearest sampler
                &compositor_pipeline.nearest_sampler,
                // 5: compositor uniform
                uniform_binding,
            )),
        );

        let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
            label: Some("pixel_art_compositor_pass"),
            color_attachments: &[Some(RenderPassColorAttachment {
                view: post_process.destination,
                depth_slice: None,
                resolve_target: None,
                ops: Operations::default(),
            })],
            depth_stencil_attachment: None,
            timestamp_writes: None,
            occlusion_query_set: None,
        });

        render_pass.set_render_pipeline(pipeline);
        render_pass.set_bind_group(0, &bind_group, &[uniform_index.index()]);
        render_pass.draw(0..3, 0..1);

        Ok(())
    }
}