Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pgpu-render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ pub struct PgpuRect {
/// Computes the bounding box for the glyph after applying the specified
/// transform.
#[no_mangle]
pub unsafe extern "C" fn pgpu_glyph_bbox(glyph: *const PgpuGlyph, transform: &[f32; 6]) -> PgpuRect {
pub unsafe extern "C" fn pgpu_glyph_bbox(
glyph: *const PgpuGlyph,
transform: &[f32; 6],
) -> PgpuRect {
let transform = piet_scene::geometry::Affine::new(transform);
let rect = (*glyph).bbox(Some(transform));
PgpuRect {
Expand Down
9 changes: 7 additions & 2 deletions pgpu-render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use piet_gpu::{EncodedSceneRef, PixelFormat, RenderConfig};
use piet_gpu_hal::{QueryPool, Session};
use piet_scene::glyph::pinot::{types::Tag, FontDataRef};
use piet_scene::geometry::{Affine, Rect};
use piet_scene::glyph::pinot::{types::Tag, FontDataRef};
use piet_scene::glyph::{GlyphContext, GlyphProvider};
use piet_scene::resource::ResourceContext;
use piet_scene::scene::{Fragment, Scene};
Expand Down Expand Up @@ -214,7 +214,12 @@ pub struct PgpuGlyph {
impl PgpuGlyph {
pub fn bbox(&self, transform: Option<Affine>) -> Rect {
if let Some(transform) = &transform {
Rect::from_points(self.fragment.points().iter().map(|p| p.transform(transform)))
Rect::from_points(
self.fragment
.points()
.iter()
.map(|p| p.transform(transform)),
)
} else {
Rect::from_points(self.fragment.points())
}
Expand Down
3 changes: 2 additions & 1 deletion piet-gpu/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ fn main() -> Result<(), Error> {
println!("parsing time: {:?}", start.elapsed());
test_scenes::render_svg(&mut ctx, &svg);
} else {
test_scenes::render_scene(&mut ctx);
//test_scenes::render_scene(&mut ctx);
test_scenes::render_blend_grid(&mut ctx);
}

let mut renderer = Renderer::new(&session, WIDTH, HEIGHT, 1)?;
Expand Down
2 changes: 1 addition & 1 deletion piet-gpu/bin/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn main() -> Result<(), Error> {
}

let mut ctx = PietGpuRenderContext::new();
let test_blend = false;
let test_blend = true;
if let Some(svg) = &svg {
test_scenes::render_svg(&mut ctx, svg);
} else if test_blend {
Expand Down
53 changes: 42 additions & 11 deletions piet-gpu/shader/blend.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define Blend_Saturation 13
#define Blend_Color 14
#define Blend_Luminosity 15
#define Blend_Clip 128

vec3 screen(vec3 cb, vec3 cs) {
return cb + cs - (cb * cs);
Expand Down Expand Up @@ -45,20 +46,20 @@ vec3 hard_light(vec3 cb, vec3 cs) {
return mix(
screen(cb, 2.0 * cs - 1.0),
cb * 2.0 * cs,
vec3(lessThanEqual(cs, vec3(0.5)))
lessThanEqual(cs, vec3(0.5))
);
}

vec3 soft_light(vec3 cb, vec3 cs) {
vec3 d = mix(
sqrt(cb),
((16.0 * cb - vec3(12.0)) * cb + vec3(4.0)) * cb,
vec3(lessThanEqual(cb, vec3(0.25)))
lessThanEqual(cb, vec3(0.25))
);
return mix(
cb + (2.0 * cs - vec3(1.0)) * (d - cb),
cb - (vec3(1.0) - 2.0 * cs) * cb * (vec3(1.0) - cb),
vec3(lessThanEqual(cs, vec3(0.5)))
lessThanEqual(cs, vec3(0.5))
);
}

Expand Down Expand Up @@ -122,6 +123,8 @@ vec3 set_sat(vec3 c, float s) {
return c;
}

// Blends two RGB colors together. The colors are assumed to be in sRGB
// color space, and this function does not take alpha into account.
vec3 mix_blend(vec3 cb, vec3 cs, uint mode) {
vec3 b = vec3(0.0);
switch (mode) {
Expand Down Expand Up @@ -190,9 +193,10 @@ vec3 mix_blend(vec3 cb, vec3 cs, uint mode) {
#define Comp_DestAtop 10
#define Comp_Xor 11
#define Comp_Plus 12
#define Comp_PlusDarker 13
#define Comp_PlusLighter 14
#define Comp_PlusLighter 13

// Apply general compositing operation.
// Inputs are separated colors and alpha, output is premultiplied.
vec4 mix_compose(vec3 cb, vec3 cs, float ab, float as, uint mode) {
float fa = 0.0;
float fb = 0.0;
Expand Down Expand Up @@ -245,16 +249,43 @@ vec4 mix_compose(vec3 cb, vec3 cs, float ab, float as, uint mode) {
fa = 1.0;
fb = 1.0;
break;
case Comp_PlusDarker:
return vec4(max(vec4(0.0), 1.0 - as * vec4(cs, as) + 1.0 - ab * vec4(cb, ab)).xyz,
max(0.0, 1.0 - as + 1.0 - ab));
case Comp_PlusLighter:
return vec4(min(vec4(1.0), as * vec4(cs, as) + ab * vec4(cb, ab)).xyz,
min(1.0, as + ab));
return min(vec4(1.0), vec4(as * cs + ab * cb, as + ab));
default:
break;
}
return as * fa * vec4(cs, as) + ab * fb * vec4(cb, ab);
float as_fa = as * fa;
float ab_fb = ab * fb;
vec3 co = as_fa * cs + ab_fb * cb;
return vec4(co, as_fa + ab_fb);
}

#define BlendComp_default (Blend_Normal << 8 | Comp_SrcOver)
#define BlendComp_clip (Blend_Clip << 8 | Comp_SrcOver)

// This is added to alpha to prevent divide-by-zero
#define EPSILON 1e-15

// Apply blending and composition. Both input and output colors are
// premultiplied RGB.
vec4 mix_blend_compose(vec4 backdrop, vec4 src, uint mode) {
if ((mode & 0x7fff) == BlendComp_default) {
// Both normal+src_over blend and clip case
return backdrop * (1.0 - src.a) + src;
}
// Un-premultiply colors for blending
float inv_src_a = 1.0 / (src.a + EPSILON);
vec3 cs = src.rgb * inv_src_a;
float inv_backdrop_a = 1.0 / (backdrop.a + EPSILON);
vec3 cb = backdrop.rgb * inv_backdrop_a;
uint blend_mode = mode >> 8;
vec3 blended = mix_blend(cb, cs, blend_mode);
cs = mix(cs, blended, backdrop.a);
uint comp_mode = mode & 0xff;
if (comp_mode == Comp_SrcOver) {
vec3 co = mix(backdrop.rgb, cs, src.a);
return vec4(co, src.a + backdrop.a * (1 - src.a));
} else {
return mix_compose(cb, cs, backdrop.a, src.a, comp_mode);
}
}
2 changes: 1 addition & 1 deletion piet-gpu/shader/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ build gen/kernel4.hlsl: hlsl gen/kernel4.spv
build gen/kernel4.dxil: dxil gen/kernel4.hlsl
build gen/kernel4.msl: msl gen/kernel4.spv

build gen/kernel4_gray.spv: glsl kernel4.comp | ptcl.h setup.h mem.h
build gen/kernel4_gray.spv: glsl kernel4.comp | blend.h ptcl.h setup.h mem.h
flags = -DGRAY
build gen/kernel4_gray.hlsl: hlsl gen/kernel4_gray.spv
build gen/kernel4_gray.dxil: dxil gen/kernel4_gray.hlsl
Expand Down
2 changes: 1 addition & 1 deletion piet-gpu/shader/coarse.comp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void main() {
uint scene_offset = memory[drawmonoid_base + 2];
uint dd = drawdata_start + (scene_offset >> 2);
uint blend = scene[dd];
is_blend = (blend != BlendComp_default);
is_blend = (blend != BlendComp_clip);
}
include_tile = tile.tile.offset != 0 || (tile.backdrop == 0) == is_clip
|| is_blend;
Expand Down
Binary file modified piet-gpu/shader/gen/backdrop.dxil
Binary file not shown.
Binary file modified piet-gpu/shader/gen/backdrop_lg.dxil
Binary file not shown.
Binary file modified piet-gpu/shader/gen/bbox_clear.dxil
Binary file not shown.
Binary file modified piet-gpu/shader/gen/clip_leaf.dxil
Binary file not shown.
Binary file modified piet-gpu/shader/gen/clip_reduce.dxil
Binary file not shown.
Binary file modified piet-gpu/shader/gen/coarse.dxil
Binary file not shown.
82 changes: 41 additions & 41 deletions piet-gpu/shader/gen/coarse.hlsl

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

Loading