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
use crate::draw::Draw;
use notan_graphics::prelude::*;
#[derive(Default, Clone, Debug)]
pub(crate) struct CustomPipeline {
pub pipeline: Option<Pipeline>,
pub uniforms: Option<Vec<Buffer>>,
}
impl CustomPipeline {
fn clear(&mut self) {
self.pipeline = None;
self.uniforms = None;
}
}
impl std::cmp::PartialEq for CustomPipeline {
fn eq(&self, other: &Self) -> bool {
self.pipeline == other.pipeline && self.uniforms == other.uniforms
}
}
pub trait DrawCustomPipeline {
fn image_pipeline(&mut self) -> CustomPipelineBuilder;
fn shape_pipeline(&mut self) -> CustomPipelineBuilder;
fn pattern_pipeline(&mut self) -> CustomPipelineBuilder;
fn text_pipeline(&mut self) -> CustomPipelineBuilder;
}
impl DrawCustomPipeline for Draw {
fn image_pipeline(&mut self) -> CustomPipelineBuilder {
CustomPipelineBuilder::new(self, CustomPipelineType::Image)
}
fn shape_pipeline(&mut self) -> CustomPipelineBuilder {
CustomPipelineBuilder::new(self, CustomPipelineType::Shape)
}
fn pattern_pipeline(&mut self) -> CustomPipelineBuilder {
CustomPipelineBuilder::new(self, CustomPipelineType::Pattern)
}
fn text_pipeline(&mut self) -> CustomPipelineBuilder {
CustomPipelineBuilder::new(self, CustomPipelineType::Text)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum CustomPipelineType {
Image,
Shape,
Pattern,
Text,
}
pub struct CustomPipelineBuilder<'a> {
draw: &'a mut Draw,
typ: CustomPipelineType,
pipeline: Option<&'a Pipeline>,
uniforms: Option<Vec<&'a Buffer>>,
removing: bool,
}
impl<'a> CustomPipelineBuilder<'a> {
fn new(draw: &'a mut Draw, typ: CustomPipelineType) -> Self {
CustomPipelineBuilder {
draw,
typ,
pipeline: None,
uniforms: None,
removing: false,
}
}
pub fn pipeline(&mut self, pipeline: &'a Pipeline) -> &mut Self {
self.pipeline = Some(pipeline);
self
}
pub fn uniform_buffer(&mut self, buffer: &'a Buffer) -> &mut Self {
let uniforms = self.uniforms.get_or_insert(vec![]);
uniforms.push(buffer);
self
}
pub fn remove(&mut self) {
self.removing = true;
}
}
impl Drop for CustomPipelineBuilder<'_> {
fn drop(&mut self) {
if self.removing {
remove_pipeline(self.draw, self.typ);
return;
}
process_pipeline(self, self.typ);
}
}
fn process_pipeline(builder: &mut CustomPipelineBuilder, typ: CustomPipelineType) {
let pip = match builder.pipeline.take() {
Some(pip) => pip,
_ => return,
};
let custom = get_custom_pipeline(builder.draw, typ);
let needs_update = match &custom.pipeline {
Some(c_pip) => *c_pip != *pip,
_ => true,
};
if !needs_update {
return;
}
custom.pipeline = Some(pip.clone());
custom.uniforms = builder
.uniforms
.take()
.map(|u| u.into_iter().cloned().collect::<Vec<_>>());
}
fn remove_pipeline(draw: &mut Draw, typ: CustomPipelineType) {
get_custom_pipeline(draw, typ).clear();
}
fn get_custom_pipeline(draw: &mut Draw, typ: CustomPipelineType) -> &mut CustomPipeline {
match typ {
CustomPipelineType::Image => &mut draw.image_pipeline,
CustomPipelineType::Shape => &mut draw.shape_pipeline,
CustomPipelineType::Pattern => &mut draw.pattern_pipeline,
CustomPipelineType::Text => &mut draw.text_pipeline,
}
}