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

#include "config.h"

#include "gskgpucolorizeopprivate.h"

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


typedef struct _GskGpuColorizeInstance GskGpuColorizeInstance;

struct _GskGpuColorizeInstance {
  float rect[4];
  float tex_rect[4];
  float color[4];
};

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

  /* FIXME: Implement */
}

static inline void
gsk_gpu_colorize_setup_vao (gsize offset)
{
  glEnableVertexAttribArray (0);
  glVertexAttribDivisor (0, 1);
  glVertexAttribPointer (0,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         48,
                         GSIZE_TO_POINTER (offset + 0));
  glEnableVertexAttribArray (1);
  glVertexAttribDivisor (1, 1);
  glVertexAttribPointer (1,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         48,
                         GSIZE_TO_POINTER (offset + 16));
  glEnableVertexAttribArray (2);
  glVertexAttribDivisor (2, 1);
  glVertexAttribPointer (2,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         48,
                         GSIZE_TO_POINTER (offset + 32));
}

static inline void
gsk_gpu_colorize_setup_attrib_locations (GLuint program)
{
  glBindAttribLocation (program, 0, "in_rect");
  glBindAttribLocation (program, 1, "in_tex_rect");
  glBindAttribLocation (program, 2, "in_color");
}

#ifdef GDK_RENDERING_VULKAN

static const VkPipelineVertexInputStateCreateInfo gsk_gpu_colorize_info = {
  .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  .vertexBindingDescriptionCount = 1,
  .pVertexBindingDescriptions = (VkVertexInputBindingDescription[1]) {
      {
        .binding = 0,
        .stride = 48,
        .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
      }
  },
  .vertexAttributeDescriptionCount = 3,
  .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[3]) {
      {
        .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,
      },
  },
};

#endif /* GDK_RENDERING_VULKAN */

static const GskGpuShaderOpClass GSK_GPU_COLORIZE_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
  },
  "gskgpucolorize",
  1,
  6,
  sizeof (GskGpuColorizeInstance),
#ifdef GDK_RENDERING_VULKAN
  &gsk_gpu_colorize_info,
#endif
  gsk_gpu_colorize_op_print_instance,
  gsk_gpu_colorize_setup_attrib_locations,
  gsk_gpu_colorize_setup_vao
};

void
gsk_gpu_colorize_op (GskGpuFrame            *frame,
                     GskGpuShaderClip        clip,
                     GdkColorState          *ccs,
                     GdkColorState          *acs,
                     float                   opacity,
                     const graphene_point_t *offset,
                     GskGpuImage            *image1,
                     GskGpuSampler           sampler1,
                     const graphene_rect_t  *rect,
                     const graphene_rect_t  *tex_rect,
                     const GdkColor         *color)
{
  GskGpuColorizeInstance *instance;

  gsk_gpu_shader_op_alloc (frame,
                           &GSK_GPU_COLORIZE_OP_CLASS,
                           ccs ? gsk_gpu_color_states_create (ccs, TRUE, acs, FALSE)
                               : gsk_gpu_color_states_create_equal (TRUE, FALSE),
                           0,
                           clip,
                           (GskGpuImage *[1]) { image1 },
                           (GskGpuSampler[1]) { sampler1 },
                           &instance);
  gsk_gpu_rect_to_float (rect, offset, instance->rect);
  gsk_gpu_rect_to_float (tex_rect, offset, instance->tex_rect);
  gdk_color_to_float (color, acs, instance->color);  instance->color[3] *= opacity;
}

