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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Provides a map between fancy/flexible consoles and webgpu back-end.
use super::index_array_helper::IndexBuffer;
use super::vertex_array_helper::FloatBuffer;
use crate::hal::{
scaler::{FontScaler, ScreenScaler},
Font, Shader, WgpuLink,
};
use crate::prelude::FlexiTile;
use crate::BResult;
use bracket_color::prelude::RGBA;
use bracket_geometry::prelude::PointF;
use wgpu::{BufferUsages, RenderPipeline};
/// Provides a mapping between fancy/flexible terminals and webgpu.
/// Maintains a vertex buffer of font positions, an index buffer,
/// and its own pipeline.
pub struct FancyConsoleBackend {
/// The vertex buffer representing how to draw this console.
vao: FloatBuffer<f32>,
/// The index buffer representing how to draw this console.
index: IndexBuffer,
/// The WGPU render pipeline used to render fancy consoles.
render_pipeline: RenderPipeline,
}
impl FancyConsoleBackend {
/// Instantiate a new FancyConsoleBackend. This should be called from the
/// "rebuild consoles" step of the main loop.
pub fn new(wgpu: &WgpuLink, shader: &Shader, font: &Font) -> FancyConsoleBackend {
let mut vao = FancyConsoleBackend::init_buffer_for_console(1000);
let mut index = IndexBuffer::new(1000);
vao.build(wgpu);
index.build(wgpu);
// Setup the pipeline
let render_pipeline_layout =
wgpu.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[font.bind_group_layout.as_ref().unwrap()],
push_constant_ranges: &[],
});
let render_pipeline = wgpu
.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
multiview: None,
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader.0,
entry_point: "vs_main",
buffers: &[vao.descriptor()],
},
fragment: Some(wgpu::FragmentState {
module: &shader.0,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
format: wgpu.config.format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
//cull_mode: Some(wgpu::Face::Back),
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
unclipped_depth: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
});
FancyConsoleBackend {
vao,
render_pipeline,
index,
}
}
/// Creates a vertex buffer matching the appropriate shader (fancy.wgsl)
fn init_buffer_for_console(vertex_capacity: usize) -> FloatBuffer<f32> {
FloatBuffer::<f32>::new(
&[3, 4, 4, 2, 3, 2], // Pos, fg, col, tex, rot, scale
vertex_capacity,
BufferUsages::VERTEX,
)
}
/// Helper to push a point to the shader.
#[allow(clippy::too_many_arguments)]
fn push_point(
vertex_buffer: &mut Vec<f32>,
x: f32,
y: f32,
fg: RGBA,
bg: RGBA,
ux: f32,
uy: f32,
rotation: f32,
screen_x: f32,
screen_y: f32,
scale: PointF,
) {
vertex_buffer.extend_from_slice(&[
x, y, 0.0, fg.r, fg.g, fg.b, fg.a, bg.r, bg.g, bg.b, bg.a, ux, uy, rotation, screen_x,
screen_y, scale.x, scale.y,
]);
}
/// Helper to build vertices for the sparse grid.
#[allow(clippy::too_many_arguments)]
pub fn rebuild_vertices(
&mut self,
wgpu: &WgpuLink,
height: u32,
width: u32,
offset_x: f32,
offset_y: f32,
scale: f32,
scale_center: (i32, i32),
tiles: &[FlexiTile],
font_scaler: FontScaler,
screen_scaler: &ScreenScaler,
) {
if tiles.is_empty() {
return;
}
self.vao.data.clear();
self.index.data.clear();
let (step_x, step_y, left_x, top_y) = {
let (step_x, step_y) = screen_scaler.calc_step(width, height, scale);
let (left_x, top_y) = screen_scaler.top_left_pixel();
(step_x, step_y, left_x, top_y)
};
//let step_x: f32 = scale * 2.0 / width as f32;
//let step_y: f32 = scale * 2.0 / height as f32;
let mut index_count: u16 = 0;
//let screen_x_start: f32 = -1.0 * scale
// - 2.0 * (scale_center.0 - width as i32 / 2) as f32 * (scale - 1.0) / width as f32;
//let screen_y_start: f32 = -1.0 * scale
// + 2.0 * (scale_center.1 - height as i32 / 2) as f32 * (scale - 1.0) / height as f32;
for t in tiles.iter() {
let x = t.position.x;
let y = t.position.y;
let screen_x = ((step_x * x) + left_x) + offset_x;
let screen_y = ((step_y * y) + top_y) + offset_y;
let fg = t.fg;
let bg = t.bg;
let glyph = t.glyph;
let gp = font_scaler.glyph_position(glyph);
let rot_center_x = screen_x + (step_x / 2.0);
let rot_center_y = screen_y + (step_y / 2.0);
FancyConsoleBackend::push_point(
&mut self.vao.data,
screen_x + step_x,
screen_y + step_y,
fg,
bg,
gp.glyph_right,
gp.glyph_top,
t.rotation,
rot_center_x,
rot_center_y,
t.scale,
);
FancyConsoleBackend::push_point(
&mut self.vao.data,
screen_x + step_x,
screen_y,
fg,
bg,
gp.glyph_right,
gp.glyph_bottom,
t.rotation,
rot_center_x,
rot_center_y,
t.scale,
);
FancyConsoleBackend::push_point(
&mut self.vao.data,
screen_x,
screen_y,
fg,
bg,
gp.glyph_left,
gp.glyph_bottom,
t.rotation,
rot_center_x,
rot_center_y,
t.scale,
);
FancyConsoleBackend::push_point(
&mut self.vao.data,
screen_x,
screen_y + step_y,
fg,
bg,
gp.glyph_left,
gp.glyph_top,
t.rotation,
rot_center_x,
rot_center_y,
t.scale,
);
self.index.data.push(index_count);
self.index.data.push(1 + index_count);
self.index.data.push(3 + index_count);
self.index.data.push(1 + index_count);
self.index.data.push(2 + index_count);
self.index.data.push(3 + index_count);
index_count += 4;
}
self.vao.build(wgpu);
self.index.build(wgpu);
}
/// Uses WGPU to render the console. Note that it grabs its own accessor
/// to the backend mutex, so it doesn't need to be passed in - AND CANNOT
/// BE LOCKED when you call this, or things will deadlock.
pub fn wgpu_draw(&mut self, font: &Font) -> BResult<()> {
use crate::hal::BACKEND;
let mut be = BACKEND.lock();
if let Some(wgpu) = be.wgpu.as_mut() {
let mut encoder = wgpu
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: wgpu.backing_buffer.view(),
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
},
})],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, font.bind_group.as_ref().unwrap(), &[]);
render_pass.set_vertex_buffer(0, self.vao.slice());
render_pass.set_index_buffer(self.index.slice(), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.index.len(), 0, 0..1);
}
// submit will accept anything that implements IntoIter
wgpu.queue.submit(std::iter::once(encoder.finish()));
//output.present();
}
Ok(())
}
}