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"
45 nir_float_to_native(nir_builder
*b
, nir_ssa_def
*c_float
)
47 /* First, we degrade quality to fp16; we don't need the extra bits */
48 nir_ssa_def
*degraded
= nir_f2f16(b
, c_float
);
50 /* Scale from [0, 1] to [0, 255.0] */
51 nir_ssa_def
*scaled
= nir_fmul_imm(b
, nir_fsat(b
, degraded
), 255.0);
53 /* Next, we type convert */
54 nir_ssa_def
*converted
= nir_u2u8(b
, nir_f2u32(b
,
55 nir_fround_even(b
, scaled
)));
61 nir_native_to_float(nir_builder
*b
, nir_ssa_def
*c_native
)
63 /* First, we convert up from u8 to f32 */
64 nir_ssa_def
*converted
= nir_u2f32(b
, nir_u2u32(b
, c_native
));
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));
73 nir_lower_framebuffer(nir_shader
*shader
)
75 /* Blend shaders are represented as special fragment shaders */
76 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
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
)
84 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
86 bool is_load
= intr
->intrinsic
== nir_intrinsic_load_deref
;
87 bool is_store
= intr
->intrinsic
== nir_intrinsic_store_deref
;
89 if (!(is_load
|| is_store
))
92 /* Don't worry about MRT */
93 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
95 if (var
->data
.location
!= FRAG_RESULT_COLOR
)
99 nir_builder_init(&b
, func
->impl
);
102 /* For stores, add conversion before */
103 b
.cursor
= nir_before_instr(instr
);
105 /* Grab the input color */
106 nir_ssa_def
*c_nir
= nir_ssa_for_src(&b
, intr
->src
[1], 4);
109 nir_ssa_def
*converted
= nir_float_to_native(&b
, c_nir
);
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
);
116 /* TODO: What about non-RGBA? Is that different? */
117 new->num_components
= 4;
119 nir_builder_instr_insert(&b
, &new->instr
);
121 /* (And finally removing the old) */
122 nir_instr_remove(instr
);
124 /* For loads, add conversion after */
125 b
.cursor
= nir_after_instr(instr
);
127 /* Rewrite to use a native load by creating a new intrinsic */
129 nir_intrinsic_instr
*new =
130 nir_intrinsic_instr_create(shader
, nir_intrinsic_load_raw_output_pan
);
132 new->num_components
= 4;
134 unsigned bitsize
= 8;
135 nir_ssa_dest_init(&new->instr
, &new->dest
, 4, bitsize
, NULL
);
136 nir_builder_instr_insert(&b
, &new->instr
);
138 /* Convert the raw value */
139 nir_ssa_def
*raw
= &new->dest
.ssa
;
140 nir_ssa_def
*converted
= nir_native_to_float(&b
, raw
);
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
);
146 /* Finally, remove the old load */
147 nir_instr_remove(instr
);
152 nir_metadata_preserve(func
->impl
, nir_metadata_block_index
|
153 nir_metadata_dominance
);