Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Update wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed May 3, 2022
1 parent fe0ed68 commit ac4b413
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 69 deletions.
21 changes: 11 additions & 10 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl SDFInstance {
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct Globals {
resolution: [f32; 2],
_padding: [f32; 2], // TODO: this is needed for aligning to 16 bit
layer_clippings: [[[f32; 2]; 2]; MAX_CLIPPINGS],
}

Expand Down Expand Up @@ -444,6 +445,7 @@ impl WgpuGraphicsDevice {
0,
bytemuck::cast_slice(&[Globals {
resolution: [self.width as _, self.height as _],
_padding: [0., 0.],
layer_clippings: self.layer_clippings.to_array(),
}]),
);
Expand Down
67 changes: 34 additions & 33 deletions src/rust/src/shaders/sdf_shape.wgsl
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
struct VertexInput {
@location(0) pos: vec2<f32>;
@location(0) pos: vec2<f32>,
};

struct VertexOutput {
@builtin(position) coords: vec4<f32>;
@location(0) center: vec2<f32>;
@location(1) radius: f32;
@location(2) stroke_width: f32;
@location(3) fill_color: u32;
@location(4) stroke_color: u32;
@builtin(position) coords: vec4<f32>,
@location(0) center: vec2<f32>,
@location(1) radius: f32,
@location(2) stroke_width: f32,
@location(3) fill_color: u32,
@location(4) stroke_color: u32,

};

let MAX_CLIPPINGS = 64;


struct GlobalsUniform {
@location(0) resolution: vec2<f32>;
@location(1) layer_clippings: array<mat2x2<f32>, MAX_CLIPPINGS>;
@location(0) resolution: vec2<f32>,
// TODO: this @size annotation is needed otherwise I get "offset 8 is not a multiple of the required alignment 16" error
@align(16)
@location(1) layer_clippings: array<mat2x2<f32>, MAX_CLIPPINGS>,
};

@group(0)
@binding(0)
@group(0) @binding(0)
var<uniform> globals: GlobalsUniform;

struct InstanceInput {
@location(1) center: vec2<f32>;
@location(2) radius: f32;
@location(3) stroke_width: f32;
@location(4) fill_color: u32;
@location(5) stroke_color: u32;
@location(6) z: f32;
@location(1) center: vec2<f32>,
@location(2) radius: f32,
@location(3) stroke_width: f32,
@location(4) fill_color: u32,
@location(5) stroke_color: u32,
@location(6) z: f32,
};

@stage(vertex)
@vertex
fn vs_main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
var out: VertexOutput;
var vs_out: VertexOutput;

out.coords = vec4<f32>(model.pos, instance.z, 1.0);
vs_out.coords = vec4<f32>(model.pos, instance.z, 1.0);
// Y-axis is opposite
out.center = vec2<f32>(instance.center.x, globals.resolution.y - instance.center.y);
out.radius = instance.radius;
out.stroke_width = instance.stroke_width;
out.fill_color = instance.fill_color;
out.stroke_color = instance.stroke_color;
vs_out.center = vec2<f32>(instance.center.x, globals.resolution.y - instance.center.y);
vs_out.radius = instance.radius;
vs_out.stroke_width = instance.stroke_width;
vs_out.fill_color = instance.fill_color;
vs_out.stroke_color = instance.stroke_color;

return out;
return vs_out;
}

@stage(fragment)
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
@fragment
fn fs_main(vs_out: VertexOutput) -> @location(0) vec4<f32> {
// width to apply anti-aliase
let HALF_PIXEL = 0.5;

var fill_color: vec4<f32> = unpack4x8unorm(in.fill_color);
var stroke_color: vec4<f32> = unpack4x8unorm(in.stroke_color);
var fill_color: vec4<f32> = unpack4x8unorm(vs_out.fill_color);
var stroke_color: vec4<f32> = unpack4x8unorm(vs_out.stroke_color);

var dist_fill = distance(in.coords.xy, in.center) - in.radius;
var dist_stroke_inner = distance(in.coords.xy, in.center) - (in.radius - in.stroke_width * 0.5);
var dist_stroke_outer = distance(in.coords.xy, in.center) - (in.radius + in.stroke_width * 0.5);
var dist_fill = distance(vs_out.coords.xy, vs_out.center) - vs_out.radius;
var dist_stroke_inner = distance(vs_out.coords.xy, vs_out.center) - (vs_out.radius - vs_out.stroke_width * 0.5);
var dist_stroke_outer = distance(vs_out.coords.xy, vs_out.center) - (vs_out.radius + vs_out.stroke_width * 0.5);

// TODO: A poor-man's anti-aliasing. I don't know how to do it correctly atm...
fill_color.a *= clamp(HALF_PIXEL - dist_fill, 0.0, 1.0);
Expand Down
51 changes: 26 additions & 25 deletions src/rust/src/shaders/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
struct VertexInput {
@location(0) pos: vec3<f32>;
@location(1) color: u32;
@location(2) clipping_id: i32;
@location(0) pos: vec3<f32>,
@location(1) color: u32,
@location(2) clipping_id: i32,
};

struct VertexOutput {
@builtin(position) coords: vec4<f32>;
@location(0) color: u32;
@location(1) clipping_id: i32;
@builtin(position) coords: vec4<f32>,
@location(0) color: u32,
@location(1) clipping_id: i32,
};

let MAX_LAYERS = 8;

struct GlobalsUniform {
@location(0) resolution: vec2<f32>;
@location(1) layer_clippings: array<mat2x2<f32>, MAX_LAYERS>;
@location(0) resolution: vec2<f32>,
// TODO: this @size annotation is needed otherwise I get "offset 8 is not a multiple of the required alignment 16" error
@align(16)
@location(1) layer_clippings: array<mat2x2<f32>, MAX_LAYERS>,
};

@group(0)
@binding(0)
@group(0) @binding(0)
var<uniform> globals: GlobalsUniform;

@stage(vertex)
@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
var vs_out: VertexOutput;

out.color = model.color;
vs_out.color = model.color;

// Scale the X and Y positions from [0, width or height] to [-1, 1]
out.coords = vec4<f32>(2.0 * model.pos.xy / globals.resolution - 1.0, model.pos.z, 1.0);
vs_out.coords = vec4<f32>(2.0 * model.pos.xy / globals.resolution - 1.0, model.pos.z, 1.0);

out.clipping_id = model.clipping_id;
vs_out.clipping_id = model.clipping_id;

return out;
return vs_out;
}

@stage(fragment)
@fragment
fn fs_main(
in: VertexOutput
vs_out: VertexOutput
) -> @location(0) vec4<f32> {
var within_clip: bool = false;

// If the clipping ID is negative, no clipping
if (in.clipping_id < 0) {
if (vs_out.clipping_id < 0) {
within_clip = true;
} else {
// Note to self: at the fragment stage, `position` represents the 2D pixel
// position in framebuffer space, which is NOT [-1, 1].
var bottom_left: vec2<f32> = vec2<f32>(
globals.layer_clippings[in.clipping_id][0].x,
globals.layer_clippings[vs_out.clipping_id][0].x,
// Y-axis is from top to bottom, so the coordinates needs to be flipped.
// TODO: probably I can write this more nicely with vector products
globals.resolution.y - globals.layer_clippings[in.clipping_id][1].y,
globals.resolution.y - globals.layer_clippings[vs_out.clipping_id][1].y,
);
var top_right: vec2<f32> = vec2<f32>(
globals.layer_clippings[in.clipping_id][1].x,
globals.resolution.y - globals.layer_clippings[in.clipping_id][0].y,
globals.layer_clippings[vs_out.clipping_id][1].x,
globals.resolution.y - globals.layer_clippings[vs_out.clipping_id][0].y,
);

// TOOD: Can I do this more nicely with vector products? (c.f.
// https://math.stackexchange.com/a/190373)
within_clip = all((bottom_left <= in.coords.xy) & (in.coords.xy <= top_right));
within_clip = all((bottom_left <= vs_out.coords.xy) & (vs_out.coords.xy <= top_right));
}

if (!within_clip) {
Expand All @@ -74,5 +75,5 @@ fn fs_main(
//
// https://github.com/wch/r-source/blob/8ebcb33a9f70e729109b1adf60edd5a3b22d3c6f/src/include/R_ext/GraphicsDevice.h#L766-L796
// https://www.w3.org/TR/WGSL/#unpack-builtin-functions
return unpack4x8unorm(in.color);
return unpack4x8unorm(vs_out.color);
}
7 changes: 7 additions & 0 deletions src/rust/src/shaders/test.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct GlobalsUniform {
@location(0) @size(16) resolution: vec2<f32>,
@location(1) mat_array: array<mat2x2<f32>, 3>,
};

@group(0) @binding(0)
var<uniform> globals: GlobalsUniform;
1 change: 0 additions & 1 deletion wgpugd.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageCleanBeforeInstall: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

0 comments on commit ac4b413

Please sign in to comment.