• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Google, Inc.
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, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "compiler/nir/nir_builder.h"
25 #include "ir3_nir.h"
26 
27 /**
28  * This pass lowers load_barycentric_at_offset to dsx.3d/dsy.3d and alu
29  * instructions.
30  */
31 
32 static nir_ssa_def *
load(nir_builder * b,unsigned ncomp,nir_intrinsic_op op)33 load(nir_builder *b, unsigned ncomp, nir_intrinsic_op op)
34 {
35    nir_intrinsic_instr *load_size = nir_intrinsic_instr_create(b->shader, op);
36    nir_ssa_dest_init(&load_size->instr, &load_size->dest, ncomp, 32, NULL);
37    nir_builder_instr_insert(b, &load_size->instr);
38 
39    return &load_size->dest.ssa;
40 }
41 
42 static nir_ssa_def *
ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder * b,nir_instr * instr,void * data)43 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder *b, nir_instr *instr,
44                                                void *data)
45 {
46    nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
47 
48 #define chan(var, c) nir_channel(b, var, c)
49 
50    nir_ssa_def *off = intr->src[0].ssa;
51    nir_ssa_def *ij = load(b, 2, nir_intrinsic_load_barycentric_pixel);
52    nir_ssa_def *s = load(b, 1, nir_intrinsic_load_size_ir3);
53 
54    s = nir_frcp(b, s);
55 
56    /* scaled ij with s as 3rd component: */
57    nir_ssa_def *sij =
58       nir_vec3(b, nir_fmul(b, chan(ij, 0), s), nir_fmul(b, chan(ij, 1), s), s);
59 
60    nir_ssa_def *foo = nir_fddx(b, sij);
61    nir_ssa_def *bar = nir_fddy(b, sij);
62 
63    if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
64       b->shader->info.fs.needs_quad_helper_invocations = true;
65 
66    nir_ssa_def *x, *y, *z, *i, *j;
67 
68    x = nir_ffma(b, chan(off, 0), chan(foo, 0), chan(sij, 0));
69    y = nir_ffma(b, chan(off, 0), chan(foo, 1), chan(sij, 1));
70    z = nir_ffma(b, chan(off, 0), chan(foo, 2), chan(sij, 2));
71 
72    x = nir_ffma(b, chan(off, 1), chan(bar, 0), x);
73    y = nir_ffma(b, chan(off, 1), chan(bar, 1), y);
74    z = nir_ffma(b, chan(off, 1), chan(bar, 2), z);
75 
76    /* convert back into primitive space: */
77    z = nir_frcp(b, z);
78    i = nir_fmul(b, z, x);
79    j = nir_fmul(b, z, y);
80 
81    ij = nir_vec2(b, i, j);
82 
83    return ij;
84 }
85 
86 static bool
ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr * instr,const void * data)87 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr *instr,
88                                                 const void *data)
89 {
90    return (instr->type == nir_instr_type_intrinsic &&
91            nir_instr_as_intrinsic(instr)->intrinsic ==
92               nir_intrinsic_load_barycentric_at_offset);
93 }
94 
95 bool
ir3_nir_lower_load_barycentric_at_offset(nir_shader * shader)96 ir3_nir_lower_load_barycentric_at_offset(nir_shader *shader)
97 {
98    debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
99 
100    return nir_shader_lower_instructions(
101       shader, ir3_nir_lower_load_barycentric_at_offset_filter,
102       ir3_nir_lower_load_barycentric_at_offset_instr, NULL);
103 }
104