[retro-gtk/wip/aplazas/gl-display] fix loading filters



commit e7525b0fde656553f18497e5af7cb58f1410b764
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed Nov 15 21:02:19 2017 +0100

    fix loading filters

 retro-gtk/retro-gl-display.c   |  155 +++++++++++++++++++---------------------
 retro-gtk/retro-video-filter.c |    1 +
 retro-gtk/retro-video-filter.h |    2 +
 3 files changed, 76 insertions(+), 82 deletions(-)
---
diff --git a/retro-gtk/retro-gl-display.c b/retro-gtk/retro-gl-display.c
index 7769c55..372bd7c 100644
--- a/retro-gtk/retro-gl-display.c
+++ b/retro-gtk/retro-gl-display.c
@@ -15,7 +15,7 @@ struct _RetroGLDisplay
   gfloat aspect_ratio;
   gulong on_video_output_id;
 
-  RetroGLSLFilter *glsl_filter;
+  RetroGLSLFilter *glsl_filter[RETRO_VIDEO_FILTER_COUNT];
   GLuint texture;
 };
 
@@ -28,6 +28,33 @@ enum {
 
 static GParamSpec *properties [N_PROPS];
 
+typedef struct {
+  struct {
+    float x, y;
+  } position;
+  struct {
+    float x, y;
+  } texture_coordinates;
+} RetroVertex;
+
+static float vertices[] = {
+  -1.0f,  1.0f, 0.0f, 0.0f, // Top-left
+   1.0f,  1.0f, 1.0f, 0.0f, // Top-right
+   1.0f, -1.0f, 1.0f, 1.0f, // Bottom-right
+  -1.0f, -1.0f, 0.0f, 1.0f, // Bottom-left
+};
+
+static GLuint elements[] = {
+    0, 1, 2,
+    2, 3, 0,
+};
+
+static const gchar *filter_uris[] = {
+  "resource:///org/gnome/Retro/glsl-filters/bicubic.filter",
+  "resource:///org/gnome/Retro/glsl-filters/sharp.filter",
+  "resource:///org/gnome/Retro/glsl-filters/crt-simple.filter",
+};
+
 /* Private */
 
 static void
@@ -70,83 +97,13 @@ retro_gl_display_get_video_box (RetroGLDisplay *self,
   *y = (h - *height) / 2;
 }
 
-typedef struct {
-  struct {
-    float x, y;
-  } position;
-  struct {
-    float x, y;
-  } texture_coordinates;
-} RetroVertex;
-
-static float vertices[] = {
-  -1.0f,  1.0f, 0.0f, 0.0f, // Top-left
-   1.0f,  1.0f, 1.0f, 0.0f, // Top-right
-   1.0f, -1.0f, 1.0f, 1.0f, // Bottom-right
-  -1.0f, -1.0f, 0.0f, 1.0f, // Bottom-left
-};
-
-static GLuint elements[] = {
-    0, 1, 2,
-    2, 3, 0,
-};
-
-static void
-prepare_shader_program (RetroGLDisplay *self)
-{
-  const gchar *uri;
-  GLint attrib;
-
-  if (self->glsl_filter != NULL)
-    return;
-
-  switch (self->filter) {
-  case RETRO_VIDEO_FILTER_SHARP:
-    uri = "resource:///org/gnome/Retro/glsl-filters/sharp.filter";
-
-    break;
-  case RETRO_VIDEO_FILTER_CRT:
-    uri = "resource:///org/gnome/Retro/glsl-filters/crt-simple.filter";
-
-    break;
-  case RETRO_VIDEO_FILTER_SMOOTH:
-  default:
-    uri = "resource:///org/gnome/Retro/glsl-filters/bicubic.filter";
-
-    break;
-  }
-
-  self->glsl_filter = retro_glsl_filter_new (uri, NULL);
-  retro_glsl_filter_use_program (self->glsl_filter);
-
-  retro_glsl_filter_set_attribute_pointer (self->glsl_filter,
-                                           "position",
-                                           sizeof (((RetroVertex *) NULL)->position) / sizeof (float),
-                                           GL_FLOAT,
-                                           GL_FALSE,
-                                           sizeof (RetroVertex),
-                                           offsetof (RetroVertex, position));
-
-  retro_glsl_filter_set_attribute_pointer (self->glsl_filter,
-                                           "texCoord",
-                                           sizeof (((RetroVertex *) NULL)->texture_coordinates) / sizeof 
(float),
-                                           GL_FLOAT,
-                                           GL_FALSE,
-                                           sizeof (RetroVertex),
-                                           offsetof (RetroVertex, texture_coordinates));
-
-  glDeleteTextures (1, &self->texture);
-  self->texture = 0;
-  glGenTextures (1, &self->texture);
-  glBindTexture (GL_TEXTURE_2D, self->texture);
-}
-
 static void
 retro_gl_display_realize (RetroGLDisplay *self)
 {
   GLuint vertex_buffer_object;
   GLuint vertex_array_object;
   GLuint element_buffer_object;
+  RetroVideoFilter filter;
 
   gtk_gl_area_make_current (GTK_GL_AREA (self));
 
@@ -160,16 +117,45 @@ retro_gl_display_realize (RetroGLDisplay *self)
   glGenBuffers (1, &element_buffer_object);
   glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, element_buffer_object);
   glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (elements), elements, GL_STATIC_DRAW);
+
+  for (filter = 0; filter < RETRO_VIDEO_FILTER_COUNT; filter++) {
+    self->glsl_filter[filter] = retro_glsl_filter_new (filter_uris[filter], NULL);
+    retro_glsl_filter_use_program (self->glsl_filter[filter]);
+
+    retro_glsl_filter_set_attribute_pointer (self->glsl_filter[filter],
+                                             "position",
+                                             sizeof (((RetroVertex *) NULL)->position) / sizeof (float),
+                                             GL_FLOAT,
+                                             GL_FALSE,
+                                             sizeof (RetroVertex),
+                                             offsetof (RetroVertex, position));
+
+    retro_glsl_filter_set_attribute_pointer (self->glsl_filter[filter],
+                                             "texCoord",
+                                             sizeof (((RetroVertex *) NULL)->texture_coordinates) / sizeof 
(float),
+                                             GL_FLOAT,
+                                             GL_FALSE,
+                                             sizeof (RetroVertex),
+                                             offsetof (RetroVertex, texture_coordinates));
+  }
+
+  glDeleteTextures (1, &self->texture);
+  self->texture = 0;
+  glGenTextures (1, &self->texture);
+  glBindTexture (GL_TEXTURE_2D, self->texture);
 }
 
 static void
 retro_gl_display_unrealize (RetroGLDisplay *self)
 {
+  RetroVideoFilter filter;
+
   gtk_gl_area_make_current (GTK_GL_AREA (self));
 
   glDeleteTextures (1, &self->texture);
   self->texture = 0;
-  g_clear_object (&self->glsl_filter);
+  for (filter = 0; filter < RETRO_VIDEO_FILTER_COUNT; filter++)
+    g_clear_object (&self->glsl_filter[filter]);
 }
 
 static gboolean
@@ -182,12 +168,17 @@ retro_gl_display_render (RetroGLDisplay *self)
   GLfloat source_width, source_height;
   GLfloat target_width, target_height;
   GLfloat output_width, output_height;
+  RetroVideoFilter filter;
 
   g_return_val_if_fail (self != NULL, FALSE);
 
   retro_gl_display_get_video_box (self, &w, &h, &x, &y);
 
-  prepare_shader_program (self);
+  filter = self->filter >= RETRO_VIDEO_FILTER_COUNT ?
+    RETRO_VIDEO_FILTER_SMOOTH :
+    self->filter;
+
+  retro_glsl_filter_use_program (self->glsl_filter[filter]);
 
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -203,10 +194,9 @@ retro_gl_display_render (RetroGLDisplay *self)
                 GL_RGBA, GL_UNSIGNED_BYTE,
                 gdk_pixbuf_get_pixels (self->pixbuf));
 
-  if (self->glsl_filter != NULL)
-    retro_glsl_filter_apply_texture_params (self->glsl_filter);
+  retro_glsl_filter_apply_texture_params (self->glsl_filter[filter]);
 
-  retro_glsl_filter_set_uniform_1f (self->glsl_filter, "relative_aspect_ratio",
+  retro_glsl_filter_set_uniform_1f (self->glsl_filter[filter], "relative_aspect_ratio",
     (gfloat) gtk_widget_get_allocated_width (GTK_WIDGET (self)) /
     (gfloat) gtk_widget_get_allocated_height (GTK_WIDGET (self)) /
     self->aspect_ratio);
@@ -218,15 +208,15 @@ retro_gl_display_render (RetroGLDisplay *self)
   output_width = (GLfloat) gtk_widget_get_allocated_width (GTK_WIDGET (self));
   output_height = (GLfloat) gtk_widget_get_allocated_height (GTK_WIDGET (self));
 
-  retro_glsl_filter_set_uniform_4f (self->glsl_filter, "sourceSize[0]",
+  retro_glsl_filter_set_uniform_4f (self->glsl_filter[filter], "sourceSize[0]",
                                     source_width, source_height,
                                     1.0f / source_width, 1.0f / source_height);
 
-  retro_glsl_filter_set_uniform_4f (self->glsl_filter, "targetSize",
+  retro_glsl_filter_set_uniform_4f (self->glsl_filter[filter], "targetSize",
                                     target_width, target_height,
                                     1.0f / target_width, 1.0f / target_height);
 
-  retro_glsl_filter_set_uniform_4f (self->glsl_filter, "outputSize",
+  retro_glsl_filter_set_uniform_4f (self->glsl_filter[filter], "outputSize",
                                     output_width, output_height,
                                     1.0f / output_width, 1.0f / output_height);
 
@@ -239,10 +229,12 @@ static void
 retro_gl_display_finalize (GObject *object)
 {
   RetroGLDisplay *self = (RetroGLDisplay *) object;
+  RetroVideoFilter filter;
 
   glDeleteTextures (1, &self->texture);
   self->texture = 0;
-  g_clear_object (&self->glsl_filter);
+  for (filter = 0; filter < RETRO_VIDEO_FILTER_COUNT; filter++)
+    g_clear_object (&self->glsl_filter[filter]);
   if (self->core != NULL)
     g_object_unref (self->core);
   if (self->pixbuf != NULL)
@@ -458,7 +450,6 @@ retro_gl_display_set_filter (RetroGLDisplay   *self,
   g_return_if_fail (self != NULL);
 
   self->filter = filter;
-  g_clear_object (&self->glsl_filter);
   gtk_widget_queue_draw (GTK_WIDGET (self));
 }
 
diff --git a/retro-gtk/retro-video-filter.c b/retro-gtk/retro-video-filter.c
index 70e9c9c..dc86d7f 100644
--- a/retro-gtk/retro-video-filter.c
+++ b/retro-gtk/retro-video-filter.c
@@ -42,6 +42,7 @@ retro_video_filter_get_type (void)
       { RETRO_VIDEO_FILTER_SMOOTH, "RETRO_VIDEO_FILTER_SMOOTH", "smooth" },
       { RETRO_VIDEO_FILTER_SHARP, "RETRO_VIDEO_FILTER_SHARP", "sharp" },
       { RETRO_VIDEO_FILTER_CRT, "RETRO_VIDEO_FILTER_CRT", "crt" },
+      { RETRO_VIDEO_FILTER_COUNT, "RETRO_VIDEO_FILTER_COUNT", "count" },
       { 0, NULL, NULL },
     };
     GType type;
diff --git a/retro-gtk/retro-video-filter.h b/retro-gtk/retro-video-filter.h
index 0926278..9cdb647 100644
--- a/retro-gtk/retro-video-filter.h
+++ b/retro-gtk/retro-video-filter.h
@@ -20,6 +20,7 @@ GType retro_video_filter_get_type (void) G_GNUC_CONST;
  * @RETRO_VIDEO_FILTER_SMOOTH: a smooth but blurry video filer
  * @RETRO_VIDEO_FILTER_SHARP: a sharp video filter showing every pixel
  * @RETRO_VIDEO_FILTER_CRT: a video filter mimicking CRT screens
+ * @RETRO_VIDEO_FILTER_COUNT: the number of video filters
  *
  * Represents the filters that can be applied to the video output.
  */
@@ -28,6 +29,7 @@ typedef enum
   RETRO_VIDEO_FILTER_SMOOTH,
   RETRO_VIDEO_FILTER_SHARP,
   RETRO_VIDEO_FILTER_CRT,
+  RETRO_VIDEO_FILTER_COUNT,
 } RetroVideoFilter;
 
 RetroVideoFilter retro_video_filter_from_string (const gchar *filter);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]