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 nir_builder_init(&b, impl);
44
45 in = nir_variable_create(shader, nir_var_uniform,
46 glsl_vec4_type(), "gl_PointSizeClampedMESA");
47 in->num_state_slots = 1;
48 in->state_slots = ralloc_array(in, nir_state_slot, 1);
49 in->state_slots[0].swizzle = BITFIELD_MASK(4);
50 memcpy(in->state_slots[0].tokens,
51 pointsize_state_tokens,
52 sizeof(in->state_slots[0].tokens));
53
54 /* the existing output can't be removed in order to avoid breaking xfb.
55 * drivers must check var->data.explicit_location to find the original output
56 * and only emit that one for xfb
57 */
58 if (!out || out->data.explicit_location) {
59 new_out = nir_variable_create(shader, nir_var_shader_out,
60 glsl_float_type(), "gl_PointSizeMESA");
61 new_out->data.location = VARYING_SLOT_PSIZ;
62 }
63
64
65 if (!out) {
66 b.cursor = nir_before_cf_list(&impl->body);
67 nir_ssa_def *load = nir_load_var(&b, in);
68 load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
69 nir_store_var(&b, new_out, load, 0x1);
70 } else {
71 bool found = false;
72 nir_foreach_block_safe(block, impl) {
73 nir_foreach_instr_safe(instr, block) {
74 if (instr->type == nir_instr_type_intrinsic) {
75 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
76 if (intr->intrinsic == nir_intrinsic_store_deref) {
77 nir_variable *var = nir_intrinsic_get_var(intr, 0);
78 if (var == out) {
79 b.cursor = nir_after_instr(instr);
80 nir_ssa_def *load = nir_load_var(&b, in);
81 load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
82 nir_store_var(&b, new_out ? new_out : out, load, 0x1);
83 found = true;
84 }
85 }
86 }
87 }
88 }
89 if (!found) {
90 b.cursor = nir_before_cf_list(&impl->body);
91 nir_ssa_def *load = nir_load_var(&b, in);
92 load = nir_fclamp(&b, nir_channel(&b, load, 0), nir_channel(&b, load, 1), nir_channel(&b, load, 2));
93 nir_store_var(&b, new_out, load, 0x1);
94 }
95 }
96
97 nir_metadata_preserve(impl, nir_metadata_block_index |
98 nir_metadata_dominance);
99 return true;
100 }
101
102 void
nir_lower_point_size_mov(nir_shader * shader,const gl_state_index16 * pointsize_state_tokens)103 nir_lower_point_size_mov(nir_shader *shader,
104 const gl_state_index16 *pointsize_state_tokens)
105 {
106 assert(shader->info.stage != MESA_SHADER_FRAGMENT &&
107 shader->info.stage != MESA_SHADER_COMPUTE);
108
109 nir_variable *out =
110 nir_find_variable_with_location(shader, nir_var_shader_out,
111 VARYING_SLOT_PSIZ);
112
113 lower_impl(nir_shader_get_entrypoint(shader), pointsize_state_tokens,
114 out);
115 }
116