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 #include "compiler/glsl/ir.h"
25 #include "main/shaderimage.h"
27 #include "brw_fs_surface_builder.h"
29 #include "brw_program.h"
32 using namespace brw::surface_access
;
35 fs_visitor::emit_nir_code()
37 /* emit the arrays used for inputs and outputs - load/store intrinsics will
38 * be converted to reads/writes of these arrays
43 nir_emit_system_values();
45 /* get the main function and emit it */
46 nir_foreach_function(nir
, function
) {
47 assert(strcmp(function
->name
, "main") == 0);
48 assert(function
->impl
);
49 nir_emit_impl(function
->impl
);
54 fs_visitor::nir_setup_inputs()
56 if (stage
!= MESA_SHADER_FRAGMENT
)
59 nir_inputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, nir
->num_inputs
);
61 nir_foreach_variable(var
, &nir
->inputs
) {
62 fs_reg input
= offset(nir_inputs
, bld
, var
->data
.driver_location
);
65 if (var
->data
.location
== VARYING_SLOT_POS
) {
66 reg
= *emit_fragcoord_interpolation(var
->data
.pixel_center_integer
,
67 var
->data
.origin_upper_left
);
68 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
70 } else if (var
->data
.location
== VARYING_SLOT_LAYER
) {
71 struct brw_reg reg
= suboffset(interp_reg(VARYING_SLOT_LAYER
, 1), 3);
72 reg
.type
= BRW_REGISTER_TYPE_D
;
73 bld
.emit(FS_OPCODE_CINTERP
, retype(input
, BRW_REGISTER_TYPE_D
), reg
);
74 } else if (var
->data
.location
== VARYING_SLOT_VIEWPORT
) {
75 struct brw_reg reg
= suboffset(interp_reg(VARYING_SLOT_VIEWPORT
, 2), 3);
76 reg
.type
= BRW_REGISTER_TYPE_D
;
77 bld
.emit(FS_OPCODE_CINTERP
, retype(input
, BRW_REGISTER_TYPE_D
), reg
);
79 int location
= var
->data
.location
;
80 emit_general_interpolation(&input
, var
->name
, var
->type
,
81 (glsl_interp_qualifier
) var
->data
.interpolation
,
82 &location
, var
->data
.centroid
,
89 fs_visitor::nir_setup_single_output_varying(fs_reg
*reg
,
90 const glsl_type
*type
,
93 if (type
->is_array() || type
->is_matrix()) {
94 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
95 const unsigned length
= glsl_get_length(type
);
97 for (unsigned i
= 0; i
< length
; i
++) {
98 nir_setup_single_output_varying(reg
, elem_type
, location
);
100 } else if (type
->is_record()) {
101 for (unsigned i
= 0; i
< type
->length
; i
++) {
102 const struct glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
103 nir_setup_single_output_varying(reg
, field_type
, location
);
106 assert(type
->is_scalar() || type
->is_vector());
107 this->outputs
[*location
] = *reg
;
108 this->output_components
[*location
] = type
->vector_elements
;
109 *reg
= offset(*reg
, bld
, 4);
115 fs_visitor::nir_setup_outputs()
117 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
119 nir_outputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, nir
->num_outputs
);
121 nir_foreach_variable(var
, &nir
->outputs
) {
122 fs_reg reg
= offset(nir_outputs
, bld
, var
->data
.driver_location
);
125 case MESA_SHADER_VERTEX
:
126 case MESA_SHADER_TESS_EVAL
:
127 case MESA_SHADER_GEOMETRY
: {
128 unsigned location
= var
->data
.location
;
129 nir_setup_single_output_varying(®
, var
->type
, &location
);
132 case MESA_SHADER_FRAGMENT
:
133 if (key
->force_dual_color_blend
&&
134 var
->data
.location
== FRAG_RESULT_DATA1
) {
135 this->dual_src_output
= reg
;
136 this->do_dual_src
= true;
137 } else if (var
->data
.index
> 0) {
138 assert(var
->data
.location
== FRAG_RESULT_DATA0
);
139 assert(var
->data
.index
== 1);
140 this->dual_src_output
= reg
;
141 this->do_dual_src
= true;
142 } else if (var
->data
.location
== FRAG_RESULT_COLOR
) {
143 /* Writing gl_FragColor outputs to all color regions. */
144 for (unsigned int i
= 0; i
< MAX2(key
->nr_color_regions
, 1); i
++) {
145 this->outputs
[i
] = reg
;
146 this->output_components
[i
] = 4;
148 } else if (var
->data
.location
== FRAG_RESULT_DEPTH
) {
149 this->frag_depth
= reg
;
150 } else if (var
->data
.location
== FRAG_RESULT_STENCIL
) {
151 this->frag_stencil
= reg
;
152 } else if (var
->data
.location
== FRAG_RESULT_SAMPLE_MASK
) {
153 this->sample_mask
= reg
;
155 int vector_elements
= var
->type
->without_array()->vector_elements
;
157 /* gl_FragData or a user-defined FS output */
158 assert(var
->data
.location
>= FRAG_RESULT_DATA0
&&
159 var
->data
.location
< FRAG_RESULT_DATA0
+BRW_MAX_DRAW_BUFFERS
);
161 /* General color output. */
162 for (unsigned int i
= 0; i
< MAX2(1, var
->type
->length
); i
++) {
163 int output
= var
->data
.location
- FRAG_RESULT_DATA0
+ i
;
164 this->outputs
[output
] = offset(reg
, bld
, vector_elements
* i
);
165 this->output_components
[output
] = vector_elements
;
170 unreachable("unhandled shader stage");
176 fs_visitor::nir_setup_uniforms()
178 if (dispatch_width
!= 8)
181 uniforms
= nir
->num_uniforms
/ 4;
185 emit_system_values_block(nir_block
*block
, void *void_visitor
)
187 fs_visitor
*v
= (fs_visitor
*)void_visitor
;
190 nir_foreach_instr(block
, instr
) {
191 if (instr
->type
!= nir_instr_type_intrinsic
)
194 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
195 switch (intrin
->intrinsic
) {
196 case nir_intrinsic_load_vertex_id
:
197 unreachable("should be lowered by lower_vertex_id().");
199 case nir_intrinsic_load_vertex_id_zero_base
:
200 assert(v
->stage
== MESA_SHADER_VERTEX
);
201 reg
= &v
->nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
202 if (reg
->file
== BAD_FILE
)
203 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
);
206 case nir_intrinsic_load_base_vertex
:
207 assert(v
->stage
== MESA_SHADER_VERTEX
);
208 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
209 if (reg
->file
== BAD_FILE
)
210 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX
);
213 case nir_intrinsic_load_instance_id
:
214 assert(v
->stage
== MESA_SHADER_VERTEX
);
215 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
216 if (reg
->file
== BAD_FILE
)
217 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID
);
220 case nir_intrinsic_load_base_instance
:
221 assert(v
->stage
== MESA_SHADER_VERTEX
);
222 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_INSTANCE
];
223 if (reg
->file
== BAD_FILE
)
224 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE
);
227 case nir_intrinsic_load_draw_id
:
228 assert(v
->stage
== MESA_SHADER_VERTEX
);
229 reg
= &v
->nir_system_values
[SYSTEM_VALUE_DRAW_ID
];
230 if (reg
->file
== BAD_FILE
)
231 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID
);
234 case nir_intrinsic_load_invocation_id
:
235 assert(v
->stage
== MESA_SHADER_GEOMETRY
);
236 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
237 if (reg
->file
== BAD_FILE
) {
238 const fs_builder abld
= v
->bld
.annotate("gl_InvocationID", NULL
);
239 fs_reg
g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
240 fs_reg iid
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
241 abld
.SHR(iid
, g1
, brw_imm_ud(27u));
246 case nir_intrinsic_load_sample_pos
:
247 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
248 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
249 if (reg
->file
== BAD_FILE
)
250 *reg
= *v
->emit_samplepos_setup();
253 case nir_intrinsic_load_sample_id
:
254 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
255 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
256 if (reg
->file
== BAD_FILE
)
257 *reg
= *v
->emit_sampleid_setup();
260 case nir_intrinsic_load_sample_mask_in
:
261 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
262 assert(v
->devinfo
->gen
>= 7);
263 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
264 if (reg
->file
== BAD_FILE
)
265 *reg
= fs_reg(retype(brw_vec8_grf(v
->payload
.sample_mask_in_reg
, 0),
266 BRW_REGISTER_TYPE_D
));
269 case nir_intrinsic_load_local_invocation_id
:
270 assert(v
->stage
== MESA_SHADER_COMPUTE
);
271 reg
= &v
->nir_system_values
[SYSTEM_VALUE_LOCAL_INVOCATION_ID
];
272 if (reg
->file
== BAD_FILE
)
273 *reg
= *v
->emit_cs_local_invocation_id_setup();
276 case nir_intrinsic_load_work_group_id
:
277 assert(v
->stage
== MESA_SHADER_COMPUTE
);
278 reg
= &v
->nir_system_values
[SYSTEM_VALUE_WORK_GROUP_ID
];
279 if (reg
->file
== BAD_FILE
)
280 *reg
= *v
->emit_cs_work_group_id_setup();
283 case nir_intrinsic_load_helper_invocation
:
284 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
285 reg
= &v
->nir_system_values
[SYSTEM_VALUE_HELPER_INVOCATION
];
286 if (reg
->file
== BAD_FILE
) {
287 const fs_builder abld
=
288 v
->bld
.annotate("gl_HelperInvocation", NULL
);
290 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
291 * pixel mask is in g1.7 of the thread payload.
293 * We move the per-channel pixel enable bit to the low bit of each
294 * channel by shifting the byte containing the pixel mask by the
295 * vector immediate 0x76543210UV.
297 * The region of <1,8,0> reads only 1 byte (the pixel masks for
298 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
299 * masks for 2 and 3) in SIMD16.
301 fs_reg shifted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
303 stride(byte_offset(retype(brw_vec1_grf(1, 0),
304 BRW_REGISTER_TYPE_UB
), 28),
306 brw_imm_uv(0x76543210));
308 /* A set bit in the pixel mask means the channel is enabled, but
309 * that is the opposite of gl_HelperInvocation so we need to invert
312 * The negate source-modifier bit of logical instructions on Gen8+
313 * performs 1's complement negation, so we can use that instead of
316 fs_reg inverted
= negate(shifted
);
317 if (v
->devinfo
->gen
< 8) {
318 inverted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
319 abld
.NOT(inverted
, shifted
);
322 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
323 * with 1 and negating.
325 fs_reg anded
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
326 abld
.AND(anded
, inverted
, brw_imm_uw(1));
328 fs_reg dst
= abld
.vgrf(BRW_REGISTER_TYPE_D
, 1);
329 abld
.MOV(dst
, negate(retype(anded
, BRW_REGISTER_TYPE_D
)));
343 fs_visitor::nir_emit_system_values()
345 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
346 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
347 nir_system_values
[i
] = fs_reg();
350 nir_foreach_function(nir
, function
) {
351 assert(strcmp(function
->name
, "main") == 0);
352 assert(function
->impl
);
353 nir_foreach_block(function
->impl
, emit_system_values_block
, this);
358 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
360 nir_locals
= ralloc_array(mem_ctx
, fs_reg
, impl
->reg_alloc
);
361 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
362 nir_locals
[i
] = fs_reg();
365 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
366 unsigned array_elems
=
367 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
368 unsigned size
= array_elems
* reg
->num_components
;
369 nir_locals
[reg
->index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
372 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
375 nir_emit_cf_list(&impl
->body
);
379 fs_visitor::nir_emit_cf_list(exec_list
*list
)
381 exec_list_validate(list
);
382 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
383 switch (node
->type
) {
385 nir_emit_if(nir_cf_node_as_if(node
));
388 case nir_cf_node_loop
:
389 nir_emit_loop(nir_cf_node_as_loop(node
));
392 case nir_cf_node_block
:
393 nir_emit_block(nir_cf_node_as_block(node
));
397 unreachable("Invalid CFG node block");
403 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
405 /* first, put the condition into f0 */
406 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
407 retype(get_nir_src(if_stmt
->condition
),
408 BRW_REGISTER_TYPE_D
));
409 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
411 bld
.IF(BRW_PREDICATE_NORMAL
);
413 nir_emit_cf_list(&if_stmt
->then_list
);
415 /* note: if the else is empty, dead CF elimination will remove it */
416 bld
.emit(BRW_OPCODE_ELSE
);
418 nir_emit_cf_list(&if_stmt
->else_list
);
420 bld
.emit(BRW_OPCODE_ENDIF
);
424 fs_visitor::nir_emit_loop(nir_loop
*loop
)
426 bld
.emit(BRW_OPCODE_DO
);
428 nir_emit_cf_list(&loop
->body
);
430 bld
.emit(BRW_OPCODE_WHILE
);
434 fs_visitor::nir_emit_block(nir_block
*block
)
436 nir_foreach_instr(block
, instr
) {
437 nir_emit_instr(instr
);
442 fs_visitor::nir_emit_instr(nir_instr
*instr
)
444 const fs_builder abld
= bld
.annotate(NULL
, instr
);
446 switch (instr
->type
) {
447 case nir_instr_type_alu
:
448 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
451 case nir_instr_type_intrinsic
:
453 case MESA_SHADER_VERTEX
:
454 nir_emit_vs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
456 case MESA_SHADER_TESS_EVAL
:
457 nir_emit_tes_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
459 case MESA_SHADER_GEOMETRY
:
460 nir_emit_gs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
462 case MESA_SHADER_FRAGMENT
:
463 nir_emit_fs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
465 case MESA_SHADER_COMPUTE
:
466 nir_emit_cs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
469 unreachable("unsupported shader stage");
473 case nir_instr_type_tex
:
474 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
477 case nir_instr_type_load_const
:
478 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
481 case nir_instr_type_ssa_undef
:
482 nir_emit_undef(abld
, nir_instr_as_ssa_undef(instr
));
485 case nir_instr_type_jump
:
486 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
490 unreachable("unknown instruction type");
495 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
496 const fs_reg
&result
)
498 if (!instr
->src
[0].src
.is_ssa
||
499 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
502 nir_intrinsic_instr
*src0
=
503 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
505 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
508 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
509 if (!value1
|| fabsf(value1
->f
[0]) != 1.0f
)
512 nir_const_value
*value2
= nir_src_as_const_value(instr
->src
[2].src
);
513 if (!value2
|| fabsf(value2
->f
[0]) != 1.0f
)
516 fs_reg tmp
= vgrf(glsl_type::int_type
);
518 if (devinfo
->gen
>= 6) {
519 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
520 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
522 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
524 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
525 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
527 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
529 * This negation looks like it's safe in practice, because bits 0:4 will
530 * surely be TRIANGLES
533 if (value1
->f
[0] == -1.0f
) {
537 tmp
.type
= BRW_REGISTER_TYPE_W
;
538 tmp
.subreg_offset
= 2;
541 bld
.OR(tmp
, g0
, brw_imm_uw(0x3f80));
543 tmp
.type
= BRW_REGISTER_TYPE_D
;
544 tmp
.subreg_offset
= 0;
547 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
548 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
550 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
552 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
553 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
555 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
557 * This negation looks like it's safe in practice, because bits 0:4 will
558 * surely be TRIANGLES
561 if (value1
->f
[0] == -1.0f
) {
565 bld
.OR(tmp
, g1_6
, brw_imm_d(0x3f800000));
567 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, brw_imm_d(0xbf800000));
573 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
575 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
578 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
579 result
.type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].output_type
);
582 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
583 op
[i
] = get_nir_src(instr
->src
[i
].src
);
584 op
[i
].type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].input_types
[i
]);
585 op
[i
].abs
= instr
->src
[i
].abs
;
586 op
[i
].negate
= instr
->src
[i
].negate
;
589 /* We get a bunch of mov's out of the from_ssa pass and they may still
590 * be vectorized. We'll handle them as a special-case. We'll also
591 * handle vecN here because it's basically the same thing.
599 fs_reg temp
= result
;
600 bool need_extra_copy
= false;
601 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
602 if (!instr
->src
[i
].src
.is_ssa
&&
603 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
604 need_extra_copy
= true;
605 temp
= bld
.vgrf(result
.type
, 4);
610 for (unsigned i
= 0; i
< 4; i
++) {
611 if (!(instr
->dest
.write_mask
& (1 << i
)))
614 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
615 inst
= bld
.MOV(offset(temp
, bld
, i
),
616 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
618 inst
= bld
.MOV(offset(temp
, bld
, i
),
619 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
621 inst
->saturate
= instr
->dest
.saturate
;
624 /* In this case the source and destination registers were the same,
625 * so we need to insert an extra set of moves in order to deal with
628 if (need_extra_copy
) {
629 for (unsigned i
= 0; i
< 4; i
++) {
630 if (!(instr
->dest
.write_mask
& (1 << i
)))
633 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
642 /* At this point, we have dealt with any instruction that operates on
643 * more than a single channel. Therefore, we can just adjust the source
644 * and destination registers for that channel and emit the instruction.
646 unsigned channel
= 0;
647 if (nir_op_infos
[instr
->op
].output_size
== 0) {
648 /* Since NIR is doing the scalarizing for us, we should only ever see
649 * vectorized operations with a single channel.
651 assert(_mesa_bitcount(instr
->dest
.write_mask
) == 1);
652 channel
= ffs(instr
->dest
.write_mask
) - 1;
654 result
= offset(result
, bld
, channel
);
657 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
658 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
659 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
665 inst
= bld
.MOV(result
, op
[0]);
666 inst
->saturate
= instr
->dest
.saturate
;
671 bld
.MOV(result
, op
[0]);
675 /* AND(val, 0x80000000) gives the sign bit.
677 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
680 bld
.CMP(bld
.null_reg_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
682 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
683 op
[0].type
= BRW_REGISTER_TYPE_UD
;
684 result
.type
= BRW_REGISTER_TYPE_UD
;
685 bld
.AND(result_int
, op
[0], brw_imm_ud(0x80000000u
));
687 inst
= bld
.OR(result_int
, result_int
, brw_imm_ud(0x3f800000u
));
688 inst
->predicate
= BRW_PREDICATE_NORMAL
;
689 if (instr
->dest
.saturate
) {
690 inst
= bld
.MOV(result
, result
);
691 inst
->saturate
= true;
697 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
698 * -> non-negative val generates 0x00000000.
699 * Predicated OR sets 1 if val is positive.
701 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_G
);
702 bld
.ASR(result
, op
[0], brw_imm_d(31));
703 inst
= bld
.OR(result
, result
, brw_imm_d(1));
704 inst
->predicate
= BRW_PREDICATE_NORMAL
;
708 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
709 inst
->saturate
= instr
->dest
.saturate
;
713 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
714 inst
->saturate
= instr
->dest
.saturate
;
718 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
719 inst
->saturate
= instr
->dest
.saturate
;
723 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
724 inst
= bld
.emit(SHADER_OPCODE_SIN
, tmp
, op
[0]);
725 if (instr
->dest
.saturate
) {
727 inst
->saturate
= true;
729 bld
.MUL(result
, tmp
, brw_imm_f(0.99997));
735 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
736 inst
= bld
.emit(SHADER_OPCODE_COS
, tmp
, op
[0]);
737 if (instr
->dest
.saturate
) {
739 inst
->saturate
= true;
741 bld
.MUL(result
, tmp
, brw_imm_f(0.99997));
747 if (fs_key
->high_quality_derivatives
) {
748 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
750 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
752 inst
->saturate
= instr
->dest
.saturate
;
754 case nir_op_fddx_fine
:
755 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
756 inst
->saturate
= instr
->dest
.saturate
;
758 case nir_op_fddx_coarse
:
759 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
760 inst
->saturate
= instr
->dest
.saturate
;
763 if (fs_key
->high_quality_derivatives
) {
764 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
765 brw_imm_d(fs_key
->render_to_fbo
));
767 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
768 brw_imm_d(fs_key
->render_to_fbo
));
770 inst
->saturate
= instr
->dest
.saturate
;
772 case nir_op_fddy_fine
:
773 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
774 brw_imm_d(fs_key
->render_to_fbo
));
775 inst
->saturate
= instr
->dest
.saturate
;
777 case nir_op_fddy_coarse
:
778 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
779 brw_imm_d(fs_key
->render_to_fbo
));
780 inst
->saturate
= instr
->dest
.saturate
;
785 inst
= bld
.ADD(result
, op
[0], op
[1]);
786 inst
->saturate
= instr
->dest
.saturate
;
790 inst
= bld
.MUL(result
, op
[0], op
[1]);
791 inst
->saturate
= instr
->dest
.saturate
;
795 bld
.MUL(result
, op
[0], op
[1]);
798 case nir_op_imul_high
:
799 case nir_op_umul_high
:
800 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
805 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
808 case nir_op_uadd_carry
:
809 unreachable("Should have been lowered by carry_to_arith().");
811 case nir_op_usub_borrow
:
812 unreachable("Should have been lowered by borrow_to_arith().");
816 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
817 * appears that our hardware just does the right thing for signed
820 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
824 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
825 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
827 /* Math instructions don't support conditional mod */
828 inst
= bld
.MOV(bld
.null_reg_d(), result
);
829 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
831 /* Now, we need to determine if signs of the sources are different.
832 * When we XOR the sources, the top bit is 0 if they are the same and 1
833 * if they are different. We can then use a conditional modifier to
834 * turn that into a predicate. This leads us to an XOR.l instruction.
836 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
837 inst
= bld
.XOR(tmp
, op
[0], op
[1]);
838 inst
->predicate
= BRW_PREDICATE_NORMAL
;
839 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
841 /* If the result of the initial remainder operation is non-zero and the
842 * two sources have different signs, add in a copy of op[1] to get the
843 * final integer modulus value.
845 inst
= bld
.ADD(result
, result
, op
[1]);
846 inst
->predicate
= BRW_PREDICATE_NORMAL
;
853 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
859 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
864 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_Z
);
869 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_NZ
);
873 if (devinfo
->gen
>= 8) {
874 op
[0] = resolve_source_modifiers(op
[0]);
876 bld
.NOT(result
, op
[0]);
879 if (devinfo
->gen
>= 8) {
880 op
[0] = resolve_source_modifiers(op
[0]);
881 op
[1] = resolve_source_modifiers(op
[1]);
883 bld
.XOR(result
, op
[0], op
[1]);
886 if (devinfo
->gen
>= 8) {
887 op
[0] = resolve_source_modifiers(op
[0]);
888 op
[1] = resolve_source_modifiers(op
[1]);
890 bld
.OR(result
, op
[0], op
[1]);
893 if (devinfo
->gen
>= 8) {
894 op
[0] = resolve_source_modifiers(op
[0]);
895 op
[1] = resolve_source_modifiers(op
[1]);
897 bld
.AND(result
, op
[0], op
[1]);
903 case nir_op_ball_fequal2
:
904 case nir_op_ball_iequal2
:
905 case nir_op_ball_fequal3
:
906 case nir_op_ball_iequal3
:
907 case nir_op_ball_fequal4
:
908 case nir_op_ball_iequal4
:
909 case nir_op_bany_fnequal2
:
910 case nir_op_bany_inequal2
:
911 case nir_op_bany_fnequal3
:
912 case nir_op_bany_inequal3
:
913 case nir_op_bany_fnequal4
:
914 case nir_op_bany_inequal4
:
915 unreachable("Lowered by nir_lower_alu_reductions");
917 case nir_op_fnoise1_1
:
918 case nir_op_fnoise1_2
:
919 case nir_op_fnoise1_3
:
920 case nir_op_fnoise1_4
:
921 case nir_op_fnoise2_1
:
922 case nir_op_fnoise2_2
:
923 case nir_op_fnoise2_3
:
924 case nir_op_fnoise2_4
:
925 case nir_op_fnoise3_1
:
926 case nir_op_fnoise3_2
:
927 case nir_op_fnoise3_3
:
928 case nir_op_fnoise3_4
:
929 case nir_op_fnoise4_1
:
930 case nir_op_fnoise4_2
:
931 case nir_op_fnoise4_3
:
932 case nir_op_fnoise4_4
:
933 unreachable("not reached: should be handled by lower_noise");
936 unreachable("not reached: should be handled by ldexp_to_arith()");
939 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
940 inst
->saturate
= instr
->dest
.saturate
;
944 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
945 inst
->saturate
= instr
->dest
.saturate
;
950 bld
.MOV(result
, negate(op
[0]));
954 bld
.CMP(result
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
957 bld
.CMP(result
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
961 inst
= bld
.RNDZ(result
, op
[0]);
962 inst
->saturate
= instr
->dest
.saturate
;
966 op
[0].negate
= !op
[0].negate
;
967 fs_reg temp
= vgrf(glsl_type::float_type
);
968 bld
.RNDD(temp
, op
[0]);
970 inst
= bld
.MOV(result
, temp
);
971 inst
->saturate
= instr
->dest
.saturate
;
975 inst
= bld
.RNDD(result
, op
[0]);
976 inst
->saturate
= instr
->dest
.saturate
;
979 inst
= bld
.FRC(result
, op
[0]);
980 inst
->saturate
= instr
->dest
.saturate
;
982 case nir_op_fround_even
:
983 inst
= bld
.RNDE(result
, op
[0]);
984 inst
->saturate
= instr
->dest
.saturate
;
987 case nir_op_fquantize2f16
: {
988 fs_reg tmp16
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
989 fs_reg tmp32
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
990 fs_reg zero
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
992 /* The destination stride must be at least as big as the source stride. */
993 tmp16
.type
= BRW_REGISTER_TYPE_W
;
996 /* Check for denormal */
997 fs_reg abs_src0
= op
[0];
999 bld
.CMP(bld
.null_reg_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1001 /* Get the appropriately signed zero */
1002 bld
.AND(retype(zero
, BRW_REGISTER_TYPE_UD
),
1003 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1004 brw_imm_ud(0x80000000));
1005 /* Do the actual F32 -> F16 -> F32 conversion */
1006 bld
.emit(BRW_OPCODE_F32TO16
, tmp16
, op
[0]);
1007 bld
.emit(BRW_OPCODE_F16TO32
, tmp32
, tmp16
);
1008 /* Select that or zero based on normal status */
1009 inst
= bld
.SEL(result
, zero
, tmp32
);
1010 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1011 inst
->saturate
= instr
->dest
.saturate
;
1018 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
1019 inst
->saturate
= instr
->dest
.saturate
;
1025 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
1026 inst
->saturate
= instr
->dest
.saturate
;
1029 case nir_op_pack_snorm_2x16
:
1030 case nir_op_pack_snorm_4x8
:
1031 case nir_op_pack_unorm_2x16
:
1032 case nir_op_pack_unorm_4x8
:
1033 case nir_op_unpack_snorm_2x16
:
1034 case nir_op_unpack_snorm_4x8
:
1035 case nir_op_unpack_unorm_2x16
:
1036 case nir_op_unpack_unorm_4x8
:
1037 case nir_op_unpack_half_2x16
:
1038 case nir_op_pack_half_2x16
:
1039 unreachable("not reached: should be handled by lower_packing_builtins");
1041 case nir_op_unpack_half_2x16_split_x
:
1042 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
1043 inst
->saturate
= instr
->dest
.saturate
;
1045 case nir_op_unpack_half_2x16_split_y
:
1046 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
1047 inst
->saturate
= instr
->dest
.saturate
;
1051 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1052 inst
->saturate
= instr
->dest
.saturate
;
1055 case nir_op_bitfield_reverse
:
1056 bld
.BFREV(result
, op
[0]);
1059 case nir_op_bit_count
:
1060 bld
.CBIT(result
, op
[0]);
1063 case nir_op_ufind_msb
:
1064 case nir_op_ifind_msb
: {
1065 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1067 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1068 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1069 * subtract the result from 31 to convert the MSB count into an LSB count.
1071 bld
.CMP(bld
.null_reg_d(), result
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1073 inst
= bld
.ADD(result
, result
, brw_imm_d(31));
1074 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1075 inst
->src
[0].negate
= true;
1079 case nir_op_find_lsb
:
1080 bld
.FBL(result
, op
[0]);
1083 case nir_op_ubitfield_extract
:
1084 case nir_op_ibitfield_extract
:
1085 unreachable("should have been lowered");
1088 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1091 bld
.BFI1(result
, op
[0], op
[1]);
1094 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1097 case nir_op_bitfield_insert
:
1098 unreachable("not reached: should have been lowered");
1101 bld
.SHL(result
, op
[0], op
[1]);
1104 bld
.ASR(result
, op
[0], op
[1]);
1107 bld
.SHR(result
, op
[0], op
[1]);
1110 case nir_op_pack_half_2x16_split
:
1111 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1115 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1116 inst
->saturate
= instr
->dest
.saturate
;
1120 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1121 inst
->saturate
= instr
->dest
.saturate
;
1125 if (optimize_frontfacing_ternary(instr
, result
))
1128 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1129 inst
= bld
.SEL(result
, op
[1], op
[2]);
1130 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1133 case nir_op_extract_u8
:
1134 case nir_op_extract_i8
: {
1135 nir_const_value
*byte
= nir_src_as_const_value(instr
->src
[1].src
);
1136 bld
.emit(SHADER_OPCODE_EXTRACT_BYTE
,
1137 result
, op
[0], brw_imm_ud(byte
->u
[0]));
1141 case nir_op_extract_u16
:
1142 case nir_op_extract_i16
: {
1143 nir_const_value
*word
= nir_src_as_const_value(instr
->src
[1].src
);
1144 bld
.emit(SHADER_OPCODE_EXTRACT_WORD
,
1145 result
, op
[0], brw_imm_ud(word
->u
[0]));
1150 unreachable("unhandled instruction");
1153 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1154 * to sign extend the low bit to 0/~0
1156 if (devinfo
->gen
<= 5 &&
1157 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1158 fs_reg masked
= vgrf(glsl_type::int_type
);
1159 bld
.AND(masked
, result
, brw_imm_d(1));
1160 masked
.negate
= true;
1161 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1166 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1167 nir_load_const_instr
*instr
)
1169 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_D
, instr
->def
.num_components
);
1171 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1172 bld
.MOV(offset(reg
, bld
, i
), brw_imm_d(instr
->value
.i
[i
]));
1174 nir_ssa_values
[instr
->def
.index
] = reg
;
1178 fs_visitor::nir_emit_undef(const fs_builder
&bld
, nir_ssa_undef_instr
*instr
)
1180 nir_ssa_values
[instr
->def
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_D
,
1181 instr
->def
.num_components
);
1185 fs_visitor::get_nir_src(nir_src src
)
1189 reg
= nir_ssa_values
[src
.ssa
->index
];
1191 /* We don't handle indirects on locals */
1192 assert(src
.reg
.indirect
== NULL
);
1193 reg
= offset(nir_locals
[src
.reg
.reg
->index
], bld
,
1194 src
.reg
.base_offset
* src
.reg
.reg
->num_components
);
1197 /* to avoid floating-point denorm flushing problems, set the type by
1198 * default to D - instructions that need floating point semantics will set
1199 * this to F if they need to
1201 return retype(reg
, BRW_REGISTER_TYPE_D
);
1205 fs_visitor::get_nir_dest(nir_dest dest
)
1208 nir_ssa_values
[dest
.ssa
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
,
1209 dest
.ssa
.num_components
);
1210 return nir_ssa_values
[dest
.ssa
.index
];
1212 /* We don't handle indirects on locals */
1213 assert(dest
.reg
.indirect
== NULL
);
1214 return offset(nir_locals
[dest
.reg
.reg
->index
], bld
,
1215 dest
.reg
.base_offset
* dest
.reg
.reg
->num_components
);
1220 fs_visitor::get_nir_image_deref(const nir_deref_var
*deref
)
1222 fs_reg
image(UNIFORM
, deref
->var
->data
.driver_location
/ 4,
1223 BRW_REGISTER_TYPE_UD
);
1225 unsigned indirect_max
= 0;
1227 for (const nir_deref
*tail
= &deref
->deref
; tail
->child
;
1228 tail
= tail
->child
) {
1229 const nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1230 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1231 const unsigned size
= glsl_get_length(tail
->type
);
1232 const unsigned element_size
= type_size_scalar(deref_array
->deref
.type
);
1233 const unsigned base
= MIN2(deref_array
->base_offset
, size
- 1);
1234 image
= offset(image
, bld
, base
* element_size
);
1236 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1237 fs_reg tmp
= vgrf(glsl_type::uint_type
);
1239 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
) {
1240 /* IVB hangs when trying to access an invalid surface index with
1241 * the dataport. According to the spec "if the index used to
1242 * select an individual element is negative or greater than or
1243 * equal to the size of the array, the results of the operation
1244 * are undefined but may not lead to termination" -- which is one
1245 * of the possible outcomes of the hang. Clamp the index to
1246 * prevent access outside of the array bounds.
1248 bld
.emit_minmax(tmp
, retype(get_nir_src(deref_array
->indirect
),
1249 BRW_REGISTER_TYPE_UD
),
1250 brw_imm_ud(size
- base
- 1), BRW_CONDITIONAL_L
);
1252 bld
.MOV(tmp
, get_nir_src(deref_array
->indirect
));
1255 indirect_max
+= element_size
* (tail
->type
->length
- 1);
1257 bld
.MUL(tmp
, tmp
, brw_imm_ud(element_size
* 4));
1258 if (indirect
.file
== BAD_FILE
) {
1261 bld
.ADD(indirect
, indirect
, tmp
);
1266 if (indirect
.file
== BAD_FILE
) {
1269 /* Emit a pile of MOVs to load the uniform into a temporary. The
1270 * dead-code elimination pass will get rid of what we don't use.
1272 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, BRW_IMAGE_PARAM_SIZE
);
1273 for (unsigned j
= 0; j
< BRW_IMAGE_PARAM_SIZE
; j
++) {
1274 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
1275 offset(tmp
, bld
, j
), offset(image
, bld
, j
),
1276 indirect
, brw_imm_ud((indirect_max
+ 1) * 4));
1283 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1286 for (unsigned i
= 0; i
< 4; i
++) {
1287 if (!((wr_mask
>> i
) & 1))
1290 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1291 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1292 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1293 if (new_inst
->src
[j
].file
== VGRF
)
1294 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1301 * Get the matching channel register datatype for an image intrinsic of the
1302 * specified GLSL image type.
1305 get_image_base_type(const glsl_type
*type
)
1307 switch ((glsl_base_type
)type
->sampled_type
) {
1308 case GLSL_TYPE_UINT
:
1309 return BRW_REGISTER_TYPE_UD
;
1311 return BRW_REGISTER_TYPE_D
;
1312 case GLSL_TYPE_FLOAT
:
1313 return BRW_REGISTER_TYPE_F
;
1315 unreachable("Not reached.");
1320 * Get the appropriate atomic op for an image atomic intrinsic.
1323 get_image_atomic_op(nir_intrinsic_op op
, const glsl_type
*type
)
1326 case nir_intrinsic_image_atomic_add
:
1328 case nir_intrinsic_image_atomic_min
:
1329 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1330 BRW_AOP_IMIN
: BRW_AOP_UMIN
);
1331 case nir_intrinsic_image_atomic_max
:
1332 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1333 BRW_AOP_IMAX
: BRW_AOP_UMAX
);
1334 case nir_intrinsic_image_atomic_and
:
1336 case nir_intrinsic_image_atomic_or
:
1338 case nir_intrinsic_image_atomic_xor
:
1340 case nir_intrinsic_image_atomic_exchange
:
1342 case nir_intrinsic_image_atomic_comp_swap
:
1343 return BRW_AOP_CMPWR
;
1345 unreachable("Not reachable.");
1350 emit_pixel_interpolater_send(const fs_builder
&bld
,
1355 glsl_interp_qualifier interpolation
)
1361 if (src
.file
== BAD_FILE
) {
1363 payload
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 1);
1367 mlen
= 2 * bld
.dispatch_width() / 8;
1370 inst
= bld
.emit(opcode
, dst
, payload
, desc
);
1372 /* 2 floats per slot returned */
1373 inst
->regs_written
= 2 * bld
.dispatch_width() / 8;
1374 inst
->pi_noperspective
= interpolation
== INTERP_QUALIFIER_NOPERSPECTIVE
;
1380 * Computes 1 << x, given a D/UD register containing some value x.
1383 intexp2(const fs_builder
&bld
, const fs_reg
&x
)
1385 assert(x
.type
== BRW_REGISTER_TYPE_UD
|| x
.type
== BRW_REGISTER_TYPE_D
);
1387 fs_reg result
= bld
.vgrf(x
.type
, 1);
1388 fs_reg one
= bld
.vgrf(x
.type
, 1);
1390 bld
.MOV(one
, retype(brw_imm_d(1), one
.type
));
1391 bld
.SHL(result
, one
, x
);
1396 fs_visitor::emit_gs_end_primitive(const nir_src
&vertex_count_nir_src
)
1398 assert(stage
== MESA_SHADER_GEOMETRY
);
1400 struct brw_gs_prog_data
*gs_prog_data
=
1401 (struct brw_gs_prog_data
*) prog_data
;
1403 /* We can only do EndPrimitive() functionality when the control data
1404 * consists of cut bits. Fortunately, the only time it isn't is when the
1405 * output type is points, in which case EndPrimitive() is a no-op.
1407 if (gs_prog_data
->control_data_format
!=
1408 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
1412 /* Cut bits use one bit per vertex. */
1413 assert(gs_compile
->control_data_bits_per_vertex
== 1);
1415 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1416 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1418 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1419 * vertex n, 0 otherwise. So all we need to do here is mark bit
1420 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1421 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1422 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1424 * Note that if EndPrimitive() is called before emitting any vertices, this
1425 * will cause us to set bit 31 of the control_data_bits register to 1.
1426 * That's fine because:
1428 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1429 * output, so the hardware will ignore cut bit 31.
1431 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1432 * last vertex, so setting cut bit 31 has no effect (since the primitive
1433 * is automatically ended when the GS terminates).
1435 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1436 * control_data_bits register to 0 when the first vertex is emitted.
1439 const fs_builder abld
= bld
.annotate("end primitive");
1441 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1442 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1443 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1444 fs_reg mask
= intexp2(abld
, prev_count
);
1445 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1446 * attention to the lower 5 bits of its second source argument, so on this
1447 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1448 * ((vertex_count - 1) % 32).
1450 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1454 fs_visitor::emit_gs_control_data_bits(const fs_reg
&vertex_count
)
1456 assert(stage
== MESA_SHADER_GEOMETRY
);
1457 assert(gs_compile
->control_data_bits_per_vertex
!= 0);
1459 struct brw_gs_prog_data
*gs_prog_data
=
1460 (struct brw_gs_prog_data
*) prog_data
;
1462 const fs_builder abld
= bld
.annotate("emit control data bits");
1463 const fs_builder fwa_bld
= bld
.exec_all();
1465 /* We use a single UD register to accumulate control data bits (32 bits
1466 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1469 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1470 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1471 * use the Channel Mask phase to enable/disable which DWord within that
1472 * group to write. (Remember, different SIMD8 channels may have emitted
1473 * different numbers of vertices, so we may need per-slot offsets.)
1475 * Channel masking presents an annoying problem: we may have to replicate
1476 * the data up to 4 times:
1478 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1480 * To avoid penalizing shaders that emit a small number of vertices, we
1481 * can avoid these sometimes: if the size of the control data header is
1482 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1483 * land in the same 128-bit group, so we can skip per-slot offsets.
1485 * Similarly, if the control data header is <= 32 bits, there is only one
1486 * DWord, so we can skip channel masks.
1488 enum opcode opcode
= SHADER_OPCODE_URB_WRITE_SIMD8
;
1490 fs_reg channel_mask
, per_slot_offset
;
1492 if (gs_compile
->control_data_header_size_bits
> 32) {
1493 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
1494 channel_mask
= vgrf(glsl_type::uint_type
);
1497 if (gs_compile
->control_data_header_size_bits
> 128) {
1498 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
;
1499 per_slot_offset
= vgrf(glsl_type::uint_type
);
1502 /* Figure out which DWord we're trying to write to using the formula:
1504 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1506 * Since bits_per_vertex is a power of two, and is known at compile
1507 * time, this can be optimized to:
1509 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1511 if (opcode
!= SHADER_OPCODE_URB_WRITE_SIMD8
) {
1512 fs_reg dword_index
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1513 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1514 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1515 unsigned log2_bits_per_vertex
=
1516 _mesa_fls(gs_compile
->control_data_bits_per_vertex
);
1517 abld
.SHR(dword_index
, prev_count
, brw_imm_ud(6u - log2_bits_per_vertex
));
1519 if (per_slot_offset
.file
!= BAD_FILE
) {
1520 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1521 * the appropriate OWord within the control data header.
1523 abld
.SHR(per_slot_offset
, dword_index
, brw_imm_ud(2u));
1526 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1527 * write to the appropriate DWORD within the OWORD.
1529 fs_reg channel
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1530 fwa_bld
.AND(channel
, dword_index
, brw_imm_ud(3u));
1531 channel_mask
= intexp2(fwa_bld
, channel
);
1532 /* Then the channel masks need to be in bits 23:16. */
1533 fwa_bld
.SHL(channel_mask
, channel_mask
, brw_imm_ud(16u));
1536 /* Store the control data bits in the message payload and send it. */
1538 if (channel_mask
.file
!= BAD_FILE
)
1539 mlen
+= 4; /* channel masks, plus 3 extra copies of the data */
1540 if (per_slot_offset
.file
!= BAD_FILE
)
1543 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
1544 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, mlen
);
1546 sources
[i
++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1547 if (per_slot_offset
.file
!= BAD_FILE
)
1548 sources
[i
++] = per_slot_offset
;
1549 if (channel_mask
.file
!= BAD_FILE
)
1550 sources
[i
++] = channel_mask
;
1552 sources
[i
++] = this->control_data_bits
;
1555 abld
.LOAD_PAYLOAD(payload
, sources
, mlen
, mlen
);
1556 fs_inst
*inst
= abld
.emit(opcode
, reg_undef
, payload
);
1558 /* We need to increment Global Offset by 256-bits to make room for
1559 * Broadwell's extra "Vertex Count" payload at the beginning of the
1560 * URB entry. Since this is an OWord message, Global Offset is counted
1561 * in 128-bit units, so we must set it to 2.
1563 if (gs_prog_data
->static_vertex_count
== -1)
1568 fs_visitor::set_gs_stream_control_data_bits(const fs_reg
&vertex_count
,
1571 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1573 /* Note: we are calling this *before* increasing vertex_count, so
1574 * this->vertex_count == vertex_count - 1 in the formula above.
1577 /* Stream mode uses 2 bits per vertex */
1578 assert(gs_compile
->control_data_bits_per_vertex
== 2);
1580 /* Must be a valid stream */
1581 assert(stream_id
>= 0 && stream_id
< MAX_VERTEX_STREAMS
);
1583 /* Control data bits are initialized to 0 so we don't have to set any
1584 * bits when sending vertices to stream 0.
1589 const fs_builder abld
= bld
.annotate("set stream control data bits", NULL
);
1591 /* reg::sid = stream_id */
1592 fs_reg sid
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1593 abld
.MOV(sid
, brw_imm_ud(stream_id
));
1595 /* reg:shift_count = 2 * (vertex_count - 1) */
1596 fs_reg shift_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1597 abld
.SHL(shift_count
, vertex_count
, brw_imm_ud(1u));
1599 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1600 * attention to the lower 5 bits of its second source argument, so on this
1601 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1602 * stream_id << ((2 * (vertex_count - 1)) % 32).
1604 fs_reg mask
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1605 abld
.SHL(mask
, sid
, shift_count
);
1606 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1610 fs_visitor::emit_gs_vertex(const nir_src
&vertex_count_nir_src
,
1613 assert(stage
== MESA_SHADER_GEOMETRY
);
1615 struct brw_gs_prog_data
*gs_prog_data
=
1616 (struct brw_gs_prog_data
*) prog_data
;
1618 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1619 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1621 /* Haswell and later hardware ignores the "Render Stream Select" bits
1622 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1623 * and instead sends all primitives down the pipeline for rasterization.
1624 * If the SOL stage is enabled, "Render Stream Select" is honored and
1625 * primitives bound to non-zero streams are discarded after stream output.
1627 * Since the only purpose of primives sent to non-zero streams is to
1628 * be recorded by transform feedback, we can simply discard all geometry
1629 * bound to these streams when transform feedback is disabled.
1631 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
1634 /* If we're outputting 32 control data bits or less, then we can wait
1635 * until the shader is over to output them all. Otherwise we need to
1636 * output them as we go. Now is the time to do it, since we're about to
1637 * output the vertex_count'th vertex, so it's guaranteed that the
1638 * control data bits associated with the (vertex_count - 1)th vertex are
1641 if (gs_compile
->control_data_header_size_bits
> 32) {
1642 const fs_builder abld
=
1643 bld
.annotate("emit vertex: emit control data bits");
1645 /* Only emit control data bits if we've finished accumulating a batch
1646 * of 32 bits. This is the case when:
1648 * (vertex_count * bits_per_vertex) % 32 == 0
1650 * (in other words, when the last 5 bits of vertex_count *
1651 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1652 * integer n (which is always the case, since bits_per_vertex is
1653 * always 1 or 2), this is equivalent to requiring that the last 5-n
1654 * bits of vertex_count are 0:
1656 * vertex_count & (2^(5-n) - 1) == 0
1658 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1661 * vertex_count & (32 / bits_per_vertex - 1) == 0
1663 * TODO: If vertex_count is an immediate, we could do some of this math
1664 * at compile time...
1667 abld
.AND(bld
.null_reg_d(), vertex_count
,
1668 brw_imm_ud(32u / gs_compile
->control_data_bits_per_vertex
- 1u));
1669 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1671 abld
.IF(BRW_PREDICATE_NORMAL
);
1672 /* If vertex_count is 0, then no control data bits have been
1673 * accumulated yet, so we can skip emitting them.
1675 abld
.CMP(bld
.null_reg_d(), vertex_count
, brw_imm_ud(0u),
1676 BRW_CONDITIONAL_NEQ
);
1677 abld
.IF(BRW_PREDICATE_NORMAL
);
1678 emit_gs_control_data_bits(vertex_count
);
1679 abld
.emit(BRW_OPCODE_ENDIF
);
1681 /* Reset control_data_bits to 0 so we can start accumulating a new
1684 * Note: in the case where vertex_count == 0, this neutralizes the
1685 * effect of any call to EndPrimitive() that the shader may have
1686 * made before outputting its first vertex.
1688 inst
= abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
1689 inst
->force_writemask_all
= true;
1690 abld
.emit(BRW_OPCODE_ENDIF
);
1693 emit_urb_writes(vertex_count
);
1695 /* In stream mode we have to set control data bits for all vertices
1696 * unless we have disabled control data bits completely (which we do
1697 * do for GL_POINTS outputs that don't use streams).
1699 if (gs_compile
->control_data_header_size_bits
> 0 &&
1700 gs_prog_data
->control_data_format
==
1701 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
1702 set_gs_stream_control_data_bits(vertex_count
, stream_id
);
1707 fs_visitor::emit_gs_input_load(const fs_reg
&dst
,
1708 const nir_src
&vertex_src
,
1709 unsigned base_offset
,
1710 const nir_src
&offset_src
,
1711 unsigned num_components
)
1713 struct brw_gs_prog_data
*gs_prog_data
= (struct brw_gs_prog_data
*) prog_data
;
1715 nir_const_value
*vertex_const
= nir_src_as_const_value(vertex_src
);
1716 nir_const_value
*offset_const
= nir_src_as_const_value(offset_src
);
1717 const unsigned push_reg_count
= gs_prog_data
->base
.urb_read_length
* 8;
1719 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
1720 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
1721 * gl_PointSize is available as a GS input, however, so it must be that.
1723 const bool is_point_size
= (base_offset
== 0);
1725 if (offset_const
!= NULL
&& vertex_const
!= NULL
&&
1726 4 * (base_offset
+ offset_const
->u
[0]) < push_reg_count
) {
1727 int imm_offset
= (base_offset
+ offset_const
->u
[0]) * 4 +
1728 vertex_const
->u
[0] * push_reg_count
;
1729 /* This input was pushed into registers. */
1730 if (is_point_size
) {
1731 /* gl_PointSize comes in .w */
1732 assert(imm_offset
== 0);
1733 bld
.MOV(dst
, fs_reg(ATTR
, imm_offset
+ 3, dst
.type
));
1735 for (unsigned i
= 0; i
< num_components
; i
++) {
1736 bld
.MOV(offset(dst
, bld
, i
),
1737 fs_reg(ATTR
, imm_offset
+ i
, dst
.type
));
1741 /* Resort to the pull model. Ensure the VUE handles are provided. */
1742 gs_prog_data
->base
.include_vue_handles
= true;
1744 unsigned first_icp_handle
= gs_prog_data
->include_primitive_id
? 3 : 2;
1748 /* The vertex index is constant; just select the proper URB handle. */
1750 retype(brw_vec8_grf(first_icp_handle
+ vertex_const
->i
[0], 0),
1751 BRW_REGISTER_TYPE_UD
);
1753 /* The vertex index is non-constant. We need to use indirect
1754 * addressing to fetch the proper URB handle.
1756 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
1757 * indicating that channel <n> should read the handle from
1758 * DWord <n>. We convert that to bytes by multiplying by 4.
1760 * Next, we convert the vertex index to bytes by multiplying
1761 * by 32 (shifting by 5), and add the two together. This is
1762 * the final indirect byte offset.
1764 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_W
, 1);
1765 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1766 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1767 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1768 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1770 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
1771 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
1772 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
1773 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
1774 /* Convert vertex_index to bytes (multiply by 32) */
1775 bld
.SHL(vertex_offset_bytes
,
1776 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
1778 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
1780 /* Use first_icp_handle as the base offset. There is one register
1781 * of URB handles per vertex, so inform the register allocator that
1782 * we might read up to nir->info.gs.vertices_in registers.
1784 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
1785 fs_reg(brw_vec8_grf(first_icp_handle
, 0)),
1786 fs_reg(icp_offset_bytes
),
1787 brw_imm_ud(nir
->info
.gs
.vertices_in
* REG_SIZE
));
1792 /* Constant indexing - use global offset. */
1793 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
1794 inst
->offset
= base_offset
+ offset_const
->u
[0];
1795 inst
->base_mrf
= -1;
1797 inst
->regs_written
= num_components
;
1799 /* Indirect indexing - use per-slot offsets as well. */
1800 const fs_reg srcs
[] = { icp_handle
, get_nir_src(offset_src
) };
1801 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1802 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
1804 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
, payload
);
1805 inst
->offset
= base_offset
;
1806 inst
->base_mrf
= -1;
1808 inst
->regs_written
= num_components
;
1811 if (is_point_size
) {
1812 /* Read the whole VUE header (because of alignment) and read .w. */
1813 fs_reg tmp
= bld
.vgrf(dst
.type
, 4);
1815 inst
->regs_written
= 4;
1816 bld
.MOV(dst
, offset(tmp
, bld
, 3));
1822 fs_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
1824 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
1825 nir_const_value
*const_value
= nir_src_as_const_value(*offset_src
);
1828 /* The only constant offset we should find is 0. brw_nir.c's
1829 * add_const_offset_to_base() will fold other constant offsets
1830 * into instr->const_index[0].
1832 assert(const_value
->u
[0] == 0);
1836 return get_nir_src(*offset_src
);
1840 fs_visitor::nir_emit_vs_intrinsic(const fs_builder
&bld
,
1841 nir_intrinsic_instr
*instr
)
1843 assert(stage
== MESA_SHADER_VERTEX
);
1846 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1847 dest
= get_nir_dest(instr
->dest
);
1849 switch (instr
->intrinsic
) {
1850 case nir_intrinsic_load_vertex_id
:
1851 unreachable("should be lowered by lower_vertex_id()");
1853 case nir_intrinsic_load_vertex_id_zero_base
:
1854 case nir_intrinsic_load_base_vertex
:
1855 case nir_intrinsic_load_instance_id
:
1856 case nir_intrinsic_load_base_instance
:
1857 case nir_intrinsic_load_draw_id
: {
1858 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
1859 fs_reg val
= nir_system_values
[sv
];
1860 assert(val
.file
!= BAD_FILE
);
1861 dest
.type
= val
.type
;
1867 nir_emit_intrinsic(bld
, instr
);
1873 fs_visitor::nir_emit_tes_intrinsic(const fs_builder
&bld
,
1874 nir_intrinsic_instr
*instr
)
1876 assert(stage
== MESA_SHADER_TESS_EVAL
);
1877 struct brw_tes_prog_data
*tes_prog_data
= (struct brw_tes_prog_data
*) prog_data
;
1880 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1881 dest
= get_nir_dest(instr
->dest
);
1883 switch (instr
->intrinsic
) {
1884 case nir_intrinsic_load_primitive_id
:
1885 bld
.MOV(dest
, fs_reg(brw_vec1_grf(0, 1)));
1887 case nir_intrinsic_load_tess_coord
:
1888 /* gl_TessCoord is part of the payload in g1-3 */
1889 for (unsigned i
= 0; i
< 3; i
++) {
1890 bld
.MOV(offset(dest
, bld
, i
), fs_reg(brw_vec8_grf(1 + i
, 0)));
1894 case nir_intrinsic_load_tess_level_outer
:
1895 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
1896 * appears as a push-model input. So, we can simply use the ATTR file
1897 * rather than issuing URB read messages. The data is stored in the
1898 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
1901 switch (tes_prog_data
->domain
) {
1902 case BRW_TESS_DOMAIN_QUAD
:
1903 for (unsigned i
= 0; i
< 4; i
++)
1904 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1906 case BRW_TESS_DOMAIN_TRI
:
1907 for (unsigned i
= 0; i
< 3; i
++)
1908 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1910 case BRW_TESS_DOMAIN_ISOLINE
:
1911 for (unsigned i
= 0; i
< 2; i
++)
1912 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1917 case nir_intrinsic_load_tess_level_inner
:
1918 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
1919 * appears as a push-model input. So, we can simply use the ATTR file
1920 * rather than issuing URB read messages.
1922 switch (tes_prog_data
->domain
) {
1923 case BRW_TESS_DOMAIN_QUAD
:
1924 bld
.MOV(dest
, component(fs_reg(ATTR
, 0), 3));
1925 bld
.MOV(offset(dest
, bld
, 1), component(fs_reg(ATTR
, 0), 2));
1927 case BRW_TESS_DOMAIN_TRI
:
1928 bld
.MOV(dest
, component(fs_reg(ATTR
, 0), 4));
1930 case BRW_TESS_DOMAIN_ISOLINE
:
1931 /* ignore - value is undefined */
1936 case nir_intrinsic_load_input
:
1937 case nir_intrinsic_load_per_vertex_input
: {
1938 fs_reg indirect_offset
= get_indirect_offset(instr
);
1939 unsigned imm_offset
= instr
->const_index
[0];
1942 if (indirect_offset
.file
== BAD_FILE
) {
1943 /* Arbitrarily only push up to 32 vec4 slots worth of data,
1944 * which is 16 registers (since each holds 2 vec4 slots).
1946 const unsigned max_push_slots
= 32;
1947 if (imm_offset
< max_push_slots
) {
1948 fs_reg src
= fs_reg(ATTR
, imm_offset
/ 2, dest
.type
);
1949 for (int i
= 0; i
< instr
->num_components
; i
++) {
1950 bld
.MOV(offset(dest
, bld
, i
),
1951 component(src
, 4 * (imm_offset
% 2) + i
));
1953 tes_prog_data
->base
.urb_read_length
=
1954 MAX2(tes_prog_data
->base
.urb_read_length
,
1955 DIV_ROUND_UP(imm_offset
+ 1, 2));
1957 /* Replicate the patch handle to all enabled channels */
1958 const fs_reg srcs
[] = {
1959 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)
1961 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1962 bld
.LOAD_PAYLOAD(patch_handle
, srcs
, ARRAY_SIZE(srcs
), 0);
1964 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dest
, patch_handle
);
1966 inst
->offset
= imm_offset
;
1967 inst
->base_mrf
= -1;
1968 inst
->regs_written
= instr
->num_components
;
1971 /* Indirect indexing - use per-slot offsets as well. */
1972 const fs_reg srcs
[] = {
1973 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
1976 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1977 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
1979 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dest
, payload
);
1981 inst
->offset
= imm_offset
;
1982 inst
->base_mrf
= -1;
1983 inst
->regs_written
= instr
->num_components
;
1988 nir_emit_intrinsic(bld
, instr
);
1994 fs_visitor::nir_emit_gs_intrinsic(const fs_builder
&bld
,
1995 nir_intrinsic_instr
*instr
)
1997 assert(stage
== MESA_SHADER_GEOMETRY
);
1998 fs_reg indirect_offset
;
2001 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2002 dest
= get_nir_dest(instr
->dest
);
2004 switch (instr
->intrinsic
) {
2005 case nir_intrinsic_load_primitive_id
:
2006 assert(stage
== MESA_SHADER_GEOMETRY
);
2007 assert(((struct brw_gs_prog_data
*)prog_data
)->include_primitive_id
);
2008 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
2009 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD
));
2012 case nir_intrinsic_load_input
:
2013 unreachable("load_input intrinsics are invalid for the GS stage");
2015 case nir_intrinsic_load_per_vertex_input
:
2016 emit_gs_input_load(dest
, instr
->src
[0], instr
->const_index
[0],
2017 instr
->src
[1], instr
->num_components
);
2020 case nir_intrinsic_emit_vertex_with_counter
:
2021 emit_gs_vertex(instr
->src
[0], instr
->const_index
[0]);
2024 case nir_intrinsic_end_primitive_with_counter
:
2025 emit_gs_end_primitive(instr
->src
[0]);
2028 case nir_intrinsic_set_vertex_count
:
2029 bld
.MOV(this->final_gs_vertex_count
, get_nir_src(instr
->src
[0]));
2032 case nir_intrinsic_load_invocation_id
: {
2033 fs_reg val
= nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
2034 assert(val
.file
!= BAD_FILE
);
2035 dest
.type
= val
.type
;
2041 nir_emit_intrinsic(bld
, instr
);
2047 fs_visitor::nir_emit_fs_intrinsic(const fs_builder
&bld
,
2048 nir_intrinsic_instr
*instr
)
2050 assert(stage
== MESA_SHADER_FRAGMENT
);
2051 struct brw_wm_prog_data
*wm_prog_data
=
2052 (struct brw_wm_prog_data
*) prog_data
;
2055 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2056 dest
= get_nir_dest(instr
->dest
);
2058 switch (instr
->intrinsic
) {
2059 case nir_intrinsic_load_front_face
:
2060 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
2061 *emit_frontfacing_interpolation());
2064 case nir_intrinsic_load_sample_pos
: {
2065 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
2066 assert(sample_pos
.file
!= BAD_FILE
);
2067 dest
.type
= sample_pos
.type
;
2068 bld
.MOV(dest
, sample_pos
);
2069 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
2073 case nir_intrinsic_load_helper_invocation
:
2074 case nir_intrinsic_load_sample_mask_in
:
2075 case nir_intrinsic_load_sample_id
: {
2076 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
2077 fs_reg val
= nir_system_values
[sv
];
2078 assert(val
.file
!= BAD_FILE
);
2079 dest
.type
= val
.type
;
2084 case nir_intrinsic_discard
:
2085 case nir_intrinsic_discard_if
: {
2086 /* We track our discarded pixels in f0.1. By predicating on it, we can
2087 * update just the flag bits that aren't yet discarded. If there's no
2088 * condition, we emit a CMP of g0 != g0, so all currently executing
2089 * channels will get turned off.
2092 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2093 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
2094 brw_imm_d(0), BRW_CONDITIONAL_Z
);
2096 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
2097 BRW_REGISTER_TYPE_UW
));
2098 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
2100 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
2101 cmp
->flag_subreg
= 1;
2103 if (devinfo
->gen
>= 6) {
2104 emit_discard_jump();
2109 case nir_intrinsic_interp_var_at_centroid
:
2110 case nir_intrinsic_interp_var_at_sample
:
2111 case nir_intrinsic_interp_var_at_offset
: {
2112 /* Handle ARB_gpu_shader5 interpolation intrinsics
2114 * It's worth a quick word of explanation as to why we handle the full
2115 * variable-based interpolation intrinsic rather than a lowered version
2116 * with like we do for other inputs. We have to do that because the way
2117 * we set up inputs doesn't allow us to use the already setup inputs for
2118 * interpolation. At the beginning of the shader, we go through all of
2119 * the input variables and do the initial interpolation and put it in
2120 * the nir_inputs array based on its location as determined in
2121 * nir_lower_io. If the input isn't used, dead code cleans up and
2122 * everything works fine. However, when we get to the ARB_gpu_shader5
2123 * interpolation intrinsics, we need to reinterpolate the input
2124 * differently. If we used an intrinsic that just had an index it would
2125 * only give us the offset into the nir_inputs array. However, this is
2126 * useless because that value is post-interpolation and we need
2127 * pre-interpolation. In order to get the actual location of the bits
2128 * we get from the vertex fetching hardware, we need the variable.
2130 wm_prog_data
->pulls_bary
= true;
2132 fs_reg dst_xy
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 2);
2133 const glsl_interp_qualifier interpolation
=
2134 (glsl_interp_qualifier
) instr
->variables
[0]->var
->data
.interpolation
;
2136 switch (instr
->intrinsic
) {
2137 case nir_intrinsic_interp_var_at_centroid
:
2138 emit_pixel_interpolater_send(bld
,
2139 FS_OPCODE_INTERPOLATE_AT_CENTROID
,
2146 case nir_intrinsic_interp_var_at_sample
: {
2147 nir_const_value
*const_sample
= nir_src_as_const_value(instr
->src
[0]);
2150 unsigned msg_data
= const_sample
->i
[0] << 4;
2152 emit_pixel_interpolater_send(bld
,
2153 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2156 brw_imm_ud(msg_data
),
2159 const fs_reg sample_src
= retype(get_nir_src(instr
->src
[0]),
2160 BRW_REGISTER_TYPE_UD
);
2162 if (nir_src_is_dynamically_uniform(instr
->src
[0])) {
2163 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
2164 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
2165 bld
.exec_all().group(1, 0)
2166 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
2167 emit_pixel_interpolater_send(bld
,
2168 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2174 /* Make a loop that sends a message to the pixel interpolater
2175 * for the sample number in each live channel. If there are
2176 * multiple channels with the same sample number then these
2177 * will be handled simultaneously with a single interation of
2180 bld
.emit(BRW_OPCODE_DO
);
2182 /* Get the next live sample number into sample_id_reg */
2183 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
2185 /* Set the flag register so that we can perform the send
2186 * message on all channels that have the same sample number
2188 bld
.CMP(bld
.null_reg_ud(),
2189 sample_src
, sample_id
,
2190 BRW_CONDITIONAL_EQ
);
2191 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
2192 bld
.exec_all().group(1, 0)
2193 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
2195 emit_pixel_interpolater_send(bld
,
2196 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2201 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
2203 /* Continue the loop if there are any live channels left */
2204 set_predicate_inv(BRW_PREDICATE_NORMAL
,
2206 bld
.emit(BRW_OPCODE_WHILE
));
2213 case nir_intrinsic_interp_var_at_offset
: {
2214 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2217 unsigned off_x
= MIN2((int)(const_offset
->f
[0] * 16), 7) & 0xf;
2218 unsigned off_y
= MIN2((int)(const_offset
->f
[1] * 16), 7) & 0xf;
2220 emit_pixel_interpolater_send(bld
,
2221 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
,
2224 brw_imm_ud(off_x
| (off_y
<< 4)),
2227 fs_reg src
= vgrf(glsl_type::ivec2_type
);
2228 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
2229 BRW_REGISTER_TYPE_F
);
2230 for (int i
= 0; i
< 2; i
++) {
2231 fs_reg temp
= vgrf(glsl_type::float_type
);
2232 bld
.MUL(temp
, offset(offset_src
, bld
, i
), brw_imm_f(16.0f
));
2233 fs_reg itemp
= vgrf(glsl_type::int_type
);
2234 bld
.MOV(itemp
, temp
); /* float to int */
2236 /* Clamp the upper end of the range to +7/16.
2237 * ARB_gpu_shader5 requires that we support a maximum offset
2238 * of +0.5, which isn't representable in a S0.4 value -- if
2239 * we didn't clamp it, we'd end up with -8/16, which is the
2240 * opposite of what the shader author wanted.
2242 * This is legal due to ARB_gpu_shader5's quantization
2245 * "Not all values of <offset> may be supported; x and y
2246 * offsets may be rounded to fixed-point values with the
2247 * number of fraction bits given by the
2248 * implementation-dependent constant
2249 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
2251 set_condmod(BRW_CONDITIONAL_L
,
2252 bld
.SEL(offset(src
, bld
, i
), itemp
, brw_imm_d(7)));
2255 const enum opcode opcode
= FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
;
2256 emit_pixel_interpolater_send(bld
,
2267 unreachable("Invalid intrinsic");
2270 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2271 fs_reg src
= interp_reg(instr
->variables
[0]->var
->data
.location
, j
);
2272 src
.type
= dest
.type
;
2274 bld
.emit(FS_OPCODE_LINTERP
, dest
, dst_xy
, src
);
2275 dest
= offset(dest
, bld
, 1);
2280 nir_emit_intrinsic(bld
, instr
);
2286 fs_visitor::nir_emit_cs_intrinsic(const fs_builder
&bld
,
2287 nir_intrinsic_instr
*instr
)
2289 assert(stage
== MESA_SHADER_COMPUTE
);
2290 struct brw_cs_prog_data
*cs_prog_data
=
2291 (struct brw_cs_prog_data
*) prog_data
;
2294 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2295 dest
= get_nir_dest(instr
->dest
);
2297 switch (instr
->intrinsic
) {
2298 case nir_intrinsic_barrier
:
2300 cs_prog_data
->uses_barrier
= true;
2303 case nir_intrinsic_load_local_invocation_id
:
2304 case nir_intrinsic_load_work_group_id
: {
2305 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
2306 fs_reg val
= nir_system_values
[sv
];
2307 assert(val
.file
!= BAD_FILE
);
2308 dest
.type
= val
.type
;
2309 for (unsigned i
= 0; i
< 3; i
++)
2310 bld
.MOV(offset(dest
, bld
, i
), offset(val
, bld
, i
));
2314 case nir_intrinsic_load_num_work_groups
: {
2315 const unsigned surface
=
2316 cs_prog_data
->binding_table
.work_groups_start
;
2318 cs_prog_data
->uses_num_work_groups
= true;
2320 fs_reg surf_index
= brw_imm_ud(surface
);
2321 brw_mark_surface_used(prog_data
, surface
);
2323 /* Read the 3 GLuint components of gl_NumWorkGroups */
2324 for (unsigned i
= 0; i
< 3; i
++) {
2325 fs_reg read_result
=
2326 emit_untyped_read(bld
, surf_index
,
2328 1 /* dims */, 1 /* size */,
2329 BRW_PREDICATE_NONE
);
2330 read_result
.type
= dest
.type
;
2331 bld
.MOV(dest
, read_result
);
2332 dest
= offset(dest
, bld
, 1);
2337 case nir_intrinsic_shared_atomic_add
:
2338 nir_emit_shared_atomic(bld
, BRW_AOP_ADD
, instr
);
2340 case nir_intrinsic_shared_atomic_imin
:
2341 nir_emit_shared_atomic(bld
, BRW_AOP_IMIN
, instr
);
2343 case nir_intrinsic_shared_atomic_umin
:
2344 nir_emit_shared_atomic(bld
, BRW_AOP_UMIN
, instr
);
2346 case nir_intrinsic_shared_atomic_imax
:
2347 nir_emit_shared_atomic(bld
, BRW_AOP_IMAX
, instr
);
2349 case nir_intrinsic_shared_atomic_umax
:
2350 nir_emit_shared_atomic(bld
, BRW_AOP_UMAX
, instr
);
2352 case nir_intrinsic_shared_atomic_and
:
2353 nir_emit_shared_atomic(bld
, BRW_AOP_AND
, instr
);
2355 case nir_intrinsic_shared_atomic_or
:
2356 nir_emit_shared_atomic(bld
, BRW_AOP_OR
, instr
);
2358 case nir_intrinsic_shared_atomic_xor
:
2359 nir_emit_shared_atomic(bld
, BRW_AOP_XOR
, instr
);
2361 case nir_intrinsic_shared_atomic_exchange
:
2362 nir_emit_shared_atomic(bld
, BRW_AOP_MOV
, instr
);
2364 case nir_intrinsic_shared_atomic_comp_swap
:
2365 nir_emit_shared_atomic(bld
, BRW_AOP_CMPWR
, instr
);
2368 case nir_intrinsic_load_shared
: {
2369 assert(devinfo
->gen
>= 7);
2371 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
2373 /* Get the offset to read from */
2375 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2377 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u
[0]);
2379 offset_reg
= vgrf(glsl_type::uint_type
);
2381 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
2382 brw_imm_ud(instr
->const_index
[0]));
2385 /* Read the vector */
2386 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
2388 instr
->num_components
,
2389 BRW_PREDICATE_NONE
);
2390 read_result
.type
= dest
.type
;
2391 for (int i
= 0; i
< instr
->num_components
; i
++)
2392 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
2397 case nir_intrinsic_store_shared
: {
2398 assert(devinfo
->gen
>= 7);
2401 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
2404 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
2407 unsigned writemask
= instr
->const_index
[1];
2409 /* Combine groups of consecutive enabled channels in one write
2410 * message. We use ffs to find the first enabled channel and then ffs on
2411 * the bit-inverse, down-shifted writemask to determine the length of
2412 * the block of enabled bits.
2415 unsigned first_component
= ffs(writemask
) - 1;
2416 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
2419 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2421 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u
[0] +
2422 4 * first_component
);
2424 offset_reg
= vgrf(glsl_type::uint_type
);
2426 retype(get_nir_src(instr
->src
[1]), BRW_REGISTER_TYPE_UD
),
2427 brw_imm_ud(instr
->const_index
[0] + 4 * first_component
));
2430 emit_untyped_write(bld
, surf_index
, offset_reg
,
2431 offset(val_reg
, bld
, first_component
),
2432 1 /* dims */, length
,
2433 BRW_PREDICATE_NONE
);
2435 /* Clear the bits in the writemask that we just wrote, then try
2436 * again to see if more channels are left.
2438 writemask
&= (15 << (first_component
+ length
));
2445 nir_emit_intrinsic(bld
, instr
);
2451 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
2454 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2455 dest
= get_nir_dest(instr
->dest
);
2457 switch (instr
->intrinsic
) {
2458 case nir_intrinsic_atomic_counter_inc
:
2459 case nir_intrinsic_atomic_counter_dec
:
2460 case nir_intrinsic_atomic_counter_read
: {
2461 using namespace surface_access
;
2463 /* Get the arguments of the atomic intrinsic. */
2464 const fs_reg offset
= get_nir_src(instr
->src
[0]);
2465 const unsigned surface
= (stage_prog_data
->binding_table
.abo_start
+
2466 instr
->const_index
[0]);
2469 /* Emit a surface read or atomic op. */
2470 switch (instr
->intrinsic
) {
2471 case nir_intrinsic_atomic_counter_read
:
2472 tmp
= emit_untyped_read(bld
, brw_imm_ud(surface
), offset
, 1, 1);
2475 case nir_intrinsic_atomic_counter_inc
:
2476 tmp
= emit_untyped_atomic(bld
, brw_imm_ud(surface
), offset
, fs_reg(),
2477 fs_reg(), 1, 1, BRW_AOP_INC
);
2480 case nir_intrinsic_atomic_counter_dec
:
2481 tmp
= emit_untyped_atomic(bld
, brw_imm_ud(surface
), offset
, fs_reg(),
2482 fs_reg(), 1, 1, BRW_AOP_PREDEC
);
2486 unreachable("Unreachable");
2489 /* Assign the result. */
2490 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), tmp
);
2492 /* Mark the surface as used. */
2493 brw_mark_surface_used(stage_prog_data
, surface
);
2497 case nir_intrinsic_image_load
:
2498 case nir_intrinsic_image_store
:
2499 case nir_intrinsic_image_atomic_add
:
2500 case nir_intrinsic_image_atomic_min
:
2501 case nir_intrinsic_image_atomic_max
:
2502 case nir_intrinsic_image_atomic_and
:
2503 case nir_intrinsic_image_atomic_or
:
2504 case nir_intrinsic_image_atomic_xor
:
2505 case nir_intrinsic_image_atomic_exchange
:
2506 case nir_intrinsic_image_atomic_comp_swap
: {
2507 using namespace image_access
;
2509 /* Get the referenced image variable and type. */
2510 const nir_variable
*var
= instr
->variables
[0]->var
;
2511 const glsl_type
*type
= var
->type
->without_array();
2512 const brw_reg_type base_type
= get_image_base_type(type
);
2514 /* Get some metadata from the image intrinsic. */
2515 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
2516 const unsigned arr_dims
= type
->sampler_array
? 1 : 0;
2517 const unsigned surf_dims
= type
->coordinate_components() - arr_dims
;
2518 const mesa_format format
=
2519 (var
->data
.image
.write_only
? MESA_FORMAT_NONE
:
2520 _mesa_get_shader_image_format(var
->data
.image
.format
));
2522 /* Get the arguments of the image intrinsic. */
2523 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
2524 const fs_reg addr
= retype(get_nir_src(instr
->src
[0]),
2525 BRW_REGISTER_TYPE_UD
);
2526 const fs_reg src0
= (info
->num_srcs
>= 3 ?
2527 retype(get_nir_src(instr
->src
[2]), base_type
) :
2529 const fs_reg src1
= (info
->num_srcs
>= 4 ?
2530 retype(get_nir_src(instr
->src
[3]), base_type
) :
2534 /* Emit an image load, store or atomic op. */
2535 if (instr
->intrinsic
== nir_intrinsic_image_load
)
2536 tmp
= emit_image_load(bld
, image
, addr
, surf_dims
, arr_dims
, format
);
2538 else if (instr
->intrinsic
== nir_intrinsic_image_store
)
2539 emit_image_store(bld
, image
, addr
, src0
, surf_dims
, arr_dims
, format
);
2542 tmp
= emit_image_atomic(bld
, image
, addr
, src0
, src1
,
2543 surf_dims
, arr_dims
, info
->dest_components
,
2544 get_image_atomic_op(instr
->intrinsic
, type
));
2546 /* Assign the result. */
2547 for (unsigned c
= 0; c
< info
->dest_components
; ++c
)
2548 bld
.MOV(offset(retype(dest
, base_type
), bld
, c
),
2549 offset(tmp
, bld
, c
));
2553 case nir_intrinsic_memory_barrier_atomic_counter
:
2554 case nir_intrinsic_memory_barrier_buffer
:
2555 case nir_intrinsic_memory_barrier_image
:
2556 case nir_intrinsic_memory_barrier
: {
2557 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 16 / dispatch_width
);
2558 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
2563 case nir_intrinsic_group_memory_barrier
:
2564 case nir_intrinsic_memory_barrier_shared
:
2565 /* We treat these workgroup-level barriers as no-ops. This should be
2566 * safe at present and as long as:
2568 * - Memory access instructions are not subsequently reordered by the
2569 * compiler back-end.
2571 * - All threads from a given compute shader workgroup fit within a
2572 * single subslice and therefore talk to the same HDC shared unit
2573 * what supposedly guarantees ordering and coherency between threads
2574 * from the same workgroup. This may change in the future when we
2575 * start splitting workgroups across multiple subslices.
2577 * - The context is not in fault-and-stream mode, which could cause
2578 * memory transactions (including to SLM) prior to the barrier to be
2579 * replayed after the barrier if a pagefault occurs. This shouldn't
2580 * be a problem up to and including SKL because fault-and-stream is
2581 * not usable due to hardware issues, but that's likely to change in
2586 case nir_intrinsic_shader_clock
: {
2587 /* We cannot do anything if there is an event, so ignore it for now */
2588 fs_reg shader_clock
= get_timestamp(bld
);
2589 const fs_reg srcs
[] = { shader_clock
.set_smear(0), shader_clock
.set_smear(1) };
2591 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
2595 case nir_intrinsic_image_size
: {
2596 /* Get the referenced image variable and type. */
2597 const nir_variable
*var
= instr
->variables
[0]->var
;
2598 const glsl_type
*type
= var
->type
->without_array();
2600 /* Get the size of the image. */
2601 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
2602 const fs_reg size
= offset(image
, bld
, BRW_IMAGE_PARAM_SIZE_OFFSET
);
2604 /* For 1DArray image types, the array index is stored in the Z component.
2605 * Fix this by swizzling the Z component to the Y component.
2607 const bool is_1d_array_image
=
2608 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
&&
2609 type
->sampler_array
;
2611 /* For CubeArray images, we should count the number of cubes instead
2612 * of the number of faces. Fix it by dividing the (Z component) by 6.
2614 const bool is_cube_array_image
=
2615 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2616 type
->sampler_array
;
2618 /* Copy all the components. */
2619 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
2620 for (unsigned c
= 0; c
< info
->dest_components
; ++c
) {
2621 if ((int)c
>= type
->coordinate_components()) {
2622 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2624 } else if (c
== 1 && is_1d_array_image
) {
2625 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2626 offset(size
, bld
, 2));
2627 } else if (c
== 2 && is_cube_array_image
) {
2628 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
2629 offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2630 offset(size
, bld
, c
), brw_imm_d(6));
2632 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2633 offset(size
, bld
, c
));
2640 case nir_intrinsic_image_samples
:
2641 /* The driver does not support multi-sampled images. */
2642 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(1));
2645 case nir_intrinsic_load_uniform
: {
2646 /* Offsets are in bytes but they should always be multiples of 4 */
2647 assert(instr
->const_index
[0] % 4 == 0);
2649 fs_reg
src(UNIFORM
, instr
->const_index
[0] / 4, dest
.type
);
2651 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2653 /* Offsets are in bytes but they should always be multiples of 4 */
2654 assert(const_offset
->u
[0] % 4 == 0);
2655 src
.reg_offset
= const_offset
->u
[0] / 4;
2657 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2658 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
2661 fs_reg indirect
= retype(get_nir_src(instr
->src
[0]),
2662 BRW_REGISTER_TYPE_UD
);
2664 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
2665 * go past the end of the uniform. In order to keep the n'th
2666 * component from running past, we subtract off the size of all but
2667 * one component of the vector.
2669 assert(instr
->const_index
[1] >= instr
->num_components
* 4);
2670 unsigned read_size
= instr
->const_index
[1] -
2671 (instr
->num_components
- 1) * 4;
2673 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2674 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
2675 offset(dest
, bld
, j
), offset(src
, bld
, j
),
2676 indirect
, brw_imm_ud(read_size
));
2682 case nir_intrinsic_load_ubo
: {
2683 nir_const_value
*const_index
= nir_src_as_const_value(instr
->src
[0]);
2687 const unsigned index
= stage_prog_data
->binding_table
.ubo_start
+
2689 surf_index
= brw_imm_ud(index
);
2690 brw_mark_surface_used(prog_data
, index
);
2692 /* The block index is not a constant. Evaluate the index expression
2693 * per-channel and add the base UBO index; we have to select a value
2694 * from any live channel.
2696 surf_index
= vgrf(glsl_type::uint_type
);
2697 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
2698 brw_imm_ud(stage_prog_data
->binding_table
.ubo_start
));
2699 surf_index
= bld
.emit_uniformize(surf_index
);
2701 /* Assume this may touch any UBO. It would be nice to provide
2702 * a tighter bound, but the array information is already lowered away.
2704 brw_mark_surface_used(prog_data
,
2705 stage_prog_data
->binding_table
.ubo_start
+
2706 nir
->info
.num_ubos
- 1);
2709 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2710 if (const_offset
== NULL
) {
2711 fs_reg base_offset
= retype(get_nir_src(instr
->src
[1]),
2712 BRW_REGISTER_TYPE_D
);
2714 for (int i
= 0; i
< instr
->num_components
; i
++)
2715 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
2716 base_offset
, i
* 4);
2718 fs_reg packed_consts
= vgrf(glsl_type::float_type
);
2719 packed_consts
.type
= dest
.type
;
2721 struct brw_reg const_offset_reg
= brw_imm_ud(const_offset
->u
[0] & ~15);
2722 bld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, packed_consts
,
2723 surf_index
, const_offset_reg
);
2725 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2726 packed_consts
.set_smear(const_offset
->u
[0] % 16 / 4 + i
);
2728 /* The std140 packing rules don't allow vectors to cross 16-byte
2729 * boundaries, and a reg is 32 bytes.
2731 assert(packed_consts
.subreg_offset
< 32);
2733 bld
.MOV(dest
, packed_consts
);
2734 dest
= offset(dest
, bld
, 1);
2740 case nir_intrinsic_load_ssbo
: {
2741 assert(devinfo
->gen
>= 7);
2743 nir_const_value
*const_uniform_block
=
2744 nir_src_as_const_value(instr
->src
[0]);
2747 if (const_uniform_block
) {
2748 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
2749 const_uniform_block
->u
[0];
2750 surf_index
= brw_imm_ud(index
);
2751 brw_mark_surface_used(prog_data
, index
);
2753 surf_index
= vgrf(glsl_type::uint_type
);
2754 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
2755 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2757 /* Assume this may touch any UBO. It would be nice to provide
2758 * a tighter bound, but the array information is already lowered away.
2760 brw_mark_surface_used(prog_data
,
2761 stage_prog_data
->binding_table
.ssbo_start
+
2762 nir
->info
.num_ssbos
- 1);
2766 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2768 offset_reg
= brw_imm_ud(const_offset
->u
[0]);
2770 offset_reg
= get_nir_src(instr
->src
[1]);
2773 /* Read the vector */
2774 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
2776 instr
->num_components
,
2777 BRW_PREDICATE_NONE
);
2778 read_result
.type
= dest
.type
;
2779 for (int i
= 0; i
< instr
->num_components
; i
++)
2780 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
2785 case nir_intrinsic_load_input
: {
2787 if (stage
== MESA_SHADER_VERTEX
) {
2788 src
= fs_reg(ATTR
, instr
->const_index
[0], dest
.type
);
2790 src
= offset(retype(nir_inputs
, dest
.type
), bld
,
2791 instr
->const_index
[0]);
2794 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2795 assert(const_offset
&& "Indirect input loads not allowed");
2796 src
= offset(src
, bld
, const_offset
->u
[0]);
2798 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2799 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
2804 case nir_intrinsic_store_ssbo
: {
2805 assert(devinfo
->gen
>= 7);
2809 nir_const_value
*const_uniform_block
=
2810 nir_src_as_const_value(instr
->src
[1]);
2811 if (const_uniform_block
) {
2812 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
2813 const_uniform_block
->u
[0];
2814 surf_index
= brw_imm_ud(index
);
2815 brw_mark_surface_used(prog_data
, index
);
2817 surf_index
= vgrf(glsl_type::uint_type
);
2818 bld
.ADD(surf_index
, get_nir_src(instr
->src
[1]),
2819 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2821 brw_mark_surface_used(prog_data
,
2822 stage_prog_data
->binding_table
.ssbo_start
+
2823 nir
->info
.num_ssbos
- 1);
2827 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
2830 unsigned writemask
= instr
->const_index
[0];
2832 /* Combine groups of consecutive enabled channels in one write
2833 * message. We use ffs to find the first enabled channel and then ffs on
2834 * the bit-inverse, down-shifted writemask to determine the length of
2835 * the block of enabled bits.
2838 unsigned first_component
= ffs(writemask
) - 1;
2839 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
2842 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[2]);
2844 offset_reg
= brw_imm_ud(const_offset
->u
[0] + 4 * first_component
);
2846 offset_reg
= vgrf(glsl_type::uint_type
);
2848 retype(get_nir_src(instr
->src
[2]), BRW_REGISTER_TYPE_UD
),
2849 brw_imm_ud(4 * first_component
));
2852 emit_untyped_write(bld
, surf_index
, offset_reg
,
2853 offset(val_reg
, bld
, first_component
),
2854 1 /* dims */, length
,
2855 BRW_PREDICATE_NONE
);
2857 /* Clear the bits in the writemask that we just wrote, then try
2858 * again to see if more channels are left.
2860 writemask
&= (15 << (first_component
+ length
));
2865 case nir_intrinsic_store_output
: {
2866 fs_reg src
= get_nir_src(instr
->src
[0]);
2867 fs_reg new_dest
= offset(retype(nir_outputs
, src
.type
), bld
,
2868 instr
->const_index
[0]);
2870 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2871 assert(const_offset
&& "Indirect output stores not allowed");
2872 new_dest
= offset(new_dest
, bld
, const_offset
->u
[0]);
2874 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2875 bld
.MOV(offset(new_dest
, bld
, j
), offset(src
, bld
, j
));
2880 case nir_intrinsic_ssbo_atomic_add
:
2881 nir_emit_ssbo_atomic(bld
, BRW_AOP_ADD
, instr
);
2883 case nir_intrinsic_ssbo_atomic_imin
:
2884 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMIN
, instr
);
2886 case nir_intrinsic_ssbo_atomic_umin
:
2887 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMIN
, instr
);
2889 case nir_intrinsic_ssbo_atomic_imax
:
2890 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMAX
, instr
);
2892 case nir_intrinsic_ssbo_atomic_umax
:
2893 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMAX
, instr
);
2895 case nir_intrinsic_ssbo_atomic_and
:
2896 nir_emit_ssbo_atomic(bld
, BRW_AOP_AND
, instr
);
2898 case nir_intrinsic_ssbo_atomic_or
:
2899 nir_emit_ssbo_atomic(bld
, BRW_AOP_OR
, instr
);
2901 case nir_intrinsic_ssbo_atomic_xor
:
2902 nir_emit_ssbo_atomic(bld
, BRW_AOP_XOR
, instr
);
2904 case nir_intrinsic_ssbo_atomic_exchange
:
2905 nir_emit_ssbo_atomic(bld
, BRW_AOP_MOV
, instr
);
2907 case nir_intrinsic_ssbo_atomic_comp_swap
:
2908 nir_emit_ssbo_atomic(bld
, BRW_AOP_CMPWR
, instr
);
2911 case nir_intrinsic_get_buffer_size
: {
2912 nir_const_value
*const_uniform_block
= nir_src_as_const_value(instr
->src
[0]);
2913 unsigned ssbo_index
= const_uniform_block
? const_uniform_block
->u
[0] : 0;
2914 int reg_width
= dispatch_width
/ 8;
2917 fs_reg source
= brw_imm_d(0);
2919 int mlen
= 1 * reg_width
;
2921 /* A resinfo's sampler message is used to get the buffer size.
2922 * The SIMD8's writeback message consists of four registers and
2923 * SIMD16's writeback message consists of 8 destination registers
2924 * (two per each component), although we are only interested on the
2925 * first component, where resinfo returns the buffer size for
2928 int regs_written
= 4 * mlen
;
2929 fs_reg src_payload
= fs_reg(VGRF
, alloc
.allocate(mlen
),
2930 BRW_REGISTER_TYPE_UD
);
2931 bld
.LOAD_PAYLOAD(src_payload
, &source
, 1, 0);
2932 fs_reg buffer_size
= fs_reg(VGRF
, alloc
.allocate(regs_written
),
2933 BRW_REGISTER_TYPE_UD
);
2934 const unsigned index
= prog_data
->binding_table
.ssbo_start
+ ssbo_index
;
2935 fs_inst
*inst
= bld
.emit(FS_OPCODE_GET_BUFFER_SIZE
, buffer_size
,
2936 src_payload
, brw_imm_ud(index
));
2937 inst
->header_size
= 0;
2939 inst
->regs_written
= regs_written
;
2941 bld
.MOV(retype(dest
, buffer_size
.type
), buffer_size
);
2943 brw_mark_surface_used(prog_data
, index
);
2948 unreachable("unknown intrinsic");
2953 fs_visitor::nir_emit_ssbo_atomic(const fs_builder
&bld
,
2954 int op
, nir_intrinsic_instr
*instr
)
2957 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2958 dest
= get_nir_dest(instr
->dest
);
2961 nir_const_value
*const_surface
= nir_src_as_const_value(instr
->src
[0]);
2962 if (const_surface
) {
2963 unsigned surf_index
= stage_prog_data
->binding_table
.ssbo_start
+
2964 const_surface
->u
[0];
2965 surface
= brw_imm_ud(surf_index
);
2966 brw_mark_surface_used(prog_data
, surf_index
);
2968 surface
= vgrf(glsl_type::uint_type
);
2969 bld
.ADD(surface
, get_nir_src(instr
->src
[0]),
2970 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2972 /* Assume this may touch any SSBO. This is the same we do for other
2973 * UBO/SSBO accesses with non-constant surface.
2975 brw_mark_surface_used(prog_data
,
2976 stage_prog_data
->binding_table
.ssbo_start
+
2977 nir
->info
.num_ssbos
- 1);
2980 fs_reg offset
= get_nir_src(instr
->src
[1]);
2981 fs_reg data1
= get_nir_src(instr
->src
[2]);
2983 if (op
== BRW_AOP_CMPWR
)
2984 data2
= get_nir_src(instr
->src
[3]);
2986 /* Emit the actual atomic operation operation */
2988 fs_reg atomic_result
=
2989 surface_access::emit_untyped_atomic(bld
, surface
, offset
,
2991 1 /* dims */, 1 /* rsize */,
2993 BRW_PREDICATE_NONE
);
2994 dest
.type
= atomic_result
.type
;
2995 bld
.MOV(dest
, atomic_result
);
2999 fs_visitor::nir_emit_shared_atomic(const fs_builder
&bld
,
3000 int op
, nir_intrinsic_instr
*instr
)
3003 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3004 dest
= get_nir_dest(instr
->dest
);
3006 fs_reg surface
= brw_imm_ud(GEN7_BTI_SLM
);
3007 fs_reg offset
= get_nir_src(instr
->src
[0]);
3008 fs_reg data1
= get_nir_src(instr
->src
[1]);
3010 if (op
== BRW_AOP_CMPWR
)
3011 data2
= get_nir_src(instr
->src
[2]);
3013 /* Emit the actual atomic operation operation */
3015 fs_reg atomic_result
=
3016 surface_access::emit_untyped_atomic(bld
, surface
, offset
,
3018 1 /* dims */, 1 /* rsize */,
3020 BRW_PREDICATE_NONE
);
3021 dest
.type
= atomic_result
.type
;
3022 bld
.MOV(dest
, atomic_result
);
3026 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
3028 unsigned texture
= instr
->texture_index
;
3029 unsigned sampler
= instr
->sampler_index
;
3030 fs_reg
texture_reg(brw_imm_ud(texture
));
3031 fs_reg
sampler_reg(brw_imm_ud(sampler
));
3033 int gather_component
= instr
->component
;
3035 bool is_cube_array
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3038 int lod_components
= 0;
3040 fs_reg coordinate
, shadow_comparitor
, lod
, lod2
, sample_index
, mcs
, tex_offset
;
3042 /* Our hardware requires a LOD for buffer textures */
3043 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3046 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3047 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
3048 switch (instr
->src
[i
].src_type
) {
3049 case nir_tex_src_bias
:
3050 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3052 case nir_tex_src_comparitor
:
3053 shadow_comparitor
= retype(src
, BRW_REGISTER_TYPE_F
);
3055 case nir_tex_src_coord
:
3056 switch (instr
->op
) {
3058 case nir_texop_txf_ms
:
3059 case nir_texop_samples_identical
:
3060 coordinate
= retype(src
, BRW_REGISTER_TYPE_D
);
3063 coordinate
= retype(src
, BRW_REGISTER_TYPE_F
);
3067 case nir_tex_src_ddx
:
3068 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3069 lod_components
= nir_tex_instr_src_size(instr
, i
);
3071 case nir_tex_src_ddy
:
3072 lod2
= retype(src
, BRW_REGISTER_TYPE_F
);
3074 case nir_tex_src_lod
:
3075 switch (instr
->op
) {
3077 lod
= retype(src
, BRW_REGISTER_TYPE_UD
);
3080 lod
= retype(src
, BRW_REGISTER_TYPE_D
);
3083 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3087 case nir_tex_src_ms_index
:
3088 sample_index
= retype(src
, BRW_REGISTER_TYPE_UD
);
3091 case nir_tex_src_offset
: {
3092 nir_const_value
*const_offset
=
3093 nir_src_as_const_value(instr
->src
[i
].src
);
3095 tex_offset
= brw_imm_ud(brw_texture_offset(const_offset
->i
, 3));
3097 tex_offset
= retype(src
, BRW_REGISTER_TYPE_D
);
3102 case nir_tex_src_projector
:
3103 unreachable("should be lowered");
3105 case nir_tex_src_texture_offset
: {
3106 /* Figure out the highest possible texture index and mark it as used */
3107 uint32_t max_used
= texture
+ instr
->texture_array_size
- 1;
3108 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
3109 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
3111 max_used
+= stage_prog_data
->binding_table
.texture_start
;
3113 brw_mark_surface_used(prog_data
, max_used
);
3115 /* Emit code to evaluate the actual indexing expression */
3116 texture_reg
= vgrf(glsl_type::uint_type
);
3117 bld
.ADD(texture_reg
, src
, brw_imm_ud(texture
));
3118 texture_reg
= bld
.emit_uniformize(texture_reg
);
3122 case nir_tex_src_sampler_offset
: {
3123 /* Emit code to evaluate the actual indexing expression */
3124 sampler_reg
= vgrf(glsl_type::uint_type
);
3125 bld
.ADD(sampler_reg
, src
, brw_imm_ud(sampler
));
3126 sampler_reg
= bld
.emit_uniformize(sampler_reg
);
3131 unreachable("unknown texture source");
3135 if (instr
->op
== nir_texop_txf_ms
||
3136 instr
->op
== nir_texop_samples_identical
) {
3137 if (devinfo
->gen
>= 7 &&
3138 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
3139 mcs
= emit_mcs_fetch(coordinate
, instr
->coord_components
, texture_reg
);
3141 mcs
= brw_imm_ud(0u);
3145 enum glsl_base_type dest_base_type
=
3146 brw_glsl_base_type_for_nir_type (instr
->dest_type
);
3148 const glsl_type
*dest_type
=
3149 glsl_type::get_instance(dest_base_type
, nir_tex_instr_dest_size(instr
),
3152 ir_texture_opcode op
;
3153 switch (instr
->op
) {
3154 case nir_texop_lod
: op
= ir_lod
; break;
3155 case nir_texop_query_levels
: op
= ir_query_levels
; break;
3156 case nir_texop_tex
: op
= ir_tex
; break;
3157 case nir_texop_tg4
: op
= ir_tg4
; break;
3158 case nir_texop_txb
: op
= ir_txb
; break;
3159 case nir_texop_txd
: op
= ir_txd
; break;
3160 case nir_texop_txf
: op
= ir_txf
; break;
3161 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
3162 case nir_texop_txl
: op
= ir_txl
; break;
3163 case nir_texop_txs
: op
= ir_txs
; break;
3164 case nir_texop_texture_samples
: {
3165 fs_reg dst
= retype(get_nir_dest(instr
->dest
), BRW_REGISTER_TYPE_D
);
3166 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_SAMPLEINFO
, dst
,
3167 bld
.vgrf(BRW_REGISTER_TYPE_D
, 1),
3168 texture_reg
, texture_reg
);
3170 inst
->header_size
= 1;
3171 inst
->base_mrf
= -1;
3174 case nir_texop_samples_identical
: op
= ir_samples_identical
; break;
3176 unreachable("unknown texture opcode");
3179 emit_texture(op
, dest_type
, coordinate
, instr
->coord_components
,
3180 shadow_comparitor
, lod
, lod2
, lod_components
, sample_index
,
3181 tex_offset
, mcs
, gather_component
, is_cube_array
,
3182 texture
, texture_reg
, sampler
, sampler_reg
);
3184 fs_reg dest
= get_nir_dest(instr
->dest
);
3185 dest
.type
= this->result
.type
;
3186 unsigned num_components
= nir_tex_instr_dest_size(instr
);
3187 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
3188 dest
, this->result
),
3189 (1 << num_components
) - 1);
3193 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
3195 switch (instr
->type
) {
3196 case nir_jump_break
:
3197 bld
.emit(BRW_OPCODE_BREAK
);
3199 case nir_jump_continue
:
3200 bld
.emit(BRW_OPCODE_CONTINUE
);
3202 case nir_jump_return
:
3204 unreachable("unknown jump");