dymokomi/luce-browser-render
Source of release 0.6.1
0.6.1 / src / raster / highp.lucb
#==============================================================================================
#
# highp - the high precision (f32) raster pipeline: driver, stage table, memory stages
#
# DESCRIPTION:
# Port of tiny-skia pipeline/highp.rs, part 1, brought to Skia m144's highp stages
# (src/opts/SkRasterPipeline_opts.h) as its NEON build computes them: `mad` fuses
# (vfmaq_f32), min/max are vminq/vmaxq, stores round half to even (vcvtnq). This part
# has the Pipeline state, the stage table, `start`, and the stages that move, clamp,
# load, store, scale, lerp and dither colors. The pipeline is f32x8 (`F8`); Skia's
# NEON highp is 4 lanes wide, which computes the same per-pixel values. The blend
# stages are in highp_blend.lucb, the shader stages in highp_shaders.lucb and
# highp_image.lucb.
#
# Copyright (c) 2018 Google Inc. All rights reserved.
# Copyright (c) 2020 Yevhenii Reizner All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia, LICENSE-tiny-skia)
#
#==============================================================================================
let highp_stage_width: usize = 8
type HighpStageFn = func(HighpPipeline*) -> unit
struct HighpPipeline:
var index: usize
var functions: const HighpStageFn[]
var pixmap_src: PixmapRef
var pixmap_dst: SubPixmapMut*
var ctx: Context*
var mask_ctx: MaskCtx
var aa_mask_ctx: AAMaskCtx
var r: F8
var g: F8
var b: F8
var a: F8
var dr: F8
var dg: F8
var db: F8
var da: F8
var tail: usize
var dx: usize
var dy: usize
func highp_next_stage(p: HighpPipeline*):
let next = p.functions[p.index]
p.index += 1
next(p)
# Must be in the same order as Stage
let highp_stages: HighpStageFn[94] = [
highp_move_source_to_destination,
highp_move_destination_to_source,
highp_clamp_01,
highp_clamp_gamut,
highp_premultiply,
highp_uniform_color,
highp_seed_shader,
highp_load_dst,
highp_store,
highp_load_dst_u8,
highp_store_u8,
highp_gather,
highp_load_mask_u8,
highp_mask_u8,
highp_scale_u8,
highp_lerp_u8,
highp_scale_1_float,
highp_lerp_1_float,
highp_scale_paint_alpha,
highp_dither,
highp_destination_atop,
highp_destination_in,
highp_destination_out,
highp_destination_over,
highp_source_atop,
highp_source_in,
highp_source_out,
highp_source_over,
highp_clear,
highp_modulate,
highp_multiply,
highp_plus,
highp_screen,
highp_xor,
highp_color_burn,
highp_color_dodge,
highp_darken,
highp_difference,
highp_exclusion,
highp_hard_light,
highp_lighten,
highp_overlay,
highp_soft_light,
highp_hue,
highp_saturation,
highp_color,
highp_luminosity,
highp_transform,
highp_scale_translate_x,
highp_repeat,
highp_mirror,
highp_bilinear,
highp_bicubic,
highp_bilerp_clamp_8888,
highp_bicubic_clamp_8888,
highp_clamp_x_1,
highp_mirror_x_1,
highp_repeat_x_1,
highp_gradient,
highp_evenly_spaced_gradient,
highp_evenly_spaced_2_stop_gradient,
highp_xy_to_unit_angle,
highp_xy_to_radius,
highp_xy_to_2pt_conical_focal_on_circle,
highp_xy_to_2pt_conical_well_behaved,
highp_xy_to_2pt_conical_smaller,
highp_xy_to_2pt_conical_greater,
highp_xy_to_2pt_conical_strip,
highp_mask_2pt_conical_nan,
highp_mask_2pt_conical_degenerates,
highp_apply_vector_mask,
highp_alter_2pt_conical_compensate_focal,
highp_alter_2pt_conical_unswap,
highp_negate_x,
highp_gamma_expand_2,
highp_gamma_expand_dst_2,
highp_gamma_compress_2,
highp_gamma_expand_22,
highp_gamma_expand_dst_22,
highp_gamma_compress_22,
highp_gamma_expand_srgb,
highp_gamma_expand_dst_srgb,
highp_gamma_compress_srgb,
highp_transform_perspective,
highp_unpremul,
highp_unpremul_polar,
highp_css_lab_to_xyz,
highp_css_oklab_to_linear_srgb,
highp_css_hcl_to_lab,
highp_css_hsl_to_srgb,
highp_css_hwb_to_srgb,
highp_bilinear_mipmap,
highp_color_xform,
highp_color_filter_xform,
]
func highp_start(functions: const HighpStageFn[], functions_tail: const HighpStageFn[], rect: ScreenIntRect, aa_mask_ctx: AAMaskCtx, mask_ctx: MaskCtx, ctx: Context*, pixmap_src: PixmapRef, pixmap_dst: SubPixmapMut*):
var p = HighpPipeline(
index = 0,
functions = functions,
pixmap_src = pixmap_src,
pixmap_dst = pixmap_dst,
ctx = ctx,
mask_ctx = mask_ctx,
aa_mask_ctx = aa_mask_ctx,
tail = 0,
dx = 0,
dy = 0,
)
for y in rect.y()..<rect.bottom():
var x = (usize)rect.x()
let end = (usize)rect.right()
p.functions = functions
while x + highp_stage_width <= end:
p.index = 0
p.dx = x
p.dy = (usize)y
p.tail = highp_stage_width
highp_next_stage(&p)
x += highp_stage_width
if x != end:
p.index = 0
p.functions = functions_tail
p.dx = x
p.dy = (usize)y
p.tail = end - x
highp_next_stage(&p)
func highp_just_return(_p: HighpPipeline*):
return # Ends the loop.
# mark: Basic stages ==========================================================================
func highp_move_source_to_destination(p: HighpPipeline*):
p.dr = p.r
p.dg = p.g
p.db = p.b
p.da = p.a
highp_next_stage(p)
func highp_move_destination_to_source(p: HighpPipeline*):
p.r = p.dr
p.g = p.dg
p.b = p.db
p.a = p.da
highp_next_stage(p)
func highp_clamp_01(p: HighpPipeline*):
p.r = f8_clamp_01(p.r)
p.g = f8_clamp_01(p.g)
p.b = f8_clamp_01(p.b)
p.a = f8_clamp_01(p.a)
highp_next_stage(p)
func highp_clamp_gamut(p: HighpPipeline*):
let zero = f8_splat(0.0)
p.a = f8_vmin(f8_vmax(p.a, zero), f8_splat(1.0))
p.r = f8_vmin(f8_vmax(p.r, zero), p.a)
p.g = f8_vmin(f8_vmax(p.g, zero), p.a)
p.b = f8_vmin(f8_vmax(p.b, zero), p.a)
highp_next_stage(p)
func highp_premultiply(p: HighpPipeline*):
for h: usize in 0..<2:
p.r[h] = p.r[h] * p.a[h]
p.g[h] = p.g[h] * p.a[h]
p.b[h] = p.b[h] * p.a[h]
highp_next_stage(p)
func highp_uniform_color(p: HighpPipeline*):
let ctx = p.ctx.uniform_color
p.r = f8_splat(ctx.r)
p.g = f8_splat(ctx.g)
p.b = f8_splat(ctx.b)
p.a = f8_splat(ctx.a)
highp_next_stage(p)
func highp_seed_shader(p: HighpPipeline*):
let iota: F8 = [[0.5, 1.5, 2.5, 3.5], [4.5, 5.5, 6.5, 7.5]]
p.r = f8_add(f8_splat((f32)p.dx), iota)
p.g = f8_splat((f32)p.dy + 0.5)
p.b = f8_splat(1.0) # This is w=1 for matrix multiplies by the device coords.
p.a = f8_splat(0.0)
highp_next_stage(p)
## 8x8 ordered dithering (https://en.wikipedia.org/wiki/Ordered_dithering) with the matrix
## 1/64 x [ 0 48 12 60 ... ], scaled by the context's rate and clamped to [0, a].
func highp_dither(p: HighpPipeline*):
var d: F8 = ---
let y0 = (u32)p.dy
for i: usize in 0..<8:
let x = (u32)p.dx +% (u32)i
# We only need X and X^Y from here on, so it's easier to just think of that as "Y".
let y = y0 ^ x
# We'll mix the bottom 3 bits of each of X and Y to make 6 bits, for 2^6 == 64 ==
# 8x8 matrix values. If X=abc and Y=def, we make fcebda.
let m = (y & 1) << 5 | (x & 1) << 4 | (y & 2) << 2 | (x & 2) << 1 | (y & 4) >> 1 | (x & 4) >> 2
# Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as
# 0.5-epsilon. We want to make sure our dither is less than 0.5 in either direction to
# keep exact values like 0 and 1 unchanged after rounding.
d[i >> 2][i & 3] = math32.fma((f32)m, 2.0 / 128.0, -63.0 / 128.0)
let rate = f8_splat(p.ctx.dither_rate)
p.r = f8_fma(d, rate, p.r)
p.g = f8_fma(d, rate, p.g)
p.b = f8_fma(d, rate, p.b)
let zero = f8_splat(0.0)
p.r = f8_vmax(zero, f8_vmin(p.r, p.a))
p.g = f8_vmax(zero, f8_vmin(p.g, p.a))
p.b = f8_vmax(zero, f8_vmin(p.b, p.a))
highp_next_stage(p)
# mark: Memory ================================================================================
func highp_dst_pixels(p: HighpPipeline*) -> PremultipliedColorU8[]:
let offset = p.pixmap_dst.offset(p.dx, p.dy)
return pixels_of(p.pixmap_dst.data)[offset..]
func highp_load_dst(p: HighpPipeline*):
highp_load_8888(highp_dst_pixels(p), &p.dr, &p.dg, &p.db, &p.da)
highp_next_stage(p)
func highp_load_dst_tail(p: HighpPipeline*):
highp_load_8888_tail(p.tail, highp_dst_pixels(p), &p.dr, &p.dg, &p.db, &p.da)
highp_next_stage(p)
func highp_store(p: HighpPipeline*):
highp_store_8888(&p.r, &p.g, &p.b, &p.a, highp_dst_pixels(p))
highp_next_stage(p)
func highp_store_tail(p: HighpPipeline*):
highp_store_8888_tail(&p.r, &p.g, &p.b, &p.a, p.tail, highp_dst_pixels(p))
highp_next_stage(p)
# Currently, all mask/A8 pixmaps are handled by lowp.
func highp_load_dst_u8(_p: HighpPipeline*):
return # unreachable
func highp_load_dst_u8_tail(_p: HighpPipeline*):
return # unreachable
func highp_store_u8(_p: HighpPipeline*):
return # unreachable
func highp_store_u8_tail(_p: HighpPipeline*):
return # unreachable
func highp_load_mask_u8(_p: HighpPipeline*):
return # unreachable
# tiny-skia's clip mask stage (Skia clips with a clip blitter instead).
func highp_mask_u8(p: HighpPipeline*):
let offset = p.mask_ctx.offset(p.dx, p.dy)
var c: f32[8] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
for i in 0..<p.tail:
c[i] = (f32)p.mask_ctx.data[offset + i]
let cc = f8_div(f8_from(c), f8_splat(255.0))
var all_zero = true
for i: usize in 0..<8:
if f8_lane(cc, i) != 0.0:
all_zero = false
if all_zero:
return
p.r = f8_mul(p.r, cc)
p.g = f8_mul(p.g, cc)
p.b = f8_mul(p.b, cc)
p.a = f8_mul(p.a, cc)
highp_next_stage(p)
# mark: Coverage ==============================================================================
## The blitted mask's coverage of the stage's lanes (from_byte: zero past `tail`).
func highp_aa_coverage(p: HighpPipeline*) -> F8:
var bytes: u8[8]
p.aa_mask_ctx.load(p.dx, p.dy, p.tail, bytes)
let factor: f32 = 1.0 / 255.0
var c: F8 = ---
for i: usize in 0..<8:
c[i >> 2][i & 3] = (f32)bytes[i] * factor
return c
func highp_scale_u8(p: HighpPipeline*):
let c = highp_aa_coverage(p)
for h: usize in 0..<2:
p.r[h] = p.r[h] * c[h]
p.g[h] = p.g[h] * c[h]
p.b[h] = p.b[h] * c[h]
p.a[h] = p.a[h] * c[h]
highp_next_stage(p)
func highp_lerp_u8(p: HighpPipeline*):
let c = highp_aa_coverage(p)
for h: usize in 0..<2:
p.r[h] = highp_lerp4(p.dr[h], p.r[h], c[h])
p.g[h] = highp_lerp4(p.dg[h], p.g[h], c[h])
p.b[h] = highp_lerp4(p.db[h], p.b[h], c[h])
p.a[h] = highp_lerp4(p.da[h], p.a[h], c[h])
highp_next_stage(p)
func highp_scale_by(p: HighpPipeline*, f: f32):
let c = f32[4](f)
for h: usize in 0..<2:
p.r[h] = p.r[h] * c
p.g[h] = p.g[h] * c
p.b[h] = p.b[h] * c
p.a[h] = p.a[h] * c
highp_next_stage(p)
func highp_scale_1_float(p: HighpPipeline*):
highp_scale_by(p, p.ctx.current_coverage)
func highp_scale_paint_alpha(p: HighpPipeline*):
highp_scale_by(p, p.ctx.paint_alpha)
func highp_lerp_1_float(p: HighpPipeline*):
let c = f32[4](p.ctx.current_coverage)
for h: usize in 0..<2:
p.r[h] = highp_lerp4(p.dr[h], p.r[h], c)
p.g[h] = highp_lerp4(p.dg[h], p.g[h], c)
p.b[h] = highp_lerp4(p.db[h], p.b[h], c)
p.a[h] = highp_lerp4(p.da[h], p.a[h], c)
highp_next_stage(p)
# mark: Load and store ========================================================================
## from_8888: each byte times 1/255.
func highp_load_8888(data: const PremultipliedColorU8[], r: F8*, g: F8*, b: F8*, a: F8*):
let factor: f32 = 1.0 / 255.0
if data.length < 8:
trap("index out of bounds")
let src = (const u8*)data.data
for h: usize in 0..<2:
var rr: F4 = ---
var gg: F4 = ---
var bb: F4 = ---
var aa: F4 = ---
for l: usize in 0..<4:
let q = (h * 4 + l) * 4
rr[l] = (f32)src[q] * factor
gg[l] = (f32)src[q + 1] * factor
bb[l] = (f32)src[q + 2] * factor
aa[l] = (f32)src[q + 3] * factor
(*r)[h] = rr
(*g)[h] = gg
(*b)[h] = bb
(*a)[h] = aa
func highp_load_8888_tail(tail: usize, data: const PremultipliedColorU8[], r: F8*, g: F8*, b: F8*, a: F8*):
# Fill a dummy array with `tail` values. `tail` is always in a 1..STAGE_WIDTH-1 range.
var tmp: PremultipliedColorU8[8]
for i in 0..<tail:
tmp[i] = data[i]
highp_load_8888(tmp, r, g, b, a)
func highp_store_8888(r: const F8*, g: const F8*, b: const F8*, a: const F8*, data: PremultipliedColorU8[]):
if data.length < 8:
trap("index out of bounds")
let dst = (u8*)data.data
for h: usize in 0..<2:
let rr = highp_unnorm4((*r)[h])
let gg = highp_unnorm4((*g)[h])
let bb = highp_unnorm4((*b)[h])
let aa = highp_unnorm4((*a)[h])
for l: usize in 0..<4:
let q = (h * 4 + l) * 4
dst[q] = (u8)rr[l]
dst[q + 1] = (u8)gg[l]
dst[q + 2] = (u8)bb[l]
dst[q + 3] = (u8)aa[l]
func highp_store_8888_tail(r: const F8*, g: const F8*, b: const F8*, a: const F8*, tail: usize, data: PremultipliedColorU8[]):
for i in 0..<tail:
let h = i >> 2
let l = i & 3
let rr = highp_unnorm4((*r)[h])
let gg = highp_unnorm4((*g)[h])
let bb = highp_unnorm4((*b)[h])
let aa = highp_unnorm4((*a)[h])
data[i] = PremultipliedColorU8(rgba = [(u8)rr[l], (u8)gg[l], (u8)bb[l], (u8)aa[l]])
## to_unorm(v, 255): round(min(max(0, v * 255), 255)), half to even, NaN to zero.
func highp_unnorm4(v: F4) -> U4:
let c = f4_vmin(f4_vmax(f32[4](0.0), v * f32[4](255.0)), f32[4](255.0))
return [f32_round_nonneg(c[0]), f32_round_nonneg(c[1]), f32_round_nonneg(c[2]), f32_round_nonneg(c[3])]
# mark: Helpers ===============================================================================
func highp_inv(v: F8) -> F8:
return f8_sub(f8_splat(1.0), v)
func highp_two(v: F8) -> F8:
return f8_add(v, v)
## highp's mad: f * m + a, fused (vfmaq_f32).
func highp_mad(f: F8, m: F8, a: F8) -> F8:
return f8_fma(f, m, a)
## highp's lerp: mad(to - from, t, from).
func highp_lerp4(from_: F4, to: F4, t: F4) -> F4:
return f4_fma(to - from_, t, from_)