Path: blob/21.2-virgl/src/gallium/drivers/etnaviv/etnaviv_etc2.c
4570 views
/*1* Copyright (c) 2019 Etnaviv Project2* Copyright (C) 2019 Zodiac Inflight Innovations3*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, sub license,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 the12* next paragraph) shall be included in all copies or substantial portions13* of the 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 NON-INFRINGEMENT. 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, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23* Authors:24* Christian Gmeiner <[email protected]>25*/2627#include "etnaviv_etc2.h"28#include "etnaviv_resource.h"29#include "etnaviv_screen.h"30#include "hw/common.xml.h"31#include "util/format/u_format.h"3233bool34etna_etc2_needs_patching(const struct pipe_resource *prsc)35{36const struct etna_screen *screen = etna_screen(prsc->screen);3738if (!util_format_is_etc(prsc->format))39return false;4041if (VIV_FEATURE(screen, chipMinorFeatures2, HALTI1))42return false;4344switch (prsc->format) {45case PIPE_FORMAT_ETC2_RGB8:46case PIPE_FORMAT_ETC2_SRGB8:47case PIPE_FORMAT_ETC2_RGB8A1:48case PIPE_FORMAT_ETC2_SRGB8A1:49case PIPE_FORMAT_ETC2_RGBA8:50case PIPE_FORMAT_ETC2_SRGBA8:51return true;52break;5354default:55return false;56}57}5859static inline bool60needs_patching(uint8_t *buffer, bool punchthrough_alpha)61{62/* punchthrough_alpha or etc2 individual mode? */63if (!punchthrough_alpha && !(buffer[3] & 0x2))64return false;6566/* etc2 t-mode? */67static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };68const int R_plus_dR = (buffer[0] >> 3) + lookup[buffer[0] & 0x7];6970if (R_plus_dR < 0 || R_plus_dR > 31)71return true;7273return false;74}7576void77etna_etc2_calculate_blocks(uint8_t *buffer, unsigned stride,78unsigned width, unsigned height,79enum pipe_format format,80struct util_dynarray *offsets)81{82const unsigned bw = util_format_get_blockwidth(format);83const unsigned bh = util_format_get_blockheight(format);84const unsigned bs = util_format_get_blocksize(format);85bool punchthrough_alpha = false;86unsigned offset = 0;87const uint8_t *base = buffer;8889if (format == PIPE_FORMAT_ETC2_RGB8A1 ||90format == PIPE_FORMAT_ETC2_SRGB8A1)91punchthrough_alpha = true;9293if (format == PIPE_FORMAT_ETC2_RGBA8 ||94format == PIPE_FORMAT_ETC2_SRGBA8 ||95format == PIPE_FORMAT_ETC2_SRGB8A1)96offset = 8;9798for (unsigned y = 0; y < height; y += bh) {99uint8_t *src = buffer;100101for (unsigned x = 0; x < width; x += bw) {102if (needs_patching(src + offset, punchthrough_alpha))103util_dynarray_append(offsets, unsigned, src + offset - base);104105src += bs;106}107108buffer += stride;109}110}111112static inline void113swap_colors(uint8_t *buffer)114{115const uint8_t r1a = (buffer[0] & 0x18) >> 3;116const uint8_t r1b = (buffer[0] & 0x3);117const uint8_t r1 = (r1a << 2) | r1b;118const uint8_t g1 = (buffer[1] & 0xf0) >> 4;119const uint8_t b1 = (buffer[1] & 0x0f);120const uint8_t r2 = (buffer[2] & 0xf0) >> 4;121const uint8_t g2 = buffer[2] & 0x0f;122const uint8_t b2 = (buffer[3] & 0xf0) >> 4;123const uint8_t rest = (buffer[3] & 0x0f);124125/* fixup R and dR used for t-mode detection */126static const uint8_t fixup[16] = {1270x04, 0x04, 0x04, 0x04,1280x04, 0x04, 0x04, 0xe0,1290x04, 0x04, 0xe0, 0xe0,1300x04, 0xe0, 0xe0, 0xe0131};132133/* swap colors */134buffer[0] = fixup[r2] | ((r2 & 0x0c) << 1) | (r2 & 0x03);135buffer[1] = (g2 << 4) | b2;136buffer[2] = (r1 << 4) | g1;137buffer[3] = (b1 << 4) | rest;138}139140void141etna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets)142{143util_dynarray_foreach(offsets, unsigned, offset)144swap_colors(buffer + *offset);145}146147148