Add entry points for GPU
This commit is contained in:
+37
-38
@@ -5,14 +5,9 @@
|
||||
pub mod camera;
|
||||
pub mod chaos_game;
|
||||
pub mod transform;
|
||||
mod variation;
|
||||
pub mod variation;
|
||||
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use core::f32::consts::PI;
|
||||
use glam::{Affine2, Vec3, Vec4, vec2, vec3};
|
||||
#[cfg(target_arch = "spirv")]
|
||||
use spirv_std::num_traits::Float;
|
||||
use spirv_std::spirv;
|
||||
use glam::Affine2;
|
||||
|
||||
/// Utility trait to convert between `flam3` notation and [`glam`].
|
||||
#[allow(missing_docs)]
|
||||
@@ -39,6 +34,28 @@ pub trait Coefficients2 {
|
||||
/// ```
|
||||
fn from_coefficients(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Affine2;
|
||||
|
||||
/// Convert affine transformation coefficients to the [`glam`] representation.
|
||||
/// Parameters use the following form:
|
||||
///
|
||||
/// ```text
|
||||
/// (a * x + b * y + c, d * x + e * y + f)
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// # use glam::{Affine2, vec2};
|
||||
/// # use crate::enkou_shaders::Coefficients2;
|
||||
/// let coefs = Affine2::from_coefficients_arr([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
|
||||
/// let (x, y) = (7.0, 8.0);
|
||||
/// assert_eq!(
|
||||
/// coefs.transform_point2(vec2(x, y)),
|
||||
/// vec2(
|
||||
/// coefs.a() * x + coefs.b() * y + coefs.c(),
|
||||
/// coefs.d() * x + coefs.e() * y + coefs.f()
|
||||
/// )
|
||||
/// );
|
||||
/// ```
|
||||
fn from_coefficients_arr(coefficients: [f32; 6]) -> Affine2;
|
||||
|
||||
fn a(&self) -> f32;
|
||||
fn b(&self) -> f32;
|
||||
fn c(&self) -> f32;
|
||||
@@ -48,10 +65,23 @@ pub trait Coefficients2 {
|
||||
}
|
||||
|
||||
impl Coefficients2 for Affine2 {
|
||||
#[inline]
|
||||
fn from_coefficients(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Affine2 {
|
||||
Affine2::from_cols_array(&[a, d, b, e, c, f])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_coefficients_arr(coefficients: [f32; 6]) -> Affine2 {
|
||||
Affine2::from_coefficients(
|
||||
coefficients[0],
|
||||
coefficients[1],
|
||||
coefficients[2],
|
||||
coefficients[3],
|
||||
coefficients[4],
|
||||
coefficients[5],
|
||||
)
|
||||
}
|
||||
|
||||
fn a(&self) -> f32 {
|
||||
self.matrix2.x_axis.x
|
||||
}
|
||||
@@ -76,34 +106,3 @@ impl Coefficients2 for Affine2 {
|
||||
self.translation.y
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[allow(missing_docs)]
|
||||
pub struct ShaderConstants {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub time: f32,
|
||||
}
|
||||
|
||||
#[spirv(fragment)]
|
||||
#[allow(missing_docs)]
|
||||
pub fn main_fs(vtx_color: Vec3, output: &mut Vec4) {
|
||||
*output = Vec4::from((vtx_color, 1.));
|
||||
}
|
||||
|
||||
#[spirv(vertex)]
|
||||
#[allow(missing_docs)]
|
||||
pub fn main_vs(
|
||||
#[spirv(vertex_index)] vert_id: i32,
|
||||
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] constants: &ShaderConstants,
|
||||
#[spirv(position)] vtx_pos: &mut Vec4,
|
||||
vtx_color: &mut Vec3,
|
||||
) {
|
||||
let speed = 0.4;
|
||||
let time = constants.time * speed + vert_id as f32 * (2. * PI * 120. / 360.);
|
||||
let position = vec2(f32::sin(time), f32::cos(time));
|
||||
*vtx_pos = Vec4::from((position, 0.0, 1.0));
|
||||
|
||||
*vtx_color = [vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 0., 1.)][vert_id as usize % 3];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user