| 1 |
/* |
| 2 |
* Copyright 2023 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "poly/geometry.h" |
| 7 |
#include "poly/tessellator.h" |
| 8 |
|
| 9 |
KERNEL(1) |
| 10 |
libagx_tess_setup_indirect( |
| 11 |
global struct poly_tess_params *p, |
| 12 |
global uint32_t *grids /* output: VS then TCS then tess */, |
| 13 |
global struct poly_vertex_params *vp /* output */, global uint32_t *indirect, |
| 14 |
uint64_t in_index_buffer, |
| 15 |
uint32_t in_index_buffer_range_el, uint32_t in_index_size_B, |
| 16 |
uint64_t vertex_outputs /* bitfield */, |
| 17 |
|
| 18 |
/* Tess control invocation counter if active, else zero */ |
| 19 |
global uint32_t *tcs_statistic) |
| 20 |
{ |
| 21 |
uint count = indirect[0], instance_count = indirect[1]; |
| 22 |
unsigned in_patches = count / p->input_patch_size; |
| 23 |
|
| 24 |
/* TCS invocation counter increments once per-patch */ |
| 25 |
if (tcs_statistic) { |
| 26 |
*tcs_statistic += in_patches; |
| 27 |
} |
| 28 |
|
| 29 |
size_t draw_stride = 5 * sizeof(uint32_t); |
| 30 |
unsigned unrolled_patches = in_patches * instance_count; |
| 31 |
|
| 32 |
uint32_t alloc = 0; |
| 33 |
uint32_t tcs_out_offs = alloc; |
| 34 |
alloc += unrolled_patches * p->tcs_stride_el * 4; |
| 35 |
|
| 36 |
uint32_t patch_coord_offs = alloc; |
| 37 |
alloc += unrolled_patches * 4; |
| 38 |
|
| 39 |
uint32_t count_offs = alloc; |
| 40 |
alloc += unrolled_patches * sizeof(uint32_t); |
| 41 |
|
| 42 |
uint vb_offs = alloc; |
| 43 |
uint vb_size = poly_tcs_in_size(count * instance_count, vertex_outputs); |
| 44 |
alloc += vb_size; |
| 45 |
|
| 46 |
/* Allocate all patch calculations in one go */ |
| 47 |
global uchar *blob = poly_heap_alloc(p->heap, alloc); |
| 48 |
|
| 49 |
p->tcs_buffer = (global float *)(blob + tcs_out_offs); |
| 50 |
p->patches_per_instance = in_patches; |
| 51 |
p->coord_allocs = (global uint *)(blob + patch_coord_offs); |
| 52 |
p->nr_patches = unrolled_patches; |
| 53 |
|
| 54 |
vp->output_buffer = (uintptr_t)(blob + vb_offs); |
| 55 |
vp->outputs = vertex_outputs; |
| 56 |
p->counts = (global uint32_t *)(blob + count_offs); |
| 57 |
|
| 58 |
if (vp) { |
| 59 |
vp->verts_per_instance = count; |
| 60 |
} |
| 61 |
|
| 62 |
/* If indexing is enabled, the third word is the offset into the index buffer |
| 63 |
* in elements. Apply that offset now that we have it. For a hardware |
| 64 |
* indirect draw, the hardware would do this for us, but for software input |
| 65 |
* assembly we need to do it ourselves. |
| 66 |
* |
| 67 |
* XXX: Deduplicate? |
| 68 |
*/ |
| 69 |
if (in_index_size_B) { |
| 70 |
vp->index_buffer = |
| 71 |
poly_index_buffer(in_index_buffer, in_index_buffer_range_el, |
| 72 |
indirect[2], in_index_size_B); |
| 73 |
|
| 74 |
vp->index_buffer_range_el = |
| 75 |
poly_index_buffer_range_el(in_index_buffer_range_el, indirect[2]); |
| 76 |
} |
| 77 |
|
| 78 |
/* VS grid size */ |
| 79 |
grids[0] = count; |
| 80 |
grids[1] = instance_count; |
| 81 |
grids[2] = 1; |
| 82 |
|
| 83 |
/* VS workgroup size */ |
| 84 |
grids[3] = 64; |
| 85 |
grids[4] = 1; |
| 86 |
grids[5] = 1; |
| 87 |
|
| 88 |
/* TCS grid size */ |
| 89 |
grids[6] = in_patches * p->output_patch_size; |
| 90 |
grids[7] = instance_count; |
| 91 |
grids[8] = 1; |
| 92 |
|
| 93 |
/* TCS workgroup size */ |
| 94 |
grids[9] = p->output_patch_size; |
| 95 |
grids[10] = 1; |
| 96 |
grids[11] = 1; |
| 97 |
|
| 98 |
/* Tess grid size */ |
| 99 |
grids[12] = unrolled_patches; |
| 100 |
grids[13] = 1; |
| 101 |
grids[14] = 1; |
| 102 |
|
| 103 |
/* Tess workgroup size */ |
| 104 |
grids[15] = 64; |
| 105 |
grids[16] = 1; |
| 106 |
grids[17] = 1; |
| 107 |
} |