e5b2e1758c08dab35cd1aababeca4dbb71e0a0ce
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs.cpp
1 /*
2 * Copyright © 2010 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 extern "C" {
29
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "main/uniforms.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "program/prog_optimize.h"
38 #include "program/register_allocate.h"
39 #include "program/sampler.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 }
45 #include "brw_fs.h"
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir_optimization.h"
48 #include "../glsl/ir_print_visitor.h"
49
50 #define MAX_INSTRUCTION (1 << 30)
51 static struct brw_reg brw_reg_from_fs_reg(class fs_reg *reg);
52
53 struct gl_shader *
54 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
55 {
56 struct brw_shader *shader;
57
58 shader = talloc_zero(NULL, struct brw_shader);
59 if (shader) {
60 shader->base.Type = type;
61 shader->base.Name = name;
62 _mesa_init_shader(ctx, &shader->base);
63 }
64
65 return &shader->base;
66 }
67
68 struct gl_shader_program *
69 brw_new_shader_program(struct gl_context *ctx, GLuint name)
70 {
71 struct brw_shader_program *prog;
72 prog = talloc_zero(NULL, struct brw_shader_program);
73 if (prog) {
74 prog->base.Name = name;
75 _mesa_init_shader_program(ctx, &prog->base);
76 }
77 return &prog->base;
78 }
79
80 GLboolean
81 brw_compile_shader(struct gl_context *ctx, struct gl_shader *shader)
82 {
83 if (!_mesa_ir_compile_shader(ctx, shader))
84 return GL_FALSE;
85
86 return GL_TRUE;
87 }
88
89 GLboolean
90 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
91 {
92 struct brw_context *brw = brw_context(ctx);
93 struct intel_context *intel = &brw->intel;
94
95 struct brw_shader *shader =
96 (struct brw_shader *)prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
97 if (shader != NULL) {
98 void *mem_ctx = talloc_new(NULL);
99 bool progress;
100
101 if (shader->ir)
102 talloc_free(shader->ir);
103 shader->ir = new(shader) exec_list;
104 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
105
106 do_mat_op_to_vec(shader->ir);
107 lower_instructions(shader->ir,
108 MOD_TO_FRACT |
109 DIV_TO_MUL_RCP |
110 SUB_TO_ADD_NEG |
111 EXP_TO_EXP2 |
112 LOG_TO_LOG2);
113
114 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
115 * if-statements need to be flattened.
116 */
117 if (intel->gen < 6)
118 lower_if_to_cond_assign(shader->ir, 16);
119
120 do_lower_texture_projection(shader->ir);
121 do_vec_index_to_cond_assign(shader->ir);
122 brw_do_cubemap_normalize(shader->ir);
123
124 do {
125 progress = false;
126
127 brw_do_channel_expressions(shader->ir);
128 brw_do_vector_splitting(shader->ir);
129
130 progress = do_lower_jumps(shader->ir, true, true,
131 true, /* main return */
132 false, /* continue */
133 false /* loops */
134 ) || progress;
135
136 progress = do_common_optimization(shader->ir, true, 32) || progress;
137
138 progress = lower_noise(shader->ir) || progress;
139 progress =
140 lower_variable_index_to_cond_assign(shader->ir,
141 GL_TRUE, /* input */
142 GL_TRUE, /* output */
143 GL_TRUE, /* temp */
144 GL_TRUE /* uniform */
145 ) || progress;
146 progress = lower_quadop_vector(shader->ir, false) || progress;
147 } while (progress);
148
149 validate_ir_tree(shader->ir);
150
151 reparent_ir(shader->ir, shader->ir);
152 talloc_free(mem_ctx);
153 }
154
155 if (!_mesa_ir_link_shader(ctx, prog))
156 return GL_FALSE;
157
158 return GL_TRUE;
159 }
160
161 static int
162 type_size(const struct glsl_type *type)
163 {
164 unsigned int size, i;
165
166 switch (type->base_type) {
167 case GLSL_TYPE_UINT:
168 case GLSL_TYPE_INT:
169 case GLSL_TYPE_FLOAT:
170 case GLSL_TYPE_BOOL:
171 return type->components();
172 case GLSL_TYPE_ARRAY:
173 return type_size(type->fields.array) * type->length;
174 case GLSL_TYPE_STRUCT:
175 size = 0;
176 for (i = 0; i < type->length; i++) {
177 size += type_size(type->fields.structure[i].type);
178 }
179 return size;
180 case GLSL_TYPE_SAMPLER:
181 /* Samplers take up no register space, since they're baked in at
182 * link time.
183 */
184 return 0;
185 default:
186 assert(!"not reached");
187 return 0;
188 }
189 }
190
191 /**
192 * Returns how many MRFs an FS opcode will write over.
193 *
194 * Note that this is not the 0 or 1 implied writes in an actual gen
195 * instruction -- the FS opcodes often generate MOVs in addition.
196 */
197 int
198 fs_visitor::implied_mrf_writes(fs_inst *inst)
199 {
200 if (inst->mlen == 0)
201 return 0;
202
203 switch (inst->opcode) {
204 case FS_OPCODE_RCP:
205 case FS_OPCODE_RSQ:
206 case FS_OPCODE_SQRT:
207 case FS_OPCODE_EXP2:
208 case FS_OPCODE_LOG2:
209 case FS_OPCODE_SIN:
210 case FS_OPCODE_COS:
211 return 1;
212 case FS_OPCODE_POW:
213 return 2;
214 case FS_OPCODE_TEX:
215 case FS_OPCODE_TXB:
216 case FS_OPCODE_TXL:
217 return 1;
218 case FS_OPCODE_FB_WRITE:
219 return 2;
220 case FS_OPCODE_PULL_CONSTANT_LOAD:
221 case FS_OPCODE_UNSPILL:
222 return 1;
223 case FS_OPCODE_SPILL:
224 return 2;
225 default:
226 assert(!"not reached");
227 return inst->mlen;
228 }
229 }
230
231 int
232 fs_visitor::virtual_grf_alloc(int size)
233 {
234 if (virtual_grf_array_size <= virtual_grf_next) {
235 if (virtual_grf_array_size == 0)
236 virtual_grf_array_size = 16;
237 else
238 virtual_grf_array_size *= 2;
239 virtual_grf_sizes = talloc_realloc(mem_ctx, virtual_grf_sizes,
240 int, virtual_grf_array_size);
241
242 /* This slot is always unused. */
243 virtual_grf_sizes[0] = 0;
244 }
245 virtual_grf_sizes[virtual_grf_next] = size;
246 return virtual_grf_next++;
247 }
248
249 /** Fixed HW reg constructor. */
250 fs_reg::fs_reg(enum register_file file, int hw_reg)
251 {
252 init();
253 this->file = file;
254 this->hw_reg = hw_reg;
255 this->type = BRW_REGISTER_TYPE_F;
256 }
257
258 /** Fixed HW reg constructor. */
259 fs_reg::fs_reg(enum register_file file, int hw_reg, uint32_t type)
260 {
261 init();
262 this->file = file;
263 this->hw_reg = hw_reg;
264 this->type = type;
265 }
266
267 int
268 brw_type_for_base_type(const struct glsl_type *type)
269 {
270 switch (type->base_type) {
271 case GLSL_TYPE_FLOAT:
272 return BRW_REGISTER_TYPE_F;
273 case GLSL_TYPE_INT:
274 case GLSL_TYPE_BOOL:
275 return BRW_REGISTER_TYPE_D;
276 case GLSL_TYPE_UINT:
277 return BRW_REGISTER_TYPE_UD;
278 case GLSL_TYPE_ARRAY:
279 case GLSL_TYPE_STRUCT:
280 case GLSL_TYPE_SAMPLER:
281 /* These should be overridden with the type of the member when
282 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
283 * way to trip up if we don't.
284 */
285 return BRW_REGISTER_TYPE_UD;
286 default:
287 assert(!"not reached");
288 return BRW_REGISTER_TYPE_F;
289 }
290 }
291
292 /** Automatic reg constructor. */
293 fs_reg::fs_reg(class fs_visitor *v, const struct glsl_type *type)
294 {
295 init();
296
297 this->file = GRF;
298 this->reg = v->virtual_grf_alloc(type_size(type));
299 this->reg_offset = 0;
300 this->type = brw_type_for_base_type(type);
301 }
302
303 fs_reg *
304 fs_visitor::variable_storage(ir_variable *var)
305 {
306 return (fs_reg *)hash_table_find(this->variable_ht, var);
307 }
308
309 /* Our support for uniforms is piggy-backed on the struct
310 * gl_fragment_program, because that's where the values actually
311 * get stored, rather than in some global gl_shader_program uniform
312 * store.
313 */
314 int
315 fs_visitor::setup_uniform_values(int loc, const glsl_type *type)
316 {
317 unsigned int offset = 0;
318
319 if (type->is_matrix()) {
320 const glsl_type *column = glsl_type::get_instance(GLSL_TYPE_FLOAT,
321 type->vector_elements,
322 1);
323
324 for (unsigned int i = 0; i < type->matrix_columns; i++) {
325 offset += setup_uniform_values(loc + offset, column);
326 }
327
328 return offset;
329 }
330
331 switch (type->base_type) {
332 case GLSL_TYPE_FLOAT:
333 case GLSL_TYPE_UINT:
334 case GLSL_TYPE_INT:
335 case GLSL_TYPE_BOOL:
336 for (unsigned int i = 0; i < type->vector_elements; i++) {
337 unsigned int param = c->prog_data.nr_params++;
338
339 assert(param < ARRAY_SIZE(c->prog_data.param));
340
341 switch (type->base_type) {
342 case GLSL_TYPE_FLOAT:
343 c->prog_data.param_convert[param] = PARAM_NO_CONVERT;
344 break;
345 case GLSL_TYPE_UINT:
346 c->prog_data.param_convert[param] = PARAM_CONVERT_F2U;
347 break;
348 case GLSL_TYPE_INT:
349 c->prog_data.param_convert[param] = PARAM_CONVERT_F2I;
350 break;
351 case GLSL_TYPE_BOOL:
352 c->prog_data.param_convert[param] = PARAM_CONVERT_F2B;
353 break;
354 default:
355 assert(!"not reached");
356 c->prog_data.param_convert[param] = PARAM_NO_CONVERT;
357 break;
358 }
359 this->param_index[param] = loc;
360 this->param_offset[param] = i;
361 }
362 return 1;
363
364 case GLSL_TYPE_STRUCT:
365 for (unsigned int i = 0; i < type->length; i++) {
366 offset += setup_uniform_values(loc + offset,
367 type->fields.structure[i].type);
368 }
369 return offset;
370
371 case GLSL_TYPE_ARRAY:
372 for (unsigned int i = 0; i < type->length; i++) {
373 offset += setup_uniform_values(loc + offset, type->fields.array);
374 }
375 return offset;
376
377 case GLSL_TYPE_SAMPLER:
378 /* The sampler takes up a slot, but we don't use any values from it. */
379 return 1;
380
381 default:
382 assert(!"not reached");
383 return 0;
384 }
385 }
386
387
388 /* Our support for builtin uniforms is even scarier than non-builtin.
389 * It sits on top of the PROG_STATE_VAR parameters that are
390 * automatically updated from GL context state.
391 */
392 void
393 fs_visitor::setup_builtin_uniform_values(ir_variable *ir)
394 {
395 const struct gl_builtin_uniform_desc *statevar = NULL;
396
397 for (unsigned int i = 0; _mesa_builtin_uniform_desc[i].name; i++) {
398 statevar = &_mesa_builtin_uniform_desc[i];
399 if (strcmp(ir->name, _mesa_builtin_uniform_desc[i].name) == 0)
400 break;
401 }
402
403 if (!statevar->name) {
404 this->fail = true;
405 printf("Failed to find builtin uniform `%s'\n", ir->name);
406 return;
407 }
408
409 int array_count;
410 if (ir->type->is_array()) {
411 array_count = ir->type->length;
412 } else {
413 array_count = 1;
414 }
415
416 for (int a = 0; a < array_count; a++) {
417 for (unsigned int i = 0; i < statevar->num_elements; i++) {
418 struct gl_builtin_uniform_element *element = &statevar->elements[i];
419 int tokens[STATE_LENGTH];
420
421 memcpy(tokens, element->tokens, sizeof(element->tokens));
422 if (ir->type->is_array()) {
423 tokens[1] = a;
424 }
425
426 /* This state reference has already been setup by ir_to_mesa,
427 * but we'll get the same index back here.
428 */
429 int index = _mesa_add_state_reference(this->fp->Base.Parameters,
430 (gl_state_index *)tokens);
431
432 /* Add each of the unique swizzles of the element as a
433 * parameter. This'll end up matching the expected layout of
434 * the array/matrix/structure we're trying to fill in.
435 */
436 int last_swiz = -1;
437 for (unsigned int i = 0; i < 4; i++) {
438 int swiz = GET_SWZ(element->swizzle, i);
439 if (swiz == last_swiz)
440 break;
441 last_swiz = swiz;
442
443 c->prog_data.param_convert[c->prog_data.nr_params] =
444 PARAM_NO_CONVERT;
445 this->param_index[c->prog_data.nr_params] = index;
446 this->param_offset[c->prog_data.nr_params] = swiz;
447 c->prog_data.nr_params++;
448 }
449 }
450 }
451 }
452
453 fs_reg *
454 fs_visitor::emit_fragcoord_interpolation(ir_variable *ir)
455 {
456 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
457 fs_reg wpos = *reg;
458 fs_reg neg_y = this->pixel_y;
459 neg_y.negate = true;
460 bool flip = !ir->origin_upper_left ^ c->key.render_to_fbo;
461
462 /* gl_FragCoord.x */
463 if (ir->pixel_center_integer) {
464 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_x));
465 } else {
466 emit(fs_inst(BRW_OPCODE_ADD, wpos, this->pixel_x, fs_reg(0.5f)));
467 }
468 wpos.reg_offset++;
469
470 /* gl_FragCoord.y */
471 if (!flip && ir->pixel_center_integer) {
472 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_y));
473 } else {
474 fs_reg pixel_y = this->pixel_y;
475 float offset = (ir->pixel_center_integer ? 0.0 : 0.5);
476
477 if (flip) {
478 pixel_y.negate = true;
479 offset += c->key.drawable_height - 1.0;
480 }
481
482 emit(fs_inst(BRW_OPCODE_ADD, wpos, pixel_y, fs_reg(offset)));
483 }
484 wpos.reg_offset++;
485
486 /* gl_FragCoord.z */
487 if (intel->gen >= 6) {
488 emit(fs_inst(BRW_OPCODE_MOV, wpos,
489 fs_reg(brw_vec8_grf(c->source_depth_reg, 0))));
490 } else {
491 emit(fs_inst(FS_OPCODE_LINTERP, wpos, this->delta_x, this->delta_y,
492 interp_reg(FRAG_ATTRIB_WPOS, 2)));
493 }
494 wpos.reg_offset++;
495
496 /* gl_FragCoord.w: Already set up in emit_interpolation */
497 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->wpos_w));
498
499 return reg;
500 }
501
502 fs_reg *
503 fs_visitor::emit_general_interpolation(ir_variable *ir)
504 {
505 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
506 /* Interpolation is always in floating point regs. */
507 reg->type = BRW_REGISTER_TYPE_F;
508 fs_reg attr = *reg;
509
510 unsigned int array_elements;
511 const glsl_type *type;
512
513 if (ir->type->is_array()) {
514 array_elements = ir->type->length;
515 if (array_elements == 0) {
516 this->fail = true;
517 }
518 type = ir->type->fields.array;
519 } else {
520 array_elements = 1;
521 type = ir->type;
522 }
523
524 int location = ir->location;
525 for (unsigned int i = 0; i < array_elements; i++) {
526 for (unsigned int j = 0; j < type->matrix_columns; j++) {
527 if (urb_setup[location] == -1) {
528 /* If there's no incoming setup data for this slot, don't
529 * emit interpolation for it.
530 */
531 attr.reg_offset += type->vector_elements;
532 location++;
533 continue;
534 }
535
536 if (c->key.flat_shade && (location == FRAG_ATTRIB_COL0 ||
537 location == FRAG_ATTRIB_COL1)) {
538 /* Constant interpolation (flat shading) case. The SF has
539 * handed us defined values in only the constant offset
540 * field of the setup reg.
541 */
542 for (unsigned int c = 0; c < type->vector_elements; c++) {
543 struct brw_reg interp = interp_reg(location, c);
544 interp = suboffset(interp, 3);
545 emit(fs_inst(FS_OPCODE_CINTERP, attr, fs_reg(interp)));
546 attr.reg_offset++;
547 }
548 } else {
549 /* Perspective interpolation case. */
550 for (unsigned int c = 0; c < type->vector_elements; c++) {
551 struct brw_reg interp = interp_reg(location, c);
552 emit(fs_inst(FS_OPCODE_LINTERP,
553 attr,
554 this->delta_x,
555 this->delta_y,
556 fs_reg(interp)));
557 attr.reg_offset++;
558 }
559
560 if (intel->gen < 6) {
561 attr.reg_offset -= type->vector_elements;
562 for (unsigned int c = 0; c < type->vector_elements; c++) {
563 emit(fs_inst(BRW_OPCODE_MUL,
564 attr,
565 attr,
566 this->pixel_w));
567 attr.reg_offset++;
568 }
569 }
570 }
571 location++;
572 }
573 }
574
575 return reg;
576 }
577
578 fs_reg *
579 fs_visitor::emit_frontfacing_interpolation(ir_variable *ir)
580 {
581 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
582
583 /* The frontfacing comes in as a bit in the thread payload. */
584 if (intel->gen >= 6) {
585 emit(fs_inst(BRW_OPCODE_ASR,
586 *reg,
587 fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D)),
588 fs_reg(15)));
589 emit(fs_inst(BRW_OPCODE_NOT,
590 *reg,
591 *reg));
592 emit(fs_inst(BRW_OPCODE_AND,
593 *reg,
594 *reg,
595 fs_reg(1)));
596 } else {
597 struct brw_reg r1_6ud = retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD);
598 /* bit 31 is "primitive is back face", so checking < (1 << 31) gives
599 * us front face
600 */
601 fs_inst *inst = emit(fs_inst(BRW_OPCODE_CMP,
602 *reg,
603 fs_reg(r1_6ud),
604 fs_reg(1u << 31)));
605 inst->conditional_mod = BRW_CONDITIONAL_L;
606 emit(fs_inst(BRW_OPCODE_AND, *reg, *reg, fs_reg(1u)));
607 }
608
609 return reg;
610 }
611
612 fs_inst *
613 fs_visitor::emit_math(fs_opcodes opcode, fs_reg dst, fs_reg src)
614 {
615 switch (opcode) {
616 case FS_OPCODE_RCP:
617 case FS_OPCODE_RSQ:
618 case FS_OPCODE_SQRT:
619 case FS_OPCODE_EXP2:
620 case FS_OPCODE_LOG2:
621 case FS_OPCODE_SIN:
622 case FS_OPCODE_COS:
623 break;
624 default:
625 assert(!"not reached: bad math opcode");
626 return NULL;
627 }
628
629 /* Can't do hstride == 0 args to gen6 math, so expand it out. We
630 * might be able to do better by doing execsize = 1 math and then
631 * expanding that result out, but we would need to be careful with
632 * masking.
633 *
634 * The hardware ignores source modifiers (negate and abs) on math
635 * instructions, so we also move to a temp to set those up.
636 */
637 if (intel->gen >= 6 && (src.file == UNIFORM ||
638 src.abs ||
639 src.negate)) {
640 fs_reg expanded = fs_reg(this, glsl_type::float_type);
641 emit(fs_inst(BRW_OPCODE_MOV, expanded, src));
642 src = expanded;
643 }
644
645 fs_inst *inst = emit(fs_inst(opcode, dst, src));
646
647 if (intel->gen < 6) {
648 inst->base_mrf = 2;
649 inst->mlen = 1;
650 }
651
652 return inst;
653 }
654
655 fs_inst *
656 fs_visitor::emit_math(fs_opcodes opcode, fs_reg dst, fs_reg src0, fs_reg src1)
657 {
658 int base_mrf = 2;
659 fs_inst *inst;
660
661 assert(opcode == FS_OPCODE_POW);
662
663 if (intel->gen >= 6) {
664 /* Can't do hstride == 0 args to gen6 math, so expand it out. */
665 if (src0.file == UNIFORM) {
666 fs_reg expanded = fs_reg(this, glsl_type::float_type);
667 emit(fs_inst(BRW_OPCODE_MOV, expanded, src0));
668 src0 = expanded;
669 }
670
671 if (src1.file == UNIFORM) {
672 fs_reg expanded = fs_reg(this, glsl_type::float_type);
673 emit(fs_inst(BRW_OPCODE_MOV, expanded, src1));
674 src1 = expanded;
675 }
676
677 inst = emit(fs_inst(opcode, dst, src0, src1));
678 } else {
679 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + 1), src1));
680 inst = emit(fs_inst(opcode, dst, src0, reg_null_f));
681
682 inst->base_mrf = base_mrf;
683 inst->mlen = 2;
684 }
685 return inst;
686 }
687
688 void
689 fs_visitor::visit(ir_variable *ir)
690 {
691 fs_reg *reg = NULL;
692
693 if (variable_storage(ir))
694 return;
695
696 if (strcmp(ir->name, "gl_FragColor") == 0) {
697 this->frag_color = ir;
698 } else if (strcmp(ir->name, "gl_FragData") == 0) {
699 this->frag_data = ir;
700 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
701 this->frag_depth = ir;
702 }
703
704 if (ir->mode == ir_var_in) {
705 if (!strcmp(ir->name, "gl_FragCoord")) {
706 reg = emit_fragcoord_interpolation(ir);
707 } else if (!strcmp(ir->name, "gl_FrontFacing")) {
708 reg = emit_frontfacing_interpolation(ir);
709 } else {
710 reg = emit_general_interpolation(ir);
711 }
712 assert(reg);
713 hash_table_insert(this->variable_ht, reg, ir);
714 return;
715 }
716
717 if (ir->mode == ir_var_uniform) {
718 int param_index = c->prog_data.nr_params;
719
720 if (!strncmp(ir->name, "gl_", 3)) {
721 setup_builtin_uniform_values(ir);
722 } else {
723 setup_uniform_values(ir->location, ir->type);
724 }
725
726 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
727 reg->type = brw_type_for_base_type(ir->type);
728 }
729
730 if (!reg)
731 reg = new(this->mem_ctx) fs_reg(this, ir->type);
732
733 hash_table_insert(this->variable_ht, reg, ir);
734 }
735
736 void
737 fs_visitor::visit(ir_dereference_variable *ir)
738 {
739 fs_reg *reg = variable_storage(ir->var);
740 this->result = *reg;
741 }
742
743 void
744 fs_visitor::visit(ir_dereference_record *ir)
745 {
746 const glsl_type *struct_type = ir->record->type;
747
748 ir->record->accept(this);
749
750 unsigned int offset = 0;
751 for (unsigned int i = 0; i < struct_type->length; i++) {
752 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
753 break;
754 offset += type_size(struct_type->fields.structure[i].type);
755 }
756 this->result.reg_offset += offset;
757 this->result.type = brw_type_for_base_type(ir->type);
758 }
759
760 void
761 fs_visitor::visit(ir_dereference_array *ir)
762 {
763 ir_constant *index;
764 int element_size;
765
766 ir->array->accept(this);
767 index = ir->array_index->as_constant();
768
769 element_size = type_size(ir->type);
770 this->result.type = brw_type_for_base_type(ir->type);
771
772 if (index) {
773 assert(this->result.file == UNIFORM ||
774 (this->result.file == GRF &&
775 this->result.reg != 0));
776 this->result.reg_offset += index->value.i[0] * element_size;
777 } else {
778 assert(!"FINISHME: non-constant array element");
779 }
780 }
781
782 /* Instruction selection: Produce a MOV.sat instead of
783 * MIN(MAX(val, 0), 1) when possible.
784 */
785 bool
786 fs_visitor::try_emit_saturate(ir_expression *ir)
787 {
788 ir_rvalue *sat_val = ir->as_rvalue_to_saturate();
789
790 if (!sat_val)
791 return false;
792
793 sat_val->accept(this);
794 fs_reg src = this->result;
795
796 this->result = fs_reg(this, ir->type);
797 fs_inst *inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, src));
798 inst->saturate = true;
799
800 return true;
801 }
802
803 static uint32_t
804 brw_conditional_for_comparison(unsigned int op)
805 {
806 switch (op) {
807 case ir_binop_less:
808 return BRW_CONDITIONAL_L;
809 case ir_binop_greater:
810 return BRW_CONDITIONAL_G;
811 case ir_binop_lequal:
812 return BRW_CONDITIONAL_LE;
813 case ir_binop_gequal:
814 return BRW_CONDITIONAL_GE;
815 case ir_binop_equal:
816 case ir_binop_all_equal: /* same as equal for scalars */
817 return BRW_CONDITIONAL_Z;
818 case ir_binop_nequal:
819 case ir_binop_any_nequal: /* same as nequal for scalars */
820 return BRW_CONDITIONAL_NZ;
821 default:
822 assert(!"not reached: bad operation for comparison");
823 return BRW_CONDITIONAL_NZ;
824 }
825 }
826
827 void
828 fs_visitor::visit(ir_expression *ir)
829 {
830 unsigned int operand;
831 fs_reg op[2], temp;
832 fs_inst *inst;
833
834 assert(ir->get_num_operands() <= 2);
835
836 if (try_emit_saturate(ir))
837 return;
838
839 for (operand = 0; operand < ir->get_num_operands(); operand++) {
840 ir->operands[operand]->accept(this);
841 if (this->result.file == BAD_FILE) {
842 ir_print_visitor v;
843 printf("Failed to get tree for expression operand:\n");
844 ir->operands[operand]->accept(&v);
845 this->fail = true;
846 }
847 op[operand] = this->result;
848
849 /* Matrix expression operands should have been broken down to vector
850 * operations already.
851 */
852 assert(!ir->operands[operand]->type->is_matrix());
853 /* And then those vector operands should have been broken down to scalar.
854 */
855 assert(!ir->operands[operand]->type->is_vector());
856 }
857
858 /* Storage for our result. If our result goes into an assignment, it will
859 * just get copy-propagated out, so no worries.
860 */
861 this->result = fs_reg(this, ir->type);
862
863 switch (ir->operation) {
864 case ir_unop_logic_not:
865 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
866 * ones complement of the whole register, not just bit 0.
867 */
868 emit(fs_inst(BRW_OPCODE_XOR, this->result, op[0], fs_reg(1)));
869 break;
870 case ir_unop_neg:
871 op[0].negate = !op[0].negate;
872 this->result = op[0];
873 break;
874 case ir_unop_abs:
875 op[0].abs = true;
876 op[0].negate = false;
877 this->result = op[0];
878 break;
879 case ir_unop_sign:
880 temp = fs_reg(this, ir->type);
881
882 emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(0.0f)));
883
884 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f)));
885 inst->conditional_mod = BRW_CONDITIONAL_G;
886 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(1.0f)));
887 inst->predicated = true;
888
889 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f)));
890 inst->conditional_mod = BRW_CONDITIONAL_L;
891 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(-1.0f)));
892 inst->predicated = true;
893
894 break;
895 case ir_unop_rcp:
896 emit_math(FS_OPCODE_RCP, this->result, op[0]);
897 break;
898
899 case ir_unop_exp2:
900 emit_math(FS_OPCODE_EXP2, this->result, op[0]);
901 break;
902 case ir_unop_log2:
903 emit_math(FS_OPCODE_LOG2, this->result, op[0]);
904 break;
905 case ir_unop_exp:
906 case ir_unop_log:
907 assert(!"not reached: should be handled by ir_explog_to_explog2");
908 break;
909 case ir_unop_sin:
910 case ir_unop_sin_reduced:
911 emit_math(FS_OPCODE_SIN, this->result, op[0]);
912 break;
913 case ir_unop_cos:
914 case ir_unop_cos_reduced:
915 emit_math(FS_OPCODE_COS, this->result, op[0]);
916 break;
917
918 case ir_unop_dFdx:
919 emit(fs_inst(FS_OPCODE_DDX, this->result, op[0]));
920 break;
921 case ir_unop_dFdy:
922 emit(fs_inst(FS_OPCODE_DDY, this->result, op[0]));
923 break;
924
925 case ir_binop_add:
926 emit(fs_inst(BRW_OPCODE_ADD, this->result, op[0], op[1]));
927 break;
928 case ir_binop_sub:
929 assert(!"not reached: should be handled by ir_sub_to_add_neg");
930 break;
931
932 case ir_binop_mul:
933 emit(fs_inst(BRW_OPCODE_MUL, this->result, op[0], op[1]));
934 break;
935 case ir_binop_div:
936 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
937 break;
938 case ir_binop_mod:
939 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
940 break;
941
942 case ir_binop_less:
943 case ir_binop_greater:
944 case ir_binop_lequal:
945 case ir_binop_gequal:
946 case ir_binop_equal:
947 case ir_binop_all_equal:
948 case ir_binop_nequal:
949 case ir_binop_any_nequal:
950 temp = this->result;
951 /* original gen4 does implicit conversion before comparison. */
952 if (intel->gen < 5)
953 temp.type = op[0].type;
954
955 inst = emit(fs_inst(BRW_OPCODE_CMP, temp, op[0], op[1]));
956 inst->conditional_mod = brw_conditional_for_comparison(ir->operation);
957 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
958 break;
959
960 case ir_binop_logic_xor:
961 emit(fs_inst(BRW_OPCODE_XOR, this->result, op[0], op[1]));
962 break;
963
964 case ir_binop_logic_or:
965 emit(fs_inst(BRW_OPCODE_OR, this->result, op[0], op[1]));
966 break;
967
968 case ir_binop_logic_and:
969 emit(fs_inst(BRW_OPCODE_AND, this->result, op[0], op[1]));
970 break;
971
972 case ir_binop_dot:
973 case ir_unop_any:
974 assert(!"not reached: should be handled by brw_fs_channel_expressions");
975 break;
976
977 case ir_unop_noise:
978 assert(!"not reached: should be handled by lower_noise");
979 break;
980
981 case ir_quadop_vector:
982 assert(!"not reached: should be handled by lower_quadop_vector");
983 break;
984
985 case ir_unop_sqrt:
986 emit_math(FS_OPCODE_SQRT, this->result, op[0]);
987 break;
988
989 case ir_unop_rsq:
990 emit_math(FS_OPCODE_RSQ, this->result, op[0]);
991 break;
992
993 case ir_unop_i2f:
994 case ir_unop_b2f:
995 case ir_unop_b2i:
996 case ir_unop_f2i:
997 emit(fs_inst(BRW_OPCODE_MOV, this->result, op[0]));
998 break;
999 case ir_unop_f2b:
1000 case ir_unop_i2b:
1001 temp = this->result;
1002 /* original gen4 does implicit conversion before comparison. */
1003 if (intel->gen < 5)
1004 temp.type = op[0].type;
1005
1006 inst = emit(fs_inst(BRW_OPCODE_CMP, temp, op[0], fs_reg(0.0f)));
1007 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1008 inst = emit(fs_inst(BRW_OPCODE_AND, this->result,
1009 this->result, fs_reg(1)));
1010 break;
1011
1012 case ir_unop_trunc:
1013 emit(fs_inst(BRW_OPCODE_RNDZ, this->result, op[0]));
1014 break;
1015 case ir_unop_ceil:
1016 op[0].negate = !op[0].negate;
1017 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
1018 this->result.negate = true;
1019 break;
1020 case ir_unop_floor:
1021 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
1022 break;
1023 case ir_unop_fract:
1024 inst = emit(fs_inst(BRW_OPCODE_FRC, this->result, op[0]));
1025 break;
1026 case ir_unop_round_even:
1027 emit(fs_inst(BRW_OPCODE_RNDE, this->result, op[0]));
1028 break;
1029
1030 case ir_binop_min:
1031 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1032 inst->conditional_mod = BRW_CONDITIONAL_L;
1033
1034 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
1035 inst->predicated = true;
1036 break;
1037 case ir_binop_max:
1038 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1039 inst->conditional_mod = BRW_CONDITIONAL_G;
1040
1041 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
1042 inst->predicated = true;
1043 break;
1044
1045 case ir_binop_pow:
1046 emit_math(FS_OPCODE_POW, this->result, op[0], op[1]);
1047 break;
1048
1049 case ir_unop_bit_not:
1050 inst = emit(fs_inst(BRW_OPCODE_NOT, this->result, op[0]));
1051 break;
1052 case ir_binop_bit_and:
1053 inst = emit(fs_inst(BRW_OPCODE_AND, this->result, op[0], op[1]));
1054 break;
1055 case ir_binop_bit_xor:
1056 inst = emit(fs_inst(BRW_OPCODE_XOR, this->result, op[0], op[1]));
1057 break;
1058 case ir_binop_bit_or:
1059 inst = emit(fs_inst(BRW_OPCODE_OR, this->result, op[0], op[1]));
1060 break;
1061
1062 case ir_unop_u2f:
1063 case ir_binop_lshift:
1064 case ir_binop_rshift:
1065 assert(!"GLSL 1.30 features unsupported");
1066 break;
1067 }
1068 }
1069
1070 void
1071 fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
1072 const glsl_type *type, bool predicated)
1073 {
1074 switch (type->base_type) {
1075 case GLSL_TYPE_FLOAT:
1076 case GLSL_TYPE_UINT:
1077 case GLSL_TYPE_INT:
1078 case GLSL_TYPE_BOOL:
1079 for (unsigned int i = 0; i < type->components(); i++) {
1080 l.type = brw_type_for_base_type(type);
1081 r.type = brw_type_for_base_type(type);
1082
1083 fs_inst *inst = emit(fs_inst(BRW_OPCODE_MOV, l, r));
1084 inst->predicated = predicated;
1085
1086 l.reg_offset++;
1087 r.reg_offset++;
1088 }
1089 break;
1090 case GLSL_TYPE_ARRAY:
1091 for (unsigned int i = 0; i < type->length; i++) {
1092 emit_assignment_writes(l, r, type->fields.array, predicated);
1093 }
1094 break;
1095
1096 case GLSL_TYPE_STRUCT:
1097 for (unsigned int i = 0; i < type->length; i++) {
1098 emit_assignment_writes(l, r, type->fields.structure[i].type,
1099 predicated);
1100 }
1101 break;
1102
1103 case GLSL_TYPE_SAMPLER:
1104 break;
1105
1106 default:
1107 assert(!"not reached");
1108 break;
1109 }
1110 }
1111
1112 void
1113 fs_visitor::visit(ir_assignment *ir)
1114 {
1115 struct fs_reg l, r;
1116 fs_inst *inst;
1117
1118 /* FINISHME: arrays on the lhs */
1119 ir->lhs->accept(this);
1120 l = this->result;
1121
1122 ir->rhs->accept(this);
1123 r = this->result;
1124
1125 assert(l.file != BAD_FILE);
1126 assert(r.file != BAD_FILE);
1127
1128 if (ir->condition) {
1129 emit_bool_to_cond_code(ir->condition);
1130 }
1131
1132 if (ir->lhs->type->is_scalar() ||
1133 ir->lhs->type->is_vector()) {
1134 for (int i = 0; i < ir->lhs->type->vector_elements; i++) {
1135 if (ir->write_mask & (1 << i)) {
1136 inst = emit(fs_inst(BRW_OPCODE_MOV, l, r));
1137 if (ir->condition)
1138 inst->predicated = true;
1139 r.reg_offset++;
1140 }
1141 l.reg_offset++;
1142 }
1143 } else {
1144 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL);
1145 }
1146 }
1147
1148 fs_inst *
1149 fs_visitor::emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate)
1150 {
1151 int mlen;
1152 int base_mrf = 1;
1153 bool simd16 = false;
1154 fs_reg orig_dst;
1155
1156 /* g0 header. */
1157 mlen = 1;
1158
1159 if (ir->shadow_comparitor) {
1160 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1161 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i),
1162 coordinate));
1163 coordinate.reg_offset++;
1164 }
1165 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1166 mlen += 3;
1167
1168 if (ir->op == ir_tex) {
1169 /* There's no plain shadow compare message, so we use shadow
1170 * compare with a bias of 0.0.
1171 */
1172 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1173 fs_reg(0.0f)));
1174 mlen++;
1175 } else if (ir->op == ir_txb) {
1176 ir->lod_info.bias->accept(this);
1177 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1178 this->result));
1179 mlen++;
1180 } else {
1181 assert(ir->op == ir_txl);
1182 ir->lod_info.lod->accept(this);
1183 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1184 this->result));
1185 mlen++;
1186 }
1187
1188 ir->shadow_comparitor->accept(this);
1189 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1190 mlen++;
1191 } else if (ir->op == ir_tex) {
1192 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1193 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i),
1194 coordinate));
1195 coordinate.reg_offset++;
1196 }
1197 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1198 mlen += 3;
1199 } else {
1200 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
1201 * instructions. We'll need to do SIMD16 here.
1202 */
1203 assert(ir->op == ir_txb || ir->op == ir_txl);
1204
1205 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1206 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i * 2),
1207 coordinate));
1208 coordinate.reg_offset++;
1209 }
1210
1211 /* lod/bias appears after u/v/r. */
1212 mlen += 6;
1213
1214 if (ir->op == ir_txb) {
1215 ir->lod_info.bias->accept(this);
1216 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1217 this->result));
1218 mlen++;
1219 } else {
1220 ir->lod_info.lod->accept(this);
1221 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1222 this->result));
1223 mlen++;
1224 }
1225
1226 /* The unused upper half. */
1227 mlen++;
1228
1229 /* Now, since we're doing simd16, the return is 2 interleaved
1230 * vec4s where the odd-indexed ones are junk. We'll need to move
1231 * this weirdness around to the expected layout.
1232 */
1233 simd16 = true;
1234 orig_dst = dst;
1235 dst = fs_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type,
1236 2));
1237 dst.type = BRW_REGISTER_TYPE_F;
1238 }
1239
1240 fs_inst *inst = NULL;
1241 switch (ir->op) {
1242 case ir_tex:
1243 inst = emit(fs_inst(FS_OPCODE_TEX, dst));
1244 break;
1245 case ir_txb:
1246 inst = emit(fs_inst(FS_OPCODE_TXB, dst));
1247 break;
1248 case ir_txl:
1249 inst = emit(fs_inst(FS_OPCODE_TXL, dst));
1250 break;
1251 case ir_txd:
1252 case ir_txf:
1253 assert(!"GLSL 1.30 features unsupported");
1254 break;
1255 }
1256 inst->base_mrf = base_mrf;
1257 inst->mlen = mlen;
1258
1259 if (simd16) {
1260 for (int i = 0; i < 4; i++) {
1261 emit(fs_inst(BRW_OPCODE_MOV, orig_dst, dst));
1262 orig_dst.reg_offset++;
1263 dst.reg_offset += 2;
1264 }
1265 }
1266
1267 return inst;
1268 }
1269
1270 fs_inst *
1271 fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate)
1272 {
1273 /* gen5's SIMD8 sampler has slots for u, v, r, array index, then
1274 * optional parameters like shadow comparitor or LOD bias. If
1275 * optional parameters aren't present, those base slots are
1276 * optional and don't need to be included in the message.
1277 *
1278 * We don't fill in the unnecessary slots regardless, which may
1279 * look surprising in the disassembly.
1280 */
1281 int mlen = 1; /* g0 header always present. */
1282 int base_mrf = 1;
1283
1284 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1285 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i),
1286 coordinate));
1287 coordinate.reg_offset++;
1288 }
1289 mlen += ir->coordinate->type->vector_elements;
1290
1291 if (ir->shadow_comparitor) {
1292 mlen = MAX2(mlen, 5);
1293
1294 ir->shadow_comparitor->accept(this);
1295 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1296 mlen++;
1297 }
1298
1299 fs_inst *inst = NULL;
1300 switch (ir->op) {
1301 case ir_tex:
1302 inst = emit(fs_inst(FS_OPCODE_TEX, dst));
1303 break;
1304 case ir_txb:
1305 ir->lod_info.bias->accept(this);
1306 mlen = MAX2(mlen, 5);
1307 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1308 mlen++;
1309
1310 inst = emit(fs_inst(FS_OPCODE_TXB, dst));
1311 break;
1312 case ir_txl:
1313 ir->lod_info.lod->accept(this);
1314 mlen = MAX2(mlen, 5);
1315 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1316 mlen++;
1317
1318 inst = emit(fs_inst(FS_OPCODE_TXL, dst));
1319 break;
1320 case ir_txd:
1321 case ir_txf:
1322 assert(!"GLSL 1.30 features unsupported");
1323 break;
1324 }
1325 inst->base_mrf = base_mrf;
1326 inst->mlen = mlen;
1327
1328 return inst;
1329 }
1330
1331 void
1332 fs_visitor::visit(ir_texture *ir)
1333 {
1334 int sampler;
1335 fs_inst *inst = NULL;
1336
1337 ir->coordinate->accept(this);
1338 fs_reg coordinate = this->result;
1339
1340 /* Should be lowered by do_lower_texture_projection */
1341 assert(!ir->projector);
1342
1343 sampler = _mesa_get_sampler_uniform_value(ir->sampler,
1344 ctx->Shader.CurrentFragmentProgram,
1345 &brw->fragment_program->Base);
1346 sampler = c->fp->program.Base.SamplerUnits[sampler];
1347
1348 /* The 965 requires the EU to do the normalization of GL rectangle
1349 * texture coordinates. We use the program parameter state
1350 * tracking to get the scaling factor.
1351 */
1352 if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) {
1353 struct gl_program_parameter_list *params = c->fp->program.Base.Parameters;
1354 int tokens[STATE_LENGTH] = {
1355 STATE_INTERNAL,
1356 STATE_TEXRECT_SCALE,
1357 sampler,
1358 0,
1359 0
1360 };
1361
1362 c->prog_data.param_convert[c->prog_data.nr_params] =
1363 PARAM_NO_CONVERT;
1364 c->prog_data.param_convert[c->prog_data.nr_params + 1] =
1365 PARAM_NO_CONVERT;
1366
1367 fs_reg scale_x = fs_reg(UNIFORM, c->prog_data.nr_params);
1368 fs_reg scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1);
1369 GLuint index = _mesa_add_state_reference(params,
1370 (gl_state_index *)tokens);
1371
1372 this->param_index[c->prog_data.nr_params] = index;
1373 this->param_offset[c->prog_data.nr_params] = 0;
1374 c->prog_data.nr_params++;
1375 this->param_index[c->prog_data.nr_params] = index;
1376 this->param_offset[c->prog_data.nr_params] = 1;
1377 c->prog_data.nr_params++;
1378
1379 fs_reg dst = fs_reg(this, ir->coordinate->type);
1380 fs_reg src = coordinate;
1381 coordinate = dst;
1382
1383 emit(fs_inst(BRW_OPCODE_MUL, dst, src, scale_x));
1384 dst.reg_offset++;
1385 src.reg_offset++;
1386 emit(fs_inst(BRW_OPCODE_MUL, dst, src, scale_y));
1387 }
1388
1389 /* Writemasking doesn't eliminate channels on SIMD8 texture
1390 * samples, so don't worry about them.
1391 */
1392 fs_reg dst = fs_reg(this, glsl_type::vec4_type);
1393
1394 if (intel->gen < 5) {
1395 inst = emit_texture_gen4(ir, dst, coordinate);
1396 } else {
1397 inst = emit_texture_gen5(ir, dst, coordinate);
1398 }
1399
1400 inst->sampler = sampler;
1401
1402 this->result = dst;
1403
1404 if (ir->shadow_comparitor)
1405 inst->shadow_compare = true;
1406
1407 if (c->key.tex_swizzles[inst->sampler] != SWIZZLE_NOOP) {
1408 fs_reg swizzle_dst = fs_reg(this, glsl_type::vec4_type);
1409
1410 for (int i = 0; i < 4; i++) {
1411 int swiz = GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1412 fs_reg l = swizzle_dst;
1413 l.reg_offset += i;
1414
1415 if (swiz == SWIZZLE_ZERO) {
1416 emit(fs_inst(BRW_OPCODE_MOV, l, fs_reg(0.0f)));
1417 } else if (swiz == SWIZZLE_ONE) {
1418 emit(fs_inst(BRW_OPCODE_MOV, l, fs_reg(1.0f)));
1419 } else {
1420 fs_reg r = dst;
1421 r.reg_offset += GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1422 emit(fs_inst(BRW_OPCODE_MOV, l, r));
1423 }
1424 }
1425 this->result = swizzle_dst;
1426 }
1427 }
1428
1429 void
1430 fs_visitor::visit(ir_swizzle *ir)
1431 {
1432 ir->val->accept(this);
1433 fs_reg val = this->result;
1434
1435 if (ir->type->vector_elements == 1) {
1436 this->result.reg_offset += ir->mask.x;
1437 return;
1438 }
1439
1440 fs_reg result = fs_reg(this, ir->type);
1441 this->result = result;
1442
1443 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1444 fs_reg channel = val;
1445 int swiz = 0;
1446
1447 switch (i) {
1448 case 0:
1449 swiz = ir->mask.x;
1450 break;
1451 case 1:
1452 swiz = ir->mask.y;
1453 break;
1454 case 2:
1455 swiz = ir->mask.z;
1456 break;
1457 case 3:
1458 swiz = ir->mask.w;
1459 break;
1460 }
1461
1462 channel.reg_offset += swiz;
1463 emit(fs_inst(BRW_OPCODE_MOV, result, channel));
1464 result.reg_offset++;
1465 }
1466 }
1467
1468 void
1469 fs_visitor::visit(ir_discard *ir)
1470 {
1471 fs_reg temp = fs_reg(this, glsl_type::uint_type);
1472
1473 assert(ir->condition == NULL); /* FINISHME */
1474
1475 emit(fs_inst(FS_OPCODE_DISCARD_NOT, temp, reg_null_d));
1476 emit(fs_inst(FS_OPCODE_DISCARD_AND, reg_null_d, temp));
1477 kill_emitted = true;
1478 }
1479
1480 void
1481 fs_visitor::visit(ir_constant *ir)
1482 {
1483 /* Set this->result to reg at the bottom of the function because some code
1484 * paths will cause this visitor to be applied to other fields. This will
1485 * cause the value stored in this->result to be modified.
1486 *
1487 * Make reg constant so that it doesn't get accidentally modified along the
1488 * way. Yes, I actually had this problem. :(
1489 */
1490 const fs_reg reg(this, ir->type);
1491 fs_reg dst_reg = reg;
1492
1493 if (ir->type->is_array()) {
1494 const unsigned size = type_size(ir->type->fields.array);
1495
1496 for (unsigned i = 0; i < ir->type->length; i++) {
1497 ir->array_elements[i]->accept(this);
1498 fs_reg src_reg = this->result;
1499
1500 dst_reg.type = src_reg.type;
1501 for (unsigned j = 0; j < size; j++) {
1502 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, src_reg));
1503 src_reg.reg_offset++;
1504 dst_reg.reg_offset++;
1505 }
1506 }
1507 } else if (ir->type->is_record()) {
1508 foreach_list(node, &ir->components) {
1509 ir_instruction *const field = (ir_instruction *) node;
1510 const unsigned size = type_size(field->type);
1511
1512 field->accept(this);
1513 fs_reg src_reg = this->result;
1514
1515 dst_reg.type = src_reg.type;
1516 for (unsigned j = 0; j < size; j++) {
1517 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, src_reg));
1518 src_reg.reg_offset++;
1519 dst_reg.reg_offset++;
1520 }
1521 }
1522 } else {
1523 const unsigned size = type_size(ir->type);
1524
1525 for (unsigned i = 0; i < size; i++) {
1526 switch (ir->type->base_type) {
1527 case GLSL_TYPE_FLOAT:
1528 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i])));
1529 break;
1530 case GLSL_TYPE_UINT:
1531 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i])));
1532 break;
1533 case GLSL_TYPE_INT:
1534 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i])));
1535 break;
1536 case GLSL_TYPE_BOOL:
1537 emit(fs_inst(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i])));
1538 break;
1539 default:
1540 assert(!"Non-float/uint/int/bool constant");
1541 }
1542 dst_reg.reg_offset++;
1543 }
1544 }
1545
1546 this->result = reg;
1547 }
1548
1549 void
1550 fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
1551 {
1552 ir_expression *expr = ir->as_expression();
1553
1554 if (expr) {
1555 fs_reg op[2];
1556 fs_inst *inst;
1557
1558 assert(expr->get_num_operands() <= 2);
1559 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1560 assert(expr->operands[i]->type->is_scalar());
1561
1562 expr->operands[i]->accept(this);
1563 op[i] = this->result;
1564 }
1565
1566 switch (expr->operation) {
1567 case ir_unop_logic_not:
1568 inst = emit(fs_inst(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1)));
1569 inst->conditional_mod = BRW_CONDITIONAL_Z;
1570 break;
1571
1572 case ir_binop_logic_xor:
1573 inst = emit(fs_inst(BRW_OPCODE_XOR, reg_null_d, op[0], op[1]));
1574 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1575 break;
1576
1577 case ir_binop_logic_or:
1578 inst = emit(fs_inst(BRW_OPCODE_OR, reg_null_d, op[0], op[1]));
1579 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1580 break;
1581
1582 case ir_binop_logic_and:
1583 inst = emit(fs_inst(BRW_OPCODE_AND, reg_null_d, op[0], op[1]));
1584 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1585 break;
1586
1587 case ir_unop_f2b:
1588 if (intel->gen >= 6) {
1589 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_d,
1590 op[0], fs_reg(0.0f)));
1591 } else {
1592 inst = emit(fs_inst(BRW_OPCODE_MOV, reg_null_f, op[0]));
1593 }
1594 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1595 break;
1596
1597 case ir_unop_i2b:
1598 if (intel->gen >= 6) {
1599 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0)));
1600 } else {
1601 inst = emit(fs_inst(BRW_OPCODE_MOV, reg_null_d, op[0]));
1602 }
1603 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1604 break;
1605
1606 case ir_binop_greater:
1607 case ir_binop_gequal:
1608 case ir_binop_less:
1609 case ir_binop_lequal:
1610 case ir_binop_equal:
1611 case ir_binop_all_equal:
1612 case ir_binop_nequal:
1613 case ir_binop_any_nequal:
1614 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]));
1615 inst->conditional_mod =
1616 brw_conditional_for_comparison(expr->operation);
1617 break;
1618
1619 default:
1620 assert(!"not reached");
1621 this->fail = true;
1622 break;
1623 }
1624 return;
1625 }
1626
1627 ir->accept(this);
1628
1629 if (intel->gen >= 6) {
1630 fs_inst *inst = emit(fs_inst(BRW_OPCODE_AND, reg_null_d,
1631 this->result, fs_reg(1)));
1632 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1633 } else {
1634 fs_inst *inst = emit(fs_inst(BRW_OPCODE_MOV, reg_null_d, this->result));
1635 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1636 }
1637 }
1638
1639 /**
1640 * Emit a gen6 IF statement with the comparison folded into the IF
1641 * instruction.
1642 */
1643 void
1644 fs_visitor::emit_if_gen6(ir_if *ir)
1645 {
1646 ir_expression *expr = ir->condition->as_expression();
1647
1648 if (expr) {
1649 fs_reg op[2];
1650 fs_inst *inst;
1651 fs_reg temp;
1652
1653 assert(expr->get_num_operands() <= 2);
1654 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1655 assert(expr->operands[i]->type->is_scalar());
1656
1657 expr->operands[i]->accept(this);
1658 op[i] = this->result;
1659 }
1660
1661 switch (expr->operation) {
1662 case ir_unop_logic_not:
1663 inst = emit(fs_inst(BRW_OPCODE_IF, temp, op[0], fs_reg(0)));
1664 inst->conditional_mod = BRW_CONDITIONAL_Z;
1665 return;
1666
1667 case ir_binop_logic_xor:
1668 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, op[0], op[1]));
1669 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1670 return;
1671
1672 case ir_binop_logic_or:
1673 temp = fs_reg(this, glsl_type::bool_type);
1674 emit(fs_inst(BRW_OPCODE_OR, temp, op[0], op[1]));
1675 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)));
1676 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1677 return;
1678
1679 case ir_binop_logic_and:
1680 temp = fs_reg(this, glsl_type::bool_type);
1681 emit(fs_inst(BRW_OPCODE_AND, temp, op[0], op[1]));
1682 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)));
1683 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1684 return;
1685
1686 case ir_unop_f2b:
1687 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0)));
1688 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1689 return;
1690
1691 case ir_unop_i2b:
1692 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)));
1693 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1694 return;
1695
1696 case ir_binop_greater:
1697 case ir_binop_gequal:
1698 case ir_binop_less:
1699 case ir_binop_lequal:
1700 case ir_binop_equal:
1701 case ir_binop_all_equal:
1702 case ir_binop_nequal:
1703 case ir_binop_any_nequal:
1704 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, op[0], op[1]));
1705 inst->conditional_mod =
1706 brw_conditional_for_comparison(expr->operation);
1707 return;
1708 default:
1709 assert(!"not reached");
1710 inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)));
1711 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1712 this->fail = true;
1713 return;
1714 }
1715 return;
1716 }
1717
1718 ir->condition->accept(this);
1719
1720 fs_inst *inst = emit(fs_inst(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0)));
1721 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1722 }
1723
1724 void
1725 fs_visitor::visit(ir_if *ir)
1726 {
1727 fs_inst *inst;
1728
1729 /* Don't point the annotation at the if statement, because then it plus
1730 * the then and else blocks get printed.
1731 */
1732 this->base_ir = ir->condition;
1733
1734 if (intel->gen >= 6) {
1735 emit_if_gen6(ir);
1736 } else {
1737 emit_bool_to_cond_code(ir->condition);
1738
1739 inst = emit(fs_inst(BRW_OPCODE_IF));
1740 inst->predicated = true;
1741 }
1742
1743 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1744 ir_instruction *ir = (ir_instruction *)iter.get();
1745 this->base_ir = ir;
1746
1747 ir->accept(this);
1748 }
1749
1750 if (!ir->else_instructions.is_empty()) {
1751 emit(fs_inst(BRW_OPCODE_ELSE));
1752
1753 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1754 ir_instruction *ir = (ir_instruction *)iter.get();
1755 this->base_ir = ir;
1756
1757 ir->accept(this);
1758 }
1759 }
1760
1761 emit(fs_inst(BRW_OPCODE_ENDIF));
1762 }
1763
1764 void
1765 fs_visitor::visit(ir_loop *ir)
1766 {
1767 fs_reg counter = reg_undef;
1768
1769 if (ir->counter) {
1770 this->base_ir = ir->counter;
1771 ir->counter->accept(this);
1772 counter = *(variable_storage(ir->counter));
1773
1774 if (ir->from) {
1775 this->base_ir = ir->from;
1776 ir->from->accept(this);
1777
1778 emit(fs_inst(BRW_OPCODE_MOV, counter, this->result));
1779 }
1780 }
1781
1782 emit(fs_inst(BRW_OPCODE_DO));
1783
1784 if (ir->to) {
1785 this->base_ir = ir->to;
1786 ir->to->accept(this);
1787
1788 fs_inst *inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null_cmp,
1789 counter, this->result));
1790 inst->conditional_mod = brw_conditional_for_comparison(ir->cmp);
1791
1792 inst = emit(fs_inst(BRW_OPCODE_BREAK));
1793 inst->predicated = true;
1794 }
1795
1796 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1797 ir_instruction *ir = (ir_instruction *)iter.get();
1798
1799 this->base_ir = ir;
1800 ir->accept(this);
1801 }
1802
1803 if (ir->increment) {
1804 this->base_ir = ir->increment;
1805 ir->increment->accept(this);
1806 emit(fs_inst(BRW_OPCODE_ADD, counter, counter, this->result));
1807 }
1808
1809 emit(fs_inst(BRW_OPCODE_WHILE));
1810 }
1811
1812 void
1813 fs_visitor::visit(ir_loop_jump *ir)
1814 {
1815 switch (ir->mode) {
1816 case ir_loop_jump::jump_break:
1817 emit(fs_inst(BRW_OPCODE_BREAK));
1818 break;
1819 case ir_loop_jump::jump_continue:
1820 emit(fs_inst(BRW_OPCODE_CONTINUE));
1821 break;
1822 }
1823 }
1824
1825 void
1826 fs_visitor::visit(ir_call *ir)
1827 {
1828 assert(!"FINISHME");
1829 }
1830
1831 void
1832 fs_visitor::visit(ir_return *ir)
1833 {
1834 assert(!"FINISHME");
1835 }
1836
1837 void
1838 fs_visitor::visit(ir_function *ir)
1839 {
1840 /* Ignore function bodies other than main() -- we shouldn't see calls to
1841 * them since they should all be inlined before we get to ir_to_mesa.
1842 */
1843 if (strcmp(ir->name, "main") == 0) {
1844 const ir_function_signature *sig;
1845 exec_list empty;
1846
1847 sig = ir->matching_signature(&empty);
1848
1849 assert(sig);
1850
1851 foreach_iter(exec_list_iterator, iter, sig->body) {
1852 ir_instruction *ir = (ir_instruction *)iter.get();
1853 this->base_ir = ir;
1854
1855 ir->accept(this);
1856 }
1857 }
1858 }
1859
1860 void
1861 fs_visitor::visit(ir_function_signature *ir)
1862 {
1863 assert(!"not reached");
1864 (void)ir;
1865 }
1866
1867 fs_inst *
1868 fs_visitor::emit(fs_inst inst)
1869 {
1870 fs_inst *list_inst = new(mem_ctx) fs_inst;
1871 *list_inst = inst;
1872
1873 list_inst->annotation = this->current_annotation;
1874 list_inst->ir = this->base_ir;
1875
1876 this->instructions.push_tail(list_inst);
1877
1878 return list_inst;
1879 }
1880
1881 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1882 void
1883 fs_visitor::emit_dummy_fs()
1884 {
1885 /* Everyone's favorite color. */
1886 emit(fs_inst(BRW_OPCODE_MOV,
1887 fs_reg(MRF, 2),
1888 fs_reg(1.0f)));
1889 emit(fs_inst(BRW_OPCODE_MOV,
1890 fs_reg(MRF, 3),
1891 fs_reg(0.0f)));
1892 emit(fs_inst(BRW_OPCODE_MOV,
1893 fs_reg(MRF, 4),
1894 fs_reg(1.0f)));
1895 emit(fs_inst(BRW_OPCODE_MOV,
1896 fs_reg(MRF, 5),
1897 fs_reg(0.0f)));
1898
1899 fs_inst *write;
1900 write = emit(fs_inst(FS_OPCODE_FB_WRITE,
1901 fs_reg(0),
1902 fs_reg(0)));
1903 write->base_mrf = 0;
1904 }
1905
1906 /* The register location here is relative to the start of the URB
1907 * data. It will get adjusted to be a real location before
1908 * generate_code() time.
1909 */
1910 struct brw_reg
1911 fs_visitor::interp_reg(int location, int channel)
1912 {
1913 int regnr = urb_setup[location] * 2 + channel / 2;
1914 int stride = (channel & 1) * 4;
1915
1916 assert(urb_setup[location] != -1);
1917
1918 return brw_vec1_grf(regnr, stride);
1919 }
1920
1921 /** Emits the interpolation for the varying inputs. */
1922 void
1923 fs_visitor::emit_interpolation_setup_gen4()
1924 {
1925 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1926
1927 this->current_annotation = "compute pixel centers";
1928 this->pixel_x = fs_reg(this, glsl_type::uint_type);
1929 this->pixel_y = fs_reg(this, glsl_type::uint_type);
1930 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1931 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1932 emit(fs_inst(BRW_OPCODE_ADD,
1933 this->pixel_x,
1934 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1935 fs_reg(brw_imm_v(0x10101010))));
1936 emit(fs_inst(BRW_OPCODE_ADD,
1937 this->pixel_y,
1938 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1939 fs_reg(brw_imm_v(0x11001100))));
1940
1941 this->current_annotation = "compute pixel deltas from v0";
1942 if (brw->has_pln) {
1943 this->delta_x = fs_reg(this, glsl_type::vec2_type);
1944 this->delta_y = this->delta_x;
1945 this->delta_y.reg_offset++;
1946 } else {
1947 this->delta_x = fs_reg(this, glsl_type::float_type);
1948 this->delta_y = fs_reg(this, glsl_type::float_type);
1949 }
1950 emit(fs_inst(BRW_OPCODE_ADD,
1951 this->delta_x,
1952 this->pixel_x,
1953 fs_reg(negate(brw_vec1_grf(1, 0)))));
1954 emit(fs_inst(BRW_OPCODE_ADD,
1955 this->delta_y,
1956 this->pixel_y,
1957 fs_reg(negate(brw_vec1_grf(1, 1)))));
1958
1959 this->current_annotation = "compute pos.w and 1/pos.w";
1960 /* Compute wpos.w. It's always in our setup, since it's needed to
1961 * interpolate the other attributes.
1962 */
1963 this->wpos_w = fs_reg(this, glsl_type::float_type);
1964 emit(fs_inst(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y,
1965 interp_reg(FRAG_ATTRIB_WPOS, 3)));
1966 /* Compute the pixel 1/W value from wpos.w. */
1967 this->pixel_w = fs_reg(this, glsl_type::float_type);
1968 emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w);
1969 this->current_annotation = NULL;
1970 }
1971
1972 /** Emits the interpolation for the varying inputs. */
1973 void
1974 fs_visitor::emit_interpolation_setup_gen6()
1975 {
1976 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1977
1978 /* If the pixel centers end up used, the setup is the same as for gen4. */
1979 this->current_annotation = "compute pixel centers";
1980 fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type);
1981 fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type);
1982 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
1983 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
1984 emit(fs_inst(BRW_OPCODE_ADD,
1985 int_pixel_x,
1986 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1987 fs_reg(brw_imm_v(0x10101010))));
1988 emit(fs_inst(BRW_OPCODE_ADD,
1989 int_pixel_y,
1990 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1991 fs_reg(brw_imm_v(0x11001100))));
1992
1993 /* As of gen6, we can no longer mix float and int sources. We have
1994 * to turn the integer pixel centers into floats for their actual
1995 * use.
1996 */
1997 this->pixel_x = fs_reg(this, glsl_type::float_type);
1998 this->pixel_y = fs_reg(this, glsl_type::float_type);
1999 emit(fs_inst(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x));
2000 emit(fs_inst(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y));
2001
2002 this->current_annotation = "compute 1/pos.w";
2003 this->wpos_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0));
2004 this->pixel_w = fs_reg(this, glsl_type::float_type);
2005 emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w);
2006
2007 this->delta_x = fs_reg(brw_vec8_grf(2, 0));
2008 this->delta_y = fs_reg(brw_vec8_grf(3, 0));
2009
2010 this->current_annotation = NULL;
2011 }
2012
2013 void
2014 fs_visitor::emit_fb_writes()
2015 {
2016 this->current_annotation = "FB write header";
2017 GLboolean header_present = GL_TRUE;
2018 int nr = 0;
2019
2020 if (intel->gen >= 6 &&
2021 !this->kill_emitted &&
2022 c->key.nr_color_regions == 1) {
2023 header_present = false;
2024 }
2025
2026 if (header_present) {
2027 /* m0, m1 header */
2028 nr += 2;
2029 }
2030
2031 if (c->aa_dest_stencil_reg) {
2032 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
2033 fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0))));
2034 }
2035
2036 /* Reserve space for color. It'll be filled in per MRT below. */
2037 int color_mrf = nr;
2038 nr += 4;
2039
2040 if (c->source_depth_to_render_target) {
2041 if (c->computes_depth) {
2042 /* Hand over gl_FragDepth. */
2043 assert(this->frag_depth);
2044 fs_reg depth = *(variable_storage(this->frag_depth));
2045
2046 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++), depth));
2047 } else {
2048 /* Pass through the payload depth. */
2049 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
2050 fs_reg(brw_vec8_grf(c->source_depth_reg, 0))));
2051 }
2052 }
2053
2054 if (c->dest_depth_reg) {
2055 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
2056 fs_reg(brw_vec8_grf(c->dest_depth_reg, 0))));
2057 }
2058
2059 fs_reg color = reg_undef;
2060 if (this->frag_color)
2061 color = *(variable_storage(this->frag_color));
2062 else if (this->frag_data) {
2063 color = *(variable_storage(this->frag_data));
2064 color.type = BRW_REGISTER_TYPE_F;
2065 }
2066
2067 for (int target = 0; target < c->key.nr_color_regions; target++) {
2068 this->current_annotation = talloc_asprintf(this->mem_ctx,
2069 "FB write target %d",
2070 target);
2071 if (this->frag_color || this->frag_data) {
2072 for (int i = 0; i < 4; i++) {
2073 emit(fs_inst(BRW_OPCODE_MOV,
2074 fs_reg(MRF, color_mrf + i),
2075 color));
2076 color.reg_offset++;
2077 }
2078 }
2079
2080 if (this->frag_color)
2081 color.reg_offset -= 4;
2082
2083 fs_inst *inst = emit(fs_inst(FS_OPCODE_FB_WRITE,
2084 reg_undef, reg_undef));
2085 inst->target = target;
2086 inst->base_mrf = 0;
2087 inst->mlen = nr;
2088 if (target == c->key.nr_color_regions - 1)
2089 inst->eot = true;
2090 inst->header_present = header_present;
2091 }
2092
2093 if (c->key.nr_color_regions == 0) {
2094 fs_inst *inst = emit(fs_inst(FS_OPCODE_FB_WRITE,
2095 reg_undef, reg_undef));
2096 inst->base_mrf = 0;
2097 inst->mlen = nr;
2098 inst->eot = true;
2099 inst->header_present = header_present;
2100 }
2101
2102 this->current_annotation = NULL;
2103 }
2104
2105 void
2106 fs_visitor::generate_fb_write(fs_inst *inst)
2107 {
2108 GLboolean eot = inst->eot;
2109 struct brw_reg implied_header;
2110
2111 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
2112 * move, here's g1.
2113 */
2114 brw_push_insn_state(p);
2115 brw_set_mask_control(p, BRW_MASK_DISABLE);
2116 brw_set_compression_control(p, BRW_COMPRESSION_NONE);
2117
2118 if (inst->header_present) {
2119 if (intel->gen >= 6) {
2120 brw_MOV(p,
2121 brw_message_reg(inst->base_mrf),
2122 brw_vec8_grf(0, 0));
2123
2124 if (inst->target > 0) {
2125 /* Set the render target index for choosing BLEND_STATE. */
2126 brw_MOV(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 0, 2),
2127 BRW_REGISTER_TYPE_UD),
2128 brw_imm_ud(inst->target));
2129 }
2130
2131 /* Clear viewport index, render target array index. */
2132 brw_AND(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 0, 0),
2133 BRW_REGISTER_TYPE_UD),
2134 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2135 brw_imm_ud(0xf7ff));
2136
2137 implied_header = brw_null_reg();
2138 } else {
2139 implied_header = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
2140 }
2141
2142 brw_MOV(p,
2143 brw_message_reg(inst->base_mrf + 1),
2144 brw_vec8_grf(1, 0));
2145 } else {
2146 implied_header = brw_null_reg();
2147 }
2148
2149 brw_pop_insn_state(p);
2150
2151 brw_fb_WRITE(p,
2152 8, /* dispatch_width */
2153 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW),
2154 inst->base_mrf,
2155 implied_header,
2156 inst->target,
2157 inst->mlen,
2158 0,
2159 eot,
2160 inst->header_present);
2161 }
2162
2163 void
2164 fs_visitor::generate_linterp(fs_inst *inst,
2165 struct brw_reg dst, struct brw_reg *src)
2166 {
2167 struct brw_reg delta_x = src[0];
2168 struct brw_reg delta_y = src[1];
2169 struct brw_reg interp = src[2];
2170
2171 if (brw->has_pln &&
2172 delta_y.nr == delta_x.nr + 1 &&
2173 (intel->gen >= 6 || (delta_x.nr & 1) == 0)) {
2174 brw_PLN(p, dst, interp, delta_x);
2175 } else {
2176 brw_LINE(p, brw_null_reg(), interp, delta_x);
2177 brw_MAC(p, dst, suboffset(interp, 1), delta_y);
2178 }
2179 }
2180
2181 void
2182 fs_visitor::generate_math(fs_inst *inst,
2183 struct brw_reg dst, struct brw_reg *src)
2184 {
2185 int op;
2186
2187 switch (inst->opcode) {
2188 case FS_OPCODE_RCP:
2189 op = BRW_MATH_FUNCTION_INV;
2190 break;
2191 case FS_OPCODE_RSQ:
2192 op = BRW_MATH_FUNCTION_RSQ;
2193 break;
2194 case FS_OPCODE_SQRT:
2195 op = BRW_MATH_FUNCTION_SQRT;
2196 break;
2197 case FS_OPCODE_EXP2:
2198 op = BRW_MATH_FUNCTION_EXP;
2199 break;
2200 case FS_OPCODE_LOG2:
2201 op = BRW_MATH_FUNCTION_LOG;
2202 break;
2203 case FS_OPCODE_POW:
2204 op = BRW_MATH_FUNCTION_POW;
2205 break;
2206 case FS_OPCODE_SIN:
2207 op = BRW_MATH_FUNCTION_SIN;
2208 break;
2209 case FS_OPCODE_COS:
2210 op = BRW_MATH_FUNCTION_COS;
2211 break;
2212 default:
2213 assert(!"not reached: unknown math function");
2214 op = 0;
2215 break;
2216 }
2217
2218 if (intel->gen >= 6) {
2219 assert(inst->mlen == 0);
2220
2221 if (inst->opcode == FS_OPCODE_POW) {
2222 brw_math2(p, dst, op, src[0], src[1]);
2223 } else {
2224 brw_math(p, dst,
2225 op,
2226 inst->saturate ? BRW_MATH_SATURATE_SATURATE :
2227 BRW_MATH_SATURATE_NONE,
2228 0, src[0],
2229 BRW_MATH_DATA_VECTOR,
2230 BRW_MATH_PRECISION_FULL);
2231 }
2232 } else {
2233 assert(inst->mlen >= 1);
2234
2235 brw_math(p, dst,
2236 op,
2237 inst->saturate ? BRW_MATH_SATURATE_SATURATE :
2238 BRW_MATH_SATURATE_NONE,
2239 inst->base_mrf, src[0],
2240 BRW_MATH_DATA_VECTOR,
2241 BRW_MATH_PRECISION_FULL);
2242 }
2243 }
2244
2245 void
2246 fs_visitor::generate_tex(fs_inst *inst, struct brw_reg dst)
2247 {
2248 int msg_type = -1;
2249 int rlen = 4;
2250 uint32_t simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
2251
2252 if (intel->gen >= 5) {
2253 switch (inst->opcode) {
2254 case FS_OPCODE_TEX:
2255 if (inst->shadow_compare) {
2256 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_COMPARE_GEN5;
2257 } else {
2258 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_GEN5;
2259 }
2260 break;
2261 case FS_OPCODE_TXB:
2262 if (inst->shadow_compare) {
2263 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE_GEN5;
2264 } else {
2265 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_GEN5;
2266 }
2267 break;
2268 }
2269 } else {
2270 switch (inst->opcode) {
2271 case FS_OPCODE_TEX:
2272 /* Note that G45 and older determines shadow compare and dispatch width
2273 * from message length for most messages.
2274 */
2275 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
2276 if (inst->shadow_compare) {
2277 assert(inst->mlen == 6);
2278 } else {
2279 assert(inst->mlen <= 4);
2280 }
2281 break;
2282 case FS_OPCODE_TXB:
2283 if (inst->shadow_compare) {
2284 assert(inst->mlen == 6);
2285 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
2286 } else {
2287 assert(inst->mlen == 9);
2288 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
2289 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
2290 }
2291 break;
2292 }
2293 }
2294 assert(msg_type != -1);
2295
2296 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
2297 rlen = 8;
2298 dst = vec16(dst);
2299 }
2300
2301 brw_SAMPLE(p,
2302 retype(dst, BRW_REGISTER_TYPE_UW),
2303 inst->base_mrf,
2304 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW),
2305 SURF_INDEX_TEXTURE(inst->sampler),
2306 inst->sampler,
2307 WRITEMASK_XYZW,
2308 msg_type,
2309 rlen,
2310 inst->mlen,
2311 0,
2312 1,
2313 simd_mode);
2314 }
2315
2316
2317 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
2318 * looking like:
2319 *
2320 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
2321 *
2322 * and we're trying to produce:
2323 *
2324 * DDX DDY
2325 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
2326 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
2327 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
2328 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
2329 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
2330 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
2331 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
2332 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
2333 *
2334 * and add another set of two more subspans if in 16-pixel dispatch mode.
2335 *
2336 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
2337 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
2338 * pair. But for DDY, it's harder, as we want to produce the pairs swizzled
2339 * between each other. We could probably do it like ddx and swizzle the right
2340 * order later, but bail for now and just produce
2341 * ((ss0.tl - ss0.bl)x4 (ss1.tl - ss1.bl)x4)
2342 */
2343 void
2344 fs_visitor::generate_ddx(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
2345 {
2346 struct brw_reg src0 = brw_reg(src.file, src.nr, 1,
2347 BRW_REGISTER_TYPE_F,
2348 BRW_VERTICAL_STRIDE_2,
2349 BRW_WIDTH_2,
2350 BRW_HORIZONTAL_STRIDE_0,
2351 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2352 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
2353 BRW_REGISTER_TYPE_F,
2354 BRW_VERTICAL_STRIDE_2,
2355 BRW_WIDTH_2,
2356 BRW_HORIZONTAL_STRIDE_0,
2357 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2358 brw_ADD(p, dst, src0, negate(src1));
2359 }
2360
2361 void
2362 fs_visitor::generate_ddy(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
2363 {
2364 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
2365 BRW_REGISTER_TYPE_F,
2366 BRW_VERTICAL_STRIDE_4,
2367 BRW_WIDTH_4,
2368 BRW_HORIZONTAL_STRIDE_0,
2369 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2370 struct brw_reg src1 = brw_reg(src.file, src.nr, 2,
2371 BRW_REGISTER_TYPE_F,
2372 BRW_VERTICAL_STRIDE_4,
2373 BRW_WIDTH_4,
2374 BRW_HORIZONTAL_STRIDE_0,
2375 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2376 brw_ADD(p, dst, src0, negate(src1));
2377 }
2378
2379 void
2380 fs_visitor::generate_discard_not(fs_inst *inst, struct brw_reg mask)
2381 {
2382 if (intel->gen >= 6) {
2383 /* Gen6 no longer has the mask reg for us to just read the
2384 * active channels from. However, cmp updates just the channels
2385 * of the flag reg that are enabled, so we can get at the
2386 * channel enables that way. In this step, make a reg of ones
2387 * we'll compare to.
2388 */
2389 brw_MOV(p, mask, brw_imm_ud(1));
2390 } else {
2391 brw_push_insn_state(p);
2392 brw_set_mask_control(p, BRW_MASK_DISABLE);
2393 brw_NOT(p, mask, brw_mask_reg(1)); /* IMASK */
2394 brw_pop_insn_state(p);
2395 }
2396 }
2397
2398 void
2399 fs_visitor::generate_discard_and(fs_inst *inst, struct brw_reg mask)
2400 {
2401 if (intel->gen >= 6) {
2402 struct brw_reg f0 = brw_flag_reg();
2403 struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
2404
2405 brw_push_insn_state(p);
2406 brw_set_mask_control(p, BRW_MASK_DISABLE);
2407 brw_MOV(p, f0, brw_imm_uw(0xffff)); /* inactive channels undiscarded */
2408 brw_pop_insn_state(p);
2409
2410 brw_CMP(p, retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
2411 BRW_CONDITIONAL_Z, mask, brw_imm_ud(0)); /* active channels fail test */
2412 /* Undo CMP's whacking of predication*/
2413 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
2414
2415 brw_push_insn_state(p);
2416 brw_set_mask_control(p, BRW_MASK_DISABLE);
2417 brw_AND(p, g1, f0, g1);
2418 brw_pop_insn_state(p);
2419 } else {
2420 struct brw_reg g0 = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
2421
2422 mask = brw_uw1_reg(mask.file, mask.nr, 0);
2423
2424 brw_push_insn_state(p);
2425 brw_set_mask_control(p, BRW_MASK_DISABLE);
2426 brw_AND(p, g0, mask, g0);
2427 brw_pop_insn_state(p);
2428 }
2429 }
2430
2431 void
2432 fs_visitor::generate_spill(fs_inst *inst, struct brw_reg src)
2433 {
2434 assert(inst->mlen != 0);
2435
2436 brw_MOV(p,
2437 retype(brw_message_reg(inst->base_mrf + 1), BRW_REGISTER_TYPE_UD),
2438 retype(src, BRW_REGISTER_TYPE_UD));
2439 brw_oword_block_write_scratch(p, brw_message_reg(inst->base_mrf), 1,
2440 inst->offset);
2441 }
2442
2443 void
2444 fs_visitor::generate_unspill(fs_inst *inst, struct brw_reg dst)
2445 {
2446 assert(inst->mlen != 0);
2447
2448 /* Clear any post destination dependencies that would be ignored by
2449 * the block read. See the B-Spec for pre-gen5 send instruction.
2450 *
2451 * This could use a better solution, since texture sampling and
2452 * math reads could potentially run into it as well -- anywhere
2453 * that we have a SEND with a destination that is a register that
2454 * was written but not read within the last N instructions (what's
2455 * N? unsure). This is rare because of dead code elimination, but
2456 * not impossible.
2457 */
2458 if (intel->gen == 4 && !intel->is_g4x)
2459 brw_MOV(p, brw_null_reg(), dst);
2460
2461 brw_oword_block_read_scratch(p, dst, brw_message_reg(inst->base_mrf), 1,
2462 inst->offset);
2463
2464 if (intel->gen == 4 && !intel->is_g4x) {
2465 /* gen4 errata: destination from a send can't be used as a
2466 * destination until it's been read. Just read it so we don't
2467 * have to worry.
2468 */
2469 brw_MOV(p, brw_null_reg(), dst);
2470 }
2471 }
2472
2473
2474 void
2475 fs_visitor::generate_pull_constant_load(fs_inst *inst, struct brw_reg dst)
2476 {
2477 assert(inst->mlen != 0);
2478
2479 /* Clear any post destination dependencies that would be ignored by
2480 * the block read. See the B-Spec for pre-gen5 send instruction.
2481 *
2482 * This could use a better solution, since texture sampling and
2483 * math reads could potentially run into it as well -- anywhere
2484 * that we have a SEND with a destination that is a register that
2485 * was written but not read within the last N instructions (what's
2486 * N? unsure). This is rare because of dead code elimination, but
2487 * not impossible.
2488 */
2489 if (intel->gen == 4 && !intel->is_g4x)
2490 brw_MOV(p, brw_null_reg(), dst);
2491
2492 brw_oword_block_read(p, dst, brw_message_reg(inst->base_mrf),
2493 inst->offset, SURF_INDEX_FRAG_CONST_BUFFER);
2494
2495 if (intel->gen == 4 && !intel->is_g4x) {
2496 /* gen4 errata: destination from a send can't be used as a
2497 * destination until it's been read. Just read it so we don't
2498 * have to worry.
2499 */
2500 brw_MOV(p, brw_null_reg(), dst);
2501 }
2502 }
2503
2504 /**
2505 * To be called after the last _mesa_add_state_reference() call, to
2506 * set up prog_data.param[] for assign_curb_setup() and
2507 * setup_pull_constants().
2508 */
2509 void
2510 fs_visitor::setup_paramvalues_refs()
2511 {
2512 /* Set up the pointers to ParamValues now that that array is finalized. */
2513 for (unsigned int i = 0; i < c->prog_data.nr_params; i++) {
2514 c->prog_data.param[i] =
2515 fp->Base.Parameters->ParameterValues[this->param_index[i]] +
2516 this->param_offset[i];
2517 }
2518 }
2519
2520 void
2521 fs_visitor::assign_curb_setup()
2522 {
2523 c->prog_data.first_curbe_grf = c->nr_payload_regs;
2524 c->prog_data.curb_read_length = ALIGN(c->prog_data.nr_params, 8) / 8;
2525
2526 /* Map the offsets in the UNIFORM file to fixed HW regs. */
2527 foreach_iter(exec_list_iterator, iter, this->instructions) {
2528 fs_inst *inst = (fs_inst *)iter.get();
2529
2530 for (unsigned int i = 0; i < 3; i++) {
2531 if (inst->src[i].file == UNIFORM) {
2532 int constant_nr = inst->src[i].hw_reg + inst->src[i].reg_offset;
2533 struct brw_reg brw_reg = brw_vec1_grf(c->prog_data.first_curbe_grf +
2534 constant_nr / 8,
2535 constant_nr % 8);
2536
2537 inst->src[i].file = FIXED_HW_REG;
2538 inst->src[i].fixed_hw_reg = retype(brw_reg, inst->src[i].type);
2539 }
2540 }
2541 }
2542 }
2543
2544 void
2545 fs_visitor::calculate_urb_setup()
2546 {
2547 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
2548 urb_setup[i] = -1;
2549 }
2550
2551 int urb_next = 0;
2552 /* Figure out where each of the incoming setup attributes lands. */
2553 if (intel->gen >= 6) {
2554 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
2555 if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(i)) {
2556 urb_setup[i] = urb_next++;
2557 }
2558 }
2559 } else {
2560 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
2561 for (unsigned int i = 0; i < VERT_RESULT_MAX; i++) {
2562 if (c->key.vp_outputs_written & BITFIELD64_BIT(i)) {
2563 int fp_index;
2564
2565 if (i >= VERT_RESULT_VAR0)
2566 fp_index = i - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
2567 else if (i <= VERT_RESULT_TEX7)
2568 fp_index = i;
2569 else
2570 fp_index = -1;
2571
2572 if (fp_index >= 0)
2573 urb_setup[fp_index] = urb_next++;
2574 }
2575 }
2576 }
2577
2578 /* Each attribute is 4 setup channels, each of which is half a reg. */
2579 c->prog_data.urb_read_length = urb_next * 2;
2580 }
2581
2582 void
2583 fs_visitor::assign_urb_setup()
2584 {
2585 int urb_start = c->prog_data.first_curbe_grf + c->prog_data.curb_read_length;
2586
2587 /* Offset all the urb_setup[] index by the actual position of the
2588 * setup regs, now that the location of the constants has been chosen.
2589 */
2590 foreach_iter(exec_list_iterator, iter, this->instructions) {
2591 fs_inst *inst = (fs_inst *)iter.get();
2592
2593 if (inst->opcode == FS_OPCODE_LINTERP) {
2594 assert(inst->src[2].file == FIXED_HW_REG);
2595 inst->src[2].fixed_hw_reg.nr += urb_start;
2596 }
2597
2598 if (inst->opcode == FS_OPCODE_CINTERP) {
2599 assert(inst->src[0].file == FIXED_HW_REG);
2600 inst->src[0].fixed_hw_reg.nr += urb_start;
2601 }
2602 }
2603
2604 this->first_non_payload_grf = urb_start + c->prog_data.urb_read_length;
2605 }
2606
2607 /**
2608 * Split large virtual GRFs into separate components if we can.
2609 *
2610 * This is mostly duplicated with what brw_fs_vector_splitting does,
2611 * but that's really conservative because it's afraid of doing
2612 * splitting that doesn't result in real progress after the rest of
2613 * the optimization phases, which would cause infinite looping in
2614 * optimization. We can do it once here, safely. This also has the
2615 * opportunity to split interpolated values, or maybe even uniforms,
2616 * which we don't have at the IR level.
2617 *
2618 * We want to split, because virtual GRFs are what we register
2619 * allocate and spill (due to contiguousness requirements for some
2620 * instructions), and they're what we naturally generate in the
2621 * codegen process, but most virtual GRFs don't actually need to be
2622 * contiguous sets of GRFs. If we split, we'll end up with reduced
2623 * live intervals and better dead code elimination and coalescing.
2624 */
2625 void
2626 fs_visitor::split_virtual_grfs()
2627 {
2628 int num_vars = this->virtual_grf_next;
2629 bool split_grf[num_vars];
2630 int new_virtual_grf[num_vars];
2631
2632 /* Try to split anything > 0 sized. */
2633 for (int i = 0; i < num_vars; i++) {
2634 if (this->virtual_grf_sizes[i] != 1)
2635 split_grf[i] = true;
2636 else
2637 split_grf[i] = false;
2638 }
2639
2640 if (brw->has_pln) {
2641 /* PLN opcodes rely on the delta_xy being contiguous. */
2642 split_grf[this->delta_x.reg] = false;
2643 }
2644
2645 foreach_iter(exec_list_iterator, iter, this->instructions) {
2646 fs_inst *inst = (fs_inst *)iter.get();
2647
2648 /* Texturing produces 4 contiguous registers, so no splitting. */
2649 if (inst->is_tex()) {
2650 split_grf[inst->dst.reg] = false;
2651 }
2652 }
2653
2654 /* Allocate new space for split regs. Note that the virtual
2655 * numbers will be contiguous.
2656 */
2657 for (int i = 0; i < num_vars; i++) {
2658 if (split_grf[i]) {
2659 new_virtual_grf[i] = virtual_grf_alloc(1);
2660 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
2661 int reg = virtual_grf_alloc(1);
2662 assert(reg == new_virtual_grf[i] + j - 1);
2663 (void) reg;
2664 }
2665 this->virtual_grf_sizes[i] = 1;
2666 }
2667 }
2668
2669 foreach_iter(exec_list_iterator, iter, this->instructions) {
2670 fs_inst *inst = (fs_inst *)iter.get();
2671
2672 if (inst->dst.file == GRF &&
2673 split_grf[inst->dst.reg] &&
2674 inst->dst.reg_offset != 0) {
2675 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
2676 inst->dst.reg_offset - 1);
2677 inst->dst.reg_offset = 0;
2678 }
2679 for (int i = 0; i < 3; i++) {
2680 if (inst->src[i].file == GRF &&
2681 split_grf[inst->src[i].reg] &&
2682 inst->src[i].reg_offset != 0) {
2683 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
2684 inst->src[i].reg_offset - 1);
2685 inst->src[i].reg_offset = 0;
2686 }
2687 }
2688 }
2689 this->live_intervals_valid = false;
2690 }
2691
2692 /**
2693 * Choose accesses from the UNIFORM file to demote to using the pull
2694 * constant buffer.
2695 *
2696 * We allow a fragment shader to have more than the specified minimum
2697 * maximum number of fragment shader uniform components (64). If
2698 * there are too many of these, they'd fill up all of register space.
2699 * So, this will push some of them out to the pull constant buffer and
2700 * update the program to load them.
2701 */
2702 void
2703 fs_visitor::setup_pull_constants()
2704 {
2705 /* Only allow 16 registers (128 uniform components) as push constants. */
2706 unsigned int max_uniform_components = 16 * 8;
2707 if (c->prog_data.nr_params <= max_uniform_components)
2708 return;
2709
2710 /* Just demote the end of the list. We could probably do better
2711 * here, demoting things that are rarely used in the program first.
2712 */
2713 int pull_uniform_base = max_uniform_components;
2714 int pull_uniform_count = c->prog_data.nr_params - pull_uniform_base;
2715
2716 foreach_iter(exec_list_iterator, iter, this->instructions) {
2717 fs_inst *inst = (fs_inst *)iter.get();
2718
2719 for (int i = 0; i < 3; i++) {
2720 if (inst->src[i].file != UNIFORM)
2721 continue;
2722
2723 int uniform_nr = inst->src[i].hw_reg + inst->src[i].reg_offset;
2724 if (uniform_nr < pull_uniform_base)
2725 continue;
2726
2727 fs_reg dst = fs_reg(this, glsl_type::float_type);
2728 fs_inst *pull = new(mem_ctx) fs_inst(FS_OPCODE_PULL_CONSTANT_LOAD,
2729 dst);
2730 pull->offset = ((uniform_nr - pull_uniform_base) * 4) & ~15;
2731 pull->ir = inst->ir;
2732 pull->annotation = inst->annotation;
2733 pull->base_mrf = 14;
2734 pull->mlen = 1;
2735
2736 inst->insert_before(pull);
2737
2738 inst->src[i].file = GRF;
2739 inst->src[i].reg = dst.reg;
2740 inst->src[i].reg_offset = 0;
2741 inst->src[i].smear = (uniform_nr - pull_uniform_base) & 3;
2742 }
2743 }
2744
2745 for (int i = 0; i < pull_uniform_count; i++) {
2746 c->prog_data.pull_param[i] = c->prog_data.param[pull_uniform_base + i];
2747 c->prog_data.pull_param_convert[i] =
2748 c->prog_data.param_convert[pull_uniform_base + i];
2749 }
2750 c->prog_data.nr_params -= pull_uniform_count;
2751 c->prog_data.nr_pull_params = pull_uniform_count;
2752 }
2753
2754 void
2755 fs_visitor::calculate_live_intervals()
2756 {
2757 int num_vars = this->virtual_grf_next;
2758 int *def = talloc_array(mem_ctx, int, num_vars);
2759 int *use = talloc_array(mem_ctx, int, num_vars);
2760 int loop_depth = 0;
2761 int loop_start = 0;
2762 int bb_header_ip = 0;
2763
2764 if (this->live_intervals_valid)
2765 return;
2766
2767 for (int i = 0; i < num_vars; i++) {
2768 def[i] = MAX_INSTRUCTION;
2769 use[i] = -1;
2770 }
2771
2772 int ip = 0;
2773 foreach_iter(exec_list_iterator, iter, this->instructions) {
2774 fs_inst *inst = (fs_inst *)iter.get();
2775
2776 if (inst->opcode == BRW_OPCODE_DO) {
2777 if (loop_depth++ == 0)
2778 loop_start = ip;
2779 } else if (inst->opcode == BRW_OPCODE_WHILE) {
2780 loop_depth--;
2781
2782 if (loop_depth == 0) {
2783 /* Patches up the use of vars marked for being live across
2784 * the whole loop.
2785 */
2786 for (int i = 0; i < num_vars; i++) {
2787 if (use[i] == loop_start) {
2788 use[i] = ip;
2789 }
2790 }
2791 }
2792 } else {
2793 for (unsigned int i = 0; i < 3; i++) {
2794 if (inst->src[i].file == GRF && inst->src[i].reg != 0) {
2795 int reg = inst->src[i].reg;
2796
2797 if (!loop_depth || (this->virtual_grf_sizes[reg] == 1 &&
2798 def[reg] >= bb_header_ip)) {
2799 use[reg] = ip;
2800 } else {
2801 def[reg] = MIN2(loop_start, def[reg]);
2802 use[reg] = loop_start;
2803
2804 /* Nobody else is going to go smash our start to
2805 * later in the loop now, because def[reg] now
2806 * points before the bb header.
2807 */
2808 }
2809 }
2810 }
2811 if (inst->dst.file == GRF && inst->dst.reg != 0) {
2812 int reg = inst->dst.reg;
2813
2814 if (!loop_depth || (this->virtual_grf_sizes[reg] == 1 &&
2815 !inst->predicated)) {
2816 def[reg] = MIN2(def[reg], ip);
2817 } else {
2818 def[reg] = MIN2(def[reg], loop_start);
2819 }
2820 }
2821 }
2822
2823 ip++;
2824
2825 /* Set the basic block header IP. This is used for determining
2826 * if a complete def of single-register virtual GRF in a loop
2827 * dominates a use in the same basic block. It's a quick way to
2828 * reduce the live interval range of most register used in a
2829 * loop.
2830 */
2831 if (inst->opcode == BRW_OPCODE_IF ||
2832 inst->opcode == BRW_OPCODE_ELSE ||
2833 inst->opcode == BRW_OPCODE_ENDIF ||
2834 inst->opcode == BRW_OPCODE_DO ||
2835 inst->opcode == BRW_OPCODE_WHILE ||
2836 inst->opcode == BRW_OPCODE_BREAK ||
2837 inst->opcode == BRW_OPCODE_CONTINUE) {
2838 bb_header_ip = ip;
2839 }
2840 }
2841
2842 talloc_free(this->virtual_grf_def);
2843 talloc_free(this->virtual_grf_use);
2844 this->virtual_grf_def = def;
2845 this->virtual_grf_use = use;
2846
2847 this->live_intervals_valid = true;
2848 }
2849
2850 /**
2851 * Attempts to move immediate constants into the immediate
2852 * constant slot of following instructions.
2853 *
2854 * Immediate constants are a bit tricky -- they have to be in the last
2855 * operand slot, you can't do abs/negate on them,
2856 */
2857
2858 bool
2859 fs_visitor::propagate_constants()
2860 {
2861 bool progress = false;
2862
2863 calculate_live_intervals();
2864
2865 foreach_iter(exec_list_iterator, iter, this->instructions) {
2866 fs_inst *inst = (fs_inst *)iter.get();
2867
2868 if (inst->opcode != BRW_OPCODE_MOV ||
2869 inst->predicated ||
2870 inst->dst.file != GRF || inst->src[0].file != IMM ||
2871 inst->dst.type != inst->src[0].type)
2872 continue;
2873
2874 /* Don't bother with cases where we should have had the
2875 * operation on the constant folded in GLSL already.
2876 */
2877 if (inst->saturate)
2878 continue;
2879
2880 /* Found a move of a constant to a GRF. Find anything else using the GRF
2881 * before it's written, and replace it with the constant if we can.
2882 */
2883 exec_list_iterator scan_iter = iter;
2884 scan_iter.next();
2885 for (; scan_iter.has_next(); scan_iter.next()) {
2886 fs_inst *scan_inst = (fs_inst *)scan_iter.get();
2887
2888 if (scan_inst->opcode == BRW_OPCODE_DO ||
2889 scan_inst->opcode == BRW_OPCODE_WHILE ||
2890 scan_inst->opcode == BRW_OPCODE_ELSE ||
2891 scan_inst->opcode == BRW_OPCODE_ENDIF) {
2892 break;
2893 }
2894
2895 for (int i = 2; i >= 0; i--) {
2896 if (scan_inst->src[i].file != GRF ||
2897 scan_inst->src[i].reg != inst->dst.reg ||
2898 scan_inst->src[i].reg_offset != inst->dst.reg_offset)
2899 continue;
2900
2901 /* Don't bother with cases where we should have had the
2902 * operation on the constant folded in GLSL already.
2903 */
2904 if (scan_inst->src[i].negate || scan_inst->src[i].abs)
2905 continue;
2906
2907 switch (scan_inst->opcode) {
2908 case BRW_OPCODE_MOV:
2909 scan_inst->src[i] = inst->src[0];
2910 progress = true;
2911 break;
2912
2913 case BRW_OPCODE_MUL:
2914 case BRW_OPCODE_ADD:
2915 if (i == 1) {
2916 scan_inst->src[i] = inst->src[0];
2917 progress = true;
2918 } else if (i == 0 && scan_inst->src[1].file != IMM) {
2919 /* Fit this constant in by commuting the operands */
2920 scan_inst->src[0] = scan_inst->src[1];
2921 scan_inst->src[1] = inst->src[0];
2922 progress = true;
2923 }
2924 break;
2925 case BRW_OPCODE_CMP:
2926 case BRW_OPCODE_SEL:
2927 if (i == 1) {
2928 scan_inst->src[i] = inst->src[0];
2929 progress = true;
2930 }
2931 }
2932 }
2933
2934 if (scan_inst->dst.file == GRF &&
2935 scan_inst->dst.reg == inst->dst.reg &&
2936 (scan_inst->dst.reg_offset == inst->dst.reg_offset ||
2937 scan_inst->is_tex())) {
2938 break;
2939 }
2940 }
2941 }
2942
2943 if (progress)
2944 this->live_intervals_valid = false;
2945
2946 return progress;
2947 }
2948 /**
2949 * Must be called after calculate_live_intervales() to remove unused
2950 * writes to registers -- register allocation will fail otherwise
2951 * because something deffed but not used won't be considered to
2952 * interfere with other regs.
2953 */
2954 bool
2955 fs_visitor::dead_code_eliminate()
2956 {
2957 bool progress = false;
2958 int pc = 0;
2959
2960 calculate_live_intervals();
2961
2962 foreach_iter(exec_list_iterator, iter, this->instructions) {
2963 fs_inst *inst = (fs_inst *)iter.get();
2964
2965 if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
2966 inst->remove();
2967 progress = true;
2968 }
2969
2970 pc++;
2971 }
2972
2973 if (progress)
2974 live_intervals_valid = false;
2975
2976 return progress;
2977 }
2978
2979 bool
2980 fs_visitor::register_coalesce()
2981 {
2982 bool progress = false;
2983 int if_depth = 0;
2984 int loop_depth = 0;
2985
2986 foreach_iter(exec_list_iterator, iter, this->instructions) {
2987 fs_inst *inst = (fs_inst *)iter.get();
2988
2989 /* Make sure that we dominate the instructions we're going to
2990 * scan for interfering with our coalescing, or we won't have
2991 * scanned enough to see if anything interferes with our
2992 * coalescing. We don't dominate the following instructions if
2993 * we're in a loop or an if block.
2994 */
2995 switch (inst->opcode) {
2996 case BRW_OPCODE_DO:
2997 loop_depth++;
2998 break;
2999 case BRW_OPCODE_WHILE:
3000 loop_depth--;
3001 break;
3002 case BRW_OPCODE_IF:
3003 if_depth++;
3004 break;
3005 case BRW_OPCODE_ENDIF:
3006 if_depth--;
3007 break;
3008 }
3009 if (loop_depth || if_depth)
3010 continue;
3011
3012 if (inst->opcode != BRW_OPCODE_MOV ||
3013 inst->predicated ||
3014 inst->saturate ||
3015 inst->dst.file != GRF || inst->src[0].file != GRF ||
3016 inst->dst.type != inst->src[0].type)
3017 continue;
3018
3019 /* Found a move of a GRF to a GRF. Let's see if we can coalesce
3020 * them: check for no writes to either one until the exit of the
3021 * program.
3022 */
3023 bool interfered = false;
3024 exec_list_iterator scan_iter = iter;
3025 scan_iter.next();
3026 for (; scan_iter.has_next(); scan_iter.next()) {
3027 fs_inst *scan_inst = (fs_inst *)scan_iter.get();
3028
3029 if (scan_inst->dst.file == GRF) {
3030 if (scan_inst->dst.reg == inst->dst.reg &&
3031 (scan_inst->dst.reg_offset == inst->dst.reg_offset ||
3032 scan_inst->is_tex())) {
3033 interfered = true;
3034 break;
3035 }
3036 if (scan_inst->dst.reg == inst->src[0].reg &&
3037 (scan_inst->dst.reg_offset == inst->src[0].reg_offset ||
3038 scan_inst->is_tex())) {
3039 interfered = true;
3040 break;
3041 }
3042 }
3043 }
3044 if (interfered) {
3045 continue;
3046 }
3047
3048 /* Rewrite the later usage to point at the source of the move to
3049 * be removed.
3050 */
3051 for (exec_list_iterator scan_iter = iter; scan_iter.has_next();
3052 scan_iter.next()) {
3053 fs_inst *scan_inst = (fs_inst *)scan_iter.get();
3054
3055 for (int i = 0; i < 3; i++) {
3056 if (scan_inst->src[i].file == GRF &&
3057 scan_inst->src[i].reg == inst->dst.reg &&
3058 scan_inst->src[i].reg_offset == inst->dst.reg_offset) {
3059 scan_inst->src[i].reg = inst->src[0].reg;
3060 scan_inst->src[i].reg_offset = inst->src[0].reg_offset;
3061 scan_inst->src[i].abs |= inst->src[0].abs;
3062 scan_inst->src[i].negate ^= inst->src[0].negate;
3063 scan_inst->src[i].smear = inst->src[0].smear;
3064 }
3065 }
3066 }
3067
3068 inst->remove();
3069 progress = true;
3070 }
3071
3072 if (progress)
3073 live_intervals_valid = false;
3074
3075 return progress;
3076 }
3077
3078
3079 bool
3080 fs_visitor::compute_to_mrf()
3081 {
3082 bool progress = false;
3083 int next_ip = 0;
3084
3085 calculate_live_intervals();
3086
3087 foreach_iter(exec_list_iterator, iter, this->instructions) {
3088 fs_inst *inst = (fs_inst *)iter.get();
3089
3090 int ip = next_ip;
3091 next_ip++;
3092
3093 if (inst->opcode != BRW_OPCODE_MOV ||
3094 inst->predicated ||
3095 inst->dst.file != MRF || inst->src[0].file != GRF ||
3096 inst->dst.type != inst->src[0].type ||
3097 inst->src[0].abs || inst->src[0].negate || inst->src[0].smear != -1)
3098 continue;
3099
3100 /* Can't compute-to-MRF this GRF if someone else was going to
3101 * read it later.
3102 */
3103 if (this->virtual_grf_use[inst->src[0].reg] > ip)
3104 continue;
3105
3106 /* Found a move of a GRF to a MRF. Let's see if we can go
3107 * rewrite the thing that made this GRF to write into the MRF.
3108 */
3109 fs_inst *scan_inst;
3110 for (scan_inst = (fs_inst *)inst->prev;
3111 scan_inst->prev != NULL;
3112 scan_inst = (fs_inst *)scan_inst->prev) {
3113 if (scan_inst->dst.file == GRF &&
3114 scan_inst->dst.reg == inst->src[0].reg) {
3115 /* Found the last thing to write our reg we want to turn
3116 * into a compute-to-MRF.
3117 */
3118
3119 if (scan_inst->is_tex()) {
3120 /* texturing writes several continuous regs, so we can't
3121 * compute-to-mrf that.
3122 */
3123 break;
3124 }
3125
3126 /* If it's predicated, it (probably) didn't populate all
3127 * the channels.
3128 */
3129 if (scan_inst->predicated)
3130 break;
3131
3132 /* SEND instructions can't have MRF as a destination. */
3133 if (scan_inst->mlen)
3134 break;
3135
3136 if (intel->gen >= 6) {
3137 /* gen6 math instructions must have the destination be
3138 * GRF, so no compute-to-MRF for them.
3139 */
3140 if (scan_inst->is_math()) {
3141 break;
3142 }
3143 }
3144
3145 if (scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
3146 /* Found the creator of our MRF's source value. */
3147 scan_inst->dst.file = MRF;
3148 scan_inst->dst.hw_reg = inst->dst.hw_reg;
3149 scan_inst->saturate |= inst->saturate;
3150 inst->remove();
3151 progress = true;
3152 }
3153 break;
3154 }
3155
3156 /* We don't handle flow control here. Most computation of
3157 * values that end up in MRFs are shortly before the MRF
3158 * write anyway.
3159 */
3160 if (scan_inst->opcode == BRW_OPCODE_DO ||
3161 scan_inst->opcode == BRW_OPCODE_WHILE ||
3162 scan_inst->opcode == BRW_OPCODE_ELSE ||
3163 scan_inst->opcode == BRW_OPCODE_ENDIF) {
3164 break;
3165 }
3166
3167 /* You can't read from an MRF, so if someone else reads our
3168 * MRF's source GRF that we wanted to rewrite, that stops us.
3169 */
3170 bool interfered = false;
3171 for (int i = 0; i < 3; i++) {
3172 if (scan_inst->src[i].file == GRF &&
3173 scan_inst->src[i].reg == inst->src[0].reg &&
3174 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
3175 interfered = true;
3176 }
3177 }
3178 if (interfered)
3179 break;
3180
3181 if (scan_inst->dst.file == MRF &&
3182 scan_inst->dst.hw_reg == inst->dst.hw_reg) {
3183 /* Somebody else wrote our MRF here, so we can't can't
3184 * compute-to-MRF before that.
3185 */
3186 break;
3187 }
3188
3189 if (scan_inst->mlen > 0) {
3190 /* Found a SEND instruction, which means that there are
3191 * live values in MRFs from base_mrf to base_mrf +
3192 * scan_inst->mlen - 1. Don't go pushing our MRF write up
3193 * above it.
3194 */
3195 if (inst->dst.hw_reg >= scan_inst->base_mrf &&
3196 inst->dst.hw_reg < scan_inst->base_mrf + scan_inst->mlen) {
3197 break;
3198 }
3199 }
3200 }
3201 }
3202
3203 return progress;
3204 }
3205
3206 /**
3207 * Walks through basic blocks, locking for repeated MRF writes and
3208 * removing the later ones.
3209 */
3210 bool
3211 fs_visitor::remove_duplicate_mrf_writes()
3212 {
3213 fs_inst *last_mrf_move[16];
3214 bool progress = false;
3215
3216 memset(last_mrf_move, 0, sizeof(last_mrf_move));
3217
3218 foreach_iter(exec_list_iterator, iter, this->instructions) {
3219 fs_inst *inst = (fs_inst *)iter.get();
3220
3221 switch (inst->opcode) {
3222 case BRW_OPCODE_DO:
3223 case BRW_OPCODE_WHILE:
3224 case BRW_OPCODE_IF:
3225 case BRW_OPCODE_ELSE:
3226 case BRW_OPCODE_ENDIF:
3227 memset(last_mrf_move, 0, sizeof(last_mrf_move));
3228 continue;
3229 default:
3230 break;
3231 }
3232
3233 if (inst->opcode == BRW_OPCODE_MOV &&
3234 inst->dst.file == MRF) {
3235 fs_inst *prev_inst = last_mrf_move[inst->dst.hw_reg];
3236 if (prev_inst && inst->equals(prev_inst)) {
3237 inst->remove();
3238 progress = true;
3239 continue;
3240 }
3241 }
3242
3243 /* Clear out the last-write records for MRFs that were overwritten. */
3244 if (inst->dst.file == MRF) {
3245 last_mrf_move[inst->dst.hw_reg] = NULL;
3246 }
3247
3248 if (inst->mlen > 0) {
3249 /* Found a SEND instruction, which will include two or fewer
3250 * implied MRF writes. We could do better here.
3251 */
3252 for (int i = 0; i < implied_mrf_writes(inst); i++) {
3253 last_mrf_move[inst->base_mrf + i] = NULL;
3254 }
3255 }
3256
3257 /* Clear out any MRF move records whose sources got overwritten. */
3258 if (inst->dst.file == GRF) {
3259 for (unsigned int i = 0; i < Elements(last_mrf_move); i++) {
3260 if (last_mrf_move[i] &&
3261 last_mrf_move[i]->src[0].reg == inst->dst.reg) {
3262 last_mrf_move[i] = NULL;
3263 }
3264 }
3265 }
3266
3267 if (inst->opcode == BRW_OPCODE_MOV &&
3268 inst->dst.file == MRF &&
3269 inst->src[0].file == GRF &&
3270 !inst->predicated) {
3271 last_mrf_move[inst->dst.hw_reg] = inst;
3272 }
3273 }
3274
3275 return progress;
3276 }
3277
3278 bool
3279 fs_visitor::virtual_grf_interferes(int a, int b)
3280 {
3281 int start = MAX2(this->virtual_grf_def[a], this->virtual_grf_def[b]);
3282 int end = MIN2(this->virtual_grf_use[a], this->virtual_grf_use[b]);
3283
3284 /* We can't handle dead register writes here, without iterating
3285 * over the whole instruction stream to find every single dead
3286 * write to that register to compare to the live interval of the
3287 * other register. Just assert that dead_code_eliminate() has been
3288 * called.
3289 */
3290 assert((this->virtual_grf_use[a] != -1 ||
3291 this->virtual_grf_def[a] == MAX_INSTRUCTION) &&
3292 (this->virtual_grf_use[b] != -1 ||
3293 this->virtual_grf_def[b] == MAX_INSTRUCTION));
3294
3295 return start < end;
3296 }
3297
3298 static struct brw_reg brw_reg_from_fs_reg(fs_reg *reg)
3299 {
3300 struct brw_reg brw_reg;
3301
3302 switch (reg->file) {
3303 case GRF:
3304 case ARF:
3305 case MRF:
3306 if (reg->smear == -1) {
3307 brw_reg = brw_vec8_reg(reg->file,
3308 reg->hw_reg, 0);
3309 } else {
3310 brw_reg = brw_vec1_reg(reg->file,
3311 reg->hw_reg, reg->smear);
3312 }
3313 brw_reg = retype(brw_reg, reg->type);
3314 break;
3315 case IMM:
3316 switch (reg->type) {
3317 case BRW_REGISTER_TYPE_F:
3318 brw_reg = brw_imm_f(reg->imm.f);
3319 break;
3320 case BRW_REGISTER_TYPE_D:
3321 brw_reg = brw_imm_d(reg->imm.i);
3322 break;
3323 case BRW_REGISTER_TYPE_UD:
3324 brw_reg = brw_imm_ud(reg->imm.u);
3325 break;
3326 default:
3327 assert(!"not reached");
3328 brw_reg = brw_null_reg();
3329 break;
3330 }
3331 break;
3332 case FIXED_HW_REG:
3333 brw_reg = reg->fixed_hw_reg;
3334 break;
3335 case BAD_FILE:
3336 /* Probably unused. */
3337 brw_reg = brw_null_reg();
3338 break;
3339 case UNIFORM:
3340 assert(!"not reached");
3341 brw_reg = brw_null_reg();
3342 break;
3343 default:
3344 assert(!"not reached");
3345 brw_reg = brw_null_reg();
3346 break;
3347 }
3348 if (reg->abs)
3349 brw_reg = brw_abs(brw_reg);
3350 if (reg->negate)
3351 brw_reg = negate(brw_reg);
3352
3353 return brw_reg;
3354 }
3355
3356 void
3357 fs_visitor::generate_code()
3358 {
3359 int last_native_inst = 0;
3360 struct brw_instruction *if_stack[16], *loop_stack[16];
3361 int if_stack_depth = 0, loop_stack_depth = 0;
3362 int if_depth_in_loop[16];
3363 const char *last_annotation_string = NULL;
3364 ir_instruction *last_annotation_ir = NULL;
3365
3366 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
3367 printf("Native code for fragment shader %d:\n",
3368 ctx->Shader.CurrentFragmentProgram->Name);
3369 }
3370
3371 if_depth_in_loop[loop_stack_depth] = 0;
3372
3373 memset(&if_stack, 0, sizeof(if_stack));
3374 foreach_iter(exec_list_iterator, iter, this->instructions) {
3375 fs_inst *inst = (fs_inst *)iter.get();
3376 struct brw_reg src[3], dst;
3377
3378 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
3379 if (last_annotation_ir != inst->ir) {
3380 last_annotation_ir = inst->ir;
3381 if (last_annotation_ir) {
3382 printf(" ");
3383 last_annotation_ir->print();
3384 printf("\n");
3385 }
3386 }
3387 if (last_annotation_string != inst->annotation) {
3388 last_annotation_string = inst->annotation;
3389 if (last_annotation_string)
3390 printf(" %s\n", last_annotation_string);
3391 }
3392 }
3393
3394 for (unsigned int i = 0; i < 3; i++) {
3395 src[i] = brw_reg_from_fs_reg(&inst->src[i]);
3396 }
3397 dst = brw_reg_from_fs_reg(&inst->dst);
3398
3399 brw_set_conditionalmod(p, inst->conditional_mod);
3400 brw_set_predicate_control(p, inst->predicated);
3401 brw_set_saturate(p, inst->saturate);
3402
3403 switch (inst->opcode) {
3404 case BRW_OPCODE_MOV:
3405 brw_MOV(p, dst, src[0]);
3406 break;
3407 case BRW_OPCODE_ADD:
3408 brw_ADD(p, dst, src[0], src[1]);
3409 break;
3410 case BRW_OPCODE_MUL:
3411 brw_MUL(p, dst, src[0], src[1]);
3412 break;
3413
3414 case BRW_OPCODE_FRC:
3415 brw_FRC(p, dst, src[0]);
3416 break;
3417 case BRW_OPCODE_RNDD:
3418 brw_RNDD(p, dst, src[0]);
3419 break;
3420 case BRW_OPCODE_RNDE:
3421 brw_RNDE(p, dst, src[0]);
3422 break;
3423 case BRW_OPCODE_RNDZ:
3424 brw_RNDZ(p, dst, src[0]);
3425 break;
3426
3427 case BRW_OPCODE_AND:
3428 brw_AND(p, dst, src[0], src[1]);
3429 break;
3430 case BRW_OPCODE_OR:
3431 brw_OR(p, dst, src[0], src[1]);
3432 break;
3433 case BRW_OPCODE_XOR:
3434 brw_XOR(p, dst, src[0], src[1]);
3435 break;
3436 case BRW_OPCODE_NOT:
3437 brw_NOT(p, dst, src[0]);
3438 break;
3439 case BRW_OPCODE_ASR:
3440 brw_ASR(p, dst, src[0], src[1]);
3441 break;
3442 case BRW_OPCODE_SHR:
3443 brw_SHR(p, dst, src[0], src[1]);
3444 break;
3445 case BRW_OPCODE_SHL:
3446 brw_SHL(p, dst, src[0], src[1]);
3447 break;
3448
3449 case BRW_OPCODE_CMP:
3450 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
3451 break;
3452 case BRW_OPCODE_SEL:
3453 brw_SEL(p, dst, src[0], src[1]);
3454 break;
3455
3456 case BRW_OPCODE_IF:
3457 assert(if_stack_depth < 16);
3458 if (inst->src[0].file != BAD_FILE) {
3459 assert(intel->gen >= 6);
3460 if_stack[if_stack_depth] = brw_IF_gen6(p, inst->conditional_mod, src[0], src[1]);
3461 } else {
3462 if_stack[if_stack_depth] = brw_IF(p, BRW_EXECUTE_8);
3463 }
3464 if_depth_in_loop[loop_stack_depth]++;
3465 if_stack_depth++;
3466 break;
3467
3468 case BRW_OPCODE_ELSE:
3469 if_stack[if_stack_depth - 1] =
3470 brw_ELSE(p, if_stack[if_stack_depth - 1]);
3471 break;
3472 case BRW_OPCODE_ENDIF:
3473 if_stack_depth--;
3474 brw_ENDIF(p , if_stack[if_stack_depth]);
3475 if_depth_in_loop[loop_stack_depth]--;
3476 break;
3477
3478 case BRW_OPCODE_DO:
3479 loop_stack[loop_stack_depth++] = brw_DO(p, BRW_EXECUTE_8);
3480 if_depth_in_loop[loop_stack_depth] = 0;
3481 break;
3482
3483 case BRW_OPCODE_BREAK:
3484 brw_BREAK(p, if_depth_in_loop[loop_stack_depth]);
3485 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
3486 break;
3487 case BRW_OPCODE_CONTINUE:
3488 /* FINISHME: We need to write the loop instruction support still. */
3489 if (intel->gen >= 6)
3490 brw_CONT_gen6(p, loop_stack[loop_stack_depth - 1]);
3491 else
3492 brw_CONT(p, if_depth_in_loop[loop_stack_depth]);
3493 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
3494 break;
3495
3496 case BRW_OPCODE_WHILE: {
3497 struct brw_instruction *inst0, *inst1;
3498 GLuint br = 1;
3499
3500 if (intel->gen >= 5)
3501 br = 2;
3502
3503 assert(loop_stack_depth > 0);
3504 loop_stack_depth--;
3505 inst0 = inst1 = brw_WHILE(p, loop_stack[loop_stack_depth]);
3506 if (intel->gen < 6) {
3507 /* patch all the BREAK/CONT instructions from last BGNLOOP */
3508 while (inst0 > loop_stack[loop_stack_depth]) {
3509 inst0--;
3510 if (inst0->header.opcode == BRW_OPCODE_BREAK &&
3511 inst0->bits3.if_else.jump_count == 0) {
3512 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
3513 }
3514 else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
3515 inst0->bits3.if_else.jump_count == 0) {
3516 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
3517 }
3518 }
3519 }
3520 }
3521 break;
3522
3523 case FS_OPCODE_RCP:
3524 case FS_OPCODE_RSQ:
3525 case FS_OPCODE_SQRT:
3526 case FS_OPCODE_EXP2:
3527 case FS_OPCODE_LOG2:
3528 case FS_OPCODE_POW:
3529 case FS_OPCODE_SIN:
3530 case FS_OPCODE_COS:
3531 generate_math(inst, dst, src);
3532 break;
3533 case FS_OPCODE_CINTERP:
3534 brw_MOV(p, dst, src[0]);
3535 break;
3536 case FS_OPCODE_LINTERP:
3537 generate_linterp(inst, dst, src);
3538 break;
3539 case FS_OPCODE_TEX:
3540 case FS_OPCODE_TXB:
3541 case FS_OPCODE_TXL:
3542 generate_tex(inst, dst);
3543 break;
3544 case FS_OPCODE_DISCARD_NOT:
3545 generate_discard_not(inst, dst);
3546 break;
3547 case FS_OPCODE_DISCARD_AND:
3548 generate_discard_and(inst, src[0]);
3549 break;
3550 case FS_OPCODE_DDX:
3551 generate_ddx(inst, dst, src[0]);
3552 break;
3553 case FS_OPCODE_DDY:
3554 generate_ddy(inst, dst, src[0]);
3555 break;
3556
3557 case FS_OPCODE_SPILL:
3558 generate_spill(inst, src[0]);
3559 break;
3560
3561 case FS_OPCODE_UNSPILL:
3562 generate_unspill(inst, dst);
3563 break;
3564
3565 case FS_OPCODE_PULL_CONSTANT_LOAD:
3566 generate_pull_constant_load(inst, dst);
3567 break;
3568
3569 case FS_OPCODE_FB_WRITE:
3570 generate_fb_write(inst);
3571 break;
3572 default:
3573 if (inst->opcode < (int)ARRAY_SIZE(brw_opcodes)) {
3574 _mesa_problem(ctx, "Unsupported opcode `%s' in FS",
3575 brw_opcodes[inst->opcode].name);
3576 } else {
3577 _mesa_problem(ctx, "Unsupported opcode %d in FS", inst->opcode);
3578 }
3579 this->fail = true;
3580 }
3581
3582 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
3583 for (unsigned int i = last_native_inst; i < p->nr_insn; i++) {
3584 if (0) {
3585 printf("0x%08x 0x%08x 0x%08x 0x%08x ",
3586 ((uint32_t *)&p->store[i])[3],
3587 ((uint32_t *)&p->store[i])[2],
3588 ((uint32_t *)&p->store[i])[1],
3589 ((uint32_t *)&p->store[i])[0]);
3590 }
3591 brw_disasm(stdout, &p->store[i], intel->gen);
3592 }
3593 }
3594
3595 last_native_inst = p->nr_insn;
3596 }
3597
3598 brw_set_uip_jip(p);
3599
3600 /* OK, while the INTEL_DEBUG=wm above is very nice for debugging FS
3601 * emit issues, it doesn't get the jump distances into the output,
3602 * which is often something we want to debug. So this is here in
3603 * case you're doing that.
3604 */
3605 if (0) {
3606 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
3607 for (unsigned int i = 0; i < p->nr_insn; i++) {
3608 printf("0x%08x 0x%08x 0x%08x 0x%08x ",
3609 ((uint32_t *)&p->store[i])[3],
3610 ((uint32_t *)&p->store[i])[2],
3611 ((uint32_t *)&p->store[i])[1],
3612 ((uint32_t *)&p->store[i])[0]);
3613 brw_disasm(stdout, &p->store[i], intel->gen);
3614 }
3615 }
3616 }
3617 }
3618
3619 GLboolean
3620 brw_wm_fs_emit(struct brw_context *brw, struct brw_wm_compile *c)
3621 {
3622 struct intel_context *intel = &brw->intel;
3623 struct gl_context *ctx = &intel->ctx;
3624 struct gl_shader_program *prog = ctx->Shader.CurrentFragmentProgram;
3625
3626 if (!prog)
3627 return GL_FALSE;
3628
3629 struct brw_shader *shader =
3630 (brw_shader *) prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
3631 if (!shader)
3632 return GL_FALSE;
3633
3634 /* We always use 8-wide mode, at least for now. For one, flow
3635 * control only works in 8-wide. Also, when we're fragment shader
3636 * bound, we're almost always under register pressure as well, so
3637 * 8-wide would save us from the performance cliff of spilling
3638 * regs.
3639 */
3640 c->dispatch_width = 8;
3641
3642 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
3643 printf("GLSL IR for native fragment shader %d:\n", prog->Name);
3644 _mesa_print_ir(shader->ir, NULL);
3645 printf("\n");
3646 }
3647
3648 /* Now the main event: Visit the shader IR and generate our FS IR for it.
3649 */
3650 fs_visitor v(c, shader);
3651
3652 if (0) {
3653 v.emit_dummy_fs();
3654 } else {
3655 v.calculate_urb_setup();
3656 if (intel->gen < 6)
3657 v.emit_interpolation_setup_gen4();
3658 else
3659 v.emit_interpolation_setup_gen6();
3660
3661 /* Generate FS IR for main(). (the visitor only descends into
3662 * functions called "main").
3663 */
3664 foreach_iter(exec_list_iterator, iter, *shader->ir) {
3665 ir_instruction *ir = (ir_instruction *)iter.get();
3666 v.base_ir = ir;
3667 ir->accept(&v);
3668 }
3669
3670 v.emit_fb_writes();
3671
3672 v.split_virtual_grfs();
3673
3674 v.setup_paramvalues_refs();
3675 v.setup_pull_constants();
3676
3677 bool progress;
3678 do {
3679 progress = false;
3680
3681 progress = v.remove_duplicate_mrf_writes() || progress;
3682
3683 progress = v.propagate_constants() || progress;
3684 progress = v.register_coalesce() || progress;
3685 progress = v.compute_to_mrf() || progress;
3686 progress = v.dead_code_eliminate() || progress;
3687 } while (progress);
3688
3689 v.schedule_instructions();
3690
3691 v.assign_curb_setup();
3692 v.assign_urb_setup();
3693
3694 if (0) {
3695 /* Debug of register spilling: Go spill everything. */
3696 int virtual_grf_count = v.virtual_grf_next;
3697 for (int i = 1; i < virtual_grf_count; i++) {
3698 v.spill_reg(i);
3699 }
3700 }
3701
3702 if (0)
3703 v.assign_regs_trivial();
3704 else {
3705 while (!v.assign_regs()) {
3706 if (v.fail)
3707 break;
3708 }
3709 }
3710 }
3711
3712 if (!v.fail)
3713 v.generate_code();
3714
3715 assert(!v.fail); /* FINISHME: Cleanly fail, tested at link time, etc. */
3716
3717 if (v.fail)
3718 return GL_FALSE;
3719
3720 c->prog_data.total_grf = v.grf_used;
3721
3722 return GL_TRUE;
3723 }