Skip to content

Commit

Permalink
Well, at least it's running. But it's fcking slow :/
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Oct 6, 2021
1 parent d3becb9 commit 2dc54bc
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 114 deletions.
112 changes: 112 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -15,3 +15,4 @@ log = "0.4"
wgpu = "0.10"
pollster = "0.2"
wgpu_glyph = "0.14"
portable-pty = "0.4.0"
117 changes: 117 additions & 0 deletions src/frontend.rs
@@ -0,0 +1,117 @@
use wgpu::{Backends, Color, CommandEncoderDescriptor, Device, DeviceDescriptor, Features, Instance, Limits, LoadOp, Operations, PowerPreference, Queue, RenderPassColorAttachment, RenderPassDescriptor, RequestAdapterOptions, Surface, SurfaceConfiguration, SurfaceError, TextureFormat, TextureUsages, TextureViewDescriptor, util::StagingBelt};
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section, Text, ab_glyph};
use winit::{dpi::PhysicalSize, window::Window};

const TITLEBAR_MARGIN: f32 = 30.0;

pub struct AppFrontend {
pub surface: Surface,
pub device: Device,
pub queue: Queue,
pub config: SurfaceConfiguration,
pub size: PhysicalSize<u32>,
pub glyph_brush: GlyphBrush<()>,
pub staging_belt: StagingBelt,
pub data: String,
pub scale_factor: f32,
}

impl AppFrontend {
pub async fn new(window: &Window) -> Self {
let size = window.inner_size();

let instance = Instance::new(Backends::all());
let surface = unsafe { instance.create_surface(window) };
let adapter = instance.request_adapter(
&RequestAdapterOptions {
power_preference: PowerPreference::default(),
compatible_surface: Some(&surface)
}
).await.unwrap();

let (device, queue) = adapter.request_device(
&DeviceDescriptor { label: None, features: Features::empty(), limits: Limits::default() },
None
).await.unwrap();

let config = SurfaceConfiguration {
usage: TextureUsages::RENDER_ATTACHMENT,
format: surface.get_preferred_format(&adapter).unwrap(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo
};
surface.configure(&device, &config);

let render_format = TextureFormat::Bgra8UnormSrgb;
let font = ab_glyph::FontArc::try_from_slice(include_bytes!("iosevka.ttf")).unwrap();
let glyph_brush = GlyphBrushBuilder::using_font(font).build(&device, render_format);

let staging_belt = StagingBelt::new(1024);

Self {
surface, device, queue, config, size, glyph_brush, staging_belt,
data: "Press any key".to_string(),
scale_factor: window.scale_factor() as f32,
}
}

pub fn resize(&mut self, new_size: PhysicalSize<u32>, scale_factor: f32) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
if scale_factor > 0.0 {
self.scale_factor = scale_factor;
}
}
}

pub fn append_data(&mut self, content: &str) {
self.data = format!("{}{}", self.data, content);
}


pub fn render(&mut self) -> Result<(), SurfaceError> {
let output = self.surface.get_current_frame()?.output;
let view = output.texture.create_view(&TextureViewDescriptor::default());
let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor { label: Some("Render encoder") });
{
let _render_pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Render pass"),
color_attachments: &[RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: Operations {
load: LoadOp::Clear(Color {
r: 0.01,
g: 0.01,
b: 0.01,
a: 1.0
}),
store: true
}
}],
depth_stencil_attachment: None
});
}

self.glyph_brush.queue(Section {
screen_position: (30.0, 30.0 + TITLEBAR_MARGIN),
bounds: (self.size.width as f32, self.size.height as f32),
text: vec![Text::new(&self.data)
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(20.0 * self.scale_factor)],
..Section::default()
});

self.glyph_brush.draw_queued(&self.device, &mut self.staging_belt, &mut encoder, &view, self.size.width, self.size.height).ok();
self.staging_belt.finish();

self.queue.submit(std::iter::once(encoder.finish()));

Ok(())
}
}

0 comments on commit 2dc54bc

Please sign in to comment.