6b41fee055a16c6582084cf3fea497b80dae9288
[mesa.git] / src / compiler / glsl / lower_vertex_id.cpp
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
42 namespace {
43
44 class lower_vertex_id_visitor : public ir_hierarchical_visitor {
45 public:
46 explicit lower_vertex_id_visitor(ir_function_signature *main_sig,
47 exec_list *ir_list)
48 : progress(false), VertexID(NULL), gl_VertexID(NULL),
49 gl_BaseVertex(NULL), main_sig(main_sig), ir_list(ir_list)
50 {
51 foreach_in_list(ir_instruction, ir, ir_list) {
52 ir_variable *const var = ir->as_variable();
53
54 if (var != NULL && var->data.mode == ir_var_system_value &&
55 var->data.location == SYSTEM_VALUE_BASE_VERTEX) {
56 gl_BaseVertex = var;
57 break;
58 }
59 }
60 }
61
62 virtual ir_visitor_status visit(ir_dereference_variable *);
63
64 bool progress;
65
66 private:
67 ir_variable *VertexID;
68 ir_variable *gl_VertexID;
69 ir_variable *gl_BaseVertex;
70
71 ir_function_signature *main_sig;
72 exec_list *ir_list;
73 };
74
75 } /* anonymous namespace */
76
77 ir_visitor_status
78 lower_vertex_id_visitor::visit(ir_dereference_variable *ir)
79 {
80 if (ir->var->data.mode != ir_var_system_value ||
81 ir->var->data.location != SYSTEM_VALUE_VERTEX_ID)
82 return visit_continue;
83
84 if (VertexID == NULL) {
85 const glsl_type *const int_t = glsl_type::int_type;
86 void *const mem_ctx = ralloc_parent(ir);
87
88 VertexID = new(mem_ctx) ir_variable(int_t, "__VertexID",
89 ir_var_temporary);
90 ir_list->push_head(VertexID);
91
92 gl_VertexID = new(mem_ctx) ir_variable(int_t, "gl_VertexIDMESA",
93 ir_var_system_value);
94 gl_VertexID->data.how_declared = ir_var_declared_implicitly;
95 gl_VertexID->data.read_only = true;
96 gl_VertexID->data.location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
97 gl_VertexID->data.explicit_location = true;
98 gl_VertexID->data.explicit_index = 0;
99 ir_list->push_head(gl_VertexID);
100
101 if (gl_BaseVertex == NULL) {
102 gl_BaseVertex = new(mem_ctx) ir_variable(int_t, "gl_BaseVertex",
103 ir_var_system_value);
104 gl_BaseVertex->data.how_declared = ir_var_hidden;
105 gl_BaseVertex->data.read_only = true;
106 gl_BaseVertex->data.location = SYSTEM_VALUE_BASE_VERTEX;
107 gl_BaseVertex->data.explicit_location = true;
108 gl_BaseVertex->data.explicit_index = 0;
109 ir_list->push_head(gl_BaseVertex);
110 }
111
112 ir_instruction *const inst =
113 ir_builder::assign(VertexID,
114 ir_builder::add(gl_VertexID, gl_BaseVertex));
115
116 main_sig->body.push_head(inst);
117 }
118
119 ir->var = VertexID;
120 progress = true;
121
122 return visit_continue;
123 }
124
125 bool
126 lower_vertex_id(gl_linked_shader *shader)
127 {
128 /* gl_VertexID only exists in the vertex shader.
129 */
130 if (shader->Stage != MESA_SHADER_VERTEX)
131 return false;
132
133 ir_function_signature *const main_sig =
134 _mesa_get_main_function_signature(shader->symbols);
135 if (main_sig == NULL) {
136 assert(main_sig != NULL);
137 return false;
138 }
139
140 lower_vertex_id_visitor v(main_sig, shader->ir);
141
142 v.run(shader->ir);
143
144 return v.progress;
145 }