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
2 changes: 1 addition & 1 deletion src/asahi/lib/agx_chip_caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ enum agx_chip_cap {
#define AGX_CAPS_M5 \
(AGX_CAP_ELIDE_APP_DISPATCH_BARRIER | AGX_CAP_VDM_INDIRECT_INDEX_BOUNDS | \
AGX_CAP_HW_TXF_BOUNDS | AGX_CAP_COOPERATIVE_MATRIX | \
AGX_CAP_CDM_WAR_WEAK_BARRIER)
AGX_CAP_DESCRIPTOR_BUFFER | AGX_CAP_CDM_WAR_WEAK_BARRIER)

/*
* One row per chip of AGX_CHIPS, in the same order: chip, capabilities.
Expand Down
69 changes: 37 additions & 32 deletions src/asahi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -382,39 +382,44 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests
suite : ['asahi'],
)

# Descriptor buffer sizes and strides as vkd3d-proton lays out its heaps.
test(
'hk-g17p-descriptor-buffer-sizes',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_descriptor_buffer_sizes,
],
env : [
'ASAHI_SHIM_CHIP=g17p',
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)
foreach chip : ['g15g', 'g15s', 'g15c', 'g16g', 'g17p', 'g17s']
# Descriptor buffer sizes and strides as vkd3d-proton lays out its heaps.
test(
'hk-' + chip + '-descriptor-buffer-sizes',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_descriptor_buffer_sizes,
],
env : [
'ASAHI_SHIM_CHIP=' + chip,
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)
endforeach

# Texel buffer descriptors from vkGetDescriptorEXT: their software metadata.
test(
'hk-g17p-descriptor-buffer-texel',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_descriptor_buffer_texel,
],
env : [
'ASAHI_SHIM_CHIP=g17p',
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)
foreach chip : ['g17p', 'g17s']
# Texel and storage buffer descriptors from vkGetDescriptorEXT: their
# software metadata.
test(
'hk-' + chip + '-descriptor-buffer-texel',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_descriptor_buffer_texel,
] + (chip == 'g17s' ? ['m5'] : []),
env : [
'ASAHI_SHIM_CHIP=' + chip,
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)
endforeach

# A render on one queue read by compute on another, ordered by timeline
# semaphores, including waits made by submissions without command buffers;
Expand Down
54 changes: 38 additions & 16 deletions src/asahi/vulkan/hk_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ hk_buffer_sparse_texel_word(const struct hk_buffer *buffer,
(uint32_t)util_logbase2(blocksize_B);
}

/* The sparse buffer that holds address. Call with hk_device::sparse_buffers.lock
* held. */
static const struct hk_buffer *
hk_sparse_buffer_at_locked(struct hk_device *dev, uint64_t address)
{
list_for_each_entry(struct hk_buffer, buffer, &dev->sparse_buffers.list,
sparse_link) {
if (address - buffer->va->addr < buffer->vk.size)
return buffer;
}
return NULL;
}

uint32_t
hk_sparse_texel_word_at(struct hk_device *dev, uint64_t address,
unsigned blocksize_B)
Expand All @@ -93,34 +106,43 @@ hk_sparse_texel_word_at(struct hk_device *dev, uint64_t address,
if (!p_atomic_read(&dev->sparse_buffers.count))
return 0;

uint32_t word = 0;
simple_mtx_lock(&dev->sparse_buffers.lock);
list_for_each_entry(struct hk_buffer, buffer, &dev->sparse_buffers.list,
sparse_link) {
if (address - buffer->va->addr < buffer->vk.size) {
word = hk_buffer_sparse_texel_word(buffer, blocksize_B);
break;
}
}
const uint32_t word = hk_buffer_sparse_texel_word(
hk_sparse_buffer_at_locked(dev, address), blocksize_B);
simple_mtx_unlock(&dev->sparse_buffers.lock);
return word;
}

uint32_t
hk_sparse_map_page_at(struct hk_device *dev, uint64_t address)
{
/* Only M5 lowers storage loads through this word (get_buffer_address()).
* Elsewhere it is the address format's byte offset and must stay zero. */
if (!agx_chip_g17_m5(dev->dev.chip) ||
!p_atomic_read(&dev->sparse_buffers.count))
return 0;

uint32_t page = 0;
simple_mtx_lock(&dev->sparse_buffers.lock);
const struct hk_buffer *buffer = hk_sparse_buffer_at_locked(dev, address);
if (buffer && buffer->sparse_map) {
const uint64_t map_addr = buffer->sparse_map->va->addr;
assert((map_addr % AIL_PAGESIZE) == 0);
assert((map_addr / AIL_PAGESIZE) <= UINT32_MAX);
page = map_addr / AIL_PAGESIZE;
}
simple_mtx_unlock(&dev->sparse_buffers.lock);
return page;
}

bool
hk_sparse_address_is_sparse(struct hk_device *dev, uint64_t address)
{
if (!p_atomic_read(&dev->sparse_buffers.count))
return false;

bool sparse = false;
simple_mtx_lock(&dev->sparse_buffers.lock);
list_for_each_entry(struct hk_buffer, buffer, &dev->sparse_buffers.list,
sparse_link) {
if (address - buffer->va->addr < buffer->vk.size) {
sparse = true;
break;
}
}
const bool sparse = hk_sparse_buffer_at_locked(dev, address) != NULL;
simple_mtx_unlock(&dev->sparse_buffers.lock);
return sparse;
}
Expand Down
3 changes: 3 additions & 0 deletions src/asahi/vulkan/hk_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ uint32_t hk_buffer_sparse_texel_word(const struct hk_buffer *buffer,
unsigned blocksize_B);
uint32_t hk_sparse_texel_word_at(struct hk_device *dev, uint64_t address,
unsigned blocksize_B);
/* The residency map page of the sparse buffer holding address, as
* hk_buffer_address::sparse_map_page carries it, 0 for none. */
uint32_t hk_sparse_map_page_at(struct hk_device *dev, uint64_t address);
bool hk_sparse_address_is_sparse(struct hk_device *dev, uint64_t address);

/* Publish a bound/unbound sparse buffer range to physical pointer reads. */
Expand Down
10 changes: 10 additions & 0 deletions src/asahi/vulkan/hk_descriptor_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ write_sampled_image_view_desc(struct hk_descriptor_set *set,
else
plane_count = MAX2(plane_count, sampler->plane_count);

/* A null converted image and a standalone sampler each have a single
* output record in vkGetDescriptorEXT, even with two filter modes.
* Any converted sample of the null image stops at its first plane.
*/
if (view_planes == 0 && sampler->vk.ycbcr_conversion)
plane_count = MIN2(plane_count, binding_layout->stride / native.stride);

for (uint8_t plane = 0; plane < plane_count; plane++) {
/* We need to replicate the last sampler plane out to all image
* planes due to sampler table entry limitations. See
Expand Down Expand Up @@ -1610,6 +1617,9 @@ hk_GetDescriptorEXT(VkDevice _device,
desc = (struct hk_buffer_address){
.base_addr = info->address,
.size = MIN2(info->range, UINT32_MAX),
.sparse_map_page = type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
? hk_sparse_map_page_at(dev, info->address)
: 0,
};
}
write_desc(&set, 0, 0, &desc, sizeof(desc));
Expand Down
45 changes: 25 additions & 20 deletions src/asahi/vulkan/hk_descriptor_set_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ binding_has_immutable_samplers(const VkDescriptorSetLayoutBinding *binding)
}
}

/* Converted immutable samplers reserve one native record per image plane. */
static unsigned
binding_plane_count(const VkDescriptorSetLayoutBinding *binding)
{
unsigned planes = 1;
if (binding_has_immutable_samplers(binding)) {
for (uint32_t i = 0; i < binding->descriptorCount; i++) {
VK_FROM_HANDLE(hk_sampler, sampler, binding->pImmutableSamplers[i]);
if (sampler->vk.ycbcr_conversion)
planes = MAX2(planes, vk_format_get_plane_count(
sampler->vk.ycbcr_conversion->state.format));
}
}
return planes;
}

void
hk_descriptor_stride_align_for_type(
const struct hk_physical_device *pdev, VkDescriptorType type,
Expand Down Expand Up @@ -375,25 +391,12 @@ hk_CreateDescriptorSetLayout(VkDevice device,
binding->descriptorType,
type_list, &stride, &alignment);

uint8_t max_plane_count = 1;

if (binding_has_immutable_samplers(binding)) {
layout->binding[b].immutable_samplers = samplers;
samplers += binding->descriptorCount;
for (uint32_t i = 0; i < binding->descriptorCount; i++) {
VK_FROM_HANDLE(hk_sampler, sampler, binding->pImmutableSamplers[i]);
layout->binding[b].immutable_samplers[i] = sampler;
const uint8_t sampler_plane_count =
sampler->vk.ycbcr_conversion
? vk_format_get_plane_count(
sampler->vk.ycbcr_conversion->state.format)
: 1;
if (max_plane_count < sampler_plane_count)
max_plane_count = sampler_plane_count;
}
for (uint32_t i = 0; i < binding->descriptorCount; i++)
*samplers++ = hk_sampler_from_handle(binding->pImmutableSamplers[i]);
}

stride *= max_plane_count;
stride *= binding_plane_count(binding);

if (stride > 0) {
assert(stride <= UINT16_MAX);
Expand Down Expand Up @@ -485,7 +488,7 @@ hk_GetDescriptorSetLayoutSupport(
uint32_t variable_stride = 0;
uint32_t variable_count = 0;
bool variable_is_inline_uniform_block = false;
uint8_t dynamic_buffer_count = 0;
uint64_t dynamic_buffer_count = 0;

for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[i];
Expand All @@ -505,6 +508,8 @@ hk_GetDescriptorSetLayoutSupport(
binding->descriptorType,
type_list, &stride, &alignment);

stride *= binding_plane_count(binding);

if (stride > 0) {
assert(stride <= UINT16_MAX);
assert(util_is_power_of_two_nonzero(alignment));
Expand All @@ -527,15 +532,15 @@ hk_GetDescriptorSetLayoutSupport(
* check for whether or not the max buffer size is big enough, we
* keep non_variable_size aligned to max_align.
*/
non_variable_size += stride * binding->descriptorCount;
non_variable_size += (uint64_t)stride * binding->descriptorCount;
non_variable_size = align64(non_variable_size, max_align);
}
}
}

uint64_t buffer_size = non_variable_size;
if (variable_stride > 0) {
buffer_size += variable_stride * variable_count;
buffer_size += (uint64_t)variable_stride * variable_count;
buffer_size = align64(buffer_size, max_align);
}

Expand All @@ -555,7 +560,7 @@ hk_GetDescriptorSetLayoutSupport(
VkDescriptorSetVariableDescriptorCountLayoutSupport *vs = ext;
uint32_t max_var_count;

if (variable_stride > 0) {
if (variable_stride > 0 && non_variable_size < max_buffer_size) {
max_var_count =
(max_buffer_size - non_variable_size) / variable_stride;
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/asahi/vulkan/hk_physical_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ hk_get_device_extensions(const struct agx_device *dev,
* depth clip. */
.EXT_depth_clip_control = !agx_chip_hal300_path(dev->chip),
.EXT_depth_clip_enable = true,
/* Descriptor buffers use AGX_CAP_DESCRIPTOR_BUFFER (agx_chip_caps.h):
* AGX2, native AGX3, and M5 are qualified; G18G still uses AGX2-biased
* sampler indices and record-time compute image lowering. */
.EXT_descriptor_buffer =
agx_chip_has_cap(dev->chip, AGX_CAP_DESCRIPTOR_BUFFER),
.EXT_descriptor_indexing = true,
Expand Down Expand Up @@ -781,8 +784,13 @@ hk_get_device_features(
* layout, and push descriptors keep their own command-buffer storage.
*/
.descriptorBuffer = supported_extensions->EXT_descriptor_buffer,
/* M5 storage buffer descriptors embed the residency map page of a
* sparse buffer (hk_buffer_address::sparse_map_page), and capture/
* replay keeps the buffer's VA but not its map's: a replayed
* descriptor would name a stale map. */
.descriptorBufferCaptureReplay =
supported_extensions->EXT_descriptor_buffer,
supported_extensions->EXT_descriptor_buffer &&
!agx_chip_g17_m5(dev->chip),
.descriptorBufferImageLayoutIgnored =
supported_extensions->EXT_descriptor_buffer,
.descriptorBufferPushDescriptors =
Expand Down
45 changes: 42 additions & 3 deletions src/asahi/vulkan/hk_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ lookup_ycbcr_sampler(const void *_state, uint32_t set, uint32_t binding,
}

static const struct hk_sampler *
lookup_packed_ycbcr_sampler(nir_tex_instr *tex, const void *state)
tex_ycbcr_sampler(nir_tex_instr *tex, const void *state)
{
int handle = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
if (handle < 0)
Expand All @@ -1970,11 +1970,48 @@ lookup_packed_ycbcr_sampler(nir_tex_instr *tex, const void *state)
return NULL;
index = nir_src_as_uint(deref->arr.index);
}
const struct hk_sampler *sampler = lookup_ycbcr_sampler(
state, var->data.descriptor_set, var->data.binding, index);
return lookup_ycbcr_sampler(state, var->data.descriptor_set,
var->data.binding, index);
}

static const struct hk_sampler *
lookup_packed_ycbcr_sampler(nir_tex_instr *tex, const void *state)
{
const struct hk_sampler *sampler = tex_ycbcr_sampler(tex, state);
return sampler && hk_packed_ycbcr_bits(sampler->ycbcr.format) ? sampler : NULL;
}

/* A null converted image occupies just the first descriptor-buffer record.
* Test the first plane before lowering the sample into its plane accesses
* and color conversion. The conversion itself can map zero to nonzero.
*/
static bool
guard_null_ycbcr_sample(nir_builder *b, nir_tex_instr *tex, void *data)
{
if (nir_tex_instr_is_query(tex) || !tex_ycbcr_sampler(tex, data))
return false;

const struct lower_ycbcr_state *state = data;
int src = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
nir_variable *var =
nir_deref_instr_get_variable(nir_src_as_deref(tex->src[src].src));
if (!(state->set_layouts[var->data.descriptor_set]->flags &
VK_DESCRIPTOR_SET_LAYOUT_CREATE_DESCRIPTOR_BUFFER_BIT_EXT))
return false;

b->cursor = nir_before_instr(&tex->instr);
nir_def *levels = nir_build_texture_query(b, tex, nir_texop_query_levels,
1, nir_type_int32, false, true);
nir_def *zero = nir_imm_zero(b, tex->def.num_components, tex->def.bit_size);
nir_if *valid = nir_push_if(b, nir_ine_imm(b, levels, 0));
valid->control = nir_selection_control_dont_flatten;
nir_instr *sample = nir_instr_clone(b->shader, &tex->instr);
nir_builder_instr_insert(b, sample);
nir_pop_if(b, valid);
nir_def_replace(&tex->def, nir_if_phi(b, &nir_instr_as_tex(sample)->def, zero));
return true;
}

/* API size queries return luma pixels. Run before adding private word queries. */
static bool
lower_packed_ycbcr_size(nir_builder *b, nir_tex_instr *tex, void *state)
Expand Down Expand Up @@ -3264,6 +3301,8 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir,
.set_layout_count = set_layout_count,
.set_layouts = set_layouts,
};
NIR_PASS(lowered_complex, nir, nir_shader_tex_pass, guard_null_ycbcr_sample,
nir_metadata_none, (void *)&ycbcr_state);
NIR_PASS(lowered_complex, nir, nir_shader_tex_pass, lower_packed_ycbcr_size,
nir_metadata_control_flow, (void *)&ycbcr_state);
NIR_PASS(lowered_complex, nir, nir_shader_tex_pass, lower_ycbcr_explicit,
Expand Down
Loading