From 26de72b020771a788859f64e10e5a04dbeb95df3 Mon Sep 17 00:00:00 2001 From: cDurham <47431093+cDurham@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:51:14 -0700 Subject: [PATCH] pvr, pco: preload constant-offset UBO reads into shared registers pco turns every load_ubo into a per-instance memory load: the address from the descriptor's shared registers, two 64-bit adds, ld and wdf, behind the null-descriptor tests when nullDescriptor is on. A fragment shader pays that for every uniform scalar on every fragment. Zink keeps all of a GL program's uniforms in a UBO, so every GL client pays it. On the Apple A7 (iPad mini 2, 1536x2048 offscreen target through Zink, GPU at 450 MHz, fragment time the firmware reports for one full-screen draw) a constant colour takes 5 ms and the same colour read from a vec4 uniform 57 ms, about 13 ms per scalar read. Read such uniforms once per draw instead, the way push constants already are. A new pass, pco_nir_preload_ubos, runs before the robust-access and null-descriptor lowering. It rewrites 32-bit load_ubo with a constant offset in the block's first 64 dwords, on a single-element uniform buffer binding of a vertex or fragment shader, to load_ubo_preload_pco, and records per binding the range of dwords read (up to four bindings a stage). The pipeline gives each range shared registers and a PVR_BUFFER_TYPE_UBO entry in the PDS descriptor program; that type and pvr_const_map_entry_constant_buffer existed but had no user. pvr_setup_descriptor_mappings() writes the DMA's source for each draw from the bound buffer, dynamic or not, through the new pvr_descriptor_set_buffer(). The load is translated by trans_load_common_store(), as a push constant is. The offset of a load_ubo is still the descriptor's third component plus constants at that point, so the pass folds constants first and follows the adds. A binding with no buffer, or a null one, reads the device's zero page. So does a buffer whose bound range ends before the preloaded range when reading on would cross a page of the general heap: members inside the bound range then read as zero as well. Zink binds the whole block, so it never gets there. Same target and measurement, before -> after: - vec4 uniform colour: 57 ms -> 6.5 ms - vec4 uniform * varying: 57 ms -> 6.5 ms - texture * vec4 uniform: 59 ms -> 17 ms - texture * float uniform: 23 ms -> 17 ms - texture alone: 17 ms -> 17 ms 65 GLES2 draws read back with glReadPixels (uniform colours and floats, a uniform array read from its last element only and from two elements with a gap, textures, eight draws in one frame each with another colour, blending) give the same pixels as the build without this. No GPU fault. The Omarchy A desktop (Labwc on wlroots' GLES2 renderer, Quickshell) runs on it. Not run: dynamic uniform buffers, a Vulkan client other than Zink, the dEQP lists, and Rogue hardware other than the A7. The A8, A9 and A10 have their own backends and do not take this path. Generated-by: LLM Co-Authored-By: Claude Fable 5.2 Signed-off-by: cDurham <47431093+cDurham@users.noreply.github.com> --- src/compiler/nir/nir_intrinsics.py | 2 + src/imagination/pco/pco_data.h | 15 +++ src/imagination/pco/pco_internal.h | 14 +++ src/imagination/pco/pco_nir.c | 3 + src/imagination/pco/pco_nir_io.c | 114 ++++++++++++++++++ src/imagination/pco/pco_trans_nir.c | 28 +++++ src/imagination/vulkan/pds/pvr_pipeline_pds.c | 14 +++ src/imagination/vulkan/pvr_arch_cmd_buffer.c | 35 ++++++ src/imagination/vulkan/pvr_arch_pipeline.c | 29 +++++ src/imagination/vulkan/pvr_descriptor_set.h | 24 ++++ 10 files changed, 278 insertions(+) diff --git a/src/compiler/nir/nir_intrinsics.py b/src/compiler/nir/nir_intrinsics.py index 002a3d85cc2..6d9b378f472 100644 --- a/src/compiler/nir/nir_intrinsics.py +++ b/src/compiler/nir/nir_intrinsics.py @@ -3219,6 +3219,8 @@ def store(name, srcs, indices=[], flags=[]): intrinsic("load_tex_meta_pco", src_comp=[1], dest_comp=0, indices=[DESC_SET, BINDING, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER], bit_sizes=[32]) intrinsic("load_smp_meta_pco", src_comp=[1], dest_comp=0, indices=[DESC_SET, BINDING, COMPONENT], flags=[CAN_ELIMINATE, CAN_REORDER], bit_sizes=[32]) +intrinsic("load_ubo_preload_pco", dest_comp=0, indices=[DESC_SET, BINDING, BASE], flags=[CAN_ELIMINATE, CAN_REORDER], bit_sizes=[32]) + index("uint16_t", "smp_flags_pco") # smp_pco(data, tex_state, smp_state) diff --git a/src/imagination/pco/pco_data.h b/src/imagination/pco/pco_data.h index ab4129b6a9e..61cf2dbfc4e 100644 --- a/src/imagination/pco/pco_data.h +++ b/src/imagination/pco/pco_data.h @@ -159,6 +159,16 @@ enum pco_sampler_meta { PCO_SAMPLER_META_COUNT, }; +#define PCO_MAX_UBO_PRELOADS 4U +#define PCO_UBO_PRELOAD_MAX_DWORDS 64U + +typedef struct _pco_ubo_preload { + unsigned desc_set; + unsigned binding; + unsigned first; + pco_range range; +} pco_ubo_preload; + /** PCO descriptor binding data. */ typedef struct _pco_binding_data { pco_range range; /** Descriptor location range. */ @@ -169,6 +179,8 @@ typedef struct _pco_binding_data { /** Whether the descriptor binding is an inline uniform block. */ bool is_inline_ubo; + + bool can_preload_ubo; } pco_binding_data; /** PCO descriptor set data. */ @@ -200,6 +212,9 @@ typedef struct _pco_common_data { /** Push constant data. */ pco_push_const_data push_consts; + pco_ubo_preload ubo_preloads[PCO_MAX_UBO_PRELOADS]; + unsigned ubo_preload_count; + pco_range point_sampler; pco_range ia_sampler; diff --git a/src/imagination/pco/pco_internal.h b/src/imagination/pco/pco_internal.h index ff4b4c70e41..4226e67b01b 100644 --- a/src/imagination/pco/pco_internal.h +++ b/src/imagination/pco/pco_internal.h @@ -1873,6 +1873,7 @@ bool pco_nir_lower_vs_intrinsics(nir_shader *shader); bool pco_nir_lower_images(nir_shader *shader, pco_data *data, pco_ctx *ctx); bool pco_nir_lower_interpolation(nir_shader *shader, pco_fs_data *fs); bool pco_nir_lower_io(nir_shader *shader, pco_data *data); +bool pco_nir_preload_ubos(nir_shader *shader, pco_data *data); bool pco_nir_lower_sample_mask_out(nir_shader *shader); bool pco_nir_lower_shared_io_to_global(nir_shader *shader, unsigned usc_slots); bool pco_nir_lower_subgroups(nir_shader *shader); @@ -3356,6 +3357,19 @@ pco_unpack_desc(uint32_t packed, unsigned *desc_set, unsigned *binding) *binding = packed >> 16; } +static inline pco_ubo_preload * +pco_ubo_preload_find(pco_common_data *common, unsigned desc_set, unsigned binding) +{ + for (unsigned u = 0; u < common->ubo_preload_count; ++u) { + pco_ubo_preload *preload = &common->ubo_preloads[u]; + + if (preload->desc_set == desc_set && preload->binding == binding) + return preload; + } + + return NULL; +} + /** * \brief Returns a reference to the execution mask counter, * allocating and initialising one if it doesn't exist. diff --git a/src/imagination/pco/pco_nir.c b/src/imagination/pco/pco_nir.c index 6115d6b16fd..c12b171fc70 100644 --- a/src/imagination/pco/pco_nir.c +++ b/src/imagination/pco/pco_nir.c @@ -991,6 +991,9 @@ void pco_lower_nir(pco_ctx *ctx, nir_shader *nir, pco_data *data) NULL, NULL); + NIR_PASS(_, nir, nir_opt_constant_folding); + NIR_PASS(_, nir, pco_nir_preload_ubos, data); + if (data->common.robust_buffer_access) { NIR_PASS(_, nir, diff --git a/src/imagination/pco/pco_nir_io.c b/src/imagination/pco/pco_nir_io.c index 670032749f0..cc8af9a44bc 100644 --- a/src/imagination/pco/pco_nir_io.c +++ b/src/imagination/pco/pco_nir_io.c @@ -102,6 +102,120 @@ bool pco_nir_lower_io(nir_shader *shader, pco_data *data) return progress; } +static bool ubo_preload_offset(nir_scalar scalar, unsigned *offset) +{ + *offset = 0; + + while (true) { + scalar = nir_scalar_chase_movs(scalar); + + if (nir_scalar_is_const(scalar)) { + *offset += nir_scalar_as_uint(scalar); + return true; + } + + if (nir_scalar_is_intrinsic(scalar)) { + return nir_scalar_intrinsic_op(scalar) == + nir_intrinsic_load_vulkan_descriptor && + scalar.comp == 2; + } + + if (!nir_scalar_is_alu(scalar) || nir_scalar_alu_op(scalar) != nir_op_iadd) + return false; + + nir_scalar lhs = nir_scalar_chase_alu_src(scalar, 0); + nir_scalar rhs = nir_scalar_chase_alu_src(scalar, 1); + + if (nir_scalar_is_const(rhs)) { + *offset += nir_scalar_as_uint(rhs); + scalar = lhs; + } else if (nir_scalar_is_const(lhs)) { + *offset += nir_scalar_as_uint(lhs); + scalar = rhs; + } else { + return false; + } + } +} + +static pco_ubo_preload * +ubo_preload_get(pco_common_data *common, unsigned desc_set, unsigned binding) +{ + pco_ubo_preload *preload = pco_ubo_preload_find(common, desc_set, binding); + + if (preload || common->ubo_preload_count == PCO_MAX_UBO_PRELOADS) + return preload; + + preload = &common->ubo_preloads[common->ubo_preload_count++]; + *preload = (pco_ubo_preload){ + .desc_set = desc_set, + .binding = binding, + .first = PCO_UBO_PRELOAD_MAX_DWORDS, + }; + + return preload; +} + +static bool preload_ubo(nir_builder *b, nir_intrinsic_instr *intr, void *cb_data) +{ + pco_common_data *common = cb_data; + + if (intr->intrinsic != nir_intrinsic_load_ubo || intr->def.bit_size != 32) + return false; + + nir_binding res = nir_chase_binding(intr->src[0]); + if (!res.success || res.desc_set >= ARRAY_SIZE(common->desc_sets)) + return false; + + const pco_descriptor_set_data *desc_set_data = + &common->desc_sets[res.desc_set]; + if (!desc_set_data->bindings || res.binding >= desc_set_data->binding_count || + !desc_set_data->bindings[res.binding].can_preload_ubo) + return false; + + unsigned offset; + if (!ubo_preload_offset(nir_get_scalar(intr->src[1].ssa, 0), &offset) || + offset % sizeof(uint32_t)) + return false; + + unsigned first = offset / sizeof(uint32_t); + unsigned end = first + intr->def.num_components; + if (end > PCO_UBO_PRELOAD_MAX_DWORDS) + return false; + + pco_ubo_preload *preload = + ubo_preload_get(common, res.desc_set, res.binding); + if (!preload) + return false; + + preload->first = MIN2(preload->first, first); + preload->range.count = MAX2(preload->range.count, end); + + b->cursor = nir_before_instr(&intr->instr); + nir_def_replace(&intr->def, + nir_load_ubo_preload_pco(b, + intr->def.num_components, + .desc_set = res.desc_set, + .binding = res.binding, + .base = first)); + + return true; +} + +bool pco_nir_preload_ubos(nir_shader *shader, pco_data *data) +{ + pco_common_data *common = &data->common; + bool progress = nir_shader_intrinsics_pass(shader, + preload_ubo, + nir_metadata_control_flow, + common); + + for (unsigned u = 0; u < common->ubo_preload_count; ++u) + common->ubo_preloads[u].range.count -= common->ubo_preloads[u].first; + + return progress; +} + static bool lower_shared_io_to_global(nir_builder *b, nir_intrinsic_instr *intr, void *cb_data) diff --git a/src/imagination/pco/pco_trans_nir.c b/src/imagination/pco/pco_trans_nir.c index 5420b4539b3..7bdcce76258 100644 --- a/src/imagination/pco/pco_trans_nir.c +++ b/src/imagination/pco/pco_trans_nir.c @@ -1261,6 +1261,30 @@ static pco_instr *trans_atomic_shared(trans_ctx *tctx, return instr; } +static pco_instr *trans_load_ubo_preload(trans_ctx *tctx, + nir_intrinsic_instr *intr, + pco_ref dest) +{ + const pco_ubo_preload *preload = + pco_ubo_preload_find(&tctx->shader->data.common, + nir_intrinsic_desc_set(intr), + nir_intrinsic_binding(intr)); + unsigned offset = nir_intrinsic_base(intr) - preload->first; + + assert(offset < preload->range.count); + + return trans_load_common_store(tctx, + intr, + dest, + pco_ref_null(), + 0u, + false, + &(pco_range){ + .start = preload->range.start + offset, + .count = preload->range.count - offset, + }); +} + static pco_instr *trans_load_buffer(trans_ctx *tctx, nir_intrinsic_instr *intr, pco_ref dest, @@ -2731,6 +2755,10 @@ static pco_instr *trans_intr(trans_ctx *tctx, nir_intrinsic_instr *intr) instr = trans_atomic_shared(tctx, intr, dest, src[0], src[1], src[2]); break; + case nir_intrinsic_load_ubo_preload_pco: + instr = trans_load_ubo_preload(tctx, intr, dest); + break; + case nir_intrinsic_load_ubo: case nir_intrinsic_load_ssbo: instr = trans_load_buffer(tctx, intr, dest, src[0], src[1]); diff --git a/src/imagination/vulkan/pds/pvr_pipeline_pds.c b/src/imagination/vulkan/pds/pvr_pipeline_pds.c index 0b58cbab52d..662f8c334c7 100644 --- a/src/imagination/vulkan/pds/pvr_pipeline_pds.c +++ b/src/imagination/vulkan/pds/pvr_pipeline_pds.c @@ -1638,6 +1638,20 @@ void pvr_pds_generate_descriptor_upload_program( } break; } + case PVR_BUFFER_TYPE_UBO: { + struct pvr_const_map_entry_constant_buffer *constant_buffer_entry; + + constant_buffer_entry = + pvr_prepare_next_pds_const_map_entry(&entry_write_state, + sizeof(*constant_buffer_entry)); + constant_buffer_entry->type = + PVR_PDS_CONST_MAP_ENTRY_TYPE_CONSTANT_BUFFER; + constant_buffer_entry->desc_set = buffer->desc_set; + constant_buffer_entry->binding = buffer->binding; + constant_buffer_entry->offset = buffer->source_offset; + constant_buffer_entry->size_in_dwords = buffer->size_in_dwords; + break; + } case PVR_BUFFER_TYPE_COMPILE_TIME: { struct pvr_const_map_entry_special_buffer *special_buffer_entry; diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index bc6efa3cdff..fe7369f677e 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -6411,6 +6411,41 @@ static VkResult pvr_setup_descriptor_mappings( break; } + case PVR_PDS_CONST_MAP_ENTRY_TYPE_CONSTANT_BUFFER: { + const struct pvr_const_map_entry_constant_buffer *const buffer_entry = + (struct pvr_const_map_entry_constant_buffer *)entries; + const struct pvr_descriptor_set *const descriptor_set = + desc_state->sets[buffer_entry->desc_set]; + const uint64_t end = (uint64_t)buffer_entry->offset + + buffer_entry->size_in_dwords * sizeof(uint32_t); + const uint32_t page_shift = + device->heaps.general_heap->log2_page_size; + uint64_t addr; + uint32_t range; + + if (descriptor_set && + pvr_descriptor_set_buffer( + descriptor_set, + &descriptor_set->layout->bindings[buffer_entry->binding], + &addr, + &range) && + range && + (range >= end || (addr + end - 1) >> page_shift == + (addr + range - 1) >> page_shift)) { + addr += buffer_entry->offset; + } else { + addr = device->null_state.zero_bo->vma->dev_addr.addr; + } + + PVR_WRITE(qword_buffer, + addr, + buffer_entry->const_offset, + pds_info->data_size_in_dwords); + + entries += sizeof(*buffer_entry); + break; + } + case PVR_PDS_CONST_MAP_ENTRY_TYPE_SPECIAL_BUFFER: { const struct pvr_const_map_entry_special_buffer *special_buff_entry = (struct pvr_const_map_entry_special_buffer *)entries; diff --git a/src/imagination/vulkan/pvr_arch_pipeline.c b/src/imagination/vulkan/pvr_arch_pipeline.c index 69992ff1301..db8a457dbdf 100644 --- a/src/imagination/vulkan/pvr_arch_pipeline.c +++ b/src/imagination/vulkan/pvr_arch_pipeline.c @@ -543,6 +543,19 @@ static VkResult pvr_pds_descriptor_program_create_and_upload( goto err_free_static_consts; } + for (unsigned u = 0; u < data->common.ubo_preload_count; ++u) { + const pco_ubo_preload *preload = &data->common.ubo_preloads[u]; + + program.buffers[program.buffer_count++] = (struct pvr_pds_buffer){ + .type = PVR_BUFFER_TYPE_UBO, + .size_in_dwords = preload->range.count, + .destination = preload->range.start, + .desc_set = preload->desc_set, + .binding = preload->binding, + .source_offset = preload->first * sizeof(uint32_t), + }; + } + if (data->common.push_consts.range.count > 0) { program.buffers[program.buffer_count++] = (struct pvr_pds_buffer){ .type = PVR_BUFFER_TYPE_PUSH_CONSTS, @@ -2587,6 +2600,14 @@ static void pvr_init_descriptors(pco_data *data, binding_data->is_inline_ubo = layout_binding->type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK; + + binding_data->can_preload_ubo = + (nir->info.stage == MESA_SHADER_VERTEX || + nir->info.stage == MESA_SHADER_FRAGMENT) && + (layout_binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || + layout_binding->type == + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && + layout_binding->descriptor_count == 1; } } } @@ -2720,6 +2741,14 @@ static void pvr_setup_descriptors(pco_data *data, data->common.shareds += count; } + for (unsigned u = 0; u < data->common.ubo_preload_count; ++u) { + pco_range *range = &data->common.ubo_preloads[u].range; + + data->common.shareds = ALIGN_POT(data->common.shareds, 4); + range->start = data->common.shareds; + data->common.shareds += range->count; + } + if (data->common.uses.point_sampler) { data->common.shareds = ALIGN_POT(data->common.shareds, 4); data->common.point_sampler = (pco_range){ diff --git a/src/imagination/vulkan/pvr_descriptor_set.h b/src/imagination/vulkan/pvr_descriptor_set.h index 4bb2cf56ef7..7335e16f093 100644 --- a/src/imagination/vulkan/pvr_descriptor_set.h +++ b/src/imagination/vulkan/pvr_descriptor_set.h @@ -122,6 +122,30 @@ pvr_descriptor_set_dynamic_buffer( return true; } +static inline bool +pvr_descriptor_set_buffer( + const struct pvr_descriptor_set *set, + const struct pvr_descriptor_set_layout_binding *binding, + uint64_t *addr, + uint32_t *range) +{ + struct pvr_buffer_descriptor d; + + if (pvr_descriptor_set_binding_is_dynamic(binding)) + return pvr_descriptor_set_dynamic_buffer(set, binding, 0, addr, range); + + if (!set->mapping) + return false; + + memcpy(&d, (const uint8_t *)set->mapping + binding->offset, sizeof(d)); + if (!d.addr) + return false; + + *addr = d.addr + d.offset; + *range = d.size; + return true; +} + /* For a dispatch that DMAs a buffer slot's two dwords from descriptor * memory (the A9 and A10 compute bind tasks): when the slot's binding is * dynamic, the dwords it would read (the address, or the size word and the