Mesa (glsl-pp-rework-2): glsl/pp: Add support for user-defined macros.

Michał Król michal at kemper.freedesktop.org
Thu Dec 10 03:59:45 PST 2009


Module: Mesa
Branch: glsl-pp-rework-2
Commit: 22200bcafcc77ecdca0127ac72d68e75e2ad7aee
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=22200bcafcc77ecdca0127ac72d68e75e2ad7aee

Author: Michal Krol <michal at vmware.com>
Date:   Thu Dec 10 12:58:21 2009 +0100

glsl/pp: Add support for user-defined macros.

---

 src/glsl/pp/sl_pp_context.c |   25 +++++++++++++++++++++++++
 src/glsl/pp/sl_pp_context.h |   10 ++++++++++
 src/glsl/pp/sl_pp_macro.c   |   17 +++++++++++++++++
 src/glsl/pp/sl_pp_public.h  |    5 +++++
 4 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/glsl/pp/sl_pp_context.c b/src/glsl/pp/sl_pp_context.c
index 134588d..afc1b84 100644
--- a/src/glsl/pp/sl_pp_context.c
+++ b/src/glsl/pp/sl_pp_context.c
@@ -80,6 +80,31 @@ sl_pp_context_error_message(const struct sl_pp_context *context)
 }
 
 int
+sl_pp_context_add_predefined(struct sl_pp_context *context,
+                             const char *name,
+                             const char *value)
+{
+   struct sl_pp_predefined pre;
+
+   if (context->num_predefined == SL_PP_MAX_PREDEFINED) {
+      return -1;
+   }
+
+   pre.name = sl_pp_context_add_unique_str(context, name);
+   if (pre.name == -1) {
+      return -1;
+   }
+
+   pre.value = sl_pp_context_add_unique_str(context, value);
+   if (pre.value == -1) {
+      return -1;
+   }
+
+   context->predefined[context->num_predefined++] = pre;
+   return 0;
+}
+
+int
 sl_pp_context_add_unique_str(struct sl_pp_context *context,
                              const char *str)
 {
diff --git a/src/glsl/pp/sl_pp_context.h b/src/glsl/pp/sl_pp_context.h
index 5e3ae72..d95d29e 100644
--- a/src/glsl/pp/sl_pp_context.h
+++ b/src/glsl/pp/sl_pp_context.h
@@ -39,11 +39,18 @@
 
 #define SL_PP_MAX_EXTENSIONS  16
 
+#define SL_PP_MAX_PREDEFINED  16
+
 struct sl_pp_extension {
    int name;         /*< VENDOR_extension_name */
    int name_string;  /*< GL_VENDOR_extension_name */
 };
 
+struct sl_pp_predefined {
+   int name;
+   int value;
+};
+
 struct sl_pp_context {
    char *cstr_pool;
    unsigned int cstr_pool_max;
@@ -56,6 +63,9 @@ struct sl_pp_context {
    struct sl_pp_extension extensions[SL_PP_MAX_EXTENSIONS];
    unsigned int num_extensions;
 
+   struct sl_pp_predefined predefined[SL_PP_MAX_PREDEFINED];
+   unsigned int num_predefined;
+
    unsigned int if_stack[SL_PP_MAX_IF_NESTING];
    unsigned int if_ptr;
    unsigned int if_value;
diff --git a/src/glsl/pp/sl_pp_macro.c b/src/glsl/pp/sl_pp_macro.c
index 05466c9..08b44c7 100644
--- a/src/glsl/pp/sl_pp_macro.c
+++ b/src/glsl/pp/sl_pp_macro.c
@@ -163,6 +163,23 @@ sl_pp_macro_expand(struct sl_pp_context *context,
       return 0;
    }
 
+   for (j = 0; j < context->num_predefined; j++) {
+      if (macro_name == context->predefined[j].name) {
+         if (!mute) {
+            struct sl_pp_token_info ti;
+
+            ti.token = SL_PP_UINT;
+            ti.data._uint = context->predefined[j].value;
+            if (sl_pp_process_out(state, &ti)) {
+               strcpy(context->error_msg, "out of memory");
+               return -1;
+            }
+         }
+         (*pi)++;
+         return 0;
+      }
+   }
+
    /* Replace extension names with 1.
     */
    for (j = 0; j < context->num_extensions; j++) {
diff --git a/src/glsl/pp/sl_pp_public.h b/src/glsl/pp/sl_pp_public.h
index 20f2089..0769036 100644
--- a/src/glsl/pp/sl_pp_public.h
+++ b/src/glsl/pp/sl_pp_public.h
@@ -51,6 +51,11 @@ sl_pp_context_add_extension(struct sl_pp_context *context,
                             const char *name_string);
 
 int
+sl_pp_context_add_predefined(struct sl_pp_context *context,
+                             const char *name,
+                             const char *value);
+
+int
 sl_pp_context_add_unique_str(struct sl_pp_context *context,
                              const char *str);
 



More information about the mesa-commit mailing list