28c44f7b0cb1dad5244d3af605c8f822de5b2d78
[mesa.git] / src / panfrost / util / pan_lower_framebuffer.c
1 /*
2 * Copyright (C) 2020 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 * Implements framebuffer format conversions in software for Midgard/Bifrost
29 * blend shaders. This pass is designed for a single render target; Midgard
30 * duplicates blend shaders for MRT to simplify everything. A particular
31 * framebuffer format may be categorized as 1) typed load available, 2) typed
32 * unpack available, or 3) software unpack only, and likewise for stores. The
33 * first two types are handled in the compiler backend directly, so this module
34 * is responsible for identifying type 3 formats (hardware dependent) and
35 * inserting appropriate ALU code to perform the conversion from the packed
36 * type to a designated unpacked type, and vice versa.
37 *
38 * The unpacked type depends on the format:
39 *
40 * - For 32-bit float formats, 32-bit floats.
41 * - For other floats, 16-bit floats.
42 * - For 32-bit ints, 32-bit ints.
43 * - For 8-bit ints, 8-bit ints.
44 * - For other ints, 16-bit ints.
45 *
46 * The rationale is to optimize blending and logic op instructions by using the
47 * smallest precision necessary to store the pixel losslessly.
48 */
49
50 #include "compiler/nir/nir.h"
51 #include "compiler/nir/nir_builder.h"
52 #include "compiler/nir/nir_format_convert.h"
53 #include "util/format/u_format.h"
54 #include "pan_lower_framebuffer.h"
55
56 /* Determines the unpacked type best suiting a given format, so the rest of the
57 * pipeline may be adjusted accordingly */
58
59 nir_alu_type
60 pan_unpacked_type_for_format(const struct util_format_description *desc)
61 {
62 int c = util_format_get_first_non_void_channel(desc->format);
63
64 if (c == -1)
65 unreachable("Void format not renderable");
66
67 bool large = (desc->channel[c].size > 16);
68 bool bit8 = (desc->channel[c].size == 8);
69 assert(desc->channel[c].size <= 32);
70
71 if (desc->channel[c].normalized)
72 return large ? nir_type_float32 : nir_type_float16;
73
74 switch (desc->channel[c].type) {
75 case UTIL_FORMAT_TYPE_UNSIGNED:
76 return bit8 ? nir_type_uint8 :
77 large ? nir_type_uint32 : nir_type_uint16;
78 case UTIL_FORMAT_TYPE_SIGNED:
79 return bit8 ? nir_type_int8 :
80 large ? nir_type_int32 : nir_type_int16;
81 case UTIL_FORMAT_TYPE_FLOAT:
82 return large ? nir_type_float32 : nir_type_float16;
83 default:
84 unreachable("Format not renderable");
85 }
86 }