| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* Copyright © 2024 Arm Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "panvk_buffer.h" |
| 8 |
#include "panvk_cmd_buffer.h" |
| 9 |
#include "panvk_device_memory.h" |
| 10 |
#include "panvk_entrypoints.h" |
| 11 |
|
| 12 |
#include "pan_desc.h" |
| 13 |
#include "pan_util.h" |
| 14 |
|
| 15 |
static enum pan_fb_load_op |
| 16 |
get_att_fb_load_op(const VkRenderingAttachmentInfo *att) |
| 17 |
{ |
| 18 |
switch (att->loadOp) { |
| 19 |
case VK_ATTACHMENT_LOAD_OP_CLEAR: |
| 20 |
return PAN_FB_LOAD_CLEAR; |
| 21 |
case VK_ATTACHMENT_LOAD_OP_LOAD: |
| 22 |
return PAN_FB_LOAD_IMAGE; |
| 23 |
case VK_ATTACHMENT_LOAD_OP_NONE: |
| 24 |
case VK_ATTACHMENT_LOAD_OP_DONT_CARE: |
| 25 |
/* This is a very frustrating corner case. From the spec: |
| 26 |
* |
| 27 |
* VK_ATTACHMENT_STORE_OP_NONE specifies the contents within the |
| 28 |
* render area are not accessed by the store operation as long as |
| 29 |
* no values are written to the attachment during the render pass. |
| 30 |
* |
| 31 |
* With VK_ATTACHMENT_LOAD_OP_DONT_CARE + VK_ATTACHMENT_STORE_OP_NONE, |
| 32 |
* we need to preserve the contents throughout partial renders. The |
| 33 |
* easiest way to do that is forcing a preload, so that partial stores |
| 34 |
* for unused attachments will be no-op'd by writing existing contents. |
| 35 |
* |
| 36 |
* TODO: disable preload when we have clean_tile_write_enable = false |
| 37 |
* as an optimization |
| 38 |
*/ |
| 39 |
if (att->storeOp == VK_ATTACHMENT_STORE_OP_NONE) |
| 40 |
return PAN_FB_LOAD_IMAGE; |
| 41 |
else |
| 42 |
return PAN_FB_LOAD_NONE; |
| 43 |
default: |
| 44 |
UNREACHABLE("Unsupported loadOp"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
static enum pan_fb_msaa_copy_op |
| 49 |
vk_to_pan_fb_resolve_mode(VkResolveModeFlagBits resolveMode) |
| 50 |
{ |
| 51 |
switch (resolveMode) { |
| 52 |
case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT: return PAN_FB_MSAA_COPY_SAMPLE_0; |
| 53 |
case VK_RESOLVE_MODE_AVERAGE_BIT: return PAN_FB_MSAA_COPY_AVERAGE; |
| 54 |
case VK_RESOLVE_MODE_MIN_BIT: return PAN_FB_MSAA_COPY_MIN; |
| 55 |
case VK_RESOLVE_MODE_MAX_BIT: return PAN_FB_MSAA_COPY_MAX; |
| 56 |
default: UNREACHABLE("Unsupported resolveMode"); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static struct panvk_image_view * |
| 61 |
get_ms2ss_image_view(struct panvk_image_view *iview, uint32_t nr_samples) |
| 62 |
{ |
| 63 |
assert(nr_samples >= 2 && nr_samples <= 16); |
| 64 |
assert(iview->pview.nr_samples == 1); |
| 65 |
assert(iview->vk.image->create_flags & |
| 66 |
VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT); |
| 67 |
|
| 68 |
/* sample count 2 is at index 0, 4 at 1, .. */ |
| 69 |
uint32_t vidx = 0; |
| 70 |
switch (nr_samples) { |
| 71 |
case VK_SAMPLE_COUNT_2_BIT: |
| 72 |
vidx = 0; |
| 73 |
break; |
| 74 |
case VK_SAMPLE_COUNT_4_BIT: |
| 75 |
vidx = 1; |
| 76 |
break; |
| 77 |
case VK_SAMPLE_COUNT_8_BIT: |
| 78 |
vidx = 2; |
| 79 |
break; |
| 80 |
case VK_SAMPLE_COUNT_16_BIT: |
| 81 |
vidx = 3; |
| 82 |
break; |
| 83 |
default: |
| 84 |
UNREACHABLE("unhandled sample count"); |
| 85 |
} |
| 86 |
assert(iview->ms_views[vidx] != VK_NULL_HANDLE); |
| 87 |
|
| 88 |
struct panvk_image_view *res = |
| 89 |
panvk_image_view_from_handle(iview->ms_views[vidx]); |
| 90 |
|
| 91 |
assert(res->pview.nr_samples == nr_samples); |
| 92 |
|
| 93 |
return res; |
| 94 |
} |
| 95 |
|
| 96 |
static bool |
| 97 |
avoid_direct_resolve_to(const struct pan_image *img) |
| 98 |
{ |
| 99 |
/* There is an issue with AFBC and small tiles where, if the tile size is |
| 100 |
* not a multiple of superblock size then writes and reads may race within |
| 101 |
* a superblock. On v7+, there's a hardware bit to help us work around |
| 102 |
* this but it doesn't exist on v6 and earlier. |
| 103 |
* |
| 104 |
* This is particularly likely to happen with MSAA since the sample count |
| 105 |
* is a multiplier on the color allocation, making it much more likely that |
| 106 |
* we'll hit this case. MSAA images are safe because they don't allow AFBC |
| 107 |
* but we have a real problem if we attempt to resolve directly to an |
| 108 |
* AFBC-compressed single-sampled image. Skip the resolve optimization in |
| 109 |
* this case. |
| 110 |
* |
| 111 |
* TODO: If we moved this decision later, it could be based on the final |
| 112 |
* framebuffer layout and we could potentially allow direct resolves in |
| 113 |
* more cases. |
| 114 |
*/ |
| 115 |
return PAN_ARCH < 7 && drm_is_afbc(img->props.modifier); |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
render_state_set_color_attachment(struct panvk_cmd_buffer *cmdbuf, |
| 120 |
const VkRenderingAttachmentInfo *att, |
| 121 |
uint32_t index) |
| 122 |
{ |
| 123 |
struct panvk_rendering_state *render = &cmdbuf->state.gfx.render; |
| 124 |
VK_FROM_HANDLE(panvk_image_view, iview, att->imageView); |
| 125 |
|
| 126 |
struct panvk_image_view *iview_ss = NULL; |
| 127 |
const bool ms2ss = render->fb.nr_samples > 1 && |
| 128 |
iview->pview.nr_samples == 1; |
| 129 |
|
| 130 |
if (ms2ss) { |
| 131 |
iview_ss = iview; |
| 132 |
iview = get_ms2ss_image_view(iview, render->fb.nr_samples); |
| 133 |
} |
| 134 |
|
| 135 |
struct panvk_image *img = |
| 136 |
container_of(iview->vk.image, struct panvk_image, vk); |
| 137 |
|
| 138 |
render->bound_attachments |= MESA_VK_RP_ATTACHMENT_COLOR_BIT(index); |
| 139 |
render->color_attachments.iviews[index] = iview; |
| 140 |
render->color_attachments.preload_iviews[index] = |
| 141 |
ms2ss ? iview_ss : NULL; |
| 142 |
render->color_attachments.fmts[index] = iview->vk.format; |
| 143 |
render->color_attachments.samples[index] = img->vk.samples; |
| 144 |
|
| 145 |
#if PAN_ARCH < 9 |
| 146 |
for (uint8_t p = 0; p < ARRAY_SIZE(iview->pview.planes); p++) { |
| 147 |
struct pan_image_plane_ref pref = |
| 148 |
pan_image_view_get_plane(&iview->pview, p); |
| 149 |
|
| 150 |
if (!pref.image) |
| 151 |
continue; |
| 152 |
|
| 153 |
assert(pref.plane_idx < ARRAY_SIZE(img->planes)); |
| 154 |
assert(img->planes[pref.plane_idx].mem->bo != NULL); |
| 155 |
render->fb.bos[render->fb.bo_count++] = |
| 156 |
img->planes[pref.plane_idx].mem->bo; |
| 157 |
} |
| 158 |
#endif |
| 159 |
|
| 160 |
render->fb.layout.rt_formats[index] = iview->pview.format; |
| 161 |
render->fb.nr_samples = |
| 162 |
MAX2(render->fb.nr_samples, |
| 163 |
pan_image_view_get_nr_samples(&iview->pview)); |
| 164 |
|
| 165 |
render->fb.load.rts[index] = (struct pan_fb_load_target) { |
| 166 |
.in_bounds_load = get_att_fb_load_op(att), |
| 167 |
.border_load = PAN_FB_LOAD_IMAGE, |
| 168 |
.always = att->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 169 |
.msaa = ms2ss ? PAN_FB_MSAA_COPY_SINGLE |
| 170 |
: PAN_FB_MSAA_COPY_ALL, |
| 171 |
.iview = ms2ss ? &iview_ss->pview : &iview->pview, |
| 172 |
.clear.color.ui = { |
| 173 |
att->clearValue.color.uint32[0], |
| 174 |
att->clearValue.color.uint32[1], |
| 175 |
att->clearValue.color.uint32[2], |
| 176 |
att->clearValue.color.uint32[3], |
| 177 |
}, |
| 178 |
}; |
| 179 |
render->fb.spill.load.rts[index] = pan_fb_load_iview(&iview->pview); |
| 180 |
render->fb.spill.store.rts[index] = pan_fb_store_iview(&iview->pview); |
| 181 |
if (att->storeOp == VK_ATTACHMENT_STORE_OP_STORE && !ms2ss) |
| 182 |
render->fb.store.rts[index] = pan_fb_store_iview(&iview->pview); |
| 183 |
|
| 184 |
if (att->resolveMode != VK_RESOLVE_MODE_NONE) { |
| 185 |
VK_FROM_HANDLE(panvk_image_view, resolve_iview, att->resolveImageView); |
| 186 |
|
| 187 |
/* VUID-VkRenderingAttachmentInfo-imageView-06862 and |
| 188 |
* VUID-VkRenderingAttachmentInfo-imageView-06863: |
| 189 |
* If resolveMode != NONE, then |
| 190 |
* resolveView == NULL iff. multisampledRenderToSingleSampledEnable */ |
| 191 |
assert(ms2ss == (resolve_iview == NULL)); |
| 192 |
|
| 193 |
const struct panvk_resolve_attachment resolve = { |
| 194 |
.dst_iview = ms2ss ? iview_ss : resolve_iview, |
| 195 |
.mode = att->resolveMode, |
| 196 |
}; |
| 197 |
assert(resolve.dst_iview != NULL); |
| 198 |
assert(resolve.dst_iview->pview.nr_samples == 1); |
| 199 |
|
| 200 |
const struct pan_image *resolve_pimage = |
| 201 |
pan_image_view_get_color_plane(&resolve.dst_iview->pview).image; |
| 202 |
|
| 203 |
if ((ms2ss || att->storeOp != VK_ATTACHMENT_STORE_OP_STORE) && |
| 204 |
!avoid_direct_resolve_to(resolve_pimage)) { |
| 205 |
render->fb.resolve.rts[index] = (struct pan_fb_resolve_target) { |
| 206 |
.in_bounds = { |
| 207 |
.resolve = PAN_FB_RESOLVE_RT(index), |
| 208 |
.msaa = vk_to_pan_fb_resolve_mode(att->resolveMode), |
| 209 |
}, |
| 210 |
.border = { |
| 211 |
.resolve = PAN_FB_RESOLVE_IMAGE, |
| 212 |
.msaa = PAN_FB_MSAA_COPY_SINGLE, |
| 213 |
}, |
| 214 |
.iview = &resolve.dst_iview->pview, |
| 215 |
}; |
| 216 |
render->fb.store.rts[index] = |
| 217 |
pan_fb_always_store_iview_s0(&resolve.dst_iview->pview); |
| 218 |
} else { |
| 219 |
/* We need to store so we can do the MSAA resolve later */ |
| 220 |
render->fb.store.rts[index] = pan_fb_store_iview(&iview->pview); |
| 221 |
render->color_attachments.resolve[index] = resolve; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static struct pan_image_view |
| 227 |
get_zs_pan_image_view_aspects(struct panvk_image_view *iview, |
| 228 |
VkImageAspectFlags aspects) |
| 229 |
{ |
| 230 |
struct panvk_image *img = |
| 231 |
container_of(iview->vk.image, struct panvk_image, vk); |
| 232 |
unsigned plane_idx = panvk_plane_index(img, aspects); |
| 233 |
|
| 234 |
/* From the Vulkan 1.4.335 spec: |
| 235 |
* |
| 236 |
* "The aspectMask of any image view specified for pDepthAttachment |
| 237 |
* or pStencilAttachment is ignored. Instead, depth attachments are |
| 238 |
* automatically treated as if VK_IMAGE_ASPECT_DEPTH_BIT was |
| 239 |
* specified for their aspect masks" |
| 240 |
* |
| 241 |
* We need to re-create the image view more-or-less from scratch. The old |
| 242 |
* image view might not have the right effective format or point to the |
| 243 |
* right planes. |
| 244 |
*/ |
| 245 |
|
| 246 |
struct pan_image_view pview = iview->pview; |
| 247 |
if (panvk_image_is_planar_depth_stencil(img)) { |
| 248 |
assert(util_bitcount(aspects) == 1); |
| 249 |
pview.format = aspects == VK_IMAGE_ASPECT_STENCIL_BIT |
| 250 |
? panvk_image_stencil_only_pfmt(img) |
| 251 |
: panvk_image_depth_only_pfmt(img); |
| 252 |
} else { |
| 253 |
assert(img->plane_count == 1 && plane_idx == 0); |
| 254 |
pview.format = img->planes[0].image.props.format; |
| 255 |
} |
| 256 |
|
| 257 |
memset(pview.planes, 0, sizeof(pview.planes)); |
| 258 |
pview.planes[0] = (struct pan_image_plane_ref) { |
| 259 |
.image = &img->planes[plane_idx].image, |
| 260 |
.plane_idx = 0, |
| 261 |
}; |
| 262 |
|
| 263 |
return pview; |
| 264 |
} |
| 265 |
|
| 266 |
static struct pan_image_view |
| 267 |
get_z_pan_image_view(struct panvk_image_view *iview) |
| 268 |
{ |
| 269 |
return get_zs_pan_image_view_aspects(iview, VK_IMAGE_ASPECT_DEPTH_BIT); |
| 270 |
} |
| 271 |
|
| 272 |
static struct pan_image_view |
| 273 |
get_s_pan_image_view(struct panvk_image_view *iview) |
| 274 |
{ |
| 275 |
return get_zs_pan_image_view_aspects(iview, VK_IMAGE_ASPECT_STENCIL_BIT); |
| 276 |
} |
| 277 |
|
| 278 |
static struct pan_image_view |
| 279 |
get_zs_pan_image_view(struct panvk_image_view *iview) |
| 280 |
{ |
| 281 |
return get_zs_pan_image_view_aspects(iview, VK_IMAGE_ASPECT_DEPTH_BIT | |
| 282 |
VK_IMAGE_ASPECT_STENCIL_BIT); |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
render_state_set_zs_attachments(struct panvk_cmd_buffer *cmdbuf, |
| 287 |
const VkRenderingAttachmentInfo *z_att, |
| 288 |
const VkRenderingAttachmentInfo *s_att) |
| 289 |
{ |
| 290 |
struct panvk_rendering_state *render = &cmdbuf->state.gfx.render; |
| 291 |
struct panvk_image_view *z_iview = NULL, *z_iview_ss = NULL; |
| 292 |
struct panvk_image_view *s_iview = NULL, *s_iview_ss = NULL; |
| 293 |
const struct panvk_image *z_img = NULL, *s_img = NULL; |
| 294 |
bool z_ms2ss = false, s_ms2ss = false; |
| 295 |
|
| 296 |
/* First grab our images/views and set up the API-level stuff. For |
| 297 |
* multisampled render to single sampled, this also includes fetching |
| 298 |
* the multisampled view. |
| 299 |
*/ |
| 300 |
|
| 301 |
if (z_att) { |
| 302 |
z_iview = panvk_image_view_from_handle(z_att->imageView); |
| 303 |
z_img = container_of(z_iview->vk.image, struct panvk_image, vk); |
| 304 |
|
| 305 |
z_ms2ss = render->fb.nr_samples > 1 && z_iview->pview.nr_samples == 1; |
| 306 |
if (z_ms2ss) { |
| 307 |
z_iview_ss = z_iview; |
| 308 |
z_iview = get_ms2ss_image_view(z_iview, render->fb.nr_samples); |
| 309 |
} |
| 310 |
|
| 311 |
render->bound_attachments |= MESA_VK_RP_ATTACHMENT_DEPTH_BIT; |
| 312 |
render->z_attachment.iview = z_iview; |
| 313 |
render->z_attachment.preload_iview = z_ms2ss ? z_iview_ss : NULL; |
| 314 |
render->z_attachment.fmt = z_iview->vk.format; |
| 315 |
render->fb.nr_samples = |
| 316 |
MAX2(render->fb.nr_samples, |
| 317 |
pan_image_view_get_nr_samples(&z_iview->pview)); |
| 318 |
|
| 319 |
#if PAN_ARCH < 9 |
| 320 |
/* Depth plane always comes first. */ |
| 321 |
render->fb.bos[render->fb.bo_count++] = z_img->planes[0].mem->bo; |
| 322 |
#endif |
| 323 |
} |
| 324 |
|
| 325 |
if (s_att) { |
| 326 |
s_iview = panvk_image_view_from_handle(s_att->imageView); |
| 327 |
s_img = container_of(s_iview->vk.image, struct panvk_image, vk); |
| 328 |
|
| 329 |
s_ms2ss = render->fb.nr_samples > 1 && s_iview->pview.nr_samples == 1; |
| 330 |
if (s_ms2ss) { |
| 331 |
s_iview_ss = s_iview; |
| 332 |
s_iview = get_ms2ss_image_view(s_iview, render->fb.nr_samples); |
| 333 |
} |
| 334 |
|
| 335 |
render->bound_attachments |= MESA_VK_RP_ATTACHMENT_STENCIL_BIT; |
| 336 |
render->s_attachment.iview = s_iview; |
| 337 |
render->s_attachment.preload_iview = s_ms2ss ? s_iview_ss : NULL; |
| 338 |
render->s_attachment.fmt = s_iview->vk.format; |
| 339 |
render->fb.nr_samples = |
| 340 |
MAX2(render->fb.nr_samples, |
| 341 |
pan_image_view_get_nr_samples(&s_iview->pview)); |
| 342 |
|
| 343 |
#if PAN_ARCH < 9 |
| 344 |
/* The stencil plane is always last. */ |
| 345 |
render->fb.bos[render->fb.bo_count++] = |
| 346 |
s_img->planes[s_img->plane_count - 1].mem->bo; |
| 347 |
#endif |
| 348 |
} |
| 349 |
|
| 350 |
const bool interleaved_zs = |
| 351 |
(z_img && panvk_image_is_interleaved_depth_stencil(z_img)) || |
| 352 |
(s_img && panvk_image_is_interleaved_depth_stencil(s_img)); |
| 353 |
|
| 354 |
/* When both depth and stencil are bound, they have to be effecitvly |
| 355 |
* the same image view. This is a global Vulkan requirement but it |
| 356 |
* really only matters for us in the interleaved case. |
| 357 |
*/ |
| 358 |
if (interleaved_zs && z_img && s_img) { |
| 359 |
assert(z_img == s_img); |
| 360 |
assert(z_iview->pview.dim == s_iview->pview.dim); |
| 361 |
assert(z_iview->pview.first_level == s_iview->pview.first_level); |
| 362 |
assert(z_iview->pview.last_level == s_iview->pview.last_level); |
| 363 |
assert(z_iview->pview.first_layer == s_iview->pview.first_layer); |
| 364 |
assert(z_iview->pview.last_layer == s_iview->pview.last_layer); |
| 365 |
assert(z_iview->pview.nr_samples == s_iview->pview.nr_samples); |
| 366 |
} |
| 367 |
|
| 368 |
/* Set up the framebuffer formats for Z/S as well as framefubber loads or |
| 369 |
* clears, as needed. |
| 370 |
*/ |
| 371 |
|
| 372 |
if (z_att) { |
| 373 |
render->fb.layout.z_format = panvk_image_depth_only_pfmt(z_img); |
| 374 |
render->z_pview.load = |
| 375 |
get_z_pan_image_view(z_ms2ss ? z_iview_ss : z_iview); |
| 376 |
render->fb.load.z = (struct pan_fb_load_target) { |
| 377 |
.in_bounds_load = get_att_fb_load_op(z_att), |
| 378 |
.border_load = PAN_FB_LOAD_IMAGE, |
| 379 |
.always = z_att->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 380 |
.msaa = z_ms2ss ? PAN_FB_MSAA_COPY_SINGLE |
| 381 |
: PAN_FB_MSAA_COPY_ALL, |
| 382 |
.iview = &render->z_pview.load, |
| 383 |
.clear.depth = z_att->clearValue.depthStencil.depth, |
| 384 |
}; |
| 385 |
} else if (interleaved_zs) { |
| 386 |
/* If we have interleaved Z/S and depth isn't bound, we need to load |
| 387 |
* it anyway so it doesn't get stompped when we write out at the end. |
| 388 |
*/ |
| 389 |
render->fb.layout.z_format = panvk_image_depth_only_pfmt(s_img); |
| 390 |
render->fb.load.z = pan_fb_load_iview(&render->s_pview.load); |
| 391 |
} |
| 392 |
|
| 393 |
if (s_att) { |
| 394 |
render->fb.layout.s_format = panvk_image_stencil_only_pfmt(s_img); |
| 395 |
render->s_pview.load = |
| 396 |
get_s_pan_image_view(s_ms2ss ? s_iview_ss : s_iview); |
| 397 |
render->fb.load.s = (struct pan_fb_load_target) { |
| 398 |
.in_bounds_load = get_att_fb_load_op(s_att), |
| 399 |
.border_load = PAN_FB_LOAD_IMAGE, |
| 400 |
.always = s_att->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 401 |
.msaa = s_ms2ss ? PAN_FB_MSAA_COPY_SINGLE |
| 402 |
: PAN_FB_MSAA_COPY_ALL, |
| 403 |
.iview = &render->s_pview.load, |
| 404 |
.clear.stencil = s_att->clearValue.depthStencil.stencil, |
| 405 |
}; |
| 406 |
} else if (interleaved_zs) { |
| 407 |
/* If we have interleaved Z/S and stencil isn't bound, we need to load |
| 408 |
* it anyway so it doesn't get stompped when we write out at the end. |
| 409 |
*/ |
| 410 |
render->fb.layout.s_format = panvk_image_stencil_only_pfmt(z_img); |
| 411 |
render->fb.load.s = pan_fb_load_iview(&render->z_pview.load); |
| 412 |
} |
| 413 |
|
| 414 |
/* Set up spill load/stores for incremental rendering */ |
| 415 |
|
| 416 |
if (interleaved_zs) { |
| 417 |
/* Use the same image view for both spills */ |
| 418 |
struct panvk_image_view *iview = z_iview ? z_iview : s_iview; |
| 419 |
render->z_pview.spill = get_zs_pan_image_view(iview); |
| 420 |
render->fb.spill.load.z = pan_fb_load_iview(&render->z_pview.spill); |
| 421 |
render->fb.spill.load.s = pan_fb_load_iview(&render->z_pview.spill); |
| 422 |
render->fb.spill.store.zs = pan_fb_store_iview(&render->z_pview.spill); |
| 423 |
} else { |
| 424 |
if (z_iview) { |
| 425 |
render->z_pview.spill = get_z_pan_image_view(z_iview); |
| 426 |
render->fb.spill.load.z = pan_fb_load_iview(&render->z_pview.spill); |
| 427 |
render->fb.spill.store.zs = pan_fb_store_iview(&render->z_pview.spill); |
| 428 |
} |
| 429 |
|
| 430 |
if (s_iview) { |
| 431 |
render->s_pview.spill = get_s_pan_image_view(s_iview); |
| 432 |
render->fb.spill.load.s = pan_fb_load_iview(&render->s_pview.spill); |
| 433 |
render->fb.spill.store.s = pan_fb_store_iview(&render->s_pview.spill); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/* Set up the final render target write(s) at the end, as well as any |
| 438 |
* multisample resolves. |
| 439 |
*/ |
| 440 |
|
| 441 |
struct panvk_resolve_attachment z_resolve = { }; |
| 442 |
if (z_att && z_att->resolveMode != VK_RESOLVE_MODE_NONE) { |
| 443 |
VK_FROM_HANDLE(panvk_image_view, resolve_iview, z_att->resolveImageView); |
| 444 |
assert(z_ms2ss == (resolve_iview == NULL)); |
| 445 |
|
| 446 |
z_resolve = (struct panvk_resolve_attachment) { |
| 447 |
.dst_iview = z_ms2ss ? z_iview_ss : resolve_iview, |
| 448 |
.mode = z_att->resolveMode, |
| 449 |
}; |
| 450 |
assert(z_resolve.dst_iview != NULL); |
| 451 |
assert(z_resolve.dst_iview->pview.nr_samples == 1); |
| 452 |
} |
| 453 |
|
| 454 |
struct panvk_resolve_attachment s_resolve = { }; |
| 455 |
if (s_att && s_att->resolveMode != VK_RESOLVE_MODE_NONE) { |
| 456 |
VK_FROM_HANDLE(panvk_image_view, resolve_iview, s_att->resolveImageView); |
| 457 |
assert(s_ms2ss == (resolve_iview == NULL)); |
| 458 |
|
| 459 |
s_resolve = (struct panvk_resolve_attachment) { |
| 460 |
.dst_iview = s_ms2ss ? s_iview_ss : resolve_iview, |
| 461 |
.mode = s_att->resolveMode, |
| 462 |
}; |
| 463 |
assert(s_resolve.dst_iview != NULL); |
| 464 |
assert(s_resolve.dst_iview->pview.nr_samples == 1); |
| 465 |
} |
| 466 |
|
| 467 |
if (interleaved_zs) { |
| 468 |
/* Store both Z and S together */ |
| 469 |
struct panvk_image_view *iview = z_iview ? z_iview : s_iview; |
| 470 |
render->z_pview.store = get_zs_pan_image_view(iview); |
| 471 |
render->fb.store.zs = pan_fb_store_iview(&render->z_pview.store); |
| 472 |
|
| 473 |
/* It's probably possible to make resolve shaders work with interleaved |
| 474 |
* Z/S but it's tricky at best. For now, skip this optimization in the |
| 475 |
* interleaved case. |
| 476 |
*/ |
| 477 |
render->z_attachment.resolve = z_resolve; |
| 478 |
render->s_attachment.resolve = s_resolve; |
| 479 |
} else { |
| 480 |
if (z_iview) { |
| 481 |
render->z_pview.store = get_z_pan_image_view(z_iview); |
| 482 |
if (z_att->storeOp == VK_ATTACHMENT_STORE_OP_STORE && !z_ms2ss) |
| 483 |
render->fb.store.zs = pan_fb_store_iview(&render->z_pview.store); |
| 484 |
} |
| 485 |
|
| 486 |
if (s_iview) { |
| 487 |
render->s_pview.store = get_s_pan_image_view(s_iview); |
| 488 |
if (s_att->storeOp == VK_ATTACHMENT_STORE_OP_STORE && !s_ms2ss) |
| 489 |
render->fb.store.s = pan_fb_store_iview(&render->s_pview.store); |
| 490 |
} |
| 491 |
|
| 492 |
if (z_resolve.mode != VK_RESOLVE_MODE_NONE) { |
| 493 |
const struct pan_image *z_resolve_pimage = |
| 494 |
pan_image_view_get_zs_plane(&z_resolve.dst_iview->pview).image; |
| 495 |
|
| 496 |
if ((z_ms2ss || z_att->storeOp != VK_ATTACHMENT_STORE_OP_STORE) && |
| 497 |
!avoid_direct_resolve_to(z_resolve_pimage)) { |
| 498 |
render->z_pview.resolve = get_z_pan_image_view(z_resolve.dst_iview); |
| 499 |
render->fb.resolve.z = (struct pan_fb_resolve_target) { |
| 500 |
.in_bounds = { |
| 501 |
.resolve = PAN_FB_RESOLVE_Z, |
| 502 |
.msaa = vk_to_pan_fb_resolve_mode(z_att->resolveMode), |
| 503 |
}, |
| 504 |
.border = { |
| 505 |
.resolve = PAN_FB_RESOLVE_IMAGE, |
| 506 |
.msaa = PAN_FB_MSAA_COPY_SINGLE, |
| 507 |
}, |
| 508 |
.iview = &render->z_pview.resolve, |
| 509 |
}; |
| 510 |
render->fb.store.zs = |
| 511 |
pan_fb_always_store_iview_s0(&render->z_pview.resolve); |
| 512 |
} else { |
| 513 |
/* We need to store so we can do the MSAA resolve later */ |
| 514 |
render->fb.store.zs = pan_fb_store_iview(&render->z_pview.store); |
| 515 |
render->z_attachment.resolve = z_resolve; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
if (s_resolve.mode != VK_RESOLVE_MODE_NONE) { |
| 520 |
const struct pan_image *s_resolve_pimage = |
| 521 |
pan_image_view_get_s_plane(&s_resolve.dst_iview->pview).image; |
| 522 |
|
| 523 |
if ((s_ms2ss || s_att->storeOp != VK_ATTACHMENT_STORE_OP_STORE) && |
| 524 |
!avoid_direct_resolve_to(s_resolve_pimage)) { |
| 525 |
render->s_pview.resolve = get_s_pan_image_view(s_resolve.dst_iview); |
| 526 |
render->fb.resolve.s = (struct pan_fb_resolve_target) { |
| 527 |
.in_bounds = { |
| 528 |
.resolve = PAN_FB_RESOLVE_S, |
| 529 |
.msaa = vk_to_pan_fb_resolve_mode(s_att->resolveMode), |
| 530 |
}, |
| 531 |
.border = { |
| 532 |
.resolve = PAN_FB_RESOLVE_IMAGE, |
| 533 |
.msaa = PAN_FB_MSAA_COPY_SINGLE, |
| 534 |
}, |
| 535 |
.iview = &render->s_pview.resolve, |
| 536 |
}; |
| 537 |
render->fb.store.s = |
| 538 |
pan_fb_always_store_iview_s0(&render->s_pview.resolve); |
| 539 |
} else { |
| 540 |
/* We need to store so we can do the MSAA resolve later */ |
| 541 |
render->fb.store.s = pan_fb_store_iview(&render->s_pview.store); |
| 542 |
render->s_attachment.resolve = s_resolve; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
void |
| 549 |
panvk_per_arch(cmd_init_render_state)(struct panvk_cmd_buffer *cmdbuf, |
| 550 |
const VkRenderingInfo *pRenderingInfo) |
| 551 |
{ |
| 552 |
struct panvk_physical_device *phys_dev = |
| 553 |
to_panvk_physical_device(cmdbuf->vk.base.device->physical); |
| 554 |
struct panvk_rendering_state *render = &cmdbuf->state.gfx.render; |
| 555 |
uint32_t att_width = UINT32_MAX, att_height = UINT32_MAX; |
| 556 |
|
| 557 |
render->flags = pRenderingInfo->flags; |
| 558 |
|
| 559 |
BITSET_SET(cmdbuf->state.gfx.dirty, PANVK_CMD_GRAPHICS_DIRTY_RENDER_STATE); |
| 560 |
|
| 561 |
render->first_provoking_vertex = U_TRISTATE_UNSET; |
| 562 |
#if PAN_ARCH >= 10 |
| 563 |
render->maybe_set_tds_provoking_vertex = NULL; |
| 564 |
render->maybe_set_fbds_provoking_vertex = NULL; |
| 565 |
#endif |
| 566 |
memset(&render->color_attachments, 0, |
| 567 |
sizeof(render->color_attachments)); |
| 568 |
memset(&render->z_attachment, 0, sizeof(render->z_attachment)); |
| 569 |
memset(&render->s_attachment, 0, sizeof(render->s_attachment)); |
| 570 |
memset(&render->fb, 0, sizeof(render->fb)); |
| 571 |
render->bound_attachments = 0; |
| 572 |
|
| 573 |
const VkMultisampledRenderToSingleSampledInfoEXT *ms2ss_info = |
| 574 |
vk_find_struct_const(pRenderingInfo, |
| 575 |
MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_INFO_EXT); |
| 576 |
const bool ms2ss = ms2ss_info |
| 577 |
? ms2ss_info->multisampledRenderToSingleSampledEnable |
| 578 |
: VK_FALSE; |
| 579 |
|
| 580 |
render->layer_count = pRenderingInfo->viewMask |
| 581 |
? util_last_bit(pRenderingInfo->viewMask) |
| 582 |
: pRenderingInfo->layerCount; |
| 583 |
render->view_mask = pRenderingInfo->viewMask; |
| 584 |
render->fb.layout = (struct pan_fb_layout) { |
| 585 |
/* In case ms2ss is enabled, use the provided sample count. |
| 586 |
* |
| 587 |
* All attachments need to have sample count == 1 or the provided value. |
| 588 |
* But, if all attachments have 1, we would end up choosing the wrong |
| 589 |
* value if we don't set it here already. |
| 590 |
*/ |
| 591 |
.sample_count = 0, |
| 592 |
|
| 593 |
/* The hardware requires us to have at least one color target, even if |
| 594 |
* it's a dummy. |
| 595 |
*/ |
| 596 |
.rt_count = MAX2(pRenderingInfo->colorAttachmentCount, 1), |
| 597 |
|
| 598 |
.tile_rt_budget_B = |
| 599 |
pan_query_optimal_tib_size(PAN_ARCH, phys_dev->model), |
| 600 |
.tile_z_budget_B = |
| 601 |
pan_query_optimal_z_tib_size(PAN_ARCH, phys_dev->model), |
| 602 |
}; |
| 603 |
/* In case ms2ss is enabled, use the provided sample count. |
| 604 |
* All attachments need to have sample count == 1 or the provided value. |
| 605 |
* But, if all attachments have 1, we would end up choosing the wrong value |
| 606 |
* if we don't set it here already. */ |
| 607 |
render->fb.nr_samples = ms2ss ? ms2ss_info->rasterizationSamples : 1; |
| 608 |
|
| 609 |
assert(pRenderingInfo->colorAttachmentCount <= PAN_MAX_RTS); |
| 610 |
for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) { |
| 611 |
const VkRenderingAttachmentInfo *att = |
| 612 |
&pRenderingInfo->pColorAttachments[i]; |
| 613 |
VK_FROM_HANDLE(panvk_image_view, iview, att->imageView); |
| 614 |
|
| 615 |
if (!iview) |
| 616 |
continue; |
| 617 |
|
| 618 |
render_state_set_color_attachment(cmdbuf, att, i); |
| 619 |
att_width = MIN2(iview->vk.extent.width, att_width); |
| 620 |
att_height = MIN2(iview->vk.extent.height, att_height); |
| 621 |
} |
| 622 |
|
| 623 |
const VkRenderingAttachmentInfo *z_att = NULL, *s_att = NULL; |
| 624 |
if (pRenderingInfo->pDepthAttachment && |
| 625 |
pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE) { |
| 626 |
z_att = pRenderingInfo->pDepthAttachment; |
| 627 |
|
| 628 |
VK_FROM_HANDLE(panvk_image_view, iview, z_att->imageView); |
| 629 |
att_width = MIN2(iview->vk.extent.width, att_width); |
| 630 |
att_height = MIN2(iview->vk.extent.height, att_height); |
| 631 |
} |
| 632 |
|
| 633 |
if (pRenderingInfo->pStencilAttachment && |
| 634 |
pRenderingInfo->pStencilAttachment->imageView != VK_NULL_HANDLE) { |
| 635 |
s_att = pRenderingInfo->pStencilAttachment; |
| 636 |
|
| 637 |
VK_FROM_HANDLE(panvk_image_view, iview, s_att->imageView); |
| 638 |
att_width = MIN2(iview->vk.extent.width, att_width); |
| 639 |
att_height = MIN2(iview->vk.extent.height, att_height); |
| 640 |
} |
| 641 |
|
| 642 |
if (z_att || s_att) |
| 643 |
render_state_set_zs_attachments(cmdbuf, z_att, s_att); |
| 644 |
|
| 645 |
const struct pan_fb_bbox ra_px = |
| 646 |
pan_fb_bbox_from_xywh(pRenderingInfo->renderArea.offset.x, |
| 647 |
pRenderingInfo->renderArea.offset.y, |
| 648 |
pRenderingInfo->renderArea.extent.width, |
| 649 |
pRenderingInfo->renderArea.extent.height); |
| 650 |
|
| 651 |
if (render->bound_attachments) { |
| 652 |
render->fb.layout.width_px = att_width; |
| 653 |
render->fb.layout.height_px = att_height; |
| 654 |
} else { |
| 655 |
render->fb.layout.width_px = (uint32_t)ra_px.max_x + 1; |
| 656 |
render->fb.layout.height_px = (uint32_t)ra_px.max_y + 1; |
| 657 |
} |
| 658 |
assert(render->fb.layout.width_px > 0 && |
| 659 |
render->fb.layout.height_px > 0); |
| 660 |
|
| 661 |
render->fb.layout.render_area_px = ra_px; |
| 662 |
render->fb.layout.tiling_area_px = ra_px; |
| 663 |
|
| 664 |
GENX(pan_align_fb_tiling_area)(&render->fb.layout, &render->fb.store); |
| 665 |
GENX(pan_align_fb_tiling_area)(&render->fb.layout, &render->fb.spill.store); |
| 666 |
|
| 667 |
/* Try to optimize and remove unnecessary resolves if we can */ |
| 668 |
GENX(pan_fb_fold_resolve_into_store)(&render->fb.layout, |
| 669 |
&render->fb.resolve, |
| 670 |
&render->fb.store); |
| 671 |
|
| 672 |
const bool has_partial_tiles = |
| 673 |
pan_fb_has_partial_tiles(&render->fb.layout); |
| 674 |
if (!(pRenderingInfo->flags & VK_RENDERING_RESUMING_BIT) && |
| 675 |
pan_fb_has_image_load(&render->fb.load, has_partial_tiles)) { |
| 676 |
/* Loads happen through the texture unit so, if we're going to do a |
| 677 |
* load, we need a barrier to ensure that the texture cache gets |
| 678 |
* invalidated prior to the load. |
| 679 |
*/ |
| 680 |
const VkMemoryBarrier2 mem_barrier = { |
| 681 |
.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, |
| 682 |
.srcStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | |
| 683 |
VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT | |
| 684 |
VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 685 |
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | |
| 686 |
VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 687 |
.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, |
| 688 |
.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT, |
| 689 |
}; |
| 690 |
const VkDependencyInfo dep_info = { |
| 691 |
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, |
| 692 |
.memoryBarrierCount = 1, |
| 693 |
.pMemoryBarriers = &mem_barrier, |
| 694 |
}; |
| 695 |
panvk_per_arch(CmdPipelineBarrier2)(panvk_cmd_buffer_to_handle(cmdbuf), |
| 696 |
&dep_info); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
void |
| 701 |
panvk_per_arch(cmd_select_tile_size)(struct panvk_cmd_buffer *cmdbuf) |
| 702 |
{ |
| 703 |
struct panvk_rendering_state *render = &cmdbuf->state.gfx.render; |
| 704 |
struct pan_fb_layout *fb = &render->fb.layout; |
| 705 |
|
| 706 |
/* In case we never emitted tiler/framebuffer descriptors, we emit the |
| 707 |
* current sample count and compute tile size */ |
| 708 |
if (fb->sample_count == 0) { |
| 709 |
fb->sample_count = render->fb.nr_samples; |
| 710 |
|
| 711 |
GENX(pan_select_fb_tile_size)(fb); |
| 712 |
|
| 713 |
#if PAN_ARCH != 6 |
| 714 |
if (fb->tile_rt_alloc_B > fb->tile_rt_budget_B) { |
| 715 |
vk_perf(VK_LOG_OBJS(&cmdbuf->vk.base), |
| 716 |
"Using too much tile-memory, disabling pipelining"); |
| 717 |
} |
| 718 |
#endif |
| 719 |
} else { |
| 720 |
/* In case we already emitted tiler/framebuffer descriptors, we ensure |
| 721 |
* that the sample count didn't change (this should never happen) */ |
| 722 |
assert(fb->sample_count == render->fb.nr_samples); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
static void |
| 727 |
prepare_iam_sysvals(struct panvk_cmd_buffer *cmdbuf, BITSET_WORD *dirty_sysvals) |
| 728 |
{ |
| 729 |
const struct vk_input_attachment_location_state *ial = |
| 730 |
&cmdbuf->vk.dynamic_graphics_state.ial; |
| 731 |
struct panvk_input_attachment_info iam[INPUT_ATTACHMENT_MAP_SIZE]; |
| 732 |
uint32_t catt_count = |
| 733 |
ial->color_attachment_count == MESA_VK_COLOR_ATTACHMENT_COUNT_UNKNOWN |
| 734 |
? MAX_RTS |
| 735 |
: ial->color_attachment_count; |
| 736 |
|
| 737 |
memset(iam, ~0, sizeof(iam)); |
| 738 |
|
| 739 |
assert(catt_count <= MAX_RTS); |
| 740 |
|
| 741 |
for (uint32_t i = 0; i < catt_count; i++) { |
| 742 |
if (ial->color_map[i] == MESA_VK_ATTACHMENT_UNUSED || |
| 743 |
!(cmdbuf->state.gfx.render.bound_attachments & |
| 744 |
MESA_VK_RP_ATTACHMENT_COLOR_BIT(i))) |
| 745 |
continue; |
| 746 |
|
| 747 |
VkFormat fmt = cmdbuf->state.gfx.render.color_attachments.fmts[i]; |
| 748 |
enum pipe_format pfmt = vk_format_to_pipe_format(fmt); |
| 749 |
struct mali_internal_conversion_packed conv; |
| 750 |
uint32_t ia_idx = ial->color_map[i] + 1; |
| 751 |
assert(ia_idx < ARRAY_SIZE(iam)); |
| 752 |
|
| 753 |
iam[ia_idx].target = PANVK_COLOR_ATTACHMENT(i); |
| 754 |
|
| 755 |
bool dithered = cmdbuf->state.gfx.render.flags & |
| 756 |
VK_RENDERING_ENABLE_LEGACY_DITHERING_BIT_EXT; |
| 757 |
|
| 758 |
pan_pack(&conv, INTERNAL_CONVERSION, cfg) { |
| 759 |
cfg.memory_format = |
| 760 |
GENX(pan_dithered_format_from_pipe_format)(pfmt, dithered); |
| 761 |
#if PAN_ARCH < 9 |
| 762 |
cfg.register_format = |
| 763 |
vk_format_is_uint(fmt) ? MALI_REGISTER_FILE_FORMAT_U32 |
| 764 |
: vk_format_is_sint(fmt) ? MALI_REGISTER_FILE_FORMAT_I32 |
| 765 |
: MALI_REGISTER_FILE_FORMAT_F32; |
| 766 |
#endif |
| 767 |
} |
| 768 |
|
| 769 |
iam[ia_idx].conversion = conv.opaque[0]; |
| 770 |
} |
| 771 |
|
| 772 |
if (ial->depth_att != MESA_VK_ATTACHMENT_UNUSED) { |
| 773 |
uint32_t ia_idx = |
| 774 |
ial->depth_att == MESA_VK_ATTACHMENT_NO_INDEX ? 0 : ial->depth_att + 1; |
| 775 |
|
| 776 |
assert(ia_idx < ARRAY_SIZE(iam)); |
| 777 |
iam[ia_idx].target = PANVK_ZS_ATTACHMENT; |
| 778 |
|
| 779 |
#if PAN_ARCH < 9 |
| 780 |
/* On v7, we need to pass the depth format around. If we use a conversion |
| 781 |
* of zero, like we do on v9+, the GPU reports an INVALID_INSTR_ENC. */ |
| 782 |
VkFormat fmt = cmdbuf->state.gfx.render.z_attachment.fmt; |
| 783 |
enum pipe_format pfmt = vk_format_to_pipe_format(fmt); |
| 784 |
struct mali_internal_conversion_packed conv; |
| 785 |
|
| 786 |
pan_pack(&conv, INTERNAL_CONVERSION, cfg) { |
| 787 |
cfg.register_format = MALI_REGISTER_FILE_FORMAT_F32; |
| 788 |
cfg.memory_format = |
| 789 |
GENX(pan_dithered_format_from_pipe_format)(pfmt, false); |
| 790 |
} |
| 791 |
iam[ia_idx].conversion = conv.opaque[0]; |
| 792 |
#endif |
| 793 |
} |
| 794 |
|
| 795 |
if (ial->stencil_att != MESA_VK_ATTACHMENT_UNUSED) { |
| 796 |
uint32_t ia_idx = |
| 797 |
ial->stencil_att == MESA_VK_ATTACHMENT_NO_INDEX ? 0 : ial->stencil_att + 1; |
| 798 |
|
| 799 |
assert(ia_idx < ARRAY_SIZE(iam)); |
| 800 |
iam[ia_idx].target = PANVK_ZS_ATTACHMENT; |
| 801 |
} |
| 802 |
|
| 803 |
for (uint32_t i = 0; i < ARRAY_SIZE(iam); i++) |
| 804 |
set_gfx_sysval(cmdbuf, dirty_sysvals, iam[i], iam[i]); |
| 805 |
} |
| 806 |
|
| 807 |
/* This value has been selected to get |
| 808 |
* dEQP-VK.draw.renderpass.inverted_depth_ranges.nodepthclamp_deltazero passing. |
| 809 |
*/ |
| 810 |
#define MIN_DEPTH_CLIP_RANGE 37.7E-06f |
| 811 |
|
| 812 |
void |
| 813 |
panvk_per_arch(cmd_prepare_draw_sysvals)(struct panvk_cmd_buffer *cmdbuf, |
| 814 |
const struct panvk_draw_info *info) |
| 815 |
{ |
| 816 |
struct vk_color_blend_state *cb = &cmdbuf->vk.dynamic_graphics_state.cb; |
| 817 |
const struct panvk_shader_variant *fs = |
| 818 |
panvk_shader_only_variant(get_fs(cmdbuf)); |
| 819 |
uint32_t noperspective_varyings = fs ? fs->info.varyings.noperspective : 0; |
| 820 |
BITSET_DECLARE(dirty_sysvals, MAX_SYSVAL_FAUS) = {0}; |
| 821 |
|
| 822 |
set_gfx_sysval(cmdbuf, dirty_sysvals, vs.noperspective_varyings, |
| 823 |
noperspective_varyings); |
| 824 |
set_gfx_sysval(cmdbuf, dirty_sysvals, vs.first_vertex, info->vertex.base); |
| 825 |
set_gfx_sysval(cmdbuf, dirty_sysvals, vs.base_instance, info->instance.base); |
| 826 |
|
| 827 |
#if PAN_ARCH < 9 |
| 828 |
set_gfx_sysval(cmdbuf, dirty_sysvals, vs.raw_vertex_offset, |
| 829 |
info->vertex.raw_offset); |
| 830 |
set_gfx_sysval(cmdbuf, dirty_sysvals, layer_id, info->layer_id); |
| 831 |
#endif |
| 832 |
|
| 833 |
if (dyn_gfx_state_dirty(cmdbuf, CB_BLEND_CONSTANTS)) { |
| 834 |
for (unsigned i = 0; i < ARRAY_SIZE(cb->blend_constants); i++) { |
| 835 |
set_gfx_sysval(cmdbuf, dirty_sysvals, blend.constants[i], |
| 836 |
cb->blend_constants[i]); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
for (unsigned i = 0; i < MAX_RTS; i++) { |
| 841 |
set_gfx_sysval(cmdbuf, dirty_sysvals, fs.blend_descs[i], |
| 842 |
cmdbuf->state.gfx.fs.blend_descs[i]); |
| 843 |
} |
| 844 |
|
| 845 |
if (dyn_gfx_state_dirty(cmdbuf, VP_VIEWPORTS) || |
| 846 |
dyn_gfx_state_dirty(cmdbuf, VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE) || |
| 847 |
dyn_gfx_state_dirty(cmdbuf, RS_DEPTH_CLIP_ENABLE) || |
| 848 |
dyn_gfx_state_dirty(cmdbuf, RS_DEPTH_CLAMP_ENABLE)) { |
| 849 |
const struct vk_rasterization_state *rs = |
| 850 |
&cmdbuf->vk.dynamic_graphics_state.rs; |
| 851 |
const struct vk_viewport_state *vp = |
| 852 |
&cmdbuf->vk.dynamic_graphics_state.vp; |
| 853 |
const VkViewport *viewport = &vp->viewports[0]; |
| 854 |
|
| 855 |
/* Doing the viewport transform in the vertex shader and then depth |
| 856 |
* clipping with the viewport depth range gets a similar result to |
| 857 |
* clipping in clip-space, but loses precision when the viewport depth |
| 858 |
* range is very small. When minDepth == maxDepth, this completely |
| 859 |
* flattens the clip-space depth and results in never clipping. |
| 860 |
* |
| 861 |
* To work around this, set a lower limit on depth range when clipping is |
| 862 |
* enabled. This results in slightly incorrect fragment depth values, and |
| 863 |
* doesn't help with the precision loss, but at least clipping isn't |
| 864 |
* completely broken. |
| 865 |
*/ |
| 866 |
float z_min = viewport->minDepth; |
| 867 |
float z_max = viewport->maxDepth; |
| 868 |
if (vk_rasterization_state_depth_clip_enable(rs) && |
| 869 |
fabsf(z_max - z_min) < MIN_DEPTH_CLIP_RANGE) { |
| 870 |
float z_sign = z_min <= z_max ? 1.0f : -1.0f; |
| 871 |
|
| 872 |
float z_center = 0.5f * (z_max + z_min); |
| 873 |
/* Bump offset off-center if necessary, to not go out of range */ |
| 874 |
z_center = CLAMP(z_center, 0.5f * MIN_DEPTH_CLIP_RANGE, |
| 875 |
1.0f - 0.5f * MIN_DEPTH_CLIP_RANGE); |
| 876 |
|
| 877 |
z_min = z_center - 0.5f * z_sign * MIN_DEPTH_CLIP_RANGE; |
| 878 |
z_max = z_center + 0.5f * z_sign * MIN_DEPTH_CLIP_RANGE; |
| 879 |
} |
| 880 |
|
| 881 |
/* Upload the viewport scale. Defined as (px/2, py/2, pz) at the start of |
| 882 |
* section 24.5 ("Controlling the Viewport") of the Vulkan spec. At the |
| 883 |
* end of the section, the spec defines: |
| 884 |
* |
| 885 |
* px = width |
| 886 |
* py = height |
| 887 |
* pz = maxDepth - minDepth if negativeOneToOne is false |
| 888 |
* pz = (maxDepth - minDepth) / 2 if negativeOneToOne is true |
| 889 |
*/ |
| 890 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.scale.x, |
| 891 |
0.5f * viewport->width); |
| 892 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.scale.y, |
| 893 |
0.5f * viewport->height); |
| 894 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.scale.z, |
| 895 |
vp->depth_clip_negative_one_to_one ? |
| 896 |
0.5f * (z_max - z_min) : z_max - z_min); |
| 897 |
|
| 898 |
/* Upload the viewport offset. Defined as (ox, oy, oz) at the start of |
| 899 |
* section 24.5 ("Controlling the Viewport") of the Vulkan spec. At the |
| 900 |
* end of the section, the spec defines: |
| 901 |
* |
| 902 |
* ox = x + width/2 |
| 903 |
* oy = y + height/2 |
| 904 |
* oz = minDepth if negativeOneToOne is false |
| 905 |
* oz = (maxDepth + minDepth) / 2 if negativeOneToOne is true |
| 906 |
*/ |
| 907 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.offset.x, |
| 908 |
(0.5f * viewport->width) + viewport->x); |
| 909 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.offset.y, |
| 910 |
(0.5f * viewport->height) + viewport->y); |
| 911 |
set_gfx_sysval(cmdbuf, dirty_sysvals, viewport.offset.z, |
| 912 |
vp->depth_clip_negative_one_to_one ? |
| 913 |
0.5f * (z_min + z_max) : z_min); |
| 914 |
|
| 915 |
} |
| 916 |
|
| 917 |
if (dyn_gfx_state_dirty(cmdbuf, INPUT_ATTACHMENT_MAP)) |
| 918 |
prepare_iam_sysvals(cmdbuf, dirty_sysvals); |
| 919 |
|
| 920 |
const struct panvk_shader_variant *vs = |
| 921 |
panvk_shader_hw_variant(cmdbuf->state.gfx.vs.shader); |
| 922 |
|
| 923 |
#if PAN_ARCH < 9 |
| 924 |
struct panvk_descriptor_state *desc_state = &cmdbuf->state.gfx.desc_state; |
| 925 |
struct panvk_shader_desc_state *vs_desc_state = &cmdbuf->state.gfx.vs.desc; |
| 926 |
struct panvk_shader_desc_state *fs_desc_state = &cmdbuf->state.gfx.fs.desc; |
| 927 |
|
| 928 |
if (gfx_state_dirty(cmdbuf, DESC_STATE) || gfx_state_dirty(cmdbuf, VS)) { |
| 929 |
set_gfx_sysval(cmdbuf, dirty_sysvals, |
| 930 |
desc.sets[PANVK_DESC_TABLE_VS_DYN_SSBOS], |
| 931 |
vs_desc_state->dyn_ssbos); |
| 932 |
} |
| 933 |
|
| 934 |
if (gfx_state_dirty(cmdbuf, DESC_STATE) || gfx_state_dirty(cmdbuf, FS)) { |
| 935 |
set_gfx_sysval(cmdbuf, dirty_sysvals, |
| 936 |
desc.sets[PANVK_DESC_TABLE_FS_DYN_SSBOS], |
| 937 |
fs_desc_state->dyn_ssbos); |
| 938 |
} |
| 939 |
|
| 940 |
for (uint32_t i = 0; i < MAX_SETS; i++) { |
| 941 |
uint32_t used_set_mask = |
| 942 |
vs->desc_info.used_set_mask | (fs ? fs->desc_info.used_set_mask : 0); |
| 943 |
|
| 944 |
if (used_set_mask & BITFIELD_BIT(i)) { |
| 945 |
set_gfx_sysval(cmdbuf, dirty_sysvals, desc.sets[i], |
| 946 |
desc_state->sets[i]->descs.dev); |
| 947 |
} |
| 948 |
} |
| 949 |
#endif |
| 950 |
|
| 951 |
/* We mask the dirty sysvals by the shader usage, and only flag |
| 952 |
* the push uniforms dirty if those intersect. */ |
| 953 |
BITSET_DECLARE(dirty_shader_sysvals, MAX_SYSVAL_FAUS); |
| 954 |
BITSET_AND(dirty_shader_sysvals, dirty_sysvals, vs->fau.used_sysvals); |
| 955 |
if (!BITSET_IS_EMPTY(dirty_shader_sysvals)) |
| 956 |
gfx_state_set_dirty(cmdbuf, VS_PUSH_UNIFORMS); |
| 957 |
|
| 958 |
if (fs) { |
| 959 |
BITSET_AND(dirty_shader_sysvals, dirty_sysvals, fs->fau.used_sysvals); |
| 960 |
|
| 961 |
/* If blend constants are not read by the blend shader, we can consider |
| 962 |
* they are not read at all, so clear the dirty bits to avoid re-emitting |
| 963 |
* FAUs when we can. */ |
| 964 |
if (!cmdbuf->state.gfx.cb.info.shader_loads_blend_const) |
| 965 |
BITSET_CLEAR_COUNT(dirty_shader_sysvals, 0, 4); |
| 966 |
|
| 967 |
if (!BITSET_IS_EMPTY(dirty_shader_sysvals)) |
| 968 |
gfx_state_set_dirty(cmdbuf, FS_PUSH_UNIFORMS); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
VKAPI_ATTR void VKAPI_CALL |
| 973 |
panvk_per_arch(CmdBindVertexBuffers2)(VkCommandBuffer commandBuffer, |
| 974 |
uint32_t firstBinding, |
| 975 |
uint32_t bindingCount, |
| 976 |
const VkBuffer *pBuffers, |
| 977 |
const VkDeviceSize *pOffsets, |
| 978 |
const VkDeviceSize *pSizes, |
| 979 |
const VkDeviceSize *pStrides) |
| 980 |
{ |
| 981 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmdbuf, commandBuffer); |
| 982 |
|
| 983 |
assert(firstBinding + bindingCount <= MAX_VBS); |
| 984 |
|
| 985 |
if (pStrides) { |
| 986 |
vk_cmd_set_vertex_binding_strides(&cmdbuf->vk, firstBinding, |
| 987 |
bindingCount, pStrides); |
| 988 |
} |
| 989 |
|
| 990 |
for (uint32_t i = 0; i < bindingCount; i++) { |
| 991 |
VK_FROM_HANDLE(panvk_buffer, buffer, pBuffers[i]); |
| 992 |
|
| 993 |
if (buffer) { |
| 994 |
cmdbuf->state.gfx.vb.bufs[firstBinding + i].address = |
| 995 |
panvk_buffer_gpu_ptr(buffer, pOffsets[i]); |
| 996 |
cmdbuf->state.gfx.vb.bufs[firstBinding + i].size = panvk_buffer_range( |
| 997 |
buffer, pOffsets[i], pSizes ? pSizes[i] : VK_WHOLE_SIZE); |
| 998 |
} else { |
| 999 |
cmdbuf->state.gfx.vb.bufs[firstBinding + i].address = 0; |
| 1000 |
cmdbuf->state.gfx.vb.bufs[firstBinding + i].size = 0; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
cmdbuf->state.gfx.vb.count = |
| 1005 |
MAX2(cmdbuf->state.gfx.vb.count, firstBinding + bindingCount); |
| 1006 |
gfx_state_set_dirty(cmdbuf, VB); |
| 1007 |
} |
| 1008 |
|
| 1009 |
VKAPI_ATTR void VKAPI_CALL |
| 1010 |
panvk_per_arch(CmdBindIndexBuffer2)(VkCommandBuffer commandBuffer, |
| 1011 |
VkBuffer buffer, VkDeviceSize offset, |
| 1012 |
VkDeviceSize size, VkIndexType indexType) |
| 1013 |
{ |
| 1014 |
VK_FROM_HANDLE(panvk_cmd_buffer, cmdbuf, commandBuffer); |
| 1015 |
VK_FROM_HANDLE(panvk_buffer, buf, buffer); |
| 1016 |
|
| 1017 |
if (buf) { |
| 1018 |
cmdbuf->state.gfx.ib.size = panvk_buffer_range(buf, offset, size); |
| 1019 |
assert(cmdbuf->state.gfx.ib.size <= UINT32_MAX); |
| 1020 |
cmdbuf->state.gfx.ib.dev_addr = panvk_buffer_gpu_ptr(buf, offset); |
| 1021 |
} else { |
| 1022 |
cmdbuf->state.gfx.ib.size = 0; |
| 1023 |
/* In case of NullDescriptors, we need to set a non-NULL address and rely |
| 1024 |
* on out-of-bounds behavior against the zero size of the buffer. Note |
| 1025 |
* that this only works for v10+, as v9 does not have a way to specify the |
| 1026 |
* index buffer size. */ |
| 1027 |
cmdbuf->state.gfx.ib.dev_addr = PAN_ARCH >= 10 ? 0x1000 : 0; |
| 1028 |
} |
| 1029 |
cmdbuf->state.gfx.ib.index_size = vk_index_type_to_bytes(indexType); |
| 1030 |
|
| 1031 |
gfx_state_set_dirty(cmdbuf, IB); |
| 1032 |
} |