nir/scheduler: Move nir_scheduler to its own header
[mesa.git] / src / compiler / nir / nir_lower_fragcoord_wtrans.c
1 /*
2 * Copyright (C) 2019 Andreas Baierl
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 /* Lower gl_FragCoord and transform the w component
28 * according to the following pseudocode:
29 *
30 * gl_FragCoord.xyz = gl_FragCoord_orig.xyz
31 * gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w
32 *
33 * To trigger the transformation, gl_FragCoord currently has to be treated
34 * as a system value with PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL enabled.
35 */
36
37 static void
38 lower_fragcoord_wtrans(nir_builder *b, nir_intrinsic_instr *intr)
39 {
40 assert(intr->dest.is_ssa);
41
42 b->cursor = nir_before_instr(&intr->instr);
43
44 nir_ssa_def *fragcoord_in = nir_load_frag_coord(b);
45 nir_ssa_def *w_rcp = nir_frcp(b, nir_channel(b, fragcoord_in, 3));
46 nir_ssa_def *fragcoord_wtrans = nir_vec4(b,
47 nir_channel(b, fragcoord_in, 0),
48 nir_channel(b, fragcoord_in, 1),
49 nir_channel(b, fragcoord_in, 2),
50 w_rcp);
51 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
52 nir_src_for_ssa(fragcoord_wtrans));
53 }
54
55 void
56 nir_lower_fragcoord_wtrans(nir_shader *shader)
57 {
58 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
59
60 nir_foreach_function(func, shader) {
61 if (!func->impl)
62 continue;
63
64 nir_builder b;
65 nir_builder_init(&b, func->impl);
66 nir_foreach_block(block, func->impl) {
67 nir_foreach_instr_safe(instr, block) {
68 if (instr->type != nir_instr_type_intrinsic)
69 continue;
70
71 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
72 if (intr->intrinsic != nir_intrinsic_load_frag_coord)
73 continue;
74
75 lower_fragcoord_wtrans(&b, intr);
76 }
77 }
78 nir_metadata_preserve(func->impl, nir_metadata_block_index |
79 nir_metadata_dominance);
80 }
81 }