1 /*
2 * Copyright © 2021 Raspberry Pi Ltd
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 /*
30 * Packs and ensure bo for the shader state (the latter can be temporal).
31 */
32 static void
pack_texture_shader_state_helper(struct v3dv_device * device,struct v3dv_image_view * image_view,bool for_cube_map_array_storage)33 pack_texture_shader_state_helper(struct v3dv_device *device,
34 struct v3dv_image_view *image_view,
35 bool for_cube_map_array_storage)
36 {
37 assert(!for_cube_map_array_storage ||
38 image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
39 const uint32_t index = for_cube_map_array_storage ? 1 : 0;
40
41 assert(image_view->vk.image);
42 const struct v3dv_image *image = (struct v3dv_image *) image_view->vk.image;
43
44 assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT ||
45 image->vk.samples == VK_SAMPLE_COUNT_4_BIT);
46 const uint32_t msaa_scale = image->vk.samples == VK_SAMPLE_COUNT_1_BIT ? 1 : 2;
47
48 for (uint8_t plane = 0; plane < image_view->plane_count; plane++) {
49 uint8_t iplane = image_view->planes[plane].image_plane;
50 v3dvx_pack(image_view->planes[plane].texture_shader_state[index], TEXTURE_SHADER_STATE, tex) {
51
52 tex.level_0_is_strictly_uif =
53 (image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_XOR ||
54 image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_NO_XOR);
55
56 tex.level_0_xor_enable = (image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_XOR);
57
58 if (tex.level_0_is_strictly_uif)
59 tex.level_0_ub_pad = image->planes[iplane].slices[0].ub_pad;
60
61 /* FIXME: v3d never sets uif_xor_disable, but uses it on the following
62 * check so let's set the default value
63 */
64 tex.uif_xor_disable = false;
65 if (tex.uif_xor_disable ||
66 tex.level_0_is_strictly_uif) {
67 tex.extended = true;
68 }
69
70 tex.base_level = image_view->vk.base_mip_level;
71 tex.max_level = image_view->vk.base_mip_level +
72 image_view->vk.level_count - 1;
73
74 tex.swizzle_r = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[0]);
75 tex.swizzle_g = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[1]);
76 tex.swizzle_b = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[2]);
77 tex.swizzle_a = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[3]);
78
79 tex.texture_type = image_view->format->planes[plane].tex_type;
80
81 if (image->vk.image_type == VK_IMAGE_TYPE_3D) {
82 tex.image_depth = image->vk.extent.depth;
83 } else {
84 tex.image_depth = image_view->vk.layer_count;
85 }
86
87 /* Empirical testing with CTS shows that when we are sampling from cube
88 * arrays we want to set image depth to layers / 6, but not when doing
89 * image load/store.
90 */
91 if (image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY &&
92 !for_cube_map_array_storage) {
93 assert(tex.image_depth % 6 == 0);
94 tex.image_depth /= 6;
95 }
96
97 tex.image_height = image->planes[iplane].height * msaa_scale;
98 tex.image_width = image->planes[iplane].width * msaa_scale;
99
100 /* On 4.x, the height of a 1D texture is redefined to be the
101 * upper 14 bits of the width (which is only usable with txf).
102 */
103 if (image->vk.image_type == VK_IMAGE_TYPE_1D)
104 tex.image_height = tex.image_width >> 14;
105
106 tex.image_width &= (1 << 14) - 1;
107 tex.image_height &= (1 << 14) - 1;
108
109 tex.array_stride_64_byte_aligned = image->planes[iplane].cube_map_stride / 64;
110
111 /* At this point we don't have the job. That's the reason the first
112 * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
113 * add the bo to the job. This also means that we need to add manually
114 * the image bo to the job using the texture.
115 */
116 const uint32_t base_offset =
117 image->planes[iplane].mem->bo->offset +
118 v3dv_layer_offset(image, 0, image_view->vk.base_array_layer,
119 iplane);
120 tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
121
122 bool is_srgb = vk_format_is_srgb(image_view->vk.format);
123
124 /* V3D 4.x doesn't have the reverse and swap_r/b bits, so we compose
125 * the reverse and/or swap_r/b swizzle from the format table with the
126 * image view swizzle. This, however, doesn't work for border colors,
127 * for that there is the reverse_standard_border_color.
128 *
129 * In v3d 7.x, however, there is no reverse_standard_border_color bit,
130 * since the reverse and swap_r/b bits also affect border colors. It is
131 * because of this that we absolutely need to use these bits with
132 * reversed and swpaped formats, since that's the only way to ensure
133 * correct border colors. In that case we don't want to program the
134 * swizzle to the composition of the format swizzle and the view
135 * swizzle like we do in v3d 4.x, since the format swizzle is applied
136 * via the reverse and swap_r/b bits.
137 */
138 #if V3D_VERSION == 42
139 tex.srgb = is_srgb;
140 tex.reverse_standard_border_color =
141 image_view->planes[plane].channel_reverse;
142 #endif
143 #if V3D_VERSION >= 71
144 tex.transfer_func = is_srgb ? TRANSFER_FUNC_SRGB : TRANSFER_FUNC_NONE;
145
146 tex.reverse = image_view->planes[plane].channel_reverse;
147 tex.r_b_swap = image_view->planes[plane].swap_rb;
148
149 if (tex.reverse || tex.r_b_swap) {
150 tex.swizzle_r =
151 v3d_translate_pipe_swizzle(image_view->view_swizzle[0]);
152 tex.swizzle_g =
153 v3d_translate_pipe_swizzle(image_view->view_swizzle[1]);
154 tex.swizzle_b =
155 v3d_translate_pipe_swizzle(image_view->view_swizzle[2]);
156 tex.swizzle_a =
157 v3d_translate_pipe_swizzle(image_view->view_swizzle[3]);
158 }
159
160 tex.chroma_offset_x = 1;
161 tex.chroma_offset_y = 1;
162 /* See comment in XML field definition for rationale of the shifts */
163 tex.texture_base_pointer_cb = base_offset >> 6;
164 tex.texture_base_pointer_cr = base_offset >> 6;
165 #endif
166 }
167 }
168 }
169
170 void
v3dX(pack_texture_shader_state)171 v3dX(pack_texture_shader_state)(struct v3dv_device *device,
172 struct v3dv_image_view *iview)
173 {
174 pack_texture_shader_state_helper(device, iview, false);
175 if (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
176 pack_texture_shader_state_helper(device, iview, true);
177 }
178
179 void
v3dX(pack_texture_shader_state_from_buffer_view)180 v3dX(pack_texture_shader_state_from_buffer_view)(struct v3dv_device *device,
181 struct v3dv_buffer_view *buffer_view)
182 {
183 assert(buffer_view->buffer);
184 const struct v3dv_buffer *buffer = buffer_view->buffer;
185
186 v3dvx_pack(buffer_view->texture_shader_state, TEXTURE_SHADER_STATE, tex) {
187 tex.swizzle_r =
188 v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[0]);
189 tex.swizzle_g =
190 v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[1]);
191 tex.swizzle_b =
192 v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[2]);
193 tex.swizzle_a =
194 v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[3]);
195
196 tex.image_depth = 1;
197
198 /* On 4.x, the height of a 1D texture is redefined to be the upper 14
199 * bits of the width (which is only usable with txf) (or in other words,
200 * we are providing a 28 bit field for size, but split on the usual
201 * 14bit height/width).
202 */
203 tex.image_width = buffer_view->num_elements;
204 tex.image_height = tex.image_width >> 14;
205 tex.image_width &= (1 << 14) - 1;
206 tex.image_height &= (1 << 14) - 1;
207
208 assert(buffer_view->format->plane_count == 1);
209 tex.texture_type = buffer_view->format->planes[0].tex_type;
210
211 bool is_srgb = vk_format_is_srgb(buffer_view->vk_format);
212 #if V3D_VERSION == 42
213 tex.srgb = is_srgb;
214 #endif
215 #if V3D_VERSION >= 71
216 tex.transfer_func = is_srgb ? TRANSFER_FUNC_SRGB : TRANSFER_FUNC_NONE;
217 #endif
218
219 /* At this point we don't have the job. That's the reason the first
220 * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
221 * add the bo to the job. This also means that we need to add manually
222 * the image bo to the job using the texture.
223 */
224 const uint32_t base_offset =
225 buffer->mem->bo->offset +
226 buffer->mem_offset +
227 buffer_view->offset;
228
229 tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
230
231 #if V3D_VERSION >= 71
232 tex.chroma_offset_x = 1;
233 tex.chroma_offset_y = 1;
234 /* See comment in XML field definition for rationale of the shifts */
235 tex.texture_base_pointer_cb = base_offset >> 6;
236 tex.texture_base_pointer_cr = base_offset >> 6;
237 #endif
238 }
239 }
240