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