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 unsigned y_swizzle = 1;
64
65 if (nir_intrinsic_has_component(intr)) {
66 unsigned component = nir_intrinsic_component(intr);
67
68 assert(component <= 1);
69 assert(component + intr->num_components <= 2);
70 if (component + intr->num_components == 1)
71 return; /* only gl_PointCoord.x is loaded */
72
73 y_swizzle -= component;
74 }
75
76 nir_def *pntc = &intr->def;
77 nir_def *transform = get_pntc_transform(state);
78 nir_def *flipped_y = nir_ffma(b, nir_channel(b, pntc, y_swizzle),
79 /* Flip the sign of y if we're flipping. */
80 nir_channel(b, transform, 0),
81 /* The offset is 1 if we're flipping, 0 otherwise. */
82 nir_channel(b, transform, 1));
83
84 /* Reassemble the vector. */
85 pntc = nir_vector_insert_imm(b, pntc, flipped_y, y_swizzle);
86 nir_def_rewrite_uses_after(&intr->def, pntc, pntc->parent_instr);
87 }
88
89 static void
lower_pntc_ytransform_block(lower_pntc_ytransform_state * state,nir_block * block)90 lower_pntc_ytransform_block(lower_pntc_ytransform_state *state,
91 nir_block *block)
92 {
93 nir_foreach_instr_safe(instr, block) {
94 if (instr->type == nir_instr_type_intrinsic) {
95 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
96 if (intr->intrinsic == nir_intrinsic_load_deref) {
97 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
98 nir_variable *var = nir_deref_instr_get_variable(deref);
99
100 if ((var->data.mode == nir_var_shader_in &&
101 var->data.location == VARYING_SLOT_PNTC) ||
102 (var->data.mode == nir_var_system_value &&
103 var->data.location == SYSTEM_VALUE_POINT_COORD)) {
104 lower_load_pointcoord(state, intr);
105 }
106 }
107
108 if (intr->intrinsic == nir_intrinsic_load_interpolated_input &&
109 nir_intrinsic_io_semantics(intr).location == VARYING_SLOT_PNTC)
110 lower_load_pointcoord(state, intr);
111 }
112 }
113 }
114
115 bool
nir_lower_pntc_ytransform(nir_shader * shader,const gl_state_index16 pntc_state_tokens[][STATE_LENGTH])116 nir_lower_pntc_ytransform(nir_shader *shader,
117 const gl_state_index16 pntc_state_tokens[][STATE_LENGTH])
118 {
119 if (!shader->options->lower_wpos_pntc)
120 return false;
121
122 lower_pntc_ytransform_state state = {
123 .pntc_state_tokens = *pntc_state_tokens,
124 .shader = shader,
125 .pntc_transform = NULL,
126 };
127
128 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
129
130 nir_foreach_function_impl(impl, shader) {
131 state.b = nir_builder_create(impl);
132
133 nir_foreach_block(block, impl) {
134 lower_pntc_ytransform_block(&state, block);
135 }
136 nir_metadata_preserve(impl, nir_metadata_control_flow);
137 }
138
139 return state.pntc_transform != NULL;
140 }
141