freedreno: Include binning shaders in shader-db.
[mesa.git] / src / freedreno / ir3 / ir3_nir_lower_load_barycentric_at_sample.c
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "ir3_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * This pass lowers load_barycentric_at_sample to load_sample_pos_from_id
29 * plus load_barycentric_at_offset.
30 *
31 * It also lowers load_sample_pos to load_sample_pos_from_id, mostly because
32 * that needs to happen at the same early stage (before wpos_ytransform)
33 */
34
35 static nir_ssa_def *
36 load_sample_pos(nir_builder *b, nir_ssa_def *samp_id)
37 {
38 nir_intrinsic_instr *load_sp =
39 nir_intrinsic_instr_create(b->shader,
40 nir_intrinsic_load_sample_pos_from_id);
41 load_sp->num_components = 2;
42 load_sp->src[0] = nir_src_for_ssa(samp_id);
43 nir_ssa_dest_init(&load_sp->instr, &load_sp->dest, 2, 32, NULL);
44 nir_builder_instr_insert(b, &load_sp->instr);
45
46 return &load_sp->dest.ssa;
47 }
48
49 static void
50 lower_load_barycentric_at_sample(nir_builder *b, nir_intrinsic_instr *intr)
51 {
52 nir_ssa_def *pos = load_sample_pos(b, intr->src[0].ssa);
53
54 nir_intrinsic_instr *load_bary_at_offset =
55 nir_intrinsic_instr_create(b->shader,
56 nir_intrinsic_load_barycentric_at_offset);
57 load_bary_at_offset->num_components = 2;
58 load_bary_at_offset->src[0] = nir_src_for_ssa(pos);
59 nir_ssa_dest_init(&load_bary_at_offset->instr,
60 &load_bary_at_offset->dest, 2, 32, NULL);
61 nir_builder_instr_insert(b, &load_bary_at_offset->instr);
62
63 nir_ssa_def_rewrite_uses(&intr->dest.ssa,
64 nir_src_for_ssa(&load_bary_at_offset->dest.ssa));
65 }
66
67 static void
68 lower_load_sample_pos(nir_builder *b, nir_intrinsic_instr *intr)
69 {
70 nir_ssa_def *pos = load_sample_pos(b, nir_load_sample_id(b));
71
72 /* Note that gl_SamplePosition is offset by +vec2(0.5, 0.5) vs the
73 * offset passed to interpolateAtOffset(). See
74 * dEQP-GLES31.functional.shaders.multisample_interpolation.interpolate_at_offset.at_sample_position.default_framebuffer
75 * for example.
76 */
77 nir_ssa_def *half = nir_imm_float(b, 0.5);
78 pos = nir_fadd(b, pos, nir_vec2(b, half, half));
79
80 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(pos));
81 }
82
83 bool
84 ir3_nir_lower_load_barycentric_at_sample(nir_shader *shader)
85 {
86 bool progress = false;
87
88 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
89
90 nir_foreach_function (function, shader) {
91 if (!function->impl)
92 continue;
93
94 nir_builder b;
95 nir_builder_init(&b, function->impl);
96
97 nir_foreach_block (block, function->impl) {
98 nir_foreach_instr_safe(instr, block) {
99 if (instr->type != nir_instr_type_intrinsic)
100 continue;
101
102 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
103
104 if (intr->intrinsic != nir_intrinsic_load_barycentric_at_sample &&
105 intr->intrinsic != nir_intrinsic_load_sample_pos)
106 continue;
107
108 debug_assert(intr->dest.is_ssa);
109
110 b.cursor = nir_before_instr(instr);
111
112 if (intr->intrinsic == nir_intrinsic_load_sample_pos) {
113 lower_load_sample_pos(&b, intr);
114 } else {
115 debug_assert(intr->src[0].is_ssa);
116 lower_load_barycentric_at_sample(&b, intr);
117 }
118
119 progress = true;
120 }
121 }
122
123 if (progress) {
124 nir_metadata_preserve(function->impl,
125 nir_metadata_block_index | nir_metadata_dominance);
126 }
127 }
128
129 return progress;
130 }