Add a virtual clone() method to ir_instruction.
[mesa.git] / ir_variable.cpp
index 0edb19e28fbedb78b148574dbb2a5ea509f8f991..efebe9199fad9cddb8aaf7decfd4195af384f4ed 100644 (file)
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "ir.h"
 #include "glsl_parser_extras.h"
 #include "glsl_symbol_table.h"
-#include "ir.h"
 #include "builtin_variables.h"
 
 #ifndef Elements
 #define Elements(x) (sizeof(x)/sizeof(*(x)))
 #endif
 
-static void
-add_variable(const char *name, enum ir_variable_mode mode,
+static ir_variable *
+add_variable(const char *name, enum ir_variable_mode mode, int slot,
             const glsl_type *type, exec_list *instructions,
                     glsl_symbol_table *symtab)
 {
    ir_variable *var = new ir_variable(type, name);
 
    var->mode = mode;
-   if (var->mode != ir_var_out)
+   switch (var->mode) {
+   case ir_var_in:
+      var->shader_in = true;
+      var->read_only = true;
+      break;
+   case ir_var_inout:
+      var->shader_in = true;
+      var->shader_out = true;
+      break;
+   case ir_var_out:
+      var->shader_out = true;
+      break;
+   case ir_var_uniform:
+      var->shader_in = true;
       var->read_only = true;
+      break;
+   default:
+      assert(0);
+      break;
+   }
 
+   var->location = slot;
 
    /* Once the variable is created an initialized, add it to the symbol table
     * and add the declaration to the IR stream.
@@ -48,6 +67,7 @@ add_variable(const char *name, enum ir_variable_mode mode,
    instructions->push_tail(var);
 
    symtab->add_variable(var->name, var);
+   return var;
 }
 
 
@@ -62,7 +82,8 @@ add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
 
    assert(type != NULL);
 
-   add_variable(proto->name, proto->mode, type, instructions, symtab);
+   add_variable(proto->name, proto->mode, proto->slot, type, instructions,
+               symtab);
 }
 
 
@@ -77,10 +98,16 @@ generate_110_uniforms(exec_list *instructions,
                           instructions, symtab);
    }
 
-   /* FINISHME: Add support for gl_TextureMatrix[].  The size of this array is
-    * FINISHME: implementation dependent based on the value of
-    * FINISHME: GL_MAX_TEXTURE_COORDS.
+   /* FINISHME: The size of this array is implementation dependent based on the
+    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
+    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
+    * FINISHME: for now.
     */
+   const glsl_type *const mat4_array_type =
+      glsl_type::get_array_instance(glsl_type::mat4_type, 4);
+
+   add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
+               instructions, symtab);
 
    /* FINISHME: Add support for gl_DepthRangeParameters */
    /* FINISHME: Add support for gl_ClipPlane[] */
@@ -90,7 +117,16 @@ generate_110_uniforms(exec_list *instructions,
     * FINISHME: (glFrontMaterial, glBackMaterial)
     */
 
-   /* FINISHME: Add support for gl_LightSource[] */
+   /* FINISHME: The size of this array is implementation dependent based on the
+    * FINISHME: value of GL_MAX_TEXTURE_LIGHTS.  GL_MAX_TEXTURE_LIGHTS must be
+    * FINISHME: at least 8, so hard-code 8 for now.
+    */
+   const glsl_type *const light_source_array_type =
+      glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8);
+
+   add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
+               instructions, symtab);
+
    /* FINISHME: Add support for gl_LightModel */
    /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
    /* FINISHME: Add support for gl_TextureEnvColor[] */
@@ -116,16 +152,15 @@ generate_110_vs_variables(exec_list *instructions,
    generate_110_uniforms(instructions, symtab);
 
    /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  GL_MAX_TEXTURE_COORDS must be
-    * FINISHME: at least 2, so hard-code 2 for now.
+    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
+    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
+    * FINISHME: for now.
     */
-   const glsl_type *const vec4_type =
-      glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
    const glsl_type *const vec4_array_type =
-      glsl_type::get_array_instance(vec4_type, 2);
+      glsl_type::get_array_instance(glsl_type::vec4_type, 4);
 
-   add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
-               symtab);
+   add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
+               instructions, symtab);
 }
 
 
@@ -151,10 +186,16 @@ generate_130_vs_variables(exec_list *instructions,
                           instructions, symtab);
    }
 
-   /* FINISHME: Add support fo gl_ClipDistance.  The size of this array is
-    * FINISHME: implementation dependent based on the value of
-    * FINISHME: GL_MAX_CLIP_DISTANCES.
+   /* FINISHME: The size of this array is implementation dependent based on
+    * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
     */
+   const glsl_type *const clip_distance_array_type =
+      glsl_type::get_array_instance(glsl_type::float_type, 8);
+
+   /* FINISHME: gl_ClipDistance needs a real location assigned. */
+   add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
+               instructions, symtab);
+
 }
 
 
@@ -193,29 +234,45 @@ generate_110_fs_variables(exec_list *instructions,
    }
    generate_110_uniforms(instructions, symtab);
 
-   /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
+   /* FINISHME: The size of this array is implementation dependent based on the
+    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
+    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
+    * FINISHME: for now.
+    */
+   const glsl_type *const vec4_array_type =
+      glsl_type::get_array_instance(glsl_type::vec4_type, 4);
+
+   add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
+               instructions, symtab);
+}
 
+
+static void
+generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
+                                      glsl_symbol_table *symtab, bool warn)
+{
    /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  GL_MAX_TEXTURE_COORDS must be
-    * FINISHME: at least 2, so hard-code 2 for now.
+    * FINISHME: value of GL_MAX_DRAW_BUFFERS.  GL_MAX_DRAW_BUFFERS must be
+    * FINISHME: at least 1, so hard-code 1 for now.
     */
-   const glsl_type *const vec4_type =
-      glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
    const glsl_type *const vec4_array_type =
-      glsl_type::get_array_instance(vec4_type, 2);
+      glsl_type::get_array_instance(glsl_type::vec4_type, 1);
 
-   add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
-               symtab);
+   ir_variable *const fd =
+      add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
+                  vec4_array_type, instructions, symtab);
+
+   if (warn)
+      fd->warn_extension = "GL_ARB_draw_buffers";
 }
 
+
 static void
 generate_120_fs_variables(exec_list *instructions,
                          glsl_symbol_table *symtab)
 {
-   /* GLSL version 1.20 did not add any built-in variables in the fragment
-    * shader.
-    */
    generate_110_fs_variables(instructions, symtab);
+   generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
 }
 
 static void
@@ -224,10 +281,15 @@ generate_130_fs_variables(exec_list *instructions,
 {
    generate_120_fs_variables(instructions, symtab);
 
-   /* FINISHME: Add support fo gl_ClipDistance.  The size of this array is
-    * FINISHME: implementation dependent based on the value of
-    * FINISHME: GL_MAX_CLIP_DISTANCES.
+   /* FINISHME: The size of this array is implementation dependent based on
+    * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
     */
+   const glsl_type *const clip_distance_array_type =
+      glsl_type::get_array_instance(glsl_type::float_type, 8);
+
+   /* FINISHME: gl_ClipDistance needs a real location assigned. */
+   add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
+               instructions, symtab);
 }
 
 static void
@@ -246,6 +308,17 @@ initialize_fs_variables(exec_list *instructions,
       generate_130_fs_variables(instructions, state->symbols);
       break;
    }
+
+
+   /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
+    * can basically ignore any extension settings for it.
+    */
+   if (state->language_version < 120) {
+      if (state->ARB_draw_buffers_enable) {
+        generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
+                                               state->ARB_draw_buffers_warn);
+      }
+   }
 }
 
 void
@@ -261,5 +334,9 @@ _mesa_glsl_initialize_variables(exec_list *instructions,
    case fragment_shader:
       initialize_fs_variables(instructions, state);
       break;
+   case ir_shader:
+      fprintf(stderr, "ir reader has no builtin variables");
+      exit(1);
+      break;
    }
 }