[gegl/gsoc2009-gpu] brightness-contrast op: Implement GPU processor



commit f4280accb8b7a24ba0a8967995607f315a3f1946
Author: Jerson Michael Perpetua <jersonperpetua gmail com>
Date:   Wed Jul 8 01:05:15 2009 +0800

    brightness-contrast op: Implement GPU processor
    
    This should serve as an initial prototype to show how GPU acceleration is
    supported API/architecture-wise.

 operations/Makefile-common.am           |    4 +-
 operations/common/brightness-contrast.c |  109 +++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 2 deletions(-)
---
diff --git a/operations/Makefile-common.am b/operations/Makefile-common.am
index a439887..ba9e6dd 100644
--- a/operations/Makefile-common.am
+++ b/operations/Makefile-common.am
@@ -3,7 +3,7 @@ no_undefined = -no-undefined
 libgegl = $(top_builddir)/gegl/libgegl-$(GEGL_API_VERSION).la $(BABL_LIBS)
 endif
 
-op_libs = $(DEP_LIBS) $(libgegl)
+op_libs = $(DEP_LIBS) $(libgegl) $(GPU_LIBS)
 
 GEGLHEADERS     = $(wildcard $(top_srcdir)/gegl/*.h)\
                   $(wildcard $(top_srcdir)/gegl/buffer/*.h)
@@ -23,7 +23,7 @@ AM_CPPFLAGS = \
 	-I$(top_builddir)/gegl/gpu \
 	-I$(top_srcdir)/gegl/gpu
 
-AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
+AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS) $(GPU_CFLAGS)
 
 AM_LDFLAGS = -avoid-version -export-dynamic -module $(no_undefined)
 
diff --git a/operations/common/brightness-contrast.c b/operations/common/brightness-contrast.c
index ade7864..cbdeaab 100644
--- a/operations/common/brightness-contrast.c
+++ b/operations/common/brightness-contrast.c
@@ -16,6 +16,8 @@
  * Copyright 2006 �yvind Kolås <pippin gimp org>
  */
 
+#include <GL/glew.h>
+
 #include "config.h"
 #include <glib/gi18n-lib.h>
 
@@ -56,6 +58,44 @@ gegl_chant_double (brightness, _("Brightness"), -3.0, 3.0, 0.0,
  */
 #include "gegl-chant.h"
 
+#include "gegl-gpu-types.h"
+#include "gegl-gpu-init.h"
+
+static const char* shader_program_str = "                       \
+uniform sampler2DRect pixels;                                   \
+uniform float brightness, contrast;                             \
+                                                                \
+void main()                                                     \
+{                                                               \
+  vec4 pixel = texture2DRect(pixels, gl_TexCoord[0].st);        \
+  vec3 color = (pixel.rgb - 0.5) * contrast + brightness + 0.5; \
+  gl_FragColor = vec4(color, pixel.a);                          \
+}                                                               ";
+
+static GLuint shader_program = 0;
+static GLuint pixels_param;
+static GLuint brightness_param;
+static GLuint contrast_param;
+
+static GLuint
+create_shader_program (void)
+{
+  GLuint shader = glCreateShader (GL_FRAGMENT_SHADER);
+  GLuint program;
+
+  glShaderSource (shader, 1, &shader_program_str, NULL);
+  glCompileShader (shader);
+
+  program = glCreateProgram ();
+  glAttachShader (program, shader);
+  glLinkProgram (program);
+
+  pixels_param = glGetUniformLocation (program, "pixels");
+  brightness_param = glGetUniformLocation (program, "brightness");
+  contrast_param = glGetUniformLocation (program, "contrast");
+
+  return program;
+}
 
 /* prepare() is called on each operation providing data to a node that
  * is requested to provide a rendered result. When prepare is called
@@ -108,6 +148,72 @@ process (GeglOperation       *op,
   return TRUE;
 }
 
+static gboolean
+process_gpu (GeglOperation       *op,
+             GeglGpuTexture      *in,
+             GeglGpuTexture      *out,
+             glong                samples,
+             const GeglRectangle *roi)
+{
+  /* Retrieve a pointer to GeglChantO structure which contains all the
+   * chanted properties
+   */
+  GeglChantO *o          = GEGL_CHANT_PROPERTIES (op);
+  gfloat      brightness = o->brightness;
+  gfloat      contrast   = o->contrast;
+
+  /* attach *out* texture to offscreen framebuffer */
+  glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, 
+                             GL_COLOR_ATTACHMENT0_EXT,
+                             GL_TEXTURE_RECTANGLE_ARB,
+                             out->handle,
+                             0);
+
+  /* set *out* texture as render target */
+  glDrawBuffer (GL_COLOR_ATTACHMENT0_EXT);
+
+  /* create and register shader program, all shader programs will be deleted
+   * after GEGL terminates
+   */
+  if (shader_program == 0)
+    {
+      shader_program = create_shader_program ();
+      gegl_gpu_register_shader_program (shader_program);
+    }
+  glUseProgram (shader_program);
+
+  /* setup shader parameters */
+  glActiveTexture (GL_TEXTURE0);
+  glBindTexture (GL_TEXTURE_RECTANGLE_ARB, in->handle);
+  glUniform1i (pixels_param, 0);
+  glUniform1f (brightness_param, brightness);
+  glUniform1f (contrast_param, contrast);
+
+  /* viewport transform for 1:1 pixel=texel=data mapping */
+  glMatrixMode (GL_PROJECTION);
+  glLoadIdentity ();
+  gluOrtho2D (0.0, roi->width, 0.0, roi->height);
+  glMatrixMode (GL_MODELVIEW);
+  glLoadIdentity ();
+  glViewport (0, 0, roi->width, roi->height);
+
+  /* make quad filled to hit every pixel/texel */
+  glPolygonMode (GL_FRONT, GL_FILL);
+
+  /* and render quad */
+  glBegin (GL_QUADS);
+    glTexCoord2f (0.0, 0.0);
+    glVertex2f (0.0, 0.0);
+    glTexCoord2f (roi->width, 0.0);
+    glVertex2f (roi->width, 0.0);
+    glTexCoord2f (roi->width, roi->height);
+    glVertex2f (roi->width, roi->height);
+    glTexCoord2f (0.0, roi->height);
+    glVertex2f (0.0, roi->height);
+  glEnd ();
+
+  return TRUE;
+}
 
 #ifdef HAS_G4FLOAT
 /* The compiler supports vector extensions allowing an version of
@@ -183,6 +289,9 @@ gegl_chant_class_init (GeglChantClass *klass)
   gegl_operation_class_add_processor (operation_class,
                                       G_CALLBACK (process_simd), "simd");
 #endif
+  gegl_operation_class_add_processor (operation_class,
+                                      G_CALLBACK (process_gpu),
+                                      "gpu:reference");
 }
 
 #endif /* closing #ifdef GEGL_CHANT_PROPERTIES ... else ... */



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