use anyhow::Result;
use wgpu::{Device, Queue, Surface, SurfaceConfiguration, RenderPass, CommandEncoder};
use wgpu::util::DeviceExt;
pub struct Renderer {
device: Device,
queue: Queue,
surface: Surface,
config: SurfaceConfiguration,
}
impl Renderer {
pub fn new(device: Device, queue: Queue, surface: Surface, config: SurfaceConfiguration) -> Self {
Self {
device,
queue,
surface,
config,
}
}
pub fn device(&self) -> &Device {
&self.device
}
pub fn queue(&self) -> &Queue {
&self.queue
}
pub fn surface(&self) -> &Surface {
&self.surface
}
pub fn config(&self) -> &SurfaceConfiguration {
&self.config
}
pub fn update_config(&mut self, config: SurfaceConfiguration) {
self.config = config;
self.surface.configure(&self.device, &self.config);
}
pub fn begin_frame(&mut self) -> Result<(wgpu::SurfaceTexture, wgpu::TextureView, CommandEncoder)> {
let output = self.surface.get_current_texture()?;
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
Ok((output, view, encoder))
}
pub fn end_frame(&mut self,
output: wgpu::SurfaceTexture,
encoder: CommandEncoder) {
self.queue.submit(std::iter::once(encoder.finish()));
output.present();
}
pub fn create_render_pass<'a>(&self,
encoder: &'a mut CommandEncoder,
view: &'a wgpu::TextureView,
clear_color: Option<wgpu::Color>) -> RenderPass<'a> {
let mut render_pass_descriptor = wgpu::RenderPassDescriptor {
label: Some("Main Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}),
store: true,
},
})],
depth_stencil_attachment: None,
};
if let Some(color) = clear_color {
render_pass_descriptor.color_attachments[0].as_mut().unwrap().ops.load =
wgpu::LoadOp::Clear(color);
}
encoder.begin_render_pass(&render_pass_descriptor)
}
pub fn draw_rect(&self,
render_pass: &mut RenderPass,
x: f32,
y: f32,
width: f32,
height: f32,
color: [f32; 4]) {
}
pub fn draw_circle(&self,
render_pass: &mut RenderPass,
x: f32,
y: f32,
radius: f32,
color: [f32; 4]) {
}
pub fn draw_line(&self,
render_pass: &mut RenderPass,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
thickness: f32,
color: [f32; 4]) {
}
pub fn cleanup(&mut self) {
}
}