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
26 #include "broadcom/common/v3d_macros.h"
27 #include "broadcom/cle/v3dx_pack.h"
28 #include "broadcom/compiler/v3d_compiler.h"
29 #include "util/u_pack_color.h"
30 #include "util/half_float.h"
31
32 static const enum V3DX(Wrap_Mode) vk_to_v3d_wrap_mode[] = {
33 [VK_SAMPLER_ADDRESS_MODE_REPEAT] = V3D_WRAP_MODE_REPEAT,
34 [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = V3D_WRAP_MODE_MIRROR,
35 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = V3D_WRAP_MODE_CLAMP,
36 [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = V3D_WRAP_MODE_MIRROR_ONCE,
37 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = V3D_WRAP_MODE_BORDER,
38 };
39
40 static const enum V3DX(Compare_Function)
41 vk_to_v3d_compare_func[] = {
42 [VK_COMPARE_OP_NEVER] = V3D_COMPARE_FUNC_NEVER,
43 [VK_COMPARE_OP_LESS] = V3D_COMPARE_FUNC_LESS,
44 [VK_COMPARE_OP_EQUAL] = V3D_COMPARE_FUNC_EQUAL,
45 [VK_COMPARE_OP_LESS_OR_EQUAL] = V3D_COMPARE_FUNC_LEQUAL,
46 [VK_COMPARE_OP_GREATER] = V3D_COMPARE_FUNC_GREATER,
47 [VK_COMPARE_OP_NOT_EQUAL] = V3D_COMPARE_FUNC_NOTEQUAL,
48 [VK_COMPARE_OP_GREATER_OR_EQUAL] = V3D_COMPARE_FUNC_GEQUAL,
49 [VK_COMPARE_OP_ALWAYS] = V3D_COMPARE_FUNC_ALWAYS,
50 };
51
encode_border_color(const struct v3dv_device * device,const VkSamplerCustomBorderColorCreateInfoEXT * bc_info)52 static union pipe_color_union encode_border_color(
53 const struct v3dv_device *device,
54 const VkSamplerCustomBorderColorCreateInfoEXT *bc_info)
55 {
56 const struct util_format_description *desc =
57 vk_format_description(bc_info->format);
58
59 const struct v3dv_format *format = v3dX(get_format)(bc_info->format);
60
61 /* YCbCr doesn't interact with border color at all. From spec:
62 *
63 * "If sampler YCBCR conversion is enabled, addressModeU, addressModeV,
64 * and addressModeW must be VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
65 * anisotropyEnable must be VK_FALSE, and unnormalizedCoordinates must
66 * be VK_FALSE"
67 */
68 assert(format->plane_count == 1);
69
70 /* We use the swizzle in our format table to determine swizzle configuration
71 * for sampling as well as to decide if we need to use the Swap R/B and
72 * Reverse Channels bits for Tile Load/Store operations. The order of the
73 * R/B swap and Reverse operations matters and gives different swizzles.
74 * Our format table assumes that Reverse happens first and R/B Swap second.
75 * This seems to match semantics for texture sampling and Tile load/store,
76 * however, it seems that the semantics are reversed for custom border
77 * colors so we need to fix up the swizzle manually for this case.
78 */
79 uint8_t swizzle[4];
80 const bool v3d_has_reverse_swap_rb_bits =
81 v3dv_texture_shader_state_has_rb_swap_reverse_bits(device);
82 if (!v3d_has_reverse_swap_rb_bits &&
83 v3dv_format_swizzle_needs_reverse(format->planes[0].swizzle) &&
84 v3dv_format_swizzle_needs_rb_swap(format->planes[0].swizzle)) {
85 swizzle[0] = PIPE_SWIZZLE_W;
86 swizzle[1] = PIPE_SWIZZLE_X;
87 swizzle[2] = PIPE_SWIZZLE_Y;
88 swizzle[3] = PIPE_SWIZZLE_Z;
89 }
90 /* In v3d 7.x we no longer have a reverse flag for the border color. Instead
91 * we have to use the new reverse and swap_r/b flags in the texture shader
92 * state which will apply the format swizzle automatically when sampling
93 * the border color too and we should not apply it manually here.
94 */
95 else if (v3d_has_reverse_swap_rb_bits &&
96 (v3dv_format_swizzle_needs_rb_swap(format->planes[0].swizzle) ||
97 v3dv_format_swizzle_needs_reverse(format->planes[0].swizzle))) {
98 swizzle[0] = PIPE_SWIZZLE_X;
99 swizzle[1] = PIPE_SWIZZLE_Y;
100 swizzle[2] = PIPE_SWIZZLE_Z;
101 swizzle[3] = PIPE_SWIZZLE_W;
102 } else {
103 memcpy(swizzle, format->planes[0].swizzle, sizeof (swizzle));
104 }
105
106 union pipe_color_union border;
107 for (int i = 0; i < 4; i++) {
108 if (format->planes[0].swizzle[i] <= 3)
109 border.ui[i] = bc_info->customBorderColor.uint32[swizzle[i]];
110 else
111 border.ui[i] = 0;
112 }
113
114 /* handle clamping */
115 if (vk_format_has_depth(bc_info->format) &&
116 vk_format_has_stencil(bc_info->format)) {
117 border.f[0] = CLAMP(border.f[0], 0, 1);
118 border.ui[1] = CLAMP(border.ui[1], 0, 0xff);
119 } else if (vk_format_is_unorm(bc_info->format)) {
120 for (int i = 0; i < desc->nr_channels; i++)
121 border.f[i] = CLAMP(border.f[i], 0, 1);
122 } else if (vk_format_is_snorm(bc_info->format)) {
123 for (int i = 0; i < desc->nr_channels; i++)
124 border.f[i] = CLAMP(border.f[i], -1, 1);
125 } else if (vk_format_is_uint(bc_info->format) &&
126 desc->channel[0].size < 32) {
127 for (int i = 0; i < desc->nr_channels; i++)
128 border.ui[i] = CLAMP(border.ui[i], 0, (1 << desc->channel[i].size));
129 } else if (vk_format_is_sint(bc_info->format) &&
130 desc->channel[0].size < 32) {
131 for (int i = 0; i < desc->nr_channels; i++)
132 border.i[i] = CLAMP(border.i[i],
133 -(1 << (desc->channel[i].size - 1)),
134 (1 << (desc->channel[i].size - 1)) - 1);
135 }
136
137 #if V3D_VERSION <= 42
138 /* The TMU in V3D 7.x always takes 32-bit floats and handles conversions
139 * for us. In V3D 4.x we need to manually convert floating point color
140 * values to the expected format.
141 */
142 if (vk_format_is_srgb(bc_info->format) ||
143 vk_format_is_compressed(bc_info->format)) {
144 for (int i = 0; i < 4; i++)
145 border.ui[i] = _mesa_float_to_half(border.f[i]);
146 } else if (vk_format_is_unorm(bc_info->format)) {
147 for (int i = 0; i < 4; i++) {
148 switch (desc->channel[i].size) {
149 case 8:
150 case 16:
151 /* expect u16 for non depth values */
152 if (!vk_format_has_depth(bc_info->format))
153 border.ui[i] = (uint32_t) (border.f[i] * (float) 0xffff);
154 break;
155 case 24:
156 case 32:
157 /* uses full f32; no conversion needed */
158 break;
159 default:
160 border.ui[i] = _mesa_float_to_half(border.f[i]);
161 break;
162 }
163 }
164 } else if (vk_format_is_snorm(bc_info->format)) {
165 for (int i = 0; i < 4; i++) {
166 switch (desc->channel[i].size) {
167 case 8:
168 border.ui[i] = (int32_t) (border.f[i] * (float) 0x3fff);
169 break;
170 case 16:
171 border.i[i] = (int32_t) (border.f[i] * (float) 0x7fff);
172 break;
173 case 24:
174 case 32:
175 /* uses full f32; no conversion needed */
176 break;
177 default:
178 border.ui[i] = _mesa_float_to_half(border.f[i]);
179 break;
180 }
181 }
182 } else if (vk_format_is_float(bc_info->format)) {
183 for (int i = 0; i < 4; i++) {
184 switch(desc->channel[i].size) {
185 case 16:
186 border.ui[i] = _mesa_float_to_half(border.f[i]);
187 break;
188 default:
189 break;
190 }
191 }
192 }
193 #endif
194
195 return border;
196 }
197
198 void
v3dX(pack_sampler_state)199 v3dX(pack_sampler_state)(const struct v3dv_device *device,
200 struct v3dv_sampler *sampler,
201 const VkSamplerCreateInfo *pCreateInfo,
202 const VkSamplerCustomBorderColorCreateInfoEXT *bc_info)
203 {
204 enum V3DX(Border_Color_Mode) border_color_mode;
205
206 switch (pCreateInfo->borderColor) {
207 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
208 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
209 border_color_mode = V3D_BORDER_COLOR_0000;
210 break;
211 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
212 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
213 border_color_mode = V3D_BORDER_COLOR_0001;
214 break;
215 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
216 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
217 border_color_mode = V3D_BORDER_COLOR_1111;
218 break;
219 case VK_BORDER_COLOR_FLOAT_CUSTOM_EXT:
220 case VK_BORDER_COLOR_INT_CUSTOM_EXT:
221 border_color_mode = V3D_BORDER_COLOR_FOLLOWS;
222 break;
223 default:
224 unreachable("Unknown border color");
225 break;
226 }
227
228 v3dvx_pack(sampler->sampler_state, SAMPLER_STATE, s) {
229 if (pCreateInfo->anisotropyEnable) {
230 s.anisotropy_enable = true;
231 if (pCreateInfo->maxAnisotropy > 8)
232 s.maximum_anisotropy = 3;
233 else if (pCreateInfo->maxAnisotropy > 4)
234 s.maximum_anisotropy = 2;
235 else if (pCreateInfo->maxAnisotropy > 2)
236 s.maximum_anisotropy = 1;
237 }
238
239 s.border_color_mode = border_color_mode;
240
241 if (s.border_color_mode == V3D_BORDER_COLOR_FOLLOWS) {
242 union pipe_color_union border = encode_border_color(device, bc_info);
243
244 s.border_color_word_0 = border.ui[0];
245 s.border_color_word_1 = border.ui[1];
246 s.border_color_word_2 = border.ui[2];
247 s.border_color_word_3 = border.ui[3];
248 }
249
250 s.wrap_i_border = false; /* Also hardcoded on v3d */
251 s.wrap_s = vk_to_v3d_wrap_mode[pCreateInfo->addressModeU];
252 s.wrap_t = vk_to_v3d_wrap_mode[pCreateInfo->addressModeV];
253 s.wrap_r = vk_to_v3d_wrap_mode[pCreateInfo->addressModeW];
254 s.fixed_bias = pCreateInfo->mipLodBias;
255 s.max_level_of_detail = MIN2(MAX2(0, pCreateInfo->maxLod), 15);
256 s.min_level_of_detail = MIN2(MAX2(0, pCreateInfo->minLod), 15);
257 s.srgb_disable = 0; /* Not even set by v3d */
258 s.depth_compare_function =
259 vk_to_v3d_compare_func[pCreateInfo->compareEnable ?
260 pCreateInfo->compareOp : VK_COMPARE_OP_NEVER];
261 s.mip_filter_nearest = pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST;
262 s.min_filter_nearest = pCreateInfo->minFilter == VK_FILTER_NEAREST;
263 s.mag_filter_nearest = pCreateInfo->magFilter == VK_FILTER_NEAREST;
264 }
265 }
266
267 /**
268 * This computes the maximum bpp used by any of the render targets used by
269 * a particular subpass and checks if any of those render targets are
270 * multisampled. If we don't have a subpass (when we are not inside a
271 * render pass), then we assume that all framebuffer attachments are used.
272 */
273 void
v3dX(framebuffer_compute_internal_bpp_msaa)274 v3dX(framebuffer_compute_internal_bpp_msaa)(
275 const struct v3dv_framebuffer *framebuffer,
276 const struct v3dv_cmd_buffer_attachment_state *attachments,
277 const struct v3dv_subpass *subpass,
278 uint8_t *max_internal_bpp,
279 uint8_t *total_color_bpp,
280 bool *msaa)
281 {
282 STATIC_ASSERT(V3D_INTERNAL_BPP_32 == 0);
283 *max_internal_bpp = V3D_INTERNAL_BPP_32;
284 *total_color_bpp = 0;
285 *msaa = false;
286
287 if (subpass) {
288 for (uint32_t i = 0; i < subpass->color_count; i++) {
289 uint32_t att_idx = subpass->color_attachments[i].attachment;
290 if (att_idx == VK_ATTACHMENT_UNUSED)
291 continue;
292
293 const struct v3dv_image_view *att = attachments[att_idx].image_view;
294 assert(att);
295 assert(att->plane_count == 1);
296
297 if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
298 const uint32_t internal_bpp = att->planes[0].internal_bpp;
299 *max_internal_bpp = MAX2(*max_internal_bpp, internal_bpp);
300 *total_color_bpp += 4 * v3d_internal_bpp_words(internal_bpp);
301 }
302
303 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
304 *msaa = true;
305 }
306
307 if (!*msaa && subpass->ds_attachment.attachment != VK_ATTACHMENT_UNUSED) {
308 const struct v3dv_image_view *att =
309 attachments[subpass->ds_attachment.attachment].image_view;
310 assert(att);
311
312 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
313 *msaa = true;
314 }
315 return;
316 }
317
318 assert(framebuffer->attachment_count <= 4);
319 for (uint32_t i = 0; i < framebuffer->attachment_count; i++) {
320 const struct v3dv_image_view *att = attachments[i].image_view;
321 assert(att);
322 assert(att->plane_count == 1);
323
324 if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
325 const uint32_t internal_bpp = att->planes[0].internal_bpp;
326 *max_internal_bpp = MAX2(*max_internal_bpp, internal_bpp);
327 *total_color_bpp += 4 * v3d_internal_bpp_words(internal_bpp);
328 }
329
330 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
331 *msaa = true;
332 }
333
334 return;
335 }
336
337 uint32_t
v3dX(zs_buffer_from_aspect_bits)338 v3dX(zs_buffer_from_aspect_bits)(VkImageAspectFlags aspects)
339 {
340 const VkImageAspectFlags zs_aspects =
341 VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
342 const VkImageAspectFlags filtered_aspects = aspects & zs_aspects;
343
344 if (filtered_aspects == zs_aspects)
345 return ZSTENCIL;
346 else if (filtered_aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
347 return Z;
348 else if (filtered_aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
349 return STENCIL;
350 else
351 return NONE;
352 }
353
354 void
v3dX(get_hw_clear_color)355 v3dX(get_hw_clear_color)(const VkClearColorValue *color,
356 uint32_t internal_type,
357 uint32_t internal_size,
358 uint32_t *hw_color)
359 {
360 union util_color uc;
361 switch (internal_type) {
362 case V3D_INTERNAL_TYPE_8:
363 util_pack_color(color->float32, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
364 memcpy(hw_color, uc.ui, internal_size);
365 break;
366 case V3D_INTERNAL_TYPE_8I:
367 case V3D_INTERNAL_TYPE_8UI:
368 hw_color[0] = ((color->uint32[0] & 0xff) |
369 (color->uint32[1] & 0xff) << 8 |
370 (color->uint32[2] & 0xff) << 16 |
371 (color->uint32[3] & 0xff) << 24);
372 break;
373 case V3D_INTERNAL_TYPE_16F:
374 util_pack_color(color->float32, PIPE_FORMAT_R16G16B16A16_FLOAT, &uc);
375 memcpy(hw_color, uc.ui, internal_size);
376 break;
377 case V3D_INTERNAL_TYPE_16I:
378 case V3D_INTERNAL_TYPE_16UI:
379 hw_color[0] = ((color->uint32[0] & 0xffff) | color->uint32[1] << 16);
380 hw_color[1] = ((color->uint32[2] & 0xffff) | color->uint32[3] << 16);
381 break;
382 case V3D_INTERNAL_TYPE_32F:
383 case V3D_INTERNAL_TYPE_32I:
384 case V3D_INTERNAL_TYPE_32UI:
385 memcpy(hw_color, color->uint32, internal_size);
386 break;
387 }
388 }
389
390 #if MESA_DEBUG
391 void
v3dX(device_check_prepacked_sizes)392 v3dX(device_check_prepacked_sizes)(void)
393 {
394 STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
395 cl_packet_length(SAMPLER_STATE));
396 STATIC_ASSERT(V3DV_TEXTURE_SHADER_STATE_LENGTH >=
397 cl_packet_length(TEXTURE_SHADER_STATE));
398 STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
399 cl_packet_length(SAMPLER_STATE));
400 STATIC_ASSERT(V3DV_BLEND_CFG_LENGTH>=
401 cl_packet_length(BLEND_CFG));
402 STATIC_ASSERT(V3DV_CFG_BITS_LENGTH>=
403 cl_packet_length(CFG_BITS));
404 STATIC_ASSERT(V3DV_GL_SHADER_STATE_RECORD_LENGTH >=
405 cl_packet_length(GL_SHADER_STATE_RECORD));
406 STATIC_ASSERT(V3DV_VCM_CACHE_SIZE_LENGTH>=
407 cl_packet_length(VCM_CACHE_SIZE));
408 STATIC_ASSERT(V3DV_GL_SHADER_STATE_ATTRIBUTE_RECORD_LENGTH >=
409 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD));
410 STATIC_ASSERT(V3DV_STENCIL_CFG_LENGTH >=
411 cl_packet_length(STENCIL_CFG));
412 }
413 #endif
414