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