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 load_size
->num_components
= ncomp
;
37 nir_ssa_dest_init(&load_size
->instr
, &load_size
->dest
, ncomp
, 32, NULL
);
38 nir_builder_instr_insert(b
, &load_size
->instr
);
40 return &load_size
->dest
.ssa
;
44 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder
*b
,
45 nir_instr
*instr
, void *data
)
47 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
49 #define chan(var, c) nir_channel(b, var, c)
51 nir_ssa_def
*off
= intr
->src
[0].ssa
;
52 nir_ssa_def
*ij
= load(b
, 2, nir_intrinsic_load_barycentric_pixel
);
53 nir_ssa_def
*s
= load(b
, 1, nir_intrinsic_load_size_ir3
);
57 /* scaled ij with s as 3rd component: */
58 nir_ssa_def
*sij
= nir_vec3(b
,
59 nir_fmul(b
, chan(ij
, 0), s
),
60 nir_fmul(b
, chan(ij
, 1), s
),
63 nir_ssa_def
*foo
= nir_fddx(b
, sij
);
64 nir_ssa_def
*bar
= nir_fddy(b
, sij
);
66 nir_ssa_def
*x
, *y
, *z
, *i
, *j
;
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));
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
);
76 /* convert back into primitive space: */
78 i
= nir_fmul(b
, z
, x
);
79 j
= nir_fmul(b
, z
, y
);
81 ij
= nir_vec2(b
, i
, j
);
87 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr
*instr
,
90 return (instr
->type
== nir_instr_type_intrinsic
&&
91 nir_instr_as_intrinsic(instr
)->intrinsic
==
92 nir_intrinsic_load_barycentric_at_offset
);
96 ir3_nir_lower_load_barycentric_at_offset(nir_shader
*shader
)
98 debug_assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
100 return nir_shader_lower_instructions(shader
,
101 ir3_nir_lower_load_barycentric_at_offset_filter
,
102 ir3_nir_lower_load_barycentric_at_offset_instr
,