19f464c52Smaya/* 29f464c52Smaya * Copyright (c) 2019 Etnaviv Project 39f464c52Smaya * Copyright (C) 2019 Zodiac Inflight Innovations 49f464c52Smaya * 59f464c52Smaya * Permission is hereby granted, free of charge, to any person obtaining a 69f464c52Smaya * copy of this software and associated documentation files (the "Software"), 79f464c52Smaya * to deal in the Software without restriction, including without limitation 89f464c52Smaya * the rights to use, copy, modify, merge, publish, distribute, sub license, 99f464c52Smaya * and/or sell copies of the Software, and to permit persons to whom the 109f464c52Smaya * Software is furnished to do so, subject to the following conditions: 119f464c52Smaya * 129f464c52Smaya * The above copyright notice and this permission notice (including the 139f464c52Smaya * next paragraph) shall be included in all copies or substantial portions 149f464c52Smaya * of the Software. 159f464c52Smaya * 169f464c52Smaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 179f464c52Smaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 189f464c52Smaya * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 199f464c52Smaya * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 209f464c52Smaya * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 219f464c52Smaya * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 229f464c52Smaya * DEALINGS IN THE SOFTWARE. 239f464c52Smaya * 249f464c52Smaya * Authors: 259f464c52Smaya * Christian Gmeiner <christian.gmeiner@gmail.com> 269f464c52Smaya */ 279f464c52Smaya 289f464c52Smaya#include "etnaviv_etc2.h" 299f464c52Smaya#include "etnaviv_resource.h" 309f464c52Smaya#include "etnaviv_screen.h" 319f464c52Smaya#include "hw/common.xml.h" 327ec681f3Smrg#include "util/format/u_format.h" 339f464c52Smaya 349f464c52Smayabool 359f464c52Smayaetna_etc2_needs_patching(const struct pipe_resource *prsc) 369f464c52Smaya{ 379f464c52Smaya const struct etna_screen *screen = etna_screen(prsc->screen); 389f464c52Smaya 399f464c52Smaya if (!util_format_is_etc(prsc->format)) 409f464c52Smaya return false; 419f464c52Smaya 429f464c52Smaya if (VIV_FEATURE(screen, chipMinorFeatures2, HALTI1)) 439f464c52Smaya return false; 449f464c52Smaya 459f464c52Smaya switch (prsc->format) { 469f464c52Smaya case PIPE_FORMAT_ETC2_RGB8: 479f464c52Smaya case PIPE_FORMAT_ETC2_SRGB8: 489f464c52Smaya case PIPE_FORMAT_ETC2_RGB8A1: 499f464c52Smaya case PIPE_FORMAT_ETC2_SRGB8A1: 509f464c52Smaya case PIPE_FORMAT_ETC2_RGBA8: 519f464c52Smaya case PIPE_FORMAT_ETC2_SRGBA8: 529f464c52Smaya return true; 539f464c52Smaya break; 549f464c52Smaya 559f464c52Smaya default: 569f464c52Smaya return false; 579f464c52Smaya } 589f464c52Smaya} 599f464c52Smaya 609f464c52Smayastatic inline bool 619f464c52Smayaneeds_patching(uint8_t *buffer, bool punchthrough_alpha) 629f464c52Smaya{ 639f464c52Smaya /* punchthrough_alpha or etc2 individual mode? */ 649f464c52Smaya if (!punchthrough_alpha && !(buffer[3] & 0x2)) 659f464c52Smaya return false; 669f464c52Smaya 679f464c52Smaya /* etc2 t-mode? */ 689f464c52Smaya static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 }; 699f464c52Smaya const int R_plus_dR = (buffer[0] >> 3) + lookup[buffer[0] & 0x7]; 709f464c52Smaya 719f464c52Smaya if (R_plus_dR < 0 || R_plus_dR > 31) 729f464c52Smaya return true; 739f464c52Smaya 749f464c52Smaya return false; 759f464c52Smaya} 769f464c52Smaya 779f464c52Smayavoid 789f464c52Smayaetna_etc2_calculate_blocks(uint8_t *buffer, unsigned stride, 799f464c52Smaya unsigned width, unsigned height, 809f464c52Smaya enum pipe_format format, 819f464c52Smaya struct util_dynarray *offsets) 829f464c52Smaya{ 839f464c52Smaya const unsigned bw = util_format_get_blockwidth(format); 849f464c52Smaya const unsigned bh = util_format_get_blockheight(format); 859f464c52Smaya const unsigned bs = util_format_get_blocksize(format); 869f464c52Smaya bool punchthrough_alpha = false; 879f464c52Smaya unsigned offset = 0; 889f464c52Smaya const uint8_t *base = buffer; 899f464c52Smaya 909f464c52Smaya if (format == PIPE_FORMAT_ETC2_RGB8A1 || 919f464c52Smaya format == PIPE_FORMAT_ETC2_SRGB8A1) 929f464c52Smaya punchthrough_alpha = true; 939f464c52Smaya 949f464c52Smaya if (format == PIPE_FORMAT_ETC2_RGBA8 || 959f464c52Smaya format == PIPE_FORMAT_ETC2_SRGBA8 || 969f464c52Smaya format == PIPE_FORMAT_ETC2_SRGB8A1) 979f464c52Smaya offset = 8; 989f464c52Smaya 999f464c52Smaya for (unsigned y = 0; y < height; y += bh) { 1009f464c52Smaya uint8_t *src = buffer; 1019f464c52Smaya 1029f464c52Smaya for (unsigned x = 0; x < width; x += bw) { 1039f464c52Smaya if (needs_patching(src + offset, punchthrough_alpha)) 1049f464c52Smaya util_dynarray_append(offsets, unsigned, src + offset - base); 1059f464c52Smaya 1069f464c52Smaya src += bs; 1079f464c52Smaya } 1089f464c52Smaya 1099f464c52Smaya buffer += stride; 1109f464c52Smaya } 1119f464c52Smaya} 1129f464c52Smaya 1139f464c52Smayastatic inline void 1149f464c52Smayaswap_colors(uint8_t *buffer) 1159f464c52Smaya{ 1169f464c52Smaya const uint8_t r1a = (buffer[0] & 0x18) >> 3; 1179f464c52Smaya const uint8_t r1b = (buffer[0] & 0x3); 1189f464c52Smaya const uint8_t r1 = (r1a << 2) | r1b; 1199f464c52Smaya const uint8_t g1 = (buffer[1] & 0xf0) >> 4; 1209f464c52Smaya const uint8_t b1 = (buffer[1] & 0x0f); 1219f464c52Smaya const uint8_t r2 = (buffer[2] & 0xf0) >> 4; 1229f464c52Smaya const uint8_t g2 = buffer[2] & 0x0f; 1239f464c52Smaya const uint8_t b2 = (buffer[3] & 0xf0) >> 4; 1249f464c52Smaya const uint8_t rest = (buffer[3] & 0x0f); 1259f464c52Smaya 1269f464c52Smaya /* fixup R and dR used for t-mode detection */ 1279f464c52Smaya static const uint8_t fixup[16] = { 1289f464c52Smaya 0x04, 0x04, 0x04, 0x04, 1299f464c52Smaya 0x04, 0x04, 0x04, 0xe0, 1309f464c52Smaya 0x04, 0x04, 0xe0, 0xe0, 1319f464c52Smaya 0x04, 0xe0, 0xe0, 0xe0 1329f464c52Smaya }; 1339f464c52Smaya 1349f464c52Smaya /* swap colors */ 1359f464c52Smaya buffer[0] = fixup[r2] | ((r2 & 0x0c) << 1) | (r2 & 0x03); 1369f464c52Smaya buffer[1] = (g2 << 4) | b2; 1379f464c52Smaya buffer[2] = (r1 << 4) | g1; 1389f464c52Smaya buffer[3] = (b1 << 4) | rest; 1399f464c52Smaya} 1409f464c52Smaya 1419f464c52Smayavoid 1429f464c52Smayaetna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets) 1439f464c52Smaya{ 1449f464c52Smaya util_dynarray_foreach(offsets, unsigned, offset) 1459f464c52Smaya swap_colors(buffer + *offset); 1469f464c52Smaya} 147