| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include <assert.h> |
| 8 |
#include "agx_abi.h" |
| 9 |
#include "agx_bg_eot.h" |
| 10 |
#include "agx_bo.h" |
| 11 |
#include "agx_compile.h" |
| 12 |
#include "agx_compiler.h" |
| 13 |
#include "agx_device.h" |
| 14 |
#include "agx_helpers.h" |
| 15 |
#include "agx_linker.h" |
| 16 |
#include "agx_nir_lower_vbo.h" |
| 17 |
#include "agx_ppp.h" |
| 18 |
#include "agx_tilebuffer.h" |
| 19 |
#include "agx_usc.h" |
| 20 |
#include "agx_uvs.h" |
| 21 |
#include "hk_buffer.h" |
| 22 |
#include "hk_cmd_buffer.h" |
| 23 |
#include "hk_device.h" |
| 24 |
#include "hk_entrypoints.h" |
| 25 |
#include "hk_image.h" |
| 26 |
#include "hk_image_view.h" |
| 27 |
#include "hk_physical_device.h" |
| 28 |
#include "hk_private.h" |
| 29 |
#include "hk_shader.h" |
| 30 |
|
| 31 |
#include "asahi/genxml/agx_pack.h" |
| 32 |
#include "asahi/libagx/compression.h" |
| 33 |
#include "asahi/libagx/libagx.h" |
| 34 |
#include "asahi/libagx/query.h" |
| 35 |
#include "asahi/libagx/tessellator.h" |
| 36 |
#include "poly/geometry.h" |
| 37 |
#include "util/blend.h" |
| 38 |
#include "util/format/format_utils.h" |
| 39 |
#include "util/format/u_formats.h" |
| 40 |
#include "util/macros.h" |
| 41 |
#include "util/ralloc.h" |
| 42 |
#include "util/u_prim.h" |
| 43 |
#include "vulkan/vulkan_core.h" |
| 44 |
#include "layout.h" |
| 45 |
#include "libagx_dgc.h" |
| 46 |
#include "libagx_shaders.h" |
| 47 |
#include "nir.h" |
| 48 |
#include "nir_builder.h" |
| 49 |
#include "nir_intrinsics.h" |
| 50 |
#include "nir_lower_blend.h" |
| 51 |
#include "nir_xfb_info.h" |
| 52 |
#include "pool.h" |
| 53 |
#include "shader_enums.h" |
| 54 |
#include "vk_blend.h" |
| 55 |
#include "vk_enum_to_str.h" |
| 56 |
#include "vk_format.h" |
| 57 |
#include "vk_graphics_state.h" |
| 58 |
#include "vk_pipeline.h" |
| 59 |
#include "vk_render_pass.h" |
| 60 |
#include "vk_shader.h" |
| 61 |
#include "vk_standard_sample_locations.h" |
| 62 |
#include "vk_util.h" |
| 63 |
|
| 64 |
#define IS_DIRTY(bit) BITSET_TEST(dyn->dirty, MESA_VK_DYNAMIC_##bit) |
| 65 |
|
| 66 |
#define IS_SHADER_DIRTY(bit) \ |
| 67 |
(cmd->state.gfx.shaders_dirty & BITFIELD_BIT(MESA_SHADER_##bit)) |
| 68 |
|
| 69 |
#define IS_LINKED_DIRTY(bit) \ |
| 70 |
(cmd->state.gfx.linked_dirty & BITFIELD_BIT(MESA_SHADER_##bit)) |
| 71 |
|
| 72 |
/* CTS coverage of indirect draws is pretty bad, so it's helpful to be able to |
| 73 |
* get some extra smoke testing. |
| 74 |
*/ |
| 75 |
#define HK_TEST_INDIRECTS (0) |
| 76 |
|
| 77 |
UNUSED static inline void |
| 78 |
print_draw(struct agx_draw d, FILE *fp) |
| 79 |
{ |
| 80 |
if (agx_is_indirect(d.b)) |
| 81 |
fprintf(fp, "indirect (buffer %" PRIx64 "):", d.b.ptr); |
| 82 |
else |
| 83 |
fprintf(fp, "direct (%ux%u):", d.b.count[0], d.b.count[1]); |
| 84 |
|
| 85 |
if (d.index_size) |
| 86 |
fprintf(fp, " index_size=%u", agx_index_size_to_B(d.index_size)); |
| 87 |
else |
| 88 |
fprintf(fp, " non-indexed"); |
| 89 |
|
| 90 |
if (d.restart) |
| 91 |
fprintf(fp, " restart"); |
| 92 |
|
| 93 |
if (d.index_bias) |
| 94 |
fprintf(fp, " index_bias=%u", d.index_bias); |
| 95 |
|
| 96 |
if (d.start) |
| 97 |
fprintf(fp, " start=%u", d.start); |
| 98 |
|
| 99 |
if (d.start_instance) |
| 100 |
fprintf(fp, " start_instance=%u", d.start_instance); |
| 101 |
|
| 102 |
fprintf(fp, "\n"); |
| 103 |
} |
| 104 |
|
| 105 |
static bool |
| 106 |
hk_rast_discard(struct hk_cmd_buffer *cmd) |
| 107 |
{ |
| 108 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 109 |
|
| 110 |
/* A non-zero rasterization stream acts as a rasterizer discard unless |
| 111 |
* there's a multistream geometry shader bound. |
| 112 |
*/ |
| 113 |
if (dyn->rs.rasterization_stream != 0) { |
| 114 |
struct hk_api_shader *gs = cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY]; |
| 115 |
return !gs || !gs->variants[HK_GS_VARIANT_COUNT].info.gs.multistream; |
| 116 |
} |
| 117 |
|
| 118 |
return dyn->rs.rasterizer_discard_enable; |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
hk_cmd_buffer_dirty_render_pass(struct hk_cmd_buffer *cmd) |
| 123 |
{ |
| 124 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 125 |
|
| 126 |
/* These depend on color attachment count */ |
| 127 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_COLOR_WRITE_ENABLES); |
| 128 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_BLEND_ENABLES); |
| 129 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_BLEND_EQUATIONS); |
| 130 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_WRITE_MASKS); |
| 131 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_CB_BLEND_ADVANCED); |
| 132 |
|
| 133 |
/* These depend on the depth/stencil format */ |
| 134 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_TEST_ENABLE); |
| 135 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_WRITE_ENABLE); |
| 136 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_ENABLE); |
| 137 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_DS_STENCIL_TEST_ENABLE); |
| 138 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_FACTORS); |
| 139 |
|
| 140 |
/* This may depend on render targets for ESO */ |
| 141 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_MS_RASTERIZATION_SAMPLES); |
| 142 |
|
| 143 |
/* This may depend on render targets */ |
| 144 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_COLOR_ATTACHMENT_MAP); |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
hk_cmd_buffer_begin_graphics(struct hk_cmd_buffer *cmd, |
| 149 |
const VkCommandBufferBeginInfo *pBeginInfo) |
| 150 |
{ |
| 151 |
if (cmd->vk.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY && |
| 152 |
(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) { |
| 153 |
char gcbiar_data[VK_GCBIARR_DATA_SIZE(HK_MAX_RTS)]; |
| 154 |
const VkRenderingInfo *resume_info = |
| 155 |
vk_get_command_buffer_inheritance_as_rendering_resume( |
| 156 |
cmd->vk.level, pBeginInfo, gcbiar_data); |
| 157 |
if (resume_info) { |
| 158 |
hk_CmdBeginRendering(hk_cmd_buffer_to_handle(cmd), resume_info); |
| 159 |
} else { |
| 160 |
const VkCommandBufferInheritanceRenderingInfo *inheritance_info = |
| 161 |
vk_get_command_buffer_inheritance_rendering_info(cmd->vk.level, |
| 162 |
pBeginInfo); |
| 163 |
assert(inheritance_info); |
| 164 |
|
| 165 |
struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 166 |
render->flags = inheritance_info->flags; |
| 167 |
render->area = (VkRect2D){}; |
| 168 |
render->layer_count = 0; |
| 169 |
render->view_mask = inheritance_info->viewMask; |
| 170 |
render->tilebuffer.nr_samples = inheritance_info->rasterizationSamples; |
| 171 |
|
| 172 |
render->color_att_count = inheritance_info->colorAttachmentCount; |
| 173 |
for (uint32_t i = 0; i < render->color_att_count; i++) { |
| 174 |
render->color_att[i].vk_format = |
| 175 |
inheritance_info->pColorAttachmentFormats[i]; |
| 176 |
} |
| 177 |
render->depth_att.vk_format = inheritance_info->depthAttachmentFormat; |
| 178 |
render->stencil_att.vk_format = |
| 179 |
inheritance_info->stencilAttachmentFormat; |
| 180 |
|
| 181 |
const VkRenderingAttachmentLocationInfoKHR att_loc_info_default = { |
| 182 |
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_LOCATION_INFO_KHR, |
| 183 |
.colorAttachmentCount = inheritance_info->colorAttachmentCount, |
| 184 |
}; |
| 185 |
const VkRenderingAttachmentLocationInfoKHR *att_loc_info = |
| 186 |
vk_get_command_buffer_rendering_attachment_location_info( |
| 187 |
cmd->vk.level, pBeginInfo); |
| 188 |
if (att_loc_info == NULL) |
| 189 |
att_loc_info = &att_loc_info_default; |
| 190 |
|
| 191 |
vk_cmd_set_rendering_attachment_locations(&cmd->vk, att_loc_info); |
| 192 |
|
| 193 |
hk_cmd_buffer_dirty_render_pass(cmd); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
hk_cmd_buffer_dirty_all(cmd); |
| 198 |
|
| 199 |
/* If multiview is disabled, always read 0. If multiview is enabled, |
| 200 |
* hk_set_view_index will dirty the root each draw. |
| 201 |
*/ |
| 202 |
cmd->state.gfx.descriptors.root.draw.view_index = 0; |
| 203 |
cmd->state.gfx.descriptors.root_dirty = true; |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
hk_cmd_invalidate_graphics_state(struct hk_cmd_buffer *cmd) |
| 208 |
{ |
| 209 |
hk_cmd_buffer_dirty_all(cmd); |
| 210 |
|
| 211 |
/* From the Vulkan 1.3.275 spec: |
| 212 |
* |
| 213 |
* "...There is one exception to this rule - if the primary command |
| 214 |
* buffer is inside a render pass instance, then the render pass and |
| 215 |
* subpass state is not disturbed by executing secondary command |
| 216 |
* buffers." |
| 217 |
* |
| 218 |
* We need to reset everything EXCEPT the render pass state. |
| 219 |
*/ |
| 220 |
struct hk_rendering_state render_save = cmd->state.gfx.render; |
| 221 |
memset(&cmd->state.gfx, 0, sizeof(cmd->state.gfx)); |
| 222 |
cmd->state.gfx.render = render_save; |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
hk_attachment_init(struct hk_attachment *att, |
| 227 |
const VkRenderingAttachmentInfo *info) |
| 228 |
{ |
| 229 |
if (info == NULL || info->imageView == VK_NULL_HANDLE) { |
| 230 |
*att = (struct hk_attachment){ |
| 231 |
.iview = NULL, |
| 232 |
}; |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
VK_FROM_HANDLE(hk_image_view, iview, info->imageView); |
| 237 |
*att = (struct hk_attachment){ |
| 238 |
.vk_format = iview->vk.format, |
| 239 |
.iview = iview, |
| 240 |
}; |
| 241 |
|
| 242 |
if (info->resolveMode != VK_RESOLVE_MODE_NONE) { |
| 243 |
VK_FROM_HANDLE(hk_image_view, res_iview, info->resolveImageView); |
| 244 |
att->resolve_mode = info->resolveMode; |
| 245 |
att->resolve_iview = res_iview; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
VKAPI_ATTR void VKAPI_CALL |
| 250 |
hk_GetRenderingAreaGranularityKHR( |
| 251 |
VkDevice device, const VkRenderingAreaInfoKHR *pRenderingAreaInfo, |
| 252 |
VkExtent2D *pGranularity) |
| 253 |
{ |
| 254 |
*pGranularity = (VkExtent2D){.width = 1, .height = 1}; |
| 255 |
} |
| 256 |
|
| 257 |
static bool |
| 258 |
is_attachment_stored(const VkRenderingAttachmentInfo *att) |
| 259 |
{ |
| 260 |
/* When resolving, we store the intermediate multisampled image as the |
| 261 |
* resolve is a separate control stream. This could be optimized. |
| 262 |
*/ |
| 263 |
return att->storeOp == VK_ATTACHMENT_STORE_OP_STORE || |
| 264 |
att->resolveMode != VK_RESOLVE_MODE_NONE; |
| 265 |
} |
| 266 |
|
| 267 |
static struct hk_bg_eot |
| 268 |
hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, |
| 269 |
bool store, bool partial_render, bool incomplete_render_area) |
| 270 |
{ |
| 271 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 272 |
struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 273 |
|
| 274 |
/* Construct the key */ |
| 275 |
struct agx_bg_eot_key key = {.tib = render->tilebuffer}; |
| 276 |
static_assert(AGX_BG_EOT_NONE == 0, "default initializer"); |
| 277 |
|
| 278 |
key.tib.layered = (render->cr.layers > 1); |
| 279 |
|
| 280 |
bool needs_textures_for_spilled_rts = |
| 281 |
agx_tilebuffer_spills(&render->tilebuffer) && !partial_render && !store; |
| 282 |
|
| 283 |
for (unsigned i = 0; i < info->colorAttachmentCount; ++i) { |
| 284 |
const VkRenderingAttachmentInfo *att_info = &info->pColorAttachments[i]; |
| 285 |
if (att_info->imageView == VK_NULL_HANDLE) |
| 286 |
continue; |
| 287 |
|
| 288 |
/* Partial render programs exist only to store/load the tilebuffer to |
| 289 |
* main memory. When render targets are already spilled to main memory, |
| 290 |
* there's nothing to do. |
| 291 |
*/ |
| 292 |
if (key.tib.spilled[i] && (partial_render || store)) |
| 293 |
continue; |
| 294 |
|
| 295 |
if (store) { |
| 296 |
bool should_store = is_attachment_stored(att_info); |
| 297 |
|
| 298 |
/* Partial renders always need to flush to memory. */ |
| 299 |
should_store |= partial_render; |
| 300 |
|
| 301 |
if (should_store) |
| 302 |
key.op[i] = AGX_EOT_STORE; |
| 303 |
} else { |
| 304 |
bool load = att_info->loadOp == VK_ATTACHMENT_LOAD_OP_LOAD; |
| 305 |
bool clear = att_info->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 306 |
|
| 307 |
/* The background program used for partial renders must always load |
| 308 |
* whatever was stored in the mid-frame end-of-tile program. |
| 309 |
*/ |
| 310 |
load |= partial_render; |
| 311 |
|
| 312 |
/* With an incomplete render area, we're forced to load back tiles and |
| 313 |
* then use the 3D pipe for the clear. |
| 314 |
*/ |
| 315 |
load |= incomplete_render_area; |
| 316 |
|
| 317 |
/* Don't read back spilled render targets, they're already in memory */ |
| 318 |
load &= !key.tib.spilled[i]; |
| 319 |
|
| 320 |
/* This is a very frustrating corner case. From the spec: |
| 321 |
* |
| 322 |
* VK_ATTACHMENT_STORE_OP_NONE specifies the contents within the |
| 323 |
* render area are not accessed by the store operation as long as |
| 324 |
* no values are written to the attachment during the render pass. |
| 325 |
* |
| 326 |
* With VK_ATTACHMENT_STORE_OP_NONE, we suppress stores on the main |
| 327 |
* end-of-tile program. Unfortunately, that's not enough: we also need |
| 328 |
* to preserve the contents throughout partial renders. The easiest way |
| 329 |
* to do that is forcing a load in the background program, so that |
| 330 |
* partial stores for unused attachments will be no-op'd by writing |
| 331 |
* existing contents. |
| 332 |
* |
| 333 |
* Optimizing this would require nontrivial tracking. Fortunately, |
| 334 |
* this is all Android gunk and we don't have to care too much for |
| 335 |
* dekstop games. So do the simple thing. |
| 336 |
*/ |
| 337 |
bool no_store = (att_info->storeOp == VK_ATTACHMENT_STORE_OP_NONE); |
| 338 |
bool no_store_wa = no_store && !load && !clear; |
| 339 |
if (no_store_wa) { |
| 340 |
perf_debug(cmd, "STORE_OP_NONE workaround"); |
| 341 |
} |
| 342 |
|
| 343 |
load |= no_store_wa; |
| 344 |
|
| 345 |
/* Don't apply clears for spilled render targets when we clear the |
| 346 |
* render area explicitly after. |
| 347 |
*/ |
| 348 |
if (key.tib.spilled[i] && incomplete_render_area) |
| 349 |
continue; |
| 350 |
|
| 351 |
if (load) |
| 352 |
key.op[i] = AGX_BG_LOAD; |
| 353 |
else if (clear) |
| 354 |
key.op[i] = AGX_BG_CLEAR; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* Begin building the pipeline */ |
| 359 |
size_t usc_size = agx_usc_size(3 + HK_MAX_RTS); |
| 360 |
struct agx_ptr t = hk_pool_usc_alloc(cmd, usc_size, 64); |
| 361 |
if (!t.cpu) |
| 362 |
return (struct hk_bg_eot){.usc = t.gpu}; |
| 363 |
|
| 364 |
struct agx_usc_builder b = agx_usc_builder(t.cpu, usc_size); |
| 365 |
|
| 366 |
bool uses_txf = false; |
| 367 |
unsigned uniforms = 0; |
| 368 |
unsigned nr_tex = 0; |
| 369 |
|
| 370 |
for (unsigned rt = 0; rt < HK_MAX_RTS; ++rt) { |
| 371 |
const VkRenderingAttachmentInfo *att_info = &info->pColorAttachments[rt]; |
| 372 |
struct hk_image_view *iview = render->color_att[rt].iview; |
| 373 |
|
| 374 |
if (key.op[rt] == AGX_BG_LOAD) { |
| 375 |
uses_txf = true; |
| 376 |
|
| 377 |
const struct agx_texture_packed *desc = |
| 378 |
key.tib.layered ? &iview->planes[0].layered_background |
| 379 |
: &iview->planes[0].background; |
| 380 |
|
| 381 |
agx_usc_pack(&b, TEXTURE, cfg) { |
| 382 |
/* Shifted to match eMRT indexing, could be optimized */ |
| 383 |
cfg.start = rt * 2; |
| 384 |
cfg.count = 1; |
| 385 |
cfg.buffer = hk_pool_upload(cmd, desc, sizeof(*desc), 8); |
| 386 |
} |
| 387 |
|
| 388 |
nr_tex = (rt * 2) + 1; |
| 389 |
} else if (key.op[rt] == AGX_BG_CLEAR) { |
| 390 |
static_assert(sizeof(att_info->clearValue.color) == 16, "fixed ABI"); |
| 391 |
uint64_t colour = |
| 392 |
hk_pool_upload(cmd, &att_info->clearValue.color, 16, 16); |
| 393 |
|
| 394 |
agx_usc_uniform(&b, 4 + (8 * rt), 8, colour); |
| 395 |
uniforms = MAX2(uniforms, 4 + (8 * rt) + 8); |
| 396 |
} else if (key.op[rt] == AGX_EOT_STORE) { |
| 397 |
const struct agx_pbe_packed *desc = key.tib.layered |
| 398 |
? &iview->planes[0].layered_eot |
| 399 |
: &iview->planes[0].eot; |
| 400 |
|
| 401 |
agx_usc_pack(&b, TEXTURE, cfg) { |
| 402 |
cfg.start = rt; |
| 403 |
cfg.count = 1; |
| 404 |
cfg.buffer = hk_pool_upload(cmd, desc, sizeof(*desc), 8); |
| 405 |
} |
| 406 |
|
| 407 |
nr_tex = rt + 1; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if (needs_textures_for_spilled_rts) { |
| 412 |
hk_usc_upload_spilled_rt_descs(&b, cmd); |
| 413 |
uniforms = MAX2(uniforms, 4); |
| 414 |
} |
| 415 |
|
| 416 |
if (uses_txf) { |
| 417 |
agx_usc_push_packed(&b, SAMPLER, dev->dev.txf_sampler); |
| 418 |
} |
| 419 |
|
| 420 |
/* For attachmentless rendering, we don't know the sample count until |
| 421 |
* draw-time. But we have trivial bg/eot programs in that case too. |
| 422 |
*/ |
| 423 |
if (key.tib.nr_samples >= 1) { |
| 424 |
agx_usc_push_packed(&b, SHARED, &key.tib.usc); |
| 425 |
} else { |
| 426 |
assert(key.tib.sample_size_B == 0); |
| 427 |
agx_usc_shared_none(&b); |
| 428 |
|
| 429 |
key.tib.nr_samples = 1; |
| 430 |
} |
| 431 |
|
| 432 |
/* Get the shader */ |
| 433 |
key.reserved_preamble = uniforms; |
| 434 |
/* XXX: locking? */ |
| 435 |
struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader(&dev->bg_eot, &key); |
| 436 |
|
| 437 |
agx_usc_pack(&b, SHADER, cfg) { |
| 438 |
cfg.code = agx_usc_addr(&dev->dev, shader->ptr); |
| 439 |
cfg.unk_2 = 0; |
| 440 |
} |
| 441 |
|
| 442 |
agx_usc_pack(&b, REGISTERS, cfg) |
| 443 |
cfg.register_count = shader->info.nr_gprs; |
| 444 |
|
| 445 |
if (shader->info.has_preamble) { |
| 446 |
agx_usc_pack(&b, PRESHADER, cfg) { |
| 447 |
cfg.code = |
| 448 |
agx_usc_addr(&dev->dev, shader->ptr + shader->info.preamble_offset); |
| 449 |
} |
| 450 |
} else { |
| 451 |
agx_usc_pack(&b, NO_PRESHADER, cfg) |
| 452 |
; |
| 453 |
} |
| 454 |
|
| 455 |
struct hk_bg_eot ret = {.usc = t.gpu}; |
| 456 |
|
| 457 |
agx_pack(&ret.counts, COUNTS, cfg) { |
| 458 |
cfg.uniform_register_count = shader->info.push_count; |
| 459 |
cfg.preshader_register_count = shader->info.nr_preamble_gprs; |
| 460 |
cfg.texture_state_register_count = nr_tex; |
| 461 |
cfg.sampler_state_register_count = |
| 462 |
agx_translate_sampler_state_count(uses_txf ? 1 : 0, false); |
| 463 |
} |
| 464 |
|
| 465 |
return ret; |
| 466 |
} |
| 467 |
|
| 468 |
static void |
| 469 |
hk_merge_render_iview(struct hk_rendering_state *render, |
| 470 |
struct hk_image_view *iview, bool zls) |
| 471 |
{ |
| 472 |
if (iview) { |
| 473 |
unsigned samples = iview->vk.image->samples; |
| 474 |
/* TODO: is this right for ycbcr? */ |
| 475 |
unsigned level = iview->vk.base_mip_level; |
| 476 |
unsigned width = u_minify(iview->vk.image->extent.width, level); |
| 477 |
unsigned height = u_minify(iview->vk.image->extent.height, level); |
| 478 |
|
| 479 |
assert(render->tilebuffer.nr_samples == 0 || |
| 480 |
render->tilebuffer.nr_samples == samples); |
| 481 |
render->tilebuffer.nr_samples = samples; |
| 482 |
|
| 483 |
/* TODO: Is this merging logic sound? Not sure how this is supposed to |
| 484 |
* work conceptually. |
| 485 |
*/ |
| 486 |
render->cr.width = MAX2(render->cr.width, width); |
| 487 |
render->cr.height = MAX2(render->cr.height, height); |
| 488 |
|
| 489 |
if (zls) { |
| 490 |
render->cr.zls_width = width; |
| 491 |
render->cr.zls_height = height; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
static void |
| 497 |
hk_pack_zls_control(struct agx_zls_control_packed *packed, |
| 498 |
struct ail_layout *z_layout, struct ail_layout *s_layout, |
| 499 |
const VkRenderingAttachmentInfo *z, |
| 500 |
const VkRenderingAttachmentInfo *s, |
| 501 |
bool incomplete_render_area, bool partial_render) |
| 502 |
{ |
| 503 |
struct agx_zls zls = {0}; |
| 504 |
|
| 505 |
if (z) { |
| 506 |
/* XXX: Dropping Z stores is wrong if the render pass gets split into |
| 507 |
* multiple control streams (can that ever happen?) We need more ZLS |
| 508 |
* variants. Force || true for now. |
| 509 |
*/ |
| 510 |
zls.z_store = z->storeOp == VK_ATTACHMENT_STORE_OP_STORE || |
| 511 |
z->resolveMode != VK_RESOLVE_MODE_NONE || partial_render || |
| 512 |
true; |
| 513 |
|
| 514 |
zls.z_load = z->loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || partial_render || |
| 515 |
incomplete_render_area; |
| 516 |
} |
| 517 |
|
| 518 |
if (s) { |
| 519 |
/* TODO: |
| 520 |
* Fail |
| 521 |
* dEQP-VK.renderpass.dedicated_allocation.formats.d32_sfloat_s8_uint.input.dont_care.store.self_dep_clear_draw_use_input_aspect |
| 522 |
* without the force |
| 523 |
* .. maybe a VkRenderPass emulation bug. |
| 524 |
*/ |
| 525 |
zls.s_store = s->storeOp == VK_ATTACHMENT_STORE_OP_STORE || |
| 526 |
s->resolveMode != VK_RESOLVE_MODE_NONE || partial_render || |
| 527 |
true; |
| 528 |
|
| 529 |
zls.s_load = s->loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || partial_render || |
| 530 |
incomplete_render_area; |
| 531 |
} |
| 532 |
|
| 533 |
agx_pack_zls_control(packed, z_layout, s_layout, &zls); |
| 534 |
} |
| 535 |
|
| 536 |
VKAPI_ATTR void VKAPI_CALL |
| 537 |
hk_CmdBeginRendering(VkCommandBuffer commandBuffer, |
| 538 |
const VkRenderingInfo *pRenderingInfo) |
| 539 |
{ |
| 540 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 541 |
struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 542 |
|
| 543 |
memset(render, 0, sizeof(*render)); |
| 544 |
|
| 545 |
render->flags = pRenderingInfo->flags; |
| 546 |
render->area = pRenderingInfo->renderArea; |
| 547 |
render->view_mask = pRenderingInfo->viewMask; |
| 548 |
render->layer_count = pRenderingInfo->layerCount; |
| 549 |
render->tilebuffer.nr_samples = 0; |
| 550 |
|
| 551 |
const uint32_t layer_count = render->view_mask |
| 552 |
? util_last_bit(render->view_mask) |
| 553 |
: render->layer_count; |
| 554 |
|
| 555 |
render->color_att_count = pRenderingInfo->colorAttachmentCount; |
| 556 |
for (uint32_t i = 0; i < render->color_att_count; i++) { |
| 557 |
hk_attachment_init(&render->color_att[i], |
| 558 |
&pRenderingInfo->pColorAttachments[i]); |
| 559 |
} |
| 560 |
|
| 561 |
hk_attachment_init(&render->depth_att, pRenderingInfo->pDepthAttachment); |
| 562 |
hk_attachment_init(&render->stencil_att, pRenderingInfo->pStencilAttachment); |
| 563 |
|
| 564 |
for (uint32_t i = 0; i < render->color_att_count; i++) { |
| 565 |
hk_merge_render_iview(render, render->color_att[i].iview, false); |
| 566 |
} |
| 567 |
|
| 568 |
hk_merge_render_iview( |
| 569 |
render, render->depth_att.iview ?: render->stencil_att.iview, true); |
| 570 |
|
| 571 |
/* Infer for attachmentless. samples is inferred at draw-time. */ |
| 572 |
render->cr.width = |
| 573 |
MAX2(render->cr.width, render->area.offset.x + render->area.extent.width); |
| 574 |
|
| 575 |
render->cr.height = MAX2(render->cr.height, |
| 576 |
render->area.offset.y + render->area.extent.height); |
| 577 |
|
| 578 |
if (!render->cr.zls_width) { |
| 579 |
render->cr.zls_width = render->cr.width; |
| 580 |
render->cr.zls_height = render->cr.height; |
| 581 |
} |
| 582 |
|
| 583 |
render->cr.layers = layer_count; |
| 584 |
|
| 585 |
/* Choose a tilebuffer layout given the framebuffer key */ |
| 586 |
enum pipe_format formats[HK_MAX_RTS] = {0}; |
| 587 |
for (unsigned i = 0; i < render->color_att_count; ++i) { |
| 588 |
formats[i] = hk_format_to_pipe_format(render->color_att[i].vk_format); |
| 589 |
} |
| 590 |
|
| 591 |
/* For now, we force layered=true since it makes compatibility problems way |
| 592 |
* easier. |
| 593 |
*/ |
| 594 |
render->tilebuffer = agx_build_tilebuffer_layout( |
| 595 |
formats, render->color_att_count, render->tilebuffer.nr_samples, true); |
| 596 |
|
| 597 |
const VkRenderingAttachmentLocationInfoKHR ral_info = { |
| 598 |
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_LOCATION_INFO_KHR, |
| 599 |
.colorAttachmentCount = pRenderingInfo->colorAttachmentCount, |
| 600 |
}; |
| 601 |
vk_cmd_set_rendering_attachment_locations(&cmd->vk, &ral_info); |
| 602 |
|
| 603 |
hk_cmd_buffer_dirty_render_pass(cmd); |
| 604 |
|
| 605 |
/* Determine whether the render area is complete, enabling us to use a |
| 606 |
* fast-clear. |
| 607 |
* |
| 608 |
* TODO: If it is incomplete but tile aligned, it should be possibly to fast |
| 609 |
* clear with the appropriate settings. This is critical for performance. |
| 610 |
*/ |
| 611 |
bool incomplete_render_area = |
| 612 |
render->area.offset.x > 0 || render->area.offset.y > 0 || |
| 613 |
render->area.extent.width < render->cr.width || |
| 614 |
render->area.extent.height < render->cr.height || |
| 615 |
(render->view_mask && |
| 616 |
render->view_mask != BITFIELD64_MASK(render->cr.layers)); |
| 617 |
|
| 618 |
perf_debug(cmd, "Rendering %ux%ux%u@%u %s%s", render->cr.width, |
| 619 |
render->cr.height, render->cr.layers, |
| 620 |
render->tilebuffer.nr_samples, |
| 621 |
render->view_mask ? " multiview" : "", |
| 622 |
incomplete_render_area ? " incomplete" : ""); |
| 623 |
|
| 624 |
render->cr.bg.main = hk_build_bg_eot(cmd, pRenderingInfo, false, false, |
| 625 |
incomplete_render_area); |
| 626 |
render->cr.bg.partial = |
| 627 |
hk_build_bg_eot(cmd, pRenderingInfo, false, true, incomplete_render_area); |
| 628 |
|
| 629 |
render->cr.eot.main = |
| 630 |
hk_build_bg_eot(cmd, pRenderingInfo, true, false, incomplete_render_area); |
| 631 |
render->cr.eot.partial = |
| 632 |
hk_build_bg_eot(cmd, pRenderingInfo, true, true, incomplete_render_area); |
| 633 |
|
| 634 |
render->cr.isp_bgobjvals = 0x300; |
| 635 |
|
| 636 |
const VkRenderingAttachmentInfo *attach_z = pRenderingInfo->pDepthAttachment; |
| 637 |
const VkRenderingAttachmentInfo *attach_s = |
| 638 |
pRenderingInfo->pStencilAttachment; |
| 639 |
|
| 640 |
struct ail_layout *z_layout = NULL, *s_layout = NULL; |
| 641 |
|
| 642 |
if (attach_z != NULL && attach_z != VK_NULL_HANDLE && attach_z->imageView) { |
| 643 |
struct hk_image_view *view = render->depth_att.iview; |
| 644 |
struct hk_image *image = |
| 645 |
container_of(view->vk.image, struct hk_image, vk); |
| 646 |
|
| 647 |
z_layout = &image->planes[0].layout; |
| 648 |
|
| 649 |
unsigned level = view->vk.base_mip_level; |
| 650 |
unsigned first_layer = view->vk.base_array_layer; |
| 651 |
|
| 652 |
const struct util_format_description *desc = |
| 653 |
util_format_description(hk_format_to_pipe_format(view->vk.format)); |
| 654 |
|
| 655 |
assert(desc->format == PIPE_FORMAT_Z32_FLOAT || |
| 656 |
desc->format == PIPE_FORMAT_Z16_UNORM || |
| 657 |
desc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT); |
| 658 |
|
| 659 |
render->cr.depth.buffer = |
| 660 |
hk_image_base_address(image, 0) + |
| 661 |
ail_get_layer_level_B(z_layout, first_layer, level); |
| 662 |
|
| 663 |
/* Main stride in pages */ |
| 664 |
assert((z_layout->depth_px == 1 || |
| 665 |
util_is_aligned(z_layout->layer_stride_B, AIL_PAGESIZE)) && |
| 666 |
"Page aligned Z layers"); |
| 667 |
|
| 668 |
unsigned stride_pages = z_layout->layer_stride_B / AIL_PAGESIZE; |
| 669 |
render->cr.depth.stride = ((stride_pages - 1) << 14) | 1; |
| 670 |
|
| 671 |
assert(z_layout->tiling != AIL_TILING_LINEAR && "must tile"); |
| 672 |
|
| 673 |
if (z_layout->compressed) { |
| 674 |
render->cr.depth.meta = |
| 675 |
hk_image_base_address(image, 0) + z_layout->metadata_offset_B + |
| 676 |
(first_layer * z_layout->compression_layer_stride_B) + |
| 677 |
z_layout->level_offsets_compressed_B[level]; |
| 678 |
|
| 679 |
/* Meta stride in cache lines */ |
| 680 |
assert(util_is_aligned(z_layout->compression_layer_stride_B, |
| 681 |
AIL_CACHELINE) && |
| 682 |
"Cacheline aligned Z meta layers"); |
| 683 |
|
| 684 |
unsigned stride_lines = |
| 685 |
z_layout->compression_layer_stride_B / AIL_CACHELINE; |
| 686 |
render->cr.depth.meta_stride = (stride_lines - 1) << 14; |
| 687 |
} |
| 688 |
|
| 689 |
float clear_depth = attach_z->clearValue.depthStencil.depth; |
| 690 |
|
| 691 |
if (z_layout->format == PIPE_FORMAT_Z16_UNORM) { |
| 692 |
render->cr.isp_bgobjdepth = _mesa_float_to_unorm(clear_depth, 16); |
| 693 |
} else { |
| 694 |
render->cr.isp_bgobjdepth = fui(clear_depth); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
if (attach_s != NULL && attach_s != VK_NULL_HANDLE && attach_s->imageView) { |
| 699 |
struct hk_image_view *view = render->stencil_att.iview; |
| 700 |
struct hk_image *image = |
| 701 |
container_of(view->vk.image, struct hk_image, vk); |
| 702 |
|
| 703 |
/* Stencil is always the last plane (possibly the only plane) */ |
| 704 |
unsigned plane = image->plane_count - 1; |
| 705 |
s_layout = &image->planes[plane].layout; |
| 706 |
assert(s_layout->format == PIPE_FORMAT_S8_UINT); |
| 707 |
|
| 708 |
unsigned level = view->vk.base_mip_level; |
| 709 |
unsigned first_layer = view->vk.base_array_layer; |
| 710 |
|
| 711 |
render->cr.stencil.buffer = |
| 712 |
hk_image_base_address(image, plane) + |
| 713 |
ail_get_layer_level_B(s_layout, first_layer, level); |
| 714 |
|
| 715 |
/* Main stride in pages */ |
| 716 |
assert((s_layout->depth_px == 1 || |
| 717 |
util_is_aligned(s_layout->layer_stride_B, AIL_PAGESIZE)) && |
| 718 |
"Page aligned S layers"); |
| 719 |
unsigned stride_pages = s_layout->layer_stride_B / AIL_PAGESIZE; |
| 720 |
render->cr.stencil.stride = ((stride_pages - 1) << 14) | 1; |
| 721 |
|
| 722 |
if (s_layout->compressed) { |
| 723 |
render->cr.stencil.meta = |
| 724 |
hk_image_base_address(image, plane) + s_layout->metadata_offset_B + |
| 725 |
(first_layer * s_layout->compression_layer_stride_B) + |
| 726 |
s_layout->level_offsets_compressed_B[level]; |
| 727 |
|
| 728 |
/* Meta stride in cache lines */ |
| 729 |
assert(util_is_aligned(s_layout->compression_layer_stride_B, |
| 730 |
AIL_CACHELINE) && |
| 731 |
"Cacheline aligned S meta layers"); |
| 732 |
|
| 733 |
unsigned stride_lines = |
| 734 |
s_layout->compression_layer_stride_B / AIL_CACHELINE; |
| 735 |
|
| 736 |
render->cr.stencil.meta_stride = (stride_lines - 1) << 14; |
| 737 |
} |
| 738 |
|
| 739 |
render->cr.isp_bgobjvals |= attach_s->clearValue.depthStencil.stencil; |
| 740 |
} |
| 741 |
|
| 742 |
hk_pack_zls_control(&render->cr.zls_control, z_layout, s_layout, attach_z, |
| 743 |
attach_s, incomplete_render_area, false); |
| 744 |
|
| 745 |
hk_pack_zls_control(&render->cr.zls_control_partial, z_layout, s_layout, |
| 746 |
attach_z, attach_s, incomplete_render_area, true); |
| 747 |
|
| 748 |
/* If multiview is disabled, always read 0. If multiview is enabled, |
| 749 |
* hk_set_view_index will dirty the root each draw. |
| 750 |
*/ |
| 751 |
cmd->state.gfx.descriptors.root.draw.view_index = 0; |
| 752 |
cmd->state.gfx.descriptors.root_dirty = true; |
| 753 |
|
| 754 |
if (render->flags & VK_RENDERING_RESUMING_BIT) |
| 755 |
return; |
| 756 |
|
| 757 |
/* The first control stream of the render pass is special since it gets |
| 758 |
* the clears. Create it and swap in the clear. |
| 759 |
*/ |
| 760 |
assert(!cmd->current_cs.gfx && "not already in a render pass"); |
| 761 |
struct hk_cs *cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); |
| 762 |
if (!cs) |
| 763 |
return; |
| 764 |
|
| 765 |
cs->cr.bg.main = render->cr.bg.main; |
| 766 |
cs->cr.zls_control = render->cr.zls_control; |
| 767 |
|
| 768 |
/* Reordering barrier for post-gfx, in case we had any. */ |
| 769 |
hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.post_gfx); |
| 770 |
|
| 771 |
/* Don't reorder compute across render passes. |
| 772 |
* |
| 773 |
* TODO: Check if this is necessary if the proper PipelineBarriers are |
| 774 |
* handled... there may be CTS bugs... |
| 775 |
*/ |
| 776 |
hk_cmd_buffer_end_compute(cmd); |
| 777 |
|
| 778 |
/* If we spill colour attachments, we need to decompress them. This happens |
| 779 |
* at the start of the render; it is not re-emitted when resuming |
| 780 |
* secondaries. It could be hoisted to the start of the command buffer but |
| 781 |
* we're not that clever yet. |
| 782 |
*/ |
| 783 |
if (agx_tilebuffer_spills(&render->tilebuffer)) { |
| 784 |
perf_debug(cmd, "eMRT render pass"); |
| 785 |
|
| 786 |
for (unsigned i = 0; i < render->color_att_count; ++i) { |
| 787 |
struct hk_image_view *view = render->color_att[i].iview; |
| 788 |
if (view) { |
| 789 |
struct hk_image *image = |
| 790 |
container_of(view->vk.image, struct hk_image, vk); |
| 791 |
|
| 792 |
/* TODO: YCbCr interaction? */ |
| 793 |
uint8_t plane = 0; |
| 794 |
uint8_t image_plane = view->planes[plane].image_plane; |
| 795 |
struct ail_layout *layout = &image->planes[image_plane].layout; |
| 796 |
|
| 797 |
if (ail_is_level_logically_compressed(layout, |
| 798 |
view->vk.base_mip_level)) { |
| 799 |
|
| 800 |
perf_debug(cmd, "Decompressing in-place"); |
| 801 |
|
| 802 |
unsigned level = view->vk.base_mip_level; |
| 803 |
unsigned layer = view->vk.base_array_layer; |
| 804 |
uint64_t base = hk_image_base_address(image, image_plane); |
| 805 |
|
| 806 |
struct libagx_decompress_images imgs = { |
| 807 |
.compressed = view->planes[plane].emrt_texture, |
| 808 |
.uncompressed = view->planes[plane].emrt_pbe, |
| 809 |
}; |
| 810 |
|
| 811 |
struct agx_grid grid = |
| 812 |
agx_3d(ail_metadata_width_tl(layout, level) * 32, |
| 813 |
ail_metadata_height_tl(layout, level), layer_count); |
| 814 |
|
| 815 |
libagx_decompress(cmd, grid, AGX_BARRIER_ALL | AGX_PREGFX, |
| 816 |
layout, layer, level, base, |
| 817 |
hk_pool_upload(cmd, &imgs, sizeof(imgs), 64)); |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
uint32_t clear_count = 0; |
| 824 |
VkClearAttachment clear_att[HK_MAX_RTS + 1]; |
| 825 |
bool resolved_clear = false; |
| 826 |
|
| 827 |
for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) { |
| 828 |
const VkRenderingAttachmentInfo *att_info = |
| 829 |
&pRenderingInfo->pColorAttachments[i]; |
| 830 |
if (att_info->imageView == VK_NULL_HANDLE || |
| 831 |
att_info->loadOp != VK_ATTACHMENT_LOAD_OP_CLEAR) |
| 832 |
continue; |
| 833 |
|
| 834 |
clear_att[clear_count++] = (VkClearAttachment){ |
| 835 |
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 836 |
.colorAttachment = i, |
| 837 |
.clearValue = att_info->clearValue, |
| 838 |
}; |
| 839 |
|
| 840 |
render->color_att[i].clear = true; |
| 841 |
|
| 842 |
static_assert(sizeof(render->color_att[i].clear_colour) == |
| 843 |
sizeof(att_info->clearValue)); |
| 844 |
memcpy(render->color_att[i].clear_colour, &att_info->clearValue, |
| 845 |
sizeof(att_info->clearValue)); |
| 846 |
|
| 847 |
resolved_clear |= is_attachment_stored(att_info); |
| 848 |
} |
| 849 |
|
| 850 |
clear_att[clear_count] = (VkClearAttachment){ |
| 851 |
.aspectMask = 0, |
| 852 |
}; |
| 853 |
|
| 854 |
if (attach_z && attach_z->imageView != VK_NULL_HANDLE && |
| 855 |
attach_z->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 856 |
clear_att[clear_count].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; |
| 857 |
clear_att[clear_count].clearValue.depthStencil.depth = |
| 858 |
attach_z->clearValue.depthStencil.depth; |
| 859 |
|
| 860 |
render->depth_att.clear = true; |
| 861 |
render->depth_att.clear_colour[0] = |
| 862 |
fui(attach_z->clearValue.depthStencil.depth); |
| 863 |
|
| 864 |
resolved_clear |= is_attachment_stored(attach_z); |
| 865 |
} |
| 866 |
|
| 867 |
if (attach_s != NULL && attach_s->imageView != VK_NULL_HANDLE && |
| 868 |
attach_s->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) { |
| 869 |
clear_att[clear_count].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; |
| 870 |
clear_att[clear_count].clearValue.depthStencil.stencil = |
| 871 |
attach_s->clearValue.depthStencil.stencil; |
| 872 |
|
| 873 |
render->stencil_att.clear = true; |
| 874 |
render->stencil_att.clear_colour[1] = |
| 875 |
attach_s->clearValue.depthStencil.stencil; |
| 876 |
|
| 877 |
resolved_clear |= is_attachment_stored(attach_s); |
| 878 |
} |
| 879 |
|
| 880 |
if (clear_att[clear_count].aspectMask != 0) |
| 881 |
clear_count++; |
| 882 |
|
| 883 |
if (clear_count > 0 && incomplete_render_area) { |
| 884 |
const VkClearRect clear_rect = { |
| 885 |
.rect = render->area, |
| 886 |
.baseArrayLayer = 0, |
| 887 |
.layerCount = render->view_mask ? 1 : render->layer_count, |
| 888 |
}; |
| 889 |
|
| 890 |
hk_CmdClearAttachments(hk_cmd_buffer_to_handle(cmd), clear_count, |
| 891 |
clear_att, 1, &clear_rect); |
| 892 |
} else { |
| 893 |
/* If a tile is empty, we do not want to process it, as the redundant |
| 894 |
* roundtrip of memory-->tilebuffer-->memory wastes a tremendous amount of |
| 895 |
* memory bandwidth. Any draw marks a tile as non-empty, so we only need |
| 896 |
* to process empty tiles if the background+EOT programs have a side |
| 897 |
* effect. This is the case exactly when there is an attachment we are |
| 898 |
* fast clearing and then storing. |
| 899 |
*/ |
| 900 |
cs->cr.process_empty_tiles = resolved_clear; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
VKAPI_ATTR void VKAPI_CALL |
| 905 |
hk_CmdEndRendering(VkCommandBuffer commandBuffer) |
| 906 |
{ |
| 907 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 908 |
struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 909 |
|
| 910 |
/* The last control stream of the render pass is special since it gets its |
| 911 |
* stores dropped. Swap it in. |
| 912 |
*/ |
| 913 |
struct hk_cs *cs = cmd->current_cs.gfx; |
| 914 |
if (cs) { |
| 915 |
cs->cr.eot.main = render->cr.eot.main; |
| 916 |
} |
| 917 |
|
| 918 |
perf_debug(cmd, "End rendering"); |
| 919 |
hk_cmd_buffer_end_graphics(cmd); |
| 920 |
|
| 921 |
bool need_resolve = false; |
| 922 |
|
| 923 |
/* Translate render state back to VK for meta */ |
| 924 |
VkRenderingAttachmentInfo vk_color_att[HK_MAX_RTS]; |
| 925 |
for (uint32_t i = 0; i < render->color_att_count; i++) { |
| 926 |
if (render->color_att[i].resolve_mode != VK_RESOLVE_MODE_NONE) |
| 927 |
need_resolve = true; |
| 928 |
|
| 929 |
vk_color_att[i] = (VkRenderingAttachmentInfo){ |
| 930 |
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, |
| 931 |
.imageView = hk_image_view_to_handle(render->color_att[i].iview), |
| 932 |
.imageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 933 |
.resolveMode = render->color_att[i].resolve_mode, |
| 934 |
.resolveImageView = |
| 935 |
hk_image_view_to_handle(render->color_att[i].resolve_iview), |
| 936 |
.resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 937 |
}; |
| 938 |
} |
| 939 |
|
| 940 |
const VkRenderingAttachmentInfo vk_depth_att = { |
| 941 |
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, |
| 942 |
.imageView = hk_image_view_to_handle(render->depth_att.iview), |
| 943 |
.imageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 944 |
.resolveMode = render->depth_att.resolve_mode, |
| 945 |
.resolveImageView = |
| 946 |
hk_image_view_to_handle(render->depth_att.resolve_iview), |
| 947 |
.resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 948 |
}; |
| 949 |
if (render->depth_att.resolve_mode != VK_RESOLVE_MODE_NONE) |
| 950 |
need_resolve = true; |
| 951 |
|
| 952 |
const VkRenderingAttachmentInfo vk_stencil_att = { |
| 953 |
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, |
| 954 |
.imageView = hk_image_view_to_handle(render->stencil_att.iview), |
| 955 |
.imageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 956 |
.resolveMode = render->stencil_att.resolve_mode, |
| 957 |
.resolveImageView = |
| 958 |
hk_image_view_to_handle(render->stencil_att.resolve_iview), |
| 959 |
.resolveImageLayout = VK_IMAGE_LAYOUT_GENERAL, |
| 960 |
}; |
| 961 |
if (render->stencil_att.resolve_mode != VK_RESOLVE_MODE_NONE) |
| 962 |
need_resolve = true; |
| 963 |
|
| 964 |
const VkRenderingInfo vk_render = { |
| 965 |
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, |
| 966 |
.renderArea = render->area, |
| 967 |
.layerCount = render->layer_count, |
| 968 |
.viewMask = render->view_mask, |
| 969 |
.colorAttachmentCount = render->color_att_count, |
| 970 |
.pColorAttachments = vk_color_att, |
| 971 |
.pDepthAttachment = &vk_depth_att, |
| 972 |
.pStencilAttachment = &vk_stencil_att, |
| 973 |
}; |
| 974 |
|
| 975 |
if (render->flags & VK_RENDERING_SUSPENDING_BIT) |
| 976 |
need_resolve = false; |
| 977 |
|
| 978 |
memset(render, 0, sizeof(*render)); |
| 979 |
|
| 980 |
if (need_resolve) { |
| 981 |
perf_debug(cmd, "Resolving render pass, colour store op %u", |
| 982 |
vk_color_att[0].storeOp); |
| 983 |
|
| 984 |
hk_meta_resolve_rendering(cmd, &vk_render); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
static void |
| 989 |
hk_init_heap(const void *data) |
| 990 |
{ |
| 991 |
struct hk_cmd_buffer *cmd = (struct hk_cmd_buffer *)data; |
| 992 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 993 |
|
| 994 |
perf_debug(cmd, "Allocating heap"); |
| 995 |
|
| 996 |
size_t size = 128 * 1024 * 1024; |
| 997 |
dev->heap = agx_bo_create(&dev->dev, size, 0, 0, "Geometry heap"); |
| 998 |
|
| 999 |
/* The geometry state buffer is initialized here and then is treated by |
| 1000 |
* the CPU as rodata, even though the GPU uses it for scratch internally. |
| 1001 |
*/ |
| 1002 |
off_t off = dev->rodata.heap - dev->rodata.bo->va->addr; |
| 1003 |
struct poly_heap *map = agx_bo_map(dev->rodata.bo) + off; |
| 1004 |
|
| 1005 |
*map = (struct poly_heap){ |
| 1006 |
.base = dev->heap->va->addr, |
| 1007 |
.size = size, |
| 1008 |
}; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static uint64_t |
| 1012 |
hk_heap(struct hk_cmd_buffer *cmd) |
| 1013 |
{ |
| 1014 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1015 |
|
| 1016 |
util_call_once_data(&dev->heap_init_once, hk_init_heap, cmd); |
| 1017 |
|
| 1018 |
/* We need to free all allocations after each command buffer execution */ |
| 1019 |
if (!cmd->uses_heap) { |
| 1020 |
perf_debug(cmd, "Freeing heap"); |
| 1021 |
uint64_t addr = dev->rodata.heap; |
| 1022 |
|
| 1023 |
/* Zeroing the allocated index frees everything */ |
| 1024 |
hk_queue_write(cmd, addr + offsetof(struct poly_heap, bottom), 0, |
| 1025 |
true /* after gfx */); |
| 1026 |
|
| 1027 |
cmd->uses_heap = true; |
| 1028 |
} |
| 1029 |
|
| 1030 |
return dev->rodata.heap; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static uint64_t |
| 1034 |
hk_index_buffer(uint64_t index_buffer, uint size_el, uint offset_el, |
| 1035 |
uint elsize_B) |
| 1036 |
{ |
| 1037 |
if (offset_el < size_el) |
| 1038 |
return index_buffer + (offset_el * elsize_B); |
| 1039 |
else |
| 1040 |
return AGX_ZERO_PAGE_ADDRESS; |
| 1041 |
} |
| 1042 |
|
| 1043 |
static uint64_t |
| 1044 |
hk_upload_vertex_params(struct hk_cmd_buffer *cmd, struct agx_draw draw) |
| 1045 |
{ |
| 1046 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1047 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 1048 |
|
| 1049 |
const uint32_t wg_size[3] = {64, 1, 1}; |
| 1050 |
|
| 1051 |
struct poly_vertex_params params; |
| 1052 |
poly_vertex_params_init(¶ms, 0, wg_size); |
| 1053 |
|
| 1054 |
/* XXX: We should deduplicate this logic */ |
| 1055 |
bool indirect = agx_is_indirect(draw.b) || draw.restart; |
| 1056 |
|
| 1057 |
if (!indirect) |
| 1058 |
poly_vertex_params_set_draw(¶ms, draw.b.count[0], draw.b.count[1]); |
| 1059 |
|
| 1060 |
if (draw.indexed) { |
| 1061 |
unsigned index_size_B = agx_index_size_to_B(draw.index_size); |
| 1062 |
unsigned range_el = agx_draw_index_range_el(draw); |
| 1063 |
|
| 1064 |
params.index_buffer = hk_index_buffer(agx_draw_index_buffer(draw), |
| 1065 |
range_el, 0, index_size_B); |
| 1066 |
|
| 1067 |
params.index_buffer_range_el = range_el; |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (gfx->shaders[MESA_SHADER_TESS_EVAL] || |
| 1071 |
gfx->shaders[MESA_SHADER_GEOMETRY]) { |
| 1072 |
|
| 1073 |
struct hk_shader *vs = hk_bound_sw_vs(gfx); |
| 1074 |
params.outputs = vs->b.info.outputs; |
| 1075 |
|
| 1076 |
if (!indirect) { |
| 1077 |
uint32_t verts = draw.b.count[0], instances = draw.b.count[1]; |
| 1078 |
unsigned vb_size = |
| 1079 |
poly_tcs_in_size(verts * instances, vs->b.info.outputs); |
| 1080 |
|
| 1081 |
/* Allocate if there are any outputs, or use the null sink to trap |
| 1082 |
* reads if there aren't. Those reads are undefined but should not |
| 1083 |
* fault. Affects: |
| 1084 |
* |
| 1085 |
* dEQP-VK.pipeline.monolithic.no_position.explicit_declarations.basic.single_view.v0_g1 |
| 1086 |
*/ |
| 1087 |
params.output_buffer = vb_size ? hk_pool_alloc(cmd, vb_size, 4).gpu |
| 1088 |
: AGX_SCRATCH_PAGE_ADDRESS; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
desc->root.draw.vertex_outputs = params.outputs; |
| 1093 |
|
| 1094 |
return hk_pool_upload(cmd, ¶ms, sizeof(params), 8); |
| 1095 |
} |
| 1096 |
|
| 1097 |
static enum mesa_prim |
| 1098 |
hk_gs_in_prim(struct hk_cmd_buffer *cmd) |
| 1099 |
{ |
| 1100 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1101 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1102 |
struct hk_api_shader *tes = gfx->shaders[MESA_SHADER_TESS_EVAL]; |
| 1103 |
|
| 1104 |
if (tes != NULL) |
| 1105 |
return gfx->tess.prim; |
| 1106 |
else |
| 1107 |
return vk_topology_to_mesa(dyn->ia.primitive_topology); |
| 1108 |
} |
| 1109 |
|
| 1110 |
static enum mesa_prim |
| 1111 |
hk_rast_prim(struct hk_cmd_buffer *cmd) |
| 1112 |
{ |
| 1113 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1114 |
struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY]; |
| 1115 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1116 |
|
| 1117 |
if (gs != NULL) { |
| 1118 |
return gs->variants[HK_GS_VARIANT_RAST].info.gs.mode; |
| 1119 |
} else { |
| 1120 |
switch (dyn->ia.primitive_topology) { |
| 1121 |
case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY: |
| 1122 |
case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY: |
| 1123 |
return MESA_PRIM_LINES; |
| 1124 |
case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY: |
| 1125 |
case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY: |
| 1126 |
return MESA_PRIM_TRIANGLES; |
| 1127 |
default: |
| 1128 |
return hk_gs_in_prim(cmd); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
static uint64_t |
| 1134 |
hk_upload_geometry_params(struct hk_cmd_buffer *cmd, struct agx_draw draw) |
| 1135 |
{ |
| 1136 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1137 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 1138 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1139 |
struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY]; |
| 1140 |
struct hk_shader *fs = hk_only_variant(gfx->shaders[MESA_SHADER_FRAGMENT]); |
| 1141 |
struct hk_shader *count = hk_count_gs_variant(gs); |
| 1142 |
|
| 1143 |
/* XXX: We should deduplicate this logic */ |
| 1144 |
bool indirect = agx_is_indirect(draw.b) || |
| 1145 |
gfx->shaders[MESA_SHADER_TESS_EVAL] || draw.restart; |
| 1146 |
enum mesa_prim mode = hk_gs_in_prim(cmd); |
| 1147 |
|
| 1148 |
if (draw.restart) { |
| 1149 |
mode = u_decomposed_prim(mode); |
| 1150 |
} |
| 1151 |
|
| 1152 |
const uint32_t wg_size[3] = {64, 1, 1}; |
| 1153 |
|
| 1154 |
struct poly_geometry_params params; |
| 1155 |
poly_geometry_params_init(¶ms, mode, wg_size); |
| 1156 |
|
| 1157 |
params.flat_outputs = fs->info.fs.interp.flat; |
| 1158 |
|
| 1159 |
if (gfx->xfb_enabled) { |
| 1160 |
for (unsigned i = 0; i < ARRAY_SIZE(gfx->xfb); ++i) { |
| 1161 |
params.xfb_base_original[i] = gfx->xfb[i].addr; |
| 1162 |
params.xfb_size[i] = gfx->xfb[i].range; |
| 1163 |
params.xfb_offs_ptrs[i] = gfx->xfb_offsets + i * sizeof(uint32_t); |
| 1164 |
} |
| 1165 |
} else { |
| 1166 |
for (unsigned i = 0; i < ARRAY_SIZE(gfx->xfb); ++i) { |
| 1167 |
params.xfb_offs_ptrs[i] = AGX_ZERO_PAGE_ADDRESS; |
| 1168 |
params.xfb_base_original[i] = AGX_ZERO_PAGE_ADDRESS; |
| 1169 |
params.xfb_size[i] = 0; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
for (unsigned i = 0; i < ARRAY_SIZE(gfx->xfb_query); ++i) { |
| 1174 |
uint64_t q = gfx->xfb_query[i]; |
| 1175 |
|
| 1176 |
if (q) { |
| 1177 |
params.xfb_prims_generated_counter[i] = q; |
| 1178 |
params.prims_generated_counter[i] = q + sizeof(uint64_t); |
| 1179 |
} else { |
| 1180 |
params.xfb_prims_generated_counter[i] = AGX_SCRATCH_PAGE_ADDRESS; |
| 1181 |
params.prims_generated_counter[i] = AGX_SCRATCH_PAGE_ADDRESS; |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* TODO: Optimize out? */ |
| 1185 |
params.xfb_overflow[i] = AGX_SCRATCH_PAGE_ADDRESS; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* TODO: Optimize out? */ |
| 1189 |
params.xfb_any_overflow = AGX_SCRATCH_PAGE_ADDRESS; |
| 1190 |
|
| 1191 |
/* Calculate input primitive count for direct draws, and allocate the vertex |
| 1192 |
* & count buffers. GPU calculates and allocates for indirect draws. |
| 1193 |
*/ |
| 1194 |
params.count_buffer_stride = count->info.gs.count_words * 4; |
| 1195 |
|
| 1196 |
if (!count->info.gs.prefix_sum && params.count_buffer_stride) { |
| 1197 |
struct agx_ptr T = hk_pool_alloc(cmd, 16, 4); |
| 1198 |
memset(T.cpu, 0, 16); |
| 1199 |
params.count_buffer = T.gpu; |
| 1200 |
} |
| 1201 |
|
| 1202 |
struct poly_gs_info *gsi = &count->info.gs; |
| 1203 |
|
| 1204 |
if (indirect) { |
| 1205 |
if (gsi->shape == POLY_GS_SHAPE_DYNAMIC_INDEXED) { |
| 1206 |
/* Need to allocate heap if we haven't yet */ |
| 1207 |
hk_heap(cmd); |
| 1208 |
|
| 1209 |
cmd->geom_index_buffer = dev->heap->va->addr; |
| 1210 |
cmd->geom_index_count = dev->heap->size; |
| 1211 |
} else { |
| 1212 |
cmd->geom_index_count = |
| 1213 |
poly_gs_rast_vertices(gsi->shape, gsi->max_indices, 1, 0); |
| 1214 |
} |
| 1215 |
} else { |
| 1216 |
poly_geometry_params_set_draw(¶ms, mode, gsi->shape, gsi->max_indices, |
| 1217 |
draw.b.count[0], draw.b.count[1]); |
| 1218 |
|
| 1219 |
unsigned size = params.input_primitives * params.count_buffer_stride; |
| 1220 |
if (count->info.gs.prefix_sum && size) { |
| 1221 |
params.count_buffer = hk_pool_alloc(cmd, size, 4).gpu; |
| 1222 |
} |
| 1223 |
|
| 1224 |
cmd->geom_index_count = params.draw.index_count; |
| 1225 |
cmd->geom_instance_count = params.draw.instance_count; |
| 1226 |
|
| 1227 |
if (gsi->shape == POLY_GS_SHAPE_DYNAMIC_INDEXED) { |
| 1228 |
params.output_index_buffer = |
| 1229 |
hk_pool_alloc(cmd, cmd->geom_index_count * 4, 4).gpu; |
| 1230 |
|
| 1231 |
cmd->geom_index_buffer = params.output_index_buffer; |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (gsi->shape == POLY_GS_SHAPE_STATIC_INDEXED) { |
| 1236 |
cmd->geom_index_buffer = |
| 1237 |
hk_pool_upload(cmd, count->info.gs.topology, gsi->max_indices * 4, 4); |
| 1238 |
} |
| 1239 |
|
| 1240 |
desc->root_dirty = true; |
| 1241 |
return hk_pool_upload(cmd, ¶ms, sizeof(params), 8); |
| 1242 |
} |
| 1243 |
|
| 1244 |
static void |
| 1245 |
hk_upload_tess_params(struct hk_cmd_buffer *cmd, struct poly_tess_params *out, |
| 1246 |
struct agx_draw draw) |
| 1247 |
{ |
| 1248 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1249 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1250 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1251 |
struct hk_shader *tcs = hk_only_variant(gfx->shaders[MESA_SHADER_TESS_CTRL]); |
| 1252 |
|
| 1253 |
enum poly_tess_partitioning partitioning = |
| 1254 |
gfx->tess.info.spacing == TESS_SPACING_EQUAL |
| 1255 |
? POLY_TESS_PARTITIONING_INTEGER |
| 1256 |
: gfx->tess.info.spacing == TESS_SPACING_FRACTIONAL_ODD |
| 1257 |
? POLY_TESS_PARTITIONING_FRACTIONAL_ODD |
| 1258 |
: POLY_TESS_PARTITIONING_FRACTIONAL_EVEN; |
| 1259 |
|
| 1260 |
struct poly_tess_params args = { |
| 1261 |
.heap = hk_heap(cmd), |
| 1262 |
.tcs_stride_el = tcs->info.tess.tcs_output_stride / 4, |
| 1263 |
.statistic = hk_pipeline_stat_addr( |
| 1264 |
cmd, |
| 1265 |
VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT), |
| 1266 |
|
| 1267 |
.input_patch_size = dyn->ts.patch_control_points, |
| 1268 |
.output_patch_size = tcs->info.tess.tcs_output_patch_size, |
| 1269 |
.tcs_patch_constants = tcs->info.tess.tcs_nr_patch_outputs, |
| 1270 |
.tcs_per_vertex_outputs = tcs->info.tess.tcs_per_vertex_outputs, |
| 1271 |
.partitioning = partitioning, |
| 1272 |
.points_mode = gfx->tess.info.points, |
| 1273 |
.isolines = gfx->tess.info.mode == TESS_PRIMITIVE_ISOLINES, |
| 1274 |
}; |
| 1275 |
|
| 1276 |
if (!args.points_mode && gfx->tess.info.mode != TESS_PRIMITIVE_ISOLINES) { |
| 1277 |
args.ccw = gfx->tess.info.ccw; |
| 1278 |
args.ccw ^= |
| 1279 |
dyn->ts.domain_origin == VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT; |
| 1280 |
} |
| 1281 |
|
| 1282 |
uint32_t draw_stride_el = 5; |
| 1283 |
size_t draw_stride_B = draw_stride_el * sizeof(uint32_t); |
| 1284 |
|
| 1285 |
/* heap is allocated by hk_heap */ |
| 1286 |
args.patch_coord_buffer = dev->heap->va->addr; |
| 1287 |
|
| 1288 |
if (!agx_is_indirect(draw.b)) { |
| 1289 |
unsigned in_patches = draw.b.count[0] / args.input_patch_size; |
| 1290 |
unsigned unrolled_patches = in_patches * draw.b.count[1]; |
| 1291 |
|
| 1292 |
uint32_t alloc = 0; |
| 1293 |
uint32_t tcs_out_offs = alloc; |
| 1294 |
alloc += unrolled_patches * args.tcs_stride_el * sizeof(uint32_t); |
| 1295 |
|
| 1296 |
uint32_t patch_coord_offs = alloc; |
| 1297 |
alloc += unrolled_patches * sizeof(uint32_t); |
| 1298 |
|
| 1299 |
uint32_t count_offs = alloc; |
| 1300 |
alloc += unrolled_patches * sizeof(uint32_t); |
| 1301 |
|
| 1302 |
/* Single API draw */ |
| 1303 |
uint32_t draw_offs = alloc; |
| 1304 |
alloc += draw_stride_B; |
| 1305 |
|
| 1306 |
struct agx_ptr blob = hk_pool_alloc(cmd, alloc, 4); |
| 1307 |
args.tcs_buffer = blob.gpu + tcs_out_offs; |
| 1308 |
args.patches_per_instance = in_patches; |
| 1309 |
args.coord_allocs = blob.gpu + patch_coord_offs; |
| 1310 |
args.nr_patches = unrolled_patches; |
| 1311 |
args.out_draws = blob.gpu + draw_offs; |
| 1312 |
args.counts = blob.gpu + count_offs; |
| 1313 |
} else { |
| 1314 |
/* Allocate 3x indirect global+local grids for VS/TCS/tess */ |
| 1315 |
uint32_t grid_stride = sizeof(uint32_t) * 6; |
| 1316 |
gfx->tess.grids = hk_pool_alloc(cmd, grid_stride * 3, 4).gpu; |
| 1317 |
|
| 1318 |
args.out_draws = hk_pool_alloc(cmd, draw_stride_B, 4).gpu; |
| 1319 |
} |
| 1320 |
|
| 1321 |
gfx->tess.out_draws = args.out_draws; |
| 1322 |
memcpy(out, &args, sizeof(args)); |
| 1323 |
} |
| 1324 |
|
| 1325 |
static struct hk_api_shader * |
| 1326 |
hk_build_meta_shader_locked(struct hk_device *dev, struct hk_internal_key *key, |
| 1327 |
hk_internal_builder_t builder) |
| 1328 |
{ |
| 1329 |
/* Try to get the cached shader */ |
| 1330 |
struct hash_entry *ent = _mesa_hash_table_search(dev->kernels.ht, key); |
| 1331 |
if (ent) |
| 1332 |
return ent->data; |
| 1333 |
|
| 1334 |
nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, |
| 1335 |
&agx_nir_options, NULL); |
| 1336 |
builder(&b, key->key); |
| 1337 |
|
| 1338 |
const struct vk_pipeline_robustness_state rs = { |
| 1339 |
.images = VK_PIPELINE_ROBUSTNESS_IMAGE_BEHAVIOR_DISABLED_EXT, |
| 1340 |
.storage_buffers = VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT, |
| 1341 |
.uniform_buffers = VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT, |
| 1342 |
.vertex_inputs = VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT, |
| 1343 |
}; |
| 1344 |
|
| 1345 |
struct vk_shader_compile_info info = { |
| 1346 |
.stage = b.shader->info.stage, |
| 1347 |
.nir = b.shader, |
| 1348 |
.robustness = &rs, |
| 1349 |
}; |
| 1350 |
|
| 1351 |
hk_preprocess_nir_internal(dev->vk.physical, b.shader); |
| 1352 |
|
| 1353 |
struct hk_api_shader *s; |
| 1354 |
if (hk_compile_shader(dev, &info, NULL, NULL, NULL, &s) != VK_SUCCESS) |
| 1355 |
return NULL; |
| 1356 |
|
| 1357 |
/* ..and cache it before we return. The key is on the stack right now, so |
| 1358 |
* clone it before using it as a hash table key. The clone is logically owned |
| 1359 |
* by the hash table. |
| 1360 |
*/ |
| 1361 |
size_t total_key_size = sizeof(*key) + key->key_size; |
| 1362 |
void *cloned_key = ralloc_memdup(dev->kernels.ht, key, total_key_size); |
| 1363 |
|
| 1364 |
_mesa_hash_table_insert(dev->kernels.ht, cloned_key, s); |
| 1365 |
return s; |
| 1366 |
} |
| 1367 |
|
| 1368 |
struct hk_api_shader * |
| 1369 |
hk_meta_shader(struct hk_device *dev, hk_internal_builder_t builder, void *data, |
| 1370 |
size_t data_size) |
| 1371 |
{ |
| 1372 |
size_t total_key_size = sizeof(struct hk_internal_key) + data_size; |
| 1373 |
|
| 1374 |
struct hk_internal_key *key = alloca(total_key_size); |
| 1375 |
key->builder = builder; |
| 1376 |
key->key_size = data_size; |
| 1377 |
|
| 1378 |
if (data_size) |
| 1379 |
memcpy(key->key, data, data_size); |
| 1380 |
|
| 1381 |
simple_mtx_lock(&dev->kernels.lock); |
| 1382 |
struct hk_api_shader *s = hk_build_meta_shader_locked(dev, key, builder); |
| 1383 |
simple_mtx_unlock(&dev->kernels.lock); |
| 1384 |
|
| 1385 |
return s; |
| 1386 |
} |
| 1387 |
|
| 1388 |
static struct agx_draw |
| 1389 |
hk_draw_as_indexed_indirect(struct hk_cmd_buffer *cmd, struct agx_draw draw) |
| 1390 |
{ |
| 1391 |
assert(draw.indexed); |
| 1392 |
|
| 1393 |
if (agx_is_indirect(draw.b)) |
| 1394 |
return draw; |
| 1395 |
|
| 1396 |
VkDrawIndexedIndirectCommand desc = { |
| 1397 |
.indexCount = draw.b.count[0], |
| 1398 |
.instanceCount = draw.b.count[1], |
| 1399 |
.firstIndex = draw.start, |
| 1400 |
.vertexOffset = draw.index_bias, |
| 1401 |
.firstInstance = draw.start_instance, |
| 1402 |
}; |
| 1403 |
|
| 1404 |
return agx_draw_indexed_indirect( |
| 1405 |
hk_pool_upload(cmd, &desc, sizeof(desc), 4), draw.index_buffer, |
| 1406 |
draw.index_buffer_range_B, draw.index_size, draw.restart); |
| 1407 |
} |
| 1408 |
|
| 1409 |
static struct agx_draw |
| 1410 |
hk_draw_without_restart(struct hk_cmd_buffer *cmd, struct agx_draw draw, |
| 1411 |
uint32_t draw_count) |
| 1412 |
{ |
| 1413 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1414 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1415 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1416 |
|
| 1417 |
perf_debug(cmd, "Unrolling primitive restart due to GS/XFB"); |
| 1418 |
|
| 1419 |
/* The unroll kernel assumes an indirect draw. Synthesize one if needed */ |
| 1420 |
draw = hk_draw_as_indexed_indirect(cmd, draw); |
| 1421 |
|
| 1422 |
/* Next, we unroll the index buffer used by the indirect draw */ |
| 1423 |
enum mesa_prim prim = vk_topology_to_mesa(dyn->ia.primitive_topology); |
| 1424 |
|
| 1425 |
assert(draw_count == 1 && "TODO: multidraw"); |
| 1426 |
|
| 1427 |
struct libagx_unroll_restart_args ia = { |
| 1428 |
.heap = hk_heap(cmd), |
| 1429 |
.index_buffer = draw.index_buffer, |
| 1430 |
.in_draw = draw.b.ptr, |
| 1431 |
.out_draw = hk_pool_alloc(cmd, 5 * sizeof(uint32_t) * draw_count, 4).gpu, |
| 1432 |
.max_draws = 1 /* TODO: MDI */, |
| 1433 |
.restart_index = gfx->index.restart, |
| 1434 |
.index_buffer_size_el = agx_draw_index_range_el(draw), |
| 1435 |
.index_size_log2 = draw.index_size, |
| 1436 |
.flatshade_first = |
| 1437 |
dyn->rs.provoking_vertex == VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT, |
| 1438 |
}; |
| 1439 |
|
| 1440 |
libagx_unroll_restart_struct(cmd, agx_1d(1024 * draw_count), |
| 1441 |
AGX_BARRIER_ALL | AGX_PREGFX, ia, |
| 1442 |
poly_compact_prim(prim)); |
| 1443 |
|
| 1444 |
return agx_draw_indexed_indirect(ia.out_draw, dev->heap->va->addr, |
| 1445 |
dev->heap->size, draw.index_size, |
| 1446 |
false /* restart */); |
| 1447 |
} |
| 1448 |
|
| 1449 |
static struct agx_draw |
| 1450 |
hk_launch_gs_prerast(struct hk_cmd_buffer *cmd, struct hk_cs *cs, |
| 1451 |
struct agx_draw draw) |
| 1452 |
{ |
| 1453 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1454 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1455 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 1456 |
struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY]; |
| 1457 |
struct agx_grid grid_vs, grid_gs; |
| 1458 |
|
| 1459 |
struct hk_shader *vs = hk_bound_sw_vs_before_gs(gfx); |
| 1460 |
struct hk_shader *main = hk_main_gs_variant(gs); |
| 1461 |
struct hk_shader *count = hk_count_gs_variant(gs); |
| 1462 |
struct hk_shader *pre_gs = hk_pre_gs_variant(gs); |
| 1463 |
|
| 1464 |
uint64_t vertex_params = desc->root.draw.vertex_params; |
| 1465 |
uint64_t geometry_params = desc->root.draw.geometry_params; |
| 1466 |
unsigned count_words = count->info.gs.count_words; |
| 1467 |
struct agx_workgroup wg = agx_workgroup(64, 1, 1); |
| 1468 |
|
| 1469 |
if (false /* TODO */) |
| 1470 |
perf_debug(cmd, "Transform feedbck"); |
| 1471 |
else if (count_words) |
| 1472 |
perf_debug(cmd, "Geometry shader with counts"); |
| 1473 |
else |
| 1474 |
perf_debug(cmd, "Geometry shader without counts"); |
| 1475 |
|
| 1476 |
enum mesa_prim mode = hk_gs_in_prim(cmd); |
| 1477 |
|
| 1478 |
if (draw.restart) { |
| 1479 |
draw = hk_draw_without_restart(cmd, draw, 1); |
| 1480 |
mode = u_decomposed_prim(mode); |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* Setup grids */ |
| 1484 |
if (agx_is_indirect(draw.b)) { |
| 1485 |
struct libagx_gs_setup_indirect_args gsi = { |
| 1486 |
.heap = hk_heap(cmd), |
| 1487 |
.index_buffer = draw.index_buffer, |
| 1488 |
.draw = draw.b.ptr, |
| 1489 |
.vp = desc->root.draw.vertex_params, |
| 1490 |
.p = desc->root.draw.geometry_params, |
| 1491 |
.vs_outputs = vs->b.info.outputs, |
| 1492 |
.prim = mode, |
| 1493 |
.is_prefix_summing = count->info.gs.prefix_sum, |
| 1494 |
.max_indices = count->info.gs.max_indices, |
| 1495 |
.shape = count->info.gs.shape, |
| 1496 |
}; |
| 1497 |
|
| 1498 |
if (draw.indexed) { |
| 1499 |
gsi.index_size_B = agx_index_size_to_B(draw.index_size); |
| 1500 |
gsi.index_buffer_range_el = agx_draw_index_range_el(draw); |
| 1501 |
} |
| 1502 |
|
| 1503 |
libagx_gs_setup_indirect_struct(cmd, agx_1d(1), |
| 1504 |
AGX_BARRIER_ALL | AGX_PREGFX, gsi); |
| 1505 |
|
| 1506 |
grid_vs = agx_grid_indirect_local( |
| 1507 |
vertex_params + offsetof(struct poly_vertex_params, grid)); |
| 1508 |
|
| 1509 |
grid_gs = agx_grid_indirect_local( |
| 1510 |
geometry_params + offsetof(struct poly_geometry_params, grid)); |
| 1511 |
} else { |
| 1512 |
grid_vs = grid_gs = draw.b; |
| 1513 |
grid_gs.count[0] = u_decomposed_prims_for_vertices(mode, draw.b.count[0]); |
| 1514 |
} |
| 1515 |
|
| 1516 |
/* Launch the vertex shader first */ |
| 1517 |
hk_reserve_scratch(cmd, cs, vs); |
| 1518 |
hk_dispatch_with_usc(dev, cs, &vs->b.info, |
| 1519 |
hk_upload_usc_words(cmd, vs, |
| 1520 |
vs->info.stage == MESA_SHADER_VERTEX |
| 1521 |
? gfx->linked[MESA_SHADER_VERTEX] |
| 1522 |
: vs->only_linked), |
| 1523 |
grid_vs, wg); |
| 1524 |
|
| 1525 |
/* Transform feedback and various queries require extra dispatching, |
| 1526 |
* determine if we need that here. |
| 1527 |
*/ |
| 1528 |
VkQueryPipelineStatisticFlagBits gs_queries = |
| 1529 |
VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT | |
| 1530 |
VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT | |
| 1531 |
VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | |
| 1532 |
VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT; |
| 1533 |
|
| 1534 |
struct hk_root_descriptor_table *root = &cmd->state.gfx.descriptors.root; |
| 1535 |
bool xfb_or_queries = |
| 1536 |
main->info.gs.xfb || (root->draw.pipeline_stats_flags & gs_queries); |
| 1537 |
|
| 1538 |
if (xfb_or_queries) { |
| 1539 |
/* If we need counts, launch the count shader and prefix sum the results. */ |
| 1540 |
if (count_words) { |
| 1541 |
perf_debug(dev, "Geometry shader count"); |
| 1542 |
hk_dispatch_with_local_size(cmd, cs, count, grid_gs, wg); |
| 1543 |
} |
| 1544 |
|
| 1545 |
if (count->info.gs.prefix_sum) { |
| 1546 |
perf_debug(dev, "Geometry shader transform feedback prefix sum"); |
| 1547 |
libagx_prefix_sum_geom(cmd, agx_1d(1024 * count_words), |
| 1548 |
AGX_BARRIER_ALL | AGX_PREGFX, geometry_params); |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* Transform feedback / query program */ |
| 1552 |
perf_debug(dev, "Transform feedback / geometry query"); |
| 1553 |
hk_dispatch_with_local_size(cmd, cs, pre_gs, agx_1d(1), |
| 1554 |
agx_workgroup(1, 1, 1)); |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* Pre-rast geometry shader */ |
| 1558 |
hk_dispatch_with_local_size(cmd, cs, main, grid_gs, wg); |
| 1559 |
|
| 1560 |
if (poly_gs_indexed(count->info.gs.shape)) { |
| 1561 |
enum agx_index_size index_size = |
| 1562 |
agx_translate_index_size(poly_gs_index_size(count->info.gs.shape)); |
| 1563 |
|
| 1564 |
if (agx_is_indirect(draw.b)) { |
| 1565 |
return agx_draw_indexed_indirect( |
| 1566 |
geometry_params + offsetof(struct poly_geometry_params, draw), |
| 1567 |
cmd->geom_index_buffer, cmd->geom_index_count, index_size, true); |
| 1568 |
} else { |
| 1569 |
return agx_draw_indexed(cmd->geom_index_count, |
| 1570 |
cmd->geom_instance_count, 0, 0, 0, |
| 1571 |
cmd->geom_index_buffer, |
| 1572 |
cmd->geom_index_count * 4, index_size, true); |
| 1573 |
} |
| 1574 |
} else { |
| 1575 |
if (agx_is_indirect(draw.b)) { |
| 1576 |
return agx_draw_indirect(geometry_params + |
| 1577 |
offsetof(struct poly_geometry_params, draw)); |
| 1578 |
} else { |
| 1579 |
return (struct agx_draw){ |
| 1580 |
.b = agx_3d(cmd->geom_index_count, cmd->geom_instance_count, 1), |
| 1581 |
}; |
| 1582 |
} |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
static struct agx_draw |
| 1587 |
hk_launch_tess(struct hk_cmd_buffer *cmd, struct hk_cs *cs, |
| 1588 |
struct agx_draw draw, uint64_t c_prims, uint64_t c_inv) |
| 1589 |
{ |
| 1590 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1591 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1592 |
struct agx_grid grid_vs, grid_tcs, grid_tess; |
| 1593 |
|
| 1594 |
struct hk_shader *vs = hk_bound_sw_vs(gfx); |
| 1595 |
struct hk_shader *tcs = hk_only_variant(gfx->shaders[MESA_SHADER_TESS_CTRL]); |
| 1596 |
|
| 1597 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1598 |
uint32_t input_patch_size = dyn->ts.patch_control_points; |
| 1599 |
uint64_t state = gfx->descriptors.root.draw.tess_params; |
| 1600 |
struct hk_tess_info info = gfx->tess.info; |
| 1601 |
|
| 1602 |
perf_debug(cmd, "Tessellation"); |
| 1603 |
|
| 1604 |
uint64_t tcs_stat = hk_pipeline_stat_addr( |
| 1605 |
cmd, VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT); |
| 1606 |
|
| 1607 |
/* Setup grids */ |
| 1608 |
if (agx_is_indirect(draw.b)) { |
| 1609 |
perf_debug(cmd, "Indirect tessellation"); |
| 1610 |
|
| 1611 |
struct libagx_tess_setup_indirect_args args = { |
| 1612 |
.p = state, |
| 1613 |
.grids = gfx->tess.grids, |
| 1614 |
.indirect = draw.b.ptr, |
| 1615 |
.vp = gfx->descriptors.root.draw.vertex_params, |
| 1616 |
.vertex_outputs = vs->b.info.outputs, |
| 1617 |
.tcs_statistic = hk_pipeline_stat_addr( |
| 1618 |
cmd, |
| 1619 |
VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT), |
| 1620 |
}; |
| 1621 |
|
| 1622 |
if (draw.indexed) { |
| 1623 |
args.in_index_buffer = draw.index_buffer; |
| 1624 |
args.in_index_size_B = agx_index_size_to_B(draw.index_size); |
| 1625 |
args.in_index_buffer_range_el = agx_draw_index_range_el(draw); |
| 1626 |
} |
| 1627 |
|
| 1628 |
libagx_tess_setup_indirect_struct(cmd, agx_1d(1), |
| 1629 |
AGX_BARRIER_ALL | AGX_PREGFX, args); |
| 1630 |
|
| 1631 |
uint32_t grid_stride = sizeof(uint32_t) * 6; |
| 1632 |
grid_vs = agx_grid_indirect_local(gfx->tess.grids + 0 * grid_stride); |
| 1633 |
grid_tcs = agx_grid_indirect_local(gfx->tess.grids + 1 * grid_stride); |
| 1634 |
grid_tess = agx_grid_indirect_local(gfx->tess.grids + 2 * grid_stride); |
| 1635 |
} else { |
| 1636 |
uint32_t patches = draw.b.count[0] / input_patch_size; |
| 1637 |
grid_vs = grid_tcs = draw.b; |
| 1638 |
|
| 1639 |
grid_tcs.count[0] = patches * tcs->info.tess.tcs_output_patch_size; |
| 1640 |
grid_tess = agx_1d(patches * draw.b.count[1]); |
| 1641 |
|
| 1642 |
/* TCS invocation counter increments once per-patch */ |
| 1643 |
if (hk_stat_enabled(tcs_stat)) { |
| 1644 |
perf_debug(cmd, "Direct TCS statistic"); |
| 1645 |
libagx_increment_statistic( |
| 1646 |
cmd, agx_1d(1), AGX_BARRIER_ALL | AGX_PREGFX, tcs_stat, patches); |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
/* First launch the VS and TCS */ |
| 1651 |
hk_reserve_scratch(cmd, cs, vs); |
| 1652 |
hk_reserve_scratch(cmd, cs, tcs); |
| 1653 |
|
| 1654 |
hk_dispatch_with_usc( |
| 1655 |
dev, cs, &vs->b.info, |
| 1656 |
hk_upload_usc_words(cmd, vs, gfx->linked[MESA_SHADER_VERTEX]), grid_vs, |
| 1657 |
agx_workgroup(64, 1, 1)); |
| 1658 |
|
| 1659 |
hk_dispatch_with_usc( |
| 1660 |
dev, cs, &tcs->b.info, hk_upload_usc_words(cmd, tcs, tcs->only_linked), |
| 1661 |
grid_tcs, agx_workgroup(tcs->info.tess.tcs_output_patch_size, 1, 1)); |
| 1662 |
|
| 1663 |
/* First generate counts, then prefix sum them, and then tessellate. */ |
| 1664 |
libagx_tessellate(cmd, grid_tess, AGX_BARRIER_ALL | AGX_PREGFX, info.mode, |
| 1665 |
POLY_TESS_MODE_COUNT, state); |
| 1666 |
|
| 1667 |
libagx_prefix_sum_tess(cmd, agx_1d(1024), AGX_BARRIER_ALL | AGX_PREGFX, |
| 1668 |
state, c_prims, c_inv, c_prims || c_inv); |
| 1669 |
|
| 1670 |
libagx_tessellate(cmd, grid_tess, AGX_BARRIER_ALL | AGX_PREGFX, info.mode, |
| 1671 |
POLY_TESS_MODE_WITH_COUNTS, state); |
| 1672 |
|
| 1673 |
return agx_draw_indexed_indirect(gfx->tess.out_draws, dev->heap->va->addr, |
| 1674 |
dev->heap->size, AGX_INDEX_SIZE_U32, false); |
| 1675 |
} |
| 1676 |
|
| 1677 |
void |
| 1678 |
hk_cmd_bind_graphics_shader(struct hk_cmd_buffer *cmd, |
| 1679 |
const mesa_shader_stage stage, |
| 1680 |
struct hk_api_shader *shader) |
| 1681 |
{ |
| 1682 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1683 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1684 |
|
| 1685 |
/* Null fragment shaders are annoying to handle, because we may still need an |
| 1686 |
* actual fragment shader to attach a prolog to. Rather than adding special |
| 1687 |
* cases, we just bind an empty fragment shader instead of NULL to make |
| 1688 |
* everything work correctly. |
| 1689 |
*/ |
| 1690 |
if (stage == MESA_SHADER_FRAGMENT && shader == NULL) { |
| 1691 |
shader = dev->null_fs; |
| 1692 |
} |
| 1693 |
|
| 1694 |
assert(stage < ARRAY_SIZE(cmd->state.gfx.shaders)); |
| 1695 |
if (cmd->state.gfx.shaders[stage] == shader) |
| 1696 |
return; |
| 1697 |
|
| 1698 |
cmd->state.gfx.shaders[stage] = shader; |
| 1699 |
cmd->state.gfx.shaders_dirty |= BITFIELD_BIT(stage); |
| 1700 |
|
| 1701 |
if (stage == MESA_SHADER_FRAGMENT) { |
| 1702 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_MS_RASTERIZATION_SAMPLES); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
static void |
| 1707 |
hk_flush_shaders(struct hk_cmd_buffer *cmd) |
| 1708 |
{ |
| 1709 |
if (cmd->state.gfx.shaders_dirty == 0) |
| 1710 |
return; |
| 1711 |
|
| 1712 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 1713 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 1714 |
desc->root_dirty = true; |
| 1715 |
|
| 1716 |
/* Geometry shading overrides the restart index, reemit on rebind */ |
| 1717 |
if (IS_SHADER_DIRTY(GEOMETRY)) { |
| 1718 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 1719 |
struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY]; |
| 1720 |
|
| 1721 |
desc->root.draw.api_gs = gs && !gs->is_passthrough; |
| 1722 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE); |
| 1723 |
} |
| 1724 |
|
| 1725 |
struct hk_shader *hw_vs = hk_bound_hw_vs(gfx); |
| 1726 |
struct hk_api_shader *fs = gfx->shaders[MESA_SHADER_FRAGMENT]; |
| 1727 |
|
| 1728 |
/* If we have a new VS/FS pair, UVS locations may have changed so need to |
| 1729 |
* relink. We do this here because there's no dependence on the fast linked |
| 1730 |
* shaders. |
| 1731 |
*/ |
| 1732 |
agx_assign_uvs(&gfx->linked_varyings, &hw_vs->info.uvs, |
| 1733 |
hk_only_variant(fs)->info.fs.interp.flat, |
| 1734 |
hk_only_variant(fs)->info.fs.interp.linear); |
| 1735 |
|
| 1736 |
for (unsigned i = 0; i < VARYING_SLOT_MAX; ++i) { |
| 1737 |
desc->root.draw.uvs_index[i] = gfx->linked_varyings.slots[i]; |
| 1738 |
} |
| 1739 |
} |
| 1740 |
|
| 1741 |
static struct agx_shader_part * |
| 1742 |
hk_get_prolog_epilog_locked(struct hk_device *dev, struct hk_internal_key *key, |
| 1743 |
hk_internal_builder_t builder, bool preprocess_nir, |
| 1744 |
bool stop, unsigned cf_base) |
| 1745 |
{ |
| 1746 |
/* Try to get the cached shader */ |
| 1747 |
struct hash_entry *ent = _mesa_hash_table_search(dev->prolog_epilog.ht, key); |
| 1748 |
if (ent) |
| 1749 |
return ent->data; |
| 1750 |
|
| 1751 |
nir_builder b = nir_builder_init_simple_shader(0, &agx_nir_options, NULL); |
| 1752 |
builder(&b, key->key); |
| 1753 |
|
| 1754 |
if (preprocess_nir) |
| 1755 |
agx_preprocess_nir(b.shader); |
| 1756 |
|
| 1757 |
struct agx_shader_key backend_key = { |
| 1758 |
.dev = agx_gather_device_key(&dev->dev), |
| 1759 |
.secondary = true, |
| 1760 |
.no_stop = !stop, |
| 1761 |
}; |
| 1762 |
|
| 1763 |
/* We always use dynamic sample shading in the GL driver. Indicate that. */ |
| 1764 |
if (b.shader->info.stage == MESA_SHADER_FRAGMENT) { |
| 1765 |
backend_key.fs.cf_base = cf_base; |
| 1766 |
|
| 1767 |
if (b.shader->info.fs.uses_sample_shading) |
| 1768 |
backend_key.fs.inside_sample_loop = true; |
| 1769 |
} |
| 1770 |
|
| 1771 |
struct agx_shader_part *part = |
| 1772 |
rzalloc(dev->prolog_epilog.ht, struct agx_shader_part); |
| 1773 |
|
| 1774 |
agx_compile_shader_nir(b.shader, &backend_key, part); |
| 1775 |
|
| 1776 |
ralloc_free(b.shader); |
| 1777 |
|
| 1778 |
/* ..and cache it before we return. The key is on the stack right now, so |
| 1779 |
* clone it before using it as a hash table key. The clone is logically owned |
| 1780 |
* by the hash table. |
| 1781 |
*/ |
| 1782 |
size_t total_key_size = sizeof(*key) + key->key_size; |
| 1783 |
void *cloned_key = ralloc_memdup(dev->prolog_epilog.ht, key, total_key_size); |
| 1784 |
|
| 1785 |
_mesa_hash_table_insert(dev->prolog_epilog.ht, cloned_key, part); |
| 1786 |
return part; |
| 1787 |
} |
| 1788 |
|
| 1789 |
static struct agx_shader_part * |
| 1790 |
hk_get_prolog_epilog(struct hk_device *dev, void *data, size_t data_size, |
| 1791 |
hk_internal_builder_t builder, bool preprocess_nir, |
| 1792 |
bool stop, unsigned cf_base) |
| 1793 |
{ |
| 1794 |
/* Build the meta shader key */ |
| 1795 |
size_t total_key_size = sizeof(struct hk_internal_key) + data_size; |
| 1796 |
|
| 1797 |
struct hk_internal_key *key = alloca(total_key_size); |
| 1798 |
key->builder = builder; |
| 1799 |
key->key_size = data_size; |
| 1800 |
|
| 1801 |
if (data_size) |
| 1802 |
memcpy(key->key, data, data_size); |
| 1803 |
|
| 1804 |
simple_mtx_lock(&dev->prolog_epilog.lock); |
| 1805 |
|
| 1806 |
struct agx_shader_part *part = hk_get_prolog_epilog_locked( |
| 1807 |
dev, key, builder, preprocess_nir, stop, cf_base); |
| 1808 |
|
| 1809 |
simple_mtx_unlock(&dev->prolog_epilog.lock); |
| 1810 |
return part; |
| 1811 |
} |
| 1812 |
|
| 1813 |
static struct hk_linked_shader * |
| 1814 |
hk_get_fast_linked_locked_vs(struct hk_device *dev, struct hk_shader *shader, |
| 1815 |
struct hk_fast_link_key_vs *key) |
| 1816 |
{ |
| 1817 |
struct agx_shader_part *prolog = |
| 1818 |
hk_get_prolog_epilog(dev, &key->prolog, sizeof(key->prolog), |
| 1819 |
agx_nir_vs_prolog, false, false, 0); |
| 1820 |
|
| 1821 |
struct hk_linked_shader *linked = |
| 1822 |
hk_fast_link(dev, false, shader, prolog, NULL, 0); |
| 1823 |
|
| 1824 |
struct hk_fast_link_key *key_clone = |
| 1825 |
ralloc_memdup(shader->linked.ht, key, sizeof(*key)); |
| 1826 |
|
| 1827 |
/* XXX: Fix this higher up the stack */ |
| 1828 |
linked->sw_indexing = !key->prolog.hw || key->prolog.adjacency; |
| 1829 |
linked->b.uses_base_param |= linked->sw_indexing; |
| 1830 |
|
| 1831 |
_mesa_hash_table_insert(shader->linked.ht, key_clone, linked); |
| 1832 |
return linked; |
| 1833 |
} |
| 1834 |
|
| 1835 |
static bool |
| 1836 |
lower_fs_root(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 1837 |
{ |
| 1838 |
if (intr->intrinsic != nir_intrinsic_load_root_agx) |
| 1839 |
return false; |
| 1840 |
|
| 1841 |
b->cursor = nir_before_instr(&intr->instr); |
| 1842 |
nir_def_replace(&intr->def, |
| 1843 |
nir_load_preamble(b, 1, 64, .base = AGX_ABI_FUNI_ROOT)); |
| 1844 |
return true; |
| 1845 |
} |
| 1846 |
|
| 1847 |
static void |
| 1848 |
build_fs_prolog(nir_builder *b, const void *key) |
| 1849 |
{ |
| 1850 |
agx_nir_fs_prolog(b, key); |
| 1851 |
|
| 1852 |
/* Lower load_stat_query_address_agx, needed for FS statistics */ |
| 1853 |
NIR_PASS(_, b->shader, hk_lower_uvs_index, MESA_SHADER_FRAGMENT, 0); |
| 1854 |
NIR_PASS(_, b->shader, nir_shader_intrinsics_pass, lower_fs_root, |
| 1855 |
nir_metadata_control_flow, NULL); |
| 1856 |
} |
| 1857 |
|
| 1858 |
static struct hk_linked_shader * |
| 1859 |
hk_get_fast_linked_locked_fs(struct hk_device *dev, struct hk_shader *shader, |
| 1860 |
struct hk_fast_link_key_fs *key) |
| 1861 |
{ |
| 1862 |
/* TODO: prolog without fs needs to work too... */ |
| 1863 |
bool needs_prolog = key->prolog.statistics || |
| 1864 |
key->prolog.cull_distance_size || |
| 1865 |
key->prolog.api_sample_mask != 0xff; |
| 1866 |
|
| 1867 |
struct agx_shader_part *prolog = NULL; |
| 1868 |
if (needs_prolog) { |
| 1869 |
prolog = hk_get_prolog_epilog(dev, &key->prolog, sizeof(key->prolog), |
| 1870 |
build_fs_prolog, false, false, |
| 1871 |
key->prolog.cf_base); |
| 1872 |
} |
| 1873 |
|
| 1874 |
/* If sample shading is used, don't stop at the epilog, there's a |
| 1875 |
* footer that the fast linker will insert to stop. |
| 1876 |
*/ |
| 1877 |
bool epilog_stop = (key->nr_samples_shaded == 0); |
| 1878 |
|
| 1879 |
struct agx_shader_part *epilog = |
| 1880 |
hk_get_prolog_epilog(dev, &key->epilog, sizeof(key->epilog), |
| 1881 |
agx_nir_fs_epilog, true, epilog_stop, 0); |
| 1882 |
|
| 1883 |
struct hk_linked_shader *linked = |
| 1884 |
hk_fast_link(dev, true, shader, prolog, epilog, key->nr_samples_shaded); |
| 1885 |
|
| 1886 |
struct hk_fast_link_key *key_clone = |
| 1887 |
ralloc_memdup(shader->linked.ht, key, sizeof(*key)); |
| 1888 |
|
| 1889 |
_mesa_hash_table_insert(shader->linked.ht, key_clone, linked); |
| 1890 |
return linked; |
| 1891 |
} |
| 1892 |
|
| 1893 |
/* |
| 1894 |
* First, look for a fully linked variant. Else, build the required shader |
| 1895 |
* parts and link. |
| 1896 |
*/ |
| 1897 |
static struct hk_linked_shader * |
| 1898 |
hk_get_fast_linked(struct hk_device *dev, struct hk_shader *shader, void *key) |
| 1899 |
{ |
| 1900 |
struct hk_linked_shader *linked; |
| 1901 |
simple_mtx_lock(&shader->linked.lock); |
| 1902 |
|
| 1903 |
struct hash_entry *ent = _mesa_hash_table_search(shader->linked.ht, key); |
| 1904 |
|
| 1905 |
if (ent) |
| 1906 |
linked = ent->data; |
| 1907 |
else if (shader->info.stage == MESA_SHADER_VERTEX) |
| 1908 |
linked = hk_get_fast_linked_locked_vs(dev, shader, key); |
| 1909 |
else if (shader->info.stage == MESA_SHADER_FRAGMENT) |
| 1910 |
linked = hk_get_fast_linked_locked_fs(dev, shader, key); |
| 1911 |
else |
| 1912 |
UNREACHABLE("invalid stage"); |
| 1913 |
|
| 1914 |
simple_mtx_unlock(&shader->linked.lock); |
| 1915 |
return linked; |
| 1916 |
} |
| 1917 |
|
| 1918 |
static void |
| 1919 |
hk_update_fast_linked(struct hk_cmd_buffer *cmd, struct hk_shader *shader, |
| 1920 |
void *key) |
| 1921 |
{ |
| 1922 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 1923 |
struct hk_linked_shader *new = hk_get_fast_linked(dev, shader, key); |
| 1924 |
mesa_shader_stage stage = shader->info.stage; |
| 1925 |
|
| 1926 |
if (cmd->state.gfx.linked[stage] != new) { |
| 1927 |
cmd->state.gfx.linked[stage] = new; |
| 1928 |
cmd->state.gfx.linked_dirty |= BITFIELD_BIT(stage); |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
static enum agx_polygon_mode |
| 1933 |
translate_polygon_mode(VkPolygonMode vk_mode) |
| 1934 |
{ |
| 1935 |
static_assert((enum agx_polygon_mode)VK_POLYGON_MODE_FILL == |
| 1936 |
AGX_POLYGON_MODE_FILL); |
| 1937 |
static_assert((enum agx_polygon_mode)VK_POLYGON_MODE_LINE == |
| 1938 |
AGX_POLYGON_MODE_LINE); |
| 1939 |
static_assert((enum agx_polygon_mode)VK_POLYGON_MODE_POINT == |
| 1940 |
AGX_POLYGON_MODE_POINT); |
| 1941 |
|
| 1942 |
assert(vk_mode <= VK_POLYGON_MODE_POINT); |
| 1943 |
return (enum agx_polygon_mode)vk_mode; |
| 1944 |
} |
| 1945 |
|
| 1946 |
static enum agx_zs_func |
| 1947 |
translate_compare_op(VkCompareOp vk_mode) |
| 1948 |
{ |
| 1949 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_NEVER == AGX_ZS_FUNC_NEVER); |
| 1950 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_LESS == AGX_ZS_FUNC_LESS); |
| 1951 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_EQUAL == AGX_ZS_FUNC_EQUAL); |
| 1952 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_LESS_OR_EQUAL == |
| 1953 |
AGX_ZS_FUNC_LEQUAL); |
| 1954 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_GREATER == |
| 1955 |
AGX_ZS_FUNC_GREATER); |
| 1956 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_NOT_EQUAL == |
| 1957 |
AGX_ZS_FUNC_NOT_EQUAL); |
| 1958 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_GREATER_OR_EQUAL == |
| 1959 |
AGX_ZS_FUNC_GEQUAL); |
| 1960 |
static_assert((enum agx_zs_func)VK_COMPARE_OP_ALWAYS == AGX_ZS_FUNC_ALWAYS); |
| 1961 |
|
| 1962 |
assert(vk_mode <= VK_COMPARE_OP_ALWAYS); |
| 1963 |
return (enum agx_zs_func)vk_mode; |
| 1964 |
} |
| 1965 |
|
| 1966 |
static enum agx_stencil_op |
| 1967 |
translate_stencil_op(VkStencilOp vk_op) |
| 1968 |
{ |
| 1969 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_KEEP == |
| 1970 |
AGX_STENCIL_OP_KEEP); |
| 1971 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_ZERO == |
| 1972 |
AGX_STENCIL_OP_ZERO); |
| 1973 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_REPLACE == |
| 1974 |
AGX_STENCIL_OP_REPLACE); |
| 1975 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_INCREMENT_AND_CLAMP == |
| 1976 |
AGX_STENCIL_OP_INCR_SAT); |
| 1977 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_DECREMENT_AND_CLAMP == |
| 1978 |
AGX_STENCIL_OP_DECR_SAT); |
| 1979 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_INVERT == |
| 1980 |
AGX_STENCIL_OP_INVERT); |
| 1981 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_INCREMENT_AND_WRAP == |
| 1982 |
AGX_STENCIL_OP_INCR_WRAP); |
| 1983 |
static_assert((enum agx_stencil_op)VK_STENCIL_OP_DECREMENT_AND_WRAP == |
| 1984 |
AGX_STENCIL_OP_DECR_WRAP); |
| 1985 |
|
| 1986 |
return (enum agx_stencil_op)vk_op; |
| 1987 |
} |
| 1988 |
|
| 1989 |
static void |
| 1990 |
hk_ppp_push_stencil_face(struct agx_ppp_update *ppp, |
| 1991 |
struct vk_stencil_test_face_state s, bool enabled) |
| 1992 |
{ |
| 1993 |
if (enabled) { |
| 1994 |
agx_ppp_push(ppp, FRAGMENT_STENCIL, cfg) { |
| 1995 |
cfg.compare = translate_compare_op(s.op.compare); |
| 1996 |
cfg.write_mask = s.write_mask; |
| 1997 |
cfg.read_mask = s.compare_mask; |
| 1998 |
|
| 1999 |
cfg.depth_pass = translate_stencil_op(s.op.pass); |
| 2000 |
cfg.depth_fail = translate_stencil_op(s.op.depth_fail); |
| 2001 |
cfg.stencil_fail = translate_stencil_op(s.op.fail); |
| 2002 |
} |
| 2003 |
} else { |
| 2004 |
agx_ppp_push(ppp, FRAGMENT_STENCIL, cfg) { |
| 2005 |
cfg.compare = AGX_ZS_FUNC_ALWAYS; |
| 2006 |
cfg.write_mask = 0xFF; |
| 2007 |
cfg.read_mask = 0xFF; |
| 2008 |
|
| 2009 |
cfg.depth_pass = AGX_STENCIL_OP_KEEP; |
| 2010 |
cfg.depth_fail = AGX_STENCIL_OP_KEEP; |
| 2011 |
cfg.stencil_fail = AGX_STENCIL_OP_KEEP; |
| 2012 |
} |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
static bool |
| 2017 |
hk_stencil_test_enabled(struct hk_cmd_buffer *cmd) |
| 2018 |
{ |
| 2019 |
const struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 2020 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 2021 |
|
| 2022 |
return dyn->ds.stencil.test_enable && |
| 2023 |
render->stencil_att.vk_format != VK_FORMAT_UNDEFINED; |
| 2024 |
} |
| 2025 |
|
| 2026 |
static void |
| 2027 |
hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) |
| 2028 |
{ |
| 2029 |
const struct vk_dynamic_graphics_state *dyn = |
| 2030 |
&cmd->vk.dynamic_graphics_state; |
| 2031 |
|
| 2032 |
/* We always need at least 1 viewport for the hardware. With rasterizer |
| 2033 |
* discard the app may not supply any, but we can just program garbage. |
| 2034 |
*/ |
| 2035 |
unsigned count = MAX2(dyn->vp.viewport_count, 1); |
| 2036 |
|
| 2037 |
unsigned minx[HK_MAX_VIEWPORTS] = {0}, miny[HK_MAX_VIEWPORTS] = {0}; |
| 2038 |
unsigned maxx[HK_MAX_VIEWPORTS] = {0}, maxy[HK_MAX_VIEWPORTS] = {0}; |
| 2039 |
|
| 2040 |
/* We implicitly scissor to the viewport. We need to do a min/max dance to |
| 2041 |
* handle inverted viewports. |
| 2042 |
*/ |
| 2043 |
for (uint32_t i = 0; i < dyn->vp.viewport_count; i++) { |
| 2044 |
const VkViewport *vp = &dyn->vp.viewports[i]; |
| 2045 |
|
| 2046 |
minx[i] = MIN2(vp->x, vp->x + vp->width); |
| 2047 |
miny[i] = MIN2(vp->y, vp->y + vp->height); |
| 2048 |
maxx[i] = MAX2(vp->x, vp->x + vp->width); |
| 2049 |
maxy[i] = MAX2(vp->y, vp->y + vp->height); |
| 2050 |
} |
| 2051 |
|
| 2052 |
/* Additionally clamp to the framebuffer so we don't rasterize |
| 2053 |
* off-screen pixels. TODO: Is this necessary? the GL driver does this but |
| 2054 |
* it might be cargoculted at this point. |
| 2055 |
*/ |
| 2056 |
for (unsigned i = 0; i < count; ++i) { |
| 2057 |
minx[i] = MIN2(minx[i], cmd->state.gfx.render.cr.width); |
| 2058 |
maxx[i] = MIN2(maxx[i], cmd->state.gfx.render.cr.width); |
| 2059 |
miny[i] = MIN2(miny[i], cmd->state.gfx.render.cr.height); |
| 2060 |
maxy[i] = MIN2(maxy[i], cmd->state.gfx.render.cr.height); |
| 2061 |
} |
| 2062 |
|
| 2063 |
/* We additionally apply any API scissors */ |
| 2064 |
for (unsigned i = 0; i < dyn->vp.scissor_count; ++i) { |
| 2065 |
const VkRect2D *s = &dyn->vp.scissors[i]; |
| 2066 |
|
| 2067 |
minx[i] = MAX2(minx[i], s->offset.x); |
| 2068 |
miny[i] = MAX2(miny[i], s->offset.y); |
| 2069 |
maxx[i] = MIN2(maxx[i], s->offset.x + s->extent.width); |
| 2070 |
maxy[i] = MIN2(maxy[i], s->offset.y + s->extent.height); |
| 2071 |
} |
| 2072 |
|
| 2073 |
/* Upload a hardware scissor for each viewport, whether there's a |
| 2074 |
* corresponding API scissor or not. |
| 2075 |
*/ |
| 2076 |
unsigned index = cs->scissor.size / AGX_SCISSOR_LENGTH; |
| 2077 |
struct agx_scissor_packed *scissors = |
| 2078 |
util_dynarray_grow_bytes(&cs->scissor, count, AGX_SCISSOR_LENGTH); |
| 2079 |
|
| 2080 |
for (unsigned i = 0; i < count; ++i) { |
| 2081 |
const VkViewport *vp = &dyn->vp.viewports[i]; |
| 2082 |
|
| 2083 |
agx_pack(scissors + i, SCISSOR, cfg) { |
| 2084 |
cfg.min_x = minx[i]; |
| 2085 |
cfg.min_y = miny[i]; |
| 2086 |
cfg.max_x = maxx[i]; |
| 2087 |
cfg.max_y = maxy[i]; |
| 2088 |
|
| 2089 |
/* These settings in conjunction with the PPP control depth clip/clamp |
| 2090 |
* settings implement depth clip/clamping. Properly setting them |
| 2091 |
* together is required for conformant depth clip enable. |
| 2092 |
* |
| 2093 |
* TODO: Reverse-engineer the finer interactions here. |
| 2094 |
*/ |
| 2095 |
if (dyn->rs.depth_clamp_enable) { |
| 2096 |
cfg.min_z = MIN2(vp->minDepth, vp->maxDepth); |
| 2097 |
cfg.max_z = MAX2(vp->minDepth, vp->maxDepth); |
| 2098 |
} else { |
| 2099 |
cfg.min_z = 0.0; |
| 2100 |
cfg.max_z = 1.0; |
| 2101 |
} |
| 2102 |
} |
| 2103 |
} |
| 2104 |
|
| 2105 |
/* Upload state */ |
| 2106 |
struct AGX_PPP_HEADER present = { |
| 2107 |
.depth_bias_scissor = true, |
| 2108 |
.region_clip = true, |
| 2109 |
.viewport = true, |
| 2110 |
.viewport_count = count, |
| 2111 |
}; |
| 2112 |
|
| 2113 |
size_t size = agx_ppp_update_size(&present); |
| 2114 |
struct agx_ptr T = hk_pool_alloc(cmd, size, AGX_PPP_HEADER_ALIGN); |
| 2115 |
if (!T.cpu) |
| 2116 |
return; |
| 2117 |
|
| 2118 |
struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present); |
| 2119 |
|
| 2120 |
agx_ppp_push(&ppp, DEPTH_BIAS_SCISSOR, cfg) { |
| 2121 |
cfg.scissor = index; |
| 2122 |
|
| 2123 |
/* Use the current depth bias, we allocate linearly */ |
| 2124 |
unsigned count = cs->depth_bias.size / AGX_DEPTH_BIAS_LENGTH; |
| 2125 |
cfg.depth_bias = count ? count - 1 : 0; |
| 2126 |
}; |
| 2127 |
|
| 2128 |
for (unsigned i = 0; i < count; ++i) { |
| 2129 |
agx_ppp_push(&ppp, REGION_CLIP, cfg) { |
| 2130 |
cfg.enable = true; |
| 2131 |
cfg.min_x = minx[i] / 32; |
| 2132 |
cfg.min_y = miny[i] / 32; |
| 2133 |
cfg.max_x = DIV_ROUND_UP(MAX2(maxx[i], 1), 32); |
| 2134 |
cfg.max_y = DIV_ROUND_UP(MAX2(maxy[i], 1), 32); |
| 2135 |
} |
| 2136 |
} |
| 2137 |
|
| 2138 |
agx_ppp_push(&ppp, VIEWPORT_CONTROL, cfg) |
| 2139 |
; |
| 2140 |
|
| 2141 |
/* Upload viewports */ |
| 2142 |
for (unsigned i = 0; i < count; ++i) { |
| 2143 |
const VkViewport *vp = &dyn->vp.viewports[i]; |
| 2144 |
|
| 2145 |
agx_ppp_push(&ppp, VIEWPORT, cfg) { |
| 2146 |
cfg.translate_x = vp->x + 0.5f * vp->width; |
| 2147 |
cfg.translate_y = vp->y + 0.5f * vp->height; |
| 2148 |
cfg.translate_z = vp->minDepth; |
| 2149 |
|
| 2150 |
cfg.scale_x = vp->width * 0.5f; |
| 2151 |
cfg.scale_y = vp->height * 0.5f; |
| 2152 |
cfg.scale_z = vp->maxDepth - vp->minDepth; |
| 2153 |
} |
| 2154 |
} |
| 2155 |
|
| 2156 |
agx_ppp_fini(out, &ppp); |
| 2157 |
} |
| 2158 |
|
| 2159 |
static enum agx_object_type |
| 2160 |
translate_object_type(enum mesa_prim topology) |
| 2161 |
{ |
| 2162 |
static_assert(MESA_PRIM_LINES < MESA_PRIM_LINE_STRIP); |
| 2163 |
static_assert(MESA_PRIM_TRIANGLES >= MESA_PRIM_LINE_STRIP); |
| 2164 |
|
| 2165 |
if (topology == MESA_PRIM_POINTS) |
| 2166 |
return AGX_OBJECT_TYPE_POINT_SPRITE_UV01; |
| 2167 |
else if (topology <= MESA_PRIM_LINE_STRIP) |
| 2168 |
return AGX_OBJECT_TYPE_LINE; |
| 2169 |
else |
| 2170 |
return AGX_OBJECT_TYPE_TRIANGLE; |
| 2171 |
} |
| 2172 |
|
| 2173 |
static enum agx_primitive |
| 2174 |
translate_hw_primitive_topology(enum mesa_prim prim) |
| 2175 |
{ |
| 2176 |
switch (prim) { |
| 2177 |
case MESA_PRIM_POINTS: |
| 2178 |
return AGX_PRIMITIVE_POINTS; |
| 2179 |
case MESA_PRIM_LINES: |
| 2180 |
return AGX_PRIMITIVE_LINES; |
| 2181 |
case MESA_PRIM_LINE_STRIP: |
| 2182 |
return AGX_PRIMITIVE_LINE_STRIP; |
| 2183 |
case MESA_PRIM_TRIANGLES: |
| 2184 |
return AGX_PRIMITIVE_TRIANGLES; |
| 2185 |
case MESA_PRIM_TRIANGLE_STRIP: |
| 2186 |
return AGX_PRIMITIVE_TRIANGLE_STRIP; |
| 2187 |
case MESA_PRIM_TRIANGLE_FAN: |
| 2188 |
return AGX_PRIMITIVE_TRIANGLE_FAN; |
| 2189 |
default: |
| 2190 |
UNREACHABLE("Invalid hardware primitive topology"); |
| 2191 |
} |
| 2192 |
} |
| 2193 |
|
| 2194 |
static inline enum agx_vdm_vertex |
| 2195 |
translate_vdm_vertex(unsigned vtx) |
| 2196 |
{ |
| 2197 |
static_assert(AGX_VDM_VERTEX_0 == 0); |
| 2198 |
static_assert(AGX_VDM_VERTEX_1 == 1); |
| 2199 |
static_assert(AGX_VDM_VERTEX_2 == 2); |
| 2200 |
|
| 2201 |
assert(vtx <= 2); |
| 2202 |
return vtx; |
| 2203 |
} |
| 2204 |
|
| 2205 |
static inline enum agx_ppp_vertex |
| 2206 |
translate_ppp_vertex(unsigned vtx) |
| 2207 |
{ |
| 2208 |
static_assert(AGX_PPP_VERTEX_0 == 0 + 1); |
| 2209 |
static_assert(AGX_PPP_VERTEX_1 == 1 + 1); |
| 2210 |
static_assert(AGX_PPP_VERTEX_2 == 2 + 1); |
| 2211 |
|
| 2212 |
assert(vtx <= 2); |
| 2213 |
return vtx + 1; |
| 2214 |
} |
| 2215 |
|
| 2216 |
static void |
| 2217 |
hk_flush_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs) |
| 2218 |
{ |
| 2219 |
struct hk_api_shader *gs = cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY]; |
| 2220 |
uint32_t index = cmd->state.gfx.index.restart; |
| 2221 |
|
| 2222 |
if (gs) { |
| 2223 |
enum poly_gs_shape shape = |
| 2224 |
gs->variants[HK_GS_VARIANT_COUNT].info.gs.shape; |
| 2225 |
index = BITFIELD_MASK(8 * poly_gs_index_size(shape)); |
| 2226 |
} |
| 2227 |
|
| 2228 |
/* VDM State updates are relatively expensive, so only emit them when the |
| 2229 |
* restart index changes. This is simpler than accurate dirty tracking. |
| 2230 |
*/ |
| 2231 |
if (cs->restart_index != index) { |
| 2232 |
uint8_t *out = cs->current; |
| 2233 |
agx_push(out, VDM_STATE, cfg) { |
| 2234 |
cfg.restart_index_present = true; |
| 2235 |
} |
| 2236 |
|
| 2237 |
agx_push(out, VDM_STATE_RESTART_INDEX, cfg) { |
| 2238 |
cfg.value = index; |
| 2239 |
} |
| 2240 |
|
| 2241 |
cs->current = out; |
| 2242 |
cs->restart_index = index; |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
/* |
| 2247 |
* Return the given sample positions, packed into a 32-bit word with fixed |
| 2248 |
* point nibbles for each x/y component of the (at most 4) samples. This is |
| 2249 |
* suitable for programming the PPP_MULTISAMPLECTL control register. |
| 2250 |
*/ |
| 2251 |
static uint32_t |
| 2252 |
hk_pack_ppp_multisamplectrl(const struct vk_sample_locations_state *sl) |
| 2253 |
{ |
| 2254 |
uint32_t ctrl = 0; |
| 2255 |
|
| 2256 |
for (int32_t i = sl->per_pixel - 1; i >= 0; i--) { |
| 2257 |
VkSampleLocationEXT loc = sl->locations[i]; |
| 2258 |
|
| 2259 |
uint32_t x = CLAMP(loc.x, 0.0f, 0.9375f) * 16.0; |
| 2260 |
uint32_t y = CLAMP(loc.y, 0.0f, 0.9375f) * 16.0; |
| 2261 |
|
| 2262 |
assert(x <= 15); |
| 2263 |
assert(y <= 15); |
| 2264 |
|
| 2265 |
/* Push bytes in reverse order so we can use constant shifts. */ |
| 2266 |
ctrl = (ctrl << 8) | (y << 4) | x; |
| 2267 |
} |
| 2268 |
|
| 2269 |
return ctrl; |
| 2270 |
} |
| 2271 |
|
| 2272 |
/* |
| 2273 |
* Return the standard sample positions, prepacked as above for efficiency. |
| 2274 |
*/ |
| 2275 |
uint32_t |
| 2276 |
hk_default_sample_positions(unsigned nr_samples) |
| 2277 |
{ |
| 2278 |
switch (nr_samples) { |
| 2279 |
case 0: |
| 2280 |
case 1: |
| 2281 |
return 0x88; |
| 2282 |
case 2: |
| 2283 |
return 0x44cc; |
| 2284 |
case 4: |
| 2285 |
return 0xeaa26e26; |
| 2286 |
default: |
| 2287 |
UNREACHABLE("Invalid sample count"); |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
static void |
| 2292 |
hk_flush_ppp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) |
| 2293 |
{ |
| 2294 |
const struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 2295 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 2296 |
|
| 2297 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 2298 |
struct hk_shader *hw_vs = hk_bound_hw_vs(gfx); |
| 2299 |
struct hk_shader *fs = hk_only_variant(gfx->shaders[MESA_SHADER_FRAGMENT]); |
| 2300 |
|
| 2301 |
bool hw_vs_dirty = IS_SHADER_DIRTY(VERTEX) || IS_SHADER_DIRTY(TESS_EVAL) || |
| 2302 |
IS_SHADER_DIRTY(GEOMETRY); |
| 2303 |
bool fs_dirty = IS_SHADER_DIRTY(FRAGMENT); |
| 2304 |
|
| 2305 |
struct hk_linked_shader *linked_fs = gfx->linked[MESA_SHADER_FRAGMENT]; |
| 2306 |
bool linked_fs_dirty = IS_LINKED_DIRTY(FRAGMENT); |
| 2307 |
|
| 2308 |
bool varyings_dirty = gfx->dirty & HK_DIRTY_VARYINGS; |
| 2309 |
|
| 2310 |
bool face_dirty = |
| 2311 |
IS_DIRTY(DS_DEPTH_TEST_ENABLE) || IS_DIRTY(DS_DEPTH_WRITE_ENABLE) || |
| 2312 |
IS_DIRTY(DS_DEPTH_COMPARE_OP) || IS_DIRTY(DS_STENCIL_REFERENCE) || |
| 2313 |
IS_DIRTY(RS_LINE_WIDTH) || IS_DIRTY(RS_POLYGON_MODE) || fs_dirty; |
| 2314 |
|
| 2315 |
bool stencil_face_dirty = |
| 2316 |
IS_DIRTY(DS_STENCIL_OP) || IS_DIRTY(DS_STENCIL_COMPARE_MASK) || |
| 2317 |
IS_DIRTY(DS_STENCIL_WRITE_MASK) || IS_DIRTY(DS_STENCIL_TEST_ENABLE); |
| 2318 |
|
| 2319 |
struct AGX_PPP_HEADER dirty = { |
| 2320 |
.fragment_control = |
| 2321 |
IS_DIRTY(DS_STENCIL_TEST_ENABLE) || IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || |
| 2322 |
IS_DIRTY(RS_DEPTH_BIAS_ENABLE) || gfx->dirty & HK_DIRTY_OCCLUSION, |
| 2323 |
|
| 2324 |
.fragment_control_2 = |
| 2325 |
IS_DIRTY(RS_RASTERIZER_DISCARD_ENABLE) || linked_fs_dirty, |
| 2326 |
|
| 2327 |
.fragment_front_face = face_dirty, |
| 2328 |
.fragment_front_face_2 = fs_dirty || IS_DIRTY(IA_PRIMITIVE_TOPOLOGY), |
| 2329 |
.fragment_front_stencil = stencil_face_dirty, |
| 2330 |
.fragment_back_face = face_dirty, |
| 2331 |
.fragment_back_face_2 = fs_dirty || IS_DIRTY(IA_PRIMITIVE_TOPOLOGY), |
| 2332 |
.fragment_back_stencil = stencil_face_dirty, |
| 2333 |
.output_select = hw_vs_dirty || linked_fs_dirty || varyings_dirty, |
| 2334 |
.varying_counts_32 = varyings_dirty, |
| 2335 |
.varying_counts_16 = varyings_dirty, |
| 2336 |
.cull = IS_DIRTY(RS_CULL_MODE) || |
| 2337 |
IS_DIRTY(RS_RASTERIZER_DISCARD_ENABLE) || |
| 2338 |
IS_DIRTY(RS_FRONT_FACE) || IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || |
| 2339 |
IS_DIRTY(RS_DEPTH_CLAMP_ENABLE) || IS_DIRTY(RS_LINE_MODE) || |
| 2340 |
IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || |
| 2341 |
(gfx->dirty & HK_DIRTY_PROVOKING) || IS_SHADER_DIRTY(TESS_CTRL) || |
| 2342 |
IS_SHADER_DIRTY(TESS_EVAL) || IS_DIRTY(TS_DOMAIN_ORIGIN), |
| 2343 |
.cull_2 = varyings_dirty, |
| 2344 |
|
| 2345 |
.fragment_shader = fs_dirty || linked_fs_dirty || varyings_dirty || |
| 2346 |
gfx->descriptors.root_dirty, |
| 2347 |
|
| 2348 |
.occlusion_query = gfx->dirty & HK_DIRTY_OCCLUSION, |
| 2349 |
.output_size = hw_vs_dirty, |
| 2350 |
.viewport_count = 1, /* irrelevant */ |
| 2351 |
}; |
| 2352 |
|
| 2353 |
dirty.fragment_shader &= !linked_fs->b.no_op; |
| 2354 |
|
| 2355 |
/* Calculate the update size. If it equals the header, there is nothing to |
| 2356 |
* update so early-exit. |
| 2357 |
*/ |
| 2358 |
size_t size = agx_ppp_update_size(&dirty); |
| 2359 |
if (size == AGX_PPP_HEADER_LENGTH) |
| 2360 |
return; |
| 2361 |
|
| 2362 |
/* Otherwise, allocate enough space for the update and push it. */ |
| 2363 |
assert(size > AGX_PPP_HEADER_LENGTH); |
| 2364 |
|
| 2365 |
struct agx_ptr T = hk_pool_alloc(cmd, size, AGX_PPP_HEADER_ALIGN); |
| 2366 |
if (!T.cpu) |
| 2367 |
return; |
| 2368 |
|
| 2369 |
struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &dirty); |
| 2370 |
|
| 2371 |
if (dirty.fragment_control) { |
| 2372 |
agx_ppp_push(&ppp, FRAGMENT_CONTROL, cfg) { |
| 2373 |
cfg.visibility_mode = gfx->occlusion.mode; |
| 2374 |
cfg.stencil_test_enable = hk_stencil_test_enabled(cmd); |
| 2375 |
|
| 2376 |
/* TODO: Consider optimizing this? */ |
| 2377 |
cfg.two_sided_stencil = cfg.stencil_test_enable; |
| 2378 |
|
| 2379 |
cfg.depth_bias_enable = dyn->rs.depth_bias.enable && |
| 2380 |
gfx->object_type == AGX_OBJECT_TYPE_TRIANGLE; |
| 2381 |
|
| 2382 |
/* Always enable scissoring so we may scissor to the viewport (TODO: |
| 2383 |
* optimize this out if the viewport is the default and the app does |
| 2384 |
* not use the scissor test) |
| 2385 |
*/ |
| 2386 |
cfg.scissor_enable = true; |
| 2387 |
|
| 2388 |
/* This avoids broken derivatives along primitive edges */ |
| 2389 |
cfg.disable_tri_merging = gfx->object_type != AGX_OBJECT_TYPE_TRIANGLE; |
| 2390 |
} |
| 2391 |
} |
| 2392 |
|
| 2393 |
if (dirty.fragment_control_2) { |
| 2394 |
/* Annoying, rasterizer_discard seems to be ignored (sometimes?) in the |
| 2395 |
* main fragment control word and has to be combined into the secondary |
| 2396 |
* word for reliable behaviour. |
| 2397 |
*/ |
| 2398 |
agx_ppp_push_merged(&ppp, FRAGMENT_CONTROL, cfg, |
| 2399 |
linked_fs->b.fragment_control) { |
| 2400 |
|
| 2401 |
cfg.tag_write_disable = hk_rast_discard(cmd); |
| 2402 |
} |
| 2403 |
} |
| 2404 |
|
| 2405 |
struct agx_fragment_face_packed fragment_face = {}; |
| 2406 |
struct agx_fragment_face_2_packed fragment_face_2 = {}; |
| 2407 |
|
| 2408 |
if (dirty.fragment_front_face) { |
| 2409 |
bool has_z = render->depth_att.vk_format != VK_FORMAT_UNDEFINED; |
| 2410 |
bool z_test = has_z && dyn->ds.depth.test_enable; |
| 2411 |
|
| 2412 |
agx_pack(&fragment_face, FRAGMENT_FACE, cfg) { |
| 2413 |
cfg.line_width = agx_pack_line_width(dyn->rs.line.width); |
| 2414 |
cfg.polygon_mode = translate_polygon_mode(dyn->rs.polygon_mode); |
| 2415 |
cfg.disable_depth_write = !(z_test && dyn->ds.depth.write_enable); |
| 2416 |
|
| 2417 |
if (z_test && !gfx->descriptors.root.draw.force_never_in_shader) |
| 2418 |
cfg.depth_function = translate_compare_op(dyn->ds.depth.compare_op); |
| 2419 |
else |
| 2420 |
cfg.depth_function = AGX_ZS_FUNC_ALWAYS; |
| 2421 |
}; |
| 2422 |
|
| 2423 |
agx_ppp_push_merged(&ppp, FRAGMENT_FACE, cfg, fragment_face) { |
| 2424 |
cfg.stencil_reference = dyn->ds.stencil.front.reference; |
| 2425 |
} |
| 2426 |
} |
| 2427 |
|
| 2428 |
if (dirty.fragment_front_face_2) { |
| 2429 |
agx_pack(&fragment_face_2, FRAGMENT_FACE_2, cfg) { |
| 2430 |
cfg.object_type = gfx->object_type; |
| 2431 |
} |
| 2432 |
|
| 2433 |
agx_merge(fragment_face_2, fs->frag_face, FRAGMENT_FACE_2); |
| 2434 |
agx_ppp_push_packed(&ppp, &fragment_face_2, FRAGMENT_FACE_2); |
| 2435 |
} |
| 2436 |
|
| 2437 |
if (dirty.fragment_front_stencil) { |
| 2438 |
hk_ppp_push_stencil_face(&ppp, dyn->ds.stencil.front, |
| 2439 |
hk_stencil_test_enabled(cmd)); |
| 2440 |
} |
| 2441 |
|
| 2442 |
if (dirty.fragment_back_face) { |
| 2443 |
assert(dirty.fragment_front_face); |
| 2444 |
|
| 2445 |
agx_ppp_push_merged(&ppp, FRAGMENT_FACE, cfg, fragment_face) { |
| 2446 |
cfg.stencil_reference = dyn->ds.stencil.back.reference; |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
if (dirty.fragment_back_face_2) { |
| 2451 |
assert(dirty.fragment_front_face_2); |
| 2452 |
|
| 2453 |
agx_ppp_push_packed(&ppp, &fragment_face_2, FRAGMENT_FACE_2); |
| 2454 |
} |
| 2455 |
|
| 2456 |
if (dirty.fragment_back_stencil) { |
| 2457 |
hk_ppp_push_stencil_face(&ppp, dyn->ds.stencil.back, |
| 2458 |
hk_stencil_test_enabled(cmd)); |
| 2459 |
} |
| 2460 |
|
| 2461 |
if (dirty.output_select) { |
| 2462 |
struct agx_output_select_packed osel = hw_vs->info.uvs.osel; |
| 2463 |
|
| 2464 |
agx_ppp_push_merged_blobs(&ppp, AGX_OUTPUT_SELECT_LENGTH, &osel, |
| 2465 |
&linked_fs->b.osel); |
| 2466 |
} |
| 2467 |
|
| 2468 |
assert(dirty.varying_counts_32 == dirty.varying_counts_16); |
| 2469 |
|
| 2470 |
if (dirty.varying_counts_32) { |
| 2471 |
agx_ppp_push_packed(&ppp, &gfx->linked_varyings.counts_32, |
| 2472 |
VARYING_COUNTS); |
| 2473 |
|
| 2474 |
agx_ppp_push_packed(&ppp, &gfx->linked_varyings.counts_16, |
| 2475 |
VARYING_COUNTS); |
| 2476 |
} |
| 2477 |
|
| 2478 |
if (dirty.cull) { |
| 2479 |
agx_ppp_push(&ppp, CULL, cfg) { |
| 2480 |
cfg.cull_front = dyn->rs.cull_mode & VK_CULL_MODE_FRONT_BIT; |
| 2481 |
cfg.cull_back = dyn->rs.cull_mode & VK_CULL_MODE_BACK_BIT; |
| 2482 |
cfg.front_face_ccw = dyn->rs.front_face != VK_FRONT_FACE_CLOCKWISE; |
| 2483 |
|
| 2484 |
if (gfx->shaders[MESA_SHADER_TESS_CTRL] && |
| 2485 |
!gfx->shaders[MESA_SHADER_GEOMETRY]) { |
| 2486 |
cfg.front_face_ccw ^= gfx->tess.info.ccw; |
| 2487 |
cfg.front_face_ccw ^= dyn->ts.domain_origin == |
| 2488 |
VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT; |
| 2489 |
} |
| 2490 |
|
| 2491 |
cfg.flat_shading_vertex = translate_ppp_vertex(gfx->provoking); |
| 2492 |
cfg.rasterizer_discard = hk_rast_discard(cmd); |
| 2493 |
|
| 2494 |
/* We do not support unrestricted depth, so clamping is inverted from |
| 2495 |
* clipping. This implementation seems to pass CTS without unrestricted |
| 2496 |
* depth support. |
| 2497 |
* |
| 2498 |
* TODO: Make sure this is right with gl_FragDepth. |
| 2499 |
*/ |
| 2500 |
cfg.depth_clip = vk_rasterization_state_depth_clip_enable(&dyn->rs); |
| 2501 |
cfg.depth_clamp = !cfg.depth_clip; |
| 2502 |
|
| 2503 |
cfg.primitive_msaa = |
| 2504 |
gfx->object_type == AGX_OBJECT_TYPE_LINE && |
| 2505 |
dyn->rs.line.mode == VK_LINE_RASTERIZATION_MODE_BRESENHAM_KHR; |
| 2506 |
} |
| 2507 |
} |
| 2508 |
|
| 2509 |
if (dirty.cull_2) { |
| 2510 |
agx_ppp_push(&ppp, CULL_2, cfg) { |
| 2511 |
cfg.needs_primitive_id = gfx->generate_primitive_id; |
| 2512 |
cfg.clamp_w = true; |
| 2513 |
} |
| 2514 |
} |
| 2515 |
|
| 2516 |
if (dirty.fragment_shader) { |
| 2517 |
/* TODO: Do less often? */ |
| 2518 |
hk_reserve_scratch(cmd, cs, fs); |
| 2519 |
|
| 2520 |
agx_ppp_push_packed(&ppp, &linked_fs->fs_counts, FRAGMENT_SHADER_WORD_0); |
| 2521 |
|
| 2522 |
agx_ppp_push(&ppp, FRAGMENT_SHADER_WORD_1, cfg) { |
| 2523 |
cfg.pipeline = hk_upload_usc_words(cmd, fs, linked_fs); |
| 2524 |
} |
| 2525 |
|
| 2526 |
agx_ppp_push(&ppp, FRAGMENT_SHADER_WORD_2, cfg) { |
| 2527 |
cfg.cf_bindings = gfx->varyings; |
| 2528 |
} |
| 2529 |
|
| 2530 |
agx_ppp_push(&ppp, FRAGMENT_SHADER_WORD_3, cfg) |
| 2531 |
; |
| 2532 |
} |
| 2533 |
|
| 2534 |
if (dirty.occlusion_query) { |
| 2535 |
agx_ppp_push(&ppp, FRAGMENT_OCCLUSION_QUERY, cfg) { |
| 2536 |
cfg.index = gfx->occlusion.index; |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
if (dirty.output_size) { |
| 2541 |
agx_ppp_push(&ppp, OUTPUT_SIZE, cfg) { |
| 2542 |
cfg.count = hw_vs->info.uvs.size; |
| 2543 |
} |
| 2544 |
} |
| 2545 |
|
| 2546 |
agx_ppp_fini(out, &ppp); |
| 2547 |
} |
| 2548 |
|
| 2549 |
/* |
| 2550 |
* Based somewhat on the calculation in the PowerVR driver, and mostly trial & |
| 2551 |
* error to pass CTS. This is a mess. |
| 2552 |
*/ |
| 2553 |
static float |
| 2554 |
hk_depth_bias_factor(VkFormat format, bool exact, bool force_unorm) |
| 2555 |
{ |
| 2556 |
if (format == VK_FORMAT_D16_UNORM) { |
| 2557 |
return exact ? (1 << 16) : (1 << 15); |
| 2558 |
} else if (force_unorm) { |
| 2559 |
return exact ? (1ull << 24) : (1ull << 23); |
| 2560 |
} else { |
| 2561 |
return 1.0; |
| 2562 |
} |
| 2563 |
} |
| 2564 |
|
| 2565 |
static bool |
| 2566 |
uses_blend_constant(const struct vk_color_blend_state *cb) |
| 2567 |
{ |
| 2568 |
static_assert(VK_BLEND_FACTOR_CONSTANT_COLOR + 1 == |
| 2569 |
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR); |
| 2570 |
|
| 2571 |
static_assert(VK_BLEND_FACTOR_CONSTANT_COLOR + 2 == |
| 2572 |
VK_BLEND_FACTOR_CONSTANT_ALPHA); |
| 2573 |
|
| 2574 |
static_assert(VK_BLEND_FACTOR_CONSTANT_COLOR + 3 == |
| 2575 |
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA); |
| 2576 |
|
| 2577 |
for (unsigned i = 0; i < cb->attachment_count; ++i) { |
| 2578 |
unsigned factors[] = { |
| 2579 |
cb->attachments[i].src_color_blend_factor, |
| 2580 |
cb->attachments[i].src_alpha_blend_factor, |
| 2581 |
cb->attachments[i].dst_color_blend_factor, |
| 2582 |
cb->attachments[i].dst_alpha_blend_factor, |
| 2583 |
}; |
| 2584 |
|
| 2585 |
for (unsigned j = 0; j < ARRAY_SIZE(factors); ++j) { |
| 2586 |
if (factors[j] >= VK_BLEND_FACTOR_CONSTANT_COLOR && |
| 2587 |
factors[j] <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA) |
| 2588 |
return true; |
| 2589 |
} |
| 2590 |
} |
| 2591 |
|
| 2592 |
return false; |
| 2593 |
} |
| 2594 |
|
| 2595 |
void |
| 2596 |
agx_fill_velem_keys(const struct vk_vertex_input_state *vi, |
| 2597 |
uint64_t attribs_read, struct agx_velem_key *keys) |
| 2598 |
{ |
| 2599 |
u_foreach_bit(a, vi->attributes_valid) { |
| 2600 |
struct vk_vertex_attribute_state attr = vi->attributes[a]; |
| 2601 |
|
| 2602 |
assert(vi->bindings_valid & BITFIELD_BIT(attr.binding)); |
| 2603 |
struct vk_vertex_binding_state binding = vi->bindings[attr.binding]; |
| 2604 |
|
| 2605 |
/* nir_assign_io_var_locations compacts vertex inputs, eliminating |
| 2606 |
* unused inputs. We need to do the same here to match the locations. |
| 2607 |
*/ |
| 2608 |
unsigned slot = util_bitcount64(attribs_read & BITFIELD_MASK(a)); |
| 2609 |
|
| 2610 |
keys[slot] = (struct agx_velem_key){ |
| 2611 |
.format = hk_format_to_pipe_format(attr.format), |
| 2612 |
.stride = binding.stride, |
| 2613 |
.divisor = binding.divisor, |
| 2614 |
.instanced = binding.input_rate == VK_VERTEX_INPUT_RATE_INSTANCE, |
| 2615 |
}; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
|
| 2619 |
static void |
| 2620 |
hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, |
| 2621 |
uint32_t draw_id, struct agx_draw draw) |
| 2622 |
{ |
| 2623 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 2624 |
const struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 2625 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 2626 |
|
| 2627 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 2628 |
|
| 2629 |
struct hk_shader *hw_vs = hk_bound_hw_vs(gfx); |
| 2630 |
struct hk_shader *sw_vs = hk_bound_sw_vs(gfx); |
| 2631 |
|
| 2632 |
if (!vk_dynamic_graphics_state_any_dirty(dyn) && !gfx->dirty && |
| 2633 |
!gfx->descriptors.root_dirty && !gfx->shaders_dirty && |
| 2634 |
!sw_vs->b.info.uses_draw_id && !sw_vs->b.info.uses_base_param && |
| 2635 |
!(gfx->linked[MESA_SHADER_VERTEX] && |
| 2636 |
gfx->linked[MESA_SHADER_VERTEX]->b.uses_base_param)) |
| 2637 |
return; |
| 2638 |
|
| 2639 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 2640 |
|
| 2641 |
assert(cs->current + 0x1000 < cs->end && "already ensured space"); |
| 2642 |
uint8_t *out = cs->current; |
| 2643 |
|
| 2644 |
struct hk_shader *fs = hk_only_variant(gfx->shaders[MESA_SHADER_FRAGMENT]); |
| 2645 |
|
| 2646 |
bool gt_dirty = IS_SHADER_DIRTY(TESS_CTRL) || IS_SHADER_DIRTY(TESS_EVAL) || |
| 2647 |
IS_SHADER_DIRTY(GEOMETRY); |
| 2648 |
bool vgt_dirty = IS_SHADER_DIRTY(VERTEX) || gt_dirty; |
| 2649 |
bool fs_dirty = IS_SHADER_DIRTY(FRAGMENT); |
| 2650 |
|
| 2651 |
if (IS_DIRTY(CB_BLEND_CONSTANTS)) { |
| 2652 |
static_assert(sizeof(desc->root.draw.blend_constant) == |
| 2653 |
sizeof(dyn->cb.blend_constants), |
| 2654 |
"common size"); |
| 2655 |
|
| 2656 |
memcpy(desc->root.draw.blend_constant, dyn->cb.blend_constants, |
| 2657 |
sizeof(dyn->cb.blend_constants)); |
| 2658 |
desc->root_dirty = true; |
| 2659 |
} |
| 2660 |
|
| 2661 |
if (IS_DIRTY(CB_BLEND_EQUATIONS) || IS_DIRTY(CB_BLEND_ENABLES)) { |
| 2662 |
gfx->uses_blend_constant = uses_blend_constant(&dyn->cb); |
| 2663 |
} |
| 2664 |
|
| 2665 |
if (IS_DIRTY(MS_SAMPLE_MASK)) { |
| 2666 |
desc->root.draw.api_sample_mask = dyn->ms.sample_mask; |
| 2667 |
desc->root_dirty = true; |
| 2668 |
} |
| 2669 |
|
| 2670 |
if (IS_DIRTY(RS_RASTERIZATION_STREAM)) { |
| 2671 |
desc->root.draw.rasterization_stream = dyn->rs.rasterization_stream; |
| 2672 |
desc->root_dirty = true; |
| 2673 |
|
| 2674 |
BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE); |
| 2675 |
} |
| 2676 |
|
| 2677 |
if (fs_dirty || IS_DIRTY(DS_DEPTH_TEST_ENABLE) || |
| 2678 |
IS_DIRTY(DS_DEPTH_COMPARE_OP)) { |
| 2679 |
|
| 2680 |
const struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 2681 |
bool has_z = render->depth_att.vk_format != VK_FORMAT_UNDEFINED; |
| 2682 |
bool z_test = has_z && dyn->ds.depth.test_enable; |
| 2683 |
|
| 2684 |
desc->root.draw.force_never_in_shader = |
| 2685 |
z_test && dyn->ds.depth.compare_op == VK_COMPARE_OP_NEVER && fs && |
| 2686 |
fs->info.fs.writes_memory; |
| 2687 |
|
| 2688 |
desc->root_dirty = true; |
| 2689 |
} |
| 2690 |
|
| 2691 |
/* The main shader must not run tests if the epilog will. */ |
| 2692 |
bool nontrivial_force_early = |
| 2693 |
fs && (fs->b.info.early_fragment_tests && |
| 2694 |
(fs->b.info.writes_sample_mask || fs->info.fs.writes_memory)); |
| 2695 |
|
| 2696 |
bool epilog_discards = dyn->ms.alpha_to_coverage_enable || |
| 2697 |
(fs && (fs->info.fs.epilog_key.write_z || |
| 2698 |
fs->info.fs.epilog_key.write_s)); |
| 2699 |
epilog_discards &= !nontrivial_force_early; |
| 2700 |
|
| 2701 |
if (fs_dirty || IS_DIRTY(MS_ALPHA_TO_COVERAGE_ENABLE)) { |
| 2702 |
desc->root.draw.no_epilog_discard = !epilog_discards ? ~0 : 0; |
| 2703 |
desc->root_dirty = true; |
| 2704 |
} |
| 2705 |
|
| 2706 |
if (IS_DIRTY(VI) || IS_DIRTY(VI_BINDINGS_VALID) || |
| 2707 |
IS_DIRTY(VI_BINDING_STRIDES) || vgt_dirty || true /* TODO */) { |
| 2708 |
|
| 2709 |
struct hk_fast_link_key_vs key = { |
| 2710 |
.prolog.hw = (sw_vs == hw_vs), |
| 2711 |
|
| 2712 |
/* FIXME: handle pipeline robustness "properly" */ |
| 2713 |
.prolog.robustness.level = |
| 2714 |
(dev->vk.enabled_features.robustBufferAccess2 || |
| 2715 |
dev->vk.enabled_features.pipelineRobustness) |
| 2716 |
? AGX_ROBUSTNESS_D3D |
| 2717 |
: dev->vk.enabled_features.robustBufferAccess |
| 2718 |
? AGX_ROBUSTNESS_GL |
| 2719 |
: AGX_ROBUSTNESS_DISABLED, |
| 2720 |
|
| 2721 |
.prolog.robustness.soft_fault = agx_has_soft_fault(&dev->dev), |
| 2722 |
.prolog.static_vi = !sw_vs->info.vs.use_prolog, |
| 2723 |
}; |
| 2724 |
|
| 2725 |
enum mesa_prim prim = vk_topology_to_mesa(dyn->ia.primitive_topology); |
| 2726 |
|
| 2727 |
if (mesa_prim_has_adjacency(prim)) { |
| 2728 |
if (draw.restart) { |
| 2729 |
prim = u_decomposed_prim(prim); |
| 2730 |
} |
| 2731 |
|
| 2732 |
key.prolog.adjacency = prim; |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (key.prolog.adjacency || !key.prolog.hw) { |
| 2736 |
key.prolog.sw_index_size_B = |
| 2737 |
draw.indexed ? agx_index_size_to_B(draw.index_size) : 0; |
| 2738 |
} |
| 2739 |
|
| 2740 |
static_assert(sizeof(key.prolog.component_mask) == |
| 2741 |
sizeof(sw_vs->info.vs.attrib_components_read)); |
| 2742 |
BITSET_COPY(key.prolog.component_mask, |
| 2743 |
sw_vs->info.vs.attrib_components_read); |
| 2744 |
|
| 2745 |
if (sw_vs->info.vs.use_prolog) { |
| 2746 |
agx_fill_velem_keys(dyn->vi, sw_vs->info.vs.attribs_read, |
| 2747 |
key.prolog.attribs); |
| 2748 |
|
| 2749 |
u_foreach_bit(a, dyn->vi->attributes_valid) { |
| 2750 |
unsigned slot = |
| 2751 |
util_bitcount64(sw_vs->info.vs.attribs_read & BITFIELD_MASK(a)); |
| 2752 |
|
| 2753 |
key.prolog.attribs[slot].stride = |
| 2754 |
dyn->vi_binding_strides[dyn->vi->attributes[a].binding]; |
| 2755 |
} |
| 2756 |
} |
| 2757 |
|
| 2758 |
hk_update_fast_linked(cmd, sw_vs, &key); |
| 2759 |
} |
| 2760 |
|
| 2761 |
if (IS_DIRTY(VI) || IS_DIRTY(VI_BINDINGS_VALID) || |
| 2762 |
IS_DIRTY(VI_BINDING_STRIDES) || vgt_dirty || |
| 2763 |
(gfx->dirty & HK_DIRTY_VB)) { |
| 2764 |
|
| 2765 |
unsigned slot = 0; |
| 2766 |
u_foreach_bit(a, sw_vs->info.vs.attribs_read) { |
| 2767 |
if (dyn->vi->attributes_valid & BITFIELD_BIT(a)) { |
| 2768 |
struct vk_vertex_attribute_state attr = dyn->vi->attributes[a]; |
| 2769 |
struct hk_addr_range vb = gfx->vb[attr.binding]; |
| 2770 |
enum pipe_format fmt = hk_format_to_pipe_format(attr.format); |
| 2771 |
enum pipe_format interchange_format = agx_vbo_internal_format(fmt); |
| 2772 |
unsigned interchange_align = |
| 2773 |
util_format_get_blocksize(interchange_format); |
| 2774 |
|
| 2775 |
desc->root.draw.attrib_clamps[slot] = agx_calculate_vbo_clamp( |
| 2776 |
vb.addr, fmt, vb.range, dyn->vi_binding_strides[attr.binding], |
| 2777 |
attr.offset, &desc->root.draw.attrib_base[slot]); |
| 2778 |
|
| 2779 |
desc->root.draw.attrib_strides[slot] = |
| 2780 |
dyn->vi_binding_strides[attr.binding] / interchange_align; |
| 2781 |
} else { |
| 2782 |
desc->root.draw.attrib_base[slot] = AGX_ZERO_PAGE_ADDRESS; |
| 2783 |
desc->root.draw.attrib_clamps[slot] = 0; |
| 2784 |
desc->root.draw.attrib_strides[slot] = 0; |
| 2785 |
} |
| 2786 |
|
| 2787 |
++slot; |
| 2788 |
} |
| 2789 |
|
| 2790 |
desc->root_dirty = true; |
| 2791 |
} |
| 2792 |
|
| 2793 |
if (vgt_dirty || IS_SHADER_DIRTY(FRAGMENT) || |
| 2794 |
IS_DIRTY(MS_RASTERIZATION_SAMPLES) || IS_DIRTY(MS_SAMPLE_MASK) || |
| 2795 |
IS_DIRTY(MS_ALPHA_TO_COVERAGE_ENABLE) || |
| 2796 |
IS_DIRTY(MS_ALPHA_TO_ONE_ENABLE) || IS_DIRTY(CB_LOGIC_OP) || |
| 2797 |
IS_DIRTY(CB_LOGIC_OP_ENABLE) || IS_DIRTY(CB_WRITE_MASKS) || |
| 2798 |
IS_DIRTY(CB_COLOR_WRITE_ENABLES) || IS_DIRTY(CB_ATTACHMENT_COUNT) || |
| 2799 |
IS_DIRTY(CB_BLEND_ENABLES) || IS_DIRTY(CB_BLEND_EQUATIONS) || |
| 2800 |
IS_DIRTY(CB_BLEND_CONSTANTS) || IS_DIRTY(CB_BLEND_ADVANCED) || |
| 2801 |
desc->root_dirty /* for pipeline stats */ || true) { |
| 2802 |
|
| 2803 |
unsigned tib_sample_mask = BITFIELD_MASK(dyn->ms.rasterization_samples); |
| 2804 |
unsigned api_sample_mask = dyn->ms.sample_mask & tib_sample_mask; |
| 2805 |
bool has_sample_mask = api_sample_mask != tib_sample_mask; |
| 2806 |
|
| 2807 |
if (hw_vs->info.cull_distance_array_size) { |
| 2808 |
perf_debug(cmd, "Emulating cull distance (size %u)", |
| 2809 |
hw_vs->info.cull_distance_array_size); |
| 2810 |
} |
| 2811 |
|
| 2812 |
if (has_sample_mask) { |
| 2813 |
perf_debug(cmd, "Emulating sample mask"); |
| 2814 |
} |
| 2815 |
|
| 2816 |
unsigned samples_shaded = 0; |
| 2817 |
if (fs->info.fs.epilog_key.sample_shading) |
| 2818 |
samples_shaded = dyn->ms.rasterization_samples; |
| 2819 |
|
| 2820 |
struct hk_fast_link_key_fs key = { |
| 2821 |
.prolog.statistics = hk_stat_enabled(hk_pipeline_stat_addr( |
| 2822 |
cmd, VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT)), |
| 2823 |
|
| 2824 |
.prolog.cull_distance_size = hw_vs->info.cull_distance_array_size, |
| 2825 |
.prolog.api_sample_mask = has_sample_mask ? api_sample_mask : 0xff, |
| 2826 |
.nr_samples_shaded = samples_shaded, |
| 2827 |
}; |
| 2828 |
|
| 2829 |
bool prolog_discards = has_sample_mask || key.prolog.cull_distance_size; |
| 2830 |
|
| 2831 |
bool needs_prolog = key.prolog.statistics || prolog_discards; |
| 2832 |
|
| 2833 |
if (needs_prolog) { |
| 2834 |
/* With late main shader tests, the prolog runs tests if neither the |
| 2835 |
* main shader nor epilog will. |
| 2836 |
* |
| 2837 |
* With (nontrivial) early main shader tests, the prolog does not |
| 2838 |
* run tests, the tests will run at the start of the main shader. |
| 2839 |
* This ensures tests are after API sample mask and cull distance |
| 2840 |
* discards. |
| 2841 |
*/ |
| 2842 |
key.prolog.run_zs_tests = !nontrivial_force_early && |
| 2843 |
!fs->b.info.writes_sample_mask && |
| 2844 |
!epilog_discards && prolog_discards; |
| 2845 |
|
| 2846 |
if (key.prolog.cull_distance_size) { |
| 2847 |
key.prolog.cf_base = fs->b.info.varyings.fs.nr_cf; |
| 2848 |
} |
| 2849 |
} |
| 2850 |
|
| 2851 |
key.epilog = (struct agx_fs_epilog_key){ |
| 2852 |
.link = fs->info.fs.epilog_key, |
| 2853 |
.nr_samples = MAX2(dyn->ms.rasterization_samples, 1), |
| 2854 |
.blend.alpha_to_coverage = dyn->ms.alpha_to_coverage_enable, |
| 2855 |
.blend.alpha_to_one = dyn->ms.alpha_to_one_enable, |
| 2856 |
.blend.logicop_enable = dyn->cb.logic_op_enable, |
| 2857 |
.blend.logicop_func = vk_logic_op_to_pipe(dyn->cb.logic_op), |
| 2858 |
}; |
| 2859 |
|
| 2860 |
for (unsigned rt = 0; rt < ARRAY_SIZE(dyn->cal.color_map); ++rt) { |
| 2861 |
int map = dyn->cal.color_map[rt]; |
| 2862 |
key.epilog.remap[rt] = map == MESA_VK_ATTACHMENT_UNUSED ? -1 : map; |
| 2863 |
} |
| 2864 |
|
| 2865 |
if (dyn->ms.alpha_to_one_enable || dyn->ms.alpha_to_coverage_enable || |
| 2866 |
dyn->cb.logic_op_enable) { |
| 2867 |
|
| 2868 |
perf_debug( |
| 2869 |
cmd, "Epilog with%s%s%s", |
| 2870 |
dyn->ms.alpha_to_one_enable ? " alpha-to-one" : "", |
| 2871 |
dyn->ms.alpha_to_coverage_enable ? " alpha-to-coverage" : "", |
| 2872 |
dyn->cb.logic_op_enable ? " logic-op" : ""); |
| 2873 |
} |
| 2874 |
|
| 2875 |
key.epilog.link.already_ran_zs |= nontrivial_force_early; |
| 2876 |
|
| 2877 |
struct hk_rendering_state *render = &cmd->state.gfx.render; |
| 2878 |
for (uint32_t i = 0; i < render->color_att_count; i++) { |
| 2879 |
key.epilog.rt_formats[i] = |
| 2880 |
hk_format_to_pipe_format(render->color_att[i].vk_format); |
| 2881 |
|
| 2882 |
const struct vk_color_blend_attachment_state *cb = |
| 2883 |
&dyn->cb.attachments[i]; |
| 2884 |
|
| 2885 |
bool write_enable = dyn->cb.color_write_enables & BITFIELD_BIT(i); |
| 2886 |
unsigned write_mask = write_enable ? cb->write_mask : 0; |
| 2887 |
|
| 2888 |
/* nir_lower_blend always blends, so use a default blend state when |
| 2889 |
* blending is disabled at an API level. |
| 2890 |
*/ |
| 2891 |
if (!dyn->cb.attachments[i].blend_enable) { |
| 2892 |
key.epilog.blend.rt[i] = (struct agx_blend_rt_key){ |
| 2893 |
.colormask = write_mask, |
| 2894 |
.mode = agx_pack_blend_standard( |
| 2895 |
PIPE_BLEND_ADD, PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO, |
| 2896 |
PIPE_BLEND_ADD, PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO), |
| 2897 |
}; |
| 2898 |
} else if (cb->color_blend_op >= VK_BLEND_OP_ZERO_EXT) { |
| 2899 |
key.epilog.blend.rt[i] = (struct agx_blend_rt_key){ |
| 2900 |
.colormask = write_mask, |
| 2901 |
.advanced_blend = 1, |
| 2902 |
.mode = agx_pack_blend_advanced( |
| 2903 |
vk_advanced_blend_op_to_pipe(cb->color_blend_op), |
| 2904 |
vk_blend_overlap_to_pipe(cb->blend_overlap), |
| 2905 |
cb->src_premultiplied, cb->dst_premultiplied, |
| 2906 |
cb->clamp_results), |
| 2907 |
}; |
| 2908 |
|
| 2909 |
assert(cb->clamp_results == false); |
| 2910 |
} else { |
| 2911 |
key.epilog.blend.rt[i] = (struct agx_blend_rt_key){ |
| 2912 |
.colormask = write_mask, |
| 2913 |
.mode = agx_pack_blend_standard( |
| 2914 |
vk_blend_op_to_pipe(cb->color_blend_op), |
| 2915 |
vk_blend_factor_to_pipe(cb->src_color_blend_factor), |
| 2916 |
vk_blend_factor_to_pipe(cb->dst_color_blend_factor), |
| 2917 |
vk_blend_op_to_pipe(cb->alpha_blend_op), |
| 2918 |
vk_blend_factor_to_pipe(cb->src_alpha_blend_factor), |
| 2919 |
vk_blend_factor_to_pipe(cb->dst_alpha_blend_factor)), |
| 2920 |
}; |
| 2921 |
} |
| 2922 |
} |
| 2923 |
|
| 2924 |
hk_update_fast_linked(cmd, fs, &key); |
| 2925 |
} |
| 2926 |
|
| 2927 |
/* If the vertex shader uses draw parameters, vertex uniforms are dirty every |
| 2928 |
* draw. Fragment uniforms are unaffected. |
| 2929 |
* |
| 2930 |
* For a direct draw, we upload the draw parameters as-if indirect to |
| 2931 |
* avoid keying to indirectness. |
| 2932 |
*/ |
| 2933 |
if (gfx->linked[MESA_SHADER_VERTEX]->b.uses_base_param) { |
| 2934 |
if (agx_is_indirect(draw.b)) { |
| 2935 |
gfx->draw_params = draw.b.ptr; |
| 2936 |
|
| 2937 |
if (draw.indexed) { |
| 2938 |
gfx->draw_params += |
| 2939 |
offsetof(VkDrawIndexedIndirectCommand, vertexOffset); |
| 2940 |
} else { |
| 2941 |
gfx->draw_params += offsetof(VkDrawIndirectCommand, firstVertex); |
| 2942 |
} |
| 2943 |
} else { |
| 2944 |
uint32_t params[] = { |
| 2945 |
draw.indexed ? draw.index_bias : draw.start, |
| 2946 |
draw.start_instance, |
| 2947 |
}; |
| 2948 |
|
| 2949 |
gfx->draw_params = hk_pool_upload(cmd, params, sizeof(params), 4); |
| 2950 |
} |
| 2951 |
} else { |
| 2952 |
gfx->draw_params = 0; |
| 2953 |
} |
| 2954 |
|
| 2955 |
if (sw_vs->b.info.uses_draw_id) { |
| 2956 |
/* TODO: rodata? */ |
| 2957 |
gfx->draw_id_ptr = hk_pool_upload(cmd, &draw_id, 2, 4); |
| 2958 |
} else { |
| 2959 |
gfx->draw_id_ptr = 0; |
| 2960 |
} |
| 2961 |
|
| 2962 |
if (IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || gt_dirty) { |
| 2963 |
enum mesa_prim prim = hk_rast_prim(cmd); |
| 2964 |
|
| 2965 |
gfx->topology = translate_hw_primitive_topology(prim); |
| 2966 |
gfx->object_type = translate_object_type(prim); |
| 2967 |
} |
| 2968 |
|
| 2969 |
if (IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || IS_DIRTY(RS_PROVOKING_VERTEX)) { |
| 2970 |
unsigned provoking; |
| 2971 |
if (dyn->rs.provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT) |
| 2972 |
provoking = 2; |
| 2973 |
else if (gfx->topology == AGX_PRIMITIVE_TRIANGLE_FAN) |
| 2974 |
provoking = 1; |
| 2975 |
else |
| 2976 |
provoking = 0; |
| 2977 |
|
| 2978 |
if (provoking != gfx->provoking) { |
| 2979 |
gfx->provoking = provoking; |
| 2980 |
gfx->dirty |= HK_DIRTY_PROVOKING; |
| 2981 |
|
| 2982 |
gfx->descriptors.root.draw.provoking = provoking; |
| 2983 |
gfx->descriptors.root_dirty = true; |
| 2984 |
} |
| 2985 |
} |
| 2986 |
|
| 2987 |
/* With attachmentless rendering, we don't know the sample count until draw |
| 2988 |
* time, so we do a late tilebuffer fix up. But with rasterizer discard, |
| 2989 |
* rasterization_samples might be 0. |
| 2990 |
* |
| 2991 |
* Note that we ignore dyn->ms.rasterization_samples when we do have a sample |
| 2992 |
* count from an attachment. In Vulkan, these have to match anyway, but DX12 |
| 2993 |
* drivers are robust against this scenarios and vkd3d-proton will go out of |
| 2994 |
* spec here. No reason we can't be robust here too. |
| 2995 |
*/ |
| 2996 |
if (dyn->ms.rasterization_samples && !gfx->render.tilebuffer.nr_samples) { |
| 2997 |
agx_tilebuffer_set_samples(&gfx->render.tilebuffer, |
| 2998 |
dyn->ms.rasterization_samples); |
| 2999 |
|
| 3000 |
cs->tib = gfx->render.tilebuffer; |
| 3001 |
} |
| 3002 |
|
| 3003 |
if (IS_DIRTY(MS_SAMPLE_LOCATIONS) || IS_DIRTY(MS_SAMPLE_LOCATIONS_ENABLE) || |
| 3004 |
IS_DIRTY(MS_RASTERIZATION_SAMPLES)) { |
| 3005 |
|
| 3006 |
uint32_t ctrl; |
| 3007 |
if (dyn->ms.sample_locations_enable) { |
| 3008 |
ctrl = hk_pack_ppp_multisamplectrl(dyn->ms.sample_locations); |
| 3009 |
} else { |
| 3010 |
ctrl = hk_default_sample_positions(dyn->ms.rasterization_samples); |
| 3011 |
} |
| 3012 |
|
| 3013 |
bool dont_commit = cmd->in_meta || dyn->ms.rasterization_samples == 0; |
| 3014 |
|
| 3015 |
if (!cs->has_sample_locations) { |
| 3016 |
cs->ppp_multisamplectl = ctrl; |
| 3017 |
|
| 3018 |
/* If we're in vk_meta, do not commit to the sample locations yet. |
| 3019 |
* vk_meta doesn't care, but the app will! |
| 3020 |
*/ |
| 3021 |
cs->has_sample_locations |= !dont_commit; |
| 3022 |
} else { |
| 3023 |
assert(dont_commit || cs->ppp_multisamplectl == ctrl); |
| 3024 |
} |
| 3025 |
|
| 3026 |
gfx->descriptors.root.draw.ppp_multisamplectl = ctrl; |
| 3027 |
gfx->descriptors.root_dirty = true; |
| 3028 |
} |
| 3029 |
|
| 3030 |
/* Link varyings before uploading tessellation state, becuase the |
| 3031 |
* gfx->generate_primitive_id boolean needs to be plumbed. |
| 3032 |
*/ |
| 3033 |
struct hk_linked_shader *linked_vs = gfx->linked[MESA_SHADER_VERTEX]; |
| 3034 |
struct hk_linked_shader *linked_fs = gfx->linked[MESA_SHADER_FRAGMENT]; |
| 3035 |
bool linked_vs_dirty = IS_LINKED_DIRTY(VERTEX); |
| 3036 |
bool linked_fs_dirty = IS_LINKED_DIRTY(FRAGMENT); |
| 3037 |
|
| 3038 |
if ((gfx->dirty & HK_DIRTY_PROVOKING) || vgt_dirty || linked_fs_dirty) { |
| 3039 |
unsigned bindings = linked_fs->b.cf.nr_bindings; |
| 3040 |
if (bindings) { |
| 3041 |
size_t linkage_size = |
| 3042 |
AGX_CF_BINDING_HEADER_LENGTH + (bindings * AGX_CF_BINDING_LENGTH); |
| 3043 |
|
| 3044 |
struct agx_ptr t = hk_pool_usc_alloc(cmd, linkage_size, 16); |
| 3045 |
if (!t.cpu) |
| 3046 |
return; |
| 3047 |
|
| 3048 |
agx_link_varyings_vs_fs( |
| 3049 |
t.cpu, &gfx->linked_varyings, hw_vs->info.uvs.user_size, |
| 3050 |
&linked_fs->b.cf, gfx->provoking, 0, &gfx->generate_primitive_id); |
| 3051 |
|
| 3052 |
gfx->varyings = agx_usc_addr(&dev->dev, t.gpu); |
| 3053 |
} else { |
| 3054 |
gfx->varyings = 0; |
| 3055 |
} |
| 3056 |
|
| 3057 |
gfx->dirty |= HK_DIRTY_VARYINGS; |
| 3058 |
} |
| 3059 |
|
| 3060 |
if (gfx->shaders[MESA_SHADER_TESS_EVAL] || |
| 3061 |
gfx->shaders[MESA_SHADER_GEOMETRY] || linked_vs->sw_indexing) { |
| 3062 |
desc->root.draw.vertex_params = hk_upload_vertex_params(cmd, draw); |
| 3063 |
desc->root_dirty = true; |
| 3064 |
} |
| 3065 |
|
| 3066 |
struct agx_ptr tess_args = {0}; |
| 3067 |
if (gfx->shaders[MESA_SHADER_TESS_EVAL]) { |
| 3068 |
tess_args = hk_pool_alloc(cmd, sizeof(struct poly_tess_params), 4); |
| 3069 |
gfx->descriptors.root.draw.tess_params = tess_args.gpu; |
| 3070 |
gfx->descriptors.root_dirty = true; |
| 3071 |
} |
| 3072 |
|
| 3073 |
if (gfx->shaders[MESA_SHADER_GEOMETRY]) { |
| 3074 |
gfx->descriptors.root.draw.geometry_params = |
| 3075 |
hk_upload_geometry_params(cmd, draw); |
| 3076 |
|
| 3077 |
gfx->descriptors.root_dirty = true; |
| 3078 |
} |
| 3079 |
|
| 3080 |
/* Root must be uploaded after the above, which touch the root */ |
| 3081 |
if (gfx->descriptors.root_dirty) { |
| 3082 |
gfx->root = |
| 3083 |
hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 3084 |
|
| 3085 |
/* Tess parameters depend on the root address, so we defer the upload |
| 3086 |
* until after uploading root. But the root depends on the tess address, |
| 3087 |
* so we allocate tess parameters before uploading root. |
| 3088 |
* |
| 3089 |
* This whole mechanism is a mess ported over from the GL driver. I'm |
| 3090 |
* planning to do a massive rework of indirect geom/tess so I'm trying not |
| 3091 |
* to perfectionism it in the mean time. |
| 3092 |
*/ |
| 3093 |
if (tess_args.cpu) { |
| 3094 |
hk_upload_tess_params(cmd, tess_args.cpu, draw); |
| 3095 |
} |
| 3096 |
} |
| 3097 |
|
| 3098 |
/* Hardware dynamic state must be deferred until after the root and fast |
| 3099 |
* linking, since it will use the root address and the linked shaders. |
| 3100 |
*/ |
| 3101 |
if ((gfx->dirty & (HK_DIRTY_PROVOKING | HK_DIRTY_VARYINGS)) || |
| 3102 |
IS_DIRTY(RS_RASTERIZER_DISCARD_ENABLE) || linked_vs_dirty || vgt_dirty || |
| 3103 |
gfx->descriptors.root_dirty || gfx->draw_id_ptr || gfx->draw_params) { |
| 3104 |
|
| 3105 |
/* TODO: Do less often? */ |
| 3106 |
hk_reserve_scratch(cmd, cs, hw_vs); |
| 3107 |
|
| 3108 |
agx_push(out, VDM_STATE, cfg) { |
| 3109 |
cfg.vertex_shader_word_0_present = true; |
| 3110 |
cfg.vertex_shader_word_1_present = true; |
| 3111 |
cfg.vertex_outputs_present = true; |
| 3112 |
cfg.vertex_unknown_present = true; |
| 3113 |
} |
| 3114 |
|
| 3115 |
agx_push_packed(out, hw_vs->counts, VDM_STATE_VERTEX_SHADER_WORD_0); |
| 3116 |
|
| 3117 |
struct hk_linked_shader *linked_hw_vs = |
| 3118 |
(hw_vs == sw_vs) ? linked_vs : hw_vs->only_linked; |
| 3119 |
|
| 3120 |
agx_push(out, VDM_STATE_VERTEX_SHADER_WORD_1, cfg) { |
| 3121 |
cfg.pipeline = hk_upload_usc_words(cmd, hw_vs, linked_hw_vs); |
| 3122 |
} |
| 3123 |
|
| 3124 |
agx_push_packed(out, hw_vs->info.uvs.vdm, VDM_STATE_VERTEX_OUTPUTS); |
| 3125 |
|
| 3126 |
agx_push(out, VDM_STATE_VERTEX_UNKNOWN, cfg) { |
| 3127 |
cfg.flat_shading_control = translate_vdm_vertex(gfx->provoking); |
| 3128 |
cfg.unknown_4 = cfg.unknown_5 = hk_rast_discard(cmd); |
| 3129 |
cfg.generate_primitive_id = gfx->generate_primitive_id; |
| 3130 |
} |
| 3131 |
|
| 3132 |
/* Pad up to a multiple of 8 bytes */ |
| 3133 |
memset(out, 0, 4); |
| 3134 |
out += 4; |
| 3135 |
} |
| 3136 |
|
| 3137 |
if (IS_DIRTY(RS_DEPTH_BIAS_FACTORS)) { |
| 3138 |
void *ptr = |
| 3139 |
util_dynarray_grow_bytes(&cs->depth_bias, 1, AGX_DEPTH_BIAS_LENGTH); |
| 3140 |
|
| 3141 |
bool exact = dyn->rs.depth_bias.exact; |
| 3142 |
bool force_unorm = |
| 3143 |
dyn->rs.depth_bias.representation == |
| 3144 |
VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORCE_UNORM_EXT; |
| 3145 |
|
| 3146 |
agx_pack(ptr, DEPTH_BIAS, cfg) { |
| 3147 |
cfg.slope_scale = dyn->rs.depth_bias.slope_factor; |
| 3148 |
cfg.clamp = dyn->rs.depth_bias.clamp; |
| 3149 |
cfg.depth_bias = dyn->rs.depth_bias.constant_factor; |
| 3150 |
cfg.depth_bias /= hk_depth_bias_factor(render->depth_att.vk_format, |
| 3151 |
exact, force_unorm); |
| 3152 |
} |
| 3153 |
} |
| 3154 |
|
| 3155 |
/* Hardware viewport/scissor state is entangled with depth bias. */ |
| 3156 |
if (IS_DIRTY(RS_DEPTH_BIAS_FACTORS) || IS_DIRTY(VP_SCISSORS) || |
| 3157 |
IS_DIRTY(VP_SCISSOR_COUNT) || IS_DIRTY(VP_VIEWPORTS) || |
| 3158 |
IS_DIRTY(VP_VIEWPORT_COUNT) || |
| 3159 |
IS_DIRTY(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE) || |
| 3160 |
IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE)) { |
| 3161 |
|
| 3162 |
hk_flush_vp_state(cmd, cs, &out); |
| 3163 |
} |
| 3164 |
|
| 3165 |
hk_flush_ppp_state(cmd, cs, &out); |
| 3166 |
cs->current = out; |
| 3167 |
|
| 3168 |
vk_dynamic_graphics_state_clear_dirty(dyn); |
| 3169 |
gfx->shaders_dirty = 0; |
| 3170 |
gfx->linked_dirty = 0; |
| 3171 |
gfx->dirty = 0; |
| 3172 |
gfx->descriptors.root_dirty = false; |
| 3173 |
} |
| 3174 |
|
| 3175 |
static bool |
| 3176 |
hk_needs_index_robustness(struct hk_cmd_buffer *cmd, struct agx_draw *draw) |
| 3177 |
{ |
| 3178 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 3179 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 3180 |
|
| 3181 |
if (!draw->indexed) |
| 3182 |
return false; |
| 3183 |
|
| 3184 |
/* Geometry or tessellation use robust software index buffer fetch anyway */ |
| 3185 |
if (gfx->shaders[MESA_SHADER_GEOMETRY] || |
| 3186 |
gfx->shaders[MESA_SHADER_TESS_EVAL]) |
| 3187 |
return false; |
| 3188 |
|
| 3189 |
/* Soft fault does not cover the hardware index buffer fetch, use the zero |
| 3190 |
* page instead. |
| 3191 |
*/ |
| 3192 |
if (draw->index_buffer_range_B == 0) { |
| 3193 |
draw->index_buffer = AGX_ZERO_PAGE_ADDRESS; |
| 3194 |
draw->index_buffer_range_B = 4; |
| 3195 |
draw->start = 0; |
| 3196 |
return false; |
| 3197 |
} |
| 3198 |
|
| 3199 |
if (!(dev->vk.enabled_features.robustBufferAccess || |
| 3200 |
dev->vk.enabled_features.robustBufferAccess2 || |
| 3201 |
dev->vk.enabled_features.pipelineRobustness)) |
| 3202 |
return false; |
| 3203 |
|
| 3204 |
if (agx_is_indirect(draw->b)) |
| 3205 |
return true; |
| 3206 |
|
| 3207 |
return agx_direct_draw_overreads_indices(*draw); |
| 3208 |
} |
| 3209 |
|
| 3210 |
static void |
| 3211 |
hk_handle_passthrough_gs(struct hk_cmd_buffer *cmd, struct agx_draw draw) |
| 3212 |
{ |
| 3213 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 3214 |
struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY]; |
| 3215 |
|
| 3216 |
if (!IS_SHADER_DIRTY(VERTEX) && !IS_SHADER_DIRTY(GEOMETRY)) |
| 3217 |
return; |
| 3218 |
|
| 3219 |
/* If there's an application geometry shader, there's nothing to un/bind */ |
| 3220 |
if (gs && !gs->is_passthrough) |
| 3221 |
return; |
| 3222 |
|
| 3223 |
/* Determine if we need a geometry shader to emulate XFB or adjacency */ |
| 3224 |
struct hk_shader *last_sw = hk_bound_sw_vs_before_gs(gfx); |
| 3225 |
uint32_t xfb_outputs = last_sw->info.xfb_info.output_count; |
| 3226 |
bool needs_gs = xfb_outputs; |
| 3227 |
|
| 3228 |
/* If we don't need a GS but we do have a passthrough, unbind it */ |
| 3229 |
if (!needs_gs) { |
| 3230 |
if (gs != NULL) { |
| 3231 |
assert(gs->is_passthrough); |
| 3232 |
hk_cmd_bind_graphics_shader(cmd, MESA_SHADER_GEOMETRY, NULL); |
| 3233 |
} |
| 3234 |
return; |
| 3235 |
} |
| 3236 |
|
| 3237 |
/* Else, we need to bind a passthrough GS */ |
| 3238 |
size_t key_size = hk_passthrough_gs_key_size(xfb_outputs); |
| 3239 |
struct hk_passthrough_gs_key *key = alloca(key_size); |
| 3240 |
|
| 3241 |
*key = (struct hk_passthrough_gs_key){ |
| 3242 |
.prim = u_decomposed_prim(hk_gs_in_prim(cmd)), |
| 3243 |
.outputs = last_sw->b.info.outputs, |
| 3244 |
.clip_distance_array_size = last_sw->info.clip_distance_array_size, |
| 3245 |
.cull_distance_array_size = last_sw->info.cull_distance_array_size, |
| 3246 |
}; |
| 3247 |
|
| 3248 |
if (xfb_outputs) { |
| 3249 |
typed_memcpy(key->xfb_stride, last_sw->info.xfb_stride, |
| 3250 |
ARRAY_SIZE(key->xfb_stride)); |
| 3251 |
|
| 3252 |
memcpy(&key->xfb_info, &last_sw->info.xfb_info, |
| 3253 |
nir_xfb_info_size(xfb_outputs)); |
| 3254 |
} |
| 3255 |
|
| 3256 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 3257 |
perf_debug(cmd, "Binding passthrough GS for%s\n", xfb_outputs ? " XFB" : ""); |
| 3258 |
|
| 3259 |
gs = hk_meta_shader(dev, hk_nir_passthrough_gs, key, key_size); |
| 3260 |
gs->is_passthrough = true; |
| 3261 |
hk_cmd_bind_graphics_shader(cmd, MESA_SHADER_GEOMETRY, gs); |
| 3262 |
} |
| 3263 |
|
| 3264 |
static struct hk_cs * |
| 3265 |
hk_flush_gfx_state(struct hk_cmd_buffer *cmd, uint32_t draw_id, |
| 3266 |
struct agx_draw draw) |
| 3267 |
{ |
| 3268 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 3269 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 3270 |
struct hk_descriptor_state *desc = &gfx->descriptors; |
| 3271 |
|
| 3272 |
struct hk_cs *cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); |
| 3273 |
const struct vk_dynamic_graphics_state *dyn = |
| 3274 |
&cmd->vk.dynamic_graphics_state; |
| 3275 |
|
| 3276 |
if (!cs) |
| 3277 |
return NULL; |
| 3278 |
|
| 3279 |
/* Annoyingly, |
| 3280 |
* VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORCE_UNORM_EXT is |
| 3281 |
* render pass state on Imaginapple but draw state in Vulkan. In practice, |
| 3282 |
* Proton never changes it within a render pass, but we technically need to |
| 3283 |
* handle the switch regardless. Do so early since `cs` will be invalidated |
| 3284 |
* if we need to split the render pass to switch representation mid-frame. |
| 3285 |
* |
| 3286 |
* Note we only do this dance with depth bias is actually enabled to avoid |
| 3287 |
* senseless control stream splits with DXVK. |
| 3288 |
*/ |
| 3289 |
if ((IS_DIRTY(RS_DEPTH_BIAS_FACTORS) || IS_DIRTY(RS_DEPTH_BIAS_ENABLE)) && |
| 3290 |
dyn->rs.depth_bias.enable) { |
| 3291 |
|
| 3292 |
bool dbias_is_int = |
| 3293 |
(dyn->rs.depth_bias.representation == |
| 3294 |
VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORCE_UNORM_EXT) || |
| 3295 |
(gfx->render.depth_att.vk_format == VK_FORMAT_D16_UNORM); |
| 3296 |
|
| 3297 |
/* Attempt to set dbias_is_int per the draw requirement. If this fails, |
| 3298 |
* flush the control stream and set it on the new control stream. |
| 3299 |
*/ |
| 3300 |
bool succ = u_tristate_set(&cs->cr.dbias_is_int, dbias_is_int); |
| 3301 |
if (!succ) { |
| 3302 |
perf_debug(cmd, "Splitting control stream due to depth bias"); |
| 3303 |
|
| 3304 |
hk_cmd_buffer_end_graphics(cmd); |
| 3305 |
cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); |
| 3306 |
|
| 3307 |
succ = u_tristate_set(&cs->cr.dbias_is_int, dbias_is_int); |
| 3308 |
assert(succ && "can always set tri-state on a new control stream"); |
| 3309 |
} |
| 3310 |
} |
| 3311 |
|
| 3312 |
hk_ensure_cs_has_space(cmd, cs, 0x2000 /* TODO */); |
| 3313 |
|
| 3314 |
#ifndef NDEBUG |
| 3315 |
if (unlikely(dev->dev.debug & AGX_DBG_DIRTY)) { |
| 3316 |
hk_cmd_buffer_dirty_all(cmd); |
| 3317 |
} |
| 3318 |
#endif |
| 3319 |
|
| 3320 |
/* Merge tess info before GS construction since that depends on |
| 3321 |
* gfx->tess.prim |
| 3322 |
*/ |
| 3323 |
if ((IS_SHADER_DIRTY(TESS_CTRL) || IS_SHADER_DIRTY(TESS_EVAL)) && |
| 3324 |
gfx->shaders[MESA_SHADER_TESS_CTRL]) { |
| 3325 |
struct hk_api_shader *tcs = gfx->shaders[MESA_SHADER_TESS_CTRL]; |
| 3326 |
struct hk_api_shader *tes = gfx->shaders[MESA_SHADER_TESS_EVAL]; |
| 3327 |
struct hk_shader *tese = hk_any_variant(tes); |
| 3328 |
struct hk_shader *tesc = hk_only_variant(tcs); |
| 3329 |
|
| 3330 |
gfx->tess.info = |
| 3331 |
hk_tess_info_merge(tese->info.tess.info, tesc->info.tess.info); |
| 3332 |
|
| 3333 |
/* Determine primitive based on the merged state */ |
| 3334 |
if (gfx->tess.info.points) { |
| 3335 |
gfx->tess.prim = MESA_PRIM_POINTS; |
| 3336 |
} else if (gfx->tess.info.mode == TESS_PRIMITIVE_ISOLINES) { |
| 3337 |
gfx->tess.prim = MESA_PRIM_LINES; |
| 3338 |
} else { |
| 3339 |
gfx->tess.prim = MESA_PRIM_TRIANGLES; |
| 3340 |
} |
| 3341 |
} |
| 3342 |
|
| 3343 |
/* TODO: Try to reduce draw overhead of this */ |
| 3344 |
hk_handle_passthrough_gs(cmd, draw); |
| 3345 |
|
| 3346 |
hk_flush_shaders(cmd); |
| 3347 |
|
| 3348 |
if (desc->push_dirty) |
| 3349 |
hk_cmd_buffer_flush_push_descriptors(cmd, desc); |
| 3350 |
|
| 3351 |
if (draw.restart || gfx->shaders[MESA_SHADER_GEOMETRY]) |
| 3352 |
hk_flush_index(cmd, cs); |
| 3353 |
|
| 3354 |
hk_flush_dynamic_state(cmd, cs, draw_id, draw); |
| 3355 |
return cs; |
| 3356 |
} |
| 3357 |
|
| 3358 |
VKAPI_ATTR void VKAPI_CALL |
| 3359 |
hk_CmdBindIndexBuffer2KHR(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3360 |
VkDeviceSize offset, VkDeviceSize size, |
| 3361 |
VkIndexType indexType) |
| 3362 |
{ |
| 3363 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3364 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 3365 |
|
| 3366 |
cmd->state.gfx.index = (struct hk_index_buffer_state){ |
| 3367 |
.buffer = hk_buffer_addr_range(buffer, offset, size, true), |
| 3368 |
.size = agx_translate_index_size(vk_index_type_to_bytes(indexType)), |
| 3369 |
.restart = vk_index_to_restart(indexType), |
| 3370 |
}; |
| 3371 |
|
| 3372 |
/* TODO: check if necessary, blob does this */ |
| 3373 |
cmd->state.gfx.index.buffer.range = |
| 3374 |
align(cmd->state.gfx.index.buffer.range, 4); |
| 3375 |
} |
| 3376 |
|
| 3377 |
void |
| 3378 |
hk_cmd_bind_vertex_buffer(struct hk_cmd_buffer *cmd, uint32_t vb_idx, |
| 3379 |
struct hk_addr_range addr_range) |
| 3380 |
{ |
| 3381 |
cmd->state.gfx.vb[vb_idx] = addr_range; |
| 3382 |
cmd->state.gfx.dirty |= HK_DIRTY_VB; |
| 3383 |
} |
| 3384 |
|
| 3385 |
VKAPI_ATTR void VKAPI_CALL |
| 3386 |
hk_CmdBindVertexBuffers2(VkCommandBuffer commandBuffer, uint32_t firstBinding, |
| 3387 |
uint32_t bindingCount, const VkBuffer *pBuffers, |
| 3388 |
const VkDeviceSize *pOffsets, |
| 3389 |
const VkDeviceSize *pSizes, |
| 3390 |
const VkDeviceSize *pStrides) |
| 3391 |
{ |
| 3392 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3393 |
|
| 3394 |
if (pStrides) { |
| 3395 |
vk_cmd_set_vertex_binding_strides(&cmd->vk, firstBinding, bindingCount, |
| 3396 |
pStrides); |
| 3397 |
} |
| 3398 |
|
| 3399 |
for (uint32_t i = 0; i < bindingCount; i++) { |
| 3400 |
VK_FROM_HANDLE(hk_buffer, buffer, pBuffers[i]); |
| 3401 |
uint32_t idx = firstBinding + i; |
| 3402 |
|
| 3403 |
uint64_t size = pSizes ? pSizes[i] : VK_WHOLE_SIZE; |
| 3404 |
const struct hk_addr_range addr_range = |
| 3405 |
hk_buffer_addr_range(buffer, pOffsets[i], size, true); |
| 3406 |
|
| 3407 |
hk_cmd_bind_vertex_buffer(cmd, idx, addr_range); |
| 3408 |
} |
| 3409 |
} |
| 3410 |
|
| 3411 |
static bool |
| 3412 |
hk_set_view_index(struct hk_cmd_buffer *cmd, uint32_t view_idx) |
| 3413 |
{ |
| 3414 |
if (cmd->state.gfx.render.view_mask) { |
| 3415 |
cmd->state.gfx.descriptors.root.draw.view_index = view_idx; |
| 3416 |
cmd->state.gfx.descriptors.root_dirty = true; |
| 3417 |
} |
| 3418 |
|
| 3419 |
return true; |
| 3420 |
} |
| 3421 |
|
| 3422 |
/* |
| 3423 |
* Iterator macro to duplicate a draw for each enabled view (when multiview is |
| 3424 |
* enabled, else always view 0). Along with hk_lower_multiview, this forms the |
| 3425 |
* world's worst multiview lowering. |
| 3426 |
*/ |
| 3427 |
#define hk_foreach_view(cmd) \ |
| 3428 |
u_foreach_bit(view_idx, cmd->state.gfx.render.view_mask ?: 1) \ |
| 3429 |
if (hk_set_view_index(cmd, view_idx)) |
| 3430 |
|
| 3431 |
static void |
| 3432 |
hk_ia_update(struct hk_cmd_buffer *cmd, struct agx_draw draw, |
| 3433 |
uint64_t ia_vertices, uint64_t ia_prims, uint64_t vs_invocations, |
| 3434 |
uint64_t c_prims, uint64_t c_inv) |
| 3435 |
{ |
| 3436 |
perf_debug(cmd, "Input assembly counters"); |
| 3437 |
|
| 3438 |
uint64_t draw_ptr; |
| 3439 |
if (agx_is_indirect(draw.b)) { |
| 3440 |
draw_ptr = draw.b.ptr; |
| 3441 |
} else { |
| 3442 |
uint32_t desc[] = {draw.b.count[0], draw.b.count[1], 0}; |
| 3443 |
draw_ptr = hk_pool_upload(cmd, &desc, sizeof(desc), 4); |
| 3444 |
} |
| 3445 |
|
| 3446 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 3447 |
enum mesa_prim prim = vk_topology_to_mesa(dyn->ia.primitive_topology); |
| 3448 |
|
| 3449 |
bool geom = cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY]; |
| 3450 |
bool tess = cmd->state.gfx.shaders[MESA_SHADER_TESS_EVAL]; |
| 3451 |
|
| 3452 |
/* Clipper counters depend on geom/tess outputs and must be written with the |
| 3453 |
* geom/tess output. They are updated as IA counters only when geom/tess is |
| 3454 |
* not used. |
| 3455 |
*/ |
| 3456 |
if (geom || tess) { |
| 3457 |
c_prims = 0; |
| 3458 |
c_inv = 0; |
| 3459 |
} |
| 3460 |
|
| 3461 |
uint32_t patch_size = dyn->ts.patch_control_points; |
| 3462 |
|
| 3463 |
if (draw.restart) { |
| 3464 |
uint32_t index_size_B = agx_index_size_to_B(draw.index_size); |
| 3465 |
|
| 3466 |
libagx_increment_ia_restart( |
| 3467 |
cmd, agx_1d(1024), AGX_BARRIER_ALL | AGX_PREGFX, ia_vertices, ia_prims, |
| 3468 |
vs_invocations, c_prims, c_inv, draw_ptr, draw.index_buffer, |
| 3469 |
agx_draw_index_range_el(draw), cmd->state.gfx.index.restart, |
| 3470 |
index_size_B, prim, patch_size); |
| 3471 |
} else { |
| 3472 |
libagx_increment_ia(cmd, agx_1d(1), AGX_BARRIER_ALL | AGX_PREGFX, |
| 3473 |
ia_vertices, ia_prims, vs_invocations, c_prims, c_inv, |
| 3474 |
draw_ptr, prim, patch_size); |
| 3475 |
} |
| 3476 |
} |
| 3477 |
|
| 3478 |
static void |
| 3479 |
hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_) |
| 3480 |
{ |
| 3481 |
const struct vk_dynamic_graphics_state *dyn = |
| 3482 |
&cmd->vk.dynamic_graphics_state; |
| 3483 |
|
| 3484 |
/* Filter trivial draws so we don't need to worry about null index buffers */ |
| 3485 |
if (!agx_is_indirect(draw_.b) && |
| 3486 |
(draw_.b.count[0] == 0 || draw_.b.count[1] == 0)) |
| 3487 |
return; |
| 3488 |
|
| 3489 |
draw_.restart = dyn->ia.primitive_restart_enable && draw_.indexed; |
| 3490 |
draw_.index_size = cmd->state.gfx.index.size; |
| 3491 |
|
| 3492 |
uint64_t stat_ia_verts = hk_pipeline_stat_addr( |
| 3493 |
cmd, VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT); |
| 3494 |
|
| 3495 |
uint64_t stat_ia_prims = hk_pipeline_stat_addr( |
| 3496 |
cmd, VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT); |
| 3497 |
|
| 3498 |
uint64_t stat_vs_inv = hk_pipeline_stat_addr( |
| 3499 |
cmd, VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT); |
| 3500 |
|
| 3501 |
uint64_t stat_c_inv = hk_pipeline_stat_addr( |
| 3502 |
cmd, VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT); |
| 3503 |
|
| 3504 |
uint64_t stat_c_prims = hk_pipeline_stat_addr( |
| 3505 |
cmd, VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT); |
| 3506 |
|
| 3507 |
bool ia_stats = hk_stat_enabled(stat_ia_verts) || |
| 3508 |
hk_stat_enabled(stat_ia_prims) || |
| 3509 |
hk_stat_enabled(stat_vs_inv) || |
| 3510 |
hk_stat_enabled(stat_c_inv) || hk_stat_enabled(stat_c_prims); |
| 3511 |
struct hk_device *dev = hk_cmd_buffer_device(cmd); |
| 3512 |
|
| 3513 |
hk_foreach_view(cmd) { |
| 3514 |
struct agx_draw draw = draw_; |
| 3515 |
struct hk_cs *cs = hk_flush_gfx_state(cmd, draw_id, draw); |
| 3516 |
/* If we failed to allocate a control stream, we've already lost the |
| 3517 |
* device. Just drop the draw so we don't crash. |
| 3518 |
*/ |
| 3519 |
if (!cs) |
| 3520 |
return; |
| 3521 |
|
| 3522 |
struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; |
| 3523 |
bool geom = cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY]; |
| 3524 |
bool tess = cmd->state.gfx.shaders[MESA_SHADER_TESS_EVAL]; |
| 3525 |
bool needs_idx_robust = hk_needs_index_robustness(cmd, &draw); |
| 3526 |
bool adj = mesa_prim_has_adjacency( |
| 3527 |
vk_topology_to_mesa(dyn->ia.primitive_topology)); |
| 3528 |
adj &= !geom; |
| 3529 |
needs_idx_robust &= !adj; |
| 3530 |
|
| 3531 |
struct hk_cs *ccs = NULL; |
| 3532 |
uint8_t *out = cs->current; |
| 3533 |
assert(cs->current + 0x1000 < cs->end); |
| 3534 |
|
| 3535 |
if (tess && HK_PERF(dev, NOTESS)) |
| 3536 |
continue; |
| 3537 |
|
| 3538 |
cs->stats.calls++; |
| 3539 |
|
| 3540 |
if (geom || tess) { |
| 3541 |
ccs = |
| 3542 |
hk_cmd_buffer_get_cs_general(cmd, &cmd->current_cs.pre_gfx, true); |
| 3543 |
if (!ccs) |
| 3544 |
return; |
| 3545 |
} |
| 3546 |
|
| 3547 |
if (ia_stats) { |
| 3548 |
hk_ia_update(cmd, draw, stat_ia_verts, stat_ia_prims, stat_vs_inv, |
| 3549 |
stat_c_prims, stat_c_inv); |
| 3550 |
} |
| 3551 |
|
| 3552 |
if (tess) { |
| 3553 |
draw = hk_launch_tess(cmd, ccs, draw, geom ? 0 : stat_c_prims, |
| 3554 |
geom ? 0 : stat_c_inv); |
| 3555 |
} |
| 3556 |
|
| 3557 |
if (geom) { |
| 3558 |
draw = hk_launch_gs_prerast(cmd, ccs, draw); |
| 3559 |
} |
| 3560 |
|
| 3561 |
if (adj) { |
| 3562 |
assert(!geom && "geometry shaders handle adj directly"); |
| 3563 |
enum mesa_prim prim = vk_topology_to_mesa(dyn->ia.primitive_topology); |
| 3564 |
|
| 3565 |
if (draw.restart) { |
| 3566 |
draw = hk_draw_without_restart(cmd, draw, 1); |
| 3567 |
prim = u_decomposed_prim(prim); |
| 3568 |
} |
| 3569 |
|
| 3570 |
if (agx_is_indirect(draw.b)) { |
| 3571 |
const size_t size = sizeof(VkDrawIndexedIndirectCommand); |
| 3572 |
static_assert(sizeof(VkDrawIndexedIndirectCommand) > |
| 3573 |
sizeof(VkDrawIndirectCommand), |
| 3574 |
"allocation size is conservative"); |
| 3575 |
|
| 3576 |
uint64_t out_draw = hk_pool_alloc(cmd, size, 4).gpu; |
| 3577 |
struct hk_descriptor_state *desc = &cmd->state.gfx.descriptors; |
| 3578 |
|
| 3579 |
libagx_draw_without_adj( |
| 3580 |
cmd, agx_1d(1), AGX_BARRIER_ALL | AGX_PREGFX, out_draw, |
| 3581 |
draw.b.ptr, desc->root.draw.vertex_params, draw.index_buffer, |
| 3582 |
draw.indexed ? agx_draw_index_range_el(draw) : 0, |
| 3583 |
draw.indexed ? agx_index_size_to_B(draw.index_size) : 0, prim); |
| 3584 |
|
| 3585 |
draw = agx_draw_indirect(out_draw); |
| 3586 |
} else { |
| 3587 |
unsigned count = libagx_remap_adj_count(draw.b.count[0], prim); |
| 3588 |
|
| 3589 |
draw = (struct agx_draw){ |
| 3590 |
.b = agx_3d(count, draw.b.count[1], 1), |
| 3591 |
}; |
| 3592 |
} |
| 3593 |
} |
| 3594 |
|
| 3595 |
enum agx_primitive topology = cmd->state.gfx.topology; |
| 3596 |
if (needs_idx_robust) { |
| 3597 |
assert(!geom && !tess && !adj); |
| 3598 |
perf_debug(cmd, "lowering robust index buffer"); |
| 3599 |
|
| 3600 |
cs->current = out; |
| 3601 |
|
| 3602 |
draw = hk_draw_as_indexed_indirect(cmd, draw); |
| 3603 |
|
| 3604 |
size_t size_B = libagx_draw_robust_index_vdm_size(); |
| 3605 |
uint64_t target = hk_cs_alloc_for_indirect(cs, size_B); |
| 3606 |
|
| 3607 |
libagx_draw_robust_index(cmd, agx_1d(32), AGX_BARRIER_ALL | AGX_PREGFX, |
| 3608 |
target, hk_heap(cmd), draw.b.ptr, |
| 3609 |
draw.index_buffer, draw.index_buffer_range_B, |
| 3610 |
draw.restart, topology, draw.index_size); |
| 3611 |
} else { |
| 3612 |
cs->current = (void *)agx_vdm_draw((uint32_t *)out, dev->dev.chip, |
| 3613 |
draw, topology); |
| 3614 |
} |
| 3615 |
|
| 3616 |
cs->stats.cmds++; |
| 3617 |
} |
| 3618 |
} |
| 3619 |
|
| 3620 |
VKAPI_ATTR void VKAPI_CALL |
| 3621 |
hk_CmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, |
| 3622 |
uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) |
| 3623 |
{ |
| 3624 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3625 |
struct agx_draw draw; |
| 3626 |
|
| 3627 |
if (HK_TEST_INDIRECTS) { |
| 3628 |
uint32_t data[] = { |
| 3629 |
vertexCount, |
| 3630 |
instanceCount, |
| 3631 |
firstVertex, |
| 3632 |
firstInstance, |
| 3633 |
}; |
| 3634 |
|
| 3635 |
draw = agx_draw_indirect(hk_pool_upload(cmd, data, sizeof(data), 4)); |
| 3636 |
} else { |
| 3637 |
draw = (struct agx_draw){ |
| 3638 |
.b = agx_3d(vertexCount, instanceCount, 1), |
| 3639 |
.start = firstVertex, |
| 3640 |
.start_instance = firstInstance, |
| 3641 |
}; |
| 3642 |
} |
| 3643 |
|
| 3644 |
hk_draw(cmd, 0, draw); |
| 3645 |
} |
| 3646 |
|
| 3647 |
VKAPI_ATTR void VKAPI_CALL |
| 3648 |
hk_CmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 3649 |
const VkMultiDrawInfoEXT *pVertexInfo, |
| 3650 |
uint32_t instanceCount, uint32_t firstInstance, |
| 3651 |
uint32_t stride) |
| 3652 |
{ |
| 3653 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3654 |
|
| 3655 |
for (unsigned i = 0; i < drawCount; ++i) { |
| 3656 |
struct agx_draw draw = { |
| 3657 |
.b = agx_3d(pVertexInfo->vertexCount, instanceCount, 1), |
| 3658 |
.start = pVertexInfo->firstVertex, |
| 3659 |
.start_instance = firstInstance, |
| 3660 |
}; |
| 3661 |
|
| 3662 |
hk_draw(cmd, i, draw); |
| 3663 |
pVertexInfo = ((void *)pVertexInfo) + stride; |
| 3664 |
} |
| 3665 |
} |
| 3666 |
|
| 3667 |
static void |
| 3668 |
hk_draw_indexed(VkCommandBuffer commandBuffer, uint16_t draw_id, |
| 3669 |
uint32_t indexCount, uint32_t instanceCount, |
| 3670 |
uint32_t firstIndex, int32_t vertexOffset, |
| 3671 |
uint32_t firstInstance) |
| 3672 |
{ |
| 3673 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3674 |
struct agx_draw draw; |
| 3675 |
struct hk_addr_range buf = cmd->state.gfx.index.buffer; |
| 3676 |
|
| 3677 |
if (HK_TEST_INDIRECTS && draw_id == 0) { |
| 3678 |
uint32_t data[] = { |
| 3679 |
indexCount, instanceCount, firstIndex, vertexOffset, firstInstance, |
| 3680 |
}; |
| 3681 |
uint64_t addr = hk_pool_upload(cmd, data, sizeof(data), 4); |
| 3682 |
|
| 3683 |
draw = agx_draw_indexed_indirect(addr, buf.addr, buf.range, 0, 0); |
| 3684 |
} else { |
| 3685 |
draw = |
| 3686 |
agx_draw_indexed(indexCount, instanceCount, firstIndex, vertexOffset, |
| 3687 |
firstInstance, buf.addr, buf.range, 0, 0); |
| 3688 |
} |
| 3689 |
|
| 3690 |
hk_draw(cmd, draw_id, draw); |
| 3691 |
} |
| 3692 |
|
| 3693 |
VKAPI_ATTR void VKAPI_CALL |
| 3694 |
hk_CmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, |
| 3695 |
uint32_t instanceCount, uint32_t firstIndex, |
| 3696 |
int32_t vertexOffset, uint32_t firstInstance) |
| 3697 |
{ |
| 3698 |
hk_draw_indexed(commandBuffer, 0, indexCount, instanceCount, firstIndex, |
| 3699 |
vertexOffset, firstInstance); |
| 3700 |
} |
| 3701 |
|
| 3702 |
VKAPI_ATTR void VKAPI_CALL |
| 3703 |
hk_CmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 3704 |
const VkMultiDrawIndexedInfoEXT *pIndexInfo, |
| 3705 |
uint32_t instanceCount, uint32_t firstInstance, |
| 3706 |
uint32_t stride, const int32_t *pVertexOffset) |
| 3707 |
{ |
| 3708 |
for (unsigned i = 0; i < drawCount; ++i) { |
| 3709 |
const uint32_t vertex_offset = |
| 3710 |
pVertexOffset != NULL ? *pVertexOffset : pIndexInfo->vertexOffset; |
| 3711 |
|
| 3712 |
hk_draw_indexed(commandBuffer, i, pIndexInfo->indexCount, instanceCount, |
| 3713 |
pIndexInfo->firstIndex, vertex_offset, firstInstance); |
| 3714 |
|
| 3715 |
pIndexInfo = ((void *)pIndexInfo) + stride; |
| 3716 |
} |
| 3717 |
} |
| 3718 |
|
| 3719 |
static void |
| 3720 |
hk_draw_indirect_inner(VkCommandBuffer commandBuffer, uint64_t base, |
| 3721 |
uint32_t drawCount, uint32_t stride) |
| 3722 |
{ |
| 3723 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3724 |
|
| 3725 |
/* From the Vulkan 1.3.238 spec: |
| 3726 |
* |
| 3727 |
* VUID-vkCmdDrawIndirect-drawCount-00476 |
| 3728 |
* |
| 3729 |
* "If drawCount is greater than 1, stride must be a multiple of 4 and |
| 3730 |
* must be greater than or equal to sizeof(VkDrawIndirectCommand)" |
| 3731 |
* |
| 3732 |
* and |
| 3733 |
* |
| 3734 |
* "If drawCount is less than or equal to one, stride is ignored." |
| 3735 |
*/ |
| 3736 |
if (drawCount > 1) { |
| 3737 |
assert(stride % 4 == 0); |
| 3738 |
assert(stride >= sizeof(VkDrawIndirectCommand)); |
| 3739 |
} |
| 3740 |
|
| 3741 |
for (unsigned draw_id = 0; draw_id < drawCount; ++draw_id) { |
| 3742 |
uint64_t addr = base + stride * draw_id; |
| 3743 |
hk_draw(cmd, draw_id, agx_draw_indirect(addr)); |
| 3744 |
} |
| 3745 |
} |
| 3746 |
|
| 3747 |
VKAPI_ATTR void VKAPI_CALL |
| 3748 |
hk_CmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3749 |
VkDeviceSize offset, uint32_t drawCount, uint32_t stride) |
| 3750 |
{ |
| 3751 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 3752 |
|
| 3753 |
hk_draw_indirect_inner(commandBuffer, hk_buffer_address_ro(buffer, offset), |
| 3754 |
drawCount, stride); |
| 3755 |
} |
| 3756 |
|
| 3757 |
static void |
| 3758 |
hk_draw_indexed_indirect_inner(VkCommandBuffer commandBuffer, uint64_t buffer, |
| 3759 |
uint32_t drawCount, uint32_t stride) |
| 3760 |
{ |
| 3761 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3762 |
|
| 3763 |
/* From the Vulkan 1.3.238 spec: |
| 3764 |
* |
| 3765 |
* VUID-vkCmdDrawIndexedIndirect-drawCount-00528 |
| 3766 |
* |
| 3767 |
* "If drawCount is greater than 1, stride must be a multiple of 4 and |
| 3768 |
* must be greater than or equal to |
| 3769 |
* sizeof(VkDrawIndexedIndirectCommand)" |
| 3770 |
* |
| 3771 |
* and |
| 3772 |
* |
| 3773 |
* "If drawCount is less than or equal to one, stride is ignored." |
| 3774 |
*/ |
| 3775 |
if (drawCount > 1) { |
| 3776 |
assert(stride % 4 == 0); |
| 3777 |
assert(stride >= sizeof(VkDrawIndexedIndirectCommand)); |
| 3778 |
} |
| 3779 |
|
| 3780 |
for (unsigned draw_id = 0; draw_id < drawCount; ++draw_id) { |
| 3781 |
uint64_t addr = buffer + stride * draw_id; |
| 3782 |
struct hk_addr_range buf = cmd->state.gfx.index.buffer; |
| 3783 |
|
| 3784 |
hk_draw(cmd, draw_id, |
| 3785 |
agx_draw_indexed_indirect(addr, buf.addr, buf.range, 0, 0)); |
| 3786 |
} |
| 3787 |
} |
| 3788 |
|
| 3789 |
VKAPI_ATTR void VKAPI_CALL |
| 3790 |
hk_CmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3791 |
VkDeviceSize offset, uint32_t drawCount, |
| 3792 |
uint32_t stride) |
| 3793 |
{ |
| 3794 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 3795 |
|
| 3796 |
hk_draw_indexed_indirect_inner( |
| 3797 |
commandBuffer, hk_buffer_address_ro(buffer, offset), drawCount, stride); |
| 3798 |
} |
| 3799 |
|
| 3800 |
/* |
| 3801 |
* To implement drawIndirectCount generically, we dispatch a compute kernel to |
| 3802 |
* patch the indirect buffer and then we dispatch the predicated maxDrawCount |
| 3803 |
* indirect draws. |
| 3804 |
*/ |
| 3805 |
static void |
| 3806 |
hk_draw_indirect_count(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3807 |
VkDeviceSize offset, VkBuffer countBuffer, |
| 3808 |
VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3809 |
uint32_t stride, bool indexed) |
| 3810 |
{ |
| 3811 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3812 |
VK_FROM_HANDLE(hk_buffer, buffer, _buffer); |
| 3813 |
VK_FROM_HANDLE(hk_buffer, count_buffer, countBuffer); |
| 3814 |
|
| 3815 |
perf_debug(cmd, "Draw indirect count"); |
| 3816 |
|
| 3817 |
assert((stride % 4) == 0 && "aligned"); |
| 3818 |
|
| 3819 |
size_t out_stride = sizeof(uint32_t) * (indexed ? 5 : 4); |
| 3820 |
uint64_t patched = hk_pool_alloc(cmd, out_stride * maxDrawCount, 4).gpu; |
| 3821 |
uint64_t in = hk_buffer_address_ro(buffer, offset); |
| 3822 |
uint64_t count_addr = hk_buffer_address_ro(count_buffer, countBufferOffset); |
| 3823 |
|
| 3824 |
libagx_predicate_indirect(cmd, agx_1d(maxDrawCount), |
| 3825 |
AGX_BARRIER_ALL | AGX_PREGFX, patched, in, |
| 3826 |
count_addr, stride / 4, indexed); |
| 3827 |
|
| 3828 |
if (indexed) { |
| 3829 |
hk_draw_indexed_indirect_inner(commandBuffer, patched, maxDrawCount, |
| 3830 |
out_stride); |
| 3831 |
} else { |
| 3832 |
hk_draw_indirect_inner(commandBuffer, patched, maxDrawCount, out_stride); |
| 3833 |
} |
| 3834 |
} |
| 3835 |
|
| 3836 |
VKAPI_ATTR void VKAPI_CALL |
| 3837 |
hk_CmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3838 |
VkDeviceSize offset, VkBuffer countBuffer, |
| 3839 |
VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 3840 |
uint32_t stride) |
| 3841 |
{ |
| 3842 |
hk_draw_indirect_count(commandBuffer, _buffer, offset, countBuffer, |
| 3843 |
countBufferOffset, maxDrawCount, stride, false); |
| 3844 |
} |
| 3845 |
|
| 3846 |
VKAPI_ATTR void VKAPI_CALL |
| 3847 |
hk_CmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer _buffer, |
| 3848 |
VkDeviceSize offset, VkBuffer countBuffer, |
| 3849 |
VkDeviceSize countBufferOffset, |
| 3850 |
uint32_t maxDrawCount, uint32_t stride) |
| 3851 |
{ |
| 3852 |
hk_draw_indirect_count(commandBuffer, _buffer, offset, countBuffer, |
| 3853 |
countBufferOffset, maxDrawCount, stride, true); |
| 3854 |
} |
| 3855 |
|
| 3856 |
VKAPI_ATTR void VKAPI_CALL |
| 3857 |
hk_CmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, |
| 3858 |
uint32_t instanceCount, uint32_t firstInstance, |
| 3859 |
VkBuffer counterBuffer, |
| 3860 |
VkDeviceSize counterBufferOffset, |
| 3861 |
uint32_t counterOffset, uint32_t vertexStride) |
| 3862 |
{ |
| 3863 |
UNREACHABLE("TODO"); |
| 3864 |
} |
| 3865 |
|
| 3866 |
VKAPI_ATTR void VKAPI_CALL |
| 3867 |
hk_CmdBindTransformFeedbackBuffersEXT(VkCommandBuffer commandBuffer, |
| 3868 |
uint32_t firstBinding, |
| 3869 |
uint32_t bindingCount, |
| 3870 |
const VkBuffer *pBuffers, |
| 3871 |
const VkDeviceSize *pOffsets, |
| 3872 |
const VkDeviceSize *pSizes) |
| 3873 |
{ |
| 3874 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3875 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 3876 |
|
| 3877 |
for (uint32_t i = 0; i < bindingCount; i++) { |
| 3878 |
VK_FROM_HANDLE(hk_buffer, buffer, pBuffers[i]); |
| 3879 |
uint32_t idx = firstBinding + i; |
| 3880 |
uint64_t size = pSizes ? pSizes[i] : VK_WHOLE_SIZE; |
| 3881 |
|
| 3882 |
gfx->xfb[idx] = hk_buffer_addr_range(buffer, pOffsets[i], size, false); |
| 3883 |
} |
| 3884 |
} |
| 3885 |
|
| 3886 |
static void |
| 3887 |
hk_begin_end_xfb(VkCommandBuffer commandBuffer, uint32_t firstCounterBuffer, |
| 3888 |
uint32_t counterBufferCount, const VkBuffer *pCounterBuffers, |
| 3889 |
const VkDeviceSize *pCounterBufferOffsets, bool begin) |
| 3890 |
|
| 3891 |
{ |
| 3892 |
VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer); |
| 3893 |
struct hk_graphics_state *gfx = &cmd->state.gfx; |
| 3894 |
|
| 3895 |
gfx->xfb_enabled = begin; |
| 3896 |
|
| 3897 |
/* If we haven't reserved XFB offsets yet for the command buffer, do so. */ |
| 3898 |
if (!gfx->xfb_offsets) { |
| 3899 |
gfx->xfb_offsets = hk_pool_alloc(cmd, 4 * sizeof(uint32_t), 4).gpu; |
| 3900 |
} |
| 3901 |
|
| 3902 |
struct libagx_xfb_counter_copy params = {}; |
| 3903 |
unsigned copies = 0; |
| 3904 |
|
| 3905 |
/* For CmdBeginTransformFeedbackEXT, we need to initialize everything */ |
| 3906 |
if (begin) { |
| 3907 |
for (copies = 0; copies < 4; ++copies) { |
| 3908 |
params.dest[copies] = gfx->xfb_offsets + copies * sizeof(uint32_t); |
| 3909 |
} |
| 3910 |
} |
| 3911 |
|
| 3912 |
for (unsigned i = 0; i < counterBufferCount; ++i) { |
| 3913 |
if (pCounterBuffers[i] == VK_NULL_HANDLE) |
| 3914 |
continue; |
| 3915 |
|
| 3916 |
VK_FROM_HANDLE(hk_buffer, buffer, pCounterBuffers[i]); |
| 3917 |
|
| 3918 |
uint64_t offset = pCounterBufferOffsets ? pCounterBufferOffsets[i] : 0; |
| 3919 |
uint64_t cb_addr = hk_buffer_address_rw(buffer, offset); |
| 3920 |
uint32_t cmd_idx = firstCounterBuffer + i; |
| 3921 |
|
| 3922 |
if (begin) { |
| 3923 |
params.src[cmd_idx] = cb_addr; |
| 3924 |
} else { |
| 3925 |
params.dest[copies] = cb_addr; |
| 3926 |
params.src[copies] = gfx->xfb_offsets + cmd_idx * sizeof(uint32_t); |
| 3927 |
++copies; |
| 3928 |
} |
| 3929 |
} |
| 3930 |
|
| 3931 |
if (begin) |
| 3932 |
copies = 4; |
| 3933 |
|
| 3934 |
if (copies > 0) { |
| 3935 |
perf_debug(cmd, "XFB counter copy"); |
| 3936 |
|
| 3937 |
libagx_copy_xfb_counters(cmd, agx_1d(copies), |
| 3938 |
AGX_BARRIER_ALL | AGX_PREGFX, |
| 3939 |
hk_pool_upload(cmd, ¶ms, sizeof(params), 8)); |
| 3940 |
} |
| 3941 |
} |
| 3942 |
|
| 3943 |
VKAPI_ATTR void VKAPI_CALL |
| 3944 |
hk_CmdBeginTransformFeedbackEXT(VkCommandBuffer commandBuffer, |
| 3945 |
uint32_t firstCounterBuffer, |
| 3946 |
uint32_t counterBufferCount, |
| 3947 |
const VkBuffer *pCounterBuffers, |
| 3948 |
const VkDeviceSize *pCounterBufferOffsets) |
| 3949 |
{ |
| 3950 |
hk_begin_end_xfb(commandBuffer, firstCounterBuffer, counterBufferCount, |
| 3951 |
pCounterBuffers, pCounterBufferOffsets, true); |
| 3952 |
} |
| 3953 |
|
| 3954 |
VKAPI_ATTR void VKAPI_CALL |
| 3955 |
hk_CmdEndTransformFeedbackEXT(VkCommandBuffer commandBuffer, |
| 3956 |
uint32_t firstCounterBuffer, |
| 3957 |
uint32_t counterBufferCount, |
| 3958 |
const VkBuffer *pCounterBuffers, |
| 3959 |
const VkDeviceSize *pCounterBufferOffsets) |
| 3960 |
{ |
| 3961 |
hk_begin_end_xfb(commandBuffer, firstCounterBuffer, counterBufferCount, |
| 3962 |
pCounterBuffers, pCounterBufferOffsets, false); |
| 3963 |
} |
| 3964 |
|
| 3965 |
VKAPI_ATTR void VKAPI_CALL |
| 3966 |
hk_CmdBeginConditionalRenderingEXT( |
| 3967 |
VkCommandBuffer commandBuffer, |
| 3968 |
const VkConditionalRenderingBeginInfoEXT *pConditionalRenderingBegin) |
| 3969 |
{ |
| 3970 |
UNREACHABLE("stub"); |
| 3971 |
} |
| 3972 |
|
| 3973 |
VKAPI_ATTR void VKAPI_CALL |
| 3974 |
hk_CmdEndConditionalRenderingEXT(VkCommandBuffer commandBuffer) |
| 3975 |
{ |
| 3976 |
UNREACHABLE("stub"); |
| 3977 |
} |