gfx/webrender/res/clip_shared.glsl
author Jeff Muizelaar <jmuizelaar@mozilla.com>
Fri, 14 Sep 2018 13:39:33 -0400
changeset 436423 d212d02b75014a20afed1b4eb61ee80fd85a3769
parent 435668 8c80d535191663a92bd28e5759a557871bf62962
child 437560 d6bea517aec2a034b9eafc31c0934fd57fc2c3f8
permissions -rw-r--r--
Bug 1491130. Update webrender to commit da76f6aad61c1ba769566861ec41b42d511fa456

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include rect,render_task,resource_cache,snap,transform

#ifdef WR_VERTEX_SHADER

#define SEGMENT_ALL         0
#define SEGMENT_CORNER_TL   1
#define SEGMENT_CORNER_TR   2
#define SEGMENT_CORNER_BL   3
#define SEGMENT_CORNER_BR   4

in int aClipRenderTaskAddress;
in int aClipTransformId;
in int aPrimTransformId;
in int aClipSegment;
in ivec4 aClipDataResourceAddress;

struct ClipMaskInstance {
    int render_task_address;
    int clip_transform_id;
    int prim_transform_id;
    int segment;
    ivec2 clip_data_address;
    ivec2 resource_address;
};

ClipMaskInstance fetch_clip_item() {
    ClipMaskInstance cmi;

    cmi.render_task_address = aClipRenderTaskAddress;
    cmi.clip_transform_id = aClipTransformId;
    cmi.prim_transform_id = aPrimTransformId;
    cmi.segment = aClipSegment;
    cmi.clip_data_address = aClipDataResourceAddress.xy;
    cmi.resource_address = aClipDataResourceAddress.zw;

    return cmi;
}

struct ClipVertexInfo {
    vec3 local_pos;
    RectWithSize clipped_local_rect;
};

RectWithSize intersect_rect(RectWithSize a, RectWithSize b) {
    vec4 p = clamp(vec4(a.p0, a.p0 + a.size), b.p0.xyxy, b.p0.xyxy + b.size.xyxy);
    return RectWithSize(p.xy, max(vec2(0.0), p.zw - p.xy));
}

// The transformed vertex function that always covers the whole clip area,
// which is the intersection of all clip instances of a given primitive
ClipVertexInfo write_clip_tile_vertex(RectWithSize local_clip_rect,
                                      Transform prim_transform,
                                      Transform clip_transform,
                                      ClipArea area) {
    vec2 device_pos = area.screen_origin +
                      aPosition.xy * area.common_data.task_rect.size;

    // TODO(gw): We only check the clip transform matrix here. We should
    //           probably also be checking the prim_transform matrix. I
    //           have left it as is for now, since that matches the
    //           previous behavior.
    if (clip_transform.is_axis_aligned) {
        vec4 snap_positions = compute_snap_positions(
            clip_transform.m,
            local_clip_rect
        );

        vec2 snap_offsets = compute_snap_offset_impl(
            device_pos,
            clip_transform.m,
            local_clip_rect,
            RectWithSize(snap_positions.xy, snap_positions.zw - snap_positions.xy),
            snap_positions
        );

        device_pos -= snap_offsets;
    }

    vec2 world_pos = device_pos / uDevicePixelRatio;

    vec4 pos = prim_transform.m * vec4(world_pos, 0.0, 1.0);
    pos.xyz /= pos.w;

    vec4 p = get_node_pos(pos.xy, clip_transform);
    vec3 local_pos = p.xyw * pos.w;

    vec4 vertex_pos = vec4(
        area.common_data.task_rect.p0 + aPosition.xy * area.common_data.task_rect.size,
        0.0,
        1.0
    );

    gl_Position = uTransform * vertex_pos;

    init_transform_vs(vec4(local_clip_rect.p0, local_clip_rect.p0 + local_clip_rect.size));

    ClipVertexInfo vi = ClipVertexInfo(local_pos, local_clip_rect);
    return vi;
}

#endif //WR_VERTEX_SHADER