| 1 |
/* |
| 2 |
* Copyright 2025 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/libcl/libcl.h" |
| 7 |
#include "compiler/libcl/libcl_vk.h" |
| 8 |
#include "compiler/shader_enums.h" |
| 9 |
#include "genxml/gen_macros.h" |
| 10 |
#include "lib/pan_encoder.h" |
| 11 |
#include "poly/cl/restart.h" |
| 12 |
#include "draw_helper.h" |
| 13 |
|
| 14 |
#if PAN_ARCH >= 10 |
| 15 |
|
| 16 |
/* Subgroup size is always 16 on v10+ */ |
| 17 |
#define SUBGROUP_SIZE 16 |
| 18 |
|
| 19 |
KERNEL(SUBGROUP_SIZE * SUBGROUP_SIZE) |
| 20 |
panlib_update_prims_generated_query_restart( |
| 21 |
global atomic_uint *prims_generated, uint64_t index_buffer, |
| 22 |
uint32_t index_buffer_size_el, uint32_t cmd_stride, |
| 23 |
constant VkDrawIndexedIndirectCommand *cmd, uint32_t view_count, |
| 24 |
uint32_t compact_prim__11, uint32_t index_bytes_log2__3) |
| 25 |
{ |
| 26 |
assert(get_sub_group_size() == SUBGROUP_SIZE); |
| 27 |
|
| 28 |
enum mesa_prim prim = poly_uncompact_prim(compact_prim__11); |
| 29 |
uint32_t index_bytes = 1 << index_bytes_log2__3; |
| 30 |
uint32_t restart_index = (1 << (index_bytes * 8)) - 1; |
| 31 |
|
| 32 |
uint32_t tid = cl_local_id.x; |
| 33 |
uint32_t draw_id = cl_group_id.x; |
| 34 |
|
| 35 |
cmd = (constant VkDrawIndexedIndirectCommand *) |
| 36 |
((uintptr_t) cmd + cmd_stride * draw_id); |
| 37 |
|
| 38 |
POLY_DECL_UNROLL_RESTART_SCRATCH(scratch, SUBGROUP_SIZE * SUBGROUP_SIZE); |
| 39 |
uint32_t prims_per_instance = poly_count_restart_prims( |
| 40 |
(constant uint32_t *) cmd, index_buffer, index_buffer_size_el, |
| 41 |
index_bytes, restart_index, prim, scratch); |
| 42 |
|
| 43 |
if (tid == 0) |
| 44 |
atomic_fetch_add(prims_generated, |
| 45 |
prims_per_instance * cmd->instanceCount * view_count); |
| 46 |
} |
| 47 |
|
| 48 |
KERNEL(1) |
| 49 |
panlib_update_prims_generated_query_indirect( |
| 50 |
global uint32_t *prims_generated, global uint32_t *draw_count_buffer, |
| 51 |
uint32_t max_draw_count, uint32_t cmd_stride, constant uint32_t *cmd, |
| 52 |
uint32_t view_count, uint32_t compact_prim__11) |
| 53 |
{ |
| 54 |
enum mesa_prim prim = poly_uncompact_prim(compact_prim__11); |
| 55 |
uint32_t draw_count = draw_count_buffer ? |
| 56 |
min(*draw_count_buffer, max_draw_count) : max_draw_count; |
| 57 |
|
| 58 |
for (uint32_t draw_id = 0; draw_id < draw_count; draw_id++) { |
| 59 |
/* cmd may be either VkDrawnIndirectCommand or |
| 60 |
* VkDrawIndexedIndirectCommand. In both cases the vertex/index count is |
| 61 |
* the first field, and the instance count is the second */ |
| 62 |
uint32_t vertex_count = cmd[0]; |
| 63 |
uint32_t instance_count = cmd[1]; |
| 64 |
|
| 65 |
uint32_t prims_per_instance = |
| 66 |
u_decomposed_prims_for_vertices(prim, vertex_count); |
| 67 |
*prims_generated += prims_per_instance * instance_count; |
| 68 |
|
| 69 |
cmd = (constant uint32_t *) ((uintptr_t) cmd + cmd_stride); |
| 70 |
} |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
#if (PAN_ARCH == 6 || PAN_ARCH == 7) |
| 75 |
struct panlib_draw_info { |
| 76 |
struct { |
| 77 |
uint32_t size; |
| 78 |
uint32_t offset; |
| 79 |
} index; |
| 80 |
|
| 81 |
struct { |
| 82 |
int32_t raw_offset; |
| 83 |
int32_t base; |
| 84 |
uint32_t count; |
| 85 |
} vertex; |
| 86 |
|
| 87 |
struct { |
| 88 |
int32_t base; |
| 89 |
uint32_t count; |
| 90 |
} instance; |
| 91 |
|
| 92 |
struct { |
| 93 |
global struct mali_attribute_buffer_packed *descs; |
| 94 |
global struct libpan_draw_helper_varying_buf_info *info; |
| 95 |
} varying_bufs; |
| 96 |
|
| 97 |
struct { |
| 98 |
global struct mali_attribute_buffer_packed *descs; |
| 99 |
global struct libpan_draw_helper_attrib_buf_info *infos; |
| 100 |
uint32_t valid; |
| 101 |
} attrib_bufs; |
| 102 |
|
| 103 |
struct { |
| 104 |
global struct mali_attribute_packed *descs; |
| 105 |
global struct libpan_draw_helper_attrib_info *infos; |
| 106 |
uint32_t valid; |
| 107 |
} attribs; |
| 108 |
|
| 109 |
global uint8_t *idvs_job; |
| 110 |
global uint8_t *vertex_job; |
| 111 |
global uint8_t *tiler_job; |
| 112 |
|
| 113 |
uint32_t vertex_range; |
| 114 |
uint32_t padded_vertex_count; |
| 115 |
uint8_t primitive_vertex_count; |
| 116 |
global uint8_t *indices; |
| 117 |
|
| 118 |
uint64_t position; |
| 119 |
uint64_t psiz; |
| 120 |
}; |
| 121 |
|
| 122 |
static void |
| 123 |
panlib_patch_draw_vertex_dcd(struct panlib_draw_info *draw) |
| 124 |
{ |
| 125 |
global struct mali_invocation_packed *vertex_invocation; |
| 126 |
global struct mali_draw_packed *vertex_dcd; |
| 127 |
|
| 128 |
if (draw->idvs_job != NULL) { |
| 129 |
vertex_invocation = |
| 130 |
(global struct mali_invocation_packed *)(draw->idvs_job + |
| 131 |
pan_section_offset( |
| 132 |
INDEXED_VERTEX_JOB, |
| 133 |
INVOCATION)); |
| 134 |
vertex_dcd = (global struct mali_draw_packed *)(draw->idvs_job + |
| 135 |
pan_section_offset( |
| 136 |
INDEXED_VERTEX_JOB, |
| 137 |
VERTEX_DRAW)); |
| 138 |
} else { |
| 139 |
vertex_invocation = |
| 140 |
(global struct mali_invocation_packed *)(draw->vertex_job + |
| 141 |
pan_section_offset( |
| 142 |
COMPUTE_JOB, INVOCATION)); |
| 143 |
vertex_dcd = (global struct mali_draw_packed *)(draw->vertex_job + |
| 144 |
pan_section_offset( |
| 145 |
COMPUTE_JOB, DRAW)); |
| 146 |
} |
| 147 |
|
| 148 |
/* Patch the number of invocations on the vertex job */ |
| 149 |
pan_pack_work_groups_compute(vertex_invocation, 1, draw->vertex_range, |
| 150 |
draw->instance.count, 1, 1, 1, true, false); |
| 151 |
|
| 152 |
/* Patch the draw descriptor */ |
| 153 |
pan_unpack(vertex_dcd, DRAW, unpacked_vertex_dcd) |
| 154 |
; |
| 155 |
pan_pack(vertex_dcd, DRAW, cfg) { |
| 156 |
memcpy(&cfg, &unpacked_vertex_dcd, sizeof(cfg)); |
| 157 |
cfg.offset_start = draw->vertex.raw_offset; |
| 158 |
cfg.instance_size = |
| 159 |
draw->instance.count > 1 ? draw->padded_vertex_count : 1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
panlib_patch_draw_tiler_dcd(struct panlib_draw_info *draw) |
| 165 |
{ |
| 166 |
/* No tiler job, nothing to do here */ |
| 167 |
if (draw->idvs_job == NULL && draw->tiler_job == NULL) |
| 168 |
return; |
| 169 |
|
| 170 |
global struct mali_draw_packed *tiler_dcd; |
| 171 |
global struct mali_primitive_packed *tiler_primitive; |
| 172 |
global struct mali_primitive_size_packed *tiler_primsz; |
| 173 |
|
| 174 |
if (draw->idvs_job != NULL) { |
| 175 |
tiler_dcd = (global struct mali_draw_packed *)(draw->idvs_job + |
| 176 |
pan_section_offset( |
| 177 |
INDEXED_VERTEX_JOB, |
| 178 |
FRAGMENT_DRAW)); |
| 179 |
tiler_primitive = |
| 180 |
(global struct mali_primitive_packed *)(draw->idvs_job + |
| 181 |
pan_section_offset( |
| 182 |
INDEXED_VERTEX_JOB, |
| 183 |
PRIMITIVE)); |
| 184 |
tiler_primsz = |
| 185 |
(global struct mali_primitive_size_packed *)(draw->idvs_job + |
| 186 |
pan_section_offset( |
| 187 |
INDEXED_VERTEX_JOB, |
| 188 |
PRIMITIVE_SIZE)); |
| 189 |
} else { |
| 190 |
global struct mali_invocation_packed *vertex_invocation = |
| 191 |
(global struct mali_invocation_packed *)(draw->vertex_job + |
| 192 |
pan_section_offset( |
| 193 |
COMPUTE_JOB, INVOCATION)); |
| 194 |
global struct mali_invocation_packed *tiler_invocation = |
| 195 |
(global struct mali_invocation_packed *)(draw->tiler_job + |
| 196 |
pan_section_offset( |
| 197 |
TILER_JOB, INVOCATION)); |
| 198 |
/* Patch the tiler job invocation if present (identical to the vertex |
| 199 |
* invocations) */ |
| 200 |
memcpy(tiler_invocation, vertex_invocation, pan_size(INVOCATION)); |
| 201 |
|
| 202 |
tiler_dcd = (global struct mali_draw_packed *)(draw->tiler_job + |
| 203 |
pan_section_offset( |
| 204 |
TILER_JOB, DRAW)); |
| 205 |
tiler_primitive = |
| 206 |
(global struct mali_primitive_packed *)(draw->tiler_job + |
| 207 |
pan_section_offset(TILER_JOB, |
| 208 |
PRIMITIVE)); |
| 209 |
tiler_primsz = |
| 210 |
(global struct mali_primitive_size_packed *)(draw->tiler_job + |
| 211 |
pan_section_offset( |
| 212 |
TILER_JOB, |
| 213 |
PRIMITIVE_SIZE)); |
| 214 |
} |
| 215 |
|
| 216 |
/* Patch the attribute start offset */ |
| 217 |
pan_unpack(tiler_dcd, DRAW, unpacked_tiler_dcd) |
| 218 |
; |
| 219 |
pan_pack(tiler_dcd, DRAW, cfg) { |
| 220 |
memcpy(&cfg, &unpacked_tiler_dcd, sizeof(cfg)); |
| 221 |
cfg.position = draw->position; |
| 222 |
cfg.offset_start = draw->vertex.raw_offset; |
| 223 |
cfg.instance_size = |
| 224 |
draw->instance.count > 1 ? draw->padded_vertex_count : 1; |
| 225 |
uint32_t primitives_per_instance = |
| 226 |
DIV_ROUND_UP(draw->padded_vertex_count, draw->primitive_vertex_count); |
| 227 |
cfg.instance_primitive_size = |
| 228 |
pan_padded_vertex_count(primitives_per_instance); |
| 229 |
} |
| 230 |
|
| 231 |
/* Patch the primitive vertex offset to take offset_start into account in |
| 232 |
* case of indexed draw */ |
| 233 |
pan_unpack(tiler_primitive, PRIMITIVE, unpacked_tiler_primitive) |
| 234 |
; |
| 235 |
|
| 236 |
pan_pack(tiler_primitive, PRIMITIVE, cfg) { |
| 237 |
memcpy(&cfg, &unpacked_tiler_primitive, sizeof(cfg)); |
| 238 |
cfg.index_count = draw->vertex.count; |
| 239 |
|
| 240 |
if (draw->index.size) { |
| 241 |
cfg.indices = |
| 242 |
(uint64_t)draw->indices + draw->index.offset * draw->index.size; |
| 243 |
cfg.base_vertex_offset = |
| 244 |
(int64_t)draw->vertex.base - draw->vertex.raw_offset; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* In case we have a point size, we need to patch the descriptor there */ |
| 249 |
if (unpacked_tiler_primitive.point_size_array_format != |
| 250 |
MALI_POINT_SIZE_ARRAY_FORMAT_NONE) { |
| 251 |
pan_pack(tiler_primsz, PRIMITIVE_SIZE, cfg) { |
| 252 |
cfg.size_array = draw->psiz; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
panlib_patch_varying_bufs(struct panlib_draw_info *draw) |
| 259 |
{ |
| 260 |
uint32_t vertex_count = draw->padded_vertex_count * draw->instance.count; |
| 261 |
|
| 262 |
for (uint32_t i = 0; i < PANLIB_VARY_BUF_MAX; i++) { |
| 263 |
global struct libpan_draw_helper_varying_buf_info *info = |
| 264 |
&draw->varying_bufs.info[0]; |
| 265 |
|
| 266 |
global struct mali_attribute_buffer_packed *desc = |
| 267 |
&draw->varying_bufs.descs[i]; |
| 268 |
pan_unpack(desc, ATTRIBUTE_BUFFER, unpacked_desc) |
| 269 |
; |
| 270 |
|
| 271 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 272 |
memcpy(&cfg, &unpacked_desc, sizeof(cfg)); |
| 273 |
cfg.size = vertex_count * cfg.stride; |
| 274 |
|
| 275 |
/* Ensure we are aligned properly */ |
| 276 |
uint32_t offset_add = ALIGN_POT(cfg.size, 64); |
| 277 |
|
| 278 |
/* Allocate some space for ourself */ |
| 279 |
uint32_t current_offset = atomic_fetch_add_explicit( |
| 280 |
&info->offset, offset_add, memory_order_relaxed); |
| 281 |
|
| 282 |
/* Sanity check that we don't overrun the buffer */ |
| 283 |
assert(current_offset + cfg.size <= info->size); |
| 284 |
|
| 285 |
cfg.pointer = info->address + current_offset; |
| 286 |
|
| 287 |
if (i == PANLIB_VARY_BUF_POSITION) |
| 288 |
draw->position = cfg.pointer; |
| 289 |
else if (i == PANLIB_VARY_BUF_PSIZ) |
| 290 |
draw->psiz = cfg.pointer; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static void |
| 296 |
panlib_patch_attrib_buf(struct panlib_draw_info *draw, |
| 297 |
global struct mali_attribute_buffer_packed *desc, |
| 298 |
struct libpan_draw_helper_attrib_buf_info info) |
| 299 |
{ |
| 300 |
uint32_t divisor = draw->padded_vertex_count * info.divisor; |
| 301 |
global struct mali_attribute_buffer_continuation_npot_packed *desc_ext = |
| 302 |
(global struct mali_attribute_buffer_continuation_npot_packed *)&desc[1]; |
| 303 |
|
| 304 |
/* We are effectively recreating the attribute buffer descriptors. The only |
| 305 |
* trustworthy infos in the descriptor are the pointer and size */ |
| 306 |
pan_unpack(desc, ATTRIBUTE_BUFFER, unpacked_desc) |
| 307 |
; |
| 308 |
|
| 309 |
if (draw->instance.count <= 1) { |
| 310 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 311 |
cfg.type = MALI_ATTRIBUTE_TYPE_1D; |
| 312 |
cfg.stride = info.per_instance ? 0 : info.stride; |
| 313 |
cfg.pointer = unpacked_desc.pointer; |
| 314 |
cfg.size = unpacked_desc.size; |
| 315 |
} |
| 316 |
} else if (!info.per_instance) { |
| 317 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 318 |
cfg.type = MALI_ATTRIBUTE_TYPE_1D_MODULUS; |
| 319 |
cfg.divisor = draw->padded_vertex_count; |
| 320 |
cfg.stride = info.stride; |
| 321 |
cfg.pointer = unpacked_desc.pointer; |
| 322 |
cfg.size = unpacked_desc.size; |
| 323 |
} |
| 324 |
} else if (!divisor) { |
| 325 |
/* instance_divisor == 0 means all instances share the same value. |
| 326 |
* Make it a 1D array with a zero stride. |
| 327 |
*/ |
| 328 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 329 |
cfg.type = MALI_ATTRIBUTE_TYPE_1D; |
| 330 |
cfg.stride = 0; |
| 331 |
cfg.pointer = unpacked_desc.pointer; |
| 332 |
cfg.size = unpacked_desc.size; |
| 333 |
} |
| 334 |
} else if (util_is_power_of_two_or_zero(divisor)) { |
| 335 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 336 |
cfg.type = MALI_ATTRIBUTE_TYPE_1D_POT_DIVISOR; |
| 337 |
cfg.stride = info.stride; |
| 338 |
cfg.pointer = unpacked_desc.pointer; |
| 339 |
cfg.size = unpacked_desc.size; |
| 340 |
cfg.divisor_r = __builtin_ctz(divisor); |
| 341 |
} |
| 342 |
} else { |
| 343 |
uint32_t divisor_r = 0, divisor_e = 0; |
| 344 |
uint32_t divisor_d = |
| 345 |
pan_compute_npot_divisor(divisor, &divisor_r, &divisor_e); |
| 346 |
pan_pack(desc, ATTRIBUTE_BUFFER, cfg) { |
| 347 |
cfg.type = MALI_ATTRIBUTE_TYPE_1D_NPOT_DIVISOR; |
| 348 |
cfg.stride = info.stride; |
| 349 |
cfg.pointer = unpacked_desc.pointer; |
| 350 |
cfg.size = unpacked_desc.size; |
| 351 |
cfg.divisor_r = divisor_r; |
| 352 |
cfg.divisor_e = divisor_e; |
| 353 |
} |
| 354 |
|
| 355 |
pan_pack(desc_ext, ATTRIBUTE_BUFFER_CONTINUATION_NPOT, cfg) { |
| 356 |
cfg.type = MALI_ATTRIBUTE_TYPE_CONTINUATION; |
| 357 |
cfg.divisor_numerator = divisor_d; |
| 358 |
cfg.divisor = info.divisor; |
| 359 |
} |
| 360 |
|
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
/* If the buffer extension wasn't used, memset(0) */ |
| 365 |
for (uint32_t i = 0; i < pan_size(ATTRIBUTE_BUFFER) / 4; i++) |
| 366 |
desc_ext->opaque[i] = 0; |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
panlib_patch_attrib(struct panlib_draw_info *draw, |
| 371 |
global struct mali_attribute_packed *desc, |
| 372 |
struct libpan_draw_helper_attrib_info info) |
| 373 |
{ |
| 374 |
/* We are effectively recreating the attribute descriptors. The only |
| 375 |
* trustworthy infos in the descriptor are the buffer index and format */ |
| 376 |
pan_unpack(desc, ATTRIBUTE, unpacked_desc) |
| 377 |
; |
| 378 |
|
| 379 |
pan_pack(desc, ATTRIBUTE, cfg) { |
| 380 |
cfg.buffer_index = unpacked_desc.buffer_index; |
| 381 |
cfg.offset = info.base_offset + draw->instance.base * info.stride; |
| 382 |
cfg.offset_enable = true; |
| 383 |
cfg.format = unpacked_desc.format; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
static void |
| 388 |
panlib_patch_job_type_header(global struct mali_job_header_packed *desc, |
| 389 |
enum mali_job_type job_type) |
| 390 |
{ |
| 391 |
if (desc == NULL) |
| 392 |
return; |
| 393 |
|
| 394 |
pan_unpack(desc, JOB_HEADER, unpacked_desc) |
| 395 |
; |
| 396 |
|
| 397 |
pan_pack(desc, JOB_HEADER, cfg) { |
| 398 |
memcpy(&cfg, &unpacked_desc, sizeof(cfg)); |
| 399 |
cfg.type = job_type; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static void |
| 404 |
panlib_patch_draw(struct panlib_draw_info *draw) |
| 405 |
{ |
| 406 |
/* First of all, we need to ensure each jobs have the proper header */ |
| 407 |
bool is_null_job = draw->vertex_range == 0 || draw->instance.count == 0; |
| 408 |
|
| 409 |
if (draw->idvs_job != NULL) { |
| 410 |
panlib_patch_job_type_header( |
| 411 |
(global struct mali_job_header_packed *)draw->idvs_job, |
| 412 |
is_null_job ? MALI_JOB_TYPE_NULL : MALI_JOB_TYPE_INDEXED_VERTEX); |
| 413 |
} else { |
| 414 |
panlib_patch_job_type_header( |
| 415 |
(global struct mali_job_header_packed *)draw->vertex_job, |
| 416 |
is_null_job ? MALI_JOB_TYPE_NULL : MALI_JOB_TYPE_VERTEX); |
| 417 |
panlib_patch_job_type_header( |
| 418 |
(global struct mali_job_header_packed *)draw->tiler_job, |
| 419 |
is_null_job ? MALI_JOB_TYPE_NULL : MALI_JOB_TYPE_TILER); |
| 420 |
} |
| 421 |
|
| 422 |
/* In case there is nothing to draw, let's just bail out. */ |
| 423 |
if (is_null_job) |
| 424 |
return; |
| 425 |
|
| 426 |
/* Next we patch varying buffer descriptors and collect position and point |
| 427 |
* size varying addresses */ |
| 428 |
panlib_patch_varying_bufs(draw); |
| 429 |
|
| 430 |
/* Patch the vertex draw and invocation descriptors */ |
| 431 |
panlib_patch_draw_vertex_dcd(draw); |
| 432 |
|
| 433 |
/* Patch the tiler descriptors if present */ |
| 434 |
panlib_patch_draw_tiler_dcd(draw); |
| 435 |
|
| 436 |
/* Finally, patch attribute descriptors */ |
| 437 |
uint32_t num_vbs = util_last_bit(draw->attrib_bufs.valid); |
| 438 |
for (uint32_t i = 0; i < num_vbs; i++) { |
| 439 |
if (draw->attrib_bufs.valid & BITFIELD_BIT(i)) |
| 440 |
panlib_patch_attrib_buf(draw, &draw->attrib_bufs.descs[i * 2], |
| 441 |
draw->attrib_bufs.infos[i]); |
| 442 |
} |
| 443 |
|
| 444 |
uint32_t num_vs_attribs = util_last_bit(draw->attribs.valid); |
| 445 |
for (uint32_t i = 0; i < num_vs_attribs; i++) { |
| 446 |
if (draw->attribs.valid & BITFIELD_BIT(i)) |
| 447 |
panlib_patch_attrib(draw, &draw->attribs.descs[i], |
| 448 |
draw->attribs.infos[i]); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
static uint32_t |
| 453 |
padded_vertex_count(uint32_t vertex_count, uint32_t instance_count, bool idvs) |
| 454 |
{ |
| 455 |
if (instance_count == 1) |
| 456 |
return vertex_count; |
| 457 |
|
| 458 |
/* Index-Driven Vertex Shading requires different instances to |
| 459 |
* have different cache lines for position results. Each vertex |
| 460 |
* position is 16 bytes and the Mali cache line is 64 bytes, so |
| 461 |
* the instance count must be aligned to 4 vertices. |
| 462 |
*/ |
| 463 |
if (idvs) |
| 464 |
vertex_count = ALIGN_POT(vertex_count, 4); |
| 465 |
|
| 466 |
return pan_padded_vertex_count(vertex_count); |
| 467 |
} |
| 468 |
|
| 469 |
/* Argument ordering choosen to avoid any padding */ |
| 470 |
|
| 471 |
KERNEL(1) |
| 472 |
panlib_draw_indirect_helper( |
| 473 |
global VkDrawIndirectCommand *cmd, |
| 474 |
global struct mali_attribute_buffer_packed *varying_bufs_descs, |
| 475 |
global struct libpan_draw_helper_varying_buf_info *varying_bufs_info, |
| 476 |
global struct mali_attribute_buffer_packed *attrib_bufs_descs, |
| 477 |
global struct libpan_draw_helper_attrib_buf_info *attrib_bufs_infos, |
| 478 |
uint32_t attrib_bufs_valid, uint32_t attribs_valid, |
| 479 |
global struct mali_attribute_packed *attribs_descs, |
| 480 |
global struct libpan_draw_helper_attrib_info *attribs_infos, |
| 481 |
global uint32_t *first_vertex_sysval, global uint32_t *first_instance_sysval, |
| 482 |
global uint32_t *raw_vertex_offset_sysval, global uint8_t *idvs_job, |
| 483 |
global uint8_t *vertex_job, global uint8_t *tiler_job, |
| 484 |
uint32_t primitive_vertex_count) |
| 485 |
{ |
| 486 |
const uint32_t vertex_count = cmd->vertexCount; |
| 487 |
const uint32_t instance_count = cmd->instanceCount; |
| 488 |
const uint32_t first_vertex = cmd->firstVertex; |
| 489 |
const uint32_t first_instance = cmd->firstInstance; |
| 490 |
|
| 491 |
struct panlib_draw_info draw = { |
| 492 |
.idvs_job = idvs_job, |
| 493 |
.vertex_job = vertex_job, |
| 494 |
.tiler_job = tiler_job, |
| 495 |
|
| 496 |
.vertex.base = first_vertex, |
| 497 |
.vertex.raw_offset = first_vertex, |
| 498 |
.vertex.count = vertex_count, |
| 499 |
.instance.base = first_instance, |
| 500 |
.instance.count = instance_count, |
| 501 |
|
| 502 |
.varying_bufs.descs = varying_bufs_descs, |
| 503 |
.varying_bufs.info = varying_bufs_info, |
| 504 |
|
| 505 |
.attrib_bufs.descs = attrib_bufs_descs, |
| 506 |
.attrib_bufs.infos = attrib_bufs_infos, |
| 507 |
.attrib_bufs.valid = attrib_bufs_valid, |
| 508 |
|
| 509 |
.attribs.descs = attribs_descs, |
| 510 |
.attribs.infos = attribs_infos, |
| 511 |
.attribs.valid = attribs_valid, |
| 512 |
|
| 513 |
.vertex_range = vertex_count, |
| 514 |
.padded_vertex_count = |
| 515 |
padded_vertex_count(vertex_count, instance_count, idvs_job != NULL), |
| 516 |
.primitive_vertex_count = primitive_vertex_count, |
| 517 |
}; |
| 518 |
|
| 519 |
panlib_patch_draw(&draw); |
| 520 |
|
| 521 |
*first_vertex_sysval = draw.vertex.base; |
| 522 |
*first_instance_sysval = draw.instance.base; |
| 523 |
*raw_vertex_offset_sysval = draw.vertex.raw_offset; |
| 524 |
} |
| 525 |
|
| 526 |
KERNEL(1) |
| 527 |
panlib_draw_indexed_indirect_helper( |
| 528 |
global VkDrawIndexedIndirectCommand *cmd, global uint8_t *index_buffer_ptr, |
| 529 |
global struct libpan_draw_helper_index_min_max_result *index_min_max_res, |
| 530 |
uint32_t index_size, uint32_t primitive_vertex_count, |
| 531 |
uint32_t attrib_bufs_valid, uint32_t attribs_valid, |
| 532 |
global struct mali_attribute_buffer_packed *varying_bufs_descs, |
| 533 |
global struct libpan_draw_helper_varying_buf_info *varying_bufs_info, |
| 534 |
global struct mali_attribute_buffer_packed *attrib_bufs_descs, |
| 535 |
global struct libpan_draw_helper_attrib_buf_info *attrib_bufs_infos, |
| 536 |
global struct mali_attribute_packed *attribs_descs, |
| 537 |
global struct libpan_draw_helper_attrib_info *attribs_infos, |
| 538 |
global uint32_t *first_vertex_sysval, global uint32_t *first_instance_sysval, |
| 539 |
global uint32_t *raw_vertex_offset_sysval, global uint8_t *idvs_job, |
| 540 |
global uint8_t *vertex_job, global uint8_t *tiler_job) |
| 541 |
{ |
| 542 |
const uint32_t index_count = cmd->indexCount; |
| 543 |
const uint32_t first_index = cmd->firstIndex; |
| 544 |
const uint32_t first_instance = cmd->firstInstance; |
| 545 |
const uint32_t instance_count = cmd->instanceCount; |
| 546 |
const int32_t vertex_offset = cmd->vertexOffset; |
| 547 |
const uint32_t min_vertex = index_min_max_res->min; |
| 548 |
const uint32_t max_vertex = index_min_max_res->max; |
| 549 |
const uint32_t vertex_range = max_vertex - min_vertex + 1; |
| 550 |
|
| 551 |
struct panlib_draw_info draw = { |
| 552 |
.idvs_job = idvs_job, |
| 553 |
.vertex_job = vertex_job, |
| 554 |
.tiler_job = tiler_job, |
| 555 |
|
| 556 |
.index.size = index_size, |
| 557 |
.index.offset = first_index, |
| 558 |
.vertex.base = vertex_offset, |
| 559 |
.vertex.raw_offset = min_vertex + vertex_offset, |
| 560 |
.vertex.count = index_count, |
| 561 |
.instance.base = first_instance, |
| 562 |
.instance.count = instance_count, |
| 563 |
|
| 564 |
.varying_bufs.descs = varying_bufs_descs, |
| 565 |
.varying_bufs.info = varying_bufs_info, |
| 566 |
|
| 567 |
.attrib_bufs.descs = attrib_bufs_descs, |
| 568 |
.attrib_bufs.infos = attrib_bufs_infos, |
| 569 |
.attrib_bufs.valid = attrib_bufs_valid, |
| 570 |
|
| 571 |
.attribs.descs = attribs_descs, |
| 572 |
.attribs.infos = attribs_infos, |
| 573 |
.attribs.valid = attribs_valid, |
| 574 |
|
| 575 |
.vertex_range = vertex_range, |
| 576 |
.padded_vertex_count = |
| 577 |
padded_vertex_count(vertex_range, instance_count, idvs_job != NULL), |
| 578 |
.primitive_vertex_count = primitive_vertex_count, |
| 579 |
.indices = index_buffer_ptr, |
| 580 |
}; |
| 581 |
|
| 582 |
panlib_patch_draw(&draw); |
| 583 |
|
| 584 |
*first_vertex_sysval = draw.vertex.base; |
| 585 |
*first_instance_sysval = draw.instance.base; |
| 586 |
*raw_vertex_offset_sysval = draw.vertex.raw_offset; |
| 587 |
} |
| 588 |
|
| 589 |
KERNEL(64) |
| 590 |
panlib_draw_index_minmax_search_helper(global uint8_t *index_buffer_ptr, |
| 591 |
global VkDrawIndexedIndirectCommand *cmd, |
| 592 |
global atomic_uint *min_ptr, |
| 593 |
global atomic_uint *max_ptr, |
| 594 |
uint32_t index_bytes_log2__3, |
| 595 |
uint8_t primitive_restart__2) |
| 596 |
{ |
| 597 |
/* Max count of values to process per thread */ |
| 598 |
const uint32_t max_count_per_thread = 1024; |
| 599 |
|
| 600 |
const uint32_t index_bit_size = (1 << index_bytes_log2__3) * 8; |
| 601 |
const uint32_t start = cmd->firstIndex; |
| 602 |
const uint32_t index_count = cmd->indexCount; |
| 603 |
|
| 604 |
uint32_t base_idx = cl_global_id.x * max_count_per_thread; |
| 605 |
|
| 606 |
/* If the thread is out of range, bail out */ |
| 607 |
if (base_idx >= index_count) |
| 608 |
return; |
| 609 |
|
| 610 |
/* Compute expected max iteration to do in this thread */ |
| 611 |
uint32_t count = MIN2(max_count_per_thread, index_count - base_idx); |
| 612 |
|
| 613 |
/* Sanity check so nothing weird will happen */ |
| 614 |
assert(base_idx + count <= index_count); |
| 615 |
|
| 616 |
uint32_t local_min = ((uint64_t)1 << index_bit_size) - 1; |
| 617 |
uint32_t local_max = 0; |
| 618 |
|
| 619 |
switch (index_bit_size) { |
| 620 |
#define MINMAX_SEARCH_CASE(sz) \ |
| 621 |
case sz: { \ |
| 622 |
global uint##sz##_t *indices = (global uint##sz##_t *)index_buffer_ptr; \ |
| 623 |
for (uint32_t i = 0; i < count; i++) { \ |
| 624 |
uint32_t val = (uint32_t)indices[start + base_idx + i]; \ |
| 625 |
if (primitive_restart__2 && val == UINT##sz##_MAX) \ |
| 626 |
continue; \ |
| 627 |
local_min = min(local_min, val); \ |
| 628 |
local_max = max(local_max, val); \ |
| 629 |
} \ |
| 630 |
break; \ |
| 631 |
} |
| 632 |
MINMAX_SEARCH_CASE(32) |
| 633 |
MINMAX_SEARCH_CASE(16) |
| 634 |
MINMAX_SEARCH_CASE(8) |
| 635 |
#undef MINMAX_SEARCH_CASE |
| 636 |
default: |
| 637 |
assert(0 && "Invalid index size"); |
| 638 |
} |
| 639 |
|
| 640 |
atomic_fetch_min(min_ptr, local_min); |
| 641 |
atomic_fetch_max(max_ptr, local_max); |
| 642 |
} |
| 643 |
|
| 644 |
#endif |