Skip to content
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: 0 additions & 4 deletions crates/solstice-render/shaders/clouds/common.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const MAX_DISTANCE: f32 = 90000.0;
// The longest stretch of the layer one ray marches through.
const MAX_PATH: f32 = 40000.0;

fn remap(x: f32, low: f32, high: f32, new_low: f32, new_high: f32) -> f32 {
return new_low + (x - low) / (high - low) * (new_high - new_low);
}

// Distance along a ray from height `h` above the ground, rising at `mu` (the sine of its
// elevation), to where it leaves the sphere at height `shell` that contains it. Written to avoid
// the cancellation that planet-sized numbers cause in the textbook formula.
Expand Down
6 changes: 2 additions & 4 deletions crates/solstice-render/shaders/clouds/noise.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// weather (512^2): R is where cloud can form, in clumps. G varies the kind of cloud from place to
// place. B is long, fibrous streaks for cirrus.

#include "common/math.wgsl"

@group(0) @binding(0) var volume_out: texture_storage_3d<rgba8unorm, write>;
@group(0) @binding(1) var map_out: texture_storage_2d<rgba8unorm, write>;

Expand Down Expand Up @@ -104,10 +106,6 @@ fn worley_fbm(uvw: vec3f, period: i32, seed: u32) -> f32 {
+ worley(uvw * f32(period * 4), period * 4, seed + 2u) * 0.125;
}

fn remap(x: f32, low: f32, high: f32, new_low: f32, new_high: f32) -> f32 {
return new_low + (x - low) / (high - low) * (new_high - new_low);
}

// Spreads a value whose 2nd and 98th percentiles are `low` and `high` over 0..1. Each kind of
// noise here bunches in a narrow band; measured by the cloud_noise example, these bands are
// stretched to fill the range, so that thresholds in the cloud model mean what they say.
Expand Down
16 changes: 16 additions & 0 deletions crates/solstice-render/shaders/common/math.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@ fn luminance(c: vec3f) -> f32 {
fn sq(x: f32) -> f32 {
return x * x;
}

/// Four independent numbers from 0 to 1 for three integers.
fn hash4(a: u32, b: u32, c: u32) -> vec4f {
let h = pcg(a + pcg(b + pcg(c)));
return vec4f(
hash_to_unit(h),
hash_to_unit(h ^ 0x9e3779b9u),
hash_to_unit(h ^ 0x85ebca6bu),
hash_to_unit(h ^ 0xc2b2ae35u),
);
}

/// `x` mapped from the range `low` to `high` onto `new_low` to `new_high`, unclamped.
fn remap(x: f32, low: f32, high: f32, new_low: f32, new_high: f32) -> f32 {
return new_low + (x - low) / (high - low) * (new_high - new_low);
}
10 changes: 0 additions & 10 deletions crates/solstice-render/shaders/rain/draw.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ struct Varyings {
@location(1) color: vec4f,
}

fn hash4(a: u32, b: u32, c: u32) -> vec4f {
let h = pcg(a + pcg(b + pcg(c)));
return vec4f(
hash_to_unit(h),
hash_to_unit(h ^ 0x9e3779b9u),
hash_to_unit(h ^ 0x85ebca6bu),
hash_to_unit(h ^ 0xc2b2ae35u),
);
}

// The rain map's texel holding `xz`: the first surface's height, the puddle's depth, the eave's
// height, and 1 if anything is there; or nothing, outside it.
fn rain_map_at(xz: vec2f) -> vec4f {
Expand Down
10 changes: 0 additions & 10 deletions crates/solstice-render/shaders/rain/ripples.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ const RING_HEIGHT: f32 = 0.0012;
// Rings a cell starts a second, at most.
const RINGS_PER_SECOND: f32 = 1.6;

fn hash4(a: u32, b: u32, c: u32) -> vec4f {
let h = pcg(a + pcg(b + pcg(c)));
return vec4f(
hash_to_unit(h),
hash_to_unit(h ^ 0x9e3779b9u),
hash_to_unit(h ^ 0x85ebca6bu),
hash_to_unit(h ^ 0xc2b2ae35u),
);
}

@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) id: vec3u) {
if id.x >= RIPPLE_TEXELS || id.y >= RIPPLE_TEXELS {
Expand Down
9 changes: 2 additions & 7 deletions crates/solstice-render/src/clouds/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//! files of exactly this kind, in `weather/` beside this crate. [`Weather::observed`] turns a
//! live report of cloud cover and wind into a state, so the sky can follow real weather.

use crate::text::number;

/// What the sky is doing.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Weather {
Expand Down Expand Up @@ -371,13 +373,6 @@ impl WeatherCycle {
}
}

fn number(text: &str) -> Result<f32, String> {
match text.parse::<f32>() {
Ok(value) if value.is_finite() => Ok(value),
_ => Err(format!("{text:?} is not a number")),
}
}

fn smooth(low: f32, high: f32, x: f32) -> f32 {
let t = ((x - low) / (high - low)).clamp(0.0, 1.0);
t * t * (3.0 - 2.0 * t)
Expand Down
23 changes: 23 additions & 0 deletions crates/solstice-render/src/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,29 @@ pub fn groups_for(count: u32, group_size: u32) -> u32 {
count.div_ceil(group_size)
}

/// A GPU for the crate's own tests, or `None`, said on stderr, where there is none to have.
///
/// Devices are made one at a time: made at once from many test threads, the Vulkan loader,
/// validation layer and driver on Windows can crash or deadlock.
#[cfg(test)]
pub(crate) fn test_gpu() -> Option<Gpu> {
static MAKING: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _one_at_a_time = MAKING
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
});
pollster::block_on(Gpu::request(
&instance,
None,
wgpu::PowerPreference::HighPerformance,
))
.map_err(|error| eprintln!("skipping GPU test: {error}"))
.ok()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions crates/solstice-render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod shadow;
pub mod skin;
pub mod sky;
pub mod streaming;
mod text;
pub mod time_of_day;
pub mod vg;
pub mod wind;
Expand Down
9 changes: 2 additions & 7 deletions crates/solstice-render/src/ocean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub mod surface;
pub mod waterline;
pub mod waves;

use crate::text::number;

pub use pond::Pond;
pub use sea::Sea;
pub use spectrum::SeaState;
Expand Down Expand Up @@ -203,13 +205,6 @@ impl Ocean {
}
}

fn number(text: &str) -> Result<f32, String> {
text.parse::<f32>()
.ok()
.filter(|v| v.is_finite())
.ok_or_else(|| format!("{text:?} is not a number"))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
18 changes: 1 addition & 17 deletions crates/solstice-render/src/ocean/waves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,23 +470,7 @@ mod tests {
use std::f32::consts::TAU;
use std::sync::{Arc, Mutex};

fn request_gpu() -> Option<Gpu> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
});
match pollster::block_on(Gpu::request(
&instance,
None,
wgpu::PowerPreference::HighPerformance,
)) {
Ok(gpu) => Some(gpu),
Err(error) => {
eprintln!("skipping GPU test: {error}");
None
}
}
}
use crate::gpu::test_gpu as request_gpu;

fn sea() -> SeaState {
SeaState {
Expand Down
9 changes: 2 additions & 7 deletions crates/solstice-render/src/particles/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
//! Every module has a default, so an emitter names only what differs. The built-in effects are
//! files of exactly this kind, in `particles/` beside this crate.

use crate::text::number;

/// The most emitters one effect may have.
pub const MAX_EMITTERS_PER_EFFECT: usize = 8;
/// The largest budget a sorted emitter may have: its whole list is sorted in one workgroup's
Expand Down Expand Up @@ -514,13 +516,6 @@ fn is_name(word: &str) -> bool {
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

fn number(word: &str) -> Result<f32, String> {
word.parse::<f32>()
.ok()
.filter(|v| v.is_finite())
.ok_or_else(|| format!("{word:?} is not a number"))
}

/// A number, a parameter, or a number times a parameter.
fn value(word: &str, params: &[(String, f32)]) -> Result<Value, String> {
let param = |name: &str| {
Expand Down
18 changes: 1 addition & 17 deletions crates/solstice-render/src/particles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,7 @@ use crate::frame::FrameUniform;
use reference::{is_dead, spawn, spawn_elapsed, step};
use std::collections::{HashMap, HashSet};

fn request_gpu() -> Option<Gpu> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
});
match pollster::block_on(Gpu::request(
&instance,
None,
wgpu::PowerPreference::HighPerformance,
)) {
Ok(gpu) => Some(gpu),
Err(error) => {
eprintln!("skipping GPU test: {error}");
None
}
}
}
use crate::gpu::test_gpu as request_gpu;

/// A pool on the GPU, failing the test on any GPU error.
struct Rig {
Expand Down
40 changes: 40 additions & 0 deletions crates/solstice-render/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ pub const ENTRY_POINTS: &[&str] = &[
"vg/raster_hw_fragment.wgsl",
"vg/raster_sw.wgsl",
"vg/raster_sw_depth.wgsl",
"vg/raster_sw_masked.wgsl",
"vg/raster_sw_depth_masked.wgsl",
"vg/raster_hw_masked.wgsl",
"vg/shadow_merge.wgsl",
"vg/speck_depth.wgsl",
"vg/hzb_seed.wgsl",
Expand Down Expand Up @@ -410,6 +413,43 @@ mod tests {
assert!(failures.is_empty(), "\n{}", failures.join("\n"));
}

/// Every shader the renderer makes a module of is an entry point, so the tests above see it.
/// Only calls that name their shader as a literal are seen; one with a path in a variable
/// must be listed by hand.
#[test]
fn every_shader_made_into_a_module_is_listed() {
let mut unlisted = Vec::new();
let mut folders = vec![std::path::PathBuf::from(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src"
))];
while let Some(folder) = folders.pop() {
for entry in std::fs::read_dir(&folder).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
folders.push(path);
continue;
}
let text = std::fs::read_to_string(&path).unwrap();
for call in ["module(", "module_for("] {
for (at, _) in text.match_indices(call) {
// The first string after the call, within its arguments, is the path.
let rest = &text[at + call.len()..];
let Some(open) = rest[..rest.len().min(160)].find('"') else {
continue;
};
let name = &rest[open + 1..];
let name = &name[..name.find('"').unwrap()];
if name.ends_with(".wgsl") && !ENTRY_POINTS.contains(&name) {
unlisted.push(format!("{}: {name}", path.display()));
}
}
}
}
}
assert!(unlisted.is_empty(), "not in ENTRY_POINTS: {unlisted:?}");
}

/// Every shader `module_for` is used on, in the form scenes with nothing skinned get.
/// A scene with no emissive maps must get the shader it had before they existed.
#[test]
Expand Down
9 changes: 9 additions & 0 deletions crates/solstice-render/src/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! What the renderer's text files (weather, sea states, particle effects) share in reading them.

/// A finite number, or why not.
pub(crate) fn number(word: &str) -> Result<f32, String> {
word.parse::<f32>()
.ok()
.filter(|v| v.is_finite())
.ok_or_else(|| format!("{word:?} is not a number"))
}
24 changes: 1 addition & 23 deletions crates/solstice-render/src/wind/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! blade in it. The GPU tests pass trivially, with a note, if no GPU is available.

use super::*;
use crate::gpu::test_gpu as request_gpu;
use crate::scene::INSTANCE_SWAYS;
use std::sync::Arc;

fn weather(speed: f32, from: f32, gusts: f32, turbulence: f32) -> Weather {
Weather {
Expand Down Expand Up @@ -217,28 +217,6 @@ fn records_lay_out_as_their_shader_twins() {
);
}

fn request_gpu() -> Option<Gpu> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
});
match pollster::block_on(Gpu::request(
&instance,
None,
wgpu::PowerPreference::HighPerformance,
)) {
Ok(gpu) => {
gpu.device
.on_uncaptured_error(Arc::new(|error: wgpu::Error| panic!("GPU error: {error}")));
Some(gpu)
}
Err(error) => {
eprintln!("skipping GPU test: {error}");
None
}
}
}

/// Reads `buffer`'s first `bytes` back from the GPU.
fn read_buffer(gpu: &Gpu, buffer: &wgpu::Buffer, bytes: u64) -> Vec<u8> {
let staging = gpu.device.create_buffer(&wgpu::BufferDescriptor {
Expand Down
28 changes: 12 additions & 16 deletions crates/solstice-render/tests/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ use std::sync::{Arc, Mutex};
const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;

fn request_gpu() -> Option<Gpu> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
});
match pollster::block_on(Gpu::request(
&instance,
None,
wgpu::PowerPreference::HighPerformance,
)) {
Ok(gpu) => Some(gpu),
Err(error) => {
eprintln!("skipping GPU test: {error}");
None
}
}
request_gpu_with(None)
}

/// A GPU asking for no more than `limits`, as a small one is given, or `None` if there is none.
fn request_gpu_within(limits: &wgpu::Limits) -> Option<Gpu> {
request_gpu_with(Some(limits))
}

/// Devices are made one at a time: made at once from many test threads, the Vulkan loader,
/// validation layer and driver on Windows can crash or deadlock.
fn request_gpu_with(limits: Option<&wgpu::Limits>) -> Option<Gpu> {
static MAKING: Mutex<()> = Mutex::new(());
let _one_at_a_time = MAKING
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..wgpu::InstanceDescriptor::new_without_display_handle()
Expand All @@ -46,7 +42,7 @@ fn request_gpu_within(limits: &wgpu::Limits) -> Option<Gpu> {
&instance,
None,
wgpu::PowerPreference::HighPerformance,
Some(limits),
limits,
))
.map_err(|error| eprintln!("skipping GPU test: {error}"))
.ok()
Expand Down
Loading
Loading