1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2019 Zodiac Inflight Innovations
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, sub license,
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
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the 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 NON-INFRINGEMENT. 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
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Jonathan Marek <jonathan@marek.ca>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "etnaviv_nir.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/* io related lowering
30bf215546Sopenharmony_ci * run after lower_int_to_float because it adds i2f/f2i ops
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_civoid
33bf215546Sopenharmony_cietna_lower_io(nir_shader *shader, struct etna_shader_variant *v)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
36bf215546Sopenharmony_ci      nir_builder b;
37bf215546Sopenharmony_ci      nir_builder_init(&b, function->impl);
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci      nir_foreach_block(block, function->impl) {
40bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
41bf215546Sopenharmony_ci            if (instr->type == nir_instr_type_intrinsic) {
42bf215546Sopenharmony_ci               nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci               switch (intr->intrinsic) {
45bf215546Sopenharmony_ci               case nir_intrinsic_load_front_face: {
46bf215546Sopenharmony_ci                  /* HW front_face is 0.0/1.0, not 0/~0u for bool
47bf215546Sopenharmony_ci                   * lower with a comparison with 0
48bf215546Sopenharmony_ci                   */
49bf215546Sopenharmony_ci                  intr->dest.ssa.bit_size = 32;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci                  b.cursor = nir_after_instr(instr);
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci                  nir_ssa_def *ssa = nir_ine(&b, &intr->dest.ssa, nir_imm_int(&b, 0));
54bf215546Sopenharmony_ci                  if (v->key.front_ccw)
55bf215546Sopenharmony_ci                     nir_instr_as_alu(ssa->parent_instr)->op = nir_op_ieq;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci                  nir_ssa_def_rewrite_uses_after(&intr->dest.ssa,
58bf215546Sopenharmony_ci                                                 ssa,
59bf215546Sopenharmony_ci                                                 ssa->parent_instr);
60bf215546Sopenharmony_ci               } break;
61bf215546Sopenharmony_ci               case nir_intrinsic_store_deref: {
62bf215546Sopenharmony_ci                  nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
63bf215546Sopenharmony_ci                  if (shader->info.stage != MESA_SHADER_FRAGMENT || !v->key.frag_rb_swap)
64bf215546Sopenharmony_ci                     break;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci                  assert(deref->deref_type == nir_deref_type_var);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci                  if (deref->var->data.location != FRAG_RESULT_COLOR &&
69bf215546Sopenharmony_ci                      deref->var->data.location != FRAG_RESULT_DATA0)
70bf215546Sopenharmony_ci                      break;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci                  b.cursor = nir_before_instr(instr);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci                  nir_ssa_def *ssa = nir_mov(&b, intr->src[1].ssa);
75bf215546Sopenharmony_ci                  nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);
76bf215546Sopenharmony_ci                  alu->src[0].swizzle[0] = 2;
77bf215546Sopenharmony_ci                  alu->src[0].swizzle[2] = 0;
78bf215546Sopenharmony_ci                  nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(ssa));
79bf215546Sopenharmony_ci               } break;
80bf215546Sopenharmony_ci               case nir_intrinsic_load_vertex_id:
81bf215546Sopenharmony_ci               case nir_intrinsic_load_instance_id:
82bf215546Sopenharmony_ci                  /* detect use of vertex_id/instance_id */
83bf215546Sopenharmony_ci                  v->vs_id_in_reg = v->infile.num_reg;
84bf215546Sopenharmony_ci                  break;
85bf215546Sopenharmony_ci               default:
86bf215546Sopenharmony_ci                  break;
87bf215546Sopenharmony_ci               }
88bf215546Sopenharmony_ci            }
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_tex)
91bf215546Sopenharmony_ci               continue;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci            nir_tex_instr *tex = nir_instr_as_tex(instr);
94bf215546Sopenharmony_ci            nir_src *coord = NULL;
95bf215546Sopenharmony_ci            nir_src *src1 = NULL;
96bf215546Sopenharmony_ci            unsigned src1_idx;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci            assert(tex->sampler_index == tex->texture_index);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci            for (unsigned i = 0; i < tex->num_srcs; i++) {
101bf215546Sopenharmony_ci               switch (tex->src[i].src_type) {
102bf215546Sopenharmony_ci               case nir_tex_src_coord:
103bf215546Sopenharmony_ci                  coord = &tex->src[i].src;
104bf215546Sopenharmony_ci                  break;
105bf215546Sopenharmony_ci               case nir_tex_src_bias:
106bf215546Sopenharmony_ci               case nir_tex_src_lod:
107bf215546Sopenharmony_ci                  assert(!src1);
108bf215546Sopenharmony_ci                  src1 = &tex->src[i].src;
109bf215546Sopenharmony_ci                  src1_idx = i;
110bf215546Sopenharmony_ci                  break;
111bf215546Sopenharmony_ci               case nir_tex_src_ddx:
112bf215546Sopenharmony_ci               case nir_tex_src_ddy:
113bf215546Sopenharmony_ci               case nir_tex_src_comparator:
114bf215546Sopenharmony_ci                  break;
115bf215546Sopenharmony_ci               default:
116bf215546Sopenharmony_ci                  assert(0);
117bf215546Sopenharmony_ci                  break;
118bf215546Sopenharmony_ci               }
119bf215546Sopenharmony_ci            }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci            /* pre HALTI5 needs texture sources in a single source */
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci            if (!src1 || v->shader->specs->halti >= 5)
124bf215546Sopenharmony_ci               continue;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci            assert(coord && src1 && tex->coord_components < 4);
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci            nir_alu_instr *vec = nir_alu_instr_create(shader, nir_op_vec4);
129bf215546Sopenharmony_ci            for (unsigned i = 0; i < tex->coord_components; i++) {
130bf215546Sopenharmony_ci               vec->src[i].src = nir_src_for_ssa(coord->ssa);
131bf215546Sopenharmony_ci               vec->src[i].swizzle[0] = i;
132bf215546Sopenharmony_ci            }
133bf215546Sopenharmony_ci            for (unsigned i = tex->coord_components; i < 4; i++)
134bf215546Sopenharmony_ci               vec->src[i].src = nir_src_for_ssa(src1->ssa);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci            vec->dest.write_mask = 0xf;
137bf215546Sopenharmony_ci            nir_ssa_dest_init(&vec->instr, &vec->dest.dest, 4, 32, NULL);
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci            nir_tex_instr_remove_src(tex, src1_idx);
140bf215546Sopenharmony_ci            nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(&vec->dest.dest.ssa));
141bf215546Sopenharmony_ci            tex->coord_components = 4;
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci            nir_instr_insert_before(&tex->instr, &vec->instr);
144bf215546Sopenharmony_ci         }
145bf215546Sopenharmony_ci      }
146bf215546Sopenharmony_ci   }
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistatic void
150bf215546Sopenharmony_cietna_lower_alu_impl(nir_function_impl *impl, bool has_new_transcendentals)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci   nir_shader *shader = impl->function->shader;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   nir_builder b;
155bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   /* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */
158bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
159bf215546Sopenharmony_ci      nir_foreach_instr_safe(instr, block) {
160bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_alu)
161bf215546Sopenharmony_ci            continue;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci         nir_alu_instr *alu = nir_instr_as_alu(instr);
164bf215546Sopenharmony_ci         /* multiply sin/cos src by constant
165bf215546Sopenharmony_ci          * TODO: do this earlier (but it breaks const_prop opt)
166bf215546Sopenharmony_ci          */
167bf215546Sopenharmony_ci         if (alu->op == nir_op_fsin || alu->op == nir_op_fcos) {
168bf215546Sopenharmony_ci            b.cursor = nir_before_instr(instr);
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci            nir_ssa_def *imm = has_new_transcendentals ?
171bf215546Sopenharmony_ci               nir_imm_float(&b, 1.0 / M_PI) :
172bf215546Sopenharmony_ci               nir_imm_float(&b, 2.0 / M_PI);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci            nir_instr_rewrite_src(instr, &alu->src[0].src,
175bf215546Sopenharmony_ci               nir_src_for_ssa(nir_fmul(&b, alu->src[0].src.ssa, imm)));
176bf215546Sopenharmony_ci         }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci         /* change transcendental ops to vec2 and insert vec1 mul for the result
179bf215546Sopenharmony_ci          * TODO: do this earlier (but it breaks with optimizations)
180bf215546Sopenharmony_ci          */
181bf215546Sopenharmony_ci         if (has_new_transcendentals && (
182bf215546Sopenharmony_ci             alu->op == nir_op_fdiv || alu->op == nir_op_flog2 ||
183bf215546Sopenharmony_ci             alu->op == nir_op_fsin || alu->op == nir_op_fcos)) {
184bf215546Sopenharmony_ci            nir_ssa_def *ssa = &alu->dest.dest.ssa;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci            assert(ssa->num_components == 1);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci            nir_alu_instr *mul = nir_alu_instr_create(shader, nir_op_fmul);
189bf215546Sopenharmony_ci            mul->src[0].src = mul->src[1].src = nir_src_for_ssa(ssa);
190bf215546Sopenharmony_ci            mul->src[1].swizzle[0] = 1;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci            mul->dest.write_mask = 1;
193bf215546Sopenharmony_ci            nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci            ssa->num_components = 2;
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci            mul->dest.saturate = alu->dest.saturate;
198bf215546Sopenharmony_ci            alu->dest.saturate = 0;
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci            nir_instr_insert_after(instr, &mul->instr);
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci            nir_ssa_def_rewrite_uses_after(ssa, &mul->dest.dest.ssa,
203bf215546Sopenharmony_ci                                           &mul->instr);
204bf215546Sopenharmony_ci         }
205bf215546Sopenharmony_ci      }
206bf215546Sopenharmony_ci   }
207bf215546Sopenharmony_ci}
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_civoid
210bf215546Sopenharmony_cietna_lower_alu(nir_shader *shader, bool has_new_transcendentals)
211bf215546Sopenharmony_ci{
212bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
213bf215546Sopenharmony_ci      if (function->impl)
214bf215546Sopenharmony_ci         etna_lower_alu_impl(function->impl, has_new_transcendentals);
215bf215546Sopenharmony_ci   }
216bf215546Sopenharmony_ci}
217