From fc7b89aafaad8281f09b018354a9ad24d126d7fb Mon Sep 17 00:00:00 2001 From: Subham Nayak Date: Tue, 3 Nov 2026 15:24:09 +0530 Subject: [PATCH] freedreno/vulkan: enable Vulkan 1.4 on a6xx gen4 Vulkan 1.4 requires storageBuffer8BitAccess and at least seven bound descriptor sets. a6xx gen4 already has 8-bit storage, but a6xx exposes only five hardware bindless descriptor-set base registers per stage, so seven usable sets require descriptor-set virtualization. Enable storage_8bit on a6xx_gen4 and raise max_sets to 8 (seven usable sets plus one reserved for dynamic descriptors). Gate apiVersion 1.4 on storage_8bit and usable_sets >= 7, leaving older a6xx parts and reduced set counts on 1.3. Sets at and above hw_max_sets - 1 share the last hardware base register. Program that register with the lowest non-zero set IOVA and pass shaders a per-set descriptor-index offset through the new IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS allocation. The offset is added to the descriptor index before the bindless access and the set index is clamped to the shared register. Skip pipeline descriptor preload packets for virtualized sets and dynamic descriptors, whose offsets are only known at bind time. Enable SSBO emulation on a6xx too; it only needs global loads/stores and the driver UBO holding per-set descriptor base addresses. Use RESINFO instead of the a7xx-only RESBASE for a6xx SSBO size lowering, and rewrite SSBO address users in place so removing the original load does not invalidate NIR's safe iterator. X11 WSI: KGSL has no implicit sync and msm VM_BIND submissions carry no BO list, so the kernel cannot attach fences to the presented dma-bufs. Wait for the present fence before handing an image to X11 unless explicit sync or a dma-buf syncobj is available. EGL: when zink will use kopper, the DRI3 device is not needed because presentation goes through Vulkan WSI, which imports dma-bufs directly. Do not warn about DRI3Open failures in that case; some X servers, such as Termux:X11, implement only the dma-buf import half of DRI3. Tested on Adreno 644 (a6xx gen4): - vulkaninfo reports 1.4.363, maxBoundDescriptorSets = 7 and storageBuffer8BitAccess = true. - FD_DEV_FEATURES=max_sets=5 and storage_8bit=false still report 1.3. - vk7tex, vk7ssbo, vk7gfx, vk7texdyn, vk7gfxdyn, vk1dyn, vk1gfxdyn, vk8bit_set6 and vkbigssbo pass, including SSBO emulation. - glmark2-es2 terrain runs at ~180-190 FPS without black frames; vkcube and vkgears are clean. - Zink EGL no longer logs the DRI3 warning; native kgsl EGL still reports "Using DRI3". Signed-off-by: Subham Nayak --- src/egl/drivers/dri2/platform_x11.c | 12 +- src/freedreno/common/freedreno_devices.py | 6 + src/freedreno/ir3/ir3_shader.c | 2 + src/freedreno/ir3/ir3_shader.h | 6 +- src/freedreno/vulkan/tu_cmd_buffer.cc | 179 ++++++++++++++++---- src/freedreno/vulkan/tu_common.h | 20 +++ src/freedreno/vulkan/tu_device.cc | 26 ++- src/freedreno/vulkan/tu_pipeline.cc | 39 ++++- src/freedreno/vulkan/tu_shader.cc | 190 ++++++++++++++++------ src/freedreno/vulkan/tu_shader.h | 4 + src/freedreno/vulkan/tu_wsi.cc | 9 + src/vulkan/wsi/wsi_common.h | 6 + src/vulkan/wsi/wsi_common_x11.c | 7 +- 13 files changed, 410 insertions(+), 96 deletions(-) diff --git a/src/egl/drivers/dri2/platform_x11.c b/src/egl/drivers/dri2/platform_x11.c index 2dd79a75e2b7..abde03a95051 100644 --- a/src/egl/drivers/dri2/platform_x11.c +++ b/src/egl/drivers/dri2/platform_x11.c @@ -1243,7 +1243,17 @@ dri2_initialize_x11(_EGLDisplay *disp) * We're going to enable Zink anyway. */ if (!force_zink) { - bool status = dri3_x11_connect(dri2_dpy, disp->Options.ForceSoftware); + /* Zink+kopper does not use the DRI3 device: kopper presents through + * Vulkan WSI, which imports dma-bufs without DRI3Open. Some X servers + * (for example Termux:X11) only implement the dma-buf import half of + * DRI3, so don't warn about the missing render device when kopper will + * be used anyway. + */ + const bool zink_kopper = + disp->Options.Zink && !disp->Options.ForceSoftware && + !debug_get_bool_option("LIBGL_KOPPER_DISABLE", false); + bool status = dri3_x11_connect(dri2_dpy, + disp->Options.ForceSoftware || zink_kopper); /* the status here is ignored for zink-with-kopper and swrast, * otherwise return whatever error/fallback status as failure */ diff --git a/src/freedreno/common/freedreno_devices.py b/src/freedreno/common/freedreno_devices.py index 9385b156b6d1..2096d9ee3749 100644 --- a/src/freedreno/common/freedreno_devices.py +++ b/src/freedreno/common/freedreno_devices.py @@ -246,6 +246,12 @@ has_z24uint_s8uint = True, tess_use_shared = True, storage_16bit = True, + # Requires storage_16bit + has_isam_v (asserted in tu_descriptor_set.cc). + storage_8bit = True, + # 7 usable sets + 1 reserved for dynamic descriptors. a6xx only has + # five hardware bindless base registers, so sets 4-7 are virtualized + # on top of the last register. + max_sets = 8, has_tex_filter_cubic = True, has_separate_chroma_filter = True, has_sample_locations = True, diff --git a/src/freedreno/ir3/ir3_shader.c b/src/freedreno/ir3/ir3_shader.c index a3176564c787..83303d1759e0 100644 --- a/src/freedreno/ir3/ir3_shader.c +++ b/src/freedreno/ir3/ir3_shader.c @@ -1080,6 +1080,8 @@ ir3_const_alloc_type_to_string(enum ir3_const_alloc_type type) return "inline_uniform_addresses"; case IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS: return "bindless_base_addresses"; + case IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS: + return "virtual_set_offsets"; case IR3_CONST_ALLOC_DRIVER_PARAMS: return "driver_params"; case IR3_CONST_ALLOC_UBO_RANGES: diff --git a/src/freedreno/ir3/ir3_shader.h b/src/freedreno/ir3/ir3_shader.h index 87571bfbd939..18572d7c3f1f 100644 --- a/src/freedreno/ir3/ir3_shader.h +++ b/src/freedreno/ir3/ir3_shader.h @@ -234,7 +234,11 @@ enum ir3_const_alloc_type { IR3_CONST_ALLOC_PRIMITIVE_MAP = 11, /* For SSBO emulation */ IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS = 12, - IR3_CONST_ALLOC_MAX = 13, + /* For descriptor-set virtualization: one u32 per set, holding the offset + * in descriptor units from the shared hardware bindless base. + */ + IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS = 13, + IR3_CONST_ALLOC_MAX = 14, }; struct ir3_const_allocation { diff --git a/src/freedreno/vulkan/tu_cmd_buffer.cc b/src/freedreno/vulkan/tu_cmd_buffer.cc index d0ef5aa5ca3f..74f71de7f3b7 100644 --- a/src/freedreno/vulkan/tu_cmd_buffer.cc +++ b/src/freedreno/vulkan/tu_cmd_buffer.cc @@ -4772,6 +4772,26 @@ tu_CmdBindIndexBuffer2KHR(VkCommandBuffer commandBuffer, } TU_GENX(tu_CmdBindIndexBuffer2KHR); +/* Returns the shared hardware bindless base address used by virtualized + * descriptor sets. Sets at and above hw_max_sets - 1 share the last hardware + * base register, and are distinguished by a per-set descriptor offset. + */ +static uint64_t +tu_bindless_anchor(unsigned hw_max_sets, const uint64_t *set_iova) +{ + uint64_t anchor = 0; + + for (unsigned i = hw_max_sets - 1; i < MAX_SETS; i++) { + uint64_t va = set_iova[i] & ~0x3f; + if (!va) + continue; + if (!anchor || va < anchor) + anchor = va; + } + + return anchor; +} + template static void tu6_emit_descriptor_sets(struct tu_cmd_buffer *cmd, @@ -4782,24 +4802,56 @@ tu6_emit_descriptor_sets(struct tu_cmd_buffer *cmd, uint32_t sp_bindless_base_reg, hlsq_bindless_base_reg; struct tu_cs *cs, state_cs; + /* a6xx only has 5 bindless descriptor set base registers per stage, and + * a7xx has 8. When descriptor-set virtualization lets more sets be bound + * than that, sets at and above hw_max_sets - 1 share the last base + * register: program it with the lowest set address and let shaders add a + * per-set descriptor offset. On a6xx the next register after the array + * is SP_CS_UAV_BASE. + */ + const unsigned hw_max_sets = tu_hw_max_sets(CHIP); + const bool virtual_sets = + tu_uses_virtual_sets(CHIP, cmd->device->physical_device->usable_sets); + const unsigned max_sets_bound = + virtual_sets ? hw_max_sets : + MIN2(descriptors_state->max_sets_bound, hw_max_sets); + uint64_t set_iova[MAX_SETS]; + + /* Dynamic descriptors normally get the reserved set's base register. When + * that set is virtualized it shares the anchor register instead, so there + * is no separate dynamic-register packet to emit. + */ + const int reserved_set_idx = + cmd->device->physical_device->reserved_set_idx; + const bool has_dynamic_regs = + descriptors_state->max_dynamic_offset_size && + reserved_set_idx >= 0 && reserved_set_idx < static_cast(hw_max_sets); + + memcpy(set_iova, descriptors_state->set_iova, sizeof(set_iova)); + if (virtual_sets) { + set_iova[hw_max_sets - 1] = + tu_bindless_anchor(hw_max_sets, descriptors_state->set_iova) | + BINDLESS_DESCRIPTOR_64B; + } + if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { sp_bindless_base_reg = __SP_GFX_BINDLESS_BASE_DESCRIPTOR(0, {}).reg; hlsq_bindless_base_reg = REG_A6XX_HLSQ_BINDLESS_BASE(0); - unsigned bindless_pkt_size = descriptors_state->max_sets_bound ? - 1 + 2 * descriptors_state->max_sets_bound : + unsigned bindless_pkt_size = max_sets_bound ? + 1 + 2 * max_sets_bound : 0; if (CHIP == A6XX) { cmd->state.desc_sets = tu_cs_draw_state(&cmd->sub_cs, &state_cs, 2 + 2 * bindless_pkt_size + - (descriptors_state->max_dynamic_offset_size ? 6 : 0)); + (has_dynamic_regs ? 6 : 0)); } else { cmd->state.desc_sets = tu_cs_draw_state(&cmd->sub_cs, &state_cs, 2 + bindless_pkt_size + - (descriptors_state->max_dynamic_offset_size ? 3 : 0)); + (has_dynamic_regs ? 3 : 0)); } cs = &state_cs; } else { @@ -4811,27 +4863,28 @@ tu6_emit_descriptor_sets(struct tu_cmd_buffer *cmd, cs = &cmd->cs; } - if (descriptors_state->max_sets_bound > 0) { - tu_cs_emit_pkt4(cs, sp_bindless_base_reg, 2 * descriptors_state->max_sets_bound); - tu_cs_emit_array(cs, reinterpret_cast(descriptors_state->set_iova), - 2 * descriptors_state->max_sets_bound); + if (max_sets_bound > 0) { + tu_cs_emit_pkt4(cs, sp_bindless_base_reg, 2 * max_sets_bound); + tu_cs_emit_array(cs, reinterpret_cast(set_iova), + 2 * max_sets_bound); if (CHIP == A6XX) { - tu_cs_emit_pkt4(cs, hlsq_bindless_base_reg, 2 * descriptors_state->max_sets_bound); - tu_cs_emit_array(cs, reinterpret_cast(descriptors_state->set_iova), - 2 * descriptors_state->max_sets_bound); + tu_cs_emit_pkt4(cs, hlsq_bindless_base_reg, 2 * max_sets_bound); + tu_cs_emit_array(cs, reinterpret_cast(set_iova), + 2 * max_sets_bound); } } /* Dynamic descriptors get the reserved descriptor set. */ if (descriptors_state->max_dynamic_offset_size) { - int reserved_set_idx = cmd->device->physical_device->reserved_set_idx; assert(reserved_set_idx >= 0); /* reserved set must be bound */ - tu_cs_emit_pkt4(cs, sp_bindless_base_reg + reserved_set_idx * 2, 2); - tu_cs_emit_qw(cs, descriptors_state->set_iova[reserved_set_idx]); - if (CHIP == A6XX) { - tu_cs_emit_pkt4(cs, hlsq_bindless_base_reg + reserved_set_idx * 2, 2); + if (has_dynamic_regs) { + tu_cs_emit_pkt4(cs, sp_bindless_base_reg + reserved_set_idx * 2, 2); tu_cs_emit_qw(cs, descriptors_state->set_iova[reserved_set_idx]); + if (CHIP == A6XX) { + tu_cs_emit_pkt4(cs, hlsq_bindless_base_reg + reserved_set_idx * 2, 2); + tu_cs_emit_qw(cs, descriptors_state->set_iova[reserved_set_idx]); + } } } @@ -7775,6 +7828,11 @@ tu6_user_consts_size(const struct tu_const_state *const_state, } } + if (const_state->num_virtual_set_offsets > 0 && + !load_shader_consts_via_preamble) { + dwords += 4 + align(const_state->num_virtual_set_offsets, 4); + } + return dwords; } @@ -7978,28 +8036,48 @@ tu6_const_size(struct tu_cmd_buffer *cmd, static void fill_bindless_base_addresses(const struct tu_const_state *const_state, struct tu_descriptor_state *descriptors, + unsigned hw_max_sets, uint64_t *addresses) { assert(const_state->num_bindless_base_addresses <= MAX_SETS); for (unsigned i = 0; i < const_state->num_bindless_base_addresses; i++) addresses[i] = descriptors->set_iova[i] & ~0x3f; + + if (const_state->num_virtual_set_offsets) + addresses[hw_max_sets - 1] = + tu_bindless_anchor(hw_max_sets, descriptors->set_iova); } +/* Upload the per-set descriptor offsets for virtualized sets, in 64-byte + * descriptor units relative to the shared base register. + */ static void -tu6_emit_bindless_base_addresses(struct tu_cs *cs, - const struct tu_const_state *const_state, - const struct ir3_const_state *ir_const_state, - mesa_shader_stage type, - struct tu_descriptor_state *descriptors) +tu6_emit_virtual_set_offsets(struct tu_cs *cs, + const struct tu_const_state *const_state, + const struct ir3_const_state *ir_const_state, + mesa_shader_stage type, + struct tu_descriptor_state *descriptors) { - uint64_t addresses[MAX_SETS] = {0}; - fill_bindless_base_addresses(const_state, descriptors, addresses); + if (!const_state->num_virtual_set_offsets) + return; + + const unsigned hw_max_sets = + tu_hw_max_sets(cs->device->compiler->info->chip); + uint64_t anchor = tu_bindless_anchor(hw_max_sets, descriptors->set_iova); + uint32_t offsets[MAX_SETS] = {0}; + + for (unsigned i = hw_max_sets - 1; + i < const_state->num_virtual_set_offsets; i++) { + uint64_t va = descriptors->set_iova[i] & ~0x3f; + if (va) + offsets[i] = (va - anchor) / 64; + } uint32_t offset = - ir_const_state->allocs.consts[IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS] + ir_const_state->allocs.consts[IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS] .offset_vec4; - unsigned num_dwords = align(const_state->num_bindless_base_addresses * 2, 4); + unsigned num_dwords = align(const_state->num_virtual_set_offsets, 4); tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3 + num_dwords); tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) | @@ -8010,12 +8088,48 @@ tu6_emit_bindless_base_addresses(struct tu_cs *cs, tu_cs_emit(cs, 0); tu_cs_emit(cs, 0); - for (unsigned i = 0; i < const_state->num_bindless_base_addresses; i++) - tu_cs_emit_qw(cs, addresses[i]); + for (unsigned i = 0; i < num_dwords; i++) + tu_cs_emit(cs, i < const_state->num_virtual_set_offsets ? offsets[i] : 0); +} + +static void +tu6_emit_bindless_base_addresses(struct tu_cs *cs, + const struct tu_const_state *const_state, + const struct ir3_const_state *ir_const_state, + mesa_shader_stage type, + struct tu_descriptor_state *descriptors) +{ + if (const_state->num_bindless_base_addresses > 0) { + uint64_t addresses[MAX_SETS] = {0}; + fill_bindless_base_addresses( + const_state, descriptors, + tu_hw_max_sets(cs->device->compiler->info->chip), addresses); + + uint32_t offset = + ir_const_state->allocs.consts[IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS] + .offset_vec4; + unsigned num_dwords = + align(const_state->num_bindless_base_addresses * 2, 4); - for (unsigned i = const_state->num_bindless_base_addresses * 2; - i < num_dwords; i++) + tu_cs_emit_pkt7(cs, tu6_stage2opcode(type), 3 + num_dwords); + tu_cs_emit(cs, CP_LOAD_STATE6_0_DST_OFF(offset) | + CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) | + CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) | + CP_LOAD_STATE6_0_STATE_BLOCK(tu6_stage2shadersb(type)) | + CP_LOAD_STATE6_0_NUM_UNIT(num_dwords / 4)); tu_cs_emit(cs, 0); + tu_cs_emit(cs, 0); + + for (unsigned i = 0; i < const_state->num_bindless_base_addresses; i++) + tu_cs_emit_qw(cs, addresses[i]); + + for (unsigned i = const_state->num_bindless_base_addresses * 2; + i < num_dwords; i++) + tu_cs_emit(cs, 0); + } + + tu6_emit_virtual_set_offsets(cs, const_state, ir_const_state, type, + descriptors); } static void @@ -8029,7 +8143,9 @@ tu7_emit_bindless_base_addresses(struct tu_cs *cs, if (ubo_offset < 0) return; - fill_bindless_base_addresses(const_state, descriptors, addresses); + fill_bindless_base_addresses( + const_state, descriptors, + tu_hw_max_sets(cs->device->compiler->info->chip), addresses); /* A7XX TODO: Emit data via sub_cs instead of NOP */ uint64_t iova = tu_cs_emit_data_nop( @@ -8055,7 +8171,8 @@ tu_emit_bindless_base_addresses(struct tu_cs *cs, mesa_shader_stage type, struct tu_descriptor_state *descriptors) { - if (!const_state->num_bindless_base_addresses) + if (!const_state->num_bindless_base_addresses && + !const_state->num_virtual_set_offsets) return; if (cs->device->physical_device->info->props.load_shader_consts_via_preamble) { diff --git a/src/freedreno/vulkan/tu_common.h b/src/freedreno/vulkan/tu_common.h index dfa46d8b359a..55e07eef5950 100644 --- a/src/freedreno/vulkan/tu_common.h +++ b/src/freedreno/vulkan/tu_common.h @@ -124,6 +124,26 @@ #define MAX_INLINE_UBO_RANGE 256 #define MAX_INLINE_UBOS 4 +/* Number of hardware bindless descriptor-set base registers per stage. + * a6xx has five, while a7xx and later have eight. + */ +static inline unsigned +tu_hw_max_sets(unsigned chip) +{ + return chip == 6 ? 5 : 8; +} + +/* Whether we need to virtualize descriptor sets because more sets are exposed + * to applications than there are hardware bindless base registers. Sets at + * and above tu_hw_max_sets() - 1 then share the last hardware base register, + * with a per-set descriptor-index offset. + */ +static inline bool +tu_uses_virtual_sets(unsigned chip, unsigned usable_sets) +{ + return usable_sets >= tu_hw_max_sets(chip); +} + #define A6XX_TEX_SAMP_DWORDS 4 /* We sample the fragment density map on the CPU, so technically the diff --git a/src/freedreno/vulkan/tu_device.cc b/src/freedreno/vulkan/tu_device.cc index 44d9f7d9fb22..7952eead1a93 100644 --- a/src/freedreno/vulkan/tu_device.cc +++ b/src/freedreno/vulkan/tu_device.cc @@ -87,11 +87,15 @@ tu_physical_device_compiler_options_init(struct tu_physical_device *device, device->compiler_options.enable_texel_buffer_emulation = instance->drirc.misc.enable_texel_buffer_emulation; } - if (device->info->props.max_storage_buffer_range_bytes < TU_D3D12_MAX_STORAGE_BUFFER_RANGE_BYTES) { - assert(fd_dev_gen(&device->dev_id) == 7); - device->compiler_options.enable_ssbo_emulation = - instance->drirc.misc.enable_ssbo_emulation; - } + } + + /* SSBO emulation only needs global loads/stores and a driver UBO holding + * the per-set descriptor base addresses, both of which exist on a6xx, so + * allow the drirc option on all supported generations. + */ + if (device->info->props.max_storage_buffer_range_bytes < TU_D3D12_MAX_STORAGE_BUFFER_RANGE_BYTES) { + device->compiler_options.enable_ssbo_emulation = + instance->drirc.misc.enable_ssbo_emulation; } device->compiler_options.allow_oob_indirect_ubo_loads = @@ -1324,9 +1328,17 @@ tu_get_properties(struct tu_physical_device *pdevice, props->optimalBufferCopyRowPitchAlignment = 128; props->nonCoherentAtomSize = 64; + /* Vulkan 1.4 requires storageBuffer8BitAccess and + * maxBoundDescriptorSets >= 7. Only a6xx gen4 and newer (and a7xx/a8xx) + * support 8-bit storage; a6xx gen4 parts expose 7 usable sets (plus one + * reserved for dynamic descriptors) by virtualizing sets 4-7 on top of the + * last hardware bindless base register, see tu_uses_virtual_sets(). Keep + * older a6xx parts, or configurations that override the set count below + * seven, on 1.3. + */ props->apiVersion = - ((pdevice->info->chip >= 7) ? TU_API_VERSION : - VK_MAKE_VERSION(1, 3, VK_HEADER_VERSION)); + (pdevice->info->props.storage_8bit && pdevice->usable_sets >= 7) ? + TU_API_VERSION : VK_MAKE_VERSION(1, 3, VK_HEADER_VERSION); props->driverVersion = vk_get_driver_version(); props->vendorID = pdevice->instance->drirc.debug.force_vk_vendor != 0 ? pdevice->instance->drirc.debug.force_vk_vendor : 0x5143; diff --git a/src/freedreno/vulkan/tu_pipeline.cc b/src/freedreno/vulkan/tu_pipeline.cc index 5b5dbab8edc9..a1ae403deeb6 100644 --- a/src/freedreno/vulkan/tu_pipeline.cc +++ b/src/freedreno/vulkan/tu_pipeline.cc @@ -54,15 +54,26 @@ emit_load_state(struct tu_cs *cs, unsigned opcode, enum a6xx_state_type st, } static unsigned -tu6_load_state_size(struct tu_pipeline *pipeline, +tu6_load_state_size(struct tu_device *device, + struct tu_pipeline *pipeline, struct tu_pipeline_layout *layout) { const unsigned load_state_size = 4; + const unsigned hw_max_sets = tu_hw_max_sets(device->compiler->info->chip); + const bool virtual_sets = + tu_uses_virtual_sets(device->compiler->info->chip, + device->physical_device->usable_sets); unsigned size = 0; for (unsigned i = 0; i < layout->num_sets; i++) { if (!(pipeline->active_desc_sets & (1u << i))) continue; + /* Virtualized sets share a hardware base register and need a runtime + * descriptor offset, which can't be encoded in a preload packet. + */ + if (virtual_sets && i >= hw_max_sets - 1) + continue; + struct tu_descriptor_set_layout *set_layout = layout->set[i].layout; for (unsigned j = 0; j < set_layout->binding_count; j++) { struct tu_descriptor_set_binding_layout *binding = &set_layout->binding[j]; @@ -74,6 +85,13 @@ tu6_load_state_size(struct tu_pipeline *pipeline, if (!binding->array_size) continue; + /* Dynamic descriptors live in the reserved set. When that set is + * virtualized as well, its descriptor index offset is only known at + * bind time, so no preload packet can be built here. + */ + if (virtual_sets && vk_descriptor_type_is_dynamic(binding->type)) + continue; + switch (binding->type) { case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: @@ -118,10 +136,15 @@ tu6_emit_load_state(struct tu_device *device, struct tu_pipeline *pipeline, struct tu_pipeline_layout *layout) { - unsigned size = tu6_load_state_size(pipeline, layout); + unsigned size = tu6_load_state_size(device, pipeline, layout); if (size == 0) return; + const unsigned hw_max_sets = tu_hw_max_sets(device->compiler->info->chip); + const bool virtual_sets = + tu_uses_virtual_sets(device->compiler->info->chip, + device->physical_device->usable_sets); + struct tu_cs cs; tu_cs_begin_sub_stream(&pipeline->cs, size, &cs); @@ -145,6 +168,9 @@ tu6_emit_load_state(struct tu_device *device, if (!(pipeline->active_desc_sets & (1u << i))) continue; + if (virtual_sets && i >= hw_max_sets - 1) + continue; + struct tu_descriptor_set_layout *set_layout = layout->set[i].layout; for (unsigned j = 0; j < set_layout->binding_count; j++) { struct tu_descriptor_set_binding_layout *binding = &set_layout->binding[j]; @@ -168,6 +194,11 @@ tu6_emit_load_state(struct tu_device *device, if (count == 0 || stages == 0) continue; + + /* See the matching check in tu6_load_state_size(). */ + if (virtual_sets && vk_descriptor_type_is_dynamic(binding->type)) + continue; + switch (binding->type) { case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: assert(device->physical_device->reserved_set_idx >= 0); @@ -1487,10 +1518,10 @@ tu_pipeline_allocate_cs(struct tu_device *dev, if (set_combined_state(builder, pipeline, VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT | VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT)) { - size += tu6_load_state_size(pipeline, layout); + size += tu6_load_state_size(dev, pipeline, layout); } } else { - size += tu6_load_state_size(pipeline, layout); + size += tu6_load_state_size(dev, pipeline, layout); } /* Allocate the space for the pipeline out of the device's RO suballocator. diff --git a/src/freedreno/vulkan/tu_shader.cc b/src/freedreno/vulkan/tu_shader.cc index 8bfc3b70c869..abda3d5c58e8 100644 --- a/src/freedreno/vulkan/tu_shader.cc +++ b/src/freedreno/vulkan/tu_shader.cc @@ -425,9 +425,32 @@ lower_load_vulkan_descriptor(nir_builder *b, nir_intrinsic_instr *intrin) } static nir_def * -bindless_resource_ir3(nir_builder *b, unsigned base, nir_def *desc_offset, +bindless_resource_ir3(nir_builder *b, struct tu_device *dev, + const struct ir3_const_allocations *const_allocs, + unsigned base, nir_def *desc_offset, bool can_speculate_descriptor) { + const unsigned hw_max_sets = tu_hw_max_sets(dev->compiler->info->chip); + + /* When more descriptor sets are exposed than the hardware has base + * registers, sets from hw_max_sets - 1 upwards share the last register. + * Add the set's offset (in 64-byte descriptor units) to the descriptor + * index, and point the access at the shared base register. + */ + if (tu_uses_virtual_sets(dev->compiler->info->chip, + dev->physical_device->usable_sets) && + const_allocs->consts[IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS].size_vec4 > 0 && + base >= hw_max_sets - 1) { + unsigned offset_dword = + const_allocs->consts[IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS] + .offset_vec4 * 4 + base; + nir_def *set_offset = + nir_load_const_ir3(b, 1, 32, nir_imm_int(b, 0), + .base = offset_dword); + desc_offset = nir_iadd(b, desc_offset, set_offset); + base = hw_max_sets - 1; + } + return nir_bindless_resource_ir3(b, 32, desc_offset, .desc_set = base, .access = can_speculate_descriptor ? @@ -437,7 +460,8 @@ bindless_resource_ir3(nir_builder *b, unsigned base, nir_def *desc_offset, static bool lower_ssbo_ubo_intrinsic(struct tu_device *dev, - nir_builder *b, nir_intrinsic_instr *intrin) + nir_builder *b, nir_intrinsic_instr *intrin, + const struct ir3_const_allocations *const_allocs) { const nir_intrinsic_info *info = &nir_intrinsic_infos[intrin->intrinsic]; @@ -475,7 +499,8 @@ lower_ssbo_ubo_intrinsic(struct tu_device *dev, if (nir_scalar_is_const(scalar_idx)) { bool can_speculate_descriptor = intrin->instr.pass_flags; nir_def *bindless = - bindless_resource_ir3(b, nir_scalar_as_uint(scalar_idx), + bindless_resource_ir3(b, dev, const_allocs, + nir_scalar_as_uint(scalar_idx), descriptor_idx, can_speculate_descriptor); nir_src_rewrite(&intrin->src[buffer_src], bindless); return true; @@ -486,7 +511,8 @@ lower_ssbo_ubo_intrinsic(struct tu_device *dev, /* if (base_idx == i) { ... */ nir_if *nif = nir_push_if(b, nir_ieq_imm(b, base_idx, i)); - nir_def *bindless = bindless_resource_ir3(b, i, descriptor_idx, false); + nir_def *bindless = + bindless_resource_ir3(b, dev, const_allocs, i, descriptor_idx, false); nir_intrinsic_instr *copy = nir_intrinsic_instr_create(b->shader, intrin->intrinsic); @@ -559,6 +585,7 @@ build_bindless(struct tu_device *dev, nir_builder *b, nir_deref_instr *deref, unsigned combined_descriptor_offset, struct tu_shader *shader, const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs, uint32_t read_only_input_attachments, bool dynamic_renderpass, bool *descriptor_valid) @@ -603,7 +630,8 @@ build_bindless(struct tu_device *dev, nir_builder *b, *descriptor_valid = !bind_layout->partially_bound && can_speculate_descriptor; - return bindless_resource_ir3(b, set, desc_offset, can_speculate_descriptor); + return bindless_resource_ir3(b, dev, const_allocs, set, desc_offset, + can_speculate_descriptor); } static nir_def * @@ -683,11 +711,13 @@ lower_texel_buffers_to_image(nir_builder *b, static void lower_image_deref(struct tu_device *dev, nir_builder *b, nir_intrinsic_instr *instr, struct tu_shader *shader, - const struct tu_pipeline_layout *layout) + const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs) { bool descriptor_valid = true; nir_deref_instr *deref = nir_src_as_deref(instr->src[0]); - nir_def *bindless = build_bindless(dev, b, deref, 0, shader, layout, 0, false, + nir_def *bindless = build_bindless(dev, b, deref, 0, shader, layout, + const_allocs, 0, false, &descriptor_valid); if ((instr->intrinsic == nir_intrinsic_image_deref_load || instr->intrinsic == nir_intrinsic_image_deref_sparse_load || @@ -740,7 +770,7 @@ lower_intrinsic(nir_builder *b, nir_intrinsic_instr *instr, case nir_intrinsic_ssbo_atomic: case nir_intrinsic_ssbo_atomic_swap: case nir_intrinsic_get_ssbo_size: - return lower_ssbo_ubo_intrinsic(dev, b, instr); + return lower_ssbo_ubo_intrinsic(dev, b, instr, const_allocs); case nir_intrinsic_image_deref_load: case nir_intrinsic_image_deref_sparse_load: @@ -749,7 +779,7 @@ lower_intrinsic(nir_builder *b, nir_intrinsic_instr *instr, case nir_intrinsic_image_deref_atomic_swap: case nir_intrinsic_image_deref_size: case nir_intrinsic_image_deref_samples: - lower_image_deref(dev, b, instr, shader, layout); + lower_image_deref(dev, b, instr, shader, layout, const_allocs); return true; case nir_intrinsic_load_frag_size_ir3: @@ -815,6 +845,7 @@ lower_tex_subsampled(const struct tu_sampler *sampler, struct tu_device *dev, struct tu_shader *shader, const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs, nir_builder *b, nir_tex_instr *tex) { @@ -831,6 +862,7 @@ lower_tex_subsampled(const struct tu_sampler *sampler, assert(tex_src_idx >= 0); nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_src_idx].src); nir_def *bindless = build_bindless(dev, b, deref, 2, shader, layout, + const_allocs, 0, /* read_only_input_attachments (not used) */ false, /* dynamic_renderpass (not used)*/ &descriptor_valid); @@ -928,6 +960,7 @@ static void lower_tex_immutable(struct tu_device *dev, struct tu_shader *shader, const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs, nir_builder *builder, nir_tex_instr *tex) { @@ -968,7 +1001,8 @@ lower_tex_immutable(struct tu_device *dev, if (samplers) { const struct tu_sampler *sampler = samplers + array_index; if (sampler->vk.flags & VK_SAMPLER_CREATE_SUBSAMPLED_BIT_EXT) - lower_tex_subsampled(sampler, dev, shader, layout, builder, tex); + lower_tex_subsampled(sampler, dev, shader, layout, const_allocs, + builder, tex); } } @@ -1000,6 +1034,7 @@ lower_tex_texel_buffer_to_image(nir_builder *b, static bool lower_tex_impl(nir_builder *b, nir_tex_instr *tex, struct tu_device *dev, struct tu_shader *shader, const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs, uint32_t read_only_input_attachments, bool dynamic_renderpass, bool ref) { @@ -1008,6 +1043,7 @@ lower_tex_impl(nir_builder *b, nir_tex_instr *tex, struct tu_device *dev, if (sampler_src_idx >= 0) { nir_deref_instr *deref = nir_src_as_deref(tex->src[sampler_src_idx].src); nir_def *bindless = build_bindless(dev, b, deref, 1, shader, layout, + const_allocs, read_only_input_attachments, dynamic_renderpass, &descriptor_valid); @@ -1019,6 +1055,7 @@ lower_tex_impl(nir_builder *b, nir_tex_instr *tex, struct tu_device *dev, if (tex_src_idx >= 0) { nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_src_idx].src); nir_def *bindless = build_bindless(dev, b, deref, 0, shader, layout, + const_allocs, read_only_input_attachments, dynamic_renderpass, &descriptor_valid); @@ -1128,6 +1165,7 @@ lower_input_attachment(nir_builder *b, nir_tex_instr *tex, static bool lower_tex(nir_builder *b, nir_tex_instr *tex, struct tu_device *dev, struct tu_shader *shader, const struct tu_pipeline_layout *layout, + const struct ir3_const_allocations *const_allocs, uint32_t read_only_input_attachments, bool dynamic_renderpass) { tex->can_speculate = true; @@ -1143,11 +1181,14 @@ lower_tex(nir_builder *b, nir_tex_instr *tex, struct tu_device *dev, if (tex->op == nir_texop_block_match_sad_qcom || tex->op == nir_texop_block_match_ssd_qcom || tex->op == nir_texop_sample_weighted_qcom) { - lower_tex_impl(b, tex, dev, shader, layout, read_only_input_attachments, dynamic_renderpass, false); - lower_tex_impl(b, tex, dev, shader, layout, read_only_input_attachments, dynamic_renderpass, true); + lower_tex_impl(b, tex, dev, shader, layout, const_allocs, + read_only_input_attachments, dynamic_renderpass, false); + lower_tex_impl(b, tex, dev, shader, layout, const_allocs, + read_only_input_attachments, dynamic_renderpass, true); } else { - lower_tex_immutable(dev, shader, layout, b, tex); - lower_tex_impl(b, tex, dev, shader, layout, read_only_input_attachments, dynamic_renderpass, false); + lower_tex_immutable(dev, shader, layout, const_allocs, b, tex); + lower_tex_impl(b, tex, dev, shader, layout, const_allocs, + read_only_input_attachments, dynamic_renderpass, false); } return true; @@ -1170,6 +1211,7 @@ lower_instr(nir_builder *b, nir_instr *instr, void *cb_data) switch (instr->type) { case nir_instr_type_tex: return lower_tex(b, nir_instr_as_tex(instr), params->dev, params->shader, params->layout, + params->const_allocs, params->read_only_input_attachments, params->dynamic_renderpass); case nir_instr_type_intrinsic: @@ -1606,19 +1648,39 @@ tu_lower_io(nir_shader *shader, struct tu_device *dev, ir3_const_alloc(const_allocs, IR3_CONST_ALLOC_INLINE_UNIFORM_ADDRS, ldgk_consts, 1); - if (dev->physical_device->compiler_options.enable_ssbo_emulation) { - const_state->num_bindless_base_addresses = layout->num_sets; - const_state->bindless_base_const_offset_vec4 = const_allocs->max_const_offset_vec4; + const unsigned hw_max_sets = tu_hw_max_sets(dev->compiler->info->chip); + const bool virtual_sets = + tu_uses_virtual_sets(dev->compiler->info->chip, + dev->physical_device->usable_sets); + if (dev->physical_device->compiler_options.enable_ssbo_emulation || + virtual_sets) { + unsigned num_sets = layout->num_sets; if (dev->physical_device->reserved_set_idx >= 0) { - const_state->num_bindless_base_addresses = - MAX2(layout->num_sets, + num_sets = + MAX2(num_sets, static_cast(dev->physical_device->reserved_set_idx) + 1); } - if (!dev->compiler->info->props.load_shader_consts_via_preamble) { - ir3_const_alloc(const_allocs, IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS, - DIV_ROUND_UP(const_state->num_bindless_base_addresses * 2, 4), 1); + if (dev->physical_device->compiler_options.enable_ssbo_emulation) { + const_state->num_bindless_base_addresses = num_sets; + const_state->bindless_base_const_offset_vec4 = + const_allocs->max_const_offset_vec4; + + if (!dev->compiler->info->props.load_shader_consts_via_preamble) { + ir3_const_alloc(const_allocs, IR3_CONST_ALLOC_BINDLESS_BASE_ADDRS, + DIV_ROUND_UP(num_sets * 2, 4), 1); + } + } + + /* Sets at and above hw_max_sets - 1 share the last hardware base + * register and are distinguished by a per-set descriptor-index offset. + */ + if (virtual_sets && num_sets > hw_max_sets - 1 && + !dev->compiler->info->props.load_shader_consts_via_preamble) { + ir3_const_alloc(const_allocs, IR3_CONST_ALLOC_VIRTUAL_SET_OFFSETS, + DIV_ROUND_UP(num_sets, 4), 1); + const_state->num_virtual_set_offsets = num_sets; } } @@ -1877,11 +1939,12 @@ get_ssbo_bindless(nir_intrinsic_instr *intr) } static nir_def * -build_ssbo_global_addr(nir_builder *b, - nir_intrinsic_instr *bindless, - struct tu_shader *shader, - bool load_shader_consts_via_preamble, - const struct ir3_const_allocations *const_allocs) +load_ssbo_descriptor(nir_builder *b, + nir_intrinsic_instr *bindless, + struct tu_shader *shader, + bool load_shader_consts_via_preamble, + const struct ir3_const_allocations *const_allocs, + unsigned first_dword, unsigned num_components) { nir_def *set_base; @@ -1901,20 +1964,38 @@ build_ssbo_global_addr(nir_builder *b, nir_def *descriptor_offset = nir_iadd_imm( b, nir_imul_imm(b, bindless->src[0].ssa, FDL6_TEX_CONST_DWORDS * 4), - 11 * 4); - nir_def *descriptor_words = nir_load_global_offset( - b, 2, 32, nir_pack_64_2x32(b, set_base), descriptor_offset, + first_dword * 4); + return nir_load_global_offset( + b, num_components, 32, nir_pack_64_2x32(b, set_base), descriptor_offset, .access = static_cast( ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER | ACCESS_CAN_SPECULATE), .align_mul = 4, .align_offset = 0); +} + +static nir_def * +build_ssbo_global_addr(nir_builder *b, + nir_intrinsic_instr *bindless, + struct tu_shader *shader, + bool load_shader_consts_via_preamble, + const struct ir3_const_allocations *const_allocs) +{ + nir_def *descriptor_words = + load_ssbo_descriptor(b, bindless, shader, load_shader_consts_via_preamble, + const_allocs, 11, 2); return nir_pack_64_2x32(b, descriptor_words); } +/* On a6xx there is no RESBASE instruction, but the descriptor's width/height + * fields already encode the buffer size, so RESINFO works. Only a7xx+ needs + * the explicit RESBASE lowering. + */ + struct lower_ssbo_address_size_state { struct tu_shader *shader; const struct ir3_const_allocations *const_allocs; bool load_shader_consts_via_preamble; + unsigned gen; }; static bool @@ -1936,41 +2017,49 @@ lower_ssbo_address_size(nir_builder *b, nir_intrinsic_instr *intr, void *data) b, get_ssbo_bindless(intr), state->shader, state->load_shader_consts_via_preamble, state->const_allocs); nir_def *offset = intr->src[1].ssa; + nir_def *addr = nir_iadd(b, base, nir_u2u64(b, offset)); + /* Rewrite the users in place rather than replacing the load and + * deleting the old instruction: the old load is often the instruction + * immediately following this one, so removing it would invalidate the + * caller's safe-iterator state. + */ nir_foreach_use_safe (use, &intr->def) { nir_instr *use_instr = nir_src_use_instr(use); - b->cursor = nir_before_instr(use_instr); nir_intrinsic_instr *use_intr = nir_instr_as_intrinsic(use_instr); switch (use_intr->intrinsic) { case nir_intrinsic_global_atomic: - case nir_intrinsic_global_atomic_swap: { - nir_def *addr = nir_iadd(b, base, nir_u2u64(b, offset)); - nir_src_rewrite(nir_get_io_offset_src(use_intr), addr); + case nir_intrinsic_global_atomic_swap: + case nir_intrinsic_load_global: + nir_src_rewrite(&use_intr->src[0], addr); break; - } - case nir_intrinsic_load_global: { - nir_def *load = nir_load_global_offset( - b, use_intr->def.num_components, use_intr->def.bit_size, base, - offset, .access = nir_intrinsic_access(use_intr)); - nir_def_replace(&use_intr->def, load); + case nir_intrinsic_store_global: + nir_src_rewrite(&use_intr->src[1], addr); break; - } - case nir_intrinsic_store_global: { - nir_store_global_offset(b, nir_get_io_data_src(use_intr)->ssa, base, offset, - .access = nir_intrinsic_access(use_intr)); - nir_instr_remove(use_instr); - break; - } default: UNREACHABLE("unexpected use of @load_ssbo_address"); } } + + nir_instr_remove(&intr->instr); } else { - nir_def *ssbo_size = - build_ssbo_size_from_resbase(b, nir_get_io_index_src(intr)->ssa); - nir_def_replace(&intr->def, ssbo_size); + nir_intrinsic_instr *bindless = get_ssbo_bindless(intr); + + if (state->gen >= 7) { + /* RESBASE only exists on a7xx+. */ + nir_def *ssbo_size = + build_ssbo_size_from_resbase(b, &bindless->def); + nir_def_replace(&intr->def, ssbo_size); + } else { + /* RESINFO, which derives the size from the descriptor's + * width/height fields, works for all a6xx sets, including + * virtualized ones (their descriptor index carries the shared + * register's per-set offset). + */ + return false; + } } return true; @@ -3718,6 +3807,7 @@ tu_shader_create(struct tu_device *dev, .const_allocs = &const_allocs, .load_shader_consts_via_preamble = dev->compiler->info->props.load_shader_consts_via_preamble, + .gen = dev->compiler->info->chip, }; NIR_PASS(_, nir, tu_nir_lower_ssbo_address_size, &lower_ssbo_state); } diff --git a/src/freedreno/vulkan/tu_shader.h b/src/freedreno/vulkan/tu_shader.h index e729d44d4a07..33b210ed80df 100644 --- a/src/freedreno/vulkan/tu_shader.h +++ b/src/freedreno/vulkan/tu_shader.h @@ -52,6 +52,10 @@ struct tu_const_state struct tu_inline_ubo ubos[MAX_INLINE_UBOS]; uint32_t num_bindless_base_addresses; uint32_t bindless_base_const_offset_vec4; + /* Number of u32 entries in the virtual-set offset table, or 0 if sets + * aren't virtualized. + */ + uint32_t num_virtual_set_offsets; struct ir3_driver_ubo fdm_ubo; struct ir3_driver_ubo dynamic_offsets_ubo; diff --git a/src/freedreno/vulkan/tu_wsi.cc b/src/freedreno/vulkan/tu_wsi.cc index 5d2604f10127..4da1fdd2d242 100644 --- a/src/freedreno/vulkan/tu_wsi.cc +++ b/src/freedreno/vulkan/tu_wsi.cc @@ -66,6 +66,15 @@ tu_wsi_init(struct tu_physical_device *physical_device) physical_device->wsi_device.supports_modifiers = true; physical_device->wsi_device.can_present_on_device = tu_wsi_can_present_on_device; + /* KGSL does not implement implicit sync (see kgsl_bo_cpu_prep()), and msm + * VM_BIND submissions carry no BO list, so the kernel cannot attach fences + * to the dma-bufs backing the swapchain images. Without explicit sync or + * a dma-buf syncobj, the window system could scan out an image before the + * GPU has finished rendering it. Wait for the present fence before + * handing the image to X11 in that case. + */ + physical_device->wsi_device.x11.wait_for_fences = + tu_wsi_is_kgsl(physical_device) || physical_device->has_vm_bind; physical_device->vk.wsi_device = &physical_device->wsi_device; diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h index fef8aeef8bfc..43c1dd6e8a77 100644 --- a/src/vulkan/wsi/wsi_common.h +++ b/src/vulkan/wsi/wsi_common.h @@ -140,6 +140,12 @@ struct wsi_device { /* Select and read Present events on a separate connection to the X * server, so the WSI never reads from the application's connection. */ bool dedicated_event_connection; + + /* The driver cannot provide reliable implicit sync for WSI buffers + * (e.g. KGSL, or when every submission uses VM_BIND and bare dma-buf + * fences are not available), so wait for rendering to finish before + * presenting. */ + bool wait_for_fences; } x11; struct { diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c index 166a88ac8049..4d8f283945fd 100644 --- a/src/vulkan/wsi/wsi_common_x11.c +++ b/src/vulkan/wsi/wsi_common_x11.c @@ -2595,9 +2595,12 @@ x11_manage_present_queue(void *state) VkPresentModeKHR present_mode = chain->images[image_index].present_mode; - /* Not necessary to block when we have explicit sync */ + /* Not necessary to block when we have explicit sync or when the + * kernel can wait on a syncobj attached to the dma-buf itself. */ bool need_fence_wait = - x11_needs_wait_for_fences(chain->base.wsi, wsi_conn, present_mode) && + (x11_needs_wait_for_fences(chain->base.wsi, wsi_conn, present_mode) || + (chain->base.wsi->x11.wait_for_fences && + chain->base.dma_buf_semaphore == VK_NULL_HANDLE)) && !chain->base.image_info.explicit_sync; bool need_timing_fence_wait = x11_swapchain_present_timing_is_out_of_order_completion(