forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_constants.rs
More file actions
98 lines (85 loc) · 2.96 KB
/
Copy pathpipeline_constants.rs
File metadata and controls
98 lines (85 loc) · 2.96 KB
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
//! Demonstrates pipeline-overridable constants using posterization.
//!
//! The same shader is compiled into three distinct pipeline variants by setting the `LEVELS`
//! `override` constant to different values at pipeline creation time. Each quad on screen uses a
//! different variant, producing 2, 4, and 8 discrete color steps from the same smooth gradient.
//!
//! This is similar to `shader_defs` but operates at the GPU compiler level: constants are
//! substituted into the shader source before compilation, allowing the driver to optimize each
//! variant independently. Unlike uniforms, pipeline constants cannot change at draw time.
use bevy::{
mesh::MeshVertexBufferLayoutRef,
prelude::*,
reflect::TypePath,
render::render_resource::{
AsBindGroup, RenderPipelineDescriptor, SpecializedMeshPipelineError,
},
shader::ShaderRef,
sprite_render::{Material2d, Material2dKey, Material2dPlugin},
};
const SHADER_ASSET_PATH: &str = "shaders/pipeline_constants.wgsl";
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
Material2dPlugin::<PosterizeMaterial>::default(),
))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<PosterizeMaterial>>,
) {
commands.spawn(Camera2d);
let quad = meshes.add(Rectangle::new(200.0, 150.0));
for (i, levels) in [2u32, 4, 8].into_iter().enumerate() {
let x = (i as f32 - 1.0) * 220.0;
commands.spawn((
Mesh2d(quad.clone()),
MeshMaterial2d(materials.add(PosterizeMaterial { levels })),
Transform::from_xyz(x, 20.0, 0.0),
));
commands.spawn((
Text2d::new(format!("{levels} levels")),
Transform::from_xyz(x, -65.0, 0.0),
));
}
}
/// A material that posterizes a color gradient using a pipeline-overridable constant.
///
/// Each distinct `levels` value produces a separate compiled pipeline variant.
#[derive(Asset, TypePath, AsBindGroup, Clone)]
#[bind_group_data(PosterizeMaterialKey)]
struct PosterizeMaterial {
/// Number of discrete color steps. Compiled into the shader as a pipeline constant.
levels: u32,
}
#[derive(Clone, PartialEq, Eq, Hash)]
struct PosterizeMaterialKey {
levels: u32,
}
impl From<&PosterizeMaterial> for PosterizeMaterialKey {
fn from(m: &PosterizeMaterial) -> Self {
Self { levels: m.levels }
}
}
impl Material2d for PosterizeMaterial {
fn fragment_shader() -> ShaderRef {
SHADER_ASSET_PATH.into()
}
fn specialize(
descriptor: &mut RenderPipelineDescriptor,
_layout: &MeshVertexBufferLayoutRef,
key: Material2dKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
descriptor
.fragment
.as_mut()
.unwrap()
.constants
.push(("LEVELS".into(), key.bind_group_data.levels as f64));
Ok(())
}
}