Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/lib/pan_clear.c
4560 views
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 <string.h>
27
#include "pan_util.h"
28
#include "pan_format.h"
29
#include "gallium/auxiliary/util/u_pack_color.h"
30
#include "util/rounding.h"
31
#include "util/format_srgb.h"
32
33
/* Clear colours are packed as the internal format of the tilebuffer, looked up
34
* in the blendable formats table given the render target format.
35
*
36
* Raw formats may emulate arbitrary formats with blend shaders. For these, we
37
* defer to util_pack_colour to pack in the API format.
38
*
39
* Blendable formats, on the other hand, include extra "fractional" bits in the
40
* tilebuffer for dithering. These have a packed fixed-point representation:
41
* for a channel with m integer bits and n fractional bits, multiply by ((2^m)
42
* - 1) * 2^n and round to the nearest even.
43
*/
44
45
/* Replicate a 32-bit value to fill 128-bit */
46
47
static void
48
pan_pack_color_32(uint32_t *packed, uint32_t v)
49
{
50
for (unsigned i = 0; i < 4; ++i)
51
packed[i] = v;
52
}
53
54
/* For m integer bits and n fractional bits, calculate the conversion factor,
55
* multiply the source value, and convert to integer rounding to even */
56
57
static inline uint32_t
58
float_to_fixed(float f, unsigned bits_int, unsigned bits_frac)
59
{
60
float factor = ((1 << bits_int) - 1) << bits_frac;
61
return _mesa_roundevenf(f * factor);
62
}
63
64
/* These values are shared across hardware versions. Don't include GenXML. */
65
enum mali_color_buffer_internal_format {
66
MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW = 0,
67
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8 = 1,
68
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2 = 2,
69
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2 = 3,
70
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4 = 4,
71
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0 = 5,
72
MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1 = 6,
73
MALI_COLOR_BUFFER_NUM_FORMATS,
74
};
75
76
struct mali_tib_layout {
77
unsigned int_r, frac_r;
78
unsigned int_g, frac_g;
79
unsigned int_b, frac_b;
80
unsigned int_a, frac_a;
81
};
82
83
static const struct mali_tib_layout tib_layouts[MALI_COLOR_BUFFER_NUM_FORMATS] = {
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
92
/* Raw values are stored as-is but replicated for multisampling */
93
94
static void
95
pan_pack_raw(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
96
{
97
union util_color out = { 0 };
98
unsigned size = util_format_get_blocksize(format);
99
assert(size <= 16);
100
101
util_pack_color(color->f, format, &out);
102
103
if (size == 1) {
104
unsigned s = out.ui[0] | (out.ui[0] << 8);
105
pan_pack_color_32(packed, s | (s << 16));
106
} else if (size == 2)
107
pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
108
else if (size <= 4)
109
pan_pack_color_32(packed, out.ui[0]);
110
else if (size <= 8) {
111
memcpy(packed + 0, out.ui, 8);
112
memcpy(packed + 2, out.ui, 8);
113
} else {
114
memcpy(packed, out.ui, 16);
115
}
116
}
117
118
void
119
pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
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) {
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 < MALI_COLOR_BUFFER_NUM_FORMATS);
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) << 0;
161
uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g) << count_r;
162
uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b) << count_g;
163
uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a) << count_b;
164
165
pan_pack_color_32(packed, ur | ug | ub | ua);
166
}
167
168