• 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;
42 
43    nir_builder_init(&b, impl);
44    b.cursor = nir_before_cf_list(&impl->body);
45 
46    in = nir_variable_create(shader, nir_var_uniform,
47                             glsl_float_type(), "gl_PointSizeClampedMESA");
48    in->num_state_slots = 1;
49    in->state_slots = ralloc_array(in, nir_state_slot, 1);
50    memcpy(in->state_slots[0].tokens,
51          pointsize_state_tokens,
52          sizeof(in->state_slots[0].tokens));
53 
54    if (!out) {
55       out = nir_variable_create(shader, nir_var_shader_out,
56                                 glsl_float_type(), "gl_PointSize");
57       out->data.location = VARYING_SLOT_PSIZ;
58    }
59 
60    nir_copy_var(&b, out, in);
61 
62    nir_metadata_preserve(impl, nir_metadata_block_index |
63                                nir_metadata_dominance);
64    return true;
65 }
66 
67 void
nir_lower_point_size_mov(nir_shader * shader,const gl_state_index16 * pointsize_state_tokens)68 nir_lower_point_size_mov(nir_shader *shader,
69                          const gl_state_index16 *pointsize_state_tokens)
70 {
71    assert(shader->info.stage != MESA_SHADER_FRAGMENT &&
72           shader->info.stage != MESA_SHADER_COMPUTE);
73 
74    nir_variable *out =
75       nir_find_variable_with_location(shader, nir_var_shader_out,
76                                       VARYING_SLOT_PSIZ);
77 
78    lower_impl(nir_shader_get_entrypoint(shader), pointsize_state_tokens,
79               out);
80 }
81