2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 /** @file brw_fs_visitor.cpp
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.
30 #include <sys/types.h>
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"
44 #include "brw_vec4_gs_visitor.h"
46 #include "main/uniforms.h"
47 #include "glsl/nir/glsl_types.h"
48 #include "glsl/ir_optimization.h"
49 #include "program/sampler.h"
54 fs_visitor::emit_vs_system_value(int location
)
56 fs_reg
*reg
= new(this->mem_ctx
)
57 fs_reg(ATTR
, 4 * _mesa_bitcount_64(nir
->info
.inputs_read
),
59 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
62 case SYSTEM_VALUE_BASE_VERTEX
:
64 vs_prog_data
->uses_vertexid
= true;
66 case SYSTEM_VALUE_VERTEX_ID
:
67 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
69 vs_prog_data
->uses_vertexid
= true;
71 case SYSTEM_VALUE_INSTANCE_ID
:
73 vs_prog_data
->uses_instanceid
= true;
76 unreachable("not reached");
83 fs_visitor::rescale_texcoord(fs_reg coordinate
, int coord_components
,
84 bool is_rect
, uint32_t sampler
)
86 bool needs_gl_clamp
= true;
87 fs_reg scale_x
, scale_y
;
89 /* The 965 requires the EU to do the normalization of GL rectangle
90 * texture coordinates. We use the program parameter state
91 * tracking to get the scaling factor.
95 (devinfo
->gen
>= 6 && (key_tex
->gl_clamp_mask
[0] & (1 << sampler
) ||
96 key_tex
->gl_clamp_mask
[1] & (1 << sampler
))))) {
97 struct gl_program_parameter_list
*params
= prog
->Parameters
;
100 /* FINISHME: We're failing to recompile our programs when the sampler is
101 * updated. This only matters for the texture rectangle scale
102 * parameters (pre-gen6, or gen6+ with GL_CLAMP).
104 int tokens
[STATE_LENGTH
] = {
107 prog
->SamplerUnits
[sampler
],
112 no16("rectangle scale uniform setup not supported on SIMD16\n");
113 if (dispatch_width
== 16) {
117 GLuint index
= _mesa_add_state_reference(params
,
118 (gl_state_index
*)tokens
);
119 /* Try to find existing copies of the texrect scale uniforms. */
120 for (unsigned i
= 0; i
< uniforms
; i
++) {
121 if (stage_prog_data
->param
[i
] ==
122 &prog
->Parameters
->ParameterValues
[index
][0]) {
123 scale_x
= fs_reg(UNIFORM
, i
);
124 scale_y
= fs_reg(UNIFORM
, i
+ 1);
129 /* If we didn't already set them up, do so now. */
130 if (scale_x
.file
== BAD_FILE
) {
131 scale_x
= fs_reg(UNIFORM
, uniforms
);
132 scale_y
= fs_reg(UNIFORM
, uniforms
+ 1);
134 stage_prog_data
->param
[uniforms
++] =
135 &prog
->Parameters
->ParameterValues
[index
][0];
136 stage_prog_data
->param
[uniforms
++] =
137 &prog
->Parameters
->ParameterValues
[index
][1];
141 /* The 965 requires the EU to do the normalization of GL rectangle
142 * texture coordinates. We use the program parameter state
143 * tracking to get the scaling factor.
145 if (devinfo
->gen
< 6 && is_rect
) {
146 fs_reg dst
= fs_reg(GRF
, alloc
.allocate(coord_components
));
147 fs_reg src
= coordinate
;
150 bld
.MUL(dst
, src
, scale_x
);
151 dst
= offset(dst
, bld
, 1);
152 src
= offset(src
, bld
, 1);
153 bld
.MUL(dst
, src
, scale_y
);
154 } else if (is_rect
) {
155 /* On gen6+, the sampler handles the rectangle coordinates
156 * natively, without needing rescaling. But that means we have
157 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
158 * not [0, 1] like the default case below.
160 needs_gl_clamp
= false;
162 for (int i
= 0; i
< 2; i
++) {
163 if (key_tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
164 fs_reg chan
= coordinate
;
165 chan
= offset(chan
, bld
, i
);
167 set_condmod(BRW_CONDITIONAL_GE
,
168 bld
.emit(BRW_OPCODE_SEL
, chan
, chan
, fs_reg(0.0f
)));
170 /* Our parameter comes in as 1.0/width or 1.0/height,
171 * because that's what people normally want for doing
172 * texture rectangle handling. We need width or height
173 * for clamping, but we don't care enough to make a new
174 * parameter type, so just invert back.
176 fs_reg limit
= vgrf(glsl_type::float_type
);
177 bld
.MOV(limit
, i
== 0 ? scale_x
: scale_y
);
178 bld
.emit(SHADER_OPCODE_RCP
, limit
, limit
);
180 set_condmod(BRW_CONDITIONAL_L
,
181 bld
.emit(BRW_OPCODE_SEL
, chan
, chan
, limit
));
186 if (coord_components
> 0 && needs_gl_clamp
) {
187 for (int i
= 0; i
< MIN2(coord_components
, 3); i
++) {
188 if (key_tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
189 fs_reg chan
= coordinate
;
190 chan
= offset(chan
, bld
, i
);
191 set_saturate(true, bld
.MOV(chan
, chan
));
198 /* Sample from the MCS surface attached to this multisample texture. */
200 fs_visitor::emit_mcs_fetch(const fs_reg
&coordinate
, unsigned components
,
201 const fs_reg
&sampler
)
203 const fs_reg dest
= vgrf(glsl_type::uvec4_type
);
204 const fs_reg srcs
[] = {
205 coordinate
, fs_reg(), fs_reg(), fs_reg(), fs_reg(), fs_reg(),
206 sampler
, fs_reg(), fs_reg(components
), fs_reg(0)
208 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_TXF_MCS_LOGICAL
, dest
, srcs
,
211 /* We only care about one reg of response, but the sampler always writes
214 inst
->regs_written
= 4 * dispatch_width
/ 8;
220 fs_visitor::emit_texture(ir_texture_opcode op
,
221 const glsl_type
*dest_type
,
222 fs_reg coordinate
, int coord_components
,
224 fs_reg lod
, fs_reg lod2
, int grad_components
,
228 int gather_component
,
234 fs_inst
*inst
= NULL
;
237 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
238 * emitting anything other than setting up the constant result.
240 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], gather_component
);
241 if (swiz
== SWIZZLE_ZERO
|| swiz
== SWIZZLE_ONE
) {
243 fs_reg res
= vgrf(glsl_type::vec4_type
);
246 for (int i
=0; i
<4; i
++) {
247 bld
.MOV(res
, fs_reg(swiz
== SWIZZLE_ZERO
? 0.0f
: 1.0f
));
248 res
= offset(res
, bld
, 1);
254 if (op
== ir_query_levels
) {
255 /* textureQueryLevels() is implemented in terms of TXS so we need to
256 * pass a valid LOD argument.
258 assert(lod
.file
== BAD_FILE
);
262 if (coordinate
.file
!= BAD_FILE
) {
263 /* FINISHME: Texture coordinate rescaling doesn't work with non-constant
264 * samplers. This should only be a problem with GL_CLAMP on Gen7.
266 coordinate
= rescale_texcoord(coordinate
, coord_components
, is_rect
,
270 /* Writemasking doesn't eliminate channels on SIMD8 texture
271 * samples, so don't worry about them.
273 fs_reg dst
= vgrf(glsl_type::get_instance(dest_type
->base_type
, 4, 1));
274 const fs_reg srcs
[] = {
275 coordinate
, shadow_c
, lod
, lod2
,
276 sample_index
, mcs
, sampler_reg
, offset_value
,
277 fs_reg(coord_components
), fs_reg(grad_components
)
283 opcode
= SHADER_OPCODE_TEX_LOGICAL
;
286 opcode
= FS_OPCODE_TXB_LOGICAL
;
289 opcode
= SHADER_OPCODE_TXL_LOGICAL
;
292 opcode
= SHADER_OPCODE_TXD_LOGICAL
;
295 opcode
= SHADER_OPCODE_TXF_LOGICAL
;
298 opcode
= SHADER_OPCODE_TXF_CMS_LOGICAL
;
301 case ir_query_levels
:
302 opcode
= SHADER_OPCODE_TXS_LOGICAL
;
305 opcode
= SHADER_OPCODE_LOD_LOGICAL
;
308 opcode
= (offset_value
.file
!= BAD_FILE
&& offset_value
.file
!= IMM
?
309 SHADER_OPCODE_TG4_OFFSET_LOGICAL
: SHADER_OPCODE_TG4_LOGICAL
);
312 unreachable("Invalid texture opcode.");
315 inst
= bld
.emit(opcode
, dst
, srcs
, ARRAY_SIZE(srcs
));
316 inst
->regs_written
= 4 * dispatch_width
/ 8;
318 if (shadow_c
.file
!= BAD_FILE
)
319 inst
->shadow_compare
= true;
321 if (offset_value
.file
== IMM
)
322 inst
->offset
= offset_value
.fixed_hw_reg
.dw1
.ud
;
326 gather_channel(gather_component
, sampler
) << 16; /* M0.2:16-17 */
328 if (devinfo
->gen
== 6)
329 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[sampler
], dst
);
332 /* fixup #layers for cube map arrays */
333 if (op
== ir_txs
&& is_cube_array
) {
334 fs_reg depth
= offset(dst
, bld
, 2);
335 fs_reg fixed_depth
= vgrf(glsl_type::int_type
);
336 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, fixed_depth
, depth
, fs_reg(6));
338 fs_reg
*fixed_payload
= ralloc_array(mem_ctx
, fs_reg
, inst
->regs_written
);
339 int components
= inst
->regs_written
/ (inst
->exec_size
/ 8);
340 for (int i
= 0; i
< components
; i
++) {
342 fixed_payload
[i
] = fixed_depth
;
344 fixed_payload
[i
] = offset(dst
, bld
, i
);
347 bld
.LOAD_PAYLOAD(dst
, fixed_payload
, components
, 0);
350 swizzle_result(op
, dest_type
->vector_elements
, dst
, sampler
);
354 * Apply workarounds for Gen6 gather with UINT/SINT
357 fs_visitor::emit_gen6_gather_wa(uint8_t wa
, fs_reg dst
)
362 int width
= (wa
& WA_8BIT
) ? 8 : 16;
364 for (int i
= 0; i
< 4; i
++) {
365 fs_reg dst_f
= retype(dst
, BRW_REGISTER_TYPE_F
);
366 /* Convert from UNORM to UINT */
367 bld
.MUL(dst_f
, dst_f
, fs_reg((float)((1 << width
) - 1)));
371 /* Reinterpret the UINT value as a signed INT value by
372 * shifting the sign bit into place, then shifting back
375 bld
.SHL(dst
, dst
, fs_reg(32 - width
));
376 bld
.ASR(dst
, dst
, fs_reg(32 - width
));
379 dst
= offset(dst
, bld
, 1);
384 * Set up the gather channel based on the swizzle, for gather4.
387 fs_visitor::gather_channel(int orig_chan
, uint32_t sampler
)
389 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], orig_chan
);
391 case SWIZZLE_X
: return 0;
393 /* gather4 sampler is broken for green channel on RG32F --
394 * we must ask for blue instead.
396 if (key_tex
->gather_channel_quirk_mask
& (1 << sampler
))
399 case SWIZZLE_Z
: return 2;
400 case SWIZZLE_W
: return 3;
402 unreachable("Not reached"); /* zero, one swizzles handled already */
407 * Swizzle the result of a texture result. This is necessary for
408 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
411 fs_visitor::swizzle_result(ir_texture_opcode op
, int dest_components
,
412 fs_reg orig_val
, uint32_t sampler
)
414 if (op
== ir_query_levels
) {
415 /* # levels is in .w */
416 this->result
= offset(orig_val
, bld
, 3);
420 this->result
= orig_val
;
422 /* txs,lod don't actually sample the texture, so swizzling the result
425 if (op
== ir_txs
|| op
== ir_lod
|| op
== ir_tg4
)
428 if (dest_components
== 1) {
429 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
430 } else if (key_tex
->swizzles
[sampler
] != SWIZZLE_NOOP
) {
431 fs_reg swizzled_result
= vgrf(glsl_type::vec4_type
);
432 swizzled_result
.type
= orig_val
.type
;
434 for (int i
= 0; i
< 4; i
++) {
435 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], i
);
436 fs_reg l
= swizzled_result
;
437 l
= offset(l
, bld
, i
);
439 if (swiz
== SWIZZLE_ZERO
) {
440 bld
.MOV(l
, fs_reg(0.0f
));
441 } else if (swiz
== SWIZZLE_ONE
) {
442 bld
.MOV(l
, fs_reg(1.0f
));
444 bld
.MOV(l
, offset(orig_val
, bld
,
445 GET_SWZ(key_tex
->swizzles
[sampler
], i
)));
448 this->result
= swizzled_result
;
452 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
454 fs_visitor::emit_dummy_fs()
456 int reg_width
= dispatch_width
/ 8;
458 /* Everyone's favorite color. */
459 const float color
[4] = { 1.0, 0.0, 1.0, 0.0 };
460 for (int i
= 0; i
< 4; i
++) {
461 bld
.MOV(fs_reg(MRF
, 2 + i
* reg_width
, BRW_REGISTER_TYPE_F
),
466 write
= bld
.emit(FS_OPCODE_FB_WRITE
);
468 if (devinfo
->gen
>= 6) {
470 write
->mlen
= 4 * reg_width
;
472 write
->header_size
= 2;
474 write
->mlen
= 2 + 4 * reg_width
;
477 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
478 * varying to avoid GPU hangs, so set that.
480 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
481 wm_prog_data
->num_varying_inputs
= devinfo
->gen
< 6 ? 1 : 0;
482 memset(wm_prog_data
->urb_setup
, -1,
483 sizeof(wm_prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
485 /* We don't have any uniforms. */
486 stage_prog_data
->nr_params
= 0;
487 stage_prog_data
->nr_pull_params
= 0;
488 stage_prog_data
->curb_read_length
= 0;
489 stage_prog_data
->dispatch_grf_start_reg
= 2;
490 wm_prog_data
->dispatch_grf_start_reg_16
= 2;
491 grf_used
= 1; /* Gen4-5 don't allow zero GRF blocks */
496 /* The register location here is relative to the start of the URB
497 * data. It will get adjusted to be a real location before
498 * generate_code() time.
501 fs_visitor::interp_reg(int location
, int channel
)
503 assert(stage
== MESA_SHADER_FRAGMENT
);
504 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
505 int regnr
= prog_data
->urb_setup
[location
] * 2 + channel
/ 2;
506 int stride
= (channel
& 1) * 4;
508 assert(prog_data
->urb_setup
[location
] != -1);
510 return brw_vec1_grf(regnr
, stride
);
513 /** Emits the interpolation for the varying inputs. */
515 fs_visitor::emit_interpolation_setup_gen4()
517 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
519 fs_builder abld
= bld
.annotate("compute pixel centers");
520 this->pixel_x
= vgrf(glsl_type::uint_type
);
521 this->pixel_y
= vgrf(glsl_type::uint_type
);
522 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
523 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
524 abld
.ADD(this->pixel_x
,
525 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
526 fs_reg(brw_imm_v(0x10101010)));
527 abld
.ADD(this->pixel_y
,
528 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
529 fs_reg(brw_imm_v(0x11001100)));
531 abld
= bld
.annotate("compute pixel deltas from v0");
533 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
534 vgrf(glsl_type::vec2_type
);
535 const fs_reg
&delta_xy
= this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
];
536 const fs_reg
xstart(negate(brw_vec1_grf(1, 0)));
537 const fs_reg
ystart(negate(brw_vec1_grf(1, 1)));
539 if (devinfo
->has_pln
&& dispatch_width
== 16) {
540 for (unsigned i
= 0; i
< 2; i
++) {
541 abld
.half(i
).ADD(half(offset(delta_xy
, abld
, i
), 0),
542 half(this->pixel_x
, i
), xstart
);
543 abld
.half(i
).ADD(half(offset(delta_xy
, abld
, i
), 1),
544 half(this->pixel_y
, i
), ystart
);
547 abld
.ADD(offset(delta_xy
, abld
, 0), this->pixel_x
, xstart
);
548 abld
.ADD(offset(delta_xy
, abld
, 1), this->pixel_y
, ystart
);
551 abld
= bld
.annotate("compute pos.w and 1/pos.w");
552 /* Compute wpos.w. It's always in our setup, since it's needed to
553 * interpolate the other attributes.
555 this->wpos_w
= vgrf(glsl_type::float_type
);
556 abld
.emit(FS_OPCODE_LINTERP
, wpos_w
, delta_xy
,
557 interp_reg(VARYING_SLOT_POS
, 3));
558 /* Compute the pixel 1/W value from wpos.w. */
559 this->pixel_w
= vgrf(glsl_type::float_type
);
560 abld
.emit(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
563 /** Emits the interpolation for the varying inputs. */
565 fs_visitor::emit_interpolation_setup_gen6()
567 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
569 fs_builder abld
= bld
.annotate("compute pixel centers");
570 if (devinfo
->gen
>= 8 || dispatch_width
== 8) {
571 /* The "Register Region Restrictions" page says for BDW (and newer,
574 * "When destination spans two registers, the source may be one or
575 * two registers. The destination elements must be evenly split
576 * between the two registers."
578 * Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16 to
579 * compute our pixel centers.
581 fs_reg
int_pixel_xy(GRF
, alloc
.allocate(dispatch_width
/ 8),
582 BRW_REGISTER_TYPE_UW
);
584 const fs_builder dbld
= abld
.exec_all().group(dispatch_width
* 2, 0);
585 dbld
.ADD(int_pixel_xy
,
586 fs_reg(stride(suboffset(g1_uw
, 4), 1, 4, 0)),
587 fs_reg(brw_imm_v(0x11001010)));
589 this->pixel_x
= vgrf(glsl_type::float_type
);
590 this->pixel_y
= vgrf(glsl_type::float_type
);
591 abld
.emit(FS_OPCODE_PIXEL_X
, this->pixel_x
, int_pixel_xy
);
592 abld
.emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
, int_pixel_xy
);
594 /* The "Register Region Restrictions" page says for SNB, IVB, HSW:
596 * "When destination spans two registers, the source MUST span two
599 * Since the GRF source of the ADD will only read a single register, we
600 * must do two separate ADDs in SIMD16.
602 fs_reg int_pixel_x
= vgrf(glsl_type::uint_type
);
603 fs_reg int_pixel_y
= vgrf(glsl_type::uint_type
);
604 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
605 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
606 abld
.ADD(int_pixel_x
,
607 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
608 fs_reg(brw_imm_v(0x10101010)));
609 abld
.ADD(int_pixel_y
,
610 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
611 fs_reg(brw_imm_v(0x11001100)));
613 /* As of gen6, we can no longer mix float and int sources. We have
614 * to turn the integer pixel centers into floats for their actual
617 this->pixel_x
= vgrf(glsl_type::float_type
);
618 this->pixel_y
= vgrf(glsl_type::float_type
);
619 abld
.MOV(this->pixel_x
, int_pixel_x
);
620 abld
.MOV(this->pixel_y
, int_pixel_y
);
623 abld
= bld
.annotate("compute pos.w");
624 this->pixel_w
= fs_reg(brw_vec8_grf(payload
.source_w_reg
, 0));
625 this->wpos_w
= vgrf(glsl_type::float_type
);
626 abld
.emit(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
628 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
629 uint8_t reg
= payload
.barycentric_coord_reg
[i
];
630 this->delta_xy
[i
] = fs_reg(brw_vec16_grf(reg
, 0));
634 static enum brw_conditional_mod
635 cond_for_alpha_func(GLenum func
)
639 return BRW_CONDITIONAL_G
;
641 return BRW_CONDITIONAL_GE
;
643 return BRW_CONDITIONAL_L
;
645 return BRW_CONDITIONAL_LE
;
647 return BRW_CONDITIONAL_EQ
;
649 return BRW_CONDITIONAL_NEQ
;
651 unreachable("Not reached");
656 * Alpha test support for when we compile it into the shader instead
657 * of using the normal fixed-function alpha test.
660 fs_visitor::emit_alpha_test()
662 assert(stage
== MESA_SHADER_FRAGMENT
);
663 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
664 const fs_builder abld
= bld
.annotate("Alpha test");
667 if (key
->alpha_test_func
== GL_ALWAYS
)
670 if (key
->alpha_test_func
== GL_NEVER
) {
672 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
673 BRW_REGISTER_TYPE_UW
));
674 cmp
= abld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
,
675 BRW_CONDITIONAL_NEQ
);
678 fs_reg color
= offset(outputs
[0], bld
, 3);
680 /* f0.1 &= func(color, ref) */
681 cmp
= abld
.CMP(bld
.null_reg_f(), color
, fs_reg(key
->alpha_test_ref
),
682 cond_for_alpha_func(key
->alpha_test_func
));
684 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
685 cmp
->flag_subreg
= 1;
689 fs_visitor::emit_single_fb_write(const fs_builder
&bld
,
690 fs_reg color0
, fs_reg color1
,
691 fs_reg src0_alpha
, unsigned components
)
693 assert(stage
== MESA_SHADER_FRAGMENT
);
694 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
696 /* Hand over gl_FragDepth or the payload depth. */
697 const fs_reg dst_depth
= (payload
.dest_depth_reg
?
698 fs_reg(brw_vec8_grf(payload
.dest_depth_reg
, 0)) :
700 fs_reg src_depth
, src_stencil
;
702 if (source_depth_to_render_target
) {
703 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
704 src_depth
= frag_depth
;
706 src_depth
= fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0));
709 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
))
710 src_stencil
= frag_stencil
;
712 const fs_reg sources
[] = {
713 color0
, color1
, src0_alpha
, src_depth
, dst_depth
, src_stencil
,
714 sample_mask
, fs_reg(components
)
716 assert(ARRAY_SIZE(sources
) - 1 == FB_WRITE_LOGICAL_SRC_COMPONENTS
);
717 fs_inst
*write
= bld
.emit(FS_OPCODE_FB_WRITE_LOGICAL
, fs_reg(),
718 sources
, ARRAY_SIZE(sources
));
720 if (prog_data
->uses_kill
) {
721 write
->predicate
= BRW_PREDICATE_NORMAL
;
722 write
->flag_subreg
= 1;
729 fs_visitor::emit_fb_writes()
731 assert(stage
== MESA_SHADER_FRAGMENT
);
732 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
733 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
735 fs_inst
*inst
= NULL
;
737 if (source_depth_to_render_target
&& devinfo
->gen
== 6) {
738 /* For outputting oDepth on gen6, SIMD8 writes have to be used. This
739 * would require SIMD8 moves of each half to message regs, e.g. by using
740 * the SIMD lowering pass. Unfortunately this is more difficult than it
741 * sounds because the SIMD8 single-source message lacks channel selects
742 * for the second and third subspans.
744 no16("Missing support for simd16 depth writes on gen6\n");
747 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
748 /* From the 'Render Target Write message' section of the docs:
749 * "Output Stencil is not supported with SIMD16 Render Target Write
752 * FINISHME: split 16 into 2 8s
754 no16("FINISHME: support 2 simd8 writes for gl_FragStencilRefARB\n");
758 const fs_builder abld
= bld
.annotate("FB dual-source write");
760 inst
= emit_single_fb_write(abld
, this->outputs
[0],
761 this->dual_src_output
, reg_undef
, 4);
764 prog_data
->dual_src_blend
= true;
766 for (int target
= 0; target
< key
->nr_color_regions
; target
++) {
767 /* Skip over outputs that weren't written. */
768 if (this->outputs
[target
].file
== BAD_FILE
)
771 const fs_builder abld
= bld
.annotate(
772 ralloc_asprintf(this->mem_ctx
, "FB write target %d", target
));
775 if (devinfo
->gen
>= 6 && key
->replicate_alpha
&& target
!= 0)
776 src0_alpha
= offset(outputs
[0], bld
, 3);
778 inst
= emit_single_fb_write(abld
, this->outputs
[target
], reg_undef
,
780 this->output_components
[target
]);
781 inst
->target
= target
;
786 /* Even if there's no color buffers enabled, we still need to send
787 * alpha out the pipeline to our null renderbuffer to support
788 * alpha-testing, alpha-to-coverage, and so on.
790 /* FINISHME: Factor out this frequently recurring pattern into a
793 const fs_reg srcs
[] = { reg_undef
, reg_undef
,
794 reg_undef
, offset(this->outputs
[0], bld
, 3) };
795 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
796 bld
.LOAD_PAYLOAD(tmp
, srcs
, 4, 0);
798 inst
= emit_single_fb_write(bld
, tmp
, reg_undef
, reg_undef
, 4);
806 fs_visitor::setup_uniform_clipplane_values(gl_clip_plane
*clip_planes
)
808 const struct brw_vs_prog_key
*key
=
809 (const struct brw_vs_prog_key
*) this->key
;
811 for (int i
= 0; i
< key
->nr_userclip_plane_consts
; i
++) {
812 this->userplane
[i
] = fs_reg(UNIFORM
, uniforms
);
813 for (int j
= 0; j
< 4; ++j
) {
814 stage_prog_data
->param
[uniforms
+ j
] =
815 (gl_constant_value
*) &clip_planes
[i
][j
];
822 * Lower legacy fixed-function and gl_ClipVertex clipping to clip distances.
824 * This does nothing if the shader uses gl_ClipDistance or user clipping is
825 * disabled altogether.
827 void fs_visitor::compute_clip_distance(gl_clip_plane
*clip_planes
)
829 struct brw_vue_prog_data
*vue_prog_data
=
830 (struct brw_vue_prog_data
*) prog_data
;
831 const struct brw_vs_prog_key
*key
=
832 (const struct brw_vs_prog_key
*) this->key
;
834 /* Bail unless some sort of legacy clipping is enabled */
835 if (key
->nr_userclip_plane_consts
== 0)
838 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
840 * "If a linked set of shaders forming the vertex stage contains no
841 * static write to gl_ClipVertex or gl_ClipDistance, but the
842 * application has requested clipping against user clip planes through
843 * the API, then the coordinate written to gl_Position is used for
844 * comparison against the user clip planes."
846 * This function is only called if the shader didn't write to
847 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
848 * if the user wrote to it; otherwise we use gl_Position.
851 gl_varying_slot clip_vertex
= VARYING_SLOT_CLIP_VERTEX
;
852 if (!(vue_prog_data
->vue_map
.slots_valid
& VARYING_BIT_CLIP_VERTEX
))
853 clip_vertex
= VARYING_SLOT_POS
;
855 /* If the clip vertex isn't written, skip this. Typically this means
856 * the GS will set up clipping. */
857 if (outputs
[clip_vertex
].file
== BAD_FILE
)
860 setup_uniform_clipplane_values(clip_planes
);
862 const fs_builder abld
= bld
.annotate("user clip distances");
864 this->outputs
[VARYING_SLOT_CLIP_DIST0
] = vgrf(glsl_type::vec4_type
);
865 this->output_components
[VARYING_SLOT_CLIP_DIST0
] = 4;
866 this->outputs
[VARYING_SLOT_CLIP_DIST1
] = vgrf(glsl_type::vec4_type
);
867 this->output_components
[VARYING_SLOT_CLIP_DIST1
] = 4;
869 for (int i
= 0; i
< key
->nr_userclip_plane_consts
; i
++) {
870 fs_reg u
= userplane
[i
];
871 fs_reg output
= outputs
[VARYING_SLOT_CLIP_DIST0
+ i
/ 4];
872 output
.reg_offset
= i
& 3;
874 abld
.MUL(output
, outputs
[clip_vertex
], u
);
875 for (int j
= 1; j
< 4; j
++) {
876 u
.reg
= userplane
[i
].reg
+ j
;
877 abld
.MAD(output
, output
, offset(outputs
[clip_vertex
], bld
, j
), u
);
883 fs_visitor::emit_urb_writes()
885 int slot
, urb_offset
, length
;
886 int starting_urb_offset
= 0;
887 const struct brw_vue_prog_data
*vue_prog_data
=
888 (const struct brw_vue_prog_data
*) this->prog_data
;
889 const struct brw_vs_prog_key
*vs_key
=
890 (const struct brw_vs_prog_key
*) this->key
;
891 const GLbitfield64 psiz_mask
=
892 VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
| VARYING_BIT_PSIZ
;
893 const struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
897 /* If we don't have any valid slots to write, just do a minimal urb write
898 * send to terminate the shader. This includes 1 slot of undefined data,
899 * because it's invalid to write 0 data:
901 * From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -
902 * Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >
903 * Write Data Payload:
905 * "The write data payload can be between 1 and 8 message phases long."
907 if (vue_map
->slots_valid
== 0) {
908 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(2), BRW_REGISTER_TYPE_UD
);
909 bld
.exec_all().MOV(payload
, fs_reg(retype(brw_vec8_grf(1, 0),
910 BRW_REGISTER_TYPE_UD
)));
912 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
919 if (stage
== MESA_SHADER_GEOMETRY
) {
920 const struct brw_gs_prog_data
*gs_prog_data
=
921 (const struct brw_gs_prog_data
*) prog_data
;
923 /* We need to increment the Global Offset to skip over the control data
924 * header and the extra "Vertex Count" field (1 HWord) at the beginning
925 * of the VUE. We're counting in OWords, so the units are doubled.
927 starting_urb_offset
= 2 * gs_prog_data
->control_data_header_size_hwords
;
928 if (gs_prog_data
->static_vertex_count
== -1)
929 starting_urb_offset
+= 2;
933 urb_offset
= starting_urb_offset
;
935 for (slot
= 0; slot
< vue_map
->num_slots
; slot
++) {
936 int varying
= vue_map
->slot_to_varying
[slot
];
938 case VARYING_SLOT_PSIZ
: {
939 /* The point size varying slot is the vue header and is always in the
940 * vue map. But often none of the special varyings that live there
941 * are written and in that case we can skip writing to the vue
942 * header, provided the corresponding state properly clamps the
943 * values further down the pipeline. */
944 if ((vue_map
->slots_valid
& psiz_mask
) == 0) {
950 fs_reg
zero(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
951 bld
.MOV(zero
, fs_reg(0u));
953 sources
[length
++] = zero
;
954 if (vue_map
->slots_valid
& VARYING_BIT_LAYER
)
955 sources
[length
++] = this->outputs
[VARYING_SLOT_LAYER
];
957 sources
[length
++] = zero
;
959 if (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
)
960 sources
[length
++] = this->outputs
[VARYING_SLOT_VIEWPORT
];
962 sources
[length
++] = zero
;
964 if (vue_map
->slots_valid
& VARYING_BIT_PSIZ
)
965 sources
[length
++] = this->outputs
[VARYING_SLOT_PSIZ
];
967 sources
[length
++] = zero
;
970 case BRW_VARYING_SLOT_NDC
:
971 case VARYING_SLOT_EDGE
:
972 unreachable("unexpected scalar vs output");
976 /* gl_Position is always in the vue map, but isn't always written by
977 * the shader. Other varyings (clip distances) get added to the vue
978 * map but don't always get written. In those cases, the
979 * corresponding this->output[] slot will be invalid we and can skip
980 * the urb write for the varying. If we've already queued up a vue
981 * slot for writing we flush a mlen 5 urb write, otherwise we just
982 * advance the urb_offset.
984 if (varying
== BRW_VARYING_SLOT_PAD
||
985 this->outputs
[varying
].file
== BAD_FILE
) {
993 if (stage
== MESA_SHADER_VERTEX
&& vs_key
->clamp_vertex_color
&&
994 (varying
== VARYING_SLOT_COL0
||
995 varying
== VARYING_SLOT_COL1
||
996 varying
== VARYING_SLOT_BFC0
||
997 varying
== VARYING_SLOT_BFC1
)) {
998 /* We need to clamp these guys, so do a saturating MOV into a
999 * temp register and use that for the payload.
1001 for (int i
= 0; i
< 4; i
++) {
1002 fs_reg reg
= fs_reg(GRF
, alloc
.allocate(1), outputs
[varying
].type
);
1003 fs_reg src
= offset(this->outputs
[varying
], bld
, i
);
1004 set_saturate(true, bld
.MOV(reg
, src
));
1005 sources
[length
++] = reg
;
1008 for (unsigned i
= 0; i
< output_components
[varying
]; i
++)
1009 sources
[length
++] = offset(this->outputs
[varying
], bld
, i
);
1010 for (unsigned i
= output_components
[varying
]; i
< 4; i
++)
1011 sources
[length
++] = fs_reg(0);
1016 const fs_builder abld
= bld
.annotate("URB write");
1018 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
1019 * the last slot or if we need to flush (see BAD_FILE varying case
1020 * above), emit a URB write send now to flush out the data.
1022 int last
= slot
== vue_map
->num_slots
- 1;
1023 if (length
== 8 || last
)
1026 fs_reg
*payload_sources
= ralloc_array(mem_ctx
, fs_reg
, length
+ 1);
1027 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(length
+ 1),
1028 BRW_REGISTER_TYPE_F
);
1029 payload_sources
[0] =
1030 fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1032 memcpy(&payload_sources
[1], sources
, length
* sizeof sources
[0]);
1033 abld
.LOAD_PAYLOAD(payload
, payload_sources
, length
+ 1, 1);
1036 abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1037 inst
->eot
= last
&& stage
== MESA_SHADER_VERTEX
;
1038 inst
->mlen
= length
+ 1;
1039 inst
->offset
= urb_offset
;
1040 urb_offset
= starting_urb_offset
+ slot
+ 1;
1048 fs_visitor::emit_cs_terminate()
1050 assert(devinfo
->gen
>= 7);
1052 /* We are getting the thread ID from the compute shader header */
1053 assert(stage
== MESA_SHADER_COMPUTE
);
1055 /* We can't directly send from g0, since sends with EOT have to use
1056 * g112-127. So, copy it to a virtual register, The register allocator will
1057 * make sure it uses the appropriate register range.
1059 struct brw_reg g0
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
);
1060 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
1061 bld
.group(8, 0).exec_all().MOV(payload
, g0
);
1063 /* Send a message to the thread spawner to terminate the thread. */
1064 fs_inst
*inst
= bld
.exec_all()
1065 .emit(CS_OPCODE_CS_TERMINATE
, reg_undef
, payload
);
1070 fs_visitor::emit_barrier()
1072 assert(devinfo
->gen
>= 7);
1074 /* We are getting the barrier ID from the compute shader header */
1075 assert(stage
== MESA_SHADER_COMPUTE
);
1077 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
1079 const fs_builder pbld
= bld
.exec_all().group(8, 0);
1081 /* Clear the message payload */
1082 pbld
.MOV(payload
, fs_reg(0u));
1084 /* Copy bits 27:24 of r0.2 (barrier id) to the message payload reg.2 */
1085 fs_reg r0_2
= fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
));
1086 pbld
.AND(component(payload
, 2), r0_2
, fs_reg(0x0f000000u
));
1088 /* Emit a gateway "barrier" message using the payload we set up, followed
1089 * by a wait instruction.
1091 bld
.exec_all().emit(SHADER_OPCODE_BARRIER
, reg_undef
, payload
);
1094 fs_visitor::fs_visitor(const struct brw_compiler
*compiler
, void *log_data
,
1097 struct brw_stage_prog_data
*prog_data
,
1098 struct gl_program
*prog
,
1099 const nir_shader
*shader
,
1100 unsigned dispatch_width
,
1101 int shader_time_index
)
1102 : backend_shader(compiler
, log_data
, mem_ctx
, shader
, prog_data
),
1103 key(key
), gs_compile(NULL
), prog_data(prog_data
), prog(prog
),
1104 dispatch_width(dispatch_width
),
1105 shader_time_index(shader_time_index
),
1106 bld(fs_builder(this, dispatch_width
).at_end())
1111 fs_visitor::fs_visitor(const struct brw_compiler
*compiler
, void *log_data
,
1113 struct brw_gs_compile
*c
,
1114 struct brw_gs_prog_data
*prog_data
,
1115 const nir_shader
*shader
)
1116 : backend_shader(compiler
, log_data
, mem_ctx
, shader
,
1117 &prog_data
->base
.base
),
1118 key(&c
->key
), gs_compile(c
),
1119 prog_data(&prog_data
->base
.base
), prog(NULL
),
1121 shader_time_index(ST_GS
),
1122 bld(fs_builder(this, dispatch_width
).at_end())
1132 case MESA_SHADER_FRAGMENT
:
1133 key_tex
= &((const brw_wm_prog_key
*) key
)->tex
;
1135 case MESA_SHADER_VERTEX
:
1136 key_tex
= &((const brw_vs_prog_key
*) key
)->tex
;
1138 case MESA_SHADER_GEOMETRY
:
1139 key_tex
= &((const brw_gs_prog_key
*) key
)->tex
;
1141 case MESA_SHADER_COMPUTE
:
1142 key_tex
= &((const brw_cs_prog_key
*) key
)->tex
;
1145 unreachable("unhandled shader stage");
1148 this->prog_data
= this->stage_prog_data
;
1150 this->failed
= false;
1151 this->simd16_unsupported
= false;
1152 this->no16_msg
= NULL
;
1154 this->nir_locals
= NULL
;
1155 this->nir_ssa_values
= NULL
;
1157 memset(&this->payload
, 0, sizeof(this->payload
));
1158 memset(this->outputs
, 0, sizeof(this->outputs
));
1159 memset(this->output_components
, 0, sizeof(this->output_components
));
1160 this->source_depth_to_render_target
= false;
1161 this->runtime_check_aads_emit
= false;
1162 this->first_non_payload_grf
= 0;
1163 this->max_grf
= devinfo
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
1165 this->virtual_grf_start
= NULL
;
1166 this->virtual_grf_end
= NULL
;
1167 this->live_intervals
= NULL
;
1168 this->regs_live_at_ip
= NULL
;
1171 this->last_scratch
= 0;
1172 this->pull_constant_loc
= NULL
;
1173 this->push_constant_loc
= NULL
;
1175 this->promoted_constants
= 0,
1177 this->spilled_any_registers
= false;
1178 this->do_dual_src
= false;
1180 if (dispatch_width
== 8)
1181 this->param_size
= rzalloc_array(mem_ctx
, int, stage_prog_data
->nr_params
);
1184 fs_visitor::~fs_visitor()