+static struct vtn_pointer *
+vtn_load_param_pointer(struct vtn_builder *b,
+ struct vtn_type *param_type,
+ uint32_t param_idx)
+{
+ struct vtn_type *ptr_type = param_type;
+ if (param_type->base_type != vtn_base_type_pointer) {
+ assert(param_type->base_type == vtn_base_type_image ||
+ param_type->base_type == vtn_base_type_sampler);
+ ptr_type = rzalloc(b, struct vtn_type);
+ ptr_type->base_type = vtn_base_type_pointer;
+ ptr_type->deref = param_type;
+ ptr_type->storage_class = SpvStorageClassUniformConstant;
+ }
+
+ return vtn_pointer_from_ssa(b, nir_load_param(&b->nb, param_idx), ptr_type);
+}
+
+static unsigned
+vtn_type_count_function_params(struct vtn_type *type)
+{
+ switch (type->base_type) {
+ case vtn_base_type_array:
+ case vtn_base_type_matrix:
+ return type->length * vtn_type_count_function_params(type->array_element);
+
+ case vtn_base_type_struct: {
+ unsigned count = 0;
+ for (unsigned i = 0; i < type->length; i++)
+ count += vtn_type_count_function_params(type->members[i]);
+ return count;
+ }
+
+ case vtn_base_type_sampled_image:
+ return 2;
+
+ default:
+ return 1;
+ }
+}
+
+static void
+vtn_type_add_to_function_params(struct vtn_type *type,
+ nir_function *func,
+ unsigned *param_idx)
+{
+ static const nir_parameter nir_deref_param = {
+ .num_components = 1,
+ .bit_size = 32,
+ };
+
+ switch (type->base_type) {
+ case vtn_base_type_array:
+ case vtn_base_type_matrix:
+ for (unsigned i = 0; i < type->length; i++)
+ vtn_type_add_to_function_params(type->array_element, func, param_idx);
+ break;
+
+ case vtn_base_type_struct:
+ for (unsigned i = 0; i < type->length; i++)
+ vtn_type_add_to_function_params(type->members[i], func, param_idx);
+ break;
+
+ case vtn_base_type_sampled_image:
+ func->params[(*param_idx)++] = nir_deref_param;
+ func->params[(*param_idx)++] = nir_deref_param;
+ break;
+
+ case vtn_base_type_image:
+ case vtn_base_type_sampler:
+ func->params[(*param_idx)++] = nir_deref_param;
+ break;
+
+ case vtn_base_type_pointer:
+ if (type->type) {
+ func->params[(*param_idx)++] = (nir_parameter) {
+ .num_components = glsl_get_vector_elements(type->type),
+ .bit_size = glsl_get_bit_size(type->type),
+ };
+ } else {
+ func->params[(*param_idx)++] = nir_deref_param;
+ }
+ break;
+
+ default:
+ func->params[(*param_idx)++] = (nir_parameter) {
+ .num_components = glsl_get_vector_elements(type->type),
+ .bit_size = glsl_get_bit_size(type->type),
+ };
+ }
+}
+
+static void
+vtn_ssa_value_add_to_call_params(struct vtn_builder *b,
+ struct vtn_ssa_value *value,
+ struct vtn_type *type,
+ nir_call_instr *call,
+ unsigned *param_idx)
+{
+ switch (type->base_type) {
+ case vtn_base_type_array:
+ case vtn_base_type_matrix:
+ for (unsigned i = 0; i < type->length; i++) {
+ vtn_ssa_value_add_to_call_params(b, value->elems[i],
+ type->array_element,
+ call, param_idx);
+ }
+ break;
+
+ case vtn_base_type_struct:
+ for (unsigned i = 0; i < type->length; i++) {
+ vtn_ssa_value_add_to_call_params(b, value->elems[i],
+ type->members[i],
+ call, param_idx);
+ }
+ break;
+
+ default:
+ call->params[(*param_idx)++] = nir_src_for_ssa(value->def);
+ break;
+ }
+}
+
+static void
+vtn_ssa_value_load_function_param(struct vtn_builder *b,
+ struct vtn_ssa_value *value,
+ struct vtn_type *type,
+ unsigned *param_idx)
+{
+ switch (type->base_type) {
+ case vtn_base_type_array:
+ case vtn_base_type_matrix:
+ for (unsigned i = 0; i < type->length; i++) {
+ vtn_ssa_value_load_function_param(b, value->elems[i],
+ type->array_element, param_idx);
+ }
+ break;
+
+ case vtn_base_type_struct:
+ for (unsigned i = 0; i < type->length; i++) {
+ vtn_ssa_value_load_function_param(b, value->elems[i],
+ type->members[i], param_idx);
+ }
+ break;
+
+ default:
+ value->def = nir_load_param(&b->nb, (*param_idx)++);
+ break;
+ }
+}
+
+void
+vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
+ const uint32_t *w, unsigned count)
+{
+ struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
+ struct vtn_function *vtn_callee =
+ vtn_value(b, w[3], vtn_value_type_function)->func;
+ struct nir_function *callee = vtn_callee->impl->function;
+
+ vtn_callee->referenced = true;
+
+ nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
+
+ unsigned param_idx = 0;
+
+ nir_deref_instr *ret_deref = NULL;
+ struct vtn_type *ret_type = vtn_callee->type->return_type;
+ if (ret_type->base_type != vtn_base_type_void) {
+ nir_variable *ret_tmp =
+ nir_local_variable_create(b->nb.impl,
+ glsl_get_bare_type(ret_type->type),
+ "return_tmp");
+ ret_deref = nir_build_deref_var(&b->nb, ret_tmp);
+ call->params[param_idx++] = nir_src_for_ssa(&ret_deref->dest.ssa);
+ }
+
+ for (unsigned i = 0; i < vtn_callee->type->length; i++) {
+ struct vtn_type *arg_type = vtn_callee->type->params[i];
+ unsigned arg_id = w[4 + i];
+
+ if (arg_type->base_type == vtn_base_type_sampled_image) {
+ struct vtn_sampled_image *sampled_image =
+ vtn_value(b, arg_id, vtn_value_type_sampled_image)->sampled_image;
+
+ call->params[param_idx++] =
+ nir_src_for_ssa(&sampled_image->image->deref->dest.ssa);
+ call->params[param_idx++] =
+ nir_src_for_ssa(&sampled_image->sampler->deref->dest.ssa);
+ } else if (arg_type->base_type == vtn_base_type_pointer ||
+ arg_type->base_type == vtn_base_type_image ||
+ arg_type->base_type == vtn_base_type_sampler) {
+ struct vtn_pointer *pointer =
+ vtn_value(b, arg_id, vtn_value_type_pointer)->pointer;
+ call->params[param_idx++] =
+ nir_src_for_ssa(vtn_pointer_to_ssa(b, pointer));
+ } else {
+ vtn_ssa_value_add_to_call_params(b, vtn_ssa_value(b, arg_id),
+ arg_type, call, ¶m_idx);
+ }
+ }
+ assert(param_idx == call->num_params);
+
+ nir_builder_instr_insert(&b->nb, &call->instr);
+
+ if (ret_type->base_type == vtn_base_type_void) {
+ vtn_push_value(b, w[2], vtn_value_type_undef);
+ } else {
+ vtn_push_ssa(b, w[2], res_type, vtn_local_load(b, ret_deref, 0));
+ }
+}
+