101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file lower_vertex_id.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * There exists hardware, such as i965, that does not implement the OpenGL
2801e04c3fSmrg * semantic for gl_VertexID.  Instead, that hardware does not include the
2901e04c3fSmrg * value of basevertex in the gl_VertexID value.  To implement the OpenGL
3001e04c3fSmrg * semantic, we'll have to convert gl_Vertex_ID to
3101e04c3fSmrg * gl_VertexIDMESA+gl_BaseVertexMESA.
3201e04c3fSmrg */
3301e04c3fSmrg
3401e04c3fSmrg#include "glsl_symbol_table.h"
3501e04c3fSmrg#include "ir_hierarchical_visitor.h"
3601e04c3fSmrg#include "ir.h"
3701e04c3fSmrg#include "ir_builder.h"
3801e04c3fSmrg#include "linker.h"
3901e04c3fSmrg#include "program/prog_statevars.h"
4001e04c3fSmrg#include "builtin_functions.h"
4101e04c3fSmrg#include "main/mtypes.h"
4201e04c3fSmrg
4301e04c3fSmrgnamespace {
4401e04c3fSmrg
4501e04c3fSmrgclass lower_vertex_id_visitor : public ir_hierarchical_visitor {
4601e04c3fSmrgpublic:
4701e04c3fSmrg   explicit lower_vertex_id_visitor(ir_function_signature *main_sig,
4801e04c3fSmrg                                    exec_list *ir_list)
4901e04c3fSmrg      : progress(false), VertexID(NULL), gl_VertexID(NULL),
5001e04c3fSmrg        gl_BaseVertex(NULL), main_sig(main_sig), ir_list(ir_list)
5101e04c3fSmrg   {
5201e04c3fSmrg      foreach_in_list(ir_instruction, ir, ir_list) {
5301e04c3fSmrg         ir_variable *const var = ir->as_variable();
5401e04c3fSmrg
5501e04c3fSmrg         if (var != NULL && var->data.mode == ir_var_system_value &&
5601e04c3fSmrg             var->data.location == SYSTEM_VALUE_BASE_VERTEX) {
5701e04c3fSmrg            gl_BaseVertex = var;
5801e04c3fSmrg            break;
5901e04c3fSmrg         }
6001e04c3fSmrg      }
6101e04c3fSmrg   }
6201e04c3fSmrg
6301e04c3fSmrg   virtual ir_visitor_status visit(ir_dereference_variable *);
6401e04c3fSmrg
6501e04c3fSmrg   bool progress;
6601e04c3fSmrg
6701e04c3fSmrgprivate:
6801e04c3fSmrg   ir_variable *VertexID;
6901e04c3fSmrg   ir_variable *gl_VertexID;
7001e04c3fSmrg   ir_variable *gl_BaseVertex;
7101e04c3fSmrg
7201e04c3fSmrg   ir_function_signature *main_sig;
7301e04c3fSmrg   exec_list *ir_list;
7401e04c3fSmrg};
7501e04c3fSmrg
7601e04c3fSmrg} /* anonymous namespace */
7701e04c3fSmrg
7801e04c3fSmrgir_visitor_status
7901e04c3fSmrglower_vertex_id_visitor::visit(ir_dereference_variable *ir)
8001e04c3fSmrg{
8101e04c3fSmrg   if (ir->var->data.mode != ir_var_system_value ||
8201e04c3fSmrg       ir->var->data.location != SYSTEM_VALUE_VERTEX_ID)
8301e04c3fSmrg      return visit_continue;
8401e04c3fSmrg
8501e04c3fSmrg   if (VertexID == NULL) {
8601e04c3fSmrg      const glsl_type *const int_t = glsl_type::int_type;
8701e04c3fSmrg      void *const mem_ctx = ralloc_parent(ir);
8801e04c3fSmrg
8901e04c3fSmrg      VertexID = new(mem_ctx) ir_variable(int_t, "__VertexID",
9001e04c3fSmrg                                          ir_var_temporary);
9101e04c3fSmrg      ir_list->push_head(VertexID);
9201e04c3fSmrg
9301e04c3fSmrg      gl_VertexID = new(mem_ctx) ir_variable(int_t, "gl_VertexIDMESA",
9401e04c3fSmrg                                             ir_var_system_value);
9501e04c3fSmrg      gl_VertexID->data.how_declared = ir_var_declared_implicitly;
9601e04c3fSmrg      gl_VertexID->data.read_only = true;
9701e04c3fSmrg      gl_VertexID->data.location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
9801e04c3fSmrg      gl_VertexID->data.explicit_location = true;
9901e04c3fSmrg      gl_VertexID->data.explicit_index = 0;
10001e04c3fSmrg      ir_list->push_head(gl_VertexID);
10101e04c3fSmrg
10201e04c3fSmrg      if (gl_BaseVertex == NULL) {
10301e04c3fSmrg         gl_BaseVertex = new(mem_ctx) ir_variable(int_t, "gl_BaseVertex",
10401e04c3fSmrg                                                  ir_var_system_value);
10501e04c3fSmrg         gl_BaseVertex->data.how_declared = ir_var_hidden;
10601e04c3fSmrg         gl_BaseVertex->data.read_only = true;
10701e04c3fSmrg         gl_BaseVertex->data.location = SYSTEM_VALUE_BASE_VERTEX;
10801e04c3fSmrg         gl_BaseVertex->data.explicit_location = true;
10901e04c3fSmrg         gl_BaseVertex->data.explicit_index = 0;
11001e04c3fSmrg         ir_list->push_head(gl_BaseVertex);
11101e04c3fSmrg      }
11201e04c3fSmrg
11301e04c3fSmrg      ir_instruction *const inst =
11401e04c3fSmrg         ir_builder::assign(VertexID,
11501e04c3fSmrg                            ir_builder::add(gl_VertexID, gl_BaseVertex));
11601e04c3fSmrg
11701e04c3fSmrg      main_sig->body.push_head(inst);
11801e04c3fSmrg   }
11901e04c3fSmrg
12001e04c3fSmrg   ir->var = VertexID;
12101e04c3fSmrg   progress = true;
12201e04c3fSmrg
12301e04c3fSmrg   return visit_continue;
12401e04c3fSmrg}
12501e04c3fSmrg
12601e04c3fSmrgbool
12701e04c3fSmrglower_vertex_id(gl_linked_shader *shader)
12801e04c3fSmrg{
12901e04c3fSmrg   /* gl_VertexID only exists in the vertex shader.
13001e04c3fSmrg    */
13101e04c3fSmrg   if (shader->Stage != MESA_SHADER_VERTEX)
13201e04c3fSmrg      return false;
13301e04c3fSmrg
13401e04c3fSmrg   ir_function_signature *const main_sig =
13501e04c3fSmrg      _mesa_get_main_function_signature(shader->symbols);
13601e04c3fSmrg   if (main_sig == NULL) {
13701e04c3fSmrg      assert(main_sig != NULL);
13801e04c3fSmrg      return false;
13901e04c3fSmrg   }
14001e04c3fSmrg
14101e04c3fSmrg   lower_vertex_id_visitor v(main_sig, shader->ir);
14201e04c3fSmrg
14301e04c3fSmrg   v.run(shader->ir);
14401e04c3fSmrg
14501e04c3fSmrg   return v.progress;
14601e04c3fSmrg}
147