From 6a7ce141374d38988b0919efefd678bc246434e2 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 4 Jul 2026 12:23:57 -0400 Subject: [PATCH] Start fixes for the gasket example The image is inverted for reasons I don't entirely understand yet --- enkou-shaders/examples/gasket.rs | 17 +++++++++------ enkou-shaders/src/camera.rs | 36 +++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/enkou-shaders/examples/gasket.rs b/enkou-shaders/examples/gasket.rs index 21b2af9..5b147ed 100644 --- a/enkou-shaders/examples/gasket.rs +++ b/enkou-shaders/examples/gasket.rs @@ -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())); } } diff --git a/enkou-shaders/src/camera.rs b/enkou-shaders/src/camera.rs index 93adbee..90b0af6 100644 --- a/enkou-shaders/src/camera.rs +++ b/enkou-shaders/src/camera.rs @@ -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::{floorf, powf}; +use libm::{floorf, log10f, powf}; /// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`) /// to RGBA colors. @@ -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,17 +157,16 @@ 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; } } }