2 * Copyright © 2015 Red Hat
3 * Copyright © 2016 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "nir_builder.h"
27 #include "program/prog_instruction.h"
30 * This pass adds <0.5, 0.5> to all uses of gl_FragCoord.
32 * Run before nir_lower_io().
34 * For a more full featured pass, consider using nir_lower_wpos_ytransform(),
35 * which can handle pixel center integer / half integer, and origin lower
36 * left / upper left transformations.
38 * This simple pass is primarily intended for use by Vulkan drivers on
39 * hardware which provides an integer pixel center. Vulkan mandates that
40 * the pixel center must be half-integer, and also that the coordinate
41 * system's origin must be upper left. This means that there's no need
42 * for a uniform - we can always just add a constant. In the case that
43 * sample shading is enabled, Vulkan expects FragCoord to include sample
48 update_fragcoord(nir_builder
*b
, nir_intrinsic_instr
*intr
,
49 const bool for_sample_shading
)
51 nir_ssa_def
*wpos
= &intr
->dest
.ssa
;
53 assert(intr
->dest
.is_ssa
);
55 b
->cursor
= nir_after_instr(&intr
->instr
);
57 if (!for_sample_shading
) {
58 wpos
= nir_fadd(b
, wpos
, nir_imm_vec4(b
, 0.5f
, 0.5f
, 0.0f
, 0.0f
));
60 nir_ssa_def
*spos
= nir_load_sample_pos(b
);
62 wpos
= nir_fadd(b
, wpos
,
64 nir_channel(b
, spos
, 0),
65 nir_channel(b
, spos
, 1),
66 nir_imm_float(b
, 0.0f
),
67 nir_imm_float(b
, 0.0f
)));
70 nir_ssa_def_rewrite_uses_after(&intr
->dest
.ssa
, nir_src_for_ssa(wpos
),
75 lower_wpos_center_block(nir_builder
*b
, nir_block
*block
,
76 const bool for_sample_shading
)
78 bool progress
= false;
80 nir_foreach_instr(instr
, block
) {
81 if (instr
->type
== nir_instr_type_intrinsic
) {
82 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
83 if (intr
->intrinsic
== nir_intrinsic_load_frag_coord
) {
84 update_fragcoord(b
, intr
, for_sample_shading
);
94 nir_lower_wpos_center(nir_shader
*shader
, const bool for_sample_shading
)
96 bool progress
= false;
99 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
101 nir_foreach_function(function
, shader
) {
102 if (function
->impl
) {
103 nir_builder_init(&b
, function
->impl
);
105 nir_foreach_block(block
, function
->impl
) {
106 progress
= lower_wpos_center_block(&b
, block
, for_sample_shading
) ||
109 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
110 nir_metadata_dominance
);