glsl: Add GLSL_TYPE_INTERFACE
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_visitor.cpp
1 /*
2 * Copyright © 2011 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_vec4.h"
25 #include "glsl/ir_uniform.h"
26 extern "C" {
27 #include "main/context.h"
28 #include "main/macros.h"
29 #include "program/prog_parameter.h"
30 #include "program/sampler.h"
31 }
32
33 namespace brw {
34
35 vec4_instruction::vec4_instruction(vec4_visitor *v,
36 enum opcode opcode, dst_reg dst,
37 src_reg src0, src_reg src1, src_reg src2)
38 {
39 this->opcode = opcode;
40 this->dst = dst;
41 this->src[0] = src0;
42 this->src[1] = src1;
43 this->src[2] = src2;
44 this->ir = v->base_ir;
45 this->annotation = v->current_annotation;
46 }
47
48 vec4_instruction *
49 vec4_visitor::emit(vec4_instruction *inst)
50 {
51 this->instructions.push_tail(inst);
52
53 return inst;
54 }
55
56 vec4_instruction *
57 vec4_visitor::emit_before(vec4_instruction *inst, vec4_instruction *new_inst)
58 {
59 new_inst->ir = inst->ir;
60 new_inst->annotation = inst->annotation;
61
62 inst->insert_before(new_inst);
63
64 return inst;
65 }
66
67 vec4_instruction *
68 vec4_visitor::emit(enum opcode opcode, dst_reg dst,
69 src_reg src0, src_reg src1, src_reg src2)
70 {
71 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst,
72 src0, src1, src2));
73 }
74
75
76 vec4_instruction *
77 vec4_visitor::emit(enum opcode opcode, dst_reg dst, src_reg src0, src_reg src1)
78 {
79 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst, src0, src1));
80 }
81
82 vec4_instruction *
83 vec4_visitor::emit(enum opcode opcode, dst_reg dst, src_reg src0)
84 {
85 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst, src0));
86 }
87
88 vec4_instruction *
89 vec4_visitor::emit(enum opcode opcode)
90 {
91 return emit(new(mem_ctx) vec4_instruction(this, opcode, dst_reg()));
92 }
93
94 #define ALU1(op) \
95 vec4_instruction * \
96 vec4_visitor::op(dst_reg dst, src_reg src0) \
97 { \
98 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
99 src0); \
100 }
101
102 #define ALU2(op) \
103 vec4_instruction * \
104 vec4_visitor::op(dst_reg dst, src_reg src0, src_reg src1) \
105 { \
106 return new(mem_ctx) vec4_instruction(this, BRW_OPCODE_##op, dst, \
107 src0, src1); \
108 }
109
110 ALU1(NOT)
111 ALU1(MOV)
112 ALU1(FRC)
113 ALU1(RNDD)
114 ALU1(RNDE)
115 ALU1(RNDZ)
116 ALU1(F32TO16)
117 ALU1(F16TO32)
118 ALU2(ADD)
119 ALU2(MUL)
120 ALU2(MACH)
121 ALU2(AND)
122 ALU2(OR)
123 ALU2(XOR)
124 ALU2(DP3)
125 ALU2(DP4)
126 ALU2(DPH)
127 ALU2(SHL)
128 ALU2(SHR)
129 ALU2(ASR)
130
131 /** Gen4 predicated IF. */
132 vec4_instruction *
133 vec4_visitor::IF(uint32_t predicate)
134 {
135 vec4_instruction *inst;
136
137 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_IF);
138 inst->predicate = predicate;
139
140 return inst;
141 }
142
143 /** Gen6+ IF with embedded comparison. */
144 vec4_instruction *
145 vec4_visitor::IF(src_reg src0, src_reg src1, uint32_t condition)
146 {
147 assert(intel->gen >= 6);
148
149 vec4_instruction *inst;
150
151 resolve_ud_negate(&src0);
152 resolve_ud_negate(&src1);
153
154 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_IF, dst_null_d(),
155 src0, src1);
156 inst->conditional_mod = condition;
157
158 return inst;
159 }
160
161 /**
162 * CMP: Sets the low bit of the destination channels with the result
163 * of the comparison, while the upper bits are undefined, and updates
164 * the flag register with the packed 16 bits of the result.
165 */
166 vec4_instruction *
167 vec4_visitor::CMP(dst_reg dst, src_reg src0, src_reg src1, uint32_t condition)
168 {
169 vec4_instruction *inst;
170
171 /* original gen4 does type conversion to the destination type
172 * before before comparison, producing garbage results for floating
173 * point comparisons.
174 */
175 if (intel->gen == 4) {
176 dst.type = src0.type;
177 if (dst.file == HW_REG)
178 dst.fixed_hw_reg.type = dst.type;
179 }
180
181 resolve_ud_negate(&src0);
182 resolve_ud_negate(&src1);
183
184 inst = new(mem_ctx) vec4_instruction(this, BRW_OPCODE_CMP, dst, src0, src1);
185 inst->conditional_mod = condition;
186
187 return inst;
188 }
189
190 vec4_instruction *
191 vec4_visitor::SCRATCH_READ(dst_reg dst, src_reg index)
192 {
193 vec4_instruction *inst;
194
195 inst = new(mem_ctx) vec4_instruction(this, VS_OPCODE_SCRATCH_READ,
196 dst, index);
197 inst->base_mrf = 14;
198 inst->mlen = 2;
199
200 return inst;
201 }
202
203 vec4_instruction *
204 vec4_visitor::SCRATCH_WRITE(dst_reg dst, src_reg src, src_reg index)
205 {
206 vec4_instruction *inst;
207
208 inst = new(mem_ctx) vec4_instruction(this, VS_OPCODE_SCRATCH_WRITE,
209 dst, src, index);
210 inst->base_mrf = 13;
211 inst->mlen = 3;
212
213 return inst;
214 }
215
216 void
217 vec4_visitor::emit_dp(dst_reg dst, src_reg src0, src_reg src1, unsigned elements)
218 {
219 static enum opcode dot_opcodes[] = {
220 BRW_OPCODE_DP2, BRW_OPCODE_DP3, BRW_OPCODE_DP4
221 };
222
223 emit(dot_opcodes[elements - 2], dst, src0, src1);
224 }
225
226 src_reg
227 vec4_visitor::fix_math_operand(src_reg src)
228 {
229 /* The gen6 math instruction ignores the source modifiers --
230 * swizzle, abs, negate, and at least some parts of the register
231 * region description.
232 *
233 * Rather than trying to enumerate all these cases, *always* expand the
234 * operand to a temp GRF for gen6.
235 *
236 * For gen7, keep the operand as-is, except if immediate, which gen7 still
237 * can't use.
238 */
239
240 if (intel->gen == 7 && src.file != IMM)
241 return src;
242
243 dst_reg expanded = dst_reg(this, glsl_type::vec4_type);
244 expanded.type = src.type;
245 emit(MOV(expanded, src));
246 return src_reg(expanded);
247 }
248
249 void
250 vec4_visitor::emit_math1_gen6(enum opcode opcode, dst_reg dst, src_reg src)
251 {
252 src = fix_math_operand(src);
253
254 if (dst.writemask != WRITEMASK_XYZW) {
255 /* The gen6 math instruction must be align1, so we can't do
256 * writemasks.
257 */
258 dst_reg temp_dst = dst_reg(this, glsl_type::vec4_type);
259
260 emit(opcode, temp_dst, src);
261
262 emit(MOV(dst, src_reg(temp_dst)));
263 } else {
264 emit(opcode, dst, src);
265 }
266 }
267
268 void
269 vec4_visitor::emit_math1_gen4(enum opcode opcode, dst_reg dst, src_reg src)
270 {
271 vec4_instruction *inst = emit(opcode, dst, src);
272 inst->base_mrf = 1;
273 inst->mlen = 1;
274 }
275
276 void
277 vec4_visitor::emit_math(opcode opcode, dst_reg dst, src_reg src)
278 {
279 switch (opcode) {
280 case SHADER_OPCODE_RCP:
281 case SHADER_OPCODE_RSQ:
282 case SHADER_OPCODE_SQRT:
283 case SHADER_OPCODE_EXP2:
284 case SHADER_OPCODE_LOG2:
285 case SHADER_OPCODE_SIN:
286 case SHADER_OPCODE_COS:
287 break;
288 default:
289 assert(!"not reached: bad math opcode");
290 return;
291 }
292
293 if (intel->gen >= 6) {
294 return emit_math1_gen6(opcode, dst, src);
295 } else {
296 return emit_math1_gen4(opcode, dst, src);
297 }
298 }
299
300 void
301 vec4_visitor::emit_math2_gen6(enum opcode opcode,
302 dst_reg dst, src_reg src0, src_reg src1)
303 {
304 src0 = fix_math_operand(src0);
305 src1 = fix_math_operand(src1);
306
307 if (dst.writemask != WRITEMASK_XYZW) {
308 /* The gen6 math instruction must be align1, so we can't do
309 * writemasks.
310 */
311 dst_reg temp_dst = dst_reg(this, glsl_type::vec4_type);
312 temp_dst.type = dst.type;
313
314 emit(opcode, temp_dst, src0, src1);
315
316 emit(MOV(dst, src_reg(temp_dst)));
317 } else {
318 emit(opcode, dst, src0, src1);
319 }
320 }
321
322 void
323 vec4_visitor::emit_math2_gen4(enum opcode opcode,
324 dst_reg dst, src_reg src0, src_reg src1)
325 {
326 vec4_instruction *inst = emit(opcode, dst, src0, src1);
327 inst->base_mrf = 1;
328 inst->mlen = 2;
329 }
330
331 void
332 vec4_visitor::emit_math(enum opcode opcode,
333 dst_reg dst, src_reg src0, src_reg src1)
334 {
335 switch (opcode) {
336 case SHADER_OPCODE_POW:
337 case SHADER_OPCODE_INT_QUOTIENT:
338 case SHADER_OPCODE_INT_REMAINDER:
339 break;
340 default:
341 assert(!"not reached: unsupported binary math opcode");
342 return;
343 }
344
345 if (intel->gen >= 6) {
346 return emit_math2_gen6(opcode, dst, src0, src1);
347 } else {
348 return emit_math2_gen4(opcode, dst, src0, src1);
349 }
350 }
351
352 void
353 vec4_visitor::emit_pack_half_2x16(dst_reg dst, src_reg src0)
354 {
355 if (intel->gen < 7)
356 assert(!"ir_unop_pack_half_2x16 should be lowered");
357
358 assert(dst.type == BRW_REGISTER_TYPE_UD);
359 assert(src0.type == BRW_REGISTER_TYPE_F);
360
361 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
362 *
363 * Because this instruction does not have a 16-bit floating-point type,
364 * the destination data type must be Word (W).
365 *
366 * The destination must be DWord-aligned and specify a horizontal stride
367 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
368 * each destination channel and the upper word is not modified.
369 *
370 * The above restriction implies that the f32to16 instruction must use
371 * align1 mode, because only in align1 mode is it possible to specify
372 * horizontal stride. We choose here to defy the hardware docs and emit
373 * align16 instructions.
374 *
375 * (I [chadv] did attempt to emit align1 instructions for VS f32to16
376 * instructions. I was partially successful in that the code passed all
377 * tests. However, the code was dubiously correct and fragile, and the
378 * tests were not harsh enough to probe that frailty. Not trusting the
379 * code, I chose instead to remain in align16 mode in defiance of the hw
380 * docs).
381 *
382 * I've [chadv] experimentally confirmed that, on gen7 hardware and the
383 * simulator, emitting a f32to16 in align16 mode with UD as destination
384 * data type is safe. The behavior differs from that specified in the PRM
385 * in that the upper word of each destination channel is cleared to 0.
386 */
387
388 dst_reg tmp_dst(this, glsl_type::uvec2_type);
389 src_reg tmp_src(tmp_dst);
390
391 #if 0
392 /* Verify the undocumented behavior on which the following instructions
393 * rely. If f32to16 fails to clear the upper word of the X and Y channels,
394 * then the result of the bit-or instruction below will be incorrect.
395 *
396 * You should inspect the disasm output in order to verify that the MOV is
397 * not optimized away.
398 */
399 emit(MOV(tmp_dst, src_reg(0x12345678u)));
400 #endif
401
402 /* Give tmp the form below, where "." means untouched.
403 *
404 * w z y x w z y x
405 * |.|.|0x0000hhhh|0x0000llll|.|.|0x0000hhhh|0x0000llll|
406 *
407 * That the upper word of each write-channel be 0 is required for the
408 * following bit-shift and bit-or instructions to work. Note that this
409 * relies on the undocumented hardware behavior mentioned above.
410 */
411 tmp_dst.writemask = WRITEMASK_XY;
412 emit(F32TO16(tmp_dst, src0));
413
414 /* Give the write-channels of dst the form:
415 * 0xhhhh0000
416 */
417 tmp_src.swizzle = SWIZZLE_Y;
418 emit(SHL(dst, tmp_src, src_reg(16u)));
419
420 /* Finally, give the write-channels of dst the form of packHalf2x16's
421 * output:
422 * 0xhhhhllll
423 */
424 tmp_src.swizzle = SWIZZLE_X;
425 emit(OR(dst, src_reg(dst), tmp_src));
426 }
427
428 void
429 vec4_visitor::emit_unpack_half_2x16(dst_reg dst, src_reg src0)
430 {
431 if (intel->gen < 7)
432 assert(!"ir_unop_unpack_half_2x16 should be lowered");
433
434 assert(dst.type == BRW_REGISTER_TYPE_F);
435 assert(src0.type == BRW_REGISTER_TYPE_UD);
436
437 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
438 *
439 * Because this instruction does not have a 16-bit floating-point type,
440 * the source data type must be Word (W). The destination type must be
441 * F (Float).
442 *
443 * To use W as the source data type, we must adjust horizontal strides,
444 * which is only possible in align1 mode. All my [chadv] attempts at
445 * emitting align1 instructions for unpackHalf2x16 failed to pass the
446 * Piglit tests, so I gave up.
447 *
448 * I've verified that, on gen7 hardware and the simulator, it is safe to
449 * emit f16to32 in align16 mode with UD as source data type.
450 */
451
452 dst_reg tmp_dst(this, glsl_type::uvec2_type);
453 src_reg tmp_src(tmp_dst);
454
455 tmp_dst.writemask = WRITEMASK_X;
456 emit(AND(tmp_dst, src0, src_reg(0xffffu)));
457
458 tmp_dst.writemask = WRITEMASK_Y;
459 emit(SHR(tmp_dst, src0, src_reg(16u)));
460
461 dst.writemask = WRITEMASK_XY;
462 emit(F16TO32(dst, tmp_src));
463 }
464
465 void
466 vec4_visitor::visit_instructions(const exec_list *list)
467 {
468 foreach_list(node, list) {
469 ir_instruction *ir = (ir_instruction *)node;
470
471 base_ir = ir;
472 ir->accept(this);
473 }
474 }
475
476
477 static int
478 type_size(const struct glsl_type *type)
479 {
480 unsigned int i;
481 int size;
482
483 switch (type->base_type) {
484 case GLSL_TYPE_UINT:
485 case GLSL_TYPE_INT:
486 case GLSL_TYPE_FLOAT:
487 case GLSL_TYPE_BOOL:
488 if (type->is_matrix()) {
489 return type->matrix_columns;
490 } else {
491 /* Regardless of size of vector, it gets a vec4. This is bad
492 * packing for things like floats, but otherwise arrays become a
493 * mess. Hopefully a later pass over the code can pack scalars
494 * down if appropriate.
495 */
496 return 1;
497 }
498 case GLSL_TYPE_ARRAY:
499 assert(type->length > 0);
500 return type_size(type->fields.array) * type->length;
501 case GLSL_TYPE_STRUCT:
502 size = 0;
503 for (i = 0; i < type->length; i++) {
504 size += type_size(type->fields.structure[i].type);
505 }
506 return size;
507 case GLSL_TYPE_SAMPLER:
508 /* Samplers take up one slot in UNIFORMS[], but they're baked in
509 * at link time.
510 */
511 return 1;
512 case GLSL_TYPE_VOID:
513 case GLSL_TYPE_ERROR:
514 case GLSL_TYPE_INTERFACE:
515 assert(0);
516 break;
517 }
518
519 return 0;
520 }
521
522 int
523 vec4_visitor::virtual_grf_alloc(int size)
524 {
525 if (virtual_grf_array_size <= virtual_grf_count) {
526 if (virtual_grf_array_size == 0)
527 virtual_grf_array_size = 16;
528 else
529 virtual_grf_array_size *= 2;
530 virtual_grf_sizes = reralloc(mem_ctx, virtual_grf_sizes, int,
531 virtual_grf_array_size);
532 virtual_grf_reg_map = reralloc(mem_ctx, virtual_grf_reg_map, int,
533 virtual_grf_array_size);
534 }
535 virtual_grf_reg_map[virtual_grf_count] = virtual_grf_reg_count;
536 virtual_grf_reg_count += size;
537 virtual_grf_sizes[virtual_grf_count] = size;
538 return virtual_grf_count++;
539 }
540
541 src_reg::src_reg(class vec4_visitor *v, const struct glsl_type *type)
542 {
543 init();
544
545 this->file = GRF;
546 this->reg = v->virtual_grf_alloc(type_size(type));
547
548 if (type->is_array() || type->is_record()) {
549 this->swizzle = BRW_SWIZZLE_NOOP;
550 } else {
551 this->swizzle = swizzle_for_size(type->vector_elements);
552 }
553
554 this->type = brw_type_for_base_type(type);
555 }
556
557 dst_reg::dst_reg(class vec4_visitor *v, const struct glsl_type *type)
558 {
559 init();
560
561 this->file = GRF;
562 this->reg = v->virtual_grf_alloc(type_size(type));
563
564 if (type->is_array() || type->is_record()) {
565 this->writemask = WRITEMASK_XYZW;
566 } else {
567 this->writemask = (1 << type->vector_elements) - 1;
568 }
569
570 this->type = brw_type_for_base_type(type);
571 }
572
573 /* Our support for uniforms is piggy-backed on the struct
574 * gl_fragment_program, because that's where the values actually
575 * get stored, rather than in some global gl_shader_program uniform
576 * store.
577 */
578 void
579 vec4_visitor::setup_uniform_values(ir_variable *ir)
580 {
581 int namelen = strlen(ir->name);
582
583 /* The data for our (non-builtin) uniforms is stored in a series of
584 * gl_uniform_driver_storage structs for each subcomponent that
585 * glGetUniformLocation() could name. We know it's been set up in the same
586 * order we'd walk the type, so walk the list of storage and find anything
587 * with our name, or the prefix of a component that starts with our name.
588 */
589 for (unsigned u = 0; u < prog->NumUserUniformStorage; u++) {
590 struct gl_uniform_storage *storage = &prog->UniformStorage[u];
591
592 if (strncmp(ir->name, storage->name, namelen) != 0 ||
593 (storage->name[namelen] != 0 &&
594 storage->name[namelen] != '.' &&
595 storage->name[namelen] != '[')) {
596 continue;
597 }
598
599 gl_constant_value *components = storage->storage;
600 unsigned vector_count = (MAX2(storage->array_elements, 1) *
601 storage->type->matrix_columns);
602
603 for (unsigned s = 0; s < vector_count; s++) {
604 uniform_vector_size[uniforms] = storage->type->vector_elements;
605
606 int i;
607 for (i = 0; i < uniform_vector_size[uniforms]; i++) {
608 c->prog_data.param[uniforms * 4 + i] = &components->f;
609 components++;
610 }
611 for (; i < 4; i++) {
612 static float zero = 0;
613 c->prog_data.param[uniforms * 4 + i] = &zero;
614 }
615
616 uniforms++;
617 }
618 }
619 }
620
621 void
622 vec4_visitor::setup_uniform_clipplane_values()
623 {
624 gl_clip_plane *clip_planes = brw_select_clip_planes(ctx);
625
626 if (intel->gen < 6) {
627 /* Pre-Gen6, we compact clip planes. For example, if the user
628 * enables just clip planes 0, 1, and 3, we will enable clip planes
629 * 0, 1, and 2 in the hardware, and we'll move clip plane 3 to clip
630 * plane 2. This simplifies the implementation of the Gen6 clip
631 * thread.
632 */
633 int compacted_clipplane_index = 0;
634 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
635 if (!(c->key.userclip_planes_enabled_gen_4_5 & (1 << i)))
636 continue;
637
638 this->uniform_vector_size[this->uniforms] = 4;
639 this->userplane[compacted_clipplane_index] = dst_reg(UNIFORM, this->uniforms);
640 this->userplane[compacted_clipplane_index].type = BRW_REGISTER_TYPE_F;
641 for (int j = 0; j < 4; ++j) {
642 c->prog_data.param[this->uniforms * 4 + j] = &clip_planes[i][j];
643 }
644 ++compacted_clipplane_index;
645 ++this->uniforms;
646 }
647 } else {
648 /* In Gen6 and later, we don't compact clip planes, because this
649 * simplifies the implementation of gl_ClipDistance.
650 */
651 for (int i = 0; i < c->key.nr_userclip_plane_consts; ++i) {
652 this->uniform_vector_size[this->uniforms] = 4;
653 this->userplane[i] = dst_reg(UNIFORM, this->uniforms);
654 this->userplane[i].type = BRW_REGISTER_TYPE_F;
655 for (int j = 0; j < 4; ++j) {
656 c->prog_data.param[this->uniforms * 4 + j] = &clip_planes[i][j];
657 }
658 ++this->uniforms;
659 }
660 }
661 }
662
663 /* Our support for builtin uniforms is even scarier than non-builtin.
664 * It sits on top of the PROG_STATE_VAR parameters that are
665 * automatically updated from GL context state.
666 */
667 void
668 vec4_visitor::setup_builtin_uniform_values(ir_variable *ir)
669 {
670 const ir_state_slot *const slots = ir->state_slots;
671 assert(ir->state_slots != NULL);
672
673 for (unsigned int i = 0; i < ir->num_state_slots; i++) {
674 /* This state reference has already been setup by ir_to_mesa,
675 * but we'll get the same index back here. We can reference
676 * ParameterValues directly, since unlike brw_fs.cpp, we never
677 * add new state references during compile.
678 */
679 int index = _mesa_add_state_reference(this->vp->Base.Parameters,
680 (gl_state_index *)slots[i].tokens);
681 float *values = &this->vp->Base.Parameters->ParameterValues[index][0].f;
682
683 this->uniform_vector_size[this->uniforms] = 0;
684 /* Add each of the unique swizzled channels of the element.
685 * This will end up matching the size of the glsl_type of this field.
686 */
687 int last_swiz = -1;
688 for (unsigned int j = 0; j < 4; j++) {
689 int swiz = GET_SWZ(slots[i].swizzle, j);
690 last_swiz = swiz;
691
692 c->prog_data.param[this->uniforms * 4 + j] = &values[swiz];
693 if (swiz <= last_swiz)
694 this->uniform_vector_size[this->uniforms]++;
695 }
696 this->uniforms++;
697 }
698 }
699
700 dst_reg *
701 vec4_visitor::variable_storage(ir_variable *var)
702 {
703 return (dst_reg *)hash_table_find(this->variable_ht, var);
704 }
705
706 void
707 vec4_visitor::emit_bool_to_cond_code(ir_rvalue *ir, uint32_t *predicate)
708 {
709 ir_expression *expr = ir->as_expression();
710
711 *predicate = BRW_PREDICATE_NORMAL;
712
713 if (expr) {
714 src_reg op[2];
715 vec4_instruction *inst;
716
717 assert(expr->get_num_operands() <= 2);
718 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
719 expr->operands[i]->accept(this);
720 op[i] = this->result;
721
722 resolve_ud_negate(&op[i]);
723 }
724
725 switch (expr->operation) {
726 case ir_unop_logic_not:
727 inst = emit(AND(dst_null_d(), op[0], src_reg(1)));
728 inst->conditional_mod = BRW_CONDITIONAL_Z;
729 break;
730
731 case ir_binop_logic_xor:
732 inst = emit(XOR(dst_null_d(), op[0], op[1]));
733 inst->conditional_mod = BRW_CONDITIONAL_NZ;
734 break;
735
736 case ir_binop_logic_or:
737 inst = emit(OR(dst_null_d(), op[0], op[1]));
738 inst->conditional_mod = BRW_CONDITIONAL_NZ;
739 break;
740
741 case ir_binop_logic_and:
742 inst = emit(AND(dst_null_d(), op[0], op[1]));
743 inst->conditional_mod = BRW_CONDITIONAL_NZ;
744 break;
745
746 case ir_unop_f2b:
747 if (intel->gen >= 6) {
748 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
749 } else {
750 inst = emit(MOV(dst_null_f(), op[0]));
751 inst->conditional_mod = BRW_CONDITIONAL_NZ;
752 }
753 break;
754
755 case ir_unop_i2b:
756 if (intel->gen >= 6) {
757 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
758 } else {
759 inst = emit(MOV(dst_null_d(), op[0]));
760 inst->conditional_mod = BRW_CONDITIONAL_NZ;
761 }
762 break;
763
764 case ir_binop_all_equal:
765 inst = emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
766 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
767 break;
768
769 case ir_binop_any_nequal:
770 inst = emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
771 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
772 break;
773
774 case ir_unop_any:
775 inst = emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
776 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
777 break;
778
779 case ir_binop_greater:
780 case ir_binop_gequal:
781 case ir_binop_less:
782 case ir_binop_lequal:
783 case ir_binop_equal:
784 case ir_binop_nequal:
785 emit(CMP(dst_null_d(), op[0], op[1],
786 brw_conditional_for_comparison(expr->operation)));
787 break;
788
789 default:
790 assert(!"not reached");
791 break;
792 }
793 return;
794 }
795
796 ir->accept(this);
797
798 resolve_ud_negate(&this->result);
799
800 if (intel->gen >= 6) {
801 vec4_instruction *inst = emit(AND(dst_null_d(),
802 this->result, src_reg(1)));
803 inst->conditional_mod = BRW_CONDITIONAL_NZ;
804 } else {
805 vec4_instruction *inst = emit(MOV(dst_null_d(), this->result));
806 inst->conditional_mod = BRW_CONDITIONAL_NZ;
807 }
808 }
809
810 /**
811 * Emit a gen6 IF statement with the comparison folded into the IF
812 * instruction.
813 */
814 void
815 vec4_visitor::emit_if_gen6(ir_if *ir)
816 {
817 ir_expression *expr = ir->condition->as_expression();
818
819 if (expr) {
820 src_reg op[2];
821 dst_reg temp;
822
823 assert(expr->get_num_operands() <= 2);
824 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
825 expr->operands[i]->accept(this);
826 op[i] = this->result;
827 }
828
829 switch (expr->operation) {
830 case ir_unop_logic_not:
831 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_Z));
832 return;
833
834 case ir_binop_logic_xor:
835 emit(IF(op[0], op[1], BRW_CONDITIONAL_NZ));
836 return;
837
838 case ir_binop_logic_or:
839 temp = dst_reg(this, glsl_type::bool_type);
840 emit(OR(temp, op[0], op[1]));
841 emit(IF(src_reg(temp), src_reg(0), BRW_CONDITIONAL_NZ));
842 return;
843
844 case ir_binop_logic_and:
845 temp = dst_reg(this, glsl_type::bool_type);
846 emit(AND(temp, op[0], op[1]));
847 emit(IF(src_reg(temp), src_reg(0), BRW_CONDITIONAL_NZ));
848 return;
849
850 case ir_unop_f2b:
851 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
852 return;
853
854 case ir_unop_i2b:
855 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
856 return;
857
858 case ir_binop_greater:
859 case ir_binop_gequal:
860 case ir_binop_less:
861 case ir_binop_lequal:
862 case ir_binop_equal:
863 case ir_binop_nequal:
864 emit(IF(op[0], op[1],
865 brw_conditional_for_comparison(expr->operation)));
866 return;
867
868 case ir_binop_all_equal:
869 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
870 emit(IF(BRW_PREDICATE_ALIGN16_ALL4H));
871 return;
872
873 case ir_binop_any_nequal:
874 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
875 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H));
876 return;
877
878 case ir_unop_any:
879 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
880 emit(IF(BRW_PREDICATE_ALIGN16_ANY4H));
881 return;
882
883 default:
884 assert(!"not reached");
885 emit(IF(op[0], src_reg(0), BRW_CONDITIONAL_NZ));
886 return;
887 }
888 return;
889 }
890
891 ir->condition->accept(this);
892
893 emit(IF(this->result, src_reg(0), BRW_CONDITIONAL_NZ));
894 }
895
896 static dst_reg
897 with_writemask(dst_reg const & r, int mask)
898 {
899 dst_reg result = r;
900 result.writemask = mask;
901 return result;
902 }
903
904 void
905 vec4_visitor::emit_attribute_fixups()
906 {
907 dst_reg sign_recovery_shift;
908 dst_reg normalize_factor;
909 dst_reg es3_normalize_factor;
910
911 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
912 if (prog_data->inputs_read & BITFIELD64_BIT(i)) {
913 uint8_t wa_flags = c->key.gl_attrib_wa_flags[i];
914 dst_reg reg(ATTR, i);
915 dst_reg reg_d = reg;
916 reg_d.type = BRW_REGISTER_TYPE_D;
917 dst_reg reg_ud = reg;
918 reg_ud.type = BRW_REGISTER_TYPE_UD;
919
920 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
921 * come in as floating point conversions of the integer values.
922 */
923 if (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK) {
924 dst_reg dst = reg;
925 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
926 dst.writemask = (1 << (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK)) - 1;
927 emit(MUL(dst, src_reg(dst), src_reg(1.0f / 65536.0f)));
928 }
929
930 /* Do sign recovery for 2101010 formats if required. */
931 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
932 if (sign_recovery_shift.file == BAD_FILE) {
933 /* shift constant: <22,22,22,30> */
934 sign_recovery_shift = dst_reg(this, glsl_type::uvec4_type);
935 emit(MOV(with_writemask(sign_recovery_shift, WRITEMASK_XYZ), src_reg(22u)));
936 emit(MOV(with_writemask(sign_recovery_shift, WRITEMASK_W), src_reg(30u)));
937 }
938
939 emit(SHL(reg_ud, src_reg(reg_ud), src_reg(sign_recovery_shift)));
940 emit(ASR(reg_d, src_reg(reg_d), src_reg(sign_recovery_shift)));
941 }
942
943 /* Apply BGRA swizzle if required. */
944 if (wa_flags & BRW_ATTRIB_WA_BGRA) {
945 src_reg temp = src_reg(reg);
946 temp.swizzle = BRW_SWIZZLE4(2,1,0,3);
947 emit(MOV(reg, temp));
948 }
949
950 if (wa_flags & BRW_ATTRIB_WA_NORMALIZE) {
951 /* ES 3.0 has different rules for converting signed normalized
952 * fixed-point numbers than desktop GL.
953 */
954 if (_mesa_is_gles3(ctx) && (wa_flags & BRW_ATTRIB_WA_SIGN)) {
955 /* According to equation 2.2 of the ES 3.0 specification,
956 * signed normalization conversion is done by:
957 *
958 * f = c / (2^(b-1)-1)
959 */
960 if (es3_normalize_factor.file == BAD_FILE) {
961 /* mul constant: 1 / (2^(b-1) - 1) */
962 es3_normalize_factor = dst_reg(this, glsl_type::vec4_type);
963 emit(MOV(with_writemask(es3_normalize_factor, WRITEMASK_XYZ),
964 src_reg(1.0f / ((1<<9) - 1))));
965 emit(MOV(with_writemask(es3_normalize_factor, WRITEMASK_W),
966 src_reg(1.0f / ((1<<1) - 1))));
967 }
968
969 dst_reg dst = reg;
970 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
971 emit(MOV(dst, src_reg(reg_d)));
972 emit(MUL(dst, src_reg(dst), src_reg(es3_normalize_factor)));
973 emit_minmax(BRW_CONDITIONAL_G, dst, src_reg(dst), src_reg(-1.0f));
974 } else {
975 /* The following equations are from the OpenGL 3.2 specification:
976 *
977 * 2.1 unsigned normalization
978 * f = c/(2^n-1)
979 *
980 * 2.2 signed normalization
981 * f = (2c+1)/(2^n-1)
982 *
983 * Both of these share a common divisor, which is represented by
984 * "normalize_factor" in the code below.
985 */
986 if (normalize_factor.file == BAD_FILE) {
987 /* 1 / (2^b - 1) for b=<10,10,10,2> */
988 normalize_factor = dst_reg(this, glsl_type::vec4_type);
989 emit(MOV(with_writemask(normalize_factor, WRITEMASK_XYZ),
990 src_reg(1.0f / ((1<<10) - 1))));
991 emit(MOV(with_writemask(normalize_factor, WRITEMASK_W),
992 src_reg(1.0f / ((1<<2) - 1))));
993 }
994
995 dst_reg dst = reg;
996 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
997 emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
998
999 /* For signed normalization, we want the numerator to be 2c+1. */
1000 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
1001 emit(MUL(dst, src_reg(dst), src_reg(2.0f)));
1002 emit(ADD(dst, src_reg(dst), src_reg(1.0f)));
1003 }
1004
1005 emit(MUL(dst, src_reg(dst), src_reg(normalize_factor)));
1006 }
1007 }
1008
1009 if (wa_flags & BRW_ATTRIB_WA_SCALE) {
1010 dst_reg dst = reg;
1011 dst.type = brw_type_for_base_type(glsl_type::vec4_type);
1012 emit(MOV(dst, src_reg((wa_flags & BRW_ATTRIB_WA_SIGN) ? reg_d : reg_ud)));
1013 }
1014 }
1015 }
1016 }
1017
1018 void
1019 vec4_visitor::visit(ir_variable *ir)
1020 {
1021 dst_reg *reg = NULL;
1022
1023 if (variable_storage(ir))
1024 return;
1025
1026 switch (ir->mode) {
1027 case ir_var_shader_in:
1028 reg = new(mem_ctx) dst_reg(ATTR, ir->location);
1029 break;
1030
1031 case ir_var_shader_out:
1032 reg = new(mem_ctx) dst_reg(this, ir->type);
1033
1034 for (int i = 0; i < type_size(ir->type); i++) {
1035 output_reg[ir->location + i] = *reg;
1036 output_reg[ir->location + i].reg_offset = i;
1037 output_reg[ir->location + i].type =
1038 brw_type_for_base_type(ir->type->get_scalar_type());
1039 output_reg_annotation[ir->location + i] = ir->name;
1040 }
1041 break;
1042
1043 case ir_var_auto:
1044 case ir_var_temporary:
1045 reg = new(mem_ctx) dst_reg(this, ir->type);
1046 break;
1047
1048 case ir_var_uniform:
1049 reg = new(this->mem_ctx) dst_reg(UNIFORM, this->uniforms);
1050
1051 /* Thanks to the lower_ubo_reference pass, we will see only
1052 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
1053 * variables, so no need for them to be in variable_ht.
1054 */
1055 if (ir->uniform_block != -1)
1056 return;
1057
1058 /* Track how big the whole uniform variable is, in case we need to put a
1059 * copy of its data into pull constants for array access.
1060 */
1061 this->uniform_size[this->uniforms] = type_size(ir->type);
1062
1063 if (!strncmp(ir->name, "gl_", 3)) {
1064 setup_builtin_uniform_values(ir);
1065 } else {
1066 setup_uniform_values(ir);
1067 }
1068 break;
1069
1070 case ir_var_system_value:
1071 /* VertexID is stored by the VF as the last vertex element, but
1072 * we don't represent it with a flag in inputs_read, so we call
1073 * it VERT_ATTRIB_MAX, which setup_attributes() picks up on.
1074 */
1075 reg = new(mem_ctx) dst_reg(ATTR, VERT_ATTRIB_MAX);
1076 prog_data->uses_vertexid = true;
1077
1078 switch (ir->location) {
1079 case SYSTEM_VALUE_VERTEX_ID:
1080 reg->writemask = WRITEMASK_X;
1081 break;
1082 case SYSTEM_VALUE_INSTANCE_ID:
1083 reg->writemask = WRITEMASK_Y;
1084 break;
1085 default:
1086 assert(!"not reached");
1087 break;
1088 }
1089 break;
1090
1091 default:
1092 assert(!"not reached");
1093 }
1094
1095 reg->type = brw_type_for_base_type(ir->type);
1096 hash_table_insert(this->variable_ht, reg, ir);
1097 }
1098
1099 void
1100 vec4_visitor::visit(ir_loop *ir)
1101 {
1102 dst_reg counter;
1103
1104 /* We don't want debugging output to print the whole body of the
1105 * loop as the annotation.
1106 */
1107 this->base_ir = NULL;
1108
1109 if (ir->counter != NULL) {
1110 this->base_ir = ir->counter;
1111 ir->counter->accept(this);
1112 counter = *(variable_storage(ir->counter));
1113
1114 if (ir->from != NULL) {
1115 this->base_ir = ir->from;
1116 ir->from->accept(this);
1117
1118 emit(MOV(counter, this->result));
1119 }
1120 }
1121
1122 emit(BRW_OPCODE_DO);
1123
1124 if (ir->to) {
1125 this->base_ir = ir->to;
1126 ir->to->accept(this);
1127
1128 emit(CMP(dst_null_d(), src_reg(counter), this->result,
1129 brw_conditional_for_comparison(ir->cmp)));
1130
1131 vec4_instruction *inst = emit(BRW_OPCODE_BREAK);
1132 inst->predicate = BRW_PREDICATE_NORMAL;
1133 }
1134
1135 visit_instructions(&ir->body_instructions);
1136
1137
1138 if (ir->increment) {
1139 this->base_ir = ir->increment;
1140 ir->increment->accept(this);
1141 emit(ADD(counter, src_reg(counter), this->result));
1142 }
1143
1144 emit(BRW_OPCODE_WHILE);
1145 }
1146
1147 void
1148 vec4_visitor::visit(ir_loop_jump *ir)
1149 {
1150 switch (ir->mode) {
1151 case ir_loop_jump::jump_break:
1152 emit(BRW_OPCODE_BREAK);
1153 break;
1154 case ir_loop_jump::jump_continue:
1155 emit(BRW_OPCODE_CONTINUE);
1156 break;
1157 }
1158 }
1159
1160
1161 void
1162 vec4_visitor::visit(ir_function_signature *ir)
1163 {
1164 assert(0);
1165 (void)ir;
1166 }
1167
1168 void
1169 vec4_visitor::visit(ir_function *ir)
1170 {
1171 /* Ignore function bodies other than main() -- we shouldn't see calls to
1172 * them since they should all be inlined.
1173 */
1174 if (strcmp(ir->name, "main") == 0) {
1175 const ir_function_signature *sig;
1176 exec_list empty;
1177
1178 sig = ir->matching_signature(&empty);
1179
1180 assert(sig);
1181
1182 visit_instructions(&sig->body);
1183 }
1184 }
1185
1186 bool
1187 vec4_visitor::try_emit_sat(ir_expression *ir)
1188 {
1189 ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
1190 if (!sat_src)
1191 return false;
1192
1193 sat_src->accept(this);
1194 src_reg src = this->result;
1195
1196 this->result = src_reg(this, ir->type);
1197 vec4_instruction *inst;
1198 inst = emit(MOV(dst_reg(this->result), src));
1199 inst->saturate = true;
1200
1201 return true;
1202 }
1203
1204 void
1205 vec4_visitor::emit_bool_comparison(unsigned int op,
1206 dst_reg dst, src_reg src0, src_reg src1)
1207 {
1208 /* original gen4 does destination conversion before comparison. */
1209 if (intel->gen < 5)
1210 dst.type = src0.type;
1211
1212 emit(CMP(dst, src0, src1, brw_conditional_for_comparison(op)));
1213
1214 dst.type = BRW_REGISTER_TYPE_D;
1215 emit(AND(dst, src_reg(dst), src_reg(0x1)));
1216 }
1217
1218 void
1219 vec4_visitor::emit_minmax(uint32_t conditionalmod, dst_reg dst,
1220 src_reg src0, src_reg src1)
1221 {
1222 vec4_instruction *inst;
1223
1224 if (intel->gen >= 6) {
1225 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
1226 inst->conditional_mod = conditionalmod;
1227 } else {
1228 emit(CMP(dst, src0, src1, conditionalmod));
1229
1230 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
1231 inst->predicate = BRW_PREDICATE_NORMAL;
1232 }
1233 }
1234
1235 void
1236 vec4_visitor::visit(ir_expression *ir)
1237 {
1238 unsigned int operand;
1239 src_reg op[Elements(ir->operands)];
1240 src_reg result_src;
1241 dst_reg result_dst;
1242 vec4_instruction *inst;
1243
1244 if (try_emit_sat(ir))
1245 return;
1246
1247 for (operand = 0; operand < ir->get_num_operands(); operand++) {
1248 this->result.file = BAD_FILE;
1249 ir->operands[operand]->accept(this);
1250 if (this->result.file == BAD_FILE) {
1251 printf("Failed to get tree for expression operand:\n");
1252 ir->operands[operand]->print();
1253 exit(1);
1254 }
1255 op[operand] = this->result;
1256
1257 /* Matrix expression operands should have been broken down to vector
1258 * operations already.
1259 */
1260 assert(!ir->operands[operand]->type->is_matrix());
1261 }
1262
1263 int vector_elements = ir->operands[0]->type->vector_elements;
1264 if (ir->operands[1]) {
1265 vector_elements = MAX2(vector_elements,
1266 ir->operands[1]->type->vector_elements);
1267 }
1268
1269 this->result.file = BAD_FILE;
1270
1271 /* Storage for our result. Ideally for an assignment we'd be using
1272 * the actual storage for the result here, instead.
1273 */
1274 result_src = src_reg(this, ir->type);
1275 /* convenience for the emit functions below. */
1276 result_dst = dst_reg(result_src);
1277 /* If nothing special happens, this is the result. */
1278 this->result = result_src;
1279 /* Limit writes to the channels that will be used by result_src later.
1280 * This does limit this temp's use as a temporary for multi-instruction
1281 * sequences.
1282 */
1283 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1284
1285 switch (ir->operation) {
1286 case ir_unop_logic_not:
1287 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
1288 * ones complement of the whole register, not just bit 0.
1289 */
1290 emit(XOR(result_dst, op[0], src_reg(1)));
1291 break;
1292 case ir_unop_neg:
1293 op[0].negate = !op[0].negate;
1294 this->result = op[0];
1295 break;
1296 case ir_unop_abs:
1297 op[0].abs = true;
1298 op[0].negate = false;
1299 this->result = op[0];
1300 break;
1301
1302 case ir_unop_sign:
1303 emit(MOV(result_dst, src_reg(0.0f)));
1304
1305 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_G));
1306 inst = emit(MOV(result_dst, src_reg(1.0f)));
1307 inst->predicate = BRW_PREDICATE_NORMAL;
1308
1309 emit(CMP(dst_null_d(), op[0], src_reg(0.0f), BRW_CONDITIONAL_L));
1310 inst = emit(MOV(result_dst, src_reg(-1.0f)));
1311 inst->predicate = BRW_PREDICATE_NORMAL;
1312
1313 break;
1314
1315 case ir_unop_rcp:
1316 emit_math(SHADER_OPCODE_RCP, result_dst, op[0]);
1317 break;
1318
1319 case ir_unop_exp2:
1320 emit_math(SHADER_OPCODE_EXP2, result_dst, op[0]);
1321 break;
1322 case ir_unop_log2:
1323 emit_math(SHADER_OPCODE_LOG2, result_dst, op[0]);
1324 break;
1325 case ir_unop_exp:
1326 case ir_unop_log:
1327 assert(!"not reached: should be handled by ir_explog_to_explog2");
1328 break;
1329 case ir_unop_sin:
1330 case ir_unop_sin_reduced:
1331 emit_math(SHADER_OPCODE_SIN, result_dst, op[0]);
1332 break;
1333 case ir_unop_cos:
1334 case ir_unop_cos_reduced:
1335 emit_math(SHADER_OPCODE_COS, result_dst, op[0]);
1336 break;
1337
1338 case ir_unop_dFdx:
1339 case ir_unop_dFdy:
1340 assert(!"derivatives not valid in vertex shader");
1341 break;
1342
1343 case ir_unop_noise:
1344 assert(!"not reached: should be handled by lower_noise");
1345 break;
1346
1347 case ir_binop_add:
1348 emit(ADD(result_dst, op[0], op[1]));
1349 break;
1350 case ir_binop_sub:
1351 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1352 break;
1353
1354 case ir_binop_mul:
1355 if (ir->type->is_integer()) {
1356 /* For integer multiplication, the MUL uses the low 16 bits
1357 * of one of the operands (src0 on gen6, src1 on gen7). The
1358 * MACH accumulates in the contribution of the upper 16 bits
1359 * of that operand.
1360 *
1361 * FINISHME: Emit just the MUL if we know an operand is small
1362 * enough.
1363 */
1364 struct brw_reg acc = retype(brw_acc_reg(), BRW_REGISTER_TYPE_D);
1365
1366 emit(MUL(acc, op[0], op[1]));
1367 emit(MACH(dst_null_d(), op[0], op[1]));
1368 emit(MOV(result_dst, src_reg(acc)));
1369 } else {
1370 emit(MUL(result_dst, op[0], op[1]));
1371 }
1372 break;
1373 case ir_binop_div:
1374 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
1375 assert(ir->type->is_integer());
1376 emit_math(SHADER_OPCODE_INT_QUOTIENT, result_dst, op[0], op[1]);
1377 break;
1378 case ir_binop_mod:
1379 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
1380 assert(ir->type->is_integer());
1381 emit_math(SHADER_OPCODE_INT_REMAINDER, result_dst, op[0], op[1]);
1382 break;
1383
1384 case ir_binop_less:
1385 case ir_binop_greater:
1386 case ir_binop_lequal:
1387 case ir_binop_gequal:
1388 case ir_binop_equal:
1389 case ir_binop_nequal: {
1390 emit(CMP(result_dst, op[0], op[1],
1391 brw_conditional_for_comparison(ir->operation)));
1392 emit(AND(result_dst, result_src, src_reg(0x1)));
1393 break;
1394 }
1395
1396 case ir_binop_all_equal:
1397 /* "==" operator producing a scalar boolean. */
1398 if (ir->operands[0]->type->is_vector() ||
1399 ir->operands[1]->type->is_vector()) {
1400 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_Z));
1401 emit(MOV(result_dst, src_reg(0)));
1402 inst = emit(MOV(result_dst, src_reg(1)));
1403 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1404 } else {
1405 emit(CMP(result_dst, op[0], op[1], BRW_CONDITIONAL_Z));
1406 emit(AND(result_dst, result_src, src_reg(0x1)));
1407 }
1408 break;
1409 case ir_binop_any_nequal:
1410 /* "!=" operator producing a scalar boolean. */
1411 if (ir->operands[0]->type->is_vector() ||
1412 ir->operands[1]->type->is_vector()) {
1413 emit(CMP(dst_null_d(), op[0], op[1], BRW_CONDITIONAL_NZ));
1414
1415 emit(MOV(result_dst, src_reg(0)));
1416 inst = emit(MOV(result_dst, src_reg(1)));
1417 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1418 } else {
1419 emit(CMP(result_dst, op[0], op[1], BRW_CONDITIONAL_NZ));
1420 emit(AND(result_dst, result_src, src_reg(0x1)));
1421 }
1422 break;
1423
1424 case ir_unop_any:
1425 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
1426 emit(MOV(result_dst, src_reg(0)));
1427
1428 inst = emit(MOV(result_dst, src_reg(1)));
1429 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1430 break;
1431
1432 case ir_binop_logic_xor:
1433 emit(XOR(result_dst, op[0], op[1]));
1434 break;
1435
1436 case ir_binop_logic_or:
1437 emit(OR(result_dst, op[0], op[1]));
1438 break;
1439
1440 case ir_binop_logic_and:
1441 emit(AND(result_dst, op[0], op[1]));
1442 break;
1443
1444 case ir_binop_dot:
1445 assert(ir->operands[0]->type->is_vector());
1446 assert(ir->operands[0]->type == ir->operands[1]->type);
1447 emit_dp(result_dst, op[0], op[1], ir->operands[0]->type->vector_elements);
1448 break;
1449
1450 case ir_unop_sqrt:
1451 emit_math(SHADER_OPCODE_SQRT, result_dst, op[0]);
1452 break;
1453 case ir_unop_rsq:
1454 emit_math(SHADER_OPCODE_RSQ, result_dst, op[0]);
1455 break;
1456
1457 case ir_unop_bitcast_i2f:
1458 case ir_unop_bitcast_u2f:
1459 this->result = op[0];
1460 this->result.type = BRW_REGISTER_TYPE_F;
1461 break;
1462
1463 case ir_unop_bitcast_f2i:
1464 this->result = op[0];
1465 this->result.type = BRW_REGISTER_TYPE_D;
1466 break;
1467
1468 case ir_unop_bitcast_f2u:
1469 this->result = op[0];
1470 this->result.type = BRW_REGISTER_TYPE_UD;
1471 break;
1472
1473 case ir_unop_i2f:
1474 case ir_unop_i2u:
1475 case ir_unop_u2i:
1476 case ir_unop_u2f:
1477 case ir_unop_b2f:
1478 case ir_unop_b2i:
1479 case ir_unop_f2i:
1480 case ir_unop_f2u:
1481 emit(MOV(result_dst, op[0]));
1482 break;
1483 case ir_unop_f2b:
1484 case ir_unop_i2b: {
1485 emit(CMP(result_dst, op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
1486 emit(AND(result_dst, result_src, src_reg(1)));
1487 break;
1488 }
1489
1490 case ir_unop_trunc:
1491 emit(RNDZ(result_dst, op[0]));
1492 break;
1493 case ir_unop_ceil:
1494 op[0].negate = !op[0].negate;
1495 inst = emit(RNDD(result_dst, op[0]));
1496 this->result.negate = true;
1497 break;
1498 case ir_unop_floor:
1499 inst = emit(RNDD(result_dst, op[0]));
1500 break;
1501 case ir_unop_fract:
1502 inst = emit(FRC(result_dst, op[0]));
1503 break;
1504 case ir_unop_round_even:
1505 emit(RNDE(result_dst, op[0]));
1506 break;
1507
1508 case ir_binop_min:
1509 emit_minmax(BRW_CONDITIONAL_L, result_dst, op[0], op[1]);
1510 break;
1511 case ir_binop_max:
1512 emit_minmax(BRW_CONDITIONAL_G, result_dst, op[0], op[1]);
1513 break;
1514
1515 case ir_binop_pow:
1516 emit_math(SHADER_OPCODE_POW, result_dst, op[0], op[1]);
1517 break;
1518
1519 case ir_unop_bit_not:
1520 inst = emit(NOT(result_dst, op[0]));
1521 break;
1522 case ir_binop_bit_and:
1523 inst = emit(AND(result_dst, op[0], op[1]));
1524 break;
1525 case ir_binop_bit_xor:
1526 inst = emit(XOR(result_dst, op[0], op[1]));
1527 break;
1528 case ir_binop_bit_or:
1529 inst = emit(OR(result_dst, op[0], op[1]));
1530 break;
1531
1532 case ir_binop_lshift:
1533 inst = emit(SHL(result_dst, op[0], op[1]));
1534 break;
1535
1536 case ir_binop_rshift:
1537 if (ir->type->base_type == GLSL_TYPE_INT)
1538 inst = emit(ASR(result_dst, op[0], op[1]));
1539 else
1540 inst = emit(SHR(result_dst, op[0], op[1]));
1541 break;
1542
1543 case ir_binop_ubo_load: {
1544 ir_constant *uniform_block = ir->operands[0]->as_constant();
1545 ir_constant *const_offset_ir = ir->operands[1]->as_constant();
1546 unsigned const_offset = const_offset_ir ? const_offset_ir->value.u[0] : 0;
1547 src_reg offset = op[1];
1548
1549 /* Now, load the vector from that offset. */
1550 assert(ir->type->is_vector() || ir->type->is_scalar());
1551
1552 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
1553 packed_consts.type = result.type;
1554 src_reg surf_index =
1555 src_reg(SURF_INDEX_VS_UBO(uniform_block->value.u[0]));
1556 if (const_offset_ir) {
1557 offset = src_reg(const_offset / 16);
1558 } else {
1559 emit(SHR(dst_reg(offset), offset, src_reg(4)));
1560 }
1561
1562 vec4_instruction *pull =
1563 emit(new(mem_ctx) vec4_instruction(this,
1564 VS_OPCODE_PULL_CONSTANT_LOAD,
1565 dst_reg(packed_consts),
1566 surf_index,
1567 offset));
1568 pull->base_mrf = 14;
1569 pull->mlen = 1;
1570
1571 packed_consts.swizzle = swizzle_for_size(ir->type->vector_elements);
1572 packed_consts.swizzle += BRW_SWIZZLE4(const_offset % 16 / 4,
1573 const_offset % 16 / 4,
1574 const_offset % 16 / 4,
1575 const_offset % 16 / 4);
1576
1577 /* UBO bools are any nonzero int. We store bools as either 0 or 1. */
1578 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1579 emit(CMP(result_dst, packed_consts, src_reg(0u),
1580 BRW_CONDITIONAL_NZ));
1581 emit(AND(result_dst, result, src_reg(0x1)));
1582 } else {
1583 emit(MOV(result_dst, packed_consts));
1584 }
1585 break;
1586 }
1587
1588 case ir_quadop_vector:
1589 assert(!"not reached: should be handled by lower_quadop_vector");
1590 break;
1591
1592 case ir_unop_pack_half_2x16:
1593 emit_pack_half_2x16(result_dst, op[0]);
1594 break;
1595 case ir_unop_unpack_half_2x16:
1596 emit_unpack_half_2x16(result_dst, op[0]);
1597 break;
1598 case ir_unop_pack_snorm_2x16:
1599 case ir_unop_pack_unorm_2x16:
1600 case ir_unop_unpack_snorm_2x16:
1601 case ir_unop_unpack_unorm_2x16:
1602 assert(!"not reached: should be handled by lower_packing_builtins");
1603 break;
1604 case ir_unop_unpack_half_2x16_split_x:
1605 case ir_unop_unpack_half_2x16_split_y:
1606 case ir_binop_pack_half_2x16_split:
1607 assert(!"not reached: should not occur in vertex shader");
1608 break;
1609 }
1610 }
1611
1612
1613 void
1614 vec4_visitor::visit(ir_swizzle *ir)
1615 {
1616 src_reg src;
1617 int i = 0;
1618 int swizzle[4];
1619
1620 /* Note that this is only swizzles in expressions, not those on the left
1621 * hand side of an assignment, which do write masking. See ir_assignment
1622 * for that.
1623 */
1624
1625 ir->val->accept(this);
1626 src = this->result;
1627 assert(src.file != BAD_FILE);
1628
1629 for (i = 0; i < ir->type->vector_elements; i++) {
1630 switch (i) {
1631 case 0:
1632 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.x);
1633 break;
1634 case 1:
1635 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.y);
1636 break;
1637 case 2:
1638 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.z);
1639 break;
1640 case 3:
1641 swizzle[i] = BRW_GET_SWZ(src.swizzle, ir->mask.w);
1642 break;
1643 }
1644 }
1645 for (; i < 4; i++) {
1646 /* Replicate the last channel out. */
1647 swizzle[i] = swizzle[ir->type->vector_elements - 1];
1648 }
1649
1650 src.swizzle = BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
1651
1652 this->result = src;
1653 }
1654
1655 void
1656 vec4_visitor::visit(ir_dereference_variable *ir)
1657 {
1658 const struct glsl_type *type = ir->type;
1659 dst_reg *reg = variable_storage(ir->var);
1660
1661 if (!reg) {
1662 fail("Failed to find variable storage for %s\n", ir->var->name);
1663 this->result = src_reg(brw_null_reg());
1664 return;
1665 }
1666
1667 this->result = src_reg(*reg);
1668
1669 /* System values get their swizzle from the dst_reg writemask */
1670 if (ir->var->mode == ir_var_system_value)
1671 return;
1672
1673 if (type->is_scalar() || type->is_vector() || type->is_matrix())
1674 this->result.swizzle = swizzle_for_size(type->vector_elements);
1675 }
1676
1677 void
1678 vec4_visitor::visit(ir_dereference_array *ir)
1679 {
1680 ir_constant *constant_index;
1681 src_reg src;
1682 int element_size = type_size(ir->type);
1683
1684 constant_index = ir->array_index->constant_expression_value();
1685
1686 ir->array->accept(this);
1687 src = this->result;
1688
1689 if (constant_index) {
1690 src.reg_offset += constant_index->value.i[0] * element_size;
1691 } else {
1692 /* Variable index array dereference. It eats the "vec4" of the
1693 * base of the array and an index that offsets the Mesa register
1694 * index.
1695 */
1696 ir->array_index->accept(this);
1697
1698 src_reg index_reg;
1699
1700 if (element_size == 1) {
1701 index_reg = this->result;
1702 } else {
1703 index_reg = src_reg(this, glsl_type::int_type);
1704
1705 emit(MUL(dst_reg(index_reg), this->result, src_reg(element_size)));
1706 }
1707
1708 if (src.reladdr) {
1709 src_reg temp = src_reg(this, glsl_type::int_type);
1710
1711 emit(ADD(dst_reg(temp), *src.reladdr, index_reg));
1712
1713 index_reg = temp;
1714 }
1715
1716 src.reladdr = ralloc(mem_ctx, src_reg);
1717 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
1718 }
1719
1720 /* If the type is smaller than a vec4, replicate the last channel out. */
1721 if (ir->type->is_scalar() || ir->type->is_vector() || ir->type->is_matrix())
1722 src.swizzle = swizzle_for_size(ir->type->vector_elements);
1723 else
1724 src.swizzle = BRW_SWIZZLE_NOOP;
1725 src.type = brw_type_for_base_type(ir->type);
1726
1727 this->result = src;
1728 }
1729
1730 void
1731 vec4_visitor::visit(ir_dereference_record *ir)
1732 {
1733 unsigned int i;
1734 const glsl_type *struct_type = ir->record->type;
1735 int offset = 0;
1736
1737 ir->record->accept(this);
1738
1739 for (i = 0; i < struct_type->length; i++) {
1740 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
1741 break;
1742 offset += type_size(struct_type->fields.structure[i].type);
1743 }
1744
1745 /* If the type is smaller than a vec4, replicate the last channel out. */
1746 if (ir->type->is_scalar() || ir->type->is_vector() || ir->type->is_matrix())
1747 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
1748 else
1749 this->result.swizzle = BRW_SWIZZLE_NOOP;
1750 this->result.type = brw_type_for_base_type(ir->type);
1751
1752 this->result.reg_offset += offset;
1753 }
1754
1755 /**
1756 * We want to be careful in assignment setup to hit the actual storage
1757 * instead of potentially using a temporary like we might with the
1758 * ir_dereference handler.
1759 */
1760 static dst_reg
1761 get_assignment_lhs(ir_dereference *ir, vec4_visitor *v)
1762 {
1763 /* The LHS must be a dereference. If the LHS is a variable indexed array
1764 * access of a vector, it must be separated into a series conditional moves
1765 * before reaching this point (see ir_vec_index_to_cond_assign).
1766 */
1767 assert(ir->as_dereference());
1768 ir_dereference_array *deref_array = ir->as_dereference_array();
1769 if (deref_array) {
1770 assert(!deref_array->array->type->is_vector());
1771 }
1772
1773 /* Use the rvalue deref handler for the most part. We'll ignore
1774 * swizzles in it and write swizzles using writemask, though.
1775 */
1776 ir->accept(v);
1777 return dst_reg(v->result);
1778 }
1779
1780 void
1781 vec4_visitor::emit_block_move(dst_reg *dst, src_reg *src,
1782 const struct glsl_type *type, uint32_t predicate)
1783 {
1784 if (type->base_type == GLSL_TYPE_STRUCT) {
1785 for (unsigned int i = 0; i < type->length; i++) {
1786 emit_block_move(dst, src, type->fields.structure[i].type, predicate);
1787 }
1788 return;
1789 }
1790
1791 if (type->is_array()) {
1792 for (unsigned int i = 0; i < type->length; i++) {
1793 emit_block_move(dst, src, type->fields.array, predicate);
1794 }
1795 return;
1796 }
1797
1798 if (type->is_matrix()) {
1799 const struct glsl_type *vec_type;
1800
1801 vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
1802 type->vector_elements, 1);
1803
1804 for (int i = 0; i < type->matrix_columns; i++) {
1805 emit_block_move(dst, src, vec_type, predicate);
1806 }
1807 return;
1808 }
1809
1810 assert(type->is_scalar() || type->is_vector());
1811
1812 dst->type = brw_type_for_base_type(type);
1813 src->type = dst->type;
1814
1815 dst->writemask = (1 << type->vector_elements) - 1;
1816
1817 src->swizzle = swizzle_for_size(type->vector_elements);
1818
1819 vec4_instruction *inst = emit(MOV(*dst, *src));
1820 inst->predicate = predicate;
1821
1822 dst->reg_offset++;
1823 src->reg_offset++;
1824 }
1825
1826
1827 /* If the RHS processing resulted in an instruction generating a
1828 * temporary value, and it would be easy to rewrite the instruction to
1829 * generate its result right into the LHS instead, do so. This ends
1830 * up reliably removing instructions where it can be tricky to do so
1831 * later without real UD chain information.
1832 */
1833 bool
1834 vec4_visitor::try_rewrite_rhs_to_dst(ir_assignment *ir,
1835 dst_reg dst,
1836 src_reg src,
1837 vec4_instruction *pre_rhs_inst,
1838 vec4_instruction *last_rhs_inst)
1839 {
1840 /* This could be supported, but it would take more smarts. */
1841 if (ir->condition)
1842 return false;
1843
1844 if (pre_rhs_inst == last_rhs_inst)
1845 return false; /* No instructions generated to work with. */
1846
1847 /* Make sure the last instruction generated our source reg. */
1848 if (src.file != GRF ||
1849 src.file != last_rhs_inst->dst.file ||
1850 src.reg != last_rhs_inst->dst.reg ||
1851 src.reg_offset != last_rhs_inst->dst.reg_offset ||
1852 src.reladdr ||
1853 src.abs ||
1854 src.negate ||
1855 last_rhs_inst->predicate != BRW_PREDICATE_NONE)
1856 return false;
1857
1858 /* Check that that last instruction fully initialized the channels
1859 * we want to use, in the order we want to use them. We could
1860 * potentially reswizzle the operands of many instructions so that
1861 * we could handle out of order channels, but don't yet.
1862 */
1863
1864 for (unsigned i = 0; i < 4; i++) {
1865 if (dst.writemask & (1 << i)) {
1866 if (!(last_rhs_inst->dst.writemask & (1 << i)))
1867 return false;
1868
1869 if (BRW_GET_SWZ(src.swizzle, i) != i)
1870 return false;
1871 }
1872 }
1873
1874 /* Success! Rewrite the instruction. */
1875 last_rhs_inst->dst.file = dst.file;
1876 last_rhs_inst->dst.reg = dst.reg;
1877 last_rhs_inst->dst.reg_offset = dst.reg_offset;
1878 last_rhs_inst->dst.reladdr = dst.reladdr;
1879 last_rhs_inst->dst.writemask &= dst.writemask;
1880
1881 return true;
1882 }
1883
1884 void
1885 vec4_visitor::visit(ir_assignment *ir)
1886 {
1887 dst_reg dst = get_assignment_lhs(ir->lhs, this);
1888 uint32_t predicate = BRW_PREDICATE_NONE;
1889
1890 if (!ir->lhs->type->is_scalar() &&
1891 !ir->lhs->type->is_vector()) {
1892 ir->rhs->accept(this);
1893 src_reg src = this->result;
1894
1895 if (ir->condition) {
1896 emit_bool_to_cond_code(ir->condition, &predicate);
1897 }
1898
1899 /* emit_block_move doesn't account for swizzles in the source register.
1900 * This should be ok, since the source register is a structure or an
1901 * array, and those can't be swizzled. But double-check to be sure.
1902 */
1903 assert(src.swizzle ==
1904 (ir->rhs->type->is_matrix()
1905 ? swizzle_for_size(ir->rhs->type->vector_elements)
1906 : BRW_SWIZZLE_NOOP));
1907
1908 emit_block_move(&dst, &src, ir->rhs->type, predicate);
1909 return;
1910 }
1911
1912 /* Now we're down to just a scalar/vector with writemasks. */
1913 int i;
1914
1915 vec4_instruction *pre_rhs_inst, *last_rhs_inst;
1916 pre_rhs_inst = (vec4_instruction *)this->instructions.get_tail();
1917
1918 ir->rhs->accept(this);
1919
1920 last_rhs_inst = (vec4_instruction *)this->instructions.get_tail();
1921
1922 src_reg src = this->result;
1923
1924 int swizzles[4];
1925 int first_enabled_chan = 0;
1926 int src_chan = 0;
1927
1928 assert(ir->lhs->type->is_vector() ||
1929 ir->lhs->type->is_scalar());
1930 dst.writemask = ir->write_mask;
1931
1932 for (int i = 0; i < 4; i++) {
1933 if (dst.writemask & (1 << i)) {
1934 first_enabled_chan = BRW_GET_SWZ(src.swizzle, i);
1935 break;
1936 }
1937 }
1938
1939 /* Swizzle a small RHS vector into the channels being written.
1940 *
1941 * glsl ir treats write_mask as dictating how many channels are
1942 * present on the RHS while in our instructions we need to make
1943 * those channels appear in the slots of the vec4 they're written to.
1944 */
1945 for (int i = 0; i < 4; i++) {
1946 if (dst.writemask & (1 << i))
1947 swizzles[i] = BRW_GET_SWZ(src.swizzle, src_chan++);
1948 else
1949 swizzles[i] = first_enabled_chan;
1950 }
1951 src.swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
1952 swizzles[2], swizzles[3]);
1953
1954 if (try_rewrite_rhs_to_dst(ir, dst, src, pre_rhs_inst, last_rhs_inst)) {
1955 return;
1956 }
1957
1958 if (ir->condition) {
1959 emit_bool_to_cond_code(ir->condition, &predicate);
1960 }
1961
1962 for (i = 0; i < type_size(ir->lhs->type); i++) {
1963 vec4_instruction *inst = emit(MOV(dst, src));
1964 inst->predicate = predicate;
1965
1966 dst.reg_offset++;
1967 src.reg_offset++;
1968 }
1969 }
1970
1971 void
1972 vec4_visitor::emit_constant_values(dst_reg *dst, ir_constant *ir)
1973 {
1974 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
1975 foreach_list(node, &ir->components) {
1976 ir_constant *field_value = (ir_constant *)node;
1977
1978 emit_constant_values(dst, field_value);
1979 }
1980 return;
1981 }
1982
1983 if (ir->type->is_array()) {
1984 for (unsigned int i = 0; i < ir->type->length; i++) {
1985 emit_constant_values(dst, ir->array_elements[i]);
1986 }
1987 return;
1988 }
1989
1990 if (ir->type->is_matrix()) {
1991 for (int i = 0; i < ir->type->matrix_columns; i++) {
1992 float *vec = &ir->value.f[i * ir->type->vector_elements];
1993
1994 for (int j = 0; j < ir->type->vector_elements; j++) {
1995 dst->writemask = 1 << j;
1996 dst->type = BRW_REGISTER_TYPE_F;
1997
1998 emit(MOV(*dst, src_reg(vec[j])));
1999 }
2000 dst->reg_offset++;
2001 }
2002 return;
2003 }
2004
2005 int remaining_writemask = (1 << ir->type->vector_elements) - 1;
2006
2007 for (int i = 0; i < ir->type->vector_elements; i++) {
2008 if (!(remaining_writemask & (1 << i)))
2009 continue;
2010
2011 dst->writemask = 1 << i;
2012 dst->type = brw_type_for_base_type(ir->type);
2013
2014 /* Find other components that match the one we're about to
2015 * write. Emits fewer instructions for things like vec4(0.5,
2016 * 1.5, 1.5, 1.5).
2017 */
2018 for (int j = i + 1; j < ir->type->vector_elements; j++) {
2019 if (ir->type->base_type == GLSL_TYPE_BOOL) {
2020 if (ir->value.b[i] == ir->value.b[j])
2021 dst->writemask |= (1 << j);
2022 } else {
2023 /* u, i, and f storage all line up, so no need for a
2024 * switch case for comparing each type.
2025 */
2026 if (ir->value.u[i] == ir->value.u[j])
2027 dst->writemask |= (1 << j);
2028 }
2029 }
2030
2031 switch (ir->type->base_type) {
2032 case GLSL_TYPE_FLOAT:
2033 emit(MOV(*dst, src_reg(ir->value.f[i])));
2034 break;
2035 case GLSL_TYPE_INT:
2036 emit(MOV(*dst, src_reg(ir->value.i[i])));
2037 break;
2038 case GLSL_TYPE_UINT:
2039 emit(MOV(*dst, src_reg(ir->value.u[i])));
2040 break;
2041 case GLSL_TYPE_BOOL:
2042 emit(MOV(*dst, src_reg(ir->value.b[i])));
2043 break;
2044 default:
2045 assert(!"Non-float/uint/int/bool constant");
2046 break;
2047 }
2048
2049 remaining_writemask &= ~dst->writemask;
2050 }
2051 dst->reg_offset++;
2052 }
2053
2054 void
2055 vec4_visitor::visit(ir_constant *ir)
2056 {
2057 dst_reg dst = dst_reg(this, ir->type);
2058 this->result = src_reg(dst);
2059
2060 emit_constant_values(&dst, ir);
2061 }
2062
2063 void
2064 vec4_visitor::visit(ir_call *ir)
2065 {
2066 assert(!"not reached");
2067 }
2068
2069 void
2070 vec4_visitor::visit(ir_texture *ir)
2071 {
2072 int sampler = _mesa_get_sampler_uniform_value(ir->sampler, prog, &vp->Base);
2073
2074 /* Should be lowered by do_lower_texture_projection */
2075 assert(!ir->projector);
2076
2077 /* Generate code to compute all the subexpression trees. This has to be
2078 * done before loading any values into MRFs for the sampler message since
2079 * generating these values may involve SEND messages that need the MRFs.
2080 */
2081 src_reg coordinate;
2082 if (ir->coordinate) {
2083 ir->coordinate->accept(this);
2084 coordinate = this->result;
2085 }
2086
2087 src_reg shadow_comparitor;
2088 if (ir->shadow_comparitor) {
2089 ir->shadow_comparitor->accept(this);
2090 shadow_comparitor = this->result;
2091 }
2092
2093 const glsl_type *lod_type;
2094 src_reg lod, dPdx, dPdy;
2095 switch (ir->op) {
2096 case ir_tex:
2097 lod = src_reg(0.0f);
2098 lod_type = glsl_type::float_type;
2099 break;
2100 case ir_txf:
2101 case ir_txl:
2102 case ir_txs:
2103 ir->lod_info.lod->accept(this);
2104 lod = this->result;
2105 lod_type = ir->lod_info.lod->type;
2106 break;
2107 case ir_txd:
2108 ir->lod_info.grad.dPdx->accept(this);
2109 dPdx = this->result;
2110
2111 ir->lod_info.grad.dPdy->accept(this);
2112 dPdy = this->result;
2113
2114 lod_type = ir->lod_info.grad.dPdx->type;
2115 break;
2116 case ir_txb:
2117 break;
2118 }
2119
2120 vec4_instruction *inst = NULL;
2121 switch (ir->op) {
2122 case ir_tex:
2123 case ir_txl:
2124 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXL);
2125 break;
2126 case ir_txd:
2127 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXD);
2128 break;
2129 case ir_txf:
2130 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXF);
2131 break;
2132 case ir_txs:
2133 inst = new(mem_ctx) vec4_instruction(this, SHADER_OPCODE_TXS);
2134 break;
2135 case ir_txb:
2136 assert(!"TXB is not valid for vertex shaders.");
2137 }
2138
2139 bool use_texture_offset = ir->offset != NULL && ir->op != ir_txf;
2140
2141 /* Texel offsets go in the message header; Gen4 also requires headers. */
2142 inst->header_present = use_texture_offset || intel->gen < 5;
2143 inst->base_mrf = 2;
2144 inst->mlen = inst->header_present + 1; /* always at least one */
2145 inst->sampler = sampler;
2146 inst->dst = dst_reg(this, ir->type);
2147 inst->dst.writemask = WRITEMASK_XYZW;
2148 inst->shadow_compare = ir->shadow_comparitor != NULL;
2149
2150 if (use_texture_offset)
2151 inst->texture_offset = brw_texture_offset(ir->offset->as_constant());
2152
2153 /* MRF for the first parameter */
2154 int param_base = inst->base_mrf + inst->header_present;
2155
2156 if (ir->op == ir_txs) {
2157 int writemask = intel->gen == 4 ? WRITEMASK_W : WRITEMASK_X;
2158 emit(MOV(dst_reg(MRF, param_base, lod_type, writemask), lod));
2159 } else {
2160 int i, coord_mask = 0, zero_mask = 0;
2161 /* Load the coordinate */
2162 /* FINISHME: gl_clamp_mask and saturate */
2163 for (i = 0; i < ir->coordinate->type->vector_elements; i++)
2164 coord_mask |= (1 << i);
2165 for (; i < 4; i++)
2166 zero_mask |= (1 << i);
2167
2168 if (ir->offset && ir->op == ir_txf) {
2169 /* It appears that the ld instruction used for txf does its
2170 * address bounds check before adding in the offset. To work
2171 * around this, just add the integer offset to the integer
2172 * texel coordinate, and don't put the offset in the header.
2173 */
2174 ir_constant *offset = ir->offset->as_constant();
2175 assert(offset);
2176
2177 for (int j = 0; j < ir->coordinate->type->vector_elements; j++) {
2178 src_reg src = coordinate;
2179 src.swizzle = BRW_SWIZZLE4(BRW_GET_SWZ(src.swizzle, j),
2180 BRW_GET_SWZ(src.swizzle, j),
2181 BRW_GET_SWZ(src.swizzle, j),
2182 BRW_GET_SWZ(src.swizzle, j));
2183 emit(ADD(dst_reg(MRF, param_base, ir->coordinate->type, 1 << j),
2184 src, offset->value.i[j]));
2185 }
2186 } else {
2187 emit(MOV(dst_reg(MRF, param_base, ir->coordinate->type, coord_mask),
2188 coordinate));
2189 }
2190 emit(MOV(dst_reg(MRF, param_base, ir->coordinate->type, zero_mask),
2191 src_reg(0)));
2192 /* Load the shadow comparitor */
2193 if (ir->shadow_comparitor) {
2194 emit(MOV(dst_reg(MRF, param_base + 1, ir->shadow_comparitor->type,
2195 WRITEMASK_X),
2196 shadow_comparitor));
2197 inst->mlen++;
2198 }
2199
2200 /* Load the LOD info */
2201 if (ir->op == ir_tex || ir->op == ir_txl) {
2202 int mrf, writemask;
2203 if (intel->gen >= 5) {
2204 mrf = param_base + 1;
2205 if (ir->shadow_comparitor) {
2206 writemask = WRITEMASK_Y;
2207 /* mlen already incremented */
2208 } else {
2209 writemask = WRITEMASK_X;
2210 inst->mlen++;
2211 }
2212 } else /* intel->gen == 4 */ {
2213 mrf = param_base;
2214 writemask = WRITEMASK_Z;
2215 }
2216 emit(MOV(dst_reg(MRF, mrf, lod_type, writemask), lod));
2217 } else if (ir->op == ir_txf) {
2218 emit(MOV(dst_reg(MRF, param_base, lod_type, WRITEMASK_W),
2219 lod));
2220 } else if (ir->op == ir_txd) {
2221 const glsl_type *type = lod_type;
2222
2223 if (intel->gen >= 5) {
2224 dPdx.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2225 dPdy.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2226 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XZ), dPdx));
2227 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_YW), dPdy));
2228 inst->mlen++;
2229
2230 if (ir->type->vector_elements == 3) {
2231 dPdx.swizzle = BRW_SWIZZLE_ZZZZ;
2232 dPdy.swizzle = BRW_SWIZZLE_ZZZZ;
2233 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_X), dPdx));
2234 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_Y), dPdy));
2235 inst->mlen++;
2236 }
2237 } else /* intel->gen == 4 */ {
2238 emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XYZ), dPdx));
2239 emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_XYZ), dPdy));
2240 inst->mlen += 2;
2241 }
2242 }
2243 }
2244
2245 emit(inst);
2246
2247 /* fixup num layers (z) for cube arrays: hardware returns faces * layers;
2248 * spec requires layers.
2249 */
2250 if (ir->op == ir_txs) {
2251 glsl_type const *type = ir->sampler->type;
2252 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
2253 type->sampler_array) {
2254 emit_math(SHADER_OPCODE_INT_QUOTIENT,
2255 with_writemask(inst->dst, WRITEMASK_Z),
2256 src_reg(inst->dst), src_reg(6));
2257 }
2258 }
2259
2260 swizzle_result(ir, src_reg(inst->dst), sampler);
2261 }
2262
2263 void
2264 vec4_visitor::swizzle_result(ir_texture *ir, src_reg orig_val, int sampler)
2265 {
2266 int s = c->key.tex.swizzles[sampler];
2267
2268 this->result = src_reg(this, ir->type);
2269 dst_reg swizzled_result(this->result);
2270
2271 if (ir->op == ir_txs || ir->type == glsl_type::float_type
2272 || s == SWIZZLE_NOOP) {
2273 emit(MOV(swizzled_result, orig_val));
2274 return;
2275 }
2276
2277 int zero_mask = 0, one_mask = 0, copy_mask = 0;
2278 int swizzle[4];
2279
2280 for (int i = 0; i < 4; i++) {
2281 switch (GET_SWZ(s, i)) {
2282 case SWIZZLE_ZERO:
2283 zero_mask |= (1 << i);
2284 break;
2285 case SWIZZLE_ONE:
2286 one_mask |= (1 << i);
2287 break;
2288 default:
2289 copy_mask |= (1 << i);
2290 swizzle[i] = GET_SWZ(s, i);
2291 break;
2292 }
2293 }
2294
2295 if (copy_mask) {
2296 orig_val.swizzle = BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
2297 swizzled_result.writemask = copy_mask;
2298 emit(MOV(swizzled_result, orig_val));
2299 }
2300
2301 if (zero_mask) {
2302 swizzled_result.writemask = zero_mask;
2303 emit(MOV(swizzled_result, src_reg(0.0f)));
2304 }
2305
2306 if (one_mask) {
2307 swizzled_result.writemask = one_mask;
2308 emit(MOV(swizzled_result, src_reg(1.0f)));
2309 }
2310 }
2311
2312 void
2313 vec4_visitor::visit(ir_return *ir)
2314 {
2315 assert(!"not reached");
2316 }
2317
2318 void
2319 vec4_visitor::visit(ir_discard *ir)
2320 {
2321 assert(!"not reached");
2322 }
2323
2324 void
2325 vec4_visitor::visit(ir_if *ir)
2326 {
2327 /* Don't point the annotation at the if statement, because then it plus
2328 * the then and else blocks get printed.
2329 */
2330 this->base_ir = ir->condition;
2331
2332 if (intel->gen == 6) {
2333 emit_if_gen6(ir);
2334 } else {
2335 uint32_t predicate;
2336 emit_bool_to_cond_code(ir->condition, &predicate);
2337 emit(IF(predicate));
2338 }
2339
2340 visit_instructions(&ir->then_instructions);
2341
2342 if (!ir->else_instructions.is_empty()) {
2343 this->base_ir = ir->condition;
2344 emit(BRW_OPCODE_ELSE);
2345
2346 visit_instructions(&ir->else_instructions);
2347 }
2348
2349 this->base_ir = ir->condition;
2350 emit(BRW_OPCODE_ENDIF);
2351 }
2352
2353 void
2354 vec4_visitor::emit_ndc_computation()
2355 {
2356 /* Get the position */
2357 src_reg pos = src_reg(output_reg[VERT_RESULT_HPOS]);
2358
2359 /* Build ndc coords, which are (x/w, y/w, z/w, 1/w) */
2360 dst_reg ndc = dst_reg(this, glsl_type::vec4_type);
2361 output_reg[BRW_VERT_RESULT_NDC] = ndc;
2362
2363 current_annotation = "NDC";
2364 dst_reg ndc_w = ndc;
2365 ndc_w.writemask = WRITEMASK_W;
2366 src_reg pos_w = pos;
2367 pos_w.swizzle = BRW_SWIZZLE4(SWIZZLE_W, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W);
2368 emit_math(SHADER_OPCODE_RCP, ndc_w, pos_w);
2369
2370 dst_reg ndc_xyz = ndc;
2371 ndc_xyz.writemask = WRITEMASK_XYZ;
2372
2373 emit(MUL(ndc_xyz, pos, src_reg(ndc_w)));
2374 }
2375
2376 void
2377 vec4_visitor::emit_psiz_and_flags(struct brw_reg reg)
2378 {
2379 if (intel->gen < 6 &&
2380 ((c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) ||
2381 c->key.userclip_active || brw->has_negative_rhw_bug)) {
2382 dst_reg header1 = dst_reg(this, glsl_type::uvec4_type);
2383 dst_reg header1_w = header1;
2384 header1_w.writemask = WRITEMASK_W;
2385 GLuint i;
2386
2387 emit(MOV(header1, 0u));
2388
2389 if (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
2390 src_reg psiz = src_reg(output_reg[VERT_RESULT_PSIZ]);
2391
2392 current_annotation = "Point size";
2393 emit(MUL(header1_w, psiz, src_reg((float)(1 << 11))));
2394 emit(AND(header1_w, src_reg(header1_w), 0x7ff << 8));
2395 }
2396
2397 current_annotation = "Clipping flags";
2398 for (i = 0; i < c->key.nr_userclip_plane_consts; i++) {
2399 vec4_instruction *inst;
2400
2401 inst = emit(DP4(dst_null_f(), src_reg(output_reg[VERT_RESULT_HPOS]),
2402 src_reg(this->userplane[i])));
2403 inst->conditional_mod = BRW_CONDITIONAL_L;
2404
2405 inst = emit(OR(header1_w, src_reg(header1_w), 1u << i));
2406 inst->predicate = BRW_PREDICATE_NORMAL;
2407 }
2408
2409 /* i965 clipping workaround:
2410 * 1) Test for -ve rhw
2411 * 2) If set,
2412 * set ndc = (0,0,0,0)
2413 * set ucp[6] = 1
2414 *
2415 * Later, clipping will detect ucp[6] and ensure the primitive is
2416 * clipped against all fixed planes.
2417 */
2418 if (brw->has_negative_rhw_bug) {
2419 #if 0
2420 /* FINISHME */
2421 brw_CMP(p,
2422 vec8(brw_null_reg()),
2423 BRW_CONDITIONAL_L,
2424 brw_swizzle1(output_reg[BRW_VERT_RESULT_NDC], 3),
2425 brw_imm_f(0));
2426
2427 brw_OR(p, brw_writemask(header1, WRITEMASK_W), header1, brw_imm_ud(1<<6));
2428 brw_MOV(p, output_reg[BRW_VERT_RESULT_NDC], brw_imm_f(0));
2429 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
2430 #endif
2431 }
2432
2433 emit(MOV(retype(reg, BRW_REGISTER_TYPE_UD), src_reg(header1)));
2434 } else if (intel->gen < 6) {
2435 emit(MOV(retype(reg, BRW_REGISTER_TYPE_UD), 0u));
2436 } else {
2437 emit(MOV(retype(reg, BRW_REGISTER_TYPE_D), src_reg(0)));
2438 if (c->prog_data.outputs_written & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
2439 emit(MOV(brw_writemask(reg, WRITEMASK_W),
2440 src_reg(output_reg[VERT_RESULT_PSIZ])));
2441 }
2442 }
2443 }
2444
2445 void
2446 vec4_visitor::emit_clip_distances(struct brw_reg reg, int offset)
2447 {
2448 if (intel->gen < 6) {
2449 /* Clip distance slots are set aside in gen5, but they are not used. It
2450 * is not clear whether we actually need to set aside space for them,
2451 * but the performance cost is negligible.
2452 */
2453 return;
2454 }
2455
2456 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
2457 *
2458 * "If a linked set of shaders forming the vertex stage contains no
2459 * static write to gl_ClipVertex or gl_ClipDistance, but the
2460 * application has requested clipping against user clip planes through
2461 * the API, then the coordinate written to gl_Position is used for
2462 * comparison against the user clip planes."
2463 *
2464 * This function is only called if the shader didn't write to
2465 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
2466 * if the user wrote to it; otherwise we use gl_Position.
2467 */
2468 gl_vert_result clip_vertex = VERT_RESULT_CLIP_VERTEX;
2469 if (!(c->prog_data.outputs_written
2470 & BITFIELD64_BIT(VERT_RESULT_CLIP_VERTEX))) {
2471 clip_vertex = VERT_RESULT_HPOS;
2472 }
2473
2474 for (int i = 0; i + offset < c->key.nr_userclip_plane_consts && i < 4;
2475 ++i) {
2476 emit(DP4(dst_reg(brw_writemask(reg, 1 << i)),
2477 src_reg(output_reg[clip_vertex]),
2478 src_reg(this->userplane[i + offset])));
2479 }
2480 }
2481
2482 void
2483 vec4_visitor::emit_generic_urb_slot(dst_reg reg, int vert_result)
2484 {
2485 assert (vert_result < VERT_RESULT_MAX);
2486 reg.type = output_reg[vert_result].type;
2487 current_annotation = output_reg_annotation[vert_result];
2488 /* Copy the register, saturating if necessary */
2489 vec4_instruction *inst = emit(MOV(reg,
2490 src_reg(output_reg[vert_result])));
2491 if ((vert_result == VERT_RESULT_COL0 ||
2492 vert_result == VERT_RESULT_COL1 ||
2493 vert_result == VERT_RESULT_BFC0 ||
2494 vert_result == VERT_RESULT_BFC1) &&
2495 c->key.clamp_vertex_color) {
2496 inst->saturate = true;
2497 }
2498 }
2499
2500 void
2501 vec4_visitor::emit_urb_slot(int mrf, int vert_result)
2502 {
2503 struct brw_reg hw_reg = brw_message_reg(mrf);
2504 dst_reg reg = dst_reg(MRF, mrf);
2505 reg.type = BRW_REGISTER_TYPE_F;
2506
2507 switch (vert_result) {
2508 case VERT_RESULT_PSIZ:
2509 /* PSIZ is always in slot 0, and is coupled with other flags. */
2510 current_annotation = "indices, point width, clip flags";
2511 emit_psiz_and_flags(hw_reg);
2512 break;
2513 case BRW_VERT_RESULT_NDC:
2514 current_annotation = "NDC";
2515 emit(MOV(reg, src_reg(output_reg[BRW_VERT_RESULT_NDC])));
2516 break;
2517 case BRW_VERT_RESULT_HPOS_DUPLICATE:
2518 case VERT_RESULT_HPOS:
2519 current_annotation = "gl_Position";
2520 emit(MOV(reg, src_reg(output_reg[VERT_RESULT_HPOS])));
2521 break;
2522 case VERT_RESULT_CLIP_DIST0:
2523 case VERT_RESULT_CLIP_DIST1:
2524 if (this->c->key.uses_clip_distance) {
2525 emit_generic_urb_slot(reg, vert_result);
2526 } else {
2527 current_annotation = "user clip distances";
2528 emit_clip_distances(hw_reg, (vert_result - VERT_RESULT_CLIP_DIST0) * 4);
2529 }
2530 break;
2531 case VERT_RESULT_EDGE:
2532 /* This is present when doing unfilled polygons. We're supposed to copy
2533 * the edge flag from the user-provided vertex array
2534 * (glEdgeFlagPointer), or otherwise we'll copy from the current value
2535 * of that attribute (starts as 1.0f). This is then used in clipping to
2536 * determine which edges should be drawn as wireframe.
2537 */
2538 current_annotation = "edge flag";
2539 emit(MOV(reg, src_reg(dst_reg(ATTR, VERT_ATTRIB_EDGEFLAG,
2540 glsl_type::float_type, WRITEMASK_XYZW))));
2541 break;
2542 case BRW_VERT_RESULT_PAD:
2543 /* No need to write to this slot */
2544 break;
2545 default:
2546 emit_generic_urb_slot(reg, vert_result);
2547 break;
2548 }
2549 }
2550
2551 static int
2552 align_interleaved_urb_mlen(struct brw_context *brw, int mlen)
2553 {
2554 struct intel_context *intel = &brw->intel;
2555
2556 if (intel->gen >= 6) {
2557 /* URB data written (does not include the message header reg) must
2558 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
2559 * section 5.4.3.2.2: URB_INTERLEAVED.
2560 *
2561 * URB entries are allocated on a multiple of 1024 bits, so an
2562 * extra 128 bits written here to make the end align to 256 is
2563 * no problem.
2564 */
2565 if ((mlen % 2) != 1)
2566 mlen++;
2567 }
2568
2569 return mlen;
2570 }
2571
2572 /**
2573 * Generates the VUE payload plus the 1 or 2 URB write instructions to
2574 * complete the VS thread.
2575 *
2576 * The VUE layout is documented in Volume 2a.
2577 */
2578 void
2579 vec4_visitor::emit_urb_writes()
2580 {
2581 /* MRF 0 is reserved for the debugger, so start with message header
2582 * in MRF 1.
2583 */
2584 int base_mrf = 1;
2585 int mrf = base_mrf;
2586 /* In the process of generating our URB write message contents, we
2587 * may need to unspill a register or load from an array. Those
2588 * reads would use MRFs 14-15.
2589 */
2590 int max_usable_mrf = 13;
2591
2592 /* The following assertion verifies that max_usable_mrf causes an
2593 * even-numbered amount of URB write data, which will meet gen6's
2594 * requirements for length alignment.
2595 */
2596 assert ((max_usable_mrf - base_mrf) % 2 == 0);
2597
2598 /* First mrf is the g0-based message header containing URB handles and such,
2599 * which is implied in VS_OPCODE_URB_WRITE.
2600 */
2601 mrf++;
2602
2603 if (intel->gen < 6) {
2604 emit_ndc_computation();
2605 }
2606
2607 /* Set up the VUE data for the first URB write */
2608 int slot;
2609 for (slot = 0; slot < c->prog_data.vue_map.num_slots; ++slot) {
2610 emit_urb_slot(mrf++, c->prog_data.vue_map.slot_to_vert_result[slot]);
2611
2612 /* If this was max_usable_mrf, we can't fit anything more into this URB
2613 * WRITE.
2614 */
2615 if (mrf > max_usable_mrf) {
2616 slot++;
2617 break;
2618 }
2619 }
2620
2621 current_annotation = "URB write";
2622 vec4_instruction *inst = emit(VS_OPCODE_URB_WRITE);
2623 inst->base_mrf = base_mrf;
2624 inst->mlen = align_interleaved_urb_mlen(brw, mrf - base_mrf);
2625 inst->eot = (slot >= c->prog_data.vue_map.num_slots);
2626
2627 /* Optional second URB write */
2628 if (!inst->eot) {
2629 mrf = base_mrf + 1;
2630
2631 for (; slot < c->prog_data.vue_map.num_slots; ++slot) {
2632 assert(mrf < max_usable_mrf);
2633
2634 emit_urb_slot(mrf++, c->prog_data.vue_map.slot_to_vert_result[slot]);
2635 }
2636
2637 current_annotation = "URB write";
2638 inst = emit(VS_OPCODE_URB_WRITE);
2639 inst->base_mrf = base_mrf;
2640 inst->mlen = align_interleaved_urb_mlen(brw, mrf - base_mrf);
2641 inst->eot = true;
2642 /* URB destination offset. In the previous write, we got MRFs
2643 * 2-13 minus the one header MRF, so 12 regs. URB offset is in
2644 * URB row increments, and each of our MRFs is half of one of
2645 * those, since we're doing interleaved writes.
2646 */
2647 inst->offset = (max_usable_mrf - base_mrf) / 2;
2648 }
2649 }
2650
2651 src_reg
2652 vec4_visitor::get_scratch_offset(vec4_instruction *inst,
2653 src_reg *reladdr, int reg_offset)
2654 {
2655 /* Because we store the values to scratch interleaved like our
2656 * vertex data, we need to scale the vec4 index by 2.
2657 */
2658 int message_header_scale = 2;
2659
2660 /* Pre-gen6, the message header uses byte offsets instead of vec4
2661 * (16-byte) offset units.
2662 */
2663 if (intel->gen < 6)
2664 message_header_scale *= 16;
2665
2666 if (reladdr) {
2667 src_reg index = src_reg(this, glsl_type::int_type);
2668
2669 emit_before(inst, ADD(dst_reg(index), *reladdr, src_reg(reg_offset)));
2670 emit_before(inst, MUL(dst_reg(index),
2671 index, src_reg(message_header_scale)));
2672
2673 return index;
2674 } else {
2675 return src_reg(reg_offset * message_header_scale);
2676 }
2677 }
2678
2679 src_reg
2680 vec4_visitor::get_pull_constant_offset(vec4_instruction *inst,
2681 src_reg *reladdr, int reg_offset)
2682 {
2683 if (reladdr) {
2684 src_reg index = src_reg(this, glsl_type::int_type);
2685
2686 emit_before(inst, ADD(dst_reg(index), *reladdr, src_reg(reg_offset)));
2687
2688 /* Pre-gen6, the message header uses byte offsets instead of vec4
2689 * (16-byte) offset units.
2690 */
2691 if (intel->gen < 6) {
2692 emit_before(inst, MUL(dst_reg(index), index, src_reg(16)));
2693 }
2694
2695 return index;
2696 } else {
2697 int message_header_scale = intel->gen < 6 ? 16 : 1;
2698 return src_reg(reg_offset * message_header_scale);
2699 }
2700 }
2701
2702 /**
2703 * Emits an instruction before @inst to load the value named by @orig_src
2704 * from scratch space at @base_offset to @temp.
2705 *
2706 * @base_offset is measured in 32-byte units (the size of a register).
2707 */
2708 void
2709 vec4_visitor::emit_scratch_read(vec4_instruction *inst,
2710 dst_reg temp, src_reg orig_src,
2711 int base_offset)
2712 {
2713 int reg_offset = base_offset + orig_src.reg_offset;
2714 src_reg index = get_scratch_offset(inst, orig_src.reladdr, reg_offset);
2715
2716 emit_before(inst, SCRATCH_READ(temp, index));
2717 }
2718
2719 /**
2720 * Emits an instruction after @inst to store the value to be written
2721 * to @orig_dst to scratch space at @base_offset, from @temp.
2722 *
2723 * @base_offset is measured in 32-byte units (the size of a register).
2724 */
2725 void
2726 vec4_visitor::emit_scratch_write(vec4_instruction *inst, int base_offset)
2727 {
2728 int reg_offset = base_offset + inst->dst.reg_offset;
2729 src_reg index = get_scratch_offset(inst, inst->dst.reladdr, reg_offset);
2730
2731 /* Create a temporary register to store *inst's result in.
2732 *
2733 * We have to be careful in MOVing from our temporary result register in
2734 * the scratch write. If we swizzle from channels of the temporary that
2735 * weren't initialized, it will confuse live interval analysis, which will
2736 * make spilling fail to make progress.
2737 */
2738 src_reg temp = src_reg(this, glsl_type::vec4_type);
2739 temp.type = inst->dst.type;
2740 int first_writemask_chan = ffs(inst->dst.writemask) - 1;
2741 int swizzles[4];
2742 for (int i = 0; i < 4; i++)
2743 if (inst->dst.writemask & (1 << i))
2744 swizzles[i] = i;
2745 else
2746 swizzles[i] = first_writemask_chan;
2747 temp.swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
2748 swizzles[2], swizzles[3]);
2749
2750 dst_reg dst = dst_reg(brw_writemask(brw_vec8_grf(0, 0),
2751 inst->dst.writemask));
2752 vec4_instruction *write = SCRATCH_WRITE(dst, temp, index);
2753 write->predicate = inst->predicate;
2754 write->ir = inst->ir;
2755 write->annotation = inst->annotation;
2756 inst->insert_after(write);
2757
2758 inst->dst.file = temp.file;
2759 inst->dst.reg = temp.reg;
2760 inst->dst.reg_offset = temp.reg_offset;
2761 inst->dst.reladdr = NULL;
2762 }
2763
2764 /**
2765 * We can't generally support array access in GRF space, because a
2766 * single instruction's destination can only span 2 contiguous
2767 * registers. So, we send all GRF arrays that get variable index
2768 * access to scratch space.
2769 */
2770 void
2771 vec4_visitor::move_grf_array_access_to_scratch()
2772 {
2773 int scratch_loc[this->virtual_grf_count];
2774
2775 for (int i = 0; i < this->virtual_grf_count; i++) {
2776 scratch_loc[i] = -1;
2777 }
2778
2779 /* First, calculate the set of virtual GRFs that need to be punted
2780 * to scratch due to having any array access on them, and where in
2781 * scratch.
2782 */
2783 foreach_list(node, &this->instructions) {
2784 vec4_instruction *inst = (vec4_instruction *)node;
2785
2786 if (inst->dst.file == GRF && inst->dst.reladdr &&
2787 scratch_loc[inst->dst.reg] == -1) {
2788 scratch_loc[inst->dst.reg] = c->last_scratch;
2789 c->last_scratch += this->virtual_grf_sizes[inst->dst.reg];
2790 }
2791
2792 for (int i = 0 ; i < 3; i++) {
2793 src_reg *src = &inst->src[i];
2794
2795 if (src->file == GRF && src->reladdr &&
2796 scratch_loc[src->reg] == -1) {
2797 scratch_loc[src->reg] = c->last_scratch;
2798 c->last_scratch += this->virtual_grf_sizes[src->reg];
2799 }
2800 }
2801 }
2802
2803 /* Now, for anything that will be accessed through scratch, rewrite
2804 * it to load/store. Note that this is a _safe list walk, because
2805 * we may generate a new scratch_write instruction after the one
2806 * we're processing.
2807 */
2808 foreach_list_safe(node, &this->instructions) {
2809 vec4_instruction *inst = (vec4_instruction *)node;
2810
2811 /* Set up the annotation tracking for new generated instructions. */
2812 base_ir = inst->ir;
2813 current_annotation = inst->annotation;
2814
2815 if (inst->dst.file == GRF && scratch_loc[inst->dst.reg] != -1) {
2816 emit_scratch_write(inst, scratch_loc[inst->dst.reg]);
2817 }
2818
2819 for (int i = 0 ; i < 3; i++) {
2820 if (inst->src[i].file != GRF || scratch_loc[inst->src[i].reg] == -1)
2821 continue;
2822
2823 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
2824
2825 emit_scratch_read(inst, temp, inst->src[i],
2826 scratch_loc[inst->src[i].reg]);
2827
2828 inst->src[i].file = temp.file;
2829 inst->src[i].reg = temp.reg;
2830 inst->src[i].reg_offset = temp.reg_offset;
2831 inst->src[i].reladdr = NULL;
2832 }
2833 }
2834 }
2835
2836 /**
2837 * Emits an instruction before @inst to load the value named by @orig_src
2838 * from the pull constant buffer (surface) at @base_offset to @temp.
2839 */
2840 void
2841 vec4_visitor::emit_pull_constant_load(vec4_instruction *inst,
2842 dst_reg temp, src_reg orig_src,
2843 int base_offset)
2844 {
2845 int reg_offset = base_offset + orig_src.reg_offset;
2846 src_reg index = src_reg((unsigned)SURF_INDEX_VERT_CONST_BUFFER);
2847 src_reg offset = get_pull_constant_offset(inst, orig_src.reladdr, reg_offset);
2848 vec4_instruction *load;
2849
2850 load = new(mem_ctx) vec4_instruction(this, VS_OPCODE_PULL_CONSTANT_LOAD,
2851 temp, index, offset);
2852 load->base_mrf = 14;
2853 load->mlen = 1;
2854 emit_before(inst, load);
2855 }
2856
2857 /**
2858 * Implements array access of uniforms by inserting a
2859 * PULL_CONSTANT_LOAD instruction.
2860 *
2861 * Unlike temporary GRF array access (where we don't support it due to
2862 * the difficulty of doing relative addressing on instruction
2863 * destinations), we could potentially do array access of uniforms
2864 * that were loaded in GRF space as push constants. In real-world
2865 * usage we've seen, though, the arrays being used are always larger
2866 * than we could load as push constants, so just always move all
2867 * uniform array access out to a pull constant buffer.
2868 */
2869 void
2870 vec4_visitor::move_uniform_array_access_to_pull_constants()
2871 {
2872 int pull_constant_loc[this->uniforms];
2873
2874 for (int i = 0; i < this->uniforms; i++) {
2875 pull_constant_loc[i] = -1;
2876 }
2877
2878 /* Walk through and find array access of uniforms. Put a copy of that
2879 * uniform in the pull constant buffer.
2880 *
2881 * Note that we don't move constant-indexed accesses to arrays. No
2882 * testing has been done of the performance impact of this choice.
2883 */
2884 foreach_list_safe(node, &this->instructions) {
2885 vec4_instruction *inst = (vec4_instruction *)node;
2886
2887 for (int i = 0 ; i < 3; i++) {
2888 if (inst->src[i].file != UNIFORM || !inst->src[i].reladdr)
2889 continue;
2890
2891 int uniform = inst->src[i].reg;
2892
2893 /* If this array isn't already present in the pull constant buffer,
2894 * add it.
2895 */
2896 if (pull_constant_loc[uniform] == -1) {
2897 const float **values = &prog_data->param[uniform * 4];
2898
2899 pull_constant_loc[uniform] = prog_data->nr_pull_params / 4;
2900
2901 for (int j = 0; j < uniform_size[uniform] * 4; j++) {
2902 prog_data->pull_param[prog_data->nr_pull_params++] = values[j];
2903 }
2904 }
2905
2906 /* Set up the annotation tracking for new generated instructions. */
2907 base_ir = inst->ir;
2908 current_annotation = inst->annotation;
2909
2910 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
2911
2912 emit_pull_constant_load(inst, temp, inst->src[i],
2913 pull_constant_loc[uniform]);
2914
2915 inst->src[i].file = temp.file;
2916 inst->src[i].reg = temp.reg;
2917 inst->src[i].reg_offset = temp.reg_offset;
2918 inst->src[i].reladdr = NULL;
2919 }
2920 }
2921
2922 /* Now there are no accesses of the UNIFORM file with a reladdr, so
2923 * no need to track them as larger-than-vec4 objects. This will be
2924 * relied on in cutting out unused uniform vectors from push
2925 * constants.
2926 */
2927 split_uniform_registers();
2928 }
2929
2930 void
2931 vec4_visitor::resolve_ud_negate(src_reg *reg)
2932 {
2933 if (reg->type != BRW_REGISTER_TYPE_UD ||
2934 !reg->negate)
2935 return;
2936
2937 src_reg temp = src_reg(this, glsl_type::uvec4_type);
2938 emit(BRW_OPCODE_MOV, dst_reg(temp), *reg);
2939 *reg = temp;
2940 }
2941
2942 vec4_visitor::vec4_visitor(struct brw_context *brw,
2943 struct brw_vs_compile *c,
2944 struct gl_shader_program *prog,
2945 struct brw_shader *shader,
2946 void *mem_ctx)
2947 {
2948 this->c = c;
2949 this->brw = brw;
2950 this->intel = &brw->intel;
2951 this->ctx = &intel->ctx;
2952 this->prog = prog;
2953 this->shader = shader;
2954
2955 this->mem_ctx = mem_ctx;
2956 this->failed = false;
2957
2958 this->base_ir = NULL;
2959 this->current_annotation = NULL;
2960 memset(this->output_reg_annotation, 0, sizeof(this->output_reg_annotation));
2961
2962 this->c = c;
2963 this->vp = &c->vp->program;
2964 this->prog_data = &c->prog_data;
2965
2966 this->variable_ht = hash_table_ctor(0,
2967 hash_table_pointer_hash,
2968 hash_table_pointer_compare);
2969
2970 this->virtual_grf_def = NULL;
2971 this->virtual_grf_use = NULL;
2972 this->virtual_grf_sizes = NULL;
2973 this->virtual_grf_count = 0;
2974 this->virtual_grf_reg_map = NULL;
2975 this->virtual_grf_reg_count = 0;
2976 this->virtual_grf_array_size = 0;
2977 this->live_intervals_valid = false;
2978
2979 this->max_grf = intel->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
2980
2981 this->uniforms = 0;
2982 }
2983
2984 vec4_visitor::~vec4_visitor()
2985 {
2986 hash_table_dtor(this->variable_ht);
2987 }
2988
2989
2990 void
2991 vec4_visitor::fail(const char *format, ...)
2992 {
2993 va_list va;
2994 char *msg;
2995
2996 if (failed)
2997 return;
2998
2999 failed = true;
3000
3001 va_start(va, format);
3002 msg = ralloc_vasprintf(mem_ctx, format, va);
3003 va_end(va);
3004 msg = ralloc_asprintf(mem_ctx, "VS compile failed: %s\n", msg);
3005
3006 this->fail_msg = msg;
3007
3008 if (INTEL_DEBUG & DEBUG_VS) {
3009 fprintf(stderr, "%s", msg);
3010 }
3011 }
3012
3013 } /* namespace brw */