Add a color coordinate to the chaos game
All shader-related code has been removed; GPU entry points will be added once there's a better understanding of how the API should work.
This commit is contained in:
@@ -36,11 +36,12 @@ impl Distribution<f32> for BiUnit {
|
||||
/// * `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,
|
||||
color: f32,
|
||||
rng: &mut R,
|
||||
transforms: &[Transform],
|
||||
weights: &[f32],
|
||||
variations: &[Variation],
|
||||
) -> (Vec2, u32) {
|
||||
) -> (Vec2, f32) {
|
||||
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
|
||||
let mut transform_index: u32 = 0;
|
||||
|
||||
@@ -53,9 +54,10 @@ pub fn step_chaos_game<R: Rng>(
|
||||
transform_index += 1;
|
||||
}
|
||||
|
||||
let transform = &transforms[transform_index as usize];
|
||||
(
|
||||
transforms[transform_index as usize].transform_point(rng, variations, point),
|
||||
transform_index,
|
||||
transform.transform_point(rng, variations, point),
|
||||
transform.transform_color(color),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -65,6 +67,7 @@ pub fn step_chaos_game<R: Rng>(
|
||||
/// New points in the chaos game are produced by iterating on the chaos game.
|
||||
pub struct ChaosGame<'a, R: Rng> {
|
||||
current_point: Vec2,
|
||||
current_color: f32,
|
||||
rng: &'a mut R,
|
||||
transforms: &'a [Transform],
|
||||
weights: &'a [f32],
|
||||
@@ -80,8 +83,10 @@ impl<'a, R: Rng> ChaosGame<'a, R> {
|
||||
variations: &'a [Variation],
|
||||
) -> Self {
|
||||
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
|
||||
let current_color = rng.sample(StandardUniform);
|
||||
ChaosGame {
|
||||
current_point,
|
||||
current_color,
|
||||
rng,
|
||||
transforms,
|
||||
weights,
|
||||
@@ -91,54 +96,20 @@ impl<'a, R: Rng> ChaosGame<'a, R> {
|
||||
}
|
||||
|
||||
impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
|
||||
type Item = Vec2;
|
||||
type Item = (Vec2, f32);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let (next_point, _) = step_chaos_game(
|
||||
let (next_point, next_color) = step_chaos_game(
|
||||
self.current_point,
|
||||
self.current_color,
|
||||
self.rng,
|
||||
self.transforms,
|
||||
self.weights,
|
||||
self.variations,
|
||||
);
|
||||
self.current_point = next_point;
|
||||
self.current_color = next_color;
|
||||
|
||||
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();
|
||||
}
|
||||
Some((next_point, next_color))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
//! # Enkou
|
||||
#![no_std]
|
||||
#![deny(missing_docs)]
|
||||
#![allow(clippy::needless_range_loop)] // SPIR-V backend has issues with iteration over items
|
||||
|
||||
pub mod camera;
|
||||
pub mod chaos_game;
|
||||
mod rng;
|
||||
pub mod transform;
|
||||
pub mod variation;
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
use rand::SeedableRng;
|
||||
use rand_xoshiro::Xoshiro256StarStar;
|
||||
|
||||
/// Convert an RNG state buffer to an instance of [`Xoshiro256StarStar`].
|
||||
///
|
||||
/// While [`SeedableRng::from_seed`] is an infallible function,
|
||||
/// it relies on some methods that can't be compiled by the SPIR-V
|
||||
/// backend (specifically, formatting functions in the core crate).
|
||||
///
|
||||
/// In practice, the xoshiro RNG state is entirely defined by its seed,
|
||||
/// so this function does the work of [`SeedableRng::from_seed`] by
|
||||
/// transmuting the seed value to an RNG instance.
|
||||
///
|
||||
/// 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(
|
||||
rng_state: <Xoshiro256StarStar as SeedableRng>::Seed,
|
||||
) -> Xoshiro256StarStar {
|
||||
let mut rng_state_actual = [0u64; 4];
|
||||
|
||||
// NOTE: Bit shifting is bad, but we don't have great alternatives:
|
||||
// - `chunks_exact` has issues with pointer casting
|
||||
// - `u64::from_le_bytes` has issues with `OpBitcast` in SPIR-V validation
|
||||
for i in 0..rng_state_actual.len() {
|
||||
for j in 0..size_of::<u64>() {
|
||||
rng_state_actual[i] |= (rng_state[i * size_of::<u64>() + j] as u64) << (j * 8);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe { core::mem::transmute(rng_state_actual) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::rng::xoshiro256starstar_from_seed;
|
||||
use core::iter::zip;
|
||||
use rand::{RngExt, SeedableRng};
|
||||
use rand_xoshiro::Xoshiro256StarStar;
|
||||
|
||||
#[test]
|
||||
fn match_seeded() {
|
||||
let mut seed: <Xoshiro256StarStar as SeedableRng>::Seed = [0u8; 32];
|
||||
for i in 0..seed.len() {
|
||||
seed[i] = i as u8;
|
||||
}
|
||||
|
||||
let rng1 = Xoshiro256StarStar::from_seed(seed).random_iter::<u64>();
|
||||
let rng2 = xoshiro256starstar_from_seed(seed).random_iter::<u64>();
|
||||
|
||||
zip(rng1, rng2)
|
||||
.take(100)
|
||||
.for_each(|(rng1_value, rng2_value)| assert_eq!(rng1_value, rng2_value));
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
//! but produce more interesting images once we add variations.
|
||||
use crate::variation::Variation;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Affine2, UVec2, Vec2};
|
||||
use glam::{Affine2, FloatExt, UVec2, Vec2};
|
||||
use rand::Rng;
|
||||
|
||||
/// Affine transform for use in the [`chaos_game`](crate::chaos_game).
|
||||
@@ -15,15 +15,25 @@ pub struct Transform {
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: f32,
|
||||
color_speed: f32,
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
/// Create a new transform from an affine transformation matrix
|
||||
pub fn new(coefficients: Affine2, coefficients_post: Affine2, variation_range: UVec2) -> Self {
|
||||
pub fn new(
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: f32,
|
||||
color_speed: f32,
|
||||
) -> Self {
|
||||
Transform {
|
||||
coefficients,
|
||||
coefficients_post,
|
||||
variation_range,
|
||||
color,
|
||||
color_speed,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,14 +57,20 @@ impl Transform {
|
||||
|
||||
self.coefficients_post.transform_point2(point)
|
||||
}
|
||||
|
||||
/// Apply this transform to a color in IFS coordinates, producing a new color
|
||||
pub fn transform_color(&self, color: f32) -> f32 {
|
||||
self.color.lerp(color, self.color_speed)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::rng::xoshiro256starstar_from_seed;
|
||||
use crate::transform::Transform;
|
||||
use crate::variation::{Variation, VariationKind};
|
||||
use glam::{Affine2, uvec2, vec2};
|
||||
use rand::SeedableRng;
|
||||
use rand_xoshiro::Xoshiro256StarStar;
|
||||
|
||||
#[test]
|
||||
fn transform_scaling() {
|
||||
@@ -63,9 +79,11 @@ mod test {
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
Affine2::IDENTITY,
|
||||
uvec2(0, 1),
|
||||
0.0,
|
||||
0.0,
|
||||
);
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed([0; 32]);
|
||||
let mut rng = Xoshiro256StarStar::from_seed([0u8; 32]);
|
||||
let variations = [Variation::IDENTITY];
|
||||
let point = vec2(1.0, 1.0);
|
||||
|
||||
@@ -78,14 +96,17 @@ mod test {
|
||||
#[test]
|
||||
fn transform_scaling_post() {
|
||||
let scale_coefficients = vec2(2.0, 0.5);
|
||||
let transform_pdj = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1));
|
||||
let transform_pdj =
|
||||
Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), 0.0, 0.0);
|
||||
let transform_pdj_post = Transform::new(
|
||||
Affine2::IDENTITY,
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
uvec2(0, 1),
|
||||
0.0,
|
||||
0.0,
|
||||
);
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed([0; 32]);
|
||||
let mut rng = Xoshiro256StarStar::from_seed([0; 32]);
|
||||
let variations = [Variation::new(VariationKind::Pdj, 1.0, [0.0f32; 4].into())];
|
||||
let point = vec2(1.0, 1.0);
|
||||
|
||||
@@ -94,4 +115,24 @@ mod test {
|
||||
|
||||
assert_eq!(point_pdj * scale_coefficients, point_pdj_post);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transform_color() {
|
||||
let starting_color = 0.0;
|
||||
let transform_color = 1.0;
|
||||
let transform_speed = 0.5;
|
||||
|
||||
let transform = Transform::new(
|
||||
Affine2::IDENTITY,
|
||||
Affine2::IDENTITY,
|
||||
uvec2(0, 1),
|
||||
starting_color,
|
||||
transform_speed,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
transform.transform_color(starting_color),
|
||||
starting_color * (1.0 - transform_speed) + transform_color * transform_speed
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user