OSDN Git Service

Merge remote-tracking branch 'jekstrand/wip/i965-uniforms' into vulkan
[android-x86/external-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    dst_reg sign_recovery_shift;
34    dst_reg normalize_factor;
35    dst_reg es3_normalize_factor;
36
37    for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
38       if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
39          uint8_t wa_flags = key->gl_attrib_wa_flags[i];
40          dst_reg reg(ATTR, i);
41          dst_reg reg_d = reg;
42          reg_d.type = BRW_REGISTER_TYPE_D;
43          dst_reg reg_ud = reg;
44          reg_ud.type = BRW_REGISTER_TYPE_UD;
45
46          /* Do GL_FIXED rescaling for GLES2.0.  Our GL_FIXED attributes
47           * come in as floating point conversions of the integer values.
48           */
49          if (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK) {
50             dst_reg dst = reg;
51             dst.type = brw_type_for_base_type(glsl_type::vec4_type);
52             dst.writemask = (1 << (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK)) - 1;
53             emit(MUL(dst, src_reg(dst), brw_imm_f(1.0f / 65536.0f)));
54          }
55
56          /* Do sign recovery for 2101010 formats if required. */
57          if (wa_flags & BRW_ATTRIB_WA_SIGN) {
58             if (sign_recovery_shift.file == BAD_FILE) {
59                /* shift constant: <22,22,22,30> */
60                sign_recovery_shift = dst_reg(this, glsl_type::uvec4_type);
61                emit(MOV(writemask(sign_recovery_shift, WRITEMASK_XYZ), brw_imm_ud(22u)));
62                emit(MOV(writemask(sign_recovery_shift, WRITEMASK_W), brw_imm_ud(30u)));
63             }
64
65             emit(SHL(reg_ud, src_reg(reg_ud), src_reg(sign_recovery_shift)));
66             emit(ASR(reg_d, src_reg(reg_d), src_reg(sign_recovery_shift)));
67          }
68
69          /* Apply BGRA swizzle if required. */
70          if (wa_flags & BRW_ATTRIB_WA_BGRA) {
71             src_reg temp = src_reg(reg);
72             temp.swizzle = BRW_SWIZZLE4(2,1,0,3);
73             emit(MOV(reg, temp));
74          }
75
76          if (wa_flags & BRW_ATTRIB_WA_NORMALIZE) {
77             /* ES 3.0 has different rules for converting signed normalized
78              * fixed-point numbers than desktop GL.
79              */
80             if ((wa_flags & BRW_ATTRIB_WA_SIGN) && !use_legacy_snorm_formula) {
81                /* According to equation 2.2 of the ES 3.0 specification,
82                 * signed normalization conversion is done by:
83                 *
84                 * f = c / (2^(b-1)-1)
85                 */
86                if (es3_normalize_factor.file == BAD_FILE) {
87                   /* mul constant: 1 / (2^(b-1) - 1) */
88                   es3_normalize_factor = dst_reg(this, glsl_type::vec4_type);
89                   emit(MOV(writemask(es3_normalize_factor, WRITEMASK_XYZ),
90                            brw_imm_f(1.0f / ((1<<9) - 1))));
91                   emit(MOV(writemask(es3_normalize_factor, WRITEMASK_W),
92                            brw_imm_f(1.0f / ((1<<1) - 1))));
93                }
94
95                dst_reg dst = reg;
96                dst.type = brw_type_for_base_type(glsl_type::vec4_type);
97                emit(MOV(dst, src_reg(reg_d)));
98                emit(MUL(dst, src_reg(dst), src_reg(es3_normalize_factor)));
99                emit_minmax(BRW_CONDITIONAL_GE, dst, src_reg(dst), brw_imm_f(-1.0f));
100             } else {
101                /* The following equations are from the OpenGL 3.2 specification:
102                 *
103                 * 2.1 unsigned normalization
104                 * f = c/(2^n-1)
105                 *
106                 * 2.2 signed normalization
107                 * f = (2c+1)/(2^n-1)
108                 *
109                 * Both of these share a common divisor, which is represented by
110                 * "normalize_factor" in the code below.
111                 */
112                if (normalize_factor.file == BAD_FILE) {
113                   /* 1 / (2^b - 1) for b=<10,10,10,2> */
114                   normalize_factor = dst_reg(this, glsl_type::vec4_type);
115                   emit(MOV(writemask(normalize_factor, WRITEMASK_XYZ),
116                            brw_imm_f(1.0f / ((1<<10) - 1))));
117                   emit(MOV(writemask(normalize_factor, WRITEMASK_W),
118                            brw_imm_f(1.0f / ((1<<2) - 1))));
119                }
120
121                dst_reg dst = reg;
122                dst.type = brw_type_for_base_type(glsl_type::vec4_type);
123                emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
124
125                /* For signed normalization, we want the numerator to be 2c+1. */
126                if (wa_flags & BRW_ATTRIB_WA_SIGN) {
127                   emit(MUL(dst, src_reg(dst), brw_imm_f(2.0f)));
128                   emit(ADD(dst, src_reg(dst), brw_imm_f(1.0f)));
129                }
130
131                emit(MUL(dst, src_reg(dst), src_reg(normalize_factor)));
132             }
133          }
134
135          if (wa_flags & BRW_ATTRIB_WA_SCALE) {
136             dst_reg dst = reg;
137             dst.type = brw_type_for_base_type(glsl_type::vec4_type);
138             emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
139          }
140       }
141    }
142 }
143
144
145 dst_reg *
146 vec4_vs_visitor::make_reg_for_system_value(int location,
147                                            const glsl_type *type)
148 {
149    /* VertexID is stored by the VF as the last vertex element, but
150     * we don't represent it with a flag in inputs_read, so we call
151     * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
152     */
153    dst_reg *reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX);
154
155    switch (location) {
156    case SYSTEM_VALUE_BASE_VERTEX:
157       reg->writemask = WRITEMASK_X;
158       vs_prog_data->uses_basevertex = true;
159       break;
160    case SYSTEM_VALUE_BASE_INSTANCE:
161       reg->writemask = WRITEMASK_Y;
162       vs_prog_data->uses_baseinstance = true;
163       break;
164    case SYSTEM_VALUE_VERTEX_ID:
165    case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
166       reg->writemask = WRITEMASK_Z;
167       vs_prog_data->uses_vertexid = true;
168       break;
169    case SYSTEM_VALUE_INSTANCE_ID:
170       reg->writemask = WRITEMASK_W;
171       vs_prog_data->uses_instanceid = true;
172       break;
173    case SYSTEM_VALUE_DRAW_ID:
174       reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX + 1);
175       reg->writemask = WRITEMASK_X;
176       vs_prog_data->uses_drawid = true;
177       break;
178    default:
179       unreachable("not reached");
180    }
181
182    return reg;
183 }
184
185
186 void
187 vec4_vs_visitor::emit_urb_write_header(int mrf)
188 {
189    /* No need to do anything for VS; an implied write to this MRF will be
190     * performed by VS_OPCODE_URB_WRITE.
191     */
192    (void) mrf;
193 }
194
195
196 vec4_instruction *
197 vec4_vs_visitor::emit_urb_write_opcode(bool complete)
198 {
199    /* For VS, the URB writes end the thread. */
200    if (complete) {
201       if (INTEL_DEBUG & DEBUG_SHADER_TIME)
202          emit_shader_time_end();
203    }
204
205    vec4_instruction *inst = emit(VS_OPCODE_URB_WRITE);
206    inst->urb_write_flags = complete ?
207       BRW_URB_WRITE_EOT_COMPLETE : BRW_URB_WRITE_NO_FLAGS;
208
209    return inst;
210 }
211
212
213 void
214 vec4_vs_visitor::emit_urb_slot(dst_reg reg, int varying)
215 {
216    reg.type = BRW_REGISTER_TYPE_F;
217    output_reg[varying].type = reg.type;
218
219    switch (varying) {
220    case VARYING_SLOT_COL0:
221    case VARYING_SLOT_COL1:
222    case VARYING_SLOT_BFC0:
223    case VARYING_SLOT_BFC1: {
224       /* These built-in varyings are only supported in compatibility mode,
225        * and we only support GS in core profile.  So, this must be a vertex
226        * shader.
227        */
228       vec4_instruction *inst = emit_generic_urb_slot(reg, varying);
229       if (inst && key->clamp_vertex_color)
230          inst->saturate = true;
231       break;
232    }
233    default:
234       return vec4_visitor::emit_urb_slot(reg, varying);
235    }
236 }
237
238
239 void
240 vec4_vs_visitor::emit_clip_distances(dst_reg reg, int offset)
241 {
242    /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
243     *
244     *     "If a linked set of shaders forming the vertex stage contains no
245     *     static write to gl_ClipVertex or gl_ClipDistance, but the
246     *     application has requested clipping against user clip planes through
247     *     the API, then the coordinate written to gl_Position is used for
248     *     comparison against the user clip planes."
249     *
250     * This function is only called if the shader didn't write to
251     * gl_ClipDistance.  Accordingly, we use gl_ClipVertex to perform clipping
252     * if the user wrote to it; otherwise we use gl_Position.
253     */
254    gl_varying_slot clip_vertex = VARYING_SLOT_CLIP_VERTEX;
255    if (!(prog_data->vue_map.slots_valid & VARYING_BIT_CLIP_VERTEX)) {
256       clip_vertex = VARYING_SLOT_POS;
257    }
258
259    for (int i = 0; i + offset < key->nr_userclip_plane_consts && i < 4;
260         ++i) {
261       reg.writemask = 1 << i;
262       emit(DP4(reg,
263                src_reg(output_reg[clip_vertex]),
264                src_reg(this->userplane[i + offset])));
265    }
266 }
267
268
269 void
270 vec4_vs_visitor::setup_uniform_clipplane_values()
271 {
272    for (int i = 0; i < key->nr_userclip_plane_consts; ++i) {
273       this->userplane[i] = dst_reg(UNIFORM, this->uniforms);
274       this->userplane[i].type = BRW_REGISTER_TYPE_F;
275       for (int j = 0; j < 4; ++j) {
276          stage_prog_data->param[this->uniforms * 4 + j] =
277             (gl_constant_value *) &clip_planes[i][j];
278       }
279       ++this->uniforms;
280    }
281 }
282
283
284 void
285 vec4_vs_visitor::emit_thread_end()
286 {
287    setup_uniform_clipplane_values();
288
289    /* Lower legacy ff and ClipVertex clipping to clip distances */
290    if (key->nr_userclip_plane_consts > 0) {
291       current_annotation = "user clip distances";
292
293       output_reg[VARYING_SLOT_CLIP_DIST0] = dst_reg(this, glsl_type::vec4_type);
294       output_reg[VARYING_SLOT_CLIP_DIST1] = dst_reg(this, glsl_type::vec4_type);
295
296       emit_clip_distances(output_reg[VARYING_SLOT_CLIP_DIST0], 0);
297       emit_clip_distances(output_reg[VARYING_SLOT_CLIP_DIST1], 4);
298    }
299
300    /* For VS, we always end the thread by emitting a single vertex.
301     * emit_urb_write_opcode() will take care of setting the eot flag on the
302     * SEND instruction.
303     */
304    emit_vertex();
305 }
306
307
308 vec4_vs_visitor::vec4_vs_visitor(const struct brw_compiler *compiler,
309                                  void *log_data,
310                                  const struct brw_vs_prog_key *key,
311                                  struct brw_vs_prog_data *vs_prog_data,
312                                  const nir_shader *shader,
313                                  gl_clip_plane *clip_planes,
314                                  void *mem_ctx,
315                                  int shader_time_index,
316                                  bool use_legacy_snorm_formula)
317    : vec4_visitor(compiler, log_data, &key->tex, &vs_prog_data->base, shader,
318                   mem_ctx, false /* no_spills */, shader_time_index),
319      key(key),
320      vs_prog_data(vs_prog_data),
321      clip_planes(clip_planes),
322      use_legacy_snorm_formula(use_legacy_snorm_formula)
323 {
324 }
325
326
327 } /* namespace brw */