i965/fs: Reimplement emit_texture() in terms of logical send messages.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_visitor.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file brw_fs_visitor.cpp
25 *
26 * This file supports generating the FS LIR from the GLSL IR. The LIR
27 * makes it easier to do backend-specific optimizations than doing so
28 * in the GLSL IR or in the native code.
29 */
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "util/register_allocate.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
40 #include "brw_eu.h"
41 #include "brw_wm.h"
42 #include "brw_cs.h"
43 #include "brw_vec4.h"
44 #include "brw_fs.h"
45 #include "main/uniforms.h"
46 #include "glsl/glsl_types.h"
47 #include "glsl/ir_optimization.h"
48 #include "program/sampler.h"
49
50 using namespace brw;
51
52 fs_reg *
53 fs_visitor::emit_vs_system_value(int location)
54 {
55 fs_reg *reg = new(this->mem_ctx)
56 fs_reg(ATTR, VERT_ATTRIB_MAX, BRW_REGISTER_TYPE_D);
57 brw_vs_prog_data *vs_prog_data = (brw_vs_prog_data *) prog_data;
58
59 switch (location) {
60 case SYSTEM_VALUE_BASE_VERTEX:
61 reg->reg_offset = 0;
62 vs_prog_data->uses_vertexid = true;
63 break;
64 case SYSTEM_VALUE_VERTEX_ID:
65 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
66 reg->reg_offset = 2;
67 vs_prog_data->uses_vertexid = true;
68 break;
69 case SYSTEM_VALUE_INSTANCE_ID:
70 reg->reg_offset = 3;
71 vs_prog_data->uses_instanceid = true;
72 break;
73 default:
74 unreachable("not reached");
75 }
76
77 return reg;
78 }
79
80 fs_inst *
81 fs_visitor::emit_texture_gen4(ir_texture_opcode op, fs_reg dst,
82 fs_reg coordinate, int coord_components,
83 fs_reg shadow_c,
84 fs_reg lod, fs_reg dPdy, int grad_components,
85 uint32_t sampler)
86 {
87 int mlen;
88 int base_mrf = 1;
89 bool simd16 = false;
90 fs_reg orig_dst;
91
92 /* g0 header. */
93 mlen = 1;
94
95 if (shadow_c.file != BAD_FILE) {
96 for (int i = 0; i < coord_components; i++) {
97 bld.MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate);
98 coordinate = offset(coordinate, bld, 1);
99 }
100
101 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
102 * the unused slots must be zeroed.
103 */
104 for (int i = coord_components; i < 3; i++) {
105 bld.MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f));
106 }
107 mlen += 3;
108
109 if (op == ir_tex) {
110 /* There's no plain shadow compare message, so we use shadow
111 * compare with a bias of 0.0.
112 */
113 bld.MOV(fs_reg(MRF, base_mrf + mlen), fs_reg(0.0f));
114 mlen++;
115 } else if (op == ir_txb || op == ir_txl) {
116 bld.MOV(fs_reg(MRF, base_mrf + mlen), lod);
117 mlen++;
118 } else {
119 unreachable("Should not get here.");
120 }
121
122 bld.MOV(fs_reg(MRF, base_mrf + mlen), shadow_c);
123 mlen++;
124 } else if (op == ir_tex) {
125 for (int i = 0; i < coord_components; i++) {
126 bld.MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate);
127 coordinate = offset(coordinate, bld, 1);
128 }
129 /* zero the others. */
130 for (int i = coord_components; i<3; i++) {
131 bld.MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f));
132 }
133 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
134 mlen += 3;
135 } else if (op == ir_txd) {
136 fs_reg &dPdx = lod;
137
138 for (int i = 0; i < coord_components; i++) {
139 bld.MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate);
140 coordinate = offset(coordinate, bld, 1);
141 }
142 /* the slots for u and v are always present, but r is optional */
143 mlen += MAX2(coord_components, 2);
144
145 /* P = u, v, r
146 * dPdx = dudx, dvdx, drdx
147 * dPdy = dudy, dvdy, drdy
148 *
149 * 1-arg: Does not exist.
150 *
151 * 2-arg: dudx dvdx dudy dvdy
152 * dPdx.x dPdx.y dPdy.x dPdy.y
153 * m4 m5 m6 m7
154 *
155 * 3-arg: dudx dvdx drdx dudy dvdy drdy
156 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
157 * m5 m6 m7 m8 m9 m10
158 */
159 for (int i = 0; i < grad_components; i++) {
160 bld.MOV(fs_reg(MRF, base_mrf + mlen), dPdx);
161 dPdx = offset(dPdx, bld, 1);
162 }
163 mlen += MAX2(grad_components, 2);
164
165 for (int i = 0; i < grad_components; i++) {
166 bld.MOV(fs_reg(MRF, base_mrf + mlen), dPdy);
167 dPdy = offset(dPdy, bld, 1);
168 }
169 mlen += MAX2(grad_components, 2);
170 } else if (op == ir_txs) {
171 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
172 simd16 = true;
173 bld.MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), lod);
174 mlen += 2;
175 } else {
176 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
177 * instructions. We'll need to do SIMD16 here.
178 */
179 simd16 = true;
180 assert(op == ir_txb || op == ir_txl || op == ir_txf);
181
182 for (int i = 0; i < coord_components; i++) {
183 bld.MOV(fs_reg(MRF, base_mrf + mlen + i * 2, coordinate.type),
184 coordinate);
185 coordinate = offset(coordinate, bld, 1);
186 }
187
188 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
189 * be necessary for TXF (ld), but seems wise to do for all messages.
190 */
191 for (int i = coord_components; i < 3; i++) {
192 bld.MOV(fs_reg(MRF, base_mrf + mlen + i * 2), fs_reg(0.0f));
193 }
194
195 /* lod/bias appears after u/v/r. */
196 mlen += 6;
197
198 bld.MOV(fs_reg(MRF, base_mrf + mlen, lod.type), lod);
199 mlen++;
200
201 /* The unused upper half. */
202 mlen++;
203 }
204
205 if (simd16) {
206 /* Now, since we're doing simd16, the return is 2 interleaved
207 * vec4s where the odd-indexed ones are junk. We'll need to move
208 * this weirdness around to the expected layout.
209 */
210 orig_dst = dst;
211 dst = fs_reg(GRF, alloc.allocate(8), orig_dst.type);
212 }
213
214 enum opcode opcode;
215 switch (op) {
216 case ir_tex: opcode = SHADER_OPCODE_TEX; break;
217 case ir_txb: opcode = FS_OPCODE_TXB; break;
218 case ir_txl: opcode = SHADER_OPCODE_TXL; break;
219 case ir_txd: opcode = SHADER_OPCODE_TXD; break;
220 case ir_txs: opcode = SHADER_OPCODE_TXS; break;
221 case ir_txf: opcode = SHADER_OPCODE_TXF; break;
222 default:
223 unreachable("not reached");
224 }
225
226 fs_inst *inst = bld.emit(opcode, dst, reg_undef, fs_reg(sampler));
227 inst->base_mrf = base_mrf;
228 inst->mlen = mlen;
229 inst->header_size = 1;
230 inst->regs_written = simd16 ? 8 : 4;
231
232 if (simd16) {
233 for (int i = 0; i < 4; i++) {
234 bld.MOV(orig_dst, dst);
235 orig_dst = offset(orig_dst, bld, 1);
236 dst = offset(dst, bld, 2);
237 }
238 }
239
240 return inst;
241 }
242
243 fs_inst *
244 fs_visitor::emit_texture_gen4_simd16(ir_texture_opcode op, fs_reg dst,
245 fs_reg coordinate, int vector_elements,
246 fs_reg shadow_c, fs_reg lod,
247 uint32_t sampler)
248 {
249 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F);
250 bool has_lod = op == ir_txl || op == ir_txb || op == ir_txf || op == ir_txs;
251
252 if (has_lod && shadow_c.file != BAD_FILE)
253 no16("TXB and TXL with shadow comparison unsupported in SIMD16.");
254
255 if (op == ir_txd)
256 no16("textureGrad unsupported in SIMD16.");
257
258 /* Copy the coordinates. */
259 for (int i = 0; i < vector_elements; i++) {
260 bld.MOV(retype(offset(message, bld, i), coordinate.type), coordinate);
261 coordinate = offset(coordinate, bld, 1);
262 }
263
264 fs_reg msg_end = offset(message, bld, vector_elements);
265
266 /* Messages other than sample and ld require all three components */
267 if (vector_elements > 0 && (has_lod || shadow_c.file != BAD_FILE)) {
268 for (int i = vector_elements; i < 3; i++) {
269 bld.MOV(offset(message, bld, i), fs_reg(0.0f));
270 }
271 msg_end = offset(message, bld, 3);
272 }
273
274 if (has_lod) {
275 fs_reg msg_lod = retype(msg_end, op == ir_txf ?
276 BRW_REGISTER_TYPE_UD : BRW_REGISTER_TYPE_F);
277 bld.MOV(msg_lod, lod);
278 msg_end = offset(msg_lod, bld, 1);
279 }
280
281 if (shadow_c.file != BAD_FILE) {
282 fs_reg msg_ref = offset(message, bld, 3 + has_lod);
283 bld.MOV(msg_ref, shadow_c);
284 msg_end = offset(msg_ref, bld, 1);
285 }
286
287 enum opcode opcode;
288 switch (op) {
289 case ir_tex: opcode = SHADER_OPCODE_TEX; break;
290 case ir_txb: opcode = FS_OPCODE_TXB; break;
291 case ir_txd: opcode = SHADER_OPCODE_TXD; break;
292 case ir_txl: opcode = SHADER_OPCODE_TXL; break;
293 case ir_txs: opcode = SHADER_OPCODE_TXS; break;
294 case ir_txf: opcode = SHADER_OPCODE_TXF; break;
295 default: unreachable("not reached");
296 }
297
298 fs_inst *inst = bld.emit(opcode, dst, reg_undef, fs_reg(sampler));
299 inst->base_mrf = message.reg - 1;
300 inst->mlen = msg_end.reg - inst->base_mrf;
301 inst->header_size = 1;
302 inst->regs_written = 8;
303
304 return inst;
305 }
306
307 /* gen5's sampler has slots for u, v, r, array index, then optional
308 * parameters like shadow comparitor or LOD bias. If optional
309 * parameters aren't present, those base slots are optional and don't
310 * need to be included in the message.
311 *
312 * We don't fill in the unnecessary slots regardless, which may look
313 * surprising in the disassembly.
314 */
315 fs_inst *
316 fs_visitor::emit_texture_gen5(ir_texture_opcode op, fs_reg dst,
317 fs_reg coordinate, int vector_elements,
318 fs_reg shadow_c,
319 fs_reg lod, fs_reg lod2, int grad_components,
320 fs_reg sample_index, uint32_t sampler,
321 bool has_offset)
322 {
323 int reg_width = dispatch_width / 8;
324 unsigned header_size = 0;
325
326 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F);
327 fs_reg msg_coords = message;
328
329 if (has_offset) {
330 /* The offsets set up by the ir_texture visitor are in the
331 * m1 header, so we can't go headerless.
332 */
333 header_size = 1;
334 message.reg--;
335 }
336
337 for (int i = 0; i < vector_elements; i++) {
338 bld.MOV(retype(offset(msg_coords, bld, i), coordinate.type), coordinate);
339 coordinate = offset(coordinate, bld, 1);
340 }
341 fs_reg msg_end = offset(msg_coords, bld, vector_elements);
342 fs_reg msg_lod = offset(msg_coords, bld, 4);
343
344 if (shadow_c.file != BAD_FILE) {
345 fs_reg msg_shadow = msg_lod;
346 bld.MOV(msg_shadow, shadow_c);
347 msg_lod = offset(msg_shadow, bld, 1);
348 msg_end = msg_lod;
349 }
350
351 enum opcode opcode;
352 switch (op) {
353 case ir_tex:
354 opcode = SHADER_OPCODE_TEX;
355 break;
356 case ir_txb:
357 bld.MOV(msg_lod, lod);
358 msg_end = offset(msg_lod, bld, 1);
359
360 opcode = FS_OPCODE_TXB;
361 break;
362 case ir_txl:
363 bld.MOV(msg_lod, lod);
364 msg_end = offset(msg_lod, bld, 1);
365
366 opcode = SHADER_OPCODE_TXL;
367 break;
368 case ir_txd: {
369 /**
370 * P = u, v, r
371 * dPdx = dudx, dvdx, drdx
372 * dPdy = dudy, dvdy, drdy
373 *
374 * Load up these values:
375 * - dudx dudy dvdx dvdy drdx drdy
376 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
377 */
378 msg_end = msg_lod;
379 for (int i = 0; i < grad_components; i++) {
380 bld.MOV(msg_end, lod);
381 lod = offset(lod, bld, 1);
382 msg_end = offset(msg_end, bld, 1);
383
384 bld.MOV(msg_end, lod2);
385 lod2 = offset(lod2, bld, 1);
386 msg_end = offset(msg_end, bld, 1);
387 }
388
389 opcode = SHADER_OPCODE_TXD;
390 break;
391 }
392 case ir_txs:
393 msg_lod = retype(msg_end, BRW_REGISTER_TYPE_UD);
394 bld.MOV(msg_lod, lod);
395 msg_end = offset(msg_lod, bld, 1);
396
397 opcode = SHADER_OPCODE_TXS;
398 break;
399 case ir_query_levels:
400 msg_lod = msg_end;
401 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), fs_reg(0u));
402 msg_end = offset(msg_lod, bld, 1);
403
404 opcode = SHADER_OPCODE_TXS;
405 break;
406 case ir_txf:
407 msg_lod = offset(msg_coords, bld, 3);
408 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), lod);
409 msg_end = offset(msg_lod, bld, 1);
410
411 opcode = SHADER_OPCODE_TXF;
412 break;
413 case ir_txf_ms:
414 msg_lod = offset(msg_coords, bld, 3);
415 /* lod */
416 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), fs_reg(0u));
417 /* sample index */
418 bld.MOV(retype(offset(msg_lod, bld, 1), BRW_REGISTER_TYPE_UD), sample_index);
419 msg_end = offset(msg_lod, bld, 2);
420
421 opcode = SHADER_OPCODE_TXF_CMS;
422 break;
423 case ir_lod:
424 opcode = SHADER_OPCODE_LOD;
425 break;
426 case ir_tg4:
427 opcode = SHADER_OPCODE_TG4;
428 break;
429 default:
430 unreachable("not reached");
431 }
432
433 fs_inst *inst = bld.emit(opcode, dst, reg_undef, fs_reg(sampler));
434 inst->base_mrf = message.reg;
435 inst->mlen = msg_end.reg - message.reg;
436 inst->header_size = header_size;
437 inst->regs_written = 4 * reg_width;
438
439 if (inst->mlen > MAX_SAMPLER_MESSAGE_SIZE) {
440 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE)
441 " disallowed by hardware\n");
442 }
443
444 return inst;
445 }
446
447 static bool
448 is_high_sampler(const struct brw_device_info *devinfo, fs_reg sampler)
449 {
450 if (devinfo->gen < 8 && !devinfo->is_haswell)
451 return false;
452
453 return sampler.file != IMM || sampler.fixed_hw_reg.dw1.ud >= 16;
454 }
455
456 fs_inst *
457 fs_visitor::emit_texture_gen7(ir_texture_opcode op, fs_reg dst,
458 fs_reg coordinate, int coord_components,
459 fs_reg shadow_c,
460 fs_reg lod, fs_reg lod2, int grad_components,
461 fs_reg sample_index, fs_reg mcs, fs_reg sampler,
462 fs_reg offset_value)
463 {
464 int reg_width = dispatch_width / 8;
465 unsigned header_size = 0;
466
467 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, MAX_SAMPLER_MESSAGE_SIZE);
468 for (int i = 0; i < MAX_SAMPLER_MESSAGE_SIZE; i++) {
469 sources[i] = vgrf(glsl_type::float_type);
470 }
471 int length = 0;
472
473 if (op == ir_tg4 || offset_value.file != BAD_FILE ||
474 is_high_sampler(devinfo, sampler)) {
475 /* For general texture offsets (no txf workaround), we need a header to
476 * put them in. Note that we're only reserving space for it in the
477 * message payload as it will be initialized implicitly by the
478 * generator.
479 *
480 * * ir4_tg4 needs to place its channel select in the header,
481 * for interaction with ARB_texture_swizzle
482 *
483 * The sampler index is only 4-bits, so for larger sampler numbers we
484 * need to offset the Sampler State Pointer in the header.
485 */
486 header_size = 1;
487 sources[0] = fs_reg();
488 length++;
489 }
490
491 if (shadow_c.file != BAD_FILE) {
492 bld.MOV(sources[length], shadow_c);
493 length++;
494 }
495
496 bool has_nonconstant_offset =
497 offset_value.file != BAD_FILE && offset_value.file != IMM;
498 bool coordinate_done = false;
499
500 /* The sampler can only meaningfully compute LOD for fragment shader
501 * messages. For all other stages, we change the opcode to ir_txl and
502 * hardcode the LOD to 0.
503 */
504 if (stage != MESA_SHADER_FRAGMENT && op == ir_tex) {
505 op = ir_txl;
506 lod = fs_reg(0.0f);
507 }
508
509 /* Set up the LOD info */
510 switch (op) {
511 case ir_tex:
512 case ir_lod:
513 break;
514 case ir_txb:
515 bld.MOV(sources[length], lod);
516 length++;
517 break;
518 case ir_txl:
519 bld.MOV(sources[length], lod);
520 length++;
521 break;
522 case ir_txd: {
523 no16("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
524
525 /* Load dPdx and the coordinate together:
526 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
527 */
528 for (int i = 0; i < coord_components; i++) {
529 bld.MOV(sources[length], coordinate);
530 coordinate = offset(coordinate, bld, 1);
531 length++;
532
533 /* For cube map array, the coordinate is (u,v,r,ai) but there are
534 * only derivatives for (u, v, r).
535 */
536 if (i < grad_components) {
537 bld.MOV(sources[length], lod);
538 lod = offset(lod, bld, 1);
539 length++;
540
541 bld.MOV(sources[length], lod2);
542 lod2 = offset(lod2, bld, 1);
543 length++;
544 }
545 }
546
547 coordinate_done = true;
548 break;
549 }
550 case ir_txs:
551 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), lod);
552 length++;
553 break;
554 case ir_query_levels:
555 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), fs_reg(0u));
556 length++;
557 break;
558 case ir_txf:
559 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
560 * On Gen9 they are u, v, lod, r
561 */
562
563 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
564 coordinate = offset(coordinate, bld, 1);
565 length++;
566
567 if (devinfo->gen >= 9) {
568 if (coord_components >= 2) {
569 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
570 coordinate = offset(coordinate, bld, 1);
571 }
572 length++;
573 }
574
575 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), lod);
576 length++;
577
578 for (int i = devinfo->gen >= 9 ? 2 : 1; i < coord_components; i++) {
579 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
580 coordinate = offset(coordinate, bld, 1);
581 length++;
582 }
583
584 coordinate_done = true;
585 break;
586 case ir_txf_ms:
587 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), sample_index);
588 length++;
589
590 /* data from the multisample control surface */
591 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), mcs);
592 length++;
593
594 /* there is no offsetting for this message; just copy in the integer
595 * texture coordinates
596 */
597 for (int i = 0; i < coord_components; i++) {
598 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
599 coordinate = offset(coordinate, bld, 1);
600 length++;
601 }
602
603 coordinate_done = true;
604 break;
605 case ir_tg4:
606 if (has_nonconstant_offset) {
607 if (shadow_c.file != BAD_FILE)
608 no16("Gen7 does not support gather4_po_c in SIMD16 mode.");
609
610 /* More crazy intermixing */
611 for (int i = 0; i < 2; i++) { /* u, v */
612 bld.MOV(sources[length], coordinate);
613 coordinate = offset(coordinate, bld, 1);
614 length++;
615 }
616
617 for (int i = 0; i < 2; i++) { /* offu, offv */
618 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), offset_value);
619 offset_value = offset(offset_value, bld, 1);
620 length++;
621 }
622
623 if (coord_components == 3) { /* r if present */
624 bld.MOV(sources[length], coordinate);
625 coordinate = offset(coordinate, bld, 1);
626 length++;
627 }
628
629 coordinate_done = true;
630 }
631 break;
632 }
633
634 /* Set up the coordinate (except for cases where it was done above) */
635 if (!coordinate_done) {
636 for (int i = 0; i < coord_components; i++) {
637 bld.MOV(sources[length], coordinate);
638 coordinate = offset(coordinate, bld, 1);
639 length++;
640 }
641 }
642
643 int mlen;
644 if (reg_width == 2)
645 mlen = length * reg_width - header_size;
646 else
647 mlen = length * reg_width;
648
649 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
650 BRW_REGISTER_TYPE_F);
651 bld.LOAD_PAYLOAD(src_payload, sources, length, header_size);
652
653 /* Generate the SEND */
654 enum opcode opcode;
655 switch (op) {
656 case ir_tex: opcode = SHADER_OPCODE_TEX; break;
657 case ir_txb: opcode = FS_OPCODE_TXB; break;
658 case ir_txl: opcode = SHADER_OPCODE_TXL; break;
659 case ir_txd: opcode = SHADER_OPCODE_TXD; break;
660 case ir_txf: opcode = SHADER_OPCODE_TXF; break;
661 case ir_txf_ms: opcode = SHADER_OPCODE_TXF_CMS; break;
662 case ir_txs: opcode = SHADER_OPCODE_TXS; break;
663 case ir_query_levels: opcode = SHADER_OPCODE_TXS; break;
664 case ir_lod: opcode = SHADER_OPCODE_LOD; break;
665 case ir_tg4:
666 if (has_nonconstant_offset)
667 opcode = SHADER_OPCODE_TG4_OFFSET;
668 else
669 opcode = SHADER_OPCODE_TG4;
670 break;
671 default:
672 unreachable("not reached");
673 }
674 fs_inst *inst = bld.emit(opcode, dst, src_payload, sampler);
675 inst->base_mrf = -1;
676 inst->mlen = mlen;
677 inst->header_size = header_size;
678 inst->regs_written = 4 * reg_width;
679
680 if (inst->mlen > MAX_SAMPLER_MESSAGE_SIZE) {
681 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE)
682 " disallowed by hardware\n");
683 }
684
685 return inst;
686 }
687
688 fs_reg
689 fs_visitor::rescale_texcoord(fs_reg coordinate, int coord_components,
690 bool is_rect, uint32_t sampler, int texunit)
691 {
692 bool needs_gl_clamp = true;
693 fs_reg scale_x, scale_y;
694
695 /* The 965 requires the EU to do the normalization of GL rectangle
696 * texture coordinates. We use the program parameter state
697 * tracking to get the scaling factor.
698 */
699 if (is_rect &&
700 (devinfo->gen < 6 ||
701 (devinfo->gen >= 6 && (key_tex->gl_clamp_mask[0] & (1 << sampler) ||
702 key_tex->gl_clamp_mask[1] & (1 << sampler))))) {
703 struct gl_program_parameter_list *params = prog->Parameters;
704 int tokens[STATE_LENGTH] = {
705 STATE_INTERNAL,
706 STATE_TEXRECT_SCALE,
707 texunit,
708 0,
709 0
710 };
711
712 no16("rectangle scale uniform setup not supported on SIMD16\n");
713 if (dispatch_width == 16) {
714 return coordinate;
715 }
716
717 GLuint index = _mesa_add_state_reference(params,
718 (gl_state_index *)tokens);
719 /* Try to find existing copies of the texrect scale uniforms. */
720 for (unsigned i = 0; i < uniforms; i++) {
721 if (stage_prog_data->param[i] ==
722 &prog->Parameters->ParameterValues[index][0]) {
723 scale_x = fs_reg(UNIFORM, i);
724 scale_y = fs_reg(UNIFORM, i + 1);
725 break;
726 }
727 }
728
729 /* If we didn't already set them up, do so now. */
730 if (scale_x.file == BAD_FILE) {
731 scale_x = fs_reg(UNIFORM, uniforms);
732 scale_y = fs_reg(UNIFORM, uniforms + 1);
733
734 stage_prog_data->param[uniforms++] =
735 &prog->Parameters->ParameterValues[index][0];
736 stage_prog_data->param[uniforms++] =
737 &prog->Parameters->ParameterValues[index][1];
738 }
739 }
740
741 /* The 965 requires the EU to do the normalization of GL rectangle
742 * texture coordinates. We use the program parameter state
743 * tracking to get the scaling factor.
744 */
745 if (devinfo->gen < 6 && is_rect) {
746 fs_reg dst = fs_reg(GRF, alloc.allocate(coord_components));
747 fs_reg src = coordinate;
748 coordinate = dst;
749
750 bld.MUL(dst, src, scale_x);
751 dst = offset(dst, bld, 1);
752 src = offset(src, bld, 1);
753 bld.MUL(dst, src, scale_y);
754 } else if (is_rect) {
755 /* On gen6+, the sampler handles the rectangle coordinates
756 * natively, without needing rescaling. But that means we have
757 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
758 * not [0, 1] like the default case below.
759 */
760 needs_gl_clamp = false;
761
762 for (int i = 0; i < 2; i++) {
763 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
764 fs_reg chan = coordinate;
765 chan = offset(chan, bld, i);
766
767 set_condmod(BRW_CONDITIONAL_GE,
768 bld.emit(BRW_OPCODE_SEL, chan, chan, fs_reg(0.0f)));
769
770 /* Our parameter comes in as 1.0/width or 1.0/height,
771 * because that's what people normally want for doing
772 * texture rectangle handling. We need width or height
773 * for clamping, but we don't care enough to make a new
774 * parameter type, so just invert back.
775 */
776 fs_reg limit = vgrf(glsl_type::float_type);
777 bld.MOV(limit, i == 0 ? scale_x : scale_y);
778 bld.emit(SHADER_OPCODE_RCP, limit, limit);
779
780 set_condmod(BRW_CONDITIONAL_L,
781 bld.emit(BRW_OPCODE_SEL, chan, chan, limit));
782 }
783 }
784 }
785
786 if (coord_components > 0 && needs_gl_clamp) {
787 for (int i = 0; i < MIN2(coord_components, 3); i++) {
788 if (key_tex->gl_clamp_mask[i] & (1 << sampler)) {
789 fs_reg chan = coordinate;
790 chan = offset(chan, bld, i);
791 set_saturate(true, bld.MOV(chan, chan));
792 }
793 }
794 }
795 return coordinate;
796 }
797
798 /* Sample from the MCS surface attached to this multisample texture. */
799 fs_reg
800 fs_visitor::emit_mcs_fetch(fs_reg coordinate, int components, fs_reg sampler)
801 {
802 int reg_width = dispatch_width / 8;
803 fs_reg payload = fs_reg(GRF, alloc.allocate(components * reg_width),
804 BRW_REGISTER_TYPE_F);
805 fs_reg dest = vgrf(glsl_type::uvec4_type);
806 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, components);
807
808 /* parameters are: u, v, r; missing parameters are treated as zero */
809 for (int i = 0; i < components; i++) {
810 sources[i] = vgrf(glsl_type::float_type);
811 bld.MOV(retype(sources[i], BRW_REGISTER_TYPE_D), coordinate);
812 coordinate = offset(coordinate, bld, 1);
813 }
814
815 bld.LOAD_PAYLOAD(payload, sources, components, 0);
816
817 fs_inst *inst = bld.emit(SHADER_OPCODE_TXF_MCS, dest, payload, sampler);
818 inst->base_mrf = -1;
819 inst->mlen = components * reg_width;
820 inst->header_size = 0;
821 inst->regs_written = 4 * reg_width; /* we only care about one reg of
822 * response, but the sampler always
823 * writes 4/8
824 */
825
826 return dest;
827 }
828
829 void
830 fs_visitor::emit_texture(ir_texture_opcode op,
831 const glsl_type *dest_type,
832 fs_reg coordinate, int coord_components,
833 fs_reg shadow_c,
834 fs_reg lod, fs_reg lod2, int grad_components,
835 fs_reg sample_index,
836 fs_reg offset_value,
837 fs_reg mcs,
838 int gather_component,
839 bool is_cube_array,
840 bool is_rect,
841 uint32_t sampler,
842 fs_reg sampler_reg, int texunit)
843 {
844 fs_inst *inst = NULL;
845
846 if (op == ir_tg4) {
847 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
848 * emitting anything other than setting up the constant result.
849 */
850 int swiz = GET_SWZ(key_tex->swizzles[sampler], gather_component);
851 if (swiz == SWIZZLE_ZERO || swiz == SWIZZLE_ONE) {
852
853 fs_reg res = vgrf(glsl_type::vec4_type);
854 this->result = res;
855
856 for (int i=0; i<4; i++) {
857 bld.MOV(res, fs_reg(swiz == SWIZZLE_ZERO ? 0.0f : 1.0f));
858 res = offset(res, bld, 1);
859 }
860 return;
861 }
862 }
863
864 if (op == ir_query_levels) {
865 /* textureQueryLevels() is implemented in terms of TXS so we need to
866 * pass a valid LOD argument.
867 */
868 assert(lod.file == BAD_FILE);
869 lod = fs_reg(0u);
870 }
871
872 if (coordinate.file != BAD_FILE) {
873 /* FINISHME: Texture coordinate rescaling doesn't work with non-constant
874 * samplers. This should only be a problem with GL_CLAMP on Gen7.
875 */
876 coordinate = rescale_texcoord(coordinate, coord_components, is_rect,
877 sampler, texunit);
878 }
879
880 /* Writemasking doesn't eliminate channels on SIMD8 texture
881 * samples, so don't worry about them.
882 */
883 fs_reg dst = vgrf(glsl_type::get_instance(dest_type->base_type, 4, 1));
884 const fs_reg srcs[] = {
885 coordinate, shadow_c, lod, lod2,
886 sample_index, mcs, sampler_reg, offset_value,
887 fs_reg(coord_components), fs_reg(grad_components)
888 };
889 enum opcode opcode;
890
891 switch (op) {
892 case ir_tex:
893 opcode = SHADER_OPCODE_TEX_LOGICAL;
894 break;
895 case ir_txb:
896 opcode = FS_OPCODE_TXB_LOGICAL;
897 break;
898 case ir_txl:
899 opcode = SHADER_OPCODE_TXL_LOGICAL;
900 break;
901 case ir_txd:
902 opcode = SHADER_OPCODE_TXD_LOGICAL;
903 break;
904 case ir_txf:
905 opcode = SHADER_OPCODE_TXF_LOGICAL;
906 break;
907 case ir_txf_ms:
908 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
909 break;
910 case ir_txs:
911 case ir_query_levels:
912 opcode = SHADER_OPCODE_TXS_LOGICAL;
913 break;
914 case ir_lod:
915 opcode = SHADER_OPCODE_LOD_LOGICAL;
916 break;
917 case ir_tg4:
918 opcode = (offset_value.file != BAD_FILE && offset_value.file != IMM ?
919 SHADER_OPCODE_TG4_OFFSET_LOGICAL : SHADER_OPCODE_TG4_LOGICAL);
920 break;
921 default:
922 unreachable("Invalid texture opcode.");
923 }
924
925 inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
926 inst->regs_written = 4 * dispatch_width / 8;
927
928 if (shadow_c.file != BAD_FILE)
929 inst->shadow_compare = true;
930
931 if (offset_value.file == IMM)
932 inst->offset = offset_value.fixed_hw_reg.dw1.ud;
933
934 if (op == ir_tg4) {
935 inst->offset |=
936 gather_channel(gather_component, sampler) << 16; /* M0.2:16-17 */
937
938 if (devinfo->gen == 6)
939 emit_gen6_gather_wa(key_tex->gen6_gather_wa[sampler], dst);
940 }
941
942 /* fixup #layers for cube map arrays */
943 if (op == ir_txs && is_cube_array) {
944 fs_reg depth = offset(dst, bld, 2);
945 fs_reg fixed_depth = vgrf(glsl_type::int_type);
946 bld.emit(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, fs_reg(6));
947
948 fs_reg *fixed_payload = ralloc_array(mem_ctx, fs_reg, inst->regs_written);
949 int components = inst->regs_written / (inst->exec_size / 8);
950 for (int i = 0; i < components; i++) {
951 if (i == 2) {
952 fixed_payload[i] = fixed_depth;
953 } else {
954 fixed_payload[i] = offset(dst, bld, i);
955 }
956 }
957 bld.LOAD_PAYLOAD(dst, fixed_payload, components, 0);
958 }
959
960 swizzle_result(op, dest_type->vector_elements, dst, sampler);
961 }
962
963 /**
964 * Apply workarounds for Gen6 gather with UINT/SINT
965 */
966 void
967 fs_visitor::emit_gen6_gather_wa(uint8_t wa, fs_reg dst)
968 {
969 if (!wa)
970 return;
971
972 int width = (wa & WA_8BIT) ? 8 : 16;
973
974 for (int i = 0; i < 4; i++) {
975 fs_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);
976 /* Convert from UNORM to UINT */
977 bld.MUL(dst_f, dst_f, fs_reg((float)((1 << width) - 1)));
978 bld.MOV(dst, dst_f);
979
980 if (wa & WA_SIGN) {
981 /* Reinterpret the UINT value as a signed INT value by
982 * shifting the sign bit into place, then shifting back
983 * preserving sign.
984 */
985 bld.SHL(dst, dst, fs_reg(32 - width));
986 bld.ASR(dst, dst, fs_reg(32 - width));
987 }
988
989 dst = offset(dst, bld, 1);
990 }
991 }
992
993 /**
994 * Set up the gather channel based on the swizzle, for gather4.
995 */
996 uint32_t
997 fs_visitor::gather_channel(int orig_chan, uint32_t sampler)
998 {
999 int swiz = GET_SWZ(key_tex->swizzles[sampler], orig_chan);
1000 switch (swiz) {
1001 case SWIZZLE_X: return 0;
1002 case SWIZZLE_Y:
1003 /* gather4 sampler is broken for green channel on RG32F --
1004 * we must ask for blue instead.
1005 */
1006 if (key_tex->gather_channel_quirk_mask & (1 << sampler))
1007 return 2;
1008 return 1;
1009 case SWIZZLE_Z: return 2;
1010 case SWIZZLE_W: return 3;
1011 default:
1012 unreachable("Not reached"); /* zero, one swizzles handled already */
1013 }
1014 }
1015
1016 /**
1017 * Swizzle the result of a texture result. This is necessary for
1018 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1019 */
1020 void
1021 fs_visitor::swizzle_result(ir_texture_opcode op, int dest_components,
1022 fs_reg orig_val, uint32_t sampler)
1023 {
1024 if (op == ir_query_levels) {
1025 /* # levels is in .w */
1026 this->result = offset(orig_val, bld, 3);
1027 return;
1028 }
1029
1030 this->result = orig_val;
1031
1032 /* txs,lod don't actually sample the texture, so swizzling the result
1033 * makes no sense.
1034 */
1035 if (op == ir_txs || op == ir_lod || op == ir_tg4)
1036 return;
1037
1038 if (dest_components == 1) {
1039 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1040 } else if (key_tex->swizzles[sampler] != SWIZZLE_NOOP) {
1041 fs_reg swizzled_result = vgrf(glsl_type::vec4_type);
1042 swizzled_result.type = orig_val.type;
1043
1044 for (int i = 0; i < 4; i++) {
1045 int swiz = GET_SWZ(key_tex->swizzles[sampler], i);
1046 fs_reg l = swizzled_result;
1047 l = offset(l, bld, i);
1048
1049 if (swiz == SWIZZLE_ZERO) {
1050 bld.MOV(l, fs_reg(0.0f));
1051 } else if (swiz == SWIZZLE_ONE) {
1052 bld.MOV(l, fs_reg(1.0f));
1053 } else {
1054 bld.MOV(l, offset(orig_val, bld,
1055 GET_SWZ(key_tex->swizzles[sampler], i)));
1056 }
1057 }
1058 this->result = swizzled_result;
1059 }
1060 }
1061
1062 /**
1063 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
1064 *
1065 * Many GLSL shaders contain the following pattern:
1066 *
1067 * x = condition ? foo : bar
1068 *
1069 * The compiler emits an ir_if tree for this, since each subexpression might be
1070 * a complex tree that could have side-effects or short-circuit logic.
1071 *
1072 * However, the common case is to simply select one of two constants or
1073 * variable values---which is exactly what SEL is for. In this case, the
1074 * assembly looks like:
1075 *
1076 * (+f0) IF
1077 * MOV dst src0
1078 * ELSE
1079 * MOV dst src1
1080 * ENDIF
1081 *
1082 * which can be easily translated into:
1083 *
1084 * (+f0) SEL dst src0 src1
1085 *
1086 * If src0 is an immediate value, we promote it to a temporary GRF.
1087 */
1088 bool
1089 fs_visitor::try_replace_with_sel()
1090 {
1091 fs_inst *endif_inst = (fs_inst *) instructions.get_tail();
1092 assert(endif_inst->opcode == BRW_OPCODE_ENDIF);
1093
1094 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
1095 int opcodes[] = {
1096 BRW_OPCODE_IF, BRW_OPCODE_MOV, BRW_OPCODE_ELSE, BRW_OPCODE_MOV,
1097 };
1098
1099 fs_inst *match = (fs_inst *) endif_inst->prev;
1100 for (int i = 0; i < 4; i++) {
1101 if (match->is_head_sentinel() || match->opcode != opcodes[4-i-1])
1102 return false;
1103 match = (fs_inst *) match->prev;
1104 }
1105
1106 /* The opcodes match; it looks like the right sequence of instructions. */
1107 fs_inst *else_mov = (fs_inst *) endif_inst->prev;
1108 fs_inst *then_mov = (fs_inst *) else_mov->prev->prev;
1109 fs_inst *if_inst = (fs_inst *) then_mov->prev;
1110
1111 /* Check that the MOVs are the right form. */
1112 if (then_mov->dst.equals(else_mov->dst) &&
1113 !then_mov->is_partial_write() &&
1114 !else_mov->is_partial_write()) {
1115
1116 /* Remove the matched instructions; we'll emit a SEL to replace them. */
1117 while (!if_inst->next->is_tail_sentinel())
1118 if_inst->next->exec_node::remove();
1119 if_inst->exec_node::remove();
1120
1121 /* Only the last source register can be a constant, so if the MOV in
1122 * the "then" clause uses a constant, we need to put it in a temporary.
1123 */
1124 fs_reg src0(then_mov->src[0]);
1125 if (src0.file == IMM) {
1126 src0 = vgrf(glsl_type::float_type);
1127 src0.type = then_mov->src[0].type;
1128 bld.MOV(src0, then_mov->src[0]);
1129 }
1130
1131 if (if_inst->conditional_mod) {
1132 /* Sandybridge-specific IF with embedded comparison */
1133 bld.CMP(bld.null_reg_d(), if_inst->src[0], if_inst->src[1],
1134 if_inst->conditional_mod);
1135 set_predicate(BRW_PREDICATE_NORMAL,
1136 bld.emit(BRW_OPCODE_SEL, then_mov->dst,
1137 src0, else_mov->src[0]));
1138 } else {
1139 /* Separate CMP and IF instructions */
1140 set_predicate_inv(if_inst->predicate, if_inst->predicate_inverse,
1141 bld.emit(BRW_OPCODE_SEL, then_mov->dst,
1142 src0, else_mov->src[0]));
1143 }
1144
1145 return true;
1146 }
1147
1148 return false;
1149 }
1150
1151 void
1152 fs_visitor::emit_untyped_atomic(unsigned atomic_op, unsigned surf_index,
1153 fs_reg dst, fs_reg offset, fs_reg src0,
1154 fs_reg src1)
1155 {
1156 int reg_width = dispatch_width / 8;
1157 int length = 0;
1158
1159 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 4);
1160
1161 sources[0] = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1162 /* Initialize the sample mask in the message header. */
1163 bld.exec_all().MOV(sources[0], fs_reg(0u));
1164
1165 if (stage == MESA_SHADER_FRAGMENT) {
1166 if (((brw_wm_prog_data*)this->prog_data)->uses_kill) {
1167 bld.exec_all()
1168 .MOV(component(sources[0], 7), brw_flag_reg(0, 1));
1169 } else {
1170 bld.exec_all()
1171 .MOV(component(sources[0], 7),
1172 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD));
1173 }
1174 } else {
1175 /* The execution mask is part of the side-band information sent together with
1176 * the message payload to the data port. It's implicitly ANDed with the sample
1177 * mask sent in the header to compute the actual set of channels that execute
1178 * the atomic operation.
1179 */
1180 assert(stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_COMPUTE);
1181 bld.exec_all()
1182 .MOV(component(sources[0], 7), fs_reg(0xffffu));
1183 }
1184 length++;
1185
1186 /* Set the atomic operation offset. */
1187 sources[1] = vgrf(glsl_type::uint_type);
1188 bld.MOV(sources[1], offset);
1189 length++;
1190
1191 /* Set the atomic operation arguments. */
1192 if (src0.file != BAD_FILE) {
1193 sources[length] = vgrf(glsl_type::uint_type);
1194 bld.MOV(sources[length], src0);
1195 length++;
1196 }
1197
1198 if (src1.file != BAD_FILE) {
1199 sources[length] = vgrf(glsl_type::uint_type);
1200 bld.MOV(sources[length], src1);
1201 length++;
1202 }
1203
1204 int mlen = 1 + (length - 1) * reg_width;
1205 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
1206 BRW_REGISTER_TYPE_UD);
1207 bld.LOAD_PAYLOAD(src_payload, sources, length, 1);
1208
1209 /* Emit the instruction. */
1210 fs_inst *inst = bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC, dst, src_payload,
1211 fs_reg(surf_index), fs_reg(atomic_op));
1212 inst->mlen = mlen;
1213 }
1214
1215 void
1216 fs_visitor::emit_untyped_surface_read(unsigned surf_index, fs_reg dst,
1217 fs_reg offset)
1218 {
1219 int reg_width = dispatch_width / 8;
1220
1221 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, 2);
1222
1223 sources[0] = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1224 /* Initialize the sample mask in the message header. */
1225 bld.exec_all()
1226 .MOV(sources[0], fs_reg(0u));
1227
1228 if (stage == MESA_SHADER_FRAGMENT) {
1229 if (((brw_wm_prog_data*)this->prog_data)->uses_kill) {
1230 bld.exec_all()
1231 .MOV(component(sources[0], 7), brw_flag_reg(0, 1));
1232 } else {
1233 bld.exec_all()
1234 .MOV(component(sources[0], 7),
1235 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD));
1236 }
1237 } else {
1238 /* The execution mask is part of the side-band information sent together with
1239 * the message payload to the data port. It's implicitly ANDed with the sample
1240 * mask sent in the header to compute the actual set of channels that execute
1241 * the atomic operation.
1242 */
1243 assert(stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_COMPUTE);
1244 bld.exec_all()
1245 .MOV(component(sources[0], 7), fs_reg(0xffffu));
1246 }
1247
1248 /* Set the surface read offset. */
1249 sources[1] = vgrf(glsl_type::uint_type);
1250 bld.MOV(sources[1], offset);
1251
1252 int mlen = 1 + reg_width;
1253 fs_reg src_payload = fs_reg(GRF, alloc.allocate(mlen),
1254 BRW_REGISTER_TYPE_UD);
1255 fs_inst *inst = bld.LOAD_PAYLOAD(src_payload, sources, 2, 1);
1256
1257 /* Emit the instruction. */
1258 inst = bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ, dst, src_payload,
1259 fs_reg(surf_index), fs_reg(1));
1260 inst->mlen = mlen;
1261 }
1262
1263 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1264 void
1265 fs_visitor::emit_dummy_fs()
1266 {
1267 int reg_width = dispatch_width / 8;
1268
1269 /* Everyone's favorite color. */
1270 const float color[4] = { 1.0, 0.0, 1.0, 0.0 };
1271 for (int i = 0; i < 4; i++) {
1272 bld.MOV(fs_reg(MRF, 2 + i * reg_width, BRW_REGISTER_TYPE_F),
1273 fs_reg(color[i]));
1274 }
1275
1276 fs_inst *write;
1277 write = bld.emit(FS_OPCODE_FB_WRITE);
1278 write->eot = true;
1279 if (devinfo->gen >= 6) {
1280 write->base_mrf = 2;
1281 write->mlen = 4 * reg_width;
1282 } else {
1283 write->header_size = 2;
1284 write->base_mrf = 0;
1285 write->mlen = 2 + 4 * reg_width;
1286 }
1287
1288 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
1289 * varying to avoid GPU hangs, so set that.
1290 */
1291 brw_wm_prog_data *wm_prog_data = (brw_wm_prog_data *) this->prog_data;
1292 wm_prog_data->num_varying_inputs = devinfo->gen < 6 ? 1 : 0;
1293 memset(wm_prog_data->urb_setup, -1,
1294 sizeof(wm_prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
1295
1296 /* We don't have any uniforms. */
1297 stage_prog_data->nr_params = 0;
1298 stage_prog_data->nr_pull_params = 0;
1299 stage_prog_data->curb_read_length = 0;
1300 stage_prog_data->dispatch_grf_start_reg = 2;
1301 wm_prog_data->dispatch_grf_start_reg_16 = 2;
1302 grf_used = 1; /* Gen4-5 don't allow zero GRF blocks */
1303
1304 calculate_cfg();
1305 }
1306
1307 /* The register location here is relative to the start of the URB
1308 * data. It will get adjusted to be a real location before
1309 * generate_code() time.
1310 */
1311 struct brw_reg
1312 fs_visitor::interp_reg(int location, int channel)
1313 {
1314 assert(stage == MESA_SHADER_FRAGMENT);
1315 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1316 int regnr = prog_data->urb_setup[location] * 2 + channel / 2;
1317 int stride = (channel & 1) * 4;
1318
1319 assert(prog_data->urb_setup[location] != -1);
1320
1321 return brw_vec1_grf(regnr, stride);
1322 }
1323
1324 /** Emits the interpolation for the varying inputs. */
1325 void
1326 fs_visitor::emit_interpolation_setup_gen4()
1327 {
1328 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1329
1330 fs_builder abld = bld.annotate("compute pixel centers");
1331 this->pixel_x = vgrf(glsl_type::uint_type);
1332 this->pixel_y = vgrf(glsl_type::uint_type);
1333 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1334 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1335 abld.ADD(this->pixel_x,
1336 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1337 fs_reg(brw_imm_v(0x10101010)));
1338 abld.ADD(this->pixel_y,
1339 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1340 fs_reg(brw_imm_v(0x11001100)));
1341
1342 abld = bld.annotate("compute pixel deltas from v0");
1343
1344 this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
1345 vgrf(glsl_type::vec2_type);
1346 const fs_reg &delta_xy = this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC];
1347 const fs_reg xstart(negate(brw_vec1_grf(1, 0)));
1348 const fs_reg ystart(negate(brw_vec1_grf(1, 1)));
1349
1350 if (devinfo->has_pln && dispatch_width == 16) {
1351 for (unsigned i = 0; i < 2; i++) {
1352 abld.half(i).ADD(half(offset(delta_xy, abld, i), 0),
1353 half(this->pixel_x, i), xstart);
1354 abld.half(i).ADD(half(offset(delta_xy, abld, i), 1),
1355 half(this->pixel_y, i), ystart);
1356 }
1357 } else {
1358 abld.ADD(offset(delta_xy, abld, 0), this->pixel_x, xstart);
1359 abld.ADD(offset(delta_xy, abld, 1), this->pixel_y, ystart);
1360 }
1361
1362 abld = bld.annotate("compute pos.w and 1/pos.w");
1363 /* Compute wpos.w. It's always in our setup, since it's needed to
1364 * interpolate the other attributes.
1365 */
1366 this->wpos_w = vgrf(glsl_type::float_type);
1367 abld.emit(FS_OPCODE_LINTERP, wpos_w, delta_xy,
1368 interp_reg(VARYING_SLOT_POS, 3));
1369 /* Compute the pixel 1/W value from wpos.w. */
1370 this->pixel_w = vgrf(glsl_type::float_type);
1371 abld.emit(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);
1372 }
1373
1374 /** Emits the interpolation for the varying inputs. */
1375 void
1376 fs_visitor::emit_interpolation_setup_gen6()
1377 {
1378 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1379
1380 fs_builder abld = bld.annotate("compute pixel centers");
1381 if (devinfo->gen >= 8 || dispatch_width == 8) {
1382 /* The "Register Region Restrictions" page says for BDW (and newer,
1383 * presumably):
1384 *
1385 * "When destination spans two registers, the source may be one or
1386 * two registers. The destination elements must be evenly split
1387 * between the two registers."
1388 *
1389 * Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16 to
1390 * compute our pixel centers.
1391 */
1392 fs_reg int_pixel_xy(GRF, alloc.allocate(dispatch_width / 8),
1393 BRW_REGISTER_TYPE_UW);
1394
1395 const fs_builder dbld = abld.exec_all().group(dispatch_width * 2, 0);
1396 dbld.ADD(int_pixel_xy,
1397 fs_reg(stride(suboffset(g1_uw, 4), 1, 4, 0)),
1398 fs_reg(brw_imm_v(0x11001010)));
1399
1400 this->pixel_x = vgrf(glsl_type::float_type);
1401 this->pixel_y = vgrf(glsl_type::float_type);
1402 abld.emit(FS_OPCODE_PIXEL_X, this->pixel_x, int_pixel_xy);
1403 abld.emit(FS_OPCODE_PIXEL_Y, this->pixel_y, int_pixel_xy);
1404 } else {
1405 /* The "Register Region Restrictions" page says for SNB, IVB, HSW:
1406 *
1407 * "When destination spans two registers, the source MUST span two
1408 * registers."
1409 *
1410 * Since the GRF source of the ADD will only read a single register, we
1411 * must do two separate ADDs in SIMD16.
1412 */
1413 fs_reg int_pixel_x = vgrf(glsl_type::uint_type);
1414 fs_reg int_pixel_y = vgrf(glsl_type::uint_type);
1415 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
1416 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
1417 abld.ADD(int_pixel_x,
1418 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1419 fs_reg(brw_imm_v(0x10101010)));
1420 abld.ADD(int_pixel_y,
1421 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1422 fs_reg(brw_imm_v(0x11001100)));
1423
1424 /* As of gen6, we can no longer mix float and int sources. We have
1425 * to turn the integer pixel centers into floats for their actual
1426 * use.
1427 */
1428 this->pixel_x = vgrf(glsl_type::float_type);
1429 this->pixel_y = vgrf(glsl_type::float_type);
1430 abld.MOV(this->pixel_x, int_pixel_x);
1431 abld.MOV(this->pixel_y, int_pixel_y);
1432 }
1433
1434 abld = bld.annotate("compute pos.w");
1435 this->pixel_w = fs_reg(brw_vec8_grf(payload.source_w_reg, 0));
1436 this->wpos_w = vgrf(glsl_type::float_type);
1437 abld.emit(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
1438
1439 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
1440 uint8_t reg = payload.barycentric_coord_reg[i];
1441 this->delta_xy[i] = fs_reg(brw_vec16_grf(reg, 0));
1442 }
1443 }
1444
1445 static enum brw_conditional_mod
1446 cond_for_alpha_func(GLenum func)
1447 {
1448 switch(func) {
1449 case GL_GREATER:
1450 return BRW_CONDITIONAL_G;
1451 case GL_GEQUAL:
1452 return BRW_CONDITIONAL_GE;
1453 case GL_LESS:
1454 return BRW_CONDITIONAL_L;
1455 case GL_LEQUAL:
1456 return BRW_CONDITIONAL_LE;
1457 case GL_EQUAL:
1458 return BRW_CONDITIONAL_EQ;
1459 case GL_NOTEQUAL:
1460 return BRW_CONDITIONAL_NEQ;
1461 default:
1462 unreachable("Not reached");
1463 }
1464 }
1465
1466 /**
1467 * Alpha test support for when we compile it into the shader instead
1468 * of using the normal fixed-function alpha test.
1469 */
1470 void
1471 fs_visitor::emit_alpha_test()
1472 {
1473 assert(stage == MESA_SHADER_FRAGMENT);
1474 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1475 const fs_builder abld = bld.annotate("Alpha test");
1476
1477 fs_inst *cmp;
1478 if (key->alpha_test_func == GL_ALWAYS)
1479 return;
1480
1481 if (key->alpha_test_func == GL_NEVER) {
1482 /* f0.1 = 0 */
1483 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
1484 BRW_REGISTER_TYPE_UW));
1485 cmp = abld.CMP(bld.null_reg_f(), some_reg, some_reg,
1486 BRW_CONDITIONAL_NEQ);
1487 } else {
1488 /* RT0 alpha */
1489 fs_reg color = offset(outputs[0], bld, 3);
1490
1491 /* f0.1 &= func(color, ref) */
1492 cmp = abld.CMP(bld.null_reg_f(), color, fs_reg(key->alpha_test_ref),
1493 cond_for_alpha_func(key->alpha_test_func));
1494 }
1495 cmp->predicate = BRW_PREDICATE_NORMAL;
1496 cmp->flag_subreg = 1;
1497 }
1498
1499 fs_inst *
1500 fs_visitor::emit_single_fb_write(const fs_builder &bld,
1501 fs_reg color0, fs_reg color1,
1502 fs_reg src0_alpha, unsigned components)
1503 {
1504 assert(stage == MESA_SHADER_FRAGMENT);
1505 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1506
1507 /* Hand over gl_FragDepth or the payload depth. */
1508 const fs_reg dst_depth = (payload.dest_depth_reg ?
1509 fs_reg(brw_vec8_grf(payload.dest_depth_reg, 0)) :
1510 fs_reg());
1511 fs_reg src_depth;
1512
1513 if (source_depth_to_render_target) {
1514 if (prog->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
1515 src_depth = frag_depth;
1516 else
1517 src_depth = fs_reg(brw_vec8_grf(payload.source_depth_reg, 0));
1518 }
1519
1520 const fs_reg sources[] = {
1521 color0, color1, src0_alpha, src_depth, dst_depth, sample_mask,
1522 fs_reg(components)
1523 };
1524 fs_inst *write = bld.emit(FS_OPCODE_FB_WRITE_LOGICAL, fs_reg(),
1525 sources, ARRAY_SIZE(sources));
1526
1527 if (prog_data->uses_kill) {
1528 write->predicate = BRW_PREDICATE_NORMAL;
1529 write->flag_subreg = 1;
1530 }
1531
1532 return write;
1533 }
1534
1535 void
1536 fs_visitor::emit_fb_writes()
1537 {
1538 assert(stage == MESA_SHADER_FRAGMENT);
1539 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1540 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1541
1542 fs_inst *inst = NULL;
1543
1544 if (source_depth_to_render_target && devinfo->gen == 6) {
1545 /* For outputting oDepth on gen6, SIMD8 writes have to be used. This
1546 * would require SIMD8 moves of each half to message regs, e.g. by using
1547 * the SIMD lowering pass. Unfortunately this is more difficult than it
1548 * sounds because the SIMD8 single-source message lacks channel selects
1549 * for the second and third subspans.
1550 */
1551 no16("Missing support for simd16 depth writes on gen6\n");
1552 }
1553
1554 if (do_dual_src) {
1555 const fs_builder abld = bld.annotate("FB dual-source write");
1556
1557 inst = emit_single_fb_write(abld, this->outputs[0],
1558 this->dual_src_output, reg_undef, 4);
1559 inst->target = 0;
1560
1561 prog_data->dual_src_blend = true;
1562 } else {
1563 for (int target = 0; target < key->nr_color_regions; target++) {
1564 /* Skip over outputs that weren't written. */
1565 if (this->outputs[target].file == BAD_FILE)
1566 continue;
1567
1568 const fs_builder abld = bld.annotate(
1569 ralloc_asprintf(this->mem_ctx, "FB write target %d", target));
1570
1571 fs_reg src0_alpha;
1572 if (devinfo->gen >= 6 && key->replicate_alpha && target != 0)
1573 src0_alpha = offset(outputs[0], bld, 3);
1574
1575 inst = emit_single_fb_write(abld, this->outputs[target], reg_undef,
1576 src0_alpha,
1577 this->output_components[target]);
1578 inst->target = target;
1579 }
1580 }
1581
1582 if (inst == NULL) {
1583 /* Even if there's no color buffers enabled, we still need to send
1584 * alpha out the pipeline to our null renderbuffer to support
1585 * alpha-testing, alpha-to-coverage, and so on.
1586 */
1587 /* FINISHME: Factor out this frequently recurring pattern into a
1588 * helper function.
1589 */
1590 const fs_reg srcs[] = { reg_undef, reg_undef,
1591 reg_undef, offset(this->outputs[0], bld, 3) };
1592 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 4);
1593 bld.LOAD_PAYLOAD(tmp, srcs, 4, 0);
1594
1595 inst = emit_single_fb_write(bld, tmp, reg_undef, reg_undef, 4);
1596 inst->target = 0;
1597 }
1598
1599 inst->eot = true;
1600 }
1601
1602 void
1603 fs_visitor::setup_uniform_clipplane_values(gl_clip_plane *clip_planes)
1604 {
1605 const struct brw_vue_prog_key *key =
1606 (const struct brw_vue_prog_key *) this->key;
1607
1608 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
1609 this->userplane[i] = fs_reg(UNIFORM, uniforms);
1610 for (int j = 0; j < 4; ++j) {
1611 stage_prog_data->param[uniforms + j] =
1612 (gl_constant_value *) &clip_planes[i][j];
1613 }
1614 uniforms += 4;
1615 }
1616 }
1617
1618 /**
1619 * Lower legacy fixed-function and gl_ClipVertex clipping to clip distances.
1620 *
1621 * This does nothing if the shader uses gl_ClipDistance or user clipping is
1622 * disabled altogether.
1623 */
1624 void fs_visitor::compute_clip_distance(gl_clip_plane *clip_planes)
1625 {
1626 struct brw_vue_prog_data *vue_prog_data =
1627 (struct brw_vue_prog_data *) prog_data;
1628 const struct brw_vue_prog_key *key =
1629 (const struct brw_vue_prog_key *) this->key;
1630
1631 /* Bail unless some sort of legacy clipping is enabled */
1632 if (!key->userclip_active || prog->UsesClipDistanceOut)
1633 return;
1634
1635 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
1636 *
1637 * "If a linked set of shaders forming the vertex stage contains no
1638 * static write to gl_ClipVertex or gl_ClipDistance, but the
1639 * application has requested clipping against user clip planes through
1640 * the API, then the coordinate written to gl_Position is used for
1641 * comparison against the user clip planes."
1642 *
1643 * This function is only called if the shader didn't write to
1644 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
1645 * if the user wrote to it; otherwise we use gl_Position.
1646 */
1647
1648 gl_varying_slot clip_vertex = VARYING_SLOT_CLIP_VERTEX;
1649 if (!(vue_prog_data->vue_map.slots_valid & VARYING_BIT_CLIP_VERTEX))
1650 clip_vertex = VARYING_SLOT_POS;
1651
1652 /* If the clip vertex isn't written, skip this. Typically this means
1653 * the GS will set up clipping. */
1654 if (outputs[clip_vertex].file == BAD_FILE)
1655 return;
1656
1657 setup_uniform_clipplane_values(clip_planes);
1658
1659 const fs_builder abld = bld.annotate("user clip distances");
1660
1661 this->outputs[VARYING_SLOT_CLIP_DIST0] = vgrf(glsl_type::vec4_type);
1662 this->outputs[VARYING_SLOT_CLIP_DIST1] = vgrf(glsl_type::vec4_type);
1663
1664 for (int i = 0; i < key->nr_userclip_plane_consts; i++) {
1665 fs_reg u = userplane[i];
1666 fs_reg output = outputs[VARYING_SLOT_CLIP_DIST0 + i / 4];
1667 output.reg_offset = i & 3;
1668
1669 abld.MUL(output, outputs[clip_vertex], u);
1670 for (int j = 1; j < 4; j++) {
1671 u.reg = userplane[i].reg + j;
1672 abld.MAD(output, output, offset(outputs[clip_vertex], bld, j), u);
1673 }
1674 }
1675 }
1676
1677 void
1678 fs_visitor::emit_urb_writes()
1679 {
1680 int slot, urb_offset, length;
1681 struct brw_vs_prog_data *vs_prog_data =
1682 (struct brw_vs_prog_data *) prog_data;
1683 const struct brw_vs_prog_key *key =
1684 (const struct brw_vs_prog_key *) this->key;
1685 const GLbitfield64 psiz_mask =
1686 VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;
1687 const struct brw_vue_map *vue_map = &vs_prog_data->base.vue_map;
1688 bool flush;
1689 fs_reg sources[8];
1690
1691 /* If we don't have any valid slots to write, just do a minimal urb write
1692 * send to terminate the shader. This includes 1 slot of undefined data,
1693 * because it's invalid to write 0 data:
1694 *
1695 * From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -
1696 * Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >
1697 * Write Data Payload:
1698 *
1699 * "The write data payload can be between 1 and 8 message phases long."
1700 */
1701 if (vue_map->slots_valid == 0) {
1702 fs_reg payload = fs_reg(GRF, alloc.allocate(2), BRW_REGISTER_TYPE_UD);
1703 bld.exec_all().MOV(payload, fs_reg(retype(brw_vec8_grf(1, 0),
1704 BRW_REGISTER_TYPE_UD)));
1705
1706 fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
1707 inst->eot = true;
1708 inst->mlen = 2;
1709 inst->offset = 1;
1710 return;
1711 }
1712
1713 length = 0;
1714 urb_offset = 0;
1715 flush = false;
1716 for (slot = 0; slot < vue_map->num_slots; slot++) {
1717 fs_reg reg, src, zero;
1718
1719 int varying = vue_map->slot_to_varying[slot];
1720 switch (varying) {
1721 case VARYING_SLOT_PSIZ:
1722
1723 /* The point size varying slot is the vue header and is always in the
1724 * vue map. But often none of the special varyings that live there
1725 * are written and in that case we can skip writing to the vue
1726 * header, provided the corresponding state properly clamps the
1727 * values further down the pipeline. */
1728 if ((vue_map->slots_valid & psiz_mask) == 0) {
1729 assert(length == 0);
1730 urb_offset++;
1731 break;
1732 }
1733
1734 zero = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1735 bld.MOV(zero, fs_reg(0u));
1736
1737 sources[length++] = zero;
1738 if (vue_map->slots_valid & VARYING_BIT_LAYER)
1739 sources[length++] = this->outputs[VARYING_SLOT_LAYER];
1740 else
1741 sources[length++] = zero;
1742
1743 if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)
1744 sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];
1745 else
1746 sources[length++] = zero;
1747
1748 if (vue_map->slots_valid & VARYING_BIT_PSIZ)
1749 sources[length++] = this->outputs[VARYING_SLOT_PSIZ];
1750 else
1751 sources[length++] = zero;
1752 break;
1753
1754 case BRW_VARYING_SLOT_NDC:
1755 case VARYING_SLOT_EDGE:
1756 unreachable("unexpected scalar vs output");
1757 break;
1758
1759 case BRW_VARYING_SLOT_PAD:
1760 break;
1761
1762 default:
1763 /* gl_Position is always in the vue map, but isn't always written by
1764 * the shader. Other varyings (clip distances) get added to the vue
1765 * map but don't always get written. In those cases, the
1766 * corresponding this->output[] slot will be invalid we and can skip
1767 * the urb write for the varying. If we've already queued up a vue
1768 * slot for writing we flush a mlen 5 urb write, otherwise we just
1769 * advance the urb_offset.
1770 */
1771 if (this->outputs[varying].file == BAD_FILE) {
1772 if (length > 0)
1773 flush = true;
1774 else
1775 urb_offset++;
1776 break;
1777 }
1778
1779 if ((varying == VARYING_SLOT_COL0 ||
1780 varying == VARYING_SLOT_COL1 ||
1781 varying == VARYING_SLOT_BFC0 ||
1782 varying == VARYING_SLOT_BFC1) &&
1783 key->clamp_vertex_color) {
1784 /* We need to clamp these guys, so do a saturating MOV into a
1785 * temp register and use that for the payload.
1786 */
1787 for (int i = 0; i < 4; i++) {
1788 reg = fs_reg(GRF, alloc.allocate(1), outputs[varying].type);
1789 src = offset(this->outputs[varying], bld, i);
1790 set_saturate(true, bld.MOV(reg, src));
1791 sources[length++] = reg;
1792 }
1793 } else {
1794 for (int i = 0; i < 4; i++)
1795 sources[length++] = offset(this->outputs[varying], bld, i);
1796 }
1797 break;
1798 }
1799
1800 const fs_builder abld = bld.annotate("URB write");
1801
1802 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
1803 * the last slot or if we need to flush (see BAD_FILE varying case
1804 * above), emit a URB write send now to flush out the data.
1805 */
1806 int last = slot == vue_map->num_slots - 1;
1807 if (length == 8 || last)
1808 flush = true;
1809 if (flush) {
1810 fs_reg *payload_sources = ralloc_array(mem_ctx, fs_reg, length + 1);
1811 fs_reg payload = fs_reg(GRF, alloc.allocate(length + 1),
1812 BRW_REGISTER_TYPE_F);
1813 payload_sources[0] =
1814 fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1815
1816 memcpy(&payload_sources[1], sources, length * sizeof sources[0]);
1817 abld.LOAD_PAYLOAD(payload, payload_sources, length + 1, 1);
1818
1819 fs_inst *inst =
1820 abld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);
1821 inst->eot = last;
1822 inst->mlen = length + 1;
1823 inst->offset = urb_offset;
1824 urb_offset = slot + 1;
1825 length = 0;
1826 flush = false;
1827 }
1828 }
1829 }
1830
1831 void
1832 fs_visitor::emit_cs_terminate()
1833 {
1834 assert(devinfo->gen >= 7);
1835
1836 /* We are getting the thread ID from the compute shader header */
1837 assert(stage == MESA_SHADER_COMPUTE);
1838
1839 /* We can't directly send from g0, since sends with EOT have to use
1840 * g112-127. So, copy it to a virtual register, The register allocator will
1841 * make sure it uses the appropriate register range.
1842 */
1843 struct brw_reg g0 = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD);
1844 fs_reg payload = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1845 bld.group(8, 0).exec_all().MOV(payload, g0);
1846
1847 /* Send a message to the thread spawner to terminate the thread. */
1848 fs_inst *inst = bld.exec_all()
1849 .emit(CS_OPCODE_CS_TERMINATE, reg_undef, payload);
1850 inst->eot = true;
1851 }
1852
1853 void
1854 fs_visitor::emit_barrier()
1855 {
1856 assert(devinfo->gen >= 7);
1857
1858 /* We are getting the barrier ID from the compute shader header */
1859 assert(stage == MESA_SHADER_COMPUTE);
1860
1861 fs_reg payload = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
1862
1863 /* Clear the message payload */
1864 bld.exec_all().MOV(payload, fs_reg(0u));
1865
1866 /* Copy bits 27:24 of r0.2 (barrier id) to the message payload reg.2 */
1867 fs_reg r0_2 = fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD));
1868 bld.exec_all().AND(component(payload, 2), r0_2, fs_reg(0x0f000000u));
1869
1870 /* Emit a gateway "barrier" message using the payload we set up, followed
1871 * by a wait instruction.
1872 */
1873 bld.exec_all().emit(SHADER_OPCODE_BARRIER, reg_undef, payload);
1874 }
1875
1876 fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,
1877 void *mem_ctx,
1878 gl_shader_stage stage,
1879 const void *key,
1880 struct brw_stage_prog_data *prog_data,
1881 struct gl_shader_program *shader_prog,
1882 struct gl_program *prog,
1883 unsigned dispatch_width,
1884 int shader_time_index)
1885 : backend_shader(compiler, log_data, mem_ctx,
1886 shader_prog, prog, prog_data, stage),
1887 key(key), prog_data(prog_data),
1888 dispatch_width(dispatch_width),
1889 shader_time_index(shader_time_index),
1890 promoted_constants(0),
1891 bld(fs_builder(this, dispatch_width).at_end())
1892 {
1893 switch (stage) {
1894 case MESA_SHADER_FRAGMENT:
1895 key_tex = &((const brw_wm_prog_key *) key)->tex;
1896 break;
1897 case MESA_SHADER_VERTEX:
1898 case MESA_SHADER_GEOMETRY:
1899 key_tex = &((const brw_vue_prog_key *) key)->tex;
1900 break;
1901 case MESA_SHADER_COMPUTE:
1902 key_tex = &((const brw_cs_prog_key*) key)->tex;
1903 break;
1904 default:
1905 unreachable("unhandled shader stage");
1906 }
1907
1908 this->failed = false;
1909 this->simd16_unsupported = false;
1910 this->no16_msg = NULL;
1911
1912 this->nir_locals = NULL;
1913 this->nir_ssa_values = NULL;
1914
1915 memset(&this->payload, 0, sizeof(this->payload));
1916 memset(this->outputs, 0, sizeof(this->outputs));
1917 memset(this->output_components, 0, sizeof(this->output_components));
1918 this->source_depth_to_render_target = false;
1919 this->runtime_check_aads_emit = false;
1920 this->first_non_payload_grf = 0;
1921 this->max_grf = devinfo->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
1922
1923 this->virtual_grf_start = NULL;
1924 this->virtual_grf_end = NULL;
1925 this->live_intervals = NULL;
1926 this->regs_live_at_ip = NULL;
1927
1928 this->uniforms = 0;
1929 this->last_scratch = 0;
1930 this->pull_constant_loc = NULL;
1931 this->push_constant_loc = NULL;
1932
1933 this->spilled_any_registers = false;
1934 this->do_dual_src = false;
1935
1936 if (dispatch_width == 8)
1937 this->param_size = rzalloc_array(mem_ctx, int, stage_prog_data->nr_params);
1938 }
1939
1940 fs_visitor::~fs_visitor()
1941 {
1942 }