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

#include "config.h"

#include "gskgputextureopprivate.h"

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


typedef struct _GskGpuTextureInstance GskGpuTextureInstance;

struct _GskGpuTextureInstance {
  float bounds[4];
  float tex_rect[4];
};

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

GskGpuTextureInstance *instance = instance_;

  gsk_gpu_print_rect (string, instance->bounds);

  /* FIXME: Implement */
}

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

static inline void
gsk_gpu_texture_setup_attrib_locations (GLuint program)
{
  glBindAttribLocation (program, 0, "in_bounds");
  glBindAttribLocation (program, 1, "in_tex_rect");
}

#ifdef GDK_RENDERING_VULKAN

static const VkPipelineVertexInputStateCreateInfo gsk_gpu_texture_info = {
  .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  .vertexBindingDescriptionCount = 1,
  .pVertexBindingDescriptions = (VkVertexInputBindingDescription[1]) {
      {
        .binding = 0,
        .stride = 32,
        .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
      }
  },
  .vertexAttributeDescriptionCount = 2,
  .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[2]) {
      {
        .location = 0,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 0,
      },
      {
        .location = 1,
        .binding = 0,
        .format = VK_FORMAT_R32G32B32A32_SFLOAT,
        .offset = 16,
      },
  },
};

#endif /* GDK_RENDERING_VULKAN */

static const GskGpuShaderOpClass GSK_GPU_TEXTURE_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
  },
  "gskgputexture",
  1,
  6,
  sizeof (GskGpuTextureInstance),
#ifdef GDK_RENDERING_VULKAN
  &gsk_gpu_texture_info,
#endif
  gsk_gpu_texture_op_print_instance,
  gsk_gpu_texture_setup_attrib_locations,
  gsk_gpu_texture_setup_vao
};

void
gsk_gpu_texture_op (GskGpuRenderPass      *pass,
                    GdkColorState         *ccs,
                    const graphene_rect_t *bounds,
                    GskGpuImage           *image1,
                    GskGpuSampler          sampler1,
                    const graphene_rect_t *tex_rect)
{
  GskGpuTextureInstance *instance;

  gsk_gpu_render_pass_prepare_shader (pass);

  gsk_gpu_shader_op_alloc (pass->frame,
                           &GSK_GPU_TEXTURE_OP_CLASS,
                           ccs ? gsk_gpu_color_states_create (ccs, TRUE, ccs, TRUE)
                               : gsk_gpu_color_states_create_equal (TRUE, TRUE),
                           0,
                           gsk_gpu_clip_get_shader_clip (&pass->clip, &pass->offset, bounds),
                           pass->clip_mask,
                           (GskGpuImage *[1]) { image1 },
                           (GskGpuSampler[1]) { sampler1 },
                           &instance);
  gsk_gpu_rect_to_float (bounds, &pass->offset, instance->bounds);
  gsk_gpu_rect_to_float (tex_rect, &pass->offset, instance->tex_rect);
}

