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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Maps a Simple Console to WGPU backing
use super::index_array_helper::IndexBuffer;
use super::vertex_array_helper::FloatBuffer;
use crate::hal::{
scaler::{FontScaler, ScreenScaler},
Font, Shader, WgpuLink,
};
use crate::prelude::Tile;
use crate::BResult;
use bracket_color::prelude::RGBA;
use wgpu::{BufferUsages, RenderPipeline};
/// Provide WGPU rendering services for a Simple Console
pub struct SimpleConsoleBackend {
/// The vertex buffer
vao: FloatBuffer<f32>,
/// The index buffer
index: IndexBuffer,
/// # elements in the index
index_counter: usize,
/// # elements in the vertex list
vertex_counter: usize,
/// The WGPU render pipeline
render_pipeline: RenderPipeline,
/// For dirty optimization
previous_console: Option<Vec<Tile>>,
}
impl SimpleConsoleBackend {
/// Create a new simple console backend. Call this from the main loop's
/// console rebuild.
pub fn new(
width: usize,
height: usize,
wgpu: &WgpuLink,
shader: &Shader,
font: &Font,
) -> SimpleConsoleBackend {
let vertex_capacity: usize = (13 * width as usize * height as usize) * 4;
let index_capacity: usize = 6 * width as usize * height as usize;
let mut vao = SimpleConsoleBackend::init_buffer_for_console(vertex_capacity);
vao.data = vec![0.0f32; vertex_capacity];
let index = IndexBuffer::new(index_capacity);
// 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,
},
});
// Build the result
let result = SimpleConsoleBackend {
vao,
index,
vertex_counter: 0,
index_counter: 0,
render_pipeline,
previous_console: None,
};
result
}
/// Creates a buffer definition mapped to the simple console shader.
fn init_buffer_for_console(vertex_capacity: usize) -> FloatBuffer<f32> {
FloatBuffer::<f32>::new(
&[3, 4, 4, 2], // Pos, FG, BG, TexPos
vertex_capacity,
BufferUsages::VERTEX,
)
}
/// Helper function to add all the elements required by the shader for a given point.
#[allow(clippy::too_many_arguments)]
fn push_point(
&mut self,
x: f32,
y: f32,
fg: RGBA,
bg: RGBA,
ux: f32,
uy: f32,
offset_x: f32,
offset_y: f32,
) {
self.vao.data[self.vertex_counter] = x + offset_x;
self.vao.data[self.vertex_counter + 1] = y + offset_y;
self.vao.data[self.vertex_counter + 2] = 0.0f32;
self.vao.data[self.vertex_counter + 3] = fg.r;
self.vao.data[self.vertex_counter + 4] = fg.g;
self.vao.data[self.vertex_counter + 5] = fg.b;
self.vao.data[self.vertex_counter + 6] = fg.a;
self.vao.data[self.vertex_counter + 7] = bg.r;
self.vao.data[self.vertex_counter + 8] = bg.g;
self.vao.data[self.vertex_counter + 9] = bg.b;
self.vao.data[self.vertex_counter + 10] = bg.a;
self.vao.data[self.vertex_counter + 11] = ux;
self.vao.data[self.vertex_counter + 12] = uy;
self.vertex_counter += 13;
}
/// Rebuilds the OpenGL backing buffer.
#[allow(clippy::too_many_arguments)]
pub fn rebuild_vertices(
&mut self,
wgpu: &WgpuLink,
height: u32,
width: u32,
tiles: &Vec<Tile>,
offset_x: f32,
offset_y: f32,
scale: f32,
scale_center: (i32, i32),
needs_resize: bool,
font_scaler: FontScaler,
screen_scaler: &ScreenScaler,
) {
/*if !needs_resize {
if let Some(old) = &self.previous_console {
if old.len() == tiles.len() {
let no_change = tiles.iter().zip(old.iter()).all(|(a, b)| *a==*b);
if no_change {
return;
}
}
}
}*/
if needs_resize {
let vertex_capacity: usize = (13 * width as usize * height as usize) * 4;
let index_capacity: usize = 6 * width as usize * height as usize;
self.vao.data.clear();
self.vao.data.resize(vertex_capacity, 0.0);
self.index.data.clear();
self.index.data.resize(index_capacity, 0);
}
self.vertex_counter = 0;
self.index_counter = 0;
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.0f32 / width as f32;
//let step_y: f32 = scale * 2.0f32 / height as f32;
//let mut screen_y: f32 = -1.0 * scale
// + 2.0 * (scale_center.1 - height as i32 / 2) as f32 * (scale - 1.0) / height as f32;
let mut index_count: u16 = 0;
let mut screen_y = top_y;
for y in 0..height {
let mut screen_x = left_x;
for x in 0..width {
let fg = tiles[((y * width) + x) as usize].fg;
let bg = tiles[((y * width) + x) as usize].bg;
let glyph = tiles[((y * width) + x) as usize].glyph;
let gp = font_scaler.glyph_position(glyph);
self.push_point(
screen_x + step_x,
screen_y + step_y,
fg,
bg,
gp.glyph_right,
gp.glyph_top,
offset_x,
offset_y,
);
self.push_point(
screen_x + step_x,
screen_y,
fg,
bg,
gp.glyph_right,
gp.glyph_bottom,
offset_x,
offset_y,
);
self.push_point(
screen_x,
screen_y,
fg,
bg,
gp.glyph_left,
gp.glyph_bottom,
offset_x,
offset_y,
);
self.push_point(
screen_x,
screen_y + step_y,
fg,
bg,
gp.glyph_left,
gp.glyph_top,
offset_x,
offset_y,
);
self.index.data[self.index_counter] = index_count;
self.index.data[self.index_counter + 1] = 1 + index_count;
self.index.data[self.index_counter + 2] = 3 + index_count;
self.index.data[self.index_counter + 3] = 1 + index_count;
self.index.data[self.index_counter + 4] = 2 + index_count;
self.index.data[self.index_counter + 5] = 3 + index_count;
self.index_counter += 6;
index_count += 4;
screen_x += step_x;
}
screen_y += step_y;
}
self.vao.build(wgpu);
self.index.build(wgpu);
self.previous_console = Some(tiles.clone());
}
/// Renders the console via wgpu.
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::Clear(wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
}),*/
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(())
}
}