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