2 * Copyright (C) 2019 Collabora, Ltd.
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:
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
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
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
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.
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"
46 nir_float_to_unorm8(nir_builder
*b
, nir_ssa_def
*c_float
)
48 /* First, we degrade quality to fp16; we don't need the extra bits */
49 nir_ssa_def
*degraded
= nir_f2f16(b
, c_float
);
51 /* Scale from [0, 1] to [0, 255.0] */
52 nir_ssa_def
*scaled
= nir_fmul_imm(b
, nir_fsat(b
, degraded
), 255.0);
54 /* Next, we type convert */
55 nir_ssa_def
*converted
= nir_u2u8(b
, nir_f2u16(b
,
56 nir_fround_even(b
, scaled
)));
62 nir_unorm8_to_float(nir_builder
*b
, nir_ssa_def
*c_native
)
64 /* First, we convert up from u8 to f16 */
65 nir_ssa_def
*converted
= nir_u2f16(b
, nir_u2u16(b
, c_native
));
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));
76 nir_float_to_native(nir_builder
*b
,
78 const struct util_format_description
*desc
)
80 if (util_format_is_unorm8(desc
))
81 return nir_float_to_unorm8(b
, c_float
);
83 printf("%s\n", desc
->name
);
84 unreachable("Unknown format name");
89 nir_native_to_float(nir_builder
*b
,
90 nir_ssa_def
*c_native
,
91 const struct util_format_description
*desc
)
93 if (util_format_is_unorm8(desc
))
94 return nir_unorm8_to_float(b
, c_native
);
96 printf("%s\n", desc
->name
);
97 unreachable("Unknown format name");
102 nir_lower_framebuffer(nir_shader
*shader
)
104 /* Blend shaders are represented as special fragment shaders */
105 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
107 enum pipe_format format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
108 const struct util_format_description
*format_desc
=
109 util_format_description(format
);
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
)
117 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
119 bool is_load
= intr
->intrinsic
== nir_intrinsic_load_deref
;
120 bool is_store
= intr
->intrinsic
== nir_intrinsic_store_deref
;
122 if (!(is_load
|| is_store
))
125 /* Don't worry about MRT */
126 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
128 if (var
->data
.location
!= FRAG_RESULT_COLOR
)
132 nir_builder_init(&b
, func
->impl
);
135 /* For stores, add conversion before */
136 b
.cursor
= nir_before_instr(instr
);
138 /* Grab the input color */
139 nir_ssa_def
*c_nir
= nir_ssa_for_src(&b
, intr
->src
[1], 4);
142 nir_ssa_def
*converted
= nir_float_to_native(&b
, c_nir
, format_desc
);
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
);
149 /* TODO: What about non-RGBA? Is that different? */
150 new->num_components
= 4;
152 nir_builder_instr_insert(&b
, &new->instr
);
154 /* (And finally removing the old) */
155 nir_instr_remove(instr
);
157 /* For loads, add conversion after */
158 b
.cursor
= nir_after_instr(instr
);
160 /* Rewrite to use a native load by creating a new intrinsic */
162 nir_intrinsic_instr
*new =
163 nir_intrinsic_instr_create(shader
, nir_intrinsic_load_raw_output_pan
);
165 new->num_components
= 4;
167 unsigned bitsize
= 8;
168 nir_ssa_dest_init(&new->instr
, &new->dest
, 4, bitsize
, NULL
);
169 nir_builder_instr_insert(&b
, &new->instr
);
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
);
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
);
179 /* Finally, remove the old load */
180 nir_instr_remove(instr
);
185 nir_metadata_preserve(func
->impl
, nir_metadata_block_index
|
186 nir_metadata_dominance
);