• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pan_util.h"
30 #include "pan_format.h"
31 #include "gallium/auxiliary/util/u_pack_color.h"
32 #include "util/rounding.h"
33 #include "util/format_srgb.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 static const struct mali_tib_layout tib_layouts[] = {
83         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8] = { 8, 0, 8, 0, 8, 0, 8, 0 },
84         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2] = { 10, 0, 10, 0, 10, 0, 2, 0 },
85         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2] = { 8, 2, 8, 2, 8, 2, 2, 0 },
86         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4] = { 4, 4, 4, 4, 4, 4, 4, 4 },
87         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0] = { 5, 5, 6, 4, 5, 5, 0, 2 },
88         [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1] = { 5, 5, 5, 5, 5, 5, 1, 1 },
89 };
90 
91 /* Raw values are stored as-is but replicated for multisampling */
92 
93 static void
pan_pack_raw(uint32_t * packed,const union pipe_color_union * color,enum pipe_format format)94 pan_pack_raw(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
95 {
96         union util_color out = { 0 };
97         unsigned size = util_format_get_blocksize(format);
98         assert(size <= 16);
99 
100         util_pack_color(color->f, format, &out);
101 
102         if (size == 1) {
103                 unsigned s = out.ui[0] | (out.ui[0] << 8);
104                 pan_pack_color_32(packed, s | (s << 16));
105         } else if (size == 2)
106                 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
107         else if (size <= 4)
108                 pan_pack_color_32(packed, out.ui[0]);
109         else if (size <= 8) {
110                 memcpy(packed + 0, out.ui, 8);
111                 memcpy(packed + 2, out.ui, 8);
112         } else {
113                 memcpy(packed, out.ui, 16);
114         }
115 }
116 
117 void
pan_pack_color(uint32_t * packed,const union pipe_color_union * color,enum pipe_format format,bool dithered)118 pan_pack_color(uint32_t *packed, const union pipe_color_union *color,
119                enum pipe_format format, bool dithered)
120 {
121         /* Set of blendable formats is common across versions. TODO: v9 */
122         enum mali_color_buffer_internal_format internal =
123                 panfrost_blendable_formats_v7[format].internal;
124 
125         if (internal == MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW_VALUE) {
126                 pan_pack_raw(packed, color, format);
127                 return;
128         }
129 
130         /* Saturate to [0, 1] by definition of UNORM. Prevents overflow. */
131         float r = SATURATE(color->f[0]);
132         float g = SATURATE(color->f[1]);
133         float b = SATURATE(color->f[2]);
134         float a = SATURATE(color->f[3]);
135 
136         /* Fill in alpha = 1.0 by default */
137         if (!util_format_has_alpha(format))
138                 a = 1.0;
139 
140         /* Convert colourspace while we still have floats */
141         if (util_format_is_srgb(format)) {
142                 r = util_format_linear_to_srgb_float(r);
143                 g = util_format_linear_to_srgb_float(g);
144                 b = util_format_linear_to_srgb_float(b);
145         }
146 
147         /* Look up the layout of the tilebuffer */
148         assert(internal < ARRAY_SIZE(tib_layouts));
149         struct mali_tib_layout l = tib_layouts[internal];
150 
151         unsigned count_r = l.int_r + l.frac_r;
152         unsigned count_g = l.int_g + l.frac_g + count_r;
153         unsigned count_b = l.int_b + l.frac_b + count_g;
154         ASSERTED unsigned count_a = l.int_a + l.frac_a + count_b;
155 
156         /* Must fill the word */
157         assert(count_a == 32);
158 
159         /* Convert the transformed float colour to the given layout */
160         uint32_t ur = float_to_fixed(r, l.int_r, l.frac_r, dithered) << 0;
161         uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g, dithered) << count_r;
162         uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b, dithered) << count_g;
163         uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a, dithered) << count_b;
164 
165         pan_pack_color_32(packed, ur | ug | ub | ua);
166 }
167