Debian Codegraph @

src/panfrost/lib/pan_clear.c

raw
1 /*
2 * Copyright (C) 2019-2021 Collabora, Ltd.
3 * Copyright (C) 2019 Alyssa Rosenzweig
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "genxml/gen_macros.h"
8
9 #include <string.h>
10 #include "util/format_srgb.h"
11 #include "util/rounding.h"
12 #include "util/u_pack_color.h"
13 #include "pan_format.h"
14 #include "pan_util.h"
15
16 /* Clear colours are packed as the internal format of the tilebuffer, looked up
17 * in the blendable formats table given the render target format.
18 *
19 * Raw formats may emulate arbitrary formats with blend shaders. For these, we
20 * defer to util_pack_colour to pack in the API format.
21 *
22 * Blendable formats, on the other hand, include extra "fractional" bits in the
23 * tilebuffer for dithering. These have a packed fixed-point representation:
24 * for a channel with m integer bits and n fractional bits, multiply by ((2^m)
25 * - 1) * 2^n and round to the nearest even.
26 */
27
28 /* Replicate a 32-bit value to fill 128-bit */
29
30 static void
31 pan_pack_color_32(uint32_t *packed, uint32_t v)
32 {
33 for (unsigned i = 0; i < 4; ++i)
34 packed[i] = v;
35 }
36
37 /* For m integer bits and n fractional bits, calculate the conversion factor,
38 * multiply the source value, and convert to integer rounding to even. When
39 * dithering, the fractional bits are used. When not dithered, only the integer
40 * bits are used and the fractional bits must remain zero. */
41
42 static inline uint32_t
43 float_to_fixed(float f, unsigned bits_int, unsigned bits_frac, bool dither)
44 {
45 uint32_t m = (1 << bits_int) - 1;
46
47 if (dither) {
48 float factor = m << bits_frac;
49 return _mesa_roundevenf(f * factor);
50 } else {
51 uint32_t v = _mesa_roundevenf(f * (float)m);
52 return v << bits_frac;
53 }
54 }
55
56 struct mali_tib_layout {
57 unsigned int_r, frac_r;
58 unsigned int_g, frac_g;
59 unsigned int_b, frac_b;
60 unsigned int_a, frac_a;
61 };
62
63 /* clang-format off */
64 static const struct mali_tib_layout tib_layouts[] = {
65 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8] = { 8, 0, 8, 0, 8, 0, 8, 0 },
66 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2] = { 10, 0, 10, 0, 10, 0, 2, 0 },
67 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2] = { 8, 2, 8, 2, 8, 2, 2, 0 },
68 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4] = { 4, 4, 4, 4, 4, 4, 4, 4 },
69 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0] = { 5, 5, 6, 4, 5, 5, 0, 2 },
70 [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1] = { 5, 5, 5, 5, 5, 5, 1, 1 },
71 };
72 /* clang-format on */
73
74 /* Raw values are stored as-is but replicated for multisampling */
75
76 static void
77 pan_pack_raw(uint32_t *packed, const union pipe_color_union *color,
78 enum pipe_format format)
79 {
80 union util_color out = {0};
81 unsigned size = util_format_get_blocksize(format);
82 assert(size <= 16);
83
84 util_pack_color(color->f, format, &out);
85
86 if (size == 1) {
87 unsigned b = out.ui[0] & 0xff;
88 unsigned w = b | (b << 8);
89 pan_pack_color_32(packed, w | (w << 16));
90 } else if (size == 2) {
91 unsigned w = out.ui[0] & 0xffff;
92 pan_pack_color_32(packed, w | (w << 16));
93 } else if (size <= 4) {
94 pan_pack_color_32(packed, out.ui[0]);
95 } else if (size <= 8) {
96 memcpy(packed + 0, out.ui, 8);
97 memcpy(packed + 2, out.ui, 8);
98 } else {
99 memcpy(packed, out.ui, 16);
100 }
101 }
102
103 void
104 pan_pack_color(const struct pan_blendable_format *blendable_formats,
105 uint32_t *packed, const union pipe_color_union *color,
106 enum pipe_format format, bool dithered)
107 {
108 enum mali_color_buffer_internal_format internal =
109 blendable_formats[format].internal;
110
111 if (util_format_is_float(format) ||
112 internal == MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW_VALUE) {
113 pan_pack_raw(packed, color, format);
114 return;
115 }
116
117 /* Saturate to [0, 1] by definition of UNORM. Prevents overflow. */
118 float r = SATURATE(color->f[0]);
119 float g = SATURATE(color->f[1]);
120 float b = SATURATE(color->f[2]);
121 float a = SATURATE(color->f[3]);
122
123 /* Fill in alpha = 1.0 by default */
124 if (!util_format_has_alpha(format))
125 a = 1.0;
126
127 /* Convert colourspace while we still have floats */
128 if (util_format_is_srgb(format)) {
129 r = util_format_linear_to_srgb_float(r);
130 g = util_format_linear_to_srgb_float(g);
131 b = util_format_linear_to_srgb_float(b);
132 }
133
134 /* Look up the layout of the tilebuffer */
135 assert(internal < ARRAY_SIZE(tib_layouts));
136 struct mali_tib_layout l = tib_layouts[internal];
137
138 unsigned count_r = l.int_r + l.frac_r;
139 unsigned count_g = l.int_g + l.frac_g + count_r;
140 unsigned count_b = l.int_b + l.frac_b + count_g;
141 ASSERTED unsigned count_a = l.int_a + l.frac_a + count_b;
142
143 /* Must fill the word */
144 assert(count_a == 32);
145
146 /* Convert the transformed float colour to the given layout */
147 uint32_t ur = float_to_fixed(r, l.int_r, l.frac_r, dithered) << 0;
148 uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g, dithered) << count_r;
149 uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b, dithered) << count_g;
150 uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a, dithered) << count_b;
151
152 pan_pack_color_32(packed, ur | ug | ub | ua);
153 }