[gegl/gsoc2009-gpu] gegl:invert op: implement GPU processor
- From: Jerson Michael Perpetua <jperpetua src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gegl/gsoc2009-gpu] gegl:invert op: implement GPU processor
- Date: Sun, 23 Aug 2009 14:32:35 +0000 (UTC)
commit c9e085adb51b085fe42434c14f23ed637579d422
Author: Jerson Michael Perpetua <jersonperpetua gmail com>
Date: Sun Aug 23 22:26:47 2009 +0800
gegl:invert op: implement GPU processor
operations/common/invert.c | 97 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 97 insertions(+), 0 deletions(-)
---
diff --git a/operations/common/invert.c b/operations/common/invert.c
index 02c4937..04613a1 100644
--- a/operations/common/invert.c
+++ b/operations/common/invert.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>
@@ -32,6 +34,40 @@
#include "gegl-chant.h"
+#include "gegl-gpu-types.h"
+#include "gegl-gpu-init.h"
+
+static const char* shader_program_str = " \
+uniform sampler2DRect pixels; \
+ \
+void main() \
+{ \
+ vec4 pixel = texture2DRect (pixels, gl_TexCoord[0].st); \
+ vec3 color = 1.0 - pixel.rgb; \
+ gl_FragColor = vec4 (color, pixel.a); \
+} ";
+
+static GLuint shader_program = 0;
+static GLuint pixels_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");
+
+ return program;
+}
+
static gboolean
process (GeglOperation *op,
void *in_buf,
@@ -60,6 +96,64 @@ process (GeglOperation *op,
return TRUE;
}
+static gboolean
+process_gpu (GeglOperation *op,
+ GeglGpuTexture *in,
+ GeglGpuTexture *out,
+ glong samples,
+ const GeglRectangle *roi)
+{
+ /* 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);
+
+ /* 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
static gboolean
process_simd (GeglOperation *op,
@@ -105,6 +199,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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]