panfrost/midgard: Implement f2f16/f2f32
[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 degrade quality to fp16; we don't need the extra bits */
48 nir_ssa_def *degraded = nir_f2f16(b, c_float);
49
50 /* Scale from [0, 1] to [0, 255.0] */
51 nir_ssa_def *scaled = nir_fmul_imm(b, nir_fsat(b, degraded), 255.0);
52
53 /* Next, we type convert */
54 nir_ssa_def *converted = nir_u2u8(b, nir_f2u32(b,
55 nir_fround_even(b, scaled)));
56
57 return converted;
58 }
59
60 static nir_ssa_def *
61 nir_native_to_float(nir_builder *b, nir_ssa_def *c_native)
62 {
63 /* First, we convert up from u8 to f32 */
64 nir_ssa_def *converted = nir_u2f32(b, nir_u2u32(b, c_native));
65
66 /* Next, we scale down from [0, 255.0] to [0, 1] */
67 nir_ssa_def *scaled = nir_fsat(b, nir_fmul_imm(b, converted, 1.0/255.0));
68
69 return scaled;
70 }
71
72 void
73 nir_lower_framebuffer(nir_shader *shader)
74 {
75 /* Blend shaders are represented as special fragment shaders */
76 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
77
78 nir_foreach_function(func, shader) {
79 nir_foreach_block(block, func->impl) {
80 nir_foreach_instr_safe(instr, block) {
81 if (instr->type != nir_instr_type_intrinsic)
82 continue;
83
84 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
85
86 bool is_load = intr->intrinsic == nir_intrinsic_load_deref;
87 bool is_store = intr->intrinsic == nir_intrinsic_store_deref;
88
89 if (!(is_load || is_store))
90 continue;
91
92 /* Don't worry about MRT */
93 nir_variable *var = nir_intrinsic_get_var(intr, 0);
94
95 if (var->data.location != FRAG_RESULT_COLOR)
96 continue;
97
98 nir_builder b;
99 nir_builder_init(&b, func->impl);
100
101 if (is_store) {
102 /* For stores, add conversion before */
103 b.cursor = nir_before_instr(instr);
104
105 /* Grab the input color */
106 nir_ssa_def *c_nir = nir_ssa_for_src(&b, intr->src[1], 4);
107
108 /* Format convert */
109 nir_ssa_def *converted = nir_float_to_native(&b, c_nir);
110
111 /* Rewrite to use a native store by creating a new intrinsic */
112 nir_intrinsic_instr *new =
113 nir_intrinsic_instr_create(shader, nir_intrinsic_store_raw_output_pan);
114 new->src[0] = nir_src_for_ssa(converted);
115
116 /* TODO: What about non-RGBA? Is that different? */
117 new->num_components = 4;
118
119 nir_builder_instr_insert(&b, &new->instr);
120
121 /* (And finally removing the old) */
122 nir_instr_remove(instr);
123 } else {
124 /* For loads, add conversion after */
125 b.cursor = nir_after_instr(instr);
126
127 /* Rewrite to use a native load by creating a new intrinsic */
128
129 nir_intrinsic_instr *new =
130 nir_intrinsic_instr_create(shader, nir_intrinsic_load_raw_output_pan);
131
132 new->num_components = 4;
133
134 unsigned bitsize = 8;
135 nir_ssa_dest_init(&new->instr, &new->dest, 4, bitsize, NULL);
136 nir_builder_instr_insert(&b, &new->instr);
137
138 /* Convert the raw value */
139 nir_ssa_def *raw = &new->dest.ssa;
140 nir_ssa_def *converted = nir_native_to_float(&b, raw);
141
142 /* Rewrite to use the converted value */
143 nir_src rewritten = nir_src_for_ssa(converted);
144 nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, rewritten, instr);
145
146 /* Finally, remove the old load */
147 nir_instr_remove(instr);
148 }
149 }
150 }
151
152 nir_metadata_preserve(func->impl, nir_metadata_block_index |
153 nir_metadata_dominance);
154 }
155 }