nir: add a pass to clamp gl_PointSize to a range
[mesa.git] / src / compiler / nir / nir_lower_point_size.c
1 /*
2 * Copyright © 2019 Raspberry Pi
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_builder.h"
25
26 /** @file nir_lower_point_size.c
27 *
28 * The OpenGL spec requires that implementations clamp gl_PointSize to an
29 * implementation-dependant point size range. The OpenGL ES 3.0 spec further
30 * requires that this range must match GL_ALIASED_POINT_SIZE_RANGE.
31 * Some hardware such as V3D don't clamp to a valid range automatically so
32 * the driver must clamp the point size written by the shader manually to a
33 * valid range.
34 */
35
36 static void
37 lower_point_size_instr(nir_builder *b, nir_instr *psiz_instr,
38 float min, float max)
39 {
40 b->cursor = nir_before_instr(psiz_instr);
41
42 nir_intrinsic_instr *instr = nir_instr_as_intrinsic(psiz_instr);
43
44 /* Some fixed function vertex programs generate PSIZ as a vec4
45 * instead of a scalar, where the actual point size is stored in the
46 * first component.
47 */
48 assert(instr->src[1].is_ssa);
49 nir_ssa_def *psiz = nir_channel(b, instr->src[1].ssa, 0);
50
51 if (min > 0.0f)
52 psiz = nir_fmax(b, psiz, nir_imm_float(b, min));
53
54 if (max > 0.0f)
55 psiz = nir_fmin(b, psiz, nir_imm_float(b, max));
56
57 nir_ssa_def *src_chans[4];
58 src_chans[0] = psiz;
59 for (int i = 1; i < instr->src[1].ssa->num_components; i++)
60 src_chans[i] = nir_channel(b, instr->src[1].ssa, i);
61 nir_ssa_def *lowered_src =
62 nir_vec(b, src_chans, instr->src[1].ssa->num_components);
63
64 nir_instr_rewrite_src(&instr->instr, &instr->src[1],
65 nir_src_for_ssa(lowered_src));
66 }
67
68 static bool
69 instr_is_point_size(const nir_instr *instr)
70 {
71 if (instr->type != nir_instr_type_intrinsic)
72 return false;
73
74 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
75 if (intr->intrinsic != nir_intrinsic_store_deref)
76 return false;
77
78 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
79 nir_variable *var = nir_deref_instr_get_variable(deref);
80 if (var->data.location != VARYING_SLOT_PSIZ)
81 return false;
82
83 return true;
84 }
85
86 /**
87 * Clamps gl_PointSize to the range [min, max]. If either min or max are not
88 * greater than 0 then no clamping is done for that side of the range.
89 */
90 bool
91 nir_lower_point_size(nir_shader *s, float min, float max)
92 {
93 assert(s->info.stage != MESA_SHADER_FRAGMENT &&
94 s->info.stage != MESA_SHADER_COMPUTE);
95
96 assert(min > 0.0f || max > 0.0f);
97 assert(min <= 0.0f || max <= 0.0f || min <= max);
98
99 bool progress = false;
100 nir_foreach_function(function, s) {
101 if (!function->impl)
102 continue;
103
104 nir_builder b;
105 nir_builder_init(&b, function->impl);
106
107 nir_foreach_block(block, function->impl) {
108 nir_foreach_instr_safe(instr, block) {
109 if (instr_is_point_size(instr)) {
110 lower_point_size_instr(&b, instr, min, max);
111 progress = true;
112 }
113 }
114 }
115
116 if (progress) {
117 nir_metadata_preserve(function->impl,
118 nir_metadata_block_index |
119 nir_metadata_dominance);
120 }
121 }
122
123 return progress;
124 }