gltestsrc: implement checkers pattern with GLSL
authorWang Xin-yu (王昕宇) <comicfans44@gmail.com>
Sun, 28 Sep 2014 02:51:09 +0000 (10:51 +0800)
committerTim-Philipp Müller <tim@centricular.com>
Sat, 9 Dec 2017 19:31:48 +0000 (19:31 +0000)
https://bugzilla.gnome.org/show_bug.cgi?id=737505

ext/gl/gltestsrc.c
ext/gl/gstgltestsrc.c

index 61a140f2942de249e562fd00ee081d7bccb833a3..525a959a6854963ed2ecc3a76896715d82f5d468 100644 (file)
@@ -188,28 +188,30 @@ gst_gl_test_src_smpte (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
 #endif
 }
 
-void
-gst_gl_test_src_shader (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
-{
-
-  GstGLFuncs *gl = v->context->gl_vtable;
-
 /* *INDENT-OFF* */
 
-  const GLfloat positions[] = {
+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,
   };
-
-  const GLfloat identitiy_matrix[] = {
+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)
+{
+
+  GstGLFuncs *gl = v->context->gl_vtable;
+
+/* *INDENT-OFF* */
   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);
 }
 
-void
-gst_gl_test_src_checkers1 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
+static void
+gst_gl_test_src_checkers (GstGLTestSrc * v, gint checker_width)
 {
-#if 0
-  int x, y;
-  paintinfo pi = { NULL, };
-  paintinfo *p = &pi;
-  struct fourcc_list_struct *fourcc;
 
-  p->width = w;
-  p->height = h;
-  fourcc = v->fourcc;
-  if (fourcc == NULL)
-    return;
+  GstGLFuncs *gl = v->context->gl_vtable;
 
-  fourcc->paint_setup (p, dest);
-  p->paint_hline = fourcc->paint_hline;
+  GLushort indices[] = { 0, 1, 2, 3, 0 };
 
-  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
-}
+  GLint attr_position_loc = 0;
 
-void
-gst_gl_test_src_checkers2 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
-{
-#if 0
-  int x, y;
-  paintinfo pi = { NULL, };
-  paintinfo *p = &pi;
-  struct fourcc_list_struct *fourcc;
+  if (gst_gl_context_get_gl_api (v->context)) {
 
-  p->width = w;
-  p->height = h;
-  fourcc = v->fourcc;
-  if (fourcc == NULL)
-    return;
+    gst_gl_context_clear_shader (v->context);
+    gl->BindTexture (GL_TEXTURE_2D, 0);
+    gl->Disable (GL_TEXTURE_2D);
 
-  fourcc->paint_setup (p, dest);
-  p->paint_hline = fourcc->paint_hline;
+    gst_gl_shader_use (v->shader);
 
-  p->color = vts_colors + COLOR_GREEN;
-  for (y = 0; y < h; y++) {
-    p->paint_hline (p, 0, y, w);
-  }
+    attr_position_loc =
+        gst_gl_shader_get_attribute_location (v->shader, "position");
 
-  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);
+    /* Load the vertex position */
+    gl->VertexAttribPointer (attr_position_loc, 4, GL_FLOAT,
+        GL_FALSE, 0, positions);
 
-      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
-}
+    gl->EnableVertexAttribArray (attr_position_loc);
 
-void
-gst_gl_test_src_checkers4 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
-{
-#if 0
-  int x, y;
-  paintinfo pi = { NULL, };
-  paintinfo *p = &pi;
-  struct fourcc_list_struct *fourcc;
+    gst_gl_shader_set_uniform_matrix_4fv (v->shader, "mvp",
+        1, GL_FALSE, identitiy_matrix);
 
-  p->width = w;
-  p->height = h;
-  fourcc = v->fourcc;
-  if (fourcc == NULL)
-    return;
+    gst_gl_shader_set_uniform_1f (v->shader, "checker_width", checker_width);
 
-  fourcc->paint_setup (p, dest);
-  p->paint_hline = fourcc->paint_hline;
+    gl->DrawElements (GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, indices);
 
-  p->color = vts_colors + COLOR_GREEN;
-  for (y = 0; y < h; y++) {
-    p->paint_hline (p, 0, y, w);
-  }
+    gl->DisableVertexAttribArray (attr_position_loc);
 
-  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);
-          }
-        }
-      }
-    }
+    gst_gl_context_clear_shader (v->context);
   }
-#endif
 }
 
+
 void
-gst_gl_test_src_checkers8 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
+gst_gl_test_src_checkers1 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
 {
-#if 0
-  int x, y;
-  paintinfo pi = { NULL, };
-  paintinfo *p = &pi;
-  struct fourcc_list_struct *fourcc;
+  gst_gl_test_src_checkers (v, 1);
+}
 
-  p->width = w;
-  p->height = h;
-  fourcc = v->fourcc;
-  if (fourcc == NULL)
-    return;
 
-  fourcc->paint_setup (p, dest);
-  p->paint_hline = fourcc->paint_hline;
+void
+gst_gl_test_src_checkers2 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
+{
+  gst_gl_test_src_checkers (v, 2);
+}
 
-  p->color = vts_colors + COLOR_GREEN;
-  for (y = 0; y < h; y++) {
-    p->paint_hline (p, 0, y, w);
-  }
+void
+gst_gl_test_src_checkers4 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
+{
+  gst_gl_test_src_checkers (v, 4);
 
-  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
+}
+
+void
+gst_gl_test_src_checkers8 (GstGLTestSrc * v, GstBuffer * buffer, int w, int h)
+{
+  gst_gl_test_src_checkers (v, 8);
 }
 
 void
index 0c7f2c1b9f89d9b38e1ef4674572a6e978ea1290..5abeeadbc2f0a24df46e8d0dd848b7f83b382021 100644 (file)
@@ -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: