-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathuni_buffer.rs
177 lines (152 loc) · 4.28 KB
/
uni_buffer.rs
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
//! This program shows how to use uniform buffers to pass large chunks of data to shaders, that can be shared between
//! shaders and changed only once (instead of for every shader programs that require that data).
//!
//! Uniform buffers are often used to implement geometry instancing and various kinds of effects requiring shared
//! data and/or large amount of it.
//!
//! <https://docs.rs/luminance>
use std::f32::consts::PI;
use luminance::{
backend::{Backend, Error},
context::Context,
dim::{Dim2, Size2},
framebuffer::{Back, Framebuffer},
pipeline::PipelineState,
primitive::TriangleFan,
render_state::RenderState,
shader::{Program, ProgramBuilder, Std140, Uni, UniBuffer},
vertex_entity::{VertexEntity, VertexEntityBuilder, View},
vertex_storage::{Interleaved, Interleaving},
Std140, Uniforms,
};
use crate::{
shared::{FragSlot, Vertex},
Example, InputAction, LoopFeedback, PlatformServices,
};
const VS: &str = include_str!("./uni-buffer-2d-vs.glsl");
const FS: &str = include_str!("./simple-fs.glsl");
const VERTICES: [Vertex; 4] = [
Vertex::new(
mint::Vector2 { x: -0.01, y: -0.01 },
mint::Vector3 {
x: 0.5,
y: 1.,
z: 0.5,
},
),
Vertex::new(
mint::Vector2 { x: 0.01, y: -0.01 },
mint::Vector3 {
x: 0.5,
y: 1.,
z: 0.5,
},
),
Vertex::new(
mint::Vector2 { x: 0.01, y: 0.01 },
mint::Vector3 {
x: 0.5,
y: 1.,
z: 0.5,
},
),
Vertex::new(
mint::Vector2 { x: -0.01, y: 0.01 },
mint::Vector3 {
x: 0.5,
y: 1.,
z: 0.5,
},
),
];
#[derive(Uniforms)]
struct ShaderUniforms {
#[uniform(name = "Positions")]
positions: Uni<UniBuffer<UniBlock, Std140>>,
}
#[derive(Debug, Std140)]
pub struct UniBlock {
p: [mint::Vector2<f32>; 100],
}
pub struct LocalExample {
square: VertexEntity<Vertex, TriangleFan, Interleaving>,
program: Program<Vertex, (), TriangleFan, FragSlot, ShaderUniforms>,
uni_buffer: UniBuffer<UniBlock, Std140>,
back_buffer: Framebuffer<Dim2, Back<FragSlot>, Back<()>>,
}
impl Example for LocalExample {
type Err = Error;
const TITLE: &'static str = "Uniform buffer";
fn bootstrap(
[width, height]: [u32; 2],
_: &mut impl PlatformServices,
ctx: &mut Context<impl Backend>,
) -> Result<Self, Self::Err> {
let square = ctx.new_vertex_entity(
VertexEntityBuilder::new().add_vertices(Interleaved::new().set_vertices(VERTICES)),
)?;
let program = ctx.new_program(
ProgramBuilder::new()
.add_vertex_stage(VS)
.no_primitive_stage()
.add_shading_stage(FS),
)?;
let uni_buffer = ctx.new_uni_buffer(
UniBlock {
p: [mint::Vector2 { x: 0., y: 0. }; 100],
}
.into(),
)?;
let back_buffer = ctx.back_buffer(Size2::new(width, height))?;
Ok(Self {
square,
program,
uni_buffer,
back_buffer,
})
}
fn render_frame(
mut self,
time: f32,
actions: impl Iterator<Item = InputAction>,
ctx: &mut Context<impl Backend>,
) -> Result<LoopFeedback<Self>, Self::Err> {
for action in actions {
match action {
InputAction::Quit => return Ok(LoopFeedback::Exit),
InputAction::Resized { width, height } => {
self.back_buffer = ctx.back_buffer(Size2::new(width, height))?;
}
_ => (),
}
}
let square = &self.square;
let program = &self.program;
let uni_buffer = &self.uni_buffer;
// update the positions of the squares
{
ctx
.sync_uni_buffer(uni_buffer)?
.p
.iter_mut()
.enumerate()
.for_each(|(i, p)| {
let i = i as f32;
let phi = i * 2. * PI * 0.01 + time * 0.2;
let radius = 0.8;
p.x = phi.cos() * radius;
p.y = (phi + i).sin() * radius;
});
}
ctx.with_framebuffer(&self.back_buffer, &PipelineState::default(), |mut frame| {
let in_use_uni_buffer = frame.use_uni_buffer(uni_buffer)?;
frame.with_program(program, |mut frame| {
frame.update(|mut update, unis| update.set(&unis.positions, &in_use_uni_buffer))?;
frame.with_render_state(&RenderState::default(), |mut frame| {
frame.render_vertex_entity(square.view(..).set_instance_count(100))
})
})
})?;
Ok(LoopFeedback::Continue(self))
}
}