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;
183 nir_foreach_variable(var
, &nir
->uniforms
) {
184 /* UBO's and atomics don't take up space in the uniform file */
185 if (var
->interface_type
!= NULL
|| var
->type
->contains_atomic())
188 if (type_size_scalar(var
->type
) > 0)
189 param_size
[var
->data
.driver_location
/ 4] = type_size_scalar(var
->type
);
194 emit_system_values_block(nir_block
*block
, void *void_visitor
)
196 fs_visitor
*v
= (fs_visitor
*)void_visitor
;
199 nir_foreach_instr(block
, instr
) {
200 if (instr
->type
!= nir_instr_type_intrinsic
)
203 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
204 switch (intrin
->intrinsic
) {
205 case nir_intrinsic_load_vertex_id
:
206 unreachable("should be lowered by lower_vertex_id().");
208 case nir_intrinsic_load_vertex_id_zero_base
:
209 assert(v
->stage
== MESA_SHADER_VERTEX
);
210 reg
= &v
->nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
211 if (reg
->file
== BAD_FILE
)
212 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
);
215 case nir_intrinsic_load_base_vertex
:
216 assert(v
->stage
== MESA_SHADER_VERTEX
);
217 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
218 if (reg
->file
== BAD_FILE
)
219 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX
);
222 case nir_intrinsic_load_instance_id
:
223 assert(v
->stage
== MESA_SHADER_VERTEX
);
224 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
225 if (reg
->file
== BAD_FILE
)
226 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID
);
229 case nir_intrinsic_load_base_instance
:
230 assert(v
->stage
== MESA_SHADER_VERTEX
);
231 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_INSTANCE
];
232 if (reg
->file
== BAD_FILE
)
233 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE
);
236 case nir_intrinsic_load_draw_id
:
237 assert(v
->stage
== MESA_SHADER_VERTEX
);
238 reg
= &v
->nir_system_values
[SYSTEM_VALUE_DRAW_ID
];
239 if (reg
->file
== BAD_FILE
)
240 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID
);
243 case nir_intrinsic_load_invocation_id
:
244 assert(v
->stage
== MESA_SHADER_GEOMETRY
);
245 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
246 if (reg
->file
== BAD_FILE
) {
247 const fs_builder abld
= v
->bld
.annotate("gl_InvocationID", NULL
);
248 fs_reg
g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
249 fs_reg iid
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
250 abld
.SHR(iid
, g1
, brw_imm_ud(27u));
255 case nir_intrinsic_load_sample_pos
:
256 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
257 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
258 if (reg
->file
== BAD_FILE
)
259 *reg
= *v
->emit_samplepos_setup();
262 case nir_intrinsic_load_sample_id
:
263 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
264 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
265 if (reg
->file
== BAD_FILE
)
266 *reg
= *v
->emit_sampleid_setup();
269 case nir_intrinsic_load_sample_mask_in
:
270 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
271 assert(v
->devinfo
->gen
>= 7);
272 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
273 if (reg
->file
== BAD_FILE
)
274 *reg
= fs_reg(retype(brw_vec8_grf(v
->payload
.sample_mask_in_reg
, 0),
275 BRW_REGISTER_TYPE_D
));
278 case nir_intrinsic_load_local_invocation_id
:
279 assert(v
->stage
== MESA_SHADER_COMPUTE
);
280 reg
= &v
->nir_system_values
[SYSTEM_VALUE_LOCAL_INVOCATION_ID
];
281 if (reg
->file
== BAD_FILE
)
282 *reg
= *v
->emit_cs_local_invocation_id_setup();
285 case nir_intrinsic_load_work_group_id
:
286 assert(v
->stage
== MESA_SHADER_COMPUTE
);
287 reg
= &v
->nir_system_values
[SYSTEM_VALUE_WORK_GROUP_ID
];
288 if (reg
->file
== BAD_FILE
)
289 *reg
= *v
->emit_cs_work_group_id_setup();
292 case nir_intrinsic_load_helper_invocation
:
293 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
294 reg
= &v
->nir_system_values
[SYSTEM_VALUE_HELPER_INVOCATION
];
295 if (reg
->file
== BAD_FILE
) {
296 const fs_builder abld
=
297 v
->bld
.annotate("gl_HelperInvocation", NULL
);
299 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
300 * pixel mask is in g1.7 of the thread payload.
302 * We move the per-channel pixel enable bit to the low bit of each
303 * channel by shifting the byte containing the pixel mask by the
304 * vector immediate 0x76543210UV.
306 * The region of <1,8,0> reads only 1 byte (the pixel masks for
307 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
308 * masks for 2 and 3) in SIMD16.
310 fs_reg shifted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
312 stride(byte_offset(retype(brw_vec1_grf(1, 0),
313 BRW_REGISTER_TYPE_UB
), 28),
315 brw_imm_uv(0x76543210));
317 /* A set bit in the pixel mask means the channel is enabled, but
318 * that is the opposite of gl_HelperInvocation so we need to invert
321 * The negate source-modifier bit of logical instructions on Gen8+
322 * performs 1's complement negation, so we can use that instead of
325 fs_reg inverted
= negate(shifted
);
326 if (v
->devinfo
->gen
< 8) {
327 inverted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
328 abld
.NOT(inverted
, shifted
);
331 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
332 * with 1 and negating.
334 fs_reg anded
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
335 abld
.AND(anded
, inverted
, brw_imm_uw(1));
337 fs_reg dst
= abld
.vgrf(BRW_REGISTER_TYPE_D
, 1);
338 abld
.MOV(dst
, negate(retype(anded
, BRW_REGISTER_TYPE_D
)));
352 fs_visitor::nir_emit_system_values()
354 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
355 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
356 nir_system_values
[i
] = fs_reg();
359 nir_foreach_function(nir
, function
) {
360 assert(strcmp(function
->name
, "main") == 0);
361 assert(function
->impl
);
362 nir_foreach_block(function
->impl
, emit_system_values_block
, this);
367 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
369 nir_locals
= ralloc_array(mem_ctx
, fs_reg
, impl
->reg_alloc
);
370 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
371 nir_locals
[i
] = fs_reg();
374 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
375 unsigned array_elems
=
376 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
377 unsigned size
= array_elems
* reg
->num_components
;
378 nir_locals
[reg
->index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
381 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
384 nir_emit_cf_list(&impl
->body
);
388 fs_visitor::nir_emit_cf_list(exec_list
*list
)
390 exec_list_validate(list
);
391 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
392 switch (node
->type
) {
394 nir_emit_if(nir_cf_node_as_if(node
));
397 case nir_cf_node_loop
:
398 nir_emit_loop(nir_cf_node_as_loop(node
));
401 case nir_cf_node_block
:
402 nir_emit_block(nir_cf_node_as_block(node
));
406 unreachable("Invalid CFG node block");
412 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
414 /* first, put the condition into f0 */
415 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
416 retype(get_nir_src(if_stmt
->condition
),
417 BRW_REGISTER_TYPE_D
));
418 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
420 bld
.IF(BRW_PREDICATE_NORMAL
);
422 nir_emit_cf_list(&if_stmt
->then_list
);
424 /* note: if the else is empty, dead CF elimination will remove it */
425 bld
.emit(BRW_OPCODE_ELSE
);
427 nir_emit_cf_list(&if_stmt
->else_list
);
429 bld
.emit(BRW_OPCODE_ENDIF
);
433 fs_visitor::nir_emit_loop(nir_loop
*loop
)
435 bld
.emit(BRW_OPCODE_DO
);
437 nir_emit_cf_list(&loop
->body
);
439 bld
.emit(BRW_OPCODE_WHILE
);
443 fs_visitor::nir_emit_block(nir_block
*block
)
445 nir_foreach_instr(block
, instr
) {
446 nir_emit_instr(instr
);
451 fs_visitor::nir_emit_instr(nir_instr
*instr
)
453 const fs_builder abld
= bld
.annotate(NULL
, instr
);
455 switch (instr
->type
) {
456 case nir_instr_type_alu
:
457 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
460 case nir_instr_type_intrinsic
:
462 case MESA_SHADER_VERTEX
:
463 nir_emit_vs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
465 case MESA_SHADER_TESS_EVAL
:
466 nir_emit_tes_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
468 case MESA_SHADER_GEOMETRY
:
469 nir_emit_gs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
471 case MESA_SHADER_FRAGMENT
:
472 nir_emit_fs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
474 case MESA_SHADER_COMPUTE
:
475 nir_emit_cs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
478 unreachable("unsupported shader stage");
482 case nir_instr_type_tex
:
483 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
486 case nir_instr_type_load_const
:
487 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
490 case nir_instr_type_ssa_undef
:
491 nir_emit_undef(abld
, nir_instr_as_ssa_undef(instr
));
494 case nir_instr_type_jump
:
495 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
499 unreachable("unknown instruction type");
504 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
508 fs_visitor::optimize_extract_to_float(nir_alu_instr
*instr
,
509 const fs_reg
&result
)
511 if (!instr
->src
[0].src
.is_ssa
||
512 !instr
->src
[0].src
.ssa
->parent_instr
)
515 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
518 nir_alu_instr
*src0
=
519 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
521 if (src0
->op
!= nir_op_extract_u8
&& src0
->op
!= nir_op_extract_u16
&&
522 src0
->op
!= nir_op_extract_i8
&& src0
->op
!= nir_op_extract_i16
)
525 nir_const_value
*element
= nir_src_as_const_value(src0
->src
[1].src
);
526 assert(element
!= NULL
);
528 enum opcode extract_op
;
529 if (src0
->op
== nir_op_extract_u16
|| src0
->op
== nir_op_extract_i16
) {
530 assert(element
->u32
[0] <= 1);
531 extract_op
= SHADER_OPCODE_EXTRACT_WORD
;
533 assert(element
->u32
[0] <= 3);
534 extract_op
= SHADER_OPCODE_EXTRACT_BYTE
;
537 fs_reg op0
= get_nir_src(src0
->src
[0].src
);
538 op0
.type
= brw_type_for_nir_type(nir_op_infos
[src0
->op
].input_types
[0]);
539 op0
= offset(op0
, bld
, src0
->src
[0].swizzle
[0]);
541 set_saturate(instr
->dest
.saturate
,
542 bld
.emit(extract_op
, result
, op0
, brw_imm_ud(element
->u32
[0])));
547 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
548 const fs_reg
&result
)
550 if (!instr
->src
[0].src
.is_ssa
||
551 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
554 nir_intrinsic_instr
*src0
=
555 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
557 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
560 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
561 if (!value1
|| fabsf(value1
->f32
[0]) != 1.0f
)
564 nir_const_value
*value2
= nir_src_as_const_value(instr
->src
[2].src
);
565 if (!value2
|| fabsf(value2
->f32
[0]) != 1.0f
)
568 fs_reg tmp
= vgrf(glsl_type::int_type
);
570 if (devinfo
->gen
>= 6) {
571 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
572 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
574 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
576 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
577 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
579 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
581 * This negation looks like it's safe in practice, because bits 0:4 will
582 * surely be TRIANGLES
585 if (value1
->f32
[0] == -1.0f
) {
589 tmp
.type
= BRW_REGISTER_TYPE_W
;
590 tmp
.subreg_offset
= 2;
593 bld
.OR(tmp
, g0
, brw_imm_uw(0x3f80));
595 tmp
.type
= BRW_REGISTER_TYPE_D
;
596 tmp
.subreg_offset
= 0;
599 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
600 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
602 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
604 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
605 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
607 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
609 * This negation looks like it's safe in practice, because bits 0:4 will
610 * surely be TRIANGLES
613 if (value1
->f32
[0] == -1.0f
) {
617 bld
.OR(tmp
, g1_6
, brw_imm_d(0x3f800000));
619 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, brw_imm_d(0xbf800000));
625 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
627 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
630 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
631 result
.type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].output_type
);
634 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
635 op
[i
] = get_nir_src(instr
->src
[i
].src
);
636 op
[i
].type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].input_types
[i
]);
637 op
[i
].abs
= instr
->src
[i
].abs
;
638 op
[i
].negate
= instr
->src
[i
].negate
;
641 /* We get a bunch of mov's out of the from_ssa pass and they may still
642 * be vectorized. We'll handle them as a special-case. We'll also
643 * handle vecN here because it's basically the same thing.
651 fs_reg temp
= result
;
652 bool need_extra_copy
= false;
653 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
654 if (!instr
->src
[i
].src
.is_ssa
&&
655 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
656 need_extra_copy
= true;
657 temp
= bld
.vgrf(result
.type
, 4);
662 for (unsigned i
= 0; i
< 4; i
++) {
663 if (!(instr
->dest
.write_mask
& (1 << i
)))
666 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
667 inst
= bld
.MOV(offset(temp
, bld
, i
),
668 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
670 inst
= bld
.MOV(offset(temp
, bld
, i
),
671 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
673 inst
->saturate
= instr
->dest
.saturate
;
676 /* In this case the source and destination registers were the same,
677 * so we need to insert an extra set of moves in order to deal with
680 if (need_extra_copy
) {
681 for (unsigned i
= 0; i
< 4; i
++) {
682 if (!(instr
->dest
.write_mask
& (1 << i
)))
685 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
694 /* At this point, we have dealt with any instruction that operates on
695 * more than a single channel. Therefore, we can just adjust the source
696 * and destination registers for that channel and emit the instruction.
698 unsigned channel
= 0;
699 if (nir_op_infos
[instr
->op
].output_size
== 0) {
700 /* Since NIR is doing the scalarizing for us, we should only ever see
701 * vectorized operations with a single channel.
703 assert(_mesa_bitcount(instr
->dest
.write_mask
) == 1);
704 channel
= ffs(instr
->dest
.write_mask
) - 1;
706 result
= offset(result
, bld
, channel
);
709 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
710 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
711 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
717 if (optimize_extract_to_float(instr
, result
))
720 inst
= bld
.MOV(result
, op
[0]);
721 inst
->saturate
= instr
->dest
.saturate
;
726 bld
.MOV(result
, op
[0]);
730 /* AND(val, 0x80000000) gives the sign bit.
732 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
735 bld
.CMP(bld
.null_reg_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
737 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
738 op
[0].type
= BRW_REGISTER_TYPE_UD
;
739 result
.type
= BRW_REGISTER_TYPE_UD
;
740 bld
.AND(result_int
, op
[0], brw_imm_ud(0x80000000u
));
742 inst
= bld
.OR(result_int
, result_int
, brw_imm_ud(0x3f800000u
));
743 inst
->predicate
= BRW_PREDICATE_NORMAL
;
744 if (instr
->dest
.saturate
) {
745 inst
= bld
.MOV(result
, result
);
746 inst
->saturate
= true;
752 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
753 * -> non-negative val generates 0x00000000.
754 * Predicated OR sets 1 if val is positive.
756 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_G
);
757 bld
.ASR(result
, op
[0], brw_imm_d(31));
758 inst
= bld
.OR(result
, result
, brw_imm_d(1));
759 inst
->predicate
= BRW_PREDICATE_NORMAL
;
763 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
764 inst
->saturate
= instr
->dest
.saturate
;
768 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
769 inst
->saturate
= instr
->dest
.saturate
;
773 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
774 inst
->saturate
= instr
->dest
.saturate
;
778 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
779 inst
->saturate
= instr
->dest
.saturate
;
783 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
784 inst
->saturate
= instr
->dest
.saturate
;
788 if (fs_key
->high_quality_derivatives
) {
789 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
791 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
793 inst
->saturate
= instr
->dest
.saturate
;
795 case nir_op_fddx_fine
:
796 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
797 inst
->saturate
= instr
->dest
.saturate
;
799 case nir_op_fddx_coarse
:
800 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
801 inst
->saturate
= instr
->dest
.saturate
;
804 if (fs_key
->high_quality_derivatives
) {
805 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
806 brw_imm_d(fs_key
->render_to_fbo
));
808 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
809 brw_imm_d(fs_key
->render_to_fbo
));
811 inst
->saturate
= instr
->dest
.saturate
;
813 case nir_op_fddy_fine
:
814 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
815 brw_imm_d(fs_key
->render_to_fbo
));
816 inst
->saturate
= instr
->dest
.saturate
;
818 case nir_op_fddy_coarse
:
819 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
820 brw_imm_d(fs_key
->render_to_fbo
));
821 inst
->saturate
= instr
->dest
.saturate
;
826 inst
= bld
.ADD(result
, op
[0], op
[1]);
827 inst
->saturate
= instr
->dest
.saturate
;
831 inst
= bld
.MUL(result
, op
[0], op
[1]);
832 inst
->saturate
= instr
->dest
.saturate
;
836 bld
.MUL(result
, op
[0], op
[1]);
839 case nir_op_imul_high
:
840 case nir_op_umul_high
:
841 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
846 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
849 case nir_op_uadd_carry
:
850 unreachable("Should have been lowered by carry_to_arith().");
852 case nir_op_usub_borrow
:
853 unreachable("Should have been lowered by borrow_to_arith().");
857 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
858 * appears that our hardware just does the right thing for signed
861 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
865 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
866 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
868 /* Math instructions don't support conditional mod */
869 inst
= bld
.MOV(bld
.null_reg_d(), result
);
870 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
872 /* Now, we need to determine if signs of the sources are different.
873 * When we XOR the sources, the top bit is 0 if they are the same and 1
874 * if they are different. We can then use a conditional modifier to
875 * turn that into a predicate. This leads us to an XOR.l instruction.
877 * Technically, according to the PRM, you're not allowed to use .l on a
878 * XOR instruction. However, emperical experiments and Curro's reading
879 * of the simulator source both indicate that it's safe.
881 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
882 inst
= bld
.XOR(tmp
, op
[0], op
[1]);
883 inst
->predicate
= BRW_PREDICATE_NORMAL
;
884 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
886 /* If the result of the initial remainder operation is non-zero and the
887 * two sources have different signs, add in a copy of op[1] to get the
888 * final integer modulus value.
890 inst
= bld
.ADD(result
, result
, op
[1]);
891 inst
->predicate
= BRW_PREDICATE_NORMAL
;
898 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
904 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
909 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_Z
);
914 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_NZ
);
918 if (devinfo
->gen
>= 8) {
919 op
[0] = resolve_source_modifiers(op
[0]);
921 bld
.NOT(result
, op
[0]);
924 if (devinfo
->gen
>= 8) {
925 op
[0] = resolve_source_modifiers(op
[0]);
926 op
[1] = resolve_source_modifiers(op
[1]);
928 bld
.XOR(result
, op
[0], op
[1]);
931 if (devinfo
->gen
>= 8) {
932 op
[0] = resolve_source_modifiers(op
[0]);
933 op
[1] = resolve_source_modifiers(op
[1]);
935 bld
.OR(result
, op
[0], op
[1]);
938 if (devinfo
->gen
>= 8) {
939 op
[0] = resolve_source_modifiers(op
[0]);
940 op
[1] = resolve_source_modifiers(op
[1]);
942 bld
.AND(result
, op
[0], op
[1]);
948 case nir_op_ball_fequal2
:
949 case nir_op_ball_iequal2
:
950 case nir_op_ball_fequal3
:
951 case nir_op_ball_iequal3
:
952 case nir_op_ball_fequal4
:
953 case nir_op_ball_iequal4
:
954 case nir_op_bany_fnequal2
:
955 case nir_op_bany_inequal2
:
956 case nir_op_bany_fnequal3
:
957 case nir_op_bany_inequal3
:
958 case nir_op_bany_fnequal4
:
959 case nir_op_bany_inequal4
:
960 unreachable("Lowered by nir_lower_alu_reductions");
962 case nir_op_fnoise1_1
:
963 case nir_op_fnoise1_2
:
964 case nir_op_fnoise1_3
:
965 case nir_op_fnoise1_4
:
966 case nir_op_fnoise2_1
:
967 case nir_op_fnoise2_2
:
968 case nir_op_fnoise2_3
:
969 case nir_op_fnoise2_4
:
970 case nir_op_fnoise3_1
:
971 case nir_op_fnoise3_2
:
972 case nir_op_fnoise3_3
:
973 case nir_op_fnoise3_4
:
974 case nir_op_fnoise4_1
:
975 case nir_op_fnoise4_2
:
976 case nir_op_fnoise4_3
:
977 case nir_op_fnoise4_4
:
978 unreachable("not reached: should be handled by lower_noise");
981 unreachable("not reached: should be handled by ldexp_to_arith()");
984 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
985 inst
->saturate
= instr
->dest
.saturate
;
989 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
990 inst
->saturate
= instr
->dest
.saturate
;
995 bld
.MOV(result
, negate(op
[0]));
999 bld
.CMP(result
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
1002 bld
.CMP(result
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1006 inst
= bld
.RNDZ(result
, op
[0]);
1007 inst
->saturate
= instr
->dest
.saturate
;
1010 case nir_op_fceil
: {
1011 op
[0].negate
= !op
[0].negate
;
1012 fs_reg temp
= vgrf(glsl_type::float_type
);
1013 bld
.RNDD(temp
, op
[0]);
1015 inst
= bld
.MOV(result
, temp
);
1016 inst
->saturate
= instr
->dest
.saturate
;
1020 inst
= bld
.RNDD(result
, op
[0]);
1021 inst
->saturate
= instr
->dest
.saturate
;
1024 inst
= bld
.FRC(result
, op
[0]);
1025 inst
->saturate
= instr
->dest
.saturate
;
1027 case nir_op_fround_even
:
1028 inst
= bld
.RNDE(result
, op
[0]);
1029 inst
->saturate
= instr
->dest
.saturate
;
1032 case nir_op_fquantize2f16
: {
1033 fs_reg tmp16
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1034 fs_reg tmp32
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1035 fs_reg zero
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1037 /* The destination stride must be at least as big as the source stride. */
1038 tmp16
.type
= BRW_REGISTER_TYPE_W
;
1041 /* Check for denormal */
1042 fs_reg abs_src0
= op
[0];
1043 abs_src0
.abs
= true;
1044 bld
.CMP(bld
.null_reg_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1046 /* Get the appropriately signed zero */
1047 bld
.AND(retype(zero
, BRW_REGISTER_TYPE_UD
),
1048 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1049 brw_imm_ud(0x80000000));
1050 /* Do the actual F32 -> F16 -> F32 conversion */
1051 bld
.emit(BRW_OPCODE_F32TO16
, tmp16
, op
[0]);
1052 bld
.emit(BRW_OPCODE_F16TO32
, tmp32
, tmp16
);
1053 /* Select that or zero based on normal status */
1054 inst
= bld
.SEL(result
, zero
, tmp32
);
1055 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1056 inst
->saturate
= instr
->dest
.saturate
;
1063 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
1064 inst
->saturate
= instr
->dest
.saturate
;
1070 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
1071 inst
->saturate
= instr
->dest
.saturate
;
1074 case nir_op_pack_snorm_2x16
:
1075 case nir_op_pack_snorm_4x8
:
1076 case nir_op_pack_unorm_2x16
:
1077 case nir_op_pack_unorm_4x8
:
1078 case nir_op_unpack_snorm_2x16
:
1079 case nir_op_unpack_snorm_4x8
:
1080 case nir_op_unpack_unorm_2x16
:
1081 case nir_op_unpack_unorm_4x8
:
1082 case nir_op_unpack_half_2x16
:
1083 case nir_op_pack_half_2x16
:
1084 unreachable("not reached: should be handled by lower_packing_builtins");
1086 case nir_op_unpack_half_2x16_split_x
:
1087 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
1088 inst
->saturate
= instr
->dest
.saturate
;
1090 case nir_op_unpack_half_2x16_split_y
:
1091 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
1092 inst
->saturate
= instr
->dest
.saturate
;
1096 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1097 inst
->saturate
= instr
->dest
.saturate
;
1100 case nir_op_bitfield_reverse
:
1101 bld
.BFREV(result
, op
[0]);
1104 case nir_op_bit_count
:
1105 bld
.CBIT(result
, op
[0]);
1108 case nir_op_ufind_msb
:
1109 case nir_op_ifind_msb
: {
1110 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1112 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1113 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1114 * subtract the result from 31 to convert the MSB count into an LSB count.
1116 bld
.CMP(bld
.null_reg_d(), result
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1118 inst
= bld
.ADD(result
, result
, brw_imm_d(31));
1119 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1120 inst
->src
[0].negate
= true;
1124 case nir_op_find_lsb
:
1125 bld
.FBL(result
, op
[0]);
1128 case nir_op_ubitfield_extract
:
1129 case nir_op_ibitfield_extract
:
1130 unreachable("should have been lowered");
1133 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1136 bld
.BFI1(result
, op
[0], op
[1]);
1139 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1142 case nir_op_bitfield_insert
:
1143 unreachable("not reached: should have been lowered");
1146 bld
.SHL(result
, op
[0], op
[1]);
1149 bld
.ASR(result
, op
[0], op
[1]);
1152 bld
.SHR(result
, op
[0], op
[1]);
1155 case nir_op_pack_half_2x16_split
:
1156 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1160 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1161 inst
->saturate
= instr
->dest
.saturate
;
1165 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1166 inst
->saturate
= instr
->dest
.saturate
;
1170 if (optimize_frontfacing_ternary(instr
, result
))
1173 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1174 inst
= bld
.SEL(result
, op
[1], op
[2]);
1175 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1178 case nir_op_extract_u8
:
1179 case nir_op_extract_i8
: {
1180 nir_const_value
*byte
= nir_src_as_const_value(instr
->src
[1].src
);
1181 bld
.emit(SHADER_OPCODE_EXTRACT_BYTE
,
1182 result
, op
[0], brw_imm_ud(byte
->u32
[0]));
1186 case nir_op_extract_u16
:
1187 case nir_op_extract_i16
: {
1188 nir_const_value
*word
= nir_src_as_const_value(instr
->src
[1].src
);
1189 bld
.emit(SHADER_OPCODE_EXTRACT_WORD
,
1190 result
, op
[0], brw_imm_ud(word
->u32
[0]));
1195 unreachable("unhandled instruction");
1198 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1199 * to sign extend the low bit to 0/~0
1201 if (devinfo
->gen
<= 5 &&
1202 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1203 fs_reg masked
= vgrf(glsl_type::int_type
);
1204 bld
.AND(masked
, result
, brw_imm_d(1));
1205 masked
.negate
= true;
1206 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1211 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1212 nir_load_const_instr
*instr
)
1214 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_D
, instr
->def
.num_components
);
1216 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1217 bld
.MOV(offset(reg
, bld
, i
), brw_imm_d(instr
->value
.i32
[i
]));
1219 nir_ssa_values
[instr
->def
.index
] = reg
;
1223 fs_visitor::nir_emit_undef(const fs_builder
&bld
, nir_ssa_undef_instr
*instr
)
1225 nir_ssa_values
[instr
->def
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_D
,
1226 instr
->def
.num_components
);
1230 fs_visitor::get_nir_src(nir_src src
)
1234 reg
= nir_ssa_values
[src
.ssa
->index
];
1236 /* We don't handle indirects on locals */
1237 assert(src
.reg
.indirect
== NULL
);
1238 reg
= offset(nir_locals
[src
.reg
.reg
->index
], bld
,
1239 src
.reg
.base_offset
* src
.reg
.reg
->num_components
);
1242 /* to avoid floating-point denorm flushing problems, set the type by
1243 * default to D - instructions that need floating point semantics will set
1244 * this to F if they need to
1246 return retype(reg
, BRW_REGISTER_TYPE_D
);
1250 fs_visitor::get_nir_dest(nir_dest dest
)
1253 nir_ssa_values
[dest
.ssa
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
,
1254 dest
.ssa
.num_components
);
1255 return nir_ssa_values
[dest
.ssa
.index
];
1257 /* We don't handle indirects on locals */
1258 assert(dest
.reg
.indirect
== NULL
);
1259 return offset(nir_locals
[dest
.reg
.reg
->index
], bld
,
1260 dest
.reg
.base_offset
* dest
.reg
.reg
->num_components
);
1265 fs_visitor::get_nir_image_deref(const nir_deref_var
*deref
)
1267 fs_reg
image(UNIFORM
, deref
->var
->data
.driver_location
/ 4,
1268 BRW_REGISTER_TYPE_UD
);
1270 for (const nir_deref
*tail
= &deref
->deref
; tail
->child
;
1271 tail
= tail
->child
) {
1272 const nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1273 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1274 const unsigned size
= glsl_get_length(tail
->type
);
1275 const unsigned element_size
= type_size_scalar(deref_array
->deref
.type
);
1276 const unsigned base
= MIN2(deref_array
->base_offset
, size
- 1);
1277 image
= offset(image
, bld
, base
* element_size
);
1279 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1280 fs_reg tmp
= vgrf(glsl_type::int_type
);
1282 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
) {
1283 /* IVB hangs when trying to access an invalid surface index with
1284 * the dataport. According to the spec "if the index used to
1285 * select an individual element is negative or greater than or
1286 * equal to the size of the array, the results of the operation
1287 * are undefined but may not lead to termination" -- which is one
1288 * of the possible outcomes of the hang. Clamp the index to
1289 * prevent access outside of the array bounds.
1291 bld
.emit_minmax(tmp
, retype(get_nir_src(deref_array
->indirect
),
1292 BRW_REGISTER_TYPE_UD
),
1293 brw_imm_ud(size
- base
- 1), BRW_CONDITIONAL_L
);
1295 bld
.MOV(tmp
, get_nir_src(deref_array
->indirect
));
1298 bld
.MUL(tmp
, tmp
, brw_imm_ud(element_size
* 4));
1300 bld
.ADD(*image
.reladdr
, *image
.reladdr
, tmp
);
1302 image
.reladdr
= new(mem_ctx
) fs_reg(tmp
);
1310 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1313 for (unsigned i
= 0; i
< 4; i
++) {
1314 if (!((wr_mask
>> i
) & 1))
1317 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1318 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1319 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1320 if (new_inst
->src
[j
].file
== VGRF
)
1321 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1328 * Get the matching channel register datatype for an image intrinsic of the
1329 * specified GLSL image type.
1332 get_image_base_type(const glsl_type
*type
)
1334 switch ((glsl_base_type
)type
->sampled_type
) {
1335 case GLSL_TYPE_UINT
:
1336 return BRW_REGISTER_TYPE_UD
;
1338 return BRW_REGISTER_TYPE_D
;
1339 case GLSL_TYPE_FLOAT
:
1340 return BRW_REGISTER_TYPE_F
;
1342 unreachable("Not reached.");
1347 * Get the appropriate atomic op for an image atomic intrinsic.
1350 get_image_atomic_op(nir_intrinsic_op op
, const glsl_type
*type
)
1353 case nir_intrinsic_image_atomic_add
:
1355 case nir_intrinsic_image_atomic_min
:
1356 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1357 BRW_AOP_IMIN
: BRW_AOP_UMIN
);
1358 case nir_intrinsic_image_atomic_max
:
1359 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1360 BRW_AOP_IMAX
: BRW_AOP_UMAX
);
1361 case nir_intrinsic_image_atomic_and
:
1363 case nir_intrinsic_image_atomic_or
:
1365 case nir_intrinsic_image_atomic_xor
:
1367 case nir_intrinsic_image_atomic_exchange
:
1369 case nir_intrinsic_image_atomic_comp_swap
:
1370 return BRW_AOP_CMPWR
;
1372 unreachable("Not reachable.");
1377 emit_pixel_interpolater_send(const fs_builder
&bld
,
1382 glsl_interp_qualifier interpolation
)
1388 if (src
.file
== BAD_FILE
) {
1390 payload
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 1);
1394 mlen
= 2 * bld
.dispatch_width() / 8;
1397 inst
= bld
.emit(opcode
, dst
, payload
, desc
);
1399 /* 2 floats per slot returned */
1400 inst
->regs_written
= 2 * bld
.dispatch_width() / 8;
1401 inst
->pi_noperspective
= interpolation
== INTERP_QUALIFIER_NOPERSPECTIVE
;
1407 * Computes 1 << x, given a D/UD register containing some value x.
1410 intexp2(const fs_builder
&bld
, const fs_reg
&x
)
1412 assert(x
.type
== BRW_REGISTER_TYPE_UD
|| x
.type
== BRW_REGISTER_TYPE_D
);
1414 fs_reg result
= bld
.vgrf(x
.type
, 1);
1415 fs_reg one
= bld
.vgrf(x
.type
, 1);
1417 bld
.MOV(one
, retype(brw_imm_d(1), one
.type
));
1418 bld
.SHL(result
, one
, x
);
1423 fs_visitor::emit_gs_end_primitive(const nir_src
&vertex_count_nir_src
)
1425 assert(stage
== MESA_SHADER_GEOMETRY
);
1427 struct brw_gs_prog_data
*gs_prog_data
=
1428 (struct brw_gs_prog_data
*) prog_data
;
1430 /* We can only do EndPrimitive() functionality when the control data
1431 * consists of cut bits. Fortunately, the only time it isn't is when the
1432 * output type is points, in which case EndPrimitive() is a no-op.
1434 if (gs_prog_data
->control_data_format
!=
1435 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
1439 /* Cut bits use one bit per vertex. */
1440 assert(gs_compile
->control_data_bits_per_vertex
== 1);
1442 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1443 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1445 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1446 * vertex n, 0 otherwise. So all we need to do here is mark bit
1447 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1448 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1449 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1451 * Note that if EndPrimitive() is called before emitting any vertices, this
1452 * will cause us to set bit 31 of the control_data_bits register to 1.
1453 * That's fine because:
1455 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1456 * output, so the hardware will ignore cut bit 31.
1458 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1459 * last vertex, so setting cut bit 31 has no effect (since the primitive
1460 * is automatically ended when the GS terminates).
1462 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1463 * control_data_bits register to 0 when the first vertex is emitted.
1466 const fs_builder abld
= bld
.annotate("end primitive");
1468 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1469 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1470 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1471 fs_reg mask
= intexp2(abld
, prev_count
);
1472 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1473 * attention to the lower 5 bits of its second source argument, so on this
1474 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1475 * ((vertex_count - 1) % 32).
1477 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1481 fs_visitor::emit_gs_control_data_bits(const fs_reg
&vertex_count
)
1483 assert(stage
== MESA_SHADER_GEOMETRY
);
1484 assert(gs_compile
->control_data_bits_per_vertex
!= 0);
1486 struct brw_gs_prog_data
*gs_prog_data
=
1487 (struct brw_gs_prog_data
*) prog_data
;
1489 const fs_builder abld
= bld
.annotate("emit control data bits");
1490 const fs_builder fwa_bld
= bld
.exec_all();
1492 /* We use a single UD register to accumulate control data bits (32 bits
1493 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1496 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1497 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1498 * use the Channel Mask phase to enable/disable which DWord within that
1499 * group to write. (Remember, different SIMD8 channels may have emitted
1500 * different numbers of vertices, so we may need per-slot offsets.)
1502 * Channel masking presents an annoying problem: we may have to replicate
1503 * the data up to 4 times:
1505 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1507 * To avoid penalizing shaders that emit a small number of vertices, we
1508 * can avoid these sometimes: if the size of the control data header is
1509 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1510 * land in the same 128-bit group, so we can skip per-slot offsets.
1512 * Similarly, if the control data header is <= 32 bits, there is only one
1513 * DWord, so we can skip channel masks.
1515 enum opcode opcode
= SHADER_OPCODE_URB_WRITE_SIMD8
;
1517 fs_reg channel_mask
, per_slot_offset
;
1519 if (gs_compile
->control_data_header_size_bits
> 32) {
1520 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
1521 channel_mask
= vgrf(glsl_type::uint_type
);
1524 if (gs_compile
->control_data_header_size_bits
> 128) {
1525 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
;
1526 per_slot_offset
= vgrf(glsl_type::uint_type
);
1529 /* Figure out which DWord we're trying to write to using the formula:
1531 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1533 * Since bits_per_vertex is a power of two, and is known at compile
1534 * time, this can be optimized to:
1536 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1538 if (opcode
!= SHADER_OPCODE_URB_WRITE_SIMD8
) {
1539 fs_reg dword_index
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1540 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1541 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1542 unsigned log2_bits_per_vertex
=
1543 _mesa_fls(gs_compile
->control_data_bits_per_vertex
);
1544 abld
.SHR(dword_index
, prev_count
, brw_imm_ud(6u - log2_bits_per_vertex
));
1546 if (per_slot_offset
.file
!= BAD_FILE
) {
1547 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1548 * the appropriate OWord within the control data header.
1550 abld
.SHR(per_slot_offset
, dword_index
, brw_imm_ud(2u));
1553 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1554 * write to the appropriate DWORD within the OWORD.
1556 fs_reg channel
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1557 fwa_bld
.AND(channel
, dword_index
, brw_imm_ud(3u));
1558 channel_mask
= intexp2(fwa_bld
, channel
);
1559 /* Then the channel masks need to be in bits 23:16. */
1560 fwa_bld
.SHL(channel_mask
, channel_mask
, brw_imm_ud(16u));
1563 /* Store the control data bits in the message payload and send it. */
1565 if (channel_mask
.file
!= BAD_FILE
)
1566 mlen
+= 4; /* channel masks, plus 3 extra copies of the data */
1567 if (per_slot_offset
.file
!= BAD_FILE
)
1570 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
1571 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, mlen
);
1573 sources
[i
++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1574 if (per_slot_offset
.file
!= BAD_FILE
)
1575 sources
[i
++] = per_slot_offset
;
1576 if (channel_mask
.file
!= BAD_FILE
)
1577 sources
[i
++] = channel_mask
;
1579 sources
[i
++] = this->control_data_bits
;
1582 abld
.LOAD_PAYLOAD(payload
, sources
, mlen
, mlen
);
1583 fs_inst
*inst
= abld
.emit(opcode
, reg_undef
, payload
);
1585 /* We need to increment Global Offset by 256-bits to make room for
1586 * Broadwell's extra "Vertex Count" payload at the beginning of the
1587 * URB entry. Since this is an OWord message, Global Offset is counted
1588 * in 128-bit units, so we must set it to 2.
1590 if (gs_prog_data
->static_vertex_count
== -1)
1595 fs_visitor::set_gs_stream_control_data_bits(const fs_reg
&vertex_count
,
1598 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1600 /* Note: we are calling this *before* increasing vertex_count, so
1601 * this->vertex_count == vertex_count - 1 in the formula above.
1604 /* Stream mode uses 2 bits per vertex */
1605 assert(gs_compile
->control_data_bits_per_vertex
== 2);
1607 /* Must be a valid stream */
1608 assert(stream_id
>= 0 && stream_id
< MAX_VERTEX_STREAMS
);
1610 /* Control data bits are initialized to 0 so we don't have to set any
1611 * bits when sending vertices to stream 0.
1616 const fs_builder abld
= bld
.annotate("set stream control data bits", NULL
);
1618 /* reg::sid = stream_id */
1619 fs_reg sid
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1620 abld
.MOV(sid
, brw_imm_ud(stream_id
));
1622 /* reg:shift_count = 2 * (vertex_count - 1) */
1623 fs_reg shift_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1624 abld
.SHL(shift_count
, vertex_count
, brw_imm_ud(1u));
1626 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1627 * attention to the lower 5 bits of its second source argument, so on this
1628 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1629 * stream_id << ((2 * (vertex_count - 1)) % 32).
1631 fs_reg mask
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1632 abld
.SHL(mask
, sid
, shift_count
);
1633 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1637 fs_visitor::emit_gs_vertex(const nir_src
&vertex_count_nir_src
,
1640 assert(stage
== MESA_SHADER_GEOMETRY
);
1642 struct brw_gs_prog_data
*gs_prog_data
=
1643 (struct brw_gs_prog_data
*) prog_data
;
1645 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1646 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1648 /* Haswell and later hardware ignores the "Render Stream Select" bits
1649 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1650 * and instead sends all primitives down the pipeline for rasterization.
1651 * If the SOL stage is enabled, "Render Stream Select" is honored and
1652 * primitives bound to non-zero streams are discarded after stream output.
1654 * Since the only purpose of primives sent to non-zero streams is to
1655 * be recorded by transform feedback, we can simply discard all geometry
1656 * bound to these streams when transform feedback is disabled.
1658 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
1661 /* If we're outputting 32 control data bits or less, then we can wait
1662 * until the shader is over to output them all. Otherwise we need to
1663 * output them as we go. Now is the time to do it, since we're about to
1664 * output the vertex_count'th vertex, so it's guaranteed that the
1665 * control data bits associated with the (vertex_count - 1)th vertex are
1668 if (gs_compile
->control_data_header_size_bits
> 32) {
1669 const fs_builder abld
=
1670 bld
.annotate("emit vertex: emit control data bits");
1672 /* Only emit control data bits if we've finished accumulating a batch
1673 * of 32 bits. This is the case when:
1675 * (vertex_count * bits_per_vertex) % 32 == 0
1677 * (in other words, when the last 5 bits of vertex_count *
1678 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1679 * integer n (which is always the case, since bits_per_vertex is
1680 * always 1 or 2), this is equivalent to requiring that the last 5-n
1681 * bits of vertex_count are 0:
1683 * vertex_count & (2^(5-n) - 1) == 0
1685 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1688 * vertex_count & (32 / bits_per_vertex - 1) == 0
1690 * TODO: If vertex_count is an immediate, we could do some of this math
1691 * at compile time...
1694 abld
.AND(bld
.null_reg_d(), vertex_count
,
1695 brw_imm_ud(32u / gs_compile
->control_data_bits_per_vertex
- 1u));
1696 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1698 abld
.IF(BRW_PREDICATE_NORMAL
);
1699 /* If vertex_count is 0, then no control data bits have been
1700 * accumulated yet, so we can skip emitting them.
1702 abld
.CMP(bld
.null_reg_d(), vertex_count
, brw_imm_ud(0u),
1703 BRW_CONDITIONAL_NEQ
);
1704 abld
.IF(BRW_PREDICATE_NORMAL
);
1705 emit_gs_control_data_bits(vertex_count
);
1706 abld
.emit(BRW_OPCODE_ENDIF
);
1708 /* Reset control_data_bits to 0 so we can start accumulating a new
1711 * Note: in the case where vertex_count == 0, this neutralizes the
1712 * effect of any call to EndPrimitive() that the shader may have
1713 * made before outputting its first vertex.
1715 inst
= abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
1716 inst
->force_writemask_all
= true;
1717 abld
.emit(BRW_OPCODE_ENDIF
);
1720 emit_urb_writes(vertex_count
);
1722 /* In stream mode we have to set control data bits for all vertices
1723 * unless we have disabled control data bits completely (which we do
1724 * do for GL_POINTS outputs that don't use streams).
1726 if (gs_compile
->control_data_header_size_bits
> 0 &&
1727 gs_prog_data
->control_data_format
==
1728 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
1729 set_gs_stream_control_data_bits(vertex_count
, stream_id
);
1734 fs_visitor::emit_gs_input_load(const fs_reg
&dst
,
1735 const nir_src
&vertex_src
,
1736 unsigned base_offset
,
1737 const nir_src
&offset_src
,
1738 unsigned num_components
)
1740 struct brw_gs_prog_data
*gs_prog_data
= (struct brw_gs_prog_data
*) prog_data
;
1742 nir_const_value
*vertex_const
= nir_src_as_const_value(vertex_src
);
1743 nir_const_value
*offset_const
= nir_src_as_const_value(offset_src
);
1744 const unsigned push_reg_count
= gs_prog_data
->base
.urb_read_length
* 8;
1746 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
1747 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
1748 * gl_PointSize is available as a GS input, however, so it must be that.
1750 const bool is_point_size
= (base_offset
== 0);
1752 if (offset_const
!= NULL
&& vertex_const
!= NULL
&&
1753 4 * (base_offset
+ offset_const
->u32
[0]) < push_reg_count
) {
1754 int imm_offset
= (base_offset
+ offset_const
->u32
[0]) * 4 +
1755 vertex_const
->u32
[0] * push_reg_count
;
1756 /* This input was pushed into registers. */
1757 if (is_point_size
) {
1758 /* gl_PointSize comes in .w */
1759 assert(imm_offset
== 0);
1760 bld
.MOV(dst
, fs_reg(ATTR
, imm_offset
+ 3, dst
.type
));
1762 for (unsigned i
= 0; i
< num_components
; i
++) {
1763 bld
.MOV(offset(dst
, bld
, i
),
1764 fs_reg(ATTR
, imm_offset
+ i
, dst
.type
));
1768 /* Resort to the pull model. Ensure the VUE handles are provided. */
1769 gs_prog_data
->base
.include_vue_handles
= true;
1771 unsigned first_icp_handle
= gs_prog_data
->include_primitive_id
? 3 : 2;
1775 /* The vertex index is constant; just select the proper URB handle. */
1777 retype(brw_vec8_grf(first_icp_handle
+ vertex_const
->i32
[0], 0),
1778 BRW_REGISTER_TYPE_UD
);
1780 /* The vertex index is non-constant. We need to use indirect
1781 * addressing to fetch the proper URB handle.
1783 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
1784 * indicating that channel <n> should read the handle from
1785 * DWord <n>. We convert that to bytes by multiplying by 4.
1787 * Next, we convert the vertex index to bytes by multiplying
1788 * by 32 (shifting by 5), and add the two together. This is
1789 * the final indirect byte offset.
1791 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_W
, 1);
1792 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1793 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1794 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1795 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1797 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
1798 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
1799 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
1800 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
1801 /* Convert vertex_index to bytes (multiply by 32) */
1802 bld
.SHL(vertex_offset_bytes
,
1803 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
1805 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
1807 /* Use first_icp_handle as the base offset. There is one register
1808 * of URB handles per vertex, so inform the register allocator that
1809 * we might read up to nir->info.gs.vertices_in registers.
1811 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
1812 fs_reg(brw_vec8_grf(first_icp_handle
, 0)),
1813 fs_reg(icp_offset_bytes
),
1814 brw_imm_ud(nir
->info
.gs
.vertices_in
* REG_SIZE
));
1819 /* Constant indexing - use global offset. */
1820 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
1821 inst
->offset
= base_offset
+ offset_const
->u32
[0];
1822 inst
->base_mrf
= -1;
1824 inst
->regs_written
= num_components
;
1826 /* Indirect indexing - use per-slot offsets as well. */
1827 const fs_reg srcs
[] = { icp_handle
, get_nir_src(offset_src
) };
1828 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1829 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
1831 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
, payload
);
1832 inst
->offset
= base_offset
;
1833 inst
->base_mrf
= -1;
1835 inst
->regs_written
= num_components
;
1838 if (is_point_size
) {
1839 /* Read the whole VUE header (because of alignment) and read .w. */
1840 fs_reg tmp
= bld
.vgrf(dst
.type
, 4);
1842 inst
->regs_written
= 4;
1843 bld
.MOV(dst
, offset(tmp
, bld
, 3));
1849 fs_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
1851 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
1852 nir_const_value
*const_value
= nir_src_as_const_value(*offset_src
);
1855 /* The only constant offset we should find is 0. brw_nir.c's
1856 * add_const_offset_to_base() will fold other constant offsets
1857 * into instr->const_index[0].
1859 assert(const_value
->u32
[0] == 0);
1863 return get_nir_src(*offset_src
);
1867 fs_visitor::nir_emit_vs_intrinsic(const fs_builder
&bld
,
1868 nir_intrinsic_instr
*instr
)
1870 assert(stage
== MESA_SHADER_VERTEX
);
1873 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1874 dest
= get_nir_dest(instr
->dest
);
1876 switch (instr
->intrinsic
) {
1877 case nir_intrinsic_load_vertex_id
:
1878 unreachable("should be lowered by lower_vertex_id()");
1880 case nir_intrinsic_load_vertex_id_zero_base
:
1881 case nir_intrinsic_load_base_vertex
:
1882 case nir_intrinsic_load_instance_id
:
1883 case nir_intrinsic_load_base_instance
:
1884 case nir_intrinsic_load_draw_id
: {
1885 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
1886 fs_reg val
= nir_system_values
[sv
];
1887 assert(val
.file
!= BAD_FILE
);
1888 dest
.type
= val
.type
;
1894 nir_emit_intrinsic(bld
, instr
);
1900 fs_visitor::nir_emit_tes_intrinsic(const fs_builder
&bld
,
1901 nir_intrinsic_instr
*instr
)
1903 assert(stage
== MESA_SHADER_TESS_EVAL
);
1904 struct brw_tes_prog_data
*tes_prog_data
= (struct brw_tes_prog_data
*) prog_data
;
1907 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1908 dest
= get_nir_dest(instr
->dest
);
1910 switch (instr
->intrinsic
) {
1911 case nir_intrinsic_load_primitive_id
:
1912 bld
.MOV(dest
, fs_reg(brw_vec1_grf(0, 1)));
1914 case nir_intrinsic_load_tess_coord
:
1915 /* gl_TessCoord is part of the payload in g1-3 */
1916 for (unsigned i
= 0; i
< 3; i
++) {
1917 bld
.MOV(offset(dest
, bld
, i
), fs_reg(brw_vec8_grf(1 + i
, 0)));
1921 case nir_intrinsic_load_tess_level_outer
:
1922 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
1923 * appears as a push-model input. So, we can simply use the ATTR file
1924 * rather than issuing URB read messages. The data is stored in the
1925 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
1928 switch (tes_prog_data
->domain
) {
1929 case BRW_TESS_DOMAIN_QUAD
:
1930 for (unsigned i
= 0; i
< 4; i
++)
1931 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1933 case BRW_TESS_DOMAIN_TRI
:
1934 for (unsigned i
= 0; i
< 3; i
++)
1935 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1937 case BRW_TESS_DOMAIN_ISOLINE
:
1938 for (unsigned i
= 0; i
< 2; i
++)
1939 bld
.MOV(offset(dest
, bld
, i
), component(fs_reg(ATTR
, 0), 7 - i
));
1944 case nir_intrinsic_load_tess_level_inner
:
1945 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
1946 * appears as a push-model input. So, we can simply use the ATTR file
1947 * rather than issuing URB read messages.
1949 switch (tes_prog_data
->domain
) {
1950 case BRW_TESS_DOMAIN_QUAD
:
1951 bld
.MOV(dest
, component(fs_reg(ATTR
, 0), 3));
1952 bld
.MOV(offset(dest
, bld
, 1), component(fs_reg(ATTR
, 0), 2));
1954 case BRW_TESS_DOMAIN_TRI
:
1955 bld
.MOV(dest
, component(fs_reg(ATTR
, 0), 4));
1957 case BRW_TESS_DOMAIN_ISOLINE
:
1958 /* ignore - value is undefined */
1963 case nir_intrinsic_load_input
:
1964 case nir_intrinsic_load_per_vertex_input
: {
1965 fs_reg indirect_offset
= get_indirect_offset(instr
);
1966 unsigned imm_offset
= instr
->const_index
[0];
1969 if (indirect_offset
.file
== BAD_FILE
) {
1970 /* Arbitrarily only push up to 32 vec4 slots worth of data,
1971 * which is 16 registers (since each holds 2 vec4 slots).
1973 const unsigned max_push_slots
= 32;
1974 if (imm_offset
< max_push_slots
) {
1975 fs_reg src
= fs_reg(ATTR
, imm_offset
/ 2, dest
.type
);
1976 for (int i
= 0; i
< instr
->num_components
; i
++) {
1977 bld
.MOV(offset(dest
, bld
, i
),
1978 component(src
, 4 * (imm_offset
% 2) + i
));
1980 tes_prog_data
->base
.urb_read_length
=
1981 MAX2(tes_prog_data
->base
.urb_read_length
,
1982 DIV_ROUND_UP(imm_offset
+ 1, 2));
1984 /* Replicate the patch handle to all enabled channels */
1985 const fs_reg srcs
[] = {
1986 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)
1988 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1989 bld
.LOAD_PAYLOAD(patch_handle
, srcs
, ARRAY_SIZE(srcs
), 0);
1991 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dest
, patch_handle
);
1993 inst
->offset
= imm_offset
;
1994 inst
->base_mrf
= -1;
1995 inst
->regs_written
= instr
->num_components
;
1998 /* Indirect indexing - use per-slot offsets as well. */
1999 const fs_reg srcs
[] = {
2000 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
2003 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2004 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2006 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dest
, payload
);
2008 inst
->offset
= imm_offset
;
2009 inst
->base_mrf
= -1;
2010 inst
->regs_written
= instr
->num_components
;
2015 nir_emit_intrinsic(bld
, instr
);
2021 fs_visitor::nir_emit_gs_intrinsic(const fs_builder
&bld
,
2022 nir_intrinsic_instr
*instr
)
2024 assert(stage
== MESA_SHADER_GEOMETRY
);
2025 fs_reg indirect_offset
;
2028 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2029 dest
= get_nir_dest(instr
->dest
);
2031 switch (instr
->intrinsic
) {
2032 case nir_intrinsic_load_primitive_id
:
2033 assert(stage
== MESA_SHADER_GEOMETRY
);
2034 assert(((struct brw_gs_prog_data
*)prog_data
)->include_primitive_id
);
2035 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
2036 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD
));
2039 case nir_intrinsic_load_input
:
2040 unreachable("load_input intrinsics are invalid for the GS stage");
2042 case nir_intrinsic_load_per_vertex_input
:
2043 emit_gs_input_load(dest
, instr
->src
[0], instr
->const_index
[0],
2044 instr
->src
[1], instr
->num_components
);
2047 case nir_intrinsic_emit_vertex_with_counter
:
2048 emit_gs_vertex(instr
->src
[0], instr
->const_index
[0]);
2051 case nir_intrinsic_end_primitive_with_counter
:
2052 emit_gs_end_primitive(instr
->src
[0]);
2055 case nir_intrinsic_set_vertex_count
:
2056 bld
.MOV(this->final_gs_vertex_count
, get_nir_src(instr
->src
[0]));
2059 case nir_intrinsic_load_invocation_id
: {
2060 fs_reg val
= nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
2061 assert(val
.file
!= BAD_FILE
);
2062 dest
.type
= val
.type
;
2068 nir_emit_intrinsic(bld
, instr
);
2074 fs_visitor::nir_emit_fs_intrinsic(const fs_builder
&bld
,
2075 nir_intrinsic_instr
*instr
)
2077 assert(stage
== MESA_SHADER_FRAGMENT
);
2078 struct brw_wm_prog_data
*wm_prog_data
=
2079 (struct brw_wm_prog_data
*) prog_data
;
2082 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2083 dest
= get_nir_dest(instr
->dest
);
2085 switch (instr
->intrinsic
) {
2086 case nir_intrinsic_load_front_face
:
2087 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
2088 *emit_frontfacing_interpolation());
2091 case nir_intrinsic_load_sample_pos
: {
2092 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
2093 assert(sample_pos
.file
!= BAD_FILE
);
2094 dest
.type
= sample_pos
.type
;
2095 bld
.MOV(dest
, sample_pos
);
2096 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
2100 case nir_intrinsic_load_helper_invocation
:
2101 case nir_intrinsic_load_sample_mask_in
:
2102 case nir_intrinsic_load_sample_id
: {
2103 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
2104 fs_reg val
= nir_system_values
[sv
];
2105 assert(val
.file
!= BAD_FILE
);
2106 dest
.type
= val
.type
;
2111 case nir_intrinsic_discard
:
2112 case nir_intrinsic_discard_if
: {
2113 /* We track our discarded pixels in f0.1. By predicating on it, we can
2114 * update just the flag bits that aren't yet discarded. If there's no
2115 * condition, we emit a CMP of g0 != g0, so all currently executing
2116 * channels will get turned off.
2119 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
2120 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
2121 brw_imm_d(0), BRW_CONDITIONAL_Z
);
2123 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
2124 BRW_REGISTER_TYPE_UW
));
2125 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
2127 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
2128 cmp
->flag_subreg
= 1;
2130 if (devinfo
->gen
>= 6) {
2131 emit_discard_jump();
2136 case nir_intrinsic_interp_var_at_centroid
:
2137 case nir_intrinsic_interp_var_at_sample
:
2138 case nir_intrinsic_interp_var_at_offset
: {
2139 /* Handle ARB_gpu_shader5 interpolation intrinsics
2141 * It's worth a quick word of explanation as to why we handle the full
2142 * variable-based interpolation intrinsic rather than a lowered version
2143 * with like we do for other inputs. We have to do that because the way
2144 * we set up inputs doesn't allow us to use the already setup inputs for
2145 * interpolation. At the beginning of the shader, we go through all of
2146 * the input variables and do the initial interpolation and put it in
2147 * the nir_inputs array based on its location as determined in
2148 * nir_lower_io. If the input isn't used, dead code cleans up and
2149 * everything works fine. However, when we get to the ARB_gpu_shader5
2150 * interpolation intrinsics, we need to reinterpolate the input
2151 * differently. If we used an intrinsic that just had an index it would
2152 * only give us the offset into the nir_inputs array. However, this is
2153 * useless because that value is post-interpolation and we need
2154 * pre-interpolation. In order to get the actual location of the bits
2155 * we get from the vertex fetching hardware, we need the variable.
2157 wm_prog_data
->pulls_bary
= true;
2159 fs_reg dst_xy
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 2);
2160 const glsl_interp_qualifier interpolation
=
2161 (glsl_interp_qualifier
) instr
->variables
[0]->var
->data
.interpolation
;
2163 switch (instr
->intrinsic
) {
2164 case nir_intrinsic_interp_var_at_centroid
:
2165 emit_pixel_interpolater_send(bld
,
2166 FS_OPCODE_INTERPOLATE_AT_CENTROID
,
2173 case nir_intrinsic_interp_var_at_sample
: {
2174 nir_const_value
*const_sample
= nir_src_as_const_value(instr
->src
[0]);
2177 unsigned msg_data
= const_sample
->i32
[0] << 4;
2179 emit_pixel_interpolater_send(bld
,
2180 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2183 brw_imm_ud(msg_data
),
2186 const fs_reg sample_src
= retype(get_nir_src(instr
->src
[0]),
2187 BRW_REGISTER_TYPE_UD
);
2189 if (nir_src_is_dynamically_uniform(instr
->src
[0])) {
2190 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
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));
2194 emit_pixel_interpolater_send(bld
,
2195 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2201 /* Make a loop that sends a message to the pixel interpolater
2202 * for the sample number in each live channel. If there are
2203 * multiple channels with the same sample number then these
2204 * will be handled simultaneously with a single interation of
2207 bld
.emit(BRW_OPCODE_DO
);
2209 /* Get the next live sample number into sample_id_reg */
2210 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
2212 /* Set the flag register so that we can perform the send
2213 * message on all channels that have the same sample number
2215 bld
.CMP(bld
.null_reg_ud(),
2216 sample_src
, sample_id
,
2217 BRW_CONDITIONAL_EQ
);
2218 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
2219 bld
.exec_all().group(1, 0)
2220 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
2222 emit_pixel_interpolater_send(bld
,
2223 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
2228 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
2230 /* Continue the loop if there are any live channels left */
2231 set_predicate_inv(BRW_PREDICATE_NORMAL
,
2233 bld
.emit(BRW_OPCODE_WHILE
));
2240 case nir_intrinsic_interp_var_at_offset
: {
2241 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2244 unsigned off_x
= MIN2((int)(const_offset
->f32
[0] * 16), 7) & 0xf;
2245 unsigned off_y
= MIN2((int)(const_offset
->f32
[1] * 16), 7) & 0xf;
2247 emit_pixel_interpolater_send(bld
,
2248 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
,
2251 brw_imm_ud(off_x
| (off_y
<< 4)),
2254 fs_reg src
= vgrf(glsl_type::ivec2_type
);
2255 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
2256 BRW_REGISTER_TYPE_F
);
2257 for (int i
= 0; i
< 2; i
++) {
2258 fs_reg temp
= vgrf(glsl_type::float_type
);
2259 bld
.MUL(temp
, offset(offset_src
, bld
, i
), brw_imm_f(16.0f
));
2260 fs_reg itemp
= vgrf(glsl_type::int_type
);
2261 bld
.MOV(itemp
, temp
); /* float to int */
2263 /* Clamp the upper end of the range to +7/16.
2264 * ARB_gpu_shader5 requires that we support a maximum offset
2265 * of +0.5, which isn't representable in a S0.4 value -- if
2266 * we didn't clamp it, we'd end up with -8/16, which is the
2267 * opposite of what the shader author wanted.
2269 * This is legal due to ARB_gpu_shader5's quantization
2272 * "Not all values of <offset> may be supported; x and y
2273 * offsets may be rounded to fixed-point values with the
2274 * number of fraction bits given by the
2275 * implementation-dependent constant
2276 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
2278 set_condmod(BRW_CONDITIONAL_L
,
2279 bld
.SEL(offset(src
, bld
, i
), itemp
, brw_imm_d(7)));
2282 const enum opcode opcode
= FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
;
2283 emit_pixel_interpolater_send(bld
,
2294 unreachable("Invalid intrinsic");
2297 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2298 fs_reg src
= interp_reg(instr
->variables
[0]->var
->data
.location
, j
);
2299 src
.type
= dest
.type
;
2301 bld
.emit(FS_OPCODE_LINTERP
, dest
, dst_xy
, src
);
2302 dest
= offset(dest
, bld
, 1);
2307 nir_emit_intrinsic(bld
, instr
);
2313 fs_visitor::nir_emit_cs_intrinsic(const fs_builder
&bld
,
2314 nir_intrinsic_instr
*instr
)
2316 assert(stage
== MESA_SHADER_COMPUTE
);
2317 struct brw_cs_prog_data
*cs_prog_data
=
2318 (struct brw_cs_prog_data
*) prog_data
;
2321 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2322 dest
= get_nir_dest(instr
->dest
);
2324 switch (instr
->intrinsic
) {
2325 case nir_intrinsic_barrier
:
2327 cs_prog_data
->uses_barrier
= true;
2330 case nir_intrinsic_load_local_invocation_id
:
2331 case nir_intrinsic_load_work_group_id
: {
2332 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
2333 fs_reg val
= nir_system_values
[sv
];
2334 assert(val
.file
!= BAD_FILE
);
2335 dest
.type
= val
.type
;
2336 for (unsigned i
= 0; i
< 3; i
++)
2337 bld
.MOV(offset(dest
, bld
, i
), offset(val
, bld
, i
));
2341 case nir_intrinsic_load_num_work_groups
: {
2342 const unsigned surface
=
2343 cs_prog_data
->binding_table
.work_groups_start
;
2345 cs_prog_data
->uses_num_work_groups
= true;
2347 fs_reg surf_index
= brw_imm_ud(surface
);
2348 brw_mark_surface_used(prog_data
, surface
);
2350 /* Read the 3 GLuint components of gl_NumWorkGroups */
2351 for (unsigned i
= 0; i
< 3; i
++) {
2352 fs_reg read_result
=
2353 emit_untyped_read(bld
, surf_index
,
2355 1 /* dims */, 1 /* size */,
2356 BRW_PREDICATE_NONE
);
2357 read_result
.type
= dest
.type
;
2358 bld
.MOV(dest
, read_result
);
2359 dest
= offset(dest
, bld
, 1);
2364 case nir_intrinsic_shared_atomic_add
:
2365 nir_emit_shared_atomic(bld
, BRW_AOP_ADD
, instr
);
2367 case nir_intrinsic_shared_atomic_imin
:
2368 nir_emit_shared_atomic(bld
, BRW_AOP_IMIN
, instr
);
2370 case nir_intrinsic_shared_atomic_umin
:
2371 nir_emit_shared_atomic(bld
, BRW_AOP_UMIN
, instr
);
2373 case nir_intrinsic_shared_atomic_imax
:
2374 nir_emit_shared_atomic(bld
, BRW_AOP_IMAX
, instr
);
2376 case nir_intrinsic_shared_atomic_umax
:
2377 nir_emit_shared_atomic(bld
, BRW_AOP_UMAX
, instr
);
2379 case nir_intrinsic_shared_atomic_and
:
2380 nir_emit_shared_atomic(bld
, BRW_AOP_AND
, instr
);
2382 case nir_intrinsic_shared_atomic_or
:
2383 nir_emit_shared_atomic(bld
, BRW_AOP_OR
, instr
);
2385 case nir_intrinsic_shared_atomic_xor
:
2386 nir_emit_shared_atomic(bld
, BRW_AOP_XOR
, instr
);
2388 case nir_intrinsic_shared_atomic_exchange
:
2389 nir_emit_shared_atomic(bld
, BRW_AOP_MOV
, instr
);
2391 case nir_intrinsic_shared_atomic_comp_swap
:
2392 nir_emit_shared_atomic(bld
, BRW_AOP_CMPWR
, instr
);
2395 case nir_intrinsic_load_shared
: {
2396 assert(devinfo
->gen
>= 7);
2398 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
2400 /* Get the offset to read from */
2402 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2404 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u32
[0]);
2406 offset_reg
= vgrf(glsl_type::uint_type
);
2408 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
2409 brw_imm_ud(instr
->const_index
[0]));
2412 /* Read the vector */
2413 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
2415 instr
->num_components
,
2416 BRW_PREDICATE_NONE
);
2417 read_result
.type
= dest
.type
;
2418 for (int i
= 0; i
< instr
->num_components
; i
++)
2419 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
2424 case nir_intrinsic_store_shared
: {
2425 assert(devinfo
->gen
>= 7);
2428 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
2431 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
2434 unsigned writemask
= instr
->const_index
[1];
2436 /* Combine groups of consecutive enabled channels in one write
2437 * message. We use ffs to find the first enabled channel and then ffs on
2438 * the bit-inverse, down-shifted writemask to determine the length of
2439 * the block of enabled bits.
2442 unsigned first_component
= ffs(writemask
) - 1;
2443 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
2446 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2448 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u32
[0] +
2449 4 * first_component
);
2451 offset_reg
= vgrf(glsl_type::uint_type
);
2453 retype(get_nir_src(instr
->src
[1]), BRW_REGISTER_TYPE_UD
),
2454 brw_imm_ud(instr
->const_index
[0] + 4 * first_component
));
2457 emit_untyped_write(bld
, surf_index
, offset_reg
,
2458 offset(val_reg
, bld
, first_component
),
2459 1 /* dims */, length
,
2460 BRW_PREDICATE_NONE
);
2462 /* Clear the bits in the writemask that we just wrote, then try
2463 * again to see if more channels are left.
2465 writemask
&= (15 << (first_component
+ length
));
2472 nir_emit_intrinsic(bld
, instr
);
2478 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
2481 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2482 dest
= get_nir_dest(instr
->dest
);
2484 switch (instr
->intrinsic
) {
2485 case nir_intrinsic_atomic_counter_inc
:
2486 case nir_intrinsic_atomic_counter_dec
:
2487 case nir_intrinsic_atomic_counter_read
: {
2488 /* Get the arguments of the atomic intrinsic. */
2489 const fs_reg offset
= get_nir_src(instr
->src
[0]);
2490 const unsigned surface
= (stage_prog_data
->binding_table
.abo_start
+
2491 instr
->const_index
[0]);
2494 /* Emit a surface read or atomic op. */
2495 switch (instr
->intrinsic
) {
2496 case nir_intrinsic_atomic_counter_read
:
2497 tmp
= emit_untyped_read(bld
, brw_imm_ud(surface
), offset
, 1, 1);
2500 case nir_intrinsic_atomic_counter_inc
:
2501 tmp
= emit_untyped_atomic(bld
, brw_imm_ud(surface
), offset
, fs_reg(),
2502 fs_reg(), 1, 1, BRW_AOP_INC
);
2505 case nir_intrinsic_atomic_counter_dec
:
2506 tmp
= emit_untyped_atomic(bld
, brw_imm_ud(surface
), offset
, fs_reg(),
2507 fs_reg(), 1, 1, BRW_AOP_PREDEC
);
2511 unreachable("Unreachable");
2514 /* Assign the result. */
2515 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), tmp
);
2517 /* Mark the surface as used. */
2518 brw_mark_surface_used(stage_prog_data
, surface
);
2522 case nir_intrinsic_image_load
:
2523 case nir_intrinsic_image_store
:
2524 case nir_intrinsic_image_atomic_add
:
2525 case nir_intrinsic_image_atomic_min
:
2526 case nir_intrinsic_image_atomic_max
:
2527 case nir_intrinsic_image_atomic_and
:
2528 case nir_intrinsic_image_atomic_or
:
2529 case nir_intrinsic_image_atomic_xor
:
2530 case nir_intrinsic_image_atomic_exchange
:
2531 case nir_intrinsic_image_atomic_comp_swap
: {
2532 using namespace image_access
;
2534 /* Get the referenced image variable and type. */
2535 const nir_variable
*var
= instr
->variables
[0]->var
;
2536 const glsl_type
*type
= var
->type
->without_array();
2537 const brw_reg_type base_type
= get_image_base_type(type
);
2539 /* Get some metadata from the image intrinsic. */
2540 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
2541 const unsigned arr_dims
= type
->sampler_array
? 1 : 0;
2542 const unsigned surf_dims
= type
->coordinate_components() - arr_dims
;
2543 const mesa_format format
=
2544 (var
->data
.image
.write_only
? MESA_FORMAT_NONE
:
2545 _mesa_get_shader_image_format(var
->data
.image
.format
));
2547 /* Get the arguments of the image intrinsic. */
2548 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
2549 const fs_reg addr
= retype(get_nir_src(instr
->src
[0]),
2550 BRW_REGISTER_TYPE_UD
);
2551 const fs_reg src0
= (info
->num_srcs
>= 3 ?
2552 retype(get_nir_src(instr
->src
[2]), base_type
) :
2554 const fs_reg src1
= (info
->num_srcs
>= 4 ?
2555 retype(get_nir_src(instr
->src
[3]), base_type
) :
2559 /* Emit an image load, store or atomic op. */
2560 if (instr
->intrinsic
== nir_intrinsic_image_load
)
2561 tmp
= emit_image_load(bld
, image
, addr
, surf_dims
, arr_dims
, format
);
2563 else if (instr
->intrinsic
== nir_intrinsic_image_store
)
2564 emit_image_store(bld
, image
, addr
, src0
, surf_dims
, arr_dims
, format
);
2567 tmp
= emit_image_atomic(bld
, image
, addr
, src0
, src1
,
2568 surf_dims
, arr_dims
, info
->dest_components
,
2569 get_image_atomic_op(instr
->intrinsic
, type
));
2571 /* Assign the result. */
2572 for (unsigned c
= 0; c
< info
->dest_components
; ++c
)
2573 bld
.MOV(offset(retype(dest
, base_type
), bld
, c
),
2574 offset(tmp
, bld
, c
));
2578 case nir_intrinsic_memory_barrier_atomic_counter
:
2579 case nir_intrinsic_memory_barrier_buffer
:
2580 case nir_intrinsic_memory_barrier_image
:
2581 case nir_intrinsic_memory_barrier
: {
2582 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 16 / dispatch_width
);
2583 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
2588 case nir_intrinsic_group_memory_barrier
:
2589 case nir_intrinsic_memory_barrier_shared
:
2590 /* We treat these workgroup-level barriers as no-ops. This should be
2591 * safe at present and as long as:
2593 * - Memory access instructions are not subsequently reordered by the
2594 * compiler back-end.
2596 * - All threads from a given compute shader workgroup fit within a
2597 * single subslice and therefore talk to the same HDC shared unit
2598 * what supposedly guarantees ordering and coherency between threads
2599 * from the same workgroup. This may change in the future when we
2600 * start splitting workgroups across multiple subslices.
2602 * - The context is not in fault-and-stream mode, which could cause
2603 * memory transactions (including to SLM) prior to the barrier to be
2604 * replayed after the barrier if a pagefault occurs. This shouldn't
2605 * be a problem up to and including SKL because fault-and-stream is
2606 * not usable due to hardware issues, but that's likely to change in
2611 case nir_intrinsic_shader_clock
: {
2612 /* We cannot do anything if there is an event, so ignore it for now */
2613 fs_reg shader_clock
= get_timestamp(bld
);
2614 const fs_reg srcs
[] = { shader_clock
.set_smear(0), shader_clock
.set_smear(1) };
2616 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
2620 case nir_intrinsic_image_size
: {
2621 /* Get the referenced image variable and type. */
2622 const nir_variable
*var
= instr
->variables
[0]->var
;
2623 const glsl_type
*type
= var
->type
->without_array();
2625 /* Get the size of the image. */
2626 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
2627 const fs_reg size
= offset(image
, bld
, BRW_IMAGE_PARAM_SIZE_OFFSET
);
2629 /* For 1DArray image types, the array index is stored in the Z component.
2630 * Fix this by swizzling the Z component to the Y component.
2632 const bool is_1d_array_image
=
2633 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
&&
2634 type
->sampler_array
;
2636 /* For CubeArray images, we should count the number of cubes instead
2637 * of the number of faces. Fix it by dividing the (Z component) by 6.
2639 const bool is_cube_array_image
=
2640 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2641 type
->sampler_array
;
2643 /* Copy all the components. */
2644 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
2645 for (unsigned c
= 0; c
< info
->dest_components
; ++c
) {
2646 if ((int)c
>= type
->coordinate_components()) {
2647 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2649 } else if (c
== 1 && is_1d_array_image
) {
2650 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2651 offset(size
, bld
, 2));
2652 } else if (c
== 2 && is_cube_array_image
) {
2653 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
2654 offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2655 offset(size
, bld
, c
), brw_imm_d(6));
2657 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
2658 offset(size
, bld
, c
));
2665 case nir_intrinsic_image_samples
:
2666 /* The driver does not support multi-sampled images. */
2667 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(1));
2670 case nir_intrinsic_load_uniform
: {
2671 /* Offsets are in bytes but they should always be multiples of 4 */
2672 assert(instr
->const_index
[0] % 4 == 0);
2674 fs_reg
src(UNIFORM
, instr
->const_index
[0] / 4, dest
.type
);
2676 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2678 /* Offsets are in bytes but they should always be multiples of 4 */
2679 assert(const_offset
->u32
[0] % 4 == 0);
2680 src
.reg_offset
= const_offset
->u32
[0] / 4;
2682 src
.reladdr
= new(mem_ctx
) fs_reg(retype(get_nir_src(instr
->src
[0]),
2683 BRW_REGISTER_TYPE_UD
));
2686 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2687 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
2692 case nir_intrinsic_load_ubo
: {
2693 nir_const_value
*const_index
= nir_src_as_const_value(instr
->src
[0]);
2697 const unsigned index
= stage_prog_data
->binding_table
.ubo_start
+
2698 const_index
->u32
[0];
2699 surf_index
= brw_imm_ud(index
);
2700 brw_mark_surface_used(prog_data
, index
);
2702 /* The block index is not a constant. Evaluate the index expression
2703 * per-channel and add the base UBO index; we have to select a value
2704 * from any live channel.
2706 surf_index
= vgrf(glsl_type::uint_type
);
2707 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
2708 brw_imm_ud(stage_prog_data
->binding_table
.ubo_start
));
2709 surf_index
= bld
.emit_uniformize(surf_index
);
2711 /* Assume this may touch any UBO. It would be nice to provide
2712 * a tighter bound, but the array information is already lowered away.
2714 brw_mark_surface_used(prog_data
,
2715 stage_prog_data
->binding_table
.ubo_start
+
2716 nir
->info
.num_ubos
- 1);
2719 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2720 if (const_offset
== NULL
) {
2721 fs_reg base_offset
= retype(get_nir_src(instr
->src
[1]),
2722 BRW_REGISTER_TYPE_UD
);
2724 for (int i
= 0; i
< instr
->num_components
; i
++)
2725 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
2726 base_offset
, i
* 4);
2728 fs_reg packed_consts
= vgrf(glsl_type::float_type
);
2729 packed_consts
.type
= dest
.type
;
2731 struct brw_reg const_offset_reg
= brw_imm_ud(const_offset
->u32
[0] & ~15);
2732 bld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, packed_consts
,
2733 surf_index
, const_offset_reg
);
2735 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2736 packed_consts
.set_smear(const_offset
->u32
[0] % 16 / 4 + i
);
2738 /* The std140 packing rules don't allow vectors to cross 16-byte
2739 * boundaries, and a reg is 32 bytes.
2741 assert(packed_consts
.subreg_offset
< 32);
2743 bld
.MOV(dest
, packed_consts
);
2744 dest
= offset(dest
, bld
, 1);
2750 case nir_intrinsic_load_ssbo
: {
2751 assert(devinfo
->gen
>= 7);
2753 nir_const_value
*const_uniform_block
=
2754 nir_src_as_const_value(instr
->src
[0]);
2757 if (const_uniform_block
) {
2758 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
2759 const_uniform_block
->u32
[0];
2760 surf_index
= brw_imm_ud(index
);
2761 brw_mark_surface_used(prog_data
, index
);
2763 surf_index
= vgrf(glsl_type::uint_type
);
2764 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
2765 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2767 /* Assume this may touch any UBO. It would be nice to provide
2768 * a tighter bound, but the array information is already lowered away.
2770 brw_mark_surface_used(prog_data
,
2771 stage_prog_data
->binding_table
.ssbo_start
+
2772 nir
->info
.num_ssbos
- 1);
2776 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2778 offset_reg
= brw_imm_ud(const_offset
->u32
[0]);
2780 offset_reg
= get_nir_src(instr
->src
[1]);
2783 /* Read the vector */
2784 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
2786 instr
->num_components
,
2787 BRW_PREDICATE_NONE
);
2788 read_result
.type
= dest
.type
;
2789 for (int i
= 0; i
< instr
->num_components
; i
++)
2790 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
2795 case nir_intrinsic_load_input
: {
2797 if (stage
== MESA_SHADER_VERTEX
) {
2798 src
= fs_reg(ATTR
, instr
->const_index
[0], dest
.type
);
2800 src
= offset(retype(nir_inputs
, dest
.type
), bld
,
2801 instr
->const_index
[0]);
2804 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2805 assert(const_offset
&& "Indirect input loads not allowed");
2806 src
= offset(src
, bld
, const_offset
->u32
[0]);
2808 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2809 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
2814 case nir_intrinsic_store_ssbo
: {
2815 assert(devinfo
->gen
>= 7);
2819 nir_const_value
*const_uniform_block
=
2820 nir_src_as_const_value(instr
->src
[1]);
2821 if (const_uniform_block
) {
2822 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
2823 const_uniform_block
->u32
[0];
2824 surf_index
= brw_imm_ud(index
);
2825 brw_mark_surface_used(prog_data
, index
);
2827 surf_index
= vgrf(glsl_type::uint_type
);
2828 bld
.ADD(surf_index
, get_nir_src(instr
->src
[1]),
2829 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2831 brw_mark_surface_used(prog_data
,
2832 stage_prog_data
->binding_table
.ssbo_start
+
2833 nir
->info
.num_ssbos
- 1);
2837 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
2840 unsigned writemask
= instr
->const_index
[0];
2842 /* Combine groups of consecutive enabled channels in one write
2843 * message. We use ffs to find the first enabled channel and then ffs on
2844 * the bit-inverse, down-shifted writemask to determine the length of
2845 * the block of enabled bits.
2848 unsigned first_component
= ffs(writemask
) - 1;
2849 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
2852 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[2]);
2854 offset_reg
= brw_imm_ud(const_offset
->u32
[0] + 4 * first_component
);
2856 offset_reg
= vgrf(glsl_type::uint_type
);
2858 retype(get_nir_src(instr
->src
[2]), BRW_REGISTER_TYPE_UD
),
2859 brw_imm_ud(4 * first_component
));
2862 emit_untyped_write(bld
, surf_index
, offset_reg
,
2863 offset(val_reg
, bld
, first_component
),
2864 1 /* dims */, length
,
2865 BRW_PREDICATE_NONE
);
2867 /* Clear the bits in the writemask that we just wrote, then try
2868 * again to see if more channels are left.
2870 writemask
&= (15 << (first_component
+ length
));
2875 case nir_intrinsic_store_output
: {
2876 fs_reg src
= get_nir_src(instr
->src
[0]);
2877 fs_reg new_dest
= offset(retype(nir_outputs
, src
.type
), bld
,
2878 instr
->const_index
[0]);
2880 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
2881 assert(const_offset
&& "Indirect output stores not allowed");
2882 new_dest
= offset(new_dest
, bld
, const_offset
->u32
[0]);
2884 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
2885 bld
.MOV(offset(new_dest
, bld
, j
), offset(src
, bld
, j
));
2890 case nir_intrinsic_ssbo_atomic_add
:
2891 nir_emit_ssbo_atomic(bld
, BRW_AOP_ADD
, instr
);
2893 case nir_intrinsic_ssbo_atomic_imin
:
2894 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMIN
, instr
);
2896 case nir_intrinsic_ssbo_atomic_umin
:
2897 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMIN
, instr
);
2899 case nir_intrinsic_ssbo_atomic_imax
:
2900 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMAX
, instr
);
2902 case nir_intrinsic_ssbo_atomic_umax
:
2903 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMAX
, instr
);
2905 case nir_intrinsic_ssbo_atomic_and
:
2906 nir_emit_ssbo_atomic(bld
, BRW_AOP_AND
, instr
);
2908 case nir_intrinsic_ssbo_atomic_or
:
2909 nir_emit_ssbo_atomic(bld
, BRW_AOP_OR
, instr
);
2911 case nir_intrinsic_ssbo_atomic_xor
:
2912 nir_emit_ssbo_atomic(bld
, BRW_AOP_XOR
, instr
);
2914 case nir_intrinsic_ssbo_atomic_exchange
:
2915 nir_emit_ssbo_atomic(bld
, BRW_AOP_MOV
, instr
);
2917 case nir_intrinsic_ssbo_atomic_comp_swap
:
2918 nir_emit_ssbo_atomic(bld
, BRW_AOP_CMPWR
, instr
);
2921 case nir_intrinsic_get_buffer_size
: {
2922 nir_const_value
*const_uniform_block
= nir_src_as_const_value(instr
->src
[0]);
2923 unsigned ssbo_index
= const_uniform_block
? const_uniform_block
->u32
[0] : 0;
2924 int reg_width
= dispatch_width
/ 8;
2927 fs_reg source
= brw_imm_d(0);
2929 int mlen
= 1 * reg_width
;
2931 /* A resinfo's sampler message is used to get the buffer size.
2932 * The SIMD8's writeback message consists of four registers and
2933 * SIMD16's writeback message consists of 8 destination registers
2934 * (two per each component), although we are only interested on the
2935 * first component, where resinfo returns the buffer size for
2938 int regs_written
= 4 * mlen
;
2939 fs_reg src_payload
= fs_reg(VGRF
, alloc
.allocate(mlen
),
2940 BRW_REGISTER_TYPE_UD
);
2941 bld
.LOAD_PAYLOAD(src_payload
, &source
, 1, 0);
2942 fs_reg buffer_size
= fs_reg(VGRF
, alloc
.allocate(regs_written
),
2943 BRW_REGISTER_TYPE_UD
);
2944 const unsigned index
= prog_data
->binding_table
.ssbo_start
+ ssbo_index
;
2945 fs_inst
*inst
= bld
.emit(FS_OPCODE_GET_BUFFER_SIZE
, buffer_size
,
2946 src_payload
, brw_imm_ud(index
));
2947 inst
->header_size
= 0;
2949 inst
->regs_written
= regs_written
;
2951 bld
.MOV(retype(dest
, buffer_size
.type
), buffer_size
);
2953 brw_mark_surface_used(prog_data
, index
);
2958 unreachable("unknown intrinsic");
2963 fs_visitor::nir_emit_ssbo_atomic(const fs_builder
&bld
,
2964 int op
, nir_intrinsic_instr
*instr
)
2967 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2968 dest
= get_nir_dest(instr
->dest
);
2971 nir_const_value
*const_surface
= nir_src_as_const_value(instr
->src
[0]);
2972 if (const_surface
) {
2973 unsigned surf_index
= stage_prog_data
->binding_table
.ssbo_start
+
2974 const_surface
->u32
[0];
2975 surface
= brw_imm_ud(surf_index
);
2976 brw_mark_surface_used(prog_data
, surf_index
);
2978 surface
= vgrf(glsl_type::uint_type
);
2979 bld
.ADD(surface
, get_nir_src(instr
->src
[0]),
2980 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
2982 /* Assume this may touch any SSBO. This is the same we do for other
2983 * UBO/SSBO accesses with non-constant surface.
2985 brw_mark_surface_used(prog_data
,
2986 stage_prog_data
->binding_table
.ssbo_start
+
2987 nir
->info
.num_ssbos
- 1);
2990 fs_reg offset
= get_nir_src(instr
->src
[1]);
2991 fs_reg data1
= get_nir_src(instr
->src
[2]);
2993 if (op
== BRW_AOP_CMPWR
)
2994 data2
= get_nir_src(instr
->src
[3]);
2996 /* Emit the actual atomic operation operation */
2998 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
3000 1 /* dims */, 1 /* rsize */,
3002 BRW_PREDICATE_NONE
);
3003 dest
.type
= atomic_result
.type
;
3004 bld
.MOV(dest
, atomic_result
);
3008 fs_visitor::nir_emit_shared_atomic(const fs_builder
&bld
,
3009 int op
, nir_intrinsic_instr
*instr
)
3012 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3013 dest
= get_nir_dest(instr
->dest
);
3015 fs_reg surface
= brw_imm_ud(GEN7_BTI_SLM
);
3016 fs_reg offset
= get_nir_src(instr
->src
[0]);
3017 fs_reg data1
= get_nir_src(instr
->src
[1]);
3019 if (op
== BRW_AOP_CMPWR
)
3020 data2
= get_nir_src(instr
->src
[2]);
3022 /* Emit the actual atomic operation operation */
3024 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
3026 1 /* dims */, 1 /* rsize */,
3028 BRW_PREDICATE_NONE
);
3029 dest
.type
= atomic_result
.type
;
3030 bld
.MOV(dest
, atomic_result
);
3034 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
3036 unsigned texture
= instr
->texture_index
;
3037 unsigned sampler
= instr
->sampler_index
;
3038 fs_reg
texture_reg(brw_imm_ud(texture
));
3039 fs_reg
sampler_reg(brw_imm_ud(sampler
));
3041 int gather_component
= instr
->component
;
3043 bool is_cube_array
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
3046 int lod_components
= 0;
3048 fs_reg coordinate
, shadow_comparitor
, lod
, lod2
, sample_index
, mcs
, tex_offset
;
3050 /* The hardware requires a LOD for buffer textures */
3051 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
3054 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
3055 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
3056 switch (instr
->src
[i
].src_type
) {
3057 case nir_tex_src_bias
:
3058 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3060 case nir_tex_src_comparitor
:
3061 shadow_comparitor
= retype(src
, BRW_REGISTER_TYPE_F
);
3063 case nir_tex_src_coord
:
3064 switch (instr
->op
) {
3066 case nir_texop_txf_ms
:
3067 case nir_texop_samples_identical
:
3068 coordinate
= retype(src
, BRW_REGISTER_TYPE_D
);
3071 coordinate
= retype(src
, BRW_REGISTER_TYPE_F
);
3075 case nir_tex_src_ddx
:
3076 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3077 lod_components
= nir_tex_instr_src_size(instr
, i
);
3079 case nir_tex_src_ddy
:
3080 lod2
= retype(src
, BRW_REGISTER_TYPE_F
);
3082 case nir_tex_src_lod
:
3083 switch (instr
->op
) {
3085 lod
= retype(src
, BRW_REGISTER_TYPE_UD
);
3088 lod
= retype(src
, BRW_REGISTER_TYPE_D
);
3091 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
3095 case nir_tex_src_ms_index
:
3096 sample_index
= retype(src
, BRW_REGISTER_TYPE_UD
);
3099 case nir_tex_src_offset
: {
3100 nir_const_value
*const_offset
=
3101 nir_src_as_const_value(instr
->src
[i
].src
);
3103 tex_offset
= brw_imm_ud(brw_texture_offset(const_offset
->i32
, 3));
3105 tex_offset
= retype(src
, BRW_REGISTER_TYPE_D
);
3110 case nir_tex_src_projector
:
3111 unreachable("should be lowered");
3113 case nir_tex_src_texture_offset
: {
3114 /* Figure out the highest possible texture index and mark it as used */
3115 uint32_t max_used
= texture
+ instr
->texture_array_size
- 1;
3116 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
3117 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
3119 max_used
+= stage_prog_data
->binding_table
.texture_start
;
3121 brw_mark_surface_used(prog_data
, max_used
);
3123 /* Emit code to evaluate the actual indexing expression */
3124 texture_reg
= vgrf(glsl_type::uint_type
);
3125 bld
.ADD(texture_reg
, src
, brw_imm_ud(texture
));
3126 texture_reg
= bld
.emit_uniformize(texture_reg
);
3130 case nir_tex_src_sampler_offset
: {
3131 /* Emit code to evaluate the actual indexing expression */
3132 sampler_reg
= vgrf(glsl_type::uint_type
);
3133 bld
.ADD(sampler_reg
, src
, brw_imm_ud(sampler
));
3134 sampler_reg
= bld
.emit_uniformize(sampler_reg
);
3139 unreachable("unknown texture source");
3143 if (instr
->op
== nir_texop_txf_ms
||
3144 instr
->op
== nir_texop_samples_identical
) {
3145 if (devinfo
->gen
>= 7 &&
3146 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
3147 mcs
= emit_mcs_fetch(coordinate
, instr
->coord_components
, texture_reg
);
3149 mcs
= brw_imm_ud(0u);
3153 enum glsl_base_type dest_base_type
=
3154 brw_glsl_base_type_for_nir_type (instr
->dest_type
);
3156 const glsl_type
*dest_type
=
3157 glsl_type::get_instance(dest_base_type
, nir_tex_instr_dest_size(instr
),
3160 ir_texture_opcode op
;
3161 switch (instr
->op
) {
3162 case nir_texop_lod
: op
= ir_lod
; break;
3163 case nir_texop_query_levels
: op
= ir_query_levels
; break;
3164 case nir_texop_tex
: op
= ir_tex
; break;
3165 case nir_texop_tg4
: op
= ir_tg4
; break;
3166 case nir_texop_txb
: op
= ir_txb
; break;
3167 case nir_texop_txd
: op
= ir_txd
; break;
3168 case nir_texop_txf
: op
= ir_txf
; break;
3169 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
3170 case nir_texop_txl
: op
= ir_txl
; break;
3171 case nir_texop_txs
: op
= ir_txs
; break;
3172 case nir_texop_texture_samples
: {
3173 fs_reg dst
= retype(get_nir_dest(instr
->dest
), BRW_REGISTER_TYPE_D
);
3174 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_SAMPLEINFO
, dst
,
3175 bld
.vgrf(BRW_REGISTER_TYPE_D
, 1),
3176 texture_reg
, texture_reg
);
3178 inst
->header_size
= 1;
3179 inst
->base_mrf
= -1;
3182 case nir_texop_samples_identical
: op
= ir_samples_identical
; break;
3184 unreachable("unknown texture opcode");
3187 emit_texture(op
, dest_type
, coordinate
, instr
->coord_components
,
3188 shadow_comparitor
, lod
, lod2
, lod_components
, sample_index
,
3189 tex_offset
, mcs
, gather_component
, is_cube_array
,
3190 texture
, texture_reg
, sampler
, sampler_reg
);
3192 fs_reg dest
= get_nir_dest(instr
->dest
);
3193 dest
.type
= this->result
.type
;
3194 unsigned num_components
= nir_tex_instr_dest_size(instr
);
3195 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
3196 dest
, this->result
),
3197 (1 << num_components
) - 1);
3201 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
3203 switch (instr
->type
) {
3204 case nir_jump_break
:
3205 bld
.emit(BRW_OPCODE_BREAK
);
3207 case nir_jump_continue
:
3208 bld
.emit(BRW_OPCODE_CONTINUE
);
3210 case nir_jump_return
:
3212 unreachable("unknown jump");