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_sample to load_sample_pos_from_id
29 * plus load_barycentric_at_offset.
30 *
31 * It also lowers load_sample_pos to load_sample_pos_from_id, mostly because
32 * that needs to happen at the same early stage (before wpos_ytransform)
33 */
34
35 static nir_ssa_def *
load_sample_pos(nir_builder * b,nir_ssa_def * samp_id)36 load_sample_pos(nir_builder *b, nir_ssa_def *samp_id)
37 {
38 return nir_load_sample_pos_from_id(b, 32, samp_id);
39 }
40
41 static nir_ssa_def *
lower_load_barycentric_at_sample(nir_builder * b,nir_intrinsic_instr * intr)42 lower_load_barycentric_at_sample(nir_builder *b, nir_intrinsic_instr *intr)
43 {
44 nir_ssa_def *pos = load_sample_pos(b, intr->src[0].ssa);
45
46 return nir_load_barycentric_at_offset(b, 32, pos);
47 }
48
49 static nir_ssa_def *
lower_load_sample_pos(nir_builder * b,nir_intrinsic_instr * intr)50 lower_load_sample_pos(nir_builder *b, nir_intrinsic_instr *intr)
51 {
52 nir_ssa_def *pos = load_sample_pos(b, nir_load_sample_id(b));
53
54 /* Note that gl_SamplePosition is offset by +vec2(0.5, 0.5) vs the
55 * offset passed to interpolateAtOffset(). See
56 * dEQP-GLES31.functional.shaders.multisample_interpolation.interpolate_at_offset.at_sample_position.default_framebuffer
57 * for example.
58 */
59 nir_ssa_def *half = nir_imm_float(b, 0.5);
60 return nir_fadd(b, pos, nir_vec2(b, half, half));
61 }
62
63 static nir_ssa_def *
ir3_nir_lower_load_barycentric_at_sample_instr(nir_builder * b,nir_instr * instr,void * data)64 ir3_nir_lower_load_barycentric_at_sample_instr(nir_builder *b, nir_instr *instr,
65 void *data)
66 {
67 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
68
69 if (intr->intrinsic == nir_intrinsic_load_sample_pos)
70 return lower_load_sample_pos(b, intr);
71 else
72 return lower_load_barycentric_at_sample(b, intr);
73 }
74
75 static bool
ir3_nir_lower_load_barycentric_at_sample_filter(const nir_instr * instr,const void * data)76 ir3_nir_lower_load_barycentric_at_sample_filter(const nir_instr *instr,
77 const void *data)
78 {
79 if (instr->type != nir_instr_type_intrinsic)
80 return false;
81 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
82 return (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample ||
83 intr->intrinsic == nir_intrinsic_load_sample_pos);
84 }
85
86 bool
ir3_nir_lower_load_barycentric_at_sample(nir_shader * shader)87 ir3_nir_lower_load_barycentric_at_sample(nir_shader *shader)
88 {
89 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
90
91 return nir_shader_lower_instructions(
92 shader, ir3_nir_lower_load_barycentric_at_sample_filter,
93 ir3_nir_lower_load_barycentric_at_sample_instr, NULL);
94 }
95