8 Commits

Author SHA1 Message Date
bspeice 6a7ce14137 Start fixes for the gasket example
CI / cargo fmt (push) Successful in 25s
CI / cargo test (push) Successful in 13m26s
CI / cargo test (GPU) (push) Successful in 12m50s
The image is inverted for reasons I don't entirely understand yet
2026-07-04 12:23:57 -04:00
bspeice d79ff7be21 Merge remote-tracking branch 'origin/color' into color 2026-07-04 11:19:34 -04:00
bspeice 0d9d2b693e Fix palette blend modes, add unit tests
CI / cargo fmt (push) Successful in 27s
CI / cargo test (push) Successful in 13m16s
CI / cargo test (GPU) (push) Successful in 12m53s
2026-07-03 17:48:35 -04:00
bspeice 5bd325d0ea Implement camera colors
Still needs some unit tests, and to fix the gasket example
2026-07-03 17:48:35 -04:00
bspeice 4e508884ae Implement transform colors 2026-07-03 17:48:35 -04:00
bspeice 4005e14ab0 Merge pull request 'Include examples as part of cargo check' (#5) from ci_all_targets into main
CI / cargo fmt (push) Successful in 22s
CI / cargo test (push) Successful in 13m7s
CI / cargo test (GPU) (push) Successful in 12m35s
Reviewed-on: #5
2026-07-03 17:48:20 -04:00
bspeice 3bd01da563 Include examples as part of cargo check
CI / cargo fmt (push) Successful in 23s
CI / cargo test (push) Successful in 13m22s
CI / cargo test (GPU) (push) Successful in 12m35s
2026-07-03 16:23:11 -04:00
bspeice dfc9cf821c Fix palette blend modes, add unit tests
CI / cargo fmt (push) Successful in 21s
CI / cargo test (GPU) (push) Has been cancelled
CI / cargo test (push) Has been cancelled
2026-07-03 16:18:01 -04:00
3 changed files with 89 additions and 32 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v6
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo check
- run: cargo check --all-targets
- run: cargo test
test-gpu:
+11 -6
View File
@@ -5,8 +5,8 @@ use enkou_shaders::camera::entry::main_image_render;
use enkou_shaders::chaos_game::entry::main_chaos_game;
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, Vec2Swizzles, Vec4, uvec2, vec2};
use image::{Rgba, RgbaImage};
use std::mem;
use std::process::Command;
use tempfile::NamedTempFile;
@@ -65,7 +65,7 @@ pub fn main() -> Result<()> {
let palette = &[Vec4::ONE; 2];
let mut output_points_pixel = Vec::new();
output_points_pixel.resize(ITERATIONS as usize, Vec4::ZERO);
output_points_pixel.resize(IMAGE_DIMENSION.xy().element_product() as usize, Vec4::ZERO);
main_image_render(
&camera,
@@ -74,11 +74,16 @@ pub fn main() -> Result<()> {
&mut output_points_pixel,
);
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 mut image = RgbaImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
for y in 0..image.dimensions().1 {
for x in 0..image.dimensions().0 {
let pixel_index = y * IMAGE_DIMENSION.x + x;
let pixel = output_points_pixel[pixel_index as usize];
let pixel = pixel.to_array().map(|channel| {
let channel = if channel.is_nan() { 0.0 } else { channel };
let channel = channel * u8::MAX as f32;
channel as u8
});
image.put_pixel(x, y, Rgba(pixel.into()));
}
}
+77 -25
View File
@@ -3,7 +3,7 @@
//! 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::{ceilf, floorf, powf};
use libm::{floorf, log10f, powf};
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
/// to RGBA colors.
@@ -26,15 +26,15 @@ impl Default for BlendMode {
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 palette_index = color * palette.len() as f32;
let palette_index_lower = floorf(palette_index) as usize;
let palette_index_upper = ceilf(palette_index) as usize;
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[palette_index_lower] + palette[palette_index_upper]) / 2.0
}
BlendMode::Step => palette[palette_index_lower],
BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
BlendMode::Step => palette[index_lower],
}
}
}
@@ -102,7 +102,11 @@ impl Camera {
/// 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)> {
pub fn transform_point_to_image_hist(
&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
@@ -112,20 +116,29 @@ impl Camera {
return None;
}
let to_pixel_index = self.dimensions.with_y(0);
let to_pixel_index = self.dimensions.with_y(1);
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))
}
/// Map an accumulated RGBA value to the final pixel color value
pub fn transform_image_hist_to_rgba(&self, pixel: Vec4) -> Vec4 {
if pixel.w <= 0.0 {
Vec4::ZERO
} else {
// TODO: Fix the bootleg gamma adjustment
(pixel * log10f(pixel.w) / (pixel.w * self.image_gamma)).clamp(Vec4::ZERO, Vec4::ONE)
}
}
}
#[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.
@@ -144,29 +157,68 @@ pub mod entry {
#[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);
if let Some((pixel_index, rgba)) =
camera.transform_point_to_image_hist(coordinates_ifs[coordinate_index], palette)
{
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;
let rgba = camera.transform_image_hist_to_rgba(image[pixel_index]);
image[pixel_index] = rgba;
}
}
}
#[cfg(test)]
mod test {
use crate::camera::Camera;
use glam::{Affine2, Vec2, ivec2, uvec2, vec2};
use crate::camera::{BlendMode, Camera};
use glam::{Affine2, Vec2, Vec4, ivec2, uvec2, vec2};
use libm::powf;
fn vec4s(value: f32) -> Vec4 {
Vec4::splat(value)
}
#[test]
fn manual_camera() {
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() {
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)
@@ -204,7 +256,7 @@ mod test {
}
#[test]
fn point_outside_camera() {
fn camera_point_outside_image() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new(
uvec2(1000, 1000),
@@ -219,7 +271,7 @@ mod test {
}
#[test]
fn point_outside_camera_negative() {
fn camera_point_outside_image_negative() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new(
uvec2(1000, 1000),
@@ -234,7 +286,7 @@ mod test {
}
#[test]
fn aspect_ratio() {
fn camera_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(