panfrost/midgard: Use Gallium framebuffer formats
[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 #include "util/u_format.h"
44
45 static nir_ssa_def *
46 nir_float_to_unorm8(nir_builder *b, nir_ssa_def *c_float)
47 {
48 /* First, we degrade quality to fp16; we don't need the extra bits */
49 nir_ssa_def *degraded = nir_f2f16(b, c_float);
50
51 /* Scale from [0, 1] to [0, 255.0] */
52 nir_ssa_def *scaled = nir_fmul_imm(b, nir_fsat(b, degraded), 255.0);
53
54 /* Next, we type convert */
55 nir_ssa_def *converted = nir_u2u8(b, nir_f2u16(b,
56 nir_fround_even(b, scaled)));
57
58 return converted;
59 }
60
61 static nir_ssa_def *
62 nir_unorm8_to_float(nir_builder *b, nir_ssa_def *c_native)
63 {
64 /* First, we convert up from u8 to f16 */
65 nir_ssa_def *converted = nir_u2f16(b, nir_u2u16(b, c_native));
66
67 /* Next, we scale down from [0, 255.0] to [0, 1] */
68 nir_ssa_def *scaled = nir_fsat(b, nir_fmul_imm(b, converted, 1.0/255.0));
69
70 return scaled;
71 }
72
73
74
75 static nir_ssa_def *
76 nir_float_to_native(nir_builder *b,
77 nir_ssa_def *c_float,
78 const struct util_format_description *desc)
79 {
80 if (util_format_is_unorm8(desc))
81 return nir_float_to_unorm8(b, c_float);
82 else {
83 printf("%s\n", desc->name);
84 unreachable("Unknown format name");
85 }
86 }
87
88 static nir_ssa_def *
89 nir_native_to_float(nir_builder *b,
90 nir_ssa_def *c_native,
91 const struct util_format_description *desc)
92 {
93 if (util_format_is_unorm8(desc))
94 return nir_unorm8_to_float(b, c_native);
95 else {
96 printf("%s\n", desc->name);
97 unreachable("Unknown format name");
98 }
99 }
100
101 void
102 nir_lower_framebuffer(nir_shader *shader)
103 {
104 /* Blend shaders are represented as special fragment shaders */
105 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
106
107 enum pipe_format format = PIPE_FORMAT_R8G8B8A8_UNORM;
108 const struct util_format_description *format_desc =
109 util_format_description(format);
110
111 nir_foreach_function(func, shader) {
112 nir_foreach_block(block, func->impl) {
113 nir_foreach_instr_safe(instr, block) {
114 if (instr->type != nir_instr_type_intrinsic)
115 continue;
116
117 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
118
119 bool is_load = intr->intrinsic == nir_intrinsic_load_deref;
120 bool is_store = intr->intrinsic == nir_intrinsic_store_deref;
121
122 if (!(is_load || is_store))
123 continue;
124
125 /* Don't worry about MRT */
126 nir_variable *var = nir_intrinsic_get_var(intr, 0);
127
128 if (var->data.location != FRAG_RESULT_COLOR)
129 continue;
130
131 nir_builder b;
132 nir_builder_init(&b, func->impl);
133
134 if (is_store) {
135 /* For stores, add conversion before */
136 b.cursor = nir_before_instr(instr);
137
138 /* Grab the input color */
139 nir_ssa_def *c_nir = nir_ssa_for_src(&b, intr->src[1], 4);
140
141 /* Format convert */
142 nir_ssa_def *converted = nir_float_to_native(&b, c_nir, format_desc);
143
144 /* Rewrite to use a native store by creating a new intrinsic */
145 nir_intrinsic_instr *new =
146 nir_intrinsic_instr_create(shader, nir_intrinsic_store_raw_output_pan);
147 new->src[0] = nir_src_for_ssa(converted);
148
149 /* TODO: What about non-RGBA? Is that different? */
150 new->num_components = 4;
151
152 nir_builder_instr_insert(&b, &new->instr);
153
154 /* (And finally removing the old) */
155 nir_instr_remove(instr);
156 } else {
157 /* For loads, add conversion after */
158 b.cursor = nir_after_instr(instr);
159
160 /* Rewrite to use a native load by creating a new intrinsic */
161
162 nir_intrinsic_instr *new =
163 nir_intrinsic_instr_create(shader, nir_intrinsic_load_raw_output_pan);
164
165 new->num_components = 4;
166
167 unsigned bitsize = 8;
168 nir_ssa_dest_init(&new->instr, &new->dest, 4, bitsize, NULL);
169 nir_builder_instr_insert(&b, &new->instr);
170
171 /* Convert the raw value */
172 nir_ssa_def *raw = &new->dest.ssa;
173 nir_ssa_def *converted = nir_native_to_float(&b, raw, format_desc);
174
175 /* Rewrite to use the converted value */
176 nir_src rewritten = nir_src_for_ssa(converted);
177 nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, rewritten, instr);
178
179 /* Finally, remove the old load */
180 nir_instr_remove(instr);
181 }
182 }
183 }
184
185 nir_metadata_preserve(func->impl, nir_metadata_block_index |
186 nir_metadata_dominance);
187 }
188 }