// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <impeller/texture.glsl>
#include <impeller/types.glsl>

// Unused, see See PointFieldGeometry::GetPositionBuffer
layout(local_size_x = 16) in;

layout(std430) readonly buffer PointData {
  // Size of this input data is frame_info.count;
  vec2 points[];
}
point_data;

layout(std430) writeonly buffer GeometryData {
  // Size of this output data is frame_info.count * points_per_circle;
  vec2 geometry[];
}
geometry_data;

uniform FrameInfo {
  uint count;
  float16_t radius;
  float16_t radian_start;
  float16_t radian_step;
  uint points_per_circle;
  int divisions_per_circle;
}
frame_info;

void main() {
  uint ident = gl_GlobalInvocationID.x;
  if (ident >= frame_info.count) {
    return;
  }

  vec2 center = point_data.points[ident];
  uint bufer_offset = ident * frame_info.points_per_circle;

  float16_t elapsed_angle = frame_info.radian_start;

  vec2 origin =
      center + vec2(cos(elapsed_angle), sin(elapsed_angle)) * frame_info.radius;
  geometry_data.geometry[bufer_offset++] = origin;

  elapsed_angle += frame_info.radian_step;
  vec2 pt1 =
      center + vec2(cos(elapsed_angle), sin(elapsed_angle)) * frame_info.radius;
  geometry_data.geometry[bufer_offset++] = pt1;

  elapsed_angle += frame_info.radian_step;
  vec2 pt2 =
      center + vec2(cos(elapsed_angle), sin(elapsed_angle)) * frame_info.radius;
  geometry_data.geometry[bufer_offset++] = pt2;

  for (int i = 0; i < frame_info.divisions_per_circle - 3; i++) {
    geometry_data.geometry[bufer_offset++] = origin;
    geometry_data.geometry[bufer_offset++] = pt2;

    elapsed_angle += frame_info.radian_step;
    pt2 = center +
          vec2(cos(elapsed_angle), sin(elapsed_angle)) * frame_info.radius;
    geometry_data.geometry[bufer_offset++] = pt2;
  }
}
