1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2014 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/**
25b8e80941Smrg * \file lower_vertex_id.cpp
26b8e80941Smrg *
27b8e80941Smrg * There exists hardware, such as i965, that does not implement the OpenGL
28b8e80941Smrg * semantic for gl_VertexID.  Instead, that hardware does not include the
29b8e80941Smrg * value of basevertex in the gl_VertexID value.  To implement the OpenGL
30b8e80941Smrg * semantic, we'll have to convert gl_Vertex_ID to
31b8e80941Smrg * gl_VertexIDMESA+gl_BaseVertexMESA.
32b8e80941Smrg */
33b8e80941Smrg
34b8e80941Smrg#include "glsl_symbol_table.h"
35b8e80941Smrg#include "ir_hierarchical_visitor.h"
36b8e80941Smrg#include "ir.h"
37b8e80941Smrg#include "ir_builder.h"
38b8e80941Smrg#include "linker.h"
39b8e80941Smrg#include "program/prog_statevars.h"
40b8e80941Smrg#include "builtin_functions.h"
41b8e80941Smrg#include "main/mtypes.h"
42b8e80941Smrg
43b8e80941Smrgnamespace {
44b8e80941Smrg
45b8e80941Smrgclass lower_vertex_id_visitor : public ir_hierarchical_visitor {
46b8e80941Smrgpublic:
47b8e80941Smrg   explicit lower_vertex_id_visitor(ir_function_signature *main_sig,
48b8e80941Smrg                                    exec_list *ir_list)
49b8e80941Smrg      : progress(false), VertexID(NULL), gl_VertexID(NULL),
50b8e80941Smrg        gl_BaseVertex(NULL), main_sig(main_sig), ir_list(ir_list)
51b8e80941Smrg   {
52b8e80941Smrg      foreach_in_list(ir_instruction, ir, ir_list) {
53b8e80941Smrg         ir_variable *const var = ir->as_variable();
54b8e80941Smrg
55b8e80941Smrg         if (var != NULL && var->data.mode == ir_var_system_value &&
56b8e80941Smrg             var->data.location == SYSTEM_VALUE_BASE_VERTEX) {
57b8e80941Smrg            gl_BaseVertex = var;
58b8e80941Smrg            break;
59b8e80941Smrg         }
60b8e80941Smrg      }
61b8e80941Smrg   }
62b8e80941Smrg
63b8e80941Smrg   virtual ir_visitor_status visit(ir_dereference_variable *);
64b8e80941Smrg
65b8e80941Smrg   bool progress;
66b8e80941Smrg
67b8e80941Smrgprivate:
68b8e80941Smrg   ir_variable *VertexID;
69b8e80941Smrg   ir_variable *gl_VertexID;
70b8e80941Smrg   ir_variable *gl_BaseVertex;
71b8e80941Smrg
72b8e80941Smrg   ir_function_signature *main_sig;
73b8e80941Smrg   exec_list *ir_list;
74b8e80941Smrg};
75b8e80941Smrg
76b8e80941Smrg} /* anonymous namespace */
77b8e80941Smrg
78b8e80941Smrgir_visitor_status
79b8e80941Smrglower_vertex_id_visitor::visit(ir_dereference_variable *ir)
80b8e80941Smrg{
81b8e80941Smrg   if (ir->var->data.mode != ir_var_system_value ||
82b8e80941Smrg       ir->var->data.location != SYSTEM_VALUE_VERTEX_ID)
83b8e80941Smrg      return visit_continue;
84b8e80941Smrg
85b8e80941Smrg   if (VertexID == NULL) {
86b8e80941Smrg      const glsl_type *const int_t = glsl_type::int_type;
87b8e80941Smrg      void *const mem_ctx = ralloc_parent(ir);
88b8e80941Smrg
89b8e80941Smrg      VertexID = new(mem_ctx) ir_variable(int_t, "__VertexID",
90b8e80941Smrg                                          ir_var_temporary);
91b8e80941Smrg      ir_list->push_head(VertexID);
92b8e80941Smrg
93b8e80941Smrg      gl_VertexID = new(mem_ctx) ir_variable(int_t, "gl_VertexIDMESA",
94b8e80941Smrg                                             ir_var_system_value);
95b8e80941Smrg      gl_VertexID->data.how_declared = ir_var_declared_implicitly;
96b8e80941Smrg      gl_VertexID->data.read_only = true;
97b8e80941Smrg      gl_VertexID->data.location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
98b8e80941Smrg      gl_VertexID->data.explicit_location = true;
99b8e80941Smrg      gl_VertexID->data.explicit_index = 0;
100b8e80941Smrg      ir_list->push_head(gl_VertexID);
101b8e80941Smrg
102b8e80941Smrg      if (gl_BaseVertex == NULL) {
103b8e80941Smrg         gl_BaseVertex = new(mem_ctx) ir_variable(int_t, "gl_BaseVertex",
104b8e80941Smrg                                                  ir_var_system_value);
105b8e80941Smrg         gl_BaseVertex->data.how_declared = ir_var_hidden;
106b8e80941Smrg         gl_BaseVertex->data.read_only = true;
107b8e80941Smrg         gl_BaseVertex->data.location = SYSTEM_VALUE_BASE_VERTEX;
108b8e80941Smrg         gl_BaseVertex->data.explicit_location = true;
109b8e80941Smrg         gl_BaseVertex->data.explicit_index = 0;
110b8e80941Smrg         ir_list->push_head(gl_BaseVertex);
111b8e80941Smrg      }
112b8e80941Smrg
113b8e80941Smrg      ir_instruction *const inst =
114b8e80941Smrg         ir_builder::assign(VertexID,
115b8e80941Smrg                            ir_builder::add(gl_VertexID, gl_BaseVertex));
116b8e80941Smrg
117b8e80941Smrg      main_sig->body.push_head(inst);
118b8e80941Smrg   }
119b8e80941Smrg
120b8e80941Smrg   ir->var = VertexID;
121b8e80941Smrg   progress = true;
122b8e80941Smrg
123b8e80941Smrg   return visit_continue;
124b8e80941Smrg}
125b8e80941Smrg
126b8e80941Smrgbool
127b8e80941Smrglower_vertex_id(gl_linked_shader *shader)
128b8e80941Smrg{
129b8e80941Smrg   /* gl_VertexID only exists in the vertex shader.
130b8e80941Smrg    */
131b8e80941Smrg   if (shader->Stage != MESA_SHADER_VERTEX)
132b8e80941Smrg      return false;
133b8e80941Smrg
134b8e80941Smrg   ir_function_signature *const main_sig =
135b8e80941Smrg      _mesa_get_main_function_signature(shader->symbols);
136b8e80941Smrg   if (main_sig == NULL) {
137b8e80941Smrg      assert(main_sig != NULL);
138b8e80941Smrg      return false;
139b8e80941Smrg   }
140b8e80941Smrg
141b8e80941Smrg   lower_vertex_id_visitor v(main_sig, shader->ir);
142b8e80941Smrg
143b8e80941Smrg   v.run(shader->ir);
144b8e80941Smrg
145b8e80941Smrg   return v.progress;
146b8e80941Smrg}
147