17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Raspberry Pi
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "v3dv_private.h"
257ec681f3Smrg#include "broadcom/common/v3d_macros.h"
267ec681f3Smrg#include "broadcom/cle/v3dx_pack.h"
277ec681f3Smrg
287ec681f3Smrg#include "util/format/u_format.h"
297ec681f3Smrg
307ec681f3Smrg#define SWIZ(x,y,z,w) {   \
317ec681f3Smrg   PIPE_SWIZZLE_##x,      \
327ec681f3Smrg   PIPE_SWIZZLE_##y,      \
337ec681f3Smrg   PIPE_SWIZZLE_##z,      \
347ec681f3Smrg   PIPE_SWIZZLE_##w       \
357ec681f3Smrg}
367ec681f3Smrg
377ec681f3Smrg#define FORMAT(vk, rt, tex, swiz, return_size, supports_filtering)  \
387ec681f3Smrg   [VK_FORMAT_##vk] = {                                             \
397ec681f3Smrg      true,                                                         \
407ec681f3Smrg      V3D_OUTPUT_IMAGE_FORMAT_##rt,                                 \
417ec681f3Smrg      TEXTURE_DATA_FORMAT_##tex,                                    \
427ec681f3Smrg      swiz,                                                         \
437ec681f3Smrg      return_size,                                                  \
447ec681f3Smrg      supports_filtering,                                           \
457ec681f3Smrg   }
467ec681f3Smrg
477ec681f3Smrg#define SWIZ_X001 SWIZ(X, 0, 0, 1)
487ec681f3Smrg#define SWIZ_XY01 SWIZ(X, Y, 0, 1)
497ec681f3Smrg#define SWIZ_XYZ1 SWIZ(X, Y, Z, 1)
507ec681f3Smrg#define SWIZ_XYZW SWIZ(X, Y, Z, W)
517ec681f3Smrg#define SWIZ_YZWX SWIZ(Y, Z, W, X)
527ec681f3Smrg#define SWIZ_YZW1 SWIZ(Y, Z, W, 1)
537ec681f3Smrg#define SWIZ_ZYXW SWIZ(Z, Y, X, W)
547ec681f3Smrg#define SWIZ_ZYX1 SWIZ(Z, Y, X, 1)
557ec681f3Smrg#define SWIZ_XXXY SWIZ(X, X, X, Y)
567ec681f3Smrg#define SWIZ_XXX1 SWIZ(X, X, X, 1)
577ec681f3Smrg#define SWIZ_XXXX SWIZ(X, X, X, X)
587ec681f3Smrg#define SWIZ_000X SWIZ(0, 0, 0, X)
597ec681f3Smrg#define SWIZ_WXYZ SWIZ(W, X, Y, Z)
607ec681f3Smrg
617ec681f3Smrg/* FIXME: expand format table to describe whether the format is supported
627ec681f3Smrg * for buffer surfaces (texel buffers, vertex buffers, etc).
637ec681f3Smrg */
647ec681f3Smrgstatic const struct v3dv_format format_table[] = {
657ec681f3Smrg   /* Color, 4 channels */
667ec681f3Smrg   FORMAT(B8G8R8A8_SRGB,           SRGB8_ALPHA8, RGBA8,         SWIZ_ZYXW, 16, true),
677ec681f3Smrg   FORMAT(B8G8R8A8_UNORM,          RGBA8,        RGBA8,         SWIZ_ZYXW, 16, true),
687ec681f3Smrg
697ec681f3Smrg   FORMAT(R8G8B8A8_SRGB,           SRGB8_ALPHA8, RGBA8,         SWIZ_XYZW, 16, true),
707ec681f3Smrg   FORMAT(R8G8B8A8_UNORM,          RGBA8,        RGBA8,         SWIZ_XYZW, 16, true),
717ec681f3Smrg   FORMAT(R8G8B8A8_SNORM,          NO,           RGBA8_SNORM,   SWIZ_XYZW, 16, true),
727ec681f3Smrg   FORMAT(R8G8B8A8_SINT,           RGBA8I,       RGBA8I,        SWIZ_XYZW, 16, false),
737ec681f3Smrg   FORMAT(R8G8B8A8_UINT,           RGBA8UI,      RGBA8UI,       SWIZ_XYZW, 16, false),
747ec681f3Smrg
757ec681f3Smrg   FORMAT(R16G16B16A16_SFLOAT,     RGBA16F,      RGBA16F,       SWIZ_XYZW, 16, true),
767ec681f3Smrg   FORMAT(R16G16B16A16_UNORM,      NO,           RGBA16,        SWIZ_XYZW, 32, true),
777ec681f3Smrg   FORMAT(R16G16B16A16_SNORM,      NO,           RGBA16_SNORM,  SWIZ_XYZW, 32, true),
787ec681f3Smrg   FORMAT(R16G16B16A16_SINT,       RGBA16I,      RGBA16I,       SWIZ_XYZW, 16, false),
797ec681f3Smrg   FORMAT(R16G16B16A16_UINT,       RGBA16UI,     RGBA16UI,      SWIZ_XYZW, 16, false),
807ec681f3Smrg
817ec681f3Smrg   FORMAT(R32G32B32A32_SFLOAT,     RGBA32F,      RGBA32F,       SWIZ_XYZW, 32, false),
827ec681f3Smrg   FORMAT(R32G32B32A32_SINT,       RGBA32I,      RGBA32I,       SWIZ_XYZW, 32, false),
837ec681f3Smrg   FORMAT(R32G32B32A32_UINT,       RGBA32UI,     RGBA32UI,      SWIZ_XYZW, 32, false),
847ec681f3Smrg
857ec681f3Smrg   /* Color, 3 channels */
867ec681f3Smrg   FORMAT(R32G32B32_SFLOAT,        NO,           NO,            SWIZ_XYZ1,  0, false),
877ec681f3Smrg   FORMAT(R32G32B32_UINT,          NO,           NO,            SWIZ_XYZ1,  0, false),
887ec681f3Smrg   FORMAT(R32G32B32_SINT,          NO,           NO,            SWIZ_XYZ1,  0, false),
897ec681f3Smrg
907ec681f3Smrg   /* Color, 2 channels */
917ec681f3Smrg   FORMAT(R8G8_UNORM,              RG8,          RG8,           SWIZ_XY01, 16, true),
927ec681f3Smrg   FORMAT(R8G8_SNORM,              NO,           RG8_SNORM,     SWIZ_XY01, 16, true),
937ec681f3Smrg   FORMAT(R8G8_SINT,               RG8I,         RG8I,          SWIZ_XY01, 16, false),
947ec681f3Smrg   FORMAT(R8G8_UINT,               RG8UI,        RG8UI,         SWIZ_XY01, 16, false),
957ec681f3Smrg
967ec681f3Smrg   FORMAT(R16G16_UNORM,            NO,           RG16,          SWIZ_XY01, 32, true),
977ec681f3Smrg   FORMAT(R16G16_SNORM,            NO,           RG16_SNORM,    SWIZ_XY01, 32, true),
987ec681f3Smrg   FORMAT(R16G16_SFLOAT,           RG16F,        RG16F,         SWIZ_XY01, 16, true),
997ec681f3Smrg   FORMAT(R16G16_SINT,             RG16I,        RG16I,         SWIZ_XY01, 16, false),
1007ec681f3Smrg   FORMAT(R16G16_UINT,             RG16UI,       RG16UI,        SWIZ_XY01, 16, false),
1017ec681f3Smrg
1027ec681f3Smrg   FORMAT(R32G32_SFLOAT,           RG32F,        RG32F,         SWIZ_XY01, 32, false),
1037ec681f3Smrg   FORMAT(R32G32_SINT,             RG32I,        RG32I,         SWIZ_XY01, 32, false),
1047ec681f3Smrg   FORMAT(R32G32_UINT,             RG32UI,       RG32UI,        SWIZ_XY01, 32, false),
1057ec681f3Smrg
1067ec681f3Smrg   /* Color, 1 channel */
1077ec681f3Smrg   FORMAT(R8_UNORM,                R8,           R8,            SWIZ_X001, 16, true),
1087ec681f3Smrg   FORMAT(R8_SNORM,                NO,           R8_SNORM,      SWIZ_X001, 16, true),
1097ec681f3Smrg   FORMAT(R8_SINT,                 R8I,          R8I,           SWIZ_X001, 16, false),
1107ec681f3Smrg   FORMAT(R8_UINT,                 R8UI,         R8UI,          SWIZ_X001, 16, false),
1117ec681f3Smrg
1127ec681f3Smrg   FORMAT(R16_UNORM,               NO,           R16,           SWIZ_X001, 32, true),
1137ec681f3Smrg   FORMAT(R16_SNORM,               NO,           R16_SNORM,     SWIZ_X001, 32, true),
1147ec681f3Smrg   FORMAT(R16_SFLOAT,              R16F,         R16F,          SWIZ_X001, 16, true),
1157ec681f3Smrg   FORMAT(R16_SINT,                R16I,         R16I,          SWIZ_X001, 16, false),
1167ec681f3Smrg   FORMAT(R16_UINT,                R16UI,        R16UI,         SWIZ_X001, 16, false),
1177ec681f3Smrg
1187ec681f3Smrg   FORMAT(R32_SFLOAT,              R32F,         R32F,          SWIZ_X001, 32, false),
1197ec681f3Smrg   FORMAT(R32_SINT,                R32I,         R32I,          SWIZ_X001, 32, false),
1207ec681f3Smrg   FORMAT(R32_UINT,                R32UI,        R32UI,         SWIZ_X001, 32, false),
1217ec681f3Smrg
1227ec681f3Smrg   /* Color, packed */
1237ec681f3Smrg   FORMAT(R4G4B4A4_UNORM_PACK16,   ABGR4444,     RGBA4,         SWIZ_XYZW, 16, true),
1247ec681f3Smrg   FORMAT(B4G4R4A4_UNORM_PACK16,   ABGR4444,     RGBA4,         SWIZ_ZYXW, 16, true), /* Swap RB */
1257ec681f3Smrg   FORMAT(R5G6B5_UNORM_PACK16,     BGR565,       RGB565,        SWIZ_XYZ1, 16, true),
1267ec681f3Smrg   FORMAT(R5G5B5A1_UNORM_PACK16,   ABGR1555,     RGB5_A1,       SWIZ_XYZW, 16, true),
1277ec681f3Smrg   FORMAT(A1R5G5B5_UNORM_PACK16,   RGBA5551,     A1_RGB5,       SWIZ_ZYXW, 16, true), /* Swap RB */
1287ec681f3Smrg   FORMAT(A8B8G8R8_UNORM_PACK32,   RGBA8,        RGBA8,         SWIZ_XYZW, 16, true), /* RGBA8 UNORM */
1297ec681f3Smrg   FORMAT(A8B8G8R8_SNORM_PACK32,   NO,           RGBA8_SNORM,   SWIZ_XYZW, 16, true), /* RGBA8 SNORM */
1307ec681f3Smrg   FORMAT(A8B8G8R8_UINT_PACK32,    RGBA8UI,      RGBA8UI,       SWIZ_XYZW, 16, false), /* RGBA8 UINT */
1317ec681f3Smrg   FORMAT(A8B8G8R8_SINT_PACK32,    RGBA8I,       RGBA8I,        SWIZ_XYZW, 16, false), /* RGBA8 SINT */
1327ec681f3Smrg   FORMAT(A8B8G8R8_SRGB_PACK32,    SRGB8_ALPHA8, RGBA8,         SWIZ_XYZW, 16, true), /* RGBA8 sRGB */
1337ec681f3Smrg   FORMAT(A2B10G10R10_UNORM_PACK32,RGB10_A2,     RGB10_A2,      SWIZ_XYZW, 16, true),
1347ec681f3Smrg   FORMAT(A2B10G10R10_UINT_PACK32, RGB10_A2UI,   RGB10_A2UI,    SWIZ_XYZW, 16, false),
1357ec681f3Smrg   FORMAT(E5B9G9R9_UFLOAT_PACK32,  NO,           RGB9_E5,       SWIZ_XYZ1, 16, true),
1367ec681f3Smrg   FORMAT(B10G11R11_UFLOAT_PACK32, R11F_G11F_B10F,R11F_G11F_B10F, SWIZ_XYZ1, 16, true),
1377ec681f3Smrg
1387ec681f3Smrg   /* Depth */
1397ec681f3Smrg   FORMAT(D16_UNORM,               D16,          DEPTH_COMP16,  SWIZ_X001, 32, false),
1407ec681f3Smrg   FORMAT(D32_SFLOAT,              D32F,         DEPTH_COMP32F, SWIZ_X001, 32, false),
1417ec681f3Smrg   FORMAT(X8_D24_UNORM_PACK32,     D24S8,        DEPTH24_X8,    SWIZ_X001, 32, false),
1427ec681f3Smrg
1437ec681f3Smrg   /* Depth + Stencil */
1447ec681f3Smrg   FORMAT(D24_UNORM_S8_UINT,       D24S8,        DEPTH24_X8,    SWIZ_X001, 32, false),
1457ec681f3Smrg
1467ec681f3Smrg   /* Compressed: ETC2 / EAC */
1477ec681f3Smrg   FORMAT(ETC2_R8G8B8_UNORM_BLOCK,    NO,  RGB8_ETC2,                SWIZ_XYZ1, 16, true),
1487ec681f3Smrg   FORMAT(ETC2_R8G8B8_SRGB_BLOCK,     NO,  RGB8_ETC2,                SWIZ_XYZ1, 16, true),
1497ec681f3Smrg   FORMAT(ETC2_R8G8B8A1_UNORM_BLOCK,  NO,  RGB8_PUNCHTHROUGH_ALPHA1, SWIZ_XYZW, 16, true),
1507ec681f3Smrg   FORMAT(ETC2_R8G8B8A1_SRGB_BLOCK,   NO,  RGB8_PUNCHTHROUGH_ALPHA1, SWIZ_XYZW, 16, true),
1517ec681f3Smrg   FORMAT(ETC2_R8G8B8A8_UNORM_BLOCK,  NO,  RGBA8_ETC2_EAC,           SWIZ_XYZW, 16, true),
1527ec681f3Smrg   FORMAT(ETC2_R8G8B8A8_SRGB_BLOCK,   NO,  RGBA8_ETC2_EAC,           SWIZ_XYZW, 16, true),
1537ec681f3Smrg   FORMAT(EAC_R11_UNORM_BLOCK,        NO,  R11_EAC,                  SWIZ_X001, 16, true),
1547ec681f3Smrg   FORMAT(EAC_R11_SNORM_BLOCK,        NO,  SIGNED_R11_EAC,           SWIZ_X001, 16, true),
1557ec681f3Smrg   FORMAT(EAC_R11G11_UNORM_BLOCK,     NO,  RG11_EAC,                 SWIZ_XY01, 16, true),
1567ec681f3Smrg   FORMAT(EAC_R11G11_SNORM_BLOCK,     NO,  SIGNED_RG11_EAC,          SWIZ_XY01, 16, true),
1577ec681f3Smrg
1587ec681f3Smrg   /* Compressed: BC1-3 */
1597ec681f3Smrg   FORMAT(BC1_RGB_UNORM_BLOCK,        NO,  BC1,                      SWIZ_XYZ1, 16, true),
1607ec681f3Smrg   FORMAT(BC1_RGB_SRGB_BLOCK,         NO,  BC1,                      SWIZ_XYZ1, 16, true),
1617ec681f3Smrg   FORMAT(BC1_RGBA_UNORM_BLOCK,       NO,  BC1,                      SWIZ_XYZW, 16, true),
1627ec681f3Smrg   FORMAT(BC1_RGBA_SRGB_BLOCK,        NO,  BC1,                      SWIZ_XYZW, 16, true),
1637ec681f3Smrg   FORMAT(BC2_UNORM_BLOCK,            NO,  BC2,                      SWIZ_XYZW, 16, true),
1647ec681f3Smrg   FORMAT(BC2_SRGB_BLOCK,             NO,  BC2,                      SWIZ_XYZW, 16, true),
1657ec681f3Smrg   FORMAT(BC3_UNORM_BLOCK,            NO,  BC3,                      SWIZ_XYZW, 16, true),
1667ec681f3Smrg   FORMAT(BC3_SRGB_BLOCK,             NO,  BC3,                      SWIZ_XYZW, 16, true),
1677ec681f3Smrg
1687ec681f3Smrg   /* Compressed: ASTC */
1697ec681f3Smrg   FORMAT(ASTC_4x4_UNORM_BLOCK,       NO,  ASTC_4X4,                 SWIZ_XYZW, 16, true),
1707ec681f3Smrg   FORMAT(ASTC_4x4_SRGB_BLOCK,        NO,  ASTC_4X4,                 SWIZ_XYZW, 16, true),
1717ec681f3Smrg   FORMAT(ASTC_5x4_UNORM_BLOCK,       NO,  ASTC_5X4,                 SWIZ_XYZW, 16, true),
1727ec681f3Smrg   FORMAT(ASTC_5x4_SRGB_BLOCK,        NO,  ASTC_5X4,                 SWIZ_XYZW, 16, true),
1737ec681f3Smrg   FORMAT(ASTC_5x5_UNORM_BLOCK,       NO,  ASTC_5X5,                 SWIZ_XYZW, 16, true),
1747ec681f3Smrg   FORMAT(ASTC_5x5_SRGB_BLOCK,        NO,  ASTC_5X5,                 SWIZ_XYZW, 16, true),
1757ec681f3Smrg   FORMAT(ASTC_6x5_UNORM_BLOCK,       NO,  ASTC_6X5,                 SWIZ_XYZW, 16, true),
1767ec681f3Smrg   FORMAT(ASTC_6x5_SRGB_BLOCK,        NO,  ASTC_6X5,                 SWIZ_XYZW, 16, true),
1777ec681f3Smrg   FORMAT(ASTC_6x6_UNORM_BLOCK,       NO,  ASTC_6X6,                 SWIZ_XYZW, 16, true),
1787ec681f3Smrg   FORMAT(ASTC_6x6_SRGB_BLOCK,        NO,  ASTC_6X6,                 SWIZ_XYZW, 16, true),
1797ec681f3Smrg   FORMAT(ASTC_8x5_UNORM_BLOCK,       NO,  ASTC_8X5,                 SWIZ_XYZW, 16, true),
1807ec681f3Smrg   FORMAT(ASTC_8x5_SRGB_BLOCK,        NO,  ASTC_8X5,                 SWIZ_XYZW, 16, true),
1817ec681f3Smrg   FORMAT(ASTC_8x6_UNORM_BLOCK,       NO,  ASTC_8X6,                 SWIZ_XYZW, 16, true),
1827ec681f3Smrg   FORMAT(ASTC_8x6_SRGB_BLOCK,        NO,  ASTC_8X6,                 SWIZ_XYZW, 16, true),
1837ec681f3Smrg   FORMAT(ASTC_8x8_UNORM_BLOCK,       NO,  ASTC_8X8,                 SWIZ_XYZW, 16, true),
1847ec681f3Smrg   FORMAT(ASTC_8x8_SRGB_BLOCK,        NO,  ASTC_8X8,                 SWIZ_XYZW, 16, true),
1857ec681f3Smrg   FORMAT(ASTC_10x5_UNORM_BLOCK,      NO,  ASTC_10X5,                SWIZ_XYZW, 16, true),
1867ec681f3Smrg   FORMAT(ASTC_10x5_SRGB_BLOCK,       NO,  ASTC_10X5,                SWIZ_XYZW, 16, true),
1877ec681f3Smrg   FORMAT(ASTC_10x6_UNORM_BLOCK,      NO,  ASTC_10X6,                SWIZ_XYZW, 16, true),
1887ec681f3Smrg   FORMAT(ASTC_10x6_SRGB_BLOCK,       NO,  ASTC_10X6,                SWIZ_XYZW, 16, true),
1897ec681f3Smrg   FORMAT(ASTC_10x8_UNORM_BLOCK,      NO,  ASTC_10X8,                SWIZ_XYZW, 16, true),
1907ec681f3Smrg   FORMAT(ASTC_10x8_SRGB_BLOCK,       NO,  ASTC_10X8,                SWIZ_XYZW, 16, true),
1917ec681f3Smrg   FORMAT(ASTC_10x10_UNORM_BLOCK,     NO,  ASTC_10X10,               SWIZ_XYZW, 16, true),
1927ec681f3Smrg   FORMAT(ASTC_10x10_SRGB_BLOCK,      NO,  ASTC_10X10,               SWIZ_XYZW, 16, true),
1937ec681f3Smrg   FORMAT(ASTC_12x10_UNORM_BLOCK,     NO,  ASTC_12X10,               SWIZ_XYZW, 16, true),
1947ec681f3Smrg   FORMAT(ASTC_12x10_SRGB_BLOCK,      NO,  ASTC_12X10,               SWIZ_XYZW, 16, true),
1957ec681f3Smrg   FORMAT(ASTC_12x12_UNORM_BLOCK,     NO,  ASTC_12X12,               SWIZ_XYZW, 16, true),
1967ec681f3Smrg   FORMAT(ASTC_12x12_SRGB_BLOCK,      NO,  ASTC_12X12,               SWIZ_XYZW, 16, true),
1977ec681f3Smrg};
1987ec681f3Smrg
1997ec681f3Smrgconst struct v3dv_format *
2007ec681f3Smrgv3dX(get_format)(VkFormat format)
2017ec681f3Smrg{
2027ec681f3Smrg   if (format < ARRAY_SIZE(format_table) && format_table[format].supported)
2037ec681f3Smrg      return &format_table[format];
2047ec681f3Smrg   else
2057ec681f3Smrg      return NULL;
2067ec681f3Smrg}
2077ec681f3Smrg
2087ec681f3Smrgvoid
2097ec681f3Smrgv3dX(get_internal_type_bpp_for_output_format)(uint32_t format,
2107ec681f3Smrg                                              uint32_t *type,
2117ec681f3Smrg                                              uint32_t *bpp)
2127ec681f3Smrg{
2137ec681f3Smrg   switch (format) {
2147ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA8:
2157ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGB8:
2167ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG8:
2177ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R8:
2187ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_ABGR4444:
2197ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_BGR565:
2207ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_ABGR1555:
2217ec681f3Smrg      *type = V3D_INTERNAL_TYPE_8;
2227ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2237ec681f3Smrg      break;
2247ec681f3Smrg
2257ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA8I:
2267ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG8I:
2277ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R8I:
2287ec681f3Smrg      *type = V3D_INTERNAL_TYPE_8I;
2297ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2307ec681f3Smrg      break;
2317ec681f3Smrg
2327ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA8UI:
2337ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG8UI:
2347ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R8UI:
2357ec681f3Smrg      *type = V3D_INTERNAL_TYPE_8UI;
2367ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2377ec681f3Smrg      break;
2387ec681f3Smrg
2397ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_SRGB8_ALPHA8:
2407ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_SRGB:
2417ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGB10_A2:
2427ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R11F_G11F_B10F:
2437ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA16F:
2447ec681f3Smrg      /* Note that sRGB RTs are stored in the tile buffer at 16F,
2457ec681f3Smrg       * and the conversion to sRGB happens at tilebuffer load/store.
2467ec681f3Smrg       */
2477ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16F;
2487ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
2497ec681f3Smrg      break;
2507ec681f3Smrg
2517ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG16F:
2527ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R16F:
2537ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16F;
2547ec681f3Smrg      /* Use 64bpp to make sure the TLB doesn't throw away the alpha
2557ec681f3Smrg       * channel before alpha test happens.
2567ec681f3Smrg       */
2577ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
2587ec681f3Smrg      break;
2597ec681f3Smrg
2607ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA16I:
2617ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16I;
2627ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
2637ec681f3Smrg      break;
2647ec681f3Smrg
2657ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG16I:
2667ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R16I:
2677ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16I;
2687ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2697ec681f3Smrg      break;
2707ec681f3Smrg
2717ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGB10_A2UI:
2727ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA16UI:
2737ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16UI;
2747ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
2757ec681f3Smrg      break;
2767ec681f3Smrg
2777ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG16UI:
2787ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R16UI:
2797ec681f3Smrg      *type = V3D_INTERNAL_TYPE_16UI;
2807ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2817ec681f3Smrg      break;
2827ec681f3Smrg
2837ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA32I:
2847ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32I;
2857ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_128;
2867ec681f3Smrg      break;
2877ec681f3Smrg
2887ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG32I:
2897ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32I;
2907ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
2917ec681f3Smrg      break;
2927ec681f3Smrg
2937ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R32I:
2947ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32I;
2957ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
2967ec681f3Smrg      break;
2977ec681f3Smrg
2987ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA32UI:
2997ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32UI;
3007ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_128;
3017ec681f3Smrg      break;
3027ec681f3Smrg
3037ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG32UI:
3047ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32UI;
3057ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
3067ec681f3Smrg      break;
3077ec681f3Smrg
3087ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R32UI:
3097ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32UI;
3107ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
3117ec681f3Smrg      break;
3127ec681f3Smrg
3137ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RGBA32F:
3147ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32F;
3157ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_128;
3167ec681f3Smrg      break;
3177ec681f3Smrg
3187ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_RG32F:
3197ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32F;
3207ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_64;
3217ec681f3Smrg      break;
3227ec681f3Smrg
3237ec681f3Smrg   case V3D_OUTPUT_IMAGE_FORMAT_R32F:
3247ec681f3Smrg      *type = V3D_INTERNAL_TYPE_32F;
3257ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
3267ec681f3Smrg      break;
3277ec681f3Smrg
3287ec681f3Smrg   default:
3297ec681f3Smrg      /* Provide some default values, as we'll be called at RB
3307ec681f3Smrg       * creation time, even if an RB with this format isn't supported.
3317ec681f3Smrg       */
3327ec681f3Smrg      *type = V3D_INTERNAL_TYPE_8;
3337ec681f3Smrg      *bpp = V3D_INTERNAL_BPP_32;
3347ec681f3Smrg      break;
3357ec681f3Smrg   }
3367ec681f3Smrg}
3377ec681f3Smrg
3387ec681f3Smrgbool
3397ec681f3Smrgv3dX(format_supports_tlb_resolve)(const struct v3dv_format *format)
3407ec681f3Smrg{
3417ec681f3Smrg   uint32_t type, bpp;
3427ec681f3Smrg   v3dX(get_internal_type_bpp_for_output_format)(format->rt_type, &type, &bpp);
3437ec681f3Smrg   return type == V3D_INTERNAL_TYPE_8 || type == V3D_INTERNAL_TYPE_16F;
3447ec681f3Smrg}
3457ec681f3Smrg
3467ec681f3Smrgbool
3477ec681f3Smrgv3dX(format_supports_blending)(const struct v3dv_format *format)
3487ec681f3Smrg{
3497ec681f3Smrg   /* Hardware blending is only supported on render targets that are configured
3507ec681f3Smrg    * 4x8-bit unorm, 2x16-bit float or 4x16-bit float.
3517ec681f3Smrg    */
3527ec681f3Smrg   uint32_t type, bpp;
3537ec681f3Smrg   v3dX(get_internal_type_bpp_for_output_format)(format->rt_type, &type, &bpp);
3547ec681f3Smrg   switch (type) {
3557ec681f3Smrg   case V3D_INTERNAL_TYPE_8:
3567ec681f3Smrg      return bpp == V3D_INTERNAL_BPP_32;
3577ec681f3Smrg   case V3D_INTERNAL_TYPE_16F:
3587ec681f3Smrg      return bpp == V3D_INTERNAL_BPP_32 || V3D_INTERNAL_BPP_64;
3597ec681f3Smrg   default:
3607ec681f3Smrg      return false;
3617ec681f3Smrg   }
3627ec681f3Smrg}
3637ec681f3Smrg
3647ec681f3Smrgbool
3657ec681f3Smrgv3dX(tfu_supports_tex_format)(uint32_t tex_format)
3667ec681f3Smrg{
3677ec681f3Smrg   switch (tex_format) {
3687ec681f3Smrg   case TEXTURE_DATA_FORMAT_R8:
3697ec681f3Smrg   case TEXTURE_DATA_FORMAT_R8_SNORM:
3707ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG8:
3717ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG8_SNORM:
3727ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA8:
3737ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA8_SNORM:
3747ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB565:
3757ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA4:
3767ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB5_A1:
3777ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB10_A2:
3787ec681f3Smrg   case TEXTURE_DATA_FORMAT_R16:
3797ec681f3Smrg   case TEXTURE_DATA_FORMAT_R16_SNORM:
3807ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG16:
3817ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG16_SNORM:
3827ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA16:
3837ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA16_SNORM:
3847ec681f3Smrg   case TEXTURE_DATA_FORMAT_R16F:
3857ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG16F:
3867ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA16F:
3877ec681f3Smrg   case TEXTURE_DATA_FORMAT_R11F_G11F_B10F:
3887ec681f3Smrg   case TEXTURE_DATA_FORMAT_R4:
3897ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB9_E5:
3907ec681f3Smrg   case TEXTURE_DATA_FORMAT_R32F:
3917ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG32F:
3927ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA32F:
3937ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB8_ETC2:
3947ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGB8_PUNCHTHROUGH_ALPHA1:
3957ec681f3Smrg   case TEXTURE_DATA_FORMAT_RGBA8_ETC2_EAC:
3967ec681f3Smrg   case TEXTURE_DATA_FORMAT_R11_EAC:
3977ec681f3Smrg   case TEXTURE_DATA_FORMAT_SIGNED_R11_EAC:
3987ec681f3Smrg   case TEXTURE_DATA_FORMAT_RG11_EAC:
3997ec681f3Smrg   case TEXTURE_DATA_FORMAT_SIGNED_RG11_EAC:
4007ec681f3Smrg      return true;
4017ec681f3Smrg   default:
4027ec681f3Smrg      return false;
4037ec681f3Smrg   }
4047ec681f3Smrg}
4057ec681f3Smrg
4067ec681f3Smrguint8_t
4077ec681f3Smrgv3dX(get_internal_depth_type)(VkFormat format)
4087ec681f3Smrg{
4097ec681f3Smrg   switch (format) {
4107ec681f3Smrg   case VK_FORMAT_D16_UNORM:
4117ec681f3Smrg      return V3D_INTERNAL_TYPE_DEPTH_16;
4127ec681f3Smrg   case VK_FORMAT_D32_SFLOAT:
4137ec681f3Smrg      return V3D_INTERNAL_TYPE_DEPTH_32F;
4147ec681f3Smrg   case VK_FORMAT_X8_D24_UNORM_PACK32:
4157ec681f3Smrg   case VK_FORMAT_D24_UNORM_S8_UINT:
4167ec681f3Smrg      return V3D_INTERNAL_TYPE_DEPTH_24;
4177ec681f3Smrg   default:
4187ec681f3Smrg      unreachable("Invalid depth format");
4197ec681f3Smrg      break;
4207ec681f3Smrg   }
4217ec681f3Smrg}
4227ec681f3Smrg
4237ec681f3Smrgvoid
4247ec681f3Smrgv3dX(get_internal_type_bpp_for_image_aspects)(VkFormat vk_format,
4257ec681f3Smrg                                              VkImageAspectFlags aspect_mask,
4267ec681f3Smrg                                              uint32_t *internal_type,
4277ec681f3Smrg                                              uint32_t *internal_bpp)
4287ec681f3Smrg{
4297ec681f3Smrg   const VkImageAspectFlags ds_aspects = VK_IMAGE_ASPECT_DEPTH_BIT |
4307ec681f3Smrg                                         VK_IMAGE_ASPECT_STENCIL_BIT;
4317ec681f3Smrg
4327ec681f3Smrg   /* We can't store depth/stencil pixel formats to a raster format, so
4337ec681f3Smrg    * so instead we load our depth/stencil aspects to a compatible color
4347ec681f3Smrg    * format.
4357ec681f3Smrg    */
4367ec681f3Smrg   /* FIXME: pre-compute this at image creation time? */
4377ec681f3Smrg   if (aspect_mask & ds_aspects) {
4387ec681f3Smrg      switch (vk_format) {
4397ec681f3Smrg      case VK_FORMAT_D16_UNORM:
4407ec681f3Smrg         *internal_type = V3D_INTERNAL_TYPE_16UI;
4417ec681f3Smrg         *internal_bpp = V3D_INTERNAL_BPP_64;
4427ec681f3Smrg         break;
4437ec681f3Smrg      case VK_FORMAT_D32_SFLOAT:
4447ec681f3Smrg         *internal_type = V3D_INTERNAL_TYPE_32F;
4457ec681f3Smrg         *internal_bpp = V3D_INTERNAL_BPP_128;
4467ec681f3Smrg         break;
4477ec681f3Smrg      case VK_FORMAT_X8_D24_UNORM_PACK32:
4487ec681f3Smrg      case VK_FORMAT_D24_UNORM_S8_UINT:
4497ec681f3Smrg         /* Use RGBA8 format so we can relocate the X/S bits in the appropriate
4507ec681f3Smrg          * place to match Vulkan expectations. See the comment on the tile
4517ec681f3Smrg          * load command for more details.
4527ec681f3Smrg          */
4537ec681f3Smrg         *internal_type = V3D_INTERNAL_TYPE_8UI;
4547ec681f3Smrg         *internal_bpp = V3D_INTERNAL_BPP_32;
4557ec681f3Smrg         break;
4567ec681f3Smrg      default:
4577ec681f3Smrg         assert(!"unsupported format");
4587ec681f3Smrg         break;
4597ec681f3Smrg      }
4607ec681f3Smrg   } else {
4617ec681f3Smrg      const struct v3dv_format *format = v3dX(get_format)(vk_format);
4627ec681f3Smrg      v3dX(get_internal_type_bpp_for_output_format)(format->rt_type,
4637ec681f3Smrg                                                    internal_type, internal_bpp);
4647ec681f3Smrg   }
4657ec681f3Smrg}
466