xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_pntc_ytransform.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Igalia S.L.
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /* Lower gl_PointCoord to account for user requested point-coord origin
28  * and for whether draw buffer is flipped.
29  */
30 
31 typedef struct {
32    const gl_state_index16 *pntc_state_tokens;
33    nir_shader *shader;
34    nir_builder b;
35    nir_variable *pntc_transform;
36 } lower_pntc_ytransform_state;
37 
38 static nir_def *
get_pntc_transform(lower_pntc_ytransform_state * state)39 get_pntc_transform(lower_pntc_ytransform_state *state)
40 {
41    if (state->pntc_transform == NULL) {
42       /* NOTE: name must be prefixed w/ "gl_" to trigger slot based
43        * special handling in uniform setup:
44        */
45       nir_variable *var = nir_state_variable_create(state->shader,
46                                                     glsl_vec4_type(),
47                                                     "gl_PntcYTransform",
48                                                     state->pntc_state_tokens);
49 
50       var->data.how_declared = nir_var_hidden;
51       state->pntc_transform = var;
52    }
53    return nir_load_var(&state->b, state->pntc_transform);
54 }
55 
56 static void
lower_load_pointcoord(lower_pntc_ytransform_state * state,nir_intrinsic_instr * intr)57 lower_load_pointcoord(lower_pntc_ytransform_state *state,
58                       nir_intrinsic_instr *intr)
59 {
60    nir_builder *b = &state->b;
61    b->cursor = nir_after_instr(&intr->instr);
62 
63    nir_def *pntc = &intr->def;
64    nir_def *transform = get_pntc_transform(state);
65    nir_def *y = nir_channel(b, pntc, 1);
66    /* The offset is 1 if we're flipping, 0 otherwise. */
67    nir_def *offset = nir_channel(b, transform, 1);
68    /* Flip the sign of y if we're flipping. */
69    nir_def *scaled = nir_fmul(b, y, nir_channel(b, transform, 0));
70 
71    /* Reassemble the vector. */
72    nir_def *flipped_pntc = nir_vec2(b,
73                                     nir_channel(b, pntc, 0),
74                                     nir_fadd(b, offset, scaled));
75 
76    nir_def_rewrite_uses_after(&intr->def, flipped_pntc,
77                               flipped_pntc->parent_instr);
78 }
79 
80 static void
lower_pntc_ytransform_block(lower_pntc_ytransform_state * state,nir_block * block)81 lower_pntc_ytransform_block(lower_pntc_ytransform_state *state,
82                             nir_block *block)
83 {
84    nir_foreach_instr_safe(instr, block) {
85       if (instr->type == nir_instr_type_intrinsic) {
86          nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
87          if (intr->intrinsic == nir_intrinsic_load_deref) {
88             nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
89             nir_variable *var = nir_deref_instr_get_variable(deref);
90 
91             if ((var->data.mode == nir_var_shader_in &&
92                  var->data.location == VARYING_SLOT_PNTC) ||
93                 (var->data.mode == nir_var_system_value &&
94                  var->data.location == SYSTEM_VALUE_POINT_COORD)) {
95                lower_load_pointcoord(state, intr);
96             }
97          }
98       }
99    }
100 }
101 
102 bool
nir_lower_pntc_ytransform(nir_shader * shader,const gl_state_index16 pntc_state_tokens[][STATE_LENGTH])103 nir_lower_pntc_ytransform(nir_shader *shader,
104                           const gl_state_index16 pntc_state_tokens[][STATE_LENGTH])
105 {
106    if (!shader->options->lower_wpos_pntc)
107       return false;
108 
109    lower_pntc_ytransform_state state = {
110       .pntc_state_tokens = *pntc_state_tokens,
111       .shader = shader,
112       .pntc_transform = NULL,
113    };
114 
115    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
116 
117    nir_foreach_function_impl(impl, shader) {
118       state.b = nir_builder_create(impl);
119 
120       nir_foreach_block(block, impl) {
121          lower_pntc_ytransform_block(&state, block);
122       }
123       nir_metadata_preserve(impl, nir_metadata_control_flow);
124    }
125 
126    return state.pntc_transform != NULL;
127 }
128