1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "brw_nir.h"
25bf215546Sopenharmony_ci#include "brw_vec4.h"
26bf215546Sopenharmony_ci#include "brw_vec4_builder.h"
27bf215546Sopenharmony_ci#include "brw_vec4_surface_builder.h"
28bf215546Sopenharmony_ci#include "brw_eu.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ciusing namespace brw;
31bf215546Sopenharmony_ciusing namespace brw::surface_access;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cinamespace brw {
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_civoid
36bf215546Sopenharmony_civec4_visitor::emit_nir_code()
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   if (nir->num_uniforms > 0)
39bf215546Sopenharmony_ci      nir_setup_uniforms();
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   nir_emit_impl(nir_shader_get_entrypoint((nir_shader *)nir));
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_civoid
45bf215546Sopenharmony_civec4_visitor::nir_setup_uniforms()
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   uniforms = nir->num_uniforms / 16;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_civoid
51bf215546Sopenharmony_civec4_visitor::nir_emit_impl(nir_function_impl *impl)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   nir_locals = ralloc_array(mem_ctx, dst_reg, impl->reg_alloc);
54bf215546Sopenharmony_ci   for (unsigned i = 0; i < impl->reg_alloc; i++) {
55bf215546Sopenharmony_ci      nir_locals[i] = dst_reg();
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   foreach_list_typed(nir_register, reg, node, &impl->registers) {
59bf215546Sopenharmony_ci      unsigned array_elems =
60bf215546Sopenharmony_ci         reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
61bf215546Sopenharmony_ci      const unsigned num_regs = array_elems * DIV_ROUND_UP(reg->bit_size, 32);
62bf215546Sopenharmony_ci      nir_locals[reg->index] = dst_reg(VGRF, alloc.allocate(num_regs));
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci      if (reg->bit_size == 64)
65bf215546Sopenharmony_ci         nir_locals[reg->index].type = BRW_REGISTER_TYPE_DF;
66bf215546Sopenharmony_ci   }
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   nir_emit_cf_list(&impl->body);
71bf215546Sopenharmony_ci}
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_civoid
74bf215546Sopenharmony_civec4_visitor::nir_emit_cf_list(exec_list *list)
75bf215546Sopenharmony_ci{
76bf215546Sopenharmony_ci   exec_list_validate(list);
77bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, list) {
78bf215546Sopenharmony_ci      switch (node->type) {
79bf215546Sopenharmony_ci      case nir_cf_node_if:
80bf215546Sopenharmony_ci         nir_emit_if(nir_cf_node_as_if(node));
81bf215546Sopenharmony_ci         break;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      case nir_cf_node_loop:
84bf215546Sopenharmony_ci         nir_emit_loop(nir_cf_node_as_loop(node));
85bf215546Sopenharmony_ci         break;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci      case nir_cf_node_block:
88bf215546Sopenharmony_ci         nir_emit_block(nir_cf_node_as_block(node));
89bf215546Sopenharmony_ci         break;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci      default:
92bf215546Sopenharmony_ci         unreachable("Invalid CFG node block");
93bf215546Sopenharmony_ci      }
94bf215546Sopenharmony_ci   }
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_civec4_visitor::nir_emit_if(nir_if *if_stmt)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   /* First, put the condition in f0 */
101bf215546Sopenharmony_ci   src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
102bf215546Sopenharmony_ci   vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
103bf215546Sopenharmony_ci   inst->conditional_mod = BRW_CONDITIONAL_NZ;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   /* We can just predicate based on the X channel, as the condition only
106bf215546Sopenharmony_ci    * goes on its own line */
107bf215546Sopenharmony_ci   emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X));
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   nir_emit_cf_list(&if_stmt->then_list);
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   /* note: if the else is empty, dead CF elimination will remove it */
112bf215546Sopenharmony_ci   emit(BRW_OPCODE_ELSE);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   nir_emit_cf_list(&if_stmt->else_list);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   emit(BRW_OPCODE_ENDIF);
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_civoid
120bf215546Sopenharmony_civec4_visitor::nir_emit_loop(nir_loop *loop)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   emit(BRW_OPCODE_DO);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   nir_emit_cf_list(&loop->body);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   emit(BRW_OPCODE_WHILE);
127bf215546Sopenharmony_ci}
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_civoid
130bf215546Sopenharmony_civec4_visitor::nir_emit_block(nir_block *block)
131bf215546Sopenharmony_ci{
132bf215546Sopenharmony_ci   nir_foreach_instr(instr, block) {
133bf215546Sopenharmony_ci      nir_emit_instr(instr);
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_civoid
138bf215546Sopenharmony_civec4_visitor::nir_emit_instr(nir_instr *instr)
139bf215546Sopenharmony_ci{
140bf215546Sopenharmony_ci   base_ir = instr;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   switch (instr->type) {
143bf215546Sopenharmony_ci   case nir_instr_type_load_const:
144bf215546Sopenharmony_ci      nir_emit_load_const(nir_instr_as_load_const(instr));
145bf215546Sopenharmony_ci      break;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   case nir_instr_type_intrinsic:
148bf215546Sopenharmony_ci      nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
149bf215546Sopenharmony_ci      break;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   case nir_instr_type_alu:
152bf215546Sopenharmony_ci      nir_emit_alu(nir_instr_as_alu(instr));
153bf215546Sopenharmony_ci      break;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   case nir_instr_type_jump:
156bf215546Sopenharmony_ci      nir_emit_jump(nir_instr_as_jump(instr));
157bf215546Sopenharmony_ci      break;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   case nir_instr_type_tex:
160bf215546Sopenharmony_ci      nir_emit_texture(nir_instr_as_tex(instr));
161bf215546Sopenharmony_ci      break;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
164bf215546Sopenharmony_ci      nir_emit_undef(nir_instr_as_ssa_undef(instr));
165bf215546Sopenharmony_ci      break;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   default:
168bf215546Sopenharmony_ci      unreachable("VS instruction not yet implemented by NIR->vec4");
169bf215546Sopenharmony_ci   }
170bf215546Sopenharmony_ci}
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_cistatic dst_reg
173bf215546Sopenharmony_cidst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
174bf215546Sopenharmony_ci                    unsigned base_offset, nir_src *indirect)
175bf215546Sopenharmony_ci{
176bf215546Sopenharmony_ci   dst_reg reg;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   reg = v->nir_locals[nir_reg->index];
179bf215546Sopenharmony_ci   if (nir_reg->bit_size == 64)
180bf215546Sopenharmony_ci      reg.type = BRW_REGISTER_TYPE_DF;
181bf215546Sopenharmony_ci   reg = offset(reg, 8, base_offset);
182bf215546Sopenharmony_ci   if (indirect) {
183bf215546Sopenharmony_ci      reg.reladdr =
184bf215546Sopenharmony_ci         new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
185bf215546Sopenharmony_ci                                                BRW_REGISTER_TYPE_D,
186bf215546Sopenharmony_ci                                                1));
187bf215546Sopenharmony_ci   }
188bf215546Sopenharmony_ci   return reg;
189bf215546Sopenharmony_ci}
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_cidst_reg
192bf215546Sopenharmony_civec4_visitor::get_nir_dest(const nir_dest &dest)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   if (dest.is_ssa) {
195bf215546Sopenharmony_ci      dst_reg dst =
196bf215546Sopenharmony_ci         dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(dest.ssa.bit_size, 32)));
197bf215546Sopenharmony_ci      if (dest.ssa.bit_size == 64)
198bf215546Sopenharmony_ci         dst.type = BRW_REGISTER_TYPE_DF;
199bf215546Sopenharmony_ci      nir_ssa_values[dest.ssa.index] = dst;
200bf215546Sopenharmony_ci      return dst;
201bf215546Sopenharmony_ci   } else {
202bf215546Sopenharmony_ci      return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
203bf215546Sopenharmony_ci                                 dest.reg.indirect);
204bf215546Sopenharmony_ci   }
205bf215546Sopenharmony_ci}
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_cidst_reg
208bf215546Sopenharmony_civec4_visitor::get_nir_dest(const nir_dest &dest, enum brw_reg_type type)
209bf215546Sopenharmony_ci{
210bf215546Sopenharmony_ci   return retype(get_nir_dest(dest), type);
211bf215546Sopenharmony_ci}
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_cidst_reg
214bf215546Sopenharmony_civec4_visitor::get_nir_dest(const nir_dest &dest, nir_alu_type type)
215bf215546Sopenharmony_ci{
216bf215546Sopenharmony_ci   return get_nir_dest(dest, brw_type_for_nir_type(devinfo, type));
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_cisrc_reg
220bf215546Sopenharmony_civec4_visitor::get_nir_src(const nir_src &src, enum brw_reg_type type,
221bf215546Sopenharmony_ci                          unsigned num_components)
222bf215546Sopenharmony_ci{
223bf215546Sopenharmony_ci   dst_reg reg;
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   if (src.is_ssa) {
226bf215546Sopenharmony_ci      assert(src.ssa != NULL);
227bf215546Sopenharmony_ci      reg = nir_ssa_values[src.ssa->index];
228bf215546Sopenharmony_ci   }
229bf215546Sopenharmony_ci   else {
230bf215546Sopenharmony_ci      reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
231bf215546Sopenharmony_ci                                src.reg.indirect);
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   reg = retype(reg, type);
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci   src_reg reg_as_src = src_reg(reg);
237bf215546Sopenharmony_ci   reg_as_src.swizzle = brw_swizzle_for_size(num_components);
238bf215546Sopenharmony_ci   return reg_as_src;
239bf215546Sopenharmony_ci}
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_cisrc_reg
242bf215546Sopenharmony_civec4_visitor::get_nir_src(const nir_src &src, nir_alu_type type,
243bf215546Sopenharmony_ci                          unsigned num_components)
244bf215546Sopenharmony_ci{
245bf215546Sopenharmony_ci   return get_nir_src(src, brw_type_for_nir_type(devinfo, type),
246bf215546Sopenharmony_ci                      num_components);
247bf215546Sopenharmony_ci}
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_cisrc_reg
250bf215546Sopenharmony_civec4_visitor::get_nir_src(const nir_src &src, unsigned num_components)
251bf215546Sopenharmony_ci{
252bf215546Sopenharmony_ci   /* if type is not specified, default to signed int */
253bf215546Sopenharmony_ci   return get_nir_src(src, nir_type_int32, num_components);
254bf215546Sopenharmony_ci}
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_cisrc_reg
257bf215546Sopenharmony_civec4_visitor::get_nir_src_imm(const nir_src &src)
258bf215546Sopenharmony_ci{
259bf215546Sopenharmony_ci   assert(nir_src_num_components(src) == 1);
260bf215546Sopenharmony_ci   assert(nir_src_bit_size(src) == 32);
261bf215546Sopenharmony_ci   return nir_src_is_const(src) ? src_reg(brw_imm_d(nir_src_as_int(src))) :
262bf215546Sopenharmony_ci                                  get_nir_src(src, 1);
263bf215546Sopenharmony_ci}
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_cisrc_reg
266bf215546Sopenharmony_civec4_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
267bf215546Sopenharmony_ci{
268bf215546Sopenharmony_ci   nir_src *offset_src = nir_get_io_offset_src(instr);
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   if (nir_src_is_const(*offset_src)) {
271bf215546Sopenharmony_ci      /* The only constant offset we should find is 0.  brw_nir.c's
272bf215546Sopenharmony_ci       * add_const_offset_to_base() will fold other constant offsets
273bf215546Sopenharmony_ci       * into instr->const_index[0].
274bf215546Sopenharmony_ci       */
275bf215546Sopenharmony_ci      assert(nir_src_as_uint(*offset_src) == 0);
276bf215546Sopenharmony_ci      return src_reg();
277bf215546Sopenharmony_ci   }
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   return get_nir_src(*offset_src, BRW_REGISTER_TYPE_UD, 1);
280bf215546Sopenharmony_ci}
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_cistatic src_reg
283bf215546Sopenharmony_cisetup_imm_df(const vec4_builder &bld, double v)
284bf215546Sopenharmony_ci{
285bf215546Sopenharmony_ci   const intel_device_info *devinfo = bld.shader->devinfo;
286bf215546Sopenharmony_ci   assert(devinfo->ver == 7);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   /* gfx7.5 does not support DF immediates straightforward but the DIM
289bf215546Sopenharmony_ci    * instruction allows to set the 64-bit immediate value.
290bf215546Sopenharmony_ci    */
291bf215546Sopenharmony_ci   if (devinfo->verx10 == 75) {
292bf215546Sopenharmony_ci      const vec4_builder ubld = bld.exec_all();
293bf215546Sopenharmony_ci      const dst_reg dst = bld.vgrf(BRW_REGISTER_TYPE_DF);
294bf215546Sopenharmony_ci      ubld.DIM(dst, brw_imm_df(v));
295bf215546Sopenharmony_ci      return swizzle(src_reg(dst), BRW_SWIZZLE_XXXX);
296bf215546Sopenharmony_ci   }
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   /* gfx7 does not support DF immediates */
299bf215546Sopenharmony_ci   union {
300bf215546Sopenharmony_ci      double d;
301bf215546Sopenharmony_ci      struct {
302bf215546Sopenharmony_ci         uint32_t i1;
303bf215546Sopenharmony_ci         uint32_t i2;
304bf215546Sopenharmony_ci      };
305bf215546Sopenharmony_ci   } di;
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   di.d = v;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   /* Write the low 32-bit of the constant to the X:UD channel and the
310bf215546Sopenharmony_ci    * high 32-bit to the Y:UD channel to build the constant in a VGRF.
311bf215546Sopenharmony_ci    * We have to do this twice (offset 0 and offset 1), since a DF VGRF takes
312bf215546Sopenharmony_ci    * two SIMD8 registers in SIMD4x2 execution. Finally, return a swizzle
313bf215546Sopenharmony_ci    * XXXX so any access to the VGRF only reads the constant data in these
314bf215546Sopenharmony_ci    * channels.
315bf215546Sopenharmony_ci    */
316bf215546Sopenharmony_ci   const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
317bf215546Sopenharmony_ci   for (unsigned n = 0; n < 2; n++) {
318bf215546Sopenharmony_ci      const vec4_builder ubld = bld.exec_all().group(4, n);
319bf215546Sopenharmony_ci      ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_X), brw_imm_ud(di.i1));
320bf215546Sopenharmony_ci      ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_Y), brw_imm_ud(di.i2));
321bf215546Sopenharmony_ci   }
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci   return swizzle(src_reg(retype(tmp, BRW_REGISTER_TYPE_DF)), BRW_SWIZZLE_XXXX);
324bf215546Sopenharmony_ci}
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_civoid
327bf215546Sopenharmony_civec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
328bf215546Sopenharmony_ci{
329bf215546Sopenharmony_ci   dst_reg reg;
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   if (instr->def.bit_size == 64) {
332bf215546Sopenharmony_ci      reg = dst_reg(VGRF, alloc.allocate(2));
333bf215546Sopenharmony_ci      reg.type = BRW_REGISTER_TYPE_DF;
334bf215546Sopenharmony_ci   } else {
335bf215546Sopenharmony_ci      reg = dst_reg(VGRF, alloc.allocate(1));
336bf215546Sopenharmony_ci      reg.type = BRW_REGISTER_TYPE_D;
337bf215546Sopenharmony_ci   }
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci   const vec4_builder ibld = vec4_builder(this).at_end();
340bf215546Sopenharmony_ci   unsigned remaining = brw_writemask_for_size(instr->def.num_components);
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   /* @FIXME: consider emitting vector operations to save some MOVs in
343bf215546Sopenharmony_ci    * cases where the components are representable in 8 bits.
344bf215546Sopenharmony_ci    * For now, we emit a MOV for each distinct value.
345bf215546Sopenharmony_ci    */
346bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->def.num_components; i++) {
347bf215546Sopenharmony_ci      unsigned writemask = 1 << i;
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_ci      if ((remaining & writemask) == 0)
350bf215546Sopenharmony_ci         continue;
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci      for (unsigned j = i; j < instr->def.num_components; j++) {
353bf215546Sopenharmony_ci         if ((instr->def.bit_size == 32 &&
354bf215546Sopenharmony_ci              instr->value[i].u32 == instr->value[j].u32) ||
355bf215546Sopenharmony_ci             (instr->def.bit_size == 64 &&
356bf215546Sopenharmony_ci              instr->value[i].f64 == instr->value[j].f64)) {
357bf215546Sopenharmony_ci            writemask |= 1 << j;
358bf215546Sopenharmony_ci         }
359bf215546Sopenharmony_ci      }
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci      reg.writemask = writemask;
362bf215546Sopenharmony_ci      if (instr->def.bit_size == 64) {
363bf215546Sopenharmony_ci         emit(MOV(reg, setup_imm_df(ibld, instr->value[i].f64)));
364bf215546Sopenharmony_ci      } else {
365bf215546Sopenharmony_ci         emit(MOV(reg, brw_imm_d(instr->value[i].i32)));
366bf215546Sopenharmony_ci      }
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci      remaining &= ~writemask;
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   /* Set final writemask */
372bf215546Sopenharmony_ci   reg.writemask = brw_writemask_for_size(instr->def.num_components);
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci   nir_ssa_values[instr->def.index] = reg;
375bf215546Sopenharmony_ci}
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_cisrc_reg
378bf215546Sopenharmony_civec4_visitor::get_nir_ssbo_intrinsic_index(nir_intrinsic_instr *instr)
379bf215546Sopenharmony_ci{
380bf215546Sopenharmony_ci   /* SSBO stores are weird in that their index is in src[1] */
381bf215546Sopenharmony_ci   const unsigned src = instr->intrinsic == nir_intrinsic_store_ssbo ? 1 : 0;
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   if (nir_src_is_const(instr->src[src])) {
384bf215546Sopenharmony_ci      return brw_imm_ud(nir_src_as_uint(instr->src[src]));
385bf215546Sopenharmony_ci   } else {
386bf215546Sopenharmony_ci      return emit_uniformize(get_nir_src(instr->src[src]));
387bf215546Sopenharmony_ci   }
388bf215546Sopenharmony_ci}
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_civoid
391bf215546Sopenharmony_civec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
392bf215546Sopenharmony_ci{
393bf215546Sopenharmony_ci   dst_reg dest;
394bf215546Sopenharmony_ci   src_reg src;
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci   switch (instr->intrinsic) {
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci   case nir_intrinsic_load_input: {
399bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest) == 32);
400bf215546Sopenharmony_ci      /* We set EmitNoIndirectInput for VS */
401bf215546Sopenharmony_ci      unsigned load_offset = nir_src_as_uint(instr->src[0]);
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci      dest = get_nir_dest(instr->dest);
404bf215546Sopenharmony_ci      dest.writemask = brw_writemask_for_size(instr->num_components);
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci      src = src_reg(ATTR, instr->const_index[0] + load_offset,
407bf215546Sopenharmony_ci                    glsl_type::uvec4_type);
408bf215546Sopenharmony_ci      src = retype(src, dest.type);
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci      /* Swizzle source based on component layout qualifier */
411bf215546Sopenharmony_ci      src.swizzle = BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr));
412bf215546Sopenharmony_ci      emit(MOV(dest, src));
413bf215546Sopenharmony_ci      break;
414bf215546Sopenharmony_ci   }
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   case nir_intrinsic_store_output: {
417bf215546Sopenharmony_ci      assert(nir_src_bit_size(instr->src[0]) == 32);
418bf215546Sopenharmony_ci      unsigned store_offset = nir_src_as_uint(instr->src[1]);
419bf215546Sopenharmony_ci      int varying = instr->const_index[0] + store_offset;
420bf215546Sopenharmony_ci      src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
421bf215546Sopenharmony_ci                        instr->num_components);
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci      unsigned c = nir_intrinsic_component(instr);
424bf215546Sopenharmony_ci      output_reg[varying][c] = dst_reg(src);
425bf215546Sopenharmony_ci      output_num_components[varying][c] = instr->num_components;
426bf215546Sopenharmony_ci      break;
427bf215546Sopenharmony_ci   }
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   case nir_intrinsic_get_ssbo_size: {
430bf215546Sopenharmony_ci      assert(nir_src_num_components(instr->src[0]) == 1);
431bf215546Sopenharmony_ci      unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
432bf215546Sopenharmony_ci                            nir_src_as_uint(instr->src[0]) : 0;
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci      dst_reg result_dst = get_nir_dest(instr->dest);
435bf215546Sopenharmony_ci      vec4_instruction *inst = new(mem_ctx)
436bf215546Sopenharmony_ci         vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE, result_dst);
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci      inst->base_mrf = 2;
439bf215546Sopenharmony_ci      inst->mlen = 1; /* always at least one */
440bf215546Sopenharmony_ci      inst->src[1] = brw_imm_ud(ssbo_index);
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_ci      /* MRF for the first parameter */
443bf215546Sopenharmony_ci      src_reg lod = brw_imm_d(0);
444bf215546Sopenharmony_ci      int param_base = inst->base_mrf;
445bf215546Sopenharmony_ci      int writemask = WRITEMASK_X;
446bf215546Sopenharmony_ci      emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci      emit(inst);
449bf215546Sopenharmony_ci      break;
450bf215546Sopenharmony_ci   }
451bf215546Sopenharmony_ci
452bf215546Sopenharmony_ci   case nir_intrinsic_store_ssbo: {
453bf215546Sopenharmony_ci      assert(devinfo->ver == 7);
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci      /* brw_nir_lower_mem_access_bit_sizes takes care of this */
456bf215546Sopenharmony_ci      assert(nir_src_bit_size(instr->src[0]) == 32);
457bf215546Sopenharmony_ci      assert(nir_intrinsic_write_mask(instr) ==
458bf215546Sopenharmony_ci             (1u << instr->num_components) - 1);
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_ci      src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
461bf215546Sopenharmony_ci      src_reg offset_reg = retype(get_nir_src_imm(instr->src[2]),
462bf215546Sopenharmony_ci                                  BRW_REGISTER_TYPE_UD);
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci      /* Value */
465bf215546Sopenharmony_ci      src_reg val_reg = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F, 4);
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci      /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
468bf215546Sopenharmony_ci       * writes will use SIMD8 mode. In order to hide this and keep symmetry across
469bf215546Sopenharmony_ci       * typed and untyped messages and across hardware platforms, the
470bf215546Sopenharmony_ci       * current implementation of the untyped messages will transparently convert
471bf215546Sopenharmony_ci       * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
472bf215546Sopenharmony_ci       * and enabling only channel X on the SEND instruction.
473bf215546Sopenharmony_ci       *
474bf215546Sopenharmony_ci       * The above, works well for full vector writes, but not for partial writes
475bf215546Sopenharmony_ci       * where we want to write some channels and not others, like when we have
476bf215546Sopenharmony_ci       * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
477bf215546Sopenharmony_ci       * quite restrictive with regards to the channel enables we can configure in
478bf215546Sopenharmony_ci       * the message descriptor (not all combinations are allowed) we cannot simply
479bf215546Sopenharmony_ci       * implement these scenarios with a single message while keeping the
480bf215546Sopenharmony_ci       * aforementioned symmetry in the implementation. For now we de decided that
481bf215546Sopenharmony_ci       * it is better to keep the symmetry to reduce complexity, so in situations
482bf215546Sopenharmony_ci       * such as the one described we end up emitting two untyped write messages
483bf215546Sopenharmony_ci       * (one for xy and another for w).
484bf215546Sopenharmony_ci       *
485bf215546Sopenharmony_ci       * The code below packs consecutive channels into a single write message,
486bf215546Sopenharmony_ci       * detects gaps in the vector write and if needed, sends a second message
487bf215546Sopenharmony_ci       * with the remaining channels. If in the future we decide that we want to
488bf215546Sopenharmony_ci       * emit a single message at the expense of losing the symmetry in the
489bf215546Sopenharmony_ci       * implementation we can:
490bf215546Sopenharmony_ci       *
491bf215546Sopenharmony_ci       * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
492bf215546Sopenharmony_ci       *    message payload. In this mode we can write up to 8 offsets and dwords
493bf215546Sopenharmony_ci       *    to the red channel only (for the two vec4s in the SIMD4x2 execution)
494bf215546Sopenharmony_ci       *    and select which of the 8 channels carry data to write by setting the
495bf215546Sopenharmony_ci       *    appropriate writemask in the dst register of the SEND instruction.
496bf215546Sopenharmony_ci       *    It would require to write a new generator opcode specifically for
497bf215546Sopenharmony_ci       *    IvyBridge since we would need to prepare a SIMD8 payload that could
498bf215546Sopenharmony_ci       *    use any channel, not just X.
499bf215546Sopenharmony_ci       *
500bf215546Sopenharmony_ci       * 2) For Haswell+: Simply send a single write message but set the writemask
501bf215546Sopenharmony_ci       *    on the dst of the SEND instruction to select the channels we want to
502bf215546Sopenharmony_ci       *    write. It would require to modify the current messages to receive
503bf215546Sopenharmony_ci       *    and honor the writemask provided.
504bf215546Sopenharmony_ci       */
505bf215546Sopenharmony_ci      const vec4_builder bld = vec4_builder(this).at_end()
506bf215546Sopenharmony_ci                               .annotate(current_annotation, base_ir);
507bf215546Sopenharmony_ci
508bf215546Sopenharmony_ci      emit_untyped_write(bld, surf_index, offset_reg, val_reg,
509bf215546Sopenharmony_ci                         1 /* dims */, instr->num_components /* size */,
510bf215546Sopenharmony_ci                         BRW_PREDICATE_NONE);
511bf215546Sopenharmony_ci      break;
512bf215546Sopenharmony_ci   }
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci   case nir_intrinsic_load_ssbo: {
515bf215546Sopenharmony_ci      assert(devinfo->ver == 7);
516bf215546Sopenharmony_ci
517bf215546Sopenharmony_ci      /* brw_nir_lower_mem_access_bit_sizes takes care of this */
518bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest) == 32);
519bf215546Sopenharmony_ci
520bf215546Sopenharmony_ci      src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
521bf215546Sopenharmony_ci      src_reg offset_reg = retype(get_nir_src_imm(instr->src[1]),
522bf215546Sopenharmony_ci                                  BRW_REGISTER_TYPE_UD);
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci      /* Read the vector */
525bf215546Sopenharmony_ci      const vec4_builder bld = vec4_builder(this).at_end()
526bf215546Sopenharmony_ci         .annotate(current_annotation, base_ir);
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_ci      src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
529bf215546Sopenharmony_ci                                              1 /* dims */, 4 /* size*/,
530bf215546Sopenharmony_ci                                              BRW_PREDICATE_NONE);
531bf215546Sopenharmony_ci      dst_reg dest = get_nir_dest(instr->dest);
532bf215546Sopenharmony_ci      read_result.type = dest.type;
533bf215546Sopenharmony_ci      read_result.swizzle = brw_swizzle_for_size(instr->num_components);
534bf215546Sopenharmony_ci      emit(MOV(dest, read_result));
535bf215546Sopenharmony_ci      break;
536bf215546Sopenharmony_ci   }
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_add:
539bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imin:
540bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umin:
541bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_imax:
542bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_umax:
543bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_and:
544bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_or:
545bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_xor:
546bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_exchange:
547bf215546Sopenharmony_ci   case nir_intrinsic_ssbo_atomic_comp_swap:
548bf215546Sopenharmony_ci      nir_emit_ssbo_atomic(brw_aop_for_nir_intrinsic(instr), instr);
549bf215546Sopenharmony_ci      break;
550bf215546Sopenharmony_ci
551bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id:
552bf215546Sopenharmony_ci      unreachable("should be lowered by lower_vertex_id()");
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id_zero_base:
555bf215546Sopenharmony_ci   case nir_intrinsic_load_base_vertex:
556bf215546Sopenharmony_ci   case nir_intrinsic_load_instance_id:
557bf215546Sopenharmony_ci   case nir_intrinsic_load_base_instance:
558bf215546Sopenharmony_ci   case nir_intrinsic_load_draw_id:
559bf215546Sopenharmony_ci   case nir_intrinsic_load_invocation_id:
560bf215546Sopenharmony_ci      unreachable("should be lowered by brw_nir_lower_vs_inputs()");
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci   case nir_intrinsic_load_uniform: {
563bf215546Sopenharmony_ci      /* Offsets are in bytes but they should always be multiples of 4 */
564bf215546Sopenharmony_ci      assert(nir_intrinsic_base(instr) % 4 == 0);
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci      dest = get_nir_dest(instr->dest);
567bf215546Sopenharmony_ci
568bf215546Sopenharmony_ci      src = src_reg(dst_reg(UNIFORM, nir_intrinsic_base(instr) / 16));
569bf215546Sopenharmony_ci      src.type = dest.type;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci      /* Uniforms don't actually have to be vec4 aligned.  In the case that
572bf215546Sopenharmony_ci       * it isn't, we have to use a swizzle to shift things around.  They
573bf215546Sopenharmony_ci       * do still have the std140 alignment requirement that vec2's have to
574bf215546Sopenharmony_ci       * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
575bf215546Sopenharmony_ci       *
576bf215546Sopenharmony_ci       * The swizzle also works in the indirect case as the generator adds
577bf215546Sopenharmony_ci       * the swizzle to the offset for us.
578bf215546Sopenharmony_ci       */
579bf215546Sopenharmony_ci      const int type_size = type_sz(src.type);
580bf215546Sopenharmony_ci      unsigned shift = (nir_intrinsic_base(instr) % 16) / type_size;
581bf215546Sopenharmony_ci      assert(shift + instr->num_components <= 4);
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci      if (nir_src_is_const(instr->src[0])) {
584bf215546Sopenharmony_ci         const unsigned load_offset = nir_src_as_uint(instr->src[0]);
585bf215546Sopenharmony_ci         /* Offsets are in bytes but they should always be multiples of 4 */
586bf215546Sopenharmony_ci         assert(load_offset % 4 == 0);
587bf215546Sopenharmony_ci
588bf215546Sopenharmony_ci         src.swizzle = brw_swizzle_for_size(instr->num_components);
589bf215546Sopenharmony_ci         dest.writemask = brw_writemask_for_size(instr->num_components);
590bf215546Sopenharmony_ci         unsigned offset = load_offset + shift * type_size;
591bf215546Sopenharmony_ci         src.offset = ROUND_DOWN_TO(offset, 16);
592bf215546Sopenharmony_ci         shift = (offset % 16) / type_size;
593bf215546Sopenharmony_ci         assert(shift + instr->num_components <= 4);
594bf215546Sopenharmony_ci         src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
595bf215546Sopenharmony_ci
596bf215546Sopenharmony_ci         emit(MOV(dest, src));
597bf215546Sopenharmony_ci      } else {
598bf215546Sopenharmony_ci         /* Uniform arrays are vec4 aligned, because of std140 alignment
599bf215546Sopenharmony_ci          * rules.
600bf215546Sopenharmony_ci          */
601bf215546Sopenharmony_ci         assert(shift == 0);
602bf215546Sopenharmony_ci
603bf215546Sopenharmony_ci         src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
604bf215546Sopenharmony_ci
605bf215546Sopenharmony_ci         /* MOV_INDIRECT is going to stomp the whole thing anyway */
606bf215546Sopenharmony_ci         dest.writemask = WRITEMASK_XYZW;
607bf215546Sopenharmony_ci
608bf215546Sopenharmony_ci         emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
609bf215546Sopenharmony_ci              indirect, brw_imm_ud(instr->const_index[1]));
610bf215546Sopenharmony_ci      }
611bf215546Sopenharmony_ci      break;
612bf215546Sopenharmony_ci   }
613bf215546Sopenharmony_ci
614bf215546Sopenharmony_ci   case nir_intrinsic_load_ubo: {
615bf215546Sopenharmony_ci      src_reg surf_index;
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci      dest = get_nir_dest(instr->dest);
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci      if (nir_src_is_const(instr->src[0])) {
620bf215546Sopenharmony_ci         /* The block index is a constant, so just emit the binding table entry
621bf215546Sopenharmony_ci          * as an immediate.
622bf215546Sopenharmony_ci          */
623bf215546Sopenharmony_ci         const unsigned index = nir_src_as_uint(instr->src[0]);
624bf215546Sopenharmony_ci         surf_index = brw_imm_ud(index);
625bf215546Sopenharmony_ci      } else {
626bf215546Sopenharmony_ci         /* The block index is not a constant. Evaluate the index expression
627bf215546Sopenharmony_ci          * per-channel and add the base UBO index; we have to select a value
628bf215546Sopenharmony_ci          * from any live channel.
629bf215546Sopenharmony_ci          */
630bf215546Sopenharmony_ci         surf_index = src_reg(this, glsl_type::uint_type);
631bf215546Sopenharmony_ci         emit(MOV(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
632bf215546Sopenharmony_ci                                                   instr->num_components)));
633bf215546Sopenharmony_ci         surf_index = emit_uniformize(surf_index);
634bf215546Sopenharmony_ci      }
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci      src_reg push_reg;
637bf215546Sopenharmony_ci      src_reg offset_reg;
638bf215546Sopenharmony_ci      if (nir_src_is_const(instr->src[1])) {
639bf215546Sopenharmony_ci         unsigned load_offset = nir_src_as_uint(instr->src[1]);
640bf215546Sopenharmony_ci         unsigned aligned_offset = load_offset & ~15;
641bf215546Sopenharmony_ci         offset_reg = brw_imm_ud(aligned_offset);
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci         /* See if we've selected this as a push constant candidate */
644bf215546Sopenharmony_ci         if (nir_src_is_const(instr->src[0])) {
645bf215546Sopenharmony_ci            const unsigned ubo_block = nir_src_as_uint(instr->src[0]);
646bf215546Sopenharmony_ci            const unsigned offset_256b = aligned_offset / 32;
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci            for (int i = 0; i < 4; i++) {
649bf215546Sopenharmony_ci               const struct brw_ubo_range *range = &prog_data->base.ubo_ranges[i];
650bf215546Sopenharmony_ci               if (range->block == ubo_block &&
651bf215546Sopenharmony_ci                   offset_256b >= range->start &&
652bf215546Sopenharmony_ci                   offset_256b < range->start + range->length) {
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci                  push_reg = src_reg(dst_reg(UNIFORM, UBO_START + i));
655bf215546Sopenharmony_ci                  push_reg.type = dest.type;
656bf215546Sopenharmony_ci                  push_reg.offset = aligned_offset - 32 * range->start;
657bf215546Sopenharmony_ci                  break;
658bf215546Sopenharmony_ci               }
659bf215546Sopenharmony_ci            }
660bf215546Sopenharmony_ci         }
661bf215546Sopenharmony_ci      } else {
662bf215546Sopenharmony_ci         offset_reg = src_reg(this, glsl_type::uint_type);
663bf215546Sopenharmony_ci         emit(MOV(dst_reg(offset_reg),
664bf215546Sopenharmony_ci                  get_nir_src(instr->src[1], nir_type_uint32, 1)));
665bf215546Sopenharmony_ci      }
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci      src_reg packed_consts;
668bf215546Sopenharmony_ci      if (push_reg.file != BAD_FILE) {
669bf215546Sopenharmony_ci         packed_consts = push_reg;
670bf215546Sopenharmony_ci      } else if (nir_dest_bit_size(instr->dest) == 32) {
671bf215546Sopenharmony_ci         packed_consts = src_reg(this, glsl_type::vec4_type);
672bf215546Sopenharmony_ci         emit_pull_constant_load_reg(dst_reg(packed_consts),
673bf215546Sopenharmony_ci                                     surf_index,
674bf215546Sopenharmony_ci                                     offset_reg,
675bf215546Sopenharmony_ci                                     NULL, NULL /* before_block/inst */);
676bf215546Sopenharmony_ci         prog_data->base.has_ubo_pull = true;
677bf215546Sopenharmony_ci      } else {
678bf215546Sopenharmony_ci         src_reg temp = src_reg(this, glsl_type::dvec4_type);
679bf215546Sopenharmony_ci         src_reg temp_float = retype(temp, BRW_REGISTER_TYPE_F);
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci         emit_pull_constant_load_reg(dst_reg(temp_float),
682bf215546Sopenharmony_ci                                     surf_index, offset_reg, NULL, NULL);
683bf215546Sopenharmony_ci         if (offset_reg.file == IMM)
684bf215546Sopenharmony_ci            offset_reg.ud += 16;
685bf215546Sopenharmony_ci         else
686bf215546Sopenharmony_ci            emit(ADD(dst_reg(offset_reg), offset_reg, brw_imm_ud(16u)));
687bf215546Sopenharmony_ci         emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float, REG_SIZE)),
688bf215546Sopenharmony_ci                                     surf_index, offset_reg, NULL, NULL);
689bf215546Sopenharmony_ci         prog_data->base.has_ubo_pull = true;
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci         packed_consts = src_reg(this, glsl_type::dvec4_type);
692bf215546Sopenharmony_ci         shuffle_64bit_data(dst_reg(packed_consts), temp, false);
693bf215546Sopenharmony_ci      }
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci      packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
696bf215546Sopenharmony_ci      if (nir_src_is_const(instr->src[1])) {
697bf215546Sopenharmony_ci         unsigned load_offset = nir_src_as_uint(instr->src[1]);
698bf215546Sopenharmony_ci         unsigned type_size = type_sz(dest.type);
699bf215546Sopenharmony_ci         packed_consts.swizzle +=
700bf215546Sopenharmony_ci            BRW_SWIZZLE4(load_offset % 16 / type_size,
701bf215546Sopenharmony_ci                         load_offset % 16 / type_size,
702bf215546Sopenharmony_ci                         load_offset % 16 / type_size,
703bf215546Sopenharmony_ci                         load_offset % 16 / type_size);
704bf215546Sopenharmony_ci      }
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci      emit(MOV(dest, retype(packed_consts, dest.type)));
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_ci      break;
709bf215546Sopenharmony_ci   }
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_ci   case nir_intrinsic_scoped_barrier:
712bf215546Sopenharmony_ci      assert(nir_intrinsic_execution_scope(instr) == NIR_SCOPE_NONE);
713bf215546Sopenharmony_ci      FALLTHROUGH;
714bf215546Sopenharmony_ci   case nir_intrinsic_memory_barrier: {
715bf215546Sopenharmony_ci      const vec4_builder bld =
716bf215546Sopenharmony_ci         vec4_builder(this).at_end().annotate(current_annotation, base_ir);
717bf215546Sopenharmony_ci      const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
718bf215546Sopenharmony_ci      vec4_instruction *fence =
719bf215546Sopenharmony_ci         bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp, brw_vec8_grf(0, 0));
720bf215546Sopenharmony_ci      fence->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;
721bf215546Sopenharmony_ci      break;
722bf215546Sopenharmony_ci   }
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_ci   case nir_intrinsic_shader_clock: {
725bf215546Sopenharmony_ci      /* We cannot do anything if there is an event, so ignore it for now */
726bf215546Sopenharmony_ci      const src_reg shader_clock = get_timestamp();
727bf215546Sopenharmony_ci      const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
728bf215546Sopenharmony_ci
729bf215546Sopenharmony_ci      dest = get_nir_dest(instr->dest, type);
730bf215546Sopenharmony_ci      emit(MOV(dest, shader_clock));
731bf215546Sopenharmony_ci      break;
732bf215546Sopenharmony_ci   }
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci   default:
735bf215546Sopenharmony_ci      unreachable("Unknown intrinsic");
736bf215546Sopenharmony_ci   }
737bf215546Sopenharmony_ci}
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_civoid
740bf215546Sopenharmony_civec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
741bf215546Sopenharmony_ci{
742bf215546Sopenharmony_ci   dst_reg dest;
743bf215546Sopenharmony_ci   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
744bf215546Sopenharmony_ci      dest = get_nir_dest(instr->dest);
745bf215546Sopenharmony_ci
746bf215546Sopenharmony_ci   src_reg surface = get_nir_ssbo_intrinsic_index(instr);
747bf215546Sopenharmony_ci   src_reg offset = get_nir_src(instr->src[1], 1);
748bf215546Sopenharmony_ci   src_reg data1;
749bf215546Sopenharmony_ci   if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
750bf215546Sopenharmony_ci      data1 = get_nir_src(instr->src[2], 1);
751bf215546Sopenharmony_ci   src_reg data2;
752bf215546Sopenharmony_ci   if (op == BRW_AOP_CMPWR)
753bf215546Sopenharmony_ci      data2 = get_nir_src(instr->src[3], 1);
754bf215546Sopenharmony_ci
755bf215546Sopenharmony_ci   /* Emit the actual atomic operation operation */
756bf215546Sopenharmony_ci   const vec4_builder bld =
757bf215546Sopenharmony_ci      vec4_builder(this).at_end().annotate(current_annotation, base_ir);
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci   src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
760bf215546Sopenharmony_ci                                               data1, data2,
761bf215546Sopenharmony_ci                                               1 /* dims */, 1 /* rsize */,
762bf215546Sopenharmony_ci                                               op,
763bf215546Sopenharmony_ci                                               BRW_PREDICATE_NONE);
764bf215546Sopenharmony_ci   dest.type = atomic_result.type;
765bf215546Sopenharmony_ci   bld.MOV(dest, atomic_result);
766bf215546Sopenharmony_ci}
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_cistatic unsigned
769bf215546Sopenharmony_cibrw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
770bf215546Sopenharmony_ci{
771bf215546Sopenharmony_ci   return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
772bf215546Sopenharmony_ci}
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_cibool
775bf215546Sopenharmony_civec4_visitor::optimize_predicate(nir_alu_instr *instr,
776bf215546Sopenharmony_ci                                 enum brw_predicate *predicate)
777bf215546Sopenharmony_ci{
778bf215546Sopenharmony_ci   if (!instr->src[0].src.is_ssa ||
779bf215546Sopenharmony_ci       instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
780bf215546Sopenharmony_ci      return false;
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_ci   nir_alu_instr *cmp_instr =
783bf215546Sopenharmony_ci      nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci   switch (cmp_instr->op) {
786bf215546Sopenharmony_ci   case nir_op_b32any_fnequal2:
787bf215546Sopenharmony_ci   case nir_op_b32any_inequal2:
788bf215546Sopenharmony_ci   case nir_op_b32any_fnequal3:
789bf215546Sopenharmony_ci   case nir_op_b32any_inequal3:
790bf215546Sopenharmony_ci   case nir_op_b32any_fnequal4:
791bf215546Sopenharmony_ci   case nir_op_b32any_inequal4:
792bf215546Sopenharmony_ci      *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
793bf215546Sopenharmony_ci      break;
794bf215546Sopenharmony_ci   case nir_op_b32all_fequal2:
795bf215546Sopenharmony_ci   case nir_op_b32all_iequal2:
796bf215546Sopenharmony_ci   case nir_op_b32all_fequal3:
797bf215546Sopenharmony_ci   case nir_op_b32all_iequal3:
798bf215546Sopenharmony_ci   case nir_op_b32all_fequal4:
799bf215546Sopenharmony_ci   case nir_op_b32all_iequal4:
800bf215546Sopenharmony_ci      *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
801bf215546Sopenharmony_ci      break;
802bf215546Sopenharmony_ci   default:
803bf215546Sopenharmony_ci      return false;
804bf215546Sopenharmony_ci   }
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_ci   unsigned size_swizzle =
807bf215546Sopenharmony_ci      brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci   src_reg op[2];
810bf215546Sopenharmony_ci   assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
811bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
812bf215546Sopenharmony_ci      nir_alu_type type = nir_op_infos[cmp_instr->op].input_types[i];
813bf215546Sopenharmony_ci      unsigned bit_size = nir_src_bit_size(cmp_instr->src[i].src);
814bf215546Sopenharmony_ci      type = (nir_alu_type) (((unsigned) type) | bit_size);
815bf215546Sopenharmony_ci      op[i] = get_nir_src(cmp_instr->src[i].src, type, 4);
816bf215546Sopenharmony_ci      unsigned base_swizzle =
817bf215546Sopenharmony_ci         brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
818bf215546Sopenharmony_ci      op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
819bf215546Sopenharmony_ci   }
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_ci   emit(CMP(dst_null_d(), op[0], op[1],
822bf215546Sopenharmony_ci            brw_cmod_for_nir_comparison(cmp_instr->op)));
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci   return true;
825bf215546Sopenharmony_ci}
826bf215546Sopenharmony_ci
827bf215546Sopenharmony_cistatic void
828bf215546Sopenharmony_ciemit_find_msb_using_lzd(const vec4_builder &bld,
829bf215546Sopenharmony_ci                        const dst_reg &dst,
830bf215546Sopenharmony_ci                        const src_reg &src,
831bf215546Sopenharmony_ci                        bool is_signed)
832bf215546Sopenharmony_ci{
833bf215546Sopenharmony_ci   vec4_instruction *inst;
834bf215546Sopenharmony_ci   src_reg temp = src;
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci   if (is_signed) {
837bf215546Sopenharmony_ci      /* LZD of an absolute value source almost always does the right
838bf215546Sopenharmony_ci       * thing.  There are two problem values:
839bf215546Sopenharmony_ci       *
840bf215546Sopenharmony_ci       * * 0x80000000.  Since abs(0x80000000) == 0x80000000, LZD returns
841bf215546Sopenharmony_ci       *   0.  However, findMSB(int(0x80000000)) == 30.
842bf215546Sopenharmony_ci       *
843bf215546Sopenharmony_ci       * * 0xffffffff.  Since abs(0xffffffff) == 1, LZD returns
844bf215546Sopenharmony_ci       *   31.  Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
845bf215546Sopenharmony_ci       *
846bf215546Sopenharmony_ci       *    For a value of zero or negative one, -1 will be returned.
847bf215546Sopenharmony_ci       *
848bf215546Sopenharmony_ci       * * Negative powers of two.  LZD(abs(-(1<<x))) returns x, but
849bf215546Sopenharmony_ci       *   findMSB(-(1<<x)) should return x-1.
850bf215546Sopenharmony_ci       *
851bf215546Sopenharmony_ci       * For all negative number cases, including 0x80000000 and
852bf215546Sopenharmony_ci       * 0xffffffff, the correct value is obtained from LZD if instead of
853bf215546Sopenharmony_ci       * negating the (already negative) value the logical-not is used.  A
854bf215546Sopenharmony_ci       * conditional logical-not can be achieved in two instructions.
855bf215546Sopenharmony_ci       */
856bf215546Sopenharmony_ci      temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
857bf215546Sopenharmony_ci
858bf215546Sopenharmony_ci      bld.ASR(dst_reg(temp), src, brw_imm_d(31));
859bf215546Sopenharmony_ci      bld.XOR(dst_reg(temp), temp, src);
860bf215546Sopenharmony_ci   }
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci   bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
863bf215546Sopenharmony_ci           retype(temp, BRW_REGISTER_TYPE_UD));
864bf215546Sopenharmony_ci
865bf215546Sopenharmony_ci   /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
866bf215546Sopenharmony_ci    * from the LSB side. Subtract the result from 31 to convert the MSB count
867bf215546Sopenharmony_ci    * into an LSB count.  If no bits are set, LZD will return 32.  31-32 = -1,
868bf215546Sopenharmony_ci    * which is exactly what findMSB() is supposed to return.
869bf215546Sopenharmony_ci    */
870bf215546Sopenharmony_ci   inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
871bf215546Sopenharmony_ci                  brw_imm_d(31));
872bf215546Sopenharmony_ci   inst->src[0].negate = true;
873bf215546Sopenharmony_ci}
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_civoid
876bf215546Sopenharmony_civec4_visitor::emit_conversion_from_double(dst_reg dst, src_reg src)
877bf215546Sopenharmony_ci{
878bf215546Sopenharmony_ci   enum opcode op;
879bf215546Sopenharmony_ci   switch (dst.type) {
880bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_D:
881bf215546Sopenharmony_ci      op = VEC4_OPCODE_DOUBLE_TO_D32;
882bf215546Sopenharmony_ci      break;
883bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_UD:
884bf215546Sopenharmony_ci      op = VEC4_OPCODE_DOUBLE_TO_U32;
885bf215546Sopenharmony_ci      break;
886bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_F:
887bf215546Sopenharmony_ci      op = VEC4_OPCODE_DOUBLE_TO_F32;
888bf215546Sopenharmony_ci      break;
889bf215546Sopenharmony_ci   default:
890bf215546Sopenharmony_ci      unreachable("Unknown conversion");
891bf215546Sopenharmony_ci   }
892bf215546Sopenharmony_ci
893bf215546Sopenharmony_ci   dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
894bf215546Sopenharmony_ci   emit(MOV(temp, src));
895bf215546Sopenharmony_ci   dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
896bf215546Sopenharmony_ci   emit(op, temp2, src_reg(temp));
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci   emit(VEC4_OPCODE_PICK_LOW_32BIT, retype(temp2, dst.type), src_reg(temp2));
899bf215546Sopenharmony_ci   emit(MOV(dst, src_reg(retype(temp2, dst.type))));
900bf215546Sopenharmony_ci}
901bf215546Sopenharmony_ci
902bf215546Sopenharmony_civoid
903bf215546Sopenharmony_civec4_visitor::emit_conversion_to_double(dst_reg dst, src_reg src)
904bf215546Sopenharmony_ci{
905bf215546Sopenharmony_ci   dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
906bf215546Sopenharmony_ci   src_reg tmp_src = retype(src_reg(this, glsl_type::vec4_type), src.type);
907bf215546Sopenharmony_ci   emit(MOV(dst_reg(tmp_src), src));
908bf215546Sopenharmony_ci   emit(VEC4_OPCODE_TO_DOUBLE, tmp_dst, tmp_src);
909bf215546Sopenharmony_ci   emit(MOV(dst, src_reg(tmp_dst)));
910bf215546Sopenharmony_ci}
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_ci/**
913bf215546Sopenharmony_ci * Try to use an immediate value for a source
914bf215546Sopenharmony_ci *
915bf215546Sopenharmony_ci * In cases of flow control, constant propagation is sometimes unable to
916bf215546Sopenharmony_ci * determine that a register contains a constant value.  To work around this,
917bf215546Sopenharmony_ci * try to emit a literal as one of the sources.  If \c try_src0_also is set,
918bf215546Sopenharmony_ci * \c op[0] will also be tried for an immediate value.
919bf215546Sopenharmony_ci *
920bf215546Sopenharmony_ci * If \c op[0] is modified, the operands will be exchanged so that \c op[1]
921bf215546Sopenharmony_ci * will always be the immediate value.
922bf215546Sopenharmony_ci *
923bf215546Sopenharmony_ci * \return The index of the source that was modified, 0 or 1, if successful.
924bf215546Sopenharmony_ci * Otherwise, -1.
925bf215546Sopenharmony_ci *
926bf215546Sopenharmony_ci * \param op - Operands to the instruction
927bf215546Sopenharmony_ci * \param try_src0_also - True if \c op[0] should also be a candidate for
928bf215546Sopenharmony_ci *                        getting an immediate value.  This should only be set
929bf215546Sopenharmony_ci *                        for commutative operations.
930bf215546Sopenharmony_ci */
931bf215546Sopenharmony_cistatic int
932bf215546Sopenharmony_citry_immediate_source(const nir_alu_instr *instr, src_reg *op,
933bf215546Sopenharmony_ci                     bool try_src0_also)
934bf215546Sopenharmony_ci{
935bf215546Sopenharmony_ci   unsigned idx;
936bf215546Sopenharmony_ci
937bf215546Sopenharmony_ci   /* MOV should be the only single-source instruction passed to this
938bf215546Sopenharmony_ci    * function.  Any other unary instruction with a constant source should
939bf215546Sopenharmony_ci    * have been constant-folded away!
940bf215546Sopenharmony_ci    */
941bf215546Sopenharmony_ci   assert(nir_op_infos[instr->op].num_inputs > 1 ||
942bf215546Sopenharmony_ci          instr->op == nir_op_mov);
943bf215546Sopenharmony_ci
944bf215546Sopenharmony_ci   if (instr->op != nir_op_mov &&
945bf215546Sopenharmony_ci       nir_src_bit_size(instr->src[1].src) == 32 &&
946bf215546Sopenharmony_ci       nir_src_is_const(instr->src[1].src)) {
947bf215546Sopenharmony_ci      idx = 1;
948bf215546Sopenharmony_ci   } else if (try_src0_also &&
949bf215546Sopenharmony_ci         nir_src_bit_size(instr->src[0].src) == 32 &&
950bf215546Sopenharmony_ci         nir_src_is_const(instr->src[0].src)) {
951bf215546Sopenharmony_ci      idx = 0;
952bf215546Sopenharmony_ci   } else {
953bf215546Sopenharmony_ci      return -1;
954bf215546Sopenharmony_ci   }
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci   const enum brw_reg_type old_type = op[idx].type;
957bf215546Sopenharmony_ci
958bf215546Sopenharmony_ci   switch (old_type) {
959bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_D:
960bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_UD: {
961bf215546Sopenharmony_ci      int first_comp = -1;
962bf215546Sopenharmony_ci      int d = 0;
963bf215546Sopenharmony_ci
964bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
965bf215546Sopenharmony_ci         if (nir_alu_instr_channel_used(instr, idx, i)) {
966bf215546Sopenharmony_ci            if (first_comp < 0) {
967bf215546Sopenharmony_ci               first_comp = i;
968bf215546Sopenharmony_ci               d = nir_src_comp_as_int(instr->src[idx].src,
969bf215546Sopenharmony_ci                                       instr->src[idx].swizzle[i]);
970bf215546Sopenharmony_ci            } else if (d != nir_src_comp_as_int(instr->src[idx].src,
971bf215546Sopenharmony_ci                                                instr->src[idx].swizzle[i])) {
972bf215546Sopenharmony_ci               return -1;
973bf215546Sopenharmony_ci            }
974bf215546Sopenharmony_ci         }
975bf215546Sopenharmony_ci      }
976bf215546Sopenharmony_ci
977bf215546Sopenharmony_ci      assert(first_comp >= 0);
978bf215546Sopenharmony_ci
979bf215546Sopenharmony_ci      if (op[idx].abs)
980bf215546Sopenharmony_ci         d = MAX2(-d, d);
981bf215546Sopenharmony_ci
982bf215546Sopenharmony_ci      if (op[idx].negate)
983bf215546Sopenharmony_ci         d = -d;
984bf215546Sopenharmony_ci
985bf215546Sopenharmony_ci      op[idx] = retype(src_reg(brw_imm_d(d)), old_type);
986bf215546Sopenharmony_ci      break;
987bf215546Sopenharmony_ci   }
988bf215546Sopenharmony_ci
989bf215546Sopenharmony_ci   case BRW_REGISTER_TYPE_F: {
990bf215546Sopenharmony_ci      int first_comp = -1;
991bf215546Sopenharmony_ci      float f[NIR_MAX_VEC_COMPONENTS] = { 0.0f };
992bf215546Sopenharmony_ci      bool is_scalar = true;
993bf215546Sopenharmony_ci
994bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
995bf215546Sopenharmony_ci         if (nir_alu_instr_channel_used(instr, idx, i)) {
996bf215546Sopenharmony_ci            f[i] = nir_src_comp_as_float(instr->src[idx].src,
997bf215546Sopenharmony_ci                                         instr->src[idx].swizzle[i]);
998bf215546Sopenharmony_ci            if (first_comp < 0) {
999bf215546Sopenharmony_ci               first_comp = i;
1000bf215546Sopenharmony_ci            } else if (f[first_comp] != f[i]) {
1001bf215546Sopenharmony_ci               is_scalar = false;
1002bf215546Sopenharmony_ci            }
1003bf215546Sopenharmony_ci         }
1004bf215546Sopenharmony_ci      }
1005bf215546Sopenharmony_ci
1006bf215546Sopenharmony_ci      if (is_scalar) {
1007bf215546Sopenharmony_ci         if (op[idx].abs)
1008bf215546Sopenharmony_ci            f[first_comp] = fabs(f[first_comp]);
1009bf215546Sopenharmony_ci
1010bf215546Sopenharmony_ci         if (op[idx].negate)
1011bf215546Sopenharmony_ci            f[first_comp] = -f[first_comp];
1012bf215546Sopenharmony_ci
1013bf215546Sopenharmony_ci         op[idx] = src_reg(brw_imm_f(f[first_comp]));
1014bf215546Sopenharmony_ci         assert(op[idx].type == old_type);
1015bf215546Sopenharmony_ci      } else {
1016bf215546Sopenharmony_ci         uint8_t vf_values[4] = { 0, 0, 0, 0 };
1017bf215546Sopenharmony_ci
1018bf215546Sopenharmony_ci         for (unsigned i = 0; i < ARRAY_SIZE(vf_values); i++) {
1019bf215546Sopenharmony_ci
1020bf215546Sopenharmony_ci            if (op[idx].abs)
1021bf215546Sopenharmony_ci               f[i] = fabs(f[i]);
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci            if (op[idx].negate)
1024bf215546Sopenharmony_ci               f[i] = -f[i];
1025bf215546Sopenharmony_ci
1026bf215546Sopenharmony_ci            const int vf = brw_float_to_vf(f[i]);
1027bf215546Sopenharmony_ci            if (vf == -1)
1028bf215546Sopenharmony_ci               return -1;
1029bf215546Sopenharmony_ci
1030bf215546Sopenharmony_ci            vf_values[i] = vf;
1031bf215546Sopenharmony_ci         }
1032bf215546Sopenharmony_ci
1033bf215546Sopenharmony_ci         op[idx] = src_reg(brw_imm_vf4(vf_values[0], vf_values[1],
1034bf215546Sopenharmony_ci                                       vf_values[2], vf_values[3]));
1035bf215546Sopenharmony_ci      }
1036bf215546Sopenharmony_ci      break;
1037bf215546Sopenharmony_ci   }
1038bf215546Sopenharmony_ci
1039bf215546Sopenharmony_ci   default:
1040bf215546Sopenharmony_ci      unreachable("Non-32bit type.");
1041bf215546Sopenharmony_ci   }
1042bf215546Sopenharmony_ci
1043bf215546Sopenharmony_ci   /* If the instruction has more than one source, the instruction format only
1044bf215546Sopenharmony_ci    * allows source 1 to be an immediate value.  If the immediate value was
1045bf215546Sopenharmony_ci    * source 0, then the sources must be exchanged.
1046bf215546Sopenharmony_ci    */
1047bf215546Sopenharmony_ci   if (idx == 0 && instr->op != nir_op_mov) {
1048bf215546Sopenharmony_ci      src_reg tmp = op[0];
1049bf215546Sopenharmony_ci      op[0] = op[1];
1050bf215546Sopenharmony_ci      op[1] = tmp;
1051bf215546Sopenharmony_ci   }
1052bf215546Sopenharmony_ci
1053bf215546Sopenharmony_ci   return idx;
1054bf215546Sopenharmony_ci}
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_civoid
1057bf215546Sopenharmony_civec4_visitor::fix_float_operands(src_reg op[3], nir_alu_instr *instr)
1058bf215546Sopenharmony_ci{
1059bf215546Sopenharmony_ci   bool fixed[3] = { false, false, false };
1060bf215546Sopenharmony_ci
1061bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
1062bf215546Sopenharmony_ci      if (!nir_src_is_const(instr->src[i].src))
1063bf215546Sopenharmony_ci         continue;
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_ci      for (unsigned j = i + 1; j < 3; j++) {
1066bf215546Sopenharmony_ci         if (fixed[j])
1067bf215546Sopenharmony_ci            continue;
1068bf215546Sopenharmony_ci
1069bf215546Sopenharmony_ci         if (!nir_src_is_const(instr->src[j].src))
1070bf215546Sopenharmony_ci            continue;
1071bf215546Sopenharmony_ci
1072bf215546Sopenharmony_ci         if (nir_alu_srcs_equal(instr, instr, i, j)) {
1073bf215546Sopenharmony_ci            if (!fixed[i])
1074bf215546Sopenharmony_ci               op[i] = fix_3src_operand(op[i]);
1075bf215546Sopenharmony_ci
1076bf215546Sopenharmony_ci            op[j] = op[i];
1077bf215546Sopenharmony_ci
1078bf215546Sopenharmony_ci            fixed[i] = true;
1079bf215546Sopenharmony_ci            fixed[j] = true;
1080bf215546Sopenharmony_ci         } else if (nir_alu_srcs_negative_equal(instr, instr, i, j)) {
1081bf215546Sopenharmony_ci            if (!fixed[i])
1082bf215546Sopenharmony_ci               op[i] = fix_3src_operand(op[i]);
1083bf215546Sopenharmony_ci
1084bf215546Sopenharmony_ci            op[j] = op[i];
1085bf215546Sopenharmony_ci            op[j].negate = !op[j].negate;
1086bf215546Sopenharmony_ci
1087bf215546Sopenharmony_ci            fixed[i] = true;
1088bf215546Sopenharmony_ci            fixed[j] = true;
1089bf215546Sopenharmony_ci         }
1090bf215546Sopenharmony_ci      }
1091bf215546Sopenharmony_ci   }
1092bf215546Sopenharmony_ci
1093bf215546Sopenharmony_ci   for (unsigned i = 0; i < 3; i++) {
1094bf215546Sopenharmony_ci      if (!fixed[i])
1095bf215546Sopenharmony_ci         op[i] = fix_3src_operand(op[i]);
1096bf215546Sopenharmony_ci   }
1097bf215546Sopenharmony_ci}
1098bf215546Sopenharmony_ci
1099bf215546Sopenharmony_cistatic bool
1100bf215546Sopenharmony_ciconst_src_fits_in_16_bits(const nir_src &src, brw_reg_type type)
1101bf215546Sopenharmony_ci{
1102bf215546Sopenharmony_ci   assert(nir_src_is_const(src));
1103bf215546Sopenharmony_ci   if (brw_reg_type_is_unsigned_integer(type)) {
1104bf215546Sopenharmony_ci      return nir_src_comp_as_uint(src, 0) <= UINT16_MAX;
1105bf215546Sopenharmony_ci   } else {
1106bf215546Sopenharmony_ci      const int64_t c = nir_src_comp_as_int(src, 0);
1107bf215546Sopenharmony_ci      return c <= INT16_MAX && c >= INT16_MIN;
1108bf215546Sopenharmony_ci   }
1109bf215546Sopenharmony_ci}
1110bf215546Sopenharmony_ci
1111bf215546Sopenharmony_civoid
1112bf215546Sopenharmony_civec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1113bf215546Sopenharmony_ci{
1114bf215546Sopenharmony_ci   vec4_instruction *inst;
1115bf215546Sopenharmony_ci
1116bf215546Sopenharmony_ci   nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
1117bf215546Sopenharmony_ci                                           nir_dest_bit_size(instr->dest.dest));
1118bf215546Sopenharmony_ci   dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
1119bf215546Sopenharmony_ci   dst.writemask = instr->dest.write_mask;
1120bf215546Sopenharmony_ci
1121bf215546Sopenharmony_ci   assert(!instr->dest.saturate);
1122bf215546Sopenharmony_ci
1123bf215546Sopenharmony_ci   src_reg op[4];
1124bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1125bf215546Sopenharmony_ci      /* We don't lower to source modifiers, so they shouldn't exist. */
1126bf215546Sopenharmony_ci      assert(!instr->src[i].abs);
1127bf215546Sopenharmony_ci      assert(!instr->src[i].negate);
1128bf215546Sopenharmony_ci
1129bf215546Sopenharmony_ci      nir_alu_type src_type = (nir_alu_type)
1130bf215546Sopenharmony_ci         (nir_op_infos[instr->op].input_types[i] |
1131bf215546Sopenharmony_ci          nir_src_bit_size(instr->src[i].src));
1132bf215546Sopenharmony_ci      op[i] = get_nir_src(instr->src[i].src, src_type, 4);
1133bf215546Sopenharmony_ci      op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1134bf215546Sopenharmony_ci   }
1135bf215546Sopenharmony_ci
1136bf215546Sopenharmony_ci#ifndef NDEBUG
1137bf215546Sopenharmony_ci   /* On Gen7 and earlier, no functionality is exposed that should allow 8-bit
1138bf215546Sopenharmony_ci    * integer types to ever exist.
1139bf215546Sopenharmony_ci    */
1140bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1141bf215546Sopenharmony_ci      assert(type_sz(op[i].type) > 1);
1142bf215546Sopenharmony_ci#endif
1143bf215546Sopenharmony_ci
1144bf215546Sopenharmony_ci   switch (instr->op) {
1145bf215546Sopenharmony_ci   case nir_op_mov:
1146bf215546Sopenharmony_ci      try_immediate_source(instr, &op[0], true);
1147bf215546Sopenharmony_ci      inst = emit(MOV(dst, op[0]));
1148bf215546Sopenharmony_ci      break;
1149bf215546Sopenharmony_ci
1150bf215546Sopenharmony_ci   case nir_op_vec2:
1151bf215546Sopenharmony_ci   case nir_op_vec3:
1152bf215546Sopenharmony_ci   case nir_op_vec4:
1153bf215546Sopenharmony_ci      unreachable("not reached: should be handled by lower_vec_to_movs()");
1154bf215546Sopenharmony_ci
1155bf215546Sopenharmony_ci   case nir_op_i2f32:
1156bf215546Sopenharmony_ci   case nir_op_u2f32:
1157bf215546Sopenharmony_ci      inst = emit(MOV(dst, op[0]));
1158bf215546Sopenharmony_ci      break;
1159bf215546Sopenharmony_ci
1160bf215546Sopenharmony_ci   case nir_op_f2f32:
1161bf215546Sopenharmony_ci   case nir_op_f2i32:
1162bf215546Sopenharmony_ci   case nir_op_f2u32:
1163bf215546Sopenharmony_ci      if (nir_src_bit_size(instr->src[0].src) == 64)
1164bf215546Sopenharmony_ci         emit_conversion_from_double(dst, op[0]);
1165bf215546Sopenharmony_ci      else
1166bf215546Sopenharmony_ci         inst = emit(MOV(dst, op[0]));
1167bf215546Sopenharmony_ci      break;
1168bf215546Sopenharmony_ci
1169bf215546Sopenharmony_ci   case nir_op_f2f64:
1170bf215546Sopenharmony_ci   case nir_op_i2f64:
1171bf215546Sopenharmony_ci   case nir_op_u2f64:
1172bf215546Sopenharmony_ci      emit_conversion_to_double(dst, op[0]);
1173bf215546Sopenharmony_ci      break;
1174bf215546Sopenharmony_ci
1175bf215546Sopenharmony_ci   case nir_op_fsat:
1176bf215546Sopenharmony_ci      inst = emit(MOV(dst, op[0]));
1177bf215546Sopenharmony_ci      inst->saturate = true;
1178bf215546Sopenharmony_ci      break;
1179bf215546Sopenharmony_ci
1180bf215546Sopenharmony_ci   case nir_op_fneg:
1181bf215546Sopenharmony_ci   case nir_op_ineg:
1182bf215546Sopenharmony_ci      op[0].negate = true;
1183bf215546Sopenharmony_ci      inst = emit(MOV(dst, op[0]));
1184bf215546Sopenharmony_ci      break;
1185bf215546Sopenharmony_ci
1186bf215546Sopenharmony_ci   case nir_op_fabs:
1187bf215546Sopenharmony_ci   case nir_op_iabs:
1188bf215546Sopenharmony_ci      op[0].negate = false;
1189bf215546Sopenharmony_ci      op[0].abs = true;
1190bf215546Sopenharmony_ci      inst = emit(MOV(dst, op[0]));
1191bf215546Sopenharmony_ci      break;
1192bf215546Sopenharmony_ci
1193bf215546Sopenharmony_ci   case nir_op_iadd:
1194bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1195bf215546Sopenharmony_ci      FALLTHROUGH;
1196bf215546Sopenharmony_ci   case nir_op_fadd:
1197bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1198bf215546Sopenharmony_ci      inst = emit(ADD(dst, op[0], op[1]));
1199bf215546Sopenharmony_ci      break;
1200bf215546Sopenharmony_ci
1201bf215546Sopenharmony_ci   case nir_op_uadd_sat:
1202bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1203bf215546Sopenharmony_ci      inst = emit(ADD(dst, op[0], op[1]));
1204bf215546Sopenharmony_ci      inst->saturate = true;
1205bf215546Sopenharmony_ci      break;
1206bf215546Sopenharmony_ci
1207bf215546Sopenharmony_ci   case nir_op_fmul:
1208bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1209bf215546Sopenharmony_ci      inst = emit(MUL(dst, op[0], op[1]));
1210bf215546Sopenharmony_ci      break;
1211bf215546Sopenharmony_ci
1212bf215546Sopenharmony_ci   case nir_op_imul: {
1213bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1214bf215546Sopenharmony_ci
1215bf215546Sopenharmony_ci      /* For integer multiplication, the MUL uses the low 16 bits of one of
1216bf215546Sopenharmony_ci       * the operands (src0 through SNB, src1 on IVB and later). The MACH
1217bf215546Sopenharmony_ci       * accumulates in the contribution of the upper 16 bits of that
1218bf215546Sopenharmony_ci       * operand. If we can determine that one of the args is in the low
1219bf215546Sopenharmony_ci       * 16 bits, though, we can just emit a single MUL.
1220bf215546Sopenharmony_ci       */
1221bf215546Sopenharmony_ci      if (nir_src_is_const(instr->src[0].src) &&
1222bf215546Sopenharmony_ci          nir_alu_instr_src_read_mask(instr, 0) == 1 &&
1223bf215546Sopenharmony_ci          const_src_fits_in_16_bits(instr->src[0].src, op[0].type)) {
1224bf215546Sopenharmony_ci         if (devinfo->ver < 7)
1225bf215546Sopenharmony_ci            emit(MUL(dst, op[0], op[1]));
1226bf215546Sopenharmony_ci         else
1227bf215546Sopenharmony_ci            emit(MUL(dst, op[1], op[0]));
1228bf215546Sopenharmony_ci      } else if (nir_src_is_const(instr->src[1].src) &&
1229bf215546Sopenharmony_ci                 nir_alu_instr_src_read_mask(instr, 1) == 1 &&
1230bf215546Sopenharmony_ci                 const_src_fits_in_16_bits(instr->src[1].src, op[1].type)) {
1231bf215546Sopenharmony_ci         if (devinfo->ver < 7)
1232bf215546Sopenharmony_ci            emit(MUL(dst, op[1], op[0]));
1233bf215546Sopenharmony_ci         else
1234bf215546Sopenharmony_ci            emit(MUL(dst, op[0], op[1]));
1235bf215546Sopenharmony_ci      } else {
1236bf215546Sopenharmony_ci         struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1237bf215546Sopenharmony_ci
1238bf215546Sopenharmony_ci         emit(MUL(acc, op[0], op[1]));
1239bf215546Sopenharmony_ci         emit(MACH(dst_null_d(), op[0], op[1]));
1240bf215546Sopenharmony_ci         emit(MOV(dst, src_reg(acc)));
1241bf215546Sopenharmony_ci      }
1242bf215546Sopenharmony_ci      break;
1243bf215546Sopenharmony_ci   }
1244bf215546Sopenharmony_ci
1245bf215546Sopenharmony_ci   case nir_op_imul_high:
1246bf215546Sopenharmony_ci   case nir_op_umul_high: {
1247bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1248bf215546Sopenharmony_ci      struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1249bf215546Sopenharmony_ci
1250bf215546Sopenharmony_ci      emit(MUL(acc, op[0], op[1]));
1251bf215546Sopenharmony_ci      emit(MACH(dst, op[0], op[1]));
1252bf215546Sopenharmony_ci      break;
1253bf215546Sopenharmony_ci   }
1254bf215546Sopenharmony_ci
1255bf215546Sopenharmony_ci   case nir_op_frcp:
1256bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1257bf215546Sopenharmony_ci      break;
1258bf215546Sopenharmony_ci
1259bf215546Sopenharmony_ci   case nir_op_fexp2:
1260bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1261bf215546Sopenharmony_ci      break;
1262bf215546Sopenharmony_ci
1263bf215546Sopenharmony_ci   case nir_op_flog2:
1264bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1265bf215546Sopenharmony_ci      break;
1266bf215546Sopenharmony_ci
1267bf215546Sopenharmony_ci   case nir_op_fsin:
1268bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1269bf215546Sopenharmony_ci      break;
1270bf215546Sopenharmony_ci
1271bf215546Sopenharmony_ci   case nir_op_fcos:
1272bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1273bf215546Sopenharmony_ci      break;
1274bf215546Sopenharmony_ci
1275bf215546Sopenharmony_ci   case nir_op_idiv:
1276bf215546Sopenharmony_ci   case nir_op_udiv:
1277bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1278bf215546Sopenharmony_ci      emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1279bf215546Sopenharmony_ci      break;
1280bf215546Sopenharmony_ci
1281bf215546Sopenharmony_ci   case nir_op_umod:
1282bf215546Sopenharmony_ci   case nir_op_irem:
1283bf215546Sopenharmony_ci      /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1284bf215546Sopenharmony_ci       * appears that our hardware just does the right thing for signed
1285bf215546Sopenharmony_ci       * remainder.
1286bf215546Sopenharmony_ci       */
1287bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1288bf215546Sopenharmony_ci      emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1289bf215546Sopenharmony_ci      break;
1290bf215546Sopenharmony_ci
1291bf215546Sopenharmony_ci   case nir_op_imod: {
1292bf215546Sopenharmony_ci      /* Get a regular C-style remainder.  If a % b == 0, set the predicate. */
1293bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1294bf215546Sopenharmony_ci
1295bf215546Sopenharmony_ci      /* Math instructions don't support conditional mod */
1296bf215546Sopenharmony_ci      inst = emit(MOV(dst_null_d(), src_reg(dst)));
1297bf215546Sopenharmony_ci      inst->conditional_mod = BRW_CONDITIONAL_NZ;
1298bf215546Sopenharmony_ci
1299bf215546Sopenharmony_ci      /* Now, we need to determine if signs of the sources are different.
1300bf215546Sopenharmony_ci       * When we XOR the sources, the top bit is 0 if they are the same and 1
1301bf215546Sopenharmony_ci       * if they are different.  We can then use a conditional modifier to
1302bf215546Sopenharmony_ci       * turn that into a predicate.  This leads us to an XOR.l instruction.
1303bf215546Sopenharmony_ci       *
1304bf215546Sopenharmony_ci       * Technically, according to the PRM, you're not allowed to use .l on a
1305bf215546Sopenharmony_ci       * XOR instruction.  However, empirical experiments and Curro's reading
1306bf215546Sopenharmony_ci       * of the simulator source both indicate that it's safe.
1307bf215546Sopenharmony_ci       */
1308bf215546Sopenharmony_ci      src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1309bf215546Sopenharmony_ci      inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1310bf215546Sopenharmony_ci      inst->predicate = BRW_PREDICATE_NORMAL;
1311bf215546Sopenharmony_ci      inst->conditional_mod = BRW_CONDITIONAL_L;
1312bf215546Sopenharmony_ci
1313bf215546Sopenharmony_ci      /* If the result of the initial remainder operation is non-zero and the
1314bf215546Sopenharmony_ci       * two sources have different signs, add in a copy of op[1] to get the
1315bf215546Sopenharmony_ci       * final integer modulus value.
1316bf215546Sopenharmony_ci       */
1317bf215546Sopenharmony_ci      inst = emit(ADD(dst, src_reg(dst), op[1]));
1318bf215546Sopenharmony_ci      inst->predicate = BRW_PREDICATE_NORMAL;
1319bf215546Sopenharmony_ci      break;
1320bf215546Sopenharmony_ci   }
1321bf215546Sopenharmony_ci
1322bf215546Sopenharmony_ci   case nir_op_ldexp:
1323bf215546Sopenharmony_ci      unreachable("not reached: should be handled by ldexp_to_arith()");
1324bf215546Sopenharmony_ci
1325bf215546Sopenharmony_ci   case nir_op_fsqrt:
1326bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1327bf215546Sopenharmony_ci      break;
1328bf215546Sopenharmony_ci
1329bf215546Sopenharmony_ci   case nir_op_frsq:
1330bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1331bf215546Sopenharmony_ci      break;
1332bf215546Sopenharmony_ci
1333bf215546Sopenharmony_ci   case nir_op_fpow:
1334bf215546Sopenharmony_ci      inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1335bf215546Sopenharmony_ci      break;
1336bf215546Sopenharmony_ci
1337bf215546Sopenharmony_ci   case nir_op_uadd_carry: {
1338bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1339bf215546Sopenharmony_ci      struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1340bf215546Sopenharmony_ci
1341bf215546Sopenharmony_ci      emit(ADDC(dst_null_ud(), op[0], op[1]));
1342bf215546Sopenharmony_ci      emit(MOV(dst, src_reg(acc)));
1343bf215546Sopenharmony_ci      break;
1344bf215546Sopenharmony_ci   }
1345bf215546Sopenharmony_ci
1346bf215546Sopenharmony_ci   case nir_op_usub_borrow: {
1347bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1348bf215546Sopenharmony_ci      struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1349bf215546Sopenharmony_ci
1350bf215546Sopenharmony_ci      emit(SUBB(dst_null_ud(), op[0], op[1]));
1351bf215546Sopenharmony_ci      emit(MOV(dst, src_reg(acc)));
1352bf215546Sopenharmony_ci      break;
1353bf215546Sopenharmony_ci   }
1354bf215546Sopenharmony_ci
1355bf215546Sopenharmony_ci   case nir_op_ftrunc:
1356bf215546Sopenharmony_ci      inst = emit(RNDZ(dst, op[0]));
1357bf215546Sopenharmony_ci      if (devinfo->ver < 6) {
1358bf215546Sopenharmony_ci         inst->conditional_mod = BRW_CONDITIONAL_R;
1359bf215546Sopenharmony_ci         inst = emit(ADD(dst, src_reg(dst), brw_imm_f(1.0f)));
1360bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1361bf215546Sopenharmony_ci         inst = emit(MOV(dst, src_reg(dst))); /* for potential saturation */
1362bf215546Sopenharmony_ci      }
1363bf215546Sopenharmony_ci      break;
1364bf215546Sopenharmony_ci
1365bf215546Sopenharmony_ci   case nir_op_fceil: {
1366bf215546Sopenharmony_ci      src_reg tmp = src_reg(this, glsl_type::float_type);
1367bf215546Sopenharmony_ci      tmp.swizzle =
1368bf215546Sopenharmony_ci         brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1369bf215546Sopenharmony_ci                              instr->src[0].src.ssa->num_components :
1370bf215546Sopenharmony_ci                              instr->src[0].src.reg.reg->num_components);
1371bf215546Sopenharmony_ci
1372bf215546Sopenharmony_ci      op[0].negate = !op[0].negate;
1373bf215546Sopenharmony_ci      emit(RNDD(dst_reg(tmp), op[0]));
1374bf215546Sopenharmony_ci      tmp.negate = true;
1375bf215546Sopenharmony_ci      inst = emit(MOV(dst, tmp));
1376bf215546Sopenharmony_ci      break;
1377bf215546Sopenharmony_ci   }
1378bf215546Sopenharmony_ci
1379bf215546Sopenharmony_ci   case nir_op_ffloor:
1380bf215546Sopenharmony_ci      inst = emit(RNDD(dst, op[0]));
1381bf215546Sopenharmony_ci      break;
1382bf215546Sopenharmony_ci
1383bf215546Sopenharmony_ci   case nir_op_ffract:
1384bf215546Sopenharmony_ci      inst = emit(FRC(dst, op[0]));
1385bf215546Sopenharmony_ci      break;
1386bf215546Sopenharmony_ci
1387bf215546Sopenharmony_ci   case nir_op_fround_even:
1388bf215546Sopenharmony_ci      inst = emit(RNDE(dst, op[0]));
1389bf215546Sopenharmony_ci      if (devinfo->ver < 6) {
1390bf215546Sopenharmony_ci         inst->conditional_mod = BRW_CONDITIONAL_R;
1391bf215546Sopenharmony_ci         inst = emit(ADD(dst, src_reg(dst), brw_imm_f(1.0f)));
1392bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1393bf215546Sopenharmony_ci         inst = emit(MOV(dst, src_reg(dst))); /* for potential saturation */
1394bf215546Sopenharmony_ci      }
1395bf215546Sopenharmony_ci      break;
1396bf215546Sopenharmony_ci
1397bf215546Sopenharmony_ci   case nir_op_fquantize2f16: {
1398bf215546Sopenharmony_ci      /* See also vec4_visitor::emit_pack_half_2x16() */
1399bf215546Sopenharmony_ci      src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1400bf215546Sopenharmony_ci      src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1401bf215546Sopenharmony_ci      src_reg zero = src_reg(this, glsl_type::vec4_type);
1402bf215546Sopenharmony_ci
1403bf215546Sopenharmony_ci      /* Check for denormal */
1404bf215546Sopenharmony_ci      src_reg abs_src0 = op[0];
1405bf215546Sopenharmony_ci      abs_src0.abs = true;
1406bf215546Sopenharmony_ci      emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1407bf215546Sopenharmony_ci               BRW_CONDITIONAL_L));
1408bf215546Sopenharmony_ci      /* Get the appropriately signed zero */
1409bf215546Sopenharmony_ci      emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1410bf215546Sopenharmony_ci               retype(op[0], BRW_REGISTER_TYPE_UD),
1411bf215546Sopenharmony_ci               brw_imm_ud(0x80000000)));
1412bf215546Sopenharmony_ci      /* Do the actual F32 -> F16 -> F32 conversion */
1413bf215546Sopenharmony_ci      emit(F32TO16(dst_reg(tmp16), op[0]));
1414bf215546Sopenharmony_ci      emit(F16TO32(dst_reg(tmp32), tmp16));
1415bf215546Sopenharmony_ci      /* Select that or zero based on normal status */
1416bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1417bf215546Sopenharmony_ci      inst->predicate = BRW_PREDICATE_NORMAL;
1418bf215546Sopenharmony_ci      break;
1419bf215546Sopenharmony_ci   }
1420bf215546Sopenharmony_ci
1421bf215546Sopenharmony_ci   case nir_op_imin:
1422bf215546Sopenharmony_ci   case nir_op_umin:
1423bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1424bf215546Sopenharmony_ci      FALLTHROUGH;
1425bf215546Sopenharmony_ci   case nir_op_fmin:
1426bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1427bf215546Sopenharmony_ci      inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1428bf215546Sopenharmony_ci      break;
1429bf215546Sopenharmony_ci
1430bf215546Sopenharmony_ci   case nir_op_imax:
1431bf215546Sopenharmony_ci   case nir_op_umax:
1432bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1433bf215546Sopenharmony_ci      FALLTHROUGH;
1434bf215546Sopenharmony_ci   case nir_op_fmax:
1435bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1436bf215546Sopenharmony_ci      inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1437bf215546Sopenharmony_ci      break;
1438bf215546Sopenharmony_ci
1439bf215546Sopenharmony_ci   case nir_op_fddx:
1440bf215546Sopenharmony_ci   case nir_op_fddx_coarse:
1441bf215546Sopenharmony_ci   case nir_op_fddx_fine:
1442bf215546Sopenharmony_ci   case nir_op_fddy:
1443bf215546Sopenharmony_ci   case nir_op_fddy_coarse:
1444bf215546Sopenharmony_ci   case nir_op_fddy_fine:
1445bf215546Sopenharmony_ci      unreachable("derivatives are not valid in vertex shaders");
1446bf215546Sopenharmony_ci
1447bf215546Sopenharmony_ci   case nir_op_ilt32:
1448bf215546Sopenharmony_ci   case nir_op_ult32:
1449bf215546Sopenharmony_ci   case nir_op_ige32:
1450bf215546Sopenharmony_ci   case nir_op_uge32:
1451bf215546Sopenharmony_ci   case nir_op_ieq32:
1452bf215546Sopenharmony_ci   case nir_op_ine32:
1453bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1454bf215546Sopenharmony_ci      FALLTHROUGH;
1455bf215546Sopenharmony_ci   case nir_op_flt32:
1456bf215546Sopenharmony_ci   case nir_op_fge32:
1457bf215546Sopenharmony_ci   case nir_op_feq32:
1458bf215546Sopenharmony_ci   case nir_op_fneu32: {
1459bf215546Sopenharmony_ci      enum brw_conditional_mod conditional_mod =
1460bf215546Sopenharmony_ci         brw_cmod_for_nir_comparison(instr->op);
1461bf215546Sopenharmony_ci
1462bf215546Sopenharmony_ci      if (nir_src_bit_size(instr->src[0].src) < 64) {
1463bf215546Sopenharmony_ci         /* If the order of the sources is changed due to an immediate value,
1464bf215546Sopenharmony_ci          * then the condition must also be changed.
1465bf215546Sopenharmony_ci          */
1466bf215546Sopenharmony_ci         if (try_immediate_source(instr, op, true) == 0)
1467bf215546Sopenharmony_ci            conditional_mod = brw_swap_cmod(conditional_mod);
1468bf215546Sopenharmony_ci
1469bf215546Sopenharmony_ci         emit(CMP(dst, op[0], op[1], conditional_mod));
1470bf215546Sopenharmony_ci      } else {
1471bf215546Sopenharmony_ci         /* Produce a 32-bit boolean result from the DF comparison by selecting
1472bf215546Sopenharmony_ci          * only the low 32-bit in each DF produced. Do this in a temporary
1473bf215546Sopenharmony_ci          * so we can then move from there to the result using align16 again
1474bf215546Sopenharmony_ci          * to honor the original writemask.
1475bf215546Sopenharmony_ci          */
1476bf215546Sopenharmony_ci         dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1477bf215546Sopenharmony_ci         emit(CMP(temp, op[0], op[1], conditional_mod));
1478bf215546Sopenharmony_ci         dst_reg result = dst_reg(this, glsl_type::bvec4_type);
1479bf215546Sopenharmony_ci         emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
1480bf215546Sopenharmony_ci         emit(MOV(dst, src_reg(result)));
1481bf215546Sopenharmony_ci      }
1482bf215546Sopenharmony_ci      break;
1483bf215546Sopenharmony_ci   }
1484bf215546Sopenharmony_ci
1485bf215546Sopenharmony_ci   case nir_op_b32all_iequal2:
1486bf215546Sopenharmony_ci   case nir_op_b32all_iequal3:
1487bf215546Sopenharmony_ci   case nir_op_b32all_iequal4:
1488bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1489bf215546Sopenharmony_ci      FALLTHROUGH;
1490bf215546Sopenharmony_ci   case nir_op_b32all_fequal2:
1491bf215546Sopenharmony_ci   case nir_op_b32all_fequal3:
1492bf215546Sopenharmony_ci   case nir_op_b32all_fequal4: {
1493bf215546Sopenharmony_ci      unsigned swiz =
1494bf215546Sopenharmony_ci         brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1495bf215546Sopenharmony_ci
1496bf215546Sopenharmony_ci      emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1497bf215546Sopenharmony_ci               brw_cmod_for_nir_comparison(instr->op)));
1498bf215546Sopenharmony_ci      emit(MOV(dst, brw_imm_d(0)));
1499bf215546Sopenharmony_ci      inst = emit(MOV(dst, brw_imm_d(~0)));
1500bf215546Sopenharmony_ci      inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1501bf215546Sopenharmony_ci      break;
1502bf215546Sopenharmony_ci   }
1503bf215546Sopenharmony_ci
1504bf215546Sopenharmony_ci   case nir_op_b32any_inequal2:
1505bf215546Sopenharmony_ci   case nir_op_b32any_inequal3:
1506bf215546Sopenharmony_ci   case nir_op_b32any_inequal4:
1507bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1508bf215546Sopenharmony_ci      FALLTHROUGH;
1509bf215546Sopenharmony_ci   case nir_op_b32any_fnequal2:
1510bf215546Sopenharmony_ci   case nir_op_b32any_fnequal3:
1511bf215546Sopenharmony_ci   case nir_op_b32any_fnequal4: {
1512bf215546Sopenharmony_ci      unsigned swiz =
1513bf215546Sopenharmony_ci         brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1514bf215546Sopenharmony_ci
1515bf215546Sopenharmony_ci      emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1516bf215546Sopenharmony_ci               brw_cmod_for_nir_comparison(instr->op)));
1517bf215546Sopenharmony_ci
1518bf215546Sopenharmony_ci      emit(MOV(dst, brw_imm_d(0)));
1519bf215546Sopenharmony_ci      inst = emit(MOV(dst, brw_imm_d(~0)));
1520bf215546Sopenharmony_ci      inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1521bf215546Sopenharmony_ci      break;
1522bf215546Sopenharmony_ci   }
1523bf215546Sopenharmony_ci
1524bf215546Sopenharmony_ci   case nir_op_inot:
1525bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1526bf215546Sopenharmony_ci      emit(NOT(dst, op[0]));
1527bf215546Sopenharmony_ci      break;
1528bf215546Sopenharmony_ci
1529bf215546Sopenharmony_ci   case nir_op_ixor:
1530bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1531bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1532bf215546Sopenharmony_ci      emit(XOR(dst, op[0], op[1]));
1533bf215546Sopenharmony_ci      break;
1534bf215546Sopenharmony_ci
1535bf215546Sopenharmony_ci   case nir_op_ior:
1536bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1537bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1538bf215546Sopenharmony_ci      emit(OR(dst, op[0], op[1]));
1539bf215546Sopenharmony_ci      break;
1540bf215546Sopenharmony_ci
1541bf215546Sopenharmony_ci   case nir_op_iand:
1542bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1543bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1544bf215546Sopenharmony_ci      emit(AND(dst, op[0], op[1]));
1545bf215546Sopenharmony_ci      break;
1546bf215546Sopenharmony_ci
1547bf215546Sopenharmony_ci   case nir_op_b2i32:
1548bf215546Sopenharmony_ci   case nir_op_b2f32:
1549bf215546Sopenharmony_ci   case nir_op_b2f64:
1550bf215546Sopenharmony_ci      if (nir_dest_bit_size(instr->dest.dest) > 32) {
1551bf215546Sopenharmony_ci         assert(dst.type == BRW_REGISTER_TYPE_DF);
1552bf215546Sopenharmony_ci         emit_conversion_to_double(dst, negate(op[0]));
1553bf215546Sopenharmony_ci      } else {
1554bf215546Sopenharmony_ci         emit(MOV(dst, negate(op[0])));
1555bf215546Sopenharmony_ci      }
1556bf215546Sopenharmony_ci      break;
1557bf215546Sopenharmony_ci
1558bf215546Sopenharmony_ci   case nir_op_f2b32:
1559bf215546Sopenharmony_ci      if (nir_src_bit_size(instr->src[0].src) == 64) {
1560bf215546Sopenharmony_ci         /* We use a MOV with conditional_mod to check if the provided value is
1561bf215546Sopenharmony_ci          * 0.0. We want this to flush denormalized numbers to zero, so we set a
1562bf215546Sopenharmony_ci          * source modifier on the source operand to trigger this, as source
1563bf215546Sopenharmony_ci          * modifiers don't affect the result of the testing against 0.0.
1564bf215546Sopenharmony_ci          */
1565bf215546Sopenharmony_ci         src_reg value = op[0];
1566bf215546Sopenharmony_ci         value.abs = true;
1567bf215546Sopenharmony_ci         vec4_instruction *inst = emit(MOV(dst_null_df(), value));
1568bf215546Sopenharmony_ci         inst->conditional_mod = BRW_CONDITIONAL_NZ;
1569bf215546Sopenharmony_ci
1570bf215546Sopenharmony_ci         src_reg one = src_reg(this, glsl_type::ivec4_type);
1571bf215546Sopenharmony_ci         emit(MOV(dst_reg(one), brw_imm_d(~0)));
1572bf215546Sopenharmony_ci         inst = emit(BRW_OPCODE_SEL, dst, one, brw_imm_d(0));
1573bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1574bf215546Sopenharmony_ci      } else {
1575bf215546Sopenharmony_ci         emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1576bf215546Sopenharmony_ci      }
1577bf215546Sopenharmony_ci      break;
1578bf215546Sopenharmony_ci
1579bf215546Sopenharmony_ci   case nir_op_i2b32:
1580bf215546Sopenharmony_ci      emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1581bf215546Sopenharmony_ci      break;
1582bf215546Sopenharmony_ci
1583bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16_split_x:
1584bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16_split_y:
1585bf215546Sopenharmony_ci   case nir_op_pack_half_2x16_split:
1586bf215546Sopenharmony_ci      unreachable("not reached: should not occur in vertex shader");
1587bf215546Sopenharmony_ci
1588bf215546Sopenharmony_ci   case nir_op_unpack_snorm_2x16:
1589bf215546Sopenharmony_ci   case nir_op_unpack_unorm_2x16:
1590bf215546Sopenharmony_ci   case nir_op_pack_snorm_2x16:
1591bf215546Sopenharmony_ci   case nir_op_pack_unorm_2x16:
1592bf215546Sopenharmony_ci      unreachable("not reached: should be handled by lower_packing_builtins");
1593bf215546Sopenharmony_ci
1594bf215546Sopenharmony_ci   case nir_op_pack_uvec4_to_uint:
1595bf215546Sopenharmony_ci      unreachable("not reached");
1596bf215546Sopenharmony_ci
1597bf215546Sopenharmony_ci   case nir_op_pack_uvec2_to_uint: {
1598bf215546Sopenharmony_ci      dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1599bf215546Sopenharmony_ci      tmp1.writemask = WRITEMASK_X;
1600bf215546Sopenharmony_ci      op[0].swizzle = BRW_SWIZZLE_YYYY;
1601bf215546Sopenharmony_ci      emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1602bf215546Sopenharmony_ci
1603bf215546Sopenharmony_ci      dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1604bf215546Sopenharmony_ci      tmp2.writemask = WRITEMASK_X;
1605bf215546Sopenharmony_ci      op[0].swizzle = BRW_SWIZZLE_XXXX;
1606bf215546Sopenharmony_ci      emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1607bf215546Sopenharmony_ci
1608bf215546Sopenharmony_ci      emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1609bf215546Sopenharmony_ci      break;
1610bf215546Sopenharmony_ci   }
1611bf215546Sopenharmony_ci
1612bf215546Sopenharmony_ci   case nir_op_pack_64_2x32_split: {
1613bf215546Sopenharmony_ci      dst_reg result = dst_reg(this, glsl_type::dvec4_type);
1614bf215546Sopenharmony_ci      dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1615bf215546Sopenharmony_ci      emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
1616bf215546Sopenharmony_ci      emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
1617bf215546Sopenharmony_ci      emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
1618bf215546Sopenharmony_ci      emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
1619bf215546Sopenharmony_ci      emit(MOV(dst, src_reg(result)));
1620bf215546Sopenharmony_ci      break;
1621bf215546Sopenharmony_ci   }
1622bf215546Sopenharmony_ci
1623bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_x:
1624bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32_split_y: {
1625bf215546Sopenharmony_ci      enum opcode oper = (instr->op == nir_op_unpack_64_2x32_split_x) ?
1626bf215546Sopenharmony_ci         VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
1627bf215546Sopenharmony_ci      dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
1628bf215546Sopenharmony_ci      emit(MOV(tmp, op[0]));
1629bf215546Sopenharmony_ci      dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
1630bf215546Sopenharmony_ci      emit(oper, tmp2, src_reg(tmp));
1631bf215546Sopenharmony_ci      emit(MOV(dst, src_reg(tmp2)));
1632bf215546Sopenharmony_ci      break;
1633bf215546Sopenharmony_ci   }
1634bf215546Sopenharmony_ci
1635bf215546Sopenharmony_ci   case nir_op_unpack_half_2x16:
1636bf215546Sopenharmony_ci      /* As NIR does not guarantee that we have a correct swizzle outside the
1637bf215546Sopenharmony_ci       * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1638bf215546Sopenharmony_ci       * uses the source operand in an operation with WRITEMASK_Y while our
1639bf215546Sopenharmony_ci       * source operand has only size 1, it accessed incorrect data producing
1640bf215546Sopenharmony_ci       * regressions in Piglit. We repeat the swizzle of the first component on the
1641bf215546Sopenharmony_ci       * rest of components to avoid regressions. In the vec4_visitor IR code path
1642bf215546Sopenharmony_ci       * this is not needed because the operand has already the correct swizzle.
1643bf215546Sopenharmony_ci       */
1644bf215546Sopenharmony_ci      op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1645bf215546Sopenharmony_ci      emit_unpack_half_2x16(dst, op[0]);
1646bf215546Sopenharmony_ci      break;
1647bf215546Sopenharmony_ci
1648bf215546Sopenharmony_ci   case nir_op_pack_half_2x16:
1649bf215546Sopenharmony_ci      emit_pack_half_2x16(dst, op[0]);
1650bf215546Sopenharmony_ci      break;
1651bf215546Sopenharmony_ci
1652bf215546Sopenharmony_ci   case nir_op_unpack_unorm_4x8:
1653bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1654bf215546Sopenharmony_ci      emit_unpack_unorm_4x8(dst, op[0]);
1655bf215546Sopenharmony_ci      break;
1656bf215546Sopenharmony_ci
1657bf215546Sopenharmony_ci   case nir_op_pack_unorm_4x8:
1658bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1659bf215546Sopenharmony_ci      emit_pack_unorm_4x8(dst, op[0]);
1660bf215546Sopenharmony_ci      break;
1661bf215546Sopenharmony_ci
1662bf215546Sopenharmony_ci   case nir_op_unpack_snorm_4x8:
1663bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1664bf215546Sopenharmony_ci      emit_unpack_snorm_4x8(dst, op[0]);
1665bf215546Sopenharmony_ci      break;
1666bf215546Sopenharmony_ci
1667bf215546Sopenharmony_ci   case nir_op_pack_snorm_4x8:
1668bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1669bf215546Sopenharmony_ci      emit_pack_snorm_4x8(dst, op[0]);
1670bf215546Sopenharmony_ci      break;
1671bf215546Sopenharmony_ci
1672bf215546Sopenharmony_ci   case nir_op_bitfield_reverse:
1673bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1674bf215546Sopenharmony_ci      emit(BFREV(dst, op[0]));
1675bf215546Sopenharmony_ci      break;
1676bf215546Sopenharmony_ci
1677bf215546Sopenharmony_ci   case nir_op_bit_count:
1678bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1679bf215546Sopenharmony_ci      emit(CBIT(dst, op[0]));
1680bf215546Sopenharmony_ci      break;
1681bf215546Sopenharmony_ci
1682bf215546Sopenharmony_ci   case nir_op_ufind_msb:
1683bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1684bf215546Sopenharmony_ci      emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1685bf215546Sopenharmony_ci      break;
1686bf215546Sopenharmony_ci
1687bf215546Sopenharmony_ci   case nir_op_ifind_msb: {
1688bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1689bf215546Sopenharmony_ci      vec4_builder bld = vec4_builder(this).at_end();
1690bf215546Sopenharmony_ci      src_reg src(dst);
1691bf215546Sopenharmony_ci
1692bf215546Sopenharmony_ci      if (devinfo->ver < 7) {
1693bf215546Sopenharmony_ci         emit_find_msb_using_lzd(bld, dst, op[0], true);
1694bf215546Sopenharmony_ci      } else {
1695bf215546Sopenharmony_ci         emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1696bf215546Sopenharmony_ci
1697bf215546Sopenharmony_ci         /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1698bf215546Sopenharmony_ci          * count from the LSB side. If FBH didn't return an error
1699bf215546Sopenharmony_ci          * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1700bf215546Sopenharmony_ci          * count into an LSB count.
1701bf215546Sopenharmony_ci          */
1702bf215546Sopenharmony_ci         bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1703bf215546Sopenharmony_ci
1704bf215546Sopenharmony_ci         inst = bld.ADD(dst, src, brw_imm_d(31));
1705bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1706bf215546Sopenharmony_ci         inst->src[0].negate = true;
1707bf215546Sopenharmony_ci      }
1708bf215546Sopenharmony_ci      break;
1709bf215546Sopenharmony_ci   }
1710bf215546Sopenharmony_ci
1711bf215546Sopenharmony_ci   case nir_op_find_lsb: {
1712bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1713bf215546Sopenharmony_ci      vec4_builder bld = vec4_builder(this).at_end();
1714bf215546Sopenharmony_ci
1715bf215546Sopenharmony_ci      if (devinfo->ver < 7) {
1716bf215546Sopenharmony_ci         dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1717bf215546Sopenharmony_ci
1718bf215546Sopenharmony_ci         /* (x & -x) generates a value that consists of only the LSB of x.
1719bf215546Sopenharmony_ci          * For all powers of 2, findMSB(y) == findLSB(y).
1720bf215546Sopenharmony_ci          */
1721bf215546Sopenharmony_ci         src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1722bf215546Sopenharmony_ci         src_reg negated_src = src;
1723bf215546Sopenharmony_ci
1724bf215546Sopenharmony_ci         /* One must be negated, and the other must be non-negated.  It
1725bf215546Sopenharmony_ci          * doesn't matter which is which.
1726bf215546Sopenharmony_ci          */
1727bf215546Sopenharmony_ci         negated_src.negate = true;
1728bf215546Sopenharmony_ci         src.negate = false;
1729bf215546Sopenharmony_ci
1730bf215546Sopenharmony_ci         bld.AND(temp, src, negated_src);
1731bf215546Sopenharmony_ci         emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1732bf215546Sopenharmony_ci      } else {
1733bf215546Sopenharmony_ci         bld.FBL(dst, op[0]);
1734bf215546Sopenharmony_ci      }
1735bf215546Sopenharmony_ci      break;
1736bf215546Sopenharmony_ci   }
1737bf215546Sopenharmony_ci
1738bf215546Sopenharmony_ci   case nir_op_ubitfield_extract:
1739bf215546Sopenharmony_ci   case nir_op_ibitfield_extract:
1740bf215546Sopenharmony_ci      unreachable("should have been lowered");
1741bf215546Sopenharmony_ci   case nir_op_ubfe:
1742bf215546Sopenharmony_ci   case nir_op_ibfe:
1743bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1744bf215546Sopenharmony_ci      op[0] = fix_3src_operand(op[0]);
1745bf215546Sopenharmony_ci      op[1] = fix_3src_operand(op[1]);
1746bf215546Sopenharmony_ci      op[2] = fix_3src_operand(op[2]);
1747bf215546Sopenharmony_ci
1748bf215546Sopenharmony_ci      emit(BFE(dst, op[2], op[1], op[0]));
1749bf215546Sopenharmony_ci      break;
1750bf215546Sopenharmony_ci
1751bf215546Sopenharmony_ci   case nir_op_bfm:
1752bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1753bf215546Sopenharmony_ci      emit(BFI1(dst, op[0], op[1]));
1754bf215546Sopenharmony_ci      break;
1755bf215546Sopenharmony_ci
1756bf215546Sopenharmony_ci   case nir_op_bfi:
1757bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1758bf215546Sopenharmony_ci      op[0] = fix_3src_operand(op[0]);
1759bf215546Sopenharmony_ci      op[1] = fix_3src_operand(op[1]);
1760bf215546Sopenharmony_ci      op[2] = fix_3src_operand(op[2]);
1761bf215546Sopenharmony_ci
1762bf215546Sopenharmony_ci      emit(BFI2(dst, op[0], op[1], op[2]));
1763bf215546Sopenharmony_ci      break;
1764bf215546Sopenharmony_ci
1765bf215546Sopenharmony_ci   case nir_op_bitfield_insert:
1766bf215546Sopenharmony_ci      unreachable("not reached: should have been lowered");
1767bf215546Sopenharmony_ci
1768bf215546Sopenharmony_ci   case nir_op_fsign:
1769bf215546Sopenharmony_ci       if (type_sz(op[0].type) < 8) {
1770bf215546Sopenharmony_ci         /* AND(val, 0x80000000) gives the sign bit.
1771bf215546Sopenharmony_ci          *
1772bf215546Sopenharmony_ci          * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1773bf215546Sopenharmony_ci          * zero.
1774bf215546Sopenharmony_ci          */
1775bf215546Sopenharmony_ci         emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1776bf215546Sopenharmony_ci
1777bf215546Sopenharmony_ci         op[0].type = BRW_REGISTER_TYPE_UD;
1778bf215546Sopenharmony_ci         dst.type = BRW_REGISTER_TYPE_UD;
1779bf215546Sopenharmony_ci         emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1780bf215546Sopenharmony_ci
1781bf215546Sopenharmony_ci         inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1782bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1783bf215546Sopenharmony_ci         dst.type = BRW_REGISTER_TYPE_F;
1784bf215546Sopenharmony_ci      } else {
1785bf215546Sopenharmony_ci         /* For doubles we do the same but we need to consider:
1786bf215546Sopenharmony_ci          *
1787bf215546Sopenharmony_ci          * - We use a MOV with conditional_mod instead of a CMP so that we can
1788bf215546Sopenharmony_ci          *   skip loading a 0.0 immediate. We use a source modifier on the
1789bf215546Sopenharmony_ci          *   source of the MOV so that we flush denormalized values to 0.
1790bf215546Sopenharmony_ci          *   Since we want to compare against 0, this won't alter the result.
1791bf215546Sopenharmony_ci          * - We need to extract the high 32-bit of each DF where the sign
1792bf215546Sopenharmony_ci          *   is stored.
1793bf215546Sopenharmony_ci          * - We need to produce a DF result.
1794bf215546Sopenharmony_ci          */
1795bf215546Sopenharmony_ci
1796bf215546Sopenharmony_ci         /* Check for zero */
1797bf215546Sopenharmony_ci         src_reg value = op[0];
1798bf215546Sopenharmony_ci         value.abs = true;
1799bf215546Sopenharmony_ci         inst = emit(MOV(dst_null_df(), value));
1800bf215546Sopenharmony_ci         inst->conditional_mod = BRW_CONDITIONAL_NZ;
1801bf215546Sopenharmony_ci
1802bf215546Sopenharmony_ci         /* AND each high 32-bit channel with 0x80000000u */
1803bf215546Sopenharmony_ci         dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1804bf215546Sopenharmony_ci         emit(VEC4_OPCODE_PICK_HIGH_32BIT, tmp, op[0]);
1805bf215546Sopenharmony_ci         emit(AND(tmp, src_reg(tmp), brw_imm_ud(0x80000000u)));
1806bf215546Sopenharmony_ci
1807bf215546Sopenharmony_ci         /* Add 1.0 to each channel, predicated to skip the cases where the
1808bf215546Sopenharmony_ci          * channel's value was 0
1809bf215546Sopenharmony_ci          */
1810bf215546Sopenharmony_ci         inst = emit(OR(tmp, src_reg(tmp), brw_imm_ud(0x3f800000u)));
1811bf215546Sopenharmony_ci         inst->predicate = BRW_PREDICATE_NORMAL;
1812bf215546Sopenharmony_ci
1813bf215546Sopenharmony_ci         /* Now convert the result from float to double */
1814bf215546Sopenharmony_ci         emit_conversion_to_double(dst, retype(src_reg(tmp),
1815bf215546Sopenharmony_ci                                               BRW_REGISTER_TYPE_F));
1816bf215546Sopenharmony_ci      }
1817bf215546Sopenharmony_ci      break;
1818bf215546Sopenharmony_ci
1819bf215546Sopenharmony_ci   case nir_op_ishl:
1820bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1821bf215546Sopenharmony_ci      try_immediate_source(instr, op, false);
1822bf215546Sopenharmony_ci      emit(SHL(dst, op[0], op[1]));
1823bf215546Sopenharmony_ci      break;
1824bf215546Sopenharmony_ci
1825bf215546Sopenharmony_ci   case nir_op_ishr:
1826bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1827bf215546Sopenharmony_ci      try_immediate_source(instr, op, false);
1828bf215546Sopenharmony_ci      emit(ASR(dst, op[0], op[1]));
1829bf215546Sopenharmony_ci      break;
1830bf215546Sopenharmony_ci
1831bf215546Sopenharmony_ci   case nir_op_ushr:
1832bf215546Sopenharmony_ci      assert(nir_dest_bit_size(instr->dest.dest) < 64);
1833bf215546Sopenharmony_ci      try_immediate_source(instr, op, false);
1834bf215546Sopenharmony_ci      emit(SHR(dst, op[0], op[1]));
1835bf215546Sopenharmony_ci      break;
1836bf215546Sopenharmony_ci
1837bf215546Sopenharmony_ci   case nir_op_ffma:
1838bf215546Sopenharmony_ci      if (type_sz(dst.type) == 8) {
1839bf215546Sopenharmony_ci         dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
1840bf215546Sopenharmony_ci         emit(MUL(mul_dst, op[1], op[0]));
1841bf215546Sopenharmony_ci         inst = emit(ADD(dst, src_reg(mul_dst), op[2]));
1842bf215546Sopenharmony_ci      } else {
1843bf215546Sopenharmony_ci         fix_float_operands(op, instr);
1844bf215546Sopenharmony_ci         inst = emit(MAD(dst, op[2], op[1], op[0]));
1845bf215546Sopenharmony_ci      }
1846bf215546Sopenharmony_ci      break;
1847bf215546Sopenharmony_ci
1848bf215546Sopenharmony_ci   case nir_op_flrp:
1849bf215546Sopenharmony_ci      fix_float_operands(op, instr);
1850bf215546Sopenharmony_ci      inst = emit(LRP(dst, op[2], op[1], op[0]));
1851bf215546Sopenharmony_ci      break;
1852bf215546Sopenharmony_ci
1853bf215546Sopenharmony_ci   case nir_op_b32csel:
1854bf215546Sopenharmony_ci      enum brw_predicate predicate;
1855bf215546Sopenharmony_ci      if (!optimize_predicate(instr, &predicate)) {
1856bf215546Sopenharmony_ci         emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1857bf215546Sopenharmony_ci         switch (dst.writemask) {
1858bf215546Sopenharmony_ci         case WRITEMASK_X:
1859bf215546Sopenharmony_ci            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1860bf215546Sopenharmony_ci            break;
1861bf215546Sopenharmony_ci         case WRITEMASK_Y:
1862bf215546Sopenharmony_ci            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1863bf215546Sopenharmony_ci            break;
1864bf215546Sopenharmony_ci         case WRITEMASK_Z:
1865bf215546Sopenharmony_ci            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1866bf215546Sopenharmony_ci            break;
1867bf215546Sopenharmony_ci         case WRITEMASK_W:
1868bf215546Sopenharmony_ci            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1869bf215546Sopenharmony_ci            break;
1870bf215546Sopenharmony_ci         default:
1871bf215546Sopenharmony_ci            predicate = BRW_PREDICATE_NORMAL;
1872bf215546Sopenharmony_ci            break;
1873bf215546Sopenharmony_ci         }
1874bf215546Sopenharmony_ci      }
1875bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1876bf215546Sopenharmony_ci      inst->predicate = predicate;
1877bf215546Sopenharmony_ci      break;
1878bf215546Sopenharmony_ci
1879bf215546Sopenharmony_ci   case nir_op_fdot2_replicated:
1880bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1881bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1882bf215546Sopenharmony_ci      break;
1883bf215546Sopenharmony_ci
1884bf215546Sopenharmony_ci   case nir_op_fdot3_replicated:
1885bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1886bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1887bf215546Sopenharmony_ci      break;
1888bf215546Sopenharmony_ci
1889bf215546Sopenharmony_ci   case nir_op_fdot4_replicated:
1890bf215546Sopenharmony_ci      try_immediate_source(instr, op, true);
1891bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1892bf215546Sopenharmony_ci      break;
1893bf215546Sopenharmony_ci
1894bf215546Sopenharmony_ci   case nir_op_fdph_replicated:
1895bf215546Sopenharmony_ci      try_immediate_source(instr, op, false);
1896bf215546Sopenharmony_ci      inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1897bf215546Sopenharmony_ci      break;
1898bf215546Sopenharmony_ci
1899bf215546Sopenharmony_ci   case nir_op_fdiv:
1900bf215546Sopenharmony_ci      unreachable("not reached: should be lowered by lower_fdiv in the compiler");
1901bf215546Sopenharmony_ci
1902bf215546Sopenharmony_ci   case nir_op_fmod:
1903bf215546Sopenharmony_ci      unreachable("not reached: should be lowered by lower_fmod in the compiler");
1904bf215546Sopenharmony_ci
1905bf215546Sopenharmony_ci   case nir_op_fsub:
1906bf215546Sopenharmony_ci   case nir_op_isub:
1907bf215546Sopenharmony_ci      unreachable("not reached: should be handled by ir_sub_to_add_neg");
1908bf215546Sopenharmony_ci
1909bf215546Sopenharmony_ci   default:
1910bf215546Sopenharmony_ci      unreachable("Unimplemented ALU operation");
1911bf215546Sopenharmony_ci   }
1912bf215546Sopenharmony_ci
1913bf215546Sopenharmony_ci   /* If we need to do a boolean resolve, replace the result with -(x & 1)
1914bf215546Sopenharmony_ci    * to sign extend the low bit to 0/~0
1915bf215546Sopenharmony_ci    */
1916bf215546Sopenharmony_ci   if (devinfo->ver <= 5 &&
1917bf215546Sopenharmony_ci       (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1918bf215546Sopenharmony_ci       BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1919bf215546Sopenharmony_ci      dst_reg masked = dst_reg(this, glsl_type::int_type);
1920bf215546Sopenharmony_ci      masked.writemask = dst.writemask;
1921bf215546Sopenharmony_ci      emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1922bf215546Sopenharmony_ci      src_reg masked_neg = src_reg(masked);
1923bf215546Sopenharmony_ci      masked_neg.negate = true;
1924bf215546Sopenharmony_ci      emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1925bf215546Sopenharmony_ci   }
1926bf215546Sopenharmony_ci}
1927bf215546Sopenharmony_ci
1928bf215546Sopenharmony_civoid
1929bf215546Sopenharmony_civec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1930bf215546Sopenharmony_ci{
1931bf215546Sopenharmony_ci   switch (instr->type) {
1932bf215546Sopenharmony_ci   case nir_jump_break:
1933bf215546Sopenharmony_ci      emit(BRW_OPCODE_BREAK);
1934bf215546Sopenharmony_ci      break;
1935bf215546Sopenharmony_ci
1936bf215546Sopenharmony_ci   case nir_jump_continue:
1937bf215546Sopenharmony_ci      emit(BRW_OPCODE_CONTINUE);
1938bf215546Sopenharmony_ci      break;
1939bf215546Sopenharmony_ci
1940bf215546Sopenharmony_ci   case nir_jump_return:
1941bf215546Sopenharmony_ci      FALLTHROUGH;
1942bf215546Sopenharmony_ci   default:
1943bf215546Sopenharmony_ci      unreachable("unknown jump");
1944bf215546Sopenharmony_ci   }
1945bf215546Sopenharmony_ci}
1946bf215546Sopenharmony_ci
1947bf215546Sopenharmony_cistatic bool
1948bf215546Sopenharmony_ciis_high_sampler(const struct intel_device_info *devinfo, src_reg sampler)
1949bf215546Sopenharmony_ci{
1950bf215546Sopenharmony_ci   if (devinfo->verx10 != 75)
1951bf215546Sopenharmony_ci      return false;
1952bf215546Sopenharmony_ci
1953bf215546Sopenharmony_ci   return sampler.file != IMM || sampler.ud >= 16;
1954bf215546Sopenharmony_ci}
1955bf215546Sopenharmony_ci
1956bf215546Sopenharmony_civoid
1957bf215546Sopenharmony_civec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1958bf215546Sopenharmony_ci{
1959bf215546Sopenharmony_ci   unsigned texture = instr->texture_index;
1960bf215546Sopenharmony_ci   unsigned sampler = instr->sampler_index;
1961bf215546Sopenharmony_ci   src_reg texture_reg = brw_imm_ud(texture);
1962bf215546Sopenharmony_ci   src_reg sampler_reg = brw_imm_ud(sampler);
1963bf215546Sopenharmony_ci   src_reg coordinate;
1964bf215546Sopenharmony_ci   const glsl_type *coord_type = NULL;
1965bf215546Sopenharmony_ci   src_reg shadow_comparator;
1966bf215546Sopenharmony_ci   src_reg offset_value;
1967bf215546Sopenharmony_ci   src_reg lod, lod2;
1968bf215546Sopenharmony_ci   src_reg sample_index;
1969bf215546Sopenharmony_ci   src_reg mcs;
1970bf215546Sopenharmony_ci
1971bf215546Sopenharmony_ci   dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1972bf215546Sopenharmony_ci
1973bf215546Sopenharmony_ci   /* The hardware requires a LOD for buffer textures */
1974bf215546Sopenharmony_ci   if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
1975bf215546Sopenharmony_ci      lod = brw_imm_d(0);
1976bf215546Sopenharmony_ci
1977bf215546Sopenharmony_ci   /* Load the texture operation sources */
1978bf215546Sopenharmony_ci   uint32_t constant_offset = 0;
1979bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
1980bf215546Sopenharmony_ci      switch (instr->src[i].src_type) {
1981bf215546Sopenharmony_ci      case nir_tex_src_comparator:
1982bf215546Sopenharmony_ci         shadow_comparator = get_nir_src(instr->src[i].src,
1983bf215546Sopenharmony_ci                                         BRW_REGISTER_TYPE_F, 1);
1984bf215546Sopenharmony_ci         break;
1985bf215546Sopenharmony_ci
1986bf215546Sopenharmony_ci      case nir_tex_src_coord: {
1987bf215546Sopenharmony_ci         unsigned src_size = nir_tex_instr_src_size(instr, i);
1988bf215546Sopenharmony_ci
1989bf215546Sopenharmony_ci         switch (instr->op) {
1990bf215546Sopenharmony_ci         case nir_texop_txf:
1991bf215546Sopenharmony_ci         case nir_texop_txf_ms:
1992bf215546Sopenharmony_ci         case nir_texop_samples_identical:
1993bf215546Sopenharmony_ci            coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1994bf215546Sopenharmony_ci                                     src_size);
1995bf215546Sopenharmony_ci            coord_type = glsl_type::ivec(src_size);
1996bf215546Sopenharmony_ci            break;
1997bf215546Sopenharmony_ci
1998bf215546Sopenharmony_ci         default:
1999bf215546Sopenharmony_ci            coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2000bf215546Sopenharmony_ci                                     src_size);
2001bf215546Sopenharmony_ci            coord_type = glsl_type::vec(src_size);
2002bf215546Sopenharmony_ci            break;
2003bf215546Sopenharmony_ci         }
2004bf215546Sopenharmony_ci         break;
2005bf215546Sopenharmony_ci      }
2006bf215546Sopenharmony_ci
2007bf215546Sopenharmony_ci      case nir_tex_src_ddx:
2008bf215546Sopenharmony_ci         lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2009bf215546Sopenharmony_ci                           nir_tex_instr_src_size(instr, i));
2010bf215546Sopenharmony_ci         break;
2011bf215546Sopenharmony_ci
2012bf215546Sopenharmony_ci      case nir_tex_src_ddy:
2013bf215546Sopenharmony_ci         lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
2014bf215546Sopenharmony_ci                           nir_tex_instr_src_size(instr, i));
2015bf215546Sopenharmony_ci         break;
2016bf215546Sopenharmony_ci
2017bf215546Sopenharmony_ci      case nir_tex_src_lod:
2018bf215546Sopenharmony_ci         switch (instr->op) {
2019bf215546Sopenharmony_ci         case nir_texop_txs:
2020bf215546Sopenharmony_ci         case nir_texop_txf:
2021bf215546Sopenharmony_ci            lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2022bf215546Sopenharmony_ci            break;
2023bf215546Sopenharmony_ci
2024bf215546Sopenharmony_ci         default:
2025bf215546Sopenharmony_ci            lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
2026bf215546Sopenharmony_ci            break;
2027bf215546Sopenharmony_ci         }
2028bf215546Sopenharmony_ci         break;
2029bf215546Sopenharmony_ci
2030bf215546Sopenharmony_ci      case nir_tex_src_ms_index: {
2031bf215546Sopenharmony_ci         sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
2032bf215546Sopenharmony_ci         break;
2033bf215546Sopenharmony_ci      }
2034bf215546Sopenharmony_ci
2035bf215546Sopenharmony_ci      case nir_tex_src_offset:
2036bf215546Sopenharmony_ci         if (!brw_texture_offset(instr, i, &constant_offset)) {
2037bf215546Sopenharmony_ci            offset_value =
2038bf215546Sopenharmony_ci               get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
2039bf215546Sopenharmony_ci         }
2040bf215546Sopenharmony_ci         break;
2041bf215546Sopenharmony_ci
2042bf215546Sopenharmony_ci      case nir_tex_src_texture_offset: {
2043bf215546Sopenharmony_ci         /* Emit code to evaluate the actual indexing expression */
2044bf215546Sopenharmony_ci         src_reg src = get_nir_src(instr->src[i].src, 1);
2045bf215546Sopenharmony_ci         src_reg temp(this, glsl_type::uint_type);
2046bf215546Sopenharmony_ci         emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
2047bf215546Sopenharmony_ci         texture_reg = emit_uniformize(temp);
2048bf215546Sopenharmony_ci         break;
2049bf215546Sopenharmony_ci      }
2050bf215546Sopenharmony_ci
2051bf215546Sopenharmony_ci      case nir_tex_src_sampler_offset: {
2052bf215546Sopenharmony_ci         /* Emit code to evaluate the actual indexing expression */
2053bf215546Sopenharmony_ci         src_reg src = get_nir_src(instr->src[i].src, 1);
2054bf215546Sopenharmony_ci         src_reg temp(this, glsl_type::uint_type);
2055bf215546Sopenharmony_ci         emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
2056bf215546Sopenharmony_ci         sampler_reg = emit_uniformize(temp);
2057bf215546Sopenharmony_ci         break;
2058bf215546Sopenharmony_ci      }
2059bf215546Sopenharmony_ci
2060bf215546Sopenharmony_ci      case nir_tex_src_projector:
2061bf215546Sopenharmony_ci         unreachable("Should be lowered by nir_lower_tex");
2062bf215546Sopenharmony_ci
2063bf215546Sopenharmony_ci      case nir_tex_src_bias:
2064bf215546Sopenharmony_ci         unreachable("LOD bias is not valid for vertex shaders.\n");
2065bf215546Sopenharmony_ci
2066bf215546Sopenharmony_ci      default:
2067bf215546Sopenharmony_ci         unreachable("unknown texture source");
2068bf215546Sopenharmony_ci      }
2069bf215546Sopenharmony_ci   }
2070bf215546Sopenharmony_ci
2071bf215546Sopenharmony_ci   if (instr->op == nir_texop_txf_ms ||
2072bf215546Sopenharmony_ci       instr->op == nir_texop_samples_identical) {
2073bf215546Sopenharmony_ci      assert(coord_type != NULL);
2074bf215546Sopenharmony_ci      if (devinfo->ver >= 7 &&
2075bf215546Sopenharmony_ci          key_tex->compressed_multisample_layout_mask & (1 << texture)) {
2076bf215546Sopenharmony_ci         mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
2077bf215546Sopenharmony_ci      } else {
2078bf215546Sopenharmony_ci         mcs = brw_imm_ud(0u);
2079bf215546Sopenharmony_ci      }
2080bf215546Sopenharmony_ci   }
2081bf215546Sopenharmony_ci
2082bf215546Sopenharmony_ci   /* Stuff the channel select bits in the top of the texture offset */
2083bf215546Sopenharmony_ci   if (instr->op == nir_texop_tg4) {
2084bf215546Sopenharmony_ci      if (instr->component == 1 &&
2085bf215546Sopenharmony_ci          (key_tex->gather_channel_quirk_mask & (1 << texture))) {
2086bf215546Sopenharmony_ci         /* gather4 sampler is broken for green channel on RG32F --
2087bf215546Sopenharmony_ci          * we must ask for blue instead.
2088bf215546Sopenharmony_ci          */
2089bf215546Sopenharmony_ci         constant_offset |= 2 << 16;
2090bf215546Sopenharmony_ci      } else {
2091bf215546Sopenharmony_ci         constant_offset |= instr->component << 16;
2092bf215546Sopenharmony_ci      }
2093bf215546Sopenharmony_ci   }
2094bf215546Sopenharmony_ci
2095bf215546Sopenharmony_ci   enum opcode opcode;
2096bf215546Sopenharmony_ci   switch (instr->op) {
2097bf215546Sopenharmony_ci   case nir_texop_tex:             opcode = SHADER_OPCODE_TXL;        break;
2098bf215546Sopenharmony_ci   case nir_texop_txl:             opcode = SHADER_OPCODE_TXL;        break;
2099bf215546Sopenharmony_ci   case nir_texop_txd:             opcode = SHADER_OPCODE_TXD;        break;
2100bf215546Sopenharmony_ci   case nir_texop_txf:             opcode = SHADER_OPCODE_TXF;        break;
2101bf215546Sopenharmony_ci   case nir_texop_txf_ms:          opcode = SHADER_OPCODE_TXF_CMS;    break;
2102bf215546Sopenharmony_ci   case nir_texop_txs:             opcode = SHADER_OPCODE_TXS;        break;
2103bf215546Sopenharmony_ci   case nir_texop_query_levels:    opcode = SHADER_OPCODE_TXS;        break;
2104bf215546Sopenharmony_ci   case nir_texop_texture_samples: opcode = SHADER_OPCODE_SAMPLEINFO; break;
2105bf215546Sopenharmony_ci   case nir_texop_tg4:
2106bf215546Sopenharmony_ci      opcode = offset_value.file != BAD_FILE ? SHADER_OPCODE_TG4_OFFSET
2107bf215546Sopenharmony_ci                                             : SHADER_OPCODE_TG4;
2108bf215546Sopenharmony_ci      break;
2109bf215546Sopenharmony_ci   case nir_texop_samples_identical: {
2110bf215546Sopenharmony_ci      /* There are some challenges implementing this for vec4, and it seems
2111bf215546Sopenharmony_ci       * unlikely to be used anyway.  For now, just return false ways.
2112bf215546Sopenharmony_ci       */
2113bf215546Sopenharmony_ci      emit(MOV(dest, brw_imm_ud(0u)));
2114bf215546Sopenharmony_ci      return;
2115bf215546Sopenharmony_ci   }
2116bf215546Sopenharmony_ci   case nir_texop_txb:
2117bf215546Sopenharmony_ci   case nir_texop_lod:
2118bf215546Sopenharmony_ci      unreachable("Implicit LOD is only valid inside fragment shaders.");
2119bf215546Sopenharmony_ci   default:
2120bf215546Sopenharmony_ci      unreachable("Unrecognized tex op");
2121bf215546Sopenharmony_ci   }
2122bf215546Sopenharmony_ci
2123bf215546Sopenharmony_ci   vec4_instruction *inst = new(mem_ctx) vec4_instruction(opcode, dest);
2124bf215546Sopenharmony_ci
2125bf215546Sopenharmony_ci   inst->offset = constant_offset;
2126bf215546Sopenharmony_ci
2127bf215546Sopenharmony_ci   /* The message header is necessary for:
2128bf215546Sopenharmony_ci    * - Gfx4 (always)
2129bf215546Sopenharmony_ci    * - Texel offsets
2130bf215546Sopenharmony_ci    * - Gather channel selection
2131bf215546Sopenharmony_ci    * - Sampler indices too large to fit in a 4-bit value.
2132bf215546Sopenharmony_ci    * - Sampleinfo message - takes no parameters, but mlen = 0 is illegal
2133bf215546Sopenharmony_ci    */
2134bf215546Sopenharmony_ci   inst->header_size =
2135bf215546Sopenharmony_ci      (devinfo->ver < 5 ||
2136bf215546Sopenharmony_ci       inst->offset != 0 ||
2137bf215546Sopenharmony_ci       opcode == SHADER_OPCODE_TG4 ||
2138bf215546Sopenharmony_ci       opcode == SHADER_OPCODE_TG4_OFFSET ||
2139bf215546Sopenharmony_ci       opcode == SHADER_OPCODE_SAMPLEINFO ||
2140bf215546Sopenharmony_ci       is_high_sampler(devinfo, sampler_reg)) ? 1 : 0;
2141bf215546Sopenharmony_ci   inst->base_mrf = 2;
2142bf215546Sopenharmony_ci   inst->mlen = inst->header_size;
2143bf215546Sopenharmony_ci   inst->dst.writemask = WRITEMASK_XYZW;
2144bf215546Sopenharmony_ci   inst->shadow_compare = shadow_comparator.file != BAD_FILE;
2145bf215546Sopenharmony_ci
2146bf215546Sopenharmony_ci   inst->src[1] = texture_reg;
2147bf215546Sopenharmony_ci   inst->src[2] = sampler_reg;
2148bf215546Sopenharmony_ci
2149bf215546Sopenharmony_ci   /* MRF for the first parameter */
2150bf215546Sopenharmony_ci   int param_base = inst->base_mrf + inst->header_size;
2151bf215546Sopenharmony_ci
2152bf215546Sopenharmony_ci   if (opcode == SHADER_OPCODE_TXS) {
2153bf215546Sopenharmony_ci      int writemask = devinfo->ver == 4 ? WRITEMASK_W : WRITEMASK_X;
2154bf215546Sopenharmony_ci      emit(MOV(dst_reg(MRF, param_base, lod.type, writemask), lod));
2155bf215546Sopenharmony_ci      inst->mlen++;
2156bf215546Sopenharmony_ci   } else if (opcode == SHADER_OPCODE_SAMPLEINFO) {
2157bf215546Sopenharmony_ci      inst->dst.writemask = WRITEMASK_X;
2158bf215546Sopenharmony_ci   } else {
2159bf215546Sopenharmony_ci      /* Load the coordinate */
2160bf215546Sopenharmony_ci      /* FINISHME: gl_clamp_mask and saturate */
2161bf215546Sopenharmony_ci      int coord_mask = (1 << instr->coord_components) - 1;
2162bf215546Sopenharmony_ci      int zero_mask = 0xf & ~coord_mask;
2163bf215546Sopenharmony_ci
2164bf215546Sopenharmony_ci      emit(MOV(dst_reg(MRF, param_base, coordinate.type, coord_mask),
2165bf215546Sopenharmony_ci               coordinate));
2166bf215546Sopenharmony_ci      inst->mlen++;
2167bf215546Sopenharmony_ci
2168bf215546Sopenharmony_ci      if (zero_mask != 0) {
2169bf215546Sopenharmony_ci         emit(MOV(dst_reg(MRF, param_base, coordinate.type, zero_mask),
2170bf215546Sopenharmony_ci                  brw_imm_d(0)));
2171bf215546Sopenharmony_ci      }
2172bf215546Sopenharmony_ci      /* Load the shadow comparator */
2173bf215546Sopenharmony_ci      if (shadow_comparator.file != BAD_FILE &&
2174bf215546Sopenharmony_ci          opcode != SHADER_OPCODE_TXD &&
2175bf215546Sopenharmony_ci          opcode != SHADER_OPCODE_TG4_OFFSET) {
2176bf215546Sopenharmony_ci	 emit(MOV(dst_reg(MRF, param_base + 1, shadow_comparator.type,
2177bf215546Sopenharmony_ci			  WRITEMASK_X),
2178bf215546Sopenharmony_ci		  shadow_comparator));
2179bf215546Sopenharmony_ci	 inst->mlen++;
2180bf215546Sopenharmony_ci      }
2181bf215546Sopenharmony_ci
2182bf215546Sopenharmony_ci      /* Load the LOD info */
2183bf215546Sopenharmony_ci      switch (opcode) {
2184bf215546Sopenharmony_ci      case SHADER_OPCODE_TXL: {
2185bf215546Sopenharmony_ci	 int mrf, writemask;
2186bf215546Sopenharmony_ci	 if (devinfo->ver >= 5) {
2187bf215546Sopenharmony_ci	    mrf = param_base + 1;
2188bf215546Sopenharmony_ci	    if (shadow_comparator.file != BAD_FILE) {
2189bf215546Sopenharmony_ci	       writemask = WRITEMASK_Y;
2190bf215546Sopenharmony_ci	       /* mlen already incremented */
2191bf215546Sopenharmony_ci	    } else {
2192bf215546Sopenharmony_ci	       writemask = WRITEMASK_X;
2193bf215546Sopenharmony_ci	       inst->mlen++;
2194bf215546Sopenharmony_ci	    }
2195bf215546Sopenharmony_ci	 } else /* devinfo->ver == 4 */ {
2196bf215546Sopenharmony_ci	    mrf = param_base;
2197bf215546Sopenharmony_ci	    writemask = WRITEMASK_W;
2198bf215546Sopenharmony_ci	 }
2199bf215546Sopenharmony_ci	 emit(MOV(dst_reg(MRF, mrf, lod.type, writemask), lod));
2200bf215546Sopenharmony_ci         break;
2201bf215546Sopenharmony_ci      }
2202bf215546Sopenharmony_ci
2203bf215546Sopenharmony_ci      case SHADER_OPCODE_TXF:
2204bf215546Sopenharmony_ci         emit(MOV(dst_reg(MRF, param_base, lod.type, WRITEMASK_W), lod));
2205bf215546Sopenharmony_ci         break;
2206bf215546Sopenharmony_ci
2207bf215546Sopenharmony_ci      case SHADER_OPCODE_TXF_CMS:
2208bf215546Sopenharmony_ci         emit(MOV(dst_reg(MRF, param_base + 1, sample_index.type, WRITEMASK_X),
2209bf215546Sopenharmony_ci                  sample_index));
2210bf215546Sopenharmony_ci         if (devinfo->ver >= 7) {
2211bf215546Sopenharmony_ci            /* MCS data is in the first channel of `mcs`, but we need to get it into
2212bf215546Sopenharmony_ci             * the .y channel of the second vec4 of params, so replicate .x across
2213bf215546Sopenharmony_ci             * the whole vec4 and then mask off everything except .y
2214bf215546Sopenharmony_ci             */
2215bf215546Sopenharmony_ci            mcs.swizzle = BRW_SWIZZLE_XXXX;
2216bf215546Sopenharmony_ci            emit(MOV(dst_reg(MRF, param_base + 1, glsl_type::uint_type, WRITEMASK_Y),
2217bf215546Sopenharmony_ci                     mcs));
2218bf215546Sopenharmony_ci         }
2219bf215546Sopenharmony_ci         inst->mlen++;
2220bf215546Sopenharmony_ci         break;
2221bf215546Sopenharmony_ci
2222bf215546Sopenharmony_ci      case SHADER_OPCODE_TXD: {
2223bf215546Sopenharmony_ci         const brw_reg_type type = lod.type;
2224bf215546Sopenharmony_ci
2225bf215546Sopenharmony_ci	 if (devinfo->ver >= 5) {
2226bf215546Sopenharmony_ci	    lod.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2227bf215546Sopenharmony_ci	    lod2.swizzle = BRW_SWIZZLE4(SWIZZLE_X,SWIZZLE_X,SWIZZLE_Y,SWIZZLE_Y);
2228bf215546Sopenharmony_ci	    emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XZ), lod));
2229bf215546Sopenharmony_ci	    emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_YW), lod2));
2230bf215546Sopenharmony_ci	    inst->mlen++;
2231bf215546Sopenharmony_ci
2232bf215546Sopenharmony_ci	    if (nir_tex_instr_dest_size(instr) == 3 ||
2233bf215546Sopenharmony_ci                shadow_comparator.file != BAD_FILE) {
2234bf215546Sopenharmony_ci	       lod.swizzle = BRW_SWIZZLE_ZZZZ;
2235bf215546Sopenharmony_ci	       lod2.swizzle = BRW_SWIZZLE_ZZZZ;
2236bf215546Sopenharmony_ci	       emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_X), lod));
2237bf215546Sopenharmony_ci	       emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_Y), lod2));
2238bf215546Sopenharmony_ci	       inst->mlen++;
2239bf215546Sopenharmony_ci
2240bf215546Sopenharmony_ci               if (shadow_comparator.file != BAD_FILE) {
2241bf215546Sopenharmony_ci                  emit(MOV(dst_reg(MRF, param_base + 2,
2242bf215546Sopenharmony_ci                                   shadow_comparator.type, WRITEMASK_Z),
2243bf215546Sopenharmony_ci                           shadow_comparator));
2244bf215546Sopenharmony_ci               }
2245bf215546Sopenharmony_ci	    }
2246bf215546Sopenharmony_ci	 } else /* devinfo->ver == 4 */ {
2247bf215546Sopenharmony_ci	    emit(MOV(dst_reg(MRF, param_base + 1, type, WRITEMASK_XYZ), lod));
2248bf215546Sopenharmony_ci	    emit(MOV(dst_reg(MRF, param_base + 2, type, WRITEMASK_XYZ), lod2));
2249bf215546Sopenharmony_ci	    inst->mlen += 2;
2250bf215546Sopenharmony_ci	 }
2251bf215546Sopenharmony_ci         break;
2252bf215546Sopenharmony_ci      }
2253bf215546Sopenharmony_ci
2254bf215546Sopenharmony_ci      case SHADER_OPCODE_TG4_OFFSET:
2255bf215546Sopenharmony_ci         if (shadow_comparator.file != BAD_FILE) {
2256bf215546Sopenharmony_ci            emit(MOV(dst_reg(MRF, param_base, shadow_comparator.type, WRITEMASK_W),
2257bf215546Sopenharmony_ci                     shadow_comparator));
2258bf215546Sopenharmony_ci         }
2259bf215546Sopenharmony_ci
2260bf215546Sopenharmony_ci         emit(MOV(dst_reg(MRF, param_base + 1, glsl_type::ivec2_type, WRITEMASK_XY),
2261bf215546Sopenharmony_ci                  offset_value));
2262bf215546Sopenharmony_ci         inst->mlen++;
2263bf215546Sopenharmony_ci         break;
2264bf215546Sopenharmony_ci
2265bf215546Sopenharmony_ci      default:
2266bf215546Sopenharmony_ci         break;
2267bf215546Sopenharmony_ci      }
2268bf215546Sopenharmony_ci   }
2269bf215546Sopenharmony_ci
2270bf215546Sopenharmony_ci   emit(inst);
2271bf215546Sopenharmony_ci
2272bf215546Sopenharmony_ci   /* fixup num layers (z) for cube arrays: hardware returns faces * layers;
2273bf215546Sopenharmony_ci    * spec requires layers.
2274bf215546Sopenharmony_ci    */
2275bf215546Sopenharmony_ci   if (instr->op == nir_texop_txs && devinfo->ver < 7) {
2276bf215546Sopenharmony_ci      /* Gfx4-6 return 0 instead of 1 for single layer surfaces. */
2277bf215546Sopenharmony_ci      emit_minmax(BRW_CONDITIONAL_GE, writemask(inst->dst, WRITEMASK_Z),
2278bf215546Sopenharmony_ci                  src_reg(inst->dst), brw_imm_d(1));
2279bf215546Sopenharmony_ci   }
2280bf215546Sopenharmony_ci
2281bf215546Sopenharmony_ci   if (instr->op == nir_texop_query_levels) {
2282bf215546Sopenharmony_ci      /* # levels is in .w */
2283bf215546Sopenharmony_ci      src_reg swizzled(dest);
2284bf215546Sopenharmony_ci      swizzled.swizzle = BRW_SWIZZLE4(SWIZZLE_W, SWIZZLE_W,
2285bf215546Sopenharmony_ci                                      SWIZZLE_W, SWIZZLE_W);
2286bf215546Sopenharmony_ci      emit(MOV(dest, swizzled));
2287bf215546Sopenharmony_ci   }
2288bf215546Sopenharmony_ci}
2289bf215546Sopenharmony_ci
2290bf215546Sopenharmony_cisrc_reg
2291bf215546Sopenharmony_civec4_visitor::emit_mcs_fetch(const glsl_type *coordinate_type,
2292bf215546Sopenharmony_ci                             src_reg coordinate, src_reg surface)
2293bf215546Sopenharmony_ci{
2294bf215546Sopenharmony_ci   vec4_instruction *inst =
2295bf215546Sopenharmony_ci      new(mem_ctx) vec4_instruction(SHADER_OPCODE_TXF_MCS,
2296bf215546Sopenharmony_ci                                    dst_reg(this, glsl_type::uvec4_type));
2297bf215546Sopenharmony_ci   inst->base_mrf = 2;
2298bf215546Sopenharmony_ci   inst->src[1] = surface;
2299bf215546Sopenharmony_ci   inst->src[2] = brw_imm_ud(0); /* sampler */
2300bf215546Sopenharmony_ci   inst->mlen = 1;
2301bf215546Sopenharmony_ci
2302bf215546Sopenharmony_ci   const int param_base = inst->base_mrf;
2303bf215546Sopenharmony_ci
2304bf215546Sopenharmony_ci   /* parameters are: u, v, r, lod; lod will always be zero due to api restrictions */
2305bf215546Sopenharmony_ci   int coord_mask = (1 << coordinate_type->vector_elements) - 1;
2306bf215546Sopenharmony_ci   int zero_mask = 0xf & ~coord_mask;
2307bf215546Sopenharmony_ci
2308bf215546Sopenharmony_ci   emit(MOV(dst_reg(MRF, param_base, coordinate_type, coord_mask),
2309bf215546Sopenharmony_ci            coordinate));
2310bf215546Sopenharmony_ci
2311bf215546Sopenharmony_ci   emit(MOV(dst_reg(MRF, param_base, coordinate_type, zero_mask),
2312bf215546Sopenharmony_ci            brw_imm_d(0)));
2313bf215546Sopenharmony_ci
2314bf215546Sopenharmony_ci   emit(inst);
2315bf215546Sopenharmony_ci   return src_reg(inst->dst);
2316bf215546Sopenharmony_ci}
2317bf215546Sopenharmony_ci
2318bf215546Sopenharmony_civoid
2319bf215546Sopenharmony_civec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
2320bf215546Sopenharmony_ci{
2321bf215546Sopenharmony_ci   nir_ssa_values[instr->def.index] =
2322bf215546Sopenharmony_ci      dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(instr->def.bit_size, 32)));
2323bf215546Sopenharmony_ci}
2324bf215546Sopenharmony_ci
2325bf215546Sopenharmony_ci/* SIMD4x2 64bit data is stored in register space like this:
2326bf215546Sopenharmony_ci *
2327bf215546Sopenharmony_ci * r0.0:DF  x0 y0 z0 w0
2328bf215546Sopenharmony_ci * r1.0:DF  x1 y1 z1 w1
2329bf215546Sopenharmony_ci *
2330bf215546Sopenharmony_ci * When we need to write data such as this to memory using 32-bit write
2331bf215546Sopenharmony_ci * messages we need to shuffle it in this fashion:
2332bf215546Sopenharmony_ci *
2333bf215546Sopenharmony_ci * r0.0:DF  x0 y0 x1 y1 (to be written at base offset)
2334bf215546Sopenharmony_ci * r0.0:DF  z0 w0 z1 w1 (to be written at base offset + 16)
2335bf215546Sopenharmony_ci *
2336bf215546Sopenharmony_ci * We need to do the inverse operation when we read using 32-bit messages,
2337bf215546Sopenharmony_ci * which we can do by applying the same exact shuffling on the 64-bit data
2338bf215546Sopenharmony_ci * read, only that because the data for each vertex is positioned differently
2339bf215546Sopenharmony_ci * we need to apply different channel enables.
2340bf215546Sopenharmony_ci *
2341bf215546Sopenharmony_ci * This function takes 64bit data and shuffles it as explained above.
2342bf215546Sopenharmony_ci *
2343bf215546Sopenharmony_ci * The @for_write parameter is used to specify if the shuffling is being done
2344bf215546Sopenharmony_ci * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2345bf215546Sopenharmony_ci * write message (for_write = true), or instead we are doing the inverse
2346bf215546Sopenharmony_ci * operation and we have just read 64-bit data using a 32-bit messages that we
2347bf215546Sopenharmony_ci * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2348bf215546Sopenharmony_ci *
2349bf215546Sopenharmony_ci * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2350bf215546Sopenharmony_ci * otherwise the instructions are emitted normally at the end. The function
2351bf215546Sopenharmony_ci * returns the last instruction inserted.
2352bf215546Sopenharmony_ci *
2353bf215546Sopenharmony_ci * Notice that @src and @dst cannot be the same register.
2354bf215546Sopenharmony_ci */
2355bf215546Sopenharmony_civec4_instruction *
2356bf215546Sopenharmony_civec4_visitor::shuffle_64bit_data(dst_reg dst, src_reg src, bool for_write,
2357bf215546Sopenharmony_ci                                 bool for_scratch,
2358bf215546Sopenharmony_ci                                 bblock_t *block, vec4_instruction *ref)
2359bf215546Sopenharmony_ci{
2360bf215546Sopenharmony_ci   assert(type_sz(src.type) == 8);
2361bf215546Sopenharmony_ci   assert(type_sz(dst.type) == 8);
2362bf215546Sopenharmony_ci   assert(!regions_overlap(dst, 2 * REG_SIZE, src, 2 * REG_SIZE));
2363bf215546Sopenharmony_ci   assert(!ref == !block);
2364bf215546Sopenharmony_ci
2365bf215546Sopenharmony_ci   opcode mov_op = for_scratch ? VEC4_OPCODE_MOV_FOR_SCRATCH : BRW_OPCODE_MOV;
2366bf215546Sopenharmony_ci
2367bf215546Sopenharmony_ci   const vec4_builder bld = !ref ? vec4_builder(this).at_end() :
2368bf215546Sopenharmony_ci                                   vec4_builder(this).at(block, ref->next);
2369bf215546Sopenharmony_ci
2370bf215546Sopenharmony_ci   /* Resolve swizzle in src */
2371bf215546Sopenharmony_ci   if (src.swizzle != BRW_SWIZZLE_XYZW) {
2372bf215546Sopenharmony_ci      dst_reg data = dst_reg(this, glsl_type::dvec4_type);
2373bf215546Sopenharmony_ci      bld.emit(mov_op, data, src);
2374bf215546Sopenharmony_ci      src = src_reg(data);
2375bf215546Sopenharmony_ci   }
2376bf215546Sopenharmony_ci
2377bf215546Sopenharmony_ci   /* dst+0.XY = src+0.XY */
2378bf215546Sopenharmony_ci   bld.group(4, 0).emit(mov_op, writemask(dst, WRITEMASK_XY), src);
2379bf215546Sopenharmony_ci
2380bf215546Sopenharmony_ci   /* dst+0.ZW = src+1.XY */
2381bf215546Sopenharmony_ci   bld.group(4, for_write ? 1 : 0)
2382bf215546Sopenharmony_ci            .emit(mov_op, writemask(dst, WRITEMASK_ZW),
2383bf215546Sopenharmony_ci                  swizzle(byte_offset(src, REG_SIZE), BRW_SWIZZLE_XYXY));
2384bf215546Sopenharmony_ci
2385bf215546Sopenharmony_ci   /* dst+1.XY = src+0.ZW */
2386bf215546Sopenharmony_ci   bld.group(4, for_write ? 0 : 1)
2387bf215546Sopenharmony_ci            .emit(mov_op, writemask(byte_offset(dst, REG_SIZE), WRITEMASK_XY),
2388bf215546Sopenharmony_ci                  swizzle(src, BRW_SWIZZLE_ZWZW));
2389bf215546Sopenharmony_ci
2390bf215546Sopenharmony_ci   /* dst+1.ZW = src+1.ZW */
2391bf215546Sopenharmony_ci   return bld.group(4, 1)
2392bf215546Sopenharmony_ci            .emit(mov_op, writemask(byte_offset(dst, REG_SIZE), WRITEMASK_ZW),
2393bf215546Sopenharmony_ci                  byte_offset(src, REG_SIZE));
2394bf215546Sopenharmony_ci}
2395bf215546Sopenharmony_ci
2396bf215546Sopenharmony_ci}
2397