Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions bracket-terminal/examples/webgpu_minimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use bracket_terminal::prelude::*;

bracket_terminal::add_wasm_support!();

struct State {
player_x: i32,
player_y: i32,
glow: bool,
last_key: Option<VirtualKeyCode>,
}

impl State {
fn new() -> Self {
Self {
player_x: 40,
player_y: 25,
glow: false,
last_key: None,
}
}

fn clamp_player(&mut self) {
self.player_x = self.player_x.clamp(1, 78);
self.player_y = self.player_y.clamp(4, 48);
}
}

impl GameState for State {
fn tick(&mut self, ctx: &mut BTerm) {
if let Some(key) = ctx.key {
self.last_key = Some(key);
match key {
VirtualKeyCode::Left => self.player_x -= 1,
VirtualKeyCode::Right => self.player_x += 1,
VirtualKeyCode::Up => self.player_y -= 1,
VirtualKeyCode::Down => self.player_y += 1,
VirtualKeyCode::Space => self.glow = !self.glow,
VirtualKeyCode::Escape => ctx.quitting = true,
_ => {}
}
}
self.clamp_player();

let bg = if self.glow {
RGB::from_u8(8, 22, 40)
} else {
RGB::from_u8(0, 0, 0)
};
let marker_fg = if self.glow {
RGB::named(YELLOW)
} else {
RGB::named(WHITE)
};
let marker_bg = if self.glow {
RGB::from_u8(20, 60, 100)
} else {
RGB::named(BLACK)
};

ctx.cls_bg(bg);
ctx.draw_box(0, 0, 79, 49, RGB::named(CYAN), bg);
ctx.print_color(2, 1, RGB::named(WHITE), bg, "WebGPU Minimal Example");
ctx.print_color(
2,
2,
RGB::named(GRAY),
bg,
"Arrow keys move, Space toggles glow, Esc exits",
);

ctx.print_color(
2,
46,
RGB::named(GREEN),
bg,
format!("Mouse tile: {}, {}", ctx.mouse_pos.0, ctx.mouse_pos.1),
);
ctx.print_color(
2,
47,
RGB::named(MAGENTA),
bg,
format!("Last key: {:?}", self.last_key),
);
ctx.print_color(
2,
48,
RGB::named(YELLOW),
bg,
format!("FPS: {:.1} Frame: {:.2} ms", ctx.fps, ctx.frame_time_ms),
);

ctx.print_color(self.player_x, self.player_y, marker_fg, marker_bg, "@");
}
}

fn main() -> BError {
let context = BTermBuilder::simple80x50()
.with_title("Bracket Terminal - WebGPU Minimal")
.build()?;

main_loop(context, State::new())
}
8 changes: 7 additions & 1 deletion bracket-terminal/src/hal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ pub use dummy::*;
#[cfg(any(feature = "opengl", feature = "webgpu"))]
mod scaler;

#[cfg(all(not(feature = "opengl"), feature = "webgpu"))]
type ActivePlatformGL = PlatformGL<'static>;

#[cfg(not(all(not(feature = "opengl"), feature = "webgpu")))]
type ActivePlatformGL = PlatformGL;

/// Provides a base abstract platform for BTerm to run on, with specialized content.
pub struct BTermPlatform {
pub platform: PlatformGL,
pub platform: ActivePlatformGL,
}

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion bracket-terminal/src/hal/webgpu/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lazy_static::*;
use parking_lot::Mutex;

lazy_static! {
pub static ref BACKEND: Mutex<PlatformGL> = Mutex::new(PlatformGL {
pub static ref BACKEND: Mutex<PlatformGL<'static>> = Mutex::new(PlatformGL {
context_wrapper: None,
wgpu: None,
resize_scaling: false,
Expand Down
21 changes: 14 additions & 7 deletions bracket-terminal/src/hal/webgpu/backing/fancy_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,29 @@ impl FancyConsoleBackend {
wgpu.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[font.bind_group_layout.as_ref().unwrap()],
push_constant_ranges: &[],
bind_group_layouts: &[Some(font.bind_group_layout.as_ref().unwrap())],
immediate_size: 0,
});
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",
entry_point: Some("vs_main"),
buffers: &[vao.descriptor()],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader.0,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu.config.format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
Expand All @@ -77,6 +78,8 @@ impl FancyConsoleBackend {
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
});

FancyConsoleBackend {
Expand Down Expand Up @@ -126,7 +129,7 @@ impl FancyConsoleBackend {
offset_x: f32,
offset_y: f32,
scale: f32,
scale_center: (i32, i32),
_scale_center: (i32, i32),
tiles: &[FlexiTile],
font_scaler: FontScaler,
screen_scaler: &ScreenScaler,
Expand Down Expand Up @@ -253,12 +256,16 @@ impl FancyConsoleBackend {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: wgpu.backing_buffer.view(),
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
multiview_mask: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, font.bind_group.as_ref().unwrap(), &[]);
Expand Down
6 changes: 2 additions & 4 deletions bracket-terminal/src/hal/webgpu/backing/index_array_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ impl IndexBuffer {
/// Calls WGPU's "create_buffer_init" path to copy the index buffer
/// from local memory to GPU memory.
pub fn build(&mut self, wgpu: &WgpuLink) {
if let Some(buf) = &mut self.buffer {
std::mem::drop(buf);
}
self.buffer = None;
self.buffer = Some(
wgpu.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand All @@ -47,7 +45,7 @@ impl IndexBuffer {
}

/// Maps the index buffer into a slice, suitable for render submission.
pub fn slice(&self) -> wgpu::BufferSlice {
pub fn slice(&self) -> wgpu::BufferSlice<'_> {
self.buffer.as_ref().unwrap().slice(..)
}
}
21 changes: 14 additions & 7 deletions bracket-terminal/src/hal/webgpu/backing/simple_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,29 @@ impl SimpleConsoleBackend {
wgpu.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[font.bind_group_layout.as_ref().unwrap()],
push_constant_ranges: &[],
bind_group_layouts: &[Some(font.bind_group_layout.as_ref().unwrap())],
immediate_size: 0,
});
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",
entry_point: Some("vs_main"),
buffers: &[vao.descriptor()],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader.0,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu.config.format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
Expand All @@ -87,6 +88,8 @@ impl SimpleConsoleBackend {
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
});

// Build the result
Expand Down Expand Up @@ -150,7 +153,7 @@ impl SimpleConsoleBackend {
offset_x: f32,
offset_y: f32,
scale: f32,
scale_center: (i32, i32),
_scale_center: (i32, i32),
needs_resize: bool,
font_scaler: FontScaler,
screen_scaler: &ScreenScaler,
Expand Down Expand Up @@ -277,6 +280,7 @@ impl SimpleConsoleBackend {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: wgpu.backing_buffer.view(),
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
/*load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.0,
Expand All @@ -285,10 +289,13 @@ impl SimpleConsoleBackend {
a: 1.0,
}),*/
load: wgpu::LoadOp::Load,
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
multiview_mask: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, font.bind_group.as_ref().unwrap(), &[]);
Expand Down
21 changes: 14 additions & 7 deletions bracket-terminal/src/hal/webgpu/backing/sparse_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,29 @@ impl SparseConsoleBackend {
wgpu.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[font.bind_group_layout.as_ref().unwrap()],
push_constant_ranges: &[],
bind_group_layouts: &[Some(font.bind_group_layout.as_ref().unwrap())],
immediate_size: 0,
});
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",
entry_point: Some("vs_main"),
buffers: &[vao.descriptor()],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader.0,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu.config.format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
Expand All @@ -72,6 +73,8 @@ impl SparseConsoleBackend {
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
});

SparseConsoleBackend {
Expand Down Expand Up @@ -116,7 +119,7 @@ impl SparseConsoleBackend {
offset_x: f32,
offset_y: f32,
scale: f32,
scale_center: (i32, i32),
_scale_center: (i32, i32),
tiles: &Vec<SparseTile>,
font_scaler: FontScaler,
screen_scaler: &ScreenScaler,
Expand Down Expand Up @@ -229,12 +232,16 @@ impl SparseConsoleBackend {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: wgpu.backing_buffer.view(),
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
multiview_mask: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, font.bind_group.as_ref().unwrap(), &[]);
Expand Down
Loading