Home | History | Annotate | Line # | Download | only in vulkan
      1 /*
      2  * Copyright  2021 Raspberry Pi
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  */
     23 
     24 #include "v3dv_private.h"
     25 
     26 #include "broadcom/common/v3d_macros.h"
     27 #include "broadcom/cle/v3dx_pack.h"
     28 #include "broadcom/compiler/v3d_compiler.h"
     29 #include "vk_format_info.h"
     30 #include "util/u_pack_color.h"
     31 #include "util/half_float.h"
     32 
     33 static const enum V3DX(Wrap_Mode) vk_to_v3d_wrap_mode[] = {
     34    [VK_SAMPLER_ADDRESS_MODE_REPEAT]          = V3D_WRAP_MODE_REPEAT,
     35    [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = V3D_WRAP_MODE_MIRROR,
     36    [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE]   = V3D_WRAP_MODE_CLAMP,
     37    [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = V3D_WRAP_MODE_MIRROR_ONCE,
     38    [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = V3D_WRAP_MODE_BORDER,
     39 };
     40 
     41 static const enum V3DX(Compare_Function)
     42 vk_to_v3d_compare_func[] = {
     43    [VK_COMPARE_OP_NEVER]                        = V3D_COMPARE_FUNC_NEVER,
     44    [VK_COMPARE_OP_LESS]                         = V3D_COMPARE_FUNC_LESS,
     45    [VK_COMPARE_OP_EQUAL]                        = V3D_COMPARE_FUNC_EQUAL,
     46    [VK_COMPARE_OP_LESS_OR_EQUAL]                = V3D_COMPARE_FUNC_LEQUAL,
     47    [VK_COMPARE_OP_GREATER]                      = V3D_COMPARE_FUNC_GREATER,
     48    [VK_COMPARE_OP_NOT_EQUAL]                    = V3D_COMPARE_FUNC_NOTEQUAL,
     49    [VK_COMPARE_OP_GREATER_OR_EQUAL]             = V3D_COMPARE_FUNC_GEQUAL,
     50    [VK_COMPARE_OP_ALWAYS]                       = V3D_COMPARE_FUNC_ALWAYS,
     51 };
     52 
     53 
     54 static union pipe_color_union encode_border_color(
     55    const VkSamplerCustomBorderColorCreateInfoEXT *bc_info)
     56 {
     57    const struct util_format_description *desc =
     58       vk_format_description(bc_info->format);
     59 
     60    const struct v3dv_format *format = v3dX(get_format)(bc_info->format);
     61 
     62    union pipe_color_union border;
     63    for (int i = 0; i < 4; i++) {
     64       if (format->swizzle[i] <= 3)
     65          border.ui[i] = bc_info->customBorderColor.uint32[format->swizzle[i]];
     66       else
     67          border.ui[i] = 0;
     68    }
     69 
     70    /* handle clamping */
     71    if (vk_format_has_depth(bc_info->format) &&
     72        vk_format_has_stencil(bc_info->format)) {
     73       border.f[0] = CLAMP(border.f[0], 0, 1);
     74       border.ui[1] = CLAMP(border.ui[1], 0, 0xff);
     75    } else if (vk_format_is_unorm(bc_info->format)) {
     76       for (int i = 0; i < 4; i++)
     77          border.f[i] = CLAMP(border.f[i], 0, 1);
     78    } else if (vk_format_is_snorm(bc_info->format)) {
     79       for (int i = 0; i < 4; i++)
     80          border.f[i] = CLAMP(border.f[i], -1, 1);
     81    } else if (vk_format_is_uint(bc_info->format) &&
     82               desc->channel[0].size < 32) {
     83       for (int i = 0; i < 4; i++)
     84          border.ui[i] = CLAMP(border.ui[i], 0, (1 << desc->channel[i].size));
     85    } else if (vk_format_is_sint(bc_info->format) &&
     86               desc->channel[0].size < 32) {
     87       for (int i = 0; i < 4; i++)
     88          border.i[i] = CLAMP(border.i[i],
     89                              -(1 << (desc->channel[i].size - 1)),
     90                              (1 << (desc->channel[i].size - 1)) - 1);
     91    }
     92 
     93    /* convert from float to expected format */
     94    if (vk_format_is_srgb(bc_info->format) ||
     95        vk_format_is_compressed(bc_info->format)) {
     96       for (int i = 0; i < 4; i++)
     97          border.ui[i] = _mesa_float_to_half(border.f[i]);
     98    } else if (vk_format_is_unorm(bc_info->format)) {
     99       for (int i = 0; i < 4; i++) {
    100          switch (desc->channel[i].size) {
    101          case 8:
    102          case 16:
    103             /* expect u16 for non depth values */
    104             if (!vk_format_has_depth(bc_info->format))
    105                border.ui[i] = (uint32_t) (border.f[i] * (float) 0xffff);
    106             break;
    107          case 24:
    108          case 32:
    109             /* uses full f32; no conversion needed */
    110             break;
    111          default:
    112             border.ui[i] = _mesa_float_to_half(border.f[i]);
    113             break;
    114          }
    115       }
    116    } else if (vk_format_is_snorm(bc_info->format)) {
    117       for (int i = 0; i < 4; i++) {
    118          switch (desc->channel[i].size) {
    119          case 8:
    120             border.ui[i] = (int32_t) (border.f[i] * (float) 0x3fff);
    121             break;
    122          case 16:
    123             border.i[i] = (int32_t) (border.f[i] * (float) 0x7fff);
    124             break;
    125          case 24:
    126          case 32:
    127             /* uses full f32; no conversion needed */
    128             break;
    129          default:
    130             border.ui[i] = _mesa_float_to_half(border.f[i]);
    131             break;
    132          }
    133       }
    134    } else if (vk_format_is_float(bc_info->format)) {
    135       for (int i = 0; i < 4; i++) {
    136          switch(desc->channel[i].size) {
    137          case 16:
    138             border.ui[i] = _mesa_float_to_half(border.f[i]);
    139             break;
    140          default:
    141             break;
    142          }
    143       }
    144    }
    145 
    146    return border;
    147 }
    148 
    149 void
    150 v3dX(pack_sampler_state)(struct v3dv_sampler *sampler,
    151                          const VkSamplerCreateInfo *pCreateInfo,
    152                          const VkSamplerCustomBorderColorCreateInfoEXT *bc_info)
    153 {
    154    enum V3DX(Border_Color_Mode) border_color_mode;
    155 
    156    switch (pCreateInfo->borderColor) {
    157    case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
    158    case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
    159       border_color_mode = V3D_BORDER_COLOR_0000;
    160       break;
    161    case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
    162    case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
    163       border_color_mode = V3D_BORDER_COLOR_0001;
    164       break;
    165    case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
    166    case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
    167       border_color_mode = V3D_BORDER_COLOR_1111;
    168       break;
    169    case VK_BORDER_COLOR_FLOAT_CUSTOM_EXT:
    170    case VK_BORDER_COLOR_INT_CUSTOM_EXT:
    171       border_color_mode = V3D_BORDER_COLOR_FOLLOWS;
    172       break;
    173    default:
    174       unreachable("Unknown border color");
    175       break;
    176    }
    177 
    178    /* For some texture formats, when clamping to transparent black border the
    179     * CTS expects alpha to be set to 1 instead of 0, but the border color mode
    180     * will take priority over the texture state swizzle, so the only way to
    181     * fix that is to apply a swizzle in the shader. Here we keep track of
    182     * whether we are activating that mode and we will decide if we need to
    183     * activate the texture swizzle lowering in the shader key at compile time
    184     * depending on the actual texture format.
    185     */
    186    if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
    187         pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
    188         pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
    189        border_color_mode == V3D_BORDER_COLOR_0000) {
    190       sampler->clamp_to_transparent_black_border = true;
    191    }
    192 
    193    v3dvx_pack(sampler->sampler_state, SAMPLER_STATE, s) {
    194       if (pCreateInfo->anisotropyEnable) {
    195          s.anisotropy_enable = true;
    196          if (pCreateInfo->maxAnisotropy > 8)
    197             s.maximum_anisotropy = 3;
    198          else if (pCreateInfo->maxAnisotropy > 4)
    199             s.maximum_anisotropy = 2;
    200          else if (pCreateInfo->maxAnisotropy > 2)
    201             s.maximum_anisotropy = 1;
    202       }
    203 
    204       s.border_color_mode = border_color_mode;
    205 
    206       if (s.border_color_mode == V3D_BORDER_COLOR_FOLLOWS) {
    207          union pipe_color_union border = encode_border_color(bc_info);
    208 
    209          s.border_color_word_0 = border.ui[0];
    210          s.border_color_word_1 = border.ui[1];
    211          s.border_color_word_2 = border.ui[2];
    212          s.border_color_word_3 = border.ui[3];
    213       }
    214 
    215       s.wrap_i_border = false; /* Also hardcoded on v3d */
    216       s.wrap_s = vk_to_v3d_wrap_mode[pCreateInfo->addressModeU];
    217       s.wrap_t = vk_to_v3d_wrap_mode[pCreateInfo->addressModeV];
    218       s.wrap_r = vk_to_v3d_wrap_mode[pCreateInfo->addressModeW];
    219       s.fixed_bias = pCreateInfo->mipLodBias;
    220       s.max_level_of_detail = MIN2(MAX2(0, pCreateInfo->maxLod), 15);
    221       s.min_level_of_detail = MIN2(MAX2(0, pCreateInfo->minLod), 15);
    222       s.srgb_disable = 0; /* Not even set by v3d */
    223       s.depth_compare_function =
    224          vk_to_v3d_compare_func[pCreateInfo->compareEnable ?
    225                                 pCreateInfo->compareOp : VK_COMPARE_OP_NEVER];
    226       s.mip_filter_nearest = pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST;
    227       s.min_filter_nearest = pCreateInfo->minFilter == VK_FILTER_NEAREST;
    228       s.mag_filter_nearest = pCreateInfo->magFilter == VK_FILTER_NEAREST;
    229    }
    230 }
    231 
    232 /**
    233  * This computes the maximum bpp used by any of the render targets used by
    234  * a particular subpass and checks if any of those render targets are
    235  * multisampled. If we don't have a subpass (when we are not inside a
    236  * render pass), then we assume that all framebuffer attachments are used.
    237  */
    238 void
    239 v3dX(framebuffer_compute_internal_bpp_msaa)(
    240    const struct v3dv_framebuffer *framebuffer,
    241    const struct v3dv_subpass *subpass,
    242    uint8_t *max_bpp,
    243    bool *msaa)
    244 {
    245    STATIC_ASSERT(V3D_INTERNAL_BPP_32 == 0);
    246    *max_bpp = V3D_INTERNAL_BPP_32;
    247    *msaa = false;
    248 
    249    if (subpass) {
    250       for (uint32_t i = 0; i < subpass->color_count; i++) {
    251          uint32_t att_idx = subpass->color_attachments[i].attachment;
    252          if (att_idx == VK_ATTACHMENT_UNUSED)
    253             continue;
    254 
    255          const struct v3dv_image_view *att = framebuffer->attachments[att_idx];
    256          assert(att);
    257 
    258          if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT)
    259             *max_bpp = MAX2(*max_bpp, att->internal_bpp);
    260 
    261          if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
    262             *msaa = true;
    263       }
    264 
    265       if (!*msaa && subpass->ds_attachment.attachment != VK_ATTACHMENT_UNUSED) {
    266          const struct v3dv_image_view *att =
    267             framebuffer->attachments[subpass->ds_attachment.attachment];
    268          assert(att);
    269 
    270          if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
    271             *msaa = true;
    272       }
    273 
    274       return;
    275    }
    276 
    277    assert(framebuffer->attachment_count <= 4);
    278    for (uint32_t i = 0; i < framebuffer->attachment_count; i++) {
    279       const struct v3dv_image_view *att = framebuffer->attachments[i];
    280       assert(att);
    281 
    282       if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT)
    283          *max_bpp = MAX2(*max_bpp, att->internal_bpp);
    284 
    285       if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
    286          *msaa = true;
    287    }
    288 
    289    return;
    290 }
    291 
    292 uint32_t
    293 v3dX(zs_buffer_from_aspect_bits)(VkImageAspectFlags aspects)
    294 {
    295    const VkImageAspectFlags zs_aspects =
    296       VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
    297    const VkImageAspectFlags filtered_aspects = aspects & zs_aspects;
    298 
    299    if (filtered_aspects == zs_aspects)
    300       return ZSTENCIL;
    301    else if (filtered_aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
    302       return Z;
    303    else if (filtered_aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
    304       return STENCIL;
    305    else
    306       return NONE;
    307 }
    308 
    309 void
    310 v3dX(get_hw_clear_color)(const VkClearColorValue *color,
    311                          uint32_t internal_type,
    312                          uint32_t internal_size,
    313                          uint32_t *hw_color)
    314 {
    315    union util_color uc;
    316    switch (internal_type) {
    317    case V3D_INTERNAL_TYPE_8:
    318       util_pack_color(color->float32, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
    319       memcpy(hw_color, uc.ui, internal_size);
    320    break;
    321    case V3D_INTERNAL_TYPE_8I:
    322    case V3D_INTERNAL_TYPE_8UI:
    323       hw_color[0] = ((color->uint32[0] & 0xff) |
    324                      (color->uint32[1] & 0xff) << 8 |
    325                      (color->uint32[2] & 0xff) << 16 |
    326                      (color->uint32[3] & 0xff) << 24);
    327    break;
    328    case V3D_INTERNAL_TYPE_16F:
    329       util_pack_color(color->float32, PIPE_FORMAT_R16G16B16A16_FLOAT, &uc);
    330       memcpy(hw_color, uc.ui, internal_size);
    331    break;
    332    case V3D_INTERNAL_TYPE_16I:
    333    case V3D_INTERNAL_TYPE_16UI:
    334       hw_color[0] = ((color->uint32[0] & 0xffff) | color->uint32[1] << 16);
    335       hw_color[1] = ((color->uint32[2] & 0xffff) | color->uint32[3] << 16);
    336    break;
    337    case V3D_INTERNAL_TYPE_32F:
    338    case V3D_INTERNAL_TYPE_32I:
    339    case V3D_INTERNAL_TYPE_32UI:
    340       memcpy(hw_color, color->uint32, internal_size);
    341       break;
    342    }
    343 }
    344 
    345 #ifdef DEBUG
    346 void
    347 v3dX(device_check_prepacked_sizes)(void)
    348 {
    349    STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
    350                  cl_packet_length(SAMPLER_STATE));
    351    STATIC_ASSERT(V3DV_TEXTURE_SHADER_STATE_LENGTH >=
    352                  cl_packet_length(TEXTURE_SHADER_STATE));
    353    STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
    354                  cl_packet_length(SAMPLER_STATE));
    355    STATIC_ASSERT(V3DV_BLEND_CFG_LENGTH>=
    356                  cl_packet_length(BLEND_CFG));
    357    STATIC_ASSERT(V3DV_CFG_BITS_LENGTH>=
    358                  cl_packet_length(CFG_BITS));
    359    STATIC_ASSERT(V3DV_GL_SHADER_STATE_RECORD_LENGTH >=
    360                  cl_packet_length(GL_SHADER_STATE_RECORD));
    361    STATIC_ASSERT(V3DV_VCM_CACHE_SIZE_LENGTH>=
    362                  cl_packet_length(VCM_CACHE_SIZE));
    363    STATIC_ASSERT(V3DV_GL_SHADER_STATE_ATTRIBUTE_RECORD_LENGTH >=
    364                  cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD));
    365    STATIC_ASSERT(V3DV_STENCIL_CFG_LENGTH >=
    366                  cl_packet_length(STENCIL_CFG));
    367 }
    368 #endif
    369