Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2549124902 | |||
| e688de5204 | |||
| 81a1a2a254 | |||
| fd49ae7256 | |||
| 7ff19631ba | |||
| 9ea4261a84 | |||
| 81f23c1bd8 | |||
| 08ada94bd2 | |||
| 4005e14ab0 | |||
| 3bd01da563 |
@@ -21,7 +21,10 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
- run: cargo check
|
||||
with:
|
||||
components: clippy
|
||||
- run: cargo check --all-targets
|
||||
- run: cargo clippy --all-targets
|
||||
- run: cargo test
|
||||
|
||||
test-gpu:
|
||||
@@ -30,5 +33,5 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
- run: cargo install --git https://github.com/rust-gpu/rust-gpu cargo-gpu
|
||||
- run: cargo install --git https://github.com/rust-gpu/rust-gpu cargo-gpu --rev 67f1ff2
|
||||
- run: cargo gpu check --auto-install-rust-toolchain -p enkou-shaders
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ bytemuck = { version = "1.25.0", features = ["derive"] }
|
||||
glam = { version = "0.33.1", default-features = false, features = ["bytemuck", "scalar-math"] }
|
||||
image = { version = "0.25.10", default-features = false, features = ["default-formats"]}
|
||||
libm = "0.2.16"
|
||||
rand = { version = "0.10.1", default-features = false }
|
||||
rand = { version = "0.10.1", default-features = false }
|
||||
rand_xoshiro = "0.8.1"
|
||||
rspirv = "0.13.0"
|
||||
tempfile = "3.27.0"
|
||||
|
||||
@@ -16,12 +16,7 @@ pub fn main() -> anyhow::Result<()> {
|
||||
builder.build_script.defaults = true;
|
||||
builder.shader_panic_strategy = ShaderPanicStrategy::SilentExit;
|
||||
builder.spirv_metadata = SpirvMetadata::Full;
|
||||
builder.capabilities = vec![
|
||||
Capability::Int8,
|
||||
Capability::Int16,
|
||||
Capability::Int64,
|
||||
Capability::Float64,
|
||||
];
|
||||
builder.capabilities = vec![Capability::Int8, Capability::Int16, Capability::Int64];
|
||||
|
||||
let compile_result = builder.build()?;
|
||||
let spv_path = compile_result.module.unwrap_single();
|
||||
|
||||
@@ -19,15 +19,12 @@ mod test {
|
||||
}
|
||||
|
||||
fn has_entry_point(execution_model: ExecutionModel, name: &str) -> bool {
|
||||
for ref entry_point in shader().entry_points.iter() {
|
||||
for entry_point in shader().entry_points.iter() {
|
||||
let operands: Vec<Operand> = entry_point
|
||||
.operands
|
||||
.iter()
|
||||
.filter(|op| match op {
|
||||
Operand::ExecutionModel(_) | Operand::LiteralString(_) => true,
|
||||
_ => false,
|
||||
})
|
||||
.map(|op| op.clone())
|
||||
.filter(|op| matches!(op, Operand::ExecutionModel(_) | Operand::LiteralString(_)))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
assert_eq!(operands.len(), 2);
|
||||
@@ -56,18 +53,10 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn has_entry_main_chaos_game() {
|
||||
fn has_entry_main_camera() {
|
||||
assert!(has_entry_point(
|
||||
ExecutionModel::GLCompute,
|
||||
"main_chaos_game"
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn has_entry_main_image_render() {
|
||||
assert!(has_entry_point(
|
||||
ExecutionModel::GLCompute,
|
||||
"main_image_render"
|
||||
"main_image_accumulate"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,47 @@
|
||||
use anyhow::{Context, Result};
|
||||
use enkou_shaders::Coefficients2;
|
||||
use enkou_shaders::camera::Camera;
|
||||
use enkou_shaders::camera::entry::main_image_render;
|
||||
use enkou_shaders::chaos_game::entry::main_chaos_game;
|
||||
use enkou_shaders::chaos_game::ChaosGame;
|
||||
use enkou_shaders::image::{BlendMode, ImageSettings};
|
||||
use enkou_shaders::transform::Transform;
|
||||
use enkou_shaders::variation::Variation;
|
||||
use glam::{Affine2, UVec2, Vec2, Vec4, uvec2, vec2};
|
||||
use image::{Rgba, Rgba32FImage};
|
||||
use glam::{Affine2, UVec2, Vec2, uvec2, vec2};
|
||||
use image::{GrayImage, Luma};
|
||||
use rand::SeedableRng;
|
||||
use rand_xoshiro::Xoshiro256StarStar;
|
||||
use std::mem;
|
||||
use std::process::Command;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
const ITERATIONS_DISCARD: u32 = 20;
|
||||
const ITERATIONS: u32 = 50_000;
|
||||
const ITERATIONS_DISCARD: usize = 20;
|
||||
const ITERATIONS: usize = 50_000;
|
||||
const IMAGE_DIMENSION: UVec2 = uvec2(600, 600);
|
||||
|
||||
pub fn main() -> Result<()> {
|
||||
let mut rng = Xoshiro256StarStar::from_seed([4u8; 32]);
|
||||
|
||||
let transforms = [
|
||||
// F_0: (x / 2, y / 2)
|
||||
Transform::new(
|
||||
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0),
|
||||
uvec2(0, 1),
|
||||
vec2(0.0, 0.0),
|
||||
),
|
||||
// F_1: ((x + 1) / 2, y / 2)
|
||||
Transform::new(
|
||||
Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0),
|
||||
uvec2(0, 1),
|
||||
vec2(0.0, 0.0),
|
||||
),
|
||||
// F_2: (x / 2, (y + 1) / 2)
|
||||
Transform::new(
|
||||
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5),
|
||||
uvec2(0, 1),
|
||||
vec2(0.0, 0.0),
|
||||
),
|
||||
{
|
||||
// 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), vec2(0.0, 1.0))
|
||||
},
|
||||
{
|
||||
// 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), vec2(0.0, 1.0))
|
||||
},
|
||||
{
|
||||
// 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), vec2(0.0, 1.0))
|
||||
},
|
||||
];
|
||||
|
||||
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, Vec4::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,
|
||||
@@ -62,35 +51,26 @@ pub fn main() -> Result<()> {
|
||||
IMAGE_DIMENSION.as_vec2(),
|
||||
);
|
||||
|
||||
let palette = &[Vec4::ONE; 2];
|
||||
let image_settings = ImageSettings::new(BlendMode::Linear, IMAGE_DIMENSION);
|
||||
|
||||
let mut output_points_pixel = Vec::new();
|
||||
output_points_pixel.resize(ITERATIONS as usize, Vec4::ZERO);
|
||||
let mut image = GrayImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
|
||||
|
||||
main_image_render(
|
||||
&camera,
|
||||
palette,
|
||||
&output_points_ifs,
|
||||
&mut output_points_pixel,
|
||||
);
|
||||
let chaos_game = ChaosGame::new(&mut rng, &transforms, &weights, &variations);
|
||||
|
||||
let mut image = Rgba32FImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
|
||||
for x in 0..image.dimensions().0 {
|
||||
for y in 0..image.dimensions().1 {
|
||||
let pixel_index = y * IMAGE_DIMENSION.x + x;
|
||||
let pixel = output_points_pixel[pixel_index as usize];
|
||||
image.put_pixel(x, y, Rgba(pixel.into()));
|
||||
}
|
||||
}
|
||||
chaos_game
|
||||
.skip(ITERATIONS_DISCARD)
|
||||
.take(ITERATIONS)
|
||||
.map(|(point_ifs, _)| camera.transform_point(point_ifs))
|
||||
.filter_map(|point_pixel| image_settings.transform_point_to_image(point_pixel))
|
||||
.for_each(|point_pixel| image.put_pixel(point_pixel.x, point_pixel.y, Luma([255])));
|
||||
|
||||
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 => Some("xdg-open"),
|
||||
_ => None,
|
||||
}
|
||||
.expect("No available program to open images");
|
||||
unix => "xdg-open",
|
||||
_ => panic!("No available program to open images")
|
||||
};
|
||||
|
||||
Command::new(open_program)
|
||||
.arg(temp.path())
|
||||
|
||||
+37
-164
@@ -2,47 +2,8 @@
|
||||
//!
|
||||
//! Map points from the IFS coordinate system to pixel coordinates. This is a lossy transformation.
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Affine2, IVec2, UVec2, Vec2, Vec4, Vec4Swizzles, vec2};
|
||||
use libm::{floorf, powf};
|
||||
|
||||
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
|
||||
/// to RGBA colors.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(u32)]
|
||||
pub enum BlendMode {
|
||||
/// Map IFS color values to a linear blend of the nearest two palette colors
|
||||
Linear = 0,
|
||||
|
||||
/// Map IFS color values to the nearest single palette color
|
||||
Step = 1,
|
||||
}
|
||||
|
||||
impl Default for BlendMode {
|
||||
fn default() -> Self {
|
||||
BlendMode::Linear
|
||||
}
|
||||
}
|
||||
|
||||
impl BlendMode {
|
||||
/// Map an IFS color value to RGBA color from the provided palette.
|
||||
pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 {
|
||||
let colors_m_one = palette.len() - 1;
|
||||
let period = 1.0 / colors_m_one as f32;
|
||||
let index_lower = floorf(color / period) as usize;
|
||||
let index_upper = (index_lower + 1).clamp(0, colors_m_one);
|
||||
let rem = color % period / period;
|
||||
|
||||
match self {
|
||||
BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
|
||||
BlendMode::Step => palette[index_lower],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
|
||||
unsafe impl bytemuck::Zeroable for BlendMode {}
|
||||
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
|
||||
unsafe impl bytemuck::Pod for BlendMode {}
|
||||
use glam::{Affine2, IVec2, UVec2, Vec2, vec2};
|
||||
use libm::powf;
|
||||
|
||||
/// Settings used to map IFS coordinates to pixel coordinates.
|
||||
///
|
||||
@@ -51,10 +12,7 @@ unsafe impl bytemuck::Pod for BlendMode {}
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct Camera {
|
||||
dimensions: UVec2,
|
||||
transform: Affine2,
|
||||
blend_mode: BlendMode,
|
||||
image_gamma: f32,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
@@ -64,17 +22,17 @@ impl Camera {
|
||||
/// to express the transform steps individually.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `blend_mode` - Color blending mode for the output image
|
||||
/// * `dimensions` - Width and height of the output image (in pixels).
|
||||
/// * `center` - Location of the origin in IFS coordinates. Positive `x` shifts the image
|
||||
/// left, and positive `y` position shifts the image up.
|
||||
/// left, and positive `y` position shifts the image up.
|
||||
/// * `rotate` - Rotation angle (in radians) of IFS coordinates. Rotation is applied after the
|
||||
/// `center` translation, so it is about the new origin.
|
||||
/// `center` translation, so it is about the new origin.
|
||||
/// * `zoom` - Zoom factor applied to IFS coordinates. IFS coordinates are scaled by
|
||||
/// `pow(2, zoom)`, so a zoom factor of 0 is the identity.
|
||||
/// `pow(2, zoom)`, so a zoom factor of 0 is the identity.
|
||||
/// * `scale` - Pixels per unit of IFS coordinates. This parameter is usually chosen such
|
||||
/// that the largest dimension will cover the range `[-2, 2]`, but values higher or lower
|
||||
/// can be used as a secondary zoom.
|
||||
/// that the largest dimension will cover the range `[-2, 2]`, but values higher or lower
|
||||
/// can be used as a secondary zoom.
|
||||
pub fn new(dimensions: UVec2, center: Vec2, rotate: f32, zoom: Vec2, scale: Vec2) -> Camera {
|
||||
let ifs_center_transform = Affine2::from_translation(-center);
|
||||
let zoom_transform = Affine2::from_scale(vec2(powf(2.0, zoom.x), powf(2.0, zoom.y)));
|
||||
@@ -88,125 +46,39 @@ impl Camera {
|
||||
* zoom_transform
|
||||
* ifs_center_transform;
|
||||
|
||||
Camera {
|
||||
dimensions,
|
||||
transform,
|
||||
blend_mode: BlendMode::Linear,
|
||||
image_gamma: 1.5,
|
||||
}
|
||||
Camera { transform }
|
||||
}
|
||||
|
||||
fn transform_point(&self, point: Vec2) -> IVec2 {
|
||||
self.transform.transform_point2(point).as_ivec2()
|
||||
}
|
||||
|
||||
/// Map a point from IFS coordinates to a pixel index and RGBA value; if the IFS coordinate
|
||||
/// is outside the viewable range, return [`None`].
|
||||
pub fn transform_point_to_image(&self, point: Vec4, palette: &[Vec4]) -> Option<(usize, Vec4)> {
|
||||
let pixel_coordinates = self.transform_point(point.xy());
|
||||
if pixel_coordinates.x < 0
|
||||
|| pixel_coordinates.y < 0
|
||||
|| (pixel_coordinates.x as u32) >= self.dimensions.x
|
||||
|| (pixel_coordinates.y as u32) >= self.dimensions.y
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let to_pixel_index = self.dimensions.with_y(0);
|
||||
let pixel_index = pixel_coordinates.as_uvec2().dot(to_pixel_index) as usize;
|
||||
|
||||
let rgba = self.blend_mode.ifs_to_rgb(point.w, palette);
|
||||
|
||||
Some((pixel_index, rgba))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub mod entry {
|
||||
use crate::camera::Camera;
|
||||
use glam::Vec4;
|
||||
use libm::log10f;
|
||||
use spirv_std::spirv;
|
||||
|
||||
/// Render an output image from a list of IFS coordinates.
|
||||
/// Map a point from IFS coordinates to pixel coordinates.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * `camera` - Camera settings for mapping IFS coordinates to pixel coordinates
|
||||
/// * `palette` - Color palette to use when mapping IFS color to RGB colors. Individual elements
|
||||
/// are assumed to be RGBA values on the scale of `[0, 1]`
|
||||
/// * `coordinates_ifs` - IFS coordinates to use for the output image
|
||||
/// * `image` - Buffer for the output image
|
||||
#[spirv(compute(entry_point_name = "main_image_render", threads(1)))]
|
||||
pub fn main_image_render(
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] camera: &Camera,
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] palette: &[Vec4],
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] coordinates_ifs: &[Vec4],
|
||||
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] image: &mut [Vec4],
|
||||
) {
|
||||
for coordinate_index in 0..coordinates_ifs.len() {
|
||||
camera
|
||||
.transform_point_to_image(coordinates_ifs[coordinate_index], palette)
|
||||
.map(|(pixel_index, rgba)| image[pixel_index] += rgba);
|
||||
}
|
||||
|
||||
for pixel_index in 0..image.len() {
|
||||
// TODO: Fix the bootleg gamma adjustment
|
||||
let pixel_unscaled = image[pixel_index];
|
||||
let pixel =
|
||||
pixel_unscaled * log10f(pixel_unscaled.w) / (pixel_unscaled.w * camera.image_gamma);
|
||||
image[pixel_index] = pixel;
|
||||
}
|
||||
/// ```
|
||||
/// # use glam::{vec2, ivec2, uvec2, Vec2};
|
||||
/// # use crate::enkou_shaders::camera::Camera;
|
||||
/// // Output image is 600x600 pixels, centered at the origin, no rotation, no zoom,
|
||||
/// // and scaled such that it covers the range [-2, 2].
|
||||
/// // Use the origin as the IFS coordinate, so the pixel coordinate is the center of the image
|
||||
/// let camera = Camera::new(
|
||||
/// uvec2(600, 600),
|
||||
/// Vec2::ZERO,
|
||||
/// 0.0,
|
||||
/// Vec2::ZERO,
|
||||
/// vec2(150.0, 150.0)
|
||||
/// );
|
||||
/// assert_eq!(camera.transform_point(vec2(0.0, 0.0)), ivec2(300, 300));
|
||||
/// ```
|
||||
pub fn transform_point(&self, point: Vec2) -> IVec2 {
|
||||
self.transform.transform_point2(point).as_ivec2()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::camera::{BlendMode, Camera};
|
||||
use glam::{Affine2, Vec2, Vec4, ivec2, uvec2, vec2};
|
||||
use crate::camera::Camera;
|
||||
use glam::{Affine2, Vec2, ivec2, uvec2, vec2};
|
||||
use libm::powf;
|
||||
|
||||
fn vec4s(value: f32) -> Vec4 {
|
||||
Vec4::splat(value)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blend_linear() {
|
||||
let ifs_to_rgb = |color, palette| BlendMode::Linear.ifs_to_rgb(color, palette);
|
||||
|
||||
let palette = &[vec4s(0.0), vec4s(1.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(0.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.5));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(1.0));
|
||||
|
||||
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.0));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(3.0));
|
||||
|
||||
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0), vec4s(4.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.5));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(4.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blend_step() {
|
||||
let ifs_to_rgb = |color, palette| BlendMode::Step.ifs_to_rgb(color, palette);
|
||||
|
||||
let palette = &[vec4s(0.0), vec4s(1.0)];
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.0));
|
||||
|
||||
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.25, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.4, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), palette[1]);
|
||||
assert_eq!(ifs_to_rgb(0.7, palette), palette[1]);
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), palette[2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn camera_manual() {
|
||||
fn manual_camera() {
|
||||
let starting_point = vec2(1.0, 1.0);
|
||||
|
||||
// Move the origin; points move right and up by one unit, giving us (2.0, 2.0)
|
||||
@@ -238,13 +110,14 @@ mod test {
|
||||
|
||||
// The camera is implemented by composing affine transforms,
|
||||
// which ends up with a slightly different result because of rounding.
|
||||
let error = camera.transform_point(starting_point) - point;
|
||||
assert!(error.x.abs() <= 1);
|
||||
assert!(error.y.abs() <= 1);
|
||||
let error = (camera.transform_point(starting_point) - point)
|
||||
.abs()
|
||||
.as_uvec2();
|
||||
assert!(error.x <= 1 && error.y <= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn camera_point_outside_image() {
|
||||
fn point_outside_camera() {
|
||||
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
|
||||
let camera = Camera::new(
|
||||
uvec2(1000, 1000),
|
||||
@@ -259,7 +132,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn camera_point_outside_image_negative() {
|
||||
fn point_outside_camera_negative() {
|
||||
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
|
||||
let camera = Camera::new(
|
||||
uvec2(1000, 1000),
|
||||
@@ -274,7 +147,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn camera_aspect_ratio() {
|
||||
fn aspect_ratio() {
|
||||
// Scale 100 for an image 1600 x 900 gives an effective X range of [-8, 8],
|
||||
// and effective Y range of [-4.5, 4.5]
|
||||
let camera = Camera::new(
|
||||
|
||||
@@ -40,9 +40,9 @@ pub fn step_chaos_game<R: Rng>(
|
||||
transforms: &[Transform],
|
||||
weights: &[f32],
|
||||
variations: &[Variation],
|
||||
) -> (Vec2, f32, usize) {
|
||||
) -> (Vec2, f32, u32) {
|
||||
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
|
||||
let mut transform_index: usize = 0;
|
||||
let mut transform_index: u32 = 0;
|
||||
|
||||
for i in 0..weights.len() {
|
||||
choice_weight -= weights[i];
|
||||
@@ -53,7 +53,8 @@ pub fn step_chaos_game<R: Rng>(
|
||||
transform_index += 1;
|
||||
}
|
||||
|
||||
let ref transform = transforms[transform_index];
|
||||
let transform = transforms[transform_index as usize];
|
||||
|
||||
(
|
||||
transform.transform_point(rng, variations, point),
|
||||
transform.transform_color(color),
|
||||
@@ -82,9 +83,11 @@ impl<'a, R: Rng> ChaosGame<'a, R> {
|
||||
weights: &'a [f32],
|
||||
variations: &'a [Variation],
|
||||
) -> Self {
|
||||
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
|
||||
let current_color = rng.sample(StandardUniform);
|
||||
ChaosGame {
|
||||
current_point: vec2(rng.sample(BiUnit), rng.sample(BiUnit)),
|
||||
current_color: rng.sample(StandardUniform),
|
||||
current_point,
|
||||
current_color,
|
||||
rng,
|
||||
transforms,
|
||||
weights,
|
||||
@@ -111,48 +114,3 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
|
||||
Some((next_point, next_color))
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::Vec4;
|
||||
use spirv_std::spirv;
|
||||
|
||||
/// Given a set of fractal flame parameters, generate new IFS coordinates
|
||||
/// and store them in the output array.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * `iteration_discard` - Choas game steps to discard prior to recording into the output buffer
|
||||
/// * `output` - Output buffer to record chaos game steps into. Because of alignment issues,
|
||||
/// the output is recorded as a [`Vec4`]; the IFS (x, y) coordinate is in `x` and `y`,
|
||||
/// and color is in `w`
|
||||
#[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 [Vec4],
|
||||
) {
|
||||
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()
|
||||
.map(|output| (output.0, 0.0, output.1).into())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//! Image Accumulate
|
||||
|
||||
use crate::camera::Camera;
|
||||
use crate::chaos_game::ChaosGame;
|
||||
use crate::image::ImageSettings;
|
||||
use crate::rng::xoshiro256starstar_from_seed;
|
||||
use crate::transform::Transform;
|
||||
use crate::variation::Variation;
|
||||
use glam::{UVec2, Vec4};
|
||||
use spirv_std::spirv;
|
||||
|
||||
/// Run the chaos game and accumulate points into the output image buffer
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `iterations` - Controls the iteration count; the first `x` iterations are discarded,
|
||||
/// the next `y` iterations are accumulated into the output image
|
||||
/// * `rng_seed`
|
||||
/// * `transforms`
|
||||
/// * `weights`
|
||||
/// * `variations`
|
||||
/// * `camera` - Camera transformation to map IFS coordinates to pixel coordinates
|
||||
/// * `image_settings` - Settings to use for image accumulation
|
||||
/// * `palette` - List of colors to use for the image palette; assumed to be RGB values scaled to `[0-255]`, with an alpha of 255
|
||||
/// * `image` - Output image buffer
|
||||
#[spirv(compute(entry_point_name = "main_image_accumulate", threads(1)))]
|
||||
pub fn main_image_accumulate(
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] iterations: &UVec2,
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] rng_seed: &[u8],
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] transforms: &[Transform],
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] weights: &[f32],
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 4)] variations: &[Variation],
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 5)] camera: &Camera,
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 6)] image_settings: &ImageSettings,
|
||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 7)] palette: &[Vec4],
|
||||
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] image: &mut [Vec4],
|
||||
) {
|
||||
let mut rng_seed_actual = [0u8; 32];
|
||||
for i in 0..rng_seed_actual.len() {
|
||||
rng_seed_actual[i] = rng_seed[i];
|
||||
}
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed(rng_seed_actual);
|
||||
|
||||
let chaos_game = ChaosGame::new(&mut rng, transforms, weights, variations);
|
||||
let (iterations_fuse, iterations_accumulate) = (iterations.x, iterations.y);
|
||||
|
||||
let ifs_to_image = |(ifs_point, ifs_color)| {
|
||||
let pixel_coordinates = camera.transform_point(ifs_point);
|
||||
let pixel_color = image_settings.transform_color(ifs_color, palette);
|
||||
image_settings
|
||||
.transform_point_to_index(pixel_coordinates)
|
||||
.map(|pixel_index| (pixel_index, pixel_color))
|
||||
};
|
||||
|
||||
for ifs_point in chaos_game
|
||||
.skip(iterations_fuse as usize)
|
||||
.take(iterations_accumulate as usize)
|
||||
{
|
||||
if let Some((pixel_index, pixel_color)) = ifs_to_image(ifs_point) {
|
||||
image[pixel_index as usize] = pixel_color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
//! # Entry
|
||||
//!
|
||||
//! Entry points for Enkou shaders
|
||||
pub mod image_accumulate;
|
||||
@@ -0,0 +1,155 @@
|
||||
//! Image
|
||||
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{IVec2, UVec2, Vec4};
|
||||
use libm::floorf;
|
||||
|
||||
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
|
||||
/// to RGBA colors.
|
||||
#[derive(Copy, Clone, Default)]
|
||||
#[repr(u32)]
|
||||
pub enum BlendMode {
|
||||
/// Map IFS color values to a linear blend of the nearest two palette colors
|
||||
#[default]
|
||||
Linear = 0,
|
||||
|
||||
/// Map IFS color values to the nearest single palette color
|
||||
Step = 1,
|
||||
}
|
||||
|
||||
impl BlendMode {
|
||||
/// Map an IFS color value to RGBA color from the provided palette.
|
||||
pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 {
|
||||
let colors_m_one = palette.len() - 1;
|
||||
let period = 1.0 / colors_m_one as f32;
|
||||
let index_lower = floorf(color / period) as usize;
|
||||
let index_upper = (index_lower + 1).clamp(0, colors_m_one);
|
||||
let rem = color % period / period;
|
||||
|
||||
match self {
|
||||
BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
|
||||
BlendMode::Step => palette[index_lower],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
|
||||
unsafe impl bytemuck::Zeroable for BlendMode {}
|
||||
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
|
||||
unsafe impl bytemuck::Pod for BlendMode {}
|
||||
|
||||
/// Settings to use for mapping the IFS coordinates to an output image
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct ImageSettings {
|
||||
blend_mode: BlendMode,
|
||||
dimensions: UVec2,
|
||||
}
|
||||
|
||||
impl ImageSettings {
|
||||
/// Create a new settings object
|
||||
pub fn new(blend_mode: BlendMode, dimensions: UVec2) -> Self {
|
||||
ImageSettings {
|
||||
blend_mode,
|
||||
dimensions,
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a point from camera coordinates to pixel coordinates,
|
||||
/// and check that the result is within the provided image dimensions.
|
||||
pub fn transform_point_to_image(&self, point: IVec2) -> Option<UVec2> {
|
||||
if 0 <= point.x
|
||||
&& (point.x as u32) < self.dimensions.x
|
||||
&& 0 <= point.y
|
||||
&& (point.y as u32) < self.dimensions.y
|
||||
{
|
||||
Some(point.as_uvec2())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a point from camera coordinates to a final pixel index
|
||||
pub fn transform_point_to_index(&self, point: IVec2) -> Option<u32> {
|
||||
self.transform_point_to_image(point)
|
||||
.map(|pixel| self.dimensions.with_x(1).dot(pixel))
|
||||
}
|
||||
|
||||
/// Map an IFS color coordinate to the palette RGB value
|
||||
pub fn transform_color(&self, color: f32, palette: &[Vec4]) -> Vec4 {
|
||||
self.blend_mode.ifs_to_rgb(color, palette)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::image::{BlendMode, ImageSettings};
|
||||
use glam::{Vec4, ivec2, uvec2};
|
||||
|
||||
#[test]
|
||||
fn blend_linear() {
|
||||
let ifs_to_rgb = |color, palette| BlendMode::Linear.ifs_to_rgb(color, palette);
|
||||
|
||||
let palette = &[Vec4::splat(0.0), Vec4::splat(1.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(0.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(0.5));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(1.0));
|
||||
|
||||
let palette = &[Vec4::splat(1.0), Vec4::splat(2.0), Vec4::splat(3.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(1.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(2.0));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(3.0));
|
||||
|
||||
let palette = &[
|
||||
Vec4::splat(1.0),
|
||||
Vec4::splat(2.0),
|
||||
Vec4::splat(3.0),
|
||||
Vec4::splat(4.0),
|
||||
];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(1.0));
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(2.5));
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(4.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blend_step() {
|
||||
let ifs_to_rgb = |color, palette| BlendMode::Step.ifs_to_rgb(color, palette);
|
||||
|
||||
let palette = &[Vec4::splat(0.0), Vec4::splat(1.0)];
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(0.0));
|
||||
|
||||
let palette = &[Vec4::splat(1.0), Vec4::splat(2.0), Vec4::splat(3.0)];
|
||||
assert_eq!(ifs_to_rgb(0.0, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.25, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.4, palette), palette[0]);
|
||||
assert_eq!(ifs_to_rgb(0.5, palette), palette[1]);
|
||||
assert_eq!(ifs_to_rgb(0.7, palette), palette[1]);
|
||||
assert_eq!(ifs_to_rgb(1.0, palette), palette[2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_bounds() {
|
||||
let image_settings = ImageSettings::new(BlendMode::Linear, uvec2(100, 100));
|
||||
|
||||
assert!(
|
||||
image_settings
|
||||
.transform_point_to_image(ivec2(-1, -1))
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
image_settings
|
||||
.transform_point_to_image(ivec2(0, 0))
|
||||
.is_some()
|
||||
);
|
||||
assert!(
|
||||
image_settings
|
||||
.transform_point_to_image(ivec2(99, 99))
|
||||
.is_some()
|
||||
);
|
||||
assert!(
|
||||
image_settings
|
||||
.transform_point_to_image(ivec2(100, 100))
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
//! # Enkou
|
||||
#![no_std]
|
||||
#![warn(missing_docs)]
|
||||
// SPIR-V backend has issues with iteration over items:
|
||||
#![allow(clippy::needless_range_loop)]
|
||||
#![allow(clippy::manual_memcpy)]
|
||||
// Shader entry points are expected to have a lot of arguments:
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
pub mod camera;
|
||||
pub mod chaos_game;
|
||||
pub mod entry;
|
||||
pub mod image;
|
||||
mod rng;
|
||||
pub mod transform;
|
||||
pub mod variation;
|
||||
|
||||
@@ -14,17 +14,19 @@ 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.
|
||||
// Temporarily unused, will be required once the main image accumulation entry point is implemented
|
||||
#[allow(unused)]
|
||||
pub(crate) fn xoshiro256starstar_from_seed(
|
||||
rng_state: <Xoshiro256StarStar as SeedableRng>::Seed,
|
||||
) -> Xoshiro256StarStar {
|
||||
let mut rng_state_actual = [0u64; 4];
|
||||
|
||||
// NOTE: Bit shifting is tedious, but we don't have great alternatives:
|
||||
// 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;
|
||||
rng_state_actual[i] |= (rng_state[i * size_of::<u64>() + j] as u64) << (j * 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,20 +13,22 @@ use rand::Rng;
|
||||
#[repr(C)]
|
||||
pub struct Transform {
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: Vec2,
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
/// Create a new transform from an affine transformation matrix
|
||||
///
|
||||
/// Arguments:
|
||||
/// * `coefficients` - Affine transform coefficients for this transformation. Applied prior to variations
|
||||
/// * `variation_range` - (half-open) range of variations to apply during [`Self::transform_point`]
|
||||
/// * `color` - Color value and speed to apply during [`Self::transform_color`]
|
||||
pub fn new(coefficients: Affine2, variation_range: UVec2, color: Vec2) -> Self {
|
||||
pub fn new(
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: Vec2,
|
||||
) -> Self {
|
||||
Transform {
|
||||
coefficients,
|
||||
coefficients_post,
|
||||
variation_range,
|
||||
color,
|
||||
}
|
||||
@@ -46,14 +48,14 @@ impl Transform {
|
||||
let variation_start = self.variation_range.x;
|
||||
let variation_end = self.variation_range.y;
|
||||
for variation_index in variation_start..variation_end {
|
||||
let ref variation = variations[variation_index as usize];
|
||||
let variation = &variations[variation_index as usize];
|
||||
point_output += variation.transform_point(point, rng, &self.coefficients)
|
||||
}
|
||||
|
||||
point_output
|
||||
self.coefficients_post.transform_point2(point)
|
||||
}
|
||||
|
||||
/// Mix an existing color with this transform's color
|
||||
/// Apply this transform to a color
|
||||
pub fn transform_color(&self, color: f32) -> f32 {
|
||||
color.lerp(self.color.x, self.color.y)
|
||||
}
|
||||
@@ -61,96 +63,73 @@ impl Transform {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Coefficients2;
|
||||
use crate::rng::xoshiro256starstar_from_seed;
|
||||
use crate::transform::Transform;
|
||||
use crate::variation::{Variation, VariationKind};
|
||||
use core::convert::Infallible;
|
||||
use glam::{Affine2, Vec2, uvec2, vec2};
|
||||
use rand::TryRng;
|
||||
|
||||
struct NullRng;
|
||||
|
||||
impl NullRng {
|
||||
pub fn new() -> Self {
|
||||
NullRng
|
||||
}
|
||||
}
|
||||
|
||||
impl TryRng for NullRng {
|
||||
type Error = Infallible;
|
||||
|
||||
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
|
||||
dst.iter_mut().for_each(|b| *b = 0);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_point_identity() {
|
||||
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.0));
|
||||
let variations = [Variation::IDENTITY];
|
||||
|
||||
let transform_point =
|
||||
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
|
||||
for (input, expected) in [
|
||||
(vec2(0.0, 1.0), vec2(0.0, 1.0)),
|
||||
(vec2(1.0, 0.0), vec2(1.0, 0.0)),
|
||||
(vec2(1.0, 1.0), vec2(1.0, 1.0)),
|
||||
] {
|
||||
assert_eq!(transform_point(input), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_point_scaling() {
|
||||
fn transform_scaling() {
|
||||
let scale_coefficients = vec2(2.0, 0.5);
|
||||
let transform = Transform::new(
|
||||
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0),
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
Affine2::IDENTITY,
|
||||
uvec2(0, 1),
|
||||
vec2(0.0, 0.0),
|
||||
Vec2::ZERO,
|
||||
);
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed([0; 32]);
|
||||
let variations = [Variation::IDENTITY];
|
||||
let point = vec2(1.0, 1.0);
|
||||
|
||||
let transform_point =
|
||||
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
|
||||
for (input, expected) in [
|
||||
(vec2(0.0, 1.0), vec2(0.0, 0.5)),
|
||||
(vec2(1.0, 0.0), vec2(0.5, 0.0)),
|
||||
(vec2(1.0, 1.0), vec2(0.5, 0.5)),
|
||||
] {
|
||||
assert_eq!(transform_point(input), expected);
|
||||
}
|
||||
assert_eq!(
|
||||
transform.transform_point(&mut rng, &variations, point),
|
||||
scale_coefficients
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_point_scaling_variation() {
|
||||
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.0));
|
||||
let variations = [Variation::new(VariationKind::Linear, 2.0, [0.0; 4].into())];
|
||||
fn transform_scaling_post() {
|
||||
let scale_coefficients = vec2(2.0, 0.5);
|
||||
let transform_pdj = Transform::new(
|
||||
Affine2::IDENTITY,
|
||||
Affine2::IDENTITY,
|
||||
uvec2(0, 1),
|
||||
Vec2::ZERO,
|
||||
);
|
||||
let transform_pdj_post = Transform::new(
|
||||
Affine2::IDENTITY,
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
uvec2(0, 1),
|
||||
Vec2::ZERO,
|
||||
);
|
||||
|
||||
let transform_point =
|
||||
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
|
||||
for (input, expected) in [
|
||||
(vec2(0.0, 1.0), vec2(0.0, 2.0)),
|
||||
(vec2(1.0, 0.0), vec2(2.0, 0.0)),
|
||||
(vec2(1.0, 1.0), vec2(2.0, 2.0)),
|
||||
] {
|
||||
assert_eq!(transform_point(input), expected);
|
||||
}
|
||||
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);
|
||||
|
||||
let point_pdj = transform_pdj.transform_point(&mut rng, &variations, point);
|
||||
let point_pdj_post = transform_pdj_post.transform_point(&mut rng, &variations, point);
|
||||
|
||||
assert_eq!(point_pdj * scale_coefficients, point_pdj_post);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_color_mixing() {
|
||||
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.5));
|
||||
fn transform_color() {
|
||||
// Color 0.5, color speed 1.0, so color value will always be 0.5 after transform
|
||||
let color = vec2(0.5, 1.0);
|
||||
let transform = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), color);
|
||||
|
||||
assert_eq!(transform.transform_color(0.0), 0.0);
|
||||
assert_eq!(transform.transform_color(0.0), 0.5);
|
||||
assert_eq!(transform.transform_color(1.0), 0.5);
|
||||
assert_eq!(transform.transform_color(0.5), 0.25);
|
||||
assert_eq!(transform.transform_color(2.0), 0.5);
|
||||
|
||||
// Color 1.0, color speed 0.5, so color value moves to halfway between current and 1.0
|
||||
let color = vec2(1.0, 0.5);
|
||||
let transform = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), color);
|
||||
|
||||
assert_eq!(transform.transform_color(0.0), 0.5);
|
||||
assert_eq!(transform.transform_color(1.0), 1.0);
|
||||
assert_eq!(transform.transform_color(2.0), 1.5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ use rand::{Rng, RngExt};
|
||||
pub struct VariationParams([f32; 4]);
|
||||
|
||||
impl From<[f32; 4]> for VariationParams {
|
||||
fn from(value: [f32; 4]) -> Self {
|
||||
VariationParams(value)
|
||||
fn from(v: [f32; 4]) -> Self {
|
||||
VariationParams(v)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user