[cogl/wip/neil/snippets: 12/26] pipeline: Move the snippet source loop into a common function



commit 0a0029b3c2cf93eee78a02a0cf40bdec62e3a364
Author: Neil Roberts <neil linux intel com>
Date:   Fri Nov 25 15:31:21 2011 +0000

    pipeline: Move the snippet source loop into a common function
    
    The two loops that generate the functions for the snippets in the
    fragend and vertend are very similar so to avoid code duplication this
    patch moves the logic to its own function in a new
    cogl-pipeline-snippet.c file.

 cogl/Makefile.am                     |    2 +
 cogl/cogl-pipeline-fragend-glsl.c    |   65 ++------------
 cogl/cogl-pipeline-private.h         |   30 +------
 cogl/cogl-pipeline-snippet-private.h |  103 ++++++++++++++++++++++
 cogl/cogl-pipeline-snippet.c         |  160 ++++++++++++++++++++++++++++++++++
 cogl/cogl-pipeline-vertend-glsl.c    |   67 +++------------
 6 files changed, 288 insertions(+), 139 deletions(-)
---
diff --git a/cogl/Makefile.am b/cogl/Makefile.am
index be0254c..a49a65b 100644
--- a/cogl/Makefile.am
+++ b/cogl/Makefile.am
@@ -273,6 +273,8 @@ cogl_sources_c = \
 	$(srcdir)/cogl-pipeline-vertend-fixed-private.h	\
 	$(srcdir)/cogl-pipeline-progend-glsl.c		\
 	$(srcdir)/cogl-pipeline-progend-glsl-private.h	\
+	$(srcdir)/cogl-pipeline-snippet-private.h	\
+	$(srcdir)/cogl-pipeline-snippet.c		\
 	$(srcdir)/cogl-pipeline-cache.h			\
 	$(srcdir)/cogl-pipeline-cache.c			\
 	$(srcdir)/cogl-material-compat.c		\
diff --git a/cogl/cogl-pipeline-fragend-glsl.c b/cogl/cogl-pipeline-fragend-glsl.c
index 8bbd9fa..825e8fd 100644
--- a/cogl/cogl-pipeline-fragend-glsl.c
+++ b/cogl/cogl-pipeline-fragend-glsl.c
@@ -895,8 +895,7 @@ _cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline,
       GLint lengths[2];
       GLint compile_status;
       GLuint shader;
-      CoglPipelineSnippet *snippet;
-      int snippet_num;
+      CoglPipelineSnippetData snippet_data;
 
       COGL_STATIC_COUNTER (fragend_glsl_compile_counter,
                            "glsl fragment compile counter",
@@ -937,59 +936,15 @@ _cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline,
       /* Close the function surrounding the generated fragment processing */
       g_string_append (shader_state->source, "}\n");
 
-      /* Add all of the post-hooks for fragment processing */
-      snippet_num = 0;
-      COGL_LIST_FOREACH (snippet, get_fragment_snippets (pipeline), list_node)
-        if (snippet->hook == COGL_PIPELINE_SNIPPET_HOOK_FRAGMENT)
-          {
-            const char *source;
-
-            if ((source = cogl_snippet_get_declarations (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            g_string_append_printf (shader_state->source,
-                                    "\n"
-                                    "void\n"
-                                    "cogl_snippet%i ()\n"
-                                    "{\n",
-                                    snippet_num);
-
-            if ((source = cogl_snippet_get_pre (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            /* Chain on to the next function, or bypass it if there is
-               a replace string */
-            if ((source = cogl_snippet_get_replace (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-            else if (snippet_num > 0)
-              g_string_append_printf (shader_state->source,
-                                      "  cogl_snippet%i ();\n",
-                                      snippet_num - 1);
-            else
-              g_string_append (shader_state->source,
-                               "  cogl_generated_source ();\n");
-
-            if ((source = cogl_snippet_get_post (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            g_string_append (shader_state->source, "}\n");
-
-            snippet_num++;
-          }
-
-      g_string_append (shader_state->source,
-                       "\n"
-                       "void\n"
-                       "main ()\n"
-                       "{\n");
-      if (snippet_num > 0)
-        g_string_append_printf (shader_state->source,
-                                "  cogl_snippet%i ();\n",
-                                snippet_num - 1);
-      else
-        g_string_append (shader_state->source,
-                         "  cogl_generated_source ();\n");
-      g_string_append (shader_state->source, "}\n");
+      /* Add all of the hooks for fragment processing */
+      memset (&snippet_data, 0, sizeof (snippet_data));
+      snippet_data.snippets = get_fragment_snippets (pipeline);
+      snippet_data.hook = COGL_PIPELINE_SNIPPET_HOOK_FRAGMENT;
+      snippet_data.chain_function = "cogl_generated_source";
+      snippet_data.final_name = "main";
+      snippet_data.function_prefix = "cogl_fragment_hook";
+      snippet_data.source_buf = shader_state->source;
+      _cogl_pipeline_snippet_generate_code (&snippet_data);
 
       GE_RET( shader, ctx, glCreateShader (GL_FRAGMENT_SHADER) );
 
diff --git a/cogl/cogl-pipeline-private.h b/cogl/cogl-pipeline-private.h
index e518ecf..8e7f321 100644
--- a/cogl/cogl-pipeline-private.h
+++ b/cogl/cogl-pipeline-private.h
@@ -37,6 +37,7 @@
 #include "cogl-queue.h"
 #include "cogl-internal.h"
 #include "cogl-boxed-value.h"
+#include "cogl-pipeline-snippet-private.h"
 
 #include <glib.h>
 
@@ -366,35 +367,6 @@ typedef struct
   CoglBitmask changed_mask;
 } CoglPipelineUniformsState;
 
-/* Enumeration of all the hook points that a snippet can be attached
-   to within a pipeline. Note that although there are currently only
-   two points that directly correspond to the two state flags, the
-   idea isn't that each new enum here will mean a state flag. The
-   state flags are just intended to mark the split between hooks that
-   affect the fragment shader and hooks that affect the vertex
-   shader. For example, if we add a hook to wrap around the processing
-   for a particular layer then that hook would be part of the fragment
-   snippets state. */
-typedef enum
-{
-  COGL_PIPELINE_SNIPPET_HOOK_VERTEX,
-  COGL_PIPELINE_SNIPPET_HOOK_FRAGMENT
-} CoglPipelineSnippetHook;
-
-typedef struct _CoglPipelineSnippet CoglPipelineSnippet;
-
-COGL_LIST_HEAD (CoglPipelineSnippetList, CoglPipelineSnippet);
-
-struct _CoglPipelineSnippet
-{
-  COGL_LIST_ENTRY (CoglPipelineSnippet) list_node;
-
-  /* Hook where this snippet is attached */
-  CoglPipelineSnippetHook hook;
-
-  CoglSnippet *snippet;
-};
-
 typedef struct
 {
   CoglPipelineLightingState lighting_state;
diff --git a/cogl/cogl-pipeline-snippet-private.h b/cogl/cogl-pipeline-snippet-private.h
new file mode 100644
index 0000000..49fd6e5
--- /dev/null
+++ b/cogl/cogl-pipeline-snippet-private.h
@@ -0,0 +1,103 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ *
+ * Authors:
+ *   Neil Roberts <neil linux intel com>
+ */
+
+#ifndef __COGL_PIPELINE_SNIPPET_PRIVATE_H
+#define __COGL_PIPELINE_SNIPPET_PRIVATE_H
+
+#include "cogl-snippet.h"
+#include "cogl-queue.h"
+
+/* Enumeration of all the hook points that a snippet can be attached
+   to within a pipeline. Note that although there are currently only
+   two points that directly correspond to the two state flags, the
+   idea isn't that each new enum here will mean a state flag. The
+   state flags are just intended to mark the split between hooks that
+   affect the fragment shader and hooks that affect the vertex
+   shader. For example, if we add a hook to wrap around the processing
+   for a particular layer then that hook would be part of the fragment
+   snippets state. */
+typedef enum
+{
+  COGL_PIPELINE_SNIPPET_HOOK_VERTEX,
+  COGL_PIPELINE_SNIPPET_HOOK_FRAGMENT
+} CoglPipelineSnippetHook;
+
+typedef struct _CoglPipelineSnippet CoglPipelineSnippet;
+
+COGL_LIST_HEAD (CoglPipelineSnippetList, CoglPipelineSnippet);
+
+struct _CoglPipelineSnippet
+{
+  COGL_LIST_ENTRY (CoglPipelineSnippet) list_node;
+
+  /* Hook where this snippet is attached */
+  CoglPipelineSnippetHook hook;
+
+  CoglSnippet *snippet;
+};
+
+/* Arguments to pass to _cogl_pipeline_snippet_generate_code() */
+typedef struct
+{
+  CoglPipelineSnippetList *snippets;
+
+  /* Only snippets at this hook point will be used */
+  CoglPipelineSnippetHook hook;
+
+  /* The final function to chain on to after all of the snippets code
+     has been run */
+  const char *chain_function;
+
+  /* The name of the final generated function */
+  const char *final_name;
+
+  /* A prefix to insert before each generate function name */
+  const char *function_prefix;
+
+  /* The return type of all of the functions, or NULL to use void */
+  const char *return_type;
+
+  /* A variable to return from the functions. The snippets are
+     expected to modify this variable. Ignored if return_type is
+     NULL */
+  const char *return_variable;
+
+  /* The argument names or NULL if there are none */
+  const char *arguments;
+
+  /* The argument types or NULL */
+  const char *argument_declarations;
+
+  /* The string to generate the source into */
+  GString *source_buf;
+} CoglPipelineSnippetData;
+
+void
+_cogl_pipeline_snippet_generate_code (const CoglPipelineSnippetData *data);
+
+#endif /* __COGL_PIPELINE_SNIPPET_PRIVATE_H */
+
diff --git a/cogl/cogl-pipeline-snippet.c b/cogl/cogl-pipeline-snippet.c
new file mode 100644
index 0000000..b7a694c
--- /dev/null
+++ b/cogl/cogl-pipeline-snippet.c
@@ -0,0 +1,160 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ *
+ * Authors:
+ *   Neil Roberts <neil linux intel com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <string.h>
+
+#include "cogl-pipeline-snippet-private.h"
+
+/* Helper functions that are used by both GLSL pipeline backends */
+
+void
+_cogl_pipeline_snippet_generate_code (const CoglPipelineSnippetData *data)
+{
+  CoglPipelineSnippet *snippet;
+  int snippet_num = 0;
+
+  COGL_LIST_FOREACH (snippet, data->snippets, list_node)
+    if (snippet->hook == data->hook)
+      {
+        const char *source;
+
+        if ((source = cogl_snippet_get_declarations (snippet->snippet)))
+          g_string_append (data->source_buf, source);
+
+        g_string_append_printf (data->source_buf,
+                                "\n"
+                                "%s\n",
+                                data->return_type ?
+                                data->return_type :
+                                "void");
+
+        if (COGL_LIST_NEXT (snippet, list_node))
+          g_string_append_printf (data->source_buf,
+                                  "%s_%i",
+                                  data->function_prefix,
+                                  snippet_num);
+        else
+          g_string_append (data->source_buf, data->final_name);
+
+        g_string_append (data->source_buf, " (");
+
+        if (data->argument_declarations)
+          g_string_append (data->source_buf, data->argument_declarations);
+
+        g_string_append (data->source_buf,
+                         ")\n"
+                         "{\n");
+
+        if (data->return_type)
+          g_string_append_printf (data->source_buf,
+                                  "  %s %s;\n"
+                                  "\n",
+                                  data->return_type,
+                                  data->return_variable);
+
+        if ((source = cogl_snippet_get_pre (snippet->snippet)))
+          g_string_append (data->source_buf, source);
+
+        /* Chain on to the next function, or bypass it if there is
+           a replace string */
+        if ((source = cogl_snippet_get_replace (snippet->snippet)))
+          g_string_append (data->source_buf, source);
+        else
+          {
+            g_string_append (data->source_buf, "  ");
+
+            if (data->return_type)
+              g_string_append_printf (data->source_buf,
+                                      "%s = ",
+                                      data->return_variable);
+
+            if (snippet_num > 0)
+              g_string_append_printf (data->source_buf,
+                                      "%s_%i",
+                                      data->function_prefix,
+                                      snippet_num - 1);
+            else
+              g_string_append (data->source_buf, data->chain_function);
+
+            g_string_append (data->source_buf, " (");
+
+            if (data->arguments)
+              g_string_append (data->source_buf, data->arguments);
+
+            g_string_append (data->source_buf, ");\n");
+          }
+
+        if ((source = cogl_snippet_get_post (snippet->snippet)))
+          g_string_append (data->source_buf, source);
+
+        if (data->return_type)
+          g_string_append_printf (data->source_buf,
+                                  "  return %s;\n",
+                                  data->return_variable);
+
+        g_string_append (data->source_buf, "}\n");
+
+        snippet_num++;
+      }
+
+  /* If there weren't any snippets then generate a stub function with
+     the final name */
+  if (snippet_num == 0)
+    {
+      if (data->return_type)
+        g_string_append_printf (data->source_buf,
+                                "\n"
+                                "%s\n"
+                                "%s (%s)\n"
+                                "{\n"
+                                "  return %s (%s);\n"
+                                "}\n",
+                                data->return_type,
+                                data->final_name,
+                                data->argument_declarations ?
+                                data->argument_declarations : "",
+                                data->chain_function,
+                                data->arguments ? data->arguments : "");
+      else
+        g_string_append_printf (data->source_buf,
+                                "\n"
+                                "void\n"
+                                "%s (%s)\n"
+                                "{\n"
+                                "  %s (%s);\n"
+                                "}\n",
+                                data->final_name,
+                                data->argument_declarations ?
+                                data->argument_declarations : "",
+                                data->chain_function,
+                                data->arguments ? data->arguments : "");
+    }
+}
diff --git a/cogl/cogl-pipeline-vertend-glsl.c b/cogl/cogl-pipeline-vertend-glsl.c
index 16637ec..64b4737 100644
--- a/cogl/cogl-pipeline-vertend-glsl.c
+++ b/cogl/cogl-pipeline-vertend-glsl.c
@@ -29,6 +29,8 @@
 #include "config.h"
 #endif
 
+#include <string.h>
+
 #include "cogl-context-private.h"
 #include "cogl-pipeline-private.h"
 #include "cogl-pipeline-opengl-private.h"
@@ -360,8 +362,7 @@ _cogl_pipeline_vertend_glsl_end (CoglPipeline *pipeline,
       GLint lengths[2];
       GLint compile_status;
       GLuint shader;
-      CoglPipelineSnippet *snippet;
-      int snippet_num;
+      CoglPipelineSnippetData snippet_data;
 
       COGL_STATIC_COUNTER (vertend_glsl_compile_counter,
                            "glsl vertex compile counter",
@@ -377,59 +378,15 @@ _cogl_pipeline_vertend_glsl_end (CoglPipeline *pipeline,
                        "  cogl_color_out = cogl_color_in;\n"
                        "}\n");
 
-      /* Add all of the post-hooks for vertex processing */
-      snippet_num = 0;
-      COGL_LIST_FOREACH (snippet, get_vertex_snippets (pipeline), list_node)
-        if (snippet->hook == COGL_PIPELINE_SNIPPET_HOOK_VERTEX)
-          {
-            const char *source;
-
-            if ((source = cogl_snippet_get_declarations (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            g_string_append_printf (shader_state->source,
-                                    "\n"
-                                    "void\n"
-                                    "cogl_snippet%i ()\n"
-                                    "{\n",
-                                    snippet_num);
-
-            if ((source = cogl_snippet_get_pre (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            /* Chain on to the next function, or bypass it if there is
-               a replace string */
-            if ((source = cogl_snippet_get_replace (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-            else if (snippet_num > 0)
-              g_string_append_printf (shader_state->source,
-                                      "  cogl_snippet%i ();\n",
-                                      snippet_num - 1);
-            else
-              g_string_append (shader_state->source,
-                               "  cogl_generated_source ();\n");
-
-            if ((source = cogl_snippet_get_post (snippet->snippet)))
-              g_string_append (shader_state->source, source);
-
-            g_string_append (shader_state->source, "}\n");
-
-            snippet_num++;
-          }
-
-      g_string_append (shader_state->source,
-                       "\n"
-                       "void\n"
-                       "main ()\n"
-                       "{\n");
-      if (snippet_num > 0)
-        g_string_append_printf (shader_state->source,
-                                "  cogl_snippet%i ();\n",
-                                snippet_num - 1);
-      else
-        g_string_append (shader_state->source,
-                         "  cogl_generated_source ();\n");
-      g_string_append (shader_state->source, "}\n");
+      /* Add all of the hooks for vertex processing */
+      memset (&snippet_data, 0, sizeof (snippet_data));
+      snippet_data.snippets = get_vertex_snippets (pipeline);
+      snippet_data.hook = COGL_PIPELINE_SNIPPET_HOOK_VERTEX;
+      snippet_data.chain_function = "cogl_generated_source";
+      snippet_data.final_name = "main";
+      snippet_data.function_prefix = "cogl_vertex_hook";
+      snippet_data.source_buf = shader_state->source;
+      _cogl_pipeline_snippet_generate_code (&snippet_data);
 
       GE_RET( shader, ctx, glCreateShader (GL_VERTEX_SHADER) );
 



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