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