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 nir_store_output(b, load, nir_imm_int(b, 0),
40 .io_semantics.location = VARYING_SLOT_PSIZ);
41 }
42
43 static bool
lower_point_size_mov(nir_builder * b,nir_intrinsic_instr * intr,void * data)44 lower_point_size_mov(nir_builder *b, nir_intrinsic_instr *intr, void *data)
45 {
46 nir_variable *var = NULL;
47 switch (intr->intrinsic) {
48 case nir_intrinsic_store_output:
49 case nir_intrinsic_store_per_vertex_output:
50 case nir_intrinsic_store_per_view_output:
51 case nir_intrinsic_store_per_primitive_output: {
52 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
53 if (sem.location != VARYING_SLOT_PSIZ)
54 return false;
55 break;
56 }
57 default:
58 return false;
59 }
60 b->cursor = nir_after_instr(&intr->instr);
61 lower_point_size_mov_after(b, data);
62 if (var && !var->data.explicit_location)
63 nir_instr_remove(&intr->instr);
64 return true;
65 }
66
67 bool
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 assert(shader->info.io_lowered);
74
75 bool progress = false;
76 nir_metadata preserved = nir_metadata_control_flow;
77 nir_variable *in = nir_state_variable_create(shader, glsl_vec4_type(),
78 "gl_PointSizeClampedMESA",
79 pointsize_state_tokens);
80 if (shader->info.outputs_written & VARYING_BIT_PSIZ) {
81 progress = nir_shader_intrinsics_pass(shader, lower_point_size_mov, preserved, in);
82 } else {
83 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
84 nir_builder b = nir_builder_at(nir_before_impl(impl));
85
86 lower_point_size_mov_after(&b, in);
87 shader->info.outputs_written |= VARYING_BIT_PSIZ;
88 progress = true;
89 nir_metadata_preserve(impl, preserved);
90 }
91 return progress;
92 }
93