Skip to content

Commit e0b63f4

Browse files
authored
fix: renderling build workspace (#215)
* don't require CARGO_WORKSPACE_DIR * fix test paths * move BlockFuture to lib and gate with 'test-utils' feature flag * fix lints * derive Debug for all * allow clippy::manual_is_multiple_of on GPU code since rust-gpu is not yet on 1.91
1 parent 7808e3e commit e0b63f4

21 files changed

Lines changed: 114 additions & 78 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ opt-level = 3
7676

7777
[patch.crates-io]
7878
spirv-std = { git = "https://github.com/rust-gpu/rust-gpu.git", rev = "05b34493ce661dccd6694cf58afc13e3c8f7a7e0" }
79+
80+
[workspace.lints.rust]
81+
unexpected_cfgs = { level = "allow", check-cfg = [ 'cfg(spirv)' ] }

NOTES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Just pro-cons on tech choices and little things I don't want to forget whil impl
2222

2323
## cons / limititions / gotchas
2424

25+
* Can't use an array as a slice, it causes this error:
26+
```
27+
error: cannot cast between pointer types
28+
from `*[u32; 3]`
29+
to `*[u32]`
30+
```
31+
See the ticket I opened <https://github.com/Rust-GPU/rust-gpu/issues/465>
2532
* ~~can't use enums (but you can't in glsl or hlsl or msl or wgsl either)~~ you _can_ but they must be simple (like `#[repr(u32)]`)
2633
* ~~struct layout size/alignment errors can be really tricky~~ solved by using a slab
2734
* rust code must be no-std

crates/img-diff/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ edition = "2021"
99
glam = { workspace = true, features = ["std"] }
1010
image.workspace = true
1111
log.workspace = true
12+
renderling_build = { path = "../renderling-build" }
1213
snafu = "^0.7"

crates/img-diff/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
//! Provides image diffing for testing.
22
use glam::{Vec3, Vec4, Vec4Swizzles};
33
use image::{DynamicImage, Luma, Rgb, Rgb32FImage, Rgba32FImage};
4+
use renderling_build::{test_img_dir, test_output_dir};
45
use snafu::prelude::*;
5-
use std::path::Path;
66

7-
pub const TEST_IMG_DIR: &str = concat!(std::env!("CARGO_WORKSPACE_DIR"), "test_img");
8-
pub const TEST_OUTPUT_DIR: &str = concat!(std::env!("CARGO_WORKSPACE_DIR"), "test_output");
9-
pub const WASM_TEST_OUTPUT_DIR: &str =
10-
concat!(std::env!("CARGO_WORKSPACE_DIR"), "test_output/wasm");
117
const PIXEL_MAGNITUDE_THRESHOLD: f32 = 0.1;
128
pub const LOW_PIXEL_THRESHOLD: f32 = 0.02;
139
const IMAGE_DIFF_THRESHOLD: f32 = 0.05;
1410

1511
fn checkerboard_background_color(x: u32, y: u32) -> Vec3 {
1612
let size = 16;
1713
let x_square_index = x / size;
18-
let x_grey = x_square_index % 2 == 0;
14+
let x_grey = x_square_index.is_multiple_of(2);
1915
let y_square_index = y / size;
20-
let y_grey = y_square_index % 2 == 0;
16+
let y_grey = y_square_index.is_multiple_of(2);
2117
if (x_grey && y_grey) || (!x_grey && !y_grey) {
2218
Vec3::from([0.5, 0.5, 0.5])
2319
} else {
@@ -44,7 +40,7 @@ pub struct DiffCfg {
4440
/// The name of the test.
4541
pub test_name: Option<&'static str>,
4642
/// The output directory to store comparisons in.
47-
pub output_dir: &'static str,
43+
pub output_dir: std::path::PathBuf,
4844
}
4945

5046
impl Default for DiffCfg {
@@ -53,7 +49,7 @@ impl Default for DiffCfg {
5349
pixel_threshold: PIXEL_MAGNITUDE_THRESHOLD,
5450
image_threshold: IMAGE_DIFF_THRESHOLD,
5551
test_name: None,
56-
output_dir: TEST_OUTPUT_DIR,
52+
output_dir: test_output_dir(),
5753
}
5854
}
5955
}
@@ -143,7 +139,7 @@ pub fn save_to(
143139
}
144140

145141
pub fn save(filename: impl AsRef<std::path::Path>, seen: impl Into<DynamicImage>) {
146-
save_to(TEST_OUTPUT_DIR, filename, seen).unwrap()
142+
save_to(test_output_dir(), filename, seen).unwrap()
147143
}
148144

149145
pub fn assert_eq_cfg(
@@ -185,7 +181,7 @@ pub fn assert_eq_cfg(
185181
return Ok(());
186182
}
187183

188-
let mut dir = Path::new(output_dir).join(test_name.unwrap_or(filename));
184+
let mut dir = output_dir.join(test_name.unwrap_or(filename));
189185
dir.set_extension("");
190186
std::fs::create_dir_all(&dir).expect("cannot create test output dir");
191187
let expected = dir.join("expected.png");
@@ -228,7 +224,7 @@ pub fn assert_img_eq_cfg_result(
228224
seen: impl Into<DynamicImage>,
229225
cfg: DiffCfg,
230226
) -> Result<(), String> {
231-
let path = Path::new(TEST_IMG_DIR).join(filename);
227+
let path = test_img_dir().join(filename);
232228
let lhs = image::open(&path)
233229
.unwrap_or_else(|e| panic!("can't open expected image '{}': {e}", path.display(),));
234230
assert_eq_cfg(filename, lhs, seen, cfg)

crates/renderling-build/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,37 @@ fn wgsl(spv_filepath: impl AsRef<std::path::Path>, destination: impl AsRef<std::
138138
}
139139

140140
/// The cargo workspace directory.
141+
///
142+
/// ## Panics
143+
/// Panics if not called from a checkout of the renderling repo.
141144
pub fn workspace_dir() -> std::path::PathBuf {
142-
std::path::PathBuf::from(std::env!("CARGO_WORKSPACE_DIR"))
145+
std::path::PathBuf::from(std::env::var("CARGO_WORKSPACE_DIR").unwrap())
146+
}
147+
148+
/// The test_img directory.
149+
///
150+
/// ## Panics
151+
/// Panics if not called from a checkout of the renderling repo.
152+
pub fn test_img_dir() -> std::path::PathBuf {
153+
workspace_dir().join("test_img")
143154
}
144155

145156
/// The test_output directory.
157+
///
158+
/// ## Panics
159+
/// Panics if not called from a checkout of the renderling repo.
146160
pub fn test_output_dir() -> std::path::PathBuf {
147161
workspace_dir().join("test_output")
148162
}
149163

164+
/// The WASM test_output directory.
165+
///
166+
/// ## Panics
167+
/// Panics if not called from a checkout of the renderling repo.
168+
pub fn wasm_test_output_dir() -> std::path::PathBuf {
169+
test_output_dir().join("wasm")
170+
}
171+
150172
#[derive(Debug)]
151173
pub struct RenderlingPaths {
152174
/// `cargo_workspace` is not available when building outside of the project directory.

crates/renderling/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ crate-type = ["rlib", "cdylib"]
2626
default = ["gltf", "ui", "winit"]
2727
gltf = ["dep:gltf", "dep:serde_json"]
2828
test_i8_i16_extraction = []
29-
test-utils = ["dep:metal", "dep:wgpu-core"]
29+
test-utils = ["dep:metal", "dep:wgpu-core", "dep:futures-lite"]
3030
ui = ["dep:glyph_brush", "dep:loading-bytes", "dep:lyon"]
3131
wasm = ["wgpu/fragile-send-sync-non-atomic-wasm"]
3232
debug-slab = []
@@ -57,8 +57,9 @@ async-channel = {workspace = true}
5757
bytemuck = {workspace = true}
5858
craballoc.workspace = true
5959
crabslab = { workspace = true, features = ["default"] }
60-
dagga = {workspace=true}
6160
crunch = "0.5"
61+
dagga = {workspace=true}
62+
futures-lite = { workspace = true, optional = true }
6263
glam = { workspace = true, features = ["std"] }
6364
gltf = {workspace = true, optional = true}
6465
glyph_brush = {workspace = true, optional = true}
@@ -114,3 +115,6 @@ features = [
114115
"Navigator",
115116
"Window"
116117
]
118+
119+
[lints]
120+
workspace = true

crates/renderling/src/atlas.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ pub use cpu::*;
2424
pub mod shader;
2525

2626
/// Method of addressing the edges of a texture.
27-
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
28-
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, SlabItem)]
27+
#[derive(
28+
Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, SlabItem, core::fmt::Debug,
29+
)]
2930
pub struct TextureModes {
3031
pub s: TextureAddressMode,
3132
pub t: TextureAddressMode,
@@ -56,9 +57,10 @@ pub fn clamp(input: f32) -> f32 {
5657
}
5758

5859
/// How edges should be handled in texture addressing/wrapping.
59-
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
6060
#[repr(u32)]
61-
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, SlabItem)]
61+
#[derive(
62+
Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, SlabItem, core::fmt::Debug,
63+
)]
6264
pub enum TextureAddressMode {
6365
#[default]
6466
ClampToEdge,
@@ -93,7 +95,16 @@ impl TextureAddressMode {
9395
TextureAddressMode::MirroredRepeat => {
9496
let sign = if input >= 0.0 { 1.0f32 } else { -1.0 };
9597
let i = input.abs();
96-
let flip = i as u32 % 2 == 0;
98+
// TODO: Remove this clippy allowance after <https://github.com/Rust-GPU/rust-gpu/pull/460>
99+
// merges.
100+
#[cfg_attr(
101+
cpu,
102+
expect(
103+
clippy::manual_is_multiple_of,
104+
reason = "rust-gpu is not yet on rustc 1.91, which introduced this lint"
105+
)
106+
)]
107+
let flip = ((i as u32) % 2) == 0;
97108
let t = repeat(i);
98109
if sign > 0.0 {
99110
if flip {

crates/renderling/src/atlas/cpu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ fn pack_images<'a>(
668668
)
669669
.collect()
670670
};
671-
new_packing.sort_by_key(|a| (a.size().length_squared()));
671+
new_packing.sort_by_key(|a| a.size().length_squared());
672672
let total_images = new_packing.len();
673673
let new_packing_layers: Vec<Vec<AnotherPacking>> =
674674
fan_split_n(extent.depth_or_array_layers as usize, new_packing);

crates/renderling/src/atlas/shader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ pub struct AtlasDescriptor {
99
}
1010

1111
/// A texture inside the atlas.
12-
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
13-
#[derive(Clone, Copy, Default, PartialEq, SlabItem)]
12+
#[derive(Clone, Copy, Default, PartialEq, SlabItem, core::fmt::Debug)]
1413
pub struct AtlasTextureDescriptor {
1514
/// The top left offset of texture in the atlas.
1615
pub offset_px: UVec2,

0 commit comments

Comments
 (0)