• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 Zodiac Inflight Innovations
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Jonathan Marek <jonathan@marek.ca>
25  */
26 
27 #include "etnaviv_nir.h"
28 
29 /* io related lowering
30  * run after lower_int_to_float because it adds i2f/f2i ops
31  */
32 void
etna_lower_io(nir_shader * shader,struct etna_shader_variant * v)33 etna_lower_io(nir_shader *shader, struct etna_shader_variant *v)
34 {
35    nir_foreach_function(function, shader) {
36       nir_builder b;
37       nir_builder_init(&b, function->impl);
38 
39       nir_foreach_block(block, function->impl) {
40          nir_foreach_instr_safe(instr, block) {
41             if (instr->type == nir_instr_type_intrinsic) {
42                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
43 
44                switch (intr->intrinsic) {
45                case nir_intrinsic_load_front_face: {
46                   /* HW front_face is 0.0/1.0, not 0/~0u for bool
47                    * lower with a comparison with 0
48                    */
49                   intr->dest.ssa.bit_size = 32;
50 
51                   b.cursor = nir_after_instr(instr);
52 
53                   nir_ssa_def *ssa = nir_ine(&b, &intr->dest.ssa, nir_imm_int(&b, 0));
54                   if (v->key.front_ccw)
55                      nir_instr_as_alu(ssa->parent_instr)->op = nir_op_ieq;
56 
57                   nir_ssa_def_rewrite_uses_after(&intr->dest.ssa,
58                                                  ssa,
59                                                  ssa->parent_instr);
60                } break;
61                case nir_intrinsic_store_deref: {
62                   nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
63                   if (shader->info.stage != MESA_SHADER_FRAGMENT || !v->key.frag_rb_swap)
64                      break;
65 
66                   assert(deref->deref_type == nir_deref_type_var);
67 
68                   if (deref->var->data.location != FRAG_RESULT_COLOR &&
69                       deref->var->data.location != FRAG_RESULT_DATA0)
70                       break;
71 
72                   b.cursor = nir_before_instr(instr);
73 
74                   nir_ssa_def *ssa = nir_mov(&b, intr->src[1].ssa);
75                   nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);
76                   alu->src[0].swizzle[0] = 2;
77                   alu->src[0].swizzle[2] = 0;
78                   nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(ssa));
79                } break;
80                case nir_intrinsic_load_vertex_id:
81                case nir_intrinsic_load_instance_id:
82                   /* detect use of vertex_id/instance_id */
83                   v->vs_id_in_reg = v->infile.num_reg;
84                   break;
85                default:
86                   break;
87                }
88             }
89 
90             if (instr->type != nir_instr_type_tex)
91                continue;
92 
93             nir_tex_instr *tex = nir_instr_as_tex(instr);
94             nir_src *coord = NULL;
95             nir_src *lod_bias = NULL;
96             unsigned lod_bias_idx;
97 
98             assert(tex->sampler_index == tex->texture_index);
99 
100             for (unsigned i = 0; i < tex->num_srcs; i++) {
101                switch (tex->src[i].src_type) {
102                case nir_tex_src_coord:
103                   coord = &tex->src[i].src;
104                   break;
105                case nir_tex_src_bias:
106                case nir_tex_src_lod:
107                   assert(!lod_bias);
108                   lod_bias = &tex->src[i].src;
109                   lod_bias_idx = i;
110                   break;
111                case nir_tex_src_comparator:
112                   break;
113                default:
114                   assert(0);
115                   break;
116                }
117             }
118 
119             /* pre HALTI5 needs texture sources in a single source */
120 
121             if (!lod_bias || v->shader->specs->halti >= 5)
122                continue;
123 
124             assert(coord && lod_bias && tex->coord_components < 4);
125 
126             nir_alu_instr *vec = nir_alu_instr_create(shader, nir_op_vec4);
127             for (unsigned i = 0; i < tex->coord_components; i++) {
128                vec->src[i].src = nir_src_for_ssa(coord->ssa);
129                vec->src[i].swizzle[0] = i;
130             }
131             for (unsigned i = tex->coord_components; i < 4; i++)
132                vec->src[i].src = nir_src_for_ssa(lod_bias->ssa);
133 
134             vec->dest.write_mask = 0xf;
135             nir_ssa_dest_init(&vec->instr, &vec->dest.dest, 4, 32, NULL);
136 
137             nir_tex_instr_remove_src(tex, lod_bias_idx);
138             nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(&vec->dest.dest.ssa));
139             tex->coord_components = 4;
140 
141             nir_instr_insert_before(&tex->instr, &vec->instr);
142          }
143       }
144    }
145 }
146 
147 static void
etna_lower_alu_impl(nir_function_impl * impl,bool has_new_transcendentals)148 etna_lower_alu_impl(nir_function_impl *impl, bool has_new_transcendentals)
149 {
150    nir_shader *shader = impl->function->shader;
151 
152    nir_builder b;
153    nir_builder_init(&b, impl);
154 
155    /* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */
156    nir_foreach_block(block, impl) {
157       nir_foreach_instr_safe(instr, block) {
158          if (instr->type != nir_instr_type_alu)
159             continue;
160 
161          nir_alu_instr *alu = nir_instr_as_alu(instr);
162          /* multiply sin/cos src by constant
163           * TODO: do this earlier (but it breaks const_prop opt)
164           */
165          if (alu->op == nir_op_fsin || alu->op == nir_op_fcos) {
166             b.cursor = nir_before_instr(instr);
167 
168             nir_ssa_def *imm = has_new_transcendentals ?
169                nir_imm_float(&b, 1.0 / M_PI) :
170                nir_imm_float(&b, 2.0 / M_PI);
171 
172             nir_instr_rewrite_src(instr, &alu->src[0].src,
173                nir_src_for_ssa(nir_fmul(&b, alu->src[0].src.ssa, imm)));
174          }
175 
176          /* change transcendental ops to vec2 and insert vec1 mul for the result
177           * TODO: do this earlier (but it breaks with optimizations)
178           */
179          if (has_new_transcendentals && (
180              alu->op == nir_op_fdiv || alu->op == nir_op_flog2 ||
181              alu->op == nir_op_fsin || alu->op == nir_op_fcos)) {
182             nir_ssa_def *ssa = &alu->dest.dest.ssa;
183 
184             assert(ssa->num_components == 1);
185 
186             nir_alu_instr *mul = nir_alu_instr_create(shader, nir_op_fmul);
187             mul->src[0].src = mul->src[1].src = nir_src_for_ssa(ssa);
188             mul->src[1].swizzle[0] = 1;
189 
190             mul->dest.write_mask = 1;
191             nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL);
192 
193             ssa->num_components = 2;
194 
195             mul->dest.saturate = alu->dest.saturate;
196             alu->dest.saturate = 0;
197 
198             nir_instr_insert_after(instr, &mul->instr);
199 
200             nir_ssa_def_rewrite_uses_after(ssa, &mul->dest.dest.ssa,
201                                            &mul->instr);
202          }
203       }
204    }
205 }
206 
207 void
etna_lower_alu(nir_shader * shader,bool has_new_transcendentals)208 etna_lower_alu(nir_shader *shader, bool has_new_transcendentals)
209 {
210    nir_foreach_function(function, shader) {
211       if (function->impl)
212          etna_lower_alu_impl(function->impl, has_new_transcendentals);
213    }
214 }
215