Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vello_shaders crate to load and preprocess WGSL #563

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/copyright.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# -g "!src/special_directory"

# Check all the standard Rust source files
output=$(rg "^// Copyright (19|20)[\d]{2} (.+ and )?the Vello Authors( and .+)?$\n^// SPDX-License-Identifier: Apache-2\.0 OR MIT$\n\n" --files-without-match --multiline -g "*.rs" -g "!{shader,src/cpu_shader}" .)
output=$(rg "^// Copyright (19|20)[\d]{2} (.+ and )?the Vello Authors( and .+)?$\n^// SPDX-License-Identifier: Apache-2\.0 OR MIT$\n\n" --files-without-match --multiline -g "*.rs" -g "!{shader,crates/shaders/src/cpu}" .)

if [ -n "$output" ]; then
echo -e "The following files lack the correct copyright header:\n"
Expand All @@ -20,7 +20,7 @@ if [ -n "$output" ]; then
fi

# Check all the shaders, both WGSL and CPU shaders in Rust, as they also have Unlicense
output=$(rg "^// Copyright (19|20)[\d]{2} (.+ and )?the Vello Authors( and .+)?$\n^// SPDX-License-Identifier: Apache-2\.0 OR MIT OR Unlicense$\n\n" --files-without-match --multiline -g "{shader,src/cpu_shader}/**/*.{rs,wgsl}" .)
output=$(rg "^// Copyright (19|20)[\d]{2} (.+ and )?the Vello Authors( and .+)?$\n^// SPDX-License-Identifier: Apache-2\.0 OR MIT OR Unlicense$\n\n" --files-without-match --multiline -g "{shader,crates/shaders/src/cpu}/**/*.{rs,wgsl}" .)

if [ -n "$output" ]; then
echo -e "The following shader files lack the correct copyright header:\n"
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ default = ["wgpu"]
# bump-allocated GPU memory.
# TODO: Turn this into a runtime option used at resolve time and remove the feature.
bump_estimate = ["vello_encoding/bump_estimate"]
hot_reload = []
hot_reload = ["vello_shaders/compile"]
buffer_labels = []

[lints]
workspace = true

[dependencies]
vello_encoding = { workspace = true }
vello_shaders = { workspace = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the compile feature to be enabled even if we're not hot reloading?

I guess it doesn't really matter, because we share the Naga version with wgpu

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Even though the features match, I think there is merit to excluding the feature when it's not needed. Though I generally tend to rely on linkers being smart about excluding unused code from binaries (which is desirable in the wasm build case) I went ahead and made "compile" an optional feature, which feels right to me.

bytemuck = { workspace = true }
skrifa = { workspace = true }
peniko = { workspace = true }
Expand All @@ -71,6 +72,7 @@ clippy.semicolon_if_nothing_returned = "warn"

[workspace.dependencies]
vello_encoding = { version = "0.1.0", path = "crates/encoding" }
vello_shaders = { version = "0.1.0", path = "crates/shaders" }
bytemuck = { version = "1.15.0", features = ["derive"] }
skrifa = "0.19.0"
peniko = "0.1.0"
Expand Down
7 changes: 6 additions & 1 deletion crates/shaders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository.workspace = true
publish = false # Remove this when the package is ready for publishing

[features]
default = ["compile", "wgsl", "msl", "full"]
default = ["wgsl", "full", "cpu"]
compile = ["naga", "thiserror"]

# Enables the complete imaging model. When this feature is disabled, the fine rasterization
Expand All @@ -22,12 +22,17 @@ full = []
wgsl = []
msl = []

# Enable the CPU versions of the shaders
cpu = ["bytemuck", "vello_encoding"]

[lints]
workspace = true

[dependencies]
bytemuck = { workspace = true, optional = true }
naga = { version = "0.19.2", features = ["wgsl-in", "msl-out",], optional = true }
thiserror = { version = "1.0.58", optional = true }
vello_encoding = { workspace = true, optional = true }

[build-dependencies]
naga = { version = "0.19.2", features = ["wgsl-in", "msl-out",] }
Expand Down
2 changes: 1 addition & 1 deletion crates/shaders/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl ShaderInfo {
} else {
Default::default()
};
println!("{:?}", permutation_map);
//println!("{permutation_map:?}");
let imports = preprocess::get_imports(shader_dir);
let mut info = HashMap::default();
let defines: HashSet<_> = if cfg!(feature = "full") {
Expand Down
74 changes: 73 additions & 1 deletion src/cpu_dispatch.rs → crates/shaders/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,48 @@
//! Support for CPU implementations of compute shaders.
//!
//! Note that while this CPU implementation is useful for testing and debugging,
//! a full CPU fallback for targets without wgpu hasn't been implemented yet.
//! a full CPU fallback as an alternative to GPU shaders is not provided.

// Allow un-idiomatic Rust to more closely match shaders
#![allow(clippy::needless_range_loop)]
#![allow(clippy::too_many_arguments)]

mod backdrop;
mod bbox_clear;
mod binning;
mod clip_leaf;
mod clip_reduce;
mod coarse;
mod draw_leaf;
mod draw_reduce;
mod euler;
mod fine;
mod flatten;
mod path_count;
mod path_count_setup;
mod path_tiling;
mod path_tiling_setup;
mod pathtag_reduce;
mod pathtag_scan;
mod tile_alloc;
mod util;

pub use backdrop::backdrop;
pub use bbox_clear::bbox_clear;
pub use binning::binning;
pub use clip_leaf::clip_leaf;
pub use clip_reduce::clip_reduce;
pub use coarse::coarse;
pub use draw_leaf::draw_leaf;
pub use draw_reduce::draw_reduce;
pub use flatten::flatten;
pub use path_count::path_count;
pub use path_count_setup::path_count_setup;
pub use path_tiling::path_tiling;
pub use path_tiling_setup::path_tiling_setup;
pub use pathtag_reduce::pathtag_reduce;
pub use pathtag_scan::pathtag_scan;
pub use tile_alloc::tile_alloc;

use std::cell::{Ref, RefCell, RefMut};
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -123,3 +164,34 @@ pub struct CpuTexture {
// In RGBA format. May expand in the future.
pub pixels: Vec<u32>,
}

// Common internal definitions

const PTCL_INITIAL_ALLOC: u32 = 64;

// Tags for PTCL commands
const CMD_END: u32 = 0;
const CMD_FILL: u32 = 1;
//const CMD_STROKE: u32 = 2;
const CMD_SOLID: u32 = 3;
const CMD_COLOR: u32 = 5;
const CMD_LIN_GRAD: u32 = 6;
const CMD_RAD_GRAD: u32 = 7;
const CMD_SWEEP_GRAD: u32 = 8;
const CMD_IMAGE: u32 = 9;
const CMD_BEGIN_CLIP: u32 = 10;
const CMD_END_CLIP: u32 = 11;
const CMD_JUMP: u32 = 12;

// The following are computed in draw_leaf from the generic gradient parameters
// encoded in the scene, and stored in the gradient's info struct, for
// consumption during fine rasterization.

// Radial gradient kinds
const RAD_GRAD_KIND_CIRCULAR: u32 = 1;
const RAD_GRAD_KIND_STRIP: u32 = 2;
const RAD_GRAD_KIND_FOCAL_ON_CIRCLE: u32 = 3;
const RAD_GRAD_KIND_CONE: u32 = 4;

// Radial gradient flags
const RAD_GRAD_SWAPPED: u32 = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{ConfigUniform, Path, Tile};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

fn backdrop_main(config: &ConfigUniform, paths: &[Path], tiles: &mut [Tile]) {
for drawobj_ix in 0..config.layout.n_draw_objects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{ConfigUniform, PathBbox};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

fn bbox_clear_main(config: &ConfigUniform, path_bboxes: &mut [PathBbox]) {
for i in 0..(config.layout.n_paths as usize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{BinHeader, BumpAllocators, ConfigUniform, DrawMonoid, PathBbox};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;
const TILE_WIDTH: usize = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{Clip, ConfigUniform, DrawMonoid, PathBbox};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

struct ClipStackElement {
// index of draw object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{Clip, ClipBic, ClipElement, PathBbox};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;

Expand Down
6 changes: 2 additions & 4 deletions src/cpu_shader/coarse.rs → crates/shaders/src/cpu/coarse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use vello_encoding::{
DRAW_INFO_FLAGS_FILL_RULE_BIT,
};

use crate::cpu_dispatch::CpuBinding;

use super::{
CMD_BEGIN_CLIP, CMD_COLOR, CMD_END, CMD_END_CLIP, CMD_FILL, CMD_IMAGE, CMD_JUMP, CMD_LIN_GRAD,
CMD_RAD_GRAD, CMD_SOLID, CMD_SWEEP_GRAD, PTCL_INITIAL_ALLOC,
CpuBinding, CMD_BEGIN_CLIP, CMD_COLOR, CMD_END, CMD_END_CLIP, CMD_FILL, CMD_IMAGE, CMD_JUMP,
CMD_LIN_GRAD, CMD_RAD_GRAD, CMD_SOLID, CMD_SWEEP_GRAD, PTCL_INITIAL_ALLOC,
};

const N_TILE_X: usize = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

use vello_encoding::{Clip, ConfigUniform, DrawMonoid, DrawTag, Monoid, PathBbox};

use crate::cpu_dispatch::CpuBinding;

use super::util::{read_draw_tag_from_scene, Transform, Vec2};
use super::{
RAD_GRAD_KIND_CIRCULAR, RAD_GRAD_KIND_CONE, RAD_GRAD_KIND_FOCAL_ON_CIRCLE, RAD_GRAD_KIND_STRIP,
RAD_GRAD_SWAPPED,
util::{read_draw_tag_from_scene, Transform, Vec2},
CpuBinding, RAD_GRAD_KIND_CIRCULAR, RAD_GRAD_KIND_CONE, RAD_GRAD_KIND_FOCAL_ON_CIRCLE,
RAD_GRAD_KIND_STRIP, RAD_GRAD_SWAPPED,
};

const WG_SIZE: usize = 256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

use vello_encoding::{ConfigUniform, DrawMonoid, DrawTag, Monoid};

use crate::cpu_dispatch::CpuBinding;

use super::util::read_draw_tag_from_scene;
use super::{util::read_draw_tag_from_scene, CpuBinding};

const WG_SIZE: usize = 256;

Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions src/cpu_shader/fine.rs → crates/shaders/src/cpu/fine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

use vello_encoding::{ConfigUniform, PathSegment, Tile};

use crate::cpu_dispatch::CpuTexture;

use super::{CMD_COLOR, CMD_END, CMD_FILL, CMD_JUMP, CMD_SOLID, PTCL_INITIAL_ALLOC};
use super::{CpuTexture, CMD_COLOR, CMD_END, CMD_FILL, CMD_JUMP, CMD_SOLID, PTCL_INITIAL_ALLOC};

// These should also move into a common area
const TILE_WIDTH: usize = 16;
Expand Down
10 changes: 6 additions & 4 deletions src/cpu_shader/flatten.rs → crates/shaders/src/cpu/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

use std::f32::consts::FRAC_1_SQRT_2;

use super::euler::{
espc_int_approx, espc_int_inv_approx, CubicParams, EulerParams, EulerSeg, TANGENT_THRESH,
use super::{
euler::{
espc_int_approx, espc_int_inv_approx, CubicParams, EulerParams, EulerSeg, TANGENT_THRESH,
},
util::{Transform, Vec2, ROBUST_EPSILON},
CpuBinding,
};
use super::util::{Transform, Vec2, ROBUST_EPSILON};
use crate::cpu_dispatch::CpuBinding;
use vello_encoding::math::f16_to_f32;
use vello_encoding::{
BumpAllocators, ConfigUniform, LineSoup, Monoid, PathBbox, PathMonoid, PathTag, Style,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use vello_encoding::{BumpAllocators, LineSoup, Path, SegmentCount, Tile};

use crate::cpu_dispatch::CpuBinding;

use super::util::{span, Vec2, ONE_MINUS_ULP, ROBUST_EPSILON};
use super::{
util::{span, Vec2, ONE_MINUS_ULP, ROBUST_EPSILON},
CpuBinding,
};

const TILE_SCALE: f32 = 1.0 / 16.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{BumpAllocators, IndirectCount};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

use vello_encoding::{BumpAllocators, LineSoup, Path, PathSegment, SegmentCount, Tile};

use crate::cpu_dispatch::CpuBinding;
use crate::cpu_shader::util::{ONE_MINUS_ULP, ROBUST_EPSILON};

use super::util::{span, Vec2};
use super::{
util::{span, Vec2, ONE_MINUS_ULP, ROBUST_EPSILON},
CpuBinding,
};

const TILE_WIDTH: u32 = 16;
const TILE_HEIGHT: u32 = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{BumpAllocators, IndirectCount};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{ConfigUniform, Monoid, PathMonoid};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{ConfigUniform, Monoid, PathMonoid};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const WG_SIZE: usize = 256;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vello_encoding::{BumpAllocators, ConfigUniform, DrawTag, Path, Tile};

use crate::cpu_dispatch::CpuBinding;
use super::CpuBinding;

const TILE_WIDTH: usize = 16;
const TILE_HEIGHT: usize = 16;
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions crates/shaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ mod types;

#[cfg(feature = "compile")]
pub mod compile;

pub use types::{BindType, BindingInfo, WorkgroupBufferInfo};
#[cfg(feature = "cpu")]
pub mod cpu;

#[cfg(feature = "msl")]
pub use types::msl;
pub use types::{BindType, BindingInfo, WorkgroupBufferInfo};

use std::borrow::Cow;

Expand Down
Loading