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#include "broadcom/common/v3d_macros.h" 26#include "broadcom/cle/v3dx_pack.h" 27#include "broadcom/compiler/v3d_compiler.h" 28 29#include "vk_format_info.h" 30 31/* 32 * This method translates pipe_swizzle to the swizzle values used at the 33 * packet TEXTURE_SHADER_STATE 34 * 35 * FIXME: C&P from v3d, common place? 36 */ 37static uint32_t 38translate_swizzle(unsigned char pipe_swizzle) 39{ 40 switch (pipe_swizzle) { 41 case PIPE_SWIZZLE_0: 42 return 0; 43 case PIPE_SWIZZLE_1: 44 return 1; 45 case PIPE_SWIZZLE_X: 46 case PIPE_SWIZZLE_Y: 47 case PIPE_SWIZZLE_Z: 48 case PIPE_SWIZZLE_W: 49 return 2 + pipe_swizzle; 50 default: 51 unreachable("unknown swizzle"); 52 } 53} 54 55/* 56 * Packs and ensure bo for the shader state (the latter can be temporal). 57 */ 58static void 59pack_texture_shader_state_helper(struct v3dv_device *device, 60 struct v3dv_image_view *image_view, 61 bool for_cube_map_array_storage) 62{ 63 assert(!for_cube_map_array_storage || 64 image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY); 65 const uint32_t index = for_cube_map_array_storage ? 1 : 0; 66 67 assert(image_view->vk.image); 68 const struct v3dv_image *image = (struct v3dv_image *) image_view->vk.image; 69 70 assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT || 71 image->vk.samples == VK_SAMPLE_COUNT_4_BIT); 72 const uint32_t msaa_scale = image->vk.samples == VK_SAMPLE_COUNT_1_BIT ? 1 : 2; 73 74 v3dvx_pack(image_view->texture_shader_state[index], TEXTURE_SHADER_STATE, tex) { 75 76 tex.level_0_is_strictly_uif = 77 (image->slices[0].tiling == V3D_TILING_UIF_XOR || 78 image->slices[0].tiling == V3D_TILING_UIF_NO_XOR); 79 80 tex.level_0_xor_enable = (image->slices[0].tiling == V3D_TILING_UIF_XOR); 81 82 if (tex.level_0_is_strictly_uif) 83 tex.level_0_ub_pad = image->slices[0].ub_pad; 84 85 /* FIXME: v3d never sets uif_xor_disable, but uses it on the following 86 * check so let's set the default value 87 */ 88 tex.uif_xor_disable = false; 89 if (tex.uif_xor_disable || 90 tex.level_0_is_strictly_uif) { 91 tex.extended = true; 92 } 93 94 tex.base_level = image_view->vk.base_mip_level; 95 tex.max_level = image_view->vk.base_mip_level + 96 image_view->vk.level_count - 1; 97 98 tex.swizzle_r = translate_swizzle(image_view->swizzle[0]); 99 tex.swizzle_g = translate_swizzle(image_view->swizzle[1]); 100 tex.swizzle_b = translate_swizzle(image_view->swizzle[2]); 101 tex.swizzle_a = translate_swizzle(image_view->swizzle[3]); 102 103 tex.texture_type = image_view->format->tex_type; 104 105 if (image->vk.image_type == VK_IMAGE_TYPE_3D) { 106 tex.image_depth = image->vk.extent.depth; 107 } else { 108 tex.image_depth = image_view->vk.layer_count; 109 } 110 111 /* Empirical testing with CTS shows that when we are sampling from cube 112 * arrays we want to set image depth to layers / 6, but not when doing 113 * image load/store. 114 */ 115 if (image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY && 116 !for_cube_map_array_storage) { 117 assert(tex.image_depth % 6 == 0); 118 tex.image_depth /= 6; 119 } 120 121 tex.image_height = image->vk.extent.height * msaa_scale; 122 tex.image_width = image->vk.extent.width * msaa_scale; 123 124 /* On 4.x, the height of a 1D texture is redefined to be the 125 * upper 14 bits of the width (which is only usable with txf). 126 */ 127 if (image->vk.image_type == VK_IMAGE_TYPE_1D) { 128 tex.image_height = tex.image_width >> 14; 129 } 130 tex.image_width &= (1 << 14) - 1; 131 tex.image_height &= (1 << 14) - 1; 132 133 tex.array_stride_64_byte_aligned = image->cube_map_stride / 64; 134 135 tex.srgb = vk_format_is_srgb(image_view->vk.format); 136 137 /* At this point we don't have the job. That's the reason the first 138 * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to 139 * add the bo to the job. This also means that we need to add manually 140 * the image bo to the job using the texture. 141 */ 142 const uint32_t base_offset = 143 image->mem->bo->offset + 144 v3dv_layer_offset(image, 0, image_view->vk.base_array_layer); 145 tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset); 146 } 147} 148 149void 150v3dX(pack_texture_shader_state)(struct v3dv_device *device, 151 struct v3dv_image_view *iview) 152{ 153 pack_texture_shader_state_helper(device, iview, false); 154 if (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) 155 pack_texture_shader_state_helper(device, iview, true); 156} 157 158void 159v3dX(pack_texture_shader_state_from_buffer_view)(struct v3dv_device *device, 160 struct v3dv_buffer_view *buffer_view) 161{ 162 assert(buffer_view->buffer); 163 const struct v3dv_buffer *buffer = buffer_view->buffer; 164 165 v3dvx_pack(buffer_view->texture_shader_state, TEXTURE_SHADER_STATE, tex) { 166 tex.swizzle_r = translate_swizzle(PIPE_SWIZZLE_X); 167 tex.swizzle_g = translate_swizzle(PIPE_SWIZZLE_Y); 168 tex.swizzle_b = translate_swizzle(PIPE_SWIZZLE_Z); 169 tex.swizzle_a = translate_swizzle(PIPE_SWIZZLE_W); 170 171 tex.image_depth = 1; 172 173 /* On 4.x, the height of a 1D texture is redefined to be the upper 14 174 * bits of the width (which is only usable with txf) (or in other words, 175 * we are providing a 28 bit field for size, but split on the usual 176 * 14bit height/width). 177 */ 178 tex.image_width = buffer_view->num_elements; 179 tex.image_height = tex.image_width >> 14; 180 tex.image_width &= (1 << 14) - 1; 181 tex.image_height &= (1 << 14) - 1; 182 183 tex.texture_type = buffer_view->format->tex_type; 184 tex.srgb = vk_format_is_srgb(buffer_view->vk_format); 185 186 /* At this point we don't have the job. That's the reason the first 187 * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to 188 * add the bo to the job. This also means that we need to add manually 189 * the image bo to the job using the texture. 190 */ 191 const uint32_t base_offset = 192 buffer->mem->bo->offset + 193 buffer->mem_offset + 194 buffer_view->offset; 195 196 tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset); 197 } 198} 199