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
52
encode_border_color(const VkSamplerCustomBorderColorCreateInfoEXT * bc_info)53 static union pipe_color_union encode_border_color(
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 /* We use the swizzle in our format table to determine swizzle configuration
62 * for sampling as well as to decide if we need to use the Swap R/B and
63 * Reverse Channels bits for Tile Load/Store operations. The order of the
64 * R/B swap and Reverse operations matters and gives different swizzles.
65 * Our format table assumes that Reverse happens first and R/B Swap second.
66 * This seems to match semantics for texture sampling and Tile load/store,
67 * however, it seems that the semantics are reversed for custom border
68 * colors so we need to fix up the swizzle manually for this case.
69 */
70 uint8_t swizzle[4];
71 if (v3dv_format_swizzle_needs_reverse(format->swizzle) &&
72 v3dv_format_swizzle_needs_rb_swap(format->swizzle)) {
73 swizzle[0] = PIPE_SWIZZLE_W;
74 swizzle[1] = PIPE_SWIZZLE_X;
75 swizzle[2] = PIPE_SWIZZLE_Y;
76 swizzle[3] = PIPE_SWIZZLE_Z;
77 } else {
78 memcpy(swizzle, format->swizzle, sizeof (swizzle));
79 }
80
81 union pipe_color_union border;
82 for (int i = 0; i < 4; i++) {
83 if (format->swizzle[i] <= 3)
84 border.ui[i] = bc_info->customBorderColor.uint32[swizzle[i]];
85 else
86 border.ui[i] = 0;
87 }
88
89 /* handle clamping */
90 if (vk_format_has_depth(bc_info->format) &&
91 vk_format_has_stencil(bc_info->format)) {
92 border.f[0] = CLAMP(border.f[0], 0, 1);
93 border.ui[1] = CLAMP(border.ui[1], 0, 0xff);
94 } else if (vk_format_is_unorm(bc_info->format)) {
95 for (int i = 0; i < 4; i++)
96 border.f[i] = CLAMP(border.f[i], 0, 1);
97 } else if (vk_format_is_snorm(bc_info->format)) {
98 for (int i = 0; i < 4; i++)
99 border.f[i] = CLAMP(border.f[i], -1, 1);
100 } else if (vk_format_is_uint(bc_info->format) &&
101 desc->channel[0].size < 32) {
102 for (int i = 0; i < 4; i++)
103 border.ui[i] = CLAMP(border.ui[i], 0, (1 << desc->channel[i].size));
104 } else if (vk_format_is_sint(bc_info->format) &&
105 desc->channel[0].size < 32) {
106 for (int i = 0; i < 4; i++)
107 border.i[i] = CLAMP(border.i[i],
108 -(1 << (desc->channel[i].size - 1)),
109 (1 << (desc->channel[i].size - 1)) - 1);
110 }
111
112 /* convert from float to expected format */
113 if (vk_format_is_srgb(bc_info->format) ||
114 vk_format_is_compressed(bc_info->format)) {
115 for (int i = 0; i < 4; i++)
116 border.ui[i] = _mesa_float_to_half(border.f[i]);
117 } else if (vk_format_is_unorm(bc_info->format)) {
118 for (int i = 0; i < 4; i++) {
119 switch (desc->channel[i].size) {
120 case 8:
121 case 16:
122 /* expect u16 for non depth values */
123 if (!vk_format_has_depth(bc_info->format))
124 border.ui[i] = (uint32_t) (border.f[i] * (float) 0xffff);
125 break;
126 case 24:
127 case 32:
128 /* uses full f32; no conversion needed */
129 break;
130 default:
131 border.ui[i] = _mesa_float_to_half(border.f[i]);
132 break;
133 }
134 }
135 } else if (vk_format_is_snorm(bc_info->format)) {
136 for (int i = 0; i < 4; i++) {
137 switch (desc->channel[i].size) {
138 case 8:
139 border.ui[i] = (int32_t) (border.f[i] * (float) 0x3fff);
140 break;
141 case 16:
142 border.i[i] = (int32_t) (border.f[i] * (float) 0x7fff);
143 break;
144 case 24:
145 case 32:
146 /* uses full f32; no conversion needed */
147 break;
148 default:
149 border.ui[i] = _mesa_float_to_half(border.f[i]);
150 break;
151 }
152 }
153 } else if (vk_format_is_float(bc_info->format)) {
154 for (int i = 0; i < 4; i++) {
155 switch(desc->channel[i].size) {
156 case 16:
157 border.ui[i] = _mesa_float_to_half(border.f[i]);
158 break;
159 default:
160 break;
161 }
162 }
163 }
164
165 return border;
166 }
167
168 void
v3dX(pack_sampler_state)169 v3dX(pack_sampler_state)(struct v3dv_sampler *sampler,
170 const VkSamplerCreateInfo *pCreateInfo,
171 const VkSamplerCustomBorderColorCreateInfoEXT *bc_info)
172 {
173 enum V3DX(Border_Color_Mode) border_color_mode;
174
175 switch (pCreateInfo->borderColor) {
176 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
177 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
178 border_color_mode = V3D_BORDER_COLOR_0000;
179 break;
180 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
181 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
182 border_color_mode = V3D_BORDER_COLOR_0001;
183 break;
184 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
185 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
186 border_color_mode = V3D_BORDER_COLOR_1111;
187 break;
188 case VK_BORDER_COLOR_FLOAT_CUSTOM_EXT:
189 case VK_BORDER_COLOR_INT_CUSTOM_EXT:
190 border_color_mode = V3D_BORDER_COLOR_FOLLOWS;
191 break;
192 default:
193 unreachable("Unknown border color");
194 break;
195 }
196
197 /* For some texture formats, when clamping to transparent black border the
198 * CTS expects alpha to be set to 1 instead of 0, but the border color mode
199 * will take priority over the texture state swizzle, so the only way to
200 * fix that is to apply a swizzle in the shader. Here we keep track of
201 * whether we are activating that mode and we will decide if we need to
202 * activate the texture swizzle lowering in the shader key at compile time
203 * depending on the actual texture format.
204 */
205 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
206 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
207 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
208 border_color_mode == V3D_BORDER_COLOR_0000) {
209 sampler->clamp_to_transparent_black_border = true;
210 }
211
212 v3dvx_pack(sampler->sampler_state, SAMPLER_STATE, s) {
213 if (pCreateInfo->anisotropyEnable) {
214 s.anisotropy_enable = true;
215 if (pCreateInfo->maxAnisotropy > 8)
216 s.maximum_anisotropy = 3;
217 else if (pCreateInfo->maxAnisotropy > 4)
218 s.maximum_anisotropy = 2;
219 else if (pCreateInfo->maxAnisotropy > 2)
220 s.maximum_anisotropy = 1;
221 }
222
223 s.border_color_mode = border_color_mode;
224
225 if (s.border_color_mode == V3D_BORDER_COLOR_FOLLOWS) {
226 union pipe_color_union border = encode_border_color(bc_info);
227
228 s.border_color_word_0 = border.ui[0];
229 s.border_color_word_1 = border.ui[1];
230 s.border_color_word_2 = border.ui[2];
231 s.border_color_word_3 = border.ui[3];
232 }
233
234 s.wrap_i_border = false; /* Also hardcoded on v3d */
235 s.wrap_s = vk_to_v3d_wrap_mode[pCreateInfo->addressModeU];
236 s.wrap_t = vk_to_v3d_wrap_mode[pCreateInfo->addressModeV];
237 s.wrap_r = vk_to_v3d_wrap_mode[pCreateInfo->addressModeW];
238 s.fixed_bias = pCreateInfo->mipLodBias;
239 s.max_level_of_detail = MIN2(MAX2(0, pCreateInfo->maxLod), 15);
240 s.min_level_of_detail = MIN2(MAX2(0, pCreateInfo->minLod), 15);
241 s.srgb_disable = 0; /* Not even set by v3d */
242 s.depth_compare_function =
243 vk_to_v3d_compare_func[pCreateInfo->compareEnable ?
244 pCreateInfo->compareOp : VK_COMPARE_OP_NEVER];
245 s.mip_filter_nearest = pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST;
246 s.min_filter_nearest = pCreateInfo->minFilter == VK_FILTER_NEAREST;
247 s.mag_filter_nearest = pCreateInfo->magFilter == VK_FILTER_NEAREST;
248 }
249 }
250
251 /**
252 * This computes the maximum bpp used by any of the render targets used by
253 * a particular subpass and checks if any of those render targets are
254 * multisampled. If we don't have a subpass (when we are not inside a
255 * render pass), then we assume that all framebuffer attachments are used.
256 */
257 void
v3dX(framebuffer_compute_internal_bpp_msaa)258 v3dX(framebuffer_compute_internal_bpp_msaa)(
259 const struct v3dv_framebuffer *framebuffer,
260 const struct v3dv_cmd_buffer_attachment_state *attachments,
261 const struct v3dv_subpass *subpass,
262 uint8_t *max_bpp,
263 bool *msaa)
264 {
265 STATIC_ASSERT(V3D_INTERNAL_BPP_32 == 0);
266 *max_bpp = V3D_INTERNAL_BPP_32;
267 *msaa = false;
268
269 if (subpass) {
270 for (uint32_t i = 0; i < subpass->color_count; i++) {
271 uint32_t att_idx = subpass->color_attachments[i].attachment;
272 if (att_idx == VK_ATTACHMENT_UNUSED)
273 continue;
274
275 const struct v3dv_image_view *att = attachments[att_idx].image_view;
276 assert(att);
277
278 if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT)
279 *max_bpp = MAX2(*max_bpp, att->internal_bpp);
280
281 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
282 *msaa = true;
283 }
284
285 if (!*msaa && subpass->ds_attachment.attachment != VK_ATTACHMENT_UNUSED) {
286 const struct v3dv_image_view *att =
287 attachments[subpass->ds_attachment.attachment].image_view;
288 assert(att);
289
290 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
291 *msaa = true;
292 }
293
294 return;
295 }
296
297 assert(framebuffer->attachment_count <= 4);
298 for (uint32_t i = 0; i < framebuffer->attachment_count; i++) {
299 const struct v3dv_image_view *att = attachments[i].image_view;
300 assert(att);
301
302 if (att->vk.aspects & VK_IMAGE_ASPECT_COLOR_BIT)
303 *max_bpp = MAX2(*max_bpp, att->internal_bpp);
304
305 if (att->vk.image->samples > VK_SAMPLE_COUNT_1_BIT)
306 *msaa = true;
307 }
308
309 return;
310 }
311
312 uint32_t
v3dX(zs_buffer_from_aspect_bits)313 v3dX(zs_buffer_from_aspect_bits)(VkImageAspectFlags aspects)
314 {
315 const VkImageAspectFlags zs_aspects =
316 VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
317 const VkImageAspectFlags filtered_aspects = aspects & zs_aspects;
318
319 if (filtered_aspects == zs_aspects)
320 return ZSTENCIL;
321 else if (filtered_aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
322 return Z;
323 else if (filtered_aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
324 return STENCIL;
325 else
326 return NONE;
327 }
328
329 void
v3dX(get_hw_clear_color)330 v3dX(get_hw_clear_color)(const VkClearColorValue *color,
331 uint32_t internal_type,
332 uint32_t internal_size,
333 uint32_t *hw_color)
334 {
335 union util_color uc;
336 switch (internal_type) {
337 case V3D_INTERNAL_TYPE_8:
338 util_pack_color(color->float32, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
339 memcpy(hw_color, uc.ui, internal_size);
340 break;
341 case V3D_INTERNAL_TYPE_8I:
342 case V3D_INTERNAL_TYPE_8UI:
343 hw_color[0] = ((color->uint32[0] & 0xff) |
344 (color->uint32[1] & 0xff) << 8 |
345 (color->uint32[2] & 0xff) << 16 |
346 (color->uint32[3] & 0xff) << 24);
347 break;
348 case V3D_INTERNAL_TYPE_16F:
349 util_pack_color(color->float32, PIPE_FORMAT_R16G16B16A16_FLOAT, &uc);
350 memcpy(hw_color, uc.ui, internal_size);
351 break;
352 case V3D_INTERNAL_TYPE_16I:
353 case V3D_INTERNAL_TYPE_16UI:
354 hw_color[0] = ((color->uint32[0] & 0xffff) | color->uint32[1] << 16);
355 hw_color[1] = ((color->uint32[2] & 0xffff) | color->uint32[3] << 16);
356 break;
357 case V3D_INTERNAL_TYPE_32F:
358 case V3D_INTERNAL_TYPE_32I:
359 case V3D_INTERNAL_TYPE_32UI:
360 memcpy(hw_color, color->uint32, internal_size);
361 break;
362 }
363 }
364
365 #ifdef DEBUG
366 void
v3dX(device_check_prepacked_sizes)367 v3dX(device_check_prepacked_sizes)(void)
368 {
369 STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
370 cl_packet_length(SAMPLER_STATE));
371 STATIC_ASSERT(V3DV_TEXTURE_SHADER_STATE_LENGTH >=
372 cl_packet_length(TEXTURE_SHADER_STATE));
373 STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
374 cl_packet_length(SAMPLER_STATE));
375 STATIC_ASSERT(V3DV_BLEND_CFG_LENGTH>=
376 cl_packet_length(BLEND_CFG));
377 STATIC_ASSERT(V3DV_CFG_BITS_LENGTH>=
378 cl_packet_length(CFG_BITS));
379 STATIC_ASSERT(V3DV_GL_SHADER_STATE_RECORD_LENGTH >=
380 cl_packet_length(GL_SHADER_STATE_RECORD));
381 STATIC_ASSERT(V3DV_VCM_CACHE_SIZE_LENGTH>=
382 cl_packet_length(VCM_CACHE_SIZE));
383 STATIC_ASSERT(V3DV_GL_SHADER_STATE_ATTRIBUTE_RECORD_LENGTH >=
384 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD));
385 STATIC_ASSERT(V3DV_STENCIL_CFG_LENGTH >=
386 cl_packet_length(STENCIL_CFG));
387 }
388 #endif
389