// =================================================================
// 阶段 5:Layer 合成 blit shader(Transform / Opacity / ClipRRect)
// =================================================================
//
// 把 layer GPU texture 画到 surface 的 **特定 logical 矩形**,可选应用 effect:
// - **Transform**: 2D 仿射(平移+旋转+缩放),由 CPU 端折叠成 affine 矩阵
// - **Opacity**: alpha 乘(嵌套层已由 CPU 乘好)
// - **ClipRRect**: 最多两层圆角矩形裁剪(surface 逻辑坐标,嵌套层继承)
//
// 嵌套 layer:父层的 transform/opacity/clip 由 CPU 端递归合成进子层的
// affine / opacity / clip_rect,因此本 shader 只需处理"最终参数"。
struct BlitUniform {
offset: vec2<f32>,
size: vec2<f32>,
screen: vec2<f32>,
effect_flags: u32,
opacity: f32,
rotation: f32,
scale_x: f32,
translate_x: f32,
translate_y: f32,
clip_radius: f32,
scale_y: f32,
quad_offset: vec2<f32>,
quad_size: vec2<f32>,
phys_screen: vec2<f32>,
// 局部坐标 -> surface 逻辑坐标(CPU 端已合成所有祖先效果)
affine: vec4<f32>, // a, b, c, d
affine_translate: vec2<f32>,
_affine_pad: vec2<f32>,
clip_rect0: vec4<f32>, // x, y, w, h
clip_rect1: vec4<f32>,
clip_params: vec4<f32>, // radius0, radius1, count, unused
};
@group(0) @binding(0) var<uniform> blit: BlitUniform;
@group(0) @binding(1) var layer_tex: texture_2d<f32>;
@group(0) @binding(2) var layer_sampler: sampler;
const HAS_OPACITY: u32 = 1u << 1u;
const HAS_CLIP: u32 = 1u << 2u;
struct VsOut {
@builtin(position) clip_pos: vec4<f32>,
@location(0) uv: vec2<f32>,
};
@vertex
fn vs_main(
@builtin(vertex_index) vid: u32,
) -> VsOut {
let positions = array<vec2<f32>, 6>(
vec2<f32>(0.0, 0.0),
vec2<f32>(1.0, 0.0),
vec2<f32>(0.0, 1.0),
vec2<f32>(0.0, 1.0),
vec2<f32>(1.0, 0.0),
vec2<f32>(1.0, 1.0),
);
let p = positions[vid];
let local = p * blit.size;
let world = vec2<f32>(
blit.affine.x * local.x + blit.affine.y * local.y + blit.affine_translate.x,
blit.affine.z * local.x + blit.affine.w * local.y + blit.affine_translate.y,
);
let ndc = (world / blit.screen) * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
var out: VsOut;
out.clip_pos = vec4<f32>(ndc, 0.0, 1.0);
out.uv = p;
return out;
}
/// 圆角矩形 alpha mask(surface 逻辑坐标)
fn rrect_alpha(world: vec2<f32>, rect: vec4<f32>, radius: f32) -> f32 {
let size = rect.zw;
if size.x <= 0.0 || size.y <= 0.0 {
return 0.0;
}
let p = world - rect.xy;
let r = min(radius, min(size.x, size.y) * 0.5);
if r <= 0.0 {
if p.x >= 0.0 && p.y >= 0.0 && p.x <= size.x && p.y <= size.y {
return 1.0;
}
return 0.0;
}
// 中央条带快路径也必须先确认在矩形范围内 —— 否则子 layer 超出父层
// 上下/左右边界时会被误判为 1.0,造成四边漏裁。
let inside = p.x >= 0.0 && p.y >= 0.0 && p.x <= size.x && p.y <= size.y;
if !inside {
return 0.0;
}
if p.x >= r && p.x <= size.x - r { return 1.0; }
if p.y >= r && p.y <= size.y - r { return 1.0; }
var c: vec2<f32>;
if p.x < r && p.y < r { c = vec2<f32>(r, r); }
else if p.x > size.x - r && p.y < r { c = vec2<f32>(size.x - r, r); }
else if p.x < r && p.y > size.y - r { c = vec2<f32>(r, size.y - r); }
else { c = vec2<f32>(size.x - r, size.y - r); }
let d = distance(p, c);
return clamp(r - d + 0.5, 0.0, 1.0);
}
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
// fragment 的 @builtin(position) 是物理像素,换算回 surface 逻辑坐标
let world = in.clip_pos.xy / blit.phys_screen * blit.screen;
// 逆仿射:surface 逻辑 -> layer 局部
let a = blit.affine.x;
let b = blit.affine.y;
let c = blit.affine.z;
let d = blit.affine.w;
let det = a * d - b * c;
let rel = world - blit.affine_translate;
var uv = vec2<f32>(0.0);
var extra_alpha = 1.0;
if abs(det) > 1e-8 {
let local = vec2<f32>(
(d * rel.x - b * rel.y) / det,
(-c * rel.x + a * rel.y) / det,
);
if local.x < 0.0 || local.y < 0.0 || local.x > blit.size.x || local.y > blit.size.y {
extra_alpha = 0.0;
}
uv = clamp(local / max(blit.size, vec2<f32>(0.0001)), vec2<f32>(0.0), vec2<f32>(1.0));
} else {
extra_alpha = 0.0;
}
var color = textureSample(layer_tex, layer_sampler, uv);
color.a *= extra_alpha;
if (blit.effect_flags & HAS_OPACITY) != 0u {
color.a *= blit.opacity;
}
if (blit.effect_flags & HAS_CLIP) != 0u {
let count = i32(blit.clip_params.z);
if count > 0 {
color.a *= rrect_alpha(world, blit.clip_rect0, blit.clip_params.x);
}
if count > 1 {
color.a *= rrect_alpha(world, blit.clip_rect1, blit.clip_params.y);
}
}
return color;
}