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

#include "config.h"

#include "gskgpudisplacementopprivate.h"

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


typedef struct _GskGpuDisplacementInstance GskGpuDisplacementInstance;

struct _GskGpuDisplacementInstance {
  float rect[4];
  float displacement_rect[4];
  float child_rect[4];
  guint32 x_channel_y_channel[4];
  float displace_max_displace_scale[4];
  float displace_offset_opacity[4];
};

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

  /* FIXME: Implement */
}

static inline void
gsk_gpu_displacement_setup_vao (gsize offset)
{
  glEnableVertexAttribArray (0);
  glVertexAttribDivisor (0, 1);
  glVertexAttribPointer (0,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         96,
                         GSIZE_TO_POINTER (offset + 0));
  glEnableVertexAttribArray (1);
  glVertexAttribDivisor (1, 1);
  glVertexAttribPointer (1,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         96,
                         GSIZE_TO_POINTER (offset + 16));
  glEnableVertexAttribArray (2);
  glVertexAttribDivisor (2, 1);
  glVertexAttribPointer (2,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         96,
                         GSIZE_TO_POINTER (offset + 32));
  glEnableVertexAttribArray (3);
  glVertexAttribDivisor (3, 1);
  glVertexAttribIPointer (3,
                          2,
                          GL_UNSIGNED_INT,
                          96,
                          GSIZE_TO_POINTER (offset + 48));
  glEnableVertexAttribArray (4);
  glVertexAttribDivisor (4, 1);
  glVertexAttribPointer (4,
                         4,
                         GL_FLOAT,
                         GL_FALSE,
                         96,
                         GSIZE_TO_POINTER (offset + 64));
  glEnableVertexAttribArray (5);
  glVertexAttribDivisor (5, 1);
  glVertexAttribPointer (5,
                         3,
                         GL_FLOAT,
                         GL_FALSE,
                         96,
                         GSIZE_TO_POINTER (offset + 80));
}

static inline void
gsk_gpu_displacement_setup_attrib_locations (GLuint program)
{
  glBindAttribLocation (program, 0, "in_rect");
  glBindAttribLocation (program, 1, "in_displacement_rect");
  glBindAttribLocation (program, 2, "in_child_rect");
  glBindAttribLocation (program, 3, "in_x_channel_y_channel");
  glBindAttribLocation (program, 4, "in_displace_max_displace_scale");
  glBindAttribLocation (program, 5, "in_displace_offset_opacity");
}

#ifdef GDK_RENDERING_VULKAN

static const VkPipelineVertexInputStateCreateInfo gsk_gpu_displacement_info = {
  .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  .vertexBindingDescriptionCount = 1,
  .pVertexBindingDescriptions = (VkVertexInputBindingDescription[1]) {
      {
        .binding = 0,
        .stride = 96,
        .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
      }
  },
  .vertexAttributeDescriptionCount = 6,
  .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[6]) {
      {
        .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_R32G32_UINT,
        .offset = 48,
      },
      {
        .location = 4,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 64,
      },
      {
        .location = 5,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32_SFLOAT,
        .offset = 80,
      },
  },
};

#endif /* GDK_RENDERING_VULKAN */

static const GskGpuShaderOpClass GSK_GPU_DISPLACEMENT_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
  },
  "gskgpudisplacement",
  2,
  6,
  sizeof (GskGpuDisplacementInstance),
#ifdef GDK_RENDERING_VULKAN
  &gsk_gpu_displacement_info,
#endif
  gsk_gpu_displacement_op_print_instance,
  gsk_gpu_displacement_setup_attrib_locations,
  gsk_gpu_displacement_setup_vao
};

void
gsk_gpu_displacement_op (GskGpuFrame            *frame,
                         GskGpuShaderClip        clip,
                         GdkColorState          *ccs,
                         float                   opacity,
                         const graphene_point_t *offset,
                         GskGpuImage            *image1,
                         GskGpuSampler           sampler1,
                         GskGpuImage            *image2,
                         GskGpuSampler           sampler2,
                         const graphene_rect_t  *rect,
                         const graphene_rect_t  *displacement_rect,
                         const graphene_rect_t  *child_rect,
                         guint32                 x_channel,
                         guint32                 y_channel,
                         const graphene_size_t  *displace_max,
                         const graphene_size_t  *displace_scale,
                         const graphene_size_t  *displace_offset)
{
  GskGpuDisplacementInstance *instance;

  gsk_gpu_shader_op_alloc (frame,
                           &GSK_GPU_DISPLACEMENT_OP_CLASS,
                           ccs ? gsk_gpu_color_states_create (ccs, TRUE, ccs, TRUE)
                               : gsk_gpu_color_states_create_equal (TRUE, TRUE),
                           0,
                           clip,
                           (GskGpuImage *[2]) { image1, image2 },
                           (GskGpuSampler[2]) { sampler1, sampler2 },
                           &instance);
  gsk_gpu_rect_to_float (rect, offset, instance->rect);
  gsk_gpu_rect_to_float (displacement_rect, offset, instance->displacement_rect);
  gsk_gpu_rect_to_float (child_rect, offset, instance->child_rect);
  instance->x_channel_y_channel[0] = x_channel;
  instance->x_channel_y_channel[1] = y_channel;
  instance->displace_max_displace_scale[0] = displace_max->width;
  instance->displace_max_displace_scale[0 + 1] = displace_max->height;
  instance->displace_max_displace_scale[2] = displace_scale->width;
  instance->displace_max_displace_scale[2 + 1] = displace_scale->height;
  instance->displace_offset_opacity[0] = displace_offset->width;
  instance->displace_offset_opacity[0 + 1] = displace_offset->height;
  instance->displace_offset_opacity[2] = opacity;
}

