Files
enkou/enkou-shaders/src/chaos_game.rs
T
bspeice 53d5ad1422
CI / cargo fmt (push) Failing after 1m5s
CI / cargo test (push) Failing after 2m15s
CI / cargo test (GPU) (push) Successful in 16m58s
First attempt at a GPU runner
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)
```
2026-07-29 11:12:52 -04:00

109 lines
3.1 KiB
Rust

//! # Chaos Game
//!
//! Fractal flames are a class of
//! [iterated function systems](https://en.wikipedia.org/wiki/Iterated_function_system)
//! that generate images following a simple algorithm:
//!
//! - Pick a starting point `(x, y)`
//! - Iterate:
//! - Pick a [`Transform`] from the set of available transforms
//! - Apply the current point to the chosen transform, generating a new point `(x, y)`
//! - Plot the new point `(x, y)`
//!
//! This algorithm is also known as the ["chaos game"](https://en.wikipedia.org/wiki/Chaos_game),
//! and it forms the basic system for producing images.
use crate::transform::Transform;
use crate::variation::Variation;
use rand::distr::{Distribution, StandardUniform};
use rand::{Rng, RngExt};
use spirv_std::glam::{Vec2, vec2};
struct BiUnit;
impl Distribution<f32> for BiUnit {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
rng.sample::<f32, _>(StandardUniform) * 2.0 - 1.0
}
}
/// Iterate one step in the chaos game; choose the next transform, apply it,
/// and return the resulting point. Also returns the transform index so that
/// path-dependent weights (the "Xaos" table in Apophysis) can be chosen
/// for the next iteration step.
///
/// # Arguments
///
/// * `weights` - Weights are assumed to be normalized; adding all elements together should return the value 1
pub fn step_chaos_game<R: Rng>(
point: Vec2,
rng: &mut R,
transforms: &[Transform],
weights: &[f32],
variations: &[Variation],
) -> (Vec2, u32) {
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
let mut transform_index: u32 = 0;
for i in 0..weights.len() {
choice_weight -= weights[i];
if choice_weight <= 0.0 {
break;
}
transform_index += 1;
}
(
transforms[transform_index as usize].transform_point(rng, variations, point),
transform_index,
)
}
/// Iterator for chaos game state. Holds the current point and references to all other data
/// necessary to generate fractal flame images.
///
/// New points in the chaos game are produced by iterating on the chaos game.
pub struct ChaosGame<'a, R: Rng> {
current_point: Vec2,
rng: &'a mut R,
transforms: &'a [Transform],
weights: &'a [f32],
variations: &'a [Variation],
}
impl<'a, R: Rng> ChaosGame<'a, R> {
/// Create a new chaos game iterator
pub fn new(
rng: &'a mut R,
transforms: &'a [Transform],
weights: &'a [f32],
variations: &'a [Variation],
) -> Self {
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
ChaosGame {
current_point,
rng,
transforms,
weights,
variations,
}
}
}
impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
let (next_point, _) = step_chaos_game(
self.current_point,
self.rng,
self.transforms,
self.weights,
self.variations,
);
self.current_point = next_point;
Some(next_point)
}
}