1 /*
2 * Copyright 2018-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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "nir_builder.h"
25
26 static bool
lower_pos_write(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * cb_data)27 lower_pos_write(nir_builder *b, nir_intrinsic_instr *intr,
28 UNUSED void *cb_data)
29 {
30 if (intr->intrinsic != nir_intrinsic_store_deref)
31 return false;
32
33 nir_variable *var = nir_intrinsic_get_var(intr, 0);
34 if (var->data.mode != nir_var_shader_out ||
35 var->data.location != VARYING_SLOT_POS)
36 return false;
37
38 b->cursor = nir_before_instr(&intr->instr);
39
40 nir_def *pos = intr->src[1].ssa;
41 nir_def *def = nir_vec4(b,
42 nir_channel(b, pos, 0),
43 nir_channel(b, pos, 1),
44 nir_fmul_imm(b,
45 nir_fadd(b,
46 nir_channel(b, pos, 2),
47 nir_channel(b, pos, 3)),
48 0.5),
49 nir_channel(b, pos, 3));
50 nir_src_rewrite(intr->src + 1, def);
51 return true;
52 }
53
54 void
nir_lower_clip_halfz(nir_shader * shader)55 nir_lower_clip_halfz(nir_shader *shader)
56 {
57 if (shader->info.stage != MESA_SHADER_VERTEX &&
58 shader->info.stage != MESA_SHADER_GEOMETRY &&
59 shader->info.stage != MESA_SHADER_TESS_EVAL)
60 return;
61
62 nir_shader_intrinsics_pass(shader, lower_pos_write,
63 nir_metadata_control_flow,
64 NULL);
65 }
66