Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ui_render/src/pipeline.rs
9374 views
1
use bevy_asset::{load_embedded_asset, AssetServer, Handle};
2
use bevy_ecs::prelude::*;
3
use bevy_image::BevyDefault as _;
4
use bevy_mesh::VertexBufferLayout;
5
use bevy_render::{
6
render_resource::{
7
binding_types::{sampler, texture_2d, uniform_buffer},
8
*,
9
},
10
view::{ViewTarget, ViewUniform},
11
};
12
use bevy_shader::Shader;
13
use bevy_utils::default;
14
15
#[derive(Resource)]
16
pub struct UiPipeline {
17
pub view_layout: BindGroupLayoutDescriptor,
18
pub image_layout: BindGroupLayoutDescriptor,
19
pub shader: Handle<Shader>,
20
}
21
22
pub fn init_ui_pipeline(mut commands: Commands, asset_server: Res<AssetServer>) {
23
let view_layout = BindGroupLayoutDescriptor::new(
24
"ui_view_layout",
25
&BindGroupLayoutEntries::single(
26
ShaderStages::VERTEX_FRAGMENT,
27
uniform_buffer::<ViewUniform>(true),
28
),
29
);
30
31
let image_layout = BindGroupLayoutDescriptor::new(
32
"ui_image_layout",
33
&BindGroupLayoutEntries::sequential(
34
ShaderStages::FRAGMENT,
35
(
36
texture_2d(TextureSampleType::Float { filterable: true }),
37
sampler(SamplerBindingType::Filtering),
38
),
39
),
40
);
41
42
commands.insert_resource(UiPipeline {
43
view_layout,
44
image_layout,
45
shader: load_embedded_asset!(asset_server.as_ref(), "ui.wgsl"),
46
});
47
}
48
49
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
50
pub struct UiPipelineKey {
51
pub hdr: bool,
52
pub anti_alias: bool,
53
}
54
55
impl SpecializedRenderPipeline for UiPipeline {
56
type Key = UiPipelineKey;
57
58
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
59
let vertex_layout = VertexBufferLayout::from_vertex_formats(
60
VertexStepMode::Vertex,
61
vec![
62
// position
63
VertexFormat::Float32x3,
64
// uv
65
VertexFormat::Float32x2,
66
// color
67
VertexFormat::Float32x4,
68
// mode
69
VertexFormat::Uint32,
70
// border radius
71
VertexFormat::Float32x4,
72
// border thickness
73
VertexFormat::Float32x4,
74
// border size
75
VertexFormat::Float32x2,
76
// position relative to the center
77
VertexFormat::Float32x2,
78
],
79
);
80
let shader_defs = if key.anti_alias {
81
vec!["ANTI_ALIAS".into()]
82
} else {
83
Vec::new()
84
};
85
86
RenderPipelineDescriptor {
87
vertex: VertexState {
88
shader: self.shader.clone(),
89
shader_defs: shader_defs.clone(),
90
buffers: vec![vertex_layout],
91
..default()
92
},
93
fragment: Some(FragmentState {
94
shader: self.shader.clone(),
95
shader_defs,
96
targets: vec![Some(ColorTargetState {
97
format: if key.hdr {
98
ViewTarget::TEXTURE_FORMAT_HDR
99
} else {
100
TextureFormat::bevy_default()
101
},
102
blend: Some(BlendState::ALPHA_BLENDING),
103
write_mask: ColorWrites::ALL,
104
})],
105
..default()
106
}),
107
layout: vec![self.view_layout.clone(), self.image_layout.clone()],
108
label: Some("ui_pipeline".into()),
109
..default()
110
}
111
}
112
}
113
114