1 /*
2 * Copyright © 2019 Raspberry Pi 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_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 static bool
lower_point_size_intrin(nir_builder * b,nir_intrinsic_instr * intr,void * data)36 lower_point_size_intrin(nir_builder *b, nir_intrinsic_instr *intr, void *data)
37 {
38 float *minmax = (float *)data;
39
40 gl_varying_slot location = VARYING_SLOT_MAX;
41 nir_src *psiz_src;
42
43 if (intr->intrinsic == nir_intrinsic_store_deref) {
44 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
45 nir_variable *var = nir_deref_instr_get_variable(deref);
46 location = var->data.location;
47 psiz_src = &intr->src[1];
48 } else if (intr->intrinsic == nir_intrinsic_store_output) {
49 location = nir_intrinsic_io_semantics(intr).location;
50 psiz_src = &intr->src[0];
51 }
52
53 if (location != VARYING_SLOT_PSIZ)
54 return false;
55
56 b->cursor = nir_before_instr(&intr->instr);
57
58 nir_def *psiz = psiz_src->ssa;
59 assert(psiz->num_components == 1);
60
61 if (minmax[0] > 0.0f)
62 psiz = nir_fmax(b, psiz, nir_imm_float(b, minmax[0]));
63
64 if (minmax[1] > 0.0f)
65 psiz = nir_fmin(b, psiz, nir_imm_float(b, minmax[1]));
66
67 nir_src_rewrite(psiz_src, psiz);
68
69 return true;
70 }
71
72 /**
73 * Clamps gl_PointSize to the range [min, max]. If either min or max are not
74 * greater than 0 then no clamping is done for that side of the range.
75 */
76 bool
nir_lower_point_size(nir_shader * s,float min,float max)77 nir_lower_point_size(nir_shader *s, float min, float max)
78 {
79 assert(s->info.stage != MESA_SHADER_FRAGMENT &&
80 s->info.stage != MESA_SHADER_COMPUTE);
81
82 assert(min > 0.0f || max > 0.0f);
83 assert(min <= 0.0f || max <= 0.0f || min <= max);
84
85 float minmax[] = { min, max };
86 return nir_shader_intrinsics_pass(s, lower_point_size_intrin,
87 nir_metadata_control_flow,
88 minmax);
89 }
90