gl: add convenient functions to setup default vertex and fragment shaders

Most of our 2D filters use the same simple vertex shader.
Also define the default fragment shader as the identity.

Avoid duplicating the same vertex and fragment shader text.
This commit is contained in:
Julien Isorce 2014-04-30 15:20:23 +01:00
parent fc38c22f1b
commit 64e5b5a203
2 changed files with 97 additions and 0 deletions

View file

@ -1,6 +1,7 @@
/*
* GStreamer
* Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
* Copyright (C) 2014 Julien Isorce <julien.isorce@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -25,6 +26,29 @@
#include "gl.h"
#include "gstglshader.h"
#if GST_GL_HAVE_GLES2
/* *INDENT-OFF* */
static const gchar *simple_vertex_shader_str_gles2 =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
static const gchar *simple_fragment_shader_str_gles2 =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D tex; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( tex, v_texCoord ); \n"
"} \n";
/* *INDENT-ON* */
#endif
#ifndef GL_COMPILE_STATUS
#define GL_COMPILE_STATUS 0x8B81
#endif
@ -575,6 +599,72 @@ gst_gl_shader_compile_and_check (GstGLShader * shader,
return TRUE;
}
gboolean
gst_gl_shader_compile_all_with_attribs_and_check (GstGLShader * shader,
const gchar * v_src, const gchar * f_src, const gint n_attribs,
const gchar * attrib_names[], GLint attrib_locs[])
{
gint i = 0;
GError *error = NULL;
gst_gl_shader_set_vertex_source (shader, v_src);
gst_gl_shader_set_fragment_source (shader, f_src);
gst_gl_shader_compile (shader, &error);
if (error) {
gst_gl_context_set_error (shader->context, "%s", error->message);
g_error_free (error);
gst_gl_context_clear_shader (shader->context);
return FALSE;
}
for (i = 0; i < n_attribs; i++)
attrib_locs[i] =
gst_gl_shader_get_attribute_location (shader, attrib_names[i]);
return TRUE;
}
#if GST_GL_HAVE_GLES2
gboolean
gst_gl_shader_compile_with_default_f_and_check (GstGLShader * shader,
const gchar * v_src, const gint n_attribs, const gchar * attrib_names[],
GLint attrib_locs[])
{
return gst_gl_shader_compile_all_with_attribs_and_check (shader, v_src,
simple_fragment_shader_str_gles2, n_attribs, attrib_names, attrib_locs);
}
gboolean
gst_gl_shader_compile_with_default_v_and_check (GstGLShader * shader,
const gchar * f_src, GLint * pos_loc, GLint * tex_loc)
{
const gchar *attrib_names[2] = { "a_position", "a_texCoord" };
GLint attrib_locs[2] = { 0 };
gboolean ret = TRUE;
ret =
gst_gl_shader_compile_all_with_attribs_and_check (shader,
simple_vertex_shader_str_gles2, f_src, 2, attrib_names, attrib_locs);
if (ret) {
*pos_loc = attrib_locs[0];
*tex_loc = attrib_locs[1];
}
return ret;
}
gboolean
gst_gl_shader_compile_with_default_vf_and_check (GstGLShader * shader,
GLint * pos_loc, GLint * tex_loc)
{
return gst_gl_shader_compile_with_default_v_and_check (shader,
simple_fragment_shader_str_gles2, pos_loc, tex_loc);
}
#endif
void
gst_gl_shader_set_uniform_1f (GstGLShader * shader, const gchar * name,
gfloat value)

View file

@ -1,6 +1,7 @@
/*
* GStreamer
* Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
* Copyright (C) 2014 Julien Isorce <julien.isorce@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -75,6 +76,12 @@ void gst_gl_shader_set_active (GstGLShader *shader, gboolean active);
gboolean gst_gl_shader_is_compiled (GstGLShader *shader);
gboolean gst_gl_shader_compile (GstGLShader *shader, GError **error);
gboolean gst_gl_shader_compile_and_check (GstGLShader *shader, const gchar *source, GstGLShaderSourceType type);
gboolean gst_gl_shader_compile_all_with_attribs_and_check (GstGLShader *shader, const gchar *v_src, const gchar *f_src, const gint n_attribs, const gchar *attrib_names[], GLint attrib_locs[]);
#if GST_GL_HAVE_GLES2
gboolean gst_gl_shader_compile_with_default_f_and_check (GstGLShader *shader, const gchar *v_src, const gint n_attribs, const gchar *attrib_names[], GLint attrib_locs[]);
gboolean gst_gl_shader_compile_with_default_v_and_check (GstGLShader *shader, const gchar *f_src, GLint *pos_loc, GLint *tex_loc);
gboolean gst_gl_shader_compile_with_default_vf_and_check (GstGLShader *shader, GLint *pos_loc, GLint *tex_loc);
#endif
void gst_gl_shader_release (GstGLShader *shader);
void gst_gl_shader_use (GstGLShader *shader);