mojoshader.c
branchtrunk
changeset 189 d31efd640c13
parent 187 1c709f65cf1b
child 190 2a2fb0f656cf
--- a/mojoshader.c	Thu Apr 24 15:56:01 2008 -0400
+++ b/mojoshader.c	Thu Apr 24 16:26:52 2008 -0400
@@ -302,6 +302,7 @@
     OutputList globals;
     OutputList helpers;
     OutputList subroutines;
+    OutputList mainline_intro;
     OutputList mainline;
     OutputList ignore;
     OutputList *output_stack[2];
@@ -685,6 +686,21 @@
     return (reglist_exists(&ctx->defined_registers, rtype, regnum) != NULL);
 } // get_defined_register
 
+static const RegisterList *declared_attribute(Context *ctx,
+                                              const MOJOSHADER_usage usage,
+                                              const int index)
+{
+    const RegisterList *item = ctx->attributes.next;
+    while (item != NULL)
+    {
+        if ((item->usage == usage) && (item->index == index))
+            return item;
+        item = item->next;
+    } // while
+
+    return NULL;
+} // declared_attribute
+
 static void add_attribute_register(Context *ctx, const RegisterType rtype,
                                 const int regnum, const MOJOSHADER_usage usage,
                                 const int index, const int writemask)
@@ -1920,9 +1936,10 @@
         return;
     } // if
 
-    ctx->output = &ctx->mainline;
+    ctx->output = &ctx->mainline_intro;
     output_line(ctx, "void main()");
     output_line(ctx, "{");
+    ctx->output = &ctx->mainline;
     ctx->indent++;
 } // emit_GLSL_start
 
@@ -1936,10 +1953,26 @@
 
 static void emit_GLSL_finalize(Context *ctx)
 {
+    const RegisterList *reg = NULL;
+
     // throw some blank lines around to make source more readable.
     push_output(ctx, &ctx->globals);
     output_blank_line(ctx);
     pop_output(ctx);
+
+    push_output(ctx, &ctx->mainline_intro);
+    ctx->indent++;
+
+    // Make sure this is always set, moved from our generic attribute.
+    reg = declared_attribute(ctx, MOJOSHADER_USAGE_POSITION, 0);
+    if (reg != NULL)
+    {
+        output_line(ctx, "gl_Position = %s;",
+                            get_GLSL_varname(ctx, reg->regtype, reg->regnum));
+    } // if
+
+    ctx->indent--;
+    pop_output(ctx);
 } // emit_GLSL_finalize
 
 static void emit_GLSL_global(Context *ctx, RegisterType regtype, int regnum)
@@ -4336,6 +4369,7 @@
     ctx->globals.tail = &ctx->globals.head;
     ctx->helpers.tail = &ctx->helpers.head;
     ctx->subroutines.tail = &ctx->subroutines.head;
+    ctx->mainline_intro.tail = &ctx->mainline_intro.head;
     ctx->mainline.tail = &ctx->mainline.head;
     ctx->ignore.tail = &ctx->ignore.head;
     ctx->output = &ctx->mainline;
@@ -4375,6 +4409,7 @@
         free_output_list(f, d, ctx->globals.head.next);
         free_output_list(f, d, ctx->helpers.head.next);
         free_output_list(f, d, ctx->subroutines.head.next);
+        free_output_list(f, d, ctx->mainline_intro.head.next);
         free_output_list(f, d, ctx->mainline.head.next);
         free_output_list(f, d, ctx->ignore.head.next);
         free_reglist(f, d, ctx->used_registers.next);
@@ -4426,6 +4461,7 @@
             append_list(&wptr, endl, endllen, ctx->globals.head.next);
             append_list(&wptr, endl, endllen, ctx->helpers.head.next);
             append_list(&wptr, endl, endllen, ctx->subroutines.head.next);
+            append_list(&wptr, endl, endllen, ctx->mainline_intro.head.next);
             append_list(&wptr, endl, endllen, ctx->mainline.head.next);
             // don't append ctx->ignore ... that's why it's called "ignore"
         } // else