From e6212f5156fc043b3b0f20a6582520aa91f61569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wang=20Xin-yu=20=28=E7=8E=8B=E6=98=95=E5=AE=87=29?= Date: Sun, 28 Sep 2014 10:51:09 +0800 Subject: [PATCH] gltestsrc: implement checkers pattern with GLSL https://bugzilla.gnome.org/show_bug.cgi?id=737505 --- ext/gl/gltestsrc.c | 221 ++++++++++++------------------------------ ext/gl/gstgltestsrc.c | 28 ++++++ 2 files changed, 91 insertions(+), 158 deletions(-) diff --git a/ext/gl/gltestsrc.c b/ext/gl/gltestsrc.c index 61a140f294..525a959a68 100644 --- a/ext/gl/gltestsrc.c +++ b/ext/gl/gltestsrc.c @@ -188,6 +188,23 @@ gst_gl_test_src_smpte (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) #endif } +/* *INDENT-OFF* */ + +static const GLfloat positions[] = { + -1.0, 1.0, 0.0, 1.0, + 1.0, 1.0, 0.0, 1.0, + 1.0, -1.0, 0.0, 1.0, + -1.0, -1.0, 0.0, 1.0, + }; +static const GLfloat identitiy_matrix[] = { + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0, + }; + +/* *INDENT-ON* */ + void gst_gl_test_src_shader (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) { @@ -195,21 +212,6 @@ gst_gl_test_src_shader (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) GstGLFuncs *gl = v->context->gl_vtable; /* *INDENT-OFF* */ - - const GLfloat positions[] = { - -1.0, 1.0, 0.0, 1.0, - 1.0, 1.0, 0.0, 1.0, - 1.0, -1.0, 0.0, 1.0, - -1.0, -1.0, 0.0, 1.0, - }; - - const GLfloat identitiy_matrix[] = { - 1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0, - }; - const GLfloat uvs[] = { 0.0, 1.0, 1.0, 1.0, @@ -306,168 +308,71 @@ gst_gl_test_src_blue (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) gst_gl_test_src_unicolor (v, buffer, w, h, vts_colors + COLOR_BLUE); } +static void +gst_gl_test_src_checkers (GstGLTestSrc * v, gint checker_width) +{ + + GstGLFuncs *gl = v->context->gl_vtable; + + GLushort indices[] = { 0, 1, 2, 3, 0 }; + + GLint attr_position_loc = 0; + + if (gst_gl_context_get_gl_api (v->context)) { + + gst_gl_context_clear_shader (v->context); + gl->BindTexture (GL_TEXTURE_2D, 0); + gl->Disable (GL_TEXTURE_2D); + + gst_gl_shader_use (v->shader); + + attr_position_loc = + gst_gl_shader_get_attribute_location (v->shader, "position"); + + /* Load the vertex position */ + gl->VertexAttribPointer (attr_position_loc, 4, GL_FLOAT, + GL_FALSE, 0, positions); + + gl->EnableVertexAttribArray (attr_position_loc); + + gst_gl_shader_set_uniform_matrix_4fv (v->shader, "mvp", + 1, GL_FALSE, identitiy_matrix); + + gst_gl_shader_set_uniform_1f (v->shader, "checker_width", checker_width); + + gl->DrawElements (GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, indices); + + gl->DisableVertexAttribArray (attr_position_loc); + + gst_gl_context_clear_shader (v->context); + } +} + + void gst_gl_test_src_checkers1 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) { -#if 0 - int x, y; - paintinfo pi = { NULL, }; - paintinfo *p = π - struct fourcc_list_struct *fourcc; - - p->width = w; - p->height = h; - fourcc = v->fourcc; - if (fourcc == NULL) - return; - - fourcc->paint_setup (p, dest); - p->paint_hline = fourcc->paint_hline; - - for (y = 0; y < h; y++) { - p->color = vts_colors + COLOR_GREEN; - p->paint_hline (p, 0, y, w); - for (x = (y % 2); x < w; x += 2) { - p->color = vts_colors + COLOR_RED; - p->paint_hline (p, x, y, 1); - } - } -#endif + gst_gl_test_src_checkers (v, 1); } + void gst_gl_test_src_checkers2 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) { -#if 0 - int x, y; - paintinfo pi = { NULL, }; - paintinfo *p = π - struct fourcc_list_struct *fourcc; - - p->width = w; - p->height = h; - fourcc = v->fourcc; - if (fourcc == NULL) - return; - - fourcc->paint_setup (p, dest); - p->paint_hline = fourcc->paint_hline; - - p->color = vts_colors + COLOR_GREEN; - for (y = 0; y < h; y++) { - p->paint_hline (p, 0, y, w); - } - - for (y = 0; y < h; y += 2) { - for (x = ((y % 4) == 0) ? 0 : 2; x < w; x += 4) { - guint len = (x < (w - 1)) ? 2 : (w - x); - - p->color = vts_colors + COLOR_RED; - p->paint_hline (p, x, y + 0, len); - if (G_LIKELY ((y + 1) < h)) { - p->paint_hline (p, x, y + 1, len); - } - } - } -#endif + gst_gl_test_src_checkers (v, 2); } void gst_gl_test_src_checkers4 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) { -#if 0 - int x, y; - paintinfo pi = { NULL, }; - paintinfo *p = π - struct fourcc_list_struct *fourcc; + gst_gl_test_src_checkers (v, 4); - p->width = w; - p->height = h; - fourcc = v->fourcc; - if (fourcc == NULL) - return; - - fourcc->paint_setup (p, dest); - p->paint_hline = fourcc->paint_hline; - - p->color = vts_colors + COLOR_GREEN; - for (y = 0; y < h; y++) { - p->paint_hline (p, 0, y, w); - } - - for (y = 0; y < h; y += 4) { - for (x = ((y % 8) == 0) ? 0 : 4; x < w; x += 8) { - guint len = (x < (w - 3)) ? 4 : (w - x); - - p->color = vts_colors + COLOR_RED; - p->paint_hline (p, x, y + 0, len); - if (G_LIKELY ((y + 1) < h)) { - p->paint_hline (p, x, y + 1, len); - if (G_LIKELY ((y + 2) < h)) { - p->paint_hline (p, x, y + 2, len); - if (G_LIKELY ((y + 3) < h)) { - p->paint_hline (p, x, y + 3, len); - } - } - } - } - } -#endif } void gst_gl_test_src_checkers8 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h) { -#if 0 - int x, y; - paintinfo pi = { NULL, }; - paintinfo *p = π - struct fourcc_list_struct *fourcc; - - p->width = w; - p->height = h; - fourcc = v->fourcc; - if (fourcc == NULL) - return; - - fourcc->paint_setup (p, dest); - p->paint_hline = fourcc->paint_hline; - - p->color = vts_colors + COLOR_GREEN; - for (y = 0; y < h; y++) { - p->paint_hline (p, 0, y, w); - } - - for (y = 0; y < h; y += 8) { - for (x = ((GST_ROUND_UP_8 (y) % 16) == 0) ? 0 : 8; x < w; x += 16) { - guint len = (x < (w - 7)) ? 8 : (w - x); - - p->color = vts_colors + COLOR_RED; - p->paint_hline (p, x, y + 0, len); - if (G_LIKELY ((y + 1) < h)) { - p->paint_hline (p, x, y + 1, len); - if (G_LIKELY ((y + 2) < h)) { - p->paint_hline (p, x, y + 2, len); - if (G_LIKELY ((y + 3) < h)) { - p->paint_hline (p, x, y + 3, len); - if (G_LIKELY ((y + 4) < h)) { - p->paint_hline (p, x, y + 4, len); - if (G_LIKELY ((y + 5) < h)) { - p->paint_hline (p, x, y + 5, len); - if (G_LIKELY ((y + 6) < h)) { - p->paint_hline (p, x, y + 6, len); - if (G_LIKELY ((y + 7) < h)) { - p->paint_hline (p, x, y + 7, len); - } - } - } - } - } - } - } - } - } -#endif + gst_gl_test_src_checkers (v, 8); } void diff --git a/ext/gl/gstgltestsrc.c b/ext/gl/gstgltestsrc.c index 0c7f2c1b9f..5abeeadbc2 100644 --- a/ext/gl/gstgltestsrc.c +++ b/ext/gl/gstgltestsrc.c @@ -301,6 +301,26 @@ const gchar *mandelbrot_fragment_src = "uniform float time; \ gl_FragColor = iterate_pixel(fractal_position); \ }"; + +const gchar *checkers_vertex_src = "attribute vec4 position; \ + uniform mat4 mvp; \ + void main() \ + { \ + gl_Position = mvp * position; \ + }"; + +const gchar *checkers_fragment_src = "uniform float checker_width; \ + void main() \ + { \ + vec2 xy_index= floor((gl_FragCoord.xy-vec2(0.5,0.5))/checker_width); \ + vec2 xy_mod=mod(xy_index,vec2(2.0,2.0)); \ + float result=mod(xy_mod.x+xy_mod.y,2.0); \ + gl_FragColor.r=step(result,0.5); \ + gl_FragColor.g=1.0-gl_FragColor.r; \ + gl_FragColor.ba=vec2(0,1); \ + }"; + + static void gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc, gint pattern_type) { @@ -333,15 +353,23 @@ gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc, gint pattern_type) gltestsrc->make_image = gst_gl_test_src_blue; break; case GST_GL_TEST_SRC_CHECKERS1: + gltestsrc->vertex_src = checkers_vertex_src; + gltestsrc->fragment_src = checkers_fragment_src; gltestsrc->make_image = gst_gl_test_src_checkers1; break; case GST_GL_TEST_SRC_CHECKERS2: + gltestsrc->vertex_src = checkers_vertex_src; + gltestsrc->fragment_src = checkers_fragment_src; gltestsrc->make_image = gst_gl_test_src_checkers2; break; case GST_GL_TEST_SRC_CHECKERS4: + gltestsrc->vertex_src = checkers_vertex_src; + gltestsrc->fragment_src = checkers_fragment_src; gltestsrc->make_image = gst_gl_test_src_checkers4; break; case GST_GL_TEST_SRC_CHECKERS8: + gltestsrc->vertex_src = checkers_vertex_src; + gltestsrc->fragment_src = checkers_fragment_src; gltestsrc->make_image = gst_gl_test_src_checkers8; break; case GST_GL_TEST_SRC_CIRCULAR: