2 * Copyright © 2019 Google, Inc.
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:
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
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
25 #include "compiler/nir/nir_builder.h"
28 * This pass lowers load_barycentric_at_offset to dsx.3d/dsy.3d and alu
33 load(nir_builder
*b
, unsigned ncomp
, nir_intrinsic_op op
)
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
);
39 return &load_size
->dest
.ssa
;
43 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder
*b
,
44 nir_instr
*instr
, void *data
)
46 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
48 #define chan(var, c) nir_channel(b, var, c)
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
);
56 /* scaled ij with s as 3rd component: */
57 nir_ssa_def
*sij
= nir_vec3(b
,
58 nir_fmul(b
, chan(ij
, 0), s
),
59 nir_fmul(b
, chan(ij
, 1), s
),
62 nir_ssa_def
*foo
= nir_fddx(b
, sij
);
63 nir_ssa_def
*bar
= nir_fddy(b
, sij
);
65 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
66 b
->shader
->info
.fs
.needs_helper_invocations
= true;
68 nir_ssa_def
*x
, *y
, *z
, *i
, *j
;
70 x
= nir_ffma(b
, chan(off
, 0), chan(foo
, 0), chan(sij
, 0));
71 y
= nir_ffma(b
, chan(off
, 0), chan(foo
, 1), chan(sij
, 1));
72 z
= nir_ffma(b
, chan(off
, 0), chan(foo
, 2), chan(sij
, 2));
74 x
= nir_ffma(b
, chan(off
, 1), chan(bar
, 0), x
);
75 y
= nir_ffma(b
, chan(off
, 1), chan(bar
, 1), y
);
76 z
= nir_ffma(b
, chan(off
, 1), chan(bar
, 2), z
);
78 /* convert back into primitive space: */
80 i
= nir_fmul(b
, z
, x
);
81 j
= nir_fmul(b
, z
, y
);
83 ij
= nir_vec2(b
, i
, j
);
89 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr
*instr
,
92 return (instr
->type
== nir_instr_type_intrinsic
&&
93 nir_instr_as_intrinsic(instr
)->intrinsic
==
94 nir_intrinsic_load_barycentric_at_offset
);
98 ir3_nir_lower_load_barycentric_at_offset(nir_shader
*shader
)
100 debug_assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
102 return nir_shader_lower_instructions(shader
,
103 ir3_nir_lower_load_barycentric_at_offset_filter
,
104 ir3_nir_lower_load_barycentric_at_offset_instr
,