Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
894 changes: 808 additions & 86 deletions src/gallium/drivers/asahi/agx_batch.c

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/gallium/drivers/asahi/agx_fence.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ agx_fence_snapshot_syncobj(struct agx_context *ctx, uint32_t *out_syncobj)
if (ctx->any_faults)
return false;

/* Ensure ctx->syncobj reflects any queued work before snapshotting it. */
agx_submit_pending(ctx);
if (ctx->any_faults)
return false;

/* Common path with no deferred server-wait fence: snapshot ctx->syncobj
* directly into a fresh syncobj via DRM_IOCTL_SYNCOBJ_TRANSFER, avoiding
* sync_file allocation and fd install/close on every glFenceSync().
Expand Down Expand Up @@ -347,6 +352,10 @@ agx_fence_server_sync(struct pipe_context *pctx, struct pipe_fence_handle *f,
if (f->faulted || !f->syncobj)
return;

/* Batches queued before this server wait must not depend on the incoming
* acquire fence (which may even depend on those earlier batches). */
agx_submit_pending(ctx);

ret = drmSyncobjExportSyncFile(dev->fd, f->syncobj, &fd);
if (ret || fd < 0) {
agx_msg("server-wait fence export failed\n");
Expand Down
33 changes: 18 additions & 15 deletions src/gallium/drivers/asahi/agx_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ agx_resource_get_handle(struct pipe_screen *pscreen, struct pipe_context *ctx,
handle->format = rsrc->layout.format;
handle->modifier = rsrc->modifier;

/* The frontend passes its context, which may be a u_threaded_context. */
if (ctx)
agx_submit_pending(agx_context(threaded_context_unwrap_sync(ctx)));

return true;
}

Expand Down Expand Up @@ -1701,17 +1705,10 @@ agx_flush_resource(struct pipe_context *pctx, struct pipe_resource *pres)
agx_flush_writer(agx_context(pctx), rsrc, "flush_resource");
}
rsrc->base.is_shared = true;
}

static unsigned
build_timestamp_offset(struct agx_batch *batch, unsigned offset)
{
return (agx_batch_idx(batch) * sizeof(struct agx_timestamps)) + offset;
agx_submit_pending(agx_context(pctx));
}

#define timestamp_offset(batch, offs) \
build_timestamp_offset(batch, offsetof(struct agx_timestamps, offs))

/* Native G18 ZLS arrays use either 16-KiB pages (bit 0) or 128-byte
* cachelines. The upper field stores units minus one. In particular, a
* 128-byte layer is encoded as zero; it is not a missing array stride.
Expand Down Expand Up @@ -1893,6 +1890,11 @@ agx_cmdbuf(struct agx_device *dev, struct drm_asahi_cmd_render *c,
if ((dev->debug & AGX_DBG_NOCLUSTER) && !agx_chip_native_hal200(dev->chip))
c->flags |= DRM_ASAHI_RENDER_NO_VERTEX_CLUSTERING;

if (agx_chip_native_or_m5(dev->chip) &&
(dev->params.features & DRM_ASAHI_FEATURE_FEW_PRIMITIVES) &&
!batch->prims_unknown && batch->known_prims <= 4)
c->flags |= DRM_ASAHI_RENDER_FEW_PRIMITIVES;

c->bg.rsrc_spec = (uint32_t)pipeline_clear.rsrc_spec;
c->eot.rsrc_spec = (uint32_t)pipeline_store.rsrc_spec;
c->partial_bg.rsrc_spec = (uint32_t)pipeline_load.rsrc_spec;
Expand Down Expand Up @@ -1985,8 +1987,8 @@ agx_cmdbuf(struct agx_device *dev, struct drm_asahi_cmd_render *c,
batch->ctx->timestamps)) {
c->ts_vtx.start.handle = batch->ctx->timestamp_handle;
c->ts_frag.end.handle = batch->ctx->timestamp_handle;
c->ts_vtx.start.offset = timestamp_offset(batch, vtx_start);
c->ts_frag.end.offset = timestamp_offset(batch, frag_end);
c->ts_vtx.start.offset = agx_timestamp_offset(batch, vtx_start);
c->ts_frag.end.offset = agx_timestamp_offset(batch, frag_end);
}
}

Expand Down Expand Up @@ -2140,9 +2142,9 @@ agx_flush_compute(struct agx_context *ctx, struct agx_batch *batch,
if (batch->timestamps.size > 0 || stats) {
cmdbuf->ts.start.handle = ctx->timestamp_handle;
cmdbuf->ts.end.handle = ctx->timestamp_handle;
cmdbuf->ts.start.offset = timestamp_offset(batch, comp_start);
cmdbuf->ts.end.offset = stats ? timestamp_offset(batch, comp_end)
: timestamp_offset(batch, comp_start);
cmdbuf->ts.start.offset = agx_timestamp_offset(batch, comp_start);
cmdbuf->ts.end.offset = stats ? agx_timestamp_offset(batch, comp_end)
: agx_timestamp_offset(batch, comp_start);
}
}

Expand Down Expand Up @@ -2254,8 +2256,8 @@ agx_flush_batch(struct agx_context *ctx, struct agx_batch *batch)
return;
}

agx_batch_submit(ctx, batch, has_cdm ? &compute : NULL,
has_vdm ? &render : NULL);
agx_batch_queue_or_submit(ctx, batch, has_cdm ? &compute : NULL,
has_vdm ? &render : NULL);
}

static void
Expand Down Expand Up @@ -2431,6 +2433,7 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags)

util_dynarray_init(&ctx->writer, ctx);
util_dynarray_init(&ctx->global_buffers, ctx);
util_dynarray_init(&ctx->pending.batches, ctx);

pctx->stream_uploader = u_upload_create_default(pctx);
if (!pctx->stream_uploader) {
Expand Down
10 changes: 8 additions & 2 deletions src/gallium/drivers/asahi/agx_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,29 @@ static bool
is_query_busy(struct agx_context *ctx, struct agx_query *query, bool submit)
{
bool busy = false;
bool need_submit = false;
for (unsigned i = 0; i < AGX_MAX_BATCHES; ++i) {
if (query->writer_generation[i] == ctx->batches.generation[i] &&
!agx_poll_batch(ctx, &ctx->batches.slots[i])) {
if (!submit)
return true;

/* GL availability polling must eventually finish without an app
* flush. Submit only still-active writers, once; never wait for
* them here. Already submitted writers need only the zero-timeout
* flush. Submit only still-active or queued writers, once; never wait
* for them here. Already submitted writers need only the zero-timeout
* poll above. Return not-ready even if submission retires inline. */
busy = true;
if (agx_batch_is_active(&ctx->batches.slots[i]))
agx_flush_batch_for_reason(ctx, &ctx->batches.slots[i],
"Query result availability");
if (agx_batch_is_queued(&ctx->batches.slots[i]))
need_submit = true;
}
}

if (need_submit)
agx_submit_pending(ctx);

return busy;
}

Expand Down
53 changes: 53 additions & 0 deletions src/gallium/drivers/asahi/agx_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -6350,6 +6350,8 @@ agx_upload_textures(struct agx_batch *batch, struct agx_compiled_shader *cs,

struct agx_resource *rsrc = tex->rsrc;
agx_batch_reads(batch, tex->rsrc);
if (stage != MESA_SHADER_FRAGMENT && stage != MESA_SHADER_COMPUTE)
agx_batch_note_vs_read(batch, rsrc);

/* Admit only resources covered by the native-backed stored path. */
if (dev->chip == AGX_CHIP_G18G && rsrc->base.b.nr_samples > 1 &&
Expand Down Expand Up @@ -6429,6 +6431,13 @@ agx_upload_textures(struct agx_batch *batch, struct agx_compiled_shader *cs,

struct pipe_image_view *view = &ctx->stage[stage].images[i];
agx_batch_track_image(batch, view);
if (stage != MESA_SHADER_FRAGMENT && stage != MESA_SHADER_COMPUTE) {
if ((view->access & PIPE_IMAGE_ACCESS_WRITE) ||
(cs->so->info.image_pbe_mask & BITFIELD64_BIT(i)))
batch->vs_needs_render_barrier = true;
else
agx_batch_note_vs_read(batch, agx_resource(view->resource));
}
if (agx_chip_native_or_g18g(dev->chip) &&
view->resource->target == PIPE_BUFFER)
batch->stage_uniforms[stage].image_buffer_size[i] =
Expand Down Expand Up @@ -14167,6 +14176,7 @@ agx_index_buffer_rsrc_ptr(struct agx_batch *batch,

struct agx_resource *rsrc = agx_resource(info->index.resource);
agx_batch_reads(batch, rsrc);
agx_batch_note_vs_read(batch, rsrc);

*extent =
ALIGN_POT(rsrc->layout.size_B - rsrc->layout.level_offsets_B[0], 4);
Expand Down Expand Up @@ -14517,6 +14527,7 @@ agx_indirect_buffer_ptr(struct agx_batch *batch,

struct agx_resource *rsrc = agx_resource(indirect->buffer);
agx_batch_reads(batch, rsrc);
agx_batch_note_vs_read(batch, rsrc);
return agx_map_gpu(rsrc) + indirect->offset;
}

Expand Down Expand Up @@ -15987,6 +15998,28 @@ agx_multidraw_can_skip(struct agx_context *ctx,
return !agx_needs_passthrough_gs(ctx, info, NULL, &xfb_passthrough, false);
}

static inline void
agx_batch_note_draw_prims(struct agx_batch *batch,
const struct pipe_draw_info *info,
const struct pipe_draw_start_count_bias *draw)
{
if (batch->prims_unknown)
return;

if (info->primitive_restart || info->instance_count > 4) {
batch->prims_unknown = true;
return;
}

uint64_t p = (uint64_t)u_decomposed_prims_for_vertices(info->mode,
draw->count) *
info->instance_count;
if (p > 4 || batch->known_prims + p > 4)
batch->known_prims = 5;
else
batch->known_prims += p;
}

static void
agx_multidraw_emit_body(struct agx_context *ctx, struct agx_batch *batch,
const struct pipe_draw_info *info,
Expand All @@ -15995,6 +16028,8 @@ agx_multidraw_emit_body(struct agx_context *ctx, struct agx_batch *batch,
{
struct agx_device *dev = agx_device(ctx->base.screen);

agx_batch_note_draw_prims(batch, info, d);

if (ctx->time_elapsed)
agx_batch_add_timestamp_query(batch, ctx->time_elapsed);

Expand Down Expand Up @@ -16403,6 +16438,22 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info,
struct agx_batch *batch = agx_get_batch(ctx);
batch->g17g_prims += prims;
batch->g17g_small_budget |= g17g_budget && g17g_small;

if (indirect || ctx->cond_query ||
ctx->stage[MESA_SHADER_GEOMETRY].shader ||
ctx->stage[MESA_SHADER_TESS_EVAL].shader || ctx->in_tess ||
ctx->streamout.num_targets ||
(ctx->stage[MESA_SHADER_VERTEX].shader &&
ctx->stage[MESA_SHADER_VERTEX].shader->info.writes_memory))
batch->vs_needs_render_barrier = true;

if (indirect || info->mode == MESA_PRIM_PATCHES ||
ctx->stage[MESA_SHADER_GEOMETRY].shader ||
ctx->stage[MESA_SHADER_TESS_EVAL].shader || ctx->in_tess ||
ctx->streamout.num_targets)
batch->prims_unknown = true;
else
agx_batch_note_draw_prims(batch, info, draws);
uint64_t ib = 0;
size_t ib_extent = 0;

Expand Down Expand Up @@ -16508,6 +16559,8 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info,
return;

if (ctx->gs) {
batch->vs_needs_render_barrier = true;
batch->prims_unknown = true;
batch->uniforms.geometry_params =
agx_batch_geometry_params(batch, ib, ib_extent, info, draws, indirect);

Expand Down
73 changes: 73 additions & 0 deletions src/gallium/drivers/asahi/agx_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ struct agx_batch {
struct agx_context *ctx;
struct pipe_framebuffer_state key;
uint64_t seqnum;
uint64_t submit_seq;
uint32_t syncobj;
/* Failed waits retain these fences and all batch resources until exit. */
uint32_t failed_syncobjs[2];
Expand Down Expand Up @@ -626,6 +627,12 @@ struct agx_batch {
* class, DRM_ASAHI_RENDER_SHADER_WRITES). */
bool m5_shader_writes;

/* Conservative vertex-stage dependency and primitive-count tracking for
* DRM_ASAHI_RENDER_FRAGMENT_BARRIERS and DRM_ASAHI_RENDER_FEW_PRIMITIVES. */
bool vs_needs_render_barrier;
bool prims_unknown;
uint32_t known_prims;

uint64_t uploaded_clear_color[PIPE_MAX_COLOR_BUFS];
union pipe_color_union clear_color[PIPE_MAX_COLOR_BUFS];
double clear_depth;
Expand Down Expand Up @@ -667,6 +674,8 @@ struct agx_batch {
struct {
struct set *set;
struct util_dynarray list; /* struct agx_bo * */
/* Subset of BOs written by this batch (subset of list; borrows its ref). */
struct util_dynarray written; /* struct agx_bo * */
/* Byte ranges of buffer BOs this batch reads or writes, for buffers
* accessed only through ranged bindings. A BO in the set without an
* entry here counts as accessed in full. */
Expand Down Expand Up @@ -905,6 +914,14 @@ struct asahi_blitter {
void *saved_cs;
};

struct agx_pending_batch {
struct agx_batch *batch;
struct drm_asahi_cmd_compute compute;
struct drm_asahi_cmd_render render;
bool has_compute;
bool has_render;
};

struct agx_oq_heap;

struct agx_context {
Expand Down Expand Up @@ -966,6 +983,9 @@ struct agx_context {
/** Set of active batches for faster traversal */
BITSET_DECLARE(active, AGX_MAX_BATCHES);

/** Set of flushed batches queued for a coalesced multi-command submit */
BITSET_DECLARE(queued, AGX_MAX_BATCHES);

/** Set of submitted batches for faster traversal */
BITSET_DECLARE(submitted, AGX_MAX_BATCHES);

Expand All @@ -988,6 +1008,12 @@ struct agx_context {
} query_state[AGX_MAX_BATCHES];
} batches;

/* Hazard-flushed batches awaiting a coalesced multi-command submit. */
struct {
struct util_dynarray batches; /* struct agx_pending_batch */
unsigned hw_cmds;
} pending;

/* Queue handle */
uint32_t queue_id, virt_ring_idx;
/* The queue returns from a submission with output syncs before it retires
Expand Down Expand Up @@ -1411,8 +1437,41 @@ struct agx_resource {
* times multiplied by the resource size.
*/
size_t shadowed_bytes;

/* Set once any render batch in this context writes this resource (as a
* render target or via fragment shader image/SSBO stores). Used to avoid
* overlapping a later batch's vertex stage ahead of a render it reads. */
bool render_written;
};

static inline void
agx_batch_note_vs_read(struct agx_batch *batch, struct agx_resource *rsrc)
{
if (!rsrc || batch->vs_needs_render_barrier)
return;

if (rsrc->render_written) {
batch->vs_needs_render_barrier = true;
return;
}

if (rsrc->bo) {
if (rsrc->bo->flags & AGX_BO_SHARED) {
batch->vs_needs_render_barrier = true;
return;
}

uint64_t writer = p_atomic_read_relaxed(&rsrc->bo->writer);
if (writer && agx_bo_writer_queue(writer) != batch->ctx->queue_id) {
batch->vs_needs_render_barrier = true;
return;
}
}

if (rsrc->separate_stencil)
agx_batch_note_vs_read(batch, rsrc->separate_stencil);
}

static inline struct agx_resource *
agx_resource(struct pipe_resource *pctx)
{
Expand Down Expand Up @@ -1599,7 +1658,17 @@ struct agx_bo_range {
uint64_t start, end;
};

static inline unsigned
agx_build_timestamp_offset(struct agx_batch *batch, unsigned offset)
{
return (agx_batch_idx(batch) * sizeof(struct agx_timestamps)) + offset;
}

#define agx_timestamp_offset(batch, offs) \
agx_build_timestamp_offset(batch, offsetof(struct agx_timestamps, offs))

bool agx_batch_is_active(struct agx_batch *batch);
bool agx_batch_is_queued(struct agx_batch *batch);
bool agx_batch_is_submitted(struct agx_batch *batch);

/* BO pointers remain stable while the batch holds its reference. */
Expand Down Expand Up @@ -1638,6 +1707,10 @@ struct drm_asahi_cmd_render;
void agx_batch_submit(struct agx_context *ctx, struct agx_batch *batch,
struct drm_asahi_cmd_compute *compute,
struct drm_asahi_cmd_render *render);
void agx_batch_queue_or_submit(struct agx_context *ctx, struct agx_batch *batch,
struct drm_asahi_cmd_compute *compute,
struct drm_asahi_cmd_render *render);
void agx_submit_pending(struct agx_context *ctx);

void agx_flush_batch(struct agx_context *ctx, struct agx_batch *batch);
void agx_flush_batch_for_reason(struct agx_context *ctx,
Expand Down
Loading