1 /*
2 * Copyright © 2021 Intel Corporation
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 "nir.h"
25 #include "nir_builder.h"
26
27 static bool
lower_single_sampled_instr(nir_builder * b,nir_instr * instr,UNUSED void * cb_data)28 lower_single_sampled_instr(nir_builder *b,
29 nir_instr *instr,
30 UNUSED void *cb_data)
31 {
32 if (instr->type != nir_instr_type_intrinsic)
33 return false;
34
35 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
36
37 nir_ssa_def *lowered;
38 switch (intrin->intrinsic) {
39 case nir_intrinsic_load_sample_id:
40 b->cursor = nir_before_instr(instr);
41 lowered = nir_imm_int(b, 0);
42 break;
43
44 case nir_intrinsic_load_sample_pos:
45 b->cursor = nir_before_instr(instr);
46 lowered = nir_imm_vec2(b, 0.5, 0.5);
47 break;
48
49 case nir_intrinsic_load_sample_mask_in:
50 /* Don't lower to helper invocations if helper invocations are going
51 * to be lowered right back to sample mask.
52 */
53 if (b->shader->options->lower_helper_invocation)
54 return false;
55
56 b->cursor = nir_before_instr(instr);
57 lowered = nir_b2i32(b, nir_inot(b, nir_load_helper_invocation(b, 1)));
58 break;
59
60 case nir_intrinsic_interp_deref_at_centroid:
61 case nir_intrinsic_interp_deref_at_sample:
62 b->cursor = nir_before_instr(instr);
63 assert(intrin->src[0].is_ssa);
64 lowered = nir_load_deref(b, nir_src_as_deref(intrin->src[0]));
65 break;
66
67 case nir_intrinsic_load_barycentric_centroid:
68 case nir_intrinsic_load_barycentric_sample:
69 case nir_intrinsic_load_barycentric_at_sample:
70 b->cursor = nir_before_instr(instr);
71 lowered = nir_load_barycentric(b, nir_intrinsic_load_barycentric_pixel,
72 nir_intrinsic_interp_mode(intrin));
73
74 if (nir_intrinsic_interp_mode(intrin) == INTERP_MODE_NOPERSPECTIVE) {
75 BITSET_SET(b->shader->info.system_values_read,
76 SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL);
77 } else {
78 BITSET_SET(b->shader->info.system_values_read,
79 SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL);
80 }
81 break;
82
83 default:
84 return false;
85 }
86
87 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, lowered);
88 nir_instr_remove(instr);
89 return true;
90 }
91
92 /* Assume the fragment shader is single-sampled and lower accordingly
93 *
94 * This drops sample/centroid qualifiers from all input variables, forces
95 * barycentrics to pixel, and constant-folds various built-ins.
96 */
97 bool
nir_lower_single_sampled(nir_shader * shader)98 nir_lower_single_sampled(nir_shader *shader)
99 {
100 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
101
102 bool progress = false;
103 nir_foreach_shader_in_variable(var, shader) {
104 if (var->data.sample) {
105 var->data.sample = false;
106 progress = true;
107 }
108 if (var->data.centroid) {
109 var->data.centroid = false;
110 progress = true;
111 }
112 }
113
114 /* We're going to get rid of any uses of these */
115 BITSET_CLEAR(shader->info.system_values_read,
116 SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE);
117 BITSET_CLEAR(shader->info.system_values_read,
118 SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID);
119 BITSET_CLEAR(shader->info.system_values_read,
120 SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE);
121 BITSET_CLEAR(shader->info.system_values_read,
122 SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID);
123
124 return nir_shader_instructions_pass(shader, lower_single_sampled_instr,
125 nir_metadata_block_index |
126 nir_metadata_dominance,
127 NULL) || progress;
128 }
129