/* This file is auto-generated; changes will not be preserved */

#include "config.h"

#include "gskgpuborderopprivate.h"

#include "gskgpurenderpassprivate.h"
#include "gskgpushaderopprivate.h"
#include "gskrectprivate.h"
#include <graphene.h>


typedef struct _GskGpuBorderInstance GskGpuBorderInstance;

struct _GskGpuBorderInstance {
  float outline[12];
  float top_border_color[4];
  float right_border_color[4];
  float bottom_border_color[4];
  float left_border_color[4];
  float border_widths[4];
  float inside_offset[4];
};

static void
gsk_gpu_border_op_print_instance (GskGpuShaderOp *shader,
                                  gpointer        instance_,
                                  GString        *string)
{

  /* FIXME: Implement */
}

static inline void
gsk_gpu_border_setup_vao (gsize offset)
{
  glEnableVertexAttribArray (0);
  glVertexAttribDivisor (0, 1);
  glVertexAttribPointer (0,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 0));
  glEnableVertexAttribArray (1);
  glVertexAttribDivisor (1, 1);
  glVertexAttribPointer (1,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 16));
  glEnableVertexAttribArray (2);
  glVertexAttribDivisor (2, 1);
  glVertexAttribPointer (2,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 32));
  glEnableVertexAttribArray (3);
  glVertexAttribDivisor (3, 1);
  glVertexAttribPointer (3,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 48));
  glEnableVertexAttribArray (4);
  glVertexAttribDivisor (4, 1);
  glVertexAttribPointer (4,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 64));
  glEnableVertexAttribArray (5);
  glVertexAttribDivisor (5, 1);
  glVertexAttribPointer (5,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 80));
  glEnableVertexAttribArray (6);
  glVertexAttribDivisor (6, 1);
  glVertexAttribPointer (6,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 96));
  glEnableVertexAttribArray (7);
  glVertexAttribDivisor (7, 1);
  glVertexAttribPointer (7,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 112));
  glEnableVertexAttribArray (8);
  glVertexAttribDivisor (8, 1);
  glVertexAttribPointer (8,
                         2,
                         GL_FLOAT,
                         GL_FALSE,
                         144,
                         GSIZE_TO_POINTER (offset + 128));
}

static inline void
gsk_gpu_border_setup_attrib_locations (GLuint program)
{
  glBindAttribLocation (program, 0, "in_outline");
  glBindAttribLocation (program, 3, "in_top_border_color");
  glBindAttribLocation (program, 4, "in_right_border_color");
  glBindAttribLocation (program, 5, "in_bottom_border_color");
  glBindAttribLocation (program, 6, "in_left_border_color");
  glBindAttribLocation (program, 7, "in_border_widths");
  glBindAttribLocation (program, 8, "in_inside_offset");
}

#ifdef GDK_RENDERING_VULKAN

static const VkPipelineVertexInputStateCreateInfo gsk_gpu_border_info = {
  .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  .vertexBindingDescriptionCount = 1,
  .pVertexBindingDescriptions = (VkVertexInputBindingDescription[1]) {
      {
        .binding = 0,
        .stride = 144,
        .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
      }
  },
  .vertexAttributeDescriptionCount = 9,
  .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[9]) {
      {
        .location = 0,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 0,
      },
      {
        .location = 1,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 16,
      },
      {
        .location = 2,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 32,
      },
      {
        .location = 3,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 48,
      },
      {
        .location = 4,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 64,
      },
      {
        .location = 5,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 80,
      },
      {
        .location = 6,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 96,
      },
      {
        .location = 7,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 112,
      },
      {
        .location = 8,
        .binding = 0,
        .format = VK_FORMAT_R32G32_SFLOAT,
        .offset = 128,
      },
  },
};

#endif /* GDK_RENDERING_VULKAN */

static const GskGpuShaderOpClass GSK_GPU_BORDER_OP_CLASS = {
  {
    GSK_GPU_OP_SIZE (GskGpuShaderOp),
    GSK_GPU_STAGE_SHADER,
    gsk_gpu_shader_op_finish,
    gsk_gpu_shader_op_print,
#ifdef GDK_RENDERING_VULKAN
    gsk_gpu_shader_op_vk_command,
#endif
    gsk_gpu_shader_op_gl_command
  },
  "gskgpuborder",
  0,
  48,
  sizeof (GskGpuBorderInstance),
#ifdef GDK_RENDERING_VULKAN
  &gsk_gpu_border_info,
#endif
  gsk_gpu_border_op_print_instance,
  gsk_gpu_border_setup_attrib_locations,
  gsk_gpu_border_setup_vao
};

void
gsk_gpu_border_op (GskGpuRenderPass      *pass,
                   GdkColorState         *ccs,
                   GdkColorState         *acs,
                   const graphene_rect_t *bounds,
                   const GskRoundedRect  *outline,
                   const GdkColor        *top_border_color,
                   const GdkColor        *right_border_color,
                   const GdkColor        *bottom_border_color,
                   const GdkColor        *left_border_color,
                   const graphene_vec4_t *border_widths,
                   const graphene_size_t *inside_offset)
{
  GskGpuBorderInstance *instance;

  gsk_gpu_render_pass_prepare_shader (pass);

  gsk_gpu_shader_op_alloc (pass->frame,
                           &GSK_GPU_BORDER_OP_CLASS,
                           ccs ? gsk_gpu_color_states_create (ccs, TRUE, acs, FALSE)
                               : gsk_gpu_color_states_create_equal (TRUE, FALSE),
                           0,
                           gsk_gpu_clip_get_shader_clip (&pass->clip, &pass->offset, bounds),
                           pass->clip_mask,
                           NULL,
                           NULL,
                           &instance);
  gsk_rounded_rect_to_float (outline, &pass->offset, instance->outline);
  gdk_color_to_float (top_border_color, acs, instance->top_border_color);
  instance->top_border_color[3] *= pass->opacity;
  gdk_color_to_float (right_border_color, acs, instance->right_border_color);
  instance->right_border_color[3] *= pass->opacity;
  gdk_color_to_float (bottom_border_color, acs, instance->bottom_border_color);
  instance->bottom_border_color[3] *= pass->opacity;
  gdk_color_to_float (left_border_color, acs, instance->left_border_color);
  instance->left_border_color[3] *= pass->opacity;
  graphene_vec4_to_float (border_widths, instance->border_widths);
  instance->inside_offset[0] = inside_offset->width;
  instance->inside_offset[0 + 1] = inside_offset->height;
}

