Skip to content

Commit

Permalink
Cleanup + Clippy + Rustfmt
Browse files Browse the repository at this point in the history
This commit should have no functionality changes except for edge case
handling. The public interface has been pruned so that the types listed
by rustdoc are the minimal types, and a lot of this was done by
consolidating the wgpu pipeline logic into a new pipeline module.

Next, I added safety comments for unsafe usage. There's only a few
usages for wgpu and the rest are empty bytemuck::Pod implementations.

The rest of the changes are the fallout from enabling clippy::pedantic
and adding a rustfmt.toml.
  • Loading branch information
ecton committed Jul 3, 2023
1 parent 7e1ba79 commit f9d317c
Show file tree
Hide file tree
Showing 15 changed files with 899 additions and 728 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lyon_tessellation = "1.0.1"
image = { version = "0.24.6", optional = true, default-features = false }
cosmic-text = { version = "0.8" }
alot = "0.1"
ahash = "0.8.3"

[dev-dependencies]
image = { features = ["png"] }
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Kludgine (Redux)

This branch is a rewrite of Kludgine, aiming to be a lightweight, efficient 2d
rendering option for `wgpu`-based applications.
4 changes: 2 additions & 2 deletions examples/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;
use appit::RunningWindow;
use kludgine::app::WindowBehavior;
use kludgine::math::{Dips, Pixels, Point, Rect, Size};
use kludgine::Color;
use kludgine::{PathBuilder, PreparedGraphic};
use kludgine::shapes::PathBuilder;
use kludgine::{Color, PreparedGraphic};

fn main() {
Test::run();
Expand Down
3 changes: 1 addition & 2 deletions examples/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::time::Duration;
use appit::RunningWindow;
use kludgine::app::WindowBehavior;
use kludgine::math::{Dips, Point, Rect, Size};
use kludgine::PreparedGraphic;
use kludgine::Texture;
use kludgine::{PreparedGraphic, Texture};

fn main() {
Test::run();
Expand Down
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unstable_features = true
use_field_init_shorthand = true
imports_granularity = "Module"
group_imports = "StdExternalCrate"
format_code_in_doc_comments = true
reorder_impl_items = true
44 changes: 34 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::{Arc, OnceLock};

use appit::{RunningWindow, WindowBehavior as _};

use crate::shapes::PushConstants;
use crate::{Color, Graphics, Kludgine, Renderer, Rendering, RenderingGraphics};
use crate::pipeline::PushConstants;
use crate::render::{Renderer, Rendering};
use crate::{Color, Graphics, Kludgine, RenderingGraphics};

fn shared_wgpu() -> Arc<wgpu::Instance> {
static SHARED_WGPU: OnceLock<Arc<wgpu::Instance>> = OnceLock::new();
Expand All @@ -13,6 +14,7 @@ fn shared_wgpu() -> Arc<wgpu::Instance> {

pub trait WindowBehavior: Sized + 'static {
type Context: Send + 'static;

fn initialize(
window: &mut RunningWindow,
graphics: &mut Graphics<'_>,
Expand All @@ -28,14 +30,17 @@ pub trait WindowBehavior: Sized + 'static {
graphics: &mut RenderingGraphics<'_, 'pass>,
) -> bool;

#[must_use]
fn power_preference() -> wgpu::PowerPreference {
wgpu::PowerPreference::default()
}

#[must_use]
fn limits(adapter_limits: wgpu::Limits) -> wgpu::Limits {
wgpu::Limits::downlevel_webgl2_defaults().using_resolution(adapter_limits)
}

#[must_use]
fn clear_color() -> Option<Color> {
Some(Color::BLACK)
}
Expand All @@ -61,7 +66,7 @@ struct KludgineWindow<Behavior> {
device: wgpu::Device,
queue: wgpu::Queue,
_adapter: wgpu::Adapter,
_wgpu: Arc<wgpu::Instance>,
wgpu: Arc<wgpu::Instance>,
}

impl<T> appit::WindowBehavior for KludgineWindow<T>
Expand All @@ -70,7 +75,11 @@ where
{
type Context = (Arc<wgpu::Instance>, T::Context);

#[allow(unsafe_code)]
fn initialize(window: &mut RunningWindow, (wgpu, context): Self::Context) -> Self {
// SAFETY: This function is only invoked once the window has been
// created, and cannot be invoked after the underlying window has been
// destroyed.
let surface = unsafe { wgpu.create_surface(window.winit()).unwrap() };
let adapter = pollster::block_on(wgpu.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: T::power_preference(),
Expand All @@ -79,7 +88,9 @@ where
}))
.unwrap();
let mut limits = T::limits(adapter.limits());
limits.max_push_constant_size = size_of::<PushConstants>() as u32;
limits.max_push_constant_size = size_of::<PushConstants>()
.try_into()
.expect("should fit :)");
let (device, queue) = pollster::block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
label: None,
Expand All @@ -98,7 +109,7 @@ where
&queue,
swapchain_format,
window.inner_size().into(),
window.scale() as f32,
lossy_f64_to_f32(window.scale()),
);
let mut graphics = Graphics::new(&mut state, &device, &queue);

Expand All @@ -116,7 +127,7 @@ where
let behavior = T::initialize(window, &mut graphics, context);

Self {
_wgpu: wgpu,
wgpu,
kludgine: state,
_adapter: adapter,
behavior,
Expand All @@ -127,6 +138,7 @@ where
}
}

#[allow(unsafe_code)]
fn redraw(&mut self, window: &mut RunningWindow) {
let frame = loop {
match self.surface.get_current_texture() {
Expand All @@ -140,9 +152,9 @@ where
return;
}
wgpu::SurfaceError::Lost => {
println!("Lost surface, reconfiguring");
self.surface =
unsafe { self._wgpu.create_surface(window.winit()).unwrap() };
// SAFETY: redraw is only called while the event loop
// and window are still alive.
self.surface = unsafe { self.wgpu.create_surface(window.winit()).unwrap() };
self.surface.configure(&self.device, &self.config);
}
wgpu::SurfaceError::OutOfMemory => {
Expand Down Expand Up @@ -200,7 +212,7 @@ where
self.surface.configure(&self.device, &self.config);
self.kludgine.resize(
window.inner_size().into(),
window.scale() as f32,
lossy_f64_to_f32(window.scale()),
&self.queue,
);
// TODO pass onto kludgine
Expand Down Expand Up @@ -257,3 +269,15 @@ where
{
CallbackWindow::run_with(render_fn)
}

/// Performs `value as f32`.
///
/// This function exists solely because of clippy. The truncation of f64 -> f32
/// isn't as severe as truncation of integer types, but it's lumped into the
/// same lint. I don't want to disable the truncation lint, and I don't want
/// functions that need to do this operation to not be checking for integer
/// truncation.
#[allow(clippy::cast_possible_truncation)] // truncation desired
fn lossy_f64_to_f32(value: f64) -> f32 {
value as f32
}
19 changes: 14 additions & 5 deletions src/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use alot::{LotId, Lots};

use crate::math::{Rect, Size, ToFloat, UPixels};
use crate::pack::{TextureAllocation, TexturePacker};
use crate::sealed;
use crate::shapes::{PreparedGraphic, Vertex};
use crate::{Graphics, Texture, TextureSource, WgpuDeviceAndQueue};
use crate::pipeline::{PreparedGraphic, Vertex};
use crate::{sealed, Graphics, Texture, TextureSource, WgpuDeviceAndQueue};

#[derive(Debug, Clone)]
pub struct TextureCollection {
Expand All @@ -23,13 +22,13 @@ struct Data {
}

impl TextureCollection {
pub fn new(
pub(crate) fn new_generic(
initial_size: Size<UPixels>,
minimum_column_width: u16,
format: wgpu::TextureFormat,
graphics: &impl WgpuDeviceAndQueue,
) -> Self {
let texture = Texture::new(
let texture = Texture::new_generic(
graphics,
initial_size,
format,
Expand All @@ -45,6 +44,16 @@ impl TextureCollection {
}
}

#[must_use]
pub fn new(
initial_size: Size<UPixels>,
minimum_column_width: u16,
format: wgpu::TextureFormat,
graphics: &Graphics<'_>,
) -> Self {
Self::new_generic(initial_size, minimum_column_width, format, graphics)
}

pub fn push_texture(
&mut self,
data: &[u8],
Expand Down
Loading

0 comments on commit f9d317c

Please sign in to comment.