Skip to content

Commit

Permalink
Render via GPU
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jan 3, 2025
1 parent 8052417 commit 3c95de8
Show file tree
Hide file tree
Showing 11 changed files with 658 additions and 20 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

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

20 changes: 18 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ name = "png"
version = "0.1.0"
edition = "2021"
default-run = "png"
resolver = "2"

[[bin]]
name = "profile"
path = "./bin/profile.rs"

[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
debug = true
Expand All @@ -19,11 +22,24 @@ flate2 = "1.0.35"
anyhow = "1.0.94"

# renderer

winit = { version = "0.29", features = ["rwh_05"] }
cfg-if = "1"
bytemuck = { version = "1.16", features = [ "derive" ] }
env_logger = "0.10"
log = "0.4"
pollster = "0.3"
wgpu = "22.0"
winit = { version = "0.29", features = ["rwh_05"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1"
console_log = "1.0"
wgpu = { version = "22.0", features = ["webgl"]}
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}

minifb = "0.27.0"
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# png

This project aims to provide a PNG decoder with SIMD-accelerated filters and a feature-rich renderer.
This project aims to provide a PNG decoder with SIMD-accelerated filters and a GPU-based renderer.

## Usage
## Render

To render an image, simply run `cargo run <image_path>`. For example:

```bash
cargo run ./potatoe.png
```

Currently, the renderer supports image resizing.

## Profile

To profile `png`, the `profile.sh` script serves as syntatic sugar for running samply's profiling.
Expand Down
21 changes: 19 additions & 2 deletions src/grammar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, slice::ChunksExact};
use std::{borrow::Cow, collections::BTreeMap, slice::ChunksExact};
#[cfg(test)]
use std::{
fs::File,
Expand Down Expand Up @@ -117,14 +117,31 @@ impl Png {
}

/// The dimensions of the image (width, height).
pub const fn dimension(&self) -> (u32, u32) {
pub const fn dimensions(&self) -> (u32, u32) {
(self.width, self.height)
}

pub const fn color_type(&self) -> ColorType {
self.color_type
}

pub fn to_rgba8(&self) -> Cow<'_, [u8]> {
match self.color_type {
ColorType::RGBA => Cow::from(&self.pixel_buffer),
ColorType::RGB => {
let b = self
.pixel_buffer
.chunks_exact(3)
.map(|b| [b[0], b[1], b[2], 0])
.flatten()
.collect::<Vec<_>>();

Cow::from(b)
}
_ => todo!(),
}
}

pub fn pixel_buffer(&self) -> Vec<u32> {
match self.color_type {
ColorType::RGB => self.rgb_buffer(),
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#![warn(clippy::nursery)]

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

pub use decoder::*;
pub use grammar::*;
pub mod renderer;
pub mod test_file_parser;

mod decoder;
mod grammar;
mod texture;

mod crc32;
17 changes: 3 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use minifb::{Window, WindowOptions};
use png::Decoder;
use png::{renderer, Decoder};
use pollster::block_on;

fn main() -> Result<()> {
let mut args = std::env::args().skip(1);
Expand All @@ -12,18 +12,7 @@ fn main() -> Result<()> {
let mut decoder = Decoder::new(&content);
let png = decoder.decode()?;

let (width, height) = png.dimension();

let mut window = Window::new(
"PNG renderer",
width as usize,
height as usize,
WindowOptions::default(),
)?;

while window.is_open() {
window.update_with_buffer(&png.pixel_buffer(), width as usize, height as usize)?;
}
block_on(renderer::run(png));

Ok(())
}
Loading

0 comments on commit 3c95de8

Please sign in to comment.