1 /*
2 * Copyright © 2019 Google, Inc.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 /* Lowing for fragment shader load_output.
28 *
29 * This pass supports the blend_equation_advanced, where a fragment
30 * shader loads the output (fragcolor) to read the current framebuffer.
31 * It does this by lowering the output read to a txf_ms_fb instruction.
32 * This instruction works similarly to a normal txf_ms except without
33 * taking a texture source argument. (The driver backend is expected
34 * to wire this up to a free texture slot which is configured to read
35 * from the framebuffer.)
36 *
37 * This should be run after lower_wpos_ytransform, because the tex
38 * coordinates should be the physical fragcoord, not the logical
39 * y-flipped coord.
40 *
41 * Note that this pass explicitly does *not* add a sampler uniform
42 * (as txf_ms_fb does not reference a texture). The driver backend
43 * is going to want nif->info.num_textures to include the count of
44 * number of textures *not* including the one it inserts to sample
45 * from the framebuffer, so it more easily knows where to insert the
46 * hidden texture to read from the fb.
47 */
48
49 static void
lower_fb_read(nir_builder * b,nir_intrinsic_instr * intr)50 lower_fb_read(nir_builder *b, nir_intrinsic_instr *intr)
51 {
52 b->cursor = nir_before_instr(&intr->instr);
53
54 nir_ssa_def *fragcoord = nir_load_frag_coord(b);
55 nir_ssa_def *sampid = nir_load_sample_id(b);
56
57 fragcoord = nir_f2i32(b, fragcoord);
58
59 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 2);
60 tex->op = nir_texop_txf_ms_fb;
61 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
62 tex->coord_components = 2;
63 tex->dest_type = nir_type_float;
64 tex->src[0].src_type = nir_tex_src_coord;
65 tex->src[0].src = nir_src_for_ssa(nir_channels(b, fragcoord, 0x3));
66 tex->src[1].src_type = nir_tex_src_ms_index;
67 tex->src[1].src = nir_src_for_ssa(sampid);
68
69 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
70 nir_builder_instr_insert(b, &tex->instr);
71
72 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(&tex->dest.ssa));
73 }
74
75 bool
nir_lower_fb_read(nir_shader * shader)76 nir_lower_fb_read(nir_shader *shader)
77 {
78 bool progress = false;
79
80 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
81
82 nir_foreach_function(function, shader) {
83 nir_function_impl *impl = function->impl;
84
85 if (!impl)
86 continue;
87
88 nir_foreach_block(block, impl) {
89 nir_foreach_instr_safe(instr, block) {
90 if (instr->type != nir_instr_type_intrinsic)
91 continue;
92
93 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
94 if (intr->intrinsic != nir_intrinsic_load_output)
95 continue;
96
97 /* TODO KHR_blend_equation_advanced is limited to non-MRT
98 * scenarios.. but possible there are other extensions
99 * where this pass would be useful that do support MRT?
100 *
101 * I guess for now I'll leave that as an exercise for the
102 * reader.
103 */
104 if (nir_intrinsic_base(intr) != 0 ||
105 nir_src_as_uint(intr->src[0]) != 0)
106 continue;
107
108 nir_builder b;
109 nir_builder_init(&b, impl);
110 lower_fb_read(&b, intr);
111 progress = true;
112 }
113 }
114
115 nir_metadata_preserve(impl, nir_metadata_block_index |
116 nir_metadata_dominance);
117
118 }
119
120 return progress;
121 }
122