2
* Copyright © 2014 Intel Corporation
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:
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
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.
25
* \file lower_vertex_id.cpp
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.
34
#include "glsl_symbol_table.h"
35
#include "ir_hierarchical_visitor.h"
37
#include "ir_builder.h"
39
#include "program/prog_statevars.h"
40
#include "builtin_functions.h"
41
#include "main/shader_types.h"
45
class lower_vertex_id_visitor : public ir_hierarchical_visitor {
47
explicit lower_vertex_id_visitor(ir_function_signature *main_sig,
49
: progress(false), VertexID(NULL), gl_VertexID(NULL),
50
gl_BaseVertex(NULL), main_sig(main_sig), ir_list(ir_list)
52
foreach_in_list(ir_instruction, ir, ir_list) {
53
ir_variable *const var = ir->as_variable();
55
if (var != NULL && var->data.mode == ir_var_system_value &&
56
var->data.location == SYSTEM_VALUE_BASE_VERTEX) {
63
virtual ir_visitor_status visit(ir_dereference_variable *);
68
ir_variable *VertexID;
69
ir_variable *gl_VertexID;
70
ir_variable *gl_BaseVertex;
72
ir_function_signature *main_sig;
76
} /* anonymous namespace */
79
lower_vertex_id_visitor::visit(ir_dereference_variable *ir)
81
if (ir->var->data.mode != ir_var_system_value ||
82
ir->var->data.location != SYSTEM_VALUE_VERTEX_ID)
83
return visit_continue;
85
if (VertexID == NULL) {
86
const glsl_type *const int_t = glsl_type::int_type;
87
void *const mem_ctx = ralloc_parent(ir);
89
VertexID = new(mem_ctx) ir_variable(int_t, "__VertexID",
91
ir_list->push_head(VertexID);
93
gl_VertexID = new(mem_ctx) ir_variable(int_t, "gl_VertexIDMESA",
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);
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);
113
ir_instruction *const inst =
114
ir_builder::assign(VertexID,
115
ir_builder::add(gl_VertexID, gl_BaseVertex));
117
main_sig->body.push_head(inst);
123
return visit_continue;
127
lower_vertex_id(gl_linked_shader *shader)
129
/* gl_VertexID only exists in the vertex shader.
131
if (shader->Stage != MESA_SHADER_VERTEX)
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);
141
lower_vertex_id_visitor v(main_sig, shader->ir);