Path: blob/21.2-virgl/src/panfrost/lib/pan_clear.c
4560 views
/*1* Copyright (C) 2019-2021 Collabora, Ltd.2* Copyright (C) 2019 Alyssa Rosenzweig3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23*/2425#include <string.h>26#include "pan_util.h"27#include "pan_format.h"28#include "gallium/auxiliary/util/u_pack_color.h"29#include "util/rounding.h"30#include "util/format_srgb.h"3132/* Clear colours are packed as the internal format of the tilebuffer, looked up33* in the blendable formats table given the render target format.34*35* Raw formats may emulate arbitrary formats with blend shaders. For these, we36* defer to util_pack_colour to pack in the API format.37*38* Blendable formats, on the other hand, include extra "fractional" bits in the39* tilebuffer for dithering. These have a packed fixed-point representation:40* for a channel with m integer bits and n fractional bits, multiply by ((2^m)41* - 1) * 2^n and round to the nearest even.42*/4344/* Replicate a 32-bit value to fill 128-bit */4546static void47pan_pack_color_32(uint32_t *packed, uint32_t v)48{49for (unsigned i = 0; i < 4; ++i)50packed[i] = v;51}5253/* For m integer bits and n fractional bits, calculate the conversion factor,54* multiply the source value, and convert to integer rounding to even */5556static inline uint32_t57float_to_fixed(float f, unsigned bits_int, unsigned bits_frac)58{59float factor = ((1 << bits_int) - 1) << bits_frac;60return _mesa_roundevenf(f * factor);61}6263/* These values are shared across hardware versions. Don't include GenXML. */64enum mali_color_buffer_internal_format {65MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW = 0,66MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8 = 1,67MALI_COLOR_BUFFER_INTERNAL_FORMAT_R10G10B10A2 = 2,68MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A2 = 3,69MALI_COLOR_BUFFER_INTERNAL_FORMAT_R4G4B4A4 = 4,70MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G6B5A0 = 5,71MALI_COLOR_BUFFER_INTERNAL_FORMAT_R5G5B5A1 = 6,72MALI_COLOR_BUFFER_NUM_FORMATS,73};7475struct mali_tib_layout {76unsigned int_r, frac_r;77unsigned int_g, frac_g;78unsigned int_b, frac_b;79unsigned int_a, frac_a;80};8182static const struct mali_tib_layout tib_layouts[MALI_COLOR_BUFFER_NUM_FORMATS] = {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};9091/* Raw values are stored as-is but replicated for multisampling */9293static void94pan_pack_raw(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)95{96union util_color out = { 0 };97unsigned size = util_format_get_blocksize(format);98assert(size <= 16);99100util_pack_color(color->f, format, &out);101102if (size == 1) {103unsigned s = out.ui[0] | (out.ui[0] << 8);104pan_pack_color_32(packed, s | (s << 16));105} else if (size == 2)106pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));107else if (size <= 4)108pan_pack_color_32(packed, out.ui[0]);109else if (size <= 8) {110memcpy(packed + 0, out.ui, 8);111memcpy(packed + 2, out.ui, 8);112} else {113memcpy(packed, out.ui, 16);114}115}116117void118pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)119{120/* Set of blendable formats is common across versions. TODO: v9 */121enum mali_color_buffer_internal_format internal =122panfrost_blendable_formats_v7[format].internal;123124if (internal == MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW) {125pan_pack_raw(packed, color, format);126return;127}128129/* Saturate to [0, 1] by definition of UNORM. Prevents overflow. */130float r = SATURATE(color->f[0]);131float g = SATURATE(color->f[1]);132float b = SATURATE(color->f[2]);133float a = SATURATE(color->f[3]);134135/* Fill in alpha = 1.0 by default */136if (!util_format_has_alpha(format))137a = 1.0;138139/* Convert colourspace while we still have floats */140if (util_format_is_srgb(format)) {141r = util_format_linear_to_srgb_float(r);142g = util_format_linear_to_srgb_float(g);143b = util_format_linear_to_srgb_float(b);144}145146/* Look up the layout of the tilebuffer */147assert(internal < MALI_COLOR_BUFFER_NUM_FORMATS);148struct mali_tib_layout l = tib_layouts[internal];149150unsigned count_r = l.int_r + l.frac_r;151unsigned count_g = l.int_g + l.frac_g + count_r;152unsigned count_b = l.int_b + l.frac_b + count_g;153ASSERTED unsigned count_a = l.int_a + l.frac_a + count_b;154155/* Must fill the word */156assert(count_a == 32);157158/* Convert the transformed float colour to the given layout */159uint32_t ur = float_to_fixed(r, l.int_r, l.frac_r) << 0;160uint32_t ug = float_to_fixed(g, l.int_g, l.frac_g) << count_r;161uint32_t ub = float_to_fixed(b, l.int_b, l.frac_b) << count_g;162uint32_t ua = float_to_fixed(a, l.int_a, l.frac_a) << count_b;163164pan_pack_color_32(packed, ur | ug | ub | ua);165}166167168