• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Collabora Ltd
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 /** nir_lower_point_size_mov.c
28  *
29  * This pass lowers glPointSize into gl_PointSize, by adding a uniform
30  * and a move from that uniform to VARYING_SLOT_PSIZ. This is useful for
31  * OpenGL ES level hardware that lack constant point-size hardware state.
32  */
33 
34 static bool
lower_impl(nir_function_impl * impl,const gl_state_index16 * pointsize_state_tokens,nir_variable * out)35 lower_impl(nir_function_impl *impl,
36            const gl_state_index16 *pointsize_state_tokens,
37            nir_variable *out)
38 {
39    nir_shader *shader = impl->function->shader;
40    nir_builder b;
41    nir_variable *in, *new_out = NULL;
42 
43    b = nir_builder_create(impl);
44 
45    in = nir_state_variable_create(shader, glsl_vec4_type(),
46                                   "gl_PointSizeClampedMESA",
47                                   pointsize_state_tokens);
48 
49    /* the existing output can't be removed in order to avoid breaking xfb.
50     * drivers must check var->data.explicit_location to find the original output
51     * and only emit that one for xfb
52     */
53    if (!out || out->data.explicit_location) {
54       new_out = nir_create_variable_with_location(shader, nir_var_shader_out,
55                                                   VARYING_SLOT_PSIZ, glsl_float_type());
56    }
57 
58    if (!out) {
59       b.cursor = nir_before_impl(impl);
60       nir_def *load = nir_load_var(&b, in);
61       load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
62       nir_store_var(&b, new_out, load, 0x1);
63    } else {
64       bool found = false;
65       nir_foreach_block_safe(block, impl) {
66          nir_foreach_instr_safe(instr, block) {
67             if (instr->type == nir_instr_type_intrinsic) {
68                nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
69                if (intr->intrinsic == nir_intrinsic_store_deref) {
70                   nir_variable *var = nir_intrinsic_get_var(intr, 0);
71                   if (var == out) {
72                      b.cursor = nir_after_instr(instr);
73                      nir_def *load = nir_load_var(&b, in);
74                      load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
75                      nir_store_var(&b, new_out ? new_out : out, load, 0x1);
76                      found = true;
77                   }
78                }
79             }
80          }
81       }
82       if (!found) {
83          b.cursor = nir_before_impl(impl);
84          nir_def *load = nir_load_var(&b, in);
85          load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
86          nir_store_var(&b, new_out, load, 0x1);
87       }
88    }
89 
90    nir_metadata_preserve(impl, nir_metadata_block_index |
91                                   nir_metadata_dominance);
92    return true;
93 }
94 
95 bool
nir_lower_point_size_mov(nir_shader * shader,const gl_state_index16 * pointsize_state_tokens)96 nir_lower_point_size_mov(nir_shader *shader,
97                          const gl_state_index16 *pointsize_state_tokens)
98 {
99    assert(shader->info.stage != MESA_SHADER_FRAGMENT &&
100           shader->info.stage != MESA_SHADER_COMPUTE);
101 
102    nir_variable *out =
103       nir_find_variable_with_location(shader, nir_var_shader_out,
104                                       VARYING_SLOT_PSIZ);
105 
106    return lower_impl(nir_shader_get_entrypoint(shader), pointsize_state_tokens,
107                      out);
108 }
109