From 5bb4e6956dcd8e0a6916207cc02af3fe5f631e44 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Thu, 28 Feb 2019 07:26:37 +0100 Subject: [PATCH] etnaviv: implement ETC2 block patching for HALTI0 ETC2 is supported with HALTI0, however that implementation is buggy in hardware. The blob driver does per-block patching to work around this. We need to swap colors for t-mode etc2 blocks. Changes v2 -> v3: - Drop redundant format check Signed-off-by: Christian Gmeiner Acked-by: Lucas Stach --- src/gallium/drivers/etnaviv/Makefile.sources | 2 + src/gallium/drivers/etnaviv/etnaviv_etc2.c | 146 +++++++++++++++++++ src/gallium/drivers/etnaviv/etnaviv_etc2.h | 51 +++++++ src/gallium/drivers/etnaviv/meson.build | 2 + 4 files changed, 201 insertions(+) create mode 100644 src/gallium/drivers/etnaviv/etnaviv_etc2.c create mode 100644 src/gallium/drivers/etnaviv/etnaviv_etc2.h diff --git a/src/gallium/drivers/etnaviv/Makefile.sources b/src/gallium/drivers/etnaviv/Makefile.sources index 0b208122999..01e7e49a38a 100644 --- a/src/gallium/drivers/etnaviv/Makefile.sources +++ b/src/gallium/drivers/etnaviv/Makefile.sources @@ -25,6 +25,8 @@ C_SOURCES := \ etnaviv_disasm.h \ etnaviv_emit.c \ etnaviv_emit.h \ + etnaviv_etc2.c \ + etnaviv_etc2.h \ etnaviv_fence.c \ etnaviv_fence.h \ etnaviv_format.c \ diff --git a/src/gallium/drivers/etnaviv/etnaviv_etc2.c b/src/gallium/drivers/etnaviv/etnaviv_etc2.c new file mode 100644 index 00000000000..1bb9be12f1c --- /dev/null +++ b/src/gallium/drivers/etnaviv/etnaviv_etc2.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2019 Etnaviv Project + * Copyright (C) 2019 Zodiac Inflight Innovations + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sub license, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Christian Gmeiner + */ + +#include "etnaviv_etc2.h" +#include "etnaviv_resource.h" +#include "etnaviv_screen.h" +#include "hw/common.xml.h" +#include "util/u_format.h" + +bool +etna_etc2_needs_patching(const struct pipe_resource *prsc) +{ + const struct etna_screen *screen = etna_screen(prsc->screen); + + if (!util_format_is_etc(prsc->format)) + return false; + + if (VIV_FEATURE(screen, chipMinorFeatures2, HALTI1)) + return false; + + switch (prsc->format) { + case PIPE_FORMAT_ETC2_RGB8: + case PIPE_FORMAT_ETC2_SRGB8: + case PIPE_FORMAT_ETC2_RGB8A1: + case PIPE_FORMAT_ETC2_SRGB8A1: + case PIPE_FORMAT_ETC2_RGBA8: + case PIPE_FORMAT_ETC2_SRGBA8: + return true; + break; + + default: + return false; + } +} + +static inline bool +needs_patching(uint8_t *buffer, bool punchthrough_alpha) +{ + /* punchthrough_alpha or etc2 individual mode? */ + if (!punchthrough_alpha && !(buffer[3] & 0x2)) + return false; + + /* etc2 t-mode? */ + static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 }; + const int R_plus_dR = (buffer[0] >> 3) + lookup[buffer[0] & 0x7]; + + if (R_plus_dR < 0 || R_plus_dR > 31) + return true; + + return false; +} + +void +etna_etc2_calculate_blocks(uint8_t *buffer, unsigned stride, + unsigned width, unsigned height, + enum pipe_format format, + struct util_dynarray *offsets) +{ + const unsigned bw = util_format_get_blockwidth(format); + const unsigned bh = util_format_get_blockheight(format); + const unsigned bs = util_format_get_blocksize(format); + bool punchthrough_alpha = false; + unsigned offset = 0; + const uint8_t *base = buffer; + + if (format == PIPE_FORMAT_ETC2_RGB8A1 || + format == PIPE_FORMAT_ETC2_SRGB8A1) + punchthrough_alpha = true; + + if (format == PIPE_FORMAT_ETC2_RGBA8 || + format == PIPE_FORMAT_ETC2_SRGBA8 || + format == PIPE_FORMAT_ETC2_SRGB8A1) + offset = 8; + + for (unsigned y = 0; y < height; y += bh) { + uint8_t *src = buffer; + + for (unsigned x = 0; x < width; x += bw) { + if (needs_patching(src + offset, punchthrough_alpha)) + util_dynarray_append(offsets, unsigned, src + offset - base); + + src += bs; + } + + buffer += stride; + } +} + +static inline void +swap_colors(uint8_t *buffer) +{ + const uint8_t r1a = (buffer[0] & 0x18) >> 3; + const uint8_t r1b = (buffer[0] & 0x3); + const uint8_t r1 = (r1a << 2) | r1b; + const uint8_t g1 = (buffer[1] & 0xf0) >> 4; + const uint8_t b1 = (buffer[1] & 0x0f); + const uint8_t r2 = (buffer[2] & 0xf0) >> 4; + const uint8_t g2 = buffer[2] & 0x0f; + const uint8_t b2 = (buffer[3] & 0xf0) >> 4; + const uint8_t rest = (buffer[3] & 0x0f); + + /* fixup R and dR used for t-mode detection */ + static const uint8_t fixup[16] = { + 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0xe0, + 0x04, 0x04, 0xe0, 0xe0, + 0x04, 0xe0, 0xe0, 0xe0 + }; + + /* swap colors */ + buffer[0] = fixup[r2] | ((r2 & 0x0c) << 1) | (r2 & 0x03); + buffer[1] = (g2 << 4) | b2; + buffer[2] = (r1 << 4) | g1; + buffer[3] = (b1 << 4) | rest; +} + +void +etna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets) +{ + util_dynarray_foreach(offsets, unsigned, offset) + swap_colors(buffer + *offset); +} diff --git a/src/gallium/drivers/etnaviv/etnaviv_etc2.h b/src/gallium/drivers/etnaviv/etnaviv_etc2.h new file mode 100644 index 00000000000..334f05ae669 --- /dev/null +++ b/src/gallium/drivers/etnaviv/etnaviv_etc2.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Etnaviv Project + * Copyright (C) 2019 Zodiac Inflight Innovations + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sub license, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Christian Gmeiner + */ + +#ifndef H_ETNA_ETC2 +#define H_ETNA_ETC2 + +#include +#include + +#include "pipe/p_format.h" +#include "util/u_dynarray.h" + +struct pipe_resource; + +bool +etna_etc2_needs_patching(const struct pipe_resource *prsc); + +void +etna_etc2_calculate_blocks(uint8_t *buffer, unsigned stride, + unsigned width, unsigned height, + enum pipe_format format, + struct util_dynarray *offsets); + +void +etna_etc2_patch(uint8_t *buffer, const struct util_dynarray *offsets); + +#endif diff --git a/src/gallium/drivers/etnaviv/meson.build b/src/gallium/drivers/etnaviv/meson.build index 63553dec510..75ed93eccc7 100644 --- a/src/gallium/drivers/etnaviv/meson.build +++ b/src/gallium/drivers/etnaviv/meson.build @@ -44,6 +44,8 @@ files_etnaviv = files( 'etnaviv_disasm.h', 'etnaviv_emit.c', 'etnaviv_emit.h', + 'etnaviv_etc2.c', + 'etnaviv_etc2.h', 'etnaviv_fence.c', 'etnaviv_fence.h', 'etnaviv_format.c', -- 2.30.2