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