xref: /aosp_15_r20/external/mesa3d/src/panfrost/lib/pan_clear.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2019-2021 Collabora, Ltd.
3  * Copyright (C) 2019 Alyssa Rosenzweig
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  */
25 
26 #include "genxml/gen_macros.h"
27 
28 #include <string.h>
29 #include "util/format_srgb.h"
30 #include "util/rounding.h"
31 #include "util/u_pack_color.h"
32 #include "pan_format.h"
33 #include "pan_util.h"
34 
35 /* Clear colours are packed as the internal format of the tilebuffer, looked up
36  * in the blendable formats table given the render target format.
37  *
38  * Raw formats may emulate arbitrary formats with blend shaders. For these, we
39  * defer to util_pack_colour to pack in the API format.
40  *
41  * Blendable formats, on the other hand, include extra "fractional" bits in the
42  * tilebuffer for dithering. These have a packed fixed-point representation:
43  * for a channel with m integer bits and n fractional bits, multiply by ((2^m)
44  * - 1) * 2^n and round to the nearest even.
45  */
46 
47 /* Replicate a 32-bit value to fill 128-bit */
48 
49 static void
pan_pack_color_32(uint32_t * packed,uint32_t v)50 pan_pack_color_32(uint32_t *packed, uint32_t v)
51 {
52    for (unsigned i = 0; i < 4; ++i)
53       packed[i] = v;
54 }
55 
56 /* For m integer bits and n fractional bits, calculate the conversion factor,
57  * multiply the source value, and convert to integer rounding to even. When
58  * dithering, the fractional bits are used. When not dithered, only the integer
59  * bits are used and the fractional bits must remain zero. */
60 
61 static inline uint32_t
float_to_fixed(float f,unsigned bits_int,unsigned bits_frac,bool dither)62 float_to_fixed(float f, unsigned bits_int, unsigned bits_frac, bool dither)
63 {
64    uint32_t m = (1 << bits_int) - 1;
65 
66    if (dither) {
67       float factor = m << bits_frac;
68       return _mesa_roundevenf(f * factor);
69    } else {
70       uint32_t v = _mesa_roundevenf(f * (float)m);
71       return v << bits_frac;
72    }
73 }
74 
75 struct mali_tib_layout {
76    unsigned int_r, frac_r;
77    unsigned int_g, frac_g;
78    unsigned int_b, frac_b;
79    unsigned int_a, frac_a;
80 };
81 
82 /* clang-format off */
83 static const struct mali_tib_layout tib_layouts[] = {
84    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8]    = {  8, 0,  8, 0,  8, 0, 8, 0 },
85    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2] = { 10, 0, 10, 0, 10, 0, 2, 0 },
86    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2]    = {  8, 2,  8, 2,  8, 2, 2, 0 },
87    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4]    = {  4, 4,  4, 4,  4, 4, 4, 4 },
88    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0]    = {  5, 5,  6, 4,  5, 5, 0, 2 },
89    [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1]    = {  5, 5,  5, 5,  5, 5, 1, 1 },
90 };
91 /* clang-format on */
92 
93 /* Raw values are stored as-is but replicated for multisampling */
94 
95 static void
pan_pack_raw(uint32_t * packed,const union pipe_color_union * color,enum pipe_format format)96 pan_pack_raw(uint32_t *packed, const union pipe_color_union *color,
97              enum pipe_format format)
98 {
99    union util_color out = {0};
100    unsigned size = util_format_get_blocksize(format);
101    assert(size <= 16);
102 
103    util_pack_color(color->f, format, &out);
104 
105    if (size == 1) {
106       unsigned s = out.ui[0] | (out.ui[0] << 8);
107       pan_pack_color_32(packed, s | (s << 16));
108    } else if (size == 2)
109       pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
110    else if (size <= 4)
111       pan_pack_color_32(packed, out.ui[0]);
112    else if (size <= 8) {
113       memcpy(packed + 0, out.ui, 8);
114       memcpy(packed + 2, out.ui, 8);
115    } else {
116       memcpy(packed, out.ui, 16);
117    }
118 }
119 
120 void
pan_pack_color(const struct pan_blendable_format * blendable_formats,uint32_t * packed,const union pipe_color_union * color,enum pipe_format format,bool dithered)121 pan_pack_color(const struct pan_blendable_format *blendable_formats,
122                uint32_t *packed, const union pipe_color_union *color,
123                enum pipe_format format, bool dithered)
124 {
125    enum mali_color_buffer_internal_format internal =
126       blendable_formats[format].internal;
127 
128    if (internal == MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW_VALUE) {
129       pan_pack_raw(packed, color, format);
130       return;
131    }
132 
133    /* Saturate to [0, 1] by definition of UNORM. Prevents overflow. */
134    float r = SATURATE(color->f[0]);
135    float g = SATURATE(color->f[1]);
136    float b = SATURATE(color->f[2]);
137    float a = SATURATE(color->f[3]);
138 
139    /* Fill in alpha = 1.0 by default */
140    if (!util_format_has_alpha(format))
141       a = 1.0;
142 
143    /* Convert colourspace while we still have floats */
144    if (util_format_is_srgb(format)) {
145       r = util_format_linear_to_srgb_float(r);
146       g = util_format_linear_to_srgb_float(g);
147       b = util_format_linear_to_srgb_float(b);
148    }
149 
150    /* Look up the layout of the tilebuffer */
151    assert(internal < ARRAY_SIZE(tib_layouts));
152    struct mali_tib_layout l = tib_layouts[internal];
153 
154    unsigned count_r = l.int_r + l.frac_r;
155    unsigned count_g = l.int_g + l.frac_g + count_r;
156    unsigned count_b = l.int_b + l.frac_b + count_g;
157    ASSERTED unsigned count_a = l.int_a + l.frac_a + count_b;
158 
159    /* Must fill the word */
160    assert(count_a == 32);
161 
162    /* Convert the transformed float colour to the given layout */
163    uint32_t ur = float_to_fixed(r, l.int_r, l.frac_r, dithered) << 0;
164    uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g, dithered) << count_r;
165    uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b, dithered) << count_g;
166    uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a, dithered) << count_b;
167 
168    pan_pack_color_32(packed, ur | ug | ub | ua);
169 }
170