17ec681f3Smrg/* 27ec681f3Smrg * Copyright (C) 2019-2021 Collabora, Ltd. 37ec681f3Smrg * Copyright (C) 2019 Alyssa Rosenzweig 47ec681f3Smrg * 57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 67ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 77ec681f3Smrg * to deal in the Software without restriction, including without limitation 87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 107ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 117ec681f3Smrg * 127ec681f3Smrg * The above copyright notice and this permission notice (including the next 137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 147ec681f3Smrg * Software. 157ec681f3Smrg * 167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 217ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 227ec681f3Smrg * SOFTWARE. 237ec681f3Smrg * 247ec681f3Smrg */ 257ec681f3Smrg 267ec681f3Smrg#include "genxml/gen_macros.h" 277ec681f3Smrg 287ec681f3Smrg#include <string.h> 297ec681f3Smrg#include "pan_util.h" 307ec681f3Smrg#include "pan_format.h" 317ec681f3Smrg#include "gallium/auxiliary/util/u_pack_color.h" 327ec681f3Smrg#include "util/rounding.h" 337ec681f3Smrg#include "util/format_srgb.h" 347ec681f3Smrg 357ec681f3Smrg/* Clear colours are packed as the internal format of the tilebuffer, looked up 367ec681f3Smrg * in the blendable formats table given the render target format. 377ec681f3Smrg * 387ec681f3Smrg * Raw formats may emulate arbitrary formats with blend shaders. For these, we 397ec681f3Smrg * defer to util_pack_colour to pack in the API format. 407ec681f3Smrg * 417ec681f3Smrg * Blendable formats, on the other hand, include extra "fractional" bits in the 427ec681f3Smrg * tilebuffer for dithering. These have a packed fixed-point representation: 437ec681f3Smrg * for a channel with m integer bits and n fractional bits, multiply by ((2^m) 447ec681f3Smrg * - 1) * 2^n and round to the nearest even. 457ec681f3Smrg */ 467ec681f3Smrg 477ec681f3Smrg/* Replicate a 32-bit value to fill 128-bit */ 487ec681f3Smrg 497ec681f3Smrgstatic void 507ec681f3Smrgpan_pack_color_32(uint32_t *packed, uint32_t v) 517ec681f3Smrg{ 527ec681f3Smrg for (unsigned i = 0; i < 4; ++i) 537ec681f3Smrg packed[i] = v; 547ec681f3Smrg} 557ec681f3Smrg 567ec681f3Smrg/* For m integer bits and n fractional bits, calculate the conversion factor, 577ec681f3Smrg * multiply the source value, and convert to integer rounding to even. When 587ec681f3Smrg * dithering, the fractional bits are used. When not dithered, only the integer 597ec681f3Smrg * bits are used and the fractional bits must remain zero. */ 607ec681f3Smrg 617ec681f3Smrgstatic inline uint32_t 627ec681f3Smrgfloat_to_fixed(float f, unsigned bits_int, unsigned bits_frac, bool dither) 637ec681f3Smrg{ 647ec681f3Smrg uint32_t m = (1 << bits_int) - 1; 657ec681f3Smrg 667ec681f3Smrg if (dither) { 677ec681f3Smrg float factor = m << bits_frac; 687ec681f3Smrg return _mesa_roundevenf(f * factor); 697ec681f3Smrg } else { 707ec681f3Smrg uint32_t v = _mesa_roundevenf(f * (float) m); 717ec681f3Smrg return v << bits_frac; 727ec681f3Smrg } 737ec681f3Smrg} 747ec681f3Smrg 757ec681f3Smrgstruct mali_tib_layout { 767ec681f3Smrg unsigned int_r, frac_r; 777ec681f3Smrg unsigned int_g, frac_g; 787ec681f3Smrg unsigned int_b, frac_b; 797ec681f3Smrg unsigned int_a, frac_a; 807ec681f3Smrg}; 817ec681f3Smrg 827ec681f3Smrgstatic const struct mali_tib_layout tib_layouts[] = { 837ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8] = { 8, 0, 8, 0, 8, 0, 8, 0 }, 847ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2] = { 10, 0, 10, 0, 10, 0, 2, 0 }, 857ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2] = { 8, 2, 8, 2, 8, 2, 2, 0 }, 867ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4] = { 4, 4, 4, 4, 4, 4, 4, 4 }, 877ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0] = { 5, 5, 6, 4, 5, 5, 0, 2 }, 887ec681f3Smrg [MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1] = { 5, 5, 5, 5, 5, 5, 1, 1 }, 897ec681f3Smrg}; 907ec681f3Smrg 917ec681f3Smrg/* Raw values are stored as-is but replicated for multisampling */ 927ec681f3Smrg 937ec681f3Smrgstatic void 947ec681f3Smrgpan_pack_raw(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format) 957ec681f3Smrg{ 967ec681f3Smrg union util_color out = { 0 }; 977ec681f3Smrg unsigned size = util_format_get_blocksize(format); 987ec681f3Smrg assert(size <= 16); 997ec681f3Smrg 1007ec681f3Smrg util_pack_color(color->f, format, &out); 1017ec681f3Smrg 1027ec681f3Smrg if (size == 1) { 1037ec681f3Smrg unsigned s = out.ui[0] | (out.ui[0] << 8); 1047ec681f3Smrg pan_pack_color_32(packed, s | (s << 16)); 1057ec681f3Smrg } else if (size == 2) 1067ec681f3Smrg pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16)); 1077ec681f3Smrg else if (size <= 4) 1087ec681f3Smrg pan_pack_color_32(packed, out.ui[0]); 1097ec681f3Smrg else if (size <= 8) { 1107ec681f3Smrg memcpy(packed + 0, out.ui, 8); 1117ec681f3Smrg memcpy(packed + 2, out.ui, 8); 1127ec681f3Smrg } else { 1137ec681f3Smrg memcpy(packed, out.ui, 16); 1147ec681f3Smrg } 1157ec681f3Smrg} 1167ec681f3Smrg 1177ec681f3Smrgvoid 1187ec681f3Smrgpan_pack_color(uint32_t *packed, const union pipe_color_union *color, 1197ec681f3Smrg enum pipe_format format, bool dithered) 1207ec681f3Smrg{ 1217ec681f3Smrg /* Set of blendable formats is common across versions. TODO: v9 */ 1227ec681f3Smrg enum mali_color_buffer_internal_format internal = 1237ec681f3Smrg panfrost_blendable_formats_v7[format].internal; 1247ec681f3Smrg 1257ec681f3Smrg if (internal == MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW_VALUE) { 1267ec681f3Smrg pan_pack_raw(packed, color, format); 1277ec681f3Smrg return; 1287ec681f3Smrg } 1297ec681f3Smrg 1307ec681f3Smrg /* Saturate to [0, 1] by definition of UNORM. Prevents overflow. */ 1317ec681f3Smrg float r = SATURATE(color->f[0]); 1327ec681f3Smrg float g = SATURATE(color->f[1]); 1337ec681f3Smrg float b = SATURATE(color->f[2]); 1347ec681f3Smrg float a = SATURATE(color->f[3]); 1357ec681f3Smrg 1367ec681f3Smrg /* Fill in alpha = 1.0 by default */ 1377ec681f3Smrg if (!util_format_has_alpha(format)) 1387ec681f3Smrg a = 1.0; 1397ec681f3Smrg 1407ec681f3Smrg /* Convert colourspace while we still have floats */ 1417ec681f3Smrg if (util_format_is_srgb(format)) { 1427ec681f3Smrg r = util_format_linear_to_srgb_float(r); 1437ec681f3Smrg g = util_format_linear_to_srgb_float(g); 1447ec681f3Smrg b = util_format_linear_to_srgb_float(b); 1457ec681f3Smrg } 1467ec681f3Smrg 1477ec681f3Smrg /* Look up the layout of the tilebuffer */ 1487ec681f3Smrg assert(internal < ARRAY_SIZE(tib_layouts)); 1497ec681f3Smrg struct mali_tib_layout l = tib_layouts[internal]; 1507ec681f3Smrg 1517ec681f3Smrg unsigned count_r = l.int_r + l.frac_r; 1527ec681f3Smrg unsigned count_g = l.int_g + l.frac_g + count_r; 1537ec681f3Smrg unsigned count_b = l.int_b + l.frac_b + count_g; 1547ec681f3Smrg ASSERTED unsigned count_a = l.int_a + l.frac_a + count_b; 1557ec681f3Smrg 1567ec681f3Smrg /* Must fill the word */ 1577ec681f3Smrg assert(count_a == 32); 1587ec681f3Smrg 1597ec681f3Smrg /* Convert the transformed float colour to the given layout */ 1607ec681f3Smrg uint32_t ur = float_to_fixed(r, l.int_r, l.frac_r, dithered) << 0; 1617ec681f3Smrg uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g, dithered) << count_r; 1627ec681f3Smrg uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b, dithered) << count_g; 1637ec681f3Smrg uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a, dithered) << count_b; 1647ec681f3Smrg 1657ec681f3Smrg pan_pack_color_32(packed, ur | ug | ub | ua); 1667ec681f3Smrg} 167