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 *
ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder * b,nir_instr * instr,void * data)33 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder *b, nir_instr *instr,
34 void *data)
35 {
36 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
37 enum glsl_interp_mode interp_mode = nir_intrinsic_interp_mode(intr);
38
39 #define chan(var, c) nir_channel(b, var, c)
40
41 nir_ssa_def *off = intr->src[0].ssa;
42 /* note: at_offset is defined to be relative to the center of the pixel */
43 nir_ssa_def *ij = nir_load_barycentric_pixel(b, 32, .interp_mode = interp_mode);
44
45 /* Need helper invocations for our ddx/ddys to work. */
46 if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
47 b->shader->info.fs.needs_quad_helper_invocations = true;
48
49 if (interp_mode != INTERP_MODE_SMOOTH) {
50 /* Offset our pixel center ij by the offset argument (units of pixels)
51 * times the derivatives of ij in screen space.
52 */
53 nir_ssa_def *new_ij = ij;
54 new_ij = nir_ffma(b, chan(off, 0), nir_fddx(b, ij), new_ij);
55 new_ij = nir_ffma(b, chan(off, 1), nir_fddy(b, ij), new_ij);
56
57 return new_ij;
58 } else {
59 nir_ssa_def *center_w = nir_frcp(b, nir_load_persp_center_rhw_ir3(b, 32));
60
61 /* scaled ij -- ij comes in multiplied by by 1/center_w so multiply that
62 * back out, plus add center_w as the 3rd component for taking the
63 * derivatives.
64 *
65 * We actually suspect that we should be using rhw here instead of center_w,
66 * but no tests seem to distinguish between the two.
67 */
68 nir_ssa_def *sij =
69 nir_vec3(b, nir_fmul(b, chan(ij, 0), center_w), nir_fmul(b, chan(ij, 1), center_w), center_w);
70
71 /* Get the offset value from pixel center for ij, and also for w. */
72 nir_ssa_def *pos = sij;
73 pos = nir_ffma(b, chan(off, 0), nir_fddx(b, sij), pos);
74 pos = nir_ffma(b, chan(off, 1), nir_fddy(b, sij), pos);
75
76 /* convert back into screen space, dividing by the offset 1/w */
77 return nir_fmul(b, nir_channels(b, pos, 0x3), nir_frcp(b, chan(pos, 2)));
78 }
79 }
80
81 static bool
ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr * instr,const void * data)82 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr *instr,
83 const void *data)
84 {
85 return (instr->type == nir_instr_type_intrinsic &&
86 nir_instr_as_intrinsic(instr)->intrinsic ==
87 nir_intrinsic_load_barycentric_at_offset);
88 }
89
90 bool
ir3_nir_lower_load_barycentric_at_offset(nir_shader * shader)91 ir3_nir_lower_load_barycentric_at_offset(nir_shader *shader)
92 {
93 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
94
95 return nir_shader_lower_instructions(
96 shader, ir3_nir_lower_load_barycentric_at_offset_filter,
97 ir3_nir_lower_load_barycentric_at_offset_instr, NULL);
98 }
99