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