ff8d37e2c0ccf274d003fc9859ceb7b2273e2265
[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 #include "brw_eu.h"
31 #include "brw_fs.h"
32 #include "brw_cfg.h"
33 #include "brw_program.h"
34
35 static enum brw_reg_file
36 brw_file_from_reg(fs_reg *reg)
37 {
38 switch (reg->file) {
39 case ARF:
40 return BRW_ARCHITECTURE_REGISTER_FILE;
41 case FIXED_GRF:
42 case VGRF:
43 return BRW_GENERAL_REGISTER_FILE;
44 case MRF:
45 return BRW_MESSAGE_REGISTER_FILE;
46 case IMM:
47 return BRW_IMMEDIATE_VALUE;
48 case BAD_FILE:
49 case ATTR:
50 case UNIFORM:
51 unreachable("not reached");
52 }
53 return BRW_ARCHITECTURE_REGISTER_FILE;
54 }
55
56 static struct brw_reg
57 brw_reg_from_fs_reg(fs_inst *inst, fs_reg *reg, unsigned gen)
58 {
59 struct brw_reg brw_reg;
60
61 switch (reg->file) {
62 case MRF:
63 assert((reg->nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(gen));
64 /* Fallthrough */
65 case VGRF:
66 if (reg->stride == 0) {
67 brw_reg = brw_vec1_reg(brw_file_from_reg(reg), reg->nr, 0);
68 } else if (inst->exec_size < 8) {
69 brw_reg = brw_vec8_reg(brw_file_from_reg(reg), reg->nr, 0);
70 brw_reg = stride(brw_reg, inst->exec_size * reg->stride,
71 inst->exec_size, reg->stride);
72 } else {
73 /* From the Haswell PRM:
74 *
75 * VertStride must be used to cross GRF register boundaries. This
76 * rule implies that elements within a 'Width' cannot cross GRF
77 * boundaries.
78 *
79 * So, for registers with width > 8, we have to use a width of 8
80 * and trust the compression state to sort out the exec size.
81 */
82 brw_reg = brw_vec8_reg(brw_file_from_reg(reg), reg->nr, 0);
83 brw_reg = stride(brw_reg, 8 * reg->stride, 8, reg->stride);
84 }
85
86 brw_reg = retype(brw_reg, reg->type);
87 brw_reg = byte_offset(brw_reg, reg->subreg_offset);
88 brw_reg.abs = reg->abs;
89 brw_reg.negate = reg->negate;
90 break;
91 case ARF:
92 case FIXED_GRF:
93 case IMM:
94 brw_reg = reg->as_brw_reg();
95 break;
96 case BAD_FILE:
97 /* Probably unused. */
98 brw_reg = brw_null_reg();
99 break;
100 case ATTR:
101 case UNIFORM:
102 unreachable("not reached");
103 }
104
105 return brw_reg;
106 }
107
108 fs_generator::fs_generator(const struct brw_compiler *compiler, void *log_data,
109 void *mem_ctx,
110 const void *key,
111 struct brw_stage_prog_data *prog_data,
112 unsigned promoted_constants,
113 bool runtime_check_aads_emit,
114 gl_shader_stage stage)
115
116 : compiler(compiler), log_data(log_data),
117 devinfo(compiler->devinfo), key(key),
118 prog_data(prog_data),
119 promoted_constants(promoted_constants),
120 runtime_check_aads_emit(runtime_check_aads_emit), debug_flag(false),
121 stage(stage), mem_ctx(mem_ctx)
122 {
123 p = rzalloc(mem_ctx, struct brw_codegen);
124 brw_init_codegen(devinfo, p, mem_ctx);
125 }
126
127 fs_generator::~fs_generator()
128 {
129 }
130
131 class ip_record : public exec_node {
132 public:
133 DECLARE_RALLOC_CXX_OPERATORS(ip_record)
134
135 ip_record(int ip)
136 {
137 this->ip = ip;
138 }
139
140 int ip;
141 };
142
143 bool
144 fs_generator::patch_discard_jumps_to_fb_writes()
145 {
146 if (devinfo->gen < 6 || this->discard_halt_patches.is_empty())
147 return false;
148
149 int scale = brw_jump_scale(p->devinfo);
150
151 /* There is a somewhat strange undocumented requirement of using
152 * HALT, according to the simulator. If some channel has HALTed to
153 * a particular UIP, then by the end of the program, every channel
154 * must have HALTed to that UIP. Furthermore, the tracking is a
155 * stack, so you can't do the final halt of a UIP after starting
156 * halting to a new UIP.
157 *
158 * Symptoms of not emitting this instruction on actual hardware
159 * included GPU hangs and sparkly rendering on the piglit discard
160 * tests.
161 */
162 brw_inst *last_halt = gen6_HALT(p);
163 brw_inst_set_uip(p->devinfo, last_halt, 1 * scale);
164 brw_inst_set_jip(p->devinfo, last_halt, 1 * scale);
165
166 int ip = p->nr_insn;
167
168 foreach_in_list(ip_record, patch_ip, &discard_halt_patches) {
169 brw_inst *patch = &p->store[patch_ip->ip];
170
171 assert(brw_inst_opcode(p->devinfo, patch) == BRW_OPCODE_HALT);
172 /* HALT takes a half-instruction distance from the pre-incremented IP. */
173 brw_inst_set_uip(p->devinfo, patch, (ip - patch_ip->ip) * scale);
174 }
175
176 this->discard_halt_patches.make_empty();
177 return true;
178 }
179
180 void
181 fs_generator::fire_fb_write(fs_inst *inst,
182 struct brw_reg payload,
183 struct brw_reg implied_header,
184 GLuint nr)
185 {
186 uint32_t msg_control;
187
188 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
189
190 if (devinfo->gen < 6) {
191 brw_push_insn_state(p);
192 brw_set_default_exec_size(p, BRW_EXECUTE_8);
193 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
194 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
195 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
196 brw_MOV(p, offset(payload, 1), brw_vec8_grf(1, 0));
197 brw_pop_insn_state(p);
198 }
199
200 if (inst->opcode == FS_OPCODE_REP_FB_WRITE)
201 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
202 else if (prog_data->dual_src_blend) {
203 if (!inst->force_sechalf)
204 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01;
205 else
206 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN23;
207 } else if (inst->exec_size == 16)
208 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
209 else
210 msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01;
211
212 uint32_t surf_index =
213 prog_data->binding_table.render_target_start + inst->target;
214
215 bool last_render_target = inst->eot ||
216 (prog_data->dual_src_blend && dispatch_width == 16);
217
218
219 brw_fb_WRITE(p,
220 dispatch_width,
221 payload,
222 implied_header,
223 msg_control,
224 surf_index,
225 nr,
226 0,
227 inst->eot,
228 last_render_target,
229 inst->header_size != 0);
230
231 brw_mark_surface_used(&prog_data->base, surf_index);
232 }
233
234 void
235 fs_generator::generate_fb_write(fs_inst *inst, struct brw_reg payload)
236 {
237 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
238 const brw_wm_prog_key * const key = (brw_wm_prog_key * const) this->key;
239 struct brw_reg implied_header;
240
241 if (devinfo->gen < 8 && !devinfo->is_haswell) {
242 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
243 }
244
245 if (inst->base_mrf >= 0)
246 payload = brw_message_reg(inst->base_mrf);
247
248 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
249 * move, here's g1.
250 */
251 if (inst->header_size != 0) {
252 brw_push_insn_state(p);
253 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
254 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
255 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
256 brw_set_default_flag_reg(p, 0, 0);
257
258 /* On HSW, the GPU will use the predicate on SENDC, unless the header is
259 * present.
260 */
261 if (prog_data->uses_kill) {
262 struct brw_reg pixel_mask;
263
264 if (devinfo->gen >= 6)
265 pixel_mask = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
266 else
267 pixel_mask = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
268
269 brw_MOV(p, pixel_mask, brw_flag_reg(0, 1));
270 }
271
272 if (devinfo->gen >= 6) {
273 brw_push_insn_state(p);
274 brw_set_default_exec_size(p, BRW_EXECUTE_16);
275 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
276 brw_MOV(p,
277 retype(payload, BRW_REGISTER_TYPE_UD),
278 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
279 brw_pop_insn_state(p);
280
281 if (inst->target > 0 && key->replicate_alpha) {
282 /* Set "Source0 Alpha Present to RenderTarget" bit in message
283 * header.
284 */
285 brw_OR(p,
286 vec1(retype(payload, BRW_REGISTER_TYPE_UD)),
287 vec1(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD)),
288 brw_imm_ud(0x1 << 11));
289 }
290
291 if (inst->target > 0) {
292 /* Set the render target index for choosing BLEND_STATE. */
293 brw_MOV(p, retype(vec1(suboffset(payload, 2)),
294 BRW_REGISTER_TYPE_UD),
295 brw_imm_ud(inst->target));
296 }
297
298 /* Set computes stencil to render target */
299 if (prog_data->computed_stencil) {
300 brw_OR(p,
301 vec1(retype(payload, BRW_REGISTER_TYPE_UD)),
302 vec1(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD)),
303 brw_imm_ud(0x1 << 14));
304 }
305
306 implied_header = brw_null_reg();
307 } else {
308 implied_header = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
309 }
310
311 brw_pop_insn_state(p);
312 } else {
313 implied_header = brw_null_reg();
314 }
315
316 if (!runtime_check_aads_emit) {
317 fire_fb_write(inst, payload, implied_header, inst->mlen);
318 } else {
319 /* This can only happen in gen < 6 */
320 assert(devinfo->gen < 6);
321
322 struct brw_reg v1_null_ud = vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD));
323
324 /* Check runtime bit to detect if we have to send AA data or not */
325 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
326 brw_AND(p,
327 v1_null_ud,
328 retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD),
329 brw_imm_ud(1<<26));
330 brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
331
332 int jmp = brw_JMPI(p, brw_imm_ud(0), BRW_PREDICATE_NORMAL) - p->store;
333 brw_inst_set_exec_size(p->devinfo, brw_last_inst, BRW_EXECUTE_1);
334 {
335 /* Don't send AA data */
336 fire_fb_write(inst, offset(payload, 1), implied_header, inst->mlen-1);
337 }
338 brw_land_fwd_jump(p, jmp);
339 fire_fb_write(inst, payload, implied_header, inst->mlen);
340 }
341 }
342
343 void
344 fs_generator::generate_mov_indirect(fs_inst *inst,
345 struct brw_reg dst,
346 struct brw_reg reg,
347 struct brw_reg indirect_byte_offset)
348 {
349 assert(indirect_byte_offset.type == BRW_REGISTER_TYPE_UD);
350 assert(indirect_byte_offset.file == BRW_GENERAL_REGISTER_FILE);
351
352 unsigned imm_byte_offset = reg.nr * REG_SIZE + reg.subnr;
353
354 if (indirect_byte_offset.file == BRW_IMMEDIATE_VALUE) {
355 imm_byte_offset += indirect_byte_offset.ud;
356
357 reg.nr = imm_byte_offset / REG_SIZE;
358 reg.subnr = imm_byte_offset % REG_SIZE;
359 brw_MOV(p, dst, reg);
360 } else {
361 /* Prior to Broadwell, there are only 8 address registers. */
362 assert(inst->exec_size == 8 || devinfo->gen >= 8);
363
364 /* We use VxH indirect addressing, clobbering a0.0 through a0.7. */
365 struct brw_reg addr = vec8(brw_address_reg(0));
366
367 /* The destination stride of an instruction (in bytes) must be greater
368 * than or equal to the size of the rest of the instruction. Since the
369 * address register is of type UW, we can't use a D-type instruction.
370 * In order to get around this, re retype to UW and use a stride.
371 */
372 indirect_byte_offset =
373 retype(spread(indirect_byte_offset, 2), BRW_REGISTER_TYPE_UW);
374
375 struct brw_reg ind_src;
376 if (devinfo->gen < 8) {
377 /* From the Haswell PRM section "Register Region Restrictions":
378 *
379 * "The lower bits of the AddressImmediate must not overflow to
380 * change the register address. The lower 5 bits of Address
381 * Immediate when added to lower 5 bits of address register gives
382 * the sub-register offset. The upper bits of Address Immediate
383 * when added to upper bits of address register gives the register
384 * address. Any overflow from sub-register offset is dropped."
385 *
386 * This restriction is only listed in the Haswell PRM but emperical
387 * testing indicates that it applies on all older generations and is
388 * lifted on Broadwell.
389 *
390 * Since the indirect may cause us to cross a register boundary, this
391 * makes the base offset almost useless. We could try and do
392 * something clever where we use a actual base offset if
393 * base_offset % 32 == 0 but that would mean we were generating
394 * different code depending on the base offset. Instead, for the
395 * sake of consistency, we'll just do the add ourselves.
396 */
397 brw_ADD(p, addr, indirect_byte_offset, brw_imm_uw(imm_byte_offset));
398 ind_src = brw_VxH_indirect(0, 0);
399 } else {
400 brw_MOV(p, addr, indirect_byte_offset);
401 ind_src = brw_VxH_indirect(0, imm_byte_offset);
402 }
403
404 brw_inst *mov = brw_MOV(p, dst, retype(ind_src, dst.type));
405
406 if (devinfo->gen == 6 && dst.file == BRW_MESSAGE_REGISTER_FILE &&
407 !inst->get_next()->is_tail_sentinel() &&
408 ((fs_inst *)inst->get_next())->mlen > 0) {
409 /* From the Sandybridge PRM:
410 *
411 * "[Errata: DevSNB(SNB)] If MRF register is updated by any
412 * instruction that “indexed/indirect” source AND is followed by a
413 * send, the instruction requires a “Switch”. This is to avoid
414 * race condition where send may dispatch before MRF is updated."
415 */
416 brw_inst_set_thread_control(devinfo, mov, BRW_THREAD_SWITCH);
417 }
418 }
419 }
420
421 void
422 fs_generator::generate_urb_read(fs_inst *inst,
423 struct brw_reg dst,
424 struct brw_reg header)
425 {
426 assert(header.file == BRW_GENERAL_REGISTER_FILE);
427 assert(header.type == BRW_REGISTER_TYPE_UD);
428
429 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
430 brw_set_dest(p, send, dst);
431 brw_set_src0(p, send, header);
432 brw_set_src1(p, send, brw_imm_ud(0u));
433
434 brw_inst_set_sfid(p->devinfo, send, BRW_SFID_URB);
435 brw_inst_set_urb_opcode(p->devinfo, send, GEN8_URB_OPCODE_SIMD8_READ);
436
437 if (inst->opcode == SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT)
438 brw_inst_set_urb_per_slot_offset(p->devinfo, send, true);
439
440 brw_inst_set_mlen(p->devinfo, send, inst->mlen);
441 brw_inst_set_rlen(p->devinfo, send, inst->regs_written);
442 brw_inst_set_header_present(p->devinfo, send, true);
443 brw_inst_set_urb_global_offset(p->devinfo, send, inst->offset);
444 }
445
446 void
447 fs_generator::generate_urb_write(fs_inst *inst, struct brw_reg payload)
448 {
449 brw_inst *insn;
450
451 insn = brw_next_insn(p, BRW_OPCODE_SEND);
452
453 brw_set_dest(p, insn, brw_null_reg());
454 brw_set_src0(p, insn, payload);
455 brw_set_src1(p, insn, brw_imm_d(0));
456
457 brw_inst_set_sfid(p->devinfo, insn, BRW_SFID_URB);
458 brw_inst_set_urb_opcode(p->devinfo, insn, GEN8_URB_OPCODE_SIMD8_WRITE);
459
460 if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT ||
461 inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)
462 brw_inst_set_urb_per_slot_offset(p->devinfo, insn, true);
463
464 if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED ||
465 inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)
466 brw_inst_set_urb_channel_mask_present(p->devinfo, insn, true);
467
468 brw_inst_set_mlen(p->devinfo, insn, inst->mlen);
469 brw_inst_set_rlen(p->devinfo, insn, 0);
470 brw_inst_set_eot(p->devinfo, insn, inst->eot);
471 brw_inst_set_header_present(p->devinfo, insn, true);
472 brw_inst_set_urb_global_offset(p->devinfo, insn, inst->offset);
473 }
474
475 void
476 fs_generator::generate_cs_terminate(fs_inst *inst, struct brw_reg payload)
477 {
478 struct brw_inst *insn;
479
480 insn = brw_next_insn(p, BRW_OPCODE_SEND);
481
482 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
483 brw_set_src0(p, insn, payload);
484 brw_set_src1(p, insn, brw_imm_d(0));
485
486 /* Terminate a compute shader by sending a message to the thread spawner.
487 */
488 brw_inst_set_sfid(devinfo, insn, BRW_SFID_THREAD_SPAWNER);
489 brw_inst_set_mlen(devinfo, insn, 1);
490 brw_inst_set_rlen(devinfo, insn, 0);
491 brw_inst_set_eot(devinfo, insn, inst->eot);
492 brw_inst_set_header_present(devinfo, insn, false);
493
494 brw_inst_set_ts_opcode(devinfo, insn, 0); /* Dereference resource */
495 brw_inst_set_ts_request_type(devinfo, insn, 0); /* Root thread */
496
497 /* Note that even though the thread has a URB resource associated with it,
498 * we set the "do not dereference URB" bit, because the URB resource is
499 * managed by the fixed-function unit, so it will free it automatically.
500 */
501 brw_inst_set_ts_resource_select(devinfo, insn, 1); /* Do not dereference URB */
502
503 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);
504 }
505
506 void
507 fs_generator::generate_stencil_ref_packing(fs_inst *inst,
508 struct brw_reg dst,
509 struct brw_reg src)
510 {
511 assert(dispatch_width == 8);
512 assert(devinfo->gen >= 9);
513
514 /* Stencil value updates are provided in 8 slots of 1 byte per slot.
515 * Presumably, in order to save memory bandwidth, the stencil reference
516 * values written from the FS need to be packed into 2 dwords (this makes
517 * sense because the stencil values are limited to 1 byte each and a SIMD8
518 * send, so stencil slots 0-3 in dw0, and 4-7 in dw1.)
519 *
520 * The spec is confusing here because in the payload definition of MDP_RTW_S8
521 * (Message Data Payload for Render Target Writes with Stencil 8b) the
522 * stencil value seems to be dw4.0-dw4.7. However, if you look at the type of
523 * dw4 it is type MDPR_STENCIL (Message Data Payload Register) which is the
524 * packed values specified above and diagrammed below:
525 *
526 * 31 0
527 * --------------------------------
528 * DW | |
529 * 2-7 | IGNORED |
530 * | |
531 * --------------------------------
532 * DW1 | STC | STC | STC | STC |
533 * | slot7 | slot6 | slot5 | slot4|
534 * --------------------------------
535 * DW0 | STC | STC | STC | STC |
536 * | slot3 | slot2 | slot1 | slot0|
537 * --------------------------------
538 */
539
540 src.vstride = BRW_VERTICAL_STRIDE_4;
541 src.width = BRW_WIDTH_1;
542 src.hstride = BRW_HORIZONTAL_STRIDE_0;
543 assert(src.type == BRW_REGISTER_TYPE_UB);
544 brw_MOV(p, retype(dst, BRW_REGISTER_TYPE_UB), src);
545 }
546
547 void
548 fs_generator::generate_barrier(fs_inst *inst, struct brw_reg src)
549 {
550 brw_barrier(p, src);
551 brw_WAIT(p);
552 }
553
554 void
555 fs_generator::generate_blorp_fb_write(fs_inst *inst, struct brw_reg payload)
556 {
557 brw_fb_WRITE(p,
558 16 /* dispatch_width */,
559 inst->base_mrf >= 0 ?
560 brw_message_reg(inst->base_mrf) : payload,
561 brw_null_reg(),
562 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
563 inst->target,
564 inst->mlen,
565 0,
566 true,
567 true,
568 inst->header_size != 0);
569 }
570
571 void
572 fs_generator::generate_linterp(fs_inst *inst,
573 struct brw_reg dst, struct brw_reg *src)
574 {
575 /* PLN reads:
576 * / in SIMD16 \
577 * -----------------------------------
578 * | src1+0 | src1+1 | src1+2 | src1+3 |
579 * |-----------------------------------|
580 * |(x0, x1)|(y0, y1)|(x2, x3)|(y2, y3)|
581 * -----------------------------------
582 *
583 * but for the LINE/MAC pair, the LINE reads Xs and the MAC reads Ys:
584 *
585 * -----------------------------------
586 * | src1+0 | src1+1 | src1+2 | src1+3 |
587 * |-----------------------------------|
588 * |(x0, x1)|(y0, y1)| | | in SIMD8
589 * |-----------------------------------|
590 * |(x0, x1)|(x2, x3)|(y0, y1)|(y2, y3)| in SIMD16
591 * -----------------------------------
592 *
593 * See also: emit_interpolation_setup_gen4().
594 */
595 struct brw_reg delta_x = src[0];
596 struct brw_reg delta_y = offset(src[0], dispatch_width / 8);
597 struct brw_reg interp = src[1];
598
599 if (devinfo->has_pln &&
600 (devinfo->gen >= 7 || (delta_x.nr & 1) == 0)) {
601 brw_PLN(p, dst, interp, delta_x);
602 } else {
603 brw_LINE(p, brw_null_reg(), interp, delta_x);
604 brw_MAC(p, dst, suboffset(interp, 1), delta_y);
605 }
606 }
607
608 void
609 fs_generator::generate_math_gen6(fs_inst *inst,
610 struct brw_reg dst,
611 struct brw_reg src0,
612 struct brw_reg src1)
613 {
614 int op = brw_math_function(inst->opcode);
615 bool binop = src1.file != BRW_ARCHITECTURE_REGISTER_FILE;
616
617 if (dispatch_width == 8) {
618 gen6_math(p, dst, op, src0, src1);
619 } else if (dispatch_width == 16) {
620 brw_push_insn_state(p);
621 brw_set_default_exec_size(p, BRW_EXECUTE_8);
622 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
623 gen6_math(p, firsthalf(dst), op, firsthalf(src0), firsthalf(src1));
624 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
625 gen6_math(p, sechalf(dst), op, sechalf(src0),
626 binop ? sechalf(src1) : brw_null_reg());
627 brw_pop_insn_state(p);
628 }
629 }
630
631 void
632 fs_generator::generate_math_gen4(fs_inst *inst,
633 struct brw_reg dst,
634 struct brw_reg src)
635 {
636 int op = brw_math_function(inst->opcode);
637
638 assert(inst->mlen >= 1);
639
640 if (dispatch_width == 8) {
641 gen4_math(p, dst,
642 op,
643 inst->base_mrf, src,
644 BRW_MATH_PRECISION_FULL);
645 } else if (dispatch_width == 16) {
646 brw_set_default_exec_size(p, BRW_EXECUTE_8);
647 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
648 gen4_math(p, firsthalf(dst),
649 op,
650 inst->base_mrf, firsthalf(src),
651 BRW_MATH_PRECISION_FULL);
652 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
653 gen4_math(p, sechalf(dst),
654 op,
655 inst->base_mrf + 1, sechalf(src),
656 BRW_MATH_PRECISION_FULL);
657
658 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
659 }
660 }
661
662 void
663 fs_generator::generate_math_g45(fs_inst *inst,
664 struct brw_reg dst,
665 struct brw_reg src)
666 {
667 if (inst->opcode == SHADER_OPCODE_POW ||
668 inst->opcode == SHADER_OPCODE_INT_QUOTIENT ||
669 inst->opcode == SHADER_OPCODE_INT_REMAINDER) {
670 generate_math_gen4(inst, dst, src);
671 return;
672 }
673
674 int op = brw_math_function(inst->opcode);
675
676 assert(inst->mlen >= 1);
677
678 gen4_math(p, dst,
679 op,
680 inst->base_mrf, src,
681 BRW_MATH_PRECISION_FULL);
682 }
683
684 void
685 fs_generator::generate_get_buffer_size(fs_inst *inst,
686 struct brw_reg dst,
687 struct brw_reg src,
688 struct brw_reg surf_index)
689 {
690 assert(devinfo->gen >= 7);
691 assert(surf_index.file == BRW_IMMEDIATE_VALUE);
692
693 uint32_t simd_mode;
694 int rlen = 4;
695
696 switch (inst->exec_size) {
697 case 8:
698 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
699 break;
700 case 16:
701 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
702 break;
703 default:
704 unreachable("Invalid width for texture instruction");
705 }
706
707 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
708 rlen = 8;
709 dst = vec16(dst);
710 }
711
712 brw_SAMPLE(p,
713 retype(dst, BRW_REGISTER_TYPE_UW),
714 inst->base_mrf,
715 src,
716 surf_index.ud,
717 0,
718 GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO,
719 rlen, /* response length */
720 inst->mlen,
721 inst->header_size > 0,
722 simd_mode,
723 BRW_SAMPLER_RETURN_FORMAT_SINT32);
724
725 brw_mark_surface_used(prog_data, surf_index.ud);
726 }
727
728 void
729 fs_generator::generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src,
730 struct brw_reg surface_index,
731 struct brw_reg sampler_index)
732 {
733 int msg_type = -1;
734 uint32_t simd_mode;
735 uint32_t return_format;
736 bool is_combined_send = inst->eot;
737
738 switch (dst.type) {
739 case BRW_REGISTER_TYPE_D:
740 return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;
741 break;
742 case BRW_REGISTER_TYPE_UD:
743 return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
744 break;
745 default:
746 return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
747 break;
748 }
749
750 /* Stomp the resinfo output type to UINT32. On gens 4-5, the output type
751 * is set as part of the message descriptor. On gen4, the PRM seems to
752 * allow UINT32 and FLOAT32 (i965 PRM, Vol. 4 Section 4.8.1.1), but on
753 * later gens UINT32 is required. Once you hit Sandy Bridge, the bit is
754 * gone from the message descriptor entirely and you just get UINT32 all
755 * the time regasrdless. Since we can really only do non-UINT32 on gen4,
756 * just stomp it to UINT32 all the time.
757 */
758 if (inst->opcode == SHADER_OPCODE_TXS)
759 return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;
760
761 switch (inst->exec_size) {
762 case 8:
763 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
764 break;
765 case 16:
766 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
767 break;
768 default:
769 unreachable("Invalid width for texture instruction");
770 }
771
772 if (devinfo->gen >= 5) {
773 switch (inst->opcode) {
774 case SHADER_OPCODE_TEX:
775 if (inst->shadow_compare) {
776 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE;
777 } else {
778 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE;
779 }
780 break;
781 case FS_OPCODE_TXB:
782 if (inst->shadow_compare) {
783 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE;
784 } else {
785 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS;
786 }
787 break;
788 case SHADER_OPCODE_TXL:
789 if (inst->shadow_compare) {
790 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
791 } else {
792 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
793 }
794 break;
795 case SHADER_OPCODE_TXS:
796 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
797 break;
798 case SHADER_OPCODE_TXD:
799 if (inst->shadow_compare) {
800 /* Gen7.5+. Otherwise, lowered by brw_lower_texture_gradients(). */
801 assert(devinfo->gen >= 8 || devinfo->is_haswell);
802 msg_type = HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE;
803 } else {
804 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
805 }
806 break;
807 case SHADER_OPCODE_TXF:
808 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
809 break;
810 case SHADER_OPCODE_TXF_CMS_W:
811 assert(devinfo->gen >= 9);
812 msg_type = GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W;
813 break;
814 case SHADER_OPCODE_TXF_CMS:
815 if (devinfo->gen >= 7)
816 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS;
817 else
818 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
819 break;
820 case SHADER_OPCODE_TXF_UMS:
821 assert(devinfo->gen >= 7);
822 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS;
823 break;
824 case SHADER_OPCODE_TXF_MCS:
825 assert(devinfo->gen >= 7);
826 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS;
827 break;
828 case SHADER_OPCODE_LOD:
829 msg_type = GEN5_SAMPLER_MESSAGE_LOD;
830 break;
831 case SHADER_OPCODE_TG4:
832 if (inst->shadow_compare) {
833 assert(devinfo->gen >= 7);
834 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C;
835 } else {
836 assert(devinfo->gen >= 6);
837 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4;
838 }
839 break;
840 case SHADER_OPCODE_TG4_OFFSET:
841 assert(devinfo->gen >= 7);
842 if (inst->shadow_compare) {
843 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C;
844 } else {
845 msg_type = GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO;
846 }
847 break;
848 case SHADER_OPCODE_SAMPLEINFO:
849 msg_type = GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO;
850 break;
851 default:
852 unreachable("not reached");
853 }
854 } else {
855 switch (inst->opcode) {
856 case SHADER_OPCODE_TEX:
857 /* Note that G45 and older determines shadow compare and dispatch width
858 * from message length for most messages.
859 */
860 if (inst->exec_size == 8) {
861 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
862 if (inst->shadow_compare) {
863 assert(inst->mlen == 6);
864 } else {
865 assert(inst->mlen <= 4);
866 }
867 } else {
868 if (inst->shadow_compare) {
869 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE;
870 assert(inst->mlen == 9);
871 } else {
872 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE;
873 assert(inst->mlen <= 7 && inst->mlen % 2 == 1);
874 }
875 }
876 break;
877 case FS_OPCODE_TXB:
878 if (inst->shadow_compare) {
879 assert(inst->exec_size == 8);
880 assert(inst->mlen == 6);
881 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE;
882 } else {
883 assert(inst->mlen == 9);
884 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
885 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
886 }
887 break;
888 case SHADER_OPCODE_TXL:
889 if (inst->shadow_compare) {
890 assert(inst->exec_size == 8);
891 assert(inst->mlen == 6);
892 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE;
893 } else {
894 assert(inst->mlen == 9);
895 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD;
896 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
897 }
898 break;
899 case SHADER_OPCODE_TXD:
900 /* There is no sample_d_c message; comparisons are done manually */
901 assert(inst->exec_size == 8);
902 assert(inst->mlen == 7 || inst->mlen == 10);
903 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS;
904 break;
905 case SHADER_OPCODE_TXF:
906 assert(inst->mlen <= 9 && inst->mlen % 2 == 1);
907 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
908 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
909 break;
910 case SHADER_OPCODE_TXS:
911 assert(inst->mlen == 3);
912 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_RESINFO;
913 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
914 break;
915 default:
916 unreachable("not reached");
917 }
918 }
919 assert(msg_type != -1);
920
921 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
922 dst = vec16(dst);
923 }
924
925 assert(devinfo->gen < 7 || inst->header_size == 0 ||
926 src.file == BRW_GENERAL_REGISTER_FILE);
927
928 assert(sampler_index.type == BRW_REGISTER_TYPE_UD);
929
930 /* Load the message header if present. If there's a texture offset,
931 * we need to set it up explicitly and load the offset bitfield.
932 * Otherwise, we can use an implied move from g0 to the first message reg.
933 */
934 if (inst->header_size != 0) {
935 if (devinfo->gen < 6 && !inst->offset) {
936 /* Set up an implied move from g0 to the MRF. */
937 src = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
938 } else {
939 struct brw_reg header_reg;
940
941 if (devinfo->gen >= 7) {
942 header_reg = src;
943 } else {
944 assert(inst->base_mrf != -1);
945 header_reg = brw_message_reg(inst->base_mrf);
946 }
947
948 brw_push_insn_state(p);
949 brw_set_default_exec_size(p, BRW_EXECUTE_8);
950 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
951 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
952 /* Explicitly set up the message header by copying g0 to the MRF. */
953 brw_MOV(p, header_reg, brw_vec8_grf(0, 0));
954
955 if (inst->offset) {
956 /* Set the offset bits in DWord 2. */
957 brw_MOV(p, get_element_ud(header_reg, 2),
958 brw_imm_ud(inst->offset));
959 } else if (stage != MESA_SHADER_VERTEX &&
960 stage != MESA_SHADER_FRAGMENT) {
961 /* The vertex and fragment stages have g0.2 set to 0, so
962 * header0.2 is 0 when g0 is copied. Other stages may not, so we
963 * must set it to 0 to avoid setting undesirable bits in the
964 * message.
965 */
966 brw_MOV(p, get_element_ud(header_reg, 2), brw_imm_ud(0));
967 }
968
969 brw_adjust_sampler_state_pointer(p, header_reg, sampler_index);
970 brw_pop_insn_state(p);
971 }
972 }
973
974 uint32_t base_binding_table_index = (inst->opcode == SHADER_OPCODE_TG4 ||
975 inst->opcode == SHADER_OPCODE_TG4_OFFSET)
976 ? prog_data->binding_table.gather_texture_start
977 : prog_data->binding_table.texture_start;
978
979 if (surface_index.file == BRW_IMMEDIATE_VALUE &&
980 sampler_index.file == BRW_IMMEDIATE_VALUE) {
981 uint32_t surface = surface_index.ud;
982 uint32_t sampler = sampler_index.ud;
983
984 brw_SAMPLE(p,
985 retype(dst, BRW_REGISTER_TYPE_UW),
986 inst->base_mrf,
987 src,
988 surface + base_binding_table_index,
989 sampler % 16,
990 msg_type,
991 inst->regs_written,
992 inst->mlen,
993 inst->header_size != 0,
994 simd_mode,
995 return_format);
996
997 brw_mark_surface_used(prog_data, surface + base_binding_table_index);
998 } else {
999 /* Non-const sampler index */
1000
1001 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1002 struct brw_reg surface_reg = vec1(retype(surface_index, BRW_REGISTER_TYPE_UD));
1003 struct brw_reg sampler_reg = vec1(retype(sampler_index, BRW_REGISTER_TYPE_UD));
1004
1005 brw_push_insn_state(p);
1006 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1007 brw_set_default_access_mode(p, BRW_ALIGN_1);
1008
1009 if (memcmp(&surface_reg, &sampler_reg, sizeof(surface_reg)) == 0) {
1010 brw_MUL(p, addr, sampler_reg, brw_imm_uw(0x101));
1011 } else {
1012 brw_SHL(p, addr, sampler_reg, brw_imm_ud(8));
1013 brw_OR(p, addr, addr, surface_reg);
1014 }
1015 if (base_binding_table_index)
1016 brw_ADD(p, addr, addr, brw_imm_ud(base_binding_table_index));
1017 brw_AND(p, addr, addr, brw_imm_ud(0xfff));
1018
1019 brw_pop_insn_state(p);
1020
1021 /* dst = send(offset, a0.0 | <descriptor>) */
1022 brw_inst *insn = brw_send_indirect_message(
1023 p, BRW_SFID_SAMPLER, dst, src, addr);
1024 brw_set_sampler_message(p, insn,
1025 0 /* surface */,
1026 0 /* sampler */,
1027 msg_type,
1028 inst->regs_written,
1029 inst->mlen /* mlen */,
1030 inst->header_size != 0 /* header */,
1031 simd_mode,
1032 return_format);
1033
1034 /* visitor knows more than we do about the surface limit required,
1035 * so has already done marking.
1036 */
1037 }
1038
1039 if (is_combined_send) {
1040 brw_inst_set_eot(p->devinfo, brw_last_inst, true);
1041 brw_inst_set_opcode(p->devinfo, brw_last_inst, BRW_OPCODE_SENDC);
1042 }
1043 }
1044
1045
1046 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
1047 * looking like:
1048 *
1049 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
1050 *
1051 * Ideally, we want to produce:
1052 *
1053 * DDX DDY
1054 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
1055 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
1056 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
1057 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
1058 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
1059 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
1060 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
1061 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
1062 *
1063 * and add another set of two more subspans if in 16-pixel dispatch mode.
1064 *
1065 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
1066 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
1067 * pair. But the ideal approximation may impose a huge performance cost on
1068 * sample_d. On at least Haswell, sample_d instruction does some
1069 * optimizations if the same LOD is used for all pixels in the subspan.
1070 *
1071 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
1072 * appropriate swizzling.
1073 */
1074 void
1075 fs_generator::generate_ddx(enum opcode opcode,
1076 struct brw_reg dst, struct brw_reg src)
1077 {
1078 unsigned vstride, width;
1079
1080 if (opcode == FS_OPCODE_DDX_FINE) {
1081 /* produce accurate derivatives */
1082 vstride = BRW_VERTICAL_STRIDE_2;
1083 width = BRW_WIDTH_2;
1084 } else {
1085 /* replicate the derivative at the top-left pixel to other pixels */
1086 vstride = BRW_VERTICAL_STRIDE_4;
1087 width = BRW_WIDTH_4;
1088 }
1089
1090 struct brw_reg src0 = brw_reg(src.file, src.nr, 1,
1091 src.negate, src.abs,
1092 BRW_REGISTER_TYPE_F,
1093 vstride,
1094 width,
1095 BRW_HORIZONTAL_STRIDE_0,
1096 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
1097 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
1098 src.negate, src.abs,
1099 BRW_REGISTER_TYPE_F,
1100 vstride,
1101 width,
1102 BRW_HORIZONTAL_STRIDE_0,
1103 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
1104 brw_ADD(p, dst, src0, negate(src1));
1105 }
1106
1107 /* The negate_value boolean is used to negate the derivative computation for
1108 * FBOs, since they place the origin at the upper left instead of the lower
1109 * left.
1110 */
1111 void
1112 fs_generator::generate_ddy(enum opcode opcode,
1113 struct brw_reg dst, struct brw_reg src,
1114 bool negate_value)
1115 {
1116 if (opcode == FS_OPCODE_DDY_FINE) {
1117 /* From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
1118 * Region Restrictions):
1119 *
1120 * In Align16 access mode, SIMD16 is not allowed for DW operations
1121 * and SIMD8 is not allowed for DF operations.
1122 *
1123 * In this context, "DW operations" means "operations acting on 32-bit
1124 * values", so it includes operations on floats.
1125 *
1126 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
1127 * (Instruction Compression -> Rules and Restrictions):
1128 *
1129 * A compressed instruction must be in Align1 access mode. Align16
1130 * mode instructions cannot be compressed.
1131 *
1132 * Similar text exists in the g45 PRM.
1133 *
1134 * Empirically, compressed align16 instructions using odd register
1135 * numbers don't appear to work on Sandybridge either.
1136 *
1137 * On these platforms, if we're building a SIMD16 shader, we need to
1138 * manually unroll to a pair of SIMD8 instructions.
1139 */
1140 bool unroll_to_simd8 =
1141 (dispatch_width == 16 &&
1142 (devinfo->gen == 4 || devinfo->gen == 6 ||
1143 (devinfo->gen == 7 && !devinfo->is_haswell)));
1144
1145 /* produce accurate derivatives */
1146 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
1147 src.negate, src.abs,
1148 BRW_REGISTER_TYPE_F,
1149 BRW_VERTICAL_STRIDE_4,
1150 BRW_WIDTH_4,
1151 BRW_HORIZONTAL_STRIDE_1,
1152 BRW_SWIZZLE_XYXY, WRITEMASK_XYZW);
1153 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
1154 src.negate, src.abs,
1155 BRW_REGISTER_TYPE_F,
1156 BRW_VERTICAL_STRIDE_4,
1157 BRW_WIDTH_4,
1158 BRW_HORIZONTAL_STRIDE_1,
1159 BRW_SWIZZLE_ZWZW, WRITEMASK_XYZW);
1160 brw_push_insn_state(p);
1161 brw_set_default_access_mode(p, BRW_ALIGN_16);
1162 if (unroll_to_simd8) {
1163 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1164 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1165 if (negate_value) {
1166 brw_ADD(p, firsthalf(dst), firsthalf(src1), negate(firsthalf(src0)));
1167 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1168 brw_ADD(p, sechalf(dst), sechalf(src1), negate(sechalf(src0)));
1169 } else {
1170 brw_ADD(p, firsthalf(dst), firsthalf(src0), negate(firsthalf(src1)));
1171 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1172 brw_ADD(p, sechalf(dst), sechalf(src0), negate(sechalf(src1)));
1173 }
1174 } else {
1175 if (negate_value)
1176 brw_ADD(p, dst, src1, negate(src0));
1177 else
1178 brw_ADD(p, dst, src0, negate(src1));
1179 }
1180 brw_pop_insn_state(p);
1181 } else {
1182 /* replicate the derivative at the top-left pixel to other pixels */
1183 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
1184 src.negate, src.abs,
1185 BRW_REGISTER_TYPE_F,
1186 BRW_VERTICAL_STRIDE_4,
1187 BRW_WIDTH_4,
1188 BRW_HORIZONTAL_STRIDE_0,
1189 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
1190 struct brw_reg src1 = brw_reg(src.file, src.nr, 2,
1191 src.negate, src.abs,
1192 BRW_REGISTER_TYPE_F,
1193 BRW_VERTICAL_STRIDE_4,
1194 BRW_WIDTH_4,
1195 BRW_HORIZONTAL_STRIDE_0,
1196 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
1197 if (negate_value)
1198 brw_ADD(p, dst, src1, negate(src0));
1199 else
1200 brw_ADD(p, dst, src0, negate(src1));
1201 }
1202 }
1203
1204 void
1205 fs_generator::generate_discard_jump(fs_inst *inst)
1206 {
1207 assert(devinfo->gen >= 6);
1208
1209 /* This HALT will be patched up at FB write time to point UIP at the end of
1210 * the program, and at brw_uip_jip() JIP will be set to the end of the
1211 * current block (or the program).
1212 */
1213 this->discard_halt_patches.push_tail(new(mem_ctx) ip_record(p->nr_insn));
1214
1215 brw_push_insn_state(p);
1216 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1217 gen6_HALT(p);
1218 brw_pop_insn_state(p);
1219 }
1220
1221 void
1222 fs_generator::generate_scratch_write(fs_inst *inst, struct brw_reg src)
1223 {
1224 assert(inst->mlen != 0);
1225
1226 brw_MOV(p,
1227 brw_uvec_mrf(inst->exec_size, (inst->base_mrf + 1), 0),
1228 retype(src, BRW_REGISTER_TYPE_UD));
1229 brw_oword_block_write_scratch(p, brw_message_reg(inst->base_mrf),
1230 inst->exec_size / 8, inst->offset);
1231 }
1232
1233 void
1234 fs_generator::generate_scratch_read(fs_inst *inst, struct brw_reg dst)
1235 {
1236 assert(inst->mlen != 0);
1237
1238 brw_oword_block_read_scratch(p, dst, brw_message_reg(inst->base_mrf),
1239 inst->exec_size / 8, inst->offset);
1240 }
1241
1242 void
1243 fs_generator::generate_scratch_read_gen7(fs_inst *inst, struct brw_reg dst)
1244 {
1245 gen7_block_read_scratch(p, dst, inst->exec_size / 8, inst->offset);
1246 }
1247
1248 void
1249 fs_generator::generate_uniform_pull_constant_load(fs_inst *inst,
1250 struct brw_reg dst,
1251 struct brw_reg index,
1252 struct brw_reg offset)
1253 {
1254 assert(inst->mlen != 0);
1255
1256 assert(index.file == BRW_IMMEDIATE_VALUE &&
1257 index.type == BRW_REGISTER_TYPE_UD);
1258 uint32_t surf_index = index.ud;
1259
1260 assert(offset.file == BRW_IMMEDIATE_VALUE &&
1261 offset.type == BRW_REGISTER_TYPE_UD);
1262 uint32_t read_offset = offset.ud;
1263
1264 brw_oword_block_read(p, dst, brw_message_reg(inst->base_mrf),
1265 read_offset, surf_index);
1266 }
1267
1268 void
1269 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst *inst,
1270 struct brw_reg dst,
1271 struct brw_reg index,
1272 struct brw_reg offset)
1273 {
1274 assert(index.type == BRW_REGISTER_TYPE_UD);
1275
1276 assert(offset.file == BRW_GENERAL_REGISTER_FILE);
1277 /* Reference just the dword we need, to avoid angering validate_reg(). */
1278 offset = brw_vec1_grf(offset.nr, 0);
1279
1280 /* We use the SIMD4x2 mode because we want to end up with 4 components in
1281 * the destination loaded consecutively from the same offset (which appears
1282 * in the first component, and the rest are ignored).
1283 */
1284 dst.width = BRW_WIDTH_4;
1285
1286 struct brw_reg src = offset;
1287 bool header_present = false;
1288
1289 if (devinfo->gen >= 9) {
1290 /* Skylake requires a message header in order to use SIMD4x2 mode. */
1291 src = retype(brw_vec4_grf(offset.nr, 0), BRW_REGISTER_TYPE_UD);
1292 header_present = true;
1293
1294 brw_push_insn_state(p);
1295 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1296 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1297 brw_MOV(p, vec8(src), retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
1298 brw_set_default_access_mode(p, BRW_ALIGN_1);
1299
1300 brw_MOV(p, get_element_ud(src, 2),
1301 brw_imm_ud(GEN9_SAMPLER_SIMD_MODE_EXTENSION_SIMD4X2));
1302 brw_pop_insn_state(p);
1303 }
1304
1305 if (index.file == BRW_IMMEDIATE_VALUE) {
1306
1307 uint32_t surf_index = index.ud;
1308
1309 brw_push_insn_state(p);
1310 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1311 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1312 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1313 brw_inst_set_exec_size(devinfo, send, BRW_EXECUTE_4);
1314 brw_pop_insn_state(p);
1315
1316 brw_set_dest(p, send, dst);
1317 brw_set_src0(p, send, src);
1318 brw_set_sampler_message(p, send,
1319 surf_index,
1320 0, /* LD message ignores sampler unit */
1321 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1322 1, /* rlen */
1323 inst->mlen,
1324 header_present,
1325 BRW_SAMPLER_SIMD_MODE_SIMD4X2,
1326 0);
1327 } else {
1328
1329 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1330
1331 brw_push_insn_state(p);
1332 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1333 brw_set_default_access_mode(p, BRW_ALIGN_1);
1334
1335 /* a0.0 = surf_index & 0xff */
1336 brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
1337 brw_inst_set_exec_size(p->devinfo, insn_and, BRW_EXECUTE_1);
1338 brw_set_dest(p, insn_and, addr);
1339 brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));
1340 brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
1341
1342 /* dst = send(payload, a0.0 | <descriptor>) */
1343 brw_inst *insn = brw_send_indirect_message(
1344 p, BRW_SFID_SAMPLER, dst, src, addr);
1345 brw_set_sampler_message(p, insn,
1346 0,
1347 0, /* LD message ignores sampler unit */
1348 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1349 1, /* rlen */
1350 inst->mlen,
1351 header_present,
1352 BRW_SAMPLER_SIMD_MODE_SIMD4X2,
1353 0);
1354
1355 brw_pop_insn_state(p);
1356 }
1357 }
1358
1359 void
1360 fs_generator::generate_varying_pull_constant_load(fs_inst *inst,
1361 struct brw_reg dst,
1362 struct brw_reg index,
1363 struct brw_reg offset)
1364 {
1365 assert(devinfo->gen < 7); /* Should use the gen7 variant. */
1366 assert(inst->header_size != 0);
1367 assert(inst->mlen);
1368
1369 assert(index.file == BRW_IMMEDIATE_VALUE &&
1370 index.type == BRW_REGISTER_TYPE_UD);
1371 uint32_t surf_index = index.ud;
1372
1373 uint32_t simd_mode, rlen, msg_type;
1374 if (dispatch_width == 16) {
1375 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1376 rlen = 8;
1377 } else {
1378 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1379 rlen = 4;
1380 }
1381
1382 if (devinfo->gen >= 5)
1383 msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
1384 else {
1385 /* We always use the SIMD16 message so that we only have to load U, and
1386 * not V or R.
1387 */
1388 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;
1389 assert(inst->mlen == 3);
1390 assert(inst->regs_written == 8);
1391 rlen = 8;
1392 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1393 }
1394
1395 struct brw_reg offset_mrf = retype(brw_message_reg(inst->base_mrf + 1),
1396 BRW_REGISTER_TYPE_D);
1397 brw_MOV(p, offset_mrf, offset);
1398
1399 struct brw_reg header = brw_vec8_grf(0, 0);
1400 gen6_resolve_implied_move(p, &header, inst->base_mrf);
1401
1402 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1403 brw_inst_set_qtr_control(p->devinfo, send, BRW_COMPRESSION_NONE);
1404 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));
1405 brw_set_src0(p, send, header);
1406 if (devinfo->gen < 6)
1407 brw_inst_set_base_mrf(p->devinfo, send, inst->base_mrf);
1408
1409 /* Our surface is set up as floats, regardless of what actual data is
1410 * stored in it.
1411 */
1412 uint32_t return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
1413 brw_set_sampler_message(p, send,
1414 surf_index,
1415 0, /* sampler (unused) */
1416 msg_type,
1417 rlen,
1418 inst->mlen,
1419 inst->header_size != 0,
1420 simd_mode,
1421 return_format);
1422 }
1423
1424 void
1425 fs_generator::generate_varying_pull_constant_load_gen7(fs_inst *inst,
1426 struct brw_reg dst,
1427 struct brw_reg index,
1428 struct brw_reg offset)
1429 {
1430 assert(devinfo->gen >= 7);
1431 /* Varying-offset pull constant loads are treated as a normal expression on
1432 * gen7, so the fact that it's a send message is hidden at the IR level.
1433 */
1434 assert(inst->header_size == 0);
1435 assert(!inst->mlen);
1436 assert(index.type == BRW_REGISTER_TYPE_UD);
1437
1438 uint32_t simd_mode, rlen, mlen;
1439 if (dispatch_width == 16) {
1440 mlen = 2;
1441 rlen = 8;
1442 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
1443 } else {
1444 mlen = 1;
1445 rlen = 4;
1446 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
1447 }
1448
1449 if (index.file == BRW_IMMEDIATE_VALUE) {
1450
1451 uint32_t surf_index = index.ud;
1452
1453 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
1454 brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));
1455 brw_set_src0(p, send, offset);
1456 brw_set_sampler_message(p, send,
1457 surf_index,
1458 0, /* LD message ignores sampler unit */
1459 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1460 rlen,
1461 mlen,
1462 false, /* no header */
1463 simd_mode,
1464 0);
1465
1466 } else {
1467
1468 struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));
1469
1470 brw_push_insn_state(p);
1471 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1472 brw_set_default_access_mode(p, BRW_ALIGN_1);
1473
1474 /* a0.0 = surf_index & 0xff */
1475 brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);
1476 brw_inst_set_exec_size(p->devinfo, insn_and, BRW_EXECUTE_1);
1477 brw_set_dest(p, insn_and, addr);
1478 brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));
1479 brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));
1480
1481 brw_pop_insn_state(p);
1482
1483 /* dst = send(offset, a0.0 | <descriptor>) */
1484 brw_inst *insn = brw_send_indirect_message(
1485 p, BRW_SFID_SAMPLER, retype(dst, BRW_REGISTER_TYPE_UW),
1486 offset, addr);
1487 brw_set_sampler_message(p, insn,
1488 0 /* surface */,
1489 0 /* sampler */,
1490 GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
1491 rlen /* rlen */,
1492 mlen /* mlen */,
1493 false /* header */,
1494 simd_mode,
1495 0);
1496 }
1497 }
1498
1499 /**
1500 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
1501 * into the flags register (f0.0).
1502 *
1503 * Used only on Gen6 and above.
1504 */
1505 void
1506 fs_generator::generate_mov_dispatch_to_flags(fs_inst *inst)
1507 {
1508 struct brw_reg flags = brw_flag_reg(0, inst->flag_subreg);
1509 struct brw_reg dispatch_mask;
1510
1511 if (devinfo->gen >= 6)
1512 dispatch_mask = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1513 else
1514 dispatch_mask = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
1515
1516 brw_push_insn_state(p);
1517 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1518 brw_MOV(p, flags, dispatch_mask);
1519 brw_pop_insn_state(p);
1520 }
1521
1522 void
1523 fs_generator::generate_pixel_interpolator_query(fs_inst *inst,
1524 struct brw_reg dst,
1525 struct brw_reg src,
1526 struct brw_reg msg_data,
1527 unsigned msg_type)
1528 {
1529 assert(msg_data.type == BRW_REGISTER_TYPE_UD);
1530
1531 brw_pixel_interpolator_query(p,
1532 retype(dst, BRW_REGISTER_TYPE_UW),
1533 src,
1534 inst->pi_noperspective,
1535 msg_type,
1536 msg_data,
1537 inst->mlen,
1538 inst->regs_written);
1539 }
1540
1541
1542 /**
1543 * Sets the first word of a vgrf for gen7+ simd4x2 uniform pull constant
1544 * sampler LD messages.
1545 *
1546 * We don't want to bake it into the send message's code generation because
1547 * that means we don't get a chance to schedule the instructions.
1548 */
1549 void
1550 fs_generator::generate_set_simd4x2_offset(fs_inst *inst,
1551 struct brw_reg dst,
1552 struct brw_reg value)
1553 {
1554 assert(value.file == BRW_IMMEDIATE_VALUE);
1555
1556 brw_push_insn_state(p);
1557 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1558 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1559 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1560 brw_MOV(p, retype(brw_vec1_reg(dst.file, dst.nr, 0), value.type), value);
1561 brw_pop_insn_state(p);
1562 }
1563
1564 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1565 * the ADD instruction.
1566 */
1567 void
1568 fs_generator::generate_set_sample_id(fs_inst *inst,
1569 struct brw_reg dst,
1570 struct brw_reg src0,
1571 struct brw_reg src1)
1572 {
1573 assert(dst.type == BRW_REGISTER_TYPE_D ||
1574 dst.type == BRW_REGISTER_TYPE_UD);
1575 assert(src0.type == BRW_REGISTER_TYPE_D ||
1576 src0.type == BRW_REGISTER_TYPE_UD);
1577
1578 struct brw_reg reg = stride(src1, 1, 4, 0);
1579 if (devinfo->gen >= 8 || dispatch_width == 8) {
1580 brw_ADD(p, dst, src0, reg);
1581 } else if (dispatch_width == 16) {
1582 brw_push_insn_state(p);
1583 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1584 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1585 brw_ADD(p, firsthalf(dst), firsthalf(src0), reg);
1586 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1587 brw_ADD(p, sechalf(dst), sechalf(src0), suboffset(reg, 2));
1588 brw_pop_insn_state(p);
1589 }
1590 }
1591
1592 void
1593 fs_generator::generate_pack_half_2x16_split(fs_inst *inst,
1594 struct brw_reg dst,
1595 struct brw_reg x,
1596 struct brw_reg y)
1597 {
1598 assert(devinfo->gen >= 7);
1599 assert(dst.type == BRW_REGISTER_TYPE_UD);
1600 assert(x.type == BRW_REGISTER_TYPE_F);
1601 assert(y.type == BRW_REGISTER_TYPE_F);
1602
1603 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1604 *
1605 * Because this instruction does not have a 16-bit floating-point type,
1606 * the destination data type must be Word (W).
1607 *
1608 * The destination must be DWord-aligned and specify a horizontal stride
1609 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1610 * each destination channel and the upper word is not modified.
1611 */
1612 struct brw_reg dst_w = spread(retype(dst, BRW_REGISTER_TYPE_W), 2);
1613
1614 /* Give each 32-bit channel of dst the form below, where "." means
1615 * unchanged.
1616 * 0x....hhhh
1617 */
1618 brw_F32TO16(p, dst_w, y);
1619
1620 /* Now the form:
1621 * 0xhhhh0000
1622 */
1623 brw_SHL(p, dst, dst, brw_imm_ud(16u));
1624
1625 /* And, finally the form of packHalf2x16's output:
1626 * 0xhhhhllll
1627 */
1628 brw_F32TO16(p, dst_w, x);
1629 }
1630
1631 void
1632 fs_generator::generate_unpack_half_2x16_split(fs_inst *inst,
1633 struct brw_reg dst,
1634 struct brw_reg src)
1635 {
1636 assert(devinfo->gen >= 7);
1637 assert(dst.type == BRW_REGISTER_TYPE_F);
1638 assert(src.type == BRW_REGISTER_TYPE_UD);
1639
1640 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1641 *
1642 * Because this instruction does not have a 16-bit floating-point type,
1643 * the source data type must be Word (W). The destination type must be
1644 * F (Float).
1645 */
1646 struct brw_reg src_w = spread(retype(src, BRW_REGISTER_TYPE_W), 2);
1647
1648 /* Each channel of src has the form of unpackHalf2x16's input: 0xhhhhllll.
1649 * For the Y case, we wish to access only the upper word; therefore
1650 * a 16-bit subregister offset is needed.
1651 */
1652 assert(inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X ||
1653 inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y);
1654 if (inst->opcode == FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y)
1655 src_w.subnr += 2;
1656
1657 brw_F16TO32(p, dst, src_w);
1658 }
1659
1660 void
1661 fs_generator::generate_shader_time_add(fs_inst *inst,
1662 struct brw_reg payload,
1663 struct brw_reg offset,
1664 struct brw_reg value)
1665 {
1666 assert(devinfo->gen >= 7);
1667 brw_push_insn_state(p);
1668 brw_set_default_mask_control(p, true);
1669
1670 assert(payload.file == BRW_GENERAL_REGISTER_FILE);
1671 struct brw_reg payload_offset = retype(brw_vec1_grf(payload.nr, 0),
1672 offset.type);
1673 struct brw_reg payload_value = retype(brw_vec1_grf(payload.nr + 1, 0),
1674 value.type);
1675
1676 assert(offset.file == BRW_IMMEDIATE_VALUE);
1677 if (value.file == BRW_GENERAL_REGISTER_FILE) {
1678 value.width = BRW_WIDTH_1;
1679 value.hstride = BRW_HORIZONTAL_STRIDE_0;
1680 value.vstride = BRW_VERTICAL_STRIDE_0;
1681 } else {
1682 assert(value.file == BRW_IMMEDIATE_VALUE);
1683 }
1684
1685 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1686 * case, and we don't really care about squeezing every bit of performance
1687 * out of this path, so we just emit the MOVs from here.
1688 */
1689 brw_MOV(p, payload_offset, offset);
1690 brw_MOV(p, payload_value, value);
1691 brw_shader_time_add(p, payload,
1692 prog_data->binding_table.shader_time_start);
1693 brw_pop_insn_state(p);
1694
1695 brw_mark_surface_used(prog_data,
1696 prog_data->binding_table.shader_time_start);
1697 }
1698
1699 void
1700 fs_generator::enable_debug(const char *shader_name)
1701 {
1702 debug_flag = true;
1703 this->shader_name = shader_name;
1704 }
1705
1706 int
1707 fs_generator::generate_code(const cfg_t *cfg, int dispatch_width)
1708 {
1709 /* align to 64 byte boundary. */
1710 while (p->next_insn_offset % 64)
1711 brw_NOP(p);
1712
1713 this->dispatch_width = dispatch_width;
1714 if (dispatch_width == 16)
1715 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1716
1717 int start_offset = p->next_insn_offset;
1718 int spill_count = 0, fill_count = 0;
1719 int loop_count = 0;
1720
1721 struct annotation_info annotation;
1722 memset(&annotation, 0, sizeof(annotation));
1723
1724 foreach_block_and_inst (block, fs_inst, inst, cfg) {
1725 struct brw_reg src[3], dst;
1726 unsigned int last_insn_offset = p->next_insn_offset;
1727 bool multiple_instructions_emitted = false;
1728
1729 /* From the Broadwell PRM, Volume 7, "3D-Media-GPGPU", in the
1730 * "Register Region Restrictions" section: for BDW, SKL:
1731 *
1732 * "A POW/FDIV operation must not be followed by an instruction
1733 * that requires two destination registers."
1734 *
1735 * The documentation is often lacking annotations for Atom parts,
1736 * and empirically this affects CHV as well.
1737 */
1738 if (devinfo->gen >= 8 &&
1739 p->nr_insn > 1 &&
1740 brw_inst_opcode(devinfo, brw_last_inst) == BRW_OPCODE_MATH &&
1741 brw_inst_math_function(devinfo, brw_last_inst) == BRW_MATH_FUNCTION_POW &&
1742 inst->dst.component_size(inst->exec_size) > REG_SIZE) {
1743 brw_NOP(p);
1744 last_insn_offset = p->next_insn_offset;
1745 }
1746
1747 if (unlikely(debug_flag))
1748 annotate(p->devinfo, &annotation, cfg, inst, p->next_insn_offset);
1749
1750 for (unsigned int i = 0; i < inst->sources; i++) {
1751 src[i] = brw_reg_from_fs_reg(inst, &inst->src[i], devinfo->gen);
1752
1753 /* The accumulator result appears to get used for the
1754 * conditional modifier generation. When negating a UD
1755 * value, there is a 33rd bit generated for the sign in the
1756 * accumulator value, so now you can't check, for example,
1757 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1758 */
1759 assert(!inst->conditional_mod ||
1760 inst->src[i].type != BRW_REGISTER_TYPE_UD ||
1761 !inst->src[i].negate);
1762 }
1763 dst = brw_reg_from_fs_reg(inst, &inst->dst, devinfo->gen);
1764
1765 brw_set_default_predicate_control(p, inst->predicate);
1766 brw_set_default_predicate_inverse(p, inst->predicate_inverse);
1767 brw_set_default_flag_reg(p, 0, inst->flag_subreg);
1768 brw_set_default_saturate(p, inst->saturate);
1769 brw_set_default_mask_control(p, inst->force_writemask_all);
1770 brw_set_default_acc_write_control(p, inst->writes_accumulator);
1771 brw_set_default_exec_size(p, cvt(inst->exec_size) - 1);
1772
1773 assert(inst->base_mrf + inst->mlen <= BRW_MAX_MRF(devinfo->gen));
1774 assert(inst->mlen <= BRW_MAX_MSG_LENGTH);
1775
1776 switch (inst->exec_size) {
1777 case 1:
1778 case 2:
1779 case 4:
1780 assert(inst->force_writemask_all);
1781 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1782 break;
1783 case 8:
1784 if (inst->force_sechalf) {
1785 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1786 } else {
1787 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1788 }
1789 break;
1790 case 16:
1791 case 32:
1792 /* If the instruction writes to more than one register, it needs to
1793 * be a "compressed" instruction on Gen <= 5.
1794 */
1795 if (inst->dst.component_size(inst->exec_size) > REG_SIZE)
1796 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1797 else
1798 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1799 break;
1800 default:
1801 unreachable("Invalid instruction width");
1802 }
1803
1804 switch (inst->opcode) {
1805 case BRW_OPCODE_MOV:
1806 brw_MOV(p, dst, src[0]);
1807 break;
1808 case BRW_OPCODE_ADD:
1809 brw_ADD(p, dst, src[0], src[1]);
1810 break;
1811 case BRW_OPCODE_MUL:
1812 brw_MUL(p, dst, src[0], src[1]);
1813 break;
1814 case BRW_OPCODE_AVG:
1815 brw_AVG(p, dst, src[0], src[1]);
1816 break;
1817 case BRW_OPCODE_MACH:
1818 brw_MACH(p, dst, src[0], src[1]);
1819 break;
1820
1821 case BRW_OPCODE_LINE:
1822 brw_LINE(p, dst, src[0], src[1]);
1823 break;
1824
1825 case BRW_OPCODE_MAD:
1826 assert(devinfo->gen >= 6);
1827 brw_set_default_access_mode(p, BRW_ALIGN_16);
1828 if (dispatch_width == 16 && !devinfo->supports_simd16_3src) {
1829 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1830 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1831 brw_inst *f = brw_MAD(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1832 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1833 brw_inst *s = brw_MAD(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1834 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1835
1836 if (inst->conditional_mod) {
1837 brw_inst_set_cond_modifier(p->devinfo, f, inst->conditional_mod);
1838 brw_inst_set_cond_modifier(p->devinfo, s, inst->conditional_mod);
1839 multiple_instructions_emitted = true;
1840 }
1841 } else {
1842 brw_MAD(p, dst, src[0], src[1], src[2]);
1843 }
1844 brw_set_default_access_mode(p, BRW_ALIGN_1);
1845 break;
1846
1847 case BRW_OPCODE_LRP:
1848 assert(devinfo->gen >= 6);
1849 brw_set_default_access_mode(p, BRW_ALIGN_16);
1850 if (dispatch_width == 16 && !devinfo->supports_simd16_3src) {
1851 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1852 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1853 brw_inst *f = brw_LRP(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1854 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1855 brw_inst *s = brw_LRP(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1856 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1857
1858 if (inst->conditional_mod) {
1859 brw_inst_set_cond_modifier(p->devinfo, f, inst->conditional_mod);
1860 brw_inst_set_cond_modifier(p->devinfo, s, inst->conditional_mod);
1861 multiple_instructions_emitted = true;
1862 }
1863 } else {
1864 brw_LRP(p, dst, src[0], src[1], src[2]);
1865 }
1866 brw_set_default_access_mode(p, BRW_ALIGN_1);
1867 break;
1868
1869 case BRW_OPCODE_FRC:
1870 brw_FRC(p, dst, src[0]);
1871 break;
1872 case BRW_OPCODE_RNDD:
1873 brw_RNDD(p, dst, src[0]);
1874 break;
1875 case BRW_OPCODE_RNDE:
1876 brw_RNDE(p, dst, src[0]);
1877 break;
1878 case BRW_OPCODE_RNDZ:
1879 brw_RNDZ(p, dst, src[0]);
1880 break;
1881
1882 case BRW_OPCODE_AND:
1883 brw_AND(p, dst, src[0], src[1]);
1884 break;
1885 case BRW_OPCODE_OR:
1886 brw_OR(p, dst, src[0], src[1]);
1887 break;
1888 case BRW_OPCODE_XOR:
1889 brw_XOR(p, dst, src[0], src[1]);
1890 break;
1891 case BRW_OPCODE_NOT:
1892 brw_NOT(p, dst, src[0]);
1893 break;
1894 case BRW_OPCODE_ASR:
1895 brw_ASR(p, dst, src[0], src[1]);
1896 break;
1897 case BRW_OPCODE_SHR:
1898 brw_SHR(p, dst, src[0], src[1]);
1899 break;
1900 case BRW_OPCODE_SHL:
1901 brw_SHL(p, dst, src[0], src[1]);
1902 break;
1903 case BRW_OPCODE_F32TO16:
1904 assert(devinfo->gen >= 7);
1905 brw_F32TO16(p, dst, src[0]);
1906 break;
1907 case BRW_OPCODE_F16TO32:
1908 assert(devinfo->gen >= 7);
1909 brw_F16TO32(p, dst, src[0]);
1910 break;
1911 case BRW_OPCODE_CMP:
1912 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says
1913 * that when the destination is a GRF that the dependency-clear bit on
1914 * the flag register is cleared early.
1915 *
1916 * Suggested workarounds are to disable coissuing CMP instructions
1917 * or to split CMP(16) instructions into two CMP(8) instructions.
1918 *
1919 * We choose to split into CMP(8) instructions since disabling
1920 * coissuing would affect CMP instructions not otherwise affected by
1921 * the errata.
1922 */
1923 if (dispatch_width == 16 && devinfo->gen == 7 && !devinfo->is_haswell) {
1924 if (dst.file == BRW_GENERAL_REGISTER_FILE) {
1925 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1926 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1927 brw_CMP(p, firsthalf(dst), inst->conditional_mod,
1928 firsthalf(src[0]), firsthalf(src[1]));
1929 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1930 brw_CMP(p, sechalf(dst), inst->conditional_mod,
1931 sechalf(src[0]), sechalf(src[1]));
1932 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1933
1934 multiple_instructions_emitted = true;
1935 } else if (dst.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1936 /* For unknown reasons, the aforementioned workaround is not
1937 * sufficient. Overriding the type when the destination is the
1938 * null register is necessary but not sufficient by itself.
1939 */
1940 assert(dst.nr == BRW_ARF_NULL);
1941 dst.type = BRW_REGISTER_TYPE_D;
1942 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
1943 } else {
1944 unreachable("not reached");
1945 }
1946 } else {
1947 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
1948 }
1949 break;
1950 case BRW_OPCODE_SEL:
1951 brw_SEL(p, dst, src[0], src[1]);
1952 break;
1953 case BRW_OPCODE_BFREV:
1954 assert(devinfo->gen >= 7);
1955 /* BFREV only supports UD type for src and dst. */
1956 brw_BFREV(p, retype(dst, BRW_REGISTER_TYPE_UD),
1957 retype(src[0], BRW_REGISTER_TYPE_UD));
1958 break;
1959 case BRW_OPCODE_FBH:
1960 assert(devinfo->gen >= 7);
1961 /* FBH only supports UD type for dst. */
1962 brw_FBH(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1963 break;
1964 case BRW_OPCODE_FBL:
1965 assert(devinfo->gen >= 7);
1966 /* FBL only supports UD type for dst. */
1967 brw_FBL(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1968 break;
1969 case BRW_OPCODE_CBIT:
1970 assert(devinfo->gen >= 7);
1971 /* CBIT only supports UD type for dst. */
1972 brw_CBIT(p, retype(dst, BRW_REGISTER_TYPE_UD), src[0]);
1973 break;
1974 case BRW_OPCODE_ADDC:
1975 assert(devinfo->gen >= 7);
1976 brw_ADDC(p, dst, src[0], src[1]);
1977 break;
1978 case BRW_OPCODE_SUBB:
1979 assert(devinfo->gen >= 7);
1980 brw_SUBB(p, dst, src[0], src[1]);
1981 break;
1982 case BRW_OPCODE_MAC:
1983 brw_MAC(p, dst, src[0], src[1]);
1984 break;
1985
1986 case BRW_OPCODE_BFE:
1987 assert(devinfo->gen >= 7);
1988 brw_set_default_access_mode(p, BRW_ALIGN_16);
1989 if (dispatch_width == 16 && !devinfo->supports_simd16_3src) {
1990 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1991 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1992 brw_BFE(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
1993 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
1994 brw_BFE(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
1995 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1996 } else {
1997 brw_BFE(p, dst, src[0], src[1], src[2]);
1998 }
1999 brw_set_default_access_mode(p, BRW_ALIGN_1);
2000 break;
2001
2002 case BRW_OPCODE_BFI1:
2003 assert(devinfo->gen >= 7);
2004 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
2005 * should
2006 *
2007 * "Force BFI instructions to be executed always in SIMD8."
2008 */
2009 if (dispatch_width == 16 && devinfo->is_haswell) {
2010 brw_set_default_exec_size(p, BRW_EXECUTE_8);
2011 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
2012 brw_BFI1(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]));
2013 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
2014 brw_BFI1(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]));
2015 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
2016 } else {
2017 brw_BFI1(p, dst, src[0], src[1]);
2018 }
2019 break;
2020 case BRW_OPCODE_BFI2:
2021 assert(devinfo->gen >= 7);
2022 brw_set_default_access_mode(p, BRW_ALIGN_16);
2023 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
2024 * should
2025 *
2026 * "Force BFI instructions to be executed always in SIMD8."
2027 *
2028 * Otherwise we would be able to emit compressed instructions like we
2029 * do for the other three-source instructions.
2030 */
2031 if (dispatch_width == 16 &&
2032 (devinfo->is_haswell || !devinfo->supports_simd16_3src)) {
2033 brw_set_default_exec_size(p, BRW_EXECUTE_8);
2034 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
2035 brw_BFI2(p, firsthalf(dst), firsthalf(src[0]), firsthalf(src[1]), firsthalf(src[2]));
2036 brw_set_default_compression_control(p, BRW_COMPRESSION_2NDHALF);
2037 brw_BFI2(p, sechalf(dst), sechalf(src[0]), sechalf(src[1]), sechalf(src[2]));
2038 brw_set_default_compression_control(p, BRW_COMPRESSION_COMPRESSED);
2039 } else {
2040 brw_BFI2(p, dst, src[0], src[1], src[2]);
2041 }
2042 brw_set_default_access_mode(p, BRW_ALIGN_1);
2043 break;
2044
2045 case BRW_OPCODE_IF:
2046 if (inst->src[0].file != BAD_FILE) {
2047 /* The instruction has an embedded compare (only allowed on gen6) */
2048 assert(devinfo->gen == 6);
2049 gen6_IF(p, inst->conditional_mod, src[0], src[1]);
2050 } else {
2051 brw_IF(p, dispatch_width == 16 ? BRW_EXECUTE_16 : BRW_EXECUTE_8);
2052 }
2053 break;
2054
2055 case BRW_OPCODE_ELSE:
2056 brw_ELSE(p);
2057 break;
2058 case BRW_OPCODE_ENDIF:
2059 brw_ENDIF(p);
2060 break;
2061
2062 case BRW_OPCODE_DO:
2063 brw_DO(p, dispatch_width == 16 ? BRW_EXECUTE_16 : BRW_EXECUTE_8);
2064 break;
2065
2066 case BRW_OPCODE_BREAK:
2067 brw_BREAK(p);
2068 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2069 break;
2070 case BRW_OPCODE_CONTINUE:
2071 brw_CONT(p);
2072 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2073 break;
2074
2075 case BRW_OPCODE_WHILE:
2076 brw_WHILE(p);
2077 loop_count++;
2078 break;
2079
2080 case SHADER_OPCODE_RCP:
2081 case SHADER_OPCODE_RSQ:
2082 case SHADER_OPCODE_SQRT:
2083 case SHADER_OPCODE_EXP2:
2084 case SHADER_OPCODE_LOG2:
2085 case SHADER_OPCODE_SIN:
2086 case SHADER_OPCODE_COS:
2087 assert(devinfo->gen < 6 || inst->mlen == 0);
2088 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
2089 if (devinfo->gen >= 7) {
2090 gen6_math(p, dst, brw_math_function(inst->opcode), src[0],
2091 brw_null_reg());
2092 } else if (devinfo->gen == 6) {
2093 generate_math_gen6(inst, dst, src[0], brw_null_reg());
2094 } else if (devinfo->gen == 5 || devinfo->is_g4x) {
2095 generate_math_g45(inst, dst, src[0]);
2096 } else {
2097 generate_math_gen4(inst, dst, src[0]);
2098 }
2099 break;
2100 case SHADER_OPCODE_INT_QUOTIENT:
2101 case SHADER_OPCODE_INT_REMAINDER:
2102 case SHADER_OPCODE_POW:
2103 assert(devinfo->gen < 6 || inst->mlen == 0);
2104 assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);
2105 if (devinfo->gen >= 7 && inst->opcode == SHADER_OPCODE_POW) {
2106 gen6_math(p, dst, brw_math_function(inst->opcode), src[0], src[1]);
2107 } else if (devinfo->gen >= 6) {
2108 generate_math_gen6(inst, dst, src[0], src[1]);
2109 } else {
2110 generate_math_gen4(inst, dst, src[0]);
2111 }
2112 break;
2113 case FS_OPCODE_CINTERP:
2114 brw_MOV(p, dst, src[0]);
2115 break;
2116 case FS_OPCODE_LINTERP:
2117 generate_linterp(inst, dst, src);
2118 break;
2119 case FS_OPCODE_PIXEL_X:
2120 assert(src[0].type == BRW_REGISTER_TYPE_UW);
2121 src[0].subnr = 0 * type_sz(src[0].type);
2122 brw_MOV(p, dst, stride(src[0], 8, 4, 1));
2123 break;
2124 case FS_OPCODE_PIXEL_Y:
2125 assert(src[0].type == BRW_REGISTER_TYPE_UW);
2126 src[0].subnr = 4 * type_sz(src[0].type);
2127 brw_MOV(p, dst, stride(src[0], 8, 4, 1));
2128 break;
2129 case FS_OPCODE_GET_BUFFER_SIZE:
2130 generate_get_buffer_size(inst, dst, src[0], src[1]);
2131 break;
2132 case SHADER_OPCODE_TEX:
2133 case FS_OPCODE_TXB:
2134 case SHADER_OPCODE_TXD:
2135 case SHADER_OPCODE_TXF:
2136 case SHADER_OPCODE_TXF_CMS:
2137 case SHADER_OPCODE_TXF_CMS_W:
2138 case SHADER_OPCODE_TXF_UMS:
2139 case SHADER_OPCODE_TXF_MCS:
2140 case SHADER_OPCODE_TXL:
2141 case SHADER_OPCODE_TXS:
2142 case SHADER_OPCODE_LOD:
2143 case SHADER_OPCODE_TG4:
2144 case SHADER_OPCODE_TG4_OFFSET:
2145 case SHADER_OPCODE_SAMPLEINFO:
2146 generate_tex(inst, dst, src[0], src[1], src[2]);
2147 break;
2148 case FS_OPCODE_DDX_COARSE:
2149 case FS_OPCODE_DDX_FINE:
2150 generate_ddx(inst->opcode, dst, src[0]);
2151 break;
2152 case FS_OPCODE_DDY_COARSE:
2153 case FS_OPCODE_DDY_FINE:
2154 assert(src[1].file == BRW_IMMEDIATE_VALUE);
2155 generate_ddy(inst->opcode, dst, src[0], src[1].ud);
2156 break;
2157
2158 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
2159 generate_scratch_write(inst, src[0]);
2160 spill_count++;
2161 break;
2162
2163 case SHADER_OPCODE_GEN4_SCRATCH_READ:
2164 generate_scratch_read(inst, dst);
2165 fill_count++;
2166 break;
2167
2168 case SHADER_OPCODE_GEN7_SCRATCH_READ:
2169 generate_scratch_read_gen7(inst, dst);
2170 fill_count++;
2171 break;
2172
2173 case SHADER_OPCODE_MOV_INDIRECT:
2174 generate_mov_indirect(inst, dst, src[0], src[1]);
2175 break;
2176
2177 case SHADER_OPCODE_URB_READ_SIMD8:
2178 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:
2179 generate_urb_read(inst, dst, src[0]);
2180 break;
2181
2182 case SHADER_OPCODE_URB_WRITE_SIMD8:
2183 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:
2184 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:
2185 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:
2186 generate_urb_write(inst, src[0]);
2187 break;
2188
2189 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
2190 generate_uniform_pull_constant_load(inst, dst, src[0], src[1]);
2191 break;
2192
2193 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
2194 generate_uniform_pull_constant_load_gen7(inst, dst, src[0], src[1]);
2195 break;
2196
2197 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
2198 generate_varying_pull_constant_load(inst, dst, src[0], src[1]);
2199 break;
2200
2201 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
2202 generate_varying_pull_constant_load_gen7(inst, dst, src[0], src[1]);
2203 break;
2204
2205 case FS_OPCODE_REP_FB_WRITE:
2206 case FS_OPCODE_FB_WRITE:
2207 generate_fb_write(inst, src[0]);
2208 break;
2209
2210 case FS_OPCODE_BLORP_FB_WRITE:
2211 generate_blorp_fb_write(inst, src[0]);
2212 break;
2213
2214 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS:
2215 generate_mov_dispatch_to_flags(inst);
2216 break;
2217
2218 case FS_OPCODE_DISCARD_JUMP:
2219 generate_discard_jump(inst);
2220 break;
2221
2222 case SHADER_OPCODE_SHADER_TIME_ADD:
2223 generate_shader_time_add(inst, src[0], src[1], src[2]);
2224 break;
2225
2226 case SHADER_OPCODE_UNTYPED_ATOMIC:
2227 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2228 brw_untyped_atomic(p, dst, src[0], src[1], src[2].ud,
2229 inst->mlen, !inst->dst.is_null());
2230 break;
2231
2232 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
2233 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2234 brw_untyped_surface_read(p, dst, src[0], src[1],
2235 inst->mlen, src[2].ud);
2236 break;
2237
2238 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
2239 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2240 brw_untyped_surface_write(p, src[0], src[1],
2241 inst->mlen, src[2].ud);
2242 break;
2243
2244 case SHADER_OPCODE_TYPED_ATOMIC:
2245 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2246 brw_typed_atomic(p, dst, src[0], src[1],
2247 src[2].ud, inst->mlen, !inst->dst.is_null());
2248 break;
2249
2250 case SHADER_OPCODE_TYPED_SURFACE_READ:
2251 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2252 brw_typed_surface_read(p, dst, src[0], src[1],
2253 inst->mlen, src[2].ud);
2254 break;
2255
2256 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
2257 assert(src[2].file == BRW_IMMEDIATE_VALUE);
2258 brw_typed_surface_write(p, src[0], src[1], inst->mlen, src[2].ud);
2259 break;
2260
2261 case SHADER_OPCODE_MEMORY_FENCE:
2262 brw_memory_fence(p, dst);
2263 break;
2264
2265 case FS_OPCODE_SET_SIMD4X2_OFFSET:
2266 generate_set_simd4x2_offset(inst, dst, src[0]);
2267 break;
2268
2269 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
2270 brw_find_live_channel(p, dst);
2271 break;
2272
2273 case SHADER_OPCODE_BROADCAST:
2274 brw_broadcast(p, dst, src[0], src[1]);
2275 break;
2276
2277 case SHADER_OPCODE_EXTRACT_BYTE: {
2278 assert(src[0].type == BRW_REGISTER_TYPE_D ||
2279 src[0].type == BRW_REGISTER_TYPE_UD);
2280
2281 enum brw_reg_type type =
2282 src[0].type == BRW_REGISTER_TYPE_D ? BRW_REGISTER_TYPE_B
2283 : BRW_REGISTER_TYPE_UB;
2284 brw_MOV(p, dst, spread(suboffset(retype(src[0], type), src[1].ud), 4));
2285 break;
2286 }
2287
2288 case SHADER_OPCODE_EXTRACT_WORD: {
2289 assert(src[0].type == BRW_REGISTER_TYPE_D ||
2290 src[0].type == BRW_REGISTER_TYPE_UD);
2291
2292 enum brw_reg_type type =
2293 src[0].type == BRW_REGISTER_TYPE_D ? BRW_REGISTER_TYPE_W
2294 : BRW_REGISTER_TYPE_UW;
2295 brw_MOV(p, dst, spread(suboffset(retype(src[0], type), src[1].ud), 2));
2296 break;
2297 }
2298
2299 case FS_OPCODE_SET_SAMPLE_ID:
2300 generate_set_sample_id(inst, dst, src[0], src[1]);
2301 break;
2302
2303 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
2304 generate_pack_half_2x16_split(inst, dst, src[0], src[1]);
2305 break;
2306
2307 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X:
2308 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y:
2309 generate_unpack_half_2x16_split(inst, dst, src[0]);
2310 break;
2311
2312 case FS_OPCODE_PLACEHOLDER_HALT:
2313 /* This is the place where the final HALT needs to be inserted if
2314 * we've emitted any discards. If not, this will emit no code.
2315 */
2316 if (!patch_discard_jumps_to_fb_writes()) {
2317 if (unlikely(debug_flag)) {
2318 annotation.ann_count--;
2319 }
2320 }
2321 break;
2322
2323 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
2324 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2325 GEN7_PIXEL_INTERPOLATOR_LOC_CENTROID);
2326 break;
2327
2328 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
2329 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2330 GEN7_PIXEL_INTERPOLATOR_LOC_SAMPLE);
2331 break;
2332
2333 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
2334 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2335 GEN7_PIXEL_INTERPOLATOR_LOC_SHARED_OFFSET);
2336 break;
2337
2338 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
2339 generate_pixel_interpolator_query(inst, dst, src[0], src[1],
2340 GEN7_PIXEL_INTERPOLATOR_LOC_PER_SLOT_OFFSET);
2341 break;
2342
2343 case CS_OPCODE_CS_TERMINATE:
2344 generate_cs_terminate(inst, src[0]);
2345 break;
2346
2347 case SHADER_OPCODE_BARRIER:
2348 generate_barrier(inst, src[0]);
2349 break;
2350
2351 case FS_OPCODE_PACK_STENCIL_REF:
2352 generate_stencil_ref_packing(inst, dst, src[0]);
2353 break;
2354
2355 default:
2356 unreachable("Unsupported opcode");
2357
2358 case SHADER_OPCODE_LOAD_PAYLOAD:
2359 unreachable("Should be lowered by lower_load_payload()");
2360 }
2361
2362 if (multiple_instructions_emitted)
2363 continue;
2364
2365 if (inst->no_dd_clear || inst->no_dd_check || inst->conditional_mod) {
2366 assert(p->next_insn_offset == last_insn_offset + 16 ||
2367 !"conditional_mod, no_dd_check, or no_dd_clear set for IR "
2368 "emitting more than 1 instruction");
2369
2370 brw_inst *last = &p->store[last_insn_offset / 16];
2371
2372 if (inst->conditional_mod)
2373 brw_inst_set_cond_modifier(p->devinfo, last, inst->conditional_mod);
2374 brw_inst_set_no_dd_clear(p->devinfo, last, inst->no_dd_clear);
2375 brw_inst_set_no_dd_check(p->devinfo, last, inst->no_dd_check);
2376 }
2377 }
2378
2379 brw_set_uip_jip(p);
2380 annotation_finalize(&annotation, p->next_insn_offset);
2381
2382 #ifndef NDEBUG
2383 bool validated = brw_validate_instructions(p, start_offset, &annotation);
2384 #else
2385 if (unlikely(debug_flag))
2386 brw_validate_instructions(p, start_offset, &annotation);
2387 #endif
2388
2389 int before_size = p->next_insn_offset - start_offset;
2390 brw_compact_instructions(p, start_offset, annotation.ann_count,
2391 annotation.ann);
2392 int after_size = p->next_insn_offset - start_offset;
2393
2394 if (unlikely(debug_flag)) {
2395 fprintf(stderr, "Native code for %s\n"
2396 "SIMD%d shader: %d instructions. %d loops. %u cycles. %d:%d spills:fills. Promoted %u constants. Compacted %d to %d"
2397 " bytes (%.0f%%)\n",
2398 shader_name, dispatch_width, before_size / 16, loop_count, cfg->cycle_count,
2399 spill_count, fill_count, promoted_constants, before_size, after_size,
2400 100.0f * (before_size - after_size) / before_size);
2401
2402 dump_assembly(p->store, annotation.ann_count, annotation.ann,
2403 p->devinfo);
2404 ralloc_free(annotation.mem_ctx);
2405 }
2406 assert(validated);
2407
2408 compiler->shader_debug_log(log_data,
2409 "%s SIMD%d shader: %d inst, %d loops, %u cycles, "
2410 "%d:%d spills:fills, Promoted %u constants, "
2411 "compacted %d to %d bytes.",
2412 _mesa_shader_stage_to_abbrev(stage),
2413 dispatch_width, before_size / 16,
2414 loop_count, cfg->cycle_count, spill_count,
2415 fill_count, promoted_constants, before_size,
2416 after_size);
2417
2418 return start_offset;
2419 }
2420
2421 const unsigned *
2422 fs_generator::get_assembly(unsigned int *assembly_size)
2423 {
2424 return brw_get_program(p, assembly_size);
2425 }