i965: Add functions to abstract access to 3src register types
[mesa.git] / src / intel / compiler / brw_eu_emit.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32
33 #include "brw_eu_defines.h"
34 #include "brw_eu.h"
35
36 #include "util/ralloc.h"
37
38 /**
39 * Prior to Sandybridge, the SEND instruction accepted non-MRF source
40 * registers, implicitly moving the operand to a message register.
41 *
42 * On Sandybridge, this is no longer the case. This function performs the
43 * explicit move; it should be called before emitting a SEND instruction.
44 */
45 void
46 gen6_resolve_implied_move(struct brw_codegen *p,
47 struct brw_reg *src,
48 unsigned msg_reg_nr)
49 {
50 const struct gen_device_info *devinfo = p->devinfo;
51 if (devinfo->gen < 6)
52 return;
53
54 if (src->file == BRW_MESSAGE_REGISTER_FILE)
55 return;
56
57 if (src->file != BRW_ARCHITECTURE_REGISTER_FILE || src->nr != BRW_ARF_NULL) {
58 brw_push_insn_state(p);
59 brw_set_default_exec_size(p, BRW_EXECUTE_8);
60 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
61 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
62 brw_MOV(p, retype(brw_message_reg(msg_reg_nr), BRW_REGISTER_TYPE_UD),
63 retype(*src, BRW_REGISTER_TYPE_UD));
64 brw_pop_insn_state(p);
65 }
66 *src = brw_message_reg(msg_reg_nr);
67 }
68
69 static void
70 gen7_convert_mrf_to_grf(struct brw_codegen *p, struct brw_reg *reg)
71 {
72 /* From the Ivybridge PRM, Volume 4 Part 3, page 218 ("send"):
73 * "The send with EOT should use register space R112-R127 for <src>. This is
74 * to enable loading of a new thread into the same slot while the message
75 * with EOT for current thread is pending dispatch."
76 *
77 * Since we're pretending to have 16 MRFs anyway, we may as well use the
78 * registers required for messages with EOT.
79 */
80 const struct gen_device_info *devinfo = p->devinfo;
81 if (devinfo->gen >= 7 && reg->file == BRW_MESSAGE_REGISTER_FILE) {
82 reg->file = BRW_GENERAL_REGISTER_FILE;
83 reg->nr += GEN7_MRF_HACK_START;
84 }
85 }
86
87 void
88 brw_set_dest(struct brw_codegen *p, brw_inst *inst, struct brw_reg dest)
89 {
90 const struct gen_device_info *devinfo = p->devinfo;
91
92 if (dest.file == BRW_MESSAGE_REGISTER_FILE)
93 assert((dest.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
94 else if (dest.file != BRW_ARCHITECTURE_REGISTER_FILE)
95 assert(dest.nr < 128);
96
97 gen7_convert_mrf_to_grf(p, &dest);
98
99 brw_inst_set_dst_file_type(devinfo, inst, dest.file, dest.type);
100 brw_inst_set_dst_address_mode(devinfo, inst, dest.address_mode);
101
102 if (dest.address_mode == BRW_ADDRESS_DIRECT) {
103 brw_inst_set_dst_da_reg_nr(devinfo, inst, dest.nr);
104
105 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
106 brw_inst_set_dst_da1_subreg_nr(devinfo, inst, dest.subnr);
107 if (dest.hstride == BRW_HORIZONTAL_STRIDE_0)
108 dest.hstride = BRW_HORIZONTAL_STRIDE_1;
109 brw_inst_set_dst_hstride(devinfo, inst, dest.hstride);
110 } else {
111 brw_inst_set_dst_da16_subreg_nr(devinfo, inst, dest.subnr / 16);
112 brw_inst_set_da16_writemask(devinfo, inst, dest.writemask);
113 if (dest.file == BRW_GENERAL_REGISTER_FILE ||
114 dest.file == BRW_MESSAGE_REGISTER_FILE) {
115 assert(dest.writemask != 0);
116 }
117 /* From the Ivybridge PRM, Vol 4, Part 3, Section 5.2.4.1:
118 * Although Dst.HorzStride is a don't care for Align16, HW needs
119 * this to be programmed as "01".
120 */
121 brw_inst_set_dst_hstride(devinfo, inst, 1);
122 }
123 } else {
124 brw_inst_set_dst_ia_subreg_nr(devinfo, inst, dest.subnr);
125
126 /* These are different sizes in align1 vs align16:
127 */
128 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
129 brw_inst_set_dst_ia1_addr_imm(devinfo, inst,
130 dest.indirect_offset);
131 if (dest.hstride == BRW_HORIZONTAL_STRIDE_0)
132 dest.hstride = BRW_HORIZONTAL_STRIDE_1;
133 brw_inst_set_dst_hstride(devinfo, inst, dest.hstride);
134 } else {
135 brw_inst_set_dst_ia16_addr_imm(devinfo, inst,
136 dest.indirect_offset);
137 /* even ignored in da16, still need to set as '01' */
138 brw_inst_set_dst_hstride(devinfo, inst, 1);
139 }
140 }
141
142 /* Generators should set a default exec_size of either 8 (SIMD4x2 or SIMD8)
143 * or 16 (SIMD16), as that's normally correct. However, when dealing with
144 * small registers, we automatically reduce it to match the register size.
145 *
146 * In platforms that support fp64 we can emit instructions with a width of
147 * 4 that need two SIMD8 registers and an exec_size of 8 or 16. In these
148 * cases we need to make sure that these instructions have their exec sizes
149 * set properly when they are emitted and we can't rely on this code to fix
150 * it.
151 */
152 bool fix_exec_size;
153 if (devinfo->gen >= 6)
154 fix_exec_size = dest.width < BRW_EXECUTE_4;
155 else
156 fix_exec_size = dest.width < BRW_EXECUTE_8;
157
158 if (fix_exec_size)
159 brw_inst_set_exec_size(devinfo, inst, dest.width);
160 }
161
162 void
163 brw_set_src0(struct brw_codegen *p, brw_inst *inst, struct brw_reg reg)
164 {
165 const struct gen_device_info *devinfo = p->devinfo;
166
167 if (reg.file == BRW_MESSAGE_REGISTER_FILE)
168 assert((reg.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
169 else if (reg.file != BRW_ARCHITECTURE_REGISTER_FILE)
170 assert(reg.nr < 128);
171
172 gen7_convert_mrf_to_grf(p, &reg);
173
174 if (devinfo->gen >= 6 && (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND ||
175 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SENDC)) {
176 /* Any source modifiers or regions will be ignored, since this just
177 * identifies the MRF/GRF to start reading the message contents from.
178 * Check for some likely failures.
179 */
180 assert(!reg.negate);
181 assert(!reg.abs);
182 assert(reg.address_mode == BRW_ADDRESS_DIRECT);
183 }
184
185 brw_inst_set_src0_file_type(devinfo, inst, reg.file, reg.type);
186 brw_inst_set_src0_abs(devinfo, inst, reg.abs);
187 brw_inst_set_src0_negate(devinfo, inst, reg.negate);
188 brw_inst_set_src0_address_mode(devinfo, inst, reg.address_mode);
189
190 if (reg.file == BRW_IMMEDIATE_VALUE) {
191 if (reg.type == BRW_REGISTER_TYPE_DF ||
192 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_DIM)
193 brw_inst_set_imm_df(devinfo, inst, reg.df);
194 else if (reg.type == BRW_REGISTER_TYPE_UQ ||
195 reg.type == BRW_REGISTER_TYPE_Q)
196 brw_inst_set_imm_uq(devinfo, inst, reg.u64);
197 else
198 brw_inst_set_imm_ud(devinfo, inst, reg.ud);
199
200 if (type_sz(reg.type) < 8) {
201 brw_inst_set_src1_reg_file(devinfo, inst,
202 BRW_ARCHITECTURE_REGISTER_FILE);
203 brw_inst_set_src1_reg_hw_type(devinfo, inst,
204 brw_inst_src0_reg_hw_type(devinfo, inst));
205 }
206 } else {
207 if (reg.address_mode == BRW_ADDRESS_DIRECT) {
208 brw_inst_set_src0_da_reg_nr(devinfo, inst, reg.nr);
209 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
210 brw_inst_set_src0_da1_subreg_nr(devinfo, inst, reg.subnr);
211 } else {
212 brw_inst_set_src0_da16_subreg_nr(devinfo, inst, reg.subnr / 16);
213 }
214 } else {
215 brw_inst_set_src0_ia_subreg_nr(devinfo, inst, reg.subnr);
216
217 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
218 brw_inst_set_src0_ia1_addr_imm(devinfo, inst, reg.indirect_offset);
219 } else {
220 brw_inst_set_src0_ia16_addr_imm(devinfo, inst, reg.indirect_offset);
221 }
222 }
223
224 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
225 if (reg.width == BRW_WIDTH_1 &&
226 brw_inst_exec_size(devinfo, inst) == BRW_EXECUTE_1) {
227 brw_inst_set_src0_hstride(devinfo, inst, BRW_HORIZONTAL_STRIDE_0);
228 brw_inst_set_src0_width(devinfo, inst, BRW_WIDTH_1);
229 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_0);
230 } else {
231 brw_inst_set_src0_hstride(devinfo, inst, reg.hstride);
232 brw_inst_set_src0_width(devinfo, inst, reg.width);
233 brw_inst_set_src0_vstride(devinfo, inst, reg.vstride);
234 }
235 } else {
236 brw_inst_set_src0_da16_swiz_x(devinfo, inst,
237 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_X));
238 brw_inst_set_src0_da16_swiz_y(devinfo, inst,
239 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Y));
240 brw_inst_set_src0_da16_swiz_z(devinfo, inst,
241 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Z));
242 brw_inst_set_src0_da16_swiz_w(devinfo, inst,
243 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_W));
244
245 if (reg.vstride == BRW_VERTICAL_STRIDE_8) {
246 /* This is an oddity of the fact we're using the same
247 * descriptions for registers in align_16 as align_1:
248 */
249 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
250 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
251 reg.type == BRW_REGISTER_TYPE_DF &&
252 reg.vstride == BRW_VERTICAL_STRIDE_2) {
253 /* From SNB PRM:
254 *
255 * "For Align16 access mode, only encodings of 0000 and 0011
256 * are allowed. Other codes are reserved."
257 *
258 * Presumably the DevSNB behavior applies to IVB as well.
259 */
260 brw_inst_set_src0_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
261 } else {
262 brw_inst_set_src0_vstride(devinfo, inst, reg.vstride);
263 }
264 }
265 }
266 }
267
268
269 void
270 brw_set_src1(struct brw_codegen *p, brw_inst *inst, struct brw_reg reg)
271 {
272 const struct gen_device_info *devinfo = p->devinfo;
273
274 if (reg.file != BRW_ARCHITECTURE_REGISTER_FILE)
275 assert(reg.nr < 128);
276
277 /* From the IVB PRM Vol. 4, Pt. 3, Section 3.3.3.5:
278 *
279 * "Accumulator registers may be accessed explicitly as src0
280 * operands only."
281 */
282 assert(reg.file != BRW_ARCHITECTURE_REGISTER_FILE ||
283 reg.nr != BRW_ARF_ACCUMULATOR);
284
285 gen7_convert_mrf_to_grf(p, &reg);
286 assert(reg.file != BRW_MESSAGE_REGISTER_FILE);
287
288 brw_inst_set_src1_file_type(devinfo, inst, reg.file, reg.type);
289 brw_inst_set_src1_abs(devinfo, inst, reg.abs);
290 brw_inst_set_src1_negate(devinfo, inst, reg.negate);
291
292 /* Only src1 can be immediate in two-argument instructions.
293 */
294 assert(brw_inst_src0_reg_file(devinfo, inst) != BRW_IMMEDIATE_VALUE);
295
296 if (reg.file == BRW_IMMEDIATE_VALUE) {
297 /* two-argument instructions can only use 32-bit immediates */
298 assert(type_sz(reg.type) < 8);
299 brw_inst_set_imm_ud(devinfo, inst, reg.ud);
300 } else {
301 /* This is a hardware restriction, which may or may not be lifted
302 * in the future:
303 */
304 assert (reg.address_mode == BRW_ADDRESS_DIRECT);
305 /* assert (reg.file == BRW_GENERAL_REGISTER_FILE); */
306
307 brw_inst_set_src1_da_reg_nr(devinfo, inst, reg.nr);
308 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
309 brw_inst_set_src1_da1_subreg_nr(devinfo, inst, reg.subnr);
310 } else {
311 brw_inst_set_src1_da16_subreg_nr(devinfo, inst, reg.subnr / 16);
312 }
313
314 if (brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_1) {
315 if (reg.width == BRW_WIDTH_1 &&
316 brw_inst_exec_size(devinfo, inst) == BRW_EXECUTE_1) {
317 brw_inst_set_src1_hstride(devinfo, inst, BRW_HORIZONTAL_STRIDE_0);
318 brw_inst_set_src1_width(devinfo, inst, BRW_WIDTH_1);
319 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_0);
320 } else {
321 brw_inst_set_src1_hstride(devinfo, inst, reg.hstride);
322 brw_inst_set_src1_width(devinfo, inst, reg.width);
323 brw_inst_set_src1_vstride(devinfo, inst, reg.vstride);
324 }
325 } else {
326 brw_inst_set_src1_da16_swiz_x(devinfo, inst,
327 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_X));
328 brw_inst_set_src1_da16_swiz_y(devinfo, inst,
329 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Y));
330 brw_inst_set_src1_da16_swiz_z(devinfo, inst,
331 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_Z));
332 brw_inst_set_src1_da16_swiz_w(devinfo, inst,
333 BRW_GET_SWZ(reg.swizzle, BRW_CHANNEL_W));
334
335 if (reg.vstride == BRW_VERTICAL_STRIDE_8) {
336 /* This is an oddity of the fact we're using the same
337 * descriptions for registers in align_16 as align_1:
338 */
339 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
340 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
341 reg.type == BRW_REGISTER_TYPE_DF &&
342 reg.vstride == BRW_VERTICAL_STRIDE_2) {
343 /* From SNB PRM:
344 *
345 * "For Align16 access mode, only encodings of 0000 and 0011
346 * are allowed. Other codes are reserved."
347 *
348 * Presumably the DevSNB behavior applies to IVB as well.
349 */
350 brw_inst_set_src1_vstride(devinfo, inst, BRW_VERTICAL_STRIDE_4);
351 } else {
352 brw_inst_set_src1_vstride(devinfo, inst, reg.vstride);
353 }
354 }
355 }
356 }
357
358 /**
359 * Set the Message Descriptor and Extended Message Descriptor fields
360 * for SEND messages.
361 *
362 * \note This zeroes out the Function Control bits, so it must be called
363 * \b before filling out any message-specific data. Callers can
364 * choose not to fill in irrelevant bits; they will be zero.
365 */
366 void
367 brw_set_message_descriptor(struct brw_codegen *p,
368 brw_inst *inst,
369 enum brw_message_target sfid,
370 unsigned msg_length,
371 unsigned response_length,
372 bool header_present,
373 bool end_of_thread)
374 {
375 const struct gen_device_info *devinfo = p->devinfo;
376
377 brw_set_src1(p, inst, brw_imm_d(0));
378
379 /* For indirect sends, `inst` will not be the SEND/SENDC instruction
380 * itself; instead, it will be a MOV/OR into the address register.
381 *
382 * In this case, we avoid setting the extended message descriptor bits,
383 * since they go on the later SEND/SENDC instead and if set here would
384 * instead clobber the conditionalmod bits.
385 */
386 unsigned opcode = brw_inst_opcode(devinfo, inst);
387 if (opcode == BRW_OPCODE_SEND || opcode == BRW_OPCODE_SENDC) {
388 brw_inst_set_sfid(devinfo, inst, sfid);
389 }
390
391 brw_inst_set_mlen(devinfo, inst, msg_length);
392 brw_inst_set_rlen(devinfo, inst, response_length);
393 brw_inst_set_eot(devinfo, inst, end_of_thread);
394
395 if (devinfo->gen >= 5) {
396 brw_inst_set_header_present(devinfo, inst, header_present);
397 }
398 }
399
400 static void brw_set_math_message( struct brw_codegen *p,
401 brw_inst *inst,
402 unsigned function,
403 unsigned integer_type,
404 bool low_precision,
405 unsigned dataType )
406 {
407 const struct gen_device_info *devinfo = p->devinfo;
408 unsigned msg_length;
409 unsigned response_length;
410
411 /* Infer message length from the function */
412 switch (function) {
413 case BRW_MATH_FUNCTION_POW:
414 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT:
415 case BRW_MATH_FUNCTION_INT_DIV_REMAINDER:
416 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
417 msg_length = 2;
418 break;
419 default:
420 msg_length = 1;
421 break;
422 }
423
424 /* Infer response length from the function */
425 switch (function) {
426 case BRW_MATH_FUNCTION_SINCOS:
427 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
428 response_length = 2;
429 break;
430 default:
431 response_length = 1;
432 break;
433 }
434
435
436 brw_set_message_descriptor(p, inst, BRW_SFID_MATH,
437 msg_length, response_length, false, false);
438 brw_inst_set_math_msg_function(devinfo, inst, function);
439 brw_inst_set_math_msg_signed_int(devinfo, inst, integer_type);
440 brw_inst_set_math_msg_precision(devinfo, inst, low_precision);
441 brw_inst_set_math_msg_saturate(devinfo, inst, brw_inst_saturate(devinfo, inst));
442 brw_inst_set_math_msg_data_type(devinfo, inst, dataType);
443 brw_inst_set_saturate(devinfo, inst, 0);
444 }
445
446
447 static void brw_set_ff_sync_message(struct brw_codegen *p,
448 brw_inst *insn,
449 bool allocate,
450 unsigned response_length,
451 bool end_of_thread)
452 {
453 const struct gen_device_info *devinfo = p->devinfo;
454
455 brw_set_message_descriptor(p, insn, BRW_SFID_URB,
456 1, response_length, true, end_of_thread);
457 brw_inst_set_urb_opcode(devinfo, insn, 1); /* FF_SYNC */
458 brw_inst_set_urb_allocate(devinfo, insn, allocate);
459 /* The following fields are not used by FF_SYNC: */
460 brw_inst_set_urb_global_offset(devinfo, insn, 0);
461 brw_inst_set_urb_swizzle_control(devinfo, insn, 0);
462 brw_inst_set_urb_used(devinfo, insn, 0);
463 brw_inst_set_urb_complete(devinfo, insn, 0);
464 }
465
466 static void brw_set_urb_message( struct brw_codegen *p,
467 brw_inst *insn,
468 enum brw_urb_write_flags flags,
469 unsigned msg_length,
470 unsigned response_length,
471 unsigned offset,
472 unsigned swizzle_control )
473 {
474 const struct gen_device_info *devinfo = p->devinfo;
475
476 assert(devinfo->gen < 7 || swizzle_control != BRW_URB_SWIZZLE_TRANSPOSE);
477 assert(devinfo->gen < 7 || !(flags & BRW_URB_WRITE_ALLOCATE));
478 assert(devinfo->gen >= 7 || !(flags & BRW_URB_WRITE_PER_SLOT_OFFSET));
479
480 brw_set_message_descriptor(p, insn, BRW_SFID_URB,
481 msg_length, response_length, true,
482 flags & BRW_URB_WRITE_EOT);
483
484 if (flags & BRW_URB_WRITE_OWORD) {
485 assert(msg_length == 2); /* header + one OWORD of data */
486 brw_inst_set_urb_opcode(devinfo, insn, BRW_URB_OPCODE_WRITE_OWORD);
487 } else {
488 brw_inst_set_urb_opcode(devinfo, insn, BRW_URB_OPCODE_WRITE_HWORD);
489 }
490
491 brw_inst_set_urb_global_offset(devinfo, insn, offset);
492 brw_inst_set_urb_swizzle_control(devinfo, insn, swizzle_control);
493
494 if (devinfo->gen < 8) {
495 brw_inst_set_urb_complete(devinfo, insn, !!(flags & BRW_URB_WRITE_COMPLETE));
496 }
497
498 if (devinfo->gen < 7) {
499 brw_inst_set_urb_allocate(devinfo, insn, !!(flags & BRW_URB_WRITE_ALLOCATE));
500 brw_inst_set_urb_used(devinfo, insn, !(flags & BRW_URB_WRITE_UNUSED));
501 } else {
502 brw_inst_set_urb_per_slot_offset(devinfo, insn,
503 !!(flags & BRW_URB_WRITE_PER_SLOT_OFFSET));
504 }
505 }
506
507 void
508 brw_set_dp_write_message(struct brw_codegen *p,
509 brw_inst *insn,
510 unsigned binding_table_index,
511 unsigned msg_control,
512 unsigned msg_type,
513 unsigned target_cache,
514 unsigned msg_length,
515 bool header_present,
516 unsigned last_render_target,
517 unsigned response_length,
518 unsigned end_of_thread,
519 unsigned send_commit_msg)
520 {
521 const struct gen_device_info *devinfo = p->devinfo;
522 const unsigned sfid = (devinfo->gen >= 6 ? target_cache :
523 BRW_SFID_DATAPORT_WRITE);
524
525 brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
526 header_present, end_of_thread);
527
528 brw_inst_set_binding_table_index(devinfo, insn, binding_table_index);
529 brw_inst_set_dp_write_msg_type(devinfo, insn, msg_type);
530 brw_inst_set_dp_write_msg_control(devinfo, insn, msg_control);
531 brw_inst_set_rt_last(devinfo, insn, last_render_target);
532 if (devinfo->gen < 7) {
533 brw_inst_set_dp_write_commit(devinfo, insn, send_commit_msg);
534 }
535 }
536
537 void
538 brw_set_dp_read_message(struct brw_codegen *p,
539 brw_inst *insn,
540 unsigned binding_table_index,
541 unsigned msg_control,
542 unsigned msg_type,
543 unsigned target_cache,
544 unsigned msg_length,
545 bool header_present,
546 unsigned response_length)
547 {
548 const struct gen_device_info *devinfo = p->devinfo;
549 const unsigned sfid = (devinfo->gen >= 6 ? target_cache :
550 BRW_SFID_DATAPORT_READ);
551
552 brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
553 header_present, false);
554
555 brw_inst_set_binding_table_index(devinfo, insn, binding_table_index);
556 brw_inst_set_dp_read_msg_type(devinfo, insn, msg_type);
557 brw_inst_set_dp_read_msg_control(devinfo, insn, msg_control);
558 if (devinfo->gen < 6)
559 brw_inst_set_dp_read_target_cache(devinfo, insn, target_cache);
560 }
561
562 void
563 brw_set_sampler_message(struct brw_codegen *p,
564 brw_inst *inst,
565 unsigned binding_table_index,
566 unsigned sampler,
567 unsigned msg_type,
568 unsigned response_length,
569 unsigned msg_length,
570 unsigned header_present,
571 unsigned simd_mode,
572 unsigned return_format)
573 {
574 const struct gen_device_info *devinfo = p->devinfo;
575
576 brw_set_message_descriptor(p, inst, BRW_SFID_SAMPLER, msg_length,
577 response_length, header_present, false);
578
579 brw_inst_set_binding_table_index(devinfo, inst, binding_table_index);
580 brw_inst_set_sampler(devinfo, inst, sampler);
581 brw_inst_set_sampler_msg_type(devinfo, inst, msg_type);
582 if (devinfo->gen >= 5) {
583 brw_inst_set_sampler_simd_mode(devinfo, inst, simd_mode);
584 } else if (devinfo->gen == 4 && !devinfo->is_g4x) {
585 brw_inst_set_sampler_return_format(devinfo, inst, return_format);
586 }
587 }
588
589 static void
590 gen7_set_dp_scratch_message(struct brw_codegen *p,
591 brw_inst *inst,
592 bool write,
593 bool dword,
594 bool invalidate_after_read,
595 unsigned num_regs,
596 unsigned addr_offset,
597 unsigned mlen,
598 unsigned rlen,
599 bool header_present)
600 {
601 const struct gen_device_info *devinfo = p->devinfo;
602 assert(num_regs == 1 || num_regs == 2 || num_regs == 4 ||
603 (devinfo->gen >= 8 && num_regs == 8));
604 const unsigned block_size = (devinfo->gen >= 8 ? _mesa_logbase2(num_regs) :
605 num_regs - 1);
606
607 brw_set_message_descriptor(p, inst, GEN7_SFID_DATAPORT_DATA_CACHE,
608 mlen, rlen, header_present, false);
609 brw_inst_set_dp_category(devinfo, inst, 1); /* Scratch Block Read/Write msgs */
610 brw_inst_set_scratch_read_write(devinfo, inst, write);
611 brw_inst_set_scratch_type(devinfo, inst, dword);
612 brw_inst_set_scratch_invalidate_after_read(devinfo, inst, invalidate_after_read);
613 brw_inst_set_scratch_block_size(devinfo, inst, block_size);
614 brw_inst_set_scratch_addr_offset(devinfo, inst, addr_offset);
615 }
616
617 #define next_insn brw_next_insn
618 brw_inst *
619 brw_next_insn(struct brw_codegen *p, unsigned opcode)
620 {
621 const struct gen_device_info *devinfo = p->devinfo;
622 brw_inst *insn;
623
624 if (p->nr_insn + 1 > p->store_size) {
625 p->store_size <<= 1;
626 p->store = reralloc(p->mem_ctx, p->store, brw_inst, p->store_size);
627 }
628
629 p->next_insn_offset += 16;
630 insn = &p->store[p->nr_insn++];
631 memcpy(insn, p->current, sizeof(*insn));
632
633 brw_inst_set_opcode(devinfo, insn, opcode);
634 return insn;
635 }
636
637 static brw_inst *
638 brw_alu1(struct brw_codegen *p, unsigned opcode,
639 struct brw_reg dest, struct brw_reg src)
640 {
641 brw_inst *insn = next_insn(p, opcode);
642 brw_set_dest(p, insn, dest);
643 brw_set_src0(p, insn, src);
644 return insn;
645 }
646
647 static brw_inst *
648 brw_alu2(struct brw_codegen *p, unsigned opcode,
649 struct brw_reg dest, struct brw_reg src0, struct brw_reg src1)
650 {
651 /* 64-bit immediates are only supported on 1-src instructions */
652 assert(src0.file != BRW_IMMEDIATE_VALUE || type_sz(src0.type) <= 4);
653 assert(src1.file != BRW_IMMEDIATE_VALUE || type_sz(src1.type) <= 4);
654
655 brw_inst *insn = next_insn(p, opcode);
656 brw_set_dest(p, insn, dest);
657 brw_set_src0(p, insn, src0);
658 brw_set_src1(p, insn, src1);
659 return insn;
660 }
661
662 static int
663 get_3src_subreg_nr(struct brw_reg reg)
664 {
665 /* Normally, SubRegNum is in bytes (0..31). However, 3-src instructions
666 * use 32-bit units (components 0..7). Since they only support F/D/UD
667 * types, this doesn't lose any flexibility, but uses fewer bits.
668 */
669 return reg.subnr / 4;
670 }
671
672 static brw_inst *
673 brw_alu3(struct brw_codegen *p, unsigned opcode, struct brw_reg dest,
674 struct brw_reg src0, struct brw_reg src1, struct brw_reg src2)
675 {
676 const struct gen_device_info *devinfo = p->devinfo;
677 brw_inst *inst = next_insn(p, opcode);
678
679 gen7_convert_mrf_to_grf(p, &dest);
680
681 assert(brw_inst_access_mode(devinfo, inst) == BRW_ALIGN_16);
682
683 assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
684 dest.file == BRW_MESSAGE_REGISTER_FILE);
685 assert(dest.nr < 128);
686 assert(dest.address_mode == BRW_ADDRESS_DIRECT);
687 assert(dest.type == BRW_REGISTER_TYPE_F ||
688 dest.type == BRW_REGISTER_TYPE_DF ||
689 dest.type == BRW_REGISTER_TYPE_D ||
690 dest.type == BRW_REGISTER_TYPE_UD);
691 if (devinfo->gen == 6) {
692 brw_inst_set_3src_a16_dst_reg_file(devinfo, inst,
693 dest.file == BRW_MESSAGE_REGISTER_FILE);
694 }
695 brw_inst_set_3src_dst_reg_nr(devinfo, inst, dest.nr);
696 brw_inst_set_3src_a16_dst_subreg_nr(devinfo, inst, dest.subnr / 16);
697 brw_inst_set_3src_a16_dst_writemask(devinfo, inst, dest.writemask);
698
699 assert(src0.file == BRW_GENERAL_REGISTER_FILE);
700 assert(src0.address_mode == BRW_ADDRESS_DIRECT);
701 assert(src0.nr < 128);
702 brw_inst_set_3src_a16_src0_swizzle(devinfo, inst, src0.swizzle);
703 brw_inst_set_3src_a16_src0_subreg_nr(devinfo, inst, get_3src_subreg_nr(src0));
704 brw_inst_set_3src_src0_reg_nr(devinfo, inst, src0.nr);
705 brw_inst_set_3src_src0_abs(devinfo, inst, src0.abs);
706 brw_inst_set_3src_src0_negate(devinfo, inst, src0.negate);
707 brw_inst_set_3src_a16_src0_rep_ctrl(devinfo, inst,
708 src0.vstride == BRW_VERTICAL_STRIDE_0);
709
710 assert(src1.file == BRW_GENERAL_REGISTER_FILE);
711 assert(src1.address_mode == BRW_ADDRESS_DIRECT);
712 assert(src1.nr < 128);
713 brw_inst_set_3src_a16_src1_swizzle(devinfo, inst, src1.swizzle);
714 brw_inst_set_3src_a16_src1_subreg_nr(devinfo, inst, get_3src_subreg_nr(src1));
715 brw_inst_set_3src_src1_reg_nr(devinfo, inst, src1.nr);
716 brw_inst_set_3src_src1_abs(devinfo, inst, src1.abs);
717 brw_inst_set_3src_src1_negate(devinfo, inst, src1.negate);
718 brw_inst_set_3src_a16_src1_rep_ctrl(devinfo, inst,
719 src1.vstride == BRW_VERTICAL_STRIDE_0);
720
721 assert(src2.file == BRW_GENERAL_REGISTER_FILE);
722 assert(src2.address_mode == BRW_ADDRESS_DIRECT);
723 assert(src2.nr < 128);
724 brw_inst_set_3src_a16_src2_swizzle(devinfo, inst, src2.swizzle);
725 brw_inst_set_3src_a16_src2_subreg_nr(devinfo, inst, get_3src_subreg_nr(src2));
726 brw_inst_set_3src_src2_reg_nr(devinfo, inst, src2.nr);
727 brw_inst_set_3src_src2_abs(devinfo, inst, src2.abs);
728 brw_inst_set_3src_src2_negate(devinfo, inst, src2.negate);
729 brw_inst_set_3src_a16_src2_rep_ctrl(devinfo, inst,
730 src2.vstride == BRW_VERTICAL_STRIDE_0);
731
732 if (devinfo->gen >= 7) {
733 /* Set both the source and destination types based on dest.type,
734 * ignoring the source register types. The MAD and LRP emitters ensure
735 * that all four types are float. The BFE and BFI2 emitters, however,
736 * may send us mixed D and UD types and want us to ignore that and use
737 * the destination type.
738 */
739 brw_inst_set_3src_a16_src_type(devinfo, inst, dest.type);
740 brw_inst_set_3src_a16_dst_type(devinfo, inst, dest.type);
741 }
742
743 return inst;
744 }
745
746
747 /***********************************************************************
748 * Convenience routines.
749 */
750 #define ALU1(OP) \
751 brw_inst *brw_##OP(struct brw_codegen *p, \
752 struct brw_reg dest, \
753 struct brw_reg src0) \
754 { \
755 return brw_alu1(p, BRW_OPCODE_##OP, dest, src0); \
756 }
757
758 #define ALU2(OP) \
759 brw_inst *brw_##OP(struct brw_codegen *p, \
760 struct brw_reg dest, \
761 struct brw_reg src0, \
762 struct brw_reg src1) \
763 { \
764 return brw_alu2(p, BRW_OPCODE_##OP, dest, src0, src1); \
765 }
766
767 #define ALU3(OP) \
768 brw_inst *brw_##OP(struct brw_codegen *p, \
769 struct brw_reg dest, \
770 struct brw_reg src0, \
771 struct brw_reg src1, \
772 struct brw_reg src2) \
773 { \
774 return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2); \
775 }
776
777 #define ALU3F(OP) \
778 brw_inst *brw_##OP(struct brw_codegen *p, \
779 struct brw_reg dest, \
780 struct brw_reg src0, \
781 struct brw_reg src1, \
782 struct brw_reg src2) \
783 { \
784 assert(dest.type == BRW_REGISTER_TYPE_F || \
785 dest.type == BRW_REGISTER_TYPE_DF); \
786 if (dest.type == BRW_REGISTER_TYPE_F) { \
787 assert(src0.type == BRW_REGISTER_TYPE_F); \
788 assert(src1.type == BRW_REGISTER_TYPE_F); \
789 assert(src2.type == BRW_REGISTER_TYPE_F); \
790 } else if (dest.type == BRW_REGISTER_TYPE_DF) { \
791 assert(src0.type == BRW_REGISTER_TYPE_DF); \
792 assert(src1.type == BRW_REGISTER_TYPE_DF); \
793 assert(src2.type == BRW_REGISTER_TYPE_DF); \
794 } \
795 return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2); \
796 }
797
798 /* Rounding operations (other than RNDD) require two instructions - the first
799 * stores a rounded value (possibly the wrong way) in the dest register, but
800 * also sets a per-channel "increment bit" in the flag register. A predicated
801 * add of 1.0 fixes dest to contain the desired result.
802 *
803 * Sandybridge and later appear to round correctly without an ADD.
804 */
805 #define ROUND(OP) \
806 void brw_##OP(struct brw_codegen *p, \
807 struct brw_reg dest, \
808 struct brw_reg src) \
809 { \
810 const struct gen_device_info *devinfo = p->devinfo; \
811 brw_inst *rnd, *add; \
812 rnd = next_insn(p, BRW_OPCODE_##OP); \
813 brw_set_dest(p, rnd, dest); \
814 brw_set_src0(p, rnd, src); \
815 \
816 if (devinfo->gen < 6) { \
817 /* turn on round-increments */ \
818 brw_inst_set_cond_modifier(devinfo, rnd, BRW_CONDITIONAL_R); \
819 add = brw_ADD(p, dest, dest, brw_imm_f(1.0f)); \
820 brw_inst_set_pred_control(devinfo, add, BRW_PREDICATE_NORMAL); \
821 } \
822 }
823
824
825 ALU2(SEL)
826 ALU1(NOT)
827 ALU2(AND)
828 ALU2(OR)
829 ALU2(XOR)
830 ALU2(SHR)
831 ALU2(SHL)
832 ALU1(DIM)
833 ALU2(ASR)
834 ALU1(FRC)
835 ALU1(RNDD)
836 ALU2(MAC)
837 ALU2(MACH)
838 ALU1(LZD)
839 ALU2(DP4)
840 ALU2(DPH)
841 ALU2(DP3)
842 ALU2(DP2)
843 ALU3F(MAD)
844 ALU3F(LRP)
845 ALU1(BFREV)
846 ALU3(BFE)
847 ALU2(BFI1)
848 ALU3(BFI2)
849 ALU1(FBH)
850 ALU1(FBL)
851 ALU1(CBIT)
852 ALU2(ADDC)
853 ALU2(SUBB)
854
855 ROUND(RNDZ)
856 ROUND(RNDE)
857
858 brw_inst *
859 brw_MOV(struct brw_codegen *p, struct brw_reg dest, struct brw_reg src0)
860 {
861 const struct gen_device_info *devinfo = p->devinfo;
862
863 /* When converting F->DF on IVB/BYT, every odd source channel is ignored.
864 * To avoid the problems that causes, we use a <1,2,0> source region to read
865 * each element twice.
866 */
867 if (devinfo->gen == 7 && !devinfo->is_haswell &&
868 brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1 &&
869 dest.type == BRW_REGISTER_TYPE_DF &&
870 (src0.type == BRW_REGISTER_TYPE_F ||
871 src0.type == BRW_REGISTER_TYPE_D ||
872 src0.type == BRW_REGISTER_TYPE_UD) &&
873 !has_scalar_region(src0)) {
874 assert(src0.vstride == BRW_VERTICAL_STRIDE_4 &&
875 src0.width == BRW_WIDTH_4 &&
876 src0.hstride == BRW_HORIZONTAL_STRIDE_1);
877
878 src0.vstride = BRW_VERTICAL_STRIDE_1;
879 src0.width = BRW_WIDTH_2;
880 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
881 }
882
883 return brw_alu1(p, BRW_OPCODE_MOV, dest, src0);
884 }
885
886 brw_inst *
887 brw_ADD(struct brw_codegen *p, struct brw_reg dest,
888 struct brw_reg src0, struct brw_reg src1)
889 {
890 /* 6.2.2: add */
891 if (src0.type == BRW_REGISTER_TYPE_F ||
892 (src0.file == BRW_IMMEDIATE_VALUE &&
893 src0.type == BRW_REGISTER_TYPE_VF)) {
894 assert(src1.type != BRW_REGISTER_TYPE_UD);
895 assert(src1.type != BRW_REGISTER_TYPE_D);
896 }
897
898 if (src1.type == BRW_REGISTER_TYPE_F ||
899 (src1.file == BRW_IMMEDIATE_VALUE &&
900 src1.type == BRW_REGISTER_TYPE_VF)) {
901 assert(src0.type != BRW_REGISTER_TYPE_UD);
902 assert(src0.type != BRW_REGISTER_TYPE_D);
903 }
904
905 return brw_alu2(p, BRW_OPCODE_ADD, dest, src0, src1);
906 }
907
908 brw_inst *
909 brw_AVG(struct brw_codegen *p, struct brw_reg dest,
910 struct brw_reg src0, struct brw_reg src1)
911 {
912 assert(dest.type == src0.type);
913 assert(src0.type == src1.type);
914 switch (src0.type) {
915 case BRW_REGISTER_TYPE_B:
916 case BRW_REGISTER_TYPE_UB:
917 case BRW_REGISTER_TYPE_W:
918 case BRW_REGISTER_TYPE_UW:
919 case BRW_REGISTER_TYPE_D:
920 case BRW_REGISTER_TYPE_UD:
921 break;
922 default:
923 unreachable("Bad type for brw_AVG");
924 }
925
926 return brw_alu2(p, BRW_OPCODE_AVG, dest, src0, src1);
927 }
928
929 brw_inst *
930 brw_MUL(struct brw_codegen *p, struct brw_reg dest,
931 struct brw_reg src0, struct brw_reg src1)
932 {
933 /* 6.32.38: mul */
934 if (src0.type == BRW_REGISTER_TYPE_D ||
935 src0.type == BRW_REGISTER_TYPE_UD ||
936 src1.type == BRW_REGISTER_TYPE_D ||
937 src1.type == BRW_REGISTER_TYPE_UD) {
938 assert(dest.type != BRW_REGISTER_TYPE_F);
939 }
940
941 if (src0.type == BRW_REGISTER_TYPE_F ||
942 (src0.file == BRW_IMMEDIATE_VALUE &&
943 src0.type == BRW_REGISTER_TYPE_VF)) {
944 assert(src1.type != BRW_REGISTER_TYPE_UD);
945 assert(src1.type != BRW_REGISTER_TYPE_D);
946 }
947
948 if (src1.type == BRW_REGISTER_TYPE_F ||
949 (src1.file == BRW_IMMEDIATE_VALUE &&
950 src1.type == BRW_REGISTER_TYPE_VF)) {
951 assert(src0.type != BRW_REGISTER_TYPE_UD);
952 assert(src0.type != BRW_REGISTER_TYPE_D);
953 }
954
955 assert(src0.file != BRW_ARCHITECTURE_REGISTER_FILE ||
956 src0.nr != BRW_ARF_ACCUMULATOR);
957 assert(src1.file != BRW_ARCHITECTURE_REGISTER_FILE ||
958 src1.nr != BRW_ARF_ACCUMULATOR);
959
960 return brw_alu2(p, BRW_OPCODE_MUL, dest, src0, src1);
961 }
962
963 brw_inst *
964 brw_LINE(struct brw_codegen *p, struct brw_reg dest,
965 struct brw_reg src0, struct brw_reg src1)
966 {
967 src0.vstride = BRW_VERTICAL_STRIDE_0;
968 src0.width = BRW_WIDTH_1;
969 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
970 return brw_alu2(p, BRW_OPCODE_LINE, dest, src0, src1);
971 }
972
973 brw_inst *
974 brw_PLN(struct brw_codegen *p, struct brw_reg dest,
975 struct brw_reg src0, struct brw_reg src1)
976 {
977 src0.vstride = BRW_VERTICAL_STRIDE_0;
978 src0.width = BRW_WIDTH_1;
979 src0.hstride = BRW_HORIZONTAL_STRIDE_0;
980 src1.vstride = BRW_VERTICAL_STRIDE_8;
981 src1.width = BRW_WIDTH_8;
982 src1.hstride = BRW_HORIZONTAL_STRIDE_1;
983 return brw_alu2(p, BRW_OPCODE_PLN, dest, src0, src1);
984 }
985
986 brw_inst *
987 brw_F32TO16(struct brw_codegen *p, struct brw_reg dst, struct brw_reg src)
988 {
989 const struct gen_device_info *devinfo = p->devinfo;
990 const bool align16 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_16;
991 /* The F32TO16 instruction doesn't support 32-bit destination types in
992 * Align1 mode, and neither does the Gen8 implementation in terms of a
993 * converting MOV. Gen7 does zero out the high 16 bits in Align16 mode as
994 * an undocumented feature.
995 */
996 const bool needs_zero_fill = (dst.type == BRW_REGISTER_TYPE_UD &&
997 (!align16 || devinfo->gen >= 8));
998 brw_inst *inst;
999
1000 if (align16) {
1001 assert(dst.type == BRW_REGISTER_TYPE_UD);
1002 } else {
1003 assert(dst.type == BRW_REGISTER_TYPE_UD ||
1004 dst.type == BRW_REGISTER_TYPE_W ||
1005 dst.type == BRW_REGISTER_TYPE_UW ||
1006 dst.type == BRW_REGISTER_TYPE_HF);
1007 }
1008
1009 brw_push_insn_state(p);
1010
1011 if (needs_zero_fill) {
1012 brw_set_default_access_mode(p, BRW_ALIGN_1);
1013 dst = spread(retype(dst, BRW_REGISTER_TYPE_W), 2);
1014 }
1015
1016 if (devinfo->gen >= 8) {
1017 inst = brw_MOV(p, retype(dst, BRW_REGISTER_TYPE_HF), src);
1018 } else {
1019 assert(devinfo->gen == 7);
1020 inst = brw_alu1(p, BRW_OPCODE_F32TO16, dst, src);
1021 }
1022
1023 if (needs_zero_fill) {
1024 brw_inst_set_no_dd_clear(devinfo, inst, true);
1025 inst = brw_MOV(p, suboffset(dst, 1), brw_imm_w(0));
1026 brw_inst_set_no_dd_check(devinfo, inst, true);
1027 }
1028
1029 brw_pop_insn_state(p);
1030 return inst;
1031 }
1032
1033 brw_inst *
1034 brw_F16TO32(struct brw_codegen *p, struct brw_reg dst, struct brw_reg src)
1035 {
1036 const struct gen_device_info *devinfo = p->devinfo;
1037 bool align16 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_16;
1038
1039 if (align16) {
1040 assert(src.type == BRW_REGISTER_TYPE_UD);
1041 } else {
1042 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1043 *
1044 * Because this instruction does not have a 16-bit floating-point
1045 * type, the source data type must be Word (W). The destination type
1046 * must be F (Float).
1047 */
1048 if (src.type == BRW_REGISTER_TYPE_UD)
1049 src = spread(retype(src, BRW_REGISTER_TYPE_W), 2);
1050
1051 assert(src.type == BRW_REGISTER_TYPE_W ||
1052 src.type == BRW_REGISTER_TYPE_UW ||
1053 src.type == BRW_REGISTER_TYPE_HF);
1054 }
1055
1056 if (devinfo->gen >= 8) {
1057 return brw_MOV(p, dst, retype(src, BRW_REGISTER_TYPE_HF));
1058 } else {
1059 assert(devinfo->gen == 7);
1060 return brw_alu1(p, BRW_OPCODE_F16TO32, dst, src);
1061 }
1062 }
1063
1064
1065 void brw_NOP(struct brw_codegen *p)
1066 {
1067 brw_inst *insn = next_insn(p, BRW_OPCODE_NOP);
1068 memset(insn, 0, sizeof(*insn));
1069 brw_inst_set_opcode(p->devinfo, insn, BRW_OPCODE_NOP);
1070 }
1071
1072
1073
1074
1075
1076 /***********************************************************************
1077 * Comparisons, if/else/endif
1078 */
1079
1080 brw_inst *
1081 brw_JMPI(struct brw_codegen *p, struct brw_reg index,
1082 unsigned predicate_control)
1083 {
1084 const struct gen_device_info *devinfo = p->devinfo;
1085 struct brw_reg ip = brw_ip_reg();
1086 brw_inst *inst = brw_alu2(p, BRW_OPCODE_JMPI, ip, ip, index);
1087
1088 brw_inst_set_exec_size(devinfo, inst, BRW_EXECUTE_2);
1089 brw_inst_set_qtr_control(devinfo, inst, BRW_COMPRESSION_NONE);
1090 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_DISABLE);
1091 brw_inst_set_pred_control(devinfo, inst, predicate_control);
1092
1093 return inst;
1094 }
1095
1096 static void
1097 push_if_stack(struct brw_codegen *p, brw_inst *inst)
1098 {
1099 p->if_stack[p->if_stack_depth] = inst - p->store;
1100
1101 p->if_stack_depth++;
1102 if (p->if_stack_array_size <= p->if_stack_depth) {
1103 p->if_stack_array_size *= 2;
1104 p->if_stack = reralloc(p->mem_ctx, p->if_stack, int,
1105 p->if_stack_array_size);
1106 }
1107 }
1108
1109 static brw_inst *
1110 pop_if_stack(struct brw_codegen *p)
1111 {
1112 p->if_stack_depth--;
1113 return &p->store[p->if_stack[p->if_stack_depth]];
1114 }
1115
1116 static void
1117 push_loop_stack(struct brw_codegen *p, brw_inst *inst)
1118 {
1119 if (p->loop_stack_array_size <= (p->loop_stack_depth + 1)) {
1120 p->loop_stack_array_size *= 2;
1121 p->loop_stack = reralloc(p->mem_ctx, p->loop_stack, int,
1122 p->loop_stack_array_size);
1123 p->if_depth_in_loop = reralloc(p->mem_ctx, p->if_depth_in_loop, int,
1124 p->loop_stack_array_size);
1125 }
1126
1127 p->loop_stack[p->loop_stack_depth] = inst - p->store;
1128 p->loop_stack_depth++;
1129 p->if_depth_in_loop[p->loop_stack_depth] = 0;
1130 }
1131
1132 static brw_inst *
1133 get_inner_do_insn(struct brw_codegen *p)
1134 {
1135 return &p->store[p->loop_stack[p->loop_stack_depth - 1]];
1136 }
1137
1138 /* EU takes the value from the flag register and pushes it onto some
1139 * sort of a stack (presumably merging with any flag value already on
1140 * the stack). Within an if block, the flags at the top of the stack
1141 * control execution on each channel of the unit, eg. on each of the
1142 * 16 pixel values in our wm programs.
1143 *
1144 * When the matching 'else' instruction is reached (presumably by
1145 * countdown of the instruction count patched in by our ELSE/ENDIF
1146 * functions), the relevant flags are inverted.
1147 *
1148 * When the matching 'endif' instruction is reached, the flags are
1149 * popped off. If the stack is now empty, normal execution resumes.
1150 */
1151 brw_inst *
1152 brw_IF(struct brw_codegen *p, unsigned execute_size)
1153 {
1154 const struct gen_device_info *devinfo = p->devinfo;
1155 brw_inst *insn;
1156
1157 insn = next_insn(p, BRW_OPCODE_IF);
1158
1159 /* Override the defaults for this instruction:
1160 */
1161 if (devinfo->gen < 6) {
1162 brw_set_dest(p, insn, brw_ip_reg());
1163 brw_set_src0(p, insn, brw_ip_reg());
1164 brw_set_src1(p, insn, brw_imm_d(0x0));
1165 } else if (devinfo->gen == 6) {
1166 brw_set_dest(p, insn, brw_imm_w(0));
1167 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1168 brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1169 brw_set_src1(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1170 } else if (devinfo->gen == 7) {
1171 brw_set_dest(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1172 brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1173 brw_set_src1(p, insn, brw_imm_w(0));
1174 brw_inst_set_jip(devinfo, insn, 0);
1175 brw_inst_set_uip(devinfo, insn, 0);
1176 } else {
1177 brw_set_dest(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
1178 brw_set_src0(p, insn, brw_imm_d(0));
1179 brw_inst_set_jip(devinfo, insn, 0);
1180 brw_inst_set_uip(devinfo, insn, 0);
1181 }
1182
1183 brw_inst_set_exec_size(devinfo, insn, execute_size);
1184 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1185 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NORMAL);
1186 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1187 if (!p->single_program_flow && devinfo->gen < 6)
1188 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1189
1190 push_if_stack(p, insn);
1191 p->if_depth_in_loop[p->loop_stack_depth]++;
1192 return insn;
1193 }
1194
1195 /* This function is only used for gen6-style IF instructions with an
1196 * embedded comparison (conditional modifier). It is not used on gen7.
1197 */
1198 brw_inst *
1199 gen6_IF(struct brw_codegen *p, enum brw_conditional_mod conditional,
1200 struct brw_reg src0, struct brw_reg src1)
1201 {
1202 const struct gen_device_info *devinfo = p->devinfo;
1203 brw_inst *insn;
1204
1205 insn = next_insn(p, BRW_OPCODE_IF);
1206
1207 brw_set_dest(p, insn, brw_imm_w(0));
1208 brw_inst_set_exec_size(devinfo, insn,
1209 brw_inst_exec_size(devinfo, p->current));
1210 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1211 brw_set_src0(p, insn, src0);
1212 brw_set_src1(p, insn, src1);
1213
1214 assert(brw_inst_qtr_control(devinfo, insn) == BRW_COMPRESSION_NONE);
1215 assert(brw_inst_pred_control(devinfo, insn) == BRW_PREDICATE_NONE);
1216 brw_inst_set_cond_modifier(devinfo, insn, conditional);
1217
1218 push_if_stack(p, insn);
1219 return insn;
1220 }
1221
1222 /**
1223 * In single-program-flow (SPF) mode, convert IF and ELSE into ADDs.
1224 */
1225 static void
1226 convert_IF_ELSE_to_ADD(struct brw_codegen *p,
1227 brw_inst *if_inst, brw_inst *else_inst)
1228 {
1229 const struct gen_device_info *devinfo = p->devinfo;
1230
1231 /* The next instruction (where the ENDIF would be, if it existed) */
1232 brw_inst *next_inst = &p->store[p->nr_insn];
1233
1234 assert(p->single_program_flow);
1235 assert(if_inst != NULL && brw_inst_opcode(devinfo, if_inst) == BRW_OPCODE_IF);
1236 assert(else_inst == NULL || brw_inst_opcode(devinfo, else_inst) == BRW_OPCODE_ELSE);
1237 assert(brw_inst_exec_size(devinfo, if_inst) == BRW_EXECUTE_1);
1238
1239 /* Convert IF to an ADD instruction that moves the instruction pointer
1240 * to the first instruction of the ELSE block. If there is no ELSE
1241 * block, point to where ENDIF would be. Reverse the predicate.
1242 *
1243 * There's no need to execute an ENDIF since we don't need to do any
1244 * stack operations, and if we're currently executing, we just want to
1245 * continue normally.
1246 */
1247 brw_inst_set_opcode(devinfo, if_inst, BRW_OPCODE_ADD);
1248 brw_inst_set_pred_inv(devinfo, if_inst, true);
1249
1250 if (else_inst != NULL) {
1251 /* Convert ELSE to an ADD instruction that points where the ENDIF
1252 * would be.
1253 */
1254 brw_inst_set_opcode(devinfo, else_inst, BRW_OPCODE_ADD);
1255
1256 brw_inst_set_imm_ud(devinfo, if_inst, (else_inst - if_inst + 1) * 16);
1257 brw_inst_set_imm_ud(devinfo, else_inst, (next_inst - else_inst) * 16);
1258 } else {
1259 brw_inst_set_imm_ud(devinfo, if_inst, (next_inst - if_inst) * 16);
1260 }
1261 }
1262
1263 /**
1264 * Patch IF and ELSE instructions with appropriate jump targets.
1265 */
1266 static void
1267 patch_IF_ELSE(struct brw_codegen *p,
1268 brw_inst *if_inst, brw_inst *else_inst, brw_inst *endif_inst)
1269 {
1270 const struct gen_device_info *devinfo = p->devinfo;
1271
1272 /* We shouldn't be patching IF and ELSE instructions in single program flow
1273 * mode when gen < 6, because in single program flow mode on those
1274 * platforms, we convert flow control instructions to conditional ADDs that
1275 * operate on IP (see brw_ENDIF).
1276 *
1277 * However, on Gen6, writing to IP doesn't work in single program flow mode
1278 * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
1279 * not be updated by non-flow control instructions."). And on later
1280 * platforms, there is no significant benefit to converting control flow
1281 * instructions to conditional ADDs. So we do patch IF and ELSE
1282 * instructions in single program flow mode on those platforms.
1283 */
1284 if (devinfo->gen < 6)
1285 assert(!p->single_program_flow);
1286
1287 assert(if_inst != NULL && brw_inst_opcode(devinfo, if_inst) == BRW_OPCODE_IF);
1288 assert(endif_inst != NULL);
1289 assert(else_inst == NULL || brw_inst_opcode(devinfo, else_inst) == BRW_OPCODE_ELSE);
1290
1291 unsigned br = brw_jump_scale(devinfo);
1292
1293 assert(brw_inst_opcode(devinfo, endif_inst) == BRW_OPCODE_ENDIF);
1294 brw_inst_set_exec_size(devinfo, endif_inst, brw_inst_exec_size(devinfo, if_inst));
1295
1296 if (else_inst == NULL) {
1297 /* Patch IF -> ENDIF */
1298 if (devinfo->gen < 6) {
1299 /* Turn it into an IFF, which means no mask stack operations for
1300 * all-false and jumping past the ENDIF.
1301 */
1302 brw_inst_set_opcode(devinfo, if_inst, BRW_OPCODE_IFF);
1303 brw_inst_set_gen4_jump_count(devinfo, if_inst,
1304 br * (endif_inst - if_inst + 1));
1305 brw_inst_set_gen4_pop_count(devinfo, if_inst, 0);
1306 } else if (devinfo->gen == 6) {
1307 /* As of gen6, there is no IFF and IF must point to the ENDIF. */
1308 brw_inst_set_gen6_jump_count(devinfo, if_inst, br*(endif_inst - if_inst));
1309 } else {
1310 brw_inst_set_uip(devinfo, if_inst, br * (endif_inst - if_inst));
1311 brw_inst_set_jip(devinfo, if_inst, br * (endif_inst - if_inst));
1312 }
1313 } else {
1314 brw_inst_set_exec_size(devinfo, else_inst, brw_inst_exec_size(devinfo, if_inst));
1315
1316 /* Patch IF -> ELSE */
1317 if (devinfo->gen < 6) {
1318 brw_inst_set_gen4_jump_count(devinfo, if_inst,
1319 br * (else_inst - if_inst));
1320 brw_inst_set_gen4_pop_count(devinfo, if_inst, 0);
1321 } else if (devinfo->gen == 6) {
1322 brw_inst_set_gen6_jump_count(devinfo, if_inst,
1323 br * (else_inst - if_inst + 1));
1324 }
1325
1326 /* Patch ELSE -> ENDIF */
1327 if (devinfo->gen < 6) {
1328 /* BRW_OPCODE_ELSE pre-gen6 should point just past the
1329 * matching ENDIF.
1330 */
1331 brw_inst_set_gen4_jump_count(devinfo, else_inst,
1332 br * (endif_inst - else_inst + 1));
1333 brw_inst_set_gen4_pop_count(devinfo, else_inst, 1);
1334 } else if (devinfo->gen == 6) {
1335 /* BRW_OPCODE_ELSE on gen6 should point to the matching ENDIF. */
1336 brw_inst_set_gen6_jump_count(devinfo, else_inst,
1337 br * (endif_inst - else_inst));
1338 } else {
1339 /* The IF instruction's JIP should point just past the ELSE */
1340 brw_inst_set_jip(devinfo, if_inst, br * (else_inst - if_inst + 1));
1341 /* The IF instruction's UIP and ELSE's JIP should point to ENDIF */
1342 brw_inst_set_uip(devinfo, if_inst, br * (endif_inst - if_inst));
1343 brw_inst_set_jip(devinfo, else_inst, br * (endif_inst - else_inst));
1344 if (devinfo->gen >= 8) {
1345 /* Since we don't set branch_ctrl, the ELSE's JIP and UIP both
1346 * should point to ENDIF.
1347 */
1348 brw_inst_set_uip(devinfo, else_inst, br * (endif_inst - else_inst));
1349 }
1350 }
1351 }
1352 }
1353
1354 void
1355 brw_ELSE(struct brw_codegen *p)
1356 {
1357 const struct gen_device_info *devinfo = p->devinfo;
1358 brw_inst *insn;
1359
1360 insn = next_insn(p, BRW_OPCODE_ELSE);
1361
1362 if (devinfo->gen < 6) {
1363 brw_set_dest(p, insn, brw_ip_reg());
1364 brw_set_src0(p, insn, brw_ip_reg());
1365 brw_set_src1(p, insn, brw_imm_d(0x0));
1366 } else if (devinfo->gen == 6) {
1367 brw_set_dest(p, insn, brw_imm_w(0));
1368 brw_inst_set_gen6_jump_count(devinfo, insn, 0);
1369 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1370 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1371 } else if (devinfo->gen == 7) {
1372 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1373 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1374 brw_set_src1(p, insn, brw_imm_w(0));
1375 brw_inst_set_jip(devinfo, insn, 0);
1376 brw_inst_set_uip(devinfo, insn, 0);
1377 } else {
1378 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1379 brw_set_src0(p, insn, brw_imm_d(0));
1380 brw_inst_set_jip(devinfo, insn, 0);
1381 brw_inst_set_uip(devinfo, insn, 0);
1382 }
1383
1384 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1385 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1386 if (!p->single_program_flow && devinfo->gen < 6)
1387 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1388
1389 push_if_stack(p, insn);
1390 }
1391
1392 void
1393 brw_ENDIF(struct brw_codegen *p)
1394 {
1395 const struct gen_device_info *devinfo = p->devinfo;
1396 brw_inst *insn = NULL;
1397 brw_inst *else_inst = NULL;
1398 brw_inst *if_inst = NULL;
1399 brw_inst *tmp;
1400 bool emit_endif = true;
1401
1402 /* In single program flow mode, we can express IF and ELSE instructions
1403 * equivalently as ADD instructions that operate on IP. On platforms prior
1404 * to Gen6, flow control instructions cause an implied thread switch, so
1405 * this is a significant savings.
1406 *
1407 * However, on Gen6, writing to IP doesn't work in single program flow mode
1408 * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
1409 * not be updated by non-flow control instructions."). And on later
1410 * platforms, there is no significant benefit to converting control flow
1411 * instructions to conditional ADDs. So we only do this trick on Gen4 and
1412 * Gen5.
1413 */
1414 if (devinfo->gen < 6 && p->single_program_flow)
1415 emit_endif = false;
1416
1417 /*
1418 * A single next_insn() may change the base address of instruction store
1419 * memory(p->store), so call it first before referencing the instruction
1420 * store pointer from an index
1421 */
1422 if (emit_endif)
1423 insn = next_insn(p, BRW_OPCODE_ENDIF);
1424
1425 /* Pop the IF and (optional) ELSE instructions from the stack */
1426 p->if_depth_in_loop[p->loop_stack_depth]--;
1427 tmp = pop_if_stack(p);
1428 if (brw_inst_opcode(devinfo, tmp) == BRW_OPCODE_ELSE) {
1429 else_inst = tmp;
1430 tmp = pop_if_stack(p);
1431 }
1432 if_inst = tmp;
1433
1434 if (!emit_endif) {
1435 /* ENDIF is useless; don't bother emitting it. */
1436 convert_IF_ELSE_to_ADD(p, if_inst, else_inst);
1437 return;
1438 }
1439
1440 if (devinfo->gen < 6) {
1441 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1442 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1443 brw_set_src1(p, insn, brw_imm_d(0x0));
1444 } else if (devinfo->gen == 6) {
1445 brw_set_dest(p, insn, brw_imm_w(0));
1446 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1447 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1448 } else if (devinfo->gen == 7) {
1449 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1450 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1451 brw_set_src1(p, insn, brw_imm_w(0));
1452 } else {
1453 brw_set_src0(p, insn, brw_imm_d(0));
1454 }
1455
1456 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1457 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_ENABLE);
1458 if (devinfo->gen < 6)
1459 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1460
1461 /* Also pop item off the stack in the endif instruction: */
1462 if (devinfo->gen < 6) {
1463 brw_inst_set_gen4_jump_count(devinfo, insn, 0);
1464 brw_inst_set_gen4_pop_count(devinfo, insn, 1);
1465 } else if (devinfo->gen == 6) {
1466 brw_inst_set_gen6_jump_count(devinfo, insn, 2);
1467 } else {
1468 brw_inst_set_jip(devinfo, insn, 2);
1469 }
1470 patch_IF_ELSE(p, if_inst, else_inst, insn);
1471 }
1472
1473 brw_inst *
1474 brw_BREAK(struct brw_codegen *p)
1475 {
1476 const struct gen_device_info *devinfo = p->devinfo;
1477 brw_inst *insn;
1478
1479 insn = next_insn(p, BRW_OPCODE_BREAK);
1480 if (devinfo->gen >= 8) {
1481 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1482 brw_set_src0(p, insn, brw_imm_d(0x0));
1483 } else if (devinfo->gen >= 6) {
1484 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1485 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1486 brw_set_src1(p, insn, brw_imm_d(0x0));
1487 } else {
1488 brw_set_dest(p, insn, brw_ip_reg());
1489 brw_set_src0(p, insn, brw_ip_reg());
1490 brw_set_src1(p, insn, brw_imm_d(0x0));
1491 brw_inst_set_gen4_pop_count(devinfo, insn,
1492 p->if_depth_in_loop[p->loop_stack_depth]);
1493 }
1494 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1495 brw_inst_set_exec_size(devinfo, insn,
1496 brw_inst_exec_size(devinfo, p->current));
1497
1498 return insn;
1499 }
1500
1501 brw_inst *
1502 brw_CONT(struct brw_codegen *p)
1503 {
1504 const struct gen_device_info *devinfo = p->devinfo;
1505 brw_inst *insn;
1506
1507 insn = next_insn(p, BRW_OPCODE_CONTINUE);
1508 brw_set_dest(p, insn, brw_ip_reg());
1509 if (devinfo->gen >= 8) {
1510 brw_set_src0(p, insn, brw_imm_d(0x0));
1511 } else {
1512 brw_set_src0(p, insn, brw_ip_reg());
1513 brw_set_src1(p, insn, brw_imm_d(0x0));
1514 }
1515
1516 if (devinfo->gen < 6) {
1517 brw_inst_set_gen4_pop_count(devinfo, insn,
1518 p->if_depth_in_loop[p->loop_stack_depth]);
1519 }
1520 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1521 brw_inst_set_exec_size(devinfo, insn,
1522 brw_inst_exec_size(devinfo, p->current));
1523 return insn;
1524 }
1525
1526 brw_inst *
1527 gen6_HALT(struct brw_codegen *p)
1528 {
1529 const struct gen_device_info *devinfo = p->devinfo;
1530 brw_inst *insn;
1531
1532 insn = next_insn(p, BRW_OPCODE_HALT);
1533 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1534 if (devinfo->gen >= 8) {
1535 brw_set_src0(p, insn, brw_imm_d(0x0));
1536 } else {
1537 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1538 brw_set_src1(p, insn, brw_imm_d(0x0)); /* UIP and JIP, updated later. */
1539 }
1540
1541 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1542 brw_inst_set_exec_size(devinfo, insn,
1543 brw_inst_exec_size(devinfo, p->current));
1544 return insn;
1545 }
1546
1547 /* DO/WHILE loop:
1548 *
1549 * The DO/WHILE is just an unterminated loop -- break or continue are
1550 * used for control within the loop. We have a few ways they can be
1551 * done.
1552 *
1553 * For uniform control flow, the WHILE is just a jump, so ADD ip, ip,
1554 * jip and no DO instruction.
1555 *
1556 * For non-uniform control flow pre-gen6, there's a DO instruction to
1557 * push the mask, and a WHILE to jump back, and BREAK to get out and
1558 * pop the mask.
1559 *
1560 * For gen6, there's no more mask stack, so no need for DO. WHILE
1561 * just points back to the first instruction of the loop.
1562 */
1563 brw_inst *
1564 brw_DO(struct brw_codegen *p, unsigned execute_size)
1565 {
1566 const struct gen_device_info *devinfo = p->devinfo;
1567
1568 if (devinfo->gen >= 6 || p->single_program_flow) {
1569 push_loop_stack(p, &p->store[p->nr_insn]);
1570 return &p->store[p->nr_insn];
1571 } else {
1572 brw_inst *insn = next_insn(p, BRW_OPCODE_DO);
1573
1574 push_loop_stack(p, insn);
1575
1576 /* Override the defaults for this instruction:
1577 */
1578 brw_set_dest(p, insn, brw_null_reg());
1579 brw_set_src0(p, insn, brw_null_reg());
1580 brw_set_src1(p, insn, brw_null_reg());
1581
1582 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1583 brw_inst_set_exec_size(devinfo, insn, execute_size);
1584 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE);
1585
1586 return insn;
1587 }
1588 }
1589
1590 /**
1591 * For pre-gen6, we patch BREAK/CONT instructions to point at the WHILE
1592 * instruction here.
1593 *
1594 * For gen6+, see brw_set_uip_jip(), which doesn't care so much about the loop
1595 * nesting, since it can always just point to the end of the block/current loop.
1596 */
1597 static void
1598 brw_patch_break_cont(struct brw_codegen *p, brw_inst *while_inst)
1599 {
1600 const struct gen_device_info *devinfo = p->devinfo;
1601 brw_inst *do_inst = get_inner_do_insn(p);
1602 brw_inst *inst;
1603 unsigned br = brw_jump_scale(devinfo);
1604
1605 assert(devinfo->gen < 6);
1606
1607 for (inst = while_inst - 1; inst != do_inst; inst--) {
1608 /* If the jump count is != 0, that means that this instruction has already
1609 * been patched because it's part of a loop inside of the one we're
1610 * patching.
1611 */
1612 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_BREAK &&
1613 brw_inst_gen4_jump_count(devinfo, inst) == 0) {
1614 brw_inst_set_gen4_jump_count(devinfo, inst, br*((while_inst - inst) + 1));
1615 } else if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_CONTINUE &&
1616 brw_inst_gen4_jump_count(devinfo, inst) == 0) {
1617 brw_inst_set_gen4_jump_count(devinfo, inst, br * (while_inst - inst));
1618 }
1619 }
1620 }
1621
1622 brw_inst *
1623 brw_WHILE(struct brw_codegen *p)
1624 {
1625 const struct gen_device_info *devinfo = p->devinfo;
1626 brw_inst *insn, *do_insn;
1627 unsigned br = brw_jump_scale(devinfo);
1628
1629 if (devinfo->gen >= 6) {
1630 insn = next_insn(p, BRW_OPCODE_WHILE);
1631 do_insn = get_inner_do_insn(p);
1632
1633 if (devinfo->gen >= 8) {
1634 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1635 brw_set_src0(p, insn, brw_imm_d(0));
1636 brw_inst_set_jip(devinfo, insn, br * (do_insn - insn));
1637 } else if (devinfo->gen == 7) {
1638 brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1639 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1640 brw_set_src1(p, insn, brw_imm_w(0));
1641 brw_inst_set_jip(devinfo, insn, br * (do_insn - insn));
1642 } else {
1643 brw_set_dest(p, insn, brw_imm_w(0));
1644 brw_inst_set_gen6_jump_count(devinfo, insn, br * (do_insn - insn));
1645 brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1646 brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
1647 }
1648
1649 brw_inst_set_exec_size(devinfo, insn,
1650 brw_inst_exec_size(devinfo, p->current));
1651
1652 } else {
1653 if (p->single_program_flow) {
1654 insn = next_insn(p, BRW_OPCODE_ADD);
1655 do_insn = get_inner_do_insn(p);
1656
1657 brw_set_dest(p, insn, brw_ip_reg());
1658 brw_set_src0(p, insn, brw_ip_reg());
1659 brw_set_src1(p, insn, brw_imm_d((do_insn - insn) * 16));
1660 brw_inst_set_exec_size(devinfo, insn, BRW_EXECUTE_1);
1661 } else {
1662 insn = next_insn(p, BRW_OPCODE_WHILE);
1663 do_insn = get_inner_do_insn(p);
1664
1665 assert(brw_inst_opcode(devinfo, do_insn) == BRW_OPCODE_DO);
1666
1667 brw_set_dest(p, insn, brw_ip_reg());
1668 brw_set_src0(p, insn, brw_ip_reg());
1669 brw_set_src1(p, insn, brw_imm_d(0));
1670
1671 brw_inst_set_exec_size(devinfo, insn, brw_inst_exec_size(devinfo, do_insn));
1672 brw_inst_set_gen4_jump_count(devinfo, insn, br * (do_insn - insn + 1));
1673 brw_inst_set_gen4_pop_count(devinfo, insn, 0);
1674
1675 brw_patch_break_cont(p, insn);
1676 }
1677 }
1678 brw_inst_set_qtr_control(devinfo, insn, BRW_COMPRESSION_NONE);
1679
1680 p->loop_stack_depth--;
1681
1682 return insn;
1683 }
1684
1685 /* FORWARD JUMPS:
1686 */
1687 void brw_land_fwd_jump(struct brw_codegen *p, int jmp_insn_idx)
1688 {
1689 const struct gen_device_info *devinfo = p->devinfo;
1690 brw_inst *jmp_insn = &p->store[jmp_insn_idx];
1691 unsigned jmpi = 1;
1692
1693 if (devinfo->gen >= 5)
1694 jmpi = 2;
1695
1696 assert(brw_inst_opcode(devinfo, jmp_insn) == BRW_OPCODE_JMPI);
1697 assert(brw_inst_src1_reg_file(devinfo, jmp_insn) == BRW_IMMEDIATE_VALUE);
1698
1699 brw_inst_set_gen4_jump_count(devinfo, jmp_insn,
1700 jmpi * (p->nr_insn - jmp_insn_idx - 1));
1701 }
1702
1703 /* To integrate with the above, it makes sense that the comparison
1704 * instruction should populate the flag register. It might be simpler
1705 * just to use the flag reg for most WM tasks?
1706 */
1707 void brw_CMP(struct brw_codegen *p,
1708 struct brw_reg dest,
1709 unsigned conditional,
1710 struct brw_reg src0,
1711 struct brw_reg src1)
1712 {
1713 const struct gen_device_info *devinfo = p->devinfo;
1714 brw_inst *insn = next_insn(p, BRW_OPCODE_CMP);
1715
1716 brw_inst_set_cond_modifier(devinfo, insn, conditional);
1717 brw_set_dest(p, insn, dest);
1718 brw_set_src0(p, insn, src0);
1719 brw_set_src1(p, insn, src1);
1720
1721 /* Item WaCMPInstNullDstForcesThreadSwitch in the Haswell Bspec workarounds
1722 * page says:
1723 * "Any CMP instruction with a null destination must use a {switch}."
1724 *
1725 * It also applies to other Gen7 platforms (IVB, BYT) even though it isn't
1726 * mentioned on their work-arounds pages.
1727 */
1728 if (devinfo->gen == 7) {
1729 if (dest.file == BRW_ARCHITECTURE_REGISTER_FILE &&
1730 dest.nr == BRW_ARF_NULL) {
1731 brw_inst_set_thread_control(devinfo, insn, BRW_THREAD_SWITCH);
1732 }
1733 }
1734 }
1735
1736 /***********************************************************************
1737 * Helpers for the various SEND message types:
1738 */
1739
1740 /** Extended math function, float[8].
1741 */
1742 void gen4_math(struct brw_codegen *p,
1743 struct brw_reg dest,
1744 unsigned function,
1745 unsigned msg_reg_nr,
1746 struct brw_reg src,
1747 unsigned precision )
1748 {
1749 const struct gen_device_info *devinfo = p->devinfo;
1750 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
1751 unsigned data_type;
1752 if (has_scalar_region(src)) {
1753 data_type = BRW_MATH_DATA_SCALAR;
1754 } else {
1755 data_type = BRW_MATH_DATA_VECTOR;
1756 }
1757
1758 assert(devinfo->gen < 6);
1759
1760 /* Example code doesn't set predicate_control for send
1761 * instructions.
1762 */
1763 brw_inst_set_pred_control(devinfo, insn, 0);
1764 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
1765
1766 brw_set_dest(p, insn, dest);
1767 brw_set_src0(p, insn, src);
1768 brw_set_math_message(p,
1769 insn,
1770 function,
1771 src.type == BRW_REGISTER_TYPE_D,
1772 precision,
1773 data_type);
1774 }
1775
1776 void gen6_math(struct brw_codegen *p,
1777 struct brw_reg dest,
1778 unsigned function,
1779 struct brw_reg src0,
1780 struct brw_reg src1)
1781 {
1782 const struct gen_device_info *devinfo = p->devinfo;
1783 brw_inst *insn = next_insn(p, BRW_OPCODE_MATH);
1784
1785 assert(devinfo->gen >= 6);
1786
1787 assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
1788 (devinfo->gen >= 7 && dest.file == BRW_MESSAGE_REGISTER_FILE));
1789
1790 assert(dest.hstride == BRW_HORIZONTAL_STRIDE_1);
1791 if (devinfo->gen == 6) {
1792 assert(src0.hstride == BRW_HORIZONTAL_STRIDE_1);
1793 assert(src1.hstride == BRW_HORIZONTAL_STRIDE_1);
1794 }
1795
1796 if (function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT ||
1797 function == BRW_MATH_FUNCTION_INT_DIV_REMAINDER ||
1798 function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER) {
1799 assert(src0.type != BRW_REGISTER_TYPE_F);
1800 assert(src1.type != BRW_REGISTER_TYPE_F);
1801 assert(src1.file == BRW_GENERAL_REGISTER_FILE ||
1802 (devinfo->gen >= 8 && src1.file == BRW_IMMEDIATE_VALUE));
1803 } else {
1804 assert(src0.type == BRW_REGISTER_TYPE_F);
1805 assert(src1.type == BRW_REGISTER_TYPE_F);
1806 }
1807
1808 /* Source modifiers are ignored for extended math instructions on Gen6. */
1809 if (devinfo->gen == 6) {
1810 assert(!src0.negate);
1811 assert(!src0.abs);
1812 assert(!src1.negate);
1813 assert(!src1.abs);
1814 }
1815
1816 brw_inst_set_math_function(devinfo, insn, function);
1817
1818 brw_set_dest(p, insn, dest);
1819 brw_set_src0(p, insn, src0);
1820 brw_set_src1(p, insn, src1);
1821 }
1822
1823 /**
1824 * Return the right surface index to access the thread scratch space using
1825 * stateless dataport messages.
1826 */
1827 unsigned
1828 brw_scratch_surface_idx(const struct brw_codegen *p)
1829 {
1830 /* The scratch space is thread-local so IA coherency is unnecessary. */
1831 if (p->devinfo->gen >= 8)
1832 return GEN8_BTI_STATELESS_NON_COHERENT;
1833 else
1834 return BRW_BTI_STATELESS;
1835 }
1836
1837 /**
1838 * Write a block of OWORDs (half a GRF each) from the scratch buffer,
1839 * using a constant offset per channel.
1840 *
1841 * The offset must be aligned to oword size (16 bytes). Used for
1842 * register spilling.
1843 */
1844 void brw_oword_block_write_scratch(struct brw_codegen *p,
1845 struct brw_reg mrf,
1846 int num_regs,
1847 unsigned offset)
1848 {
1849 const struct gen_device_info *devinfo = p->devinfo;
1850 const unsigned target_cache =
1851 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
1852 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
1853 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
1854 uint32_t msg_type;
1855
1856 if (devinfo->gen >= 6)
1857 offset /= 16;
1858
1859 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
1860
1861 const unsigned mlen = 1 + num_regs;
1862
1863 /* Set up the message header. This is g0, with g0.2 filled with
1864 * the offset. We don't want to leave our offset around in g0 or
1865 * it'll screw up texture samples, so set it up inside the message
1866 * reg.
1867 */
1868 {
1869 brw_push_insn_state(p);
1870 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1871 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1872 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1873
1874 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
1875
1876 /* set message header global offset field (reg 0, element 2) */
1877 brw_MOV(p,
1878 retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE,
1879 mrf.nr,
1880 2), BRW_REGISTER_TYPE_UD),
1881 brw_imm_ud(offset));
1882
1883 brw_pop_insn_state(p);
1884 }
1885
1886 {
1887 struct brw_reg dest;
1888 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
1889 int send_commit_msg;
1890 struct brw_reg src_header = retype(brw_vec8_grf(0, 0),
1891 BRW_REGISTER_TYPE_UW);
1892
1893 brw_inst_set_compression(devinfo, insn, false);
1894
1895 if (brw_inst_exec_size(devinfo, insn) >= 16)
1896 src_header = vec16(src_header);
1897
1898 assert(brw_inst_pred_control(devinfo, insn) == BRW_PREDICATE_NONE);
1899 if (devinfo->gen < 6)
1900 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
1901
1902 /* Until gen6, writes followed by reads from the same location
1903 * are not guaranteed to be ordered unless write_commit is set.
1904 * If set, then a no-op write is issued to the destination
1905 * register to set a dependency, and a read from the destination
1906 * can be used to ensure the ordering.
1907 *
1908 * For gen6, only writes between different threads need ordering
1909 * protection. Our use of DP writes is all about register
1910 * spilling within a thread.
1911 */
1912 if (devinfo->gen >= 6) {
1913 dest = retype(vec16(brw_null_reg()), BRW_REGISTER_TYPE_UW);
1914 send_commit_msg = 0;
1915 } else {
1916 dest = src_header;
1917 send_commit_msg = 1;
1918 }
1919
1920 brw_set_dest(p, insn, dest);
1921 if (devinfo->gen >= 6) {
1922 brw_set_src0(p, insn, mrf);
1923 } else {
1924 brw_set_src0(p, insn, brw_null_reg());
1925 }
1926
1927 if (devinfo->gen >= 6)
1928 msg_type = GEN6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE;
1929 else
1930 msg_type = BRW_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE;
1931
1932 brw_set_dp_write_message(p,
1933 insn,
1934 brw_scratch_surface_idx(p),
1935 BRW_DATAPORT_OWORD_BLOCK_DWORDS(num_regs * 8),
1936 msg_type,
1937 target_cache,
1938 mlen,
1939 true, /* header_present */
1940 0, /* not a render target */
1941 send_commit_msg, /* response_length */
1942 0, /* eot */
1943 send_commit_msg);
1944 }
1945 }
1946
1947
1948 /**
1949 * Read a block of owords (half a GRF each) from the scratch buffer
1950 * using a constant index per channel.
1951 *
1952 * Offset must be aligned to oword size (16 bytes). Used for register
1953 * spilling.
1954 */
1955 void
1956 brw_oword_block_read_scratch(struct brw_codegen *p,
1957 struct brw_reg dest,
1958 struct brw_reg mrf,
1959 int num_regs,
1960 unsigned offset)
1961 {
1962 const struct gen_device_info *devinfo = p->devinfo;
1963
1964 if (devinfo->gen >= 6)
1965 offset /= 16;
1966
1967 if (p->devinfo->gen >= 7) {
1968 /* On gen 7 and above, we no longer have message registers and we can
1969 * send from any register we want. By using the destination register
1970 * for the message, we guarantee that the implied message write won't
1971 * accidentally overwrite anything. This has been a problem because
1972 * the MRF registers and source for the final FB write are both fixed
1973 * and may overlap.
1974 */
1975 mrf = retype(dest, BRW_REGISTER_TYPE_UD);
1976 } else {
1977 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
1978 }
1979 dest = retype(dest, BRW_REGISTER_TYPE_UW);
1980
1981 const unsigned rlen = num_regs;
1982 const unsigned target_cache =
1983 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
1984 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
1985 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
1986
1987 {
1988 brw_push_insn_state(p);
1989 brw_set_default_exec_size(p, BRW_EXECUTE_8);
1990 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
1991 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
1992
1993 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
1994
1995 /* set message header global offset field (reg 0, element 2) */
1996 brw_MOV(p, get_element_ud(mrf, 2), brw_imm_ud(offset));
1997
1998 brw_pop_insn_state(p);
1999 }
2000
2001 {
2002 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2003
2004 assert(brw_inst_pred_control(devinfo, insn) == 0);
2005 brw_inst_set_compression(devinfo, insn, false);
2006
2007 brw_set_dest(p, insn, dest); /* UW? */
2008 if (devinfo->gen >= 6) {
2009 brw_set_src0(p, insn, mrf);
2010 } else {
2011 brw_set_src0(p, insn, brw_null_reg());
2012 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
2013 }
2014
2015 brw_set_dp_read_message(p,
2016 insn,
2017 brw_scratch_surface_idx(p),
2018 BRW_DATAPORT_OWORD_BLOCK_DWORDS(num_regs * 8),
2019 BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ, /* msg_type */
2020 target_cache,
2021 1, /* msg_length */
2022 true, /* header_present */
2023 rlen);
2024 }
2025 }
2026
2027 void
2028 gen7_block_read_scratch(struct brw_codegen *p,
2029 struct brw_reg dest,
2030 int num_regs,
2031 unsigned offset)
2032 {
2033 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2034 assert(brw_inst_pred_control(p->devinfo, insn) == BRW_PREDICATE_NONE);
2035
2036 brw_set_dest(p, insn, retype(dest, BRW_REGISTER_TYPE_UW));
2037
2038 /* The HW requires that the header is present; this is to get the g0.5
2039 * scratch offset.
2040 */
2041 brw_set_src0(p, insn, brw_vec8_grf(0, 0));
2042
2043 /* According to the docs, offset is "A 12-bit HWord offset into the memory
2044 * Immediate Memory buffer as specified by binding table 0xFF." An HWORD
2045 * is 32 bytes, which happens to be the size of a register.
2046 */
2047 offset /= REG_SIZE;
2048 assert(offset < (1 << 12));
2049
2050 gen7_set_dp_scratch_message(p, insn,
2051 false, /* scratch read */
2052 false, /* OWords */
2053 false, /* invalidate after read */
2054 num_regs,
2055 offset,
2056 1, /* mlen: just g0 */
2057 num_regs, /* rlen */
2058 true); /* header present */
2059 }
2060
2061 /**
2062 * Read float[4] vectors from the data port constant cache.
2063 * Location (in buffer) should be a multiple of 16.
2064 * Used for fetching shader constants.
2065 */
2066 void brw_oword_block_read(struct brw_codegen *p,
2067 struct brw_reg dest,
2068 struct brw_reg mrf,
2069 uint32_t offset,
2070 uint32_t bind_table_index)
2071 {
2072 const struct gen_device_info *devinfo = p->devinfo;
2073 const unsigned target_cache =
2074 (devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_CONSTANT_CACHE :
2075 BRW_DATAPORT_READ_TARGET_DATA_CACHE);
2076 const unsigned exec_size = 1 << brw_inst_exec_size(devinfo, p->current);
2077
2078 /* On newer hardware, offset is in units of owords. */
2079 if (devinfo->gen >= 6)
2080 offset /= 16;
2081
2082 mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
2083
2084 brw_push_insn_state(p);
2085 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2086 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
2087 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2088
2089 brw_push_insn_state(p);
2090 brw_set_default_exec_size(p, BRW_EXECUTE_8);
2091 brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
2092
2093 /* set message header global offset field (reg 0, element 2) */
2094 brw_MOV(p,
2095 retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE,
2096 mrf.nr,
2097 2), BRW_REGISTER_TYPE_UD),
2098 brw_imm_ud(offset));
2099 brw_pop_insn_state(p);
2100
2101 brw_inst *insn = next_insn(p, BRW_OPCODE_SEND);
2102
2103 /* cast dest to a uword[8] vector */
2104 dest = retype(vec8(dest), BRW_REGISTER_TYPE_UW);
2105
2106 brw_set_dest(p, insn, dest);
2107 if (devinfo->gen >= 6) {
2108 brw_set_src0(p, insn, mrf);
2109 } else {
2110 brw_set_src0(p, insn, brw_null_reg());
2111 brw_inst_set_base_mrf(devinfo, insn, mrf.nr);
2112 }
2113
2114 brw_set_dp_read_message(p, insn, bind_table_index,
2115 BRW_DATAPORT_OWORD_BLOCK_DWORDS(exec_size),
2116 BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ,
2117 target_cache,
2118 1, /* msg_length */
2119 true, /* header_present */
2120 DIV_ROUND_UP(exec_size, 8)); /* response_length */
2121
2122 brw_pop_insn_state(p);
2123 }
2124
2125
2126 void brw_fb_WRITE(struct brw_codegen *p,
2127 struct brw_reg payload,
2128 struct brw_reg implied_header,
2129 unsigned msg_control,
2130 unsigned binding_table_index,
2131 unsigned msg_length,
2132 unsigned response_length,
2133 bool eot,
2134 bool last_render_target,
2135 bool header_present)
2136 {
2137 const struct gen_device_info *devinfo = p->devinfo;
2138 const unsigned target_cache =
2139 (devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
2140 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
2141 brw_inst *insn;
2142 unsigned msg_type;
2143 struct brw_reg dest, src0;
2144
2145 if (brw_inst_exec_size(devinfo, p->current) >= BRW_EXECUTE_16)
2146 dest = retype(vec16(brw_null_reg()), BRW_REGISTER_TYPE_UW);
2147 else
2148 dest = retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW);
2149
2150 if (devinfo->gen >= 6) {
2151 insn = next_insn(p, BRW_OPCODE_SENDC);
2152 } else {
2153 insn = next_insn(p, BRW_OPCODE_SEND);
2154 }
2155 brw_inst_set_compression(devinfo, insn, false);
2156
2157 if (devinfo->gen >= 6) {
2158 /* headerless version, just submit color payload */
2159 src0 = payload;
2160
2161 msg_type = GEN6_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE;
2162 } else {
2163 assert(payload.file == BRW_MESSAGE_REGISTER_FILE);
2164 brw_inst_set_base_mrf(devinfo, insn, payload.nr);
2165 src0 = implied_header;
2166
2167 msg_type = BRW_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE;
2168 }
2169
2170 brw_set_dest(p, insn, dest);
2171 brw_set_src0(p, insn, src0);
2172 brw_set_dp_write_message(p,
2173 insn,
2174 binding_table_index,
2175 msg_control,
2176 msg_type,
2177 target_cache,
2178 msg_length,
2179 header_present,
2180 last_render_target,
2181 response_length,
2182 eot,
2183 0 /* send_commit_msg */);
2184 }
2185
2186 brw_inst *
2187 gen9_fb_READ(struct brw_codegen *p,
2188 struct brw_reg dst,
2189 struct brw_reg payload,
2190 unsigned binding_table_index,
2191 unsigned msg_length,
2192 unsigned response_length,
2193 bool per_sample)
2194 {
2195 const struct gen_device_info *devinfo = p->devinfo;
2196 assert(devinfo->gen >= 9);
2197 const unsigned msg_subtype =
2198 brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16 ? 0 : 1;
2199 brw_inst *insn = next_insn(p, BRW_OPCODE_SENDC);
2200
2201 brw_set_dest(p, insn, dst);
2202 brw_set_src0(p, insn, payload);
2203 brw_set_dp_read_message(p, insn, binding_table_index,
2204 per_sample << 5 | msg_subtype,
2205 GEN9_DATAPORT_RC_RENDER_TARGET_READ,
2206 GEN6_SFID_DATAPORT_RENDER_CACHE,
2207 msg_length, true /* header_present */,
2208 response_length);
2209 brw_inst_set_rt_slot_group(devinfo, insn,
2210 brw_inst_qtr_control(devinfo, p->current) / 2);
2211
2212 return insn;
2213 }
2214
2215 /**
2216 * Texture sample instruction.
2217 * Note: the msg_type plus msg_length values determine exactly what kind
2218 * of sampling operation is performed. See volume 4, page 161 of docs.
2219 */
2220 void brw_SAMPLE(struct brw_codegen *p,
2221 struct brw_reg dest,
2222 unsigned msg_reg_nr,
2223 struct brw_reg src0,
2224 unsigned binding_table_index,
2225 unsigned sampler,
2226 unsigned msg_type,
2227 unsigned response_length,
2228 unsigned msg_length,
2229 unsigned header_present,
2230 unsigned simd_mode,
2231 unsigned return_format)
2232 {
2233 const struct gen_device_info *devinfo = p->devinfo;
2234 brw_inst *insn;
2235
2236 if (msg_reg_nr != -1)
2237 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2238
2239 insn = next_insn(p, BRW_OPCODE_SEND);
2240 brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE); /* XXX */
2241
2242 /* From the 965 PRM (volume 4, part 1, section 14.2.41):
2243 *
2244 * "Instruction compression is not allowed for this instruction (that
2245 * is, send). The hardware behavior is undefined if this instruction is
2246 * set as compressed. However, compress control can be set to "SecHalf"
2247 * to affect the EMask generation."
2248 *
2249 * No similar wording is found in later PRMs, but there are examples
2250 * utilizing send with SecHalf. More importantly, SIMD8 sampler messages
2251 * are allowed in SIMD16 mode and they could not work without SecHalf. For
2252 * these reasons, we allow BRW_COMPRESSION_2NDHALF here.
2253 */
2254 brw_inst_set_compression(devinfo, insn, false);
2255
2256 if (devinfo->gen < 6)
2257 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2258
2259 brw_set_dest(p, insn, dest);
2260 brw_set_src0(p, insn, src0);
2261 brw_set_sampler_message(p, insn,
2262 binding_table_index,
2263 sampler,
2264 msg_type,
2265 response_length,
2266 msg_length,
2267 header_present,
2268 simd_mode,
2269 return_format);
2270 }
2271
2272 /* Adjust the message header's sampler state pointer to
2273 * select the correct group of 16 samplers.
2274 */
2275 void brw_adjust_sampler_state_pointer(struct brw_codegen *p,
2276 struct brw_reg header,
2277 struct brw_reg sampler_index)
2278 {
2279 /* The "Sampler Index" field can only store values between 0 and 15.
2280 * However, we can add an offset to the "Sampler State Pointer"
2281 * field, effectively selecting a different set of 16 samplers.
2282 *
2283 * The "Sampler State Pointer" needs to be aligned to a 32-byte
2284 * offset, and each sampler state is only 16-bytes, so we can't
2285 * exclusively use the offset - we have to use both.
2286 */
2287
2288 const struct gen_device_info *devinfo = p->devinfo;
2289
2290 if (sampler_index.file == BRW_IMMEDIATE_VALUE) {
2291 const int sampler_state_size = 16; /* 16 bytes */
2292 uint32_t sampler = sampler_index.ud;
2293
2294 if (sampler >= 16) {
2295 assert(devinfo->is_haswell || devinfo->gen >= 8);
2296 brw_ADD(p,
2297 get_element_ud(header, 3),
2298 get_element_ud(brw_vec8_grf(0, 0), 3),
2299 brw_imm_ud(16 * (sampler / 16) * sampler_state_size));
2300 }
2301 } else {
2302 /* Non-const sampler array indexing case */
2303 if (devinfo->gen < 8 && !devinfo->is_haswell) {
2304 return;
2305 }
2306
2307 struct brw_reg temp = get_element_ud(header, 3);
2308
2309 brw_AND(p, temp, get_element_ud(sampler_index, 0), brw_imm_ud(0x0f0));
2310 brw_SHL(p, temp, temp, brw_imm_ud(4));
2311 brw_ADD(p,
2312 get_element_ud(header, 3),
2313 get_element_ud(brw_vec8_grf(0, 0), 3),
2314 temp);
2315 }
2316 }
2317
2318 /* All these variables are pretty confusing - we might be better off
2319 * using bitmasks and macros for this, in the old style. Or perhaps
2320 * just having the caller instantiate the fields in dword3 itself.
2321 */
2322 void brw_urb_WRITE(struct brw_codegen *p,
2323 struct brw_reg dest,
2324 unsigned msg_reg_nr,
2325 struct brw_reg src0,
2326 enum brw_urb_write_flags flags,
2327 unsigned msg_length,
2328 unsigned response_length,
2329 unsigned offset,
2330 unsigned swizzle)
2331 {
2332 const struct gen_device_info *devinfo = p->devinfo;
2333 brw_inst *insn;
2334
2335 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2336
2337 if (devinfo->gen >= 7 && !(flags & BRW_URB_WRITE_USE_CHANNEL_MASKS)) {
2338 /* Enable Channel Masks in the URB_WRITE_HWORD message header */
2339 brw_push_insn_state(p);
2340 brw_set_default_access_mode(p, BRW_ALIGN_1);
2341 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2342 brw_OR(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, msg_reg_nr, 5),
2343 BRW_REGISTER_TYPE_UD),
2344 retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
2345 brw_imm_ud(0xff00));
2346 brw_pop_insn_state(p);
2347 }
2348
2349 insn = next_insn(p, BRW_OPCODE_SEND);
2350
2351 assert(msg_length < BRW_MAX_MRF(devinfo->gen));
2352
2353 brw_set_dest(p, insn, dest);
2354 brw_set_src0(p, insn, src0);
2355 brw_set_src1(p, insn, brw_imm_d(0));
2356
2357 if (devinfo->gen < 6)
2358 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2359
2360 brw_set_urb_message(p,
2361 insn,
2362 flags,
2363 msg_length,
2364 response_length,
2365 offset,
2366 swizzle);
2367 }
2368
2369 struct brw_inst *
2370 brw_send_indirect_message(struct brw_codegen *p,
2371 unsigned sfid,
2372 struct brw_reg dst,
2373 struct brw_reg payload,
2374 struct brw_reg desc)
2375 {
2376 const struct gen_device_info *devinfo = p->devinfo;
2377 struct brw_inst *send;
2378 int setup;
2379
2380 dst = retype(dst, BRW_REGISTER_TYPE_UW);
2381
2382 assert(desc.type == BRW_REGISTER_TYPE_UD);
2383
2384 /* We hold on to the setup instruction (the SEND in the direct case, the OR
2385 * in the indirect case) by its index in the instruction store. The
2386 * pointer returned by next_insn() may become invalid if emitting the SEND
2387 * in the indirect case reallocs the store.
2388 */
2389
2390 if (desc.file == BRW_IMMEDIATE_VALUE) {
2391 setup = p->nr_insn;
2392 send = next_insn(p, BRW_OPCODE_SEND);
2393 brw_set_src1(p, send, desc);
2394
2395 } else {
2396 struct brw_reg addr = retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
2397
2398 brw_push_insn_state(p);
2399 brw_set_default_access_mode(p, BRW_ALIGN_1);
2400 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2401 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2402
2403 /* Load the indirect descriptor to an address register using OR so the
2404 * caller can specify additional descriptor bits with the usual
2405 * brw_set_*_message() helper functions.
2406 */
2407 setup = p->nr_insn;
2408 brw_OR(p, addr, desc, brw_imm_ud(0));
2409
2410 brw_pop_insn_state(p);
2411
2412 send = next_insn(p, BRW_OPCODE_SEND);
2413 brw_set_src1(p, send, addr);
2414 }
2415
2416 if (dst.width < BRW_EXECUTE_8)
2417 brw_inst_set_exec_size(devinfo, send, dst.width);
2418
2419 brw_set_dest(p, send, dst);
2420 brw_set_src0(p, send, retype(payload, BRW_REGISTER_TYPE_UD));
2421 brw_inst_set_sfid(devinfo, send, sfid);
2422
2423 return &p->store[setup];
2424 }
2425
2426 static struct brw_inst *
2427 brw_send_indirect_surface_message(struct brw_codegen *p,
2428 unsigned sfid,
2429 struct brw_reg dst,
2430 struct brw_reg payload,
2431 struct brw_reg surface,
2432 unsigned message_len,
2433 unsigned response_len,
2434 bool header_present)
2435 {
2436 const struct gen_device_info *devinfo = p->devinfo;
2437 struct brw_inst *insn;
2438
2439 if (surface.file != BRW_IMMEDIATE_VALUE) {
2440 struct brw_reg addr = retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
2441
2442 brw_push_insn_state(p);
2443 brw_set_default_access_mode(p, BRW_ALIGN_1);
2444 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
2445 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
2446
2447 /* Mask out invalid bits from the surface index to avoid hangs e.g. when
2448 * some surface array is accessed out of bounds.
2449 */
2450 insn = brw_AND(p, addr,
2451 suboffset(vec1(retype(surface, BRW_REGISTER_TYPE_UD)),
2452 BRW_GET_SWZ(surface.swizzle, 0)),
2453 brw_imm_ud(0xff));
2454
2455 brw_pop_insn_state(p);
2456
2457 surface = addr;
2458 }
2459
2460 insn = brw_send_indirect_message(p, sfid, dst, payload, surface);
2461 brw_inst_set_mlen(devinfo, insn, message_len);
2462 brw_inst_set_rlen(devinfo, insn, response_len);
2463 brw_inst_set_header_present(devinfo, insn, header_present);
2464
2465 return insn;
2466 }
2467
2468 static bool
2469 while_jumps_before_offset(const struct gen_device_info *devinfo,
2470 brw_inst *insn, int while_offset, int start_offset)
2471 {
2472 int scale = 16 / brw_jump_scale(devinfo);
2473 int jip = devinfo->gen == 6 ? brw_inst_gen6_jump_count(devinfo, insn)
2474 : brw_inst_jip(devinfo, insn);
2475 assert(jip < 0);
2476 return while_offset + jip * scale <= start_offset;
2477 }
2478
2479
2480 static int
2481 brw_find_next_block_end(struct brw_codegen *p, int start_offset)
2482 {
2483 int offset;
2484 void *store = p->store;
2485 const struct gen_device_info *devinfo = p->devinfo;
2486
2487 int depth = 0;
2488
2489 for (offset = next_offset(devinfo, store, start_offset);
2490 offset < p->next_insn_offset;
2491 offset = next_offset(devinfo, store, offset)) {
2492 brw_inst *insn = store + offset;
2493
2494 switch (brw_inst_opcode(devinfo, insn)) {
2495 case BRW_OPCODE_IF:
2496 depth++;
2497 break;
2498 case BRW_OPCODE_ENDIF:
2499 if (depth == 0)
2500 return offset;
2501 depth--;
2502 break;
2503 case BRW_OPCODE_WHILE:
2504 /* If the while doesn't jump before our instruction, it's the end
2505 * of a sibling do...while loop. Ignore it.
2506 */
2507 if (!while_jumps_before_offset(devinfo, insn, offset, start_offset))
2508 continue;
2509 /* fallthrough */
2510 case BRW_OPCODE_ELSE:
2511 case BRW_OPCODE_HALT:
2512 if (depth == 0)
2513 return offset;
2514 }
2515 }
2516
2517 return 0;
2518 }
2519
2520 /* There is no DO instruction on gen6, so to find the end of the loop
2521 * we have to see if the loop is jumping back before our start
2522 * instruction.
2523 */
2524 static int
2525 brw_find_loop_end(struct brw_codegen *p, int start_offset)
2526 {
2527 const struct gen_device_info *devinfo = p->devinfo;
2528 int offset;
2529 void *store = p->store;
2530
2531 assert(devinfo->gen >= 6);
2532
2533 /* Always start after the instruction (such as a WHILE) we're trying to fix
2534 * up.
2535 */
2536 for (offset = next_offset(devinfo, store, start_offset);
2537 offset < p->next_insn_offset;
2538 offset = next_offset(devinfo, store, offset)) {
2539 brw_inst *insn = store + offset;
2540
2541 if (brw_inst_opcode(devinfo, insn) == BRW_OPCODE_WHILE) {
2542 if (while_jumps_before_offset(devinfo, insn, offset, start_offset))
2543 return offset;
2544 }
2545 }
2546 assert(!"not reached");
2547 return start_offset;
2548 }
2549
2550 /* After program generation, go back and update the UIP and JIP of
2551 * BREAK, CONT, and HALT instructions to their correct locations.
2552 */
2553 void
2554 brw_set_uip_jip(struct brw_codegen *p, int start_offset)
2555 {
2556 const struct gen_device_info *devinfo = p->devinfo;
2557 int offset;
2558 int br = brw_jump_scale(devinfo);
2559 int scale = 16 / br;
2560 void *store = p->store;
2561
2562 if (devinfo->gen < 6)
2563 return;
2564
2565 for (offset = start_offset; offset < p->next_insn_offset; offset += 16) {
2566 brw_inst *insn = store + offset;
2567 assert(brw_inst_cmpt_control(devinfo, insn) == 0);
2568
2569 int block_end_offset = brw_find_next_block_end(p, offset);
2570 switch (brw_inst_opcode(devinfo, insn)) {
2571 case BRW_OPCODE_BREAK:
2572 assert(block_end_offset != 0);
2573 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2574 /* Gen7 UIP points to WHILE; Gen6 points just after it */
2575 brw_inst_set_uip(devinfo, insn,
2576 (brw_find_loop_end(p, offset) - offset +
2577 (devinfo->gen == 6 ? 16 : 0)) / scale);
2578 break;
2579 case BRW_OPCODE_CONTINUE:
2580 assert(block_end_offset != 0);
2581 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2582 brw_inst_set_uip(devinfo, insn,
2583 (brw_find_loop_end(p, offset) - offset) / scale);
2584
2585 assert(brw_inst_uip(devinfo, insn) != 0);
2586 assert(brw_inst_jip(devinfo, insn) != 0);
2587 break;
2588
2589 case BRW_OPCODE_ENDIF: {
2590 int32_t jump = (block_end_offset == 0) ?
2591 1 * br : (block_end_offset - offset) / scale;
2592 if (devinfo->gen >= 7)
2593 brw_inst_set_jip(devinfo, insn, jump);
2594 else
2595 brw_inst_set_gen6_jump_count(devinfo, insn, jump);
2596 break;
2597 }
2598
2599 case BRW_OPCODE_HALT:
2600 /* From the Sandy Bridge PRM (volume 4, part 2, section 8.3.19):
2601 *
2602 * "In case of the halt instruction not inside any conditional
2603 * code block, the value of <JIP> and <UIP> should be the
2604 * same. In case of the halt instruction inside conditional code
2605 * block, the <UIP> should be the end of the program, and the
2606 * <JIP> should be end of the most inner conditional code block."
2607 *
2608 * The uip will have already been set by whoever set up the
2609 * instruction.
2610 */
2611 if (block_end_offset == 0) {
2612 brw_inst_set_jip(devinfo, insn, brw_inst_uip(devinfo, insn));
2613 } else {
2614 brw_inst_set_jip(devinfo, insn, (block_end_offset - offset) / scale);
2615 }
2616 assert(brw_inst_uip(devinfo, insn) != 0);
2617 assert(brw_inst_jip(devinfo, insn) != 0);
2618 break;
2619 }
2620 }
2621 }
2622
2623 void brw_ff_sync(struct brw_codegen *p,
2624 struct brw_reg dest,
2625 unsigned msg_reg_nr,
2626 struct brw_reg src0,
2627 bool allocate,
2628 unsigned response_length,
2629 bool eot)
2630 {
2631 const struct gen_device_info *devinfo = p->devinfo;
2632 brw_inst *insn;
2633
2634 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2635
2636 insn = next_insn(p, BRW_OPCODE_SEND);
2637 brw_set_dest(p, insn, dest);
2638 brw_set_src0(p, insn, src0);
2639 brw_set_src1(p, insn, brw_imm_d(0));
2640
2641 if (devinfo->gen < 6)
2642 brw_inst_set_base_mrf(devinfo, insn, msg_reg_nr);
2643
2644 brw_set_ff_sync_message(p,
2645 insn,
2646 allocate,
2647 response_length,
2648 eot);
2649 }
2650
2651 /**
2652 * Emit the SEND instruction necessary to generate stream output data on Gen6
2653 * (for transform feedback).
2654 *
2655 * If send_commit_msg is true, this is the last piece of stream output data
2656 * from this thread, so send the data as a committed write. According to the
2657 * Sandy Bridge PRM (volume 2 part 1, section 4.5.1):
2658 *
2659 * "Prior to End of Thread with a URB_WRITE, the kernel must ensure all
2660 * writes are complete by sending the final write as a committed write."
2661 */
2662 void
2663 brw_svb_write(struct brw_codegen *p,
2664 struct brw_reg dest,
2665 unsigned msg_reg_nr,
2666 struct brw_reg src0,
2667 unsigned binding_table_index,
2668 bool send_commit_msg)
2669 {
2670 const struct gen_device_info *devinfo = p->devinfo;
2671 const unsigned target_cache =
2672 (devinfo->gen >= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE :
2673 devinfo->gen >= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE :
2674 BRW_DATAPORT_READ_TARGET_RENDER_CACHE);
2675 brw_inst *insn;
2676
2677 gen6_resolve_implied_move(p, &src0, msg_reg_nr);
2678
2679 insn = next_insn(p, BRW_OPCODE_SEND);
2680 brw_set_dest(p, insn, dest);
2681 brw_set_src0(p, insn, src0);
2682 brw_set_src1(p, insn, brw_imm_d(0));
2683 brw_set_dp_write_message(p, insn,
2684 binding_table_index,
2685 0, /* msg_control: ignored */
2686 GEN6_DATAPORT_WRITE_MESSAGE_STREAMED_VB_WRITE,
2687 target_cache,
2688 1, /* msg_length */
2689 true, /* header_present */
2690 0, /* last_render_target: ignored */
2691 send_commit_msg, /* response_length */
2692 0, /* end_of_thread */
2693 send_commit_msg); /* send_commit_msg */
2694 }
2695
2696 static unsigned
2697 brw_surface_payload_size(struct brw_codegen *p,
2698 unsigned num_channels,
2699 bool has_simd4x2,
2700 bool has_simd16)
2701 {
2702 if (has_simd4x2 &&
2703 brw_inst_access_mode(p->devinfo, p->current) == BRW_ALIGN_16)
2704 return 1;
2705 else if (has_simd16 &&
2706 brw_inst_exec_size(p->devinfo, p->current) == BRW_EXECUTE_16)
2707 return 2 * num_channels;
2708 else
2709 return num_channels;
2710 }
2711
2712 static void
2713 brw_set_dp_untyped_atomic_message(struct brw_codegen *p,
2714 brw_inst *insn,
2715 unsigned atomic_op,
2716 bool response_expected)
2717 {
2718 const struct gen_device_info *devinfo = p->devinfo;
2719 unsigned msg_control =
2720 atomic_op | /* Atomic Operation Type: BRW_AOP_* */
2721 (response_expected ? 1 << 5 : 0); /* Return data expected */
2722
2723 if (devinfo->gen >= 8 || devinfo->is_haswell) {
2724 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2725 if (brw_inst_exec_size(devinfo, p->current) != BRW_EXECUTE_16)
2726 msg_control |= 1 << 4; /* SIMD8 mode */
2727
2728 brw_inst_set_dp_msg_type(devinfo, insn,
2729 HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP);
2730 } else {
2731 brw_inst_set_dp_msg_type(devinfo, insn,
2732 HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP_SIMD4X2);
2733 }
2734 } else {
2735 brw_inst_set_dp_msg_type(devinfo, insn,
2736 GEN7_DATAPORT_DC_UNTYPED_ATOMIC_OP);
2737
2738 if (brw_inst_exec_size(devinfo, p->current) != BRW_EXECUTE_16)
2739 msg_control |= 1 << 4; /* SIMD8 mode */
2740 }
2741
2742 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2743 }
2744
2745 void
2746 brw_untyped_atomic(struct brw_codegen *p,
2747 struct brw_reg dst,
2748 struct brw_reg payload,
2749 struct brw_reg surface,
2750 unsigned atomic_op,
2751 unsigned msg_length,
2752 bool response_expected)
2753 {
2754 const struct gen_device_info *devinfo = p->devinfo;
2755 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2756 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2757 GEN7_SFID_DATAPORT_DATA_CACHE);
2758 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
2759 /* Mask out unused components -- This is especially important in Align16
2760 * mode on generations that don't have native support for SIMD4x2 atomics,
2761 * because unused but enabled components will cause the dataport to perform
2762 * additional atomic operations on the addresses that happen to be in the
2763 * uninitialized Y, Z and W coordinates of the payload.
2764 */
2765 const unsigned mask = align1 ? WRITEMASK_XYZW : WRITEMASK_X;
2766 struct brw_inst *insn = brw_send_indirect_surface_message(
2767 p, sfid, brw_writemask(dst, mask), payload, surface, msg_length,
2768 brw_surface_payload_size(p, response_expected,
2769 devinfo->gen >= 8 || devinfo->is_haswell, true),
2770 align1);
2771
2772 brw_set_dp_untyped_atomic_message(
2773 p, insn, atomic_op, response_expected);
2774 }
2775
2776 static void
2777 brw_set_dp_untyped_surface_read_message(struct brw_codegen *p,
2778 struct brw_inst *insn,
2779 unsigned num_channels)
2780 {
2781 const struct gen_device_info *devinfo = p->devinfo;
2782 /* Set mask of 32-bit channels to drop. */
2783 unsigned msg_control = 0xf & (0xf << num_channels);
2784
2785 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2786 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
2787 msg_control |= 1 << 4; /* SIMD16 mode */
2788 else
2789 msg_control |= 2 << 4; /* SIMD8 mode */
2790 }
2791
2792 brw_inst_set_dp_msg_type(devinfo, insn,
2793 (devinfo->gen >= 8 || devinfo->is_haswell ?
2794 HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_READ :
2795 GEN7_DATAPORT_DC_UNTYPED_SURFACE_READ));
2796 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2797 }
2798
2799 void
2800 brw_untyped_surface_read(struct brw_codegen *p,
2801 struct brw_reg dst,
2802 struct brw_reg payload,
2803 struct brw_reg surface,
2804 unsigned msg_length,
2805 unsigned num_channels)
2806 {
2807 const struct gen_device_info *devinfo = p->devinfo;
2808 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2809 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2810 GEN7_SFID_DATAPORT_DATA_CACHE);
2811 struct brw_inst *insn = brw_send_indirect_surface_message(
2812 p, sfid, dst, payload, surface, msg_length,
2813 brw_surface_payload_size(p, num_channels, true, true),
2814 false);
2815
2816 brw_set_dp_untyped_surface_read_message(
2817 p, insn, num_channels);
2818 }
2819
2820 static void
2821 brw_set_dp_untyped_surface_write_message(struct brw_codegen *p,
2822 struct brw_inst *insn,
2823 unsigned num_channels)
2824 {
2825 const struct gen_device_info *devinfo = p->devinfo;
2826 /* Set mask of 32-bit channels to drop. */
2827 unsigned msg_control = 0xf & (0xf << num_channels);
2828
2829 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2830 if (brw_inst_exec_size(devinfo, p->current) == BRW_EXECUTE_16)
2831 msg_control |= 1 << 4; /* SIMD16 mode */
2832 else
2833 msg_control |= 2 << 4; /* SIMD8 mode */
2834 } else {
2835 if (devinfo->gen >= 8 || devinfo->is_haswell)
2836 msg_control |= 0 << 4; /* SIMD4x2 mode */
2837 else
2838 msg_control |= 2 << 4; /* SIMD8 mode */
2839 }
2840
2841 brw_inst_set_dp_msg_type(devinfo, insn,
2842 devinfo->gen >= 8 || devinfo->is_haswell ?
2843 HSW_DATAPORT_DC_PORT1_UNTYPED_SURFACE_WRITE :
2844 GEN7_DATAPORT_DC_UNTYPED_SURFACE_WRITE);
2845 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2846 }
2847
2848 void
2849 brw_untyped_surface_write(struct brw_codegen *p,
2850 struct brw_reg payload,
2851 struct brw_reg surface,
2852 unsigned msg_length,
2853 unsigned num_channels)
2854 {
2855 const struct gen_device_info *devinfo = p->devinfo;
2856 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2857 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2858 GEN7_SFID_DATAPORT_DATA_CACHE);
2859 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
2860 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
2861 const unsigned mask = devinfo->gen == 7 && !devinfo->is_haswell && !align1 ?
2862 WRITEMASK_X : WRITEMASK_XYZW;
2863 struct brw_inst *insn = brw_send_indirect_surface_message(
2864 p, sfid, brw_writemask(brw_null_reg(), mask),
2865 payload, surface, msg_length, 0, align1);
2866
2867 brw_set_dp_untyped_surface_write_message(
2868 p, insn, num_channels);
2869 }
2870
2871 static void
2872 brw_set_dp_typed_atomic_message(struct brw_codegen *p,
2873 struct brw_inst *insn,
2874 unsigned atomic_op,
2875 bool response_expected)
2876 {
2877 const struct gen_device_info *devinfo = p->devinfo;
2878 unsigned msg_control =
2879 atomic_op | /* Atomic Operation Type: BRW_AOP_* */
2880 (response_expected ? 1 << 5 : 0); /* Return data expected */
2881
2882 if (devinfo->gen >= 8 || devinfo->is_haswell) {
2883 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2884 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
2885 msg_control |= 1 << 4; /* Use high 8 slots of the sample mask */
2886
2887 brw_inst_set_dp_msg_type(devinfo, insn,
2888 HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP);
2889 } else {
2890 brw_inst_set_dp_msg_type(devinfo, insn,
2891 HSW_DATAPORT_DC_PORT1_TYPED_ATOMIC_OP_SIMD4X2);
2892 }
2893
2894 } else {
2895 brw_inst_set_dp_msg_type(devinfo, insn,
2896 GEN7_DATAPORT_RC_TYPED_ATOMIC_OP);
2897
2898 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
2899 msg_control |= 1 << 4; /* Use high 8 slots of the sample mask */
2900 }
2901
2902 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2903 }
2904
2905 void
2906 brw_typed_atomic(struct brw_codegen *p,
2907 struct brw_reg dst,
2908 struct brw_reg payload,
2909 struct brw_reg surface,
2910 unsigned atomic_op,
2911 unsigned msg_length,
2912 bool response_expected) {
2913 const struct gen_device_info *devinfo = p->devinfo;
2914 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2915 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2916 GEN6_SFID_DATAPORT_RENDER_CACHE);
2917 const bool align1 = (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
2918 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
2919 const unsigned mask = align1 ? WRITEMASK_XYZW : WRITEMASK_X;
2920 struct brw_inst *insn = brw_send_indirect_surface_message(
2921 p, sfid, brw_writemask(dst, mask), payload, surface, msg_length,
2922 brw_surface_payload_size(p, response_expected,
2923 devinfo->gen >= 8 || devinfo->is_haswell, false),
2924 true);
2925
2926 brw_set_dp_typed_atomic_message(
2927 p, insn, atomic_op, response_expected);
2928 }
2929
2930 static void
2931 brw_set_dp_typed_surface_read_message(struct brw_codegen *p,
2932 struct brw_inst *insn,
2933 unsigned num_channels)
2934 {
2935 const struct gen_device_info *devinfo = p->devinfo;
2936 /* Set mask of unused channels. */
2937 unsigned msg_control = 0xf & (0xf << num_channels);
2938
2939 if (devinfo->gen >= 8 || devinfo->is_haswell) {
2940 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2941 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
2942 msg_control |= 2 << 4; /* Use high 8 slots of the sample mask */
2943 else
2944 msg_control |= 1 << 4; /* Use low 8 slots of the sample mask */
2945 }
2946
2947 brw_inst_set_dp_msg_type(devinfo, insn,
2948 HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_READ);
2949 } else {
2950 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2951 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
2952 msg_control |= 1 << 5; /* Use high 8 slots of the sample mask */
2953 }
2954
2955 brw_inst_set_dp_msg_type(devinfo, insn,
2956 GEN7_DATAPORT_RC_TYPED_SURFACE_READ);
2957 }
2958
2959 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
2960 }
2961
2962 void
2963 brw_typed_surface_read(struct brw_codegen *p,
2964 struct brw_reg dst,
2965 struct brw_reg payload,
2966 struct brw_reg surface,
2967 unsigned msg_length,
2968 unsigned num_channels)
2969 {
2970 const struct gen_device_info *devinfo = p->devinfo;
2971 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
2972 HSW_SFID_DATAPORT_DATA_CACHE_1 :
2973 GEN6_SFID_DATAPORT_RENDER_CACHE);
2974 struct brw_inst *insn = brw_send_indirect_surface_message(
2975 p, sfid, dst, payload, surface, msg_length,
2976 brw_surface_payload_size(p, num_channels,
2977 devinfo->gen >= 8 || devinfo->is_haswell, false),
2978 true);
2979
2980 brw_set_dp_typed_surface_read_message(
2981 p, insn, num_channels);
2982 }
2983
2984 static void
2985 brw_set_dp_typed_surface_write_message(struct brw_codegen *p,
2986 struct brw_inst *insn,
2987 unsigned num_channels)
2988 {
2989 const struct gen_device_info *devinfo = p->devinfo;
2990 /* Set mask of unused channels. */
2991 unsigned msg_control = 0xf & (0xf << num_channels);
2992
2993 if (devinfo->gen >= 8 || devinfo->is_haswell) {
2994 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
2995 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
2996 msg_control |= 2 << 4; /* Use high 8 slots of the sample mask */
2997 else
2998 msg_control |= 1 << 4; /* Use low 8 slots of the sample mask */
2999 }
3000
3001 brw_inst_set_dp_msg_type(devinfo, insn,
3002 HSW_DATAPORT_DC_PORT1_TYPED_SURFACE_WRITE);
3003
3004 } else {
3005 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3006 if (brw_inst_qtr_control(devinfo, p->current) % 2 == 1)
3007 msg_control |= 1 << 5; /* Use high 8 slots of the sample mask */
3008 }
3009
3010 brw_inst_set_dp_msg_type(devinfo, insn,
3011 GEN7_DATAPORT_RC_TYPED_SURFACE_WRITE);
3012 }
3013
3014 brw_inst_set_dp_msg_control(devinfo, insn, msg_control);
3015 }
3016
3017 void
3018 brw_typed_surface_write(struct brw_codegen *p,
3019 struct brw_reg payload,
3020 struct brw_reg surface,
3021 unsigned msg_length,
3022 unsigned num_channels)
3023 {
3024 const struct gen_device_info *devinfo = p->devinfo;
3025 const unsigned sfid = (devinfo->gen >= 8 || devinfo->is_haswell ?
3026 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3027 GEN6_SFID_DATAPORT_RENDER_CACHE);
3028 const bool align1 = (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1);
3029 /* Mask out unused components -- See comment in brw_untyped_atomic(). */
3030 const unsigned mask = (devinfo->gen == 7 && !devinfo->is_haswell && !align1 ?
3031 WRITEMASK_X : WRITEMASK_XYZW);
3032 struct brw_inst *insn = brw_send_indirect_surface_message(
3033 p, sfid, brw_writemask(brw_null_reg(), mask),
3034 payload, surface, msg_length, 0, true);
3035
3036 brw_set_dp_typed_surface_write_message(
3037 p, insn, num_channels);
3038 }
3039
3040 static void
3041 brw_set_memory_fence_message(struct brw_codegen *p,
3042 struct brw_inst *insn,
3043 enum brw_message_target sfid,
3044 bool commit_enable)
3045 {
3046 const struct gen_device_info *devinfo = p->devinfo;
3047
3048 brw_set_message_descriptor(p, insn, sfid,
3049 1 /* message length */,
3050 (commit_enable ? 1 : 0) /* response length */,
3051 true /* header present */,
3052 false);
3053
3054 switch (sfid) {
3055 case GEN6_SFID_DATAPORT_RENDER_CACHE:
3056 brw_inst_set_dp_msg_type(devinfo, insn, GEN7_DATAPORT_RC_MEMORY_FENCE);
3057 break;
3058 case GEN7_SFID_DATAPORT_DATA_CACHE:
3059 brw_inst_set_dp_msg_type(devinfo, insn, GEN7_DATAPORT_DC_MEMORY_FENCE);
3060 break;
3061 default:
3062 unreachable("Not reached");
3063 }
3064
3065 if (commit_enable)
3066 brw_inst_set_dp_msg_control(devinfo, insn, 1 << 5);
3067 }
3068
3069 void
3070 brw_memory_fence(struct brw_codegen *p,
3071 struct brw_reg dst)
3072 {
3073 const struct gen_device_info *devinfo = p->devinfo;
3074 const bool commit_enable = devinfo->gen == 7 && !devinfo->is_haswell;
3075 struct brw_inst *insn;
3076
3077 brw_push_insn_state(p);
3078 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3079 brw_set_default_exec_size(p, BRW_EXECUTE_1);
3080 dst = vec1(dst);
3081
3082 /* Set dst as destination for dependency tracking, the MEMORY_FENCE
3083 * message doesn't write anything back.
3084 */
3085 insn = next_insn(p, BRW_OPCODE_SEND);
3086 dst = retype(dst, BRW_REGISTER_TYPE_UW);
3087 brw_set_dest(p, insn, dst);
3088 brw_set_src0(p, insn, dst);
3089 brw_set_memory_fence_message(p, insn, GEN7_SFID_DATAPORT_DATA_CACHE,
3090 commit_enable);
3091
3092 if (devinfo->gen == 7 && !devinfo->is_haswell) {
3093 /* IVB does typed surface access through the render cache, so we need to
3094 * flush it too. Use a different register so both flushes can be
3095 * pipelined by the hardware.
3096 */
3097 insn = next_insn(p, BRW_OPCODE_SEND);
3098 brw_set_dest(p, insn, offset(dst, 1));
3099 brw_set_src0(p, insn, offset(dst, 1));
3100 brw_set_memory_fence_message(p, insn, GEN6_SFID_DATAPORT_RENDER_CACHE,
3101 commit_enable);
3102
3103 /* Now write the response of the second message into the response of the
3104 * first to trigger a pipeline stall -- This way future render and data
3105 * cache messages will be properly ordered with respect to past data and
3106 * render cache messages.
3107 */
3108 brw_MOV(p, dst, offset(dst, 1));
3109 }
3110
3111 brw_pop_insn_state(p);
3112 }
3113
3114 void
3115 brw_pixel_interpolator_query(struct brw_codegen *p,
3116 struct brw_reg dest,
3117 struct brw_reg mrf,
3118 bool noperspective,
3119 unsigned mode,
3120 struct brw_reg data,
3121 unsigned msg_length,
3122 unsigned response_length)
3123 {
3124 const struct gen_device_info *devinfo = p->devinfo;
3125 struct brw_inst *insn;
3126 const uint16_t exec_size = brw_inst_exec_size(devinfo, p->current);
3127
3128 /* brw_send_indirect_message will automatically use a direct send message
3129 * if data is actually immediate.
3130 */
3131 insn = brw_send_indirect_message(p,
3132 GEN7_SFID_PIXEL_INTERPOLATOR,
3133 dest,
3134 mrf,
3135 vec1(data));
3136 brw_inst_set_mlen(devinfo, insn, msg_length);
3137 brw_inst_set_rlen(devinfo, insn, response_length);
3138
3139 brw_inst_set_pi_simd_mode(devinfo, insn, exec_size == BRW_EXECUTE_16);
3140 brw_inst_set_pi_slot_group(devinfo, insn, 0); /* zero unless 32/64px dispatch */
3141 brw_inst_set_pi_nopersp(devinfo, insn, noperspective);
3142 brw_inst_set_pi_message_type(devinfo, insn, mode);
3143 }
3144
3145 void
3146 brw_find_live_channel(struct brw_codegen *p, struct brw_reg dst,
3147 struct brw_reg mask)
3148 {
3149 const struct gen_device_info *devinfo = p->devinfo;
3150 const unsigned exec_size = 1 << brw_inst_exec_size(devinfo, p->current);
3151 const unsigned qtr_control = brw_inst_qtr_control(devinfo, p->current);
3152 brw_inst *inst;
3153
3154 assert(devinfo->gen >= 7);
3155 assert(mask.type == BRW_REGISTER_TYPE_UD);
3156
3157 brw_push_insn_state(p);
3158
3159 if (brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1) {
3160 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3161
3162 if (devinfo->gen >= 8) {
3163 /* Getting the first active channel index is easy on Gen8: Just find
3164 * the first bit set in the execution mask. The register exists on
3165 * HSW already but it reads back as all ones when the current
3166 * instruction has execution masking disabled, so it's kind of
3167 * useless.
3168 */
3169 struct brw_reg exec_mask =
3170 retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD);
3171
3172 if (mask.file != BRW_IMMEDIATE_VALUE || mask.ud != 0xffffffff) {
3173 /* Unfortunately, ce0 does not take into account the thread
3174 * dispatch mask, which may be a problem in cases where it's not
3175 * tightly packed (i.e. it doesn't have the form '2^n - 1' for
3176 * some n). Combine ce0 with the given dispatch (or vector) mask
3177 * to mask off those channels which were never dispatched by the
3178 * hardware.
3179 */
3180 brw_SHR(p, vec1(dst), mask, brw_imm_ud(qtr_control * 8));
3181 brw_AND(p, vec1(dst), exec_mask, vec1(dst));
3182 exec_mask = vec1(dst);
3183 }
3184
3185 /* Quarter control has the effect of magically shifting the value of
3186 * ce0 so you'll get the first active channel relative to the
3187 * specified quarter control as result.
3188 */
3189 inst = brw_FBL(p, vec1(dst), exec_mask);
3190 } else {
3191 const struct brw_reg flag = brw_flag_reg(1, 0);
3192
3193 brw_MOV(p, retype(flag, BRW_REGISTER_TYPE_UD), brw_imm_ud(0));
3194
3195 /* Run enough instructions returning zero with execution masking and
3196 * a conditional modifier enabled in order to get the full execution
3197 * mask in f1.0. We could use a single 32-wide move here if it
3198 * weren't because of the hardware bug that causes channel enables to
3199 * be applied incorrectly to the second half of 32-wide instructions
3200 * on Gen7.
3201 */
3202 const unsigned lower_size = MIN2(16, exec_size);
3203 for (unsigned i = 0; i < exec_size / lower_size; i++) {
3204 inst = brw_MOV(p, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW),
3205 brw_imm_uw(0));
3206 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_ENABLE);
3207 brw_inst_set_group(devinfo, inst, lower_size * i + 8 * qtr_control);
3208 brw_inst_set_cond_modifier(devinfo, inst, BRW_CONDITIONAL_Z);
3209 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3210 brw_inst_set_exec_size(devinfo, inst, cvt(lower_size) - 1);
3211 }
3212
3213 /* Find the first bit set in the exec_size-wide portion of the flag
3214 * register that was updated by the last sequence of MOV
3215 * instructions.
3216 */
3217 const enum brw_reg_type type = brw_int_type(exec_size / 8, false);
3218 brw_FBL(p, vec1(dst), byte_offset(retype(flag, type), qtr_control));
3219 }
3220 } else {
3221 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3222
3223 if (devinfo->gen >= 8 &&
3224 mask.file == BRW_IMMEDIATE_VALUE && mask.ud == 0xffffffff) {
3225 /* In SIMD4x2 mode the first active channel index is just the
3226 * negation of the first bit of the mask register. Note that ce0
3227 * doesn't take into account the dispatch mask, so the Gen7 path
3228 * should be used instead unless you have the guarantee that the
3229 * dispatch mask is tightly packed (i.e. it has the form '2^n - 1'
3230 * for some n).
3231 */
3232 inst = brw_AND(p, brw_writemask(dst, WRITEMASK_X),
3233 negate(retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD)),
3234 brw_imm_ud(1));
3235
3236 } else {
3237 /* Overwrite the destination without and with execution masking to
3238 * find out which of the channels is active.
3239 */
3240 brw_push_insn_state(p);
3241 brw_set_default_exec_size(p, BRW_EXECUTE_4);
3242 brw_MOV(p, brw_writemask(vec4(dst), WRITEMASK_X),
3243 brw_imm_ud(1));
3244
3245 inst = brw_MOV(p, brw_writemask(vec4(dst), WRITEMASK_X),
3246 brw_imm_ud(0));
3247 brw_pop_insn_state(p);
3248 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_ENABLE);
3249 }
3250 }
3251
3252 brw_pop_insn_state(p);
3253 }
3254
3255 void
3256 brw_broadcast(struct brw_codegen *p,
3257 struct brw_reg dst,
3258 struct brw_reg src,
3259 struct brw_reg idx)
3260 {
3261 const struct gen_device_info *devinfo = p->devinfo;
3262 const bool align1 = brw_inst_access_mode(devinfo, p->current) == BRW_ALIGN_1;
3263 brw_inst *inst;
3264
3265 brw_push_insn_state(p);
3266 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3267 brw_set_default_exec_size(p, align1 ? BRW_EXECUTE_1 : BRW_EXECUTE_4);
3268
3269 assert(src.file == BRW_GENERAL_REGISTER_FILE &&
3270 src.address_mode == BRW_ADDRESS_DIRECT);
3271
3272 if ((src.vstride == 0 && (src.hstride == 0 || !align1)) ||
3273 idx.file == BRW_IMMEDIATE_VALUE) {
3274 /* Trivial, the source is already uniform or the index is a constant.
3275 * We will typically not get here if the optimizer is doing its job, but
3276 * asserting would be mean.
3277 */
3278 const unsigned i = idx.file == BRW_IMMEDIATE_VALUE ? idx.ud : 0;
3279 brw_MOV(p, dst,
3280 (align1 ? stride(suboffset(src, i), 0, 1, 0) :
3281 stride(suboffset(src, 4 * i), 0, 4, 1)));
3282 } else {
3283 if (align1) {
3284 const struct brw_reg addr =
3285 retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD);
3286 const unsigned offset = src.nr * REG_SIZE + src.subnr;
3287 /* Limit in bytes of the signed indirect addressing immediate. */
3288 const unsigned limit = 512;
3289
3290 brw_push_insn_state(p);
3291 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3292 brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
3293
3294 /* Take into account the component size and horizontal stride. */
3295 assert(src.vstride == src.hstride + src.width);
3296 brw_SHL(p, addr, vec1(idx),
3297 brw_imm_ud(_mesa_logbase2(type_sz(src.type)) +
3298 src.hstride - 1));
3299
3300 /* We can only address up to limit bytes using the indirect
3301 * addressing immediate, account for the difference if the source
3302 * register is above this limit.
3303 */
3304 if (offset >= limit)
3305 brw_ADD(p, addr, addr, brw_imm_ud(offset - offset % limit));
3306
3307 brw_pop_insn_state(p);
3308
3309 /* Use indirect addressing to fetch the specified component. */
3310 brw_MOV(p, dst,
3311 retype(brw_vec1_indirect(addr.subnr, offset % limit),
3312 src.type));
3313 } else {
3314 /* In SIMD4x2 mode the index can be either zero or one, replicate it
3315 * to all bits of a flag register,
3316 */
3317 inst = brw_MOV(p,
3318 brw_null_reg(),
3319 stride(brw_swizzle(idx, BRW_SWIZZLE_XXXX), 4, 4, 1));
3320 brw_inst_set_pred_control(devinfo, inst, BRW_PREDICATE_NONE);
3321 brw_inst_set_cond_modifier(devinfo, inst, BRW_CONDITIONAL_NZ);
3322 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3323
3324 /* and use predicated SEL to pick the right channel. */
3325 inst = brw_SEL(p, dst,
3326 stride(suboffset(src, 4), 4, 4, 1),
3327 stride(src, 4, 4, 1));
3328 brw_inst_set_pred_control(devinfo, inst, BRW_PREDICATE_NORMAL);
3329 brw_inst_set_flag_reg_nr(devinfo, inst, 1);
3330 }
3331 }
3332
3333 brw_pop_insn_state(p);
3334 }
3335
3336 /**
3337 * This instruction is generated as a single-channel align1 instruction by
3338 * both the VS and FS stages when using INTEL_DEBUG=shader_time.
3339 *
3340 * We can't use the typed atomic op in the FS because that has the execution
3341 * mask ANDed with the pixel mask, but we just want to write the one dword for
3342 * all the pixels.
3343 *
3344 * We don't use the SIMD4x2 atomic ops in the VS because want to just write
3345 * one u32. So we use the same untyped atomic write message as the pixel
3346 * shader.
3347 *
3348 * The untyped atomic operation requires a BUFFER surface type with RAW
3349 * format, and is only accessible through the legacy DATA_CACHE dataport
3350 * messages.
3351 */
3352 void brw_shader_time_add(struct brw_codegen *p,
3353 struct brw_reg payload,
3354 uint32_t surf_index)
3355 {
3356 const unsigned sfid = (p->devinfo->gen >= 8 || p->devinfo->is_haswell ?
3357 HSW_SFID_DATAPORT_DATA_CACHE_1 :
3358 GEN7_SFID_DATAPORT_DATA_CACHE);
3359 assert(p->devinfo->gen >= 7);
3360
3361 brw_push_insn_state(p);
3362 brw_set_default_access_mode(p, BRW_ALIGN_1);
3363 brw_set_default_mask_control(p, BRW_MASK_DISABLE);
3364 brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);
3365 brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);
3366
3367 /* We use brw_vec1_reg and unmasked because we want to increment the given
3368 * offset only once.
3369 */
3370 brw_set_dest(p, send, brw_vec1_reg(BRW_ARCHITECTURE_REGISTER_FILE,
3371 BRW_ARF_NULL, 0));
3372 brw_set_src0(p, send, brw_vec1_reg(payload.file,
3373 payload.nr, 0));
3374 brw_set_src1(p, send, brw_imm_ud(0));
3375 brw_set_message_descriptor(p, send, sfid, 2, 0, false, false);
3376 brw_inst_set_binding_table_index(p->devinfo, send, surf_index);
3377 brw_set_dp_untyped_atomic_message(p, send, BRW_AOP_ADD, false);
3378
3379 brw_pop_insn_state(p);
3380 }
3381
3382
3383 /**
3384 * Emit the SEND message for a barrier
3385 */
3386 void
3387 brw_barrier(struct brw_codegen *p, struct brw_reg src)
3388 {
3389 const struct gen_device_info *devinfo = p->devinfo;
3390 struct brw_inst *inst;
3391
3392 assert(devinfo->gen >= 7);
3393
3394 brw_push_insn_state(p);
3395 brw_set_default_access_mode(p, BRW_ALIGN_1);
3396 inst = next_insn(p, BRW_OPCODE_SEND);
3397 brw_set_dest(p, inst, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
3398 brw_set_src0(p, inst, src);
3399 brw_set_src1(p, inst, brw_null_reg());
3400
3401 brw_set_message_descriptor(p, inst, BRW_SFID_MESSAGE_GATEWAY,
3402 1 /* msg_length */,
3403 0 /* response_length */,
3404 false /* header_present */,
3405 false /* end_of_thread */);
3406
3407 brw_inst_set_gateway_notify(devinfo, inst, 1);
3408 brw_inst_set_gateway_subfuncid(devinfo, inst,
3409 BRW_MESSAGE_GATEWAY_SFID_BARRIER_MSG);
3410
3411 brw_inst_set_mask_control(devinfo, inst, BRW_MASK_DISABLE);
3412 brw_pop_insn_state(p);
3413 }
3414
3415
3416 /**
3417 * Emit the wait instruction for a barrier
3418 */
3419 void
3420 brw_WAIT(struct brw_codegen *p)
3421 {
3422 const struct gen_device_info *devinfo = p->devinfo;
3423 struct brw_inst *insn;
3424
3425 struct brw_reg src = brw_notification_reg();
3426
3427 insn = next_insn(p, BRW_OPCODE_WAIT);
3428 brw_set_dest(p, insn, src);
3429 brw_set_src0(p, insn, src);
3430 brw_set_src1(p, insn, brw_null_reg());
3431
3432 brw_inst_set_exec_size(devinfo, insn, BRW_EXECUTE_1);
3433 brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);
3434 }