panfrost/midgard: Move blend load/store into NIR
[mesa.git] / src / gallium / drivers / panfrost / midgard / nir_lower_framebuffer.c
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 /**
28 * @file
29 *
30 * Implements framebuffer format conversions in software, specifically for
31 * blend shaders on Midgard/Bifrost. load_output/store_output (derefs more
32 * correctly -- pre I/O lowering) normally for the fragment stage within the
33 * blend shader will operate with purely vec4 float ("nir") encodings. This
34 * lowering stage, to be run before I/O is lowered, converts the native
35 * framebuffer format to a NIR encoding after loads and vice versa before
36 * stores. This pass is designed for a single render target; Midgard duplicates
37 * blend shaders for MRT to simplify everything.
38 */
39
40 #include "compiler/nir/nir.h"
41 #include "compiler/nir/nir_builder.h"
42 #include "nir_lower_blend.h"
43
44 static nir_ssa_def *
45 nir_float_to_native(nir_builder *b, nir_ssa_def *c_float)
46 {
47 /* First, we scale from [0, 1] to [0, 255.0] */
48 nir_ssa_def *scaled = nir_fmul_imm(b, nir_fsat(b, c_float), 255.0);
49
50 /* Next, we type convert */
51 nir_ssa_def *converted = nir_u2u8(b, nir_f2u32(b,
52 nir_fround_even(b, scaled)));
53
54 return converted;
55 }
56
57 static nir_ssa_def *
58 nir_native_to_float(nir_builder *b, nir_ssa_def *c_native)
59 {
60 return c_native;
61 }
62
63 void
64 nir_lower_framebuffer(nir_shader *shader)
65 {
66 /* Blend shaders are represented as special fragment shaders */
67 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
68
69 nir_foreach_function(func, shader) {
70 nir_foreach_block(block, func->impl) {
71 nir_foreach_instr_safe(instr, block) {
72 if (instr->type != nir_instr_type_intrinsic)
73 continue;
74
75 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
76
77 bool is_load = intr->intrinsic == nir_intrinsic_load_deref;
78 bool is_store = intr->intrinsic == nir_intrinsic_store_deref;
79
80 if (!(is_load || is_store))
81 continue;
82
83 /* Don't worry about MRT */
84 nir_variable *var = nir_intrinsic_get_var(intr, 0);
85
86 if (var->data.location != FRAG_RESULT_COLOR)
87 continue;
88
89 nir_builder b;
90 nir_builder_init(&b, func->impl);
91
92 if (is_store) {
93 /* For stores, add conversion before */
94 b.cursor = nir_before_instr(instr);
95
96 /* Grab the input color */
97 nir_ssa_def *c_nir = nir_ssa_for_src(&b, intr->src[1], 4);
98
99 /* Format convert */
100 nir_ssa_def *converted = nir_float_to_native(&b, c_nir);
101
102 /* Rewrite to use a native store by creating a new intrinsic */
103 nir_intrinsic_instr *new =
104 nir_intrinsic_instr_create(shader, nir_intrinsic_store_raw_output_pan);
105 new->src[0] = nir_src_for_ssa(converted);
106
107 /* TODO: What about non-RGBA? Is that different? */
108 new->num_components = 4;
109
110 nir_builder_instr_insert(&b, &new->instr);
111
112 /* (And finally removing the old) */
113 nir_instr_remove(instr);
114 } else {
115 /* For loads, add conversion after */
116 b.cursor = nir_after_instr(instr);
117
118 /* Rewrite to use a native load by creating a new intrinsic */
119
120 nir_intrinsic_instr *new =
121 nir_intrinsic_instr_create(shader, nir_intrinsic_load_raw_output_pan);
122
123 new->num_components = 4;
124
125 unsigned bitsize = 32;
126 nir_ssa_dest_init(&new->instr, &new->dest, 4, bitsize, NULL);
127 nir_builder_instr_insert(&b, &new->instr);
128
129 /* Convert the raw value */
130 nir_ssa_def *raw = &new->dest.ssa;
131 nir_ssa_def *converted = nir_native_to_float(&b, raw);
132
133 /* Rewrite to use the converted value */
134 nir_src rewritten = nir_src_for_ssa(converted);
135 nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, rewritten, instr);
136
137 /* Finally, remove the old load */
138 nir_instr_remove(instr);
139 }
140 }
141 }
142
143 nir_metadata_preserve(func->impl, nir_metadata_block_index |
144 nir_metadata_dominance);
145 }
146 }