ir3: Plumb through bindless support
[mesa.git] / src / freedreno / ir3 / ir3_nir_lower_load_barycentric_at_offset.c
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 "ir3_nir.h"
25 #include "compiler/nir/nir_builder.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 *
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 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);
39
40 return &load_size->dest.ssa;
41 }
42
43 static nir_ssa_def *
44 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder *b,
45 nir_instr *instr, void *data)
46 {
47 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
48
49 #define chan(var, c) nir_channel(b, var, c)
50
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);
54
55 s = nir_frcp(b, s);
56
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),
61 s);
62
63 nir_ssa_def *foo = nir_fddx(b, sij);
64 nir_ssa_def *bar = nir_fddy(b, sij);
65
66 if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
67 b->shader->info.fs.needs_helper_invocations = true;
68
69 nir_ssa_def *x, *y, *z, *i, *j;
70
71 x = nir_ffma(b, chan(off, 0), chan(foo, 0), chan(sij, 0));
72 y = nir_ffma(b, chan(off, 0), chan(foo, 1), chan(sij, 1));
73 z = nir_ffma(b, chan(off, 0), chan(foo, 2), chan(sij, 2));
74
75 x = nir_ffma(b, chan(off, 1), chan(bar, 0), x);
76 y = nir_ffma(b, chan(off, 1), chan(bar, 1), y);
77 z = nir_ffma(b, chan(off, 1), chan(bar, 2), z);
78
79 /* convert back into primitive space: */
80 z = nir_frcp(b, z);
81 i = nir_fmul(b, z, x);
82 j = nir_fmul(b, z, y);
83
84 ij = nir_vec2(b, i, j);
85
86 return ij;
87 }
88
89 static bool
90 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr *instr,
91 const void *data)
92 {
93 return (instr->type == nir_instr_type_intrinsic &&
94 nir_instr_as_intrinsic(instr)->intrinsic ==
95 nir_intrinsic_load_barycentric_at_offset);
96 }
97
98 bool
99 ir3_nir_lower_load_barycentric_at_offset(nir_shader *shader)
100 {
101 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
102
103 return nir_shader_lower_instructions(shader,
104 ir3_nir_lower_load_barycentric_at_offset_filter,
105 ir3_nir_lower_load_barycentric_at_offset_instr,
106 NULL);
107 }