feat: Include coefs in the transform
This commit is contained in:
parent
ff2f7f859d
commit
0bc36bf3d5
@ -255,30 +255,4 @@ pub mod tests {
|
|||||||
let result = block_on(output_fut);
|
let result = block_on(output_fut);
|
||||||
assert_eq!(result, glam::vec2(0.5, 0.5));
|
assert_eq!(result, glam::vec2(0.5, 0.5));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn apply_linear_transform_cpu() {
|
|
||||||
let transform = TransformSpec {
|
|
||||||
coefs: Coefs::identity(),
|
|
||||||
post_coefs: Coefs::identity(),
|
|
||||||
variation_offset: 0,
|
|
||||||
variation_count: 1,
|
|
||||||
};
|
|
||||||
let variations = vec![VariationSpec {
|
|
||||||
kind: VariationKind::Linear,
|
|
||||||
weight: 0.5,
|
|
||||||
params: VariationParams::default(),
|
|
||||||
}];
|
|
||||||
let point_in = glam::vec2(1., 1.);
|
|
||||||
|
|
||||||
let mut point_out = glam::vec2(0., 0.);
|
|
||||||
flare_shader::apply_transform_f32(
|
|
||||||
glam::uvec3(0, 0, 0),
|
|
||||||
&transform,
|
|
||||||
&variations,
|
|
||||||
&point_in,
|
|
||||||
&mut point_out,
|
|
||||||
);
|
|
||||||
assert_eq!(point_out, glam::vec2(0.5, 0.5));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,25 @@
|
|||||||
#![no_std]
|
#![cfg_attr(not(test), no_std)]
|
||||||
pub mod transforms;
|
pub mod transforms;
|
||||||
pub mod variations;
|
pub mod variations;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
pub use transforms::*;
|
pub use transforms::*;
|
||||||
pub use variations::*;
|
pub use variations::*;
|
||||||
|
|
||||||
use spirv_std::glam::{UVec3, Vec2};
|
use spirv_std::glam;
|
||||||
use spirv_std::spirv;
|
use spirv_std::spirv;
|
||||||
|
|
||||||
#[spirv(compute(threads(1)))]
|
#[spirv(compute(threads(1)))]
|
||||||
pub fn apply_transform_f32(
|
pub fn apply_transform_f32(
|
||||||
#[spirv(global_invocation_id)] _global_id: UVec3,
|
#[spirv(global_invocation_id)] _global_id: glam::UVec3,
|
||||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] transform: &TransformSpec,
|
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] transform: &TransformSpec,
|
||||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] variations: &[VariationSpec],
|
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] variations: &[VariationSpec],
|
||||||
#[spirv(uniform, descriptor_set = 0, binding = 2)] point_in: &Vec2,
|
#[spirv(uniform, descriptor_set = 0, binding = 2)] point_in: &glam::Vec2,
|
||||||
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] point_out: &mut Vec2,
|
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] point_out: &mut glam::Vec2,
|
||||||
) {
|
) {
|
||||||
// let mut point = transform.coefs.transform_point2(*point_in);
|
let mut point = transform.coefs.transform_point(*point_in);
|
||||||
let mut point = *point_in;
|
|
||||||
|
|
||||||
let offset = transform.variation_offset as usize;
|
let offset = transform.variation_offset as usize;
|
||||||
let count = transform.variation_count as usize;
|
let count = transform.variation_count as usize;
|
||||||
@ -25,6 +27,5 @@ pub fn apply_transform_f32(
|
|||||||
point = variations[i].apply(point);
|
point = variations[i].apply(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
// *point_out = transform.post_coefs.transform_point2(point);
|
*point_out = transform.post_coefs.transform_point(point);
|
||||||
*point_out = point;
|
|
||||||
}
|
}
|
||||||
|
80
crates/flare-shader/src/tests.rs
Normal file
80
crates/flare-shader/src/tests.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
use crate::{Coefs, TransformSpec, VariationKind, VariationParams, VariationSpec};
|
||||||
|
use spirv_std::glam;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn apply_transform_coefs() {
|
||||||
|
let transform = TransformSpec {
|
||||||
|
coefs: Coefs::new(1., 2., 3., 4., 5., 6.),
|
||||||
|
post_coefs: Coefs::identity(),
|
||||||
|
variation_offset: 0,
|
||||||
|
variation_count: 1,
|
||||||
|
};
|
||||||
|
let variations = vec![VariationSpec {
|
||||||
|
kind: VariationKind::Linear,
|
||||||
|
weight: 1.,
|
||||||
|
params: VariationParams::default(),
|
||||||
|
}];
|
||||||
|
let point_in = glam::vec2(1., 1.);
|
||||||
|
|
||||||
|
let mut point_out = glam::vec2(0., 0.);
|
||||||
|
crate::apply_transform_f32(
|
||||||
|
glam::uvec3(0, 0, 0),
|
||||||
|
&transform,
|
||||||
|
&variations,
|
||||||
|
&point_in,
|
||||||
|
&mut point_out,
|
||||||
|
);
|
||||||
|
assert_eq!(point_out, glam::vec2(6., 15.));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn apply_transform_post_coefs() {
|
||||||
|
let transform = TransformSpec {
|
||||||
|
coefs: Coefs::identity(),
|
||||||
|
post_coefs: Coefs::new(1., 2., 3., 4., 5., 6.),
|
||||||
|
variation_offset: 0,
|
||||||
|
variation_count: 1,
|
||||||
|
};
|
||||||
|
let variations = vec![VariationSpec {
|
||||||
|
kind: VariationKind::Linear,
|
||||||
|
weight: 1.,
|
||||||
|
params: VariationParams::default(),
|
||||||
|
}];
|
||||||
|
let point_in = glam::vec2(1., 1.);
|
||||||
|
|
||||||
|
let mut point_out = glam::vec2(0., 0.);
|
||||||
|
crate::apply_transform_f32(
|
||||||
|
glam::uvec3(0, 0, 0),
|
||||||
|
&transform,
|
||||||
|
&variations,
|
||||||
|
&point_in,
|
||||||
|
&mut point_out,
|
||||||
|
);
|
||||||
|
assert_eq!(point_out, glam::vec2(6., 15.));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn apply_transform_variation() {
|
||||||
|
let transform = TransformSpec {
|
||||||
|
coefs: Coefs::identity(),
|
||||||
|
post_coefs: Coefs::identity(),
|
||||||
|
variation_offset: 0,
|
||||||
|
variation_count: 1,
|
||||||
|
};
|
||||||
|
let variations = vec![VariationSpec {
|
||||||
|
kind: VariationKind::Linear,
|
||||||
|
weight: 0.5,
|
||||||
|
params: VariationParams::default(),
|
||||||
|
}];
|
||||||
|
let point_in = glam::vec2(1., 1.);
|
||||||
|
|
||||||
|
let mut point_out = glam::vec2(0., 0.);
|
||||||
|
crate::apply_transform_f32(
|
||||||
|
glam::uvec3(0, 0, 0),
|
||||||
|
&transform,
|
||||||
|
&variations,
|
||||||
|
&point_in,
|
||||||
|
&mut point_out,
|
||||||
|
);
|
||||||
|
assert_eq!(point_out, glam::vec2(0.5, 0.5));
|
||||||
|
}
|
@ -1,12 +1,14 @@
|
|||||||
|
use spirv_std::glam;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Copy, Clone, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Coefs {
|
pub struct Coefs {
|
||||||
a: f32,
|
pub a: f32,
|
||||||
b: f32,
|
pub b: f32,
|
||||||
c: f32,
|
pub c: f32,
|
||||||
d: f32,
|
pub d: f32,
|
||||||
e: f32,
|
pub e: f32,
|
||||||
f: f32,
|
pub f: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Coefs {
|
impl Coefs {
|
||||||
@ -20,6 +22,21 @@ impl Coefs {
|
|||||||
f: 0.,
|
f: 0.,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
|
||||||
|
Coefs { a, b, c, d, e, f }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transform_point(&self, point: glam::Vec2) -> glam::Vec2 {
|
||||||
|
let affine: glam::Affine2 = (*self).into();
|
||||||
|
affine.transform_point2(point)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Coefs> for glam::Affine2 {
|
||||||
|
fn from(value: Coefs) -> Self {
|
||||||
|
glam::Affine2::from_cols_array(&[value.a, value.d, value.b, value.e, value.c, value.f])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Copy, Clone, Default, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
@ -30,3 +47,10 @@ pub struct TransformSpec {
|
|||||||
pub variation_offset: u32,
|
pub variation_offset: u32,
|
||||||
pub variation_count: u32,
|
pub variation_count: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TransformSpec {
|
||||||
|
pub fn apply_coefs(&self, point: glam::Vec2) -> glam::Vec2 {
|
||||||
|
let affine: glam::Affine2 = self.coefs.into();
|
||||||
|
affine.transform_point2(point)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user