i965: Don't overwrite the math function with conditional mod.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_generator.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
24 /** @file brw_fs_generator.cpp
25 *
26 * This file supports generating code from the FS LIR to the actual
27 * native instructions.
28 */
29
30 extern "C" {
31 #include "main/macros.h"
32 #include "brw_context.h"
33 #include "brw_eu.h"
34 } /* extern "C" */
35
36 #include "brw_fs.h"
37 #include "brw_cfg.h"
38
39 fs_generator::fs_generator(struct brw_context *brw,
40 void *mem_ctx,
41 const struct brw_wm_prog_key *key,
42 struct brw_wm_prog_data *prog_data,
43 struct gl_shader_program *shader_prog,
44 struct gl_fragment_program *fp,
45 bool runtime_check_aads_emit,
46 bool debug_flag)
47
48 : brw(brw), stage(MESA_SHADER_FRAGMENT), key(key),
49 prog_data(&prog_data->base), shader_prog(shader_prog),
50 prog(&fp->Base), runtime_check_aads_emit(runtime_check_aads_emit),
51 debug_flag(debug_flag), mem_ctx(mem_ctx)
52 {
53 ctx = &brw->ctx;
54
55 p = rzalloc(mem_ctx, struct brw_compile);
56 brw_init_compile(brw, p, mem_ctx);
57 }
58
59 fs_generator::~fs_generator()
60 {
61 }
62
63 class ip_record : public exec_node {
64 public:
65 DECLARE_RALLOC_CXX_OPERATORS(ip_record)
66
67 ip_record(int ip)
68 {
69 this->ip = ip;
70 }
71
72 int ip;
73 };
74
75 bool
76 fs_generator::patch_discard_jumps_to_fb_writes()
77 {
78 if (brw->gen < 6 || this->discard_halt_patches.is_empty())
79 return false;
80
81 int scale = brw_jump_scale(brw);
82
83 /* There is a somewhat strange undocumented requirement of using
84 * HALT, according to the simulator. If some channel has HALTed to
85 * a particular UIP, then by the end of the program, every channel
86 * must have HALTed to that UIP. Furthermore, the tracking is a
87 * stack, so you can't do the final halt of a UIP after starting
88 * halting to a new UIP.
89 *
90 * Symptoms of not emitting this instruction on actual hardware
91 * included GPU hangs and sparkly rendering on the piglit discard
92 * tests.
93 */
94 brw_inst *last_halt = gen6_HALT(p);
95 brw_inst_set_uip(brw, last_halt, 1 * scale);
96 brw_inst_set_jip(brw, last_halt, 1 * scale);
97
98 int ip = p->nr_insn;
99
100 foreach_in_list(ip_record, patch_ip, &discard_halt_patches) {
101 brw_inst *patch = &p->store[patch_ip->ip];
102
103 assert(brw_inst_opcode(brw, patch) == BRW_OPCODE_HALT);
104 /* HALT takes a half-instruction distance from the pre-incremented IP. */
105 brw_inst_set_uip(brw, patch, (ip - patch_ip->ip) * scale);
106 }
107
108 this->discard_halt_patches.make_empty();
109 return true;
110 }
111
112 void
113 fs_generator::fire_fb_write(fs_inst *inst,
114 struct brw_reg payload,
115 struct brw_reg implied_header,
116 GLuint nr)
117 {
118 uint32_t msg_control;
119
120 assert(stage == MESA_SHADER_FRAGMENT);
121 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
122
123 if (brw->gen < 6) {
124 brw_push_insn_state(p);
125 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
126 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
127 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
128 brw_MOV(p, offset(payload, 1), brw_vec8_grf(1, 0));
129 brw_pop_insn_state(p);
130 }
131
132 if (inst->opcode == FS_OPCODE_REP_FB_WRITE)
133 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
134 else if (prog_data->dual_src_blend)
135 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01;
136 else if (dispatch_width == 16)
137 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
138 else
139 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01;
140
141 uint32_t surf_index =
142 prog_data->binding_table.render_target_start + inst->target;
143
144 brw_fb_WRITE(p,
145 dispatch_width,
146 payload,
147 implied_header,
148 msg_control,
149 surf_index,
150 nr,
151 0,
152 inst->eot,
153 inst->header_present);
154
155 brw_mark_surface_used(&prog_data->base, surf_index);
156 }
157
158 void
159 fs_generator::generate_fb_write(fs_inst *inst, struct brw_reg payload)
160 {
161 assert(stage == MESA_SHADER_FRAGMENT);
162 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
163 const brw_wm_prog_key * const key = (brw_wm_prog_key * const) this->key;
164 struct brw_reg implied_header;
165
166 if (inst->base_mrf >= 0)
167 payload = brw_message_reg(inst->base_mrf);
168
169 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
170 * move, here's g1.
171 */
172 if (inst->header_present) {
173 brw_push_insn_state(p);
174 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
175 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
176 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
177 brw_set_default_flag_reg(p, 0, 0);
178
179 /* On HSW, the GPU will use the predicate on SENDC, unless the header is
180 * present.
181 */
182 if (prog_data->uses_kill || key->alpha_test_func) {
183 struct brw_reg pixel_mask;
184
185 if (brw->gen >= 6)
186 pixel_mask = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
187 else
188 pixel_mask = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
189
190 brw_MOV(p, pixel_mask, brw_flag_reg(0, 1));
191 }
192
193 if (brw->gen >= 6) {
194 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
195 brw_MOV(p,
196 retype(payload, BRW_REGISTER_TYPE_UD),
197 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
198 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
199
200 if (inst->target > 0 && key->replicate_alpha) {
201 /* Set "Source0 Alpha Present to RenderTarget" bit in message
202 * header.
203 */
204 brw_OR(p,
205 vec1(retype(payload, BRW_REGISTER_TYPE_UD)),
206 vec1(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD)),
207 brw_imm_ud(0x1 << 11));
208 }
209
210 if (inst->target > 0) {
211 /* Set the render target index for choosing BLEND_STATE. */
212 brw_MOV(p, retype(vec1(suboffset(payload, 2)),
213 BRW_REGISTER_TYPE_UD),
214 brw_imm_ud(inst->target));
215 }
216
217 implied_header = brw_null_reg();
218 } else {
219 implied_header = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
220 }
221
222 brw_pop_insn_state(p);
223 } else {
224 implied_header = brw_null_reg();
225 }
226
227 if (!runtime_check_aads_emit) {
228 fire_fb_write(inst, payload, implied_header, inst->mlen);
229 } else {
230 /* This can only happen in gen < 6 */
231 assert(brw->gen < 6);
232
233 struct brw_reg v1_null_ud = vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD));
234
235 /* Check runtime bit to detect if we have to send AA data or not */
236 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
237 brw_AND(p,
238 v1_null_ud,
239 retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD),
240 brw_imm_ud(1<<26));
241 brw_inst_set_cond_modifier(brw, brw_last_inst, BRW_CONDITIONAL_NZ);
242
243 int jmp = brw_JMPI(p, brw_imm_ud(0), BRW_PREDICATE_NORMAL) - p->store;
244 brw_inst_set_exec_size(brw, brw_last_inst, BRW_EXECUTE_1);
245 {
246 /* Don't send AA data */
247 fire_fb_write(inst, offset(payload, 1), implied_header, inst->mlen-1);
248 }
249 brw_land_fwd_jump(p, jmp);
250 fire_fb_write(inst, payload, implied_header, inst->mlen);
251 }
252 }
253
254 void
255 fs_generator::generate_blorp_fb_write(fs_inst *inst)
256 {
257 brw_fb_WRITE(p,
258 16 /* dispatch_width */,
259 brw_message_reg(inst->base_mrf),
260 brw_reg_from_fs_reg(&inst->src[0]),
261 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
262 inst->target,
263 inst->mlen,
264 0,
265 true,
266 inst->header_present);
267 }
268
269 /* Computes the integer pixel x,y values from the origin.
270 *
271 * This is the basis of gl_FragCoord computation, but is also used
272 * pre-gen6 for computing the deltas from v0 for computing
273 * interpolation.
274 */
275 void
276 fs_generator::generate_pixel_xy(struct brw_reg dst, bool is_x)
277 {
278 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
279 struct brw_reg src;
280 struct brw_reg deltas;
281
282 if (is_x) {
283 src = stride(suboffset(g1_uw, 4), 2, 4, 0);
284 deltas = brw_imm_v(0x10101010);
285 } else {
286 src = stride(suboffset(g1_uw, 5), 2, 4, 0);
287 deltas = brw_imm_v(0x11001100);
288 }
289
290 if (dispatch_width == 16) {
291 dst = vec16(dst);
292 }
293
294 /* We do this SIMD8 or SIMD16, but since the destination is UW we
295 * don't do compression in the SIMD16 case.
296 */
297 brw_push_insn_state(p);
298 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
299 brw_ADD(p, dst, src, deltas);
300 brw_pop_insn_state(p);
301 }
302
303 void
304 fs_generator::generate_linterp(fs_inst *inst,
305 struct brw_reg dst, struct brw_reg *src)
306 {
307 struct brw_reg delta_x = src[0];
308 struct brw_reg delta_y = src[1];
309 struct brw_reg interp = src[2];
310
311 if (brw->has_pln &&
312 delta_y.nr == delta_x.nr + 1 &&
313 (brw->gen >= 6 || (delta_x.nr & 1) == 0)) {
314 brw_PLN(p, dst, interp, delta_x);
315 } else {
316 brw_LINE(p, brw_null_reg(), interp, delta_x);
317 brw_MAC(p, dst, suboffset(interp, 1), delta_y);
318 }
319 }
320
321 void
322 fs_generator::generate_math_gen6(fs_inst *inst,
323 struct brw_reg dst,
324 struct brw_reg src0,
325 struct brw_reg src1)
326 {
327 int op = brw_math_function(inst->opcode);
328 bool binop = src1.file != BRW_ARCHITECTURE_REGISTER_FILE;
329
330 if (dispatch_width == 8) {
331 gen6_math(p, dst, op, src0, src1);
332 } else if (dispatch_width == 16) {
333 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
334 gen6_math(p, firsthalf(dst), op, firsthalf(src0), firsthalf(src1));
335 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
336 gen6_math(p, sechalf(dst), op, sechalf(src0),
337 binop ? sechalf(src1) : brw_null_reg());
338 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
339 }
340 }
341
342 void
343 fs_generator::generate_math_gen4(fs_inst *inst,
344 struct brw_reg dst,
345 struct brw_reg src)
346 {
347 int op = brw_math_function(inst->opcode);
348
349 assert(inst->mlen >= 1);
350
351 if (dispatch_width == 8) {
352 gen4_math(p, dst,
353 op,
354 inst->base_mrf, src,
355 BRW_MATH_PRECISION_FULL);
356 } else if (dispatch_width == 16) {
357 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
358 gen4_math(p, firsthalf(dst),
359 op,
360 inst->base_mrf, firsthalf(src),
361 BRW_MATH_PRECISION_FULL);
362 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
363 gen4_math(p, sechalf(dst),
364 op,
365 inst->base_mrf + 1, sechalf(src),
366 BRW_MATH_PRECISION_FULL);
367
368 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
369 }
370 }
371
372 void
373 fs_generator::generate_math_g45(fs_inst *inst,
374 struct brw_reg dst,
375 struct brw_reg src)
376 {
377 if (inst->opcode == SHADER_OPCODE_POW ||
378 inst->opcode == SHADER_OPCODE_INT_QUOTIENT ||
379 inst->opcode == SHADER_OPCODE_INT_REMAINDER) {
380 generate_math_gen4(inst, dst, src);
381 return;
382 }
383
384 int op = brw_math_function(inst->opcode);
385
386 assert(inst->mlen >= 1);
387
388 gen4_math(p, dst,
389 op,
390 inst->base_mrf, src,
391 BRW_MATH_PRECISION_FULL);
392 }
393
394 void
395 fs_generator::generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src,
396 struct brw_reg sampler_index)
397 {
398 int msg_type = -1;
399 int rlen = 4;
400 uint32_t simd_mode;
401 uint32_t return_format;
402
403 switch (dst.type) {
404 case BRW_REGISTER_TYPE_D:
405 return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;
406 break;
407 case BRW_REGISTER_TYPE_UD:
408 return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
409 break;
410 default:
411 return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
412 break;
413 }
414
415 switch (inst->exec_size) {
416 case 8:
417 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
418 break;
419 case 16:
420 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
421 break;
422 default:
423 unreachable("Invalid width for texture instruction");
424 }
425
426 if (brw->gen >= 5) {
427 switch (inst->opcode) {
428 case SHADER_OPCODE_TEX:
429 if (inst->shadow_compare) {
430 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE;
431 } else {
432 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE;
433 }
434 break;
435 case FS_OPCODE_TXB:
436 if (inst->shadow_compare) {
437 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE;
438 } else {
439 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS;
440 }
441 break;
442 case SHADER_OPCODE_TXL:
443 if (inst->shadow_compare) {
444 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
445 } else {
446 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
447 }
448 break;
449 case SHADER_OPCODE_TXS:
450 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
451 break;
452 case SHADER_OPCODE_TXD:
453 if (inst->shadow_compare) {
454 /* Gen7.5+. Otherwise, lowered by brw_lower_texture_gradients(). */
455 assert(brw->gen >= 8 || brw->is_haswell);
456 msg_type = HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE;
457 } else {
458 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
459 }
460 break;
461 case SHADER_OPCODE_TXF:
462 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
463 break;
464 case SHADER_OPCODE_TXF_CMS:
465 if (brw->gen >= 7)
466 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS;
467 else
468 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
469 break;
470 case SHADER_OPCODE_TXF_UMS:
471 assert(brw->gen >= 7);
472 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS;
473 break;
474 case SHADER_OPCODE_TXF_MCS:
475 assert(brw->gen >= 7);
476 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS;
477 break;
478 case SHADER_OPCODE_LOD:
479 msg_type = GEN5_SAMPLER_MESSAGE_LOD;
480 break;
481 case SHADER_OPCODE_TG4:
482 if (inst->shadow_compare) {
483 assert(brw->gen >= 7);
484 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C;
485 } else {
486 assert(brw->gen >= 6);
487 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4;
488 }
489 break;
490 case SHADER_OPCODE_TG4_OFFSET:
491 assert(brw->gen >= 7);
492 if (inst->shadow_compare) {
493 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C;
494 } else {
495 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO;
496 }
497 break;
498 default:
499 unreachable("not reached");
500 }
501 } else {
502 switch (inst->opcode) {
503 case SHADER_OPCODE_TEX:
504 /* Note that G45 and older determines shadow compare and dispatch width
505 * from message length for most messages.
506 */
507 assert(dispatch_width == 8);
508 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
509 if (inst->shadow_compare) {
510 assert(inst->mlen == 6);
511 } else {
512 assert(inst->mlen <= 4);
513 }
514 break;
515 case FS_OPCODE_TXB:
516 if (inst->shadow_compare) {
517 assert(inst->mlen == 6);
518 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE;
519 } else {
520 assert(inst->mlen == 9);
521 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
522 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
523 }
524 break;
525 case SHADER_OPCODE_TXL:
526 if (inst->shadow_compare) {
527 assert(inst->mlen == 6);
528 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE;
529 } else {
530 assert(inst->mlen == 9);
531 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD;
532 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
533 }
534 break;
535 case SHADER_OPCODE_TXD:
536 /* There is no sample_d_c message; comparisons are done manually */
537 assert(inst->mlen == 7 || inst->mlen == 10);
538 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS;
539 break;
540 case SHADER_OPCODE_TXF:
541 assert(inst->mlen == 9);
542 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
543 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
544 break;
545 case SHADER_OPCODE_TXS:
546 assert(inst->mlen == 3);
547 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_RESINFO;
548 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
549 break;
550 default:
551 unreachable("not reached");
552 }
553 }
554 assert(msg_type != -1);
555
556 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
557 rlen = 8;
558 dst = vec16(dst);
559 }
560
561 assert(brw->gen < 7 || !inst->header_present ||
562 src.file == BRW_GENERAL_REGISTER_FILE);
563
564 assert(sampler_index.type == BRW_REGISTER_TYPE_UD);
565
566 /* Load the message header if present. If there's a texture offset,
567 * we need to set it up explicitly and load the offset bitfield.
568 * Otherwise, we can use an implied move from g0 to the first message reg.
569 */
570 if (inst->header_present) {
571 if (brw->gen < 6 && !inst->offset) {
572 /* Set up an implied move from g0 to the MRF. */
573 src = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
574 } else {
575 struct brw_reg header_reg;
576
577 if (brw->gen >= 7) {
578 header_reg = src;
579 } else {
580 assert(inst->base_mrf != -1);
581 header_reg = brw_message_reg(inst->base_mrf);
582 }
583
584 brw_push_insn_state(p);
585 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
586 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
587 /* Explicitly set up the message header by copying g0 to the MRF. */
588 brw_MOV(p, header_reg, brw_vec8_grf(0, 0));
589
590 if (inst->offset) {
591 /* Set the offset bits in DWord 2. */
592 brw_MOV(p, get_element_ud(header_reg, 2),
593 brw_imm_ud(inst->offset));
594 }
595
596 brw_adjust_sampler_state_pointer(p, header_reg, sampler_index, dst);
597 brw_pop_insn_state(p);
598 }
599 }
600
601 uint32_t base_binding_table_index = (inst->opcode == SHADER_OPCODE_TG4 ||
602 inst->opcode == SHADER_OPCODE_TG4_OFFSET)
603 ? prog_data->binding_table.gather_texture_start
604 : prog_data->binding_table.texture_start;
605
606 if (sampler_index.file == BRW_IMMEDIATE_VALUE) {
607 uint32_t sampler = sampler_index.dw1.ud;
608
609 brw_SAMPLE(p,
610 retype(dst, BRW_REGISTER_TYPE_UW),
611 inst->base_mrf,
612 src,
613 sampler + base_binding_table_index,
614 sampler % 16,
615 msg_type,
616 rlen,
617 inst->mlen,
618 inst->header_present,
619 simd_mode,
620 return_format);
621
622 brw_mark_surface_used(prog_data, sampler + base_binding_table_index);
623 } else {
624 /* Non-const sampler index */
625 /* Note: this clobbers `dst` as a temporary before emitting the send */
626
627 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
628 struct brw_reg temp = vec1(retype(dst, BRW_REGISTER_TYPE_UD));
629
630 struct brw_reg sampler_reg = vec1(retype(sampler_index, BRW_REGISTER_TYPE_UD));
631
632 brw_push_insn_state(p);
633 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
634 brw_set_default_access_mode(p, BRW_ALIGN_1);
635
636 /* Some care required: `sampler` and `temp` may alias:
637 * addr = sampler & 0xff
638 * temp = (sampler << 8) & 0xf00
639 * addr = addr | temp
640 */
641 brw_ADD(p, addr, sampler_reg, brw_imm_ud(base_binding_table_index));
642 brw_SHL(p, temp, sampler_reg, brw_imm_ud(8u));
643 brw_AND(p, temp, temp, brw_imm_ud(0x0f00));
644 brw_AND(p, addr, addr, brw_imm_ud(0x0ff));
645 brw_OR(p, addr, addr, temp);
646
647 /* a0.0 |= <descriptor> */
648 brw_inst *insn_or = brw_next_insn(p, BRW_OPCODE_OR);
649 brw_set_sampler_message(p, insn_or,
650 0 /* surface */,
651 0 /* sampler */,
652 msg_type,
653 rlen,
654 inst->mlen /* mlen */,
655 inst->header_present /* header */,
656 simd_mode,
657 return_format);
658 brw_inst_set_exec_size(p->brw, insn_or, BRW_EXECUTE_1);
659 brw_inst_set_src1_reg_type(p->brw, insn_or, BRW_REGISTER_TYPE_UD);
660 brw_set_src0(p, insn_or, addr);
661 brw_set_dest(p, insn_or, addr);
662
663
664 /* dst = send(offset, a0.0) */
665 brw_inst *insn_send = brw_next_insn(p, BRW_OPCODE_SEND);
666 brw_set_dest(p, insn_send, dst);
667 brw_set_src0(p, insn_send, src);
668 brw_set_indirect_send_descriptor(p, insn_send, BRW_SFID_SAMPLER, addr);
669
670 brw_pop_insn_state(p);
671
672 /* visitor knows more than we do about the surface limit required,
673 * so has already done marking.
674 */
675 }
676 }
677
678
679 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
680 * looking like:
681 *
682 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
683 *
684 * Ideally, we want to produce:
685 *
686 * DDX DDY
687 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
688 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
689 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
690 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
691 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
692 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
693 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
694 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
695 *
696 * and add another set of two more subspans if in 16-pixel dispatch mode.
697 *
698 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
699 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
700 * pair. But the ideal approximation may impose a huge performance cost on
701 * sample_d. On at least Haswell, sample_d instruction does some
702 * optimizations if the same LOD is used for all pixels in the subspan.
703 *
704 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
705 * appropriate swizzling.
706 */
707 void
708 fs_generator::generate_ddx(fs_inst *inst, struct brw_reg dst, struct brw_reg src,
709 struct brw_reg quality)
710 {
711 unsigned vstride, width;
712 assert(quality.file == BRW_IMMEDIATE_VALUE);
713 assert(quality.type == BRW_REGISTER_TYPE_D);
714
715 assert(stage == MESA_SHADER_FRAGMENT);
716 const brw_wm_prog_key * const key = (brw_wm_prog_key * const) this->key;
717
718 int quality_value = quality.dw1.d;
719
720 if (quality_value == BRW_DERIVATIVE_FINE ||
721 (key->high_quality_derivatives && quality_value != BRW_DERIVATIVE_COARSE)) {
722 /* produce accurate derivatives */
723 vstride = BRW_VERTICAL_STRIDE_2;
724 width = BRW_WIDTH_2;
725 }
726 else {
727 /* replicate the derivative at the top-left pixel to other pixels */
728 vstride = BRW_VERTICAL_STRIDE_4;
729 width = BRW_WIDTH_4;
730 }
731
732 struct brw_reg src0 = brw_reg(src.file, src.nr, 1,
733 BRW_REGISTER_TYPE_F,
734 vstride,
735 width,
736 BRW_HORIZONTAL_STRIDE_0,
737 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
738 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
739 BRW_REGISTER_TYPE_F,
740 vstride,
741 width,
742 BRW_HORIZONTAL_STRIDE_0,
743 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
744 brw_ADD(p, dst, src0, negate(src1));
745 }
746
747 /* The negate_value boolean is used to negate the derivative computation for
748 * FBOs, since they place the origin at the upper left instead of the lower
749 * left.
750 */
751 void
752 fs_generator::generate_ddy(fs_inst *inst, struct brw_reg dst, struct brw_reg src,
753 struct brw_reg quality, bool negate_value)
754 {
755 assert(quality.file == BRW_IMMEDIATE_VALUE);
756 assert(quality.type == BRW_REGISTER_TYPE_D);
757
758 assert(stage == MESA_SHADER_FRAGMENT);
759 const brw_wm_prog_key * const key = (brw_wm_prog_key * const) this->key;
760
761 int quality_value = quality.dw1.d;
762
763 if (quality_value == BRW_DERIVATIVE_FINE ||
764 (key->high_quality_derivatives && quality_value != BRW_DERIVATIVE_COARSE)) {
765 /* From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
766 * Region Restrictions):
767 *
768 * In Align16 access mode, SIMD16 is not allowed for DW operations
769 * and SIMD8 is not allowed for DF operations.
770 *
771 * In this context, "DW operations" means "operations acting on 32-bit
772 * values", so it includes operations on floats.
773 *
774 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
775 * (Instruction Compression -> Rules and Restrictions):
776 *
777 * A compressed instruction must be in Align1 access mode. Align16
778 * mode instructions cannot be compressed.
779 *
780 * Similar text exists in the g45 PRM.
781 *
782 * On these platforms, if we're building a SIMD16 shader, we need to
783 * manually unroll to a pair of SIMD8 instructions.
784 */
785 bool unroll_to_simd8 =
786 (dispatch_width == 16 &&
787 (brw->gen == 4 || (brw->gen == 7 && !brw->is_haswell)));
788
789 /* produce accurate derivatives */
790 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
791 BRW_REGISTER_TYPE_F,
792 BRW_VERTICAL_STRIDE_4,
793 BRW_WIDTH_4,
794 BRW_HORIZONTAL_STRIDE_1,
795 BRW_SWIZZLE_XYXY, WRITEMASK_XYZW);
796 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
797 BRW_REGISTER_TYPE_F,
798 BRW_VERTICAL_STRIDE_4,
799 BRW_WIDTH_4,
800 BRW_HORIZONTAL_STRIDE_1,
801 BRW_SWIZZLE_ZWZW, WRITEMASK_XYZW);
802 brw_push_insn_state(p);
803 brw_set_default_access_mode(p, BRW_ALIGN_16);
804 if (unroll_to_simd8) {
805 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
806 if (negate_value) {
807 brw_ADD(p, firsthalf(dst), firsthalf(src1), negate(firsthalf(src0)));
808 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
809 brw_ADD(p, sechalf(dst), sechalf(src1), negate(sechalf(src0)));
810 } else {
811 brw_ADD(p, firsthalf(dst), firsthalf(src0), negate(firsthalf(src1)));
812 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
813 brw_ADD(p, sechalf(dst), sechalf(src0), negate(sechalf(src1)));
814 }
815 } else {
816 if (negate_value)
817 brw_ADD(p, dst, src1, negate(src0));
818 else
819 brw_ADD(p, dst, src0, negate(src1));
820 }
821 brw_pop_insn_state(p);
822 } else {
823 /* replicate the derivative at the top-left pixel to other pixels */
824 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
825 BRW_REGISTER_TYPE_F,
826 BRW_VERTICAL_STRIDE_4,
827 BRW_WIDTH_4,
828 BRW_HORIZONTAL_STRIDE_0,
829 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
830 struct brw_reg src1 = brw_reg(src.file, src.nr, 2,
831 BRW_REGISTER_TYPE_F,
832 BRW_VERTICAL_STRIDE_4,
833 BRW_WIDTH_4,
834 BRW_HORIZONTAL_STRIDE_0,
835 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
836 if (negate_value)
837 brw_ADD(p, dst, src1, negate(src0));
838 else
839 brw_ADD(p, dst, src0, negate(src1));
840 }
841 }
842
843 void
844 fs_generator::generate_discard_jump(fs_inst *inst)
845 {
846 assert(brw->gen >= 6);
847
848 /* This HALT will be patched up at FB write time to point UIP at the end of
849 * the program, and at brw_uip_jip() JIP will be set to the end of the
850 * current block (or the program).
851 */
852 this->discard_halt_patches.push_tail(new(mem_ctx) ip_record(p->nr_insn));
853
854 brw_push_insn_state(p);
855 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
856 gen6_HALT(p);
857 brw_pop_insn_state(p);
858 }
859
860 void
861 fs_generator::generate_scratch_write(fs_inst *inst, struct brw_reg src)
862 {
863 assert(inst->mlen != 0);
864
865 brw_MOV(p,
866 brw_uvec_mrf(inst->exec_size, (inst->base_mrf + 1), 0),
867 retype(src, BRW_REGISTER_TYPE_UD));
868 brw_oword_block_write_scratch(p, brw_message_reg(inst->base_mrf),
869 inst->exec_size / 8, inst->offset);
870 }
871
872 void
873 fs_generator::generate_scratch_read(fs_inst *inst, struct brw_reg dst)
874 {
875 assert(inst->mlen != 0);
876
877 brw_oword_block_read_scratch(p, dst, brw_message_reg(inst->base_mrf),
878 inst->exec_size / 8, inst->offset);
879 }
880
881 void
882 fs_generator::generate_scratch_read_gen7(fs_inst *inst, struct brw_reg dst)
883 {
884 gen7_block_read_scratch(p, dst, inst->exec_size / 8, inst->offset);
885 }
886
887 void
888 fs_generator::generate_uniform_pull_constant_load(fs_inst *inst,
889 struct brw_reg dst,
890 struct brw_reg index,
891 struct brw_reg offset)
892 {
893 assert(inst->mlen != 0);
894
895 assert(index.file == BRW_IMMEDIATE_VALUE &&
896 index.type == BRW_REGISTER_TYPE_UD);
897 uint32_t surf_index = index.dw1.ud;
898
899 assert(offset.file == BRW_IMMEDIATE_VALUE &&
900 offset.type == BRW_REGISTER_TYPE_UD);
901 uint32_t read_offset = offset.dw1.ud;
902
903 brw_oword_block_read(p, dst, brw_message_reg(inst->base_mrf),
904 read_offset, surf_index);
905
906 brw_mark_surface_used(prog_data, surf_index);
907 }
908
909 void
910 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst *inst,
911 struct brw_reg dst,
912 struct brw_reg index,
913 struct brw_reg offset)
914 {
915 assert(inst->mlen == 0);
916 assert(index.type == BRW_REGISTER_TYPE_UD);
917
918 assert(offset.file == BRW_GENERAL_REGISTER_FILE);
919 /* Reference just the dword we need, to avoid angering validate_reg(). */
920 offset = brw_vec1_grf(offset.nr, 0);
921
922 /* We use the SIMD4x2 mode because we want to end up with 4 components in
923 * the destination loaded consecutively from the same offset (which appears
924 * in the first component, and the rest are ignored).
925 */
926 dst.width = BRW_WIDTH_4;
927
928 if (index.file == BRW_IMMEDIATE_VALUE) {
929
930 uint32_t surf_index = index.dw1.ud;
931
932 brw_push_insn_state(p);
933 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
934 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
935 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
936 brw_pop_insn_state(p);
937
938 brw_set_dest(p, send, dst);
939 brw_set_src0(p, send, offset);
940 brw_set_sampler_message(p, send,
941 surf_index,
942 0, /* LD message ignores sampler unit */
943 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
944 1, /* rlen */
945 1, /* mlen */
946 false, /* no header */
947 BRW_SAMPLER_SIMD_MODE_SIMD4X2,
948 0);
949
950 brw_mark_surface_used(prog_data, surf_index);
951
952 } else {
953
954 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
955
956 brw_push_insn_state(p);
957 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
958 brw_set_default_access_mode(p, BRW_ALIGN_1);
959
960 /* a0.0 = surf_index & 0xff */
961 brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
962 brw_inst_set_exec_size(p->brw, insn_and, BRW_EXECUTE_1);
963 brw_set_dest(p, insn_and, addr);
964 brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));
965 brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
966
967
968 /* a0.0 |= <descriptor> */
969 brw_inst *insn_or = brw_next_insn(p, BRW_OPCODE_OR);
970 brw_set_sampler_message(p, insn_or,
971 0 /* surface */,
972 0 /* sampler */,
973 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
974 1 /* rlen */,
975 1 /* mlen */,
976 false /* header */,
977 BRW_SAMPLER_SIMD_MODE_SIMD4X2,
978 0);
979 brw_inst_set_exec_size(p->brw, insn_or, BRW_EXECUTE_1);
980 brw_inst_set_src1_reg_type(p->brw, insn_or, BRW_REGISTER_TYPE_UD);
981 brw_set_src0(p, insn_or, addr);
982 brw_set_dest(p, insn_or, addr);
983
984
985 /* dst = send(offset, a0.0) */
986 brw_inst *insn_send = brw_next_insn(p, BRW_OPCODE_SEND);
987 brw_set_dest(p, insn_send, dst);
988 brw_set_src0(p, insn_send, offset);
989 brw_set_indirect_send_descriptor(p, insn_send, BRW_SFID_SAMPLER, addr);
990
991 brw_pop_insn_state(p);
992
993 /* visitor knows more than we do about the surface limit required,
994 * so has already done marking.
995 */
996
997 }
998 }
999
1000 void
1001 fs_generator::generate_varying_pull_constant_load(fs_inst *inst,
1002 struct brw_reg dst,
1003 struct brw_reg index,
1004 struct brw_reg offset)
1005 {
1006 assert(brw->gen < 7); /* Should use the gen7 variant. */
1007 assert(inst->header_present);
1008 assert(inst->mlen);
1009
1010 assert(index.file == BRW_IMMEDIATE_VALUE &&
1011 index.type == BRW_REGISTER_TYPE_UD);
1012 uint32_t surf_index = index.dw1.ud;
1013
1014 uint32_t simd_mode, rlen, msg_type;
1015 if (dispatch_width == 16) {
1016 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1017 rlen = 8;
1018 } else {
1019 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1020 rlen = 4;
1021 }
1022
1023 if (brw->gen >= 5)
1024 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
1025 else {
1026 /* We always use the SIMD16 message so that we only have to load U, and
1027 * not V or R.
1028 */
1029 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
1030 assert(inst->mlen == 3);
1031 assert(inst->regs_written == 8);
1032 rlen = 8;
1033 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1034 }
1035
1036 struct brw_reg offset_mrf = retype(brw_message_reg(inst->base_mrf + 1),
1037 BRW_REGISTER_TYPE_D);
1038 brw_MOV(p, offset_mrf, offset);
1039
1040 struct brw_reg header = brw_vec8_grf(0, 0);
1041 gen6_resolve_implied_move(p, &header, inst->base_mrf);
1042
1043 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1044 brw_inst_set_qtr_control(brw, send, BRW_COMPRESSION_NONE);
1045 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));
1046 brw_set_src0(p, send, header);
1047 if (brw->gen < 6)
1048 brw_inst_set_base_mrf(brw, send, inst->base_mrf);
1049
1050 /* Our surface is set up as floats, regardless of what actual data is
1051 * stored in it.
1052 */
1053 uint32_t return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
1054 brw_set_sampler_message(p, send,
1055 surf_index,
1056 0, /* sampler (unused) */
1057 msg_type,
1058 rlen,
1059 inst->mlen,
1060 inst->header_present,
1061 simd_mode,
1062 return_format);
1063
1064 brw_mark_surface_used(prog_data, surf_index);
1065 }
1066
1067 void
1068 fs_generator::generate_varying_pull_constant_load_gen7(fs_inst *inst,
1069 struct brw_reg dst,
1070 struct brw_reg index,
1071 struct brw_reg offset)
1072 {
1073 assert(brw->gen >= 7);
1074 /* Varying-offset pull constant loads are treated as a normal expression on
1075 * gen7, so the fact that it's a send message is hidden at the IR level.
1076 */
1077 assert(!inst->header_present);
1078 assert(!inst->mlen);
1079 assert(index.type == BRW_REGISTER_TYPE_UD);
1080
1081 uint32_t simd_mode, rlen, mlen;
1082 if (dispatch_width == 16) {
1083 mlen = 2;
1084 rlen = 8;
1085 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1086 } else {
1087 mlen = 1;
1088 rlen = 4;
1089 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1090 }
1091
1092 if (index.file == BRW_IMMEDIATE_VALUE) {
1093
1094 uint32_t surf_index = index.dw1.ud;
1095
1096 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1097 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));
1098 brw_set_src0(p, send, offset);
1099 brw_set_sampler_message(p, send,
1100 surf_index,
1101 0, /* LD message ignores sampler unit */
1102 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1103 rlen,
1104 mlen,
1105 false, /* no header */
1106 simd_mode,
1107 0);
1108
1109 brw_mark_surface_used(prog_data, surf_index);
1110
1111 } else {
1112
1113 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1114
1115 brw_push_insn_state(p);
1116 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1117 brw_set_default_access_mode(p, BRW_ALIGN_1);
1118
1119 /* a0.0 = surf_index & 0xff */
1120 brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
1121 brw_inst_set_exec_size(p->brw, insn_and, BRW_EXECUTE_1);
1122 brw_set_dest(p, insn_and, addr);
1123 brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));
1124 brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
1125
1126
1127 /* a0.0 |= <descriptor> */
1128 brw_inst *insn_or = brw_next_insn(p, BRW_OPCODE_OR);
1129 brw_set_sampler_message(p, insn_or,
1130 0 /* surface */,
1131 0 /* sampler */,
1132 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1133 rlen /* rlen */,
1134 mlen /* mlen */,
1135 false /* header */,
1136 simd_mode,
1137 0);
1138 brw_inst_set_exec_size(p->brw, insn_or, BRW_EXECUTE_1);
1139 brw_inst_set_src1_reg_type(p->brw, insn_or, BRW_REGISTER_TYPE_UD);
1140 brw_set_src0(p, insn_or, addr);
1141 brw_set_dest(p, insn_or, addr);
1142
1143
1144 /* dst = send(offset, a0.0) */
1145 brw_inst *insn_send = brw_next_insn(p, BRW_OPCODE_SEND);
1146 brw_set_dest(p, insn_send, retype(dst, BRW_REGISTER_TYPE_UW));
1147 brw_set_src0(p, insn_send, offset);
1148 brw_set_indirect_send_descriptor(p, insn_send, BRW_SFID_SAMPLER, addr);
1149
1150 brw_pop_insn_state(p);
1151
1152 /* visitor knows more than we do about the surface limit required,
1153 * so has already done marking.
1154 */
1155 }
1156 }
1157
1158 /**
1159 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
1160 * into the flags register (f0.0).
1161 *
1162 * Used only on Gen6 and above.
1163 */
1164 void
1165 fs_generator::generate_mov_dispatch_to_flags(fs_inst *inst)
1166 {
1167 struct brw_reg flags = brw_flag_reg(0, inst->flag_subreg);
1168 struct brw_reg dispatch_mask;
1169
1170 if (brw->gen >= 6)
1171 dispatch_mask = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1172 else
1173 dispatch_mask = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
1174
1175 brw_push_insn_state(p);
1176 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1177 brw_MOV(p, flags, dispatch_mask);
1178 brw_pop_insn_state(p);
1179 }
1180
1181 void
1182 fs_generator::generate_pixel_interpolator_query(fs_inst *inst,
1183 struct brw_reg dst,
1184 struct brw_reg src,
1185 struct brw_reg msg_data,
1186 unsigned msg_type)
1187 {
1188 assert(msg_data.file == BRW_IMMEDIATE_VALUE &&
1189 msg_data.type == BRW_REGISTER_TYPE_UD);
1190
1191 brw_pixel_interpolator_query(p,
1192 retype(dst, BRW_REGISTER_TYPE_UW),
1193 src,
1194 inst->pi_noperspective,
1195 msg_type,
1196 msg_data.dw1.ud,
1197 inst->mlen,
1198 inst->regs_written);
1199 }
1200
1201
1202 static uint32_t brw_file_from_reg(fs_reg *reg)
1203 {
1204 switch (reg->file) {
1205 case GRF:
1206 return BRW_GENERAL_REGISTER_FILE;
1207 case MRF:
1208 return BRW_MESSAGE_REGISTER_FILE;
1209 case IMM:
1210 return BRW_IMMEDIATE_VALUE;
1211 default:
1212 unreachable("not reached");
1213 }
1214 }
1215
1216 struct brw_reg
1217 brw_reg_from_fs_reg(fs_reg *reg)
1218 {
1219 struct brw_reg brw_reg;
1220
1221 switch (reg->file) {
1222 case GRF:
1223 case MRF:
1224 if (reg->stride == 0) {
1225 brw_reg = brw_vec1_reg(brw_file_from_reg(reg), reg->reg, 0);
1226 } else if (reg->width < 8) {
1227 brw_reg = brw_vec8_reg(brw_file_from_reg(reg), reg->reg, 0);
1228 brw_reg = stride(brw_reg, reg->width * reg->stride,
1229 reg->width, reg->stride);
1230 } else {
1231 /* From the Haswell PRM:
1232 *
1233 * VertStride must be used to cross GRF register boundaries. This
1234 * rule implies that elements within a 'Width' cannot cross GRF
1235 * boundaries.
1236 *
1237 * So, for registers with width > 8, we have to use a width of 8
1238 * and trust the compression state to sort out the exec size.
1239 */
1240 brw_reg = brw_vec8_reg(brw_file_from_reg(reg), reg->reg, 0);
1241 brw_reg = stride(brw_reg, 8 * reg->stride, 8, reg->stride);
1242 }
1243
1244 brw_reg = retype(brw_reg, reg->type);
1245 brw_reg = byte_offset(brw_reg, reg->subreg_offset);
1246 break;
1247 case IMM:
1248 switch (reg->type) {
1249 case BRW_REGISTER_TYPE_F:
1250 brw_reg = brw_imm_f(reg->fixed_hw_reg.dw1.f);
1251 break;
1252 case BRW_REGISTER_TYPE_D:
1253 brw_reg = brw_imm_d(reg->fixed_hw_reg.dw1.d);
1254 break;
1255 case BRW_REGISTER_TYPE_UD:
1256 brw_reg = brw_imm_ud(reg->fixed_hw_reg.dw1.ud);
1257 break;
1258 default:
1259 unreachable("not reached");
1260 }
1261 break;
1262 case HW_REG:
1263 assert(reg->type == reg->fixed_hw_reg.type);
1264 brw_reg = reg->fixed_hw_reg;
1265 break;
1266 case BAD_FILE:
1267 /* Probably unused. */
1268 brw_reg = brw_null_reg();
1269 break;
1270 case UNIFORM:
1271 unreachable("not reached");
1272 default:
1273 unreachable("not reached");
1274 }
1275 if (reg->abs)
1276 brw_reg = brw_abs(brw_reg);
1277 if (reg->negate)
1278 brw_reg = negate(brw_reg);
1279
1280 return brw_reg;
1281 }
1282
1283 /**
1284 * Sets the first word of a vgrf for gen7+ simd4x2 uniform pull constant
1285 * sampler LD messages.
1286 *
1287 * We don't want to bake it into the send message's code generation because
1288 * that means we don't get a chance to schedule the instructions.
1289 */
1290 void
1291 fs_generator::generate_set_simd4x2_offset(fs_inst *inst,
1292 struct brw_reg dst,
1293 struct brw_reg value)
1294 {
1295 assert(value.file == BRW_IMMEDIATE_VALUE);
1296
1297 brw_push_insn_state(p);
1298 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1299 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1300 brw_MOV(p, retype(brw_vec1_reg(dst.file, dst.nr, 0), value.type), value);
1301 brw_pop_insn_state(p);
1302 }
1303
1304 /* Sets vstride=16, width=8, hstride=2 or vstride=0, width=1, hstride=0
1305 * (when mask is passed as a uniform) of register mask before moving it
1306 * to register dst.
1307 */
1308 void
1309 fs_generator::generate_set_omask(fs_inst *inst,
1310 struct brw_reg dst,
1311 struct brw_reg mask)
1312 {
1313 bool stride_8_8_1 =
1314 (mask.vstride == BRW_VERTICAL_STRIDE_8 &&
1315 mask.width == BRW_WIDTH_8 &&
1316 mask.hstride == BRW_HORIZONTAL_STRIDE_1);
1317
1318 bool stride_0_1_0 =
1319 (mask.vstride == BRW_VERTICAL_STRIDE_0 &&
1320 mask.width == BRW_WIDTH_1 &&
1321 mask.hstride == BRW_HORIZONTAL_STRIDE_0);
1322
1323 assert(stride_8_8_1 || stride_0_1_0);
1324 assert(dst.type == BRW_REGISTER_TYPE_UW);
1325
1326 if (dispatch_width == 16)
1327 dst = vec16(dst);
1328 brw_push_insn_state(p);
1329 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1330 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1331
1332 if (stride_8_8_1) {
1333 brw_MOV(p, dst, retype(stride(mask, 16, 8, 2), dst.type));
1334 } else if (stride_0_1_0) {
1335 brw_MOV(p, dst, retype(mask, dst.type));
1336 }
1337 brw_pop_insn_state(p);
1338 }
1339
1340 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1341 * the ADD instruction.
1342 */
1343 void
1344 fs_generator::generate_set_sample_id(fs_inst *inst,
1345 struct brw_reg dst,
1346 struct brw_reg src0,
1347 struct brw_reg src1)
1348 {
1349 assert(dst.type == BRW_REGISTER_TYPE_D ||
1350 dst.type == BRW_REGISTER_TYPE_UD);
1351 assert(src0.type == BRW_REGISTER_TYPE_D ||
1352 src0.type == BRW_REGISTER_TYPE_UD);
1353
1354 brw_push_insn_state(p);
1355 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1356 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1357 struct brw_reg reg = retype(stride(src1, 1, 4, 0), BRW_REGISTER_TYPE_UW);
1358 if (dispatch_width == 8) {
1359 brw_ADD(p, dst, src0, reg);
1360 } else if (dispatch_width == 16) {
1361 brw_ADD(p, firsthalf(dst), firsthalf(src0), reg);
1362 brw_ADD(p, sechalf(dst), sechalf(src0), suboffset(reg, 2));
1363 }
1364 brw_pop_insn_state(p);
1365 }
1366
1367 /**
1368 * Change the register's data type from UD to W, doubling the strides in order
1369 * to compensate for halving the data type width.
1370 */
1371 static struct brw_reg
1372 ud_reg_to_w(struct brw_reg r)
1373 {
1374 assert(r.type == BRW_REGISTER_TYPE_UD);
1375 r.type = BRW_REGISTER_TYPE_W;
1376
1377 /* The BRW_*_STRIDE enums are defined so that incrementing the field
1378 * doubles the real stride.
1379 */
1380 if (r.hstride != 0)
1381 ++r.hstride;
1382 if (r.vstride != 0)
1383 ++r.vstride;
1384
1385 return r;
1386 }
1387
1388 void
1389 fs_generator::generate_pack_half_2x16_split(fs_inst *inst,
1390 struct brw_reg dst,
1391 struct brw_reg x,
1392 struct brw_reg y)
1393 {
1394 assert(brw->gen >= 7);
1395 assert(dst.type == BRW_REGISTER_TYPE_UD);
1396 assert(x.type == BRW_REGISTER_TYPE_F);
1397 assert(y.type == BRW_REGISTER_TYPE_F);
1398
1399 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1400 *
1401 * Because this instruction does not have a 16-bit floating-point type,
1402 * the destination data type must be Word (W).
1403 *
1404 * The destination must be DWord-aligned and specify a horizontal stride
1405 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1406 * each destination channel and the upper word is not modified.
1407 */
1408 struct brw_reg dst_w = ud_reg_to_w(dst);
1409
1410 /* Give each 32-bit channel of dst the form below , where "." means
1411 * unchanged.
1412 * 0x....hhhh
1413 */
1414 brw_F32TO16(p, dst_w, y);
1415
1416 /* Now the form:
1417 * 0xhhhh0000
1418 */
1419 brw_SHL(p, dst, dst, brw_imm_ud(16u));
1420
1421 /* And, finally the form of packHalf2x16's output:
1422 * 0xhhhhllll
1423 */
1424 brw_F32TO16(p, dst_w, x);
1425 }
1426
1427 void
1428 fs_generator::generate_unpack_half_2x16_split(fs_inst *inst,
1429 struct brw_reg dst,
1430 struct brw_reg src)
1431 {
1432 assert(brw->gen >= 7);
1433 assert(dst.type == BRW_REGISTER_TYPE_F);
1434 assert(src.type == BRW_REGISTER_TYPE_UD);
1435
1436 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1437 *
1438 * Because this instruction does not have a 16-bit floating-point type,
1439 * the source data type must be Word (W). The destination type must be
1440 * F (Float).
1441 */
1442 struct brw_reg src_w = ud_reg_to_w(src);
1443
1444 /* Each channel of src has the form of unpackHalf2x16's input: 0xhhhhllll.
1445 * For the Y case, we wish to access only the upper word; therefore
1446 * a 16-bit subregister offset is needed.
1447 */
1448 assert(inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X ||
1449 inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y);
1450 if (inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y)
1451 src_w.subnr += 2;
1452
1453 brw_F16TO32(p, dst, src_w);
1454 }
1455
1456 void
1457 fs_generator::generate_shader_time_add(fs_inst *inst,
1458 struct brw_reg payload,
1459 struct brw_reg offset,
1460 struct brw_reg value)
1461 {
1462 assert(brw->gen >= 7);
1463 brw_push_insn_state(p);
1464 brw_set_default_mask_control(p, true);
1465
1466 assert(payload.file == BRW_GENERAL_REGISTER_FILE);
1467 struct brw_reg payload_offset = retype(brw_vec1_grf(payload.nr, 0),
1468 offset.type);
1469 struct brw_reg payload_value = retype(brw_vec1_grf(payload.nr + 1, 0),
1470 value.type);
1471
1472 assert(offset.file == BRW_IMMEDIATE_VALUE);
1473 if (value.file == BRW_GENERAL_REGISTER_FILE) {
1474 value.width = BRW_WIDTH_1;
1475 value.hstride = BRW_HORIZONTAL_STRIDE_0;
1476 value.vstride = BRW_VERTICAL_STRIDE_0;
1477 } else {
1478 assert(value.file == BRW_IMMEDIATE_VALUE);
1479 }
1480
1481 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1482 * case, and we don't really care about squeezing every bit of performance
1483 * out of this path, so we just emit the MOVs from here.
1484 */
1485 brw_MOV(p, payload_offset, offset);
1486 brw_MOV(p, payload_value, value);
1487 brw_shader_time_add(p, payload,
1488 prog_data->binding_table.shader_time_start);
1489 brw_pop_insn_state(p);
1490
1491 brw_mark_surface_used(prog_data,
1492 prog_data->binding_table.shader_time_start);
1493 }
1494
1495 void
1496 fs_generator::generate_untyped_atomic(fs_inst *inst, struct brw_reg dst,
1497 struct brw_reg payload,
1498 struct brw_reg atomic_op,
1499 struct brw_reg surf_index)
1500 {
1501 assert(atomic_op.file == BRW_IMMEDIATE_VALUE &&
1502 atomic_op.type == BRW_REGISTER_TYPE_UD &&
1503 surf_index.file == BRW_IMMEDIATE_VALUE &&
1504 surf_index.type == BRW_REGISTER_TYPE_UD);
1505
1506 brw_untyped_atomic(p, dst, payload, atomic_op.dw1.ud, surf_index.dw1.ud,
1507 inst->mlen, inst->exec_size / 8);
1508
1509 brw_mark_surface_used(prog_data, surf_index.dw1.ud);
1510 }
1511
1512 void
1513 fs_generator::generate_untyped_surface_read(fs_inst *inst, struct brw_reg dst,
1514 struct brw_reg payload,
1515 struct brw_reg surf_index)
1516 {
1517 assert(surf_index.file == BRW_IMMEDIATE_VALUE &&
1518 surf_index.type == BRW_REGISTER_TYPE_UD);
1519
1520 brw_untyped_surface_read(p, dst, payload,
1521 surf_index.dw1.ud,
1522 inst->mlen, inst->exec_size / 8);
1523
1524 brw_mark_surface_used(prog_data, surf_index.dw1.ud);
1525 }
1526
1527 int
1528 fs_generator::generate_code(const cfg_t *cfg, int dispatch_width)
1529 {
1530 /* align to 64 byte boundary. */
1531 while (p->next_insn_offset % 64)
1532 brw_NOP(p);
1533
1534 this->dispatch_width = dispatch_width;
1535 if (dispatch_width == 16)
1536 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1537
1538 int start_offset = p->next_insn_offset;
1539 int loop_count = 0;
1540
1541 struct annotation_info annotation;
1542 memset(&annotation, 0, sizeof(annotation));
1543
1544 foreach_block_and_inst (block, fs_inst, inst, cfg) {
1545 struct brw_reg src[3], dst;
1546 unsigned int last_insn_offset = p->next_insn_offset;
1547
1548 if (unlikely(debug_flag))
1549 annotate(brw, &annotation, cfg, inst, p->next_insn_offset);
1550
1551 for (unsigned int i = 0; i < inst->sources; i++) {
1552 src[i] = brw_reg_from_fs_reg(&inst->src[i]);
1553
1554 /* The accumulator result appears to get used for the
1555 * conditional modifier generation. When negating a UD
1556 * value, there is a 33rd bit generated for the sign in the
1557 * accumulator value, so now you can't check, for example,
1558 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1559 */
1560 assert(!inst->conditional_mod ||
1561 inst->src[i].type != BRW_REGISTER_TYPE_UD ||
1562 !inst->src[i].negate);
1563 }
1564 dst = brw_reg_from_fs_reg(&inst->dst);
1565
1566 brw_set_default_predicate_control(p, inst->predicate);
1567 brw_set_default_predicate_inverse(p, inst->predicate_inverse);
1568 brw_set_default_flag_reg(p, 0, inst->flag_subreg);
1569 brw_set_default_saturate(p, inst->saturate);
1570 brw_set_default_mask_control(p, inst->force_writemask_all);
1571 brw_set_default_acc_write_control(p, inst->writes_accumulator);
1572
1573 switch (inst->exec_size) {
1574 case 1:
1575 case 2:
1576 case 4:
1577 assert(inst->force_writemask_all);
1578 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1579 break;
1580 case 8:
1581 if (inst->force_sechalf) {
1582 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1583 } else {
1584 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1585 }
1586 break;
1587 case 16:
1588 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1589 break;
1590 default:
1591 unreachable(!"Invalid instruction width");
1592 }
1593
1594 switch (inst->opcode) {
1595 case BRW_OPCODE_MOV:
1596 brw_MOV(p, dst, src[0]);
1597 break;
1598 case BRW_OPCODE_ADD:
1599 brw_ADD(p, dst, src[0], src[1]);
1600 break;
1601 case BRW_OPCODE_MUL:
1602 brw_MUL(p, dst, src[0], src[1]);
1603 break;
1604 case BRW_OPCODE_AVG:
1605 brw_AVG(p, dst, src[0], src[1]);
1606 break;
1607 case BRW_OPCODE_MACH:
1608 brw_MACH(p, dst, src[0], src[1]);
1609 break;
1610
1611 case BRW_OPCODE_MAD:
1612 assert(brw->gen >= 6);
1613 brw_set_default_access_mode(p, BRW_ALIGN_16);
1614 if (dispatch_width == 16 && brw->gen < 8 && !brw->is_haswell) {
1615 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1616 brw_MAD(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1617 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1618 brw_MAD(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1619 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1620 } else {
1621 brw_MAD(p, dst, src[0], src[1], src[2]);
1622 }
1623 brw_set_default_access_mode(p, BRW_ALIGN_1);
1624 break;
1625
1626 case BRW_OPCODE_LRP:
1627 assert(brw->gen >= 6);
1628 brw_set_default_access_mode(p, BRW_ALIGN_16);
1629 if (dispatch_width == 16 && brw->gen < 8 && !brw->is_haswell) {
1630 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1631 brw_LRP(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1632 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1633 brw_LRP(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1634 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1635 } else {
1636 brw_LRP(p, dst, src[0], src[1], src[2]);
1637 }
1638 brw_set_default_access_mode(p, BRW_ALIGN_1);
1639 break;
1640
1641 case BRW_OPCODE_FRC:
1642 brw_FRC(p, dst, src[0]);
1643 break;
1644 case BRW_OPCODE_RNDD:
1645 brw_RNDD(p, dst, src[0]);
1646 break;
1647 case BRW_OPCODE_RNDE:
1648 brw_RNDE(p, dst, src[0]);
1649 break;
1650 case BRW_OPCODE_RNDZ:
1651 brw_RNDZ(p, dst, src[0]);
1652 break;
1653
1654 case BRW_OPCODE_AND:
1655 brw_AND(p, dst, src[0], src[1]);
1656 break;
1657 case BRW_OPCODE_OR:
1658 brw_OR(p, dst, src[0], src[1]);
1659 break;
1660 case BRW_OPCODE_XOR:
1661 brw_XOR(p, dst, src[0], src[1]);
1662 break;
1663 case BRW_OPCODE_NOT:
1664 brw_NOT(p, dst, src[0]);
1665 break;
1666 case BRW_OPCODE_ASR:
1667 brw_ASR(p, dst, src[0], src[1]);
1668 break;
1669 case BRW_OPCODE_SHR:
1670 brw_SHR(p, dst, src[0], src[1]);
1671 break;
1672 case BRW_OPCODE_SHL:
1673 brw_SHL(p, dst, src[0], src[1]);
1674 break;
1675 case BRW_OPCODE_F32TO16:
1676 assert(brw->gen >= 7);
1677 brw_F32TO16(p, dst, src[0]);
1678 break;
1679 case BRW_OPCODE_F16TO32:
1680 assert(brw->gen >= 7);
1681 brw_F16TO32(p, dst, src[0]);
1682 break;
1683 case BRW_OPCODE_CMP:
1684 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
1685 break;
1686 case BRW_OPCODE_SEL:
1687 brw_SEL(p, dst, src[0], src[1]);
1688 break;
1689 case BRW_OPCODE_BFREV:
1690 assert(brw->gen >= 7);
1691 /* BFREV only supports UD type for src and dst. */
1692 brw_BFREV(p, retype(dst, BRW_REGISTER_TYPE_UD),
1693 retype(src[0], BRW_REGISTER_TYPE_UD));
1694 break;
1695 case BRW_OPCODE_FBH:
1696 assert(brw->gen >= 7);
1697 /* FBH only supports UD type for dst. */
1698 brw_FBH(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1699 break;
1700 case BRW_OPCODE_FBL:
1701 assert(brw->gen >= 7);
1702 /* FBL only supports UD type for dst. */
1703 brw_FBL(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1704 break;
1705 case BRW_OPCODE_CBIT:
1706 assert(brw->gen >= 7);
1707 /* CBIT only supports UD type for dst. */
1708 brw_CBIT(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1709 break;
1710 case BRW_OPCODE_ADDC:
1711 assert(brw->gen >= 7);
1712 brw_ADDC(p, dst, src[0], src[1]);
1713 break;
1714 case BRW_OPCODE_SUBB:
1715 assert(brw->gen >= 7);
1716 brw_SUBB(p, dst, src[0], src[1]);
1717 break;
1718 case BRW_OPCODE_MAC:
1719 brw_MAC(p, dst, src[0], src[1]);
1720 break;
1721
1722 case BRW_OPCODE_BFE:
1723 assert(brw->gen >= 7);
1724 brw_set_default_access_mode(p, BRW_ALIGN_16);
1725 if (dispatch_width == 16 && brw->gen < 8 && !brw->is_haswell) {
1726 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1727 brw_BFE(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1728 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1729 brw_BFE(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1730 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1731 } else {
1732 brw_BFE(p, dst, src[0], src[1], src[2]);
1733 }
1734 brw_set_default_access_mode(p, BRW_ALIGN_1);
1735 break;
1736
1737 case BRW_OPCODE_BFI1:
1738 assert(brw->gen >= 7);
1739 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1740 * should
1741 *
1742 * "Force BFI instructions to be executed always in SIMD8."
1743 */
1744 if (dispatch_width == 16 && brw->is_haswell) {
1745 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1746 brw_BFI1(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]));
1747 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1748 brw_BFI1(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]));
1749 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1750 } else {
1751 brw_BFI1(p, dst, src[0], src[1]);
1752 }
1753 break;
1754 case BRW_OPCODE_BFI2:
1755 assert(brw->gen >= 7);
1756 brw_set_default_access_mode(p, BRW_ALIGN_16);
1757 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1758 * should
1759 *
1760 * "Force BFI instructions to be executed always in SIMD8."
1761 *
1762 * Otherwise we would be able to emit compressed instructions like we
1763 * do for the other three-source instructions.
1764 */
1765 if (dispatch_width == 16 && brw->gen < 8) {
1766 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1767 brw_BFI2(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1768 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1769 brw_BFI2(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1770 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1771 } else {
1772 brw_BFI2(p, dst, src[0], src[1], src[2]);
1773 }
1774 brw_set_default_access_mode(p, BRW_ALIGN_1);
1775 break;
1776
1777 case BRW_OPCODE_IF:
1778 if (inst->src[0].file != BAD_FILE) {
1779 /* The instruction has an embedded compare (only allowed on gen6) */
1780 assert(brw->gen == 6);
1781 gen6_IF(p, inst->conditional_mod, src[0], src[1]);
1782 } else {
1783 brw_IF(p, dispatch_width == 16 ? BRW_EXECUTE_16 : BRW_EXECUTE_8);
1784 }
1785 break;
1786
1787 case BRW_OPCODE_ELSE:
1788 brw_ELSE(p);
1789 break;
1790 case BRW_OPCODE_ENDIF:
1791 brw_ENDIF(p);
1792 break;
1793
1794 case BRW_OPCODE_DO:
1795 brw_DO(p, BRW_EXECUTE_8);
1796 break;
1797
1798 case BRW_OPCODE_BREAK:
1799 brw_BREAK(p);
1800 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1801 break;
1802 case BRW_OPCODE_CONTINUE:
1803 brw_CONT(p);
1804 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
1805 break;
1806
1807 case BRW_OPCODE_WHILE:
1808 brw_WHILE(p);
1809 loop_count++;
1810 break;
1811
1812 case SHADER_OPCODE_RCP:
1813 case SHADER_OPCODE_RSQ:
1814 case SHADER_OPCODE_SQRT:
1815 case SHADER_OPCODE_EXP2:
1816 case SHADER_OPCODE_LOG2:
1817 case SHADER_OPCODE_SIN:
1818 case SHADER_OPCODE_COS:
1819 assert(brw->gen < 6 || inst->mlen == 0);
1820 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
1821 if (brw->gen >= 7) {
1822 gen6_math(p, dst, brw_math_function(inst->opcode), src[0],
1823 brw_null_reg());
1824 } else if (brw->gen == 6) {
1825 generate_math_gen6(inst, dst, src[0], brw_null_reg());
1826 } else if (brw->gen == 5 || brw->is_g4x) {
1827 generate_math_g45(inst, dst, src[0]);
1828 } else {
1829 generate_math_gen4(inst, dst, src[0]);
1830 }
1831 break;
1832 case SHADER_OPCODE_INT_QUOTIENT:
1833 case SHADER_OPCODE_INT_REMAINDER:
1834 case SHADER_OPCODE_POW:
1835 assert(brw->gen < 6 || inst->mlen == 0);
1836 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
1837 if (brw->gen >= 7 && inst->opcode == SHADER_OPCODE_POW) {
1838 gen6_math(p, dst, brw_math_function(inst->opcode), src[0], src[1]);
1839 } else if (brw->gen >= 6) {
1840 generate_math_gen6(inst, dst, src[0], src[1]);
1841 } else {
1842 generate_math_gen4(inst, dst, src[0]);
1843 }
1844 break;
1845 case FS_OPCODE_PIXEL_X:
1846 generate_pixel_xy(dst, true);
1847 break;
1848 case FS_OPCODE_PIXEL_Y:
1849 generate_pixel_xy(dst, false);
1850 break;
1851 case FS_OPCODE_CINTERP:
1852 brw_MOV(p, dst, src[0]);
1853 break;
1854 case FS_OPCODE_LINTERP:
1855 generate_linterp(inst, dst, src);
1856 break;
1857 case SHADER_OPCODE_TEX:
1858 case FS_OPCODE_TXB:
1859 case SHADER_OPCODE_TXD:
1860 case SHADER_OPCODE_TXF:
1861 case SHADER_OPCODE_TXF_CMS:
1862 case SHADER_OPCODE_TXF_UMS:
1863 case SHADER_OPCODE_TXF_MCS:
1864 case SHADER_OPCODE_TXL:
1865 case SHADER_OPCODE_TXS:
1866 case SHADER_OPCODE_LOD:
1867 case SHADER_OPCODE_TG4:
1868 case SHADER_OPCODE_TG4_OFFSET:
1869 generate_tex(inst, dst, src[0], src[1]);
1870 break;
1871 case FS_OPCODE_DDX:
1872 generate_ddx(inst, dst, src[0], src[1]);
1873 break;
1874 case FS_OPCODE_DDY:
1875 /* Make sure fp->UsesDFdy flag got set (otherwise there's no
1876 * guarantee that key->render_to_fbo is set).
1877 */
1878 assert(stage == MESA_SHADER_FRAGMENT &&
1879 ((gl_fragment_program *) prog)->UsesDFdy);
1880 generate_ddy(inst, dst, src[0], src[1],
1881 ((brw_wm_prog_key * const) this->key)->render_to_fbo);
1882 break;
1883
1884 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
1885 generate_scratch_write(inst, src[0]);
1886 break;
1887
1888 case SHADER_OPCODE_GEN4_SCRATCH_READ:
1889 generate_scratch_read(inst, dst);
1890 break;
1891
1892 case SHADER_OPCODE_GEN7_SCRATCH_READ:
1893 generate_scratch_read_gen7(inst, dst);
1894 break;
1895
1896 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
1897 generate_uniform_pull_constant_load(inst, dst, src[0], src[1]);
1898 break;
1899
1900 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
1901 generate_uniform_pull_constant_load_gen7(inst, dst, src[0], src[1]);
1902 break;
1903
1904 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
1905 generate_varying_pull_constant_load(inst, dst, src[0], src[1]);
1906 break;
1907
1908 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
1909 generate_varying_pull_constant_load_gen7(inst, dst, src[0], src[1]);
1910 break;
1911
1912 case FS_OPCODE_REP_FB_WRITE:
1913 case FS_OPCODE_FB_WRITE:
1914 generate_fb_write(inst, src[0]);
1915 break;
1916
1917 case FS_OPCODE_BLORP_FB_WRITE:
1918 generate_blorp_fb_write(inst);
1919 break;
1920
1921 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS:
1922 generate_mov_dispatch_to_flags(inst);
1923 break;
1924
1925 case FS_OPCODE_DISCARD_JUMP:
1926 generate_discard_jump(inst);
1927 break;
1928
1929 case SHADER_OPCODE_SHADER_TIME_ADD:
1930 generate_shader_time_add(inst, src[0], src[1], src[2]);
1931 break;
1932
1933 case SHADER_OPCODE_UNTYPED_ATOMIC:
1934 generate_untyped_atomic(inst, dst, src[0], src[1], src[2]);
1935 break;
1936
1937 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
1938 generate_untyped_surface_read(inst, dst, src[0], src[1]);
1939 break;
1940
1941 case FS_OPCODE_SET_SIMD4X2_OFFSET:
1942 generate_set_simd4x2_offset(inst, dst, src[0]);
1943 break;
1944
1945 case FS_OPCODE_SET_OMASK:
1946 generate_set_omask(inst, dst, src[0]);
1947 break;
1948
1949 case FS_OPCODE_SET_SAMPLE_ID:
1950 generate_set_sample_id(inst, dst, src[0], src[1]);
1951 break;
1952
1953 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
1954 generate_pack_half_2x16_split(inst, dst, src[0], src[1]);
1955 break;
1956
1957 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X:
1958 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y:
1959 generate_unpack_half_2x16_split(inst, dst, src[0]);
1960 break;
1961
1962 case FS_OPCODE_PLACEHOLDER_HALT:
1963 /* This is the place where the final HALT needs to be inserted if
1964 * we've emitted any discards. If not, this will emit no code.
1965 */
1966 if (!patch_discard_jumps_to_fb_writes()) {
1967 if (unlikely(debug_flag)) {
1968 annotation.ann_count--;
1969 }
1970 }
1971 break;
1972
1973 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
1974 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
1975 GEN7_PIXEL_INTERPOLATOR_LOC_CENTROID);
1976 break;
1977
1978 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
1979 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
1980 GEN7_PIXEL_INTERPOLATOR_LOC_SAMPLE);
1981 break;
1982
1983 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
1984 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
1985 GEN7_PIXEL_INTERPOLATOR_LOC_SHARED_OFFSET);
1986 break;
1987
1988 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
1989 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
1990 GEN7_PIXEL_INTERPOLATOR_LOC_PER_SLOT_OFFSET);
1991 break;
1992
1993 default:
1994 if (inst->opcode < (int) ARRAY_SIZE(opcode_descs)) {
1995 _mesa_problem(ctx, "Unsupported opcode `%s' in FS",
1996 opcode_descs[inst->opcode].name);
1997 } else {
1998 _mesa_problem(ctx, "Unsupported opcode %d in FS", inst->opcode);
1999 }
2000 abort();
2001
2002 case SHADER_OPCODE_LOAD_PAYLOAD:
2003 unreachable("Should be lowered by lower_load_payload()");
2004 }
2005
2006 if (inst->no_dd_clear || inst->no_dd_check || inst->conditional_mod) {
2007 assert(p->next_insn_offset == last_insn_offset + 16 ||
2008 !"conditional_mod, no_dd_check, or no_dd_clear set for IR "
2009 "emitting more than 1 instruction");
2010
2011 brw_inst *last = &p->store[last_insn_offset / 16];
2012
2013 if (inst->conditional_mod)
2014 brw_inst_set_cond_modifier(brw, last, inst->conditional_mod);
2015 brw_inst_set_no_dd_clear(brw, last, inst->no_dd_clear);
2016 brw_inst_set_no_dd_check(brw, last, inst->no_dd_check);
2017 }
2018 }
2019
2020 brw_set_uip_jip(p);
2021 annotation_finalize(&annotation, p->next_insn_offset);
2022
2023 int before_size = p->next_insn_offset - start_offset;
2024 brw_compact_instructions(p, start_offset, annotation.ann_count,
2025 annotation.ann);
2026 int after_size = p->next_insn_offset - start_offset;
2027
2028 if (unlikely(debug_flag)) {
2029 if (shader_prog) {
2030 fprintf(stderr,
2031 "Native code for %s fragment shader %d (SIMD%d dispatch):\n",
2032 shader_prog->Label ? shader_prog->Label : "unnamed",
2033 shader_prog->Name, dispatch_width);
2034 } else if (prog) {
2035 fprintf(stderr,
2036 "Native code for fragment program %d (SIMD%d dispatch):\n",
2037 prog->Id, dispatch_width);
2038 } else {
2039 fprintf(stderr, "Native code for blorp program (SIMD%d dispatch):\n",
2040 dispatch_width);
2041 }
2042 fprintf(stderr, "SIMD%d shader: %d instructions. %d loops. Compacted %d to %d"
2043 " bytes (%.0f%%)\n",
2044 dispatch_width, before_size / 16, loop_count, before_size, after_size,
2045 100.0f * (before_size - after_size) / before_size);
2046
2047 dump_assembly(p->store, annotation.ann_count, annotation.ann, brw, prog);
2048 ralloc_free(annotation.ann);
2049 }
2050
2051 return start_offset;
2052 }
2053
2054 const unsigned *
2055 fs_generator::get_assembly(unsigned int *assembly_size)
2056 {
2057 return brw_get_program(p, assembly_size);
2058 }