2 * Copyright © 2010 Intel Corporation
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:
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
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
24 /** @file brw_fs_generator.cpp
26 * This file supports generating code from the FS LIR to the actual
27 * native instructions.
33 #include "brw_program.h"
35 static enum brw_reg_file
36 brw_file_from_reg(fs_reg
*reg
)
40 return BRW_ARCHITECTURE_REGISTER_FILE
;
43 return BRW_GENERAL_REGISTER_FILE
;
45 return BRW_MESSAGE_REGISTER_FILE
;
47 return BRW_IMMEDIATE_VALUE
;
51 unreachable("not reached");
53 return BRW_ARCHITECTURE_REGISTER_FILE
;
57 brw_reg_from_fs_reg(fs_inst
*inst
, fs_reg
*reg
, unsigned gen
)
59 struct brw_reg brw_reg
;
63 assert((reg
->nr
& ~BRW_MRF_COMPR4
) < BRW_MAX_MRF(gen
));
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
);
73 /* From the Haswell PRM:
75 * VertStride must be used to cross GRF register boundaries. This
76 * rule implies that elements within a 'Width' cannot cross GRF
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.
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
);
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
;
94 brw_reg
= reg
->as_brw_reg();
97 /* Probably unused. */
98 brw_reg
= brw_null_reg();
102 unreachable("not reached");
108 fs_generator::fs_generator(const struct brw_compiler
*compiler
, void *log_data
,
111 struct brw_stage_prog_data
*prog_data
,
112 unsigned promoted_constants
,
113 bool runtime_check_aads_emit
,
114 const char *stage_abbrev
)
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_abbrev(stage_abbrev
), mem_ctx(mem_ctx
)
123 p
= rzalloc(mem_ctx
, struct brw_codegen
);
124 brw_init_codegen(devinfo
, p
, mem_ctx
);
127 fs_generator::~fs_generator()
131 class ip_record
: public exec_node
{
133 DECLARE_RALLOC_CXX_OPERATORS(ip_record
)
144 fs_generator::patch_discard_jumps_to_fb_writes()
146 if (devinfo
->gen
< 6 || this->discard_halt_patches
.is_empty())
149 int scale
= brw_jump_scale(p
->devinfo
);
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.
158 * Symptoms of not emitting this instruction on actual hardware
159 * included GPU hangs and sparkly rendering on the piglit discard
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
);
168 foreach_in_list(ip_record
, patch_ip
, &discard_halt_patches
) {
169 brw_inst
*patch
= &p
->store
[patch_ip
->ip
];
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
);
176 this->discard_halt_patches
.make_empty();
181 fs_generator::fire_fb_write(fs_inst
*inst
,
182 struct brw_reg payload
,
183 struct brw_reg implied_header
,
186 uint32_t msg_control
;
188 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
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
);
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
;
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
;
210 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01
;
212 uint32_t surf_index
=
213 prog_data
->binding_table
.render_target_start
+ inst
->target
;
215 bool last_render_target
= inst
->eot
||
216 (prog_data
->dual_src_blend
&& dispatch_width
== 16);
229 inst
->header_size
!= 0);
231 brw_mark_surface_used(&prog_data
->base
, surf_index
);
235 fs_generator::generate_fb_write(fs_inst
*inst
, struct brw_reg payload
)
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
;
241 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
) {
242 brw_set_default_predicate_control(p
, BRW_PREDICATE_NONE
);
245 if (inst
->base_mrf
>= 0)
246 payload
= brw_message_reg(inst
->base_mrf
);
248 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
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);
258 /* On HSW, the GPU will use the predicate on SENDC, unless the header is
261 if (prog_data
->uses_kill
) {
262 struct brw_reg pixel_mask
;
264 if (devinfo
->gen
>= 6)
265 pixel_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
267 pixel_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
269 brw_MOV(p
, pixel_mask
, brw_flag_reg(0, 1));
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
);
277 retype(payload
, BRW_REGISTER_TYPE_UD
),
278 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
279 brw_pop_insn_state(p
);
281 if (inst
->target
> 0 && key
->replicate_alpha
) {
282 /* Set "Source0 Alpha Present to RenderTarget" bit in message
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));
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
));
298 /* Set computes stencil to render target */
299 if (prog_data
->computed_stencil
) {
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));
306 implied_header
= brw_null_reg();
308 implied_header
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
311 brw_pop_insn_state(p
);
313 implied_header
= brw_null_reg();
316 if (!runtime_check_aads_emit
) {
317 fire_fb_write(inst
, payload
, implied_header
, inst
->mlen
);
319 /* This can only happen in gen < 6 */
320 assert(devinfo
->gen
< 6);
322 struct brw_reg v1_null_ud
= vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
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
);
328 retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD
),
330 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_NZ
);
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
);
335 /* Don't send AA data */
336 fire_fb_write(inst
, offset(payload
, 1), implied_header
, inst
->mlen
-1);
338 brw_land_fwd_jump(p
, jmp
);
339 fire_fb_write(inst
, payload
, implied_header
, inst
->mlen
);
344 fs_generator::generate_mov_indirect(fs_inst
*inst
,
347 struct brw_reg indirect_byte_offset
)
349 assert(indirect_byte_offset
.type
== BRW_REGISTER_TYPE_UD
);
350 assert(indirect_byte_offset
.file
== BRW_GENERAL_REGISTER_FILE
);
352 unsigned imm_byte_offset
= reg
.nr
* REG_SIZE
+ reg
.subnr
;
354 /* We use VxH indirect addressing, clobbering a0.0 through a0.7. */
355 struct brw_reg addr
= vec8(brw_address_reg(0));
357 /* The destination stride of an instruction (in bytes) must be greater
358 * than or equal to the size of the rest of the instruction. Since the
359 * address register is of type UW, we can't use a D-type instruction.
360 * In order to get around this, re re-type to UW and use a stride.
362 indirect_byte_offset
=
363 retype(spread(indirect_byte_offset
, 2), BRW_REGISTER_TYPE_UW
);
365 /* Prior to Broadwell, there are only 8 address registers. */
366 assert(inst
->exec_size
== 8 || devinfo
->gen
>= 8);
368 brw_MOV(p
, addr
, indirect_byte_offset
);
369 brw_inst_set_mask_control(devinfo
, brw_last_inst
, BRW_MASK_DISABLE
);
370 brw_MOV(p
, dst
, retype(brw_VxH_indirect(0, imm_byte_offset
), dst
.type
));
374 fs_generator::generate_urb_read(fs_inst
*inst
,
376 struct brw_reg header
)
378 assert(header
.file
== BRW_GENERAL_REGISTER_FILE
);
379 assert(header
.type
== BRW_REGISTER_TYPE_UD
);
381 brw_inst
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
382 brw_set_dest(p
, send
, dst
);
383 brw_set_src0(p
, send
, header
);
384 brw_set_src1(p
, send
, brw_imm_ud(0u));
386 brw_inst_set_sfid(p
->devinfo
, send
, BRW_SFID_URB
);
387 brw_inst_set_urb_opcode(p
->devinfo
, send
, GEN8_URB_OPCODE_SIMD8_READ
);
389 if (inst
->opcode
== SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
)
390 brw_inst_set_urb_per_slot_offset(p
->devinfo
, send
, true);
392 brw_inst_set_mlen(p
->devinfo
, send
, inst
->mlen
);
393 brw_inst_set_rlen(p
->devinfo
, send
, inst
->regs_written
);
394 brw_inst_set_header_present(p
->devinfo
, send
, true);
395 brw_inst_set_urb_global_offset(p
->devinfo
, send
, inst
->offset
);
399 fs_generator::generate_urb_write(fs_inst
*inst
, struct brw_reg payload
)
403 insn
= brw_next_insn(p
, BRW_OPCODE_SEND
);
405 brw_set_dest(p
, insn
, brw_null_reg());
406 brw_set_src0(p
, insn
, payload
);
407 brw_set_src1(p
, insn
, brw_imm_d(0));
409 brw_inst_set_sfid(p
->devinfo
, insn
, BRW_SFID_URB
);
410 brw_inst_set_urb_opcode(p
->devinfo
, insn
, GEN8_URB_OPCODE_SIMD8_WRITE
);
412 if (inst
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
413 inst
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
)
414 brw_inst_set_urb_per_slot_offset(p
->devinfo
, insn
, true);
416 if (inst
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
417 inst
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
)
418 brw_inst_set_urb_channel_mask_present(p
->devinfo
, insn
, true);
420 brw_inst_set_mlen(p
->devinfo
, insn
, inst
->mlen
);
421 brw_inst_set_rlen(p
->devinfo
, insn
, 0);
422 brw_inst_set_eot(p
->devinfo
, insn
, inst
->eot
);
423 brw_inst_set_header_present(p
->devinfo
, insn
, true);
424 brw_inst_set_urb_global_offset(p
->devinfo
, insn
, inst
->offset
);
428 fs_generator::generate_cs_terminate(fs_inst
*inst
, struct brw_reg payload
)
430 struct brw_inst
*insn
;
432 insn
= brw_next_insn(p
, BRW_OPCODE_SEND
);
434 brw_set_dest(p
, insn
, brw_null_reg());
435 brw_set_src0(p
, insn
, payload
);
436 brw_set_src1(p
, insn
, brw_imm_d(0));
438 /* Terminate a compute shader by sending a message to the thread spawner.
440 brw_inst_set_sfid(devinfo
, insn
, BRW_SFID_THREAD_SPAWNER
);
441 brw_inst_set_mlen(devinfo
, insn
, 1);
442 brw_inst_set_rlen(devinfo
, insn
, 0);
443 brw_inst_set_eot(devinfo
, insn
, inst
->eot
);
444 brw_inst_set_header_present(devinfo
, insn
, false);
446 brw_inst_set_ts_opcode(devinfo
, insn
, 0); /* Dereference resource */
447 brw_inst_set_ts_request_type(devinfo
, insn
, 0); /* Root thread */
449 /* Note that even though the thread has a URB resource associated with it,
450 * we set the "do not dereference URB" bit, because the URB resource is
451 * managed by the fixed-function unit, so it will free it automatically.
453 brw_inst_set_ts_resource_select(devinfo
, insn
, 1); /* Do not dereference URB */
455 brw_inst_set_mask_control(devinfo
, insn
, BRW_MASK_DISABLE
);
459 fs_generator::generate_stencil_ref_packing(fs_inst
*inst
,
463 assert(dispatch_width
== 8);
464 assert(devinfo
->gen
>= 9);
466 /* Stencil value updates are provided in 8 slots of 1 byte per slot.
467 * Presumably, in order to save memory bandwidth, the stencil reference
468 * values written from the FS need to be packed into 2 dwords (this makes
469 * sense because the stencil values are limited to 1 byte each and a SIMD8
470 * send, so stencil slots 0-3 in dw0, and 4-7 in dw1.)
472 * The spec is confusing here because in the payload definition of MDP_RTW_S8
473 * (Message Data Payload for Render Target Writes with Stencil 8b) the
474 * stencil value seems to be dw4.0-dw4.7. However, if you look at the type of
475 * dw4 it is type MDPR_STENCIL (Message Data Payload Register) which is the
476 * packed values specified above and diagrammed below:
479 * --------------------------------
483 * --------------------------------
484 * DW1 | STC | STC | STC | STC |
485 * | slot7 | slot6 | slot5 | slot4|
486 * --------------------------------
487 * DW0 | STC | STC | STC | STC |
488 * | slot3 | slot2 | slot1 | slot0|
489 * --------------------------------
492 src
.vstride
= BRW_VERTICAL_STRIDE_4
;
493 src
.width
= BRW_WIDTH_1
;
494 src
.hstride
= BRW_HORIZONTAL_STRIDE_0
;
495 assert(src
.type
== BRW_REGISTER_TYPE_UB
);
496 brw_MOV(p
, retype(dst
, BRW_REGISTER_TYPE_UB
), src
);
500 fs_generator::generate_barrier(fs_inst
*inst
, struct brw_reg src
)
507 fs_generator::generate_blorp_fb_write(fs_inst
*inst
)
510 16 /* dispatch_width */,
511 brw_message_reg(inst
->base_mrf
),
512 brw_reg_from_fs_reg(inst
, &inst
->src
[0], devinfo
->gen
),
513 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
,
519 inst
->header_size
!= 0);
523 fs_generator::generate_linterp(fs_inst
*inst
,
524 struct brw_reg dst
, struct brw_reg
*src
)
528 * -----------------------------------
529 * | src1+0 | src1+1 | src1+2 | src1+3 |
530 * |-----------------------------------|
531 * |(x0, x1)|(y0, y1)|(x2, x3)|(y2, y3)|
532 * -----------------------------------
534 * but for the LINE/MAC pair, the LINE reads Xs and the MAC reads Ys:
536 * -----------------------------------
537 * | src1+0 | src1+1 | src1+2 | src1+3 |
538 * |-----------------------------------|
539 * |(x0, x1)|(y0, y1)| | | in SIMD8
540 * |-----------------------------------|
541 * |(x0, x1)|(x2, x3)|(y0, y1)|(y2, y3)| in SIMD16
542 * -----------------------------------
544 * See also: emit_interpolation_setup_gen4().
546 struct brw_reg delta_x
= src
[0];
547 struct brw_reg delta_y
= offset(src
[0], dispatch_width
/ 8);
548 struct brw_reg interp
= src
[1];
550 if (devinfo
->has_pln
&&
551 (devinfo
->gen
>= 7 || (delta_x
.nr
& 1) == 0)) {
552 brw_PLN(p
, dst
, interp
, delta_x
);
554 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
555 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
560 fs_generator::generate_math_gen6(fs_inst
*inst
,
565 int op
= brw_math_function(inst
->opcode
);
566 bool binop
= src1
.file
!= BRW_ARCHITECTURE_REGISTER_FILE
;
568 if (dispatch_width
== 8) {
569 gen6_math(p
, dst
, op
, src0
, src1
);
570 } else if (dispatch_width
== 16) {
571 brw_push_insn_state(p
);
572 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
573 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
574 gen6_math(p
, firsthalf(dst
), op
, firsthalf(src0
), firsthalf(src1
));
575 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
576 gen6_math(p
, sechalf(dst
), op
, sechalf(src0
),
577 binop
? sechalf(src1
) : brw_null_reg());
578 brw_pop_insn_state(p
);
583 fs_generator::generate_math_gen4(fs_inst
*inst
,
587 int op
= brw_math_function(inst
->opcode
);
589 assert(inst
->mlen
>= 1);
591 if (dispatch_width
== 8) {
595 BRW_MATH_PRECISION_FULL
);
596 } else if (dispatch_width
== 16) {
597 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
598 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
599 gen4_math(p
, firsthalf(dst
),
601 inst
->base_mrf
, firsthalf(src
),
602 BRW_MATH_PRECISION_FULL
);
603 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
604 gen4_math(p
, sechalf(dst
),
606 inst
->base_mrf
+ 1, sechalf(src
),
607 BRW_MATH_PRECISION_FULL
);
609 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
614 fs_generator::generate_math_g45(fs_inst
*inst
,
618 if (inst
->opcode
== SHADER_OPCODE_POW
||
619 inst
->opcode
== SHADER_OPCODE_INT_QUOTIENT
||
620 inst
->opcode
== SHADER_OPCODE_INT_REMAINDER
) {
621 generate_math_gen4(inst
, dst
, src
);
625 int op
= brw_math_function(inst
->opcode
);
627 assert(inst
->mlen
>= 1);
632 BRW_MATH_PRECISION_FULL
);
636 fs_generator::generate_get_buffer_size(fs_inst
*inst
,
639 struct brw_reg surf_index
)
641 assert(devinfo
->gen
>= 7);
642 assert(surf_index
.file
== BRW_IMMEDIATE_VALUE
);
647 switch (inst
->exec_size
) {
649 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
652 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
655 unreachable("Invalid width for texture instruction");
658 if (simd_mode
== BRW_SAMPLER_SIMD_MODE_SIMD16
) {
664 retype(dst
, BRW_REGISTER_TYPE_UW
),
669 GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
,
670 rlen
, /* response length */
672 inst
->header_size
> 0,
674 BRW_SAMPLER_RETURN_FORMAT_SINT32
);
676 brw_mark_surface_used(prog_data
, surf_index
.ud
);
680 fs_generator::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
,
681 struct brw_reg sampler_index
)
686 uint32_t return_format
;
687 bool is_combined_send
= inst
->eot
;
690 case BRW_REGISTER_TYPE_D
:
691 return_format
= BRW_SAMPLER_RETURN_FORMAT_SINT32
;
693 case BRW_REGISTER_TYPE_UD
:
694 return_format
= BRW_SAMPLER_RETURN_FORMAT_UINT32
;
697 return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
701 /* Stomp the resinfo output type to UINT32. On gens 4-5, the output type
702 * is set as part of the message descriptor. On gen4, the PRM seems to
703 * allow UINT32 and FLOAT32 (i965 PRM, Vol. 4 Section 4.8.1.1), but on
704 * later gens UINT32 is required. Once you hit Sandy Bridge, the bit is
705 * gone from the message descriptor entirely and you just get UINT32 all
706 * the time regasrdless. Since we can really only do non-UINT32 on gen4,
707 * just stomp it to UINT32 all the time.
709 if (inst
->opcode
== SHADER_OPCODE_TXS
)
710 return_format
= BRW_SAMPLER_RETURN_FORMAT_UINT32
;
712 switch (inst
->exec_size
) {
714 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
717 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
720 unreachable("Invalid width for texture instruction");
723 if (devinfo
->gen
>= 5) {
724 switch (inst
->opcode
) {
725 case SHADER_OPCODE_TEX
:
726 if (inst
->shadow_compare
) {
727 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
;
729 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE
;
733 if (inst
->shadow_compare
) {
734 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
;
736 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
739 case SHADER_OPCODE_TXL
:
740 if (inst
->shadow_compare
) {
741 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
743 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
746 case SHADER_OPCODE_TXS
:
747 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
749 case SHADER_OPCODE_TXD
:
750 if (inst
->shadow_compare
) {
751 /* Gen7.5+. Otherwise, lowered by brw_lower_texture_gradients(). */
752 assert(devinfo
->gen
>= 8 || devinfo
->is_haswell
);
753 msg_type
= HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
;
755 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
758 case SHADER_OPCODE_TXF
:
759 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
761 case SHADER_OPCODE_TXF_CMS_W
:
762 assert(devinfo
->gen
>= 9);
763 msg_type
= GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W
;
765 case SHADER_OPCODE_TXF_CMS
:
766 if (devinfo
->gen
>= 7)
767 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
;
769 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
771 case SHADER_OPCODE_TXF_UMS
:
772 assert(devinfo
->gen
>= 7);
773 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
775 case SHADER_OPCODE_TXF_MCS
:
776 assert(devinfo
->gen
>= 7);
777 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
779 case SHADER_OPCODE_LOD
:
780 msg_type
= GEN5_SAMPLER_MESSAGE_LOD
;
782 case SHADER_OPCODE_TG4
:
783 if (inst
->shadow_compare
) {
784 assert(devinfo
->gen
>= 7);
785 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
;
787 assert(devinfo
->gen
>= 6);
788 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
791 case SHADER_OPCODE_TG4_OFFSET
:
792 assert(devinfo
->gen
>= 7);
793 if (inst
->shadow_compare
) {
794 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
;
796 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
799 case SHADER_OPCODE_SAMPLEINFO
:
800 msg_type
= GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO
;
803 unreachable("not reached");
806 switch (inst
->opcode
) {
807 case SHADER_OPCODE_TEX
:
808 /* Note that G45 and older determines shadow compare and dispatch width
809 * from message length for most messages.
811 if (inst
->exec_size
== 8) {
812 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE
;
813 if (inst
->shadow_compare
) {
814 assert(inst
->mlen
== 6);
816 assert(inst
->mlen
<= 4);
819 if (inst
->shadow_compare
) {
820 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE
;
821 assert(inst
->mlen
== 9);
823 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE
;
824 assert(inst
->mlen
<= 7 && inst
->mlen
% 2 == 1);
829 if (inst
->shadow_compare
) {
830 assert(inst
->exec_size
== 8);
831 assert(inst
->mlen
== 6);
832 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE
;
834 assert(inst
->mlen
== 9);
835 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
836 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
839 case SHADER_OPCODE_TXL
:
840 if (inst
->shadow_compare
) {
841 assert(inst
->exec_size
== 8);
842 assert(inst
->mlen
== 6);
843 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE
;
845 assert(inst
->mlen
== 9);
846 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD
;
847 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
850 case SHADER_OPCODE_TXD
:
851 /* There is no sample_d_c message; comparisons are done manually */
852 assert(inst
->exec_size
== 8);
853 assert(inst
->mlen
== 7 || inst
->mlen
== 10);
854 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS
;
856 case SHADER_OPCODE_TXF
:
857 assert(inst
->mlen
<= 9 && inst
->mlen
% 2 == 1);
858 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
859 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
861 case SHADER_OPCODE_TXS
:
862 assert(inst
->mlen
== 3);
863 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_RESINFO
;
864 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
867 unreachable("not reached");
870 assert(msg_type
!= -1);
872 if (simd_mode
== BRW_SAMPLER_SIMD_MODE_SIMD16
) {
877 if (is_combined_send
) {
878 assert(devinfo
->gen
>= 9 || devinfo
->is_cherryview
);
882 assert(devinfo
->gen
< 7 || inst
->header_size
== 0 ||
883 src
.file
== BRW_GENERAL_REGISTER_FILE
);
885 assert(sampler_index
.type
== BRW_REGISTER_TYPE_UD
);
887 /* Load the message header if present. If there's a texture offset,
888 * we need to set it up explicitly and load the offset bitfield.
889 * Otherwise, we can use an implied move from g0 to the first message reg.
891 if (inst
->header_size
!= 0) {
892 if (devinfo
->gen
< 6 && !inst
->offset
) {
893 /* Set up an implied move from g0 to the MRF. */
894 src
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
896 struct brw_reg header_reg
;
898 if (devinfo
->gen
>= 7) {
901 assert(inst
->base_mrf
!= -1);
902 header_reg
= brw_message_reg(inst
->base_mrf
);
905 brw_push_insn_state(p
);
906 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
907 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
908 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
909 /* Explicitly set up the message header by copying g0 to the MRF. */
910 brw_MOV(p
, header_reg
, brw_vec8_grf(0, 0));
913 /* Set the offset bits in DWord 2. */
914 brw_MOV(p
, get_element_ud(header_reg
, 2),
915 brw_imm_ud(inst
->offset
));
918 brw_adjust_sampler_state_pointer(p
, header_reg
, sampler_index
);
919 brw_pop_insn_state(p
);
923 uint32_t base_binding_table_index
= (inst
->opcode
== SHADER_OPCODE_TG4
||
924 inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
925 ? prog_data
->binding_table
.gather_texture_start
926 : prog_data
->binding_table
.texture_start
;
928 if (sampler_index
.file
== BRW_IMMEDIATE_VALUE
) {
929 uint32_t sampler
= sampler_index
.ud
;
932 retype(dst
, BRW_REGISTER_TYPE_UW
),
935 sampler
+ base_binding_table_index
,
940 inst
->header_size
!= 0,
944 brw_mark_surface_used(prog_data
, sampler
+ base_binding_table_index
);
946 /* Non-const sampler index */
948 struct brw_reg addr
= vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD
));
949 struct brw_reg sampler_reg
= vec1(retype(sampler_index
, BRW_REGISTER_TYPE_UD
));
951 brw_push_insn_state(p
);
952 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
953 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
955 /* addr = ((sampler * 0x101) + base_binding_table_index) & 0xfff */
956 brw_MUL(p
, addr
, sampler_reg
, brw_imm_uw(0x101));
957 if (base_binding_table_index
)
958 brw_ADD(p
, addr
, addr
, brw_imm_ud(base_binding_table_index
));
959 brw_AND(p
, addr
, addr
, brw_imm_ud(0xfff));
961 brw_pop_insn_state(p
);
963 /* dst = send(offset, a0.0 | <descriptor>) */
964 brw_inst
*insn
= brw_send_indirect_message(
965 p
, BRW_SFID_SAMPLER
, dst
, src
, addr
);
966 brw_set_sampler_message(p
, insn
,
971 inst
->mlen
/* mlen */,
972 inst
->header_size
!= 0 /* header */,
976 /* visitor knows more than we do about the surface limit required,
977 * so has already done marking.
981 if (is_combined_send
) {
982 brw_inst_set_eot(p
->devinfo
, brw_last_inst
, true);
983 brw_inst_set_opcode(p
->devinfo
, brw_last_inst
, BRW_OPCODE_SENDC
);
988 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
991 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
993 * Ideally, we want to produce:
996 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
997 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
998 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
999 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
1000 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
1001 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
1002 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
1003 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
1005 * and add another set of two more subspans if in 16-pixel dispatch mode.
1007 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
1008 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
1009 * pair. But the ideal approximation may impose a huge performance cost on
1010 * sample_d. On at least Haswell, sample_d instruction does some
1011 * optimizations if the same LOD is used for all pixels in the subspan.
1013 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
1014 * appropriate swizzling.
1017 fs_generator::generate_ddx(enum opcode opcode
,
1018 struct brw_reg dst
, struct brw_reg src
)
1020 unsigned vstride
, width
;
1022 if (opcode
== FS_OPCODE_DDX_FINE
) {
1023 /* produce accurate derivatives */
1024 vstride
= BRW_VERTICAL_STRIDE_2
;
1025 width
= BRW_WIDTH_2
;
1027 /* replicate the derivative at the top-left pixel to other pixels */
1028 vstride
= BRW_VERTICAL_STRIDE_4
;
1029 width
= BRW_WIDTH_4
;
1032 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
1033 src
.negate
, src
.abs
,
1034 BRW_REGISTER_TYPE_F
,
1037 BRW_HORIZONTAL_STRIDE_0
,
1038 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1039 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
1040 src
.negate
, src
.abs
,
1041 BRW_REGISTER_TYPE_F
,
1044 BRW_HORIZONTAL_STRIDE_0
,
1045 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1046 brw_ADD(p
, dst
, src0
, negate(src1
));
1049 /* The negate_value boolean is used to negate the derivative computation for
1050 * FBOs, since they place the origin at the upper left instead of the lower
1054 fs_generator::generate_ddy(enum opcode opcode
,
1055 struct brw_reg dst
, struct brw_reg src
,
1058 if (opcode
== FS_OPCODE_DDY_FINE
) {
1059 /* From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
1060 * Region Restrictions):
1062 * In Align16 access mode, SIMD16 is not allowed for DW operations
1063 * and SIMD8 is not allowed for DF operations.
1065 * In this context, "DW operations" means "operations acting on 32-bit
1066 * values", so it includes operations on floats.
1068 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
1069 * (Instruction Compression -> Rules and Restrictions):
1071 * A compressed instruction must be in Align1 access mode. Align16
1072 * mode instructions cannot be compressed.
1074 * Similar text exists in the g45 PRM.
1076 * On these platforms, if we're building a SIMD16 shader, we need to
1077 * manually unroll to a pair of SIMD8 instructions.
1079 bool unroll_to_simd8
=
1080 (dispatch_width
== 16 &&
1081 (devinfo
->gen
== 4 || (devinfo
->gen
== 7 && !devinfo
->is_haswell
)));
1083 /* produce accurate derivatives */
1084 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
1085 src
.negate
, src
.abs
,
1086 BRW_REGISTER_TYPE_F
,
1087 BRW_VERTICAL_STRIDE_4
,
1089 BRW_HORIZONTAL_STRIDE_1
,
1090 BRW_SWIZZLE_XYXY
, WRITEMASK_XYZW
);
1091 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
1092 src
.negate
, src
.abs
,
1093 BRW_REGISTER_TYPE_F
,
1094 BRW_VERTICAL_STRIDE_4
,
1096 BRW_HORIZONTAL_STRIDE_1
,
1097 BRW_SWIZZLE_ZWZW
, WRITEMASK_XYZW
);
1098 brw_push_insn_state(p
);
1099 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
1100 if (unroll_to_simd8
) {
1101 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1102 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1104 brw_ADD(p
, firsthalf(dst
), firsthalf(src1
), negate(firsthalf(src0
)));
1105 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1106 brw_ADD(p
, sechalf(dst
), sechalf(src1
), negate(sechalf(src0
)));
1108 brw_ADD(p
, firsthalf(dst
), firsthalf(src0
), negate(firsthalf(src1
)));
1109 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1110 brw_ADD(p
, sechalf(dst
), sechalf(src0
), negate(sechalf(src1
)));
1114 brw_ADD(p
, dst
, src1
, negate(src0
));
1116 brw_ADD(p
, dst
, src0
, negate(src1
));
1118 brw_pop_insn_state(p
);
1120 /* replicate the derivative at the top-left pixel to other pixels */
1121 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
1122 src
.negate
, src
.abs
,
1123 BRW_REGISTER_TYPE_F
,
1124 BRW_VERTICAL_STRIDE_4
,
1126 BRW_HORIZONTAL_STRIDE_0
,
1127 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1128 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
1129 src
.negate
, src
.abs
,
1130 BRW_REGISTER_TYPE_F
,
1131 BRW_VERTICAL_STRIDE_4
,
1133 BRW_HORIZONTAL_STRIDE_0
,
1134 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1136 brw_ADD(p
, dst
, src1
, negate(src0
));
1138 brw_ADD(p
, dst
, src0
, negate(src1
));
1143 fs_generator::generate_discard_jump(fs_inst
*inst
)
1145 assert(devinfo
->gen
>= 6);
1147 /* This HALT will be patched up at FB write time to point UIP at the end of
1148 * the program, and at brw_uip_jip() JIP will be set to the end of the
1149 * current block (or the program).
1151 this->discard_halt_patches
.push_tail(new(mem_ctx
) ip_record(p
->nr_insn
));
1153 brw_push_insn_state(p
);
1154 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1156 brw_pop_insn_state(p
);
1160 fs_generator::generate_scratch_write(fs_inst
*inst
, struct brw_reg src
)
1162 assert(inst
->mlen
!= 0);
1165 brw_uvec_mrf(inst
->exec_size
, (inst
->base_mrf
+ 1), 0),
1166 retype(src
, BRW_REGISTER_TYPE_UD
));
1167 brw_oword_block_write_scratch(p
, brw_message_reg(inst
->base_mrf
),
1168 inst
->exec_size
/ 8, inst
->offset
);
1172 fs_generator::generate_scratch_read(fs_inst
*inst
, struct brw_reg dst
)
1174 assert(inst
->mlen
!= 0);
1176 brw_oword_block_read_scratch(p
, dst
, brw_message_reg(inst
->base_mrf
),
1177 inst
->exec_size
/ 8, inst
->offset
);
1181 fs_generator::generate_scratch_read_gen7(fs_inst
*inst
, struct brw_reg dst
)
1183 gen7_block_read_scratch(p
, dst
, inst
->exec_size
/ 8, inst
->offset
);
1187 fs_generator::generate_uniform_pull_constant_load(fs_inst
*inst
,
1189 struct brw_reg index
,
1190 struct brw_reg offset
)
1192 assert(inst
->mlen
!= 0);
1194 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
1195 index
.type
== BRW_REGISTER_TYPE_UD
);
1196 uint32_t surf_index
= index
.ud
;
1198 assert(offset
.file
== BRW_IMMEDIATE_VALUE
&&
1199 offset
.type
== BRW_REGISTER_TYPE_UD
);
1200 uint32_t read_offset
= offset
.ud
;
1202 brw_oword_block_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
1203 read_offset
, surf_index
);
1207 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst
*inst
,
1209 struct brw_reg index
,
1210 struct brw_reg offset
)
1212 assert(index
.type
== BRW_REGISTER_TYPE_UD
);
1214 assert(offset
.file
== BRW_GENERAL_REGISTER_FILE
);
1215 /* Reference just the dword we need, to avoid angering validate_reg(). */
1216 offset
= brw_vec1_grf(offset
.nr
, 0);
1218 /* We use the SIMD4x2 mode because we want to end up with 4 components in
1219 * the destination loaded consecutively from the same offset (which appears
1220 * in the first component, and the rest are ignored).
1222 dst
.width
= BRW_WIDTH_4
;
1224 struct brw_reg src
= offset
;
1225 bool header_present
= false;
1227 if (devinfo
->gen
>= 9) {
1228 /* Skylake requires a message header in order to use SIMD4x2 mode. */
1229 src
= retype(brw_vec4_grf(offset
.nr
, 0), BRW_REGISTER_TYPE_UD
);
1230 header_present
= true;
1232 brw_push_insn_state(p
);
1233 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1234 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1235 brw_MOV(p
, vec8(src
), retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
1236 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1238 brw_MOV(p
, get_element_ud(src
, 2),
1239 brw_imm_ud(GEN9_SAMPLER_SIMD_MODE_EXTENSION_SIMD4X2
));
1240 brw_pop_insn_state(p
);
1243 if (index
.file
== BRW_IMMEDIATE_VALUE
) {
1245 uint32_t surf_index
= index
.ud
;
1247 brw_push_insn_state(p
);
1248 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1249 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1250 brw_inst
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
1251 brw_pop_insn_state(p
);
1253 brw_set_dest(p
, send
, dst
);
1254 brw_set_src0(p
, send
, src
);
1255 brw_set_sampler_message(p
, send
,
1257 0, /* LD message ignores sampler unit */
1258 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
1262 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
1266 struct brw_reg addr
= vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD
));
1268 brw_push_insn_state(p
);
1269 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1270 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1272 /* a0.0 = surf_index & 0xff */
1273 brw_inst
*insn_and
= brw_next_insn(p
, BRW_OPCODE_AND
);
1274 brw_inst_set_exec_size(p
->devinfo
, insn_and
, BRW_EXECUTE_1
);
1275 brw_set_dest(p
, insn_and
, addr
);
1276 brw_set_src0(p
, insn_and
, vec1(retype(index
, BRW_REGISTER_TYPE_UD
)));
1277 brw_set_src1(p
, insn_and
, brw_imm_ud(0x0ff));
1279 /* dst = send(payload, a0.0 | <descriptor>) */
1280 brw_inst
*insn
= brw_send_indirect_message(
1281 p
, BRW_SFID_SAMPLER
, dst
, src
, addr
);
1282 brw_set_sampler_message(p
, insn
,
1284 0, /* LD message ignores sampler unit */
1285 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
1289 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
1292 brw_pop_insn_state(p
);
1297 fs_generator::generate_varying_pull_constant_load(fs_inst
*inst
,
1299 struct brw_reg index
,
1300 struct brw_reg offset
)
1302 assert(devinfo
->gen
< 7); /* Should use the gen7 variant. */
1303 assert(inst
->header_size
!= 0);
1306 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
1307 index
.type
== BRW_REGISTER_TYPE_UD
);
1308 uint32_t surf_index
= index
.ud
;
1310 uint32_t simd_mode
, rlen
, msg_type
;
1311 if (dispatch_width
== 16) {
1312 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
1315 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
1319 if (devinfo
->gen
>= 5)
1320 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
1322 /* We always use the SIMD16 message so that we only have to load U, and
1325 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
1326 assert(inst
->mlen
== 3);
1327 assert(inst
->regs_written
== 8);
1329 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
1332 struct brw_reg offset_mrf
= retype(brw_message_reg(inst
->base_mrf
+ 1),
1333 BRW_REGISTER_TYPE_D
);
1334 brw_MOV(p
, offset_mrf
, offset
);
1336 struct brw_reg header
= brw_vec8_grf(0, 0);
1337 gen6_resolve_implied_move(p
, &header
, inst
->base_mrf
);
1339 brw_inst
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
1340 brw_inst_set_qtr_control(p
->devinfo
, send
, BRW_COMPRESSION_NONE
);
1341 brw_set_dest(p
, send
, retype(dst
, BRW_REGISTER_TYPE_UW
));
1342 brw_set_src0(p
, send
, header
);
1343 if (devinfo
->gen
< 6)
1344 brw_inst_set_base_mrf(p
->devinfo
, send
, inst
->base_mrf
);
1346 /* Our surface is set up as floats, regardless of what actual data is
1349 uint32_t return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
1350 brw_set_sampler_message(p
, send
,
1352 0, /* sampler (unused) */
1356 inst
->header_size
!= 0,
1362 fs_generator::generate_varying_pull_constant_load_gen7(fs_inst
*inst
,
1364 struct brw_reg index
,
1365 struct brw_reg offset
)
1367 assert(devinfo
->gen
>= 7);
1368 /* Varying-offset pull constant loads are treated as a normal expression on
1369 * gen7, so the fact that it's a send message is hidden at the IR level.
1371 assert(inst
->header_size
== 0);
1372 assert(!inst
->mlen
);
1373 assert(index
.type
== BRW_REGISTER_TYPE_UD
);
1375 uint32_t simd_mode
, rlen
, mlen
;
1376 if (dispatch_width
== 16) {
1379 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
1383 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
1386 if (index
.file
== BRW_IMMEDIATE_VALUE
) {
1388 uint32_t surf_index
= index
.ud
;
1390 brw_inst
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
1391 brw_set_dest(p
, send
, retype(dst
, BRW_REGISTER_TYPE_UW
));
1392 brw_set_src0(p
, send
, offset
);
1393 brw_set_sampler_message(p
, send
,
1395 0, /* LD message ignores sampler unit */
1396 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
1399 false, /* no header */
1405 struct brw_reg addr
= vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD
));
1407 brw_push_insn_state(p
);
1408 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1409 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1411 /* a0.0 = surf_index & 0xff */
1412 brw_inst
*insn_and
= brw_next_insn(p
, BRW_OPCODE_AND
);
1413 brw_inst_set_exec_size(p
->devinfo
, insn_and
, BRW_EXECUTE_1
);
1414 brw_set_dest(p
, insn_and
, addr
);
1415 brw_set_src0(p
, insn_and
, vec1(retype(index
, BRW_REGISTER_TYPE_UD
)));
1416 brw_set_src1(p
, insn_and
, brw_imm_ud(0x0ff));
1418 brw_pop_insn_state(p
);
1420 /* dst = send(offset, a0.0 | <descriptor>) */
1421 brw_inst
*insn
= brw_send_indirect_message(
1422 p
, BRW_SFID_SAMPLER
, retype(dst
, BRW_REGISTER_TYPE_UW
),
1424 brw_set_sampler_message(p
, insn
,
1427 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
1437 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
1438 * into the flags register (f0.0).
1440 * Used only on Gen6 and above.
1443 fs_generator::generate_mov_dispatch_to_flags(fs_inst
*inst
)
1445 struct brw_reg flags
= brw_flag_reg(0, inst
->flag_subreg
);
1446 struct brw_reg dispatch_mask
;
1448 if (devinfo
->gen
>= 6)
1449 dispatch_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
1451 dispatch_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
1453 brw_push_insn_state(p
);
1454 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1455 brw_MOV(p
, flags
, dispatch_mask
);
1456 brw_pop_insn_state(p
);
1460 fs_generator::generate_pixel_interpolator_query(fs_inst
*inst
,
1463 struct brw_reg msg_data
,
1466 assert(msg_data
.type
== BRW_REGISTER_TYPE_UD
);
1468 brw_pixel_interpolator_query(p
,
1469 retype(dst
, BRW_REGISTER_TYPE_UW
),
1471 inst
->pi_noperspective
,
1475 inst
->regs_written
);
1480 * Sets the first word of a vgrf for gen7+ simd4x2 uniform pull constant
1481 * sampler LD messages.
1483 * We don't want to bake it into the send message's code generation because
1484 * that means we don't get a chance to schedule the instructions.
1487 fs_generator::generate_set_simd4x2_offset(fs_inst
*inst
,
1489 struct brw_reg value
)
1491 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1493 brw_push_insn_state(p
);
1494 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1495 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1496 brw_set_default_mask_control(p
, BRW_MASK_DISABLE
);
1497 brw_MOV(p
, retype(brw_vec1_reg(dst
.file
, dst
.nr
, 0), value
.type
), value
);
1498 brw_pop_insn_state(p
);
1501 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1502 * the ADD instruction.
1505 fs_generator::generate_set_sample_id(fs_inst
*inst
,
1507 struct brw_reg src0
,
1508 struct brw_reg src1
)
1510 assert(dst
.type
== BRW_REGISTER_TYPE_D
||
1511 dst
.type
== BRW_REGISTER_TYPE_UD
);
1512 assert(src0
.type
== BRW_REGISTER_TYPE_D
||
1513 src0
.type
== BRW_REGISTER_TYPE_UD
);
1515 struct brw_reg reg
= stride(src1
, 1, 4, 0);
1516 if (devinfo
->gen
>= 8 || dispatch_width
== 8) {
1517 brw_ADD(p
, dst
, src0
, reg
);
1518 } else if (dispatch_width
== 16) {
1519 brw_push_insn_state(p
);
1520 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1521 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1522 brw_ADD(p
, firsthalf(dst
), firsthalf(src0
), reg
);
1523 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1524 brw_ADD(p
, sechalf(dst
), sechalf(src0
), suboffset(reg
, 2));
1525 brw_pop_insn_state(p
);
1530 fs_generator::generate_pack_half_2x16_split(fs_inst
*inst
,
1535 assert(devinfo
->gen
>= 7);
1536 assert(dst
.type
== BRW_REGISTER_TYPE_UD
);
1537 assert(x
.type
== BRW_REGISTER_TYPE_F
);
1538 assert(y
.type
== BRW_REGISTER_TYPE_F
);
1540 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1542 * Because this instruction does not have a 16-bit floating-point type,
1543 * the destination data type must be Word (W).
1545 * The destination must be DWord-aligned and specify a horizontal stride
1546 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1547 * each destination channel and the upper word is not modified.
1549 struct brw_reg dst_w
= spread(retype(dst
, BRW_REGISTER_TYPE_W
), 2);
1551 /* Give each 32-bit channel of dst the form below, where "." means
1555 brw_F32TO16(p
, dst_w
, y
);
1560 brw_SHL(p
, dst
, dst
, brw_imm_ud(16u));
1562 /* And, finally the form of packHalf2x16's output:
1565 brw_F32TO16(p
, dst_w
, x
);
1569 fs_generator::generate_unpack_half_2x16_split(fs_inst
*inst
,
1573 assert(devinfo
->gen
>= 7);
1574 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1575 assert(src
.type
== BRW_REGISTER_TYPE_UD
);
1577 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1579 * Because this instruction does not have a 16-bit floating-point type,
1580 * the source data type must be Word (W). The destination type must be
1583 struct brw_reg src_w
= spread(retype(src
, BRW_REGISTER_TYPE_W
), 2);
1585 /* Each channel of src has the form of unpackHalf2x16's input: 0xhhhhllll.
1586 * For the Y case, we wish to access only the upper word; therefore
1587 * a 16-bit subregister offset is needed.
1589 assert(inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
||
1590 inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
);
1591 if (inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
)
1594 brw_F16TO32(p
, dst
, src_w
);
1598 fs_generator::generate_shader_time_add(fs_inst
*inst
,
1599 struct brw_reg payload
,
1600 struct brw_reg offset
,
1601 struct brw_reg value
)
1603 assert(devinfo
->gen
>= 7);
1604 brw_push_insn_state(p
);
1605 brw_set_default_mask_control(p
, true);
1607 assert(payload
.file
== BRW_GENERAL_REGISTER_FILE
);
1608 struct brw_reg payload_offset
= retype(brw_vec1_grf(payload
.nr
, 0),
1610 struct brw_reg payload_value
= retype(brw_vec1_grf(payload
.nr
+ 1, 0),
1613 assert(offset
.file
== BRW_IMMEDIATE_VALUE
);
1614 if (value
.file
== BRW_GENERAL_REGISTER_FILE
) {
1615 value
.width
= BRW_WIDTH_1
;
1616 value
.hstride
= BRW_HORIZONTAL_STRIDE_0
;
1617 value
.vstride
= BRW_VERTICAL_STRIDE_0
;
1619 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1622 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1623 * case, and we don't really care about squeezing every bit of performance
1624 * out of this path, so we just emit the MOVs from here.
1626 brw_MOV(p
, payload_offset
, offset
);
1627 brw_MOV(p
, payload_value
, value
);
1628 brw_shader_time_add(p
, payload
,
1629 prog_data
->binding_table
.shader_time_start
);
1630 brw_pop_insn_state(p
);
1632 brw_mark_surface_used(prog_data
,
1633 prog_data
->binding_table
.shader_time_start
);
1637 fs_generator::enable_debug(const char *shader_name
)
1640 this->shader_name
= shader_name
;
1644 fs_generator::generate_code(const cfg_t
*cfg
, int dispatch_width
)
1646 /* align to 64 byte boundary. */
1647 while (p
->next_insn_offset
% 64)
1650 this->dispatch_width
= dispatch_width
;
1651 if (dispatch_width
== 16)
1652 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1654 int start_offset
= p
->next_insn_offset
;
1655 int spill_count
= 0, fill_count
= 0;
1658 struct annotation_info annotation
;
1659 memset(&annotation
, 0, sizeof(annotation
));
1661 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
1662 struct brw_reg src
[3], dst
;
1663 unsigned int last_insn_offset
= p
->next_insn_offset
;
1664 bool multiple_instructions_emitted
= false;
1666 if (unlikely(debug_flag
))
1667 annotate(p
->devinfo
, &annotation
, cfg
, inst
, p
->next_insn_offset
);
1669 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1670 src
[i
] = brw_reg_from_fs_reg(inst
, &inst
->src
[i
], devinfo
->gen
);
1672 /* The accumulator result appears to get used for the
1673 * conditional modifier generation. When negating a UD
1674 * value, there is a 33rd bit generated for the sign in the
1675 * accumulator value, so now you can't check, for example,
1676 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1678 assert(!inst
->conditional_mod
||
1679 inst
->src
[i
].type
!= BRW_REGISTER_TYPE_UD
||
1680 !inst
->src
[i
].negate
);
1682 dst
= brw_reg_from_fs_reg(inst
, &inst
->dst
, devinfo
->gen
);
1684 brw_set_default_predicate_control(p
, inst
->predicate
);
1685 brw_set_default_predicate_inverse(p
, inst
->predicate_inverse
);
1686 brw_set_default_flag_reg(p
, 0, inst
->flag_subreg
);
1687 brw_set_default_saturate(p
, inst
->saturate
);
1688 brw_set_default_mask_control(p
, inst
->force_writemask_all
);
1689 brw_set_default_acc_write_control(p
, inst
->writes_accumulator
);
1690 brw_set_default_exec_size(p
, cvt(inst
->exec_size
) - 1);
1692 assert(inst
->base_mrf
+ inst
->mlen
<= BRW_MAX_MRF(devinfo
->gen
));
1693 assert(inst
->mlen
<= BRW_MAX_MSG_LENGTH
);
1695 switch (inst
->exec_size
) {
1699 assert(inst
->force_writemask_all
);
1700 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1703 if (inst
->force_sechalf
) {
1704 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1706 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1711 /* If the instruction writes to more than one register, it needs to
1712 * be a "compressed" instruction on Gen <= 5.
1714 if (inst
->dst
.component_size(inst
->exec_size
) > REG_SIZE
)
1715 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1717 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1720 unreachable("Invalid instruction width");
1723 switch (inst
->opcode
) {
1724 case BRW_OPCODE_MOV
:
1725 brw_MOV(p
, dst
, src
[0]);
1727 case BRW_OPCODE_ADD
:
1728 brw_ADD(p
, dst
, src
[0], src
[1]);
1730 case BRW_OPCODE_MUL
:
1731 brw_MUL(p
, dst
, src
[0], src
[1]);
1733 case BRW_OPCODE_AVG
:
1734 brw_AVG(p
, dst
, src
[0], src
[1]);
1736 case BRW_OPCODE_MACH
:
1737 brw_MACH(p
, dst
, src
[0], src
[1]);
1740 case BRW_OPCODE_LINE
:
1741 brw_LINE(p
, dst
, src
[0], src
[1]);
1744 case BRW_OPCODE_MAD
:
1745 assert(devinfo
->gen
>= 6);
1746 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
1747 if (dispatch_width
== 16 && !devinfo
->supports_simd16_3src
) {
1748 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1749 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1750 brw_inst
*f
= brw_MAD(p
, firsthalf(dst
), firsthalf(src
[0]), firsthalf(src
[1]), firsthalf(src
[2]));
1751 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1752 brw_inst
*s
= brw_MAD(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1753 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1755 if (inst
->conditional_mod
) {
1756 brw_inst_set_cond_modifier(p
->devinfo
, f
, inst
->conditional_mod
);
1757 brw_inst_set_cond_modifier(p
->devinfo
, s
, inst
->conditional_mod
);
1758 multiple_instructions_emitted
= true;
1761 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
1763 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1766 case BRW_OPCODE_LRP
:
1767 assert(devinfo
->gen
>= 6);
1768 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
1769 if (dispatch_width
== 16 && !devinfo
->supports_simd16_3src
) {
1770 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1771 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1772 brw_inst
*f
= brw_LRP(p
, firsthalf(dst
), firsthalf(src
[0]), firsthalf(src
[1]), firsthalf(src
[2]));
1773 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1774 brw_inst
*s
= brw_LRP(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1775 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1777 if (inst
->conditional_mod
) {
1778 brw_inst_set_cond_modifier(p
->devinfo
, f
, inst
->conditional_mod
);
1779 brw_inst_set_cond_modifier(p
->devinfo
, s
, inst
->conditional_mod
);
1780 multiple_instructions_emitted
= true;
1783 brw_LRP(p
, dst
, src
[0], src
[1], src
[2]);
1785 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1788 case BRW_OPCODE_FRC
:
1789 brw_FRC(p
, dst
, src
[0]);
1791 case BRW_OPCODE_RNDD
:
1792 brw_RNDD(p
, dst
, src
[0]);
1794 case BRW_OPCODE_RNDE
:
1795 brw_RNDE(p
, dst
, src
[0]);
1797 case BRW_OPCODE_RNDZ
:
1798 brw_RNDZ(p
, dst
, src
[0]);
1801 case BRW_OPCODE_AND
:
1802 brw_AND(p
, dst
, src
[0], src
[1]);
1805 brw_OR(p
, dst
, src
[0], src
[1]);
1807 case BRW_OPCODE_XOR
:
1808 brw_XOR(p
, dst
, src
[0], src
[1]);
1810 case BRW_OPCODE_NOT
:
1811 brw_NOT(p
, dst
, src
[0]);
1813 case BRW_OPCODE_ASR
:
1814 brw_ASR(p
, dst
, src
[0], src
[1]);
1816 case BRW_OPCODE_SHR
:
1817 brw_SHR(p
, dst
, src
[0], src
[1]);
1819 case BRW_OPCODE_SHL
:
1820 brw_SHL(p
, dst
, src
[0], src
[1]);
1822 case BRW_OPCODE_F32TO16
:
1823 assert(devinfo
->gen
>= 7);
1824 brw_F32TO16(p
, dst
, src
[0]);
1826 case BRW_OPCODE_F16TO32
:
1827 assert(devinfo
->gen
>= 7);
1828 brw_F16TO32(p
, dst
, src
[0]);
1830 case BRW_OPCODE_CMP
:
1831 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says
1832 * that when the destination is a GRF that the dependency-clear bit on
1833 * the flag register is cleared early.
1835 * Suggested workarounds are to disable coissuing CMP instructions
1836 * or to split CMP(16) instructions into two CMP(8) instructions.
1838 * We choose to split into CMP(8) instructions since disabling
1839 * coissuing would affect CMP instructions not otherwise affected by
1842 if (dispatch_width
== 16 && devinfo
->gen
== 7 && !devinfo
->is_haswell
) {
1843 if (dst
.file
== BRW_GENERAL_REGISTER_FILE
) {
1844 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1845 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1846 brw_CMP(p
, firsthalf(dst
), inst
->conditional_mod
,
1847 firsthalf(src
[0]), firsthalf(src
[1]));
1848 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1849 brw_CMP(p
, sechalf(dst
), inst
->conditional_mod
,
1850 sechalf(src
[0]), sechalf(src
[1]));
1851 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1853 multiple_instructions_emitted
= true;
1854 } else if (dst
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
1855 /* For unknown reasons, the aforementioned workaround is not
1856 * sufficient. Overriding the type when the destination is the
1857 * null register is necessary but not sufficient by itself.
1859 assert(dst
.nr
== BRW_ARF_NULL
);
1860 dst
.type
= BRW_REGISTER_TYPE_D
;
1861 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
1863 unreachable("not reached");
1866 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
1869 case BRW_OPCODE_SEL
:
1870 brw_SEL(p
, dst
, src
[0], src
[1]);
1872 case BRW_OPCODE_BFREV
:
1873 assert(devinfo
->gen
>= 7);
1874 /* BFREV only supports UD type for src and dst. */
1875 brw_BFREV(p
, retype(dst
, BRW_REGISTER_TYPE_UD
),
1876 retype(src
[0], BRW_REGISTER_TYPE_UD
));
1878 case BRW_OPCODE_FBH
:
1879 assert(devinfo
->gen
>= 7);
1880 /* FBH only supports UD type for dst. */
1881 brw_FBH(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1883 case BRW_OPCODE_FBL
:
1884 assert(devinfo
->gen
>= 7);
1885 /* FBL only supports UD type for dst. */
1886 brw_FBL(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1888 case BRW_OPCODE_CBIT
:
1889 assert(devinfo
->gen
>= 7);
1890 /* CBIT only supports UD type for dst. */
1891 brw_CBIT(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1893 case BRW_OPCODE_ADDC
:
1894 assert(devinfo
->gen
>= 7);
1895 brw_ADDC(p
, dst
, src
[0], src
[1]);
1897 case BRW_OPCODE_SUBB
:
1898 assert(devinfo
->gen
>= 7);
1899 brw_SUBB(p
, dst
, src
[0], src
[1]);
1901 case BRW_OPCODE_MAC
:
1902 brw_MAC(p
, dst
, src
[0], src
[1]);
1905 case BRW_OPCODE_BFE
:
1906 assert(devinfo
->gen
>= 7);
1907 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
1908 if (dispatch_width
== 16 && !devinfo
->supports_simd16_3src
) {
1909 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1910 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1911 brw_BFE(p
, firsthalf(dst
), firsthalf(src
[0]), firsthalf(src
[1]), firsthalf(src
[2]));
1912 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1913 brw_BFE(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1914 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1916 brw_BFE(p
, dst
, src
[0], src
[1], src
[2]);
1918 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1921 case BRW_OPCODE_BFI1
:
1922 assert(devinfo
->gen
>= 7);
1923 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1926 * "Force BFI instructions to be executed always in SIMD8."
1928 if (dispatch_width
== 16 && devinfo
->is_haswell
) {
1929 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1930 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1931 brw_BFI1(p
, firsthalf(dst
), firsthalf(src
[0]), firsthalf(src
[1]));
1932 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1933 brw_BFI1(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]));
1934 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1936 brw_BFI1(p
, dst
, src
[0], src
[1]);
1939 case BRW_OPCODE_BFI2
:
1940 assert(devinfo
->gen
>= 7);
1941 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
1942 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1945 * "Force BFI instructions to be executed always in SIMD8."
1947 * Otherwise we would be able to emit compressed instructions like we
1948 * do for the other three-source instructions.
1950 if (dispatch_width
== 16 &&
1951 (devinfo
->is_haswell
|| !devinfo
->supports_simd16_3src
)) {
1952 brw_set_default_exec_size(p
, BRW_EXECUTE_8
);
1953 brw_set_default_compression_control(p
, BRW_COMPRESSION_NONE
);
1954 brw_BFI2(p
, firsthalf(dst
), firsthalf(src
[0]), firsthalf(src
[1]), firsthalf(src
[2]));
1955 brw_set_default_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1956 brw_BFI2(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1957 brw_set_default_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1959 brw_BFI2(p
, dst
, src
[0], src
[1], src
[2]);
1961 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
1965 if (inst
->src
[0].file
!= BAD_FILE
) {
1966 /* The instruction has an embedded compare (only allowed on gen6) */
1967 assert(devinfo
->gen
== 6);
1968 gen6_IF(p
, inst
->conditional_mod
, src
[0], src
[1]);
1970 brw_IF(p
, dispatch_width
== 16 ? BRW_EXECUTE_16
: BRW_EXECUTE_8
);
1974 case BRW_OPCODE_ELSE
:
1977 case BRW_OPCODE_ENDIF
:
1982 brw_DO(p
, dispatch_width
== 16 ? BRW_EXECUTE_16
: BRW_EXECUTE_8
);
1985 case BRW_OPCODE_BREAK
:
1987 brw_set_default_predicate_control(p
, BRW_PREDICATE_NONE
);
1989 case BRW_OPCODE_CONTINUE
:
1991 brw_set_default_predicate_control(p
, BRW_PREDICATE_NONE
);
1994 case BRW_OPCODE_WHILE
:
1999 case SHADER_OPCODE_RCP
:
2000 case SHADER_OPCODE_RSQ
:
2001 case SHADER_OPCODE_SQRT
:
2002 case SHADER_OPCODE_EXP2
:
2003 case SHADER_OPCODE_LOG2
:
2004 case SHADER_OPCODE_SIN
:
2005 case SHADER_OPCODE_COS
:
2006 assert(devinfo
->gen
< 6 || inst
->mlen
== 0);
2007 assert(inst
->conditional_mod
== BRW_CONDITIONAL_NONE
);
2008 if (devinfo
->gen
>= 7) {
2009 gen6_math(p
, dst
, brw_math_function(inst
->opcode
), src
[0],
2011 } else if (devinfo
->gen
== 6) {
2012 generate_math_gen6(inst
, dst
, src
[0], brw_null_reg());
2013 } else if (devinfo
->gen
== 5 || devinfo
->is_g4x
) {
2014 generate_math_g45(inst
, dst
, src
[0]);
2016 generate_math_gen4(inst
, dst
, src
[0]);
2019 case SHADER_OPCODE_INT_QUOTIENT
:
2020 case SHADER_OPCODE_INT_REMAINDER
:
2021 case SHADER_OPCODE_POW
:
2022 assert(devinfo
->gen
< 6 || inst
->mlen
== 0);
2023 assert(inst
->conditional_mod
== BRW_CONDITIONAL_NONE
);
2024 if (devinfo
->gen
>= 7 && inst
->opcode
== SHADER_OPCODE_POW
) {
2025 gen6_math(p
, dst
, brw_math_function(inst
->opcode
), src
[0], src
[1]);
2026 } else if (devinfo
->gen
>= 6) {
2027 generate_math_gen6(inst
, dst
, src
[0], src
[1]);
2029 generate_math_gen4(inst
, dst
, src
[0]);
2032 case FS_OPCODE_CINTERP
:
2033 brw_MOV(p
, dst
, src
[0]);
2035 case FS_OPCODE_LINTERP
:
2036 generate_linterp(inst
, dst
, src
);
2038 case FS_OPCODE_PIXEL_X
:
2039 assert(src
[0].type
== BRW_REGISTER_TYPE_UW
);
2040 src
[0].subnr
= 0 * type_sz(src
[0].type
);
2041 brw_MOV(p
, dst
, stride(src
[0], 8, 4, 1));
2043 case FS_OPCODE_PIXEL_Y
:
2044 assert(src
[0].type
== BRW_REGISTER_TYPE_UW
);
2045 src
[0].subnr
= 4 * type_sz(src
[0].type
);
2046 brw_MOV(p
, dst
, stride(src
[0], 8, 4, 1));
2048 case FS_OPCODE_GET_BUFFER_SIZE
:
2049 generate_get_buffer_size(inst
, dst
, src
[0], src
[1]);
2051 case SHADER_OPCODE_TEX
:
2053 case SHADER_OPCODE_TXD
:
2054 case SHADER_OPCODE_TXF
:
2055 case SHADER_OPCODE_TXF_CMS
:
2056 case SHADER_OPCODE_TXF_CMS_W
:
2057 case SHADER_OPCODE_TXF_UMS
:
2058 case SHADER_OPCODE_TXF_MCS
:
2059 case SHADER_OPCODE_TXL
:
2060 case SHADER_OPCODE_TXS
:
2061 case SHADER_OPCODE_LOD
:
2062 case SHADER_OPCODE_TG4
:
2063 case SHADER_OPCODE_TG4_OFFSET
:
2064 case SHADER_OPCODE_SAMPLEINFO
:
2065 generate_tex(inst
, dst
, src
[0], src
[1]);
2067 case FS_OPCODE_DDX_COARSE
:
2068 case FS_OPCODE_DDX_FINE
:
2069 generate_ddx(inst
->opcode
, dst
, src
[0]);
2071 case FS_OPCODE_DDY_COARSE
:
2072 case FS_OPCODE_DDY_FINE
:
2073 assert(src
[1].file
== BRW_IMMEDIATE_VALUE
);
2074 generate_ddy(inst
->opcode
, dst
, src
[0], src
[1].ud
);
2077 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
2078 generate_scratch_write(inst
, src
[0]);
2082 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
2083 generate_scratch_read(inst
, dst
);
2087 case SHADER_OPCODE_GEN7_SCRATCH_READ
:
2088 generate_scratch_read_gen7(inst
, dst
);
2092 case SHADER_OPCODE_MOV_INDIRECT
:
2093 generate_mov_indirect(inst
, dst
, src
[0], src
[1]);
2096 case SHADER_OPCODE_URB_READ_SIMD8
:
2097 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
2098 generate_urb_read(inst
, dst
, src
[0]);
2101 case SHADER_OPCODE_URB_WRITE_SIMD8
:
2102 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
2103 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
2104 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
2105 generate_urb_write(inst
, src
[0]);
2108 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
2109 generate_uniform_pull_constant_load(inst
, dst
, src
[0], src
[1]);
2112 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
2113 generate_uniform_pull_constant_load_gen7(inst
, dst
, src
[0], src
[1]);
2116 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
2117 generate_varying_pull_constant_load(inst
, dst
, src
[0], src
[1]);
2120 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
2121 generate_varying_pull_constant_load_gen7(inst
, dst
, src
[0], src
[1]);
2124 case FS_OPCODE_REP_FB_WRITE
:
2125 case FS_OPCODE_FB_WRITE
:
2126 generate_fb_write(inst
, src
[0]);
2129 case FS_OPCODE_BLORP_FB_WRITE
:
2130 generate_blorp_fb_write(inst
);
2133 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS
:
2134 generate_mov_dispatch_to_flags(inst
);
2137 case FS_OPCODE_DISCARD_JUMP
:
2138 generate_discard_jump(inst
);
2141 case SHADER_OPCODE_SHADER_TIME_ADD
:
2142 generate_shader_time_add(inst
, src
[0], src
[1], src
[2]);
2145 case SHADER_OPCODE_UNTYPED_ATOMIC
:
2146 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2147 brw_untyped_atomic(p
, dst
, src
[0], src
[1], src
[2].ud
,
2148 inst
->mlen
, !inst
->dst
.is_null());
2151 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
2152 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2153 brw_untyped_surface_read(p
, dst
, src
[0], src
[1],
2154 inst
->mlen
, src
[2].ud
);
2157 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
2158 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2159 brw_untyped_surface_write(p
, src
[0], src
[1],
2160 inst
->mlen
, src
[2].ud
);
2163 case SHADER_OPCODE_TYPED_ATOMIC
:
2164 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2165 brw_typed_atomic(p
, dst
, src
[0], src
[1],
2166 src
[2].ud
, inst
->mlen
, !inst
->dst
.is_null());
2169 case SHADER_OPCODE_TYPED_SURFACE_READ
:
2170 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2171 brw_typed_surface_read(p
, dst
, src
[0], src
[1],
2172 inst
->mlen
, src
[2].ud
);
2175 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
2176 assert(src
[2].file
== BRW_IMMEDIATE_VALUE
);
2177 brw_typed_surface_write(p
, src
[0], src
[1], inst
->mlen
, src
[2].ud
);
2180 case SHADER_OPCODE_MEMORY_FENCE
:
2181 brw_memory_fence(p
, dst
);
2184 case FS_OPCODE_SET_SIMD4X2_OFFSET
:
2185 generate_set_simd4x2_offset(inst
, dst
, src
[0]);
2188 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2189 brw_find_live_channel(p
, dst
);
2192 case SHADER_OPCODE_BROADCAST
:
2193 brw_broadcast(p
, dst
, src
[0], src
[1]);
2196 case FS_OPCODE_SET_SAMPLE_ID
:
2197 generate_set_sample_id(inst
, dst
, src
[0], src
[1]);
2200 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
2201 generate_pack_half_2x16_split(inst
, dst
, src
[0], src
[1]);
2204 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
:
2205 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
:
2206 generate_unpack_half_2x16_split(inst
, dst
, src
[0]);
2209 case FS_OPCODE_PLACEHOLDER_HALT
:
2210 /* This is the place where the final HALT needs to be inserted if
2211 * we've emitted any discards. If not, this will emit no code.
2213 if (!patch_discard_jumps_to_fb_writes()) {
2214 if (unlikely(debug_flag
)) {
2215 annotation
.ann_count
--;
2220 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
2221 generate_pixel_interpolator_query(inst
, dst
, src
[0], src
[1],
2222 GEN7_PIXEL_INTERPOLATOR_LOC_CENTROID
);
2225 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
2226 generate_pixel_interpolator_query(inst
, dst
, src
[0], src
[1],
2227 GEN7_PIXEL_INTERPOLATOR_LOC_SAMPLE
);
2230 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
2231 generate_pixel_interpolator_query(inst
, dst
, src
[0], src
[1],
2232 GEN7_PIXEL_INTERPOLATOR_LOC_SHARED_OFFSET
);
2235 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
2236 generate_pixel_interpolator_query(inst
, dst
, src
[0], src
[1],
2237 GEN7_PIXEL_INTERPOLATOR_LOC_PER_SLOT_OFFSET
);
2240 case CS_OPCODE_CS_TERMINATE
:
2241 generate_cs_terminate(inst
, src
[0]);
2244 case SHADER_OPCODE_BARRIER
:
2245 generate_barrier(inst
, src
[0]);
2248 case FS_OPCODE_PACK_STENCIL_REF
:
2249 generate_stencil_ref_packing(inst
, dst
, src
[0]);
2253 unreachable("Unsupported opcode");
2255 case SHADER_OPCODE_LOAD_PAYLOAD
:
2256 unreachable("Should be lowered by lower_load_payload()");
2259 if (multiple_instructions_emitted
)
2262 if (inst
->no_dd_clear
|| inst
->no_dd_check
|| inst
->conditional_mod
) {
2263 assert(p
->next_insn_offset
== last_insn_offset
+ 16 ||
2264 !"conditional_mod, no_dd_check, or no_dd_clear set for IR "
2265 "emitting more than 1 instruction");
2267 brw_inst
*last
= &p
->store
[last_insn_offset
/ 16];
2269 if (inst
->conditional_mod
)
2270 brw_inst_set_cond_modifier(p
->devinfo
, last
, inst
->conditional_mod
);
2271 brw_inst_set_no_dd_clear(p
->devinfo
, last
, inst
->no_dd_clear
);
2272 brw_inst_set_no_dd_check(p
->devinfo
, last
, inst
->no_dd_check
);
2277 annotation_finalize(&annotation
, p
->next_insn_offset
);
2280 bool validated
= brw_validate_instructions(p
, start_offset
, &annotation
);
2282 if (unlikely(debug_flag
))
2283 brw_validate_instructions(p
, start_offset
, &annotation
);
2286 int before_size
= p
->next_insn_offset
- start_offset
;
2287 brw_compact_instructions(p
, start_offset
, annotation
.ann_count
,
2289 int after_size
= p
->next_insn_offset
- start_offset
;
2291 if (unlikely(debug_flag
)) {
2292 fprintf(stderr
, "Native code for %s\n"
2293 "SIMD%d shader: %d instructions. %d loops. %u cycles. %d:%d spills:fills. Promoted %u constants. Compacted %d to %d"
2294 " bytes (%.0f%%)\n",
2295 shader_name
, dispatch_width
, before_size
/ 16, loop_count
, cfg
->cycle_count
,
2296 spill_count
, fill_count
, promoted_constants
, before_size
, after_size
,
2297 100.0f
* (before_size
- after_size
) / before_size
);
2299 dump_assembly(p
->store
, annotation
.ann_count
, annotation
.ann
,
2301 ralloc_free(annotation
.mem_ctx
);
2305 compiler
->shader_debug_log(log_data
,
2306 "%s SIMD%d shader: %d inst, %d loops, %u cycles, "
2307 "%d:%d spills:fills, Promoted %u constants, "
2308 "compacted %d to %d bytes.\n",
2309 stage_abbrev
, dispatch_width
, before_size
/ 16,
2310 loop_count
, cfg
->cycle_count
, spill_count
,
2311 fill_count
, promoted_constants
, before_size
,
2314 return start_offset
;
2318 fs_generator::get_assembly(unsigned int *assembly_size
)
2320 return brw_get_program(p
, assembly_size
);