#version 330 core
// Magnifier decorative cover image (Style1/Style2 presets; docs-vn/rd/
// windows/plan/2026-08-12-magnifier.md §2.3). Draws
// MagnifierModel::coverSvgContent (rasterised by
// GpuRasterPrimitive::resolveMagnifierCoverTexture) centred over the
// lens's cutout hole -- a static decoration, unaffected by zoomScale/
// borderWidth, drawn as its own pass on top of everything else
// (magnifier.fs's core pass + any border pass).
//
// The source texture is straight (non-premultiplied) alpha -- it comes
// from a plain QPainter::render() onto a QImage, same as every other
// SVG rasterize in this codebase (loadShapeTextureFromSvgContent,
// mosaic's own maskTex) -- so this shader premultiplies at output,
// unlike drawRgbaPremulFromTex's shader which assumes an
// ALREADY-premultiplied source and would double-darken semi-transparent
// cover edges if reused here directly.
//
// Paired vertex shader: transition_dissolve.vs (plain passthrough).

out vec4 FragColor;
in  vec2 TexCoord;   // [0,1] over the dst-rect-sized viewport

uniform sampler2D u_coverTex;
uniform float u_opacity;

// Rotation support -- the cover is fixed relative to the LENS's own
// local frame (it's authored in the same 1000x1000 canvas as the mask),
// so it must rotate WITH the lens when the layer rotates, same as the
// core pass (magnifier.fs) and the sdf border pass
// (magnifier_sdf_border.fs). Same applyOrientationUniforms CPU-side
// helper and applyOrient math as those two; see magnifier.fs's own
// comment for the full rationale. Defaults are no-op.
uniform int   u_flipX;
uniform int   u_flipY;
uniform float u_rotRad;
uniform vec2  u_dstSize;
uniform vec2  u_unrotSize;
uniform vec2  u_rotPivot01;
uniform vec2  u_anchorPoint01;

vec2 applyOrient(vec2 uv) {
    if (u_rotRad != 0.0) {
        if (u_unrotSize.x > 0.0 && u_unrotSize.y > 0.0) {
            vec2 dPx = (uv - u_rotPivot01) * u_dstSize;
            float s = sin(-u_rotRad);
            float co = cos(-u_rotRad);
            vec2 sPx = vec2(co * dPx.x - s * dPx.y, s * dPx.x + co * dPx.y);
            uv = sPx / u_unrotSize + u_anchorPoint01;
        } else {
            vec2 c = uv - vec2(0.5);
            float s = sin(u_rotRad);
            float co = cos(u_rotRad);
            uv = vec2(co * c.x - s * c.y, s * c.x + co * c.y) + vec2(0.5);
        }
    }
    if (u_flipX != 0) uv.x = 1.0 - uv.x;
    if (u_flipY != 0) uv.y = 1.0 - uv.y;
    return uv;
}

void main() {
    vec2 lensUv = applyOrient(TexCoord);
    vec4 straight = texture(u_coverTex, lensUv);
    float alpha = straight.a * u_opacity;
    FragColor = vec4(straight.rgb * alpha, alpha);
}
