From db8eb1c97cda5c62ad1844e0cbb36c6bf408349f Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 29 Apr 2019 16:45:29 -0700 Subject: [PATCH] Run `rustfmt` on the renderer crate --- renderer/src/builder.rs | 97 ++- renderer/src/gpu/debug.rs | 154 +++-- renderer/src/gpu/renderer.rs | 1061 +++++++++++++++++++++------------ renderer/src/gpu_data.rs | 138 +++-- renderer/src/post.rs | 10 +- renderer/src/scene.rs | 69 ++- renderer/src/sorted_vector.rs | 2 +- renderer/src/tile_map.rs | 33 +- renderer/src/tiles.rs | 172 +++--- renderer/src/z_buffer.rs | 4 +- 10 files changed, 1108 insertions(+), 632 deletions(-) diff --git a/renderer/src/builder.rs b/renderer/src/builder.rs index 029dfbb6..1a34d9f5 100644 --- a/renderer/src/builder.rs +++ b/renderer/src/builder.rs @@ -37,10 +37,11 @@ pub struct SceneBuilder<'a> { } impl<'a> SceneBuilder<'a> { - pub fn new(scene: &'a Scene, - built_options: &'a PreparedRenderOptions, - listener: Box) - -> SceneBuilder<'a> { + pub fn new( + scene: &'a Scene, + built_options: &'a PreparedRenderOptions, + listener: Box, + ) -> SceneBuilder<'a> { let effective_view_box = scene.effective_view_box(built_options); SceneBuilder { scene, @@ -55,12 +56,17 @@ impl<'a> SceneBuilder<'a> { pub fn build_sequentially(&mut self) { let effective_view_box = self.scene.effective_view_box(self.built_options); let object_count = self.scene.objects.len(); - let alpha_tiles: Vec<_> = (0..object_count).into_iter().flat_map(|object_index| { - self.build_object(object_index, - effective_view_box, - &self.built_options, - &self.scene) - }).collect(); + let alpha_tiles: Vec<_> = (0..object_count) + .into_iter() + .flat_map(|object_index| { + self.build_object( + object_index, + effective_view_box, + &self.built_options, + &self.scene, + ) + }) + .collect(); self.finish_building(alpha_tiles) } @@ -68,36 +74,46 @@ impl<'a> SceneBuilder<'a> { pub fn build_in_parallel(&mut self) { let effective_view_box = self.scene.effective_view_box(self.built_options); let object_count = self.scene.objects.len(); - let alpha_tiles: Vec<_> = (0..object_count).into_par_iter().flat_map(|object_index| { - self.build_object(object_index, - effective_view_box, - &self.built_options, - &self.scene) - }).collect(); + let alpha_tiles: Vec<_> = (0..object_count) + .into_par_iter() + .flat_map(|object_index| { + self.build_object( + object_index, + effective_view_box, + &self.built_options, + &self.scene, + ) + }) + .collect(); self.finish_building(alpha_tiles) } - fn build_object(&self, - object_index: usize, - view_box: RectF32, - built_options: &PreparedRenderOptions, - scene: &Scene) - -> Vec { + fn build_object( + &self, + object_index: usize, + view_box: RectF32, + built_options: &PreparedRenderOptions, + scene: &Scene, + ) -> Vec { let object = &scene.objects[object_index]; let outline = scene.apply_render_options(object.outline(), built_options); let mut tiler = Tiler::new(self, &outline, view_box, object_index as u16); tiler.generate_tiles(); - self.listener.send(RenderCommand::AddFills(tiler.built_object.fills)); + self.listener + .send(RenderCommand::AddFills(tiler.built_object.fills)); tiler.built_object.alpha_tiles } fn cull_alpha_tiles(&self, alpha_tiles: &mut Vec) { for alpha_tile in alpha_tiles { let alpha_tile_coords = alpha_tile.tile_coords(); - if self.z_buffer.test(alpha_tile_coords, alpha_tile.object_index as u32) { + if self + .z_buffer + .test(alpha_tile_coords, alpha_tile.object_index as u32) + { continue; } @@ -197,10 +213,20 @@ impl RenderTransform { } let inverse_transform = perspective.transform.inverse(); - let clip_polygon = points.into_iter().map(|point| { - inverse_transform.transform_point(point).perspective_divide().to_2d() - }).collect(); - return PreparedRenderTransform::Perspective { perspective, clip_polygon, quad }; + let clip_polygon = points + .into_iter() + .map(|point| { + inverse_transform + .transform_point(point) + .perspective_divide() + .to_2d() + }) + .collect(); + return PreparedRenderTransform::Perspective { + perspective, + clip_polygon, + quad, + }; } } @@ -223,7 +249,11 @@ impl PreparedRenderOptions { pub enum PreparedRenderTransform { None, Transform2D(Transform2DF32), - Perspective { perspective: Perspective, clip_polygon: Vec, quad: [Point3DF32; 4] } + Perspective { + perspective: Perspective, + clip_polygon: Vec, + quad: [Point3DF32; 4], + }, } impl PreparedRenderTransform { @@ -236,9 +266,14 @@ impl PreparedRenderTransform { } } -impl RenderCommandListener for F where F: Fn(RenderCommand) + Send + Sync { +impl RenderCommandListener for F +where + F: Fn(RenderCommand) + Send + Sync, +{ #[inline] - fn send(&self, command: RenderCommand) { (*self)(command) } + fn send(&self, command: RenderCommand) { + (*self)(command) + } } #[derive(Clone, Copy, Debug, Default)] diff --git a/renderer/src/gpu/debug.rs b/renderer/src/gpu/debug.rs index 65e3fc58..faa616b4 100644 --- a/renderer/src/gpu/debug.rs +++ b/renderer/src/gpu/debug.rs @@ -18,8 +18,8 @@ use crate::gpu::renderer::RenderStats; use pathfinder_geometry::basic::point::Point2DI32; use pathfinder_geometry::basic::rect::RectI32; -use pathfinder_gpu::Device; use pathfinder_gpu::resources::ResourceLoader; +use pathfinder_gpu::Device; use pathfinder_ui::{FONT_ASCENT, LINE_HEIGHT, PADDING, UI, WINDOW_COLOR}; use std::collections::VecDeque; use std::ops::{Add, Div}; @@ -30,27 +30,47 @@ const SAMPLE_BUFFER_SIZE: usize = 60; const PERF_WINDOW_WIDTH: i32 = 375; const PERF_WINDOW_HEIGHT: i32 = LINE_HEIGHT * 6 + PADDING + 2; -pub struct DebugUI where D: Device { +pub struct DebugUI +where + D: Device, +{ pub ui: UI, cpu_samples: SampleBuffer, gpu_samples: SampleBuffer, } -impl DebugUI where D: Device { - pub fn new(device: &D, resources: &dyn ResourceLoader, framebuffer_size: Point2DI32) - -> DebugUI { +impl DebugUI +where + D: Device, +{ + pub fn new( + device: &D, + resources: &dyn ResourceLoader, + framebuffer_size: Point2DI32, + ) -> DebugUI { let ui = UI::new(device, resources, framebuffer_size); - DebugUI { ui, cpu_samples: SampleBuffer::new(), gpu_samples: SampleBuffer::new() } + DebugUI { + ui, + cpu_samples: SampleBuffer::new(), + gpu_samples: SampleBuffer::new(), + } } - pub fn add_sample(&mut self, - stats: RenderStats, - tile_time: Duration, - rendering_time: Option) { - self.cpu_samples.push(CPUSample { stats, elapsed: tile_time }); + pub fn add_sample( + &mut self, + stats: RenderStats, + tile_time: Duration, + rendering_time: Option, + ) { + self.cpu_samples.push(CPUSample { + stats, + elapsed: tile_time, + }); if let Some(rendering_time) = rendering_time { - self.gpu_samples.push(GPUSample { elapsed: rendering_time }); + self.gpu_samples.push(GPUSample { + elapsed: rendering_time, + }); } } @@ -59,50 +79,80 @@ impl DebugUI where D: Device { let framebuffer_size = self.ui.framebuffer_size(); let bottom = framebuffer_size.y() - PADDING; let window_rect = RectI32::new( - Point2DI32::new(framebuffer_size.x() - PADDING - PERF_WINDOW_WIDTH, - bottom - PERF_WINDOW_HEIGHT), - Point2DI32::new(PERF_WINDOW_WIDTH, PERF_WINDOW_HEIGHT)); - self.ui.draw_solid_rounded_rect(device, window_rect, WINDOW_COLOR); + Point2DI32::new( + framebuffer_size.x() - PADDING - PERF_WINDOW_WIDTH, + bottom - PERF_WINDOW_HEIGHT, + ), + Point2DI32::new(PERF_WINDOW_WIDTH, PERF_WINDOW_HEIGHT), + ); + self.ui + .draw_solid_rounded_rect(device, window_rect, WINDOW_COLOR); let origin = window_rect.origin() + Point2DI32::new(PADDING, PADDING + FONT_ASCENT); let mean_cpu_sample = self.cpu_samples.mean(); - self.ui.draw_text(device, - &format!("Objects: {}", mean_cpu_sample.stats.object_count), - origin, - false); - self.ui.draw_text(device, - &format!("Solid Tiles: {}", mean_cpu_sample.stats.solid_tile_count), - origin + Point2DI32::new(0, LINE_HEIGHT * 1), - false); - self.ui.draw_text(device, - &format!("Alpha Tiles: {}", mean_cpu_sample.stats.alpha_tile_count), - origin + Point2DI32::new(0, LINE_HEIGHT * 2), - false); - self.ui.draw_text(device, - &format!("Fills: {}", mean_cpu_sample.stats.fill_count), - origin + Point2DI32::new(0, LINE_HEIGHT * 3), - false); + self.ui.draw_text( + device, + &format!("Objects: {}", mean_cpu_sample.stats.object_count), + origin, + false, + ); + self.ui.draw_text( + device, + &format!("Solid Tiles: {}", mean_cpu_sample.stats.solid_tile_count), + origin + Point2DI32::new(0, LINE_HEIGHT * 1), + false, + ); + self.ui.draw_text( + device, + &format!("Alpha Tiles: {}", mean_cpu_sample.stats.alpha_tile_count), + origin + Point2DI32::new(0, LINE_HEIGHT * 2), + false, + ); + self.ui.draw_text( + device, + &format!("Fills: {}", mean_cpu_sample.stats.fill_count), + origin + Point2DI32::new(0, LINE_HEIGHT * 3), + false, + ); - self.ui.draw_text(device, - &format!("CPU Time: {:.3} ms", duration_to_ms(mean_cpu_sample.elapsed)), - origin + Point2DI32::new(0, LINE_HEIGHT * 4), - false); + self.ui.draw_text( + device, + &format!( + "CPU Time: {:.3} ms", + duration_to_ms(mean_cpu_sample.elapsed) + ), + origin + Point2DI32::new(0, LINE_HEIGHT * 4), + false, + ); let mean_gpu_sample = self.gpu_samples.mean(); - self.ui.draw_text(device, - &format!("GPU Time: {:.3} ms", duration_to_ms(mean_gpu_sample.elapsed)), - origin + Point2DI32::new(0, LINE_HEIGHT * 5), - false); + self.ui.draw_text( + device, + &format!( + "GPU Time: {:.3} ms", + duration_to_ms(mean_gpu_sample.elapsed) + ), + origin + Point2DI32::new(0, LINE_HEIGHT * 5), + false, + ); } } -struct SampleBuffer where S: Add + Div + Clone + Default { +struct SampleBuffer +where + S: Add + Div + Clone + Default, +{ samples: VecDeque, } -impl SampleBuffer where S: Add + Div + Clone + Default { +impl SampleBuffer +where + S: Add + Div + Clone + Default, +{ fn new() -> SampleBuffer { - SampleBuffer { samples: VecDeque::with_capacity(SAMPLE_BUFFER_SIZE) } + SampleBuffer { + samples: VecDeque::with_capacity(SAMPLE_BUFFER_SIZE), + } } fn push(&mut self, time: S) { @@ -135,14 +185,20 @@ struct CPUSample { impl Add for CPUSample { type Output = CPUSample; fn add(self, other: CPUSample) -> CPUSample { - CPUSample { elapsed: self.elapsed + other.elapsed, stats: self.stats + other.stats } + CPUSample { + elapsed: self.elapsed + other.elapsed, + stats: self.stats + other.stats, + } } } impl Div for CPUSample { type Output = CPUSample; fn div(self, divisor: usize) -> CPUSample { - CPUSample { elapsed: self.elapsed / (divisor as u32), stats: self.stats / divisor } + CPUSample { + elapsed: self.elapsed / (divisor as u32), + stats: self.stats / divisor, + } } } @@ -154,14 +210,18 @@ struct GPUSample { impl Add for GPUSample { type Output = GPUSample; fn add(self, other: GPUSample) -> GPUSample { - GPUSample { elapsed: self.elapsed + other.elapsed } + GPUSample { + elapsed: self.elapsed + other.elapsed, + } } } impl Div for GPUSample { type Output = GPUSample; fn div(self, divisor: usize) -> GPUSample { - GPUSample { elapsed: self.elapsed / (divisor as u32) } + GPUSample { + elapsed: self.elapsed / (divisor as u32), + } } } diff --git a/renderer/src/gpu/renderer.rs b/renderer/src/gpu/renderer.rs index b4f7ab0b..c36fb276 100644 --- a/renderer/src/gpu/renderer.rs +++ b/renderer/src/gpu/renderer.rs @@ -46,7 +46,10 @@ const FILL_COLORS_TEXTURE_HEIGHT: i32 = 256; const MAX_FILLS_PER_BATCH: usize = 0x4000; -pub struct Renderer where D: Device { +pub struct Renderer +where + D: Device, +{ // Device pub device: D, @@ -97,9 +100,15 @@ pub struct Renderer where D: Device { use_depth: bool, } -impl Renderer where D: Device { - pub fn new(device: D, resources: &dyn ResourceLoader, dest_framebuffer: DestFramebuffer) - -> Renderer { +impl Renderer +where + D: Device, +{ + pub fn new( + device: D, + resources: &dyn ResourceLoader, + dest_framebuffer: DestFramebuffer, + ) -> Renderer { let fill_program = FillProgram::new(&device, resources); let solid_multicolor_tile_program = SolidTileMulticolorProgram::new(&device, resources); @@ -115,47 +124,55 @@ impl Renderer where D: Device { let gamma_lut_texture = device.create_texture_from_png(resources, "gamma-lut"); let quad_vertex_positions_buffer = device.create_buffer(); - device.allocate_buffer(&quad_vertex_positions_buffer, - BufferData::Memory(&QUAD_VERTEX_POSITIONS), - BufferTarget::Vertex, - BufferUploadMode::Static); + device.allocate_buffer( + &quad_vertex_positions_buffer, + BufferData::Memory(&QUAD_VERTEX_POSITIONS), + BufferTarget::Vertex, + BufferUploadMode::Static, + ); - let fill_vertex_array = FillVertexArray::new(&device, - &fill_program, - &quad_vertex_positions_buffer); - let alpha_multicolor_tile_vertex_array = - AlphaTileVertexArray::new(&device, - &alpha_multicolor_tile_program.alpha_tile_program, - &quad_vertex_positions_buffer); - let solid_multicolor_tile_vertex_array = - SolidTileVertexArray::new(&device, - &solid_multicolor_tile_program.solid_tile_program, - &quad_vertex_positions_buffer); - let alpha_monochrome_tile_vertex_array = - AlphaTileVertexArray::new(&device, - &alpha_monochrome_tile_program.alpha_tile_program, - &quad_vertex_positions_buffer); - let solid_monochrome_tile_vertex_array = - SolidTileVertexArray::new(&device, - &solid_monochrome_tile_program.solid_tile_program, - &quad_vertex_positions_buffer); - let postprocess_vertex_array = PostprocessVertexArray::new(&device, - &postprocess_program, - &quad_vertex_positions_buffer); + let fill_vertex_array = + FillVertexArray::new(&device, &fill_program, &quad_vertex_positions_buffer); + let alpha_multicolor_tile_vertex_array = AlphaTileVertexArray::new( + &device, + &alpha_multicolor_tile_program.alpha_tile_program, + &quad_vertex_positions_buffer, + ); + let solid_multicolor_tile_vertex_array = SolidTileVertexArray::new( + &device, + &solid_multicolor_tile_program.solid_tile_program, + &quad_vertex_positions_buffer, + ); + let alpha_monochrome_tile_vertex_array = AlphaTileVertexArray::new( + &device, + &alpha_monochrome_tile_program.alpha_tile_program, + &quad_vertex_positions_buffer, + ); + let solid_monochrome_tile_vertex_array = SolidTileVertexArray::new( + &device, + &solid_monochrome_tile_program.solid_tile_program, + &quad_vertex_positions_buffer, + ); + let postprocess_vertex_array = PostprocessVertexArray::new( + &device, + &postprocess_program, + &quad_vertex_positions_buffer, + ); let stencil_vertex_array = StencilVertexArray::new(&device, &stencil_program); - let reprojection_vertex_array = - ReprojectionVertexArray::new(&device, - &reprojection_program, - &quad_vertex_positions_buffer); + let reprojection_vertex_array = ReprojectionVertexArray::new( + &device, + &reprojection_program, + &quad_vertex_positions_buffer, + ); - let mask_framebuffer_size = Point2DI32::new(MASK_FRAMEBUFFER_WIDTH, - MASK_FRAMEBUFFER_HEIGHT); - let mask_framebuffer_texture = device.create_texture(TextureFormat::R16F, - mask_framebuffer_size); + let mask_framebuffer_size = + Point2DI32::new(MASK_FRAMEBUFFER_WIDTH, MASK_FRAMEBUFFER_HEIGHT); + let mask_framebuffer_texture = + device.create_texture(TextureFormat::R16F, mask_framebuffer_size); let mask_framebuffer = device.create_framebuffer(mask_framebuffer_texture); - let fill_colors_size = Point2DI32::new(FILL_COLORS_TEXTURE_WIDTH, - FILL_COLORS_TEXTURE_HEIGHT); + let fill_colors_size = + Point2DI32::new(FILL_COLORS_TEXTURE_WIDTH, FILL_COLORS_TEXTURE_HEIGHT); let fill_colors_texture = device.create_texture(TextureFormat::RGBA8, fill_colors_size); let debug_ui = DebugUI::new(&device, resources, dest_framebuffer.window_size(&device)); @@ -207,9 +224,10 @@ impl Renderer where D: Device { pub fn begin_scene(&mut self, scene: &SceneDescriptor) { self.init_postprocessing_framebuffer(); - let timer_query = self.free_timer_queries - .pop() - .unwrap_or_else(|| self.device.create_timer_query()); + let timer_query = self + .free_timer_queries + .pop() + .unwrap_or_else(|| self.device.create_timer_query()); self.device.begin_timer_query(&timer_query); self.current_timer_query = Some(timer_query); @@ -221,7 +239,10 @@ impl Renderer where D: Device { self.mask_framebuffer_cleared = false; - self.stats = RenderStats { object_count: scene.object_count, ..RenderStats::default() }; + self.stats = RenderStats { + object_count: scene.object_count, + ..RenderStats::default() + }; } pub fn render_command(&mut self, command: &RenderCommand) { @@ -261,7 +282,7 @@ impl Renderer where D: Device { pub fn shift_timer_query(&mut self) -> Option { let query = self.pending_timer_queries.front()?; if !self.device.timer_query_is_available(&query) { - return None + return None; } let query = self.pending_timer_queries.pop_front().unwrap(); let result = self.device.get_timer_query(&query); @@ -275,8 +296,10 @@ impl Renderer where D: Device { } #[inline] - pub fn replace_dest_framebuffer(&mut self, new_dest_framebuffer: DestFramebuffer) - -> DestFramebuffer { + pub fn replace_dest_framebuffer( + &mut self, + new_dest_framebuffer: DestFramebuffer, + ) -> DestFramebuffer { mem::replace(&mut self.dest_framebuffer, new_dest_framebuffer) } @@ -314,21 +337,26 @@ impl Renderer where D: Device { fill_colors[shader_index * 4 + 2] = shader.fill_color.b; fill_colors[shader_index * 4 + 3] = shader.fill_color.a; } - self.device.upload_to_texture(&self.fill_colors_texture, size, &fill_colors); + self.device + .upload_to_texture(&self.fill_colors_texture, size, &fill_colors); } fn upload_solid_tiles(&mut self, solid_tiles: &[SolidTileBatchPrimitive]) { - self.device.allocate_buffer(&self.solid_tile_vertex_array().vertex_buffer, - BufferData::Memory(&solid_tiles), - BufferTarget::Vertex, - BufferUploadMode::Dynamic); + self.device.allocate_buffer( + &self.solid_tile_vertex_array().vertex_buffer, + BufferData::Memory(&solid_tiles), + BufferTarget::Vertex, + BufferUploadMode::Dynamic, + ); } fn upload_alpha_tiles(&mut self, alpha_tiles: &[AlphaTileBatchPrimitive]) { - self.device.allocate_buffer(&self.alpha_tile_vertex_array().vertex_buffer, - BufferData::Memory(&alpha_tiles), - BufferTarget::Vertex, - BufferUploadMode::Dynamic); + self.device.allocate_buffer( + &self.alpha_tile_vertex_array().vertex_buffer, + BufferData::Memory(&alpha_tiles), + BufferTarget::Vertex, + BufferUploadMode::Dynamic, + ); } fn clear_mask_framebuffer(&mut self) { @@ -363,10 +391,12 @@ impl Renderer where D: Device { return; } - self.device.allocate_buffer(&self.fill_vertex_array.vertex_buffer, - BufferData::Memory(&self.buffered_fills), - BufferTarget::Vertex, - BufferUploadMode::Dynamic); + self.device.allocate_buffer( + &self.fill_vertex_array.vertex_buffer, + BufferData::Memory(&self.buffered_fills), + BufferTarget::Vertex, + BufferUploadMode::Dynamic, + ); if !self.mask_framebuffer_cleared { self.clear_mask_framebuffer(); @@ -375,30 +405,35 @@ impl Renderer where D: Device { self.device.bind_framebuffer(&self.mask_framebuffer); - self.device.bind_vertex_array(&self.fill_vertex_array.vertex_array); + self.device + .bind_vertex_array(&self.fill_vertex_array.vertex_array); self.device.use_program(&self.fill_program.program); - self.device.set_uniform(&self.fill_program.framebuffer_size_uniform, - UniformData::Vec2(I32x4::new(MASK_FRAMEBUFFER_WIDTH, - MASK_FRAMEBUFFER_HEIGHT, - 0, - 0).to_f32x4())); - self.device.set_uniform(&self.fill_program.tile_size_uniform, - UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, - TILE_HEIGHT as i32, - 0, - 0).to_f32x4())); + self.device.set_uniform( + &self.fill_program.framebuffer_size_uniform, + UniformData::Vec2( + I32x4::new(MASK_FRAMEBUFFER_WIDTH, MASK_FRAMEBUFFER_HEIGHT, 0, 0).to_f32x4(), + ), + ); + self.device.set_uniform( + &self.fill_program.tile_size_uniform, + UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, TILE_HEIGHT as i32, 0, 0).to_f32x4()), + ); self.device.bind_texture(&self.area_lut_texture, 0); - self.device.set_uniform(&self.fill_program.area_lut_uniform, - UniformData::TextureUnit(0)); + self.device.set_uniform( + &self.fill_program.area_lut_uniform, + UniformData::TextureUnit(0), + ); let render_state = RenderState { blend: BlendState::RGBOneAlphaOne, ..RenderState::default() }; debug_assert!(self.buffered_fills.len() <= u32::MAX as usize); - self.device.draw_arrays_instanced(Primitive::TriangleFan, - 4, - self.buffered_fills.len() as u32, - &render_state); + self.device.draw_arrays_instanced( + Primitive::TriangleFan, + 4, + self.buffered_fills.len() as u32, + &render_state, + ); self.buffered_fills.clear() } @@ -409,56 +444,75 @@ impl Renderer where D: Device { let alpha_tile_vertex_array = self.alpha_tile_vertex_array(); let alpha_tile_program = self.alpha_tile_program(); - self.device.bind_vertex_array(&alpha_tile_vertex_array.vertex_array); + self.device + .bind_vertex_array(&alpha_tile_vertex_array.vertex_array); self.device.use_program(&alpha_tile_program.program); - self.device.set_uniform(&alpha_tile_program.framebuffer_size_uniform, - UniformData::Vec2(self.draw_viewport().size().to_f32().0)); - self.device.set_uniform(&alpha_tile_program.tile_size_uniform, - UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, - TILE_HEIGHT as i32, - 0, - 0).to_f32x4())); - self.device.bind_texture(self.device.framebuffer_texture(&self.mask_framebuffer), 0); - self.device.set_uniform(&alpha_tile_program.stencil_texture_uniform, - UniformData::TextureUnit(0)); - self.device.set_uniform(&alpha_tile_program.stencil_texture_size_uniform, - UniformData::Vec2(I32x4::new(MASK_FRAMEBUFFER_WIDTH, - MASK_FRAMEBUFFER_HEIGHT, - 0, - 0).to_f32x4())); + self.device.set_uniform( + &alpha_tile_program.framebuffer_size_uniform, + UniformData::Vec2(self.draw_viewport().size().to_f32().0), + ); + self.device.set_uniform( + &alpha_tile_program.tile_size_uniform, + UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, TILE_HEIGHT as i32, 0, 0).to_f32x4()), + ); + self.device + .bind_texture(self.device.framebuffer_texture(&self.mask_framebuffer), 0); + self.device.set_uniform( + &alpha_tile_program.stencil_texture_uniform, + UniformData::TextureUnit(0), + ); + self.device.set_uniform( + &alpha_tile_program.stencil_texture_size_uniform, + UniformData::Vec2( + I32x4::new(MASK_FRAMEBUFFER_WIDTH, MASK_FRAMEBUFFER_HEIGHT, 0, 0).to_f32x4(), + ), + ); match self.render_mode { RenderMode::Multicolor => { self.device.bind_texture(&self.fill_colors_texture, 1); - self.device.set_uniform(&self.alpha_multicolor_tile_program - .fill_colors_texture_uniform, - UniformData::TextureUnit(1)); - self.device.set_uniform(&self.alpha_multicolor_tile_program - .fill_colors_texture_size_uniform, - UniformData::Vec2(I32x4::new(FILL_COLORS_TEXTURE_WIDTH, - FILL_COLORS_TEXTURE_HEIGHT, - 0, - 0).to_f32x4())); + self.device.set_uniform( + &self + .alpha_multicolor_tile_program + .fill_colors_texture_uniform, + UniformData::TextureUnit(1), + ); + self.device.set_uniform( + &self + .alpha_multicolor_tile_program + .fill_colors_texture_size_uniform, + UniformData::Vec2( + I32x4::new(FILL_COLORS_TEXTURE_WIDTH, FILL_COLORS_TEXTURE_HEIGHT, 0, 0) + .to_f32x4(), + ), + ); } RenderMode::Monochrome { .. } if self.postprocessing_needed() => { - self.device.set_uniform(&self.alpha_monochrome_tile_program.fill_color_uniform, - UniformData::Vec4(F32x4::splat(1.0))); + self.device.set_uniform( + &self.alpha_monochrome_tile_program.fill_color_uniform, + UniformData::Vec4(F32x4::splat(1.0)), + ); } RenderMode::Monochrome { fg_color, .. } => { - self.device.set_uniform(&self.alpha_monochrome_tile_program.fill_color_uniform, - UniformData::Vec4(fg_color.0)); + self.device.set_uniform( + &self.alpha_monochrome_tile_program.fill_color_uniform, + UniformData::Vec4(fg_color.0), + ); } } // FIXME(pcwalton): Fill this in properly! - self.device.set_uniform(&alpha_tile_program.view_box_origin_uniform, - UniformData::Vec2(F32x4::default())); + self.device.set_uniform( + &alpha_tile_program.view_box_origin_uniform, + UniformData::Vec2(F32x4::default()), + ); let render_state = RenderState { blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha, stencil: self.stencil_state(), ..RenderState::default() }; - self.device.draw_arrays_instanced(Primitive::TriangleFan, 4, count, &render_state); + self.device + .draw_arrays_instanced(Primitive::TriangleFan, 4, count, &render_state); } fn draw_solid_tiles(&mut self, count: u32) { @@ -467,47 +521,62 @@ impl Renderer where D: Device { let solid_tile_vertex_array = self.solid_tile_vertex_array(); let solid_tile_program = self.solid_tile_program(); - self.device.bind_vertex_array(&solid_tile_vertex_array.vertex_array); + self.device + .bind_vertex_array(&solid_tile_vertex_array.vertex_array); self.device.use_program(&solid_tile_program.program); - self.device.set_uniform(&solid_tile_program.framebuffer_size_uniform, - UniformData::Vec2(self.draw_viewport().size().0.to_f32x4())); - self.device.set_uniform(&solid_tile_program.tile_size_uniform, - UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, - TILE_HEIGHT as i32, - 0, - 0).to_f32x4())); + self.device.set_uniform( + &solid_tile_program.framebuffer_size_uniform, + UniformData::Vec2(self.draw_viewport().size().0.to_f32x4()), + ); + self.device.set_uniform( + &solid_tile_program.tile_size_uniform, + UniformData::Vec2(I32x4::new(TILE_WIDTH as i32, TILE_HEIGHT as i32, 0, 0).to_f32x4()), + ); match self.render_mode { RenderMode::Multicolor => { self.device.bind_texture(&self.fill_colors_texture, 0); - self.device.set_uniform(&self.solid_multicolor_tile_program - .fill_colors_texture_uniform, - UniformData::TextureUnit(0)); - self.device.set_uniform(&self.solid_multicolor_tile_program - .fill_colors_texture_size_uniform, - UniformData::Vec2(I32x4::new(FILL_COLORS_TEXTURE_WIDTH, - FILL_COLORS_TEXTURE_HEIGHT, - 0, - 0).to_f32x4())); + self.device.set_uniform( + &self + .solid_multicolor_tile_program + .fill_colors_texture_uniform, + UniformData::TextureUnit(0), + ); + self.device.set_uniform( + &self + .solid_multicolor_tile_program + .fill_colors_texture_size_uniform, + UniformData::Vec2( + I32x4::new(FILL_COLORS_TEXTURE_WIDTH, FILL_COLORS_TEXTURE_HEIGHT, 0, 0) + .to_f32x4(), + ), + ); } RenderMode::Monochrome { .. } if self.postprocessing_needed() => { - self.device.set_uniform(&self.solid_monochrome_tile_program.fill_color_uniform, - UniformData::Vec4(F32x4::splat(1.0))); + self.device.set_uniform( + &self.solid_monochrome_tile_program.fill_color_uniform, + UniformData::Vec4(F32x4::splat(1.0)), + ); } RenderMode::Monochrome { fg_color, .. } => { - self.device.set_uniform(&self.solid_monochrome_tile_program.fill_color_uniform, - UniformData::Vec4(fg_color.0)); + self.device.set_uniform( + &self.solid_monochrome_tile_program.fill_color_uniform, + UniformData::Vec4(fg_color.0), + ); } } // FIXME(pcwalton): Fill this in properly! - self.device.set_uniform(&solid_tile_program.view_box_origin_uniform, - UniformData::Vec2(F32x4::default())); + self.device.set_uniform( + &solid_tile_program.view_box_origin_uniform, + UniformData::Vec2(F32x4::default()), + ); let render_state = RenderState { stencil: self.stencil_state(), ..RenderState::default() }; - self.device.draw_arrays_instanced(Primitive::TriangleFan, 4, count, &render_state); + self.device + .draw_arrays_instanced(Primitive::TriangleFan, 4, count, &render_state); } fn postprocess(&mut self) { @@ -529,55 +598,73 @@ impl Renderer where D: Device { self.bind_dest_framebuffer(); - self.device.bind_vertex_array(&self.postprocess_vertex_array.vertex_array); + self.device + .bind_vertex_array(&self.postprocess_vertex_array.vertex_array); self.device.use_program(&self.postprocess_program.program); - self.device.set_uniform(&self.postprocess_program.framebuffer_size_uniform, - UniformData::Vec2(self.main_viewport().size().to_f32().0)); + self.device.set_uniform( + &self.postprocess_program.framebuffer_size_uniform, + UniformData::Vec2(self.main_viewport().size().to_f32().0), + ); match defringing_kernel { Some(ref kernel) => { - self.device.set_uniform(&self.postprocess_program.kernel_uniform, - UniformData::Vec4(F32x4::from_slice(&kernel.0))); + self.device.set_uniform( + &self.postprocess_program.kernel_uniform, + UniformData::Vec4(F32x4::from_slice(&kernel.0)), + ); } None => { - self.device.set_uniform(&self.postprocess_program.kernel_uniform, - UniformData::Vec4(F32x4::default())); + self.device.set_uniform( + &self.postprocess_program.kernel_uniform, + UniformData::Vec4(F32x4::default()), + ); } } let postprocess_source_framebuffer = self.postprocess_source_framebuffer.as_ref().unwrap(); - let source_texture = self.device.framebuffer_texture(postprocess_source_framebuffer); + let source_texture = self + .device + .framebuffer_texture(postprocess_source_framebuffer); let source_texture_size = self.device.texture_size(source_texture); self.device.bind_texture(&source_texture, 0); - self.device.set_uniform(&self.postprocess_program.source_uniform, - UniformData::TextureUnit(0)); - self.device.set_uniform(&self.postprocess_program.source_size_uniform, - UniformData::Vec2(source_texture_size.0.to_f32x4())); + self.device.set_uniform( + &self.postprocess_program.source_uniform, + UniformData::TextureUnit(0), + ); + self.device.set_uniform( + &self.postprocess_program.source_size_uniform, + UniformData::Vec2(source_texture_size.0.to_f32x4()), + ); self.device.bind_texture(&self.gamma_lut_texture, 1); - self.device.set_uniform(&self.postprocess_program.gamma_lut_uniform, - UniformData::TextureUnit(1)); - self.device.set_uniform(&self.postprocess_program.fg_color_uniform, - UniformData::Vec4(fg_color.0)); - self.device.set_uniform(&self.postprocess_program.bg_color_uniform, - UniformData::Vec4(bg_color.0)); - self.device.set_uniform(&self.postprocess_program.gamma_correction_enabled_uniform, - UniformData::Int(gamma_correction_enabled as i32)); - self.device.draw_arrays(Primitive::TriangleFan, 4, &RenderState::default()); + self.device.set_uniform( + &self.postprocess_program.gamma_lut_uniform, + UniformData::TextureUnit(1), + ); + self.device.set_uniform( + &self.postprocess_program.fg_color_uniform, + UniformData::Vec4(fg_color.0), + ); + self.device.set_uniform( + &self.postprocess_program.bg_color_uniform, + UniformData::Vec4(bg_color.0), + ); + self.device.set_uniform( + &self.postprocess_program.gamma_correction_enabled_uniform, + UniformData::Int(gamma_correction_enabled as i32), + ); + self.device + .draw_arrays(Primitive::TriangleFan, 4, &RenderState::default()); } fn solid_tile_program(&self) -> &SolidTileProgram { match self.render_mode { - RenderMode::Monochrome { .. } => { - &self.solid_monochrome_tile_program.solid_tile_program - } + RenderMode::Monochrome { .. } => &self.solid_monochrome_tile_program.solid_tile_program, RenderMode::Multicolor => &self.solid_multicolor_tile_program.solid_tile_program, } } fn alpha_tile_program(&self) -> &AlphaTileProgram { match self.render_mode { - RenderMode::Monochrome { .. } => { - &self.alpha_monochrome_tile_program.alpha_tile_program - } + RenderMode::Monochrome { .. } => &self.alpha_monochrome_tile_program.alpha_tile_program, RenderMode::Multicolor => &self.alpha_multicolor_tile_program.alpha_tile_program, } } @@ -597,53 +684,80 @@ impl Renderer where D: Device { } fn draw_stencil(&self, quad_positions: &[Point3DF32]) { - self.device.allocate_buffer(&self.stencil_vertex_array.vertex_buffer, - BufferData::Memory(quad_positions), - BufferTarget::Vertex, - BufferUploadMode::Dynamic); + self.device.allocate_buffer( + &self.stencil_vertex_array.vertex_buffer, + BufferData::Memory(quad_positions), + BufferTarget::Vertex, + BufferUploadMode::Dynamic, + ); self.bind_draw_framebuffer(); - self.device.bind_vertex_array(&self.stencil_vertex_array.vertex_array); + self.device + .bind_vertex_array(&self.stencil_vertex_array.vertex_array); self.device.use_program(&self.stencil_program.program); - self.device.draw_arrays(Primitive::TriangleFan, 4, &RenderState { - // FIXME(pcwalton): Should we really write to the depth buffer? - depth: Some(DepthState { func: DepthFunc::Less, write: true }), - stencil: Some(StencilState { - func: StencilFunc::Always, - reference: 1, - mask: 1, - write: true, - }), - color_mask: false, - ..RenderState::default() - }) + self.device.draw_arrays( + Primitive::TriangleFan, + 4, + &RenderState { + // FIXME(pcwalton): Should we really write to the depth buffer? + depth: Some(DepthState { + func: DepthFunc::Less, + write: true, + }), + stencil: Some(StencilState { + func: StencilFunc::Always, + reference: 1, + mask: 1, + write: true, + }), + color_mask: false, + ..RenderState::default() + }, + ) } - pub fn reproject_texture(&self, - texture: &D::Texture, - old_transform: &Transform3DF32, - new_transform: &Transform3DF32) { + pub fn reproject_texture( + &self, + texture: &D::Texture, + old_transform: &Transform3DF32, + new_transform: &Transform3DF32, + ) { self.bind_draw_framebuffer(); - self.device.bind_vertex_array(&self.reprojection_vertex_array.vertex_array); + self.device + .bind_vertex_array(&self.reprojection_vertex_array.vertex_array); self.device.use_program(&self.reprojection_program.program); - self.device.set_uniform(&self.reprojection_program.old_transform_uniform, - UniformData::from_transform_3d(old_transform)); - self.device.set_uniform(&self.reprojection_program.new_transform_uniform, - UniformData::from_transform_3d(new_transform)); + self.device.set_uniform( + &self.reprojection_program.old_transform_uniform, + UniformData::from_transform_3d(old_transform), + ); + self.device.set_uniform( + &self.reprojection_program.new_transform_uniform, + UniformData::from_transform_3d(new_transform), + ); self.device.bind_texture(texture, 0); - self.device.set_uniform(&self.reprojection_program.texture_uniform, - UniformData::TextureUnit(0)); - self.device.draw_arrays(Primitive::TriangleFan, 4, &RenderState { - blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha, - depth: Some(DepthState { func: DepthFunc::Less, write: false }), - ..RenderState::default() - }); + self.device.set_uniform( + &self.reprojection_program.texture_uniform, + UniformData::TextureUnit(0), + ); + self.device.draw_arrays( + Primitive::TriangleFan, + 4, + &RenderState { + blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha, + depth: Some(DepthState { + func: DepthFunc::Less, + write: false, + }), + ..RenderState::default() + }, + ); } pub fn bind_draw_framebuffer(&self) { if self.postprocessing_needed() { - self.device.bind_framebuffer(self.postprocess_source_framebuffer.as_ref().unwrap()); + self.device + .bind_framebuffer(self.postprocess_source_framebuffer.as_ref().unwrap()); } else { self.bind_dest_framebuffer(); } @@ -654,9 +768,7 @@ impl Renderer where D: Device { DestFramebuffer::Default { viewport, .. } => { self.device.bind_default_framebuffer(viewport) } - DestFramebuffer::Other(ref framebuffer) => { - self.device.bind_framebuffer(framebuffer) - } + DestFramebuffer::Other(ref framebuffer) => self.device.bind_framebuffer(framebuffer), } } @@ -668,17 +780,21 @@ impl Renderer where D: Device { let source_framebuffer_size = self.draw_viewport().size(); match self.postprocess_source_framebuffer { - Some(ref framebuffer) if - self.device.texture_size(self.device.framebuffer_texture(framebuffer)) == - source_framebuffer_size => {} + Some(ref framebuffer) + if self + .device + .texture_size(self.device.framebuffer_texture(framebuffer)) + == source_framebuffer_size => {} _ => { - let texture = self.device.create_texture(TextureFormat::R8, - source_framebuffer_size); + let texture = self + .device + .create_texture(TextureFormat::R8, source_framebuffer_size); self.postprocess_source_framebuffer = Some(self.device.create_framebuffer(texture)) } }; - self.device.bind_framebuffer(self.postprocess_source_framebuffer.as_ref().unwrap()); + self.device + .bind_framebuffer(self.postprocess_source_framebuffer.as_ref().unwrap()); self.device.clear(&ClearParams { color: Some(ColorF::transparent_black()), ..ClearParams::default() @@ -687,9 +803,11 @@ impl Renderer where D: Device { fn postprocessing_needed(&self) -> bool { match self.render_mode { - RenderMode::Monochrome { ref defringing_kernel, gamma_correction, .. } => { - defringing_kernel.is_some() || gamma_correction - } + RenderMode::Monochrome { + ref defringing_kernel, + gamma_correction, + .. + } => defringing_kernel.is_some() || gamma_correction, _ => false, } } @@ -699,17 +817,25 @@ impl Renderer where D: Device { return None; } - Some(StencilState { func: StencilFunc::Equal, reference: 1, mask: 1, write: false }) + Some(StencilState { + func: StencilFunc::Equal, + reference: 1, + mask: 1, + write: false, + }) } fn draw_viewport(&self) -> RectI32 { let main_viewport = self.main_viewport(); match self.render_mode { - RenderMode::Monochrome { defringing_kernel: Some(..), .. } => { + RenderMode::Monochrome { + defringing_kernel: Some(..), + .. + } => { let scale = Point2DI32::new(3, 1); RectI32::new(Point2DI32::default(), main_viewport.size().scale_xy(scale)) } - _ => main_viewport + _ => main_viewport, } } @@ -717,30 +843,43 @@ impl Renderer where D: Device { match self.dest_framebuffer { DestFramebuffer::Default { viewport, .. } => viewport, DestFramebuffer::Other(ref framebuffer) => { - let size = self.device.texture_size(self.device.framebuffer_texture(framebuffer)); + let size = self + .device + .texture_size(self.device.framebuffer_texture(framebuffer)); RectI32::new(Point2DI32::default(), size) } } } } -struct FillVertexArray where D: Device { +struct FillVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, vertex_buffer: D::Buffer, } -impl FillVertexArray where D: Device { - fn new(device: &D, fill_program: &FillProgram, quad_vertex_positions_buffer: &D::Buffer) - -> FillVertexArray { +impl FillVertexArray +where + D: Device, +{ + fn new( + device: &D, + fill_program: &FillProgram, + quad_vertex_positions_buffer: &D::Buffer, + ) -> FillVertexArray { let vertex_array = device.create_vertex_array(); let vertex_buffer = device.create_buffer(); let vertex_buffer_data: BufferData = BufferData::Uninitialized(MAX_FILLS_PER_BATCH); - device.allocate_buffer(&vertex_buffer, - vertex_buffer_data, - BufferTarget::Vertex, - BufferUploadMode::Dynamic); + device.allocate_buffer( + &vertex_buffer, + vertex_buffer_data, + BufferTarget::Vertex, + BufferUploadMode::Dynamic, + ); let tess_coord_attr = device.get_vertex_attr(&fill_program.program, "TessCoord"); let from_px_attr = device.get_vertex_attr(&fill_program.program, "FromPx"); @@ -752,61 +891,75 @@ impl FillVertexArray where D: Device { device.bind_vertex_array(&vertex_array); device.use_program(&fill_program.program); device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&tess_coord_attr, - 2, - VertexAttrType::U8, - false, - 0, - 0, - 0); + device.configure_float_vertex_attr(&tess_coord_attr, 2, VertexAttrType::U8, false, 0, 0, 0); device.bind_buffer(&vertex_buffer, BufferTarget::Vertex); - device.configure_int_vertex_attr(&from_px_attr, - 1, - VertexAttrType::U8, - FILL_INSTANCE_SIZE, - 0, - 1); - device.configure_int_vertex_attr(&to_px_attr, - 1, - VertexAttrType::U8, - FILL_INSTANCE_SIZE, - 1, - 1); - device.configure_float_vertex_attr(&from_subpx_attr, - 2, - VertexAttrType::U8, - true, - FILL_INSTANCE_SIZE, - 2, - 1); - device.configure_float_vertex_attr(&to_subpx_attr, - 2, - VertexAttrType::U8, - true, - FILL_INSTANCE_SIZE, - 4, - 1); - device.configure_int_vertex_attr(&tile_index_attr, - 1, - VertexAttrType::U16, - FILL_INSTANCE_SIZE, - 6, - 1); + device.configure_int_vertex_attr( + &from_px_attr, + 1, + VertexAttrType::U8, + FILL_INSTANCE_SIZE, + 0, + 1, + ); + device.configure_int_vertex_attr( + &to_px_attr, + 1, + VertexAttrType::U8, + FILL_INSTANCE_SIZE, + 1, + 1, + ); + device.configure_float_vertex_attr( + &from_subpx_attr, + 2, + VertexAttrType::U8, + true, + FILL_INSTANCE_SIZE, + 2, + 1, + ); + device.configure_float_vertex_attr( + &to_subpx_attr, + 2, + VertexAttrType::U8, + true, + FILL_INSTANCE_SIZE, + 4, + 1, + ); + device.configure_int_vertex_attr( + &tile_index_attr, + 1, + VertexAttrType::U16, + FILL_INSTANCE_SIZE, + 6, + 1, + ); - FillVertexArray { vertex_array, vertex_buffer } + FillVertexArray { + vertex_array, + vertex_buffer, + } } } -struct AlphaTileVertexArray where D: Device { +struct AlphaTileVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, vertex_buffer: D::Buffer, } -impl AlphaTileVertexArray where D: Device { - fn new(device: &D, - alpha_tile_program: &AlphaTileProgram, - quad_vertex_positions_buffer: &D::Buffer) - -> AlphaTileVertexArray { +impl AlphaTileVertexArray +where + D: Device, +{ + fn new( + device: &D, + alpha_tile_program: &AlphaTileProgram, + quad_vertex_positions_buffer: &D::Buffer, + ) -> AlphaTileVertexArray { let (vertex_array, vertex_buffer) = (device.create_vertex_array(), device.create_buffer()); let tess_coord_attr = device.get_vertex_attr(&alpha_tile_program.program, "TessCoord"); @@ -820,53 +973,65 @@ impl AlphaTileVertexArray where D: Device { device.bind_vertex_array(&vertex_array); device.use_program(&alpha_tile_program.program); device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&tess_coord_attr, - 2, - VertexAttrType::U8, - false, - 0, - 0, - 0); + device.configure_float_vertex_attr(&tess_coord_attr, 2, VertexAttrType::U8, false, 0, 0, 0); device.bind_buffer(&vertex_buffer, BufferTarget::Vertex); - device.configure_int_vertex_attr(&tile_origin_attr, - 3, - VertexAttrType::U8, - MASK_TILE_INSTANCE_SIZE, - 0, - 1); - device.configure_int_vertex_attr(&backdrop_attr, - 1, - VertexAttrType::I8, - MASK_TILE_INSTANCE_SIZE, - 3, - 1); - device.configure_int_vertex_attr(&object_attr, - 2, - VertexAttrType::I16, - MASK_TILE_INSTANCE_SIZE, - 4, - 1); - device.configure_int_vertex_attr(&tile_index_attr, - 2, - VertexAttrType::I16, - MASK_TILE_INSTANCE_SIZE, - 6, - 1); + device.configure_int_vertex_attr( + &tile_origin_attr, + 3, + VertexAttrType::U8, + MASK_TILE_INSTANCE_SIZE, + 0, + 1, + ); + device.configure_int_vertex_attr( + &backdrop_attr, + 1, + VertexAttrType::I8, + MASK_TILE_INSTANCE_SIZE, + 3, + 1, + ); + device.configure_int_vertex_attr( + &object_attr, + 2, + VertexAttrType::I16, + MASK_TILE_INSTANCE_SIZE, + 4, + 1, + ); + device.configure_int_vertex_attr( + &tile_index_attr, + 2, + VertexAttrType::I16, + MASK_TILE_INSTANCE_SIZE, + 6, + 1, + ); - AlphaTileVertexArray { vertex_array, vertex_buffer } + AlphaTileVertexArray { + vertex_array, + vertex_buffer, + } } } -struct SolidTileVertexArray where D: Device { +struct SolidTileVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, vertex_buffer: D::Buffer, } -impl SolidTileVertexArray where D: Device { - fn new(device: &D, - solid_tile_program: &SolidTileProgram, - quad_vertex_positions_buffer: &D::Buffer) - -> SolidTileVertexArray { +impl SolidTileVertexArray +where + D: Device, +{ + fn new( + device: &D, + solid_tile_program: &SolidTileProgram, + quad_vertex_positions_buffer: &D::Buffer, + ) -> SolidTileVertexArray { let (vertex_array, vertex_buffer) = (device.create_vertex_array(), device.create_buffer()); let tess_coord_attr = device.get_vertex_attr(&solid_tile_program.program, "TessCoord"); @@ -878,62 +1043,82 @@ impl SolidTileVertexArray where D: Device { device.bind_vertex_array(&vertex_array); device.use_program(&solid_tile_program.program); device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&tess_coord_attr, - 2, - VertexAttrType::U8, - false, - 0, - 0, - 0); + device.configure_float_vertex_attr(&tess_coord_attr, 2, VertexAttrType::U8, false, 0, 0, 0); device.bind_buffer(&vertex_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&tile_origin_attr, - 2, - VertexAttrType::I16, - false, - SOLID_TILE_INSTANCE_SIZE, - 0, - 1); - device.configure_int_vertex_attr(&object_attr, - 1, - VertexAttrType::I16, - SOLID_TILE_INSTANCE_SIZE, - 4, - 1); + device.configure_float_vertex_attr( + &tile_origin_attr, + 2, + VertexAttrType::I16, + false, + SOLID_TILE_INSTANCE_SIZE, + 0, + 1, + ); + device.configure_int_vertex_attr( + &object_attr, + 1, + VertexAttrType::I16, + SOLID_TILE_INSTANCE_SIZE, + 4, + 1, + ); - SolidTileVertexArray { vertex_array, vertex_buffer } + SolidTileVertexArray { + vertex_array, + vertex_buffer, + } } } -struct FillProgram where D: Device { +struct FillProgram +where + D: Device, +{ program: D::Program, framebuffer_size_uniform: D::Uniform, tile_size_uniform: D::Uniform, area_lut_uniform: D::Uniform, } -impl FillProgram where D: Device { +impl FillProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> FillProgram { let program = device.create_program(resources, "fill"); let framebuffer_size_uniform = device.get_uniform(&program, "FramebufferSize"); let tile_size_uniform = device.get_uniform(&program, "TileSize"); let area_lut_uniform = device.get_uniform(&program, "AreaLUT"); - FillProgram { program, framebuffer_size_uniform, tile_size_uniform, area_lut_uniform } + FillProgram { + program, + framebuffer_size_uniform, + tile_size_uniform, + area_lut_uniform, + } } } -struct SolidTileProgram where D: Device { +struct SolidTileProgram +where + D: Device, +{ program: D::Program, framebuffer_size_uniform: D::Uniform, tile_size_uniform: D::Uniform, view_box_origin_uniform: D::Uniform, } -impl SolidTileProgram where D: Device { +impl SolidTileProgram +where + D: Device, +{ fn new(device: &D, program_name: &str, resources: &dyn ResourceLoader) -> SolidTileProgram { - let program = device.create_program_from_shader_names(resources, - program_name, - program_name, - "tile_solid"); + let program = device.create_program_from_shader_names( + resources, + program_name, + program_name, + "tile_solid", + ); let framebuffer_size_uniform = device.get_uniform(&program, "FramebufferSize"); let tile_size_uniform = device.get_uniform(&program, "TileSize"); let view_box_origin_uniform = device.get_uniform(&program, "ViewBoxOrigin"); @@ -946,19 +1131,25 @@ impl SolidTileProgram where D: Device { } } -struct SolidTileMulticolorProgram where D: Device { +struct SolidTileMulticolorProgram +where + D: Device, +{ solid_tile_program: SolidTileProgram, fill_colors_texture_uniform: D::Uniform, fill_colors_texture_size_uniform: D::Uniform, } -impl SolidTileMulticolorProgram where D: Device { +impl SolidTileMulticolorProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> SolidTileMulticolorProgram { let solid_tile_program = SolidTileProgram::new(device, "tile_solid_multicolor", resources); - let fill_colors_texture_uniform = device.get_uniform(&solid_tile_program.program, - "FillColorsTexture"); - let fill_colors_texture_size_uniform = device.get_uniform(&solid_tile_program.program, - "FillColorsTextureSize"); + let fill_colors_texture_uniform = + device.get_uniform(&solid_tile_program.program, "FillColorsTexture"); + let fill_colors_texture_size_uniform = + device.get_uniform(&solid_tile_program.program, "FillColorsTextureSize"); SolidTileMulticolorProgram { solid_tile_program, fill_colors_texture_uniform, @@ -967,20 +1158,32 @@ impl SolidTileMulticolorProgram where D: Device { } } -struct SolidTileMonochromeProgram where D: Device { +struct SolidTileMonochromeProgram +where + D: Device, +{ solid_tile_program: SolidTileProgram, fill_color_uniform: D::Uniform, } -impl SolidTileMonochromeProgram where D: Device { +impl SolidTileMonochromeProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> SolidTileMonochromeProgram { let solid_tile_program = SolidTileProgram::new(device, "tile_solid_monochrome", resources); let fill_color_uniform = device.get_uniform(&solid_tile_program.program, "FillColor"); - SolidTileMonochromeProgram { solid_tile_program, fill_color_uniform } + SolidTileMonochromeProgram { + solid_tile_program, + fill_color_uniform, + } } } -struct AlphaTileProgram where D: Device { +struct AlphaTileProgram +where + D: Device, +{ program: D::Program, framebuffer_size_uniform: D::Uniform, tile_size_uniform: D::Uniform, @@ -989,12 +1192,17 @@ struct AlphaTileProgram where D: Device { view_box_origin_uniform: D::Uniform, } -impl AlphaTileProgram where D: Device { +impl AlphaTileProgram +where + D: Device, +{ fn new(device: &D, program_name: &str, resources: &dyn ResourceLoader) -> AlphaTileProgram { - let program = device.create_program_from_shader_names(resources, - program_name, - program_name, - "tile_alpha"); + let program = device.create_program_from_shader_names( + resources, + program_name, + program_name, + "tile_alpha", + ); let framebuffer_size_uniform = device.get_uniform(&program, "FramebufferSize"); let tile_size_uniform = device.get_uniform(&program, "TileSize"); let stencil_texture_uniform = device.get_uniform(&program, "StencilTexture"); @@ -1011,19 +1219,25 @@ impl AlphaTileProgram where D: Device { } } -struct AlphaTileMulticolorProgram where D: Device { +struct AlphaTileMulticolorProgram +where + D: Device, +{ alpha_tile_program: AlphaTileProgram, fill_colors_texture_uniform: D::Uniform, fill_colors_texture_size_uniform: D::Uniform, } -impl AlphaTileMulticolorProgram where D: Device { +impl AlphaTileMulticolorProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> AlphaTileMulticolorProgram { let alpha_tile_program = AlphaTileProgram::new(device, "tile_alpha_multicolor", resources); - let fill_colors_texture_uniform = device.get_uniform(&alpha_tile_program.program, - "FillColorsTexture"); - let fill_colors_texture_size_uniform = device.get_uniform(&alpha_tile_program.program, - "FillColorsTextureSize"); + let fill_colors_texture_uniform = + device.get_uniform(&alpha_tile_program.program, "FillColorsTexture"); + let fill_colors_texture_size_uniform = + device.get_uniform(&alpha_tile_program.program, "FillColorsTextureSize"); AlphaTileMulticolorProgram { alpha_tile_program, fill_colors_texture_uniform, @@ -1032,20 +1246,32 @@ impl AlphaTileMulticolorProgram where D: Device { } } -struct AlphaTileMonochromeProgram where D: Device { +struct AlphaTileMonochromeProgram +where + D: Device, +{ alpha_tile_program: AlphaTileProgram, fill_color_uniform: D::Uniform, } -impl AlphaTileMonochromeProgram where D: Device { +impl AlphaTileMonochromeProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> AlphaTileMonochromeProgram { let alpha_tile_program = AlphaTileProgram::new(device, "tile_alpha_monochrome", resources); let fill_color_uniform = device.get_uniform(&alpha_tile_program.program, "FillColor"); - AlphaTileMonochromeProgram { alpha_tile_program, fill_color_uniform } + AlphaTileMonochromeProgram { + alpha_tile_program, + fill_color_uniform, + } } } -struct PostprocessProgram where D: Device { +struct PostprocessProgram +where + D: Device, +{ program: D::Program, source_uniform: D::Uniform, source_size_uniform: D::Uniform, @@ -1057,7 +1283,10 @@ struct PostprocessProgram where D: Device { bg_color_uniform: D::Uniform, } -impl PostprocessProgram where D: Device { +impl PostprocessProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> PostprocessProgram { let program = device.create_program(resources, "post"); let source_uniform = device.get_uniform(&program, "Source"); @@ -1065,8 +1294,8 @@ impl PostprocessProgram where D: Device { let framebuffer_size_uniform = device.get_uniform(&program, "FramebufferSize"); let kernel_uniform = device.get_uniform(&program, "Kernel"); let gamma_lut_uniform = device.get_uniform(&program, "GammaLUT"); - let gamma_correction_enabled_uniform = device.get_uniform(&program, - "GammaCorrectionEnabled"); + let gamma_correction_enabled_uniform = + device.get_uniform(&program, "GammaCorrectionEnabled"); let fg_color_uniform = device.get_uniform(&program, "FGColor"); let bg_color_uniform = device.get_uniform(&program, "BGColor"); PostprocessProgram { @@ -1083,50 +1312,63 @@ impl PostprocessProgram where D: Device { } } -struct PostprocessVertexArray where D: Device { +struct PostprocessVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, } -impl PostprocessVertexArray where D: Device { - fn new(device: &D, - postprocess_program: &PostprocessProgram, - quad_vertex_positions_buffer: &D::Buffer) - -> PostprocessVertexArray { +impl PostprocessVertexArray +where + D: Device, +{ + fn new( + device: &D, + postprocess_program: &PostprocessProgram, + quad_vertex_positions_buffer: &D::Buffer, + ) -> PostprocessVertexArray { let vertex_array = device.create_vertex_array(); let position_attr = device.get_vertex_attr(&postprocess_program.program, "Position"); device.bind_vertex_array(&vertex_array); device.use_program(&postprocess_program.program); device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&position_attr, - 2, - VertexAttrType::U8, - false, - 0, - 0, - 0); + device.configure_float_vertex_attr(&position_attr, 2, VertexAttrType::U8, false, 0, 0, 0); PostprocessVertexArray { vertex_array } } } -struct StencilProgram where D: Device { +struct StencilProgram +where + D: Device, +{ program: D::Program, } -impl StencilProgram where D: Device { +impl StencilProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> StencilProgram { let program = device.create_program(resources, "stencil"); StencilProgram { program } } } -struct StencilVertexArray where D: Device { +struct StencilVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, vertex_buffer: D::Buffer, } -impl StencilVertexArray where D: Device { +impl StencilVertexArray +where + D: Device, +{ fn new(device: &D, stencil_program: &StencilProgram) -> StencilVertexArray { let (vertex_array, vertex_buffer) = (device.create_vertex_array(), device.create_buffer()); @@ -1135,26 +1377,37 @@ impl StencilVertexArray where D: Device { device.bind_vertex_array(&vertex_array); device.use_program(&stencil_program.program); device.bind_buffer(&vertex_buffer, BufferTarget::Vertex); - device.configure_float_vertex_attr(&position_attr, - 3, - VertexAttrType::F32, - false, - 4 * 4, - 0, - 0); + device.configure_float_vertex_attr( + &position_attr, + 3, + VertexAttrType::F32, + false, + 4 * 4, + 0, + 0, + ); - StencilVertexArray { vertex_array, vertex_buffer } + StencilVertexArray { + vertex_array, + vertex_buffer, + } } } -struct ReprojectionProgram where D: Device { +struct ReprojectionProgram +where + D: Device, +{ program: D::Program, old_transform_uniform: D::Uniform, new_transform_uniform: D::Uniform, texture_uniform: D::Uniform, } -impl ReprojectionProgram where D: Device { +impl ReprojectionProgram +where + D: Device, +{ fn new(device: &D, resources: &dyn ResourceLoader) -> ReprojectionProgram { let program = device.create_program(resources, "reproject"); let old_transform_uniform = device.get_uniform(&program, "OldTransform"); @@ -1170,15 +1423,22 @@ impl ReprojectionProgram where D: Device { } } -struct ReprojectionVertexArray where D: Device { +struct ReprojectionVertexArray +where + D: Device, +{ vertex_array: D::VertexArray, } -impl ReprojectionVertexArray where D: Device { - fn new(device: &D, - reprojection_program: &ReprojectionProgram, - quad_vertex_positions_buffer: &D::Buffer) - -> ReprojectionVertexArray { +impl ReprojectionVertexArray +where + D: Device, +{ + fn new( + device: &D, + reprojection_program: &ReprojectionProgram, + quad_vertex_positions_buffer: &D::Buffer, + ) -> ReprojectionVertexArray { let vertex_array = device.create_vertex_array(); let position_attr = device.get_vertex_attr(&reprojection_program.program, "Position"); @@ -1193,12 +1453,21 @@ impl ReprojectionVertexArray where D: Device { } #[derive(Clone)] -pub enum DestFramebuffer where D: Device { - Default { viewport: RectI32, window_size: Point2DI32 }, +pub enum DestFramebuffer +where + D: Device, +{ + Default { + viewport: RectI32, + window_size: Point2DI32, + }, Other(D::Framebuffer), } -impl DestFramebuffer where D: Device { +impl DestFramebuffer +where + D: Device, +{ fn window_size(&self, device: &D) -> Point2DI32 { match *self { DestFramebuffer::Default { window_size, .. } => window_size, diff --git a/renderer/src/gpu_data.rs b/renderer/src/gpu_data.rs index 6f61dcdd..465f58a8 100644 --- a/renderer/src/gpu_data.rs +++ b/renderer/src/gpu_data.rs @@ -86,7 +86,12 @@ impl BuiltObject { pub(crate) fn new(bounds: RectF32) -> BuiltObject { let tile_rect = tiles::round_rect_out_to_tile_bounds(bounds); let tiles = DenseTileMap::new(tile_rect); - BuiltObject { bounds, fills: vec![], alpha_tiles: vec![], tiles } + BuiltObject { + bounds, + fills: vec![], + alpha_tiles: vec![], + tiles, + } } #[inline] @@ -94,10 +99,12 @@ impl BuiltObject { self.tiles.rect } - fn add_fill(&mut self, - builder: &SceneBuilder, - segment: &LineSegmentF32, - tile_coords: Point2DI32) { + fn add_fill( + &mut self, + builder: &SceneBuilder, + segment: &LineSegmentF32, + tile_coords: Point2DI32, + ) { debug!("add_fill({:?} ({:?}))", segment, tile_coords); // Ensure this fill is in bounds. If not, cull it. @@ -107,22 +114,30 @@ impl BuiltObject { debug_assert_eq!(TILE_WIDTH, TILE_HEIGHT); let tile_size = F32x4::splat(TILE_WIDTH as f32); - let (min, max) = (F32x4::default(), F32x4::splat((TILE_WIDTH * 256 - 1) as f32)); + let (min, max) = ( + F32x4::default(), + F32x4::splat((TILE_WIDTH * 256 - 1) as f32), + ); let shuffle_mask = I32x4::new(0x0c08_0400, 0x0d05_0901, 0, 0).as_u8x16(); let tile_upper_left = tile_coords.to_f32().0.xyxy() * tile_size; let segment = (segment.0 - tile_upper_left) * F32x4::splat(256.0); - let segment = - segment.clamp(min, max).to_i32x4().as_u8x16().shuffle(shuffle_mask).as_i32x4(); + let segment = segment + .clamp(min, max) + .to_i32x4() + .as_u8x16() + .shuffle(shuffle_mask) + .as_i32x4(); // Unpack whole and fractional pixels. let px = LineSegmentU4((segment[1] | (segment[1] >> 12)) as u16); let subpx = LineSegmentU8(segment[0] as u32); // Cull degenerate fills. - if (px.0 & 0xf) as u8 == ((px.0 >> 8) & 0xf) as u8 && - (subpx.0 & 0xff) as u8 == ((subpx.0 >> 16) & 0xff) as u8 { + if (px.0 & 0xf) as u8 == ((px.0 >> 8) & 0xf) as u8 + && (subpx.0 & 0xff) as u8 == ((subpx.0 >> 16) & 0xff) as u8 + { debug!("... culling!"); return; } @@ -131,29 +146,39 @@ impl BuiltObject { let alpha_tile_index = self.get_or_allocate_alpha_tile_index(builder, tile_coords); debug!("... OK, pushing"); - self.fills.push(FillBatchPrimitive { px, subpx, alpha_tile_index }); + self.fills.push(FillBatchPrimitive { + px, + subpx, + alpha_tile_index, + }); } - fn get_or_allocate_alpha_tile_index(&mut self, builder: &SceneBuilder, tile_coords: Point2DI32) - -> u16 { + fn get_or_allocate_alpha_tile_index( + &mut self, + builder: &SceneBuilder, + tile_coords: Point2DI32, + ) -> u16 { let local_tile_index = self.tiles.coords_to_index_unchecked(tile_coords); let alpha_tile_index = self.tiles.data[local_tile_index].alpha_tile_index; if alpha_tile_index != !0 { return alpha_tile_index; } - let alpha_tile_index = builder.next_alpha_tile_index - .fetch_add(1, Ordering::Relaxed) as u16; + let alpha_tile_index = builder + .next_alpha_tile_index + .fetch_add(1, Ordering::Relaxed) as u16; self.tiles.data[local_tile_index].alpha_tile_index = alpha_tile_index; alpha_tile_index } - pub(crate) fn add_active_fill(&mut self, - builder: &SceneBuilder, - left: f32, - right: f32, - mut winding: i32, - tile_coords: Point2DI32) { + pub(crate) fn add_active_fill( + &mut self, + builder: &SceneBuilder, + left: f32, + right: f32, + mut winding: i32, + tile_coords: Point2DI32, + ) { let tile_origin_y = (tile_coords.y() * TILE_HEIGHT as i32) as f32; let left = Point2DF32::new(left, tile_origin_y); let right = Point2DF32::new(right, tile_origin_y); @@ -164,11 +189,13 @@ impl BuiltObject { LineSegmentF32::new(&right, &left) }; - debug!("... emitting active fill {} -> {} winding {} @ tile {:?}", - left.x(), - right.x(), - winding, - tile_coords); + debug!( + "... emitting active fill {} -> {} winding {} @ tile {:?}", + left.x(), + right.x(), + winding, + tile_coords + ); while winding != 0 { self.add_fill(builder, &segment, tile_coords); @@ -180,15 +207,19 @@ impl BuiltObject { } } - pub(crate) fn generate_fill_primitives_for_line(&mut self, - builder: &SceneBuilder, - mut segment: LineSegmentF32, - tile_y: i32) { - debug!("... generate_fill_primitives_for_line(): segment={:?} tile_y={} ({}-{})", - segment, - tile_y, - tile_y as f32 * TILE_HEIGHT as f32, - (tile_y + 1) as f32 * TILE_HEIGHT as f32); + pub(crate) fn generate_fill_primitives_for_line( + &mut self, + builder: &SceneBuilder, + mut segment: LineSegmentF32, + tile_y: i32, + ) { + debug!( + "... generate_fill_primitives_for_line(): segment={:?} tile_y={} ({}-{})", + segment, + tile_y, + tile_y as f32 * TILE_HEIGHT as f32, + (tile_y + 1) as f32 * TILE_HEIGHT as f32 + ); let winding = segment.from_x() > segment.to_x(); let (segment_left, segment_right) = if !winding { @@ -201,10 +232,12 @@ impl BuiltObject { let segment_tile_left = f32::floor(segment_left) as i32 / TILE_WIDTH as i32; let segment_tile_right = util::alignup_i32(f32::ceil(segment_right) as i32, TILE_WIDTH as i32); - debug!("segment_tile_left={} segment_tile_right={} tile_rect={:?}", - segment_tile_left, - segment_tile_right, - self.tile_rect()); + debug!( + "segment_tile_left={} segment_tile_right={} tile_rect={:?}", + segment_tile_left, + segment_tile_right, + self.tile_rect() + ); for subsegment_tile_x in segment_tile_left..segment_tile_right { let (mut fill_from, mut fill_to) = (segment.from(), segment.to()); @@ -242,7 +275,10 @@ impl BuiltObject { impl Default for TileObjectPrimitive { #[inline] fn default() -> TileObjectPrimitive { - TileObjectPrimitive { backdrop: 0, alpha_tile_index: !0 } + TileObjectPrimitive { + backdrop: 0, + alpha_tile_index: !0, + } } } @@ -255,8 +291,12 @@ impl TileObjectPrimitive { impl AlphaTileBatchPrimitive { #[inline] - pub fn new(tile_coords: Point2DI32, backdrop: i8, object_index: u16, tile_index: u16) - -> AlphaTileBatchPrimitive { + pub fn new( + tile_coords: Point2DI32, + backdrop: i8, + object_index: u16, + tile_index: u16, + ) -> AlphaTileBatchPrimitive { AlphaTileBatchPrimitive { tile_x_lo: (tile_coords.x() & 0xff) as u8, tile_y_lo: (tile_coords.y() & 0xff) as u8, @@ -269,8 +309,10 @@ impl AlphaTileBatchPrimitive { #[inline] pub fn tile_coords(&self) -> Point2DI32 { - Point2DI32::new((self.tile_x_lo as i32) | (((self.tile_hi & 0xf) as i32) << 8), - (self.tile_y_lo as i32) | (((self.tile_hi & 0xf0) as i32) << 4)) + Point2DI32::new( + (self.tile_x_lo as i32) | (((self.tile_hi & 0xf) as i32) << 8), + (self.tile_y_lo as i32) | (((self.tile_hi & 0xf0) as i32) << 4), + ) } } @@ -279,12 +321,8 @@ impl Debug for RenderCommand { match *self { RenderCommand::AddFills(ref fills) => write!(formatter, "AddFills(x{})", fills.len()), RenderCommand::FlushFills => write!(formatter, "FlushFills"), - RenderCommand::AlphaTile(ref tiles) => { - write!(formatter, "AlphaTile(x{})", tiles.len()) - } - RenderCommand::SolidTile(ref tiles) => { - write!(formatter, "SolidTile(x{})", tiles.len()) - } + RenderCommand::AlphaTile(ref tiles) => write!(formatter, "AlphaTile(x{})", tiles.len()), + RenderCommand::SolidTile(ref tiles) => write!(formatter, "SolidTile(x{})", tiles.len()), } } } diff --git a/renderer/src/post.rs b/renderer/src/post.rs index d70e721c..0e30dcb8 100644 --- a/renderer/src/post.rs +++ b/renderer/src/post.rs @@ -18,12 +18,10 @@ pub struct DefringingKernel(pub [f32; 4]); /// This intentionally does not precisely match what Core Graphics does (a /// Lanczos function), because we don't want any ringing artefacts. -pub static DEFRINGING_KERNEL_CORE_GRAPHICS: DefringingKernel = DefringingKernel([ - 0.033165660, 0.102074051, 0.221434336, 0.286651906 -]); -pub static DEFRINGING_KERNEL_FREETYPE: DefringingKernel = DefringingKernel([ - 0.0, 0.031372549, 0.301960784, 0.337254902 -]); +pub static DEFRINGING_KERNEL_CORE_GRAPHICS: DefringingKernel = + DefringingKernel([0.033165660, 0.102074051, 0.221434336, 0.286651906]); +pub static DEFRINGING_KERNEL_FREETYPE: DefringingKernel = + DefringingKernel([0.0, 0.031372549, 0.301960784, 0.337254902]); /// Should match macOS 10.13 High Sierra. pub static STEM_DARKENING_FACTORS: [f32; 2] = [0.0121, 0.0121 * 1.25]; diff --git a/renderer/src/scene.rs b/renderer/src/scene.rs index bee5502f..a018dce3 100644 --- a/renderer/src/scene.rs +++ b/renderer/src/scene.rs @@ -61,21 +61,31 @@ impl Scene { } fn build_shaders(&self) -> Vec { - self.objects.iter().map(|object| { - let paint = &self.paints[object.paint.0 as usize]; - ObjectShader { fill_color: paint.color } - }).collect() + self.objects + .iter() + .map(|object| { + let paint = &self.paints[object.paint.0 as usize]; + ObjectShader { + fill_color: paint.color, + } + }) + .collect() } - pub(crate) fn apply_render_options(&self, - original_outline: &Outline, - options: &PreparedRenderOptions) - -> Outline { + pub(crate) fn apply_render_options( + &self, + original_outline: &Outline, + options: &PreparedRenderOptions, + ) -> Outline { let effective_view_box = self.effective_view_box(options); let mut outline; match options.transform { - PreparedRenderTransform::Perspective { ref perspective, ref clip_polygon, .. } => { + PreparedRenderTransform::Perspective { + ref perspective, + ref clip_polygon, + .. + } => { if original_outline.is_outside_polygon(clip_polygon) { outline = Outline::new(); } else { @@ -96,8 +106,8 @@ impl Scene { PreparedRenderTransform::Perspective { .. } => unreachable!(), }; if options.subpixel_aa_enabled { - transform = transform.post_mul(&Transform2DF32::from_scale( - &Point2DF32::new(3.0, 1.0))) + transform = transform + .post_mul(&Transform2DF32::from_scale(&Point2DF32::new(3.0, 1.0))) } outline.transform(&transform); } @@ -120,7 +130,12 @@ impl Scene { return None; } let first_paint_id = self.objects[0].paint; - if self.objects.iter().skip(1).any(|object| object.paint != first_paint_id) { + if self + .objects + .iter() + .skip(1) + .any(|object| object.paint != first_paint_id) + { return None; } Some(self.paints[first_paint_id.0 as usize].color) @@ -138,19 +153,25 @@ impl Scene { impl Debug for Scene { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - writeln!(formatter, - "", - self.view_box.origin().x(), - self.view_box.origin().y(), - self.view_box.size().x(), - self.view_box.size().y())?; + writeln!( + formatter, + "", + self.view_box.origin().x(), + self.view_box.origin().y(), + self.view_box.size().x(), + self.view_box.size().y() + )?; for object in &self.objects { let paint = &self.paints[object.paint.0 as usize]; write!(formatter, " ", paint.color, object.outline)?; + writeln!( + formatter, + " fill=\"{:?}\" d=\"{:?}\" />", + paint.color, object.outline + )?; } writeln!(formatter, "")?; Ok(()) @@ -180,9 +201,13 @@ pub enum PathObjectKind { impl PathObject { #[inline] - pub fn new(outline: Outline, paint: PaintId, name: String, kind: PathObjectKind) - -> PathObject { - PathObject { outline, paint, name, kind } + pub fn new(outline: Outline, paint: PaintId, name: String, kind: PathObjectKind) -> PathObject { + PathObject { + outline, + paint, + name, + kind, + } } #[inline] diff --git a/renderer/src/sorted_vector.rs b/renderer/src/sorted_vector.rs index 128839fd..7317fd34 100644 --- a/renderer/src/sorted_vector.rs +++ b/renderer/src/sorted_vector.rs @@ -88,4 +88,4 @@ mod test { true } } -} \ No newline at end of file +} diff --git a/renderer/src/tile_map.rs b/renderer/src/tile_map.rs index f9b9cf2e..e83f7438 100644 --- a/renderer/src/tile_map.rs +++ b/renderer/src/tile_map.rs @@ -19,31 +19,46 @@ pub struct DenseTileMap { impl DenseTileMap { #[inline] - pub fn new(rect: RectI32) -> DenseTileMap where T: Copy + Clone + Default { + pub fn new(rect: RectI32) -> DenseTileMap + where + T: Copy + Clone + Default, + { let length = rect.size().x() as usize * rect.size().y() as usize; - DenseTileMap { data: vec![T::default(); length], rect } + DenseTileMap { + data: vec![T::default(); length], + rect, + } } #[inline] - pub fn from_builder(build: F, rect: RectI32) -> DenseTileMap where F: FnMut(usize) -> T { + pub fn from_builder(build: F, rect: RectI32) -> DenseTileMap + where + F: FnMut(usize) -> T, + { let length = rect.size().x() as usize * rect.size().y() as usize; - DenseTileMap { data: (0..length).map(build).collect(), rect } + DenseTileMap { + data: (0..length).map(build).collect(), + rect, + } } #[inline] pub fn coords_to_index(&self, coords: Point2DI32) -> Option { // TODO(pcwalton): SIMD? - if coords.x() < self.rect.min_x() || coords.x() >= self.rect.max_x() || - coords.y() < self.rect.min_y() || coords.y() >= self.rect.max_y() { - return None + if coords.x() < self.rect.min_x() + || coords.x() >= self.rect.max_x() + || coords.y() < self.rect.min_y() + || coords.y() >= self.rect.max_y() + { + return None; } Some(self.coords_to_index_unchecked(coords)) } #[inline] pub fn coords_to_index_unchecked(&self, coords: Point2DI32) -> usize { - (coords.y() - self.rect.min_y()) as usize * self.rect.size().x() as usize + - (coords.x() - self.rect.min_x()) as usize + (coords.y() - self.rect.min_y()) as usize * self.rect.size().x() as usize + + (coords.x() - self.rect.min_x()) as usize } #[inline] diff --git a/renderer/src/tiles.rs b/renderer/src/tiles.rs index d5b50d9c..97ed6291 100644 --- a/renderer/src/tiles.rs +++ b/renderer/src/tiles.rs @@ -38,12 +38,16 @@ pub(crate) struct Tiler<'a> { impl<'a> Tiler<'a> { #[allow(clippy::or_fun_call)] - pub(crate) fn new(builder: &'a SceneBuilder<'a>, - outline: &'a Outline, - view_box: RectF32, - object_index: u16) - -> Tiler<'a> { - let bounds = outline.bounds().intersection(view_box).unwrap_or(RectF32::default()); + pub(crate) fn new( + builder: &'a SceneBuilder<'a>, + outline: &'a Outline, + view_box: RectF32, + object_index: u16, + ) -> Tiler<'a> { + let bounds = outline + .bounds() + .intersection(view_box) + .unwrap_or(RectF32::default()); let built_object = BuiltObject::new(bounds); Tiler { @@ -100,7 +104,9 @@ impl<'a> Tiler<'a> { fn pack_and_cull(&mut self) { for (tile_index, tile) in self.built_object.tiles.data.iter().enumerate() { - let tile_coords = self.built_object.local_tile_index_to_coords(tile_index as u32); + let tile_coords = self + .built_object + .local_tile_index_to_coords(tile_index as u32); if tile.is_solid() { if tile.backdrop != 0 { self.builder.z_buffer.update(tile_coords, self.object_index); @@ -108,10 +114,12 @@ impl<'a> Tiler<'a> { continue; } - let alpha_tile = AlphaTileBatchPrimitive::new(tile_coords, - tile.backdrop, - self.object_index, - tile.alpha_tile_index as u16); + let alpha_tile = AlphaTileBatchPrimitive::new( + tile_coords, + tile.backdrop, + self.object_index, + tile.alpha_tile_index as u16, + ); self.built_object.alpha_tiles.push(alpha_tile); } } @@ -142,16 +150,21 @@ impl<'a> Tiler<'a> { -1 }; - debug!("tile Y {}({}): segment_x={} edge_winding={} current_tile_x={} \ - current_subtile_x={} current_winding={}", - tile_y, - tile_top, - segment_x, - edge_winding, - current_tile_x, - current_subtile_x, - current_winding); - debug!("... segment={:#?} crossing={:?}", active_edge.segment, active_edge.crossing); + debug!( + "tile Y {}({}): segment_x={} edge_winding={} current_tile_x={} \ + current_subtile_x={} current_winding={}", + tile_y, + tile_top, + segment_x, + edge_winding, + current_tile_x, + current_subtile_x, + current_winding + ); + debug!( + "... segment={:#?} crossing={:?}", + active_edge.segment, active_edge.crossing + ); // FIXME(pcwalton): Remove this debug code! debug_assert!(segment_x >= last_segment_x); @@ -164,21 +177,28 @@ impl<'a> Tiler<'a> { (i32::from(current_tile_x) * TILE_WIDTH as i32) as f32 + current_subtile_x; let tile_right_x = ((i32::from(current_tile_x) + 1) * TILE_WIDTH as i32) as f32; let current_tile_coords = Point2DI32::new(current_tile_x, tile_y); - self.built_object.add_active_fill(self.builder, - current_x, - tile_right_x, - current_winding, - current_tile_coords); + self.built_object.add_active_fill( + self.builder, + current_x, + tile_right_x, + current_winding, + current_tile_coords, + ); current_tile_x += 1; current_subtile_x = 0.0; } // Move over to the correct tile, filling in as we go. while current_tile_x < segment_tile_x { - debug!("... emitting backdrop {} @ tile {}", current_winding, current_tile_x); + debug!( + "... emitting backdrop {} @ tile {}", + current_winding, current_tile_x + ); let current_tile_coords = Point2DI32::new(current_tile_x, tile_y); - if let Some(tile_index) = self.built_object - .tile_coords_to_local_index(current_tile_coords) { + if let Some(tile_index) = self + .built_object + .tile_coords_to_local_index(current_tile_coords) + { // FIXME(pcwalton): Handle winding overflow. self.built_object.tiles.data[tile_index as usize].backdrop = current_winding as i8; @@ -196,11 +216,13 @@ impl<'a> Tiler<'a> { let current_x = (i32::from(current_tile_x) * TILE_WIDTH as i32) as f32 + current_subtile_x; let current_tile_coords = Point2DI32::new(current_tile_x, tile_y); - self.built_object.add_active_fill(self.builder, - current_x, - segment_x, - current_winding, - current_tile_coords); + self.built_object.add_active_fill( + self.builder, + current_x, + segment_x, + current_winding, + current_tile_coords, + ); current_subtile_x = segment_subtile_x; } @@ -229,15 +251,17 @@ impl<'a> Tiler<'a> { let prev_endpoint_index = contour.prev_endpoint_index_of(point_index.point()); let next_endpoint_index = contour.next_endpoint_index_of(point_index.point()); - debug!("adding new active edge, tile_y={} point_index={} prev={} next={} pos={:?} \ - prevpos={:?} nextpos={:?}", - tile_y, - point_index.point(), - prev_endpoint_index, - next_endpoint_index, - contour.position_of(point_index.point()), - contour.position_of(prev_endpoint_index), - contour.position_of(next_endpoint_index)); + debug!( + "adding new active edge, tile_y={} point_index={} prev={} next={} pos={:?} \ + prevpos={:?} nextpos={:?}", + tile_y, + point_index.point(), + prev_endpoint_index, + next_endpoint_index, + contour.position_of(point_index.point()), + contour.position_of(prev_endpoint_index), + contour.position_of(next_endpoint_index) + ); if contour.point_is_logically_above(point_index.point(), prev_endpoint_index) { debug!("... adding prev endpoint"); @@ -260,7 +284,11 @@ impl<'a> Tiler<'a> { } if contour.point_is_logically_above(point_index.point(), next_endpoint_index) { - debug!("... adding next endpoint {} -> {}", point_index.point(), next_endpoint_index); + debug!( + "... adding next endpoint {} -> {}", + point_index.point(), + next_endpoint_index + ); process_active_segment( contour, @@ -311,9 +339,12 @@ impl<'a> Tiler<'a> { } pub fn round_rect_out_to_tile_bounds(rect: RectF32) -> RectI32 { - rect.scale_xy(Point2DF32::new(1.0 / TILE_WIDTH as f32, 1.0 / TILE_HEIGHT as f32)) - .round_out() - .to_i32() + rect.scale_xy(Point2DF32::new( + 1.0 / TILE_WIDTH as f32, + 1.0 / TILE_HEIGHT as f32, + )) + .round_out() + .to_i32() } fn process_active_segment( @@ -378,20 +409,21 @@ impl ActiveEdge { fn process(&mut self, builder: &SceneBuilder, built_object: &mut BuiltObject, tile_y: i32) { let tile_bottom = ((i32::from(tile_y) + 1) * TILE_HEIGHT as i32) as f32; - debug!("process_active_edge({:#?}, tile_y={}({}))", self, tile_y, tile_bottom); + debug!( + "process_active_edge({:#?}, tile_y={}({}))", + self, tile_y, tile_bottom + ); let mut segment = self.segment; let winding = segment.baseline.y_winding(); if segment.is_line() { let line_segment = segment.as_line_segment(); - self.segment = match self.process_line_segment(&line_segment, - builder, - built_object, - tile_y) { - Some(lower_part) => Segment::line(&lower_part), - None => Segment::none(), - }; + self.segment = + match self.process_line_segment(&line_segment, builder, built_object, tile_y) { + Some(lower_part) => Segment::line(&lower_part), + None => Segment::none(), + }; return; } @@ -405,8 +437,10 @@ impl ActiveEdge { let first_line_segment = LineSegmentF32::new(&self.crossing, &segment.baseline.upper_point()) .orient(winding); - if self.process_line_segment(&first_line_segment, builder, built_object, tile_y) - .is_some() { + if self + .process_line_segment(&first_line_segment, builder, built_object, tile_y) + .is_some() + { return; } } @@ -417,7 +451,10 @@ impl ActiveEdge { let mut before_segment = oriented_segment; let mut after_segment = None; - while !before_segment.as_cubic_segment().is_flat(FLATTENING_TOLERANCE) { + while !before_segment + .as_cubic_segment() + .is_flat(FLATTENING_TOLERANCE) + { let next_t = 0.5 * split_t; let (before, after) = oriented_segment.as_cubic_segment().split(next_t); before_segment = before; @@ -425,14 +462,11 @@ impl ActiveEdge { split_t = next_t; } - debug!("... tile_y={} winding={} segment={:?} t={} before_segment={:?} + debug!( + "... tile_y={} winding={} segment={:?} t={} before_segment={:?} after_segment={:?}", - tile_y, - winding, - segment, - split_t, - before_segment, - after_segment); + tile_y, winding, segment, split_t, before_segment, after_segment + ); let line = before_segment.baseline.orient(winding); match self.process_line_segment(&line, builder, built_object, tile_y) { @@ -461,10 +495,10 @@ impl ActiveEdge { tile_y: i32, ) -> Option { let tile_bottom = ((i32::from(tile_y) + 1) * TILE_HEIGHT as i32) as f32; - debug!("process_line_segment({:?}, tile_y={}) tile_bottom={}", - line_segment, - tile_y, - tile_bottom); + debug!( + "process_line_segment({:?}, tile_y={}) tile_bottom={}", + line_segment, tile_y, tile_bottom + ); if line_segment.max_y() <= tile_bottom { built_object.generate_fill_primitives_for_line(builder, *line_segment, tile_y); diff --git a/renderer/src/z_buffer.rs b/renderer/src/z_buffer.rs index c9455dc3..a523bd9e 100644 --- a/renderer/src/z_buffer.rs +++ b/renderer/src/z_buffer.rs @@ -25,7 +25,9 @@ pub struct ZBuffer { impl ZBuffer { pub fn new(view_box: RectF32) -> ZBuffer { let tile_rect = tiles::round_rect_out_to_tile_bounds(view_box); - ZBuffer { buffer: DenseTileMap::from_builder(|_| AtomicUsize::new(0), tile_rect) } + ZBuffer { + buffer: DenseTileMap::from_builder(|_| AtomicUsize::new(0), tile_rect), + } } pub fn test(&self, coords: Point2DI32, object_index: u32) -> bool {