Skip to content

Commit

Permalink
mid refactor - primitives, transform, gradient hooked up
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelselleck committed Nov 1, 2024
1 parent a0fbf78 commit 6fe22bc
Show file tree
Hide file tree
Showing 13 changed files with 850 additions and 927 deletions.
1 change: 1 addition & 0 deletions pax-chassis-common/src/core_graphics_c_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub extern "C" fn pax_tick(
(
CoreGraphicsContext::new_y_up(ctx, height as f64, None),
Box::new(|| (/* clear screen here */)),
Box::new(|| (/* resize render texture backend here */)),
)
});

Expand Down
2 changes: 1 addition & 1 deletion pax-chassis-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include = ["/src", "/interface/public"]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]
default = ["console_error_panic_hook", "gpu"]
designtime = ["dep:pax-designtime", "pax-runtime/designtime"]
gpu = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ struct Globals {
_pad2: u32,
};

struct Primitive {
fill_id_and_type: u32,
z_index: i32,
clipping_id: u32, //not used atm
transform_id: u32,
};

struct Primitives {
primitives: array<Primitive, 512>,
};


struct Transform {
// OBS transform: mat3x2<f32> has different alignment on WebGL vs for
// example metal (don't change this unless you know what you are doing)
Expand All @@ -29,16 +17,8 @@ struct Transform {
_pad2: u32,
}

struct Transforms {
transforms: array<Transform, 64>,
}


struct Colors {
colors: array<vec4<f32>, 512>,
}

struct Gradient {
// TODO change type to support radius/solid etc
struct MeshMetadata {
colors: array<vec4<f32>, 8>,
stops_set1: vec4<f32>,
stops_set2: vec4<f32>,
Expand All @@ -47,19 +27,14 @@ struct Gradient {
off_axis: vec2<f32>,
stop_count: u32,
type_id: u32,
_pad: array<vec4<u32>, 4>,
}

struct Gradients {
gradients: array<Gradient, 64>,
_pad0: array<vec4<u32>, 4>,
}


@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var<uniform> u_primitives: Primitives;
@group(0) @binding(2) var<uniform> transforms: Transforms;
@group(0) @binding(3) var<uniform> colors: Colors;
@group(0) @binding(4) var<uniform> gradients: Gradients;
@group(0) @binding(1) var<uniform> transform: Transform;

@group(1) @binding(0) var<uniform> mesh_metadata: MeshMetadata;

struct GpuVertex {
@location(0) position: vec2<f32>,
Expand All @@ -69,7 +44,6 @@ struct GpuVertex {

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) @interpolate(flat) prim_id: u32,
};

@vertex
Expand All @@ -78,55 +52,45 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
var p = model.position;

// apply transform
let primitive = u_primitives.primitives[model.prim_id];
let m = transforms.transforms[primitive.transform_id];

let m = transform;
let t_p_x = p.x * m.xx + p.y * m.yx + m.zx;
let t_p_y = p.x * m.xy + p.y * m.yy + m.zy;

var pos = vec2<f32>(t_p_x, t_p_y);

pos /= globals.resolution;
pos *= 2.0;
pos -= 1.0;
pos.y *= -1.0;

out.prim_id = model.prim_id;
out.clip_position = vec4<f32>(pos, 0.0, 1.0);
return out;
}

// Fragment shader
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {

let primitive = u_primitives.primitives[in.prim_id];

//color/gradient
let fill_id_and_type = primitive.fill_id_and_type;
//clipping rectangle
let fill_id = fill_id_and_type & 0xFFFFu;
let fill_type = fill_id_and_type >> 16u;
let type_id = mesh_metadata.type_id;
var color: vec4<f32>;
if fill_type == 0u {
color = colors.colors[fill_id];
if type_id == 0u {
color = mesh_metadata.colors[0];
} else {
let p = in.clip_position.xy;
color = gradient(fill_id, p);
color = gradient(p);
}
return color;
}


fn gradient(fill_id: u32, coord: vec2<f32>) -> vec4<f32> {
let gradient = gradients.gradients[fill_id];

fn gradient(coord: vec2<f32>) -> vec4<f32> {
let gradient = mesh_metadata;
// Calculate color space position
let g_p = gradient.position * f32(globals.dpr);
let g_a = gradient.main_axis * f32(globals.dpr);
let p_t = coord - g_p;

// TODO check type id here, and calculate color_space (0.0 to 1.0) using
// distance instead of projection to do radial gradients!
let m_a_l = length(g_a);
let n = g_a / m_a_l;
let color_space = dot(p_t, n);
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pax-pixels/src/render_backend/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const MAX_GRADIENT_STOPS: usize = 8;

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Pod, Zeroable)]
pub(crate) struct GpuGradient {
pub(crate) struct GpuMeshMetadata {
pub colors: [[f32; 4]; MAX_GRADIENT_STOPS],
pub stops: [f32; MAX_GRADIENT_STOPS],
pub position: [f32; 2],
Expand Down
Loading

0 comments on commit 6fe22bc

Please sign in to comment.