Blame ext/gl/gstglutils.c

Packit 971217
/* 
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gl/gstglfuncs.h>
Packit 971217
Packit 971217
#include "gstglutils.h"
Packit 971217
Packit 971217
struct _compile_shader
Packit 971217
{
Packit 971217
  GstGLShader **shader;
Packit 971217
  const gchar *vertex_src;
Packit 971217
  const gchar *fragment_src;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_compile_shader (GstGLContext * context, struct _compile_shader *data)
Packit 971217
{
Packit 971217
  GstGLShader *shader;
Packit 971217
  GstGLSLStage *vert, *frag;
Packit 971217
  GError *error = NULL;
Packit 971217
Packit 971217
  shader = gst_gl_shader_new (context);
Packit 971217
Packit 971217
  if (data->vertex_src) {
Packit 971217
    vert = gst_glsl_stage_new_with_string (context, GL_VERTEX_SHADER,
Packit 971217
        GST_GLSL_VERSION_NONE,
Packit 971217
        GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, data->vertex_src);
Packit 971217
    if (!gst_glsl_stage_compile (vert, &error)) {
Packit 971217
      GST_ERROR_OBJECT (vert, "%s", error->message);
Packit 971217
      gst_object_unref (vert);
Packit 971217
      gst_object_unref (shader);
Packit 971217
      return;
Packit 971217
    }
Packit 971217
    if (!gst_gl_shader_attach (shader, vert)) {
Packit 971217
      gst_object_unref (shader);
Packit 971217
      return;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  if (data->fragment_src) {
Packit 971217
    frag = gst_glsl_stage_new_with_string (context, GL_FRAGMENT_SHADER,
Packit 971217
        GST_GLSL_VERSION_NONE,
Packit 971217
        GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
Packit 971217
        data->fragment_src);
Packit 971217
    if (!gst_glsl_stage_compile (frag, &error)) {
Packit 971217
      GST_ERROR_OBJECT (frag, "%s", error->message);
Packit 971217
      gst_object_unref (frag);
Packit 971217
      gst_object_unref (shader);
Packit 971217
      return;
Packit 971217
    }
Packit 971217
    if (!gst_gl_shader_attach (shader, frag)) {
Packit 971217
      gst_object_unref (shader);
Packit 971217
      return;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  if (!gst_gl_shader_link (shader, &error)) {
Packit 971217
    GST_ERROR_OBJECT (shader, "%s", error->message);
Packit 971217
    g_error_free (error);
Packit 971217
    error = NULL;
Packit 971217
    gst_gl_context_clear_shader (context);
Packit 971217
    gst_object_unref (shader);
Packit 971217
    return;
Packit 971217
  }
Packit 971217
Packit 971217
  *data->shader = shader;
Packit 971217
}
Packit 971217
Packit 971217
/* Called by glfilter */
Packit 971217
gboolean
Packit 971217
gst_gl_context_gen_shader (GstGLContext * context, const gchar * vert_src,
Packit 971217
    const gchar * frag_src, GstGLShader ** shader)
Packit 971217
{
Packit 971217
  struct _compile_shader data;
Packit 971217
Packit 971217
  g_return_val_if_fail (frag_src != NULL || vert_src != NULL, FALSE);
Packit 971217
  g_return_val_if_fail (shader != NULL, FALSE);
Packit 971217
Packit 971217
  data.shader = shader;
Packit 971217
  data.vertex_src = vert_src;
Packit 971217
  data.fragment_src = frag_src;
Packit 971217
Packit 971217
  gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _compile_shader,
Packit 971217
      &data);
Packit 971217
Packit 971217
  return *shader != NULL;
Packit 971217
}
Packit 971217
Packit 971217
static const gfloat identity_matrix[] = {
Packit 971217
  1.0, 0.0, 0.0, 0.0,
Packit 971217
  0.0, 1.0, 0.0, 0.0,
Packit 971217
  0.0, 0.0, 1.0, 0.0,
Packit 971217
  0.0, 0.0, 0.0, 1.0,
Packit 971217
};
Packit 971217
Packit 971217
static const gfloat from_ndc_matrix[] = {
Packit 971217
  0.5, 0.0, 0.0, 0.0,
Packit 971217
  0.0, 0.5, 0.0, 0.0,
Packit 971217
  0.0, 0.0, 0.5, 0.0,
Packit 971217
  0.5, 0.5, 0.5, 1.0,
Packit 971217
};
Packit 971217
Packit 971217
static const gfloat to_ndc_matrix[] = {
Packit 971217
  2.0, 0.0, 0.0, 0.0,
Packit 971217
  0.0, 2.0, 0.0, 0.0,
Packit 971217
  0.0, 0.0, 2.0, 0.0,
Packit 971217
  -1.0, -1.0, -1.0, 1.0,
Packit 971217
};
Packit 971217
Packit 971217
void
Packit 971217
gst_gl_multiply_matrix4 (const gfloat * a, const gfloat * b, gfloat * result)
Packit 971217
{
Packit 971217
  int i, j, k;
Packit 971217
  gfloat tmp[16] = { 0.0f };
Packit 971217
Packit 971217
  if (!a || !b || !result)
Packit 971217
    return;
Packit 971217
Packit 971217
  for (i = 0; i < 4; i++) {     /* column */
Packit 971217
    for (j = 0; j < 4; j++) {   /* row */
Packit 971217
      for (k = 0; k < 4; k++) {
Packit 971217
        tmp[j + (i * 4)] += a[k + (i * 4)] * b[j + (k * 4)];
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  for (i = 0; i < 16; i++)
Packit 971217
    result[i] = tmp[i];
Packit 971217
}
Packit 971217
Packit 971217
void gst_gl_get_affine_transformation_meta_as_ndc_ext
Packit 971217
    (GstVideoAffineTransformationMeta * meta, gfloat * matrix)
Packit 971217
{
Packit 971217
  if (!meta) {
Packit 971217
    int i;
Packit 971217
Packit 971217
    for (i = 0; i < 16; i++) {
Packit 971217
      matrix[i] = identity_matrix[i];
Packit 971217
    }
Packit 971217
  } else {
Packit 971217
    float tmp[16];
Packit 971217
Packit 971217
    gst_gl_multiply_matrix4 (to_ndc_matrix, meta->matrix, tmp);
Packit 971217
    gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, matrix);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
void gst_gl_set_affine_transformation_meta_from_ndc_ext
Packit 971217
    (GstVideoAffineTransformationMeta * meta, const gfloat * matrix)
Packit 971217
{
Packit 971217
  float tmp[16];
Packit 971217
Packit 971217
  g_return_if_fail (meta != NULL);
Packit 971217
Packit 971217
  gst_gl_multiply_matrix4 (from_ndc_matrix, matrix, tmp);
Packit 971217
  gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, meta->matrix);
Packit 971217
}