i965/vs: Set uses_vertexid and friends from brw_compile_vs
[mesa.git] / src / intel / compiler / brw_vec4_vs_visitor.cpp
1 /*
2 * Copyright © 2013 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 #include "brw_vec4_vs.h"
26 #include "common/gen_debug.h"
27
28 namespace brw {
29
30 void
31 vec4_vs_visitor::emit_prolog()
32 {
33 }
34
35
36 dst_reg *
37 vec4_vs_visitor::make_reg_for_system_value(int location)
38 {
39 /* VertexID is stored by the VF as the last vertex element, but
40 * we don't represent it with a flag in inputs_read, so we call
41 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
42 */
43 dst_reg *reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX);
44
45 switch (location) {
46 case SYSTEM_VALUE_BASE_VERTEX:
47 reg->writemask = WRITEMASK_X;
48 break;
49 case SYSTEM_VALUE_BASE_INSTANCE:
50 reg->writemask = WRITEMASK_Y;
51 break;
52 case SYSTEM_VALUE_VERTEX_ID:
53 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
54 reg->writemask = WRITEMASK_Z;
55 break;
56 case SYSTEM_VALUE_INSTANCE_ID:
57 reg->writemask = WRITEMASK_W;
58 break;
59 case SYSTEM_VALUE_DRAW_ID:
60 reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX + 1);
61 reg->writemask = WRITEMASK_X;
62 break;
63 default:
64 unreachable("not reached");
65 }
66
67 return reg;
68 }
69
70
71 void
72 vec4_vs_visitor::emit_urb_write_header(int mrf)
73 {
74 /* No need to do anything for VS; an implied write to this MRF will be
75 * performed by VS_OPCODE_URB_WRITE.
76 */
77 (void) mrf;
78 }
79
80
81 vec4_instruction *
82 vec4_vs_visitor::emit_urb_write_opcode(bool complete)
83 {
84 /* For VS, the URB writes end the thread. */
85 if (complete) {
86 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
87 emit_shader_time_end();
88 }
89
90 vec4_instruction *inst = emit(VS_OPCODE_URB_WRITE);
91 inst->urb_write_flags = complete ?
92 BRW_URB_WRITE_EOT_COMPLETE : BRW_URB_WRITE_NO_FLAGS;
93
94 return inst;
95 }
96
97
98 void
99 vec4_vs_visitor::emit_urb_slot(dst_reg reg, int varying)
100 {
101 reg.type = BRW_REGISTER_TYPE_F;
102 output_reg[varying][0].type = reg.type;
103
104 switch (varying) {
105 case VARYING_SLOT_COL0:
106 case VARYING_SLOT_COL1:
107 case VARYING_SLOT_BFC0:
108 case VARYING_SLOT_BFC1: {
109 /* These built-in varyings are only supported in compatibility mode,
110 * and we only support GS in core profile. So, this must be a vertex
111 * shader.
112 */
113 vec4_instruction *inst = emit_generic_urb_slot(reg, varying, 0);
114 if (inst && key->clamp_vertex_color)
115 inst->saturate = true;
116 break;
117 }
118 default:
119 return vec4_visitor::emit_urb_slot(reg, varying);
120 }
121 }
122
123
124 void
125 vec4_vs_visitor::emit_clip_distances(dst_reg reg, int offset)
126 {
127 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
128 *
129 * "If a linked set of shaders forming the vertex stage contains no
130 * static write to gl_ClipVertex or gl_ClipDistance, but the
131 * application has requested clipping against user clip planes through
132 * the API, then the coordinate written to gl_Position is used for
133 * comparison against the user clip planes."
134 *
135 * This function is only called if the shader didn't write to
136 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
137 * if the user wrote to it; otherwise we use gl_Position.
138 */
139 gl_varying_slot clip_vertex = VARYING_SLOT_CLIP_VERTEX;
140 if (!(prog_data->vue_map.slots_valid & VARYING_BIT_CLIP_VERTEX)) {
141 clip_vertex = VARYING_SLOT_POS;
142 }
143
144 for (int i = 0; i + offset < key->nr_userclip_plane_consts && i < 4;
145 ++i) {
146 reg.writemask = 1 << i;
147 emit(DP4(reg,
148 src_reg(output_reg[clip_vertex][0]),
149 src_reg(this->userplane[i + offset])));
150 }
151 }
152
153
154 void
155 vec4_vs_visitor::setup_uniform_clipplane_values()
156 {
157 for (int i = 0; i < key->nr_userclip_plane_consts; ++i) {
158 this->userplane[i] = dst_reg(UNIFORM, this->uniforms);
159 this->userplane[i].type = BRW_REGISTER_TYPE_F;
160 for (int j = 0; j < 4; ++j) {
161 stage_prog_data->param[this->uniforms * 4 + j] =
162 (gl_constant_value *) &clip_planes[i][j];
163 }
164 ++this->uniforms;
165 }
166 }
167
168
169 void
170 vec4_vs_visitor::emit_thread_end()
171 {
172 setup_uniform_clipplane_values();
173
174 /* Lower legacy ff and ClipVertex clipping to clip distances */
175 if (key->nr_userclip_plane_consts > 0) {
176 current_annotation = "user clip distances";
177
178 output_reg[VARYING_SLOT_CLIP_DIST0][0] =
179 dst_reg(this, glsl_type::vec4_type);
180 output_reg[VARYING_SLOT_CLIP_DIST1][0] =
181 dst_reg(this, glsl_type::vec4_type);
182 output_num_components[VARYING_SLOT_CLIP_DIST0][0] = 4;
183 output_num_components[VARYING_SLOT_CLIP_DIST1][0] = 4;
184
185 emit_clip_distances(output_reg[VARYING_SLOT_CLIP_DIST0][0], 0);
186 emit_clip_distances(output_reg[VARYING_SLOT_CLIP_DIST1][0], 4);
187 }
188
189 /* For VS, we always end the thread by emitting a single vertex.
190 * emit_urb_write_opcode() will take care of setting the eot flag on the
191 * SEND instruction.
192 */
193 emit_vertex();
194 }
195
196
197 vec4_vs_visitor::vec4_vs_visitor(const struct brw_compiler *compiler,
198 void *log_data,
199 const struct brw_vs_prog_key *key,
200 struct brw_vs_prog_data *vs_prog_data,
201 const nir_shader *shader,
202 gl_clip_plane *clip_planes,
203 void *mem_ctx,
204 int shader_time_index,
205 bool use_legacy_snorm_formula)
206 : vec4_visitor(compiler, log_data, &key->tex, &vs_prog_data->base, shader,
207 mem_ctx, false /* no_spills */, shader_time_index),
208 key(key),
209 vs_prog_data(vs_prog_data),
210 clip_planes(clip_planes),
211 use_legacy_snorm_formula(use_legacy_snorm_formula)
212 {
213 }
214
215
216 } /* namespace brw */