2021-12-10 22:21:23 +00:00
|
|
|
use bevy_ecs::prelude::*;
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_render::{
|
2021-12-10 22:21:23 +00:00
|
|
|
render_resource::*, renderer::RenderDevice, texture::BevyDefault, view::ViewUniform,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crevice::std140::AsStd140;
|
|
|
|
|
|
|
|
pub struct UiPipeline {
|
|
|
|
pub view_layout: BindGroupLayout,
|
|
|
|
pub image_layout: BindGroupLayout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for UiPipeline {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
let world = world.cell();
|
|
|
|
let render_device = world.get_resource::<RenderDevice>().unwrap();
|
|
|
|
|
|
|
|
let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
|
|
|
|
entries: &[BindGroupLayoutEntry {
|
|
|
|
binding: 0,
|
|
|
|
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
|
|
|
|
ty: BindingType::Buffer {
|
|
|
|
ty: BufferBindingType::Uniform,
|
|
|
|
has_dynamic_offset: true,
|
|
|
|
min_binding_size: BufferSize::new(ViewUniform::std140_size_static() as u64),
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
}],
|
|
|
|
label: Some("ui_view_layout"),
|
|
|
|
});
|
|
|
|
|
|
|
|
let image_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
|
|
|
|
entries: &[
|
|
|
|
BindGroupLayoutEntry {
|
|
|
|
binding: 0,
|
|
|
|
visibility: ShaderStages::FRAGMENT,
|
|
|
|
ty: BindingType::Texture {
|
|
|
|
multisampled: false,
|
|
|
|
sample_type: TextureSampleType::Float { filterable: true },
|
|
|
|
view_dimension: TextureViewDimension::D2,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
BindGroupLayoutEntry {
|
|
|
|
binding: 1,
|
|
|
|
visibility: ShaderStages::FRAGMENT,
|
|
|
|
ty: BindingType::Sampler {
|
|
|
|
comparison: false,
|
|
|
|
filtering: true,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
label: Some("ui_image_layout"),
|
|
|
|
});
|
|
|
|
|
|
|
|
UiPipeline {
|
|
|
|
view_layout,
|
|
|
|
image_layout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
|
|
|
pub struct UiPipelineKey {}
|
|
|
|
|
|
|
|
impl SpecializedPipeline for UiPipeline {
|
|
|
|
type Key = UiPipelineKey;
|
|
|
|
/// FIXME: there are no specialization for now, should this be removed?
|
|
|
|
fn specialize(&self, _key: Self::Key) -> RenderPipelineDescriptor {
|
|
|
|
let vertex_buffer_layout = VertexBufferLayout {
|
|
|
|
array_stride: 24,
|
|
|
|
step_mode: VertexStepMode::Vertex,
|
|
|
|
attributes: vec![
|
|
|
|
// Position
|
|
|
|
VertexAttribute {
|
|
|
|
format: VertexFormat::Float32x3,
|
|
|
|
offset: 0,
|
|
|
|
shader_location: 0,
|
|
|
|
},
|
|
|
|
// UV
|
|
|
|
VertexAttribute {
|
|
|
|
format: VertexFormat::Float32x2,
|
|
|
|
offset: 12,
|
|
|
|
shader_location: 1,
|
|
|
|
},
|
|
|
|
VertexAttribute {
|
|
|
|
format: VertexFormat::Uint32,
|
|
|
|
offset: 20,
|
|
|
|
shader_location: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
let shader_defs = Vec::new();
|
|
|
|
|
|
|
|
RenderPipelineDescriptor {
|
|
|
|
vertex: VertexState {
|
|
|
|
shader: super::UI_SHADER_HANDLE.typed::<Shader>(),
|
|
|
|
entry_point: "vertex".into(),
|
|
|
|
shader_defs: shader_defs.clone(),
|
|
|
|
buffers: vec![vertex_buffer_layout],
|
|
|
|
},
|
|
|
|
fragment: Some(FragmentState {
|
|
|
|
shader: super::UI_SHADER_HANDLE.typed::<Shader>(),
|
|
|
|
shader_defs,
|
|
|
|
entry_point: "fragment".into(),
|
|
|
|
targets: vec![ColorTargetState {
|
|
|
|
format: TextureFormat::bevy_default(),
|
|
|
|
blend: Some(BlendState::ALPHA_BLENDING),
|
|
|
|
write_mask: ColorWrites::ALL,
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
layout: Some(vec![self.view_layout.clone(), self.image_layout.clone()]),
|
|
|
|
primitive: PrimitiveState {
|
|
|
|
front_face: FrontFace::Ccw,
|
|
|
|
cull_mode: None,
|
|
|
|
polygon_mode: PolygonMode::Fill,
|
|
|
|
clamp_depth: false,
|
|
|
|
conservative: false,
|
|
|
|
topology: PrimitiveTopology::TriangleList,
|
|
|
|
strip_index_format: None,
|
|
|
|
},
|
|
|
|
depth_stencil: None,
|
|
|
|
multisample: MultisampleState {
|
|
|
|
count: 1,
|
|
|
|
mask: !0,
|
|
|
|
alpha_to_coverage_enabled: false,
|
|
|
|
},
|
|
|
|
label: Some("ui_pipeline".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|