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 bool
nir_lower_fb_read_instr(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * cb_data)50 nir_lower_fb_read_instr(nir_builder *b, nir_intrinsic_instr *intr,
51 UNUSED void *cb_data)
52 {
53 if (intr->intrinsic != nir_intrinsic_load_output)
54 return false;
55
56 b->cursor = nir_before_instr(&intr->instr);
57
58 nir_def *fragcoord = nir_load_frag_coord(b);
59 nir_def *sampid = nir_load_sample_id(b);
60 nir_def *layer = nir_load_layer_id(b);
61 fragcoord = nir_f2i32(b, fragcoord);
62
63 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3);
64 tex->op = nir_texop_txf_ms_fb;
65 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
66 tex->coord_components = 3;
67 tex->dest_type = nir_type_float32;
68 tex->is_array = true;
69 tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_coord,
70 nir_vec3(b, nir_channel(b, fragcoord, 0), nir_channel(b, fragcoord, 1), layer));
71 tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_ms_index, sampid);
72 struct nir_io_semantics io = nir_intrinsic_io_semantics(intr);
73 tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_texture_handle,
74 nir_imm_intN_t(b, io.location - FRAG_RESULT_DATA0, 32));
75
76 nir_def_init(&tex->instr, &tex->def, 4, 32);
77 nir_builder_instr_insert(b, &tex->instr);
78
79 nir_def_rewrite_uses(&intr->def, &tex->def);
80
81 return true;
82 }
83
84 bool
nir_lower_fb_read(nir_shader * shader)85 nir_lower_fb_read(nir_shader *shader)
86 {
87 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
88
89 return nir_shader_intrinsics_pass(shader, nir_lower_fb_read_instr,
90 nir_metadata_control_flow,
91 NULL);
92 }
93