2 * Copyright (C) 2019 Andreas Baierl
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:
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
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
25 #include "nir_builder.h"
27 /* Lower gl_FragCoord and transform the w component
28 * according to the following pseudocode:
30 * gl_FragCoord.xyz = gl_FragCoord_orig.xyz
31 * gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w
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.
38 lower_fragcoord_wtrans(nir_builder
*b
, nir_intrinsic_instr
*intr
)
40 assert(intr
->dest
.is_ssa
);
42 b
->cursor
= nir_before_instr(&intr
->instr
);
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),
51 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
,
52 nir_src_for_ssa(fragcoord_wtrans
));
56 nir_lower_fragcoord_wtrans(nir_shader
*shader
)
58 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
60 nir_foreach_function(func
, shader
) {
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
)
71 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
72 if (intr
->intrinsic
!= nir_intrinsic_load_frag_coord
)
75 lower_fragcoord_wtrans(&b
, intr
);
78 nir_metadata_preserve(func
->impl
, nir_metadata_block_index
|
79 nir_metadata_dominance
);