     vec2 clamped_local_pos = clamp_rect(clamp_rect(local_pos, local_clip_rect),
                                         layer.local_clip_rect);
 
-    // Adjust the snap rectangle.
-    RectWithSize clamped_snap_rect = intersect_rect(intersect_rect(snap_rect, local_clip_rect),
-                                                    layer.local_clip_rect);
-    // Transform the snap corners to the world space.
-    vec4 world_snap_p0 = layer.transform * vec4(clamped_snap_rect.p0, 0.0, 1.0);
-    vec4 world_snap_p1 = layer.transform * vec4(clamped_snap_rect.p0 + clamped_snap_rect.size, 0.0, 1.0);
-    // Snap bounds in world coordinates, adjusted for pixel ratio. XY = top left, ZW = bottom right
-    vec4 world_snap = uDevicePixelRatio * vec4(world_snap_p0.xy, world_snap_p1.xy) /
-                                          vec4(world_snap_p0.ww, world_snap_p1.ww);
-    /// World offsets applied to the corners of the snap rectangle.
-    vec4 snap_offsets = floor(world_snap + 0.5) - world_snap;
-
-    /// Compute the position of this vertex inside the snap rectangle.
-    vec2 normalized_snap_pos = (clamped_local_pos - clamped_snap_rect.p0) / clamped_snap_rect.size;
-    /// Compute the actual world offset for this vertex needed to make it snap.
-    vec2 snap_offset = mix(snap_offsets.xy, snap_offsets.zw, normalized_snap_pos);
+    /// Compute the snapping offset.
+    vec2 snap_offset = compute_snap_offset(clamped_local_pos, local_clip_rect, layer, snap_rect);
 
     // Transform the current vertex to the world cpace.
     vec4 world_pos = layer.transform * vec4(clamped_local_pos, 0.0, 1.0);

    return untransform(pos, n, a, layer.inv_transform);
}

struct VertexInfo {
    vec2 local_pos;
    vec2 screen_pos;

                                      adjusted_next_p0,
                                      adjusted_next_p1);

    vec4 layer_pos = get_layer_pos(device_pos / uDevicePixelRatio, layer);

    /// Compute the snapping offset.
    vec2 snap_offset = compute_snap_offset(layer_pos.xy / layer_pos.w,
                                           local_clip_rect, layer, snap_rect);

    // Apply offsets for the render task to get correct screen location.
    vec2 final_pos = device_pos + snap_offset -
                     task.screen_space_origin +
                     task.render_target_origin;

    gl_Position = uTransform * vec4(final_pos, z, 1.0);

    vLocalBounds = vec4(local_rect.p0, local_rect.p1);

    return TransformVertexInfo(layer_pos.xyw, device_pos);
}
