• 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 void
lower_point_size_mov_after(nir_builder * b,nir_variable * in)35 lower_point_size_mov_after(nir_builder *b, nir_variable *in)
36 {
37    nir_def *load = nir_load_var(b, in);
38    load = nir_fclamp(b, nir_channel(b, load, 0), nir_channel(b, load, 1), nir_channel(b, load, 2));
39    if (b->shader->info.io_lowered) {
40       nir_store_output(b, load, nir_imm_int(b, 0),
41                        .io_semantics.location = VARYING_SLOT_PSIZ);
42    } else {
43       nir_variable *out = NULL;
44       /* the existing output can't be removed in order to avoid breaking xfb.
45        * drivers must check var->data.explicit_location to find the original output
46        * and only emit that one for xfb
47        */
48       nir_foreach_shader_out_variable(var, b->shader) {
49          if (var->data.location == VARYING_SLOT_PSIZ && !var->data.explicit_location) {
50             out = var;
51             break;
52          }
53       }
54       if (!out) {
55          out = nir_create_variable_with_location(b->shader, nir_var_shader_out,
56                                                  VARYING_SLOT_PSIZ, glsl_float_type());
57       }
58       nir_store_var(b, out, load, 0x1);
59    }
60 }
61 
62 static bool
lower_point_size_mov(nir_builder * b,nir_intrinsic_instr * intr,void * data)63 lower_point_size_mov(nir_builder *b, nir_intrinsic_instr *intr, void *data)
64 {
65    nir_variable *var = NULL;
66    switch (intr->intrinsic) {
67    case nir_intrinsic_store_output:
68    case nir_intrinsic_store_per_vertex_output:
69    case nir_intrinsic_store_per_view_output:
70    case nir_intrinsic_store_per_primitive_output: {
71       nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
72       if (sem.location != VARYING_SLOT_PSIZ)
73          return false;
74       break;
75    }
76    case nir_intrinsic_store_deref:
77       var = nir_intrinsic_get_var(intr, 0);
78       if (var->data.location != VARYING_SLOT_PSIZ)
79          return false;
80       break;
81    default:
82       return false;
83    }
84    b->cursor = nir_after_instr(&intr->instr);
85    lower_point_size_mov_after(b, data);
86    if (var && !var->data.explicit_location)
87       nir_instr_remove(&intr->instr);
88    return true;
89 }
90 
91 bool
nir_lower_point_size_mov(nir_shader * shader,const gl_state_index16 * pointsize_state_tokens)92 nir_lower_point_size_mov(nir_shader *shader,
93                          const gl_state_index16 *pointsize_state_tokens)
94 {
95    assert(shader->info.stage != MESA_SHADER_FRAGMENT &&
96           shader->info.stage != MESA_SHADER_COMPUTE);
97 
98 
99    bool progress = false;
100    nir_metadata preserved = nir_metadata_control_flow;
101    nir_variable *in = nir_state_variable_create(shader, glsl_vec4_type(),
102                                                 "gl_PointSizeClampedMESA",
103                                                 pointsize_state_tokens);
104    if (shader->info.outputs_written & VARYING_BIT_PSIZ) {
105       progress = nir_shader_intrinsics_pass(shader, lower_point_size_mov, preserved, in);
106    } else {
107       nir_function_impl *impl = nir_shader_get_entrypoint(shader);
108       nir_builder b = nir_builder_at(nir_before_impl(impl));
109 
110       lower_point_size_mov_after(&b, in);
111       shader->info.outputs_written |= VARYING_BIT_PSIZ;
112       progress = true;
113       nir_metadata_preserve(impl, preserved);
114    }
115    return progress;
116 }
117