First attempt at a GPU runner
CI / cargo fmt (push) Failing after 1m5s
CI / cargo test (push) Failing after 2m15s
CI / cargo test (GPU) (push) Successful in 16m58s

Currently failing with an error I don't understand:

```
wgpu error: Validation Error

Caused by:
  In Device::create_shader_module, label = '...\image_binary.spv'

Shader '...\image_binary.spv' parsing error: InvalidTypeWidth(1)
```
This commit is contained in:
2026-07-29 11:12:52 -04:00
parent 7ff19631ba
commit 53d5ad1422
15 changed files with 1482 additions and 276 deletions
-5
View File
@@ -16,8 +16,3 @@ libm.workspace = true
rand.workspace = true
rand_xoshiro.workspace = true
spirv-std.workspace = true
[dev-dependencies]
anyhow.workspace = true
image.workspace = true
tempfile.workspace = true
-95
View File
@@ -1,95 +0,0 @@
use anyhow::{Context, Result};
use enkou_shaders::Coefficients2;
use enkou_shaders::camera::Camera;
use enkou_shaders::camera::entry::main_camera;
use enkou_shaders::chaos_game::entry::main_chaos_game;
use enkou_shaders::transform::Transform;
use enkou_shaders::variation::Variation;
use glam::{Affine2, IVec2, UVec2, Vec2, uvec2};
use image::{GrayImage, Luma};
use std::mem;
use std::process::Command;
use tempfile::NamedTempFile;
const ITERATIONS_DISCARD: u32 = 20;
const ITERATIONS: u32 = 50_000;
const IMAGE_DIMENSION: UVec2 = uvec2(600, 600);
pub fn main() -> Result<()> {
let transforms = [
{
// F_0: (x / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
},
{
// F_1: ((x + 1) / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
},
{
// F_2: (x / 2, (y + 1) / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1))
},
];
let weights = [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
let variations = [Variation::IDENTITY];
let mut output_points_ifs = Vec::new();
output_points_ifs.resize(ITERATIONS as usize, Vec2::ZERO);
main_chaos_game(
ITERATIONS_DISCARD,
&[4u8; 32],
&transforms,
&weights,
&variations,
&mut output_points_ifs,
);
// The gasket is defined on the range [0, 1] for both X and Y
let camera = Camera::new(
IMAGE_DIMENSION,
Vec2::ONE * 0.5,
0.0,
Vec2::ZERO,
IMAGE_DIMENSION.as_vec2(),
);
let mut output_points_pixel = Vec::new();
output_points_pixel.resize(ITERATIONS as usize, IVec2::ZERO);
main_camera(&camera, &output_points_ifs, &mut output_points_pixel);
let mut image = GrayImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
let dimensions = image.dimensions();
output_points_pixel
.iter()
.skip_while(|p| {
p.x < 0 || (p.x as u32) > dimensions.0 || p.y < 0 || (p.y as u32) > dimensions.1
})
.map(|p| (p.x as u32, p.y as u32))
.for_each(|(x, y)| image.put_pixel(x, y, Luma([255u8])));
let temp = NamedTempFile::with_suffix(".png").context("Unable to create file for image")?;
image.save(temp.path()).context("Unable to save image")?;
let open_program: &str = cfg_select! {
unix => "xdg-open",
_ => panic!("No available program to open images")
};
Command::new(open_program)
.arg(temp.path())
.spawn()?
.wait()?;
// In case the image viewer forks and gives control back prior to reading the file,
// drop it and don't run the destructor
mem::forget(temp);
Ok(())
}
-36
View File
@@ -106,39 +106,3 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
Some(next_point)
}
}
/// Shader entry point for running the chaos game to produce new IFS coordinates
pub mod entry {
use crate::chaos_game::ChaosGame;
use crate::rng::xoshiro256starstar_from_seed;
use crate::transform::Transform;
use crate::variation::Variation;
use glam::Vec2;
use spirv_std::spirv;
/// Given a set of fractal flame parameters, generate new IFS coordinates
/// and store them in the output array.
#[spirv(compute(entry_point_name = "main_chaos_game", threads(1)))]
pub fn main_chaos_game(
#[spirv(spec_constant(id = 1, default = 20))] iteration_discard: u32,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] rng_seed: &[u8],
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] transforms: &[Transform],
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] weights: &[f32],
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] variations: &[Variation],
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] output: &mut [Vec2],
) {
let mut rng_seed_actual = [0u8; 32];
(0..32).for_each(|i| rng_seed_actual[i] = rng_seed[i]);
let mut rng = xoshiro256starstar_from_seed(rng_seed_actual);
let mut chaos_game = ChaosGame::new(&mut rng, transforms, weights, variations);
for _ in 0..iteration_discard {
chaos_game.next().unwrap();
}
for i in 0..output.len() {
output[i] = chaos_game.next().unwrap();
}
}
}
+5 -4
View File
@@ -1,11 +1,12 @@
//! # Enkou
#![no_std]
#![deny(missing_docs)]
#![allow(clippy::needless_range_loop)] // SPIR-V backend has issues with iteration over items
#![cfg_attr(target_arch = "spirv", no_std)]
// SPIR-V backend is unable to compile iteration over items
#![allow(clippy::needless_range_loop)]
pub mod camera;
pub mod chaos_game;
mod rng;
pub mod rng;
pub mod transform;
pub mod variation;
+4 -1
View File
@@ -1,3 +1,6 @@
//! # RNG
//!
//! Random number generation utilities for shaders
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
@@ -14,7 +17,7 @@ use rand_xoshiro::Xoshiro256StarStar;
/// This function assumes a properly-initialized state array;
/// output may silently degenerate if the initial state is all zeros,
/// so this module is private to the crate.
pub(crate) fn xoshiro256starstar_from_seed(
pub fn xoshiro256starstar_from_seed(
rng_state: <Xoshiro256StarStar as SeedableRng>::Seed,
) -> Xoshiro256StarStar {
let mut rng_state_actual = [0u64; 4];
+1 -1
View File
@@ -68,7 +68,7 @@ impl Variation {
};
/// Create a new variation by providing the variation kind, weight, and parameters.
pub fn new(kind: VariationKind, weight: f32, params: VariationParams) -> Variation {
pub const fn new(kind: VariationKind, weight: f32, params: VariationParams) -> Variation {
Variation {
kind,
weight,