101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2010 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include "compiler/glsl/ir.h"
2501e04c3fSmrg#include "brw_fs.h"
2601e04c3fSmrg#include "brw_nir.h"
277ec681f3Smrg#include "brw_rt.h"
287ec681f3Smrg#include "brw_eu.h"
299f464c52Smaya#include "nir_search_helpers.h"
3001e04c3fSmrg#include "util/u_math.h"
319f464c52Smaya#include "util/bitscan.h"
3201e04c3fSmrg
3301e04c3fSmrgusing namespace brw;
3401e04c3fSmrg
3501e04c3fSmrgvoid
3601e04c3fSmrgfs_visitor::emit_nir_code()
3701e04c3fSmrg{
387ec681f3Smrg   emit_shader_float_controls_execution_mode();
397ec681f3Smrg
4001e04c3fSmrg   /* emit the arrays used for inputs and outputs - load/store intrinsics will
4101e04c3fSmrg    * be converted to reads/writes of these arrays
4201e04c3fSmrg    */
4301e04c3fSmrg   nir_setup_outputs();
4401e04c3fSmrg   nir_setup_uniforms();
4501e04c3fSmrg   nir_emit_system_values();
467ec681f3Smrg   last_scratch = ALIGN(nir->scratch_size, 4) * dispatch_width;
4701e04c3fSmrg
4801e04c3fSmrg   nir_emit_impl(nir_shader_get_entrypoint((nir_shader *)nir));
497ec681f3Smrg
507ec681f3Smrg   bld.emit(SHADER_OPCODE_HALT_TARGET);
5101e04c3fSmrg}
5201e04c3fSmrg
5301e04c3fSmrgvoid
5401e04c3fSmrgfs_visitor::nir_setup_outputs()
5501e04c3fSmrg{
5601e04c3fSmrg   if (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_FRAGMENT)
5701e04c3fSmrg      return;
5801e04c3fSmrg
5901e04c3fSmrg   unsigned vec4s[VARYING_SLOT_TESS_MAX] = { 0, };
6001e04c3fSmrg
6101e04c3fSmrg   /* Calculate the size of output registers in a separate pass, before
6201e04c3fSmrg    * allocating them.  With ARB_enhanced_layouts, multiple output variables
6301e04c3fSmrg    * may occupy the same slot, but have different type sizes.
6401e04c3fSmrg    */
657ec681f3Smrg   nir_foreach_shader_out_variable(var, nir) {
6601e04c3fSmrg      const int loc = var->data.driver_location;
6701e04c3fSmrg      const unsigned var_vec4s =
6801e04c3fSmrg         var->data.compact ? DIV_ROUND_UP(glsl_get_length(var->type), 4)
699f464c52Smaya                           : type_size_vec4(var->type, true);
7001e04c3fSmrg      vec4s[loc] = MAX2(vec4s[loc], var_vec4s);
7101e04c3fSmrg   }
7201e04c3fSmrg
7301e04c3fSmrg   for (unsigned loc = 0; loc < ARRAY_SIZE(vec4s);) {
7401e04c3fSmrg      if (vec4s[loc] == 0) {
7501e04c3fSmrg         loc++;
7601e04c3fSmrg         continue;
7701e04c3fSmrg      }
7801e04c3fSmrg
7901e04c3fSmrg      unsigned reg_size = vec4s[loc];
8001e04c3fSmrg
8101e04c3fSmrg      /* Check if there are any ranges that start within this range and extend
8201e04c3fSmrg       * past it. If so, include them in this allocation.
8301e04c3fSmrg       */
847ec681f3Smrg      for (unsigned i = 1; i < reg_size; i++) {
857ec681f3Smrg         assert(i + loc < ARRAY_SIZE(vec4s));
8601e04c3fSmrg         reg_size = MAX2(vec4s[i + loc] + i, reg_size);
877ec681f3Smrg      }
8801e04c3fSmrg
8901e04c3fSmrg      fs_reg reg = bld.vgrf(BRW_REGISTER_TYPE_F, 4 * reg_size);
907ec681f3Smrg      for (unsigned i = 0; i < reg_size; i++) {
917ec681f3Smrg         assert(loc + i < ARRAY_SIZE(outputs));
9201e04c3fSmrg         outputs[loc + i] = offset(reg, bld, 4 * i);
937ec681f3Smrg      }
9401e04c3fSmrg
9501e04c3fSmrg      loc += reg_size;
9601e04c3fSmrg   }
9701e04c3fSmrg}
9801e04c3fSmrg
9901e04c3fSmrgvoid
10001e04c3fSmrgfs_visitor::nir_setup_uniforms()
10101e04c3fSmrg{
10201e04c3fSmrg   /* Only the first compile gets to set up uniforms. */
10301e04c3fSmrg   if (push_constant_loc) {
10401e04c3fSmrg      assert(pull_constant_loc);
10501e04c3fSmrg      return;
10601e04c3fSmrg   }
10701e04c3fSmrg
10801e04c3fSmrg   uniforms = nir->num_uniforms / 4;
10901e04c3fSmrg
1107ec681f3Smrg   if ((stage == MESA_SHADER_COMPUTE || stage == MESA_SHADER_KERNEL) &&
1117ec681f3Smrg       devinfo->verx10 < 125) {
1127ec681f3Smrg      /* Add uniforms for builtins after regular NIR uniforms. */
11301e04c3fSmrg      assert(uniforms == prog_data->nr_params);
1147ec681f3Smrg
1157ec681f3Smrg      uint32_t *param;
1167ec681f3Smrg      if (nir->info.workgroup_size_variable &&
1177ec681f3Smrg          compiler->lower_variable_group_size) {
1187ec681f3Smrg         param = brw_stage_prog_data_add_params(prog_data, 3);
1197ec681f3Smrg         for (unsigned i = 0; i < 3; i++) {
1207ec681f3Smrg            param[i] = (BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_X + i);
1217ec681f3Smrg            group_size[i] = fs_reg(UNIFORM, uniforms++, BRW_REGISTER_TYPE_UD);
1227ec681f3Smrg         }
1237ec681f3Smrg      }
1247ec681f3Smrg
1257ec681f3Smrg      /* Subgroup ID must be the last uniform on the list.  This will make
1267ec681f3Smrg       * easier later to split between cross thread and per thread
1277ec681f3Smrg       * uniforms.
1287ec681f3Smrg       */
1297ec681f3Smrg      param = brw_stage_prog_data_add_params(prog_data, 1);
13001e04c3fSmrg      *param = BRW_PARAM_BUILTIN_SUBGROUP_ID;
13101e04c3fSmrg      subgroup_id = fs_reg(UNIFORM, uniforms++, BRW_REGISTER_TYPE_UD);
13201e04c3fSmrg   }
13301e04c3fSmrg}
13401e04c3fSmrg
13501e04c3fSmrgstatic bool
13601e04c3fSmrgemit_system_values_block(nir_block *block, fs_visitor *v)
13701e04c3fSmrg{
13801e04c3fSmrg   fs_reg *reg;
13901e04c3fSmrg
14001e04c3fSmrg   nir_foreach_instr(instr, block) {
14101e04c3fSmrg      if (instr->type != nir_instr_type_intrinsic)
14201e04c3fSmrg         continue;
14301e04c3fSmrg
14401e04c3fSmrg      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
14501e04c3fSmrg      switch (intrin->intrinsic) {
14601e04c3fSmrg      case nir_intrinsic_load_vertex_id:
14701e04c3fSmrg      case nir_intrinsic_load_base_vertex:
14801e04c3fSmrg         unreachable("should be lowered by nir_lower_system_values().");
14901e04c3fSmrg
15001e04c3fSmrg      case nir_intrinsic_load_vertex_id_zero_base:
15101e04c3fSmrg      case nir_intrinsic_load_is_indexed_draw:
15201e04c3fSmrg      case nir_intrinsic_load_first_vertex:
15301e04c3fSmrg      case nir_intrinsic_load_instance_id:
15401e04c3fSmrg      case nir_intrinsic_load_base_instance:
15501e04c3fSmrg      case nir_intrinsic_load_draw_id:
15601e04c3fSmrg         unreachable("should be lowered by brw_nir_lower_vs_inputs().");
15701e04c3fSmrg
15801e04c3fSmrg      case nir_intrinsic_load_invocation_id:
15901e04c3fSmrg         if (v->stage == MESA_SHADER_TESS_CTRL)
16001e04c3fSmrg            break;
16101e04c3fSmrg         assert(v->stage == MESA_SHADER_GEOMETRY);
16201e04c3fSmrg         reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
16301e04c3fSmrg         if (reg->file == BAD_FILE) {
16401e04c3fSmrg            const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
16501e04c3fSmrg            fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
16601e04c3fSmrg            fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
16701e04c3fSmrg            abld.SHR(iid, g1, brw_imm_ud(27u));
16801e04c3fSmrg            *reg = iid;
16901e04c3fSmrg         }
17001e04c3fSmrg         break;
17101e04c3fSmrg
17201e04c3fSmrg      case nir_intrinsic_load_sample_pos:
17301e04c3fSmrg         assert(v->stage == MESA_SHADER_FRAGMENT);
17401e04c3fSmrg         reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
17501e04c3fSmrg         if (reg->file == BAD_FILE)
17601e04c3fSmrg            *reg = *v->emit_samplepos_setup();
17701e04c3fSmrg         break;
17801e04c3fSmrg
17901e04c3fSmrg      case nir_intrinsic_load_sample_id:
18001e04c3fSmrg         assert(v->stage == MESA_SHADER_FRAGMENT);
18101e04c3fSmrg         reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
18201e04c3fSmrg         if (reg->file == BAD_FILE)
18301e04c3fSmrg            *reg = *v->emit_sampleid_setup();
18401e04c3fSmrg         break;
18501e04c3fSmrg
18601e04c3fSmrg      case nir_intrinsic_load_sample_mask_in:
18701e04c3fSmrg         assert(v->stage == MESA_SHADER_FRAGMENT);
1887ec681f3Smrg         assert(v->devinfo->ver >= 7);
18901e04c3fSmrg         reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
19001e04c3fSmrg         if (reg->file == BAD_FILE)
19101e04c3fSmrg            *reg = *v->emit_samplemaskin_setup();
19201e04c3fSmrg         break;
19301e04c3fSmrg
1947ec681f3Smrg      case nir_intrinsic_load_workgroup_id:
1957ec681f3Smrg         assert(v->stage == MESA_SHADER_COMPUTE ||
1967ec681f3Smrg                v->stage == MESA_SHADER_KERNEL);
1977ec681f3Smrg         reg = &v->nir_system_values[SYSTEM_VALUE_WORKGROUP_ID];
19801e04c3fSmrg         if (reg->file == BAD_FILE)
19901e04c3fSmrg            *reg = *v->emit_cs_work_group_id_setup();
20001e04c3fSmrg         break;
20101e04c3fSmrg
20201e04c3fSmrg      case nir_intrinsic_load_helper_invocation:
20301e04c3fSmrg         assert(v->stage == MESA_SHADER_FRAGMENT);
20401e04c3fSmrg         reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
20501e04c3fSmrg         if (reg->file == BAD_FILE) {
20601e04c3fSmrg            const fs_builder abld =
20701e04c3fSmrg               v->bld.annotate("gl_HelperInvocation", NULL);
20801e04c3fSmrg
2097ec681f3Smrg            /* On Gfx6+ (gl_HelperInvocation is only exposed on Gfx7+) the
21001e04c3fSmrg             * pixel mask is in g1.7 of the thread payload.
21101e04c3fSmrg             *
21201e04c3fSmrg             * We move the per-channel pixel enable bit to the low bit of each
21301e04c3fSmrg             * channel by shifting the byte containing the pixel mask by the
21401e04c3fSmrg             * vector immediate 0x76543210UV.
21501e04c3fSmrg             *
21601e04c3fSmrg             * The region of <1,8,0> reads only 1 byte (the pixel masks for
21701e04c3fSmrg             * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
21801e04c3fSmrg             * masks for 2 and 3) in SIMD16.
21901e04c3fSmrg             */
22001e04c3fSmrg            fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
22101e04c3fSmrg
22201e04c3fSmrg            for (unsigned i = 0; i < DIV_ROUND_UP(v->dispatch_width, 16); i++) {
22301e04c3fSmrg               const fs_builder hbld = abld.group(MIN2(16, v->dispatch_width), i);
22401e04c3fSmrg               hbld.SHR(offset(shifted, hbld, i),
22501e04c3fSmrg                        stride(retype(brw_vec1_grf(1 + i, 7),
22601e04c3fSmrg                                      BRW_REGISTER_TYPE_UB),
22701e04c3fSmrg                               1, 8, 0),
22801e04c3fSmrg                        brw_imm_v(0x76543210));
22901e04c3fSmrg            }
23001e04c3fSmrg
23101e04c3fSmrg            /* A set bit in the pixel mask means the channel is enabled, but
23201e04c3fSmrg             * that is the opposite of gl_HelperInvocation so we need to invert
23301e04c3fSmrg             * the mask.
23401e04c3fSmrg             *
2357ec681f3Smrg             * The negate source-modifier bit of logical instructions on Gfx8+
23601e04c3fSmrg             * performs 1's complement negation, so we can use that instead of
23701e04c3fSmrg             * a NOT instruction.
23801e04c3fSmrg             */
23901e04c3fSmrg            fs_reg inverted = negate(shifted);
2407ec681f3Smrg            if (v->devinfo->ver < 8) {
24101e04c3fSmrg               inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
24201e04c3fSmrg               abld.NOT(inverted, shifted);
24301e04c3fSmrg            }
24401e04c3fSmrg
24501e04c3fSmrg            /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
24601e04c3fSmrg             * with 1 and negating.
24701e04c3fSmrg             */
24801e04c3fSmrg            fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
24901e04c3fSmrg            abld.AND(anded, inverted, brw_imm_uw(1));
25001e04c3fSmrg
25101e04c3fSmrg            fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
25201e04c3fSmrg            abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
25301e04c3fSmrg            *reg = dst;
25401e04c3fSmrg         }
25501e04c3fSmrg         break;
25601e04c3fSmrg
2577ec681f3Smrg      case nir_intrinsic_load_frag_shading_rate:
2587ec681f3Smrg         reg = &v->nir_system_values[SYSTEM_VALUE_FRAG_SHADING_RATE];
2597ec681f3Smrg         if (reg->file == BAD_FILE)
2607ec681f3Smrg            *reg = *v->emit_shading_rate_setup();
2617ec681f3Smrg         break;
2627ec681f3Smrg
26301e04c3fSmrg      default:
26401e04c3fSmrg         break;
26501e04c3fSmrg      }
26601e04c3fSmrg   }
26701e04c3fSmrg
26801e04c3fSmrg   return true;
26901e04c3fSmrg}
27001e04c3fSmrg
27101e04c3fSmrgvoid
27201e04c3fSmrgfs_visitor::nir_emit_system_values()
27301e04c3fSmrg{
27401e04c3fSmrg   nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
27501e04c3fSmrg   for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
27601e04c3fSmrg      nir_system_values[i] = fs_reg();
27701e04c3fSmrg   }
27801e04c3fSmrg
27901e04c3fSmrg   /* Always emit SUBGROUP_INVOCATION.  Dead code will clean it up if we
28001e04c3fSmrg    * never end up using it.
28101e04c3fSmrg    */
28201e04c3fSmrg   {
28301e04c3fSmrg      const fs_builder abld = bld.annotate("gl_SubgroupInvocation", NULL);
28401e04c3fSmrg      fs_reg &reg = nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION];
28501e04c3fSmrg      reg = abld.vgrf(BRW_REGISTER_TYPE_UW);
28601e04c3fSmrg
28701e04c3fSmrg      const fs_builder allbld8 = abld.group(8, 0).exec_all();
28801e04c3fSmrg      allbld8.MOV(reg, brw_imm_v(0x76543210));
28901e04c3fSmrg      if (dispatch_width > 8)
29001e04c3fSmrg         allbld8.ADD(byte_offset(reg, 16), reg, brw_imm_uw(8u));
29101e04c3fSmrg      if (dispatch_width > 16) {
29201e04c3fSmrg         const fs_builder allbld16 = abld.group(16, 0).exec_all();
29301e04c3fSmrg         allbld16.ADD(byte_offset(reg, 32), reg, brw_imm_uw(16u));
29401e04c3fSmrg      }
29501e04c3fSmrg   }
29601e04c3fSmrg
29701e04c3fSmrg   nir_function_impl *impl = nir_shader_get_entrypoint((nir_shader *)nir);
29801e04c3fSmrg   nir_foreach_block(block, impl)
29901e04c3fSmrg      emit_system_values_block(block, this);
30001e04c3fSmrg}
30101e04c3fSmrg
30201e04c3fSmrgvoid
30301e04c3fSmrgfs_visitor::nir_emit_impl(nir_function_impl *impl)
30401e04c3fSmrg{
30501e04c3fSmrg   nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
30601e04c3fSmrg   for (unsigned i = 0; i < impl->reg_alloc; i++) {
30701e04c3fSmrg      nir_locals[i] = fs_reg();
30801e04c3fSmrg   }
30901e04c3fSmrg
31001e04c3fSmrg   foreach_list_typed(nir_register, reg, node, &impl->registers) {
31101e04c3fSmrg      unsigned array_elems =
31201e04c3fSmrg         reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
31301e04c3fSmrg      unsigned size = array_elems * reg->num_components;
3149f464c52Smaya      const brw_reg_type reg_type = reg->bit_size == 8 ? BRW_REGISTER_TYPE_B :
31501e04c3fSmrg         brw_reg_type_from_bit_size(reg->bit_size, BRW_REGISTER_TYPE_F);
31601e04c3fSmrg      nir_locals[reg->index] = bld.vgrf(reg_type, size);
31701e04c3fSmrg   }
31801e04c3fSmrg
31901e04c3fSmrg   nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
32001e04c3fSmrg                             impl->ssa_alloc);
32101e04c3fSmrg
32201e04c3fSmrg   nir_emit_cf_list(&impl->body);
32301e04c3fSmrg}
32401e04c3fSmrg
32501e04c3fSmrgvoid
32601e04c3fSmrgfs_visitor::nir_emit_cf_list(exec_list *list)
32701e04c3fSmrg{
32801e04c3fSmrg   exec_list_validate(list);
32901e04c3fSmrg   foreach_list_typed(nir_cf_node, node, node, list) {
33001e04c3fSmrg      switch (node->type) {
33101e04c3fSmrg      case nir_cf_node_if:
33201e04c3fSmrg         nir_emit_if(nir_cf_node_as_if(node));
33301e04c3fSmrg         break;
33401e04c3fSmrg
33501e04c3fSmrg      case nir_cf_node_loop:
33601e04c3fSmrg         nir_emit_loop(nir_cf_node_as_loop(node));
33701e04c3fSmrg         break;
33801e04c3fSmrg
33901e04c3fSmrg      case nir_cf_node_block:
34001e04c3fSmrg         nir_emit_block(nir_cf_node_as_block(node));
34101e04c3fSmrg         break;
34201e04c3fSmrg
34301e04c3fSmrg      default:
34401e04c3fSmrg         unreachable("Invalid CFG node block");
34501e04c3fSmrg      }
34601e04c3fSmrg   }
34701e04c3fSmrg}
34801e04c3fSmrg
34901e04c3fSmrgvoid
35001e04c3fSmrgfs_visitor::nir_emit_if(nir_if *if_stmt)
35101e04c3fSmrg{
3529f464c52Smaya   bool invert;
3539f464c52Smaya   fs_reg cond_reg;
3549f464c52Smaya
3559f464c52Smaya   /* If the condition has the form !other_condition, use other_condition as
3569f464c52Smaya    * the source, but invert the predicate on the if instruction.
3579f464c52Smaya    */
3589f464c52Smaya   nir_alu_instr *cond = nir_src_as_alu_instr(if_stmt->condition);
3599f464c52Smaya   if (cond != NULL && cond->op == nir_op_inot) {
3609f464c52Smaya      invert = true;
3619f464c52Smaya      cond_reg = get_nir_src(cond->src[0].src);
3627ec681f3Smrg      cond_reg = offset(cond_reg, bld, cond->src[0].swizzle[0]);
3639f464c52Smaya   } else {
3649f464c52Smaya      invert = false;
3659f464c52Smaya      cond_reg = get_nir_src(if_stmt->condition);
3669f464c52Smaya   }
3679f464c52Smaya
36801e04c3fSmrg   /* first, put the condition into f0 */
36901e04c3fSmrg   fs_inst *inst = bld.MOV(bld.null_reg_d(),
3709f464c52Smaya                           retype(cond_reg, BRW_REGISTER_TYPE_D));
37101e04c3fSmrg   inst->conditional_mod = BRW_CONDITIONAL_NZ;
37201e04c3fSmrg
3739f464c52Smaya   bld.IF(BRW_PREDICATE_NORMAL)->predicate_inverse = invert;
37401e04c3fSmrg
37501e04c3fSmrg   nir_emit_cf_list(&if_stmt->then_list);
37601e04c3fSmrg
3779f464c52Smaya   if (!nir_cf_list_is_empty_block(&if_stmt->else_list)) {
3789f464c52Smaya      bld.emit(BRW_OPCODE_ELSE);
3799f464c52Smaya      nir_emit_cf_list(&if_stmt->else_list);
3809f464c52Smaya   }
38101e04c3fSmrg
38201e04c3fSmrg   bld.emit(BRW_OPCODE_ENDIF);
38301e04c3fSmrg
3847ec681f3Smrg   if (devinfo->ver < 7)
38501e04c3fSmrg      limit_dispatch_width(16, "Non-uniform control flow unsupported "
38601e04c3fSmrg                           "in SIMD32 mode.");
38701e04c3fSmrg}
38801e04c3fSmrg
38901e04c3fSmrgvoid
39001e04c3fSmrgfs_visitor::nir_emit_loop(nir_loop *loop)
39101e04c3fSmrg{
39201e04c3fSmrg   bld.emit(BRW_OPCODE_DO);
39301e04c3fSmrg
39401e04c3fSmrg   nir_emit_cf_list(&loop->body);
39501e04c3fSmrg
39601e04c3fSmrg   bld.emit(BRW_OPCODE_WHILE);
39701e04c3fSmrg
3987ec681f3Smrg   if (devinfo->ver < 7)
39901e04c3fSmrg      limit_dispatch_width(16, "Non-uniform control flow unsupported "
40001e04c3fSmrg                           "in SIMD32 mode.");
40101e04c3fSmrg}
40201e04c3fSmrg
40301e04c3fSmrgvoid
40401e04c3fSmrgfs_visitor::nir_emit_block(nir_block *block)
40501e04c3fSmrg{
40601e04c3fSmrg   nir_foreach_instr(instr, block) {
40701e04c3fSmrg      nir_emit_instr(instr);
40801e04c3fSmrg   }
40901e04c3fSmrg}
41001e04c3fSmrg
41101e04c3fSmrgvoid
41201e04c3fSmrgfs_visitor::nir_emit_instr(nir_instr *instr)
41301e04c3fSmrg{
41401e04c3fSmrg   const fs_builder abld = bld.annotate(NULL, instr);
41501e04c3fSmrg
41601e04c3fSmrg   switch (instr->type) {
41701e04c3fSmrg   case nir_instr_type_alu:
4187ec681f3Smrg      nir_emit_alu(abld, nir_instr_as_alu(instr), true);
41901e04c3fSmrg      break;
42001e04c3fSmrg
42101e04c3fSmrg   case nir_instr_type_deref:
4229f464c52Smaya      unreachable("All derefs should've been lowered");
42301e04c3fSmrg      break;
42401e04c3fSmrg
42501e04c3fSmrg   case nir_instr_type_intrinsic:
42601e04c3fSmrg      switch (stage) {
42701e04c3fSmrg      case MESA_SHADER_VERTEX:
42801e04c3fSmrg         nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
42901e04c3fSmrg         break;
43001e04c3fSmrg      case MESA_SHADER_TESS_CTRL:
43101e04c3fSmrg         nir_emit_tcs_intrinsic(abld, nir_instr_as_intrinsic(instr));
43201e04c3fSmrg         break;
43301e04c3fSmrg      case MESA_SHADER_TESS_EVAL:
43401e04c3fSmrg         nir_emit_tes_intrinsic(abld, nir_instr_as_intrinsic(instr));
43501e04c3fSmrg         break;
43601e04c3fSmrg      case MESA_SHADER_GEOMETRY:
43701e04c3fSmrg         nir_emit_gs_intrinsic(abld, nir_instr_as_intrinsic(instr));
43801e04c3fSmrg         break;
43901e04c3fSmrg      case MESA_SHADER_FRAGMENT:
44001e04c3fSmrg         nir_emit_fs_intrinsic(abld, nir_instr_as_intrinsic(instr));
44101e04c3fSmrg         break;
44201e04c3fSmrg      case MESA_SHADER_COMPUTE:
4437ec681f3Smrg      case MESA_SHADER_KERNEL:
44401e04c3fSmrg         nir_emit_cs_intrinsic(abld, nir_instr_as_intrinsic(instr));
44501e04c3fSmrg         break;
4467ec681f3Smrg      case MESA_SHADER_RAYGEN:
4477ec681f3Smrg      case MESA_SHADER_ANY_HIT:
4487ec681f3Smrg      case MESA_SHADER_CLOSEST_HIT:
4497ec681f3Smrg      case MESA_SHADER_MISS:
4507ec681f3Smrg      case MESA_SHADER_INTERSECTION:
4517ec681f3Smrg      case MESA_SHADER_CALLABLE:
4527ec681f3Smrg         nir_emit_bs_intrinsic(abld, nir_instr_as_intrinsic(instr));
4537ec681f3Smrg         break;
45401e04c3fSmrg      default:
45501e04c3fSmrg         unreachable("unsupported shader stage");
45601e04c3fSmrg      }
45701e04c3fSmrg      break;
45801e04c3fSmrg
45901e04c3fSmrg   case nir_instr_type_tex:
46001e04c3fSmrg      nir_emit_texture(abld, nir_instr_as_tex(instr));
46101e04c3fSmrg      break;
46201e04c3fSmrg
46301e04c3fSmrg   case nir_instr_type_load_const:
46401e04c3fSmrg      nir_emit_load_const(abld, nir_instr_as_load_const(instr));
46501e04c3fSmrg      break;
46601e04c3fSmrg
46701e04c3fSmrg   case nir_instr_type_ssa_undef:
46801e04c3fSmrg      /* We create a new VGRF for undefs on every use (by handling
46901e04c3fSmrg       * them in get_nir_src()), rather than for each definition.
47001e04c3fSmrg       * This helps register coalescing eliminate MOVs from undef.
47101e04c3fSmrg       */
47201e04c3fSmrg      break;
47301e04c3fSmrg
47401e04c3fSmrg   case nir_instr_type_jump:
47501e04c3fSmrg      nir_emit_jump(abld, nir_instr_as_jump(instr));
47601e04c3fSmrg      break;
47701e04c3fSmrg
47801e04c3fSmrg   default:
47901e04c3fSmrg      unreachable("unknown instruction type");
48001e04c3fSmrg   }
48101e04c3fSmrg}
48201e04c3fSmrg
48301e04c3fSmrg/**
48401e04c3fSmrg * Recognizes a parent instruction of nir_op_extract_* and changes the type to
48501e04c3fSmrg * match instr.
48601e04c3fSmrg */
48701e04c3fSmrgbool
48801e04c3fSmrgfs_visitor::optimize_extract_to_float(nir_alu_instr *instr,
48901e04c3fSmrg                                      const fs_reg &result)
49001e04c3fSmrg{
49101e04c3fSmrg   if (!instr->src[0].src.is_ssa ||
49201e04c3fSmrg       !instr->src[0].src.ssa->parent_instr)
49301e04c3fSmrg      return false;
49401e04c3fSmrg
49501e04c3fSmrg   if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
49601e04c3fSmrg      return false;
49701e04c3fSmrg
49801e04c3fSmrg   nir_alu_instr *src0 =
49901e04c3fSmrg      nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
50001e04c3fSmrg
50101e04c3fSmrg   if (src0->op != nir_op_extract_u8 && src0->op != nir_op_extract_u16 &&
50201e04c3fSmrg       src0->op != nir_op_extract_i8 && src0->op != nir_op_extract_i16)
50301e04c3fSmrg      return false;
50401e04c3fSmrg
5059f464c52Smaya   unsigned element = nir_src_as_uint(src0->src[1].src);
50601e04c3fSmrg
50701e04c3fSmrg   /* Element type to extract.*/
50801e04c3fSmrg   const brw_reg_type type = brw_int_type(
50901e04c3fSmrg      src0->op == nir_op_extract_u16 || src0->op == nir_op_extract_i16 ? 2 : 1,
51001e04c3fSmrg      src0->op == nir_op_extract_i16 || src0->op == nir_op_extract_i8);
51101e04c3fSmrg
51201e04c3fSmrg   fs_reg op0 = get_nir_src(src0->src[0].src);
51301e04c3fSmrg   op0.type = brw_type_for_nir_type(devinfo,
51401e04c3fSmrg      (nir_alu_type)(nir_op_infos[src0->op].input_types[0] |
51501e04c3fSmrg                     nir_src_bit_size(src0->src[0].src)));
51601e04c3fSmrg   op0 = offset(op0, bld, src0->src[0].swizzle[0]);
51701e04c3fSmrg
5187ec681f3Smrg   bld.MOV(result, subscript(op0, type, element));
51901e04c3fSmrg   return true;
52001e04c3fSmrg}
52101e04c3fSmrg
52201e04c3fSmrgbool
52301e04c3fSmrgfs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
52401e04c3fSmrg                                         const fs_reg &result)
52501e04c3fSmrg{
5269f464c52Smaya   nir_intrinsic_instr *src0 = nir_src_as_intrinsic(instr->src[0].src);
5279f464c52Smaya   if (src0 == NULL || src0->intrinsic != nir_intrinsic_load_front_face)
52801e04c3fSmrg      return false;
52901e04c3fSmrg
5309f464c52Smaya   if (!nir_src_is_const(instr->src[1].src) ||
5319f464c52Smaya       !nir_src_is_const(instr->src[2].src))
53201e04c3fSmrg      return false;
53301e04c3fSmrg
5349f464c52Smaya   const float value1 = nir_src_as_float(instr->src[1].src);
5359f464c52Smaya   const float value2 = nir_src_as_float(instr->src[2].src);
5369f464c52Smaya   if (fabsf(value1) != 1.0f || fabsf(value2) != 1.0f)
53701e04c3fSmrg      return false;
53801e04c3fSmrg
5399f464c52Smaya   /* nir_opt_algebraic should have gotten rid of bcsel(b, a, a) */
5409f464c52Smaya   assert(value1 == -value2);
54101e04c3fSmrg
54201e04c3fSmrg   fs_reg tmp = vgrf(glsl_type::int_type);
54301e04c3fSmrg
5447ec681f3Smrg   if (devinfo->ver >= 12) {
5457ec681f3Smrg      /* Bit 15 of g1.1 is 0 if the polygon is front facing. */
5467ec681f3Smrg      fs_reg g1 = fs_reg(retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_W));
5477ec681f3Smrg
5487ec681f3Smrg      /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
5497ec681f3Smrg       *
5507ec681f3Smrg       *    or(8)  tmp.1<2>W  g1.1<0,1,0>W  0x00003f80W
5517ec681f3Smrg       *    and(8) dst<1>D    tmp<8,8,1>D   0xbf800000D
5527ec681f3Smrg       *
5537ec681f3Smrg       * and negate g1.1<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
5547ec681f3Smrg       */
5557ec681f3Smrg      if (value1 == -1.0f)
5567ec681f3Smrg         g1.negate = true;
5577ec681f3Smrg
5587ec681f3Smrg      bld.OR(subscript(tmp, BRW_REGISTER_TYPE_W, 1),
5597ec681f3Smrg             g1, brw_imm_uw(0x3f80));
5607ec681f3Smrg   } else if (devinfo->ver >= 6) {
56101e04c3fSmrg      /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
56201e04c3fSmrg      fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
56301e04c3fSmrg
56401e04c3fSmrg      /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
56501e04c3fSmrg       *
56601e04c3fSmrg       *    or(8)  tmp.1<2>W  g0.0<0,1,0>W  0x00003f80W
56701e04c3fSmrg       *    and(8) dst<1>D    tmp<8,8,1>D   0xbf800000D
56801e04c3fSmrg       *
56901e04c3fSmrg       * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
57001e04c3fSmrg       *
57101e04c3fSmrg       * This negation looks like it's safe in practice, because bits 0:4 will
57201e04c3fSmrg       * surely be TRIANGLES
57301e04c3fSmrg       */
57401e04c3fSmrg
5759f464c52Smaya      if (value1 == -1.0f) {
57601e04c3fSmrg         g0.negate = true;
57701e04c3fSmrg      }
57801e04c3fSmrg
57901e04c3fSmrg      bld.OR(subscript(tmp, BRW_REGISTER_TYPE_W, 1),
58001e04c3fSmrg             g0, brw_imm_uw(0x3f80));
58101e04c3fSmrg   } else {
58201e04c3fSmrg      /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
58301e04c3fSmrg      fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
58401e04c3fSmrg
58501e04c3fSmrg      /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
58601e04c3fSmrg       *
58701e04c3fSmrg       *    or(8)  tmp<1>D  g1.6<0,1,0>D  0x3f800000D
58801e04c3fSmrg       *    and(8) dst<1>D  tmp<8,8,1>D   0xbf800000D
58901e04c3fSmrg       *
59001e04c3fSmrg       * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
59101e04c3fSmrg       *
59201e04c3fSmrg       * This negation looks like it's safe in practice, because bits 0:4 will
59301e04c3fSmrg       * surely be TRIANGLES
59401e04c3fSmrg       */
59501e04c3fSmrg
5969f464c52Smaya      if (value1 == -1.0f) {
59701e04c3fSmrg         g1_6.negate = true;
59801e04c3fSmrg      }
59901e04c3fSmrg
60001e04c3fSmrg      bld.OR(tmp, g1_6, brw_imm_d(0x3f800000));
60101e04c3fSmrg   }
60201e04c3fSmrg   bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, brw_imm_d(0xbf800000));
60301e04c3fSmrg
60401e04c3fSmrg   return true;
60501e04c3fSmrg}
60601e04c3fSmrg
60701e04c3fSmrgstatic void
60801e04c3fSmrgemit_find_msb_using_lzd(const fs_builder &bld,
60901e04c3fSmrg                        const fs_reg &result,
61001e04c3fSmrg                        const fs_reg &src,
61101e04c3fSmrg                        bool is_signed)
61201e04c3fSmrg{
61301e04c3fSmrg   fs_inst *inst;
61401e04c3fSmrg   fs_reg temp = src;
61501e04c3fSmrg
61601e04c3fSmrg   if (is_signed) {
61701e04c3fSmrg      /* LZD of an absolute value source almost always does the right
61801e04c3fSmrg       * thing.  There are two problem values:
61901e04c3fSmrg       *
62001e04c3fSmrg       * * 0x80000000.  Since abs(0x80000000) == 0x80000000, LZD returns
62101e04c3fSmrg       *   0.  However, findMSB(int(0x80000000)) == 30.
62201e04c3fSmrg       *
62301e04c3fSmrg       * * 0xffffffff.  Since abs(0xffffffff) == 1, LZD returns
62401e04c3fSmrg       *   31.  Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
62501e04c3fSmrg       *
62601e04c3fSmrg       *    For a value of zero or negative one, -1 will be returned.
62701e04c3fSmrg       *
62801e04c3fSmrg       * * Negative powers of two.  LZD(abs(-(1<<x))) returns x, but
62901e04c3fSmrg       *   findMSB(-(1<<x)) should return x-1.
63001e04c3fSmrg       *
63101e04c3fSmrg       * For all negative number cases, including 0x80000000 and
63201e04c3fSmrg       * 0xffffffff, the correct value is obtained from LZD if instead of
63301e04c3fSmrg       * negating the (already negative) value the logical-not is used.  A
63401e04c3fSmrg       * conditonal logical-not can be achieved in two instructions.
63501e04c3fSmrg       */
63601e04c3fSmrg      temp = bld.vgrf(BRW_REGISTER_TYPE_D);
63701e04c3fSmrg
63801e04c3fSmrg      bld.ASR(temp, src, brw_imm_d(31));
63901e04c3fSmrg      bld.XOR(temp, temp, src);
64001e04c3fSmrg   }
64101e04c3fSmrg
64201e04c3fSmrg   bld.LZD(retype(result, BRW_REGISTER_TYPE_UD),
64301e04c3fSmrg           retype(temp, BRW_REGISTER_TYPE_UD));
64401e04c3fSmrg
64501e04c3fSmrg   /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
64601e04c3fSmrg    * from the LSB side. Subtract the result from 31 to convert the MSB
64701e04c3fSmrg    * count into an LSB count.  If no bits are set, LZD will return 32.
64801e04c3fSmrg    * 31-32 = -1, which is exactly what findMSB() is supposed to return.
64901e04c3fSmrg    */
65001e04c3fSmrg   inst = bld.ADD(result, retype(result, BRW_REGISTER_TYPE_D), brw_imm_d(31));
65101e04c3fSmrg   inst->src[0].negate = true;
65201e04c3fSmrg}
65301e04c3fSmrg
65401e04c3fSmrgstatic brw_rnd_mode
65501e04c3fSmrgbrw_rnd_mode_from_nir_op (const nir_op op) {
65601e04c3fSmrg   switch (op) {
65701e04c3fSmrg   case nir_op_f2f16_rtz:
65801e04c3fSmrg      return BRW_RND_MODE_RTZ;
65901e04c3fSmrg   case nir_op_f2f16_rtne:
66001e04c3fSmrg      return BRW_RND_MODE_RTNE;
66101e04c3fSmrg   default:
66201e04c3fSmrg      unreachable("Operation doesn't support rounding mode");
66301e04c3fSmrg   }
66401e04c3fSmrg}
66501e04c3fSmrg
6667ec681f3Smrgstatic brw_rnd_mode
6677ec681f3Smrgbrw_rnd_mode_from_execution_mode(unsigned execution_mode)
6687ec681f3Smrg{
6697ec681f3Smrg   if (nir_has_any_rounding_mode_rtne(execution_mode))
6707ec681f3Smrg      return BRW_RND_MODE_RTNE;
6717ec681f3Smrg   if (nir_has_any_rounding_mode_rtz(execution_mode))
6727ec681f3Smrg      return BRW_RND_MODE_RTZ;
6737ec681f3Smrg   return BRW_RND_MODE_UNSPECIFIED;
6747ec681f3Smrg}
6757ec681f3Smrg
6769f464c52Smayafs_reg
6779f464c52Smayafs_visitor::prepare_alu_destination_and_sources(const fs_builder &bld,
6789f464c52Smaya                                                nir_alu_instr *instr,
6799f464c52Smaya                                                fs_reg *op,
6809f464c52Smaya                                                bool need_dest)
68101e04c3fSmrg{
6829f464c52Smaya   fs_reg result =
6839f464c52Smaya      need_dest ? get_nir_dest(instr->dest.dest) : bld.null_reg_ud();
68401e04c3fSmrg
68501e04c3fSmrg   result.type = brw_type_for_nir_type(devinfo,
68601e04c3fSmrg      (nir_alu_type)(nir_op_infos[instr->op].output_type |
68701e04c3fSmrg                     nir_dest_bit_size(instr->dest.dest)));
68801e04c3fSmrg
6897ec681f3Smrg   assert(!instr->dest.saturate);
6907ec681f3Smrg
69101e04c3fSmrg   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
6927ec681f3Smrg      /* We don't lower to source modifiers so they should not exist. */
6937ec681f3Smrg      assert(!instr->src[i].abs);
6947ec681f3Smrg      assert(!instr->src[i].negate);
6957ec681f3Smrg
69601e04c3fSmrg      op[i] = get_nir_src(instr->src[i].src);
69701e04c3fSmrg      op[i].type = brw_type_for_nir_type(devinfo,
69801e04c3fSmrg         (nir_alu_type)(nir_op_infos[instr->op].input_types[i] |
69901e04c3fSmrg                        nir_src_bit_size(instr->src[i].src)));
70001e04c3fSmrg   }
70101e04c3fSmrg
7029f464c52Smaya   /* Move and vecN instrutions may still be vectored.  Return the raw,
7039f464c52Smaya    * vectored source and destination so that fs_visitor::nir_emit_alu can
7049f464c52Smaya    * handle it.  Other callers should not have to handle these kinds of
7059f464c52Smaya    * instructions.
70601e04c3fSmrg    */
7079f464c52Smaya   switch (instr->op) {
7087ec681f3Smrg   case nir_op_mov:
7099f464c52Smaya   case nir_op_vec2:
7109f464c52Smaya   case nir_op_vec3:
7119f464c52Smaya   case nir_op_vec4:
7127ec681f3Smrg   case nir_op_vec8:
7137ec681f3Smrg   case nir_op_vec16:
7149f464c52Smaya      return result;
7159f464c52Smaya   default:
7169f464c52Smaya      break;
7179f464c52Smaya   }
7189f464c52Smaya
7199f464c52Smaya   /* At this point, we have dealt with any instruction that operates on
7209f464c52Smaya    * more than a single channel.  Therefore, we can just adjust the source
7219f464c52Smaya    * and destination registers for that channel and emit the instruction.
7229f464c52Smaya    */
7239f464c52Smaya   unsigned channel = 0;
7249f464c52Smaya   if (nir_op_infos[instr->op].output_size == 0) {
7259f464c52Smaya      /* Since NIR is doing the scalarizing for us, we should only ever see
7269f464c52Smaya       * vectorized operations with a single channel.
7279f464c52Smaya       */
7289f464c52Smaya      assert(util_bitcount(instr->dest.write_mask) == 1);
7299f464c52Smaya      channel = ffs(instr->dest.write_mask) - 1;
7309f464c52Smaya
7319f464c52Smaya      result = offset(result, bld, channel);
7329f464c52Smaya   }
7339f464c52Smaya
7349f464c52Smaya   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
7359f464c52Smaya      assert(nir_op_infos[instr->op].input_sizes[i] < 2);
7369f464c52Smaya      op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
7379f464c52Smaya   }
7389f464c52Smaya
7399f464c52Smaya   return result;
7409f464c52Smaya}
7419f464c52Smaya
7429f464c52Smayavoid
7439f464c52Smayafs_visitor::resolve_inot_sources(const fs_builder &bld, nir_alu_instr *instr,
7449f464c52Smaya                                 fs_reg *op)
7459f464c52Smaya{
7469f464c52Smaya   for (unsigned i = 0; i < 2; i++) {
7479f464c52Smaya      nir_alu_instr *inot_instr = nir_src_as_alu_instr(instr->src[i].src);
7489f464c52Smaya
7497ec681f3Smrg      if (inot_instr != NULL && inot_instr->op == nir_op_inot) {
7509f464c52Smaya         /* The source of the inot is now the source of instr. */
7519f464c52Smaya         prepare_alu_destination_and_sources(bld, inot_instr, &op[i], false);
7529f464c52Smaya
7539f464c52Smaya         assert(!op[i].negate);
7549f464c52Smaya         op[i].negate = true;
7559f464c52Smaya      } else {
7569f464c52Smaya         op[i] = resolve_source_modifiers(op[i]);
7579f464c52Smaya      }
7589f464c52Smaya   }
7599f464c52Smaya}
7609f464c52Smaya
7619f464c52Smayabool
7629f464c52Smayafs_visitor::try_emit_b2fi_of_inot(const fs_builder &bld,
7639f464c52Smaya                                  fs_reg result,
7649f464c52Smaya                                  nir_alu_instr *instr)
7659f464c52Smaya{
7667ec681f3Smrg   if (devinfo->ver < 6 || devinfo->ver >= 12)
7679f464c52Smaya      return false;
7689f464c52Smaya
7699f464c52Smaya   nir_alu_instr *inot_instr = nir_src_as_alu_instr(instr->src[0].src);
7709f464c52Smaya
7719f464c52Smaya   if (inot_instr == NULL || inot_instr->op != nir_op_inot)
7729f464c52Smaya      return false;
7739f464c52Smaya
7749f464c52Smaya   /* HF is also possible as a destination on BDW+.  For nir_op_b2i, the set
7759f464c52Smaya    * of valid size-changing combinations is a bit more complex.
7769f464c52Smaya    *
7779f464c52Smaya    * The source restriction is just because I was lazy about generating the
7789f464c52Smaya    * constant below.
7799f464c52Smaya    */
7809f464c52Smaya   if (nir_dest_bit_size(instr->dest.dest) != 32 ||
7819f464c52Smaya       nir_src_bit_size(inot_instr->src[0].src) != 32)
7829f464c52Smaya      return false;
7839f464c52Smaya
7849f464c52Smaya   /* b2[fi](inot(a)) maps a=0 => 1, a=-1 => 0.  Since a can only be 0 or -1,
7859f464c52Smaya    * this is float(1 + a).
7869f464c52Smaya    */
7879f464c52Smaya   fs_reg op;
7889f464c52Smaya
7899f464c52Smaya   prepare_alu_destination_and_sources(bld, inot_instr, &op, false);
7909f464c52Smaya
7919f464c52Smaya   /* Ignore the saturate modifier, if there is one.  The result of the
7929f464c52Smaya    * arithmetic can only be 0 or 1, so the clamping will do nothing anyway.
7939f464c52Smaya    */
7949f464c52Smaya   bld.ADD(result, op, brw_imm_d(1));
7959f464c52Smaya
7969f464c52Smaya   return true;
7979f464c52Smaya}
7989f464c52Smaya
7999f464c52Smaya/**
8009f464c52Smaya * Emit code for nir_op_fsign possibly fused with a nir_op_fmul
8019f464c52Smaya *
8029f464c52Smaya * If \c instr is not the \c nir_op_fsign, then \c fsign_src is the index of
8039f464c52Smaya * the source of \c instr that is a \c nir_op_fsign.
8049f464c52Smaya */
8059f464c52Smayavoid
8069f464c52Smayafs_visitor::emit_fsign(const fs_builder &bld, const nir_alu_instr *instr,
8079f464c52Smaya                       fs_reg result, fs_reg *op, unsigned fsign_src)
8089f464c52Smaya{
8099f464c52Smaya   fs_inst *inst;
8109f464c52Smaya
8119f464c52Smaya   assert(instr->op == nir_op_fsign || instr->op == nir_op_fmul);
8129f464c52Smaya   assert(fsign_src < nir_op_infos[instr->op].num_inputs);
8139f464c52Smaya
8149f464c52Smaya   if (instr->op != nir_op_fsign) {
8159f464c52Smaya      const nir_alu_instr *const fsign_instr =
8169f464c52Smaya         nir_src_as_alu_instr(instr->src[fsign_src].src);
8179f464c52Smaya
8189f464c52Smaya      /* op[fsign_src] has the nominal result of the fsign, and op[1 -
8199f464c52Smaya       * fsign_src] has the other multiply source.  This must be rearranged so
8209f464c52Smaya       * that op[0] is the source of the fsign op[1] is the other multiply
8219f464c52Smaya       * source.
8229f464c52Smaya       */
8239f464c52Smaya      if (fsign_src != 0)
8249f464c52Smaya         op[1] = op[0];
8259f464c52Smaya
8269f464c52Smaya      op[0] = get_nir_src(fsign_instr->src[0].src);
8279f464c52Smaya
8289f464c52Smaya      const nir_alu_type t =
8299f464c52Smaya         (nir_alu_type)(nir_op_infos[instr->op].input_types[0] |
8309f464c52Smaya                        nir_src_bit_size(fsign_instr->src[0].src));
8319f464c52Smaya
8329f464c52Smaya      op[0].type = brw_type_for_nir_type(devinfo, t);
8339f464c52Smaya
8349f464c52Smaya      unsigned channel = 0;
8359f464c52Smaya      if (nir_op_infos[instr->op].output_size == 0) {
8369f464c52Smaya         /* Since NIR is doing the scalarizing for us, we should only ever see
8379f464c52Smaya          * vectorized operations with a single channel.
8389f464c52Smaya          */
8399f464c52Smaya         assert(util_bitcount(instr->dest.write_mask) == 1);
8409f464c52Smaya         channel = ffs(instr->dest.write_mask) - 1;
8419f464c52Smaya      }
8429f464c52Smaya
8439f464c52Smaya      op[0] = offset(op[0], bld, fsign_instr->src[0].swizzle[channel]);
8449f464c52Smaya   }
8459f464c52Smaya
8467ec681f3Smrg   if (type_sz(op[0].type) == 2) {
8479f464c52Smaya      /* AND(val, 0x8000) gives the sign bit.
8489f464c52Smaya       *
8499f464c52Smaya       * Predicated OR ORs 1.0 (0x3c00) with the sign bit if val is not zero.
8509f464c52Smaya       */
8519f464c52Smaya      fs_reg zero = retype(brw_imm_uw(0), BRW_REGISTER_TYPE_HF);
8529f464c52Smaya      bld.CMP(bld.null_reg_f(), op[0], zero, BRW_CONDITIONAL_NZ);
8539f464c52Smaya
8549f464c52Smaya      op[0].type = BRW_REGISTER_TYPE_UW;
8559f464c52Smaya      result.type = BRW_REGISTER_TYPE_UW;
8569f464c52Smaya      bld.AND(result, op[0], brw_imm_uw(0x8000u));
8579f464c52Smaya
8589f464c52Smaya      if (instr->op == nir_op_fsign)
8599f464c52Smaya         inst = bld.OR(result, result, brw_imm_uw(0x3c00u));
8609f464c52Smaya      else {
8619f464c52Smaya         /* Use XOR here to get the result sign correct. */
8629f464c52Smaya         inst = bld.XOR(result, result, retype(op[1], BRW_REGISTER_TYPE_UW));
8639f464c52Smaya      }
8649f464c52Smaya
8659f464c52Smaya      inst->predicate = BRW_PREDICATE_NORMAL;
8669f464c52Smaya   } else if (type_sz(op[0].type) == 4) {
8679f464c52Smaya      /* AND(val, 0x80000000) gives the sign bit.
8689f464c52Smaya       *
8699f464c52Smaya       * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
8709f464c52Smaya       * zero.
8719f464c52Smaya       */
8729f464c52Smaya      bld.CMP(bld.null_reg_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
8739f464c52Smaya
8749f464c52Smaya      op[0].type = BRW_REGISTER_TYPE_UD;
8759f464c52Smaya      result.type = BRW_REGISTER_TYPE_UD;
8769f464c52Smaya      bld.AND(result, op[0], brw_imm_ud(0x80000000u));
8779f464c52Smaya
8789f464c52Smaya      if (instr->op == nir_op_fsign)
8799f464c52Smaya         inst = bld.OR(result, result, brw_imm_ud(0x3f800000u));
8809f464c52Smaya      else {
8819f464c52Smaya         /* Use XOR here to get the result sign correct. */
8829f464c52Smaya         inst = bld.XOR(result, result, retype(op[1], BRW_REGISTER_TYPE_UD));
8839f464c52Smaya      }
8849f464c52Smaya
8859f464c52Smaya      inst->predicate = BRW_PREDICATE_NORMAL;
8869f464c52Smaya   } else {
8879f464c52Smaya      /* For doubles we do the same but we need to consider:
8889f464c52Smaya       *
8899f464c52Smaya       * - 2-src instructions can't operate with 64-bit immediates
8909f464c52Smaya       * - The sign is encoded in the high 32-bit of each DF
8919f464c52Smaya       * - We need to produce a DF result.
8929f464c52Smaya       */
8939f464c52Smaya
8949f464c52Smaya      fs_reg zero = vgrf(glsl_type::double_type);
8959f464c52Smaya      bld.MOV(zero, setup_imm_df(bld, 0.0));
8969f464c52Smaya      bld.CMP(bld.null_reg_df(), op[0], zero, BRW_CONDITIONAL_NZ);
8979f464c52Smaya
8989f464c52Smaya      bld.MOV(result, zero);
8999f464c52Smaya
9009f464c52Smaya      fs_reg r = subscript(result, BRW_REGISTER_TYPE_UD, 1);
9019f464c52Smaya      bld.AND(r, subscript(op[0], BRW_REGISTER_TYPE_UD, 1),
9029f464c52Smaya              brw_imm_ud(0x80000000u));
9039f464c52Smaya
9049f464c52Smaya      if (instr->op == nir_op_fsign) {
9059f464c52Smaya         set_predicate(BRW_PREDICATE_NORMAL,
9069f464c52Smaya                       bld.OR(r, r, brw_imm_ud(0x3ff00000u)));
9079f464c52Smaya      } else {
9089f464c52Smaya         /* This could be done better in some cases.  If the scale is an
9099f464c52Smaya          * immediate with the low 32-bits all 0, emitting a separate XOR and
9109f464c52Smaya          * OR would allow an algebraic optimization to remove the OR.  There
9119f464c52Smaya          * are currently zero instances of fsign(double(x))*IMM in shader-db
9129f464c52Smaya          * or any test suite, so it is hard to care at this time.
9139f464c52Smaya          */
9149f464c52Smaya         fs_reg result_int64 = retype(result, BRW_REGISTER_TYPE_UQ);
9159f464c52Smaya         inst = bld.XOR(result_int64, result_int64,
9169f464c52Smaya                        retype(op[1], BRW_REGISTER_TYPE_UQ));
9179f464c52Smaya      }
9189f464c52Smaya   }
9199f464c52Smaya}
9209f464c52Smaya
9219f464c52Smaya/**
9229f464c52Smaya * Deteremine whether sources of a nir_op_fmul can be fused with a nir_op_fsign
9239f464c52Smaya *
9249f464c52Smaya * Checks the operands of a \c nir_op_fmul to determine whether or not
9259f464c52Smaya * \c emit_fsign could fuse the multiplication with the \c sign() calculation.
9269f464c52Smaya *
9279f464c52Smaya * \param instr  The multiplication instruction
9289f464c52Smaya *
9299f464c52Smaya * \param fsign_src The source of \c instr that may or may not be a
9309f464c52Smaya *                  \c nir_op_fsign
9319f464c52Smaya */
9329f464c52Smayastatic bool
9339f464c52Smayacan_fuse_fmul_fsign(nir_alu_instr *instr, unsigned fsign_src)
9349f464c52Smaya{
9359f464c52Smaya   assert(instr->op == nir_op_fmul);
9369f464c52Smaya
9379f464c52Smaya   nir_alu_instr *const fsign_instr =
9389f464c52Smaya      nir_src_as_alu_instr(instr->src[fsign_src].src);
9399f464c52Smaya
9409f464c52Smaya   /* Rules:
9419f464c52Smaya    *
9429f464c52Smaya    * 1. instr->src[fsign_src] must be a nir_op_fsign.
9439f464c52Smaya    * 2. The nir_op_fsign can only be used by this multiplication.
9449f464c52Smaya    * 3. The source that is the nir_op_fsign does not have source modifiers.
9459f464c52Smaya    *    \c emit_fsign only examines the source modifiers of the source of the
9469f464c52Smaya    *    \c nir_op_fsign.
9479f464c52Smaya    *
9489f464c52Smaya    * The nir_op_fsign must also not have the saturate modifier, but steps
9499f464c52Smaya    * have already been taken (in nir_opt_algebraic) to ensure that.
9509f464c52Smaya    */
9519f464c52Smaya   return fsign_instr != NULL && fsign_instr->op == nir_op_fsign &&
9527ec681f3Smrg          is_used_once(fsign_instr);
9539f464c52Smaya}
9549f464c52Smaya
9559f464c52Smayavoid
9567ec681f3Smrgfs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr,
9577ec681f3Smrg                         bool need_dest)
9589f464c52Smaya{
9599f464c52Smaya   struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
9609f464c52Smaya   fs_inst *inst;
9617ec681f3Smrg   unsigned execution_mode =
9627ec681f3Smrg      bld.shader->nir->info.float_controls_execution_mode;
9637ec681f3Smrg
9647ec681f3Smrg   fs_reg op[NIR_MAX_VEC_COMPONENTS];
9657ec681f3Smrg   fs_reg result = prepare_alu_destination_and_sources(bld, instr, op, need_dest);
9667ec681f3Smrg
9677ec681f3Smrg#ifndef NDEBUG
9687ec681f3Smrg   /* Everything except raw moves, some type conversions, iabs, and ineg
9697ec681f3Smrg    * should have 8-bit sources lowered by nir_lower_bit_size in
9707ec681f3Smrg    * brw_preprocess_nir or by brw_nir_lower_conversions in
9717ec681f3Smrg    * brw_postprocess_nir.
9727ec681f3Smrg    */
9737ec681f3Smrg   switch (instr->op) {
9747ec681f3Smrg   case nir_op_mov:
9757ec681f3Smrg   case nir_op_vec2:
9767ec681f3Smrg   case nir_op_vec3:
9777ec681f3Smrg   case nir_op_vec4:
9787ec681f3Smrg   case nir_op_vec8:
9797ec681f3Smrg   case nir_op_vec16:
9807ec681f3Smrg   case nir_op_i2f16:
9817ec681f3Smrg   case nir_op_i2f32:
9827ec681f3Smrg   case nir_op_i2i16:
9837ec681f3Smrg   case nir_op_i2i32:
9847ec681f3Smrg   case nir_op_u2f16:
9857ec681f3Smrg   case nir_op_u2f32:
9867ec681f3Smrg   case nir_op_u2u16:
9877ec681f3Smrg   case nir_op_u2u32:
9887ec681f3Smrg   case nir_op_iabs:
9897ec681f3Smrg   case nir_op_ineg:
9907ec681f3Smrg   case nir_op_pack_32_4x8_split:
9917ec681f3Smrg      break;
9929f464c52Smaya
9937ec681f3Smrg   default:
9947ec681f3Smrg      for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
9957ec681f3Smrg         assert(type_sz(op[i].type) > 1);
9967ec681f3Smrg      }
9977ec681f3Smrg   }
9987ec681f3Smrg#endif
9999f464c52Smaya
100001e04c3fSmrg   switch (instr->op) {
10017ec681f3Smrg   case nir_op_mov:
100201e04c3fSmrg   case nir_op_vec2:
100301e04c3fSmrg   case nir_op_vec3:
10047ec681f3Smrg   case nir_op_vec4:
10057ec681f3Smrg   case nir_op_vec8:
10067ec681f3Smrg   case nir_op_vec16: {
100701e04c3fSmrg      fs_reg temp = result;
100801e04c3fSmrg      bool need_extra_copy = false;
100901e04c3fSmrg      for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
101001e04c3fSmrg         if (!instr->src[i].src.is_ssa &&
101101e04c3fSmrg             instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
101201e04c3fSmrg            need_extra_copy = true;
101301e04c3fSmrg            temp = bld.vgrf(result.type, 4);
101401e04c3fSmrg            break;
101501e04c3fSmrg         }
101601e04c3fSmrg      }
101701e04c3fSmrg
101801e04c3fSmrg      for (unsigned i = 0; i < 4; i++) {
101901e04c3fSmrg         if (!(instr->dest.write_mask & (1 << i)))
102001e04c3fSmrg            continue;
102101e04c3fSmrg
10227ec681f3Smrg         if (instr->op == nir_op_mov) {
10237ec681f3Smrg            bld.MOV(offset(temp, bld, i),
102401e04c3fSmrg                           offset(op[0], bld, instr->src[0].swizzle[i]));
102501e04c3fSmrg         } else {
10267ec681f3Smrg            bld.MOV(offset(temp, bld, i),
102701e04c3fSmrg                           offset(op[i], bld, instr->src[i].swizzle[0]));
102801e04c3fSmrg         }
102901e04c3fSmrg      }
103001e04c3fSmrg
103101e04c3fSmrg      /* In this case the source and destination registers were the same,
103201e04c3fSmrg       * so we need to insert an extra set of moves in order to deal with
103301e04c3fSmrg       * any swizzling.
103401e04c3fSmrg       */
103501e04c3fSmrg      if (need_extra_copy) {
103601e04c3fSmrg         for (unsigned i = 0; i < 4; i++) {
103701e04c3fSmrg            if (!(instr->dest.write_mask & (1 << i)))
103801e04c3fSmrg               continue;
103901e04c3fSmrg
104001e04c3fSmrg            bld.MOV(offset(result, bld, i), offset(temp, bld, i));
104101e04c3fSmrg         }
104201e04c3fSmrg      }
104301e04c3fSmrg      return;
104401e04c3fSmrg   }
104501e04c3fSmrg
104601e04c3fSmrg   case nir_op_i2f32:
104701e04c3fSmrg   case nir_op_u2f32:
104801e04c3fSmrg      if (optimize_extract_to_float(instr, result))
104901e04c3fSmrg         return;
105001e04c3fSmrg      inst = bld.MOV(result, op[0]);
105101e04c3fSmrg      break;
105201e04c3fSmrg
105301e04c3fSmrg   case nir_op_f2f16_rtne:
105401e04c3fSmrg   case nir_op_f2f16_rtz:
10557ec681f3Smrg   case nir_op_f2f16: {
10567ec681f3Smrg      brw_rnd_mode rnd = BRW_RND_MODE_UNSPECIFIED;
10577ec681f3Smrg
10587ec681f3Smrg      if (nir_op_f2f16 == instr->op)
10597ec681f3Smrg         rnd = brw_rnd_mode_from_execution_mode(execution_mode);
10607ec681f3Smrg      else
10617ec681f3Smrg         rnd = brw_rnd_mode_from_nir_op(instr->op);
10627ec681f3Smrg
10637ec681f3Smrg      if (BRW_RND_MODE_UNSPECIFIED != rnd)
10647ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(), brw_imm_d(rnd));
10657ec681f3Smrg
106601e04c3fSmrg      /* In theory, it would be better to use BRW_OPCODE_F32TO16. Depending
106701e04c3fSmrg       * on the HW gen, it is a special hw opcode or just a MOV, and
106801e04c3fSmrg       * brw_F32TO16 (at brw_eu_emit) would do the work to chose.
106901e04c3fSmrg       *
107001e04c3fSmrg       * But if we want to use that opcode, we need to provide support on
107101e04c3fSmrg       * different optimizations and lowerings. As right now HF support is
10727ec681f3Smrg       * only for gfx8+, it will be better to use directly the MOV, and use
10737ec681f3Smrg       * BRW_OPCODE_F32TO16 when/if we work for HF support on gfx7.
107401e04c3fSmrg       */
10759f464c52Smaya      assert(type_sz(op[0].type) < 8); /* brw_nir_lower_conversions */
107601e04c3fSmrg      inst = bld.MOV(result, op[0]);
107701e04c3fSmrg      break;
10787ec681f3Smrg   }
107901e04c3fSmrg
10809f464c52Smaya   case nir_op_b2i8:
10819f464c52Smaya   case nir_op_b2i16:
10829f464c52Smaya   case nir_op_b2i32:
10839f464c52Smaya   case nir_op_b2i64:
10849f464c52Smaya   case nir_op_b2f16:
10859f464c52Smaya   case nir_op_b2f32:
10869f464c52Smaya   case nir_op_b2f64:
10879f464c52Smaya      if (try_emit_b2fi_of_inot(bld, result, instr))
10889f464c52Smaya         break;
108901e04c3fSmrg      op[0].type = BRW_REGISTER_TYPE_D;
109001e04c3fSmrg      op[0].negate = !op[0].negate;
10917ec681f3Smrg      FALLTHROUGH;
109201e04c3fSmrg   case nir_op_i2f64:
109301e04c3fSmrg   case nir_op_i2i64:
109401e04c3fSmrg   case nir_op_u2f64:
109501e04c3fSmrg   case nir_op_u2u64:
10969f464c52Smaya   case nir_op_f2f64:
10979f464c52Smaya   case nir_op_f2i64:
10989f464c52Smaya   case nir_op_f2u64:
10999f464c52Smaya   case nir_op_i2i32:
11009f464c52Smaya   case nir_op_u2u32:
110101e04c3fSmrg   case nir_op_f2i32:
110201e04c3fSmrg   case nir_op_f2u32:
110301e04c3fSmrg   case nir_op_i2f16:
110401e04c3fSmrg   case nir_op_u2f16:
11059f464c52Smaya   case nir_op_f2i16:
11069f464c52Smaya   case nir_op_f2u16:
11079f464c52Smaya   case nir_op_f2i8:
11089f464c52Smaya   case nir_op_f2u8:
11099f464c52Smaya      if (result.type == BRW_REGISTER_TYPE_B ||
11109f464c52Smaya          result.type == BRW_REGISTER_TYPE_UB ||
11119f464c52Smaya          result.type == BRW_REGISTER_TYPE_HF)
11129f464c52Smaya         assert(type_sz(op[0].type) < 8); /* brw_nir_lower_conversions */
11139f464c52Smaya
11149f464c52Smaya      if (op[0].type == BRW_REGISTER_TYPE_B ||
11159f464c52Smaya          op[0].type == BRW_REGISTER_TYPE_UB ||
11169f464c52Smaya          op[0].type == BRW_REGISTER_TYPE_HF)
11179f464c52Smaya         assert(type_sz(result.type) < 8); /* brw_nir_lower_conversions */
11189f464c52Smaya
111901e04c3fSmrg      inst = bld.MOV(result, op[0]);
11207ec681f3Smrg      break;
11217ec681f3Smrg
11227ec681f3Smrg   case nir_op_i2i8:
11237ec681f3Smrg   case nir_op_u2u8:
11247ec681f3Smrg      assert(type_sz(op[0].type) < 8); /* brw_nir_lower_conversions */
11257ec681f3Smrg      FALLTHROUGH;
11267ec681f3Smrg   case nir_op_i2i16:
11277ec681f3Smrg   case nir_op_u2u16: {
11287ec681f3Smrg      /* Emit better code for u2u8(extract_u8(a, b)) and similar patterns.
11297ec681f3Smrg       * Emitting the instructions one by one results in two MOV instructions
11307ec681f3Smrg       * that won't be propagated.  By handling both instructions here, a
11317ec681f3Smrg       * single MOV is emitted.
11327ec681f3Smrg       */
11337ec681f3Smrg      nir_alu_instr *extract_instr = nir_src_as_alu_instr(instr->src[0].src);
11347ec681f3Smrg      if (extract_instr != NULL) {
11357ec681f3Smrg         if (extract_instr->op == nir_op_extract_u8 ||
11367ec681f3Smrg             extract_instr->op == nir_op_extract_i8) {
11377ec681f3Smrg            prepare_alu_destination_and_sources(bld, extract_instr, op, false);
11387ec681f3Smrg
11397ec681f3Smrg            const unsigned byte = nir_src_as_uint(extract_instr->src[1].src);
11407ec681f3Smrg            const brw_reg_type type =
11417ec681f3Smrg               brw_int_type(1, extract_instr->op == nir_op_extract_i8);
11427ec681f3Smrg
11437ec681f3Smrg            op[0] = subscript(op[0], type, byte);
11447ec681f3Smrg         } else if (extract_instr->op == nir_op_extract_u16 ||
11457ec681f3Smrg                    extract_instr->op == nir_op_extract_i16) {
11467ec681f3Smrg            prepare_alu_destination_and_sources(bld, extract_instr, op, false);
11477ec681f3Smrg
11487ec681f3Smrg            const unsigned word = nir_src_as_uint(extract_instr->src[1].src);
11497ec681f3Smrg            const brw_reg_type type =
11507ec681f3Smrg               brw_int_type(2, extract_instr->op == nir_op_extract_i16);
11517ec681f3Smrg
11527ec681f3Smrg            op[0] = subscript(op[0], type, word);
11537ec681f3Smrg         }
11547ec681f3Smrg      }
11557ec681f3Smrg
11567ec681f3Smrg      inst = bld.MOV(result, op[0]);
11577ec681f3Smrg      break;
11587ec681f3Smrg   }
11597ec681f3Smrg
11607ec681f3Smrg   case nir_op_fsat:
11617ec681f3Smrg      inst = bld.MOV(result, op[0]);
11627ec681f3Smrg      inst->saturate = true;
11637ec681f3Smrg      break;
11647ec681f3Smrg
11657ec681f3Smrg   case nir_op_fneg:
11667ec681f3Smrg   case nir_op_ineg:
11677ec681f3Smrg      op[0].negate = true;
11687ec681f3Smrg      inst = bld.MOV(result, op[0]);
11697ec681f3Smrg      break;
11707ec681f3Smrg
11717ec681f3Smrg   case nir_op_fabs:
11727ec681f3Smrg   case nir_op_iabs:
11737ec681f3Smrg      op[0].negate = false;
11747ec681f3Smrg      op[0].abs = true;
11757ec681f3Smrg      inst = bld.MOV(result, op[0]);
11767ec681f3Smrg      break;
11777ec681f3Smrg
11787ec681f3Smrg   case nir_op_f2f32:
11797ec681f3Smrg      if (nir_has_any_rounding_mode_enabled(execution_mode)) {
11807ec681f3Smrg         brw_rnd_mode rnd =
11817ec681f3Smrg            brw_rnd_mode_from_execution_mode(execution_mode);
11827ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
11837ec681f3Smrg                  brw_imm_d(rnd));
11847ec681f3Smrg      }
11857ec681f3Smrg
11867ec681f3Smrg      if (op[0].type == BRW_REGISTER_TYPE_HF)
11877ec681f3Smrg         assert(type_sz(result.type) < 8); /* brw_nir_lower_conversions */
11887ec681f3Smrg
11897ec681f3Smrg      inst = bld.MOV(result, op[0]);
119001e04c3fSmrg      break;
119101e04c3fSmrg
11929f464c52Smaya   case nir_op_fsign:
11939f464c52Smaya      emit_fsign(bld, instr, result, op, 0);
119401e04c3fSmrg      break;
119501e04c3fSmrg
119601e04c3fSmrg   case nir_op_frcp:
119701e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
119801e04c3fSmrg      break;
119901e04c3fSmrg
120001e04c3fSmrg   case nir_op_fexp2:
120101e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
120201e04c3fSmrg      break;
120301e04c3fSmrg
120401e04c3fSmrg   case nir_op_flog2:
120501e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
120601e04c3fSmrg      break;
120701e04c3fSmrg
120801e04c3fSmrg   case nir_op_fsin:
120901e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
121001e04c3fSmrg      break;
121101e04c3fSmrg
121201e04c3fSmrg   case nir_op_fcos:
121301e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
121401e04c3fSmrg      break;
121501e04c3fSmrg
121601e04c3fSmrg   case nir_op_fddx:
121701e04c3fSmrg      if (fs_key->high_quality_derivatives) {
121801e04c3fSmrg         inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
121901e04c3fSmrg      } else {
122001e04c3fSmrg         inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
122101e04c3fSmrg      }
122201e04c3fSmrg      break;
122301e04c3fSmrg   case nir_op_fddx_fine:
122401e04c3fSmrg      inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
122501e04c3fSmrg      break;
122601e04c3fSmrg   case nir_op_fddx_coarse:
122701e04c3fSmrg      inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
122801e04c3fSmrg      break;
122901e04c3fSmrg   case nir_op_fddy:
123001e04c3fSmrg      if (fs_key->high_quality_derivatives) {
123101e04c3fSmrg         inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
123201e04c3fSmrg      } else {
123301e04c3fSmrg         inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
123401e04c3fSmrg      }
123501e04c3fSmrg      break;
123601e04c3fSmrg   case nir_op_fddy_fine:
123701e04c3fSmrg      inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
123801e04c3fSmrg      break;
123901e04c3fSmrg   case nir_op_fddy_coarse:
124001e04c3fSmrg      inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
124101e04c3fSmrg      break;
124201e04c3fSmrg
124301e04c3fSmrg   case nir_op_fadd:
12447ec681f3Smrg      if (nir_has_any_rounding_mode_enabled(execution_mode)) {
12457ec681f3Smrg         brw_rnd_mode rnd =
12467ec681f3Smrg            brw_rnd_mode_from_execution_mode(execution_mode);
12477ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
12487ec681f3Smrg                  brw_imm_d(rnd));
12497ec681f3Smrg      }
12507ec681f3Smrg      FALLTHROUGH;
12517ec681f3Smrg   case nir_op_iadd:
125201e04c3fSmrg      inst = bld.ADD(result, op[0], op[1]);
125301e04c3fSmrg      break;
125401e04c3fSmrg
12557ec681f3Smrg   case nir_op_iadd3:
12567ec681f3Smrg      inst = bld.ADD3(result, op[0], op[1], op[2]);
12577ec681f3Smrg      break;
12587ec681f3Smrg
12597ec681f3Smrg   case nir_op_iadd_sat:
12609f464c52Smaya   case nir_op_uadd_sat:
12619f464c52Smaya      inst = bld.ADD(result, op[0], op[1]);
12629f464c52Smaya      inst->saturate = true;
12639f464c52Smaya      break;
12649f464c52Smaya
12657ec681f3Smrg   case nir_op_isub_sat:
12667ec681f3Smrg      bld.emit(SHADER_OPCODE_ISUB_SAT, result, op[0], op[1]);
12677ec681f3Smrg      break;
12687ec681f3Smrg
12697ec681f3Smrg   case nir_op_usub_sat:
12707ec681f3Smrg      bld.emit(SHADER_OPCODE_USUB_SAT, result, op[0], op[1]);
12717ec681f3Smrg      break;
12727ec681f3Smrg
12737ec681f3Smrg   case nir_op_irhadd:
12747ec681f3Smrg   case nir_op_urhadd:
12757ec681f3Smrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
12767ec681f3Smrg      inst = bld.AVG(result, op[0], op[1]);
12777ec681f3Smrg      break;
12787ec681f3Smrg
12797ec681f3Smrg   case nir_op_ihadd:
12807ec681f3Smrg   case nir_op_uhadd: {
12817ec681f3Smrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
12827ec681f3Smrg      fs_reg tmp = bld.vgrf(result.type);
12837ec681f3Smrg
12847ec681f3Smrg      if (devinfo->ver >= 8) {
12857ec681f3Smrg         op[0] = resolve_source_modifiers(op[0]);
12867ec681f3Smrg         op[1] = resolve_source_modifiers(op[1]);
12877ec681f3Smrg      }
12887ec681f3Smrg
12897ec681f3Smrg      /* AVG(x, y) - ((x ^ y) & 1) */
12907ec681f3Smrg      bld.XOR(tmp, op[0], op[1]);
12917ec681f3Smrg      bld.AND(tmp, tmp, retype(brw_imm_ud(1), result.type));
12927ec681f3Smrg      bld.AVG(result, op[0], op[1]);
12937ec681f3Smrg      inst = bld.ADD(result, result, tmp);
12947ec681f3Smrg      inst->src[1].negate = true;
12957ec681f3Smrg      break;
12967ec681f3Smrg   }
12977ec681f3Smrg
129801e04c3fSmrg   case nir_op_fmul:
12999f464c52Smaya      for (unsigned i = 0; i < 2; i++) {
13009f464c52Smaya         if (can_fuse_fmul_fsign(instr, i)) {
13019f464c52Smaya            emit_fsign(bld, instr, result, op, i);
13029f464c52Smaya            return;
13039f464c52Smaya         }
13049f464c52Smaya      }
13059f464c52Smaya
13067ec681f3Smrg      /* We emit the rounding mode after the previous fsign optimization since
13077ec681f3Smrg       * it won't result in a MUL, but will try to negate the value by other
13087ec681f3Smrg       * means.
13097ec681f3Smrg       */
13107ec681f3Smrg      if (nir_has_any_rounding_mode_enabled(execution_mode)) {
13117ec681f3Smrg         brw_rnd_mode rnd =
13127ec681f3Smrg            brw_rnd_mode_from_execution_mode(execution_mode);
13137ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
13147ec681f3Smrg                  brw_imm_d(rnd));
13157ec681f3Smrg      }
13167ec681f3Smrg
131701e04c3fSmrg      inst = bld.MUL(result, op[0], op[1]);
131801e04c3fSmrg      break;
131901e04c3fSmrg
13209f464c52Smaya   case nir_op_imul_2x32_64:
13219f464c52Smaya   case nir_op_umul_2x32_64:
13229f464c52Smaya      bld.MUL(result, op[0], op[1]);
13239f464c52Smaya      break;
13249f464c52Smaya
13257ec681f3Smrg   case nir_op_imul_32x16:
13267ec681f3Smrg   case nir_op_umul_32x16: {
13277ec681f3Smrg      const bool ud = instr->op == nir_op_umul_32x16;
13287ec681f3Smrg
13297ec681f3Smrg      assert(nir_dest_bit_size(instr->dest.dest) == 32);
13307ec681f3Smrg
13317ec681f3Smrg      /* Before Gfx7, the order of the 32-bit source and the 16-bit source was
13327ec681f3Smrg       * swapped.  The extension isn't enabled on those platforms, so don't
13337ec681f3Smrg       * pretend to support the differences.
13347ec681f3Smrg       */
13357ec681f3Smrg      assert(devinfo->ver >= 7);
13367ec681f3Smrg
13377ec681f3Smrg      if (op[1].file == IMM)
13387ec681f3Smrg         op[1] = ud ? brw_imm_uw(op[1].ud) : brw_imm_w(op[1].d);
13397ec681f3Smrg      else {
13407ec681f3Smrg         const enum brw_reg_type word_type =
13417ec681f3Smrg            ud ? BRW_REGISTER_TYPE_UW : BRW_REGISTER_TYPE_W;
13427ec681f3Smrg
13437ec681f3Smrg         op[1] = subscript(op[1], word_type, 0);
13447ec681f3Smrg      }
13457ec681f3Smrg
13467ec681f3Smrg      const enum brw_reg_type dword_type =
13477ec681f3Smrg         ud ? BRW_REGISTER_TYPE_UD : BRW_REGISTER_TYPE_D;
13487ec681f3Smrg
13497ec681f3Smrg      bld.MUL(result, retype(op[0], dword_type), op[1]);
13507ec681f3Smrg      break;
13517ec681f3Smrg   }
13527ec681f3Smrg
135301e04c3fSmrg   case nir_op_imul:
135401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
135501e04c3fSmrg      bld.MUL(result, op[0], op[1]);
135601e04c3fSmrg      break;
135701e04c3fSmrg
135801e04c3fSmrg   case nir_op_imul_high:
135901e04c3fSmrg   case nir_op_umul_high:
136001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
136101e04c3fSmrg      bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
136201e04c3fSmrg      break;
136301e04c3fSmrg
136401e04c3fSmrg   case nir_op_idiv:
136501e04c3fSmrg   case nir_op_udiv:
136601e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
136701e04c3fSmrg      bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
136801e04c3fSmrg      break;
136901e04c3fSmrg
137001e04c3fSmrg   case nir_op_uadd_carry:
137101e04c3fSmrg      unreachable("Should have been lowered by carry_to_arith().");
137201e04c3fSmrg
137301e04c3fSmrg   case nir_op_usub_borrow:
137401e04c3fSmrg      unreachable("Should have been lowered by borrow_to_arith().");
137501e04c3fSmrg
137601e04c3fSmrg   case nir_op_umod:
137701e04c3fSmrg   case nir_op_irem:
137801e04c3fSmrg      /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
137901e04c3fSmrg       * appears that our hardware just does the right thing for signed
138001e04c3fSmrg       * remainder.
138101e04c3fSmrg       */
138201e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
138301e04c3fSmrg      bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
138401e04c3fSmrg      break;
138501e04c3fSmrg
138601e04c3fSmrg   case nir_op_imod: {
138701e04c3fSmrg      /* Get a regular C-style remainder.  If a % b == 0, set the predicate. */
138801e04c3fSmrg      bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
138901e04c3fSmrg
139001e04c3fSmrg      /* Math instructions don't support conditional mod */
139101e04c3fSmrg      inst = bld.MOV(bld.null_reg_d(), result);
139201e04c3fSmrg      inst->conditional_mod = BRW_CONDITIONAL_NZ;
139301e04c3fSmrg
139401e04c3fSmrg      /* Now, we need to determine if signs of the sources are different.
139501e04c3fSmrg       * When we XOR the sources, the top bit is 0 if they are the same and 1
139601e04c3fSmrg       * if they are different.  We can then use a conditional modifier to
139701e04c3fSmrg       * turn that into a predicate.  This leads us to an XOR.l instruction.
139801e04c3fSmrg       *
139901e04c3fSmrg       * Technically, according to the PRM, you're not allowed to use .l on a
140001e04c3fSmrg       * XOR instruction.  However, emperical experiments and Curro's reading
140101e04c3fSmrg       * of the simulator source both indicate that it's safe.
140201e04c3fSmrg       */
140301e04c3fSmrg      fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
140401e04c3fSmrg      inst = bld.XOR(tmp, op[0], op[1]);
140501e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
140601e04c3fSmrg      inst->conditional_mod = BRW_CONDITIONAL_L;
140701e04c3fSmrg
140801e04c3fSmrg      /* If the result of the initial remainder operation is non-zero and the
140901e04c3fSmrg       * two sources have different signs, add in a copy of op[1] to get the
141001e04c3fSmrg       * final integer modulus value.
141101e04c3fSmrg       */
141201e04c3fSmrg      inst = bld.ADD(result, result, op[1]);
141301e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
141401e04c3fSmrg      break;
141501e04c3fSmrg   }
141601e04c3fSmrg
14179f464c52Smaya   case nir_op_flt32:
14189f464c52Smaya   case nir_op_fge32:
14199f464c52Smaya   case nir_op_feq32:
14207ec681f3Smrg   case nir_op_fneu32: {
142101e04c3fSmrg      fs_reg dest = result;
142201e04c3fSmrg
142301e04c3fSmrg      const uint32_t bit_size =  nir_src_bit_size(instr->src[0].src);
142401e04c3fSmrg      if (bit_size != 32)
142501e04c3fSmrg         dest = bld.vgrf(op[0].type, 1);
142601e04c3fSmrg
14277ec681f3Smrg      bld.CMP(dest, op[0], op[1], brw_cmod_for_nir_comparison(instr->op));
142801e04c3fSmrg
142901e04c3fSmrg      if (bit_size > 32) {
143001e04c3fSmrg         bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
143101e04c3fSmrg      } else if(bit_size < 32) {
143201e04c3fSmrg         /* When we convert the result to 32-bit we need to be careful and do
143301e04c3fSmrg          * it as a signed conversion to get sign extension (for 32-bit true)
143401e04c3fSmrg          */
143501e04c3fSmrg         const brw_reg_type src_type =
143601e04c3fSmrg            brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
143701e04c3fSmrg
143801e04c3fSmrg         bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
143901e04c3fSmrg      }
144001e04c3fSmrg      break;
144101e04c3fSmrg   }
144201e04c3fSmrg
14439f464c52Smaya   case nir_op_ilt32:
14449f464c52Smaya   case nir_op_ult32:
14459f464c52Smaya   case nir_op_ige32:
14469f464c52Smaya   case nir_op_uge32:
14479f464c52Smaya   case nir_op_ieq32:
14489f464c52Smaya   case nir_op_ine32: {
144901e04c3fSmrg      fs_reg dest = result;
145001e04c3fSmrg
14517ec681f3Smrg      const uint32_t bit_size = type_sz(op[0].type) * 8;
145201e04c3fSmrg      if (bit_size != 32)
14537ec681f3Smrg         dest = bld.vgrf(op[0].type, 1);
145401e04c3fSmrg
14557ec681f3Smrg      bld.CMP(dest, op[0], op[1],
14567ec681f3Smrg              brw_cmod_for_nir_comparison(instr->op));
145701e04c3fSmrg
145801e04c3fSmrg      if (bit_size > 32) {
145901e04c3fSmrg         bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
146001e04c3fSmrg      } else if (bit_size < 32) {
146101e04c3fSmrg         /* When we convert the result to 32-bit we need to be careful and do
146201e04c3fSmrg          * it as a signed conversion to get sign extension (for 32-bit true)
146301e04c3fSmrg          */
146401e04c3fSmrg         const brw_reg_type src_type =
146501e04c3fSmrg            brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
146601e04c3fSmrg
146701e04c3fSmrg         bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
146801e04c3fSmrg      }
146901e04c3fSmrg      break;
147001e04c3fSmrg   }
147101e04c3fSmrg
147201e04c3fSmrg   case nir_op_inot:
14737ec681f3Smrg      if (devinfo->ver >= 8) {
14749f464c52Smaya         nir_alu_instr *inot_src_instr = nir_src_as_alu_instr(instr->src[0].src);
14759f464c52Smaya
14769f464c52Smaya         if (inot_src_instr != NULL &&
14779f464c52Smaya             (inot_src_instr->op == nir_op_ior ||
14789f464c52Smaya              inot_src_instr->op == nir_op_ixor ||
14797ec681f3Smrg              inot_src_instr->op == nir_op_iand)) {
14809f464c52Smaya            /* The sources of the source logical instruction are now the
14819f464c52Smaya             * sources of the instruction that will be generated.
14829f464c52Smaya             */
14839f464c52Smaya            prepare_alu_destination_and_sources(bld, inot_src_instr, op, false);
14849f464c52Smaya            resolve_inot_sources(bld, inot_src_instr, op);
14859f464c52Smaya
14869f464c52Smaya            /* Smash all of the sources and destination to be signed.  This
14879f464c52Smaya             * doesn't matter for the operation of the instruction, but cmod
14889f464c52Smaya             * propagation fails on unsigned sources with negation (due to
14899f464c52Smaya             * fs_inst::can_do_cmod returning false).
14909f464c52Smaya             */
14919f464c52Smaya            result.type =
14929f464c52Smaya               brw_type_for_nir_type(devinfo,
14939f464c52Smaya                                     (nir_alu_type)(nir_type_int |
14949f464c52Smaya                                                    nir_dest_bit_size(instr->dest.dest)));
14959f464c52Smaya            op[0].type =
14969f464c52Smaya               brw_type_for_nir_type(devinfo,
14979f464c52Smaya                                     (nir_alu_type)(nir_type_int |
14989f464c52Smaya                                                    nir_src_bit_size(inot_src_instr->src[0].src)));
14999f464c52Smaya            op[1].type =
15009f464c52Smaya               brw_type_for_nir_type(devinfo,
15019f464c52Smaya                                     (nir_alu_type)(nir_type_int |
15029f464c52Smaya                                                    nir_src_bit_size(inot_src_instr->src[1].src)));
15039f464c52Smaya
15049f464c52Smaya            /* For XOR, only invert one of the sources.  Arbitrarily choose
15059f464c52Smaya             * the first source.
15069f464c52Smaya             */
15079f464c52Smaya            op[0].negate = !op[0].negate;
15089f464c52Smaya            if (inot_src_instr->op != nir_op_ixor)
15099f464c52Smaya               op[1].negate = !op[1].negate;
15109f464c52Smaya
15119f464c52Smaya            switch (inot_src_instr->op) {
15129f464c52Smaya            case nir_op_ior:
15139f464c52Smaya               bld.AND(result, op[0], op[1]);
15149f464c52Smaya               return;
15159f464c52Smaya
15169f464c52Smaya            case nir_op_iand:
15179f464c52Smaya               bld.OR(result, op[0], op[1]);
15189f464c52Smaya               return;
15199f464c52Smaya
15209f464c52Smaya            case nir_op_ixor:
15219f464c52Smaya               bld.XOR(result, op[0], op[1]);
15229f464c52Smaya               return;
15239f464c52Smaya
15249f464c52Smaya            default:
15259f464c52Smaya               unreachable("impossible opcode");
15269f464c52Smaya            }
15279f464c52Smaya         }
152801e04c3fSmrg         op[0] = resolve_source_modifiers(op[0]);
152901e04c3fSmrg      }
153001e04c3fSmrg      bld.NOT(result, op[0]);
153101e04c3fSmrg      break;
153201e04c3fSmrg   case nir_op_ixor:
15337ec681f3Smrg      if (devinfo->ver >= 8) {
15349f464c52Smaya         resolve_inot_sources(bld, instr, op);
153501e04c3fSmrg      }
153601e04c3fSmrg      bld.XOR(result, op[0], op[1]);
153701e04c3fSmrg      break;
153801e04c3fSmrg   case nir_op_ior:
15397ec681f3Smrg      if (devinfo->ver >= 8) {
15409f464c52Smaya         resolve_inot_sources(bld, instr, op);
154101e04c3fSmrg      }
154201e04c3fSmrg      bld.OR(result, op[0], op[1]);
154301e04c3fSmrg      break;
154401e04c3fSmrg   case nir_op_iand:
15457ec681f3Smrg      if (devinfo->ver >= 8) {
15469f464c52Smaya         resolve_inot_sources(bld, instr, op);
154701e04c3fSmrg      }
154801e04c3fSmrg      bld.AND(result, op[0], op[1]);
154901e04c3fSmrg      break;
155001e04c3fSmrg
155101e04c3fSmrg   case nir_op_fdot2:
155201e04c3fSmrg   case nir_op_fdot3:
155301e04c3fSmrg   case nir_op_fdot4:
15549f464c52Smaya   case nir_op_b32all_fequal2:
15559f464c52Smaya   case nir_op_b32all_iequal2:
15569f464c52Smaya   case nir_op_b32all_fequal3:
15579f464c52Smaya   case nir_op_b32all_iequal3:
15589f464c52Smaya   case nir_op_b32all_fequal4:
15599f464c52Smaya   case nir_op_b32all_iequal4:
15609f464c52Smaya   case nir_op_b32any_fnequal2:
15619f464c52Smaya   case nir_op_b32any_inequal2:
15629f464c52Smaya   case nir_op_b32any_fnequal3:
15639f464c52Smaya   case nir_op_b32any_inequal3:
15649f464c52Smaya   case nir_op_b32any_fnequal4:
15659f464c52Smaya   case nir_op_b32any_inequal4:
156601e04c3fSmrg      unreachable("Lowered by nir_lower_alu_reductions");
156701e04c3fSmrg
156801e04c3fSmrg   case nir_op_ldexp:
156901e04c3fSmrg      unreachable("not reached: should be handled by ldexp_to_arith()");
157001e04c3fSmrg
157101e04c3fSmrg   case nir_op_fsqrt:
157201e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
157301e04c3fSmrg      break;
157401e04c3fSmrg
157501e04c3fSmrg   case nir_op_frsq:
157601e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
157701e04c3fSmrg      break;
157801e04c3fSmrg
15799f464c52Smaya   case nir_op_i2b32:
15809f464c52Smaya   case nir_op_f2b32: {
158101e04c3fSmrg      uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
158201e04c3fSmrg      if (bit_size == 64) {
158301e04c3fSmrg         /* two-argument instructions can't take 64-bit immediates */
158401e04c3fSmrg         fs_reg zero;
158501e04c3fSmrg         fs_reg tmp;
158601e04c3fSmrg
15879f464c52Smaya         if (instr->op == nir_op_f2b32) {
158801e04c3fSmrg            zero = vgrf(glsl_type::double_type);
158901e04c3fSmrg            tmp = vgrf(glsl_type::double_type);
159001e04c3fSmrg            bld.MOV(zero, setup_imm_df(bld, 0.0));
159101e04c3fSmrg         } else {
159201e04c3fSmrg            zero = vgrf(glsl_type::int64_t_type);
159301e04c3fSmrg            tmp = vgrf(glsl_type::int64_t_type);
159401e04c3fSmrg            bld.MOV(zero, brw_imm_q(0));
159501e04c3fSmrg         }
159601e04c3fSmrg
159701e04c3fSmrg         /* A SIMD16 execution needs to be split in two instructions, so use
159801e04c3fSmrg          * a vgrf instead of the flag register as dst so instruction splitting
159901e04c3fSmrg          * works
160001e04c3fSmrg          */
160101e04c3fSmrg         bld.CMP(tmp, op[0], zero, BRW_CONDITIONAL_NZ);
160201e04c3fSmrg         bld.MOV(result, subscript(tmp, BRW_REGISTER_TYPE_UD, 0));
160301e04c3fSmrg      } else {
160401e04c3fSmrg         fs_reg zero;
160501e04c3fSmrg         if (bit_size == 32) {
16069f464c52Smaya            zero = instr->op == nir_op_f2b32 ? brw_imm_f(0.0f) : brw_imm_d(0);
160701e04c3fSmrg         } else {
160801e04c3fSmrg            assert(bit_size == 16);
16099f464c52Smaya            zero = instr->op == nir_op_f2b32 ?
161001e04c3fSmrg               retype(brw_imm_w(0), BRW_REGISTER_TYPE_HF) : brw_imm_w(0);
161101e04c3fSmrg         }
161201e04c3fSmrg         bld.CMP(result, op[0], zero, BRW_CONDITIONAL_NZ);
161301e04c3fSmrg      }
161401e04c3fSmrg      break;
161501e04c3fSmrg   }
161601e04c3fSmrg
161701e04c3fSmrg   case nir_op_ftrunc:
161801e04c3fSmrg      inst = bld.RNDZ(result, op[0]);
16197ec681f3Smrg      if (devinfo->ver < 6) {
16207ec681f3Smrg         set_condmod(BRW_CONDITIONAL_R, inst);
16217ec681f3Smrg         set_predicate(BRW_PREDICATE_NORMAL,
16227ec681f3Smrg                       bld.ADD(result, result, brw_imm_f(1.0f)));
16237ec681f3Smrg         inst = bld.MOV(result, result); /* for potential saturation */
16247ec681f3Smrg      }
162501e04c3fSmrg      break;
162601e04c3fSmrg
162701e04c3fSmrg   case nir_op_fceil: {
162801e04c3fSmrg      op[0].negate = !op[0].negate;
162901e04c3fSmrg      fs_reg temp = vgrf(glsl_type::float_type);
163001e04c3fSmrg      bld.RNDD(temp, op[0]);
163101e04c3fSmrg      temp.negate = true;
163201e04c3fSmrg      inst = bld.MOV(result, temp);
163301e04c3fSmrg      break;
163401e04c3fSmrg   }
163501e04c3fSmrg   case nir_op_ffloor:
163601e04c3fSmrg      inst = bld.RNDD(result, op[0]);
163701e04c3fSmrg      break;
163801e04c3fSmrg   case nir_op_ffract:
163901e04c3fSmrg      inst = bld.FRC(result, op[0]);
164001e04c3fSmrg      break;
164101e04c3fSmrg   case nir_op_fround_even:
164201e04c3fSmrg      inst = bld.RNDE(result, op[0]);
16437ec681f3Smrg      if (devinfo->ver < 6) {
16447ec681f3Smrg         set_condmod(BRW_CONDITIONAL_R, inst);
16457ec681f3Smrg         set_predicate(BRW_PREDICATE_NORMAL,
16467ec681f3Smrg                       bld.ADD(result, result, brw_imm_f(1.0f)));
16477ec681f3Smrg         inst = bld.MOV(result, result); /* for potential saturation */
16487ec681f3Smrg      }
164901e04c3fSmrg      break;
165001e04c3fSmrg
165101e04c3fSmrg   case nir_op_fquantize2f16: {
165201e04c3fSmrg      fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
165301e04c3fSmrg      fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
165401e04c3fSmrg      fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
165501e04c3fSmrg
165601e04c3fSmrg      /* The destination stride must be at least as big as the source stride. */
165701e04c3fSmrg      tmp16.type = BRW_REGISTER_TYPE_W;
165801e04c3fSmrg      tmp16.stride = 2;
165901e04c3fSmrg
166001e04c3fSmrg      /* Check for denormal */
166101e04c3fSmrg      fs_reg abs_src0 = op[0];
166201e04c3fSmrg      abs_src0.abs = true;
166301e04c3fSmrg      bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
166401e04c3fSmrg              BRW_CONDITIONAL_L);
166501e04c3fSmrg      /* Get the appropriately signed zero */
166601e04c3fSmrg      bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
166701e04c3fSmrg              retype(op[0], BRW_REGISTER_TYPE_UD),
166801e04c3fSmrg              brw_imm_ud(0x80000000));
166901e04c3fSmrg      /* Do the actual F32 -> F16 -> F32 conversion */
167001e04c3fSmrg      bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
167101e04c3fSmrg      bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
167201e04c3fSmrg      /* Select that or zero based on normal status */
167301e04c3fSmrg      inst = bld.SEL(result, zero, tmp32);
167401e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
167501e04c3fSmrg      break;
167601e04c3fSmrg   }
167701e04c3fSmrg
167801e04c3fSmrg   case nir_op_imin:
167901e04c3fSmrg   case nir_op_umin:
168001e04c3fSmrg   case nir_op_fmin:
168101e04c3fSmrg      inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_L);
168201e04c3fSmrg      break;
168301e04c3fSmrg
168401e04c3fSmrg   case nir_op_imax:
168501e04c3fSmrg   case nir_op_umax:
168601e04c3fSmrg   case nir_op_fmax:
168701e04c3fSmrg      inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_GE);
168801e04c3fSmrg      break;
168901e04c3fSmrg
169001e04c3fSmrg   case nir_op_pack_snorm_2x16:
169101e04c3fSmrg   case nir_op_pack_snorm_4x8:
169201e04c3fSmrg   case nir_op_pack_unorm_2x16:
169301e04c3fSmrg   case nir_op_pack_unorm_4x8:
169401e04c3fSmrg   case nir_op_unpack_snorm_2x16:
169501e04c3fSmrg   case nir_op_unpack_snorm_4x8:
169601e04c3fSmrg   case nir_op_unpack_unorm_2x16:
169701e04c3fSmrg   case nir_op_unpack_unorm_4x8:
169801e04c3fSmrg   case nir_op_unpack_half_2x16:
169901e04c3fSmrg   case nir_op_pack_half_2x16:
170001e04c3fSmrg      unreachable("not reached: should be handled by lower_packing_builtins");
170101e04c3fSmrg
17027ec681f3Smrg   case nir_op_unpack_half_2x16_split_x_flush_to_zero:
17037ec681f3Smrg      assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
17047ec681f3Smrg      FALLTHROUGH;
170501e04c3fSmrg   case nir_op_unpack_half_2x16_split_x:
17069f464c52Smaya      inst = bld.emit(BRW_OPCODE_F16TO32, result,
17079f464c52Smaya                      subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
170801e04c3fSmrg      break;
17097ec681f3Smrg
17107ec681f3Smrg   case nir_op_unpack_half_2x16_split_y_flush_to_zero:
17117ec681f3Smrg      assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
17127ec681f3Smrg      FALLTHROUGH;
171301e04c3fSmrg   case nir_op_unpack_half_2x16_split_y:
17149f464c52Smaya      inst = bld.emit(BRW_OPCODE_F16TO32, result,
17159f464c52Smaya                      subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
171601e04c3fSmrg      break;
171701e04c3fSmrg
171801e04c3fSmrg   case nir_op_pack_64_2x32_split:
171901e04c3fSmrg   case nir_op_pack_32_2x16_split:
172001e04c3fSmrg      bld.emit(FS_OPCODE_PACK, result, op[0], op[1]);
172101e04c3fSmrg      break;
172201e04c3fSmrg
17237ec681f3Smrg   case nir_op_pack_32_4x8_split:
17247ec681f3Smrg      bld.emit(FS_OPCODE_PACK, result, op, 4);
17257ec681f3Smrg      break;
17267ec681f3Smrg
172701e04c3fSmrg   case nir_op_unpack_64_2x32_split_x:
172801e04c3fSmrg   case nir_op_unpack_64_2x32_split_y: {
172901e04c3fSmrg      if (instr->op == nir_op_unpack_64_2x32_split_x)
173001e04c3fSmrg         bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 0));
173101e04c3fSmrg      else
173201e04c3fSmrg         bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
173301e04c3fSmrg      break;
173401e04c3fSmrg   }
173501e04c3fSmrg
173601e04c3fSmrg   case nir_op_unpack_32_2x16_split_x:
173701e04c3fSmrg   case nir_op_unpack_32_2x16_split_y: {
173801e04c3fSmrg      if (instr->op == nir_op_unpack_32_2x16_split_x)
173901e04c3fSmrg         bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
174001e04c3fSmrg      else
174101e04c3fSmrg         bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
174201e04c3fSmrg      break;
174301e04c3fSmrg   }
174401e04c3fSmrg
174501e04c3fSmrg   case nir_op_fpow:
174601e04c3fSmrg      inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
174701e04c3fSmrg      break;
174801e04c3fSmrg
174901e04c3fSmrg   case nir_op_bitfield_reverse:
175001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
175101e04c3fSmrg      bld.BFREV(result, op[0]);
175201e04c3fSmrg      break;
175301e04c3fSmrg
175401e04c3fSmrg   case nir_op_bit_count:
175501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
175601e04c3fSmrg      bld.CBIT(result, op[0]);
175701e04c3fSmrg      break;
175801e04c3fSmrg
175901e04c3fSmrg   case nir_op_ufind_msb: {
176001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
176101e04c3fSmrg      emit_find_msb_using_lzd(bld, result, op[0], false);
176201e04c3fSmrg      break;
176301e04c3fSmrg   }
176401e04c3fSmrg
17657ec681f3Smrg   case nir_op_uclz:
17667ec681f3Smrg      assert(nir_dest_bit_size(instr->dest.dest) == 32);
17677ec681f3Smrg      bld.LZD(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
17687ec681f3Smrg      break;
17697ec681f3Smrg
177001e04c3fSmrg   case nir_op_ifind_msb: {
177101e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
177201e04c3fSmrg
17737ec681f3Smrg      if (devinfo->ver < 7) {
177401e04c3fSmrg         emit_find_msb_using_lzd(bld, result, op[0], true);
177501e04c3fSmrg      } else {
177601e04c3fSmrg         bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
177701e04c3fSmrg
177801e04c3fSmrg         /* FBH counts from the MSB side, while GLSL's findMSB() wants the
177901e04c3fSmrg          * count from the LSB side. If FBH didn't return an error
178001e04c3fSmrg          * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
178101e04c3fSmrg          * count into an LSB count.
178201e04c3fSmrg          */
178301e04c3fSmrg         bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
178401e04c3fSmrg
178501e04c3fSmrg         inst = bld.ADD(result, result, brw_imm_d(31));
178601e04c3fSmrg         inst->predicate = BRW_PREDICATE_NORMAL;
178701e04c3fSmrg         inst->src[0].negate = true;
178801e04c3fSmrg      }
178901e04c3fSmrg      break;
179001e04c3fSmrg   }
179101e04c3fSmrg
179201e04c3fSmrg   case nir_op_find_lsb:
179301e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
179401e04c3fSmrg
17957ec681f3Smrg      if (devinfo->ver < 7) {
179601e04c3fSmrg         fs_reg temp = vgrf(glsl_type::int_type);
179701e04c3fSmrg
179801e04c3fSmrg         /* (x & -x) generates a value that consists of only the LSB of x.
179901e04c3fSmrg          * For all powers of 2, findMSB(y) == findLSB(y).
180001e04c3fSmrg          */
180101e04c3fSmrg         fs_reg src = retype(op[0], BRW_REGISTER_TYPE_D);
180201e04c3fSmrg         fs_reg negated_src = src;
180301e04c3fSmrg
180401e04c3fSmrg         /* One must be negated, and the other must be non-negated.  It
180501e04c3fSmrg          * doesn't matter which is which.
180601e04c3fSmrg          */
180701e04c3fSmrg         negated_src.negate = true;
180801e04c3fSmrg         src.negate = false;
180901e04c3fSmrg
181001e04c3fSmrg         bld.AND(temp, src, negated_src);
181101e04c3fSmrg         emit_find_msb_using_lzd(bld, result, temp, false);
181201e04c3fSmrg      } else {
181301e04c3fSmrg         bld.FBL(result, op[0]);
181401e04c3fSmrg      }
181501e04c3fSmrg      break;
181601e04c3fSmrg
181701e04c3fSmrg   case nir_op_ubitfield_extract:
181801e04c3fSmrg   case nir_op_ibitfield_extract:
181901e04c3fSmrg      unreachable("should have been lowered");
182001e04c3fSmrg   case nir_op_ubfe:
182101e04c3fSmrg   case nir_op_ibfe:
182201e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
182301e04c3fSmrg      bld.BFE(result, op[2], op[1], op[0]);
182401e04c3fSmrg      break;
182501e04c3fSmrg   case nir_op_bfm:
182601e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
182701e04c3fSmrg      bld.BFI1(result, op[0], op[1]);
182801e04c3fSmrg      break;
182901e04c3fSmrg   case nir_op_bfi:
183001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
183101e04c3fSmrg      bld.BFI2(result, op[0], op[1], op[2]);
183201e04c3fSmrg      break;
183301e04c3fSmrg
183401e04c3fSmrg   case nir_op_bitfield_insert:
183501e04c3fSmrg      unreachable("not reached: should have been lowered");
183601e04c3fSmrg
18377ec681f3Smrg   /* For all shift operations:
18387ec681f3Smrg    *
18397ec681f3Smrg    * Gen4 - Gen7: After application of source modifiers, the low 5-bits of
18407ec681f3Smrg    * src1 are used an unsigned value for the shift count.
18417ec681f3Smrg    *
18427ec681f3Smrg    * Gen8: As with earlier platforms, but for Q and UQ types on src0, the low
18437ec681f3Smrg    * 6-bit of src1 are used.
18447ec681f3Smrg    *
18457ec681f3Smrg    * Gen9+: The low bits of src1 matching the size of src0 (e.g., 4-bits for
18467ec681f3Smrg    * W or UW src0).
18477ec681f3Smrg    *
18487ec681f3Smrg    * The implication is that the following instruction will produce a
18497ec681f3Smrg    * different result on Gen9+ than on previous platforms:
18507ec681f3Smrg    *
18517ec681f3Smrg    *    shr(8)    g4<1>UW    g12<8,8,1>UW    0x0010UW
18527ec681f3Smrg    *
18537ec681f3Smrg    * where Gen9+ will shift by zero, and earlier platforms will shift by 16.
18547ec681f3Smrg    *
18557ec681f3Smrg    * This does not seem to be the case.  Experimentally, it has been
18567ec681f3Smrg    * determined that shifts of 16-bit values on Gen8 behave properly.  Shifts
18577ec681f3Smrg    * of 8-bit values on both Gen8 and Gen9 do not.  Gen11+ lowers 8-bit
18587ec681f3Smrg    * values, so those platforms were not tested.  No features expose access
18597ec681f3Smrg    * to 8- or 16-bit types on Gen7 or earlier, so those platforms were not
18607ec681f3Smrg    * tested either.  See
18617ec681f3Smrg    * https://gitlab.freedesktop.org/mesa/crucible/-/merge_requests/76.
18627ec681f3Smrg    *
18637ec681f3Smrg    * This is part of the reason 8-bit values are lowered to 16-bit on all
18647ec681f3Smrg    * platforms.
18657ec681f3Smrg    */
186601e04c3fSmrg   case nir_op_ishl:
18679f464c52Smaya      bld.SHL(result, op[0], op[1]);
18689f464c52Smaya      break;
186901e04c3fSmrg   case nir_op_ishr:
18709f464c52Smaya      bld.ASR(result, op[0], op[1]);
18719f464c52Smaya      break;
18729f464c52Smaya   case nir_op_ushr:
18739f464c52Smaya      bld.SHR(result, op[0], op[1]);
187401e04c3fSmrg      break;
187501e04c3fSmrg
18767ec681f3Smrg   case nir_op_urol:
18777ec681f3Smrg      bld.ROL(result, op[0], op[1]);
18787ec681f3Smrg      break;
18797ec681f3Smrg   case nir_op_uror:
18807ec681f3Smrg      bld.ROR(result, op[0], op[1]);
18817ec681f3Smrg      break;
18827ec681f3Smrg
188301e04c3fSmrg   case nir_op_pack_half_2x16_split:
188401e04c3fSmrg      bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
188501e04c3fSmrg      break;
188601e04c3fSmrg
18877ec681f3Smrg   case nir_op_sdot_4x8_iadd:
18887ec681f3Smrg   case nir_op_sdot_4x8_iadd_sat:
18897ec681f3Smrg      inst = bld.DP4A(result,
18907ec681f3Smrg                      retype(op[2], BRW_REGISTER_TYPE_D),
18917ec681f3Smrg                      retype(op[0], BRW_REGISTER_TYPE_D),
18927ec681f3Smrg                      retype(op[1], BRW_REGISTER_TYPE_D));
18937ec681f3Smrg
18947ec681f3Smrg      if (instr->op == nir_op_sdot_4x8_iadd_sat)
18957ec681f3Smrg         inst->saturate = true;
18967ec681f3Smrg      break;
18977ec681f3Smrg
18987ec681f3Smrg   case nir_op_udot_4x8_uadd:
18997ec681f3Smrg   case nir_op_udot_4x8_uadd_sat:
19007ec681f3Smrg      inst = bld.DP4A(result,
19017ec681f3Smrg                      retype(op[2], BRW_REGISTER_TYPE_UD),
19027ec681f3Smrg                      retype(op[0], BRW_REGISTER_TYPE_UD),
19037ec681f3Smrg                      retype(op[1], BRW_REGISTER_TYPE_UD));
19047ec681f3Smrg
19057ec681f3Smrg      if (instr->op == nir_op_udot_4x8_uadd_sat)
19067ec681f3Smrg         inst->saturate = true;
19077ec681f3Smrg      break;
19087ec681f3Smrg
19097ec681f3Smrg   case nir_op_sudot_4x8_iadd:
19107ec681f3Smrg   case nir_op_sudot_4x8_iadd_sat:
19117ec681f3Smrg      inst = bld.DP4A(result,
19127ec681f3Smrg                      retype(op[2], BRW_REGISTER_TYPE_D),
19137ec681f3Smrg                      retype(op[0], BRW_REGISTER_TYPE_D),
19147ec681f3Smrg                      retype(op[1], BRW_REGISTER_TYPE_UD));
19157ec681f3Smrg
19167ec681f3Smrg      if (instr->op == nir_op_sudot_4x8_iadd_sat)
19177ec681f3Smrg         inst->saturate = true;
19187ec681f3Smrg      break;
19197ec681f3Smrg
192001e04c3fSmrg   case nir_op_ffma:
19217ec681f3Smrg      if (nir_has_any_rounding_mode_enabled(execution_mode)) {
19227ec681f3Smrg         brw_rnd_mode rnd =
19237ec681f3Smrg            brw_rnd_mode_from_execution_mode(execution_mode);
19247ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
19257ec681f3Smrg                  brw_imm_d(rnd));
19267ec681f3Smrg      }
19277ec681f3Smrg
192801e04c3fSmrg      inst = bld.MAD(result, op[2], op[1], op[0]);
192901e04c3fSmrg      break;
193001e04c3fSmrg
193101e04c3fSmrg   case nir_op_flrp:
19327ec681f3Smrg      if (nir_has_any_rounding_mode_enabled(execution_mode)) {
19337ec681f3Smrg         brw_rnd_mode rnd =
19347ec681f3Smrg            brw_rnd_mode_from_execution_mode(execution_mode);
19357ec681f3Smrg         bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
19367ec681f3Smrg                  brw_imm_d(rnd));
19377ec681f3Smrg      }
19387ec681f3Smrg
193901e04c3fSmrg      inst = bld.LRP(result, op[0], op[1], op[2]);
194001e04c3fSmrg      break;
194101e04c3fSmrg
19429f464c52Smaya   case nir_op_b32csel:
194301e04c3fSmrg      if (optimize_frontfacing_ternary(instr, result))
194401e04c3fSmrg         return;
194501e04c3fSmrg
194601e04c3fSmrg      bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
194701e04c3fSmrg      inst = bld.SEL(result, op[1], op[2]);
194801e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
194901e04c3fSmrg      break;
195001e04c3fSmrg
195101e04c3fSmrg   case nir_op_extract_u8:
195201e04c3fSmrg   case nir_op_extract_i8: {
19539f464c52Smaya      unsigned byte = nir_src_as_uint(instr->src[1].src);
195401e04c3fSmrg
195501e04c3fSmrg      /* The PRMs say:
195601e04c3fSmrg       *
195701e04c3fSmrg       *    BDW+
195801e04c3fSmrg       *    There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
195901e04c3fSmrg       *    Use two instructions and a word or DWord intermediate integer type.
196001e04c3fSmrg       */
196101e04c3fSmrg      if (nir_dest_bit_size(instr->dest.dest) == 64) {
1962993e1d59Smrg         const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
196301e04c3fSmrg
196401e04c3fSmrg         if (instr->op == nir_op_extract_i8) {
196501e04c3fSmrg            /* If we need to sign extend, extract to a word first */
196601e04c3fSmrg            fs_reg w_temp = bld.vgrf(BRW_REGISTER_TYPE_W);
19679f464c52Smaya            bld.MOV(w_temp, subscript(op[0], type, byte));
196801e04c3fSmrg            bld.MOV(result, w_temp);
19699f464c52Smaya         } else if (byte & 1) {
1970993e1d59Smrg            /* Extract the high byte from the word containing the desired byte
1971993e1d59Smrg             * offset.
1972993e1d59Smrg             */
1973993e1d59Smrg            bld.SHR(result,
19749f464c52Smaya                    subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1975993e1d59Smrg                    brw_imm_uw(8));
197601e04c3fSmrg         } else {
197701e04c3fSmrg            /* Otherwise use an AND with 0xff and a word type */
1978993e1d59Smrg            bld.AND(result,
19799f464c52Smaya                    subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1980993e1d59Smrg                    brw_imm_uw(0xff));
198101e04c3fSmrg         }
198201e04c3fSmrg      } else {
198301e04c3fSmrg         const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
19849f464c52Smaya         bld.MOV(result, subscript(op[0], type, byte));
198501e04c3fSmrg      }
198601e04c3fSmrg      break;
198701e04c3fSmrg   }
198801e04c3fSmrg
198901e04c3fSmrg   case nir_op_extract_u16:
199001e04c3fSmrg   case nir_op_extract_i16: {
199101e04c3fSmrg      const brw_reg_type type = brw_int_type(2, instr->op == nir_op_extract_i16);
19929f464c52Smaya      unsigned word = nir_src_as_uint(instr->src[1].src);
19939f464c52Smaya      bld.MOV(result, subscript(op[0], type, word));
199401e04c3fSmrg      break;
199501e04c3fSmrg   }
199601e04c3fSmrg
199701e04c3fSmrg   default:
199801e04c3fSmrg      unreachable("unhandled instruction");
199901e04c3fSmrg   }
200001e04c3fSmrg
200101e04c3fSmrg   /* If we need to do a boolean resolve, replace the result with -(x & 1)
200201e04c3fSmrg    * to sign extend the low bit to 0/~0
200301e04c3fSmrg    */
20047ec681f3Smrg   if (devinfo->ver <= 5 &&
20057ec681f3Smrg       !result.is_null() &&
200601e04c3fSmrg       (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
200701e04c3fSmrg      fs_reg masked = vgrf(glsl_type::int_type);
200801e04c3fSmrg      bld.AND(masked, result, brw_imm_d(1));
200901e04c3fSmrg      masked.negate = true;
201001e04c3fSmrg      bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
201101e04c3fSmrg   }
201201e04c3fSmrg}
201301e04c3fSmrg
201401e04c3fSmrgvoid
201501e04c3fSmrgfs_visitor::nir_emit_load_const(const fs_builder &bld,
201601e04c3fSmrg                                nir_load_const_instr *instr)
201701e04c3fSmrg{
201801e04c3fSmrg   const brw_reg_type reg_type =
201901e04c3fSmrg      brw_reg_type_from_bit_size(instr->def.bit_size, BRW_REGISTER_TYPE_D);
202001e04c3fSmrg   fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
202101e04c3fSmrg
202201e04c3fSmrg   switch (instr->def.bit_size) {
202301e04c3fSmrg   case 8:
202401e04c3fSmrg      for (unsigned i = 0; i < instr->def.num_components; i++)
20259f464c52Smaya         bld.MOV(offset(reg, bld, i), setup_imm_b(bld, instr->value[i].i8));
202601e04c3fSmrg      break;
202701e04c3fSmrg
202801e04c3fSmrg   case 16:
202901e04c3fSmrg      for (unsigned i = 0; i < instr->def.num_components; i++)
20309f464c52Smaya         bld.MOV(offset(reg, bld, i), brw_imm_w(instr->value[i].i16));
203101e04c3fSmrg      break;
203201e04c3fSmrg
203301e04c3fSmrg   case 32:
203401e04c3fSmrg      for (unsigned i = 0; i < instr->def.num_components; i++)
20359f464c52Smaya         bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value[i].i32));
203601e04c3fSmrg      break;
203701e04c3fSmrg
203801e04c3fSmrg   case 64:
20397ec681f3Smrg      assert(devinfo->ver >= 7);
20407ec681f3Smrg      if (devinfo->ver == 7) {
20417ec681f3Smrg         /* We don't get 64-bit integer types until gfx8 */
204201e04c3fSmrg         for (unsigned i = 0; i < instr->def.num_components; i++) {
204301e04c3fSmrg            bld.MOV(retype(offset(reg, bld, i), BRW_REGISTER_TYPE_DF),
20449f464c52Smaya                    setup_imm_df(bld, instr->value[i].f64));
204501e04c3fSmrg         }
204601e04c3fSmrg      } else {
204701e04c3fSmrg         for (unsigned i = 0; i < instr->def.num_components; i++)
20489f464c52Smaya            bld.MOV(offset(reg, bld, i), brw_imm_q(instr->value[i].i64));
204901e04c3fSmrg      }
205001e04c3fSmrg      break;
205101e04c3fSmrg
205201e04c3fSmrg   default:
205301e04c3fSmrg      unreachable("Invalid bit size");
205401e04c3fSmrg   }
205501e04c3fSmrg
205601e04c3fSmrg   nir_ssa_values[instr->def.index] = reg;
205701e04c3fSmrg}
205801e04c3fSmrg
205901e04c3fSmrgfs_reg
206001e04c3fSmrgfs_visitor::get_nir_src(const nir_src &src)
206101e04c3fSmrg{
206201e04c3fSmrg   fs_reg reg;
206301e04c3fSmrg   if (src.is_ssa) {
20647ec681f3Smrg      if (nir_src_is_undef(src)) {
206501e04c3fSmrg         const brw_reg_type reg_type =
206601e04c3fSmrg            brw_reg_type_from_bit_size(src.ssa->bit_size, BRW_REGISTER_TYPE_D);
206701e04c3fSmrg         reg = bld.vgrf(reg_type, src.ssa->num_components);
206801e04c3fSmrg      } else {
206901e04c3fSmrg         reg = nir_ssa_values[src.ssa->index];
207001e04c3fSmrg      }
207101e04c3fSmrg   } else {
207201e04c3fSmrg      /* We don't handle indirects on locals */
207301e04c3fSmrg      assert(src.reg.indirect == NULL);
207401e04c3fSmrg      reg = offset(nir_locals[src.reg.reg->index], bld,
207501e04c3fSmrg                   src.reg.base_offset * src.reg.reg->num_components);
207601e04c3fSmrg   }
207701e04c3fSmrg
20787ec681f3Smrg   if (nir_src_bit_size(src) == 64 && devinfo->ver == 7) {
20797ec681f3Smrg      /* The only 64-bit type available on gfx7 is DF, so use that. */
208001e04c3fSmrg      reg.type = BRW_REGISTER_TYPE_DF;
208101e04c3fSmrg   } else {
208201e04c3fSmrg      /* To avoid floating-point denorm flushing problems, set the type by
208301e04c3fSmrg       * default to an integer type - instructions that need floating point
208401e04c3fSmrg       * semantics will set this to F if they need to
208501e04c3fSmrg       */
208601e04c3fSmrg      reg.type = brw_reg_type_from_bit_size(nir_src_bit_size(src),
208701e04c3fSmrg                                            BRW_REGISTER_TYPE_D);
208801e04c3fSmrg   }
208901e04c3fSmrg
209001e04c3fSmrg   return reg;
209101e04c3fSmrg}
209201e04c3fSmrg
209301e04c3fSmrg/**
209401e04c3fSmrg * Return an IMM for constants; otherwise call get_nir_src() as normal.
209501e04c3fSmrg *
209601e04c3fSmrg * This function should not be called on any value which may be 64 bits.
20977ec681f3Smrg * We could theoretically support 64-bit on gfx8+ but we choose not to
20987ec681f3Smrg * because it wouldn't work in general (no gfx7 support) and there are
209901e04c3fSmrg * enough restrictions in 64-bit immediates that you can't take the return
210001e04c3fSmrg * value and treat it the same as the result of get_nir_src().
210101e04c3fSmrg */
210201e04c3fSmrgfs_reg
210301e04c3fSmrgfs_visitor::get_nir_src_imm(const nir_src &src)
210401e04c3fSmrg{
210501e04c3fSmrg   assert(nir_src_bit_size(src) == 32);
21069f464c52Smaya   return nir_src_is_const(src) ?
21079f464c52Smaya          fs_reg(brw_imm_d(nir_src_as_int(src))) : get_nir_src(src);
210801e04c3fSmrg}
210901e04c3fSmrg
211001e04c3fSmrgfs_reg
211101e04c3fSmrgfs_visitor::get_nir_dest(const nir_dest &dest)
211201e04c3fSmrg{
211301e04c3fSmrg   if (dest.is_ssa) {
211401e04c3fSmrg      const brw_reg_type reg_type =
211501e04c3fSmrg         brw_reg_type_from_bit_size(dest.ssa.bit_size,
211601e04c3fSmrg                                    dest.ssa.bit_size == 8 ?
211701e04c3fSmrg                                    BRW_REGISTER_TYPE_D :
211801e04c3fSmrg                                    BRW_REGISTER_TYPE_F);
211901e04c3fSmrg      nir_ssa_values[dest.ssa.index] =
212001e04c3fSmrg         bld.vgrf(reg_type, dest.ssa.num_components);
21217ec681f3Smrg      bld.UNDEF(nir_ssa_values[dest.ssa.index]);
212201e04c3fSmrg      return nir_ssa_values[dest.ssa.index];
212301e04c3fSmrg   } else {
212401e04c3fSmrg      /* We don't handle indirects on locals */
212501e04c3fSmrg      assert(dest.reg.indirect == NULL);
212601e04c3fSmrg      return offset(nir_locals[dest.reg.reg->index], bld,
212701e04c3fSmrg                    dest.reg.base_offset * dest.reg.reg->num_components);
212801e04c3fSmrg   }
212901e04c3fSmrg}
213001e04c3fSmrg
213101e04c3fSmrgvoid
213201e04c3fSmrgfs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
213301e04c3fSmrg                         unsigned wr_mask)
213401e04c3fSmrg{
213501e04c3fSmrg   for (unsigned i = 0; i < 4; i++) {
213601e04c3fSmrg      if (!((wr_mask >> i) & 1))
213701e04c3fSmrg         continue;
213801e04c3fSmrg
213901e04c3fSmrg      fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
214001e04c3fSmrg      new_inst->dst = offset(new_inst->dst, bld, i);
214101e04c3fSmrg      for (unsigned j = 0; j < new_inst->sources; j++)
214201e04c3fSmrg         if (new_inst->src[j].file == VGRF)
214301e04c3fSmrg            new_inst->src[j] = offset(new_inst->src[j], bld, i);
214401e04c3fSmrg
214501e04c3fSmrg      bld.emit(new_inst);
214601e04c3fSmrg   }
214701e04c3fSmrg}
214801e04c3fSmrg
214901e04c3fSmrgstatic fs_inst *
215001e04c3fSmrgemit_pixel_interpolater_send(const fs_builder &bld,
215101e04c3fSmrg                             enum opcode opcode,
215201e04c3fSmrg                             const fs_reg &dst,
215301e04c3fSmrg                             const fs_reg &src,
215401e04c3fSmrg                             const fs_reg &desc,
215501e04c3fSmrg                             glsl_interp_mode interpolation)
215601e04c3fSmrg{
215701e04c3fSmrg   struct brw_wm_prog_data *wm_prog_data =
215801e04c3fSmrg      brw_wm_prog_data(bld.shader->stage_prog_data);
215901e04c3fSmrg
216001e04c3fSmrg   fs_inst *inst = bld.emit(opcode, dst, src, desc);
216101e04c3fSmrg   /* 2 floats per slot returned */
216201e04c3fSmrg   inst->size_written = 2 * dst.component_size(inst->exec_size);
216301e04c3fSmrg   inst->pi_noperspective = interpolation == INTERP_MODE_NOPERSPECTIVE;
216401e04c3fSmrg
216501e04c3fSmrg   wm_prog_data->pulls_bary = true;
216601e04c3fSmrg
216701e04c3fSmrg   return inst;
216801e04c3fSmrg}
216901e04c3fSmrg
217001e04c3fSmrg/**
217101e04c3fSmrg * Computes 1 << x, given a D/UD register containing some value x.
217201e04c3fSmrg */
217301e04c3fSmrgstatic fs_reg
217401e04c3fSmrgintexp2(const fs_builder &bld, const fs_reg &x)
217501e04c3fSmrg{
217601e04c3fSmrg   assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
217701e04c3fSmrg
217801e04c3fSmrg   fs_reg result = bld.vgrf(x.type, 1);
217901e04c3fSmrg   fs_reg one = bld.vgrf(x.type, 1);
218001e04c3fSmrg
218101e04c3fSmrg   bld.MOV(one, retype(brw_imm_d(1), one.type));
218201e04c3fSmrg   bld.SHL(result, one, x);
218301e04c3fSmrg   return result;
218401e04c3fSmrg}
218501e04c3fSmrg
218601e04c3fSmrgvoid
218701e04c3fSmrgfs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
218801e04c3fSmrg{
218901e04c3fSmrg   assert(stage == MESA_SHADER_GEOMETRY);
219001e04c3fSmrg
219101e04c3fSmrg   struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
219201e04c3fSmrg
219301e04c3fSmrg   if (gs_compile->control_data_header_size_bits == 0)
219401e04c3fSmrg      return;
219501e04c3fSmrg
219601e04c3fSmrg   /* We can only do EndPrimitive() functionality when the control data
219701e04c3fSmrg    * consists of cut bits.  Fortunately, the only time it isn't is when the
219801e04c3fSmrg    * output type is points, in which case EndPrimitive() is a no-op.
219901e04c3fSmrg    */
220001e04c3fSmrg   if (gs_prog_data->control_data_format !=
22017ec681f3Smrg       GFX7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
220201e04c3fSmrg      return;
220301e04c3fSmrg   }
220401e04c3fSmrg
220501e04c3fSmrg   /* Cut bits use one bit per vertex. */
220601e04c3fSmrg   assert(gs_compile->control_data_bits_per_vertex == 1);
220701e04c3fSmrg
220801e04c3fSmrg   fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
220901e04c3fSmrg   vertex_count.type = BRW_REGISTER_TYPE_UD;
221001e04c3fSmrg
221101e04c3fSmrg   /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
221201e04c3fSmrg    * vertex n, 0 otherwise.  So all we need to do here is mark bit
221301e04c3fSmrg    * (vertex_count - 1) % 32 in the cut_bits register to indicate that
221401e04c3fSmrg    * EndPrimitive() was called after emitting vertex (vertex_count - 1);
221501e04c3fSmrg    * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
221601e04c3fSmrg    *
221701e04c3fSmrg    * Note that if EndPrimitive() is called before emitting any vertices, this
221801e04c3fSmrg    * will cause us to set bit 31 of the control_data_bits register to 1.
221901e04c3fSmrg    * That's fine because:
222001e04c3fSmrg    *
222101e04c3fSmrg    * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
222201e04c3fSmrg    *   output, so the hardware will ignore cut bit 31.
222301e04c3fSmrg    *
222401e04c3fSmrg    * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
222501e04c3fSmrg    *   last vertex, so setting cut bit 31 has no effect (since the primitive
222601e04c3fSmrg    *   is automatically ended when the GS terminates).
222701e04c3fSmrg    *
222801e04c3fSmrg    * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
222901e04c3fSmrg    *   control_data_bits register to 0 when the first vertex is emitted.
223001e04c3fSmrg    */
223101e04c3fSmrg
223201e04c3fSmrg   const fs_builder abld = bld.annotate("end primitive");
223301e04c3fSmrg
223401e04c3fSmrg   /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
223501e04c3fSmrg   fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
223601e04c3fSmrg   abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
223701e04c3fSmrg   fs_reg mask = intexp2(abld, prev_count);
223801e04c3fSmrg   /* Note: we're relying on the fact that the GEN SHL instruction only pays
223901e04c3fSmrg    * attention to the lower 5 bits of its second source argument, so on this
224001e04c3fSmrg    * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
224101e04c3fSmrg    * ((vertex_count - 1) % 32).
224201e04c3fSmrg    */
224301e04c3fSmrg   abld.OR(this->control_data_bits, this->control_data_bits, mask);
224401e04c3fSmrg}
224501e04c3fSmrg
224601e04c3fSmrgvoid
224701e04c3fSmrgfs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
224801e04c3fSmrg{
224901e04c3fSmrg   assert(stage == MESA_SHADER_GEOMETRY);
225001e04c3fSmrg   assert(gs_compile->control_data_bits_per_vertex != 0);
225101e04c3fSmrg
225201e04c3fSmrg   struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
225301e04c3fSmrg
225401e04c3fSmrg   const fs_builder abld = bld.annotate("emit control data bits");
225501e04c3fSmrg   const fs_builder fwa_bld = bld.exec_all();
225601e04c3fSmrg
225701e04c3fSmrg   /* We use a single UD register to accumulate control data bits (32 bits
225801e04c3fSmrg    * for each of the SIMD8 channels).  So we need to write a DWord (32 bits)
225901e04c3fSmrg    * at a time.
226001e04c3fSmrg    *
226101e04c3fSmrg    * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
226201e04c3fSmrg    * We have select a 128-bit group via the Global and Per-Slot Offsets, then
226301e04c3fSmrg    * use the Channel Mask phase to enable/disable which DWord within that
226401e04c3fSmrg    * group to write.  (Remember, different SIMD8 channels may have emitted
226501e04c3fSmrg    * different numbers of vertices, so we may need per-slot offsets.)
226601e04c3fSmrg    *
226701e04c3fSmrg    * Channel masking presents an annoying problem: we may have to replicate
226801e04c3fSmrg    * the data up to 4 times:
226901e04c3fSmrg    *
227001e04c3fSmrg    * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
227101e04c3fSmrg    *
227201e04c3fSmrg    * To avoid penalizing shaders that emit a small number of vertices, we
227301e04c3fSmrg    * can avoid these sometimes: if the size of the control data header is
227401e04c3fSmrg    * <= 128 bits, then there is only 1 OWord.  All SIMD8 channels will land
227501e04c3fSmrg    * land in the same 128-bit group, so we can skip per-slot offsets.
227601e04c3fSmrg    *
227701e04c3fSmrg    * Similarly, if the control data header is <= 32 bits, there is only one
227801e04c3fSmrg    * DWord, so we can skip channel masks.
227901e04c3fSmrg    */
228001e04c3fSmrg   enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
228101e04c3fSmrg
228201e04c3fSmrg   fs_reg channel_mask, per_slot_offset;
228301e04c3fSmrg
228401e04c3fSmrg   if (gs_compile->control_data_header_size_bits > 32) {
228501e04c3fSmrg      opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
228601e04c3fSmrg      channel_mask = vgrf(glsl_type::uint_type);
228701e04c3fSmrg   }
228801e04c3fSmrg
228901e04c3fSmrg   if (gs_compile->control_data_header_size_bits > 128) {
229001e04c3fSmrg      opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
229101e04c3fSmrg      per_slot_offset = vgrf(glsl_type::uint_type);
229201e04c3fSmrg   }
229301e04c3fSmrg
229401e04c3fSmrg   /* Figure out which DWord we're trying to write to using the formula:
229501e04c3fSmrg    *
229601e04c3fSmrg    *    dword_index = (vertex_count - 1) * bits_per_vertex / 32
229701e04c3fSmrg    *
229801e04c3fSmrg    * Since bits_per_vertex is a power of two, and is known at compile
229901e04c3fSmrg    * time, this can be optimized to:
230001e04c3fSmrg    *
230101e04c3fSmrg    *    dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
230201e04c3fSmrg    */
230301e04c3fSmrg   if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
230401e04c3fSmrg      fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
230501e04c3fSmrg      fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
230601e04c3fSmrg      abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
230701e04c3fSmrg      unsigned log2_bits_per_vertex =
230801e04c3fSmrg         util_last_bit(gs_compile->control_data_bits_per_vertex);
230901e04c3fSmrg      abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
231001e04c3fSmrg
231101e04c3fSmrg      if (per_slot_offset.file != BAD_FILE) {
231201e04c3fSmrg         /* Set the per-slot offset to dword_index / 4, so that we'll write to
231301e04c3fSmrg          * the appropriate OWord within the control data header.
231401e04c3fSmrg          */
231501e04c3fSmrg         abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
231601e04c3fSmrg      }
231701e04c3fSmrg
231801e04c3fSmrg      /* Set the channel masks to 1 << (dword_index % 4), so that we'll
231901e04c3fSmrg       * write to the appropriate DWORD within the OWORD.
232001e04c3fSmrg       */
232101e04c3fSmrg      fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
232201e04c3fSmrg      fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
232301e04c3fSmrg      channel_mask = intexp2(fwa_bld, channel);
232401e04c3fSmrg      /* Then the channel masks need to be in bits 23:16. */
232501e04c3fSmrg      fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
232601e04c3fSmrg   }
232701e04c3fSmrg
232801e04c3fSmrg   /* Store the control data bits in the message payload and send it. */
23299f464c52Smaya   unsigned mlen = 2;
233001e04c3fSmrg   if (channel_mask.file != BAD_FILE)
233101e04c3fSmrg      mlen += 4; /* channel masks, plus 3 extra copies of the data */
233201e04c3fSmrg   if (per_slot_offset.file != BAD_FILE)
233301e04c3fSmrg      mlen++;
233401e04c3fSmrg
233501e04c3fSmrg   fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
233601e04c3fSmrg   fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
23379f464c52Smaya   unsigned i = 0;
233801e04c3fSmrg   sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
233901e04c3fSmrg   if (per_slot_offset.file != BAD_FILE)
234001e04c3fSmrg      sources[i++] = per_slot_offset;
234101e04c3fSmrg   if (channel_mask.file != BAD_FILE)
234201e04c3fSmrg      sources[i++] = channel_mask;
234301e04c3fSmrg   while (i < mlen) {
234401e04c3fSmrg      sources[i++] = this->control_data_bits;
234501e04c3fSmrg   }
234601e04c3fSmrg
234701e04c3fSmrg   abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
234801e04c3fSmrg   fs_inst *inst = abld.emit(opcode, reg_undef, payload);
234901e04c3fSmrg   inst->mlen = mlen;
235001e04c3fSmrg   /* We need to increment Global Offset by 256-bits to make room for
235101e04c3fSmrg    * Broadwell's extra "Vertex Count" payload at the beginning of the
235201e04c3fSmrg    * URB entry.  Since this is an OWord message, Global Offset is counted
235301e04c3fSmrg    * in 128-bit units, so we must set it to 2.
235401e04c3fSmrg    */
235501e04c3fSmrg   if (gs_prog_data->static_vertex_count == -1)
235601e04c3fSmrg      inst->offset = 2;
235701e04c3fSmrg}
235801e04c3fSmrg
235901e04c3fSmrgvoid
236001e04c3fSmrgfs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
236101e04c3fSmrg                                            unsigned stream_id)
236201e04c3fSmrg{
236301e04c3fSmrg   /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
236401e04c3fSmrg
236501e04c3fSmrg   /* Note: we are calling this *before* increasing vertex_count, so
236601e04c3fSmrg    * this->vertex_count == vertex_count - 1 in the formula above.
236701e04c3fSmrg    */
236801e04c3fSmrg
236901e04c3fSmrg   /* Stream mode uses 2 bits per vertex */
237001e04c3fSmrg   assert(gs_compile->control_data_bits_per_vertex == 2);
237101e04c3fSmrg
237201e04c3fSmrg   /* Must be a valid stream */
237301e04c3fSmrg   assert(stream_id < MAX_VERTEX_STREAMS);
237401e04c3fSmrg
237501e04c3fSmrg   /* Control data bits are initialized to 0 so we don't have to set any
237601e04c3fSmrg    * bits when sending vertices to stream 0.
237701e04c3fSmrg    */
237801e04c3fSmrg   if (stream_id == 0)
237901e04c3fSmrg      return;
238001e04c3fSmrg
238101e04c3fSmrg   const fs_builder abld = bld.annotate("set stream control data bits", NULL);
238201e04c3fSmrg
238301e04c3fSmrg   /* reg::sid = stream_id */
238401e04c3fSmrg   fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
238501e04c3fSmrg   abld.MOV(sid, brw_imm_ud(stream_id));
238601e04c3fSmrg
238701e04c3fSmrg   /* reg:shift_count = 2 * (vertex_count - 1) */
238801e04c3fSmrg   fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
238901e04c3fSmrg   abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
239001e04c3fSmrg
239101e04c3fSmrg   /* Note: we're relying on the fact that the GEN SHL instruction only pays
239201e04c3fSmrg    * attention to the lower 5 bits of its second source argument, so on this
239301e04c3fSmrg    * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
239401e04c3fSmrg    * stream_id << ((2 * (vertex_count - 1)) % 32).
239501e04c3fSmrg    */
239601e04c3fSmrg   fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
239701e04c3fSmrg   abld.SHL(mask, sid, shift_count);
239801e04c3fSmrg   abld.OR(this->control_data_bits, this->control_data_bits, mask);
239901e04c3fSmrg}
240001e04c3fSmrg
240101e04c3fSmrgvoid
240201e04c3fSmrgfs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
240301e04c3fSmrg                           unsigned stream_id)
240401e04c3fSmrg{
240501e04c3fSmrg   assert(stage == MESA_SHADER_GEOMETRY);
240601e04c3fSmrg
240701e04c3fSmrg   struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
240801e04c3fSmrg
240901e04c3fSmrg   fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
241001e04c3fSmrg   vertex_count.type = BRW_REGISTER_TYPE_UD;
241101e04c3fSmrg
241201e04c3fSmrg   /* Haswell and later hardware ignores the "Render Stream Select" bits
241301e04c3fSmrg    * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
241401e04c3fSmrg    * and instead sends all primitives down the pipeline for rasterization.
241501e04c3fSmrg    * If the SOL stage is enabled, "Render Stream Select" is honored and
241601e04c3fSmrg    * primitives bound to non-zero streams are discarded after stream output.
241701e04c3fSmrg    *
241801e04c3fSmrg    * Since the only purpose of primives sent to non-zero streams is to
241901e04c3fSmrg    * be recorded by transform feedback, we can simply discard all geometry
242001e04c3fSmrg    * bound to these streams when transform feedback is disabled.
242101e04c3fSmrg    */
242201e04c3fSmrg   if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
242301e04c3fSmrg      return;
242401e04c3fSmrg
242501e04c3fSmrg   /* If we're outputting 32 control data bits or less, then we can wait
242601e04c3fSmrg    * until the shader is over to output them all.  Otherwise we need to
242701e04c3fSmrg    * output them as we go.  Now is the time to do it, since we're about to
242801e04c3fSmrg    * output the vertex_count'th vertex, so it's guaranteed that the
242901e04c3fSmrg    * control data bits associated with the (vertex_count - 1)th vertex are
243001e04c3fSmrg    * correct.
243101e04c3fSmrg    */
243201e04c3fSmrg   if (gs_compile->control_data_header_size_bits > 32) {
243301e04c3fSmrg      const fs_builder abld =
243401e04c3fSmrg         bld.annotate("emit vertex: emit control data bits");
243501e04c3fSmrg
243601e04c3fSmrg      /* Only emit control data bits if we've finished accumulating a batch
243701e04c3fSmrg       * of 32 bits.  This is the case when:
243801e04c3fSmrg       *
243901e04c3fSmrg       *     (vertex_count * bits_per_vertex) % 32 == 0
244001e04c3fSmrg       *
244101e04c3fSmrg       * (in other words, when the last 5 bits of vertex_count *
244201e04c3fSmrg       * bits_per_vertex are 0).  Assuming bits_per_vertex == 2^n for some
244301e04c3fSmrg       * integer n (which is always the case, since bits_per_vertex is
244401e04c3fSmrg       * always 1 or 2), this is equivalent to requiring that the last 5-n
244501e04c3fSmrg       * bits of vertex_count are 0:
244601e04c3fSmrg       *
244701e04c3fSmrg       *     vertex_count & (2^(5-n) - 1) == 0
244801e04c3fSmrg       *
244901e04c3fSmrg       * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
245001e04c3fSmrg       * equivalent to:
245101e04c3fSmrg       *
245201e04c3fSmrg       *     vertex_count & (32 / bits_per_vertex - 1) == 0
245301e04c3fSmrg       *
245401e04c3fSmrg       * TODO: If vertex_count is an immediate, we could do some of this math
245501e04c3fSmrg       *       at compile time...
245601e04c3fSmrg       */
245701e04c3fSmrg      fs_inst *inst =
245801e04c3fSmrg         abld.AND(bld.null_reg_d(), vertex_count,
245901e04c3fSmrg                  brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
246001e04c3fSmrg      inst->conditional_mod = BRW_CONDITIONAL_Z;
246101e04c3fSmrg
246201e04c3fSmrg      abld.IF(BRW_PREDICATE_NORMAL);
246301e04c3fSmrg      /* If vertex_count is 0, then no control data bits have been
246401e04c3fSmrg       * accumulated yet, so we can skip emitting them.
246501e04c3fSmrg       */
246601e04c3fSmrg      abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
246701e04c3fSmrg               BRW_CONDITIONAL_NEQ);
246801e04c3fSmrg      abld.IF(BRW_PREDICATE_NORMAL);
246901e04c3fSmrg      emit_gs_control_data_bits(vertex_count);
247001e04c3fSmrg      abld.emit(BRW_OPCODE_ENDIF);
247101e04c3fSmrg
247201e04c3fSmrg      /* Reset control_data_bits to 0 so we can start accumulating a new
247301e04c3fSmrg       * batch.
247401e04c3fSmrg       *
247501e04c3fSmrg       * Note: in the case where vertex_count == 0, this neutralizes the
247601e04c3fSmrg       * effect of any call to EndPrimitive() that the shader may have
247701e04c3fSmrg       * made before outputting its first vertex.
247801e04c3fSmrg       */
247901e04c3fSmrg      inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
248001e04c3fSmrg      inst->force_writemask_all = true;
248101e04c3fSmrg      abld.emit(BRW_OPCODE_ENDIF);
248201e04c3fSmrg   }
248301e04c3fSmrg
248401e04c3fSmrg   emit_urb_writes(vertex_count);
248501e04c3fSmrg
248601e04c3fSmrg   /* In stream mode we have to set control data bits for all vertices
248701e04c3fSmrg    * unless we have disabled control data bits completely (which we do
248801e04c3fSmrg    * do for GL_POINTS outputs that don't use streams).
248901e04c3fSmrg    */
249001e04c3fSmrg   if (gs_compile->control_data_header_size_bits > 0 &&
249101e04c3fSmrg       gs_prog_data->control_data_format ==
24927ec681f3Smrg          GFX7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
249301e04c3fSmrg      set_gs_stream_control_data_bits(vertex_count, stream_id);
249401e04c3fSmrg   }
249501e04c3fSmrg}
249601e04c3fSmrg
249701e04c3fSmrgvoid
249801e04c3fSmrgfs_visitor::emit_gs_input_load(const fs_reg &dst,
249901e04c3fSmrg                               const nir_src &vertex_src,
250001e04c3fSmrg                               unsigned base_offset,
250101e04c3fSmrg                               const nir_src &offset_src,
250201e04c3fSmrg                               unsigned num_components,
250301e04c3fSmrg                               unsigned first_component)
250401e04c3fSmrg{
25057ec681f3Smrg   assert(type_sz(dst.type) == 4);
250601e04c3fSmrg   struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
250701e04c3fSmrg   const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
250801e04c3fSmrg
250901e04c3fSmrg   /* TODO: figure out push input layout for invocations == 1 */
251001e04c3fSmrg   if (gs_prog_data->invocations == 1 &&
25119f464c52Smaya       nir_src_is_const(offset_src) && nir_src_is_const(vertex_src) &&
25129f464c52Smaya       4 * (base_offset + nir_src_as_uint(offset_src)) < push_reg_count) {
25139f464c52Smaya      int imm_offset = (base_offset + nir_src_as_uint(offset_src)) * 4 +
25149f464c52Smaya                       nir_src_as_uint(vertex_src) * push_reg_count;
251501e04c3fSmrg      for (unsigned i = 0; i < num_components; i++) {
251601e04c3fSmrg         bld.MOV(offset(dst, bld, i),
251701e04c3fSmrg                 fs_reg(ATTR, imm_offset + i + first_component, dst.type));
251801e04c3fSmrg      }
251901e04c3fSmrg      return;
252001e04c3fSmrg   }
252101e04c3fSmrg
252201e04c3fSmrg   /* Resort to the pull model.  Ensure the VUE handles are provided. */
252301e04c3fSmrg   assert(gs_prog_data->base.include_vue_handles);
252401e04c3fSmrg
252501e04c3fSmrg   unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
252601e04c3fSmrg   fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
252701e04c3fSmrg
252801e04c3fSmrg   if (gs_prog_data->invocations == 1) {
25299f464c52Smaya      if (nir_src_is_const(vertex_src)) {
253001e04c3fSmrg         /* The vertex index is constant; just select the proper URB handle. */
253101e04c3fSmrg         icp_handle =
25329f464c52Smaya            retype(brw_vec8_grf(first_icp_handle + nir_src_as_uint(vertex_src), 0),
253301e04c3fSmrg                   BRW_REGISTER_TYPE_UD);
253401e04c3fSmrg      } else {
253501e04c3fSmrg         /* The vertex index is non-constant.  We need to use indirect
253601e04c3fSmrg          * addressing to fetch the proper URB handle.
253701e04c3fSmrg          *
253801e04c3fSmrg          * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
253901e04c3fSmrg          * indicating that channel <n> should read the handle from
254001e04c3fSmrg          * DWord <n>.  We convert that to bytes by multiplying by 4.
254101e04c3fSmrg          *
254201e04c3fSmrg          * Next, we convert the vertex index to bytes by multiplying
254301e04c3fSmrg          * by 32 (shifting by 5), and add the two together.  This is
254401e04c3fSmrg          * the final indirect byte offset.
254501e04c3fSmrg          */
254601e04c3fSmrg         fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
254701e04c3fSmrg         fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
254801e04c3fSmrg         fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
254901e04c3fSmrg         fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
255001e04c3fSmrg
255101e04c3fSmrg         /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
255201e04c3fSmrg         bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
255301e04c3fSmrg         /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
255401e04c3fSmrg         bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
255501e04c3fSmrg         /* Convert vertex_index to bytes (multiply by 32) */
255601e04c3fSmrg         bld.SHL(vertex_offset_bytes,
255701e04c3fSmrg                 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
255801e04c3fSmrg                 brw_imm_ud(5u));
255901e04c3fSmrg         bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
256001e04c3fSmrg
256101e04c3fSmrg         /* Use first_icp_handle as the base offset.  There is one register
256201e04c3fSmrg          * of URB handles per vertex, so inform the register allocator that
256301e04c3fSmrg          * we might read up to nir->info.gs.vertices_in registers.
256401e04c3fSmrg          */
256501e04c3fSmrg         bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
256601e04c3fSmrg                  retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
256701e04c3fSmrg                  fs_reg(icp_offset_bytes),
256801e04c3fSmrg                  brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
256901e04c3fSmrg      }
257001e04c3fSmrg   } else {
257101e04c3fSmrg      assert(gs_prog_data->invocations > 1);
257201e04c3fSmrg
25739f464c52Smaya      if (nir_src_is_const(vertex_src)) {
25749f464c52Smaya         unsigned vertex = nir_src_as_uint(vertex_src);
25757ec681f3Smrg         assert(devinfo->ver >= 9 || vertex <= 5);
257601e04c3fSmrg         bld.MOV(icp_handle,
25779f464c52Smaya                 retype(brw_vec1_grf(first_icp_handle + vertex / 8, vertex % 8),
257801e04c3fSmrg                        BRW_REGISTER_TYPE_UD));
257901e04c3fSmrg      } else {
258001e04c3fSmrg         /* The vertex index is non-constant.  We need to use indirect
258101e04c3fSmrg          * addressing to fetch the proper URB handle.
258201e04c3fSmrg          *
258301e04c3fSmrg          */
258401e04c3fSmrg         fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
258501e04c3fSmrg
258601e04c3fSmrg         /* Convert vertex_index to bytes (multiply by 4) */
258701e04c3fSmrg         bld.SHL(icp_offset_bytes,
258801e04c3fSmrg                 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
258901e04c3fSmrg                 brw_imm_ud(2u));
259001e04c3fSmrg
259101e04c3fSmrg         /* Use first_icp_handle as the base offset.  There is one DWord
259201e04c3fSmrg          * of URB handles per vertex, so inform the register allocator that
259301e04c3fSmrg          * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
259401e04c3fSmrg          */
259501e04c3fSmrg         bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
259601e04c3fSmrg                  retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
259701e04c3fSmrg                  fs_reg(icp_offset_bytes),
259801e04c3fSmrg                  brw_imm_ud(DIV_ROUND_UP(nir->info.gs.vertices_in, 8) *
259901e04c3fSmrg                             REG_SIZE));
260001e04c3fSmrg      }
260101e04c3fSmrg   }
260201e04c3fSmrg
260301e04c3fSmrg   fs_inst *inst;
260401e04c3fSmrg   fs_reg indirect_offset = get_nir_src(offset_src);
260501e04c3fSmrg
26067ec681f3Smrg   if (nir_src_is_const(offset_src)) {
26077ec681f3Smrg      /* Constant indexing - use global offset. */
26087ec681f3Smrg      if (first_component != 0) {
260901e04c3fSmrg         unsigned read_components = num_components + first_component;
261001e04c3fSmrg         fs_reg tmp = bld.vgrf(dst.type, read_components);
26117ec681f3Smrg         inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
26127ec681f3Smrg         inst->size_written = read_components *
26137ec681f3Smrg                              tmp.component_size(inst->exec_size);
26147ec681f3Smrg         for (unsigned i = 0; i < num_components; i++) {
26157ec681f3Smrg            bld.MOV(offset(dst, bld, i),
26167ec681f3Smrg                    offset(tmp, bld, i + first_component));
261701e04c3fSmrg         }
26187ec681f3Smrg      } else {
26197ec681f3Smrg         inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
26207ec681f3Smrg         inst->size_written = num_components *
26217ec681f3Smrg                              dst.component_size(inst->exec_size);
262201e04c3fSmrg      }
26237ec681f3Smrg      inst->offset = base_offset + nir_src_as_uint(offset_src);
26247ec681f3Smrg      inst->mlen = 1;
26257ec681f3Smrg   } else {
26267ec681f3Smrg      /* Indirect indexing - use per-slot offsets as well. */
26277ec681f3Smrg      const fs_reg srcs[] = { icp_handle, indirect_offset };
26287ec681f3Smrg      unsigned read_components = num_components + first_component;
26297ec681f3Smrg      fs_reg tmp = bld.vgrf(dst.type, read_components);
26307ec681f3Smrg      fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
26317ec681f3Smrg      bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
26327ec681f3Smrg      if (first_component != 0) {
26337ec681f3Smrg         inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
26347ec681f3Smrg                         payload);
26357ec681f3Smrg         inst->size_written = read_components *
26367ec681f3Smrg                              tmp.component_size(inst->exec_size);
26377ec681f3Smrg         for (unsigned i = 0; i < num_components; i++) {
26387ec681f3Smrg            bld.MOV(offset(dst, bld, i),
26397ec681f3Smrg                    offset(tmp, bld, i + first_component));
264001e04c3fSmrg         }
26417ec681f3Smrg      } else {
26427ec681f3Smrg         inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
26437ec681f3Smrg         inst->size_written = num_components *
26447ec681f3Smrg                              dst.component_size(inst->exec_size);
264501e04c3fSmrg      }
26467ec681f3Smrg      inst->offset = base_offset;
26477ec681f3Smrg      inst->mlen = 2;
264801e04c3fSmrg   }
264901e04c3fSmrg}
265001e04c3fSmrg
265101e04c3fSmrgfs_reg
265201e04c3fSmrgfs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
265301e04c3fSmrg{
265401e04c3fSmrg   nir_src *offset_src = nir_get_io_offset_src(instr);
265501e04c3fSmrg
26569f464c52Smaya   if (nir_src_is_const(*offset_src)) {
265701e04c3fSmrg      /* The only constant offset we should find is 0.  brw_nir.c's
265801e04c3fSmrg       * add_const_offset_to_base() will fold other constant offsets
265901e04c3fSmrg       * into instr->const_index[0].
266001e04c3fSmrg       */
26619f464c52Smaya      assert(nir_src_as_uint(*offset_src) == 0);
266201e04c3fSmrg      return fs_reg();
266301e04c3fSmrg   }
266401e04c3fSmrg
266501e04c3fSmrg   return get_nir_src(*offset_src);
266601e04c3fSmrg}
266701e04c3fSmrg
266801e04c3fSmrgvoid
266901e04c3fSmrgfs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
267001e04c3fSmrg                                  nir_intrinsic_instr *instr)
267101e04c3fSmrg{
267201e04c3fSmrg   assert(stage == MESA_SHADER_VERTEX);
267301e04c3fSmrg
267401e04c3fSmrg   fs_reg dest;
267501e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
267601e04c3fSmrg      dest = get_nir_dest(instr->dest);
267701e04c3fSmrg
267801e04c3fSmrg   switch (instr->intrinsic) {
267901e04c3fSmrg   case nir_intrinsic_load_vertex_id:
268001e04c3fSmrg   case nir_intrinsic_load_base_vertex:
268101e04c3fSmrg      unreachable("should be lowered by nir_lower_system_values()");
268201e04c3fSmrg
268301e04c3fSmrg   case nir_intrinsic_load_input: {
26847ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
268501e04c3fSmrg      fs_reg src = fs_reg(ATTR, nir_intrinsic_base(instr) * 4, dest.type);
26867ec681f3Smrg      src = offset(src, bld, nir_intrinsic_component(instr));
26879f464c52Smaya      src = offset(src, bld, nir_src_as_uint(instr->src[0]));
268801e04c3fSmrg
26897ec681f3Smrg      for (unsigned i = 0; i < instr->num_components; i++)
26907ec681f3Smrg         bld.MOV(offset(dest, bld, i), offset(src, bld, i));
269101e04c3fSmrg      break;
269201e04c3fSmrg   }
269301e04c3fSmrg
269401e04c3fSmrg   case nir_intrinsic_load_vertex_id_zero_base:
269501e04c3fSmrg   case nir_intrinsic_load_instance_id:
269601e04c3fSmrg   case nir_intrinsic_load_base_instance:
269701e04c3fSmrg   case nir_intrinsic_load_draw_id:
269801e04c3fSmrg   case nir_intrinsic_load_first_vertex:
269901e04c3fSmrg   case nir_intrinsic_load_is_indexed_draw:
270001e04c3fSmrg      unreachable("lowered by brw_nir_lower_vs_inputs");
270101e04c3fSmrg
270201e04c3fSmrg   default:
270301e04c3fSmrg      nir_emit_intrinsic(bld, instr);
270401e04c3fSmrg      break;
270501e04c3fSmrg   }
270601e04c3fSmrg}
270701e04c3fSmrg
27087ec681f3Smrgfs_reg
27097ec681f3Smrgfs_visitor::get_tcs_single_patch_icp_handle(const fs_builder &bld,
27107ec681f3Smrg                                            nir_intrinsic_instr *instr)
271101e04c3fSmrg{
271201e04c3fSmrg   struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
27137ec681f3Smrg   const nir_src &vertex_src = instr->src[0];
27147ec681f3Smrg   nir_intrinsic_instr *vertex_intrin = nir_src_as_intrinsic(vertex_src);
27157ec681f3Smrg   fs_reg icp_handle;
27167ec681f3Smrg
27177ec681f3Smrg   if (nir_src_is_const(vertex_src)) {
27187ec681f3Smrg      /* Emit a MOV to resolve <0,1,0> regioning. */
27197ec681f3Smrg      icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27207ec681f3Smrg      unsigned vertex = nir_src_as_uint(vertex_src);
27217ec681f3Smrg      bld.MOV(icp_handle,
27227ec681f3Smrg              retype(brw_vec1_grf(1 + (vertex >> 3), vertex & 7),
27237ec681f3Smrg                     BRW_REGISTER_TYPE_UD));
27247ec681f3Smrg   } else if (tcs_prog_data->instances == 1 && vertex_intrin &&
27257ec681f3Smrg              vertex_intrin->intrinsic == nir_intrinsic_load_invocation_id) {
27267ec681f3Smrg      /* For the common case of only 1 instance, an array index of
27277ec681f3Smrg       * gl_InvocationID means reading g1.  Skip all the indirect work.
27287ec681f3Smrg       */
27297ec681f3Smrg      icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
27307ec681f3Smrg   } else {
27317ec681f3Smrg      /* The vertex index is non-constant.  We need to use indirect
27327ec681f3Smrg       * addressing to fetch the proper URB handle.
27337ec681f3Smrg       */
27347ec681f3Smrg      icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27357ec681f3Smrg
27367ec681f3Smrg      /* Each ICP handle is a single DWord (4 bytes) */
27377ec681f3Smrg      fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27387ec681f3Smrg      bld.SHL(vertex_offset_bytes,
27397ec681f3Smrg              retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
27407ec681f3Smrg              brw_imm_ud(2u));
27417ec681f3Smrg
27427ec681f3Smrg      /* Start at g1.  We might read up to 4 registers. */
27437ec681f3Smrg      bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
27447ec681f3Smrg               retype(brw_vec8_grf(1, 0), icp_handle.type), vertex_offset_bytes,
27457ec681f3Smrg               brw_imm_ud(4 * REG_SIZE));
27467ec681f3Smrg   }
27477ec681f3Smrg
27487ec681f3Smrg   return icp_handle;
27497ec681f3Smrg}
27507ec681f3Smrg
27517ec681f3Smrgfs_reg
27527ec681f3Smrgfs_visitor::get_tcs_eight_patch_icp_handle(const fs_builder &bld,
27537ec681f3Smrg                                           nir_intrinsic_instr *instr)
27547ec681f3Smrg{
27557ec681f3Smrg   struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
27567ec681f3Smrg   struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
27577ec681f3Smrg   const nir_src &vertex_src = instr->src[0];
27587ec681f3Smrg
27597ec681f3Smrg   unsigned first_icp_handle = tcs_prog_data->include_primitive_id ? 3 : 2;
27607ec681f3Smrg
27617ec681f3Smrg   if (nir_src_is_const(vertex_src)) {
27627ec681f3Smrg      return fs_reg(retype(brw_vec8_grf(first_icp_handle +
27637ec681f3Smrg                                        nir_src_as_uint(vertex_src), 0),
27647ec681f3Smrg                           BRW_REGISTER_TYPE_UD));
27657ec681f3Smrg   }
27667ec681f3Smrg
27677ec681f3Smrg   /* The vertex index is non-constant.  We need to use indirect
27687ec681f3Smrg    * addressing to fetch the proper URB handle.
27697ec681f3Smrg    *
27707ec681f3Smrg    * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
27717ec681f3Smrg    * indicating that channel <n> should read the handle from
27727ec681f3Smrg    * DWord <n>.  We convert that to bytes by multiplying by 4.
27737ec681f3Smrg    *
27747ec681f3Smrg    * Next, we convert the vertex index to bytes by multiplying
27757ec681f3Smrg    * by 32 (shifting by 5), and add the two together.  This is
27767ec681f3Smrg    * the final indirect byte offset.
27777ec681f3Smrg    */
27787ec681f3Smrg   fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27797ec681f3Smrg   fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
27807ec681f3Smrg   fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27817ec681f3Smrg   fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27827ec681f3Smrg   fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
27837ec681f3Smrg
27847ec681f3Smrg   /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
27857ec681f3Smrg   bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
27867ec681f3Smrg   /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
27877ec681f3Smrg   bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
27887ec681f3Smrg   /* Convert vertex_index to bytes (multiply by 32) */
27897ec681f3Smrg   bld.SHL(vertex_offset_bytes,
27907ec681f3Smrg           retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
27917ec681f3Smrg           brw_imm_ud(5u));
27927ec681f3Smrg   bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
27937ec681f3Smrg
27947ec681f3Smrg   /* Use first_icp_handle as the base offset.  There is one register
27957ec681f3Smrg    * of URB handles per vertex, so inform the register allocator that
27967ec681f3Smrg    * we might read up to nir->info.gs.vertices_in registers.
27977ec681f3Smrg    */
27987ec681f3Smrg   bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
27997ec681f3Smrg            retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
28007ec681f3Smrg            icp_offset_bytes, brw_imm_ud(tcs_key->input_vertices * REG_SIZE));
28017ec681f3Smrg
28027ec681f3Smrg   return icp_handle;
28037ec681f3Smrg}
28047ec681f3Smrg
28057ec681f3Smrgstruct brw_reg
28067ec681f3Smrgfs_visitor::get_tcs_output_urb_handle()
28077ec681f3Smrg{
28087ec681f3Smrg   struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
28097ec681f3Smrg
28107ec681f3Smrg   if (vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH) {
28117ec681f3Smrg      return retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
28127ec681f3Smrg   } else {
28137ec681f3Smrg      assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH);
28147ec681f3Smrg      return retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
28157ec681f3Smrg   }
28167ec681f3Smrg}
28177ec681f3Smrg
28187ec681f3Smrgvoid
28197ec681f3Smrgfs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
28207ec681f3Smrg                                   nir_intrinsic_instr *instr)
28217ec681f3Smrg{
28227ec681f3Smrg   assert(stage == MESA_SHADER_TESS_CTRL);
28237ec681f3Smrg   struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
28247ec681f3Smrg   struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
28257ec681f3Smrg   struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
28267ec681f3Smrg
28277ec681f3Smrg   bool eight_patch =
28287ec681f3Smrg      vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH;
28297ec681f3Smrg
283001e04c3fSmrg   fs_reg dst;
283101e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
283201e04c3fSmrg      dst = get_nir_dest(instr->dest);
283301e04c3fSmrg
283401e04c3fSmrg   switch (instr->intrinsic) {
283501e04c3fSmrg   case nir_intrinsic_load_primitive_id:
28367ec681f3Smrg      bld.MOV(dst, fs_reg(eight_patch ? brw_vec8_grf(2, 0)
28377ec681f3Smrg                                      : brw_vec1_grf(0, 1)));
283801e04c3fSmrg      break;
283901e04c3fSmrg   case nir_intrinsic_load_invocation_id:
284001e04c3fSmrg      bld.MOV(retype(dst, invocation_id.type), invocation_id);
284101e04c3fSmrg      break;
284201e04c3fSmrg   case nir_intrinsic_load_patch_vertices_in:
284301e04c3fSmrg      bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
284401e04c3fSmrg              brw_imm_d(tcs_key->input_vertices));
284501e04c3fSmrg      break;
284601e04c3fSmrg
28477ec681f3Smrg   case nir_intrinsic_control_barrier: {
284801e04c3fSmrg      if (tcs_prog_data->instances == 1)
284901e04c3fSmrg         break;
285001e04c3fSmrg
285101e04c3fSmrg      fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
285201e04c3fSmrg      fs_reg m0_2 = component(m0, 2);
285301e04c3fSmrg
285401e04c3fSmrg      const fs_builder chanbld = bld.exec_all().group(1, 0);
285501e04c3fSmrg
285601e04c3fSmrg      /* Zero the message header */
285701e04c3fSmrg      bld.exec_all().MOV(m0, brw_imm_ud(0u));
285801e04c3fSmrg
28597ec681f3Smrg      if (devinfo->verx10 >= 125) {
28607ec681f3Smrg         /* From BSpec: 54006, mov r0.2[31:24] into m0.2[31:24] and m0.2[23:16] */
28617ec681f3Smrg         fs_reg m0_10ub = component(retype(m0, BRW_REGISTER_TYPE_UB), 10);
28627ec681f3Smrg         fs_reg r0_11ub =
28637ec681f3Smrg            stride(suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UB), 11),
28647ec681f3Smrg                   0, 1, 0);
28657ec681f3Smrg         bld.exec_all().group(2, 0).MOV(m0_10ub, r0_11ub);
28667ec681f3Smrg      } else if (devinfo->ver >= 11) {
28677ec681f3Smrg         chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
28687ec681f3Smrg                     brw_imm_ud(INTEL_MASK(30, 24)));
28697ec681f3Smrg
28707ec681f3Smrg         /* Set the Barrier Count and the enable bit */
28717ec681f3Smrg         chanbld.OR(m0_2, m0_2,
28727ec681f3Smrg                    brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
28737ec681f3Smrg      } else {
28749f464c52Smaya         /* Copy "Barrier ID" from r0.2, bits 16:13 */
28759f464c52Smaya         chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
28769f464c52Smaya                     brw_imm_ud(INTEL_MASK(16, 13)));
287701e04c3fSmrg
28789f464c52Smaya         /* Shift it up to bits 27:24. */
28799f464c52Smaya         chanbld.SHL(m0_2, m0_2, brw_imm_ud(11));
288001e04c3fSmrg
28817ec681f3Smrg         /* Set the Barrier Count and the enable bit */
28829f464c52Smaya         chanbld.OR(m0_2, m0_2,
28839f464c52Smaya                    brw_imm_ud(tcs_prog_data->instances << 9 | (1 << 15)));
28849f464c52Smaya      }
288501e04c3fSmrg
288601e04c3fSmrg      bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
288701e04c3fSmrg      break;
288801e04c3fSmrg   }
288901e04c3fSmrg
289001e04c3fSmrg   case nir_intrinsic_load_input:
289101e04c3fSmrg      unreachable("nir_lower_io should never give us these.");
289201e04c3fSmrg      break;
289301e04c3fSmrg
289401e04c3fSmrg   case nir_intrinsic_load_per_vertex_input: {
28957ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
289601e04c3fSmrg      fs_reg indirect_offset = get_indirect_offset(instr);
289701e04c3fSmrg      unsigned imm_offset = instr->const_index[0];
289801e04c3fSmrg      fs_inst *inst;
289901e04c3fSmrg
29007ec681f3Smrg      fs_reg icp_handle =
29017ec681f3Smrg         eight_patch ? get_tcs_eight_patch_icp_handle(bld, instr)
29027ec681f3Smrg                     : get_tcs_single_patch_icp_handle(bld, instr);
290301e04c3fSmrg
290401e04c3fSmrg      /* We can only read two double components with each URB read, so
290501e04c3fSmrg       * we send two read messages in that case, each one loading up to
290601e04c3fSmrg       * two double components.
290701e04c3fSmrg       */
290801e04c3fSmrg      unsigned num_components = instr->num_components;
290901e04c3fSmrg      unsigned first_component = nir_intrinsic_component(instr);
291001e04c3fSmrg
29117ec681f3Smrg      if (indirect_offset.file == BAD_FILE) {
29127ec681f3Smrg         /* Constant indexing - use global offset. */
29137ec681f3Smrg         if (first_component != 0) {
29147ec681f3Smrg            unsigned read_components = num_components + first_component;
29157ec681f3Smrg            fs_reg tmp = bld.vgrf(dst.type, read_components);
29167ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
29177ec681f3Smrg            for (unsigned i = 0; i < num_components; i++) {
29187ec681f3Smrg               bld.MOV(offset(dst, bld, i),
29197ec681f3Smrg                       offset(tmp, bld, i + first_component));
292001e04c3fSmrg            }
292101e04c3fSmrg         } else {
29227ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
292301e04c3fSmrg         }
29247ec681f3Smrg         inst->offset = imm_offset;
29257ec681f3Smrg         inst->mlen = 1;
29267ec681f3Smrg      } else {
29277ec681f3Smrg         /* Indirect indexing - use per-slot offsets as well. */
29287ec681f3Smrg         const fs_reg srcs[] = { icp_handle, indirect_offset };
29297ec681f3Smrg         fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
29307ec681f3Smrg         bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
29317ec681f3Smrg         if (first_component != 0) {
29327ec681f3Smrg            unsigned read_components = num_components + first_component;
29337ec681f3Smrg            fs_reg tmp = bld.vgrf(dst.type, read_components);
29347ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
29357ec681f3Smrg                            payload);
29367ec681f3Smrg            for (unsigned i = 0; i < num_components; i++) {
29377ec681f3Smrg               bld.MOV(offset(dst, bld, i),
29387ec681f3Smrg                       offset(tmp, bld, i + first_component));
29397ec681f3Smrg            }
29407ec681f3Smrg         } else {
29417ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
29427ec681f3Smrg                            payload);
294301e04c3fSmrg         }
29447ec681f3Smrg         inst->offset = imm_offset;
29457ec681f3Smrg         inst->mlen = 2;
29467ec681f3Smrg      }
29477ec681f3Smrg      inst->size_written = (num_components + first_component) *
29487ec681f3Smrg                           inst->dst.component_size(inst->exec_size);
294901e04c3fSmrg
29507ec681f3Smrg      /* Copy the temporary to the destination to deal with writemasking.
29517ec681f3Smrg       *
29527ec681f3Smrg       * Also attempt to deal with gl_PointSize being in the .w component.
29537ec681f3Smrg       */
29547ec681f3Smrg      if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
29557ec681f3Smrg         assert(type_sz(dst.type) == 4);
29567ec681f3Smrg         inst->dst = bld.vgrf(dst.type, 4);
29577ec681f3Smrg         inst->size_written = 4 * REG_SIZE;
29587ec681f3Smrg         bld.MOV(dst, offset(inst->dst, bld, 3));
295901e04c3fSmrg      }
296001e04c3fSmrg      break;
296101e04c3fSmrg   }
296201e04c3fSmrg
296301e04c3fSmrg   case nir_intrinsic_load_output:
296401e04c3fSmrg   case nir_intrinsic_load_per_vertex_output: {
29657ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
296601e04c3fSmrg      fs_reg indirect_offset = get_indirect_offset(instr);
296701e04c3fSmrg      unsigned imm_offset = instr->const_index[0];
296801e04c3fSmrg      unsigned first_component = nir_intrinsic_component(instr);
296901e04c3fSmrg
29707ec681f3Smrg      struct brw_reg output_handles = get_tcs_output_urb_handle();
29717ec681f3Smrg
297201e04c3fSmrg      fs_inst *inst;
297301e04c3fSmrg      if (indirect_offset.file == BAD_FILE) {
29747ec681f3Smrg         /* This MOV replicates the output handle to all enabled channels
29757ec681f3Smrg          * is SINGLE_PATCH mode.
29767ec681f3Smrg          */
297701e04c3fSmrg         fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
29787ec681f3Smrg         bld.MOV(patch_handle, output_handles);
297901e04c3fSmrg
298001e04c3fSmrg         {
298101e04c3fSmrg            if (first_component != 0) {
298201e04c3fSmrg               unsigned read_components =
298301e04c3fSmrg                  instr->num_components + first_component;
298401e04c3fSmrg               fs_reg tmp = bld.vgrf(dst.type, read_components);
298501e04c3fSmrg               inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
298601e04c3fSmrg                               patch_handle);
298701e04c3fSmrg               inst->size_written = read_components * REG_SIZE;
298801e04c3fSmrg               for (unsigned i = 0; i < instr->num_components; i++) {
298901e04c3fSmrg                  bld.MOV(offset(dst, bld, i),
299001e04c3fSmrg                          offset(tmp, bld, i + first_component));
299101e04c3fSmrg               }
299201e04c3fSmrg            } else {
299301e04c3fSmrg               inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst,
299401e04c3fSmrg                               patch_handle);
299501e04c3fSmrg               inst->size_written = instr->num_components * REG_SIZE;
299601e04c3fSmrg            }
299701e04c3fSmrg            inst->offset = imm_offset;
299801e04c3fSmrg            inst->mlen = 1;
299901e04c3fSmrg         }
300001e04c3fSmrg      } else {
300101e04c3fSmrg         /* Indirect indexing - use per-slot offsets as well. */
30027ec681f3Smrg         const fs_reg srcs[] = { output_handles, indirect_offset };
300301e04c3fSmrg         fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
300401e04c3fSmrg         bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
300501e04c3fSmrg         if (first_component != 0) {
300601e04c3fSmrg            unsigned read_components =
300701e04c3fSmrg               instr->num_components + first_component;
300801e04c3fSmrg            fs_reg tmp = bld.vgrf(dst.type, read_components);
300901e04c3fSmrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
301001e04c3fSmrg                            payload);
301101e04c3fSmrg            inst->size_written = read_components * REG_SIZE;
301201e04c3fSmrg            for (unsigned i = 0; i < instr->num_components; i++) {
301301e04c3fSmrg               bld.MOV(offset(dst, bld, i),
301401e04c3fSmrg                       offset(tmp, bld, i + first_component));
301501e04c3fSmrg            }
301601e04c3fSmrg         } else {
301701e04c3fSmrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
301801e04c3fSmrg                            payload);
301901e04c3fSmrg            inst->size_written = instr->num_components * REG_SIZE;
302001e04c3fSmrg         }
302101e04c3fSmrg         inst->offset = imm_offset;
302201e04c3fSmrg         inst->mlen = 2;
302301e04c3fSmrg      }
302401e04c3fSmrg      break;
302501e04c3fSmrg   }
302601e04c3fSmrg
302701e04c3fSmrg   case nir_intrinsic_store_output:
302801e04c3fSmrg   case nir_intrinsic_store_per_vertex_output: {
30297ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) == 32);
303001e04c3fSmrg      fs_reg value = get_nir_src(instr->src[0]);
303101e04c3fSmrg      fs_reg indirect_offset = get_indirect_offset(instr);
303201e04c3fSmrg      unsigned imm_offset = instr->const_index[0];
303301e04c3fSmrg      unsigned mask = instr->const_index[1];
303401e04c3fSmrg      unsigned header_regs = 0;
30357ec681f3Smrg      struct brw_reg output_handles = get_tcs_output_urb_handle();
30367ec681f3Smrg
303701e04c3fSmrg      fs_reg srcs[7];
30387ec681f3Smrg      srcs[header_regs++] = output_handles;
303901e04c3fSmrg
304001e04c3fSmrg      if (indirect_offset.file != BAD_FILE) {
304101e04c3fSmrg         srcs[header_regs++] = indirect_offset;
304201e04c3fSmrg      }
304301e04c3fSmrg
304401e04c3fSmrg      if (mask == 0)
304501e04c3fSmrg         break;
304601e04c3fSmrg
304701e04c3fSmrg      unsigned num_components = util_last_bit(mask);
304801e04c3fSmrg      enum opcode opcode;
304901e04c3fSmrg
305001e04c3fSmrg      /* We can only pack two 64-bit components in a single message, so send
305101e04c3fSmrg       * 2 messages if we have more components
305201e04c3fSmrg       */
305301e04c3fSmrg      unsigned first_component = nir_intrinsic_component(instr);
305401e04c3fSmrg      mask = mask << first_component;
305501e04c3fSmrg
30567ec681f3Smrg      if (mask != WRITEMASK_XYZW) {
30577ec681f3Smrg         srcs[header_regs++] = brw_imm_ud(mask << 16);
30587ec681f3Smrg         opcode = indirect_offset.file != BAD_FILE ?
30597ec681f3Smrg            SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
30607ec681f3Smrg            SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
30617ec681f3Smrg      } else {
30627ec681f3Smrg         opcode = indirect_offset.file != BAD_FILE ?
30637ec681f3Smrg            SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
30647ec681f3Smrg            SHADER_OPCODE_URB_WRITE_SIMD8;
30657ec681f3Smrg      }
306601e04c3fSmrg
30677ec681f3Smrg      for (unsigned i = 0; i < num_components; i++) {
30687ec681f3Smrg         if (!(mask & (1 << (i + first_component))))
30697ec681f3Smrg            continue;
307001e04c3fSmrg
30717ec681f3Smrg         srcs[header_regs + i + first_component] = offset(value, bld, i);
30727ec681f3Smrg      }
307301e04c3fSmrg
30747ec681f3Smrg      unsigned mlen = header_regs + num_components + first_component;
30757ec681f3Smrg      fs_reg payload =
30767ec681f3Smrg         bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
30777ec681f3Smrg      bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
307801e04c3fSmrg
30797ec681f3Smrg      fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
30807ec681f3Smrg      inst->offset = imm_offset;
30817ec681f3Smrg      inst->mlen = mlen;
308201e04c3fSmrg      break;
308301e04c3fSmrg   }
308401e04c3fSmrg
308501e04c3fSmrg   default:
308601e04c3fSmrg      nir_emit_intrinsic(bld, instr);
308701e04c3fSmrg      break;
308801e04c3fSmrg   }
308901e04c3fSmrg}
309001e04c3fSmrg
309101e04c3fSmrgvoid
309201e04c3fSmrgfs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
309301e04c3fSmrg                                   nir_intrinsic_instr *instr)
309401e04c3fSmrg{
309501e04c3fSmrg   assert(stage == MESA_SHADER_TESS_EVAL);
309601e04c3fSmrg   struct brw_tes_prog_data *tes_prog_data = brw_tes_prog_data(prog_data);
309701e04c3fSmrg
309801e04c3fSmrg   fs_reg dest;
309901e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
310001e04c3fSmrg      dest = get_nir_dest(instr->dest);
310101e04c3fSmrg
310201e04c3fSmrg   switch (instr->intrinsic) {
310301e04c3fSmrg   case nir_intrinsic_load_primitive_id:
310401e04c3fSmrg      bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
310501e04c3fSmrg      break;
310601e04c3fSmrg   case nir_intrinsic_load_tess_coord:
310701e04c3fSmrg      /* gl_TessCoord is part of the payload in g1-3 */
310801e04c3fSmrg      for (unsigned i = 0; i < 3; i++) {
310901e04c3fSmrg         bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
311001e04c3fSmrg      }
311101e04c3fSmrg      break;
311201e04c3fSmrg
311301e04c3fSmrg   case nir_intrinsic_load_input:
311401e04c3fSmrg   case nir_intrinsic_load_per_vertex_input: {
31157ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
311601e04c3fSmrg      fs_reg indirect_offset = get_indirect_offset(instr);
311701e04c3fSmrg      unsigned imm_offset = instr->const_index[0];
311801e04c3fSmrg      unsigned first_component = nir_intrinsic_component(instr);
311901e04c3fSmrg
312001e04c3fSmrg      fs_inst *inst;
312101e04c3fSmrg      if (indirect_offset.file == BAD_FILE) {
312201e04c3fSmrg         /* Arbitrarily only push up to 32 vec4 slots worth of data,
312301e04c3fSmrg          * which is 16 registers (since each holds 2 vec4 slots).
312401e04c3fSmrg          */
312501e04c3fSmrg         const unsigned max_push_slots = 32;
31267ec681f3Smrg         if (imm_offset < max_push_slots) {
312701e04c3fSmrg            fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
312801e04c3fSmrg            for (int i = 0; i < instr->num_components; i++) {
31297ec681f3Smrg               unsigned comp = 4 * (imm_offset % 2) + i + first_component;
313001e04c3fSmrg               bld.MOV(offset(dest, bld, i), component(src, comp));
313101e04c3fSmrg            }
313201e04c3fSmrg
313301e04c3fSmrg            tes_prog_data->base.urb_read_length =
313401e04c3fSmrg               MAX2(tes_prog_data->base.urb_read_length,
31357ec681f3Smrg                    (imm_offset / 2) + 1);
313601e04c3fSmrg         } else {
313701e04c3fSmrg            /* Replicate the patch handle to all enabled channels */
313801e04c3fSmrg            const fs_reg srcs[] = {
313901e04c3fSmrg               retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
314001e04c3fSmrg            };
314101e04c3fSmrg            fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
314201e04c3fSmrg            bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
314301e04c3fSmrg
314401e04c3fSmrg            if (first_component != 0) {
314501e04c3fSmrg               unsigned read_components =
314601e04c3fSmrg                  instr->num_components + first_component;
314701e04c3fSmrg               fs_reg tmp = bld.vgrf(dest.type, read_components);
314801e04c3fSmrg               inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
314901e04c3fSmrg                               patch_handle);
315001e04c3fSmrg               inst->size_written = read_components * REG_SIZE;
315101e04c3fSmrg               for (unsigned i = 0; i < instr->num_components; i++) {
315201e04c3fSmrg                  bld.MOV(offset(dest, bld, i),
315301e04c3fSmrg                          offset(tmp, bld, i + first_component));
315401e04c3fSmrg               }
315501e04c3fSmrg            } else {
315601e04c3fSmrg               inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest,
315701e04c3fSmrg                               patch_handle);
315801e04c3fSmrg               inst->size_written = instr->num_components * REG_SIZE;
315901e04c3fSmrg            }
316001e04c3fSmrg            inst->mlen = 1;
316101e04c3fSmrg            inst->offset = imm_offset;
316201e04c3fSmrg         }
316301e04c3fSmrg      } else {
316401e04c3fSmrg         /* Indirect indexing - use per-slot offsets as well. */
316501e04c3fSmrg
316601e04c3fSmrg         /* We can only read two double components with each URB read, so
316701e04c3fSmrg          * we send two read messages in that case, each one loading up to
316801e04c3fSmrg          * two double components.
316901e04c3fSmrg          */
317001e04c3fSmrg         unsigned num_components = instr->num_components;
31717ec681f3Smrg         const fs_reg srcs[] = {
31727ec681f3Smrg            retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
31737ec681f3Smrg            indirect_offset
31747ec681f3Smrg         };
31757ec681f3Smrg         fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
31767ec681f3Smrg         bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
317701e04c3fSmrg
31787ec681f3Smrg         if (first_component != 0) {
31797ec681f3Smrg            unsigned read_components =
31807ec681f3Smrg                num_components + first_component;
31817ec681f3Smrg            fs_reg tmp = bld.vgrf(dest.type, read_components);
31827ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
31837ec681f3Smrg                            payload);
31847ec681f3Smrg            for (unsigned i = 0; i < num_components; i++) {
31857ec681f3Smrg               bld.MOV(offset(dest, bld, i),
31867ec681f3Smrg                       offset(tmp, bld, i + first_component));
318701e04c3fSmrg            }
31887ec681f3Smrg         } else {
31897ec681f3Smrg            inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest,
31907ec681f3Smrg                            payload);
319101e04c3fSmrg         }
31927ec681f3Smrg         inst->mlen = 2;
31937ec681f3Smrg         inst->offset = imm_offset;
31947ec681f3Smrg         inst->size_written = (num_components + first_component) *
31957ec681f3Smrg                              inst->dst.component_size(inst->exec_size);
319601e04c3fSmrg      }
319701e04c3fSmrg      break;
319801e04c3fSmrg   }
319901e04c3fSmrg   default:
320001e04c3fSmrg      nir_emit_intrinsic(bld, instr);
320101e04c3fSmrg      break;
320201e04c3fSmrg   }
320301e04c3fSmrg}
320401e04c3fSmrg
320501e04c3fSmrgvoid
320601e04c3fSmrgfs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
320701e04c3fSmrg                                  nir_intrinsic_instr *instr)
320801e04c3fSmrg{
320901e04c3fSmrg   assert(stage == MESA_SHADER_GEOMETRY);
321001e04c3fSmrg   fs_reg indirect_offset;
321101e04c3fSmrg
321201e04c3fSmrg   fs_reg dest;
321301e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
321401e04c3fSmrg      dest = get_nir_dest(instr->dest);
321501e04c3fSmrg
321601e04c3fSmrg   switch (instr->intrinsic) {
321701e04c3fSmrg   case nir_intrinsic_load_primitive_id:
321801e04c3fSmrg      assert(stage == MESA_SHADER_GEOMETRY);
321901e04c3fSmrg      assert(brw_gs_prog_data(prog_data)->include_primitive_id);
322001e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
322101e04c3fSmrg              retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
322201e04c3fSmrg      break;
322301e04c3fSmrg
322401e04c3fSmrg   case nir_intrinsic_load_input:
322501e04c3fSmrg      unreachable("load_input intrinsics are invalid for the GS stage");
322601e04c3fSmrg
322701e04c3fSmrg   case nir_intrinsic_load_per_vertex_input:
322801e04c3fSmrg      emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
322901e04c3fSmrg                         instr->src[1], instr->num_components,
323001e04c3fSmrg                         nir_intrinsic_component(instr));
323101e04c3fSmrg      break;
323201e04c3fSmrg
323301e04c3fSmrg   case nir_intrinsic_emit_vertex_with_counter:
323401e04c3fSmrg      emit_gs_vertex(instr->src[0], instr->const_index[0]);
323501e04c3fSmrg      break;
323601e04c3fSmrg
323701e04c3fSmrg   case nir_intrinsic_end_primitive_with_counter:
323801e04c3fSmrg      emit_gs_end_primitive(instr->src[0]);
323901e04c3fSmrg      break;
324001e04c3fSmrg
32417ec681f3Smrg   case nir_intrinsic_set_vertex_and_primitive_count:
324201e04c3fSmrg      bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
324301e04c3fSmrg      break;
324401e04c3fSmrg
324501e04c3fSmrg   case nir_intrinsic_load_invocation_id: {
324601e04c3fSmrg      fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
324701e04c3fSmrg      assert(val.file != BAD_FILE);
324801e04c3fSmrg      dest.type = val.type;
324901e04c3fSmrg      bld.MOV(dest, val);
325001e04c3fSmrg      break;
325101e04c3fSmrg   }
325201e04c3fSmrg
325301e04c3fSmrg   default:
325401e04c3fSmrg      nir_emit_intrinsic(bld, instr);
325501e04c3fSmrg      break;
325601e04c3fSmrg   }
325701e04c3fSmrg}
325801e04c3fSmrg
325901e04c3fSmrg/**
326001e04c3fSmrg * Fetch the current render target layer index.
326101e04c3fSmrg */
326201e04c3fSmrgstatic fs_reg
326301e04c3fSmrgfetch_render_target_array_index(const fs_builder &bld)
326401e04c3fSmrg{
32657ec681f3Smrg   if (bld.shader->devinfo->ver >= 12) {
32667ec681f3Smrg      /* The render target array index is provided in the thread payload as
32677ec681f3Smrg       * bits 26:16 of r1.1.
32687ec681f3Smrg       */
32697ec681f3Smrg      const fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_UD);
32707ec681f3Smrg      bld.AND(idx, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, 1, 3),
32717ec681f3Smrg              brw_imm_uw(0x7ff));
32727ec681f3Smrg      return idx;
32737ec681f3Smrg   } else if (bld.shader->devinfo->ver >= 6) {
327401e04c3fSmrg      /* The render target array index is provided in the thread payload as
327501e04c3fSmrg       * bits 26:16 of r0.0.
327601e04c3fSmrg       */
327701e04c3fSmrg      const fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_UD);
327801e04c3fSmrg      bld.AND(idx, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, 0, 1),
327901e04c3fSmrg              brw_imm_uw(0x7ff));
328001e04c3fSmrg      return idx;
328101e04c3fSmrg   } else {
328201e04c3fSmrg      /* Pre-SNB we only ever render into the first layer of the framebuffer
328301e04c3fSmrg       * since layered rendering is not implemented.
328401e04c3fSmrg       */
328501e04c3fSmrg      return brw_imm_ud(0);
328601e04c3fSmrg   }
328701e04c3fSmrg}
328801e04c3fSmrg
328901e04c3fSmrg/**
329001e04c3fSmrg * Fake non-coherent framebuffer read implemented using TXF to fetch from the
329101e04c3fSmrg * framebuffer at the current fragment coordinates and sample index.
329201e04c3fSmrg */
329301e04c3fSmrgfs_inst *
329401e04c3fSmrgfs_visitor::emit_non_coherent_fb_read(const fs_builder &bld, const fs_reg &dst,
329501e04c3fSmrg                                      unsigned target)
329601e04c3fSmrg{
32977ec681f3Smrg   const struct intel_device_info *devinfo = bld.shader->devinfo;
329801e04c3fSmrg
329901e04c3fSmrg   assert(bld.shader->stage == MESA_SHADER_FRAGMENT);
330001e04c3fSmrg   const brw_wm_prog_key *wm_key =
330101e04c3fSmrg      reinterpret_cast<const brw_wm_prog_key *>(key);
330201e04c3fSmrg   assert(!wm_key->coherent_fb_fetch);
330301e04c3fSmrg   const struct brw_wm_prog_data *wm_prog_data =
330401e04c3fSmrg      brw_wm_prog_data(stage_prog_data);
330501e04c3fSmrg
330601e04c3fSmrg   /* Calculate the surface index relative to the start of the texture binding
330701e04c3fSmrg    * table block, since that's what the texturing messages expect.
330801e04c3fSmrg    */
330901e04c3fSmrg   const unsigned surface = target +
331001e04c3fSmrg      wm_prog_data->binding_table.render_target_read_start -
331101e04c3fSmrg      wm_prog_data->base.binding_table.texture_start;
331201e04c3fSmrg
331301e04c3fSmrg   /* Calculate the fragment coordinates. */
331401e04c3fSmrg   const fs_reg coords = bld.vgrf(BRW_REGISTER_TYPE_UD, 3);
331501e04c3fSmrg   bld.MOV(offset(coords, bld, 0), pixel_x);
331601e04c3fSmrg   bld.MOV(offset(coords, bld, 1), pixel_y);
331701e04c3fSmrg   bld.MOV(offset(coords, bld, 2), fetch_render_target_array_index(bld));
331801e04c3fSmrg
331901e04c3fSmrg   /* Calculate the sample index and MCS payload when multisampling.  Luckily
332001e04c3fSmrg    * the MCS fetch message behaves deterministically for UMS surfaces, so it
332101e04c3fSmrg    * shouldn't be necessary to recompile based on whether the framebuffer is
332201e04c3fSmrg    * CMS or UMS.
332301e04c3fSmrg    */
332401e04c3fSmrg   if (wm_key->multisample_fbo &&
332501e04c3fSmrg       nir_system_values[SYSTEM_VALUE_SAMPLE_ID].file == BAD_FILE)
332601e04c3fSmrg      nir_system_values[SYSTEM_VALUE_SAMPLE_ID] = *emit_sampleid_setup();
332701e04c3fSmrg
332801e04c3fSmrg   const fs_reg sample = nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
332901e04c3fSmrg   const fs_reg mcs = wm_key->multisample_fbo ?
33309f464c52Smaya      emit_mcs_fetch(coords, 3, brw_imm_ud(surface), fs_reg()) : fs_reg();
333101e04c3fSmrg
333201e04c3fSmrg   /* Use either a normal or a CMS texel fetch message depending on whether
333301e04c3fSmrg    * the framebuffer is single or multisample.  On SKL+ use the wide CMS
333401e04c3fSmrg    * message just in case the framebuffer uses 16x multisampling, it should
333501e04c3fSmrg    * be equivalent to the normal CMS fetch for lower multisampling modes.
333601e04c3fSmrg    */
333701e04c3fSmrg   const opcode op = !wm_key->multisample_fbo ? SHADER_OPCODE_TXF_LOGICAL :
33387ec681f3Smrg                     devinfo->ver >= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL :
333901e04c3fSmrg                     SHADER_OPCODE_TXF_CMS_LOGICAL;
334001e04c3fSmrg
334101e04c3fSmrg   /* Emit the instruction. */
33429f464c52Smaya   fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
33439f464c52Smaya   srcs[TEX_LOGICAL_SRC_COORDINATE]       = coords;
33449f464c52Smaya   srcs[TEX_LOGICAL_SRC_LOD]              = brw_imm_ud(0);
33459f464c52Smaya   srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX]     = sample;
33469f464c52Smaya   srcs[TEX_LOGICAL_SRC_MCS]              = mcs;
33479f464c52Smaya   srcs[TEX_LOGICAL_SRC_SURFACE]          = brw_imm_ud(surface);
33489f464c52Smaya   srcs[TEX_LOGICAL_SRC_SAMPLER]          = brw_imm_ud(0);
33499f464c52Smaya   srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_ud(3);
33509f464c52Smaya   srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS]  = brw_imm_ud(0);
335101e04c3fSmrg
335201e04c3fSmrg   fs_inst *inst = bld.emit(op, dst, srcs, ARRAY_SIZE(srcs));
335301e04c3fSmrg   inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
335401e04c3fSmrg
335501e04c3fSmrg   return inst;
335601e04c3fSmrg}
335701e04c3fSmrg
335801e04c3fSmrg/**
335901e04c3fSmrg * Actual coherent framebuffer read implemented using the native render target
336001e04c3fSmrg * read message.  Requires SKL+.
336101e04c3fSmrg */
336201e04c3fSmrgstatic fs_inst *
336301e04c3fSmrgemit_coherent_fb_read(const fs_builder &bld, const fs_reg &dst, unsigned target)
336401e04c3fSmrg{
33657ec681f3Smrg   assert(bld.shader->devinfo->ver >= 9);
336601e04c3fSmrg   fs_inst *inst = bld.emit(FS_OPCODE_FB_READ_LOGICAL, dst);
336701e04c3fSmrg   inst->target = target;
336801e04c3fSmrg   inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
336901e04c3fSmrg
337001e04c3fSmrg   return inst;
337101e04c3fSmrg}
337201e04c3fSmrg
337301e04c3fSmrgstatic fs_reg
337401e04c3fSmrgalloc_temporary(const fs_builder &bld, unsigned size, fs_reg *regs, unsigned n)
337501e04c3fSmrg{
337601e04c3fSmrg   if (n && regs[0].file != BAD_FILE) {
337701e04c3fSmrg      return regs[0];
337801e04c3fSmrg
337901e04c3fSmrg   } else {
338001e04c3fSmrg      const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, size);
338101e04c3fSmrg
338201e04c3fSmrg      for (unsigned i = 0; i < n; i++)
338301e04c3fSmrg         regs[i] = tmp;
338401e04c3fSmrg
338501e04c3fSmrg      return tmp;
338601e04c3fSmrg   }
338701e04c3fSmrg}
338801e04c3fSmrg
338901e04c3fSmrgstatic fs_reg
339001e04c3fSmrgalloc_frag_output(fs_visitor *v, unsigned location)
339101e04c3fSmrg{
339201e04c3fSmrg   assert(v->stage == MESA_SHADER_FRAGMENT);
339301e04c3fSmrg   const brw_wm_prog_key *const key =
339401e04c3fSmrg      reinterpret_cast<const brw_wm_prog_key *>(v->key);
339501e04c3fSmrg   const unsigned l = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_LOCATION);
339601e04c3fSmrg   const unsigned i = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_INDEX);
339701e04c3fSmrg
339801e04c3fSmrg   if (i > 0 || (key->force_dual_color_blend && l == FRAG_RESULT_DATA1))
339901e04c3fSmrg      return alloc_temporary(v->bld, 4, &v->dual_src_output, 1);
340001e04c3fSmrg
340101e04c3fSmrg   else if (l == FRAG_RESULT_COLOR)
340201e04c3fSmrg      return alloc_temporary(v->bld, 4, v->outputs,
340301e04c3fSmrg                             MAX2(key->nr_color_regions, 1));
340401e04c3fSmrg
340501e04c3fSmrg   else if (l == FRAG_RESULT_DEPTH)
340601e04c3fSmrg      return alloc_temporary(v->bld, 1, &v->frag_depth, 1);
340701e04c3fSmrg
340801e04c3fSmrg   else if (l == FRAG_RESULT_STENCIL)
340901e04c3fSmrg      return alloc_temporary(v->bld, 1, &v->frag_stencil, 1);
341001e04c3fSmrg
341101e04c3fSmrg   else if (l == FRAG_RESULT_SAMPLE_MASK)
341201e04c3fSmrg      return alloc_temporary(v->bld, 1, &v->sample_mask, 1);
341301e04c3fSmrg
341401e04c3fSmrg   else if (l >= FRAG_RESULT_DATA0 &&
341501e04c3fSmrg            l < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS)
341601e04c3fSmrg      return alloc_temporary(v->bld, 4,
341701e04c3fSmrg                             &v->outputs[l - FRAG_RESULT_DATA0], 1);
341801e04c3fSmrg
341901e04c3fSmrg   else
342001e04c3fSmrg      unreachable("Invalid location");
342101e04c3fSmrg}
342201e04c3fSmrg
342301e04c3fSmrgvoid
342401e04c3fSmrgfs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
342501e04c3fSmrg                                  nir_intrinsic_instr *instr)
342601e04c3fSmrg{
342701e04c3fSmrg   assert(stage == MESA_SHADER_FRAGMENT);
342801e04c3fSmrg
342901e04c3fSmrg   fs_reg dest;
343001e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
343101e04c3fSmrg      dest = get_nir_dest(instr->dest);
343201e04c3fSmrg
343301e04c3fSmrg   switch (instr->intrinsic) {
343401e04c3fSmrg   case nir_intrinsic_load_front_face:
343501e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
343601e04c3fSmrg              *emit_frontfacing_interpolation());
343701e04c3fSmrg      break;
343801e04c3fSmrg
343901e04c3fSmrg   case nir_intrinsic_load_sample_pos: {
344001e04c3fSmrg      fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
344101e04c3fSmrg      assert(sample_pos.file != BAD_FILE);
344201e04c3fSmrg      dest.type = sample_pos.type;
344301e04c3fSmrg      bld.MOV(dest, sample_pos);
344401e04c3fSmrg      bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
344501e04c3fSmrg      break;
344601e04c3fSmrg   }
344701e04c3fSmrg
344801e04c3fSmrg   case nir_intrinsic_load_layer_id:
344901e04c3fSmrg      dest.type = BRW_REGISTER_TYPE_UD;
345001e04c3fSmrg      bld.MOV(dest, fetch_render_target_array_index(bld));
345101e04c3fSmrg      break;
345201e04c3fSmrg
34537ec681f3Smrg   case nir_intrinsic_is_helper_invocation: {
34547ec681f3Smrg      /* Unlike the regular gl_HelperInvocation, that is defined at dispatch,
34557ec681f3Smrg       * the helperInvocationEXT() (aka SpvOpIsHelperInvocationEXT) takes into
34567ec681f3Smrg       * consideration demoted invocations.  That information is stored in
34577ec681f3Smrg       * f0.1.
34587ec681f3Smrg       */
34597ec681f3Smrg      dest.type = BRW_REGISTER_TYPE_UD;
34607ec681f3Smrg
34617ec681f3Smrg      bld.MOV(dest, brw_imm_ud(0));
34627ec681f3Smrg
34637ec681f3Smrg      fs_inst *mov = bld.MOV(dest, brw_imm_ud(~0));
34647ec681f3Smrg      mov->predicate = BRW_PREDICATE_NORMAL;
34657ec681f3Smrg      mov->predicate_inverse = true;
34667ec681f3Smrg      mov->flag_subreg = sample_mask_flag_subreg(this);
34677ec681f3Smrg      break;
34687ec681f3Smrg   }
34697ec681f3Smrg
347001e04c3fSmrg   case nir_intrinsic_load_helper_invocation:
347101e04c3fSmrg   case nir_intrinsic_load_sample_mask_in:
34727ec681f3Smrg   case nir_intrinsic_load_sample_id:
34737ec681f3Smrg   case nir_intrinsic_load_frag_shading_rate: {
347401e04c3fSmrg      gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
347501e04c3fSmrg      fs_reg val = nir_system_values[sv];
347601e04c3fSmrg      assert(val.file != BAD_FILE);
347701e04c3fSmrg      dest.type = val.type;
347801e04c3fSmrg      bld.MOV(dest, val);
347901e04c3fSmrg      break;
348001e04c3fSmrg   }
348101e04c3fSmrg
348201e04c3fSmrg   case nir_intrinsic_store_output: {
348301e04c3fSmrg      const fs_reg src = get_nir_src(instr->src[0]);
34849f464c52Smaya      const unsigned store_offset = nir_src_as_uint(instr->src[1]);
348501e04c3fSmrg      const unsigned location = nir_intrinsic_base(instr) +
34869f464c52Smaya         SET_FIELD(store_offset, BRW_NIR_FRAG_OUTPUT_LOCATION);
348701e04c3fSmrg      const fs_reg new_dest = retype(alloc_frag_output(this, location),
348801e04c3fSmrg                                     src.type);
348901e04c3fSmrg
349001e04c3fSmrg      for (unsigned j = 0; j < instr->num_components; j++)
349101e04c3fSmrg         bld.MOV(offset(new_dest, bld, nir_intrinsic_component(instr) + j),
349201e04c3fSmrg                 offset(src, bld, j));
349301e04c3fSmrg
349401e04c3fSmrg      break;
349501e04c3fSmrg   }
349601e04c3fSmrg
349701e04c3fSmrg   case nir_intrinsic_load_output: {
349801e04c3fSmrg      const unsigned l = GET_FIELD(nir_intrinsic_base(instr),
349901e04c3fSmrg                                   BRW_NIR_FRAG_OUTPUT_LOCATION);
350001e04c3fSmrg      assert(l >= FRAG_RESULT_DATA0);
35019f464c52Smaya      const unsigned load_offset = nir_src_as_uint(instr->src[0]);
35029f464c52Smaya      const unsigned target = l - FRAG_RESULT_DATA0 + load_offset;
350301e04c3fSmrg      const fs_reg tmp = bld.vgrf(dest.type, 4);
350401e04c3fSmrg
350501e04c3fSmrg      if (reinterpret_cast<const brw_wm_prog_key *>(key)->coherent_fb_fetch)
350601e04c3fSmrg         emit_coherent_fb_read(bld, tmp, target);
350701e04c3fSmrg      else
350801e04c3fSmrg         emit_non_coherent_fb_read(bld, tmp, target);
350901e04c3fSmrg
351001e04c3fSmrg      for (unsigned j = 0; j < instr->num_components; j++) {
351101e04c3fSmrg         bld.MOV(offset(dest, bld, j),
351201e04c3fSmrg                 offset(tmp, bld, nir_intrinsic_component(instr) + j));
351301e04c3fSmrg      }
351401e04c3fSmrg
351501e04c3fSmrg      break;
351601e04c3fSmrg   }
351701e04c3fSmrg
35187ec681f3Smrg   case nir_intrinsic_demote:
351901e04c3fSmrg   case nir_intrinsic_discard:
35207ec681f3Smrg   case nir_intrinsic_terminate:
35217ec681f3Smrg   case nir_intrinsic_demote_if:
35227ec681f3Smrg   case nir_intrinsic_discard_if:
35237ec681f3Smrg   case nir_intrinsic_terminate_if: {
35247ec681f3Smrg      /* We track our discarded pixels in f0.1/f1.0.  By predicating on it, we
35257ec681f3Smrg       * can update just the flag bits that aren't yet discarded.  If there's
35267ec681f3Smrg       * no condition, we emit a CMP of g0 != g0, so all currently executing
352701e04c3fSmrg       * channels will get turned off.
352801e04c3fSmrg       */
35297ec681f3Smrg      fs_inst *cmp = NULL;
35307ec681f3Smrg      if (instr->intrinsic == nir_intrinsic_demote_if ||
35317ec681f3Smrg          instr->intrinsic == nir_intrinsic_discard_if ||
35327ec681f3Smrg          instr->intrinsic == nir_intrinsic_terminate_if) {
35337ec681f3Smrg         nir_alu_instr *alu = nir_src_as_alu_instr(instr->src[0]);
35347ec681f3Smrg
35357ec681f3Smrg         if (alu != NULL &&
35367ec681f3Smrg             alu->op != nir_op_bcsel &&
35377ec681f3Smrg             (devinfo->ver > 5 ||
35387ec681f3Smrg              (alu->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) != BRW_NIR_BOOLEAN_NEEDS_RESOLVE ||
35397ec681f3Smrg              alu->op == nir_op_fneu32 || alu->op == nir_op_feq32 ||
35407ec681f3Smrg              alu->op == nir_op_flt32 || alu->op == nir_op_fge32 ||
35417ec681f3Smrg              alu->op == nir_op_ine32 || alu->op == nir_op_ieq32 ||
35427ec681f3Smrg              alu->op == nir_op_ilt32 || alu->op == nir_op_ige32 ||
35437ec681f3Smrg              alu->op == nir_op_ult32 || alu->op == nir_op_uge32)) {
35447ec681f3Smrg            /* Re-emit the instruction that generated the Boolean value, but
35457ec681f3Smrg             * do not store it.  Since this instruction will be conditional,
35467ec681f3Smrg             * other instructions that want to use the real Boolean value may
35477ec681f3Smrg             * get garbage.  This was a problem for piglit's fs-discard-exit-2
35487ec681f3Smrg             * test.
35497ec681f3Smrg             *
35507ec681f3Smrg             * Ideally we'd detect that the instruction cannot have a
35517ec681f3Smrg             * conditional modifier before emitting the instructions.  Alas,
35527ec681f3Smrg             * that is nigh impossible.  Instead, we're going to assume the
35537ec681f3Smrg             * instruction (or last instruction) generated can have a
35547ec681f3Smrg             * conditional modifier.  If it cannot, fallback to the old-style
35557ec681f3Smrg             * compare, and hope dead code elimination will clean up the
35567ec681f3Smrg             * extra instructions generated.
35577ec681f3Smrg             */
35587ec681f3Smrg            nir_emit_alu(bld, alu, false);
35597ec681f3Smrg
35607ec681f3Smrg            cmp = (fs_inst *) instructions.get_tail();
35617ec681f3Smrg            if (cmp->conditional_mod == BRW_CONDITIONAL_NONE) {
35627ec681f3Smrg               if (cmp->can_do_cmod())
35637ec681f3Smrg                  cmp->conditional_mod = BRW_CONDITIONAL_Z;
35647ec681f3Smrg               else
35657ec681f3Smrg                  cmp = NULL;
35667ec681f3Smrg            } else {
35677ec681f3Smrg               /* The old sequence that would have been generated is,
35687ec681f3Smrg                * basically, bool_result == false.  This is equivalent to
35697ec681f3Smrg                * !bool_result, so negate the old modifier.
35707ec681f3Smrg                */
35717ec681f3Smrg               cmp->conditional_mod = brw_negate_cmod(cmp->conditional_mod);
35727ec681f3Smrg            }
35737ec681f3Smrg         }
35747ec681f3Smrg
35757ec681f3Smrg         if (cmp == NULL) {
35767ec681f3Smrg            cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
35777ec681f3Smrg                          brw_imm_d(0), BRW_CONDITIONAL_Z);
35787ec681f3Smrg         }
357901e04c3fSmrg      } else {
358001e04c3fSmrg         fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
358101e04c3fSmrg                                       BRW_REGISTER_TYPE_UW));
358201e04c3fSmrg         cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
358301e04c3fSmrg      }
35847ec681f3Smrg
358501e04c3fSmrg      cmp->predicate = BRW_PREDICATE_NORMAL;
35867ec681f3Smrg      cmp->flag_subreg = sample_mask_flag_subreg(this);
35877ec681f3Smrg
35887ec681f3Smrg      fs_inst *jump = bld.emit(BRW_OPCODE_HALT);
35897ec681f3Smrg      jump->flag_subreg = sample_mask_flag_subreg(this);
35907ec681f3Smrg      jump->predicate_inverse = true;
359101e04c3fSmrg
35927ec681f3Smrg      if (instr->intrinsic == nir_intrinsic_terminate ||
35937ec681f3Smrg          instr->intrinsic == nir_intrinsic_terminate_if) {
35947ec681f3Smrg         jump->predicate = BRW_PREDICATE_NORMAL;
35957ec681f3Smrg      } else {
35967ec681f3Smrg         /* Only jump when the whole quad is demoted.  For historical
35977ec681f3Smrg          * reasons this is also used for discard.
35987ec681f3Smrg          */
35997ec681f3Smrg         jump->predicate = BRW_PREDICATE_ALIGN1_ANY4H;
360001e04c3fSmrg      }
360101e04c3fSmrg
36027ec681f3Smrg      if (devinfo->ver < 7)
36037ec681f3Smrg         limit_dispatch_width(
36047ec681f3Smrg            16, "Fragment discard/demote not implemented in SIMD32 mode.\n");
360501e04c3fSmrg      break;
360601e04c3fSmrg   }
360701e04c3fSmrg
360801e04c3fSmrg   case nir_intrinsic_load_input: {
360901e04c3fSmrg      /* load_input is only used for flat inputs */
36107ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
361101e04c3fSmrg      unsigned base = nir_intrinsic_base(instr);
361201e04c3fSmrg      unsigned comp = nir_intrinsic_component(instr);
361301e04c3fSmrg      unsigned num_components = instr->num_components;
361401e04c3fSmrg
361501e04c3fSmrg      /* Special case fields in the VUE header */
361601e04c3fSmrg      if (base == VARYING_SLOT_LAYER)
361701e04c3fSmrg         comp = 1;
361801e04c3fSmrg      else if (base == VARYING_SLOT_VIEWPORT)
361901e04c3fSmrg         comp = 2;
362001e04c3fSmrg
362101e04c3fSmrg      for (unsigned int i = 0; i < num_components; i++) {
36227ec681f3Smrg         bld.MOV(offset(dest, bld, i),
36237ec681f3Smrg                 retype(component(interp_reg(base, comp + i), 3), dest.type));
362401e04c3fSmrg      }
36257ec681f3Smrg      break;
36267ec681f3Smrg   }
362701e04c3fSmrg
36287ec681f3Smrg   case nir_intrinsic_load_fs_input_interp_deltas: {
36297ec681f3Smrg      assert(stage == MESA_SHADER_FRAGMENT);
36307ec681f3Smrg      assert(nir_src_as_uint(instr->src[0]) == 0);
36317ec681f3Smrg      fs_reg interp = interp_reg(nir_intrinsic_base(instr),
36327ec681f3Smrg                                 nir_intrinsic_component(instr));
36337ec681f3Smrg      dest.type = BRW_REGISTER_TYPE_F;
36347ec681f3Smrg      bld.MOV(offset(dest, bld, 0), component(interp, 3));
36357ec681f3Smrg      bld.MOV(offset(dest, bld, 1), component(interp, 1));
36367ec681f3Smrg      bld.MOV(offset(dest, bld, 2), component(interp, 0));
363701e04c3fSmrg      break;
363801e04c3fSmrg   }
363901e04c3fSmrg
364001e04c3fSmrg   case nir_intrinsic_load_barycentric_pixel:
364101e04c3fSmrg   case nir_intrinsic_load_barycentric_centroid:
36427ec681f3Smrg   case nir_intrinsic_load_barycentric_sample: {
36437ec681f3Smrg      /* Use the delta_xy values computed from the payload */
36447ec681f3Smrg      const glsl_interp_mode interp_mode =
36457ec681f3Smrg         (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
36467ec681f3Smrg      enum brw_barycentric_mode bary =
36477ec681f3Smrg         brw_barycentric_mode(interp_mode, instr->intrinsic);
36487ec681f3Smrg      const fs_reg srcs[] = { offset(this->delta_xy[bary], bld, 0),
36497ec681f3Smrg                              offset(this->delta_xy[bary], bld, 1) };
36507ec681f3Smrg      bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
365101e04c3fSmrg      break;
36527ec681f3Smrg   }
365301e04c3fSmrg
365401e04c3fSmrg   case nir_intrinsic_load_barycentric_at_sample: {
365501e04c3fSmrg      const glsl_interp_mode interpolation =
365601e04c3fSmrg         (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
365701e04c3fSmrg
36589f464c52Smaya      if (nir_src_is_const(instr->src[0])) {
36599f464c52Smaya         unsigned msg_data = nir_src_as_uint(instr->src[0]) << 4;
366001e04c3fSmrg
366101e04c3fSmrg         emit_pixel_interpolater_send(bld,
366201e04c3fSmrg                                      FS_OPCODE_INTERPOLATE_AT_SAMPLE,
366301e04c3fSmrg                                      dest,
366401e04c3fSmrg                                      fs_reg(), /* src */
366501e04c3fSmrg                                      brw_imm_ud(msg_data),
366601e04c3fSmrg                                      interpolation);
366701e04c3fSmrg      } else {
366801e04c3fSmrg         const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
366901e04c3fSmrg                                          BRW_REGISTER_TYPE_UD);
367001e04c3fSmrg
367101e04c3fSmrg         if (nir_src_is_dynamically_uniform(instr->src[0])) {
367201e04c3fSmrg            const fs_reg sample_id = bld.emit_uniformize(sample_src);
367301e04c3fSmrg            const fs_reg msg_data = vgrf(glsl_type::uint_type);
367401e04c3fSmrg            bld.exec_all().group(1, 0)
367501e04c3fSmrg               .SHL(msg_data, sample_id, brw_imm_ud(4u));
367601e04c3fSmrg            emit_pixel_interpolater_send(bld,
367701e04c3fSmrg                                         FS_OPCODE_INTERPOLATE_AT_SAMPLE,
367801e04c3fSmrg                                         dest,
367901e04c3fSmrg                                         fs_reg(), /* src */
36807ec681f3Smrg                                         component(msg_data, 0),
368101e04c3fSmrg                                         interpolation);
368201e04c3fSmrg         } else {
368301e04c3fSmrg            /* Make a loop that sends a message to the pixel interpolater
368401e04c3fSmrg             * for the sample number in each live channel. If there are
368501e04c3fSmrg             * multiple channels with the same sample number then these
368601e04c3fSmrg             * will be handled simultaneously with a single interation of
368701e04c3fSmrg             * the loop.
368801e04c3fSmrg             */
368901e04c3fSmrg            bld.emit(BRW_OPCODE_DO);
369001e04c3fSmrg
369101e04c3fSmrg            /* Get the next live sample number into sample_id_reg */
369201e04c3fSmrg            const fs_reg sample_id = bld.emit_uniformize(sample_src);
369301e04c3fSmrg
369401e04c3fSmrg            /* Set the flag register so that we can perform the send
369501e04c3fSmrg             * message on all channels that have the same sample number
369601e04c3fSmrg             */
369701e04c3fSmrg            bld.CMP(bld.null_reg_ud(),
369801e04c3fSmrg                    sample_src, sample_id,
369901e04c3fSmrg                    BRW_CONDITIONAL_EQ);
370001e04c3fSmrg            const fs_reg msg_data = vgrf(glsl_type::uint_type);
370101e04c3fSmrg            bld.exec_all().group(1, 0)
370201e04c3fSmrg               .SHL(msg_data, sample_id, brw_imm_ud(4u));
370301e04c3fSmrg            fs_inst *inst =
370401e04c3fSmrg               emit_pixel_interpolater_send(bld,
370501e04c3fSmrg                                            FS_OPCODE_INTERPOLATE_AT_SAMPLE,
370601e04c3fSmrg                                            dest,
370701e04c3fSmrg                                            fs_reg(), /* src */
370801e04c3fSmrg                                            component(msg_data, 0),
370901e04c3fSmrg                                            interpolation);
371001e04c3fSmrg            set_predicate(BRW_PREDICATE_NORMAL, inst);
371101e04c3fSmrg
371201e04c3fSmrg            /* Continue the loop if there are any live channels left */
371301e04c3fSmrg            set_predicate_inv(BRW_PREDICATE_NORMAL,
371401e04c3fSmrg                              true, /* inverse */
371501e04c3fSmrg                              bld.emit(BRW_OPCODE_WHILE));
371601e04c3fSmrg         }
371701e04c3fSmrg      }
371801e04c3fSmrg      break;
371901e04c3fSmrg   }
372001e04c3fSmrg
372101e04c3fSmrg   case nir_intrinsic_load_barycentric_at_offset: {
372201e04c3fSmrg      const glsl_interp_mode interpolation =
372301e04c3fSmrg         (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
372401e04c3fSmrg
372501e04c3fSmrg      nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
372601e04c3fSmrg
372701e04c3fSmrg      if (const_offset) {
37289f464c52Smaya         assert(nir_src_bit_size(instr->src[0]) == 32);
37297ec681f3Smrg         unsigned off_x = const_offset[0].u32 & 0xf;
37307ec681f3Smrg         unsigned off_y = const_offset[1].u32 & 0xf;
373101e04c3fSmrg
373201e04c3fSmrg         emit_pixel_interpolater_send(bld,
373301e04c3fSmrg                                      FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
373401e04c3fSmrg                                      dest,
373501e04c3fSmrg                                      fs_reg(), /* src */
373601e04c3fSmrg                                      brw_imm_ud(off_x | (off_y << 4)),
373701e04c3fSmrg                                      interpolation);
373801e04c3fSmrg      } else {
37397ec681f3Smrg         fs_reg src = retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_D);
374001e04c3fSmrg         const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
374101e04c3fSmrg         emit_pixel_interpolater_send(bld,
374201e04c3fSmrg                                      opcode,
374301e04c3fSmrg                                      dest,
374401e04c3fSmrg                                      src,
374501e04c3fSmrg                                      brw_imm_ud(0u),
374601e04c3fSmrg                                      interpolation);
374701e04c3fSmrg      }
374801e04c3fSmrg      break;
374901e04c3fSmrg   }
375001e04c3fSmrg
37517ec681f3Smrg   case nir_intrinsic_load_frag_coord:
37527ec681f3Smrg      emit_fragcoord_interpolation(dest);
37537ec681f3Smrg      break;
375401e04c3fSmrg
37557ec681f3Smrg   case nir_intrinsic_load_interpolated_input: {
375601e04c3fSmrg      assert(instr->src[0].ssa &&
375701e04c3fSmrg             instr->src[0].ssa->parent_instr->type == nir_instr_type_intrinsic);
375801e04c3fSmrg      nir_intrinsic_instr *bary_intrinsic =
375901e04c3fSmrg         nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
376001e04c3fSmrg      nir_intrinsic_op bary_intrin = bary_intrinsic->intrinsic;
376101e04c3fSmrg      enum glsl_interp_mode interp_mode =
376201e04c3fSmrg         (enum glsl_interp_mode) nir_intrinsic_interp_mode(bary_intrinsic);
376301e04c3fSmrg      fs_reg dst_xy;
376401e04c3fSmrg
376501e04c3fSmrg      if (bary_intrin == nir_intrinsic_load_barycentric_at_offset ||
376601e04c3fSmrg          bary_intrin == nir_intrinsic_load_barycentric_at_sample) {
37677ec681f3Smrg         /* Use the result of the PI message. */
376801e04c3fSmrg         dst_xy = retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_F);
376901e04c3fSmrg      } else {
377001e04c3fSmrg         /* Use the delta_xy values computed from the payload */
377101e04c3fSmrg         enum brw_barycentric_mode bary =
377201e04c3fSmrg            brw_barycentric_mode(interp_mode, bary_intrin);
377301e04c3fSmrg         dst_xy = this->delta_xy[bary];
377401e04c3fSmrg      }
377501e04c3fSmrg
377601e04c3fSmrg      for (unsigned int i = 0; i < instr->num_components; i++) {
377701e04c3fSmrg         fs_reg interp =
377801e04c3fSmrg            component(interp_reg(nir_intrinsic_base(instr),
377901e04c3fSmrg                                 nir_intrinsic_component(instr) + i), 0);
378001e04c3fSmrg         interp.type = BRW_REGISTER_TYPE_F;
378101e04c3fSmrg         dest.type = BRW_REGISTER_TYPE_F;
378201e04c3fSmrg
37837ec681f3Smrg         if (devinfo->ver < 6 && interp_mode == INTERP_MODE_SMOOTH) {
378401e04c3fSmrg            fs_reg tmp = vgrf(glsl_type::float_type);
378501e04c3fSmrg            bld.emit(FS_OPCODE_LINTERP, tmp, dst_xy, interp);
378601e04c3fSmrg            bld.MUL(offset(dest, bld, i), tmp, this->pixel_w);
378701e04c3fSmrg         } else {
378801e04c3fSmrg            bld.emit(FS_OPCODE_LINTERP, offset(dest, bld, i), dst_xy, interp);
378901e04c3fSmrg         }
379001e04c3fSmrg      }
379101e04c3fSmrg      break;
379201e04c3fSmrg   }
379301e04c3fSmrg
379401e04c3fSmrg   default:
379501e04c3fSmrg      nir_emit_intrinsic(bld, instr);
379601e04c3fSmrg      break;
379701e04c3fSmrg   }
379801e04c3fSmrg}
379901e04c3fSmrg
380001e04c3fSmrgvoid
380101e04c3fSmrgfs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
380201e04c3fSmrg                                  nir_intrinsic_instr *instr)
380301e04c3fSmrg{
38047ec681f3Smrg   assert(stage == MESA_SHADER_COMPUTE || stage == MESA_SHADER_KERNEL);
380501e04c3fSmrg   struct brw_cs_prog_data *cs_prog_data = brw_cs_prog_data(prog_data);
380601e04c3fSmrg
380701e04c3fSmrg   fs_reg dest;
380801e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
380901e04c3fSmrg      dest = get_nir_dest(instr->dest);
381001e04c3fSmrg
381101e04c3fSmrg   switch (instr->intrinsic) {
38127ec681f3Smrg   case nir_intrinsic_control_barrier:
38137ec681f3Smrg      /* The whole workgroup fits in a single HW thread, so all the
38147ec681f3Smrg       * invocations are already executed lock-step.  Instead of an actual
38157ec681f3Smrg       * barrier just emit a scheduling fence, that will generate no code.
38167ec681f3Smrg       */
38177ec681f3Smrg      if (!nir->info.workgroup_size_variable &&
38187ec681f3Smrg          workgroup_size() <= dispatch_width) {
38197ec681f3Smrg         bld.exec_all().group(1, 0).emit(FS_OPCODE_SCHEDULING_FENCE);
38207ec681f3Smrg         break;
38217ec681f3Smrg      }
38227ec681f3Smrg
382301e04c3fSmrg      emit_barrier();
382401e04c3fSmrg      cs_prog_data->uses_barrier = true;
382501e04c3fSmrg      break;
382601e04c3fSmrg
382701e04c3fSmrg   case nir_intrinsic_load_subgroup_id:
38287ec681f3Smrg      if (devinfo->verx10 >= 125)
38297ec681f3Smrg         bld.AND(retype(dest, BRW_REGISTER_TYPE_UD),
38307ec681f3Smrg                 retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
38317ec681f3Smrg                 brw_imm_ud(INTEL_MASK(7, 0)));
38327ec681f3Smrg      else
38337ec681f3Smrg         bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), subgroup_id);
383401e04c3fSmrg      break;
383501e04c3fSmrg
383601e04c3fSmrg   case nir_intrinsic_load_local_invocation_id:
38377ec681f3Smrg   case nir_intrinsic_load_workgroup_id: {
383801e04c3fSmrg      gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
383901e04c3fSmrg      fs_reg val = nir_system_values[sv];
384001e04c3fSmrg      assert(val.file != BAD_FILE);
384101e04c3fSmrg      dest.type = val.type;
384201e04c3fSmrg      for (unsigned i = 0; i < 3; i++)
384301e04c3fSmrg         bld.MOV(offset(dest, bld, i), offset(val, bld, i));
384401e04c3fSmrg      break;
384501e04c3fSmrg   }
384601e04c3fSmrg
38477ec681f3Smrg   case nir_intrinsic_load_num_workgroups: {
38487ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
384901e04c3fSmrg      const unsigned surface =
385001e04c3fSmrg         cs_prog_data->binding_table.work_groups_start;
385101e04c3fSmrg
385201e04c3fSmrg      cs_prog_data->uses_num_work_groups = true;
385301e04c3fSmrg
38549f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
38559f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(surface);
38569f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
38577ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(3); /* num components */
38587ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = brw_imm_ud(0);
38597ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
38607ec681f3Smrg      fs_inst *inst =
38619f464c52Smaya         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
38627ec681f3Smrg                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
38637ec681f3Smrg      inst->size_written = 3 * dispatch_width * 4;
386401e04c3fSmrg      break;
386501e04c3fSmrg   }
386601e04c3fSmrg
386701e04c3fSmrg   case nir_intrinsic_shared_atomic_add:
386801e04c3fSmrg   case nir_intrinsic_shared_atomic_imin:
386901e04c3fSmrg   case nir_intrinsic_shared_atomic_umin:
387001e04c3fSmrg   case nir_intrinsic_shared_atomic_imax:
387101e04c3fSmrg   case nir_intrinsic_shared_atomic_umax:
387201e04c3fSmrg   case nir_intrinsic_shared_atomic_and:
387301e04c3fSmrg   case nir_intrinsic_shared_atomic_or:
387401e04c3fSmrg   case nir_intrinsic_shared_atomic_xor:
387501e04c3fSmrg   case nir_intrinsic_shared_atomic_exchange:
387601e04c3fSmrg   case nir_intrinsic_shared_atomic_comp_swap:
38777ec681f3Smrg      nir_emit_shared_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
387801e04c3fSmrg      break;
387901e04c3fSmrg   case nir_intrinsic_shared_atomic_fmin:
388001e04c3fSmrg   case nir_intrinsic_shared_atomic_fmax:
388101e04c3fSmrg   case nir_intrinsic_shared_atomic_fcomp_swap:
38827ec681f3Smrg      nir_emit_shared_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
388301e04c3fSmrg      break;
388401e04c3fSmrg
388501e04c3fSmrg   case nir_intrinsic_load_shared: {
38867ec681f3Smrg      assert(devinfo->ver >= 7);
38877ec681f3Smrg      assert(stage == MESA_SHADER_COMPUTE || stage == MESA_SHADER_KERNEL);
388801e04c3fSmrg
38899f464c52Smaya      const unsigned bit_size = nir_dest_bit_size(instr->dest);
38909f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
38917ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GFX7_BTI_SLM);
38929f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[0]);
38939f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
38947ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
389501e04c3fSmrg
38969f464c52Smaya      /* Make dest unsigned because that's what the temporary will be */
38979f464c52Smaya      dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
389801e04c3fSmrg
389901e04c3fSmrg      /* Read the vector */
39007ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) <= 32);
39017ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
39027ec681f3Smrg      if (nir_dest_bit_size(instr->dest) == 32 &&
39037ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
39047ec681f3Smrg         assert(nir_dest_num_components(instr->dest) <= 4);
39059f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
39069f464c52Smaya         fs_inst *inst =
39079f464c52Smaya            bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
39089f464c52Smaya                     dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
39099f464c52Smaya         inst->size_written = instr->num_components * dispatch_width * 4;
39109f464c52Smaya      } else {
39119f464c52Smaya         assert(nir_dest_num_components(instr->dest) == 1);
39129f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
39139f464c52Smaya
39149f464c52Smaya         fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
39159f464c52Smaya         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
39169f464c52Smaya                  read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
39177ec681f3Smrg         bld.MOV(dest, subscript(read_result, dest.type, 0));
39189f464c52Smaya      }
391901e04c3fSmrg      break;
392001e04c3fSmrg   }
392101e04c3fSmrg
392201e04c3fSmrg   case nir_intrinsic_store_shared: {
39237ec681f3Smrg      assert(devinfo->ver >= 7);
39247ec681f3Smrg      assert(stage == MESA_SHADER_COMPUTE || stage == MESA_SHADER_KERNEL);
39259f464c52Smaya
39269f464c52Smaya      const unsigned bit_size = nir_src_bit_size(instr->src[0]);
39279f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
39287ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GFX7_BTI_SLM);
39299f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
39309f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
39317ec681f3Smrg      /* No point in masking with sample mask, here we're handling compute
39327ec681f3Smrg       * intrinsics.
39337ec681f3Smrg       */
39347ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
39359f464c52Smaya
39369f464c52Smaya      fs_reg data = get_nir_src(instr->src[0]);
39379f464c52Smaya      data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
39389f464c52Smaya
39397ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) <= 32);
39409f464c52Smaya      assert(nir_intrinsic_write_mask(instr) ==
39419f464c52Smaya             (1u << instr->num_components) - 1);
39427ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
39437ec681f3Smrg      if (nir_src_bit_size(instr->src[0]) == 32 &&
39447ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
39459f464c52Smaya         assert(nir_src_num_components(instr->src[0]) <= 4);
39469f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = data;
39479f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
39489f464c52Smaya         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
39499f464c52Smaya                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
39509f464c52Smaya      } else {
39519f464c52Smaya         assert(nir_src_num_components(instr->src[0]) == 1);
39529f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
395301e04c3fSmrg
39549f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
39559f464c52Smaya         bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
395601e04c3fSmrg
39579f464c52Smaya         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
39589f464c52Smaya                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
395901e04c3fSmrg      }
396001e04c3fSmrg      break;
396101e04c3fSmrg   }
396201e04c3fSmrg
39637ec681f3Smrg   case nir_intrinsic_load_workgroup_size: {
39647ec681f3Smrg      assert(compiler->lower_variable_group_size);
39657ec681f3Smrg      assert(nir->info.workgroup_size_variable);
39667ec681f3Smrg      for (unsigned i = 0; i < 3; i++) {
39677ec681f3Smrg         bld.MOV(retype(offset(dest, bld, i), BRW_REGISTER_TYPE_UD),
39687ec681f3Smrg            group_size[i]);
39697ec681f3Smrg      }
39707ec681f3Smrg      break;
39717ec681f3Smrg   }
39727ec681f3Smrg
39737ec681f3Smrg   default:
39747ec681f3Smrg      nir_emit_intrinsic(bld, instr);
39757ec681f3Smrg      break;
39767ec681f3Smrg   }
39777ec681f3Smrg}
39787ec681f3Smrg
39797ec681f3Smrgvoid
39807ec681f3Smrgfs_visitor::nir_emit_bs_intrinsic(const fs_builder &bld,
39817ec681f3Smrg                                  nir_intrinsic_instr *instr)
39827ec681f3Smrg{
39837ec681f3Smrg   assert(brw_shader_stage_is_bindless(stage));
39847ec681f3Smrg
39857ec681f3Smrg   fs_reg dest;
39867ec681f3Smrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
39877ec681f3Smrg      dest = get_nir_dest(instr->dest);
39887ec681f3Smrg
39897ec681f3Smrg   switch (instr->intrinsic) {
39907ec681f3Smrg   case nir_intrinsic_load_btd_global_arg_addr_intel:
39917ec681f3Smrg      bld.MOV(dest, retype(brw_vec1_grf(2, 0), dest.type));
39927ec681f3Smrg      break;
39937ec681f3Smrg
39947ec681f3Smrg   case nir_intrinsic_load_btd_local_arg_addr_intel:
39957ec681f3Smrg      bld.MOV(dest, retype(brw_vec1_grf(2, 2), dest.type));
39967ec681f3Smrg      break;
39977ec681f3Smrg
39987ec681f3Smrg   case nir_intrinsic_trace_ray_initial_intel:
39997ec681f3Smrg      bld.emit(RT_OPCODE_TRACE_RAY_LOGICAL,
40007ec681f3Smrg               bld.null_reg_ud(),
40017ec681f3Smrg               brw_imm_ud(BRW_RT_BVH_LEVEL_WORLD),
40027ec681f3Smrg               brw_imm_ud(GEN_RT_TRACE_RAY_INITAL));
40037ec681f3Smrg      break;
40047ec681f3Smrg
40057ec681f3Smrg   case nir_intrinsic_trace_ray_commit_intel:
40067ec681f3Smrg      bld.emit(RT_OPCODE_TRACE_RAY_LOGICAL,
40077ec681f3Smrg               bld.null_reg_ud(),
40087ec681f3Smrg               brw_imm_ud(BRW_RT_BVH_LEVEL_OBJECT),
40097ec681f3Smrg               brw_imm_ud(GEN_RT_TRACE_RAY_COMMIT));
40107ec681f3Smrg      break;
40117ec681f3Smrg
40127ec681f3Smrg   case nir_intrinsic_trace_ray_continue_intel:
40137ec681f3Smrg      bld.emit(RT_OPCODE_TRACE_RAY_LOGICAL,
40147ec681f3Smrg               bld.null_reg_ud(),
40157ec681f3Smrg               brw_imm_ud(BRW_RT_BVH_LEVEL_OBJECT),
40167ec681f3Smrg               brw_imm_ud(GEN_RT_TRACE_RAY_CONTINUE));
40177ec681f3Smrg      break;
40187ec681f3Smrg
401901e04c3fSmrg   default:
402001e04c3fSmrg      nir_emit_intrinsic(bld, instr);
402101e04c3fSmrg      break;
402201e04c3fSmrg   }
402301e04c3fSmrg}
402401e04c3fSmrg
402501e04c3fSmrgstatic fs_reg
402601e04c3fSmrgbrw_nir_reduction_op_identity(const fs_builder &bld,
402701e04c3fSmrg                              nir_op op, brw_reg_type type)
402801e04c3fSmrg{
402901e04c3fSmrg   nir_const_value value = nir_alu_binop_identity(op, type_sz(type) * 8);
403001e04c3fSmrg   switch (type_sz(type)) {
40317ec681f3Smrg   case 1:
40327ec681f3Smrg      if (type == BRW_REGISTER_TYPE_UB) {
40337ec681f3Smrg         return brw_imm_uw(value.u8);
40347ec681f3Smrg      } else {
40357ec681f3Smrg         assert(type == BRW_REGISTER_TYPE_B);
40367ec681f3Smrg         return brw_imm_w(value.i8);
40377ec681f3Smrg      }
403801e04c3fSmrg   case 2:
40399f464c52Smaya      return retype(brw_imm_uw(value.u16), type);
404001e04c3fSmrg   case 4:
40419f464c52Smaya      return retype(brw_imm_ud(value.u32), type);
404201e04c3fSmrg   case 8:
404301e04c3fSmrg      if (type == BRW_REGISTER_TYPE_DF)
40449f464c52Smaya         return setup_imm_df(bld, value.f64);
404501e04c3fSmrg      else
40469f464c52Smaya         return retype(brw_imm_u64(value.u64), type);
404701e04c3fSmrg   default:
404801e04c3fSmrg      unreachable("Invalid type size");
404901e04c3fSmrg   }
405001e04c3fSmrg}
405101e04c3fSmrg
405201e04c3fSmrgstatic opcode
405301e04c3fSmrgbrw_op_for_nir_reduction_op(nir_op op)
405401e04c3fSmrg{
405501e04c3fSmrg   switch (op) {
405601e04c3fSmrg   case nir_op_iadd: return BRW_OPCODE_ADD;
405701e04c3fSmrg   case nir_op_fadd: return BRW_OPCODE_ADD;
405801e04c3fSmrg   case nir_op_imul: return BRW_OPCODE_MUL;
405901e04c3fSmrg   case nir_op_fmul: return BRW_OPCODE_MUL;
406001e04c3fSmrg   case nir_op_imin: return BRW_OPCODE_SEL;
406101e04c3fSmrg   case nir_op_umin: return BRW_OPCODE_SEL;
406201e04c3fSmrg   case nir_op_fmin: return BRW_OPCODE_SEL;
406301e04c3fSmrg   case nir_op_imax: return BRW_OPCODE_SEL;
406401e04c3fSmrg   case nir_op_umax: return BRW_OPCODE_SEL;
406501e04c3fSmrg   case nir_op_fmax: return BRW_OPCODE_SEL;
406601e04c3fSmrg   case nir_op_iand: return BRW_OPCODE_AND;
406701e04c3fSmrg   case nir_op_ior:  return BRW_OPCODE_OR;
406801e04c3fSmrg   case nir_op_ixor: return BRW_OPCODE_XOR;
406901e04c3fSmrg   default:
407001e04c3fSmrg      unreachable("Invalid reduction operation");
407101e04c3fSmrg   }
407201e04c3fSmrg}
407301e04c3fSmrg
407401e04c3fSmrgstatic brw_conditional_mod
407501e04c3fSmrgbrw_cond_mod_for_nir_reduction_op(nir_op op)
407601e04c3fSmrg{
407701e04c3fSmrg   switch (op) {
407801e04c3fSmrg   case nir_op_iadd: return BRW_CONDITIONAL_NONE;
407901e04c3fSmrg   case nir_op_fadd: return BRW_CONDITIONAL_NONE;
408001e04c3fSmrg   case nir_op_imul: return BRW_CONDITIONAL_NONE;
408101e04c3fSmrg   case nir_op_fmul: return BRW_CONDITIONAL_NONE;
408201e04c3fSmrg   case nir_op_imin: return BRW_CONDITIONAL_L;
408301e04c3fSmrg   case nir_op_umin: return BRW_CONDITIONAL_L;
408401e04c3fSmrg   case nir_op_fmin: return BRW_CONDITIONAL_L;
408501e04c3fSmrg   case nir_op_imax: return BRW_CONDITIONAL_GE;
408601e04c3fSmrg   case nir_op_umax: return BRW_CONDITIONAL_GE;
408701e04c3fSmrg   case nir_op_fmax: return BRW_CONDITIONAL_GE;
408801e04c3fSmrg   case nir_op_iand: return BRW_CONDITIONAL_NONE;
408901e04c3fSmrg   case nir_op_ior:  return BRW_CONDITIONAL_NONE;
409001e04c3fSmrg   case nir_op_ixor: return BRW_CONDITIONAL_NONE;
409101e04c3fSmrg   default:
409201e04c3fSmrg      unreachable("Invalid reduction operation");
409301e04c3fSmrg   }
409401e04c3fSmrg}
409501e04c3fSmrg
409601e04c3fSmrgfs_reg
409701e04c3fSmrgfs_visitor::get_nir_image_intrinsic_image(const brw::fs_builder &bld,
409801e04c3fSmrg                                          nir_intrinsic_instr *instr)
409901e04c3fSmrg{
410001e04c3fSmrg   fs_reg image = retype(get_nir_src_imm(instr->src[0]), BRW_REGISTER_TYPE_UD);
41017ec681f3Smrg   fs_reg surf_index = image;
410201e04c3fSmrg
410301e04c3fSmrg   if (stage_prog_data->binding_table.image_start > 0) {
410401e04c3fSmrg      if (image.file == BRW_IMMEDIATE_VALUE) {
41057ec681f3Smrg         surf_index =
41067ec681f3Smrg            brw_imm_ud(image.d + stage_prog_data->binding_table.image_start);
410701e04c3fSmrg      } else {
41087ec681f3Smrg         surf_index = vgrf(glsl_type::uint_type);
41097ec681f3Smrg         bld.ADD(surf_index, image,
411001e04c3fSmrg                 brw_imm_d(stage_prog_data->binding_table.image_start));
411101e04c3fSmrg      }
411201e04c3fSmrg   }
411301e04c3fSmrg
41147ec681f3Smrg   return bld.emit_uniformize(surf_index);
411501e04c3fSmrg}
411601e04c3fSmrg
41179f464c52Smayafs_reg
41189f464c52Smayafs_visitor::get_nir_ssbo_intrinsic_index(const brw::fs_builder &bld,
41199f464c52Smaya                                         nir_intrinsic_instr *instr)
41209f464c52Smaya{
41219f464c52Smaya   /* SSBO stores are weird in that their index is in src[1] */
41227ec681f3Smrg   const bool is_store =
41237ec681f3Smrg      instr->intrinsic == nir_intrinsic_store_ssbo ||
41247ec681f3Smrg      instr->intrinsic == nir_intrinsic_store_ssbo_block_intel;
41257ec681f3Smrg   const unsigned src = is_store ? 1 : 0;
41269f464c52Smaya
41279f464c52Smaya   if (nir_src_is_const(instr->src[src])) {
41289f464c52Smaya      unsigned index = stage_prog_data->binding_table.ssbo_start +
41299f464c52Smaya                       nir_src_as_uint(instr->src[src]);
41307ec681f3Smrg      return brw_imm_ud(index);
41319f464c52Smaya   } else {
41327ec681f3Smrg      fs_reg surf_index = vgrf(glsl_type::uint_type);
41339f464c52Smaya      bld.ADD(surf_index, get_nir_src(instr->src[src]),
41349f464c52Smaya              brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
41357ec681f3Smrg      return bld.emit_uniformize(surf_index);
41369f464c52Smaya   }
41377ec681f3Smrg}
41389f464c52Smaya
41397ec681f3Smrg/**
41407ec681f3Smrg * The offsets we get from NIR act as if each SIMD channel has it's own blob
41417ec681f3Smrg * of contiguous space.  However, if we actually place each SIMD channel in
41427ec681f3Smrg * it's own space, we end up with terrible cache performance because each SIMD
41437ec681f3Smrg * channel accesses a different cache line even when they're all accessing the
41447ec681f3Smrg * same byte offset.  To deal with this problem, we swizzle the address using
41457ec681f3Smrg * a simple algorithm which ensures that any time a SIMD message reads or
41467ec681f3Smrg * writes the same address, it's all in the same cache line.  We have to keep
41477ec681f3Smrg * the bottom two bits fixed so that we can read/write up to a dword at a time
41487ec681f3Smrg * and the individual element is contiguous.  We do this by splitting the
41497ec681f3Smrg * address as follows:
41507ec681f3Smrg *
41517ec681f3Smrg *    31                             4-6           2          0
41527ec681f3Smrg *    +-------------------------------+------------+----------+
41537ec681f3Smrg *    |        Hi address bits        | chan index | addr low |
41547ec681f3Smrg *    +-------------------------------+------------+----------+
41557ec681f3Smrg *
41567ec681f3Smrg * In other words, the bottom two address bits stay, and the top 30 get
41577ec681f3Smrg * shifted up so that we can stick the SIMD channel index in the middle.  This
41587ec681f3Smrg * way, we can access 8, 16, or 32-bit elements and, when accessing a 32-bit
41597ec681f3Smrg * at the same logical offset, the scratch read/write instruction acts on
41607ec681f3Smrg * continuous elements and we get good cache locality.
41617ec681f3Smrg */
41627ec681f3Smrgfs_reg
41637ec681f3Smrgfs_visitor::swizzle_nir_scratch_addr(const brw::fs_builder &bld,
41647ec681f3Smrg                                     const fs_reg &nir_addr,
41657ec681f3Smrg                                     bool in_dwords)
41667ec681f3Smrg{
41677ec681f3Smrg   const fs_reg &chan_index =
41687ec681f3Smrg      nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION];
41697ec681f3Smrg   const unsigned chan_index_bits = ffs(dispatch_width) - 1;
41707ec681f3Smrg
41717ec681f3Smrg   fs_reg addr = bld.vgrf(BRW_REGISTER_TYPE_UD);
41727ec681f3Smrg   if (in_dwords) {
41737ec681f3Smrg      /* In this case, we know the address is aligned to a DWORD and we want
41747ec681f3Smrg       * the final address in DWORDs.
41757ec681f3Smrg       */
41767ec681f3Smrg      bld.SHL(addr, nir_addr, brw_imm_ud(chan_index_bits - 2));
41777ec681f3Smrg      bld.OR(addr, addr, chan_index);
41787ec681f3Smrg   } else {
41797ec681f3Smrg      /* This case substantially more annoying because we have to pay
41807ec681f3Smrg       * attention to those pesky two bottom bits.
41817ec681f3Smrg       */
41827ec681f3Smrg      fs_reg addr_hi = bld.vgrf(BRW_REGISTER_TYPE_UD);
41837ec681f3Smrg      bld.AND(addr_hi, nir_addr, brw_imm_ud(~0x3u));
41847ec681f3Smrg      bld.SHL(addr_hi, addr_hi, brw_imm_ud(chan_index_bits));
41857ec681f3Smrg      fs_reg chan_addr = bld.vgrf(BRW_REGISTER_TYPE_UD);
41867ec681f3Smrg      bld.SHL(chan_addr, chan_index, brw_imm_ud(2));
41877ec681f3Smrg      bld.AND(addr, nir_addr, brw_imm_ud(0x3u));
41887ec681f3Smrg      bld.OR(addr, addr, addr_hi);
41897ec681f3Smrg      bld.OR(addr, addr, chan_addr);
41907ec681f3Smrg   }
41917ec681f3Smrg   return addr;
41929f464c52Smaya}
41939f464c52Smaya
419401e04c3fSmrgstatic unsigned
41957ec681f3Smrgchoose_oword_block_size_dwords(unsigned dwords)
419601e04c3fSmrg{
41977ec681f3Smrg   unsigned block;
41987ec681f3Smrg   if (dwords >= 32) {
41997ec681f3Smrg      block = 32;
42007ec681f3Smrg   } else if (dwords >= 16) {
42017ec681f3Smrg      block = 16;
42027ec681f3Smrg   } else {
42037ec681f3Smrg      block = 8;
420401e04c3fSmrg   }
42057ec681f3Smrg   assert(block <= dwords);
42067ec681f3Smrg   return block;
42077ec681f3Smrg}
42087ec681f3Smrg
42097ec681f3Smrgstatic void
42107ec681f3Smrgincrement_a64_address(const fs_builder &bld, fs_reg address, uint32_t v)
42117ec681f3Smrg{
42127ec681f3Smrg   if (bld.shader->devinfo->has_64bit_int) {
42137ec681f3Smrg      bld.ADD(address, address, brw_imm_ud(v));
42147ec681f3Smrg   } else {
42157ec681f3Smrg      fs_reg low = retype(address, BRW_REGISTER_TYPE_UD);
42167ec681f3Smrg      fs_reg high = offset(low, bld, 1);
42177ec681f3Smrg
42187ec681f3Smrg      /* Add low and if that overflows, add carry to high. */
42197ec681f3Smrg      bld.ADD(low, low, brw_imm_ud(v))->conditional_mod = BRW_CONDITIONAL_O;
42207ec681f3Smrg      bld.ADD(high, high, brw_imm_ud(0x1))->predicate = BRW_PREDICATE_NORMAL;
42217ec681f3Smrg   }
42227ec681f3Smrg}
42237ec681f3Smrg
42247ec681f3Smrgstatic fs_reg
42257ec681f3Smrgemit_fence(const fs_builder &bld, enum opcode opcode,
42267ec681f3Smrg           uint8_t sfid, bool commit_enable, uint8_t bti)
42277ec681f3Smrg{
42287ec681f3Smrg   assert(opcode == SHADER_OPCODE_INTERLOCK ||
42297ec681f3Smrg          opcode == SHADER_OPCODE_MEMORY_FENCE);
42307ec681f3Smrg
42317ec681f3Smrg   fs_reg dst = bld.vgrf(BRW_REGISTER_TYPE_UD);
42327ec681f3Smrg   fs_inst *fence = bld.emit(opcode, dst, brw_vec8_grf(0, 0),
42337ec681f3Smrg                             brw_imm_ud(commit_enable),
42347ec681f3Smrg                             brw_imm_ud(bti));
42357ec681f3Smrg   fence->sfid = sfid;
42367ec681f3Smrg   return dst;
423701e04c3fSmrg}
423801e04c3fSmrg
423901e04c3fSmrgvoid
424001e04c3fSmrgfs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
424101e04c3fSmrg{
424201e04c3fSmrg   fs_reg dest;
424301e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
424401e04c3fSmrg      dest = get_nir_dest(instr->dest);
424501e04c3fSmrg
424601e04c3fSmrg   switch (instr->intrinsic) {
424701e04c3fSmrg   case nir_intrinsic_image_load:
424801e04c3fSmrg   case nir_intrinsic_image_store:
424901e04c3fSmrg   case nir_intrinsic_image_atomic_add:
42507ec681f3Smrg   case nir_intrinsic_image_atomic_imin:
42517ec681f3Smrg   case nir_intrinsic_image_atomic_umin:
42527ec681f3Smrg   case nir_intrinsic_image_atomic_imax:
42537ec681f3Smrg   case nir_intrinsic_image_atomic_umax:
425401e04c3fSmrg   case nir_intrinsic_image_atomic_and:
425501e04c3fSmrg   case nir_intrinsic_image_atomic_or:
425601e04c3fSmrg   case nir_intrinsic_image_atomic_xor:
425701e04c3fSmrg   case nir_intrinsic_image_atomic_exchange:
42589f464c52Smaya   case nir_intrinsic_image_atomic_comp_swap:
42599f464c52Smaya   case nir_intrinsic_bindless_image_load:
42609f464c52Smaya   case nir_intrinsic_bindless_image_store:
42619f464c52Smaya   case nir_intrinsic_bindless_image_atomic_add:
42627ec681f3Smrg   case nir_intrinsic_bindless_image_atomic_imin:
42637ec681f3Smrg   case nir_intrinsic_bindless_image_atomic_umin:
42647ec681f3Smrg   case nir_intrinsic_bindless_image_atomic_imax:
42657ec681f3Smrg   case nir_intrinsic_bindless_image_atomic_umax:
42669f464c52Smaya   case nir_intrinsic_bindless_image_atomic_and:
42679f464c52Smaya   case nir_intrinsic_bindless_image_atomic_or:
42689f464c52Smaya   case nir_intrinsic_bindless_image_atomic_xor:
42699f464c52Smaya   case nir_intrinsic_bindless_image_atomic_exchange:
42709f464c52Smaya   case nir_intrinsic_bindless_image_atomic_comp_swap: {
427101e04c3fSmrg      /* Get some metadata from the image intrinsic. */
427201e04c3fSmrg      const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
427301e04c3fSmrg
42749f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
42759f464c52Smaya
42769f464c52Smaya      switch (instr->intrinsic) {
42779f464c52Smaya      case nir_intrinsic_image_load:
42789f464c52Smaya      case nir_intrinsic_image_store:
42799f464c52Smaya      case nir_intrinsic_image_atomic_add:
42807ec681f3Smrg      case nir_intrinsic_image_atomic_imin:
42817ec681f3Smrg      case nir_intrinsic_image_atomic_umin:
42827ec681f3Smrg      case nir_intrinsic_image_atomic_imax:
42837ec681f3Smrg      case nir_intrinsic_image_atomic_umax:
42849f464c52Smaya      case nir_intrinsic_image_atomic_and:
42859f464c52Smaya      case nir_intrinsic_image_atomic_or:
42869f464c52Smaya      case nir_intrinsic_image_atomic_xor:
42879f464c52Smaya      case nir_intrinsic_image_atomic_exchange:
42889f464c52Smaya      case nir_intrinsic_image_atomic_comp_swap:
42899f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_SURFACE] =
42909f464c52Smaya            get_nir_image_intrinsic_image(bld, instr);
42919f464c52Smaya         break;
42929f464c52Smaya
42939f464c52Smaya      default:
42949f464c52Smaya         /* Bindless */
42959f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_SURFACE_HANDLE] =
42969f464c52Smaya            bld.emit_uniformize(get_nir_src(instr->src[0]));
42979f464c52Smaya         break;
42989f464c52Smaya      }
42999f464c52Smaya
43009f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
43019f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] =
43027ec681f3Smrg         brw_imm_ud(nir_image_intrinsic_coord_components(instr));
430301e04c3fSmrg
430401e04c3fSmrg      /* Emit an image load, store or atomic op. */
43059f464c52Smaya      if (instr->intrinsic == nir_intrinsic_image_load ||
43069f464c52Smaya          instr->intrinsic == nir_intrinsic_bindless_image_load) {
43079f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
43087ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
43099f464c52Smaya         fs_inst *inst =
43109f464c52Smaya            bld.emit(SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL,
43119f464c52Smaya                     dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
43129f464c52Smaya         inst->size_written = instr->num_components * dispatch_width * 4;
43139f464c52Smaya      } else if (instr->intrinsic == nir_intrinsic_image_store ||
43149f464c52Smaya                 instr->intrinsic == nir_intrinsic_bindless_image_store) {
43159f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
43169f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[3]);
43177ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
43189f464c52Smaya         bld.emit(SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL,
43199f464c52Smaya                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
432001e04c3fSmrg      } else {
432101e04c3fSmrg         unsigned num_srcs = info->num_srcs;
43227ec681f3Smrg         int op = brw_aop_for_nir_intrinsic(instr);
43237ec681f3Smrg         if (op == BRW_AOP_INC || op == BRW_AOP_DEC) {
432401e04c3fSmrg            assert(num_srcs == 4);
43257ec681f3Smrg            num_srcs = 3;
432601e04c3fSmrg         }
432701e04c3fSmrg
43289f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
432901e04c3fSmrg
43309f464c52Smaya         fs_reg data;
43319f464c52Smaya         if (num_srcs >= 4)
43329f464c52Smaya            data = get_nir_src(instr->src[3]);
43339f464c52Smaya         if (num_srcs >= 5) {
43349f464c52Smaya            fs_reg tmp = bld.vgrf(data.type, 2);
43359f464c52Smaya            fs_reg sources[2] = { data, get_nir_src(instr->src[4]) };
43369f464c52Smaya            bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
43379f464c52Smaya            data = tmp;
43389f464c52Smaya         }
43399f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = data;
43407ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
434101e04c3fSmrg
43429f464c52Smaya         bld.emit(SHADER_OPCODE_TYPED_ATOMIC_LOGICAL,
43439f464c52Smaya                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
434401e04c3fSmrg      }
434501e04c3fSmrg      break;
434601e04c3fSmrg   }
434701e04c3fSmrg
43489f464c52Smaya   case nir_intrinsic_image_size:
43499f464c52Smaya   case nir_intrinsic_bindless_image_size: {
43507ec681f3Smrg      /* Cube image sizes should have previously been lowered to a 2D array */
43517ec681f3Smrg      assert(nir_intrinsic_image_dim(instr) != GLSL_SAMPLER_DIM_CUBE);
43527ec681f3Smrg
435301e04c3fSmrg      /* Unlike the [un]typed load and store opcodes, the TXS that this turns
435401e04c3fSmrg       * into will handle the binding table index for us in the geneerator.
43559f464c52Smaya       * Incidentally, this means that we can handle bindless with exactly the
43569f464c52Smaya       * same code.
435701e04c3fSmrg       */
435801e04c3fSmrg      fs_reg image = retype(get_nir_src_imm(instr->src[0]),
435901e04c3fSmrg                            BRW_REGISTER_TYPE_UD);
436001e04c3fSmrg      image = bld.emit_uniformize(image);
436101e04c3fSmrg
43627ec681f3Smrg      assert(nir_src_as_uint(instr->src[1]) == 0);
43637ec681f3Smrg
43649f464c52Smaya      fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
43659f464c52Smaya      if (instr->intrinsic == nir_intrinsic_image_size)
43669f464c52Smaya         srcs[TEX_LOGICAL_SRC_SURFACE] = image;
43679f464c52Smaya      else
43689f464c52Smaya         srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = image;
43699f464c52Smaya      srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_d(0);
43709f464c52Smaya      srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(0);
43719f464c52Smaya      srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(0);
43729f464c52Smaya
437301e04c3fSmrg      /* Since the image size is always uniform, we can just emit a SIMD8
437401e04c3fSmrg       * query instruction and splat the result out.
437501e04c3fSmrg       */
437601e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(8, 0);
437701e04c3fSmrg
437801e04c3fSmrg      fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
43799f464c52Smaya      fs_inst *inst = ubld.emit(SHADER_OPCODE_IMAGE_SIZE_LOGICAL,
43809f464c52Smaya                                tmp, srcs, ARRAY_SIZE(srcs));
438101e04c3fSmrg      inst->size_written = 4 * REG_SIZE;
438201e04c3fSmrg
438301e04c3fSmrg      for (unsigned c = 0; c < instr->dest.ssa.num_components; ++c) {
43847ec681f3Smrg         bld.MOV(offset(retype(dest, tmp.type), bld, c),
43857ec681f3Smrg                 component(offset(tmp, ubld, c), 0));
438601e04c3fSmrg      }
438701e04c3fSmrg      break;
438801e04c3fSmrg   }
438901e04c3fSmrg
439001e04c3fSmrg   case nir_intrinsic_image_load_raw_intel: {
43919f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
43929f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_SURFACE] =
43939f464c52Smaya         get_nir_image_intrinsic_image(bld, instr);
43949f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
43959f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
43969f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
43977ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
439801e04c3fSmrg
43999f464c52Smaya      fs_inst *inst =
44009f464c52Smaya         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
44019f464c52Smaya                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
44029f464c52Smaya      inst->size_written = instr->num_components * dispatch_width * 4;
440301e04c3fSmrg      break;
440401e04c3fSmrg   }
440501e04c3fSmrg
440601e04c3fSmrg   case nir_intrinsic_image_store_raw_intel: {
44079f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
44089f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_SURFACE] =
44099f464c52Smaya         get_nir_image_intrinsic_image(bld, instr);
44109f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
44119f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[2]);
44129f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
44139f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
44147ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
441501e04c3fSmrg
44169f464c52Smaya      bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
44179f464c52Smaya               fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
441801e04c3fSmrg      break;
441901e04c3fSmrg   }
442001e04c3fSmrg
44217ec681f3Smrg   case nir_intrinsic_scoped_barrier:
44227ec681f3Smrg      assert(nir_intrinsic_execution_scope(instr) == NIR_SCOPE_NONE);
44237ec681f3Smrg      FALLTHROUGH;
442401e04c3fSmrg   case nir_intrinsic_group_memory_barrier:
442501e04c3fSmrg   case nir_intrinsic_memory_barrier_shared:
442601e04c3fSmrg   case nir_intrinsic_memory_barrier_buffer:
442701e04c3fSmrg   case nir_intrinsic_memory_barrier_image:
44287ec681f3Smrg   case nir_intrinsic_memory_barrier:
44297ec681f3Smrg   case nir_intrinsic_begin_invocation_interlock:
44307ec681f3Smrg   case nir_intrinsic_end_invocation_interlock: {
44317ec681f3Smrg      bool ugm_fence, slm_fence, tgm_fence, urb_fence;
44327ec681f3Smrg      const enum opcode opcode =
44337ec681f3Smrg         instr->intrinsic == nir_intrinsic_begin_invocation_interlock ?
44347ec681f3Smrg         SHADER_OPCODE_INTERLOCK : SHADER_OPCODE_MEMORY_FENCE;
44357ec681f3Smrg
44367ec681f3Smrg      switch (instr->intrinsic) {
44377ec681f3Smrg      case nir_intrinsic_scoped_barrier: {
44387ec681f3Smrg         nir_variable_mode modes = nir_intrinsic_memory_modes(instr);
44397ec681f3Smrg         ugm_fence = modes & (nir_var_mem_ssbo | nir_var_mem_global);
44407ec681f3Smrg         slm_fence = modes & nir_var_mem_shared;
44417ec681f3Smrg         tgm_fence = modes & nir_var_mem_ssbo;
44427ec681f3Smrg         urb_fence = modes & nir_var_shader_out;
44437ec681f3Smrg         break;
44447ec681f3Smrg      }
44457ec681f3Smrg
44467ec681f3Smrg      case nir_intrinsic_begin_invocation_interlock:
44477ec681f3Smrg      case nir_intrinsic_end_invocation_interlock:
44487ec681f3Smrg         /* For beginInvocationInterlockARB(), we will generate a memory fence
44497ec681f3Smrg          * but with a different opcode so that generator can pick SENDC
44507ec681f3Smrg          * instead of SEND.
44517ec681f3Smrg          *
44527ec681f3Smrg          * For endInvocationInterlockARB(), we need to insert a memory fence which
44537ec681f3Smrg          * stalls in the shader until the memory transactions prior to that
44547ec681f3Smrg          * fence are complete.  This ensures that the shader does not end before
44557ec681f3Smrg          * any writes from its critical section have landed.  Otherwise, you can
44567ec681f3Smrg          * end up with a case where the next invocation on that pixel properly
44577ec681f3Smrg          * stalls for previous FS invocation on its pixel to complete but
44587ec681f3Smrg          * doesn't actually wait for the dataport memory transactions from that
44597ec681f3Smrg          * thread to land before submitting its own.
44607ec681f3Smrg          *
44617ec681f3Smrg          * Handling them here will allow the logic for IVB render cache (see
44627ec681f3Smrg          * below) to be reused.
44637ec681f3Smrg          */
44647ec681f3Smrg         assert(stage == MESA_SHADER_FRAGMENT);
44657ec681f3Smrg         ugm_fence = tgm_fence = true;
44667ec681f3Smrg         slm_fence = urb_fence = false;
44677ec681f3Smrg         break;
44687ec681f3Smrg
44697ec681f3Smrg      default:
44707ec681f3Smrg         ugm_fence = instr->intrinsic != nir_intrinsic_memory_barrier_shared &&
44717ec681f3Smrg                     instr->intrinsic != nir_intrinsic_memory_barrier_image;
44727ec681f3Smrg         slm_fence = instr->intrinsic == nir_intrinsic_group_memory_barrier ||
44737ec681f3Smrg                     instr->intrinsic == nir_intrinsic_memory_barrier ||
44747ec681f3Smrg                     instr->intrinsic == nir_intrinsic_memory_barrier_shared;
44757ec681f3Smrg         tgm_fence = instr->intrinsic == nir_intrinsic_group_memory_barrier ||
44767ec681f3Smrg                     instr->intrinsic == nir_intrinsic_memory_barrier ||
44777ec681f3Smrg                     instr->intrinsic == nir_intrinsic_memory_barrier_image;
44787ec681f3Smrg         urb_fence = instr->intrinsic == nir_intrinsic_memory_barrier;
44797ec681f3Smrg         break;
44807ec681f3Smrg      }
44817ec681f3Smrg
44827ec681f3Smrg      if (nir->info.shared_size > 0) {
44837ec681f3Smrg         assert(gl_shader_stage_uses_workgroup(stage));
44847ec681f3Smrg      } else {
44857ec681f3Smrg         slm_fence = false;
44867ec681f3Smrg      }
44877ec681f3Smrg
44887ec681f3Smrg      /* If the workgroup fits in a single HW thread, the messages for SLM are
44897ec681f3Smrg       * processed in-order and the shader itself is already synchronized so
44907ec681f3Smrg       * the memory fence is not necessary.
44917ec681f3Smrg       *
44927ec681f3Smrg       * TODO: Check if applies for many HW threads sharing same Data Port.
44937ec681f3Smrg       */
44947ec681f3Smrg      if (!nir->info.workgroup_size_variable &&
44957ec681f3Smrg          slm_fence && workgroup_size() <= dispatch_width)
44967ec681f3Smrg         slm_fence = false;
44977ec681f3Smrg
44987ec681f3Smrg      if (stage != MESA_SHADER_TESS_CTRL)
44997ec681f3Smrg         urb_fence = false;
45007ec681f3Smrg
45017ec681f3Smrg      unsigned fence_regs_count = 0;
45027ec681f3Smrg      fs_reg fence_regs[3] = {};
45037ec681f3Smrg
450401e04c3fSmrg      const fs_builder ubld = bld.group(8, 0);
45057ec681f3Smrg
45067ec681f3Smrg      if (devinfo->has_lsc) {
45077ec681f3Smrg         assert(devinfo->verx10 >= 125);
45087ec681f3Smrg         if (ugm_fence) {
45097ec681f3Smrg            fence_regs[fence_regs_count++] =
45107ec681f3Smrg               emit_fence(ubld, opcode, GFX12_SFID_UGM,
45117ec681f3Smrg                          true /* commit_enable */,
45127ec681f3Smrg                          0 /* bti; ignored for LSC */);
45137ec681f3Smrg         }
45147ec681f3Smrg
45157ec681f3Smrg         if (tgm_fence) {
45167ec681f3Smrg            fence_regs[fence_regs_count++] =
45177ec681f3Smrg               emit_fence(ubld, opcode, GFX12_SFID_TGM,
45187ec681f3Smrg                          true /* commit_enable */,
45197ec681f3Smrg                          0 /* bti; ignored for LSC */);
45207ec681f3Smrg         }
45217ec681f3Smrg
45227ec681f3Smrg         if (slm_fence) {
45237ec681f3Smrg            assert(opcode == SHADER_OPCODE_MEMORY_FENCE);
45247ec681f3Smrg            fence_regs[fence_regs_count++] =
45257ec681f3Smrg               emit_fence(ubld, opcode, GFX12_SFID_SLM,
45267ec681f3Smrg                          true /* commit_enable */,
45277ec681f3Smrg                          0 /* BTI; ignored for LSC */);
45287ec681f3Smrg         }
45297ec681f3Smrg
45307ec681f3Smrg         if (urb_fence) {
45317ec681f3Smrg            assert(opcode == SHADER_OPCODE_MEMORY_FENCE);
45327ec681f3Smrg            fence_regs[fence_regs_count++] =
45337ec681f3Smrg               emit_fence(ubld, opcode, BRW_SFID_URB,
45347ec681f3Smrg                          true /* commit_enable */,
45357ec681f3Smrg                          0 /* BTI; ignored for LSC */);
45367ec681f3Smrg         }
45377ec681f3Smrg      } else if (devinfo->ver >= 11) {
45387ec681f3Smrg         if (tgm_fence || ugm_fence || urb_fence) {
45397ec681f3Smrg            fence_regs[fence_regs_count++] =
45407ec681f3Smrg               emit_fence(ubld, opcode, GFX7_SFID_DATAPORT_DATA_CACHE,
45417ec681f3Smrg                          true /* commit_enable HSD ES # 1404612949 */,
45427ec681f3Smrg                          0 /* BTI = 0 means data cache */);
45437ec681f3Smrg         }
45447ec681f3Smrg
45457ec681f3Smrg         if (slm_fence) {
45467ec681f3Smrg            assert(opcode == SHADER_OPCODE_MEMORY_FENCE);
45477ec681f3Smrg            fence_regs[fence_regs_count++] =
45487ec681f3Smrg               emit_fence(ubld, opcode, GFX7_SFID_DATAPORT_DATA_CACHE,
45497ec681f3Smrg                          true /* commit_enable HSD ES # 1404612949 */,
45507ec681f3Smrg                          GFX7_BTI_SLM);
45517ec681f3Smrg         }
45527ec681f3Smrg      } else {
45537ec681f3Smrg         /* Prior to Icelake, they're all lumped into a single cache except on
45547ec681f3Smrg          * Ivy Bridge and Bay Trail where typed messages actually go through
45557ec681f3Smrg          * the render cache.  There, we need both fences because we may
45567ec681f3Smrg          * access storage images as either typed or untyped.
45577ec681f3Smrg          */
45587ec681f3Smrg         const bool render_fence = tgm_fence && devinfo->verx10 == 70;
45597ec681f3Smrg
45607ec681f3Smrg         const bool commit_enable = render_fence ||
45617ec681f3Smrg            instr->intrinsic == nir_intrinsic_end_invocation_interlock;
45627ec681f3Smrg
45637ec681f3Smrg         if (tgm_fence || ugm_fence || slm_fence || urb_fence) {
45647ec681f3Smrg            fence_regs[fence_regs_count++] =
45657ec681f3Smrg               emit_fence(ubld, opcode, GFX7_SFID_DATAPORT_DATA_CACHE,
45667ec681f3Smrg                          commit_enable, 0 /* BTI */);
45677ec681f3Smrg         }
45687ec681f3Smrg
45697ec681f3Smrg         if (render_fence) {
45707ec681f3Smrg            fence_regs[fence_regs_count++] =
45717ec681f3Smrg               emit_fence(ubld, opcode, GFX6_SFID_DATAPORT_RENDER_CACHE,
45727ec681f3Smrg                          commit_enable, /* bti */ 0);
45737ec681f3Smrg         }
45747ec681f3Smrg      }
45757ec681f3Smrg
45767ec681f3Smrg      assert(fence_regs_count <= ARRAY_SIZE(fence_regs));
45777ec681f3Smrg
45787ec681f3Smrg      /* There are three cases where we want to insert a stall:
45797ec681f3Smrg       *
45807ec681f3Smrg       *  1. If we're a nir_intrinsic_end_invocation_interlock.  This is
45817ec681f3Smrg       *     required to ensure that the shader EOT doesn't happen until
45827ec681f3Smrg       *     after the fence returns.  Otherwise, we might end up with the
45837ec681f3Smrg       *     next shader invocation for that pixel not respecting our fence
45847ec681f3Smrg       *     because it may happen on a different HW thread.
45857ec681f3Smrg       *
45867ec681f3Smrg       *  2. If we have multiple fences.  This is required to ensure that
45877ec681f3Smrg       *     they all complete and nothing gets weirdly out-of-order.
45887ec681f3Smrg       *
45897ec681f3Smrg       *  3. If we have no fences.  In this case, we need at least a
45907ec681f3Smrg       *     scheduling barrier to keep the compiler from moving things
45917ec681f3Smrg       *     around in an invalid way.
45927ec681f3Smrg       */
45937ec681f3Smrg      if (instr->intrinsic == nir_intrinsic_end_invocation_interlock ||
45947ec681f3Smrg          fence_regs_count != 1) {
45957ec681f3Smrg         ubld.exec_all().group(1, 0).emit(
45967ec681f3Smrg            FS_OPCODE_SCHEDULING_FENCE, ubld.null_reg_ud(),
45977ec681f3Smrg            fence_regs, fence_regs_count);
45987ec681f3Smrg      }
45997ec681f3Smrg
460001e04c3fSmrg      break;
460101e04c3fSmrg   }
460201e04c3fSmrg
46037ec681f3Smrg   case nir_intrinsic_memory_barrier_tcs_patch:
46047ec681f3Smrg      break;
46057ec681f3Smrg
460601e04c3fSmrg   case nir_intrinsic_shader_clock: {
460701e04c3fSmrg      /* We cannot do anything if there is an event, so ignore it for now */
460801e04c3fSmrg      const fs_reg shader_clock = get_timestamp(bld);
460901e04c3fSmrg      const fs_reg srcs[] = { component(shader_clock, 0),
461001e04c3fSmrg                              component(shader_clock, 1) };
461101e04c3fSmrg      bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
461201e04c3fSmrg      break;
461301e04c3fSmrg   }
461401e04c3fSmrg
461501e04c3fSmrg   case nir_intrinsic_image_samples:
461601e04c3fSmrg      /* The driver does not support multi-sampled images. */
461701e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
461801e04c3fSmrg      break;
461901e04c3fSmrg
46207ec681f3Smrg   case nir_intrinsic_load_reloc_const_intel: {
46217ec681f3Smrg      uint32_t id = nir_intrinsic_param_idx(instr);
46227ec681f3Smrg      bld.emit(SHADER_OPCODE_MOV_RELOC_IMM,
46237ec681f3Smrg               dest, brw_imm_ud(id));
46247ec681f3Smrg      break;
46257ec681f3Smrg   }
46267ec681f3Smrg
462701e04c3fSmrg   case nir_intrinsic_load_uniform: {
462801e04c3fSmrg      /* Offsets are in bytes but they should always aligned to
462901e04c3fSmrg       * the type size
463001e04c3fSmrg       */
463101e04c3fSmrg      assert(instr->const_index[0] % 4 == 0 ||
463201e04c3fSmrg             instr->const_index[0] % type_sz(dest.type) == 0);
463301e04c3fSmrg
463401e04c3fSmrg      fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
463501e04c3fSmrg
46369f464c52Smaya      if (nir_src_is_const(instr->src[0])) {
46379f464c52Smaya         unsigned load_offset = nir_src_as_uint(instr->src[0]);
46389f464c52Smaya         assert(load_offset % type_sz(dest.type) == 0);
463901e04c3fSmrg         /* For 16-bit types we add the module of the const_index[0]
464001e04c3fSmrg          * offset to access to not 32-bit aligned element
464101e04c3fSmrg          */
46429f464c52Smaya         src.offset = load_offset + instr->const_index[0] % 4;
464301e04c3fSmrg
464401e04c3fSmrg         for (unsigned j = 0; j < instr->num_components; j++) {
464501e04c3fSmrg            bld.MOV(offset(dest, bld, j), offset(src, bld, j));
464601e04c3fSmrg         }
464701e04c3fSmrg      } else {
464801e04c3fSmrg         fs_reg indirect = retype(get_nir_src(instr->src[0]),
464901e04c3fSmrg                                  BRW_REGISTER_TYPE_UD);
465001e04c3fSmrg
465101e04c3fSmrg         /* We need to pass a size to the MOV_INDIRECT but we don't want it to
465201e04c3fSmrg          * go past the end of the uniform.  In order to keep the n'th
465301e04c3fSmrg          * component from running past, we subtract off the size of all but
465401e04c3fSmrg          * one component of the vector.
465501e04c3fSmrg          */
465601e04c3fSmrg         assert(instr->const_index[1] >=
465701e04c3fSmrg                instr->num_components * (int) type_sz(dest.type));
465801e04c3fSmrg         unsigned read_size = instr->const_index[1] -
465901e04c3fSmrg            (instr->num_components - 1) * type_sz(dest.type);
466001e04c3fSmrg
466101e04c3fSmrg         bool supports_64bit_indirects =
46627ec681f3Smrg            !devinfo->is_cherryview && !intel_device_info_is_9lp(devinfo);
466301e04c3fSmrg
466401e04c3fSmrg         if (type_sz(dest.type) != 8 || supports_64bit_indirects) {
466501e04c3fSmrg            for (unsigned j = 0; j < instr->num_components; j++) {
466601e04c3fSmrg               bld.emit(SHADER_OPCODE_MOV_INDIRECT,
466701e04c3fSmrg                        offset(dest, bld, j), offset(src, bld, j),
466801e04c3fSmrg                        indirect, brw_imm_ud(read_size));
466901e04c3fSmrg            }
467001e04c3fSmrg         } else {
467101e04c3fSmrg            const unsigned num_mov_indirects =
467201e04c3fSmrg               type_sz(dest.type) / type_sz(BRW_REGISTER_TYPE_UD);
467301e04c3fSmrg            /* We read a little bit less per MOV INDIRECT, as they are now
467401e04c3fSmrg             * 32-bits ones instead of 64-bit. Fix read_size then.
467501e04c3fSmrg             */
467601e04c3fSmrg            const unsigned read_size_32bit = read_size -
467701e04c3fSmrg                (num_mov_indirects - 1) * type_sz(BRW_REGISTER_TYPE_UD);
467801e04c3fSmrg            for (unsigned j = 0; j < instr->num_components; j++) {
467901e04c3fSmrg               for (unsigned i = 0; i < num_mov_indirects; i++) {
468001e04c3fSmrg                  bld.emit(SHADER_OPCODE_MOV_INDIRECT,
468101e04c3fSmrg                           subscript(offset(dest, bld, j), BRW_REGISTER_TYPE_UD, i),
468201e04c3fSmrg                           subscript(offset(src, bld, j), BRW_REGISTER_TYPE_UD, i),
468301e04c3fSmrg                           indirect, brw_imm_ud(read_size_32bit));
468401e04c3fSmrg               }
468501e04c3fSmrg            }
468601e04c3fSmrg         }
468701e04c3fSmrg      }
468801e04c3fSmrg      break;
468901e04c3fSmrg   }
469001e04c3fSmrg
469101e04c3fSmrg   case nir_intrinsic_load_ubo: {
469201e04c3fSmrg      fs_reg surf_index;
46939f464c52Smaya      if (nir_src_is_const(instr->src[0])) {
469401e04c3fSmrg         const unsigned index = stage_prog_data->binding_table.ubo_start +
46959f464c52Smaya                                nir_src_as_uint(instr->src[0]);
469601e04c3fSmrg         surf_index = brw_imm_ud(index);
469701e04c3fSmrg      } else {
469801e04c3fSmrg         /* The block index is not a constant. Evaluate the index expression
469901e04c3fSmrg          * per-channel and add the base UBO index; we have to select a value
470001e04c3fSmrg          * from any live channel.
470101e04c3fSmrg          */
470201e04c3fSmrg         surf_index = vgrf(glsl_type::uint_type);
470301e04c3fSmrg         bld.ADD(surf_index, get_nir_src(instr->src[0]),
470401e04c3fSmrg                 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
470501e04c3fSmrg         surf_index = bld.emit_uniformize(surf_index);
470601e04c3fSmrg      }
470701e04c3fSmrg
47089f464c52Smaya      if (!nir_src_is_const(instr->src[1])) {
470901e04c3fSmrg         fs_reg base_offset = retype(get_nir_src(instr->src[1]),
471001e04c3fSmrg                                     BRW_REGISTER_TYPE_UD);
471101e04c3fSmrg
471201e04c3fSmrg         for (int i = 0; i < instr->num_components; i++)
471301e04c3fSmrg            VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
47147ec681f3Smrg                                       base_offset, i * type_sz(dest.type),
47157ec681f3Smrg                                       nir_dest_bit_size(instr->dest) / 8);
47167ec681f3Smrg
47177ec681f3Smrg         prog_data->has_ubo_pull = true;
471801e04c3fSmrg      } else {
471901e04c3fSmrg         /* Even if we are loading doubles, a pull constant load will load
472001e04c3fSmrg          * a 32-bit vec4, so should only reserve vgrf space for that. If we
472101e04c3fSmrg          * need to load a full dvec4 we will have to emit 2 loads. This is
472201e04c3fSmrg          * similar to demote_pull_constants(), except that in that case we
472301e04c3fSmrg          * see individual accesses to each component of the vector and then
472401e04c3fSmrg          * we let CSE deal with duplicate loads. Here we see a vector access
472501e04c3fSmrg          * and we have to split it if necessary.
472601e04c3fSmrg          */
472701e04c3fSmrg         const unsigned type_size = type_sz(dest.type);
47289f464c52Smaya         const unsigned load_offset = nir_src_as_uint(instr->src[1]);
472901e04c3fSmrg
473001e04c3fSmrg         /* See if we've selected this as a push constant candidate */
47319f464c52Smaya         if (nir_src_is_const(instr->src[0])) {
47329f464c52Smaya            const unsigned ubo_block = nir_src_as_uint(instr->src[0]);
47339f464c52Smaya            const unsigned offset_256b = load_offset / 32;
473401e04c3fSmrg
473501e04c3fSmrg            fs_reg push_reg;
473601e04c3fSmrg            for (int i = 0; i < 4; i++) {
473701e04c3fSmrg               const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
473801e04c3fSmrg               if (range->block == ubo_block &&
473901e04c3fSmrg                   offset_256b >= range->start &&
474001e04c3fSmrg                   offset_256b < range->start + range->length) {
474101e04c3fSmrg
474201e04c3fSmrg                  push_reg = fs_reg(UNIFORM, UBO_START + i, dest.type);
47439f464c52Smaya                  push_reg.offset = load_offset - 32 * range->start;
474401e04c3fSmrg                  break;
474501e04c3fSmrg               }
474601e04c3fSmrg            }
474701e04c3fSmrg
474801e04c3fSmrg            if (push_reg.file != BAD_FILE) {
474901e04c3fSmrg               for (unsigned i = 0; i < instr->num_components; i++) {
475001e04c3fSmrg                  bld.MOV(offset(dest, bld, i),
475101e04c3fSmrg                          byte_offset(push_reg, i * type_size));
475201e04c3fSmrg               }
475301e04c3fSmrg               break;
475401e04c3fSmrg            }
475501e04c3fSmrg         }
475601e04c3fSmrg
47577ec681f3Smrg         prog_data->has_ubo_pull = true;
47587ec681f3Smrg
475901e04c3fSmrg         const unsigned block_sz = 64; /* Fetch one cacheline at a time. */
476001e04c3fSmrg         const fs_builder ubld = bld.exec_all().group(block_sz / 4, 0);
476101e04c3fSmrg         const fs_reg packed_consts = ubld.vgrf(BRW_REGISTER_TYPE_UD);
476201e04c3fSmrg
476301e04c3fSmrg         for (unsigned c = 0; c < instr->num_components;) {
47649f464c52Smaya            const unsigned base = load_offset + c * type_size;
476501e04c3fSmrg            /* Number of usable components in the next block-aligned load. */
476601e04c3fSmrg            const unsigned count = MIN2(instr->num_components - c,
476701e04c3fSmrg                                        (block_sz - base % block_sz) / type_size);
476801e04c3fSmrg
476901e04c3fSmrg            ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
477001e04c3fSmrg                      packed_consts, surf_index,
477101e04c3fSmrg                      brw_imm_ud(base & ~(block_sz - 1)));
477201e04c3fSmrg
477301e04c3fSmrg            const fs_reg consts =
477401e04c3fSmrg               retype(byte_offset(packed_consts, base & (block_sz - 1)),
477501e04c3fSmrg                      dest.type);
477601e04c3fSmrg
477701e04c3fSmrg            for (unsigned d = 0; d < count; d++)
477801e04c3fSmrg               bld.MOV(offset(dest, bld, c + d), component(consts, d));
477901e04c3fSmrg
478001e04c3fSmrg            c += count;
478101e04c3fSmrg         }
478201e04c3fSmrg      }
478301e04c3fSmrg      break;
478401e04c3fSmrg   }
478501e04c3fSmrg
47867ec681f3Smrg   case nir_intrinsic_load_global:
47877ec681f3Smrg   case nir_intrinsic_load_global_constant: {
47887ec681f3Smrg      assert(devinfo->ver >= 8);
47899f464c52Smaya
47907ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) <= 32);
47917ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
47927ec681f3Smrg      if (nir_dest_bit_size(instr->dest) == 32 &&
47937ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
47947ec681f3Smrg         assert(nir_dest_num_components(instr->dest) <= 4);
47959f464c52Smaya         fs_inst *inst = bld.emit(SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL,
47969f464c52Smaya                                  dest,
47979f464c52Smaya                                  get_nir_src(instr->src[0]), /* Address */
47989f464c52Smaya                                  fs_reg(), /* No source data */
47999f464c52Smaya                                  brw_imm_ud(instr->num_components));
48009f464c52Smaya         inst->size_written = instr->num_components *
48019f464c52Smaya                              inst->dst.component_size(inst->exec_size);
480201e04c3fSmrg      } else {
48039f464c52Smaya         const unsigned bit_size = nir_dest_bit_size(instr->dest);
48049f464c52Smaya         assert(nir_dest_num_components(instr->dest) == 1);
48059f464c52Smaya         fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
48069f464c52Smaya         bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL,
48079f464c52Smaya                  tmp,
48089f464c52Smaya                  get_nir_src(instr->src[0]), /* Address */
48099f464c52Smaya                  fs_reg(), /* No source data */
48109f464c52Smaya                  brw_imm_ud(bit_size));
48117ec681f3Smrg         bld.MOV(dest, subscript(tmp, dest.type, 0));
481201e04c3fSmrg      }
481301e04c3fSmrg      break;
481401e04c3fSmrg   }
481501e04c3fSmrg
48169f464c52Smaya   case nir_intrinsic_store_global:
48177ec681f3Smrg      assert(devinfo->ver >= 8);
481801e04c3fSmrg
48197ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) <= 32);
48207ec681f3Smrg      assert(nir_intrinsic_write_mask(instr) ==
48217ec681f3Smrg             (1u << instr->num_components) - 1);
48227ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
48237ec681f3Smrg      if (nir_src_bit_size(instr->src[0]) == 32 &&
48247ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
48257ec681f3Smrg         assert(nir_src_num_components(instr->src[0]) <= 4);
48269f464c52Smaya         bld.emit(SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL,
48279f464c52Smaya                  fs_reg(),
48289f464c52Smaya                  get_nir_src(instr->src[1]), /* Address */
48299f464c52Smaya                  get_nir_src(instr->src[0]), /* Data */
48309f464c52Smaya                  brw_imm_ud(instr->num_components));
483101e04c3fSmrg      } else {
48329f464c52Smaya         assert(nir_src_num_components(instr->src[0]) == 1);
48337ec681f3Smrg         const unsigned bit_size = nir_src_bit_size(instr->src[0]);
48349f464c52Smaya         brw_reg_type data_type =
48359f464c52Smaya            brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
48369f464c52Smaya         fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
48379f464c52Smaya         bld.MOV(tmp, retype(get_nir_src(instr->src[0]), data_type));
48389f464c52Smaya         bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL,
48399f464c52Smaya                  fs_reg(),
48409f464c52Smaya                  get_nir_src(instr->src[1]), /* Address */
48419f464c52Smaya                  tmp, /* Data */
48429f464c52Smaya                  brw_imm_ud(nir_src_bit_size(instr->src[0])));
484301e04c3fSmrg      }
48449f464c52Smaya      break;
484501e04c3fSmrg
48469f464c52Smaya   case nir_intrinsic_global_atomic_add:
48479f464c52Smaya   case nir_intrinsic_global_atomic_imin:
48489f464c52Smaya   case nir_intrinsic_global_atomic_umin:
48499f464c52Smaya   case nir_intrinsic_global_atomic_imax:
48509f464c52Smaya   case nir_intrinsic_global_atomic_umax:
48519f464c52Smaya   case nir_intrinsic_global_atomic_and:
48529f464c52Smaya   case nir_intrinsic_global_atomic_or:
48539f464c52Smaya   case nir_intrinsic_global_atomic_xor:
48549f464c52Smaya   case nir_intrinsic_global_atomic_exchange:
48559f464c52Smaya   case nir_intrinsic_global_atomic_comp_swap:
48567ec681f3Smrg      nir_emit_global_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
48579f464c52Smaya      break;
48587ec681f3Smrg   case nir_intrinsic_global_atomic_fadd:
48599f464c52Smaya   case nir_intrinsic_global_atomic_fmin:
48609f464c52Smaya   case nir_intrinsic_global_atomic_fmax:
48619f464c52Smaya   case nir_intrinsic_global_atomic_fcomp_swap:
48627ec681f3Smrg      nir_emit_global_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
48637ec681f3Smrg      break;
48647ec681f3Smrg
48657ec681f3Smrg   case nir_intrinsic_load_global_const_block_intel: {
48667ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
48677ec681f3Smrg      assert(instr->num_components == 8 || instr->num_components == 16);
48687ec681f3Smrg
48697ec681f3Smrg      const fs_builder ubld = bld.exec_all().group(instr->num_components, 0);
48707ec681f3Smrg      fs_reg load_val;
48717ec681f3Smrg
48727ec681f3Smrg      bool is_pred_const = nir_src_is_const(instr->src[1]);
48737ec681f3Smrg      if (is_pred_const && nir_src_as_uint(instr->src[1]) == 0) {
48747ec681f3Smrg         /* In this case, we don't want the UBO load at all.  We really
48757ec681f3Smrg          * shouldn't get here but it's possible.
48767ec681f3Smrg          */
48777ec681f3Smrg         load_val = brw_imm_ud(0);
48787ec681f3Smrg      } else {
48797ec681f3Smrg         /* The uniform process may stomp the flag so do this first */
48807ec681f3Smrg         fs_reg addr = bld.emit_uniformize(get_nir_src(instr->src[0]));
48817ec681f3Smrg
48827ec681f3Smrg         load_val = ubld.vgrf(BRW_REGISTER_TYPE_UD);
48837ec681f3Smrg
48847ec681f3Smrg         /* If the predicate is constant and we got here, then it's non-zero
48857ec681f3Smrg          * and we don't need the predicate at all.
48867ec681f3Smrg          */
48877ec681f3Smrg         if (!is_pred_const) {
48887ec681f3Smrg            /* Load the predicate */
48897ec681f3Smrg            fs_reg pred = bld.emit_uniformize(get_nir_src(instr->src[1]));
48907ec681f3Smrg            fs_inst *mov = ubld.MOV(bld.null_reg_d(), pred);
48917ec681f3Smrg            mov->conditional_mod = BRW_CONDITIONAL_NZ;
48927ec681f3Smrg
48937ec681f3Smrg            /* Stomp the destination with 0 if we're OOB */
48947ec681f3Smrg            mov = ubld.MOV(load_val, brw_imm_ud(0));
48957ec681f3Smrg            mov->predicate = BRW_PREDICATE_NORMAL;
48967ec681f3Smrg            mov->predicate_inverse = true;
48977ec681f3Smrg         }
48987ec681f3Smrg
48997ec681f3Smrg         fs_inst *load = ubld.emit(SHADER_OPCODE_A64_OWORD_BLOCK_READ_LOGICAL,
49007ec681f3Smrg                                   load_val, addr,
49017ec681f3Smrg                                   fs_reg(), /* No source data */
49027ec681f3Smrg                                   brw_imm_ud(instr->num_components));
49037ec681f3Smrg
49047ec681f3Smrg         if (!is_pred_const)
49057ec681f3Smrg            load->predicate = BRW_PREDICATE_NORMAL;
49067ec681f3Smrg      }
49077ec681f3Smrg
49087ec681f3Smrg      /* From the HW perspective, we just did a single SIMD16 instruction
49097ec681f3Smrg       * which loaded a dword in each SIMD channel.  From NIR's perspective,
49107ec681f3Smrg       * this instruction returns a vec16.  Any users of this data in the
49117ec681f3Smrg       * back-end will expect a vec16 per SIMD channel so we have to emit a
49127ec681f3Smrg       * pile of MOVs to resolve this discrepancy.  Fortunately, copy-prop
49137ec681f3Smrg       * will generally clean them up for us.
49147ec681f3Smrg       */
49157ec681f3Smrg      for (unsigned i = 0; i < instr->num_components; i++) {
49167ec681f3Smrg         bld.MOV(retype(offset(dest, bld, i), BRW_REGISTER_TYPE_UD),
49177ec681f3Smrg                 component(load_val, i));
49187ec681f3Smrg      }
49199f464c52Smaya      break;
49207ec681f3Smrg   }
492101e04c3fSmrg
49229f464c52Smaya   case nir_intrinsic_load_ssbo: {
49237ec681f3Smrg      assert(devinfo->ver >= 7);
492401e04c3fSmrg
49259f464c52Smaya      const unsigned bit_size = nir_dest_bit_size(instr->dest);
49269f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
49279f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_SURFACE] =
49289f464c52Smaya         get_nir_ssbo_intrinsic_index(bld, instr);
49299f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
49309f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
49317ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
493201e04c3fSmrg
49339f464c52Smaya      /* Make dest unsigned because that's what the temporary will be */
49349f464c52Smaya      dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
493501e04c3fSmrg
49369f464c52Smaya      /* Read the vector */
49377ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) <= 32);
49387ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
49397ec681f3Smrg      if (nir_dest_bit_size(instr->dest) == 32 &&
49407ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
49417ec681f3Smrg         assert(nir_dest_num_components(instr->dest) <= 4);
49429f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
49439f464c52Smaya         fs_inst *inst =
49449f464c52Smaya            bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
49459f464c52Smaya                     dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
49469f464c52Smaya         inst->size_written = instr->num_components * dispatch_width * 4;
49479f464c52Smaya      } else {
49489f464c52Smaya         assert(nir_dest_num_components(instr->dest) == 1);
49499f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
495001e04c3fSmrg
49519f464c52Smaya         fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
49529f464c52Smaya         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
49539f464c52Smaya                  read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
49547ec681f3Smrg         bld.MOV(dest, subscript(read_result, dest.type, 0));
49559f464c52Smaya      }
49569f464c52Smaya      break;
49579f464c52Smaya   }
495801e04c3fSmrg
49599f464c52Smaya   case nir_intrinsic_store_ssbo: {
49607ec681f3Smrg      assert(devinfo->ver >= 7);
496101e04c3fSmrg
49629f464c52Smaya      const unsigned bit_size = nir_src_bit_size(instr->src[0]);
49639f464c52Smaya      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
49649f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_SURFACE] =
49659f464c52Smaya         get_nir_ssbo_intrinsic_index(bld, instr);
49669f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[2]);
49679f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
49687ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
49699f464c52Smaya
49709f464c52Smaya      fs_reg data = get_nir_src(instr->src[0]);
49719f464c52Smaya      data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
49729f464c52Smaya
49737ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) <= 32);
49749f464c52Smaya      assert(nir_intrinsic_write_mask(instr) ==
49759f464c52Smaya             (1u << instr->num_components) - 1);
49767ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
49777ec681f3Smrg      if (nir_src_bit_size(instr->src[0]) == 32 &&
49787ec681f3Smrg          nir_intrinsic_align(instr) >= 4) {
49799f464c52Smaya         assert(nir_src_num_components(instr->src[0]) <= 4);
49809f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = data;
49819f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
49829f464c52Smaya         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
49839f464c52Smaya                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
49849f464c52Smaya      } else {
49859f464c52Smaya         assert(nir_src_num_components(instr->src[0]) == 1);
49869f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
498701e04c3fSmrg
49889f464c52Smaya         srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
49899f464c52Smaya         bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
499001e04c3fSmrg
49919f464c52Smaya         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
49929f464c52Smaya                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
499301e04c3fSmrg      }
499401e04c3fSmrg      break;
499501e04c3fSmrg   }
499601e04c3fSmrg
499701e04c3fSmrg   case nir_intrinsic_store_output: {
49987ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) == 32);
499901e04c3fSmrg      fs_reg src = get_nir_src(instr->src[0]);
500001e04c3fSmrg
50019f464c52Smaya      unsigned store_offset = nir_src_as_uint(instr->src[1]);
500201e04c3fSmrg      unsigned num_components = instr->num_components;
500301e04c3fSmrg      unsigned first_component = nir_intrinsic_component(instr);
500401e04c3fSmrg
500501e04c3fSmrg      fs_reg new_dest = retype(offset(outputs[instr->const_index[0]], bld,
50069f464c52Smaya                                      4 * store_offset), src.type);
500701e04c3fSmrg      for (unsigned j = 0; j < num_components; j++) {
500801e04c3fSmrg         bld.MOV(offset(new_dest, bld, j + first_component),
500901e04c3fSmrg                 offset(src, bld, j));
501001e04c3fSmrg      }
501101e04c3fSmrg      break;
501201e04c3fSmrg   }
501301e04c3fSmrg
501401e04c3fSmrg   case nir_intrinsic_ssbo_atomic_add:
501501e04c3fSmrg   case nir_intrinsic_ssbo_atomic_imin:
501601e04c3fSmrg   case nir_intrinsic_ssbo_atomic_umin:
501701e04c3fSmrg   case nir_intrinsic_ssbo_atomic_imax:
501801e04c3fSmrg   case nir_intrinsic_ssbo_atomic_umax:
501901e04c3fSmrg   case nir_intrinsic_ssbo_atomic_and:
502001e04c3fSmrg   case nir_intrinsic_ssbo_atomic_or:
502101e04c3fSmrg   case nir_intrinsic_ssbo_atomic_xor:
502201e04c3fSmrg   case nir_intrinsic_ssbo_atomic_exchange:
502301e04c3fSmrg   case nir_intrinsic_ssbo_atomic_comp_swap:
50247ec681f3Smrg      nir_emit_ssbo_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
502501e04c3fSmrg      break;
50267ec681f3Smrg   case nir_intrinsic_ssbo_atomic_fadd:
502701e04c3fSmrg   case nir_intrinsic_ssbo_atomic_fmin:
502801e04c3fSmrg   case nir_intrinsic_ssbo_atomic_fmax:
502901e04c3fSmrg   case nir_intrinsic_ssbo_atomic_fcomp_swap:
50307ec681f3Smrg      nir_emit_ssbo_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
503101e04c3fSmrg      break;
503201e04c3fSmrg
50337ec681f3Smrg   case nir_intrinsic_get_ssbo_size: {
50349f464c52Smaya      assert(nir_src_num_components(instr->src[0]) == 1);
50359f464c52Smaya      unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
50369f464c52Smaya                            nir_src_as_uint(instr->src[0]) : 0;
503701e04c3fSmrg
503801e04c3fSmrg      /* A resinfo's sampler message is used to get the buffer size.  The
503901e04c3fSmrg       * SIMD8's writeback message consists of four registers and SIMD16's
504001e04c3fSmrg       * writeback message consists of 8 destination registers (two per each
504101e04c3fSmrg       * component).  Because we are only interested on the first channel of
504201e04c3fSmrg       * the first returned component, where resinfo returns the buffer size
504301e04c3fSmrg       * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
504401e04c3fSmrg       * the dispatch width.
504501e04c3fSmrg       */
504601e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(8, 0);
504701e04c3fSmrg      fs_reg src_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD);
504801e04c3fSmrg      fs_reg ret_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
504901e04c3fSmrg
505001e04c3fSmrg      /* Set LOD = 0 */
505101e04c3fSmrg      ubld.MOV(src_payload, brw_imm_d(0));
505201e04c3fSmrg
505301e04c3fSmrg      const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
505401e04c3fSmrg      fs_inst *inst = ubld.emit(SHADER_OPCODE_GET_BUFFER_SIZE, ret_payload,
505501e04c3fSmrg                                src_payload, brw_imm_ud(index));
505601e04c3fSmrg      inst->header_size = 0;
505701e04c3fSmrg      inst->mlen = 1;
505801e04c3fSmrg      inst->size_written = 4 * REG_SIZE;
505901e04c3fSmrg
506001e04c3fSmrg      /* SKL PRM, vol07, 3D Media GPGPU Engine, Bounds Checking and Faulting:
506101e04c3fSmrg       *
506201e04c3fSmrg       * "Out-of-bounds checking is always performed at a DWord granularity. If
506301e04c3fSmrg       * any part of the DWord is out-of-bounds then the whole DWord is
506401e04c3fSmrg       * considered out-of-bounds."
506501e04c3fSmrg       *
506601e04c3fSmrg       * This implies that types with size smaller than 4-bytes need to be
506701e04c3fSmrg       * padded if they don't complete the last dword of the buffer. But as we
506801e04c3fSmrg       * need to maintain the original size we need to reverse the padding
506901e04c3fSmrg       * calculation to return the correct size to know the number of elements
507001e04c3fSmrg       * of an unsized array. As we stored in the last two bits of the surface
507101e04c3fSmrg       * size the needed padding for the buffer, we calculate here the
507201e04c3fSmrg       * original buffer_size reversing the surface_size calculation:
507301e04c3fSmrg       *
507401e04c3fSmrg       * surface_size = isl_align(buffer_size, 4) +
507501e04c3fSmrg       *                (isl_align(buffer_size) - buffer_size)
507601e04c3fSmrg       *
507701e04c3fSmrg       * buffer_size = surface_size & ~3 - surface_size & 3
507801e04c3fSmrg       */
507901e04c3fSmrg
508001e04c3fSmrg      fs_reg size_aligned4 = ubld.vgrf(BRW_REGISTER_TYPE_UD);
508101e04c3fSmrg      fs_reg size_padding = ubld.vgrf(BRW_REGISTER_TYPE_UD);
508201e04c3fSmrg      fs_reg buffer_size = ubld.vgrf(BRW_REGISTER_TYPE_UD);
508301e04c3fSmrg
508401e04c3fSmrg      ubld.AND(size_padding, ret_payload, brw_imm_ud(3));
508501e04c3fSmrg      ubld.AND(size_aligned4, ret_payload, brw_imm_ud(~3));
508601e04c3fSmrg      ubld.ADD(buffer_size, size_aligned4, negate(size_padding));
508701e04c3fSmrg
508801e04c3fSmrg      bld.MOV(retype(dest, ret_payload.type), component(buffer_size, 0));
508901e04c3fSmrg      break;
509001e04c3fSmrg   }
509101e04c3fSmrg
50927ec681f3Smrg   case nir_intrinsic_load_scratch: {
50937ec681f3Smrg      assert(devinfo->ver >= 7);
50947ec681f3Smrg
50957ec681f3Smrg      assert(nir_dest_num_components(instr->dest) == 1);
50967ec681f3Smrg      const unsigned bit_size = nir_dest_bit_size(instr->dest);
50977ec681f3Smrg      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
50987ec681f3Smrg
50997ec681f3Smrg      if (devinfo->verx10 >= 125) {
51007ec681f3Smrg         const fs_builder ubld = bld.exec_all().group(1, 0);
51017ec681f3Smrg         fs_reg handle = component(ubld.vgrf(BRW_REGISTER_TYPE_UD), 0);
51027ec681f3Smrg         ubld.AND(handle, retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
51037ec681f3Smrg                          brw_imm_ud(~0x3ffu));
51047ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE_HANDLE] = handle;
51057ec681f3Smrg      } else if (devinfo->ver >= 8) {
51067ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE] =
51077ec681f3Smrg            brw_imm_ud(GFX8_BTI_STATELESS_NON_COHERENT);
51087ec681f3Smrg      } else {
51097ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(BRW_BTI_STATELESS);
51107ec681f3Smrg      }
51117ec681f3Smrg
51127ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
51137ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
51147ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
51157ec681f3Smrg      const fs_reg nir_addr = get_nir_src(instr->src[0]);
51167ec681f3Smrg
51177ec681f3Smrg      /* Make dest unsigned because that's what the temporary will be */
51187ec681f3Smrg      dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
51197ec681f3Smrg
51207ec681f3Smrg      /* Read the vector */
51217ec681f3Smrg      assert(nir_dest_num_components(instr->dest) == 1);
51227ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) <= 32);
51237ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
51247ec681f3Smrg      if (devinfo->verx10 >= 125) {
51257ec681f3Smrg         assert(nir_dest_bit_size(instr->dest) == 32 &&
51267ec681f3Smrg                nir_intrinsic_align(instr) >= 4);
51277ec681f3Smrg
51287ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
51297ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, false);
51307ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(1);
51317ec681f3Smrg
51327ec681f3Smrg         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
51337ec681f3Smrg                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
51347ec681f3Smrg      } else if (nir_dest_bit_size(instr->dest) >= 4 &&
51357ec681f3Smrg                 nir_intrinsic_align(instr) >= 4) {
51367ec681f3Smrg         /* The offset for a DWORD scattered message is in dwords. */
51377ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
51387ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, true);
51397ec681f3Smrg
51407ec681f3Smrg         bld.emit(SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL,
51417ec681f3Smrg                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
51427ec681f3Smrg      } else {
51437ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
51447ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, false);
51457ec681f3Smrg
51467ec681f3Smrg         fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
51477ec681f3Smrg         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
51487ec681f3Smrg                  read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
51497ec681f3Smrg         bld.MOV(dest, read_result);
51507ec681f3Smrg      }
51517ec681f3Smrg      break;
51527ec681f3Smrg   }
51537ec681f3Smrg
51547ec681f3Smrg   case nir_intrinsic_store_scratch: {
51557ec681f3Smrg      assert(devinfo->ver >= 7);
51567ec681f3Smrg
51577ec681f3Smrg      assert(nir_src_num_components(instr->src[0]) == 1);
51587ec681f3Smrg      const unsigned bit_size = nir_src_bit_size(instr->src[0]);
51597ec681f3Smrg      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
51607ec681f3Smrg
51617ec681f3Smrg      if (devinfo->verx10 >= 125) {
51627ec681f3Smrg         const fs_builder ubld = bld.exec_all().group(1, 0);
51637ec681f3Smrg         fs_reg handle = component(ubld.vgrf(BRW_REGISTER_TYPE_UD), 0);
51647ec681f3Smrg         ubld.AND(handle, retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
51657ec681f3Smrg                          brw_imm_ud(~0x3ffu));
51667ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE_HANDLE] = handle;
51677ec681f3Smrg      } else if (devinfo->ver >= 8) {
51687ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE] =
51697ec681f3Smrg            brw_imm_ud(GFX8_BTI_STATELESS_NON_COHERENT);
51707ec681f3Smrg      } else {
51717ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(BRW_BTI_STATELESS);
51727ec681f3Smrg      }
51737ec681f3Smrg
51747ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
51757ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
51767ec681f3Smrg      /**
51777ec681f3Smrg       * While this instruction has side-effects, it should not be predicated
51787ec681f3Smrg       * on sample mask, because otherwise fs helper invocations would
51797ec681f3Smrg       * load undefined values from scratch memory. And scratch memory
51807ec681f3Smrg       * load-stores are produced from operations without side-effects, thus
51817ec681f3Smrg       * they should not have different behaviour in the helper invocations.
51827ec681f3Smrg       */
51837ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(0);
51847ec681f3Smrg      const fs_reg nir_addr = get_nir_src(instr->src[1]);
51857ec681f3Smrg
51867ec681f3Smrg      fs_reg data = get_nir_src(instr->src[0]);
51877ec681f3Smrg      data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
51887ec681f3Smrg
51897ec681f3Smrg      assert(nir_src_num_components(instr->src[0]) == 1);
51907ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) <= 32);
51917ec681f3Smrg      assert(nir_intrinsic_write_mask(instr) == 1);
51927ec681f3Smrg      assert(nir_intrinsic_align(instr) > 0);
51937ec681f3Smrg      if (devinfo->verx10 >= 125) {
51947ec681f3Smrg         assert(nir_src_bit_size(instr->src[0]) == 32 &&
51957ec681f3Smrg                nir_intrinsic_align(instr) >= 4);
51967ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_DATA] = data;
51977ec681f3Smrg
51987ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
51997ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, false);
52007ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(1);
52017ec681f3Smrg
52027ec681f3Smrg         bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
52037ec681f3Smrg                  dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
52047ec681f3Smrg      } else if (nir_src_bit_size(instr->src[0]) == 32 &&
52057ec681f3Smrg                 nir_intrinsic_align(instr) >= 4) {
52067ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_DATA] = data;
52077ec681f3Smrg
52087ec681f3Smrg         /* The offset for a DWORD scattered message is in dwords. */
52097ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
52107ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, true);
52117ec681f3Smrg
52127ec681f3Smrg         bld.emit(SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL,
52137ec681f3Smrg                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
52147ec681f3Smrg      } else {
52157ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
52167ec681f3Smrg         bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
52177ec681f3Smrg
52187ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
52197ec681f3Smrg            swizzle_nir_scratch_addr(bld, nir_addr, false);
52207ec681f3Smrg
52217ec681f3Smrg         bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
52227ec681f3Smrg                  fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
52237ec681f3Smrg      }
52247ec681f3Smrg      break;
52257ec681f3Smrg   }
52267ec681f3Smrg
52277ec681f3Smrg   case nir_intrinsic_load_subgroup_size:
52287ec681f3Smrg      /* This should only happen for fragment shaders because every other case
52297ec681f3Smrg       * is lowered in NIR so we can optimize on it.
52307ec681f3Smrg       */
52317ec681f3Smrg      assert(stage == MESA_SHADER_FRAGMENT);
52327ec681f3Smrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(dispatch_width));
52337ec681f3Smrg      break;
52347ec681f3Smrg
523501e04c3fSmrg   case nir_intrinsic_load_subgroup_invocation:
523601e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
523701e04c3fSmrg              nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION]);
523801e04c3fSmrg      break;
523901e04c3fSmrg
524001e04c3fSmrg   case nir_intrinsic_load_subgroup_eq_mask:
524101e04c3fSmrg   case nir_intrinsic_load_subgroup_ge_mask:
524201e04c3fSmrg   case nir_intrinsic_load_subgroup_gt_mask:
524301e04c3fSmrg   case nir_intrinsic_load_subgroup_le_mask:
524401e04c3fSmrg   case nir_intrinsic_load_subgroup_lt_mask:
524501e04c3fSmrg      unreachable("not reached");
524601e04c3fSmrg
524701e04c3fSmrg   case nir_intrinsic_vote_any: {
524801e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(1, 0);
524901e04c3fSmrg
525001e04c3fSmrg      /* The any/all predicates do not consider channel enables. To prevent
525101e04c3fSmrg       * dead channels from affecting the result, we initialize the flag with
525201e04c3fSmrg       * with the identity value for the logical operation.
525301e04c3fSmrg       */
525401e04c3fSmrg      if (dispatch_width == 32) {
525501e04c3fSmrg         /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
525601e04c3fSmrg         ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
525701e04c3fSmrg                         brw_imm_ud(0));
525801e04c3fSmrg      } else {
525901e04c3fSmrg         ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
526001e04c3fSmrg      }
526101e04c3fSmrg      bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
526201e04c3fSmrg
526301e04c3fSmrg      /* For some reason, the any/all predicates don't work properly with
526401e04c3fSmrg       * SIMD32.  In particular, it appears that a SEL with a QtrCtrl of 2H
526501e04c3fSmrg       * doesn't read the correct subset of the flag register and you end up
526601e04c3fSmrg       * getting garbage in the second half.  Work around this by using a pair
526701e04c3fSmrg       * of 1-wide MOVs and scattering the result.
526801e04c3fSmrg       */
526901e04c3fSmrg      fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
527001e04c3fSmrg      ubld.MOV(res1, brw_imm_d(0));
527101e04c3fSmrg      set_predicate(dispatch_width == 8  ? BRW_PREDICATE_ALIGN1_ANY8H :
527201e04c3fSmrg                    dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ANY16H :
527301e04c3fSmrg                                           BRW_PREDICATE_ALIGN1_ANY32H,
527401e04c3fSmrg                    ubld.MOV(res1, brw_imm_d(-1)));
527501e04c3fSmrg
527601e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
527701e04c3fSmrg      break;
527801e04c3fSmrg   }
527901e04c3fSmrg   case nir_intrinsic_vote_all: {
528001e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(1, 0);
528101e04c3fSmrg
528201e04c3fSmrg      /* The any/all predicates do not consider channel enables. To prevent
528301e04c3fSmrg       * dead channels from affecting the result, we initialize the flag with
528401e04c3fSmrg       * with the identity value for the logical operation.
528501e04c3fSmrg       */
528601e04c3fSmrg      if (dispatch_width == 32) {
528701e04c3fSmrg         /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
528801e04c3fSmrg         ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
528901e04c3fSmrg                         brw_imm_ud(0xffffffff));
529001e04c3fSmrg      } else {
529101e04c3fSmrg         ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
529201e04c3fSmrg      }
529301e04c3fSmrg      bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
529401e04c3fSmrg
529501e04c3fSmrg      /* For some reason, the any/all predicates don't work properly with
529601e04c3fSmrg       * SIMD32.  In particular, it appears that a SEL with a QtrCtrl of 2H
529701e04c3fSmrg       * doesn't read the correct subset of the flag register and you end up
529801e04c3fSmrg       * getting garbage in the second half.  Work around this by using a pair
529901e04c3fSmrg       * of 1-wide MOVs and scattering the result.
530001e04c3fSmrg       */
530101e04c3fSmrg      fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
530201e04c3fSmrg      ubld.MOV(res1, brw_imm_d(0));
530301e04c3fSmrg      set_predicate(dispatch_width == 8  ? BRW_PREDICATE_ALIGN1_ALL8H :
530401e04c3fSmrg                    dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
530501e04c3fSmrg                                           BRW_PREDICATE_ALIGN1_ALL32H,
530601e04c3fSmrg                    ubld.MOV(res1, brw_imm_d(-1)));
530701e04c3fSmrg
530801e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
530901e04c3fSmrg      break;
531001e04c3fSmrg   }
531101e04c3fSmrg   case nir_intrinsic_vote_feq:
531201e04c3fSmrg   case nir_intrinsic_vote_ieq: {
531301e04c3fSmrg      fs_reg value = get_nir_src(instr->src[0]);
531401e04c3fSmrg      if (instr->intrinsic == nir_intrinsic_vote_feq) {
531501e04c3fSmrg         const unsigned bit_size = nir_src_bit_size(instr->src[0]);
53169f464c52Smaya         value.type = bit_size == 8 ? BRW_REGISTER_TYPE_B :
53179f464c52Smaya            brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_F);
531801e04c3fSmrg      }
531901e04c3fSmrg
532001e04c3fSmrg      fs_reg uniformized = bld.emit_uniformize(value);
532101e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(1, 0);
532201e04c3fSmrg
532301e04c3fSmrg      /* The any/all predicates do not consider channel enables. To prevent
532401e04c3fSmrg       * dead channels from affecting the result, we initialize the flag with
532501e04c3fSmrg       * with the identity value for the logical operation.
532601e04c3fSmrg       */
532701e04c3fSmrg      if (dispatch_width == 32) {
532801e04c3fSmrg         /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
532901e04c3fSmrg         ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
533001e04c3fSmrg                         brw_imm_ud(0xffffffff));
533101e04c3fSmrg      } else {
533201e04c3fSmrg         ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
533301e04c3fSmrg      }
533401e04c3fSmrg      bld.CMP(bld.null_reg_d(), value, uniformized, BRW_CONDITIONAL_Z);
533501e04c3fSmrg
533601e04c3fSmrg      /* For some reason, the any/all predicates don't work properly with
533701e04c3fSmrg       * SIMD32.  In particular, it appears that a SEL with a QtrCtrl of 2H
533801e04c3fSmrg       * doesn't read the correct subset of the flag register and you end up
533901e04c3fSmrg       * getting garbage in the second half.  Work around this by using a pair
534001e04c3fSmrg       * of 1-wide MOVs and scattering the result.
534101e04c3fSmrg       */
534201e04c3fSmrg      fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
534301e04c3fSmrg      ubld.MOV(res1, brw_imm_d(0));
534401e04c3fSmrg      set_predicate(dispatch_width == 8  ? BRW_PREDICATE_ALIGN1_ALL8H :
534501e04c3fSmrg                    dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
534601e04c3fSmrg                                           BRW_PREDICATE_ALIGN1_ALL32H,
534701e04c3fSmrg                    ubld.MOV(res1, brw_imm_d(-1)));
534801e04c3fSmrg
534901e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
535001e04c3fSmrg      break;
535101e04c3fSmrg   }
535201e04c3fSmrg
535301e04c3fSmrg   case nir_intrinsic_ballot: {
535401e04c3fSmrg      const fs_reg value = retype(get_nir_src(instr->src[0]),
535501e04c3fSmrg                                  BRW_REGISTER_TYPE_UD);
535601e04c3fSmrg      struct brw_reg flag = brw_flag_reg(0, 0);
535701e04c3fSmrg      /* FIXME: For SIMD32 programs, this causes us to stomp on f0.1 as well
535801e04c3fSmrg       * as f0.0.  This is a problem for fragment programs as we currently use
535901e04c3fSmrg       * f0.1 for discards.  Fortunately, we don't support SIMD32 fragment
536001e04c3fSmrg       * programs yet so this isn't a problem.  When we do, something will
536101e04c3fSmrg       * have to change.
536201e04c3fSmrg       */
536301e04c3fSmrg      if (dispatch_width == 32)
536401e04c3fSmrg         flag.type = BRW_REGISTER_TYPE_UD;
536501e04c3fSmrg
536601e04c3fSmrg      bld.exec_all().group(1, 0).MOV(flag, brw_imm_ud(0u));
536701e04c3fSmrg      bld.CMP(bld.null_reg_ud(), value, brw_imm_ud(0u), BRW_CONDITIONAL_NZ);
536801e04c3fSmrg
536901e04c3fSmrg      if (instr->dest.ssa.bit_size > 32) {
537001e04c3fSmrg         dest.type = BRW_REGISTER_TYPE_UQ;
537101e04c3fSmrg      } else {
537201e04c3fSmrg         dest.type = BRW_REGISTER_TYPE_UD;
537301e04c3fSmrg      }
537401e04c3fSmrg      bld.MOV(dest, flag);
537501e04c3fSmrg      break;
537601e04c3fSmrg   }
537701e04c3fSmrg
537801e04c3fSmrg   case nir_intrinsic_read_invocation: {
537901e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
538001e04c3fSmrg      const fs_reg invocation = get_nir_src(instr->src[1]);
538101e04c3fSmrg      fs_reg tmp = bld.vgrf(value.type);
538201e04c3fSmrg
538301e04c3fSmrg      bld.exec_all().emit(SHADER_OPCODE_BROADCAST, tmp, value,
538401e04c3fSmrg                          bld.emit_uniformize(invocation));
538501e04c3fSmrg
538601e04c3fSmrg      bld.MOV(retype(dest, value.type), fs_reg(component(tmp, 0)));
538701e04c3fSmrg      break;
538801e04c3fSmrg   }
538901e04c3fSmrg
539001e04c3fSmrg   case nir_intrinsic_read_first_invocation: {
539101e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
539201e04c3fSmrg      bld.MOV(retype(dest, value.type), bld.emit_uniformize(value));
539301e04c3fSmrg      break;
539401e04c3fSmrg   }
539501e04c3fSmrg
539601e04c3fSmrg   case nir_intrinsic_shuffle: {
539701e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
539801e04c3fSmrg      const fs_reg index = get_nir_src(instr->src[1]);
539901e04c3fSmrg
540001e04c3fSmrg      bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, index);
540101e04c3fSmrg      break;
540201e04c3fSmrg   }
540301e04c3fSmrg
540401e04c3fSmrg   case nir_intrinsic_first_invocation: {
540501e04c3fSmrg      fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
540601e04c3fSmrg      bld.exec_all().emit(SHADER_OPCODE_FIND_LIVE_CHANNEL, tmp);
540701e04c3fSmrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
540801e04c3fSmrg              fs_reg(component(tmp, 0)));
540901e04c3fSmrg      break;
541001e04c3fSmrg   }
541101e04c3fSmrg
541201e04c3fSmrg   case nir_intrinsic_quad_broadcast: {
541301e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
54149f464c52Smaya      const unsigned index = nir_src_as_uint(instr->src[1]);
541501e04c3fSmrg
541601e04c3fSmrg      bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, retype(dest, value.type),
54179f464c52Smaya               value, brw_imm_ud(index), brw_imm_ud(4));
541801e04c3fSmrg      break;
541901e04c3fSmrg   }
542001e04c3fSmrg
542101e04c3fSmrg   case nir_intrinsic_quad_swap_horizontal: {
542201e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
542301e04c3fSmrg      const fs_reg tmp = bld.vgrf(value.type);
54247ec681f3Smrg      if (devinfo->ver <= 7) {
54259f464c52Smaya         /* The hardware doesn't seem to support these crazy regions with
54267ec681f3Smrg          * compressed instructions on gfx7 and earlier so we fall back to
54279f464c52Smaya          * using quad swizzles.  Fortunately, we don't support 64-bit
54287ec681f3Smrg          * anything in Vulkan on gfx7.
54299f464c52Smaya          */
54309f464c52Smaya         assert(nir_src_bit_size(instr->src[0]) == 32);
54319f464c52Smaya         const fs_builder ubld = bld.exec_all();
54329f464c52Smaya         ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
54339f464c52Smaya                   brw_imm_ud(BRW_SWIZZLE4(1,0,3,2)));
54349f464c52Smaya         bld.MOV(retype(dest, value.type), tmp);
54359f464c52Smaya      } else {
54369f464c52Smaya         const fs_builder ubld = bld.exec_all().group(dispatch_width / 2, 0);
543701e04c3fSmrg
54389f464c52Smaya         const fs_reg src_left = horiz_stride(value, 2);
54399f464c52Smaya         const fs_reg src_right = horiz_stride(horiz_offset(value, 1), 2);
54409f464c52Smaya         const fs_reg tmp_left = horiz_stride(tmp, 2);
54419f464c52Smaya         const fs_reg tmp_right = horiz_stride(horiz_offset(tmp, 1), 2);
544201e04c3fSmrg
544301e04c3fSmrg         ubld.MOV(tmp_left, src_right);
544401e04c3fSmrg         ubld.MOV(tmp_right, src_left);
54459f464c52Smaya
544601e04c3fSmrg      }
544701e04c3fSmrg      bld.MOV(retype(dest, value.type), tmp);
544801e04c3fSmrg      break;
544901e04c3fSmrg   }
545001e04c3fSmrg
545101e04c3fSmrg   case nir_intrinsic_quad_swap_vertical: {
545201e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
545301e04c3fSmrg      if (nir_src_bit_size(instr->src[0]) == 32) {
545401e04c3fSmrg         /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
545501e04c3fSmrg         const fs_reg tmp = bld.vgrf(value.type);
545601e04c3fSmrg         const fs_builder ubld = bld.exec_all();
545701e04c3fSmrg         ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
545801e04c3fSmrg                   brw_imm_ud(BRW_SWIZZLE4(2,3,0,1)));
545901e04c3fSmrg         bld.MOV(retype(dest, value.type), tmp);
546001e04c3fSmrg      } else {
546101e04c3fSmrg         /* For larger data types, we have to either emit dispatch_width many
546201e04c3fSmrg          * MOVs or else fall back to doing indirects.
546301e04c3fSmrg          */
546401e04c3fSmrg         fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
546501e04c3fSmrg         bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
546601e04c3fSmrg                      brw_imm_w(0x2));
546701e04c3fSmrg         bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
546801e04c3fSmrg      }
546901e04c3fSmrg      break;
547001e04c3fSmrg   }
547101e04c3fSmrg
547201e04c3fSmrg   case nir_intrinsic_quad_swap_diagonal: {
547301e04c3fSmrg      const fs_reg value = get_nir_src(instr->src[0]);
547401e04c3fSmrg      if (nir_src_bit_size(instr->src[0]) == 32) {
547501e04c3fSmrg         /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
547601e04c3fSmrg         const fs_reg tmp = bld.vgrf(value.type);
547701e04c3fSmrg         const fs_builder ubld = bld.exec_all();
547801e04c3fSmrg         ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
547901e04c3fSmrg                   brw_imm_ud(BRW_SWIZZLE4(3,2,1,0)));
548001e04c3fSmrg         bld.MOV(retype(dest, value.type), tmp);
548101e04c3fSmrg      } else {
548201e04c3fSmrg         /* For larger data types, we have to either emit dispatch_width many
548301e04c3fSmrg          * MOVs or else fall back to doing indirects.
548401e04c3fSmrg          */
548501e04c3fSmrg         fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
548601e04c3fSmrg         bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
548701e04c3fSmrg                      brw_imm_w(0x3));
548801e04c3fSmrg         bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
548901e04c3fSmrg      }
549001e04c3fSmrg      break;
549101e04c3fSmrg   }
549201e04c3fSmrg
549301e04c3fSmrg   case nir_intrinsic_reduce: {
549401e04c3fSmrg      fs_reg src = get_nir_src(instr->src[0]);
549501e04c3fSmrg      nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
549601e04c3fSmrg      unsigned cluster_size = nir_intrinsic_cluster_size(instr);
549701e04c3fSmrg      if (cluster_size == 0 || cluster_size > dispatch_width)
549801e04c3fSmrg         cluster_size = dispatch_width;
549901e04c3fSmrg
550001e04c3fSmrg      /* Figure out the source type */
550101e04c3fSmrg      src.type = brw_type_for_nir_type(devinfo,
550201e04c3fSmrg         (nir_alu_type)(nir_op_infos[redop].input_types[0] |
550301e04c3fSmrg                        nir_src_bit_size(instr->src[0])));
550401e04c3fSmrg
550501e04c3fSmrg      fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
550601e04c3fSmrg      opcode brw_op = brw_op_for_nir_reduction_op(redop);
550701e04c3fSmrg      brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
550801e04c3fSmrg
550901e04c3fSmrg      /* Set up a register for all of our scratching around and initialize it
551001e04c3fSmrg       * to reduction operation's identity value.
551101e04c3fSmrg       */
551201e04c3fSmrg      fs_reg scan = bld.vgrf(src.type);
551301e04c3fSmrg      bld.exec_all().emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
551401e04c3fSmrg
551501e04c3fSmrg      bld.emit_scan(brw_op, scan, cluster_size, cond_mod);
551601e04c3fSmrg
551701e04c3fSmrg      dest.type = src.type;
551801e04c3fSmrg      if (cluster_size * type_sz(src.type) >= REG_SIZE * 2) {
551901e04c3fSmrg         /* In this case, CLUSTER_BROADCAST instruction isn't needed because
552001e04c3fSmrg          * the distance between clusters is at least 2 GRFs.  In this case,
552101e04c3fSmrg          * we don't need the weird striding of the CLUSTER_BROADCAST
552201e04c3fSmrg          * instruction and can just do regular MOVs.
552301e04c3fSmrg          */
552401e04c3fSmrg         assert((cluster_size * type_sz(src.type)) % (REG_SIZE * 2) == 0);
552501e04c3fSmrg         const unsigned groups =
552601e04c3fSmrg            (dispatch_width * type_sz(src.type)) / (REG_SIZE * 2);
552701e04c3fSmrg         const unsigned group_size = dispatch_width / groups;
552801e04c3fSmrg         for (unsigned i = 0; i < groups; i++) {
552901e04c3fSmrg            const unsigned cluster = (i * group_size) / cluster_size;
553001e04c3fSmrg            const unsigned comp = cluster * cluster_size + (cluster_size - 1);
553101e04c3fSmrg            bld.group(group_size, i).MOV(horiz_offset(dest, i * group_size),
553201e04c3fSmrg                                         component(scan, comp));
553301e04c3fSmrg         }
553401e04c3fSmrg      } else {
553501e04c3fSmrg         bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, dest, scan,
553601e04c3fSmrg                  brw_imm_ud(cluster_size - 1), brw_imm_ud(cluster_size));
553701e04c3fSmrg      }
553801e04c3fSmrg      break;
553901e04c3fSmrg   }
554001e04c3fSmrg
554101e04c3fSmrg   case nir_intrinsic_inclusive_scan:
554201e04c3fSmrg   case nir_intrinsic_exclusive_scan: {
554301e04c3fSmrg      fs_reg src = get_nir_src(instr->src[0]);
554401e04c3fSmrg      nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
554501e04c3fSmrg
554601e04c3fSmrg      /* Figure out the source type */
554701e04c3fSmrg      src.type = brw_type_for_nir_type(devinfo,
554801e04c3fSmrg         (nir_alu_type)(nir_op_infos[redop].input_types[0] |
554901e04c3fSmrg                        nir_src_bit_size(instr->src[0])));
555001e04c3fSmrg
555101e04c3fSmrg      fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
555201e04c3fSmrg      opcode brw_op = brw_op_for_nir_reduction_op(redop);
555301e04c3fSmrg      brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
555401e04c3fSmrg
555501e04c3fSmrg      /* Set up a register for all of our scratching around and initialize it
555601e04c3fSmrg       * to reduction operation's identity value.
555701e04c3fSmrg       */
555801e04c3fSmrg      fs_reg scan = bld.vgrf(src.type);
555901e04c3fSmrg      const fs_builder allbld = bld.exec_all();
556001e04c3fSmrg      allbld.emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
556101e04c3fSmrg
556201e04c3fSmrg      if (instr->intrinsic == nir_intrinsic_exclusive_scan) {
556301e04c3fSmrg         /* Exclusive scan is a bit harder because we have to do an annoying
556401e04c3fSmrg          * shift of the contents before we can begin.  To make things worse,
556501e04c3fSmrg          * we can't do this with a normal stride; we have to use indirects.
556601e04c3fSmrg          */
556701e04c3fSmrg         fs_reg shifted = bld.vgrf(src.type);
556801e04c3fSmrg         fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
556901e04c3fSmrg         allbld.ADD(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
557001e04c3fSmrg                         brw_imm_w(-1));
557101e04c3fSmrg         allbld.emit(SHADER_OPCODE_SHUFFLE, shifted, scan, idx);
557201e04c3fSmrg         allbld.group(1, 0).MOV(component(shifted, 0), identity);
557301e04c3fSmrg         scan = shifted;
557401e04c3fSmrg      }
557501e04c3fSmrg
557601e04c3fSmrg      bld.emit_scan(brw_op, scan, dispatch_width, cond_mod);
557701e04c3fSmrg
557801e04c3fSmrg      bld.MOV(retype(dest, src.type), scan);
557901e04c3fSmrg      break;
558001e04c3fSmrg   }
558101e04c3fSmrg
55827ec681f3Smrg   case nir_intrinsic_load_global_block_intel: {
55837ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
55847ec681f3Smrg
55857ec681f3Smrg      fs_reg address = bld.emit_uniformize(get_nir_src(instr->src[0]));
558601e04c3fSmrg
55877ec681f3Smrg      const fs_builder ubld1 = bld.exec_all().group(1, 0);
55887ec681f3Smrg      const fs_builder ubld8 = bld.exec_all().group(8, 0);
55897ec681f3Smrg      const fs_builder ubld16 = bld.exec_all().group(16, 0);
55907ec681f3Smrg
55917ec681f3Smrg      const unsigned total = instr->num_components * dispatch_width;
55927ec681f3Smrg      unsigned loaded = 0;
55937ec681f3Smrg
55947ec681f3Smrg      while (loaded < total) {
55957ec681f3Smrg         const unsigned block =
55967ec681f3Smrg            choose_oword_block_size_dwords(total - loaded);
55977ec681f3Smrg         const unsigned block_bytes = block * 4;
55987ec681f3Smrg
55997ec681f3Smrg         const fs_builder &ubld = block == 8 ? ubld8 : ubld16;
56007ec681f3Smrg         ubld.emit(SHADER_OPCODE_A64_UNALIGNED_OWORD_BLOCK_READ_LOGICAL,
56017ec681f3Smrg                   retype(byte_offset(dest, loaded * 4), BRW_REGISTER_TYPE_UD),
56027ec681f3Smrg                   address,
56037ec681f3Smrg                   fs_reg(), /* No source data */
56047ec681f3Smrg                   brw_imm_ud(block))->size_written = block_bytes;
56057ec681f3Smrg
56067ec681f3Smrg         increment_a64_address(ubld1, address, block_bytes);
56077ec681f3Smrg         loaded += block;
56087ec681f3Smrg      }
56097ec681f3Smrg
56107ec681f3Smrg      assert(loaded == total);
561101e04c3fSmrg      break;
561201e04c3fSmrg   }
561301e04c3fSmrg
56147ec681f3Smrg   case nir_intrinsic_store_global_block_intel: {
56157ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) == 32);
56167ec681f3Smrg
56177ec681f3Smrg      fs_reg address = bld.emit_uniformize(get_nir_src(instr->src[1]));
56187ec681f3Smrg      fs_reg src = get_nir_src(instr->src[0]);
56197ec681f3Smrg
56207ec681f3Smrg      const fs_builder ubld1 = bld.exec_all().group(1, 0);
56217ec681f3Smrg      const fs_builder ubld8 = bld.exec_all().group(8, 0);
56227ec681f3Smrg      const fs_builder ubld16 = bld.exec_all().group(16, 0);
56237ec681f3Smrg
56247ec681f3Smrg      const unsigned total = instr->num_components * dispatch_width;
56257ec681f3Smrg      unsigned written = 0;
56267ec681f3Smrg
56277ec681f3Smrg      while (written < total) {
56287ec681f3Smrg         const unsigned block =
56297ec681f3Smrg            choose_oword_block_size_dwords(total - written);
56307ec681f3Smrg
56317ec681f3Smrg         const fs_builder &ubld = block == 8 ? ubld8 : ubld16;
56327ec681f3Smrg         ubld.emit(SHADER_OPCODE_A64_OWORD_BLOCK_WRITE_LOGICAL,
56337ec681f3Smrg                   fs_reg(),
56347ec681f3Smrg                   address,
56357ec681f3Smrg                   retype(byte_offset(src, written * 4), BRW_REGISTER_TYPE_UD),
56367ec681f3Smrg                   brw_imm_ud(block));
56377ec681f3Smrg
56387ec681f3Smrg         const unsigned block_bytes = block * 4;
56397ec681f3Smrg         increment_a64_address(ubld1, address, block_bytes);
56407ec681f3Smrg         written += block;
56417ec681f3Smrg      }
56427ec681f3Smrg
56437ec681f3Smrg      assert(written == total);
56447ec681f3Smrg      break;
56457ec681f3Smrg   }
56467ec681f3Smrg
56477ec681f3Smrg   case nir_intrinsic_load_shared_block_intel:
56487ec681f3Smrg   case nir_intrinsic_load_ssbo_block_intel: {
56497ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
56507ec681f3Smrg
56517ec681f3Smrg      const bool is_ssbo =
56527ec681f3Smrg         instr->intrinsic == nir_intrinsic_load_ssbo_block_intel;
56537ec681f3Smrg      fs_reg address = bld.emit_uniformize(get_nir_src(instr->src[is_ssbo ? 1 : 0]));
56547ec681f3Smrg
56557ec681f3Smrg      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
56567ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_SURFACE] = is_ssbo ?
56577ec681f3Smrg         get_nir_ssbo_intrinsic_index(bld, instr) : fs_reg(brw_imm_ud(GFX7_BTI_SLM));
56587ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = address;
56597ec681f3Smrg
56607ec681f3Smrg      const fs_builder ubld1 = bld.exec_all().group(1, 0);
56617ec681f3Smrg      const fs_builder ubld8 = bld.exec_all().group(8, 0);
56627ec681f3Smrg      const fs_builder ubld16 = bld.exec_all().group(16, 0);
56637ec681f3Smrg
56647ec681f3Smrg      const unsigned total = instr->num_components * dispatch_width;
56657ec681f3Smrg      unsigned loaded = 0;
56667ec681f3Smrg
56677ec681f3Smrg      while (loaded < total) {
56687ec681f3Smrg         const unsigned block =
56697ec681f3Smrg            choose_oword_block_size_dwords(total - loaded);
56707ec681f3Smrg         const unsigned block_bytes = block * 4;
56717ec681f3Smrg
56727ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(block);
56737ec681f3Smrg
56747ec681f3Smrg         const fs_builder &ubld = block == 8 ? ubld8 : ubld16;
56757ec681f3Smrg         ubld.emit(SHADER_OPCODE_UNALIGNED_OWORD_BLOCK_READ_LOGICAL,
56767ec681f3Smrg                   retype(byte_offset(dest, loaded * 4), BRW_REGISTER_TYPE_UD),
56777ec681f3Smrg                   srcs, SURFACE_LOGICAL_NUM_SRCS)->size_written = block_bytes;
56787ec681f3Smrg
56797ec681f3Smrg         ubld1.ADD(address, address, brw_imm_ud(block_bytes));
56807ec681f3Smrg         loaded += block;
56817ec681f3Smrg      }
56827ec681f3Smrg
56837ec681f3Smrg      assert(loaded == total);
56847ec681f3Smrg      break;
56857ec681f3Smrg   }
56867ec681f3Smrg
56877ec681f3Smrg   case nir_intrinsic_store_shared_block_intel:
56887ec681f3Smrg   case nir_intrinsic_store_ssbo_block_intel: {
56897ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) == 32);
56907ec681f3Smrg
56917ec681f3Smrg      const bool is_ssbo =
56927ec681f3Smrg         instr->intrinsic == nir_intrinsic_store_ssbo_block_intel;
56937ec681f3Smrg
56947ec681f3Smrg      fs_reg address = bld.emit_uniformize(get_nir_src(instr->src[is_ssbo ? 2 : 1]));
56957ec681f3Smrg      fs_reg src = get_nir_src(instr->src[0]);
56967ec681f3Smrg
56977ec681f3Smrg      fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
56987ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_SURFACE] = is_ssbo ?
56997ec681f3Smrg         get_nir_ssbo_intrinsic_index(bld, instr) : fs_reg(brw_imm_ud(GFX7_BTI_SLM));
57007ec681f3Smrg      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = address;
57017ec681f3Smrg
57027ec681f3Smrg      const fs_builder ubld1 = bld.exec_all().group(1, 0);
57037ec681f3Smrg      const fs_builder ubld8 = bld.exec_all().group(8, 0);
57047ec681f3Smrg      const fs_builder ubld16 = bld.exec_all().group(16, 0);
57057ec681f3Smrg
57067ec681f3Smrg      const unsigned total = instr->num_components * dispatch_width;
57077ec681f3Smrg      unsigned written = 0;
57087ec681f3Smrg
57097ec681f3Smrg      while (written < total) {
57107ec681f3Smrg         const unsigned block =
57117ec681f3Smrg            choose_oword_block_size_dwords(total - written);
57127ec681f3Smrg
57137ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(block);
57147ec681f3Smrg         srcs[SURFACE_LOGICAL_SRC_DATA] =
57157ec681f3Smrg            retype(byte_offset(src, written * 4), BRW_REGISTER_TYPE_UD);
57167ec681f3Smrg
57177ec681f3Smrg         const fs_builder &ubld = block == 8 ? ubld8 : ubld16;
57187ec681f3Smrg         ubld.emit(SHADER_OPCODE_OWORD_BLOCK_WRITE_LOGICAL,
57197ec681f3Smrg                   fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
57207ec681f3Smrg
57217ec681f3Smrg         const unsigned block_bytes = block * 4;
57227ec681f3Smrg         ubld1.ADD(address, address, brw_imm_ud(block_bytes));
57237ec681f3Smrg         written += block;
57247ec681f3Smrg      }
57257ec681f3Smrg
57267ec681f3Smrg      assert(written == total);
572701e04c3fSmrg      break;
572801e04c3fSmrg   }
572901e04c3fSmrg
57307ec681f3Smrg   case nir_intrinsic_load_btd_dss_id_intel:
57317ec681f3Smrg      bld.emit(SHADER_OPCODE_GET_DSS_ID,
57327ec681f3Smrg               retype(dest, BRW_REGISTER_TYPE_UD));
57337ec681f3Smrg      break;
57347ec681f3Smrg
57357ec681f3Smrg   case nir_intrinsic_load_btd_stack_id_intel:
57367ec681f3Smrg      if (stage == MESA_SHADER_COMPUTE) {
57377ec681f3Smrg         assert(brw_cs_prog_data(prog_data)->uses_btd_stack_ids);
57387ec681f3Smrg      } else {
57397ec681f3Smrg         assert(brw_shader_stage_is_bindless(stage));
57407ec681f3Smrg      }
57417ec681f3Smrg      /* Stack IDs are always in R1 regardless of whether we're coming from a
57427ec681f3Smrg       * bindless shader or a regular compute shader.
57437ec681f3Smrg       */
57447ec681f3Smrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
57457ec681f3Smrg              retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UW));
57467ec681f3Smrg      break;
57477ec681f3Smrg
57487ec681f3Smrg   case nir_intrinsic_btd_spawn_intel:
57497ec681f3Smrg      if (stage == MESA_SHADER_COMPUTE) {
57507ec681f3Smrg         assert(brw_cs_prog_data(prog_data)->uses_btd_stack_ids);
57517ec681f3Smrg      } else {
57527ec681f3Smrg         assert(brw_shader_stage_is_bindless(stage));
57537ec681f3Smrg      }
57547ec681f3Smrg      bld.emit(SHADER_OPCODE_BTD_SPAWN_LOGICAL, bld.null_reg_ud(),
57557ec681f3Smrg               bld.emit_uniformize(get_nir_src(instr->src[0])),
57567ec681f3Smrg               get_nir_src(instr->src[1]));
57577ec681f3Smrg      break;
57587ec681f3Smrg
57597ec681f3Smrg   case nir_intrinsic_btd_retire_intel:
57607ec681f3Smrg      if (stage == MESA_SHADER_COMPUTE) {
57617ec681f3Smrg         assert(brw_cs_prog_data(prog_data)->uses_btd_stack_ids);
57627ec681f3Smrg      } else {
57637ec681f3Smrg         assert(brw_shader_stage_is_bindless(stage));
57647ec681f3Smrg      }
57657ec681f3Smrg      bld.emit(SHADER_OPCODE_BTD_RETIRE_LOGICAL);
57667ec681f3Smrg      break;
57677ec681f3Smrg
576801e04c3fSmrg   default:
576901e04c3fSmrg      unreachable("unknown intrinsic");
577001e04c3fSmrg   }
577101e04c3fSmrg}
577201e04c3fSmrg
577301e04c3fSmrgvoid
577401e04c3fSmrgfs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
577501e04c3fSmrg                                 int op, nir_intrinsic_instr *instr)
577601e04c3fSmrg{
57779f464c52Smaya   /* The BTI untyped atomic messages only support 32-bit atomics.  If you
57789f464c52Smaya    * just look at the big table of messages in the Vol 7 of the SKL PRM, they
57799f464c52Smaya    * appear to exist.  However, if you look at Vol 2a, there are no message
57809f464c52Smaya    * descriptors provided for Qword atomic ops except for A64 messages.
57819f464c52Smaya    */
57827ec681f3Smrg   assert(nir_dest_bit_size(instr->dest) == 32 ||
57837ec681f3Smrg          (nir_dest_bit_size(instr->dest) == 64 && devinfo->has_lsc));
57849f464c52Smaya
578501e04c3fSmrg   fs_reg dest;
578601e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
578701e04c3fSmrg      dest = get_nir_dest(instr->dest);
578801e04c3fSmrg
57899f464c52Smaya   fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
57909f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
57919f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
57929f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
57939f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
57947ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
579501e04c3fSmrg
57969f464c52Smaya   fs_reg data;
579701e04c3fSmrg   if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
57989f464c52Smaya      data = get_nir_src(instr->src[2]);
57999f464c52Smaya
58009f464c52Smaya   if (op == BRW_AOP_CMPWR) {
58019f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
58029f464c52Smaya      fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
58039f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
58049f464c52Smaya      data = tmp;
58059f464c52Smaya   }
58069f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_DATA] = data;
580701e04c3fSmrg
580801e04c3fSmrg   /* Emit the actual atomic operation */
580901e04c3fSmrg
58109f464c52Smaya   bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
58119f464c52Smaya            dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
581201e04c3fSmrg}
581301e04c3fSmrg
581401e04c3fSmrgvoid
581501e04c3fSmrgfs_visitor::nir_emit_ssbo_atomic_float(const fs_builder &bld,
581601e04c3fSmrg                                       int op, nir_intrinsic_instr *instr)
581701e04c3fSmrg{
581801e04c3fSmrg   fs_reg dest;
581901e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
582001e04c3fSmrg      dest = get_nir_dest(instr->dest);
582101e04c3fSmrg
58229f464c52Smaya   fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
58239f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
58249f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
58259f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
58269f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
58277ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
58289f464c52Smaya
58299f464c52Smaya   fs_reg data = get_nir_src(instr->src[2]);
58309f464c52Smaya   if (op == BRW_AOP_FCMPWR) {
58319f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
58329f464c52Smaya      fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
58339f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
58349f464c52Smaya      data = tmp;
583501e04c3fSmrg   }
58369f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_DATA] = data;
583701e04c3fSmrg
583801e04c3fSmrg   /* Emit the actual atomic operation */
583901e04c3fSmrg
58409f464c52Smaya   bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
58419f464c52Smaya            dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
584201e04c3fSmrg}
584301e04c3fSmrg
584401e04c3fSmrgvoid
584501e04c3fSmrgfs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
584601e04c3fSmrg                                   int op, nir_intrinsic_instr *instr)
584701e04c3fSmrg{
584801e04c3fSmrg   fs_reg dest;
584901e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
585001e04c3fSmrg      dest = get_nir_dest(instr->dest);
585101e04c3fSmrg
58529f464c52Smaya   fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
58537ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GFX7_BTI_SLM);
58549f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
58559f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
58567ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
58579f464c52Smaya
58589f464c52Smaya   fs_reg data;
585901e04c3fSmrg   if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
58609f464c52Smaya      data = get_nir_src(instr->src[1]);
58619f464c52Smaya   if (op == BRW_AOP_CMPWR) {
58629f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
58639f464c52Smaya      fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
58649f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
58659f464c52Smaya      data = tmp;
58669f464c52Smaya   }
58679f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_DATA] = data;
586801e04c3fSmrg
586901e04c3fSmrg   /* Get the offset */
58709f464c52Smaya   if (nir_src_is_const(instr->src[0])) {
58719f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
58729f464c52Smaya         brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
587301e04c3fSmrg   } else {
58749f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
58759f464c52Smaya      bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
587601e04c3fSmrg	      retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
587701e04c3fSmrg	      brw_imm_ud(instr->const_index[0]));
587801e04c3fSmrg   }
587901e04c3fSmrg
588001e04c3fSmrg   /* Emit the actual atomic operation operation */
588101e04c3fSmrg
58829f464c52Smaya   bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
58839f464c52Smaya            dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
588401e04c3fSmrg}
588501e04c3fSmrg
588601e04c3fSmrgvoid
588701e04c3fSmrgfs_visitor::nir_emit_shared_atomic_float(const fs_builder &bld,
588801e04c3fSmrg                                         int op, nir_intrinsic_instr *instr)
588901e04c3fSmrg{
589001e04c3fSmrg   fs_reg dest;
589101e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
589201e04c3fSmrg      dest = get_nir_dest(instr->dest);
589301e04c3fSmrg
58949f464c52Smaya   fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
58957ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GFX7_BTI_SLM);
58969f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
58979f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
58987ec681f3Smrg   srcs[SURFACE_LOGICAL_SRC_ALLOW_SAMPLE_MASK] = brw_imm_ud(1);
58999f464c52Smaya
59009f464c52Smaya   fs_reg data = get_nir_src(instr->src[1]);
59019f464c52Smaya   if (op == BRW_AOP_FCMPWR) {
59029f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
59039f464c52Smaya      fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
59049f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
59059f464c52Smaya      data = tmp;
59069f464c52Smaya   }
59079f464c52Smaya   srcs[SURFACE_LOGICAL_SRC_DATA] = data;
590801e04c3fSmrg
590901e04c3fSmrg   /* Get the offset */
59109f464c52Smaya   if (nir_src_is_const(instr->src[0])) {
59119f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
59129f464c52Smaya         brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
591301e04c3fSmrg   } else {
59149f464c52Smaya      srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
59159f464c52Smaya      bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
59169f464c52Smaya	      retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
59179f464c52Smaya	      brw_imm_ud(instr->const_index[0]));
591801e04c3fSmrg   }
591901e04c3fSmrg
592001e04c3fSmrg   /* Emit the actual atomic operation operation */
592101e04c3fSmrg
59229f464c52Smaya   bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
59239f464c52Smaya            dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
59249f464c52Smaya}
59259f464c52Smaya
59267ec681f3Smrgstatic fs_reg
59277ec681f3Smrgexpand_to_32bit(const fs_builder &bld, const fs_reg &src)
59287ec681f3Smrg{
59297ec681f3Smrg   if (type_sz(src.type) == 2) {
59307ec681f3Smrg      fs_reg src32 = bld.vgrf(BRW_REGISTER_TYPE_UD);
59317ec681f3Smrg      bld.MOV(src32, retype(src, BRW_REGISTER_TYPE_UW));
59327ec681f3Smrg      return src32;
59337ec681f3Smrg   } else {
59347ec681f3Smrg      return src;
59357ec681f3Smrg   }
59367ec681f3Smrg}
59377ec681f3Smrg
59389f464c52Smayavoid
59399f464c52Smayafs_visitor::nir_emit_global_atomic(const fs_builder &bld,
59409f464c52Smaya                                   int op, nir_intrinsic_instr *instr)
59419f464c52Smaya{
59429f464c52Smaya   fs_reg dest;
59439f464c52Smaya   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
59449f464c52Smaya      dest = get_nir_dest(instr->dest);
59459f464c52Smaya
59469f464c52Smaya   fs_reg addr = get_nir_src(instr->src[0]);
59479f464c52Smaya
59489f464c52Smaya   fs_reg data;
59499f464c52Smaya   if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
59507ec681f3Smrg      data = expand_to_32bit(bld, get_nir_src(instr->src[1]));
59519f464c52Smaya
59529f464c52Smaya   if (op == BRW_AOP_CMPWR) {
59539f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
59547ec681f3Smrg      fs_reg sources[2] = {
59557ec681f3Smrg         data,
59567ec681f3Smrg         expand_to_32bit(bld, get_nir_src(instr->src[2]))
59577ec681f3Smrg      };
59589f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
59599f464c52Smaya      data = tmp;
59609f464c52Smaya   }
59619f464c52Smaya
59627ec681f3Smrg   switch (nir_dest_bit_size(instr->dest)) {
59637ec681f3Smrg   case 16: {
59647ec681f3Smrg      fs_reg dest32 = bld.vgrf(BRW_REGISTER_TYPE_UD);
59657ec681f3Smrg      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT16_LOGICAL,
59667ec681f3Smrg               dest32, addr, data, brw_imm_ud(op));
59677ec681f3Smrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_UW), dest32);
59687ec681f3Smrg      break;
59697ec681f3Smrg   }
59707ec681f3Smrg   case 32:
59719f464c52Smaya      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL,
59729f464c52Smaya               dest, addr, data, brw_imm_ud(op));
59737ec681f3Smrg      break;
59747ec681f3Smrg   case 64:
59757ec681f3Smrg      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL,
59767ec681f3Smrg               dest, addr, data, brw_imm_ud(op));
59777ec681f3Smrg      break;
59787ec681f3Smrg   default:
59797ec681f3Smrg      unreachable("Unsupported bit size");
59809f464c52Smaya   }
59819f464c52Smaya}
59829f464c52Smaya
59839f464c52Smayavoid
59849f464c52Smayafs_visitor::nir_emit_global_atomic_float(const fs_builder &bld,
59859f464c52Smaya                                         int op, nir_intrinsic_instr *instr)
59869f464c52Smaya{
59879f464c52Smaya   assert(nir_intrinsic_infos[instr->intrinsic].has_dest);
59889f464c52Smaya   fs_reg dest = get_nir_dest(instr->dest);
59899f464c52Smaya
59909f464c52Smaya   fs_reg addr = get_nir_src(instr->src[0]);
59919f464c52Smaya
59929f464c52Smaya   assert(op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC);
59937ec681f3Smrg   fs_reg data = expand_to_32bit(bld, get_nir_src(instr->src[1]));
59949f464c52Smaya
59959f464c52Smaya   if (op == BRW_AOP_FCMPWR) {
59969f464c52Smaya      fs_reg tmp = bld.vgrf(data.type, 2);
59977ec681f3Smrg      fs_reg sources[2] = {
59987ec681f3Smrg         data,
59997ec681f3Smrg         expand_to_32bit(bld, get_nir_src(instr->src[2]))
60007ec681f3Smrg      };
60019f464c52Smaya      bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
60029f464c52Smaya      data = tmp;
60039f464c52Smaya   }
60049f464c52Smaya
60057ec681f3Smrg   switch (nir_dest_bit_size(instr->dest)) {
60067ec681f3Smrg   case 16: {
60077ec681f3Smrg      fs_reg dest32 = bld.vgrf(BRW_REGISTER_TYPE_UD);
60087ec681f3Smrg      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT16_LOGICAL,
60097ec681f3Smrg               dest32, addr, data, brw_imm_ud(op));
60107ec681f3Smrg      bld.MOV(retype(dest, BRW_REGISTER_TYPE_UW), dest32);
60117ec681f3Smrg      break;
60127ec681f3Smrg   }
60137ec681f3Smrg   case 32:
60147ec681f3Smrg      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT32_LOGICAL,
60157ec681f3Smrg               dest, addr, data, brw_imm_ud(op));
60167ec681f3Smrg      break;
60177ec681f3Smrg   case 64:
60187ec681f3Smrg      bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT64_LOGICAL,
60197ec681f3Smrg               dest, addr, data, brw_imm_ud(op));
60207ec681f3Smrg      break;
60217ec681f3Smrg   default:
60227ec681f3Smrg      unreachable("Unsupported bit size");
60237ec681f3Smrg   }
602401e04c3fSmrg}
602501e04c3fSmrg
602601e04c3fSmrgvoid
602701e04c3fSmrgfs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
602801e04c3fSmrg{
602901e04c3fSmrg   unsigned texture = instr->texture_index;
603001e04c3fSmrg   unsigned sampler = instr->sampler_index;
603101e04c3fSmrg
603201e04c3fSmrg   fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
603301e04c3fSmrg
603401e04c3fSmrg   srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
603501e04c3fSmrg   srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
603601e04c3fSmrg
603701e04c3fSmrg   int lod_components = 0;
603801e04c3fSmrg
603901e04c3fSmrg   /* The hardware requires a LOD for buffer textures */
604001e04c3fSmrg   if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
604101e04c3fSmrg      srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
604201e04c3fSmrg
604301e04c3fSmrg   uint32_t header_bits = 0;
604401e04c3fSmrg   for (unsigned i = 0; i < instr->num_srcs; i++) {
604501e04c3fSmrg      fs_reg src = get_nir_src(instr->src[i].src);
604601e04c3fSmrg      switch (instr->src[i].src_type) {
604701e04c3fSmrg      case nir_tex_src_bias:
604801e04c3fSmrg         srcs[TEX_LOGICAL_SRC_LOD] =
604901e04c3fSmrg            retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
605001e04c3fSmrg         break;
605101e04c3fSmrg      case nir_tex_src_comparator:
605201e04c3fSmrg         srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
605301e04c3fSmrg         break;
605401e04c3fSmrg      case nir_tex_src_coord:
605501e04c3fSmrg         switch (instr->op) {
605601e04c3fSmrg         case nir_texop_txf:
605701e04c3fSmrg         case nir_texop_txf_ms:
60587ec681f3Smrg         case nir_texop_txf_ms_mcs_intel:
605901e04c3fSmrg         case nir_texop_samples_identical:
606001e04c3fSmrg            srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
606101e04c3fSmrg            break;
606201e04c3fSmrg         default:
606301e04c3fSmrg            srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
606401e04c3fSmrg            break;
606501e04c3fSmrg         }
60667ec681f3Smrg
60677ec681f3Smrg         /* Wa_14013363432:
60687ec681f3Smrg          *
60697ec681f3Smrg          * Compiler should send U,V,R parameters even if V,R are 0.
60707ec681f3Smrg          */
60717ec681f3Smrg         if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && devinfo->verx10 == 125)
60727ec681f3Smrg            assert(instr->coord_components >= 3u);
607301e04c3fSmrg         break;
607401e04c3fSmrg      case nir_tex_src_ddx:
607501e04c3fSmrg         srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
607601e04c3fSmrg         lod_components = nir_tex_instr_src_size(instr, i);
607701e04c3fSmrg         break;
607801e04c3fSmrg      case nir_tex_src_ddy:
607901e04c3fSmrg         srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
608001e04c3fSmrg         break;
608101e04c3fSmrg      case nir_tex_src_lod:
608201e04c3fSmrg         switch (instr->op) {
608301e04c3fSmrg         case nir_texop_txs:
608401e04c3fSmrg            srcs[TEX_LOGICAL_SRC_LOD] =
608501e04c3fSmrg               retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_UD);
608601e04c3fSmrg            break;
608701e04c3fSmrg         case nir_texop_txf:
608801e04c3fSmrg            srcs[TEX_LOGICAL_SRC_LOD] =
608901e04c3fSmrg               retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_D);
609001e04c3fSmrg            break;
609101e04c3fSmrg         default:
609201e04c3fSmrg            srcs[TEX_LOGICAL_SRC_LOD] =
609301e04c3fSmrg               retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
609401e04c3fSmrg            break;
609501e04c3fSmrg         }
609601e04c3fSmrg         break;
60979f464c52Smaya      case nir_tex_src_min_lod:
60989f464c52Smaya         srcs[TEX_LOGICAL_SRC_MIN_LOD] =
60999f464c52Smaya            retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
61009f464c52Smaya         break;
610101e04c3fSmrg      case nir_tex_src_ms_index:
610201e04c3fSmrg         srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
610301e04c3fSmrg         break;
610401e04c3fSmrg
610501e04c3fSmrg      case nir_tex_src_offset: {
61069f464c52Smaya         uint32_t offset_bits = 0;
61079f464c52Smaya         if (brw_texture_offset(instr, i, &offset_bits)) {
610801e04c3fSmrg            header_bits |= offset_bits;
610901e04c3fSmrg         } else {
611001e04c3fSmrg            srcs[TEX_LOGICAL_SRC_TG4_OFFSET] =
611101e04c3fSmrg               retype(src, BRW_REGISTER_TYPE_D);
611201e04c3fSmrg         }
611301e04c3fSmrg         break;
611401e04c3fSmrg      }
611501e04c3fSmrg
611601e04c3fSmrg      case nir_tex_src_projector:
611701e04c3fSmrg         unreachable("should be lowered");
611801e04c3fSmrg
611901e04c3fSmrg      case nir_tex_src_texture_offset: {
612001e04c3fSmrg         /* Emit code to evaluate the actual indexing expression */
612101e04c3fSmrg         fs_reg tmp = vgrf(glsl_type::uint_type);
612201e04c3fSmrg         bld.ADD(tmp, src, brw_imm_ud(texture));
612301e04c3fSmrg         srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
612401e04c3fSmrg         break;
612501e04c3fSmrg      }
612601e04c3fSmrg
612701e04c3fSmrg      case nir_tex_src_sampler_offset: {
612801e04c3fSmrg         /* Emit code to evaluate the actual indexing expression */
612901e04c3fSmrg         fs_reg tmp = vgrf(glsl_type::uint_type);
613001e04c3fSmrg         bld.ADD(tmp, src, brw_imm_ud(sampler));
613101e04c3fSmrg         srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
613201e04c3fSmrg         break;
613301e04c3fSmrg      }
613401e04c3fSmrg
61359f464c52Smaya      case nir_tex_src_texture_handle:
61369f464c52Smaya         assert(nir_tex_instr_src_index(instr, nir_tex_src_texture_offset) == -1);
61379f464c52Smaya         srcs[TEX_LOGICAL_SRC_SURFACE] = fs_reg();
61389f464c52Smaya         srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = bld.emit_uniformize(src);
61399f464c52Smaya         break;
61409f464c52Smaya
61419f464c52Smaya      case nir_tex_src_sampler_handle:
61429f464c52Smaya         assert(nir_tex_instr_src_index(instr, nir_tex_src_sampler_offset) == -1);
61439f464c52Smaya         srcs[TEX_LOGICAL_SRC_SAMPLER] = fs_reg();
61449f464c52Smaya         srcs[TEX_LOGICAL_SRC_SAMPLER_HANDLE] = bld.emit_uniformize(src);
61459f464c52Smaya         break;
61469f464c52Smaya
61477ec681f3Smrg      case nir_tex_src_ms_mcs_intel:
614801e04c3fSmrg         assert(instr->op == nir_texop_txf_ms);
614901e04c3fSmrg         srcs[TEX_LOGICAL_SRC_MCS] = retype(src, BRW_REGISTER_TYPE_D);
615001e04c3fSmrg         break;
615101e04c3fSmrg
615201e04c3fSmrg      case nir_tex_src_plane: {
61539f464c52Smaya         const uint32_t plane = nir_src_as_uint(instr->src[i].src);
615401e04c3fSmrg         const uint32_t texture_index =
615501e04c3fSmrg            instr->texture_index +
615601e04c3fSmrg            stage_prog_data->binding_table.plane_start[plane] -
615701e04c3fSmrg            stage_prog_data->binding_table.texture_start;
615801e04c3fSmrg
615901e04c3fSmrg         srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture_index);
616001e04c3fSmrg         break;
616101e04c3fSmrg      }
616201e04c3fSmrg
616301e04c3fSmrg      default:
616401e04c3fSmrg         unreachable("unknown texture source");
616501e04c3fSmrg      }
616601e04c3fSmrg   }
616701e04c3fSmrg
616801e04c3fSmrg   if (srcs[TEX_LOGICAL_SRC_MCS].file == BAD_FILE &&
616901e04c3fSmrg       (instr->op == nir_texop_txf_ms ||
617001e04c3fSmrg        instr->op == nir_texop_samples_identical)) {
61717ec681f3Smrg      if (devinfo->ver >= 7 &&
617201e04c3fSmrg          key_tex->compressed_multisample_layout_mask & (1 << texture)) {
617301e04c3fSmrg         srcs[TEX_LOGICAL_SRC_MCS] =
617401e04c3fSmrg            emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
617501e04c3fSmrg                           instr->coord_components,
61769f464c52Smaya                           srcs[TEX_LOGICAL_SRC_SURFACE],
61779f464c52Smaya                           srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE]);
617801e04c3fSmrg      } else {
617901e04c3fSmrg         srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
618001e04c3fSmrg      }
618101e04c3fSmrg   }
618201e04c3fSmrg
618301e04c3fSmrg   srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
618401e04c3fSmrg   srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
618501e04c3fSmrg
618601e04c3fSmrg   enum opcode opcode;
618701e04c3fSmrg   switch (instr->op) {
618801e04c3fSmrg   case nir_texop_tex:
61899f464c52Smaya      opcode = SHADER_OPCODE_TEX_LOGICAL;
619001e04c3fSmrg      break;
619101e04c3fSmrg   case nir_texop_txb:
619201e04c3fSmrg      opcode = FS_OPCODE_TXB_LOGICAL;
619301e04c3fSmrg      break;
619401e04c3fSmrg   case nir_texop_txl:
619501e04c3fSmrg      opcode = SHADER_OPCODE_TXL_LOGICAL;
619601e04c3fSmrg      break;
619701e04c3fSmrg   case nir_texop_txd:
619801e04c3fSmrg      opcode = SHADER_OPCODE_TXD_LOGICAL;
619901e04c3fSmrg      break;
620001e04c3fSmrg   case nir_texop_txf:
620101e04c3fSmrg      opcode = SHADER_OPCODE_TXF_LOGICAL;
620201e04c3fSmrg      break;
620301e04c3fSmrg   case nir_texop_txf_ms:
620401e04c3fSmrg      if ((key_tex->msaa_16 & (1 << sampler)))
620501e04c3fSmrg         opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
620601e04c3fSmrg      else
620701e04c3fSmrg         opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
620801e04c3fSmrg      break;
62097ec681f3Smrg   case nir_texop_txf_ms_mcs_intel:
621001e04c3fSmrg      opcode = SHADER_OPCODE_TXF_MCS_LOGICAL;
621101e04c3fSmrg      break;
621201e04c3fSmrg   case nir_texop_query_levels:
621301e04c3fSmrg   case nir_texop_txs:
621401e04c3fSmrg      opcode = SHADER_OPCODE_TXS_LOGICAL;
621501e04c3fSmrg      break;
621601e04c3fSmrg   case nir_texop_lod:
621701e04c3fSmrg      opcode = SHADER_OPCODE_LOD_LOGICAL;
621801e04c3fSmrg      break;
621901e04c3fSmrg   case nir_texop_tg4:
622001e04c3fSmrg      if (srcs[TEX_LOGICAL_SRC_TG4_OFFSET].file != BAD_FILE)
622101e04c3fSmrg         opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
622201e04c3fSmrg      else
622301e04c3fSmrg         opcode = SHADER_OPCODE_TG4_LOGICAL;
622401e04c3fSmrg      break;
622501e04c3fSmrg   case nir_texop_texture_samples:
622601e04c3fSmrg      opcode = SHADER_OPCODE_SAMPLEINFO_LOGICAL;
622701e04c3fSmrg      break;
622801e04c3fSmrg   case nir_texop_samples_identical: {
622901e04c3fSmrg      fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
623001e04c3fSmrg
623101e04c3fSmrg      /* If mcs is an immediate value, it means there is no MCS.  In that case
623201e04c3fSmrg       * just return false.
623301e04c3fSmrg       */
623401e04c3fSmrg      if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
623501e04c3fSmrg         bld.MOV(dst, brw_imm_ud(0u));
623601e04c3fSmrg      } else if ((key_tex->msaa_16 & (1 << sampler))) {
623701e04c3fSmrg         fs_reg tmp = vgrf(glsl_type::uint_type);
623801e04c3fSmrg         bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
623901e04c3fSmrg                offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
624001e04c3fSmrg         bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
624101e04c3fSmrg      } else {
624201e04c3fSmrg         bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
624301e04c3fSmrg                 BRW_CONDITIONAL_EQ);
624401e04c3fSmrg      }
624501e04c3fSmrg      return;
624601e04c3fSmrg   }
624701e04c3fSmrg   default:
624801e04c3fSmrg      unreachable("unknown texture opcode");
624901e04c3fSmrg   }
625001e04c3fSmrg
625101e04c3fSmrg   if (instr->op == nir_texop_tg4) {
625201e04c3fSmrg      if (instr->component == 1 &&
625301e04c3fSmrg          key_tex->gather_channel_quirk_mask & (1 << texture)) {
625401e04c3fSmrg         /* gather4 sampler is broken for green channel on RG32F --
625501e04c3fSmrg          * we must ask for blue instead.
625601e04c3fSmrg          */
625701e04c3fSmrg         header_bits |= 2 << 16;
625801e04c3fSmrg      } else {
625901e04c3fSmrg         header_bits |= instr->component << 16;
626001e04c3fSmrg      }
626101e04c3fSmrg   }
626201e04c3fSmrg
626301e04c3fSmrg   fs_reg dst = bld.vgrf(brw_type_for_nir_type(devinfo, instr->dest_type), 4);
626401e04c3fSmrg   fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
626501e04c3fSmrg   inst->offset = header_bits;
626601e04c3fSmrg
626701e04c3fSmrg   const unsigned dest_size = nir_tex_instr_dest_size(instr);
62687ec681f3Smrg   if (devinfo->ver >= 9 &&
626901e04c3fSmrg       instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
627001e04c3fSmrg      unsigned write_mask = instr->dest.is_ssa ?
627101e04c3fSmrg                            nir_ssa_def_components_read(&instr->dest.ssa):
627201e04c3fSmrg                            (1 << dest_size) - 1;
627301e04c3fSmrg      assert(write_mask != 0); /* dead code should have been eliminated */
627401e04c3fSmrg      inst->size_written = util_last_bit(write_mask) *
627501e04c3fSmrg                           inst->dst.component_size(inst->exec_size);
627601e04c3fSmrg   } else {
627701e04c3fSmrg      inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
627801e04c3fSmrg   }
627901e04c3fSmrg
628001e04c3fSmrg   if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
628101e04c3fSmrg      inst->shadow_compare = true;
628201e04c3fSmrg
62837ec681f3Smrg   if (instr->op == nir_texop_tg4 && devinfo->ver == 6)
62847ec681f3Smrg      emit_gfx6_gather_wa(key_tex->gfx6_gather_wa[texture], dst);
628501e04c3fSmrg
62867ec681f3Smrg   fs_reg nir_dest[5];
628701e04c3fSmrg   for (unsigned i = 0; i < dest_size; i++)
628801e04c3fSmrg      nir_dest[i] = offset(dst, bld, i);
628901e04c3fSmrg
629001e04c3fSmrg   if (instr->op == nir_texop_query_levels) {
629101e04c3fSmrg      /* # levels is in .w */
62927ec681f3Smrg      if (devinfo->ver <= 9) {
62937ec681f3Smrg         /**
62947ec681f3Smrg          * Wa_1940217:
62957ec681f3Smrg          *
62967ec681f3Smrg          * When a surface of type SURFTYPE_NULL is accessed by resinfo, the
62977ec681f3Smrg          * MIPCount returned is undefined instead of 0.
62987ec681f3Smrg          */
62997ec681f3Smrg         fs_inst *mov = bld.MOV(bld.null_reg_d(), dst);
63007ec681f3Smrg         mov->conditional_mod = BRW_CONDITIONAL_NZ;
63017ec681f3Smrg         nir_dest[0] = bld.vgrf(BRW_REGISTER_TYPE_D);
63027ec681f3Smrg         fs_inst *sel = bld.SEL(nir_dest[0], offset(dst, bld, 3), brw_imm_d(0));
63037ec681f3Smrg         sel->predicate = BRW_PREDICATE_NORMAL;
63047ec681f3Smrg      } else {
63057ec681f3Smrg         nir_dest[0] = offset(dst, bld, 3);
63067ec681f3Smrg      }
630701e04c3fSmrg   } else if (instr->op == nir_texop_txs &&
63087ec681f3Smrg              dest_size >= 3 && devinfo->ver < 7) {
63097ec681f3Smrg      /* Gfx4-6 return 0 instead of 1 for single layer surfaces. */
631001e04c3fSmrg      fs_reg depth = offset(dst, bld, 2);
631101e04c3fSmrg      nir_dest[2] = vgrf(glsl_type::int_type);
631201e04c3fSmrg      bld.emit_minmax(nir_dest[2], depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
631301e04c3fSmrg   }
631401e04c3fSmrg
631501e04c3fSmrg   bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
631601e04c3fSmrg}
631701e04c3fSmrg
631801e04c3fSmrgvoid
631901e04c3fSmrgfs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
632001e04c3fSmrg{
632101e04c3fSmrg   switch (instr->type) {
632201e04c3fSmrg   case nir_jump_break:
632301e04c3fSmrg      bld.emit(BRW_OPCODE_BREAK);
632401e04c3fSmrg      break;
632501e04c3fSmrg   case nir_jump_continue:
632601e04c3fSmrg      bld.emit(BRW_OPCODE_CONTINUE);
632701e04c3fSmrg      break;
63287ec681f3Smrg   case nir_jump_halt:
63297ec681f3Smrg      bld.emit(BRW_OPCODE_HALT);
63307ec681f3Smrg      break;
633101e04c3fSmrg   case nir_jump_return:
633201e04c3fSmrg   default:
633301e04c3fSmrg      unreachable("unknown jump");
633401e04c3fSmrg   }
633501e04c3fSmrg}
633601e04c3fSmrg
633701e04c3fSmrg/*
633801e04c3fSmrg * This helper takes a source register and un/shuffles it into the destination
633901e04c3fSmrg * register.
634001e04c3fSmrg *
634101e04c3fSmrg * If source type size is smaller than destination type size the operation
634201e04c3fSmrg * needed is a component shuffle. The opposite case would be an unshuffle. If
634301e04c3fSmrg * source/destination type size is equal a shuffle is done that would be
634401e04c3fSmrg * equivalent to a simple MOV.
634501e04c3fSmrg *
634601e04c3fSmrg * For example, if source is a 16-bit type and destination is 32-bit. A 3
634701e04c3fSmrg * components .xyz 16-bit vector on SIMD8 would be.
634801e04c3fSmrg *
634901e04c3fSmrg *    |x1|x2|x3|x4|x5|x6|x7|x8|y1|y2|y3|y4|y5|y6|y7|y8|
635001e04c3fSmrg *    |z1|z2|z3|z4|z5|z6|z7|z8|  |  |  |  |  |  |  |  |
635101e04c3fSmrg *
635201e04c3fSmrg * This helper will return the following 2 32-bit components with the 16-bit
635301e04c3fSmrg * values shuffled:
635401e04c3fSmrg *
635501e04c3fSmrg *    |x1 y1|x2 y2|x3 y3|x4 y4|x5 y5|x6 y6|x7 y7|x8 y8|
635601e04c3fSmrg *    |z1   |z2   |z3   |z4   |z5   |z6   |z7   |z8   |
635701e04c3fSmrg *
635801e04c3fSmrg * For unshuffle, the example would be the opposite, a 64-bit type source
635901e04c3fSmrg * and a 32-bit destination. A 2 component .xy 64-bit vector on SIMD8
636001e04c3fSmrg * would be:
636101e04c3fSmrg *
636201e04c3fSmrg *    | x1l   x1h | x2l   x2h | x3l   x3h | x4l   x4h |
636301e04c3fSmrg *    | x5l   x5h | x6l   x6h | x7l   x7h | x8l   x8h |
636401e04c3fSmrg *    | y1l   y1h | y2l   y2h | y3l   y3h | y4l   y4h |
636501e04c3fSmrg *    | y5l   y5h | y6l   y6h | y7l   y7h | y8l   y8h |
636601e04c3fSmrg *
636701e04c3fSmrg * The returned result would be the following 4 32-bit components unshuffled:
636801e04c3fSmrg *
636901e04c3fSmrg *    | x1l | x2l | x3l | x4l | x5l | x6l | x7l | x8l |
637001e04c3fSmrg *    | x1h | x2h | x3h | x4h | x5h | x6h | x7h | x8h |
637101e04c3fSmrg *    | y1l | y2l | y3l | y4l | y5l | y6l | y7l | y8l |
637201e04c3fSmrg *    | y1h | y2h | y3h | y4h | y5h | y6h | y7h | y8h |
637301e04c3fSmrg *
637401e04c3fSmrg * - Source and destination register must not be overlapped.
637501e04c3fSmrg * - components units are measured in terms of the smaller type between
637601e04c3fSmrg *   source and destination because we are un/shuffling the smaller
637701e04c3fSmrg *   components from/into the bigger ones.
637801e04c3fSmrg * - first_component parameter allows skipping source components.
637901e04c3fSmrg */
638001e04c3fSmrgvoid
638101e04c3fSmrgshuffle_src_to_dst(const fs_builder &bld,
638201e04c3fSmrg                   const fs_reg &dst,
638301e04c3fSmrg                   const fs_reg &src,
638401e04c3fSmrg                   uint32_t first_component,
638501e04c3fSmrg                   uint32_t components)
638601e04c3fSmrg{
638701e04c3fSmrg   if (type_sz(src.type) == type_sz(dst.type)) {
638801e04c3fSmrg      assert(!regions_overlap(dst,
638901e04c3fSmrg         type_sz(dst.type) * bld.dispatch_width() * components,
639001e04c3fSmrg         offset(src, bld, first_component),
639101e04c3fSmrg         type_sz(src.type) * bld.dispatch_width() * components));
639201e04c3fSmrg      for (unsigned i = 0; i < components; i++) {
639301e04c3fSmrg         bld.MOV(retype(offset(dst, bld, i), src.type),
639401e04c3fSmrg                 offset(src, bld, i + first_component));
639501e04c3fSmrg      }
639601e04c3fSmrg   } else if (type_sz(src.type) < type_sz(dst.type)) {
639701e04c3fSmrg      /* Source is shuffled into destination */
639801e04c3fSmrg      unsigned size_ratio = type_sz(dst.type) / type_sz(src.type);
639901e04c3fSmrg      assert(!regions_overlap(dst,
640001e04c3fSmrg         type_sz(dst.type) * bld.dispatch_width() *
640101e04c3fSmrg         DIV_ROUND_UP(components, size_ratio),
640201e04c3fSmrg         offset(src, bld, first_component),
640301e04c3fSmrg         type_sz(src.type) * bld.dispatch_width() * components));
640401e04c3fSmrg
640501e04c3fSmrg      brw_reg_type shuffle_type =
640601e04c3fSmrg         brw_reg_type_from_bit_size(8 * type_sz(src.type),
640701e04c3fSmrg                                    BRW_REGISTER_TYPE_D);
640801e04c3fSmrg      for (unsigned i = 0; i < components; i++) {
640901e04c3fSmrg         fs_reg shuffle_component_i =
641001e04c3fSmrg            subscript(offset(dst, bld, i / size_ratio),
641101e04c3fSmrg                      shuffle_type, i % size_ratio);
641201e04c3fSmrg         bld.MOV(shuffle_component_i,
641301e04c3fSmrg                 retype(offset(src, bld, i + first_component), shuffle_type));
641401e04c3fSmrg      }
641501e04c3fSmrg   } else {
641601e04c3fSmrg      /* Source is unshuffled into destination */
641701e04c3fSmrg      unsigned size_ratio = type_sz(src.type) / type_sz(dst.type);
641801e04c3fSmrg      assert(!regions_overlap(dst,
641901e04c3fSmrg         type_sz(dst.type) * bld.dispatch_width() * components,
642001e04c3fSmrg         offset(src, bld, first_component / size_ratio),
642101e04c3fSmrg         type_sz(src.type) * bld.dispatch_width() *
642201e04c3fSmrg         DIV_ROUND_UP(components + (first_component % size_ratio),
642301e04c3fSmrg                      size_ratio)));
642401e04c3fSmrg
642501e04c3fSmrg      brw_reg_type shuffle_type =
642601e04c3fSmrg         brw_reg_type_from_bit_size(8 * type_sz(dst.type),
642701e04c3fSmrg                                    BRW_REGISTER_TYPE_D);
642801e04c3fSmrg      for (unsigned i = 0; i < components; i++) {
642901e04c3fSmrg         fs_reg shuffle_component_i =
643001e04c3fSmrg            subscript(offset(src, bld, (first_component + i) / size_ratio),
643101e04c3fSmrg                      shuffle_type, (first_component + i) % size_ratio);
643201e04c3fSmrg         bld.MOV(retype(offset(dst, bld, i), shuffle_type),
643301e04c3fSmrg                 shuffle_component_i);
643401e04c3fSmrg      }
643501e04c3fSmrg   }
643601e04c3fSmrg}
643701e04c3fSmrg
643801e04c3fSmrgvoid
643901e04c3fSmrgshuffle_from_32bit_read(const fs_builder &bld,
644001e04c3fSmrg                        const fs_reg &dst,
644101e04c3fSmrg                        const fs_reg &src,
644201e04c3fSmrg                        uint32_t first_component,
644301e04c3fSmrg                        uint32_t components)
644401e04c3fSmrg{
644501e04c3fSmrg   assert(type_sz(src.type) == 4);
644601e04c3fSmrg
644701e04c3fSmrg   /* This function takes components in units of the destination type while
644801e04c3fSmrg    * shuffle_src_to_dst takes components in units of the smallest type
644901e04c3fSmrg    */
645001e04c3fSmrg   if (type_sz(dst.type) > 4) {
645101e04c3fSmrg      assert(type_sz(dst.type) == 8);
645201e04c3fSmrg      first_component *= 2;
645301e04c3fSmrg      components *= 2;
645401e04c3fSmrg   }
645501e04c3fSmrg
645601e04c3fSmrg   shuffle_src_to_dst(bld, dst, src, first_component, components);
645701e04c3fSmrg}
645801e04c3fSmrg
645901e04c3fSmrgfs_reg
646001e04c3fSmrgsetup_imm_df(const fs_builder &bld, double v)
646101e04c3fSmrg{
64627ec681f3Smrg   const struct intel_device_info *devinfo = bld.shader->devinfo;
64637ec681f3Smrg   assert(devinfo->ver >= 7);
646401e04c3fSmrg
64657ec681f3Smrg   if (devinfo->ver >= 8)
646601e04c3fSmrg      return brw_imm_df(v);
646701e04c3fSmrg
64687ec681f3Smrg   /* gfx7.5 does not support DF immediates straighforward but the DIM
646901e04c3fSmrg    * instruction allows to set the 64-bit immediate value.
647001e04c3fSmrg    */
647101e04c3fSmrg   if (devinfo->is_haswell) {
647201e04c3fSmrg      const fs_builder ubld = bld.exec_all().group(1, 0);
647301e04c3fSmrg      fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_DF, 1);
647401e04c3fSmrg      ubld.DIM(dst, brw_imm_df(v));
647501e04c3fSmrg      return component(dst, 0);
647601e04c3fSmrg   }
647701e04c3fSmrg
64787ec681f3Smrg   /* gfx7 does not support DF immediates, so we generate a 64-bit constant by
647901e04c3fSmrg    * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
648001e04c3fSmrg    * the high 32-bit to suboffset 4 and then applying a stride of 0.
648101e04c3fSmrg    *
648201e04c3fSmrg    * Alternatively, we could also produce a normal VGRF (without stride 0)
648301e04c3fSmrg    * by writing to all the channels in the VGRF, however, that would hit the
64847ec681f3Smrg    * gfx7 bug where we have to split writes that span more than 1 register
648501e04c3fSmrg    * into instructions with a width of 4 (otherwise the write to the second
648601e04c3fSmrg    * register written runs into an execmask hardware bug) which isn't very
648701e04c3fSmrg    * nice.
648801e04c3fSmrg    */
648901e04c3fSmrg   union {
649001e04c3fSmrg      double d;
649101e04c3fSmrg      struct {
649201e04c3fSmrg         uint32_t i1;
649301e04c3fSmrg         uint32_t i2;
649401e04c3fSmrg      };
649501e04c3fSmrg   } di;
649601e04c3fSmrg
649701e04c3fSmrg   di.d = v;
649801e04c3fSmrg
649901e04c3fSmrg   const fs_builder ubld = bld.exec_all().group(1, 0);
650001e04c3fSmrg   const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
650101e04c3fSmrg   ubld.MOV(tmp, brw_imm_ud(di.i1));
650201e04c3fSmrg   ubld.MOV(horiz_offset(tmp, 1), brw_imm_ud(di.i2));
650301e04c3fSmrg
650401e04c3fSmrg   return component(retype(tmp, BRW_REGISTER_TYPE_DF), 0);
650501e04c3fSmrg}
650601e04c3fSmrg
650701e04c3fSmrgfs_reg
650801e04c3fSmrgsetup_imm_b(const fs_builder &bld, int8_t v)
650901e04c3fSmrg{
651001e04c3fSmrg   const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_B);
651101e04c3fSmrg   bld.MOV(tmp, brw_imm_w(v));
651201e04c3fSmrg   return tmp;
651301e04c3fSmrg}
651401e04c3fSmrg
651501e04c3fSmrgfs_reg
651601e04c3fSmrgsetup_imm_ub(const fs_builder &bld, uint8_t v)
651701e04c3fSmrg{
651801e04c3fSmrg   const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UB);
651901e04c3fSmrg   bld.MOV(tmp, brw_imm_uw(v));
652001e04c3fSmrg   return tmp;
652101e04c3fSmrg}
6522