const glsl_type * glsl_channel_type(const glsl_type *t) { switch (glsl_get_base_type(t)) { case GLSL_TYPE_ARRAY: { const glsl_type *base = glsl_channel_type(glsl_get_array_element(t)); return glsl_array_type(base, glsl_get_length(t)); } case GLSL_TYPE_UINT: return glsl_uint_type(); case GLSL_TYPE_INT: return glsl_int_type(); case GLSL_TYPE_FLOAT: return glsl_float_type(); case GLSL_TYPE_BOOL: return glsl_bool_type(); case GLSL_TYPE_DOUBLE: return glsl_double_type(); case GLSL_TYPE_UINT64: return glsl_uint64_t_type(); case GLSL_TYPE_INT64: return glsl_int64_t_type(); case GLSL_TYPE_FLOAT16: return glsl_float16_t_type(); case GLSL_TYPE_UINT16: return glsl_uint16_t_type(); case GLSL_TYPE_INT16: return glsl_int16_t_type(); default: unreachable("Unhandled base type glsl_channel_type()"); } }
void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode, const uint32_t *w, unsigned count) { switch (opcode) { case SpvOpVariable: { struct vtn_variable *var = rzalloc(b, struct vtn_variable); var->type = vtn_value(b, w[1], vtn_value_type_type)->type; var->chain.var = var; var->chain.length = 0; struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_access_chain); val->access_chain = &var->chain; struct vtn_type *without_array = var->type; while(glsl_type_is_array(without_array->type)) without_array = without_array->array_element; nir_variable_mode nir_mode; switch ((SpvStorageClass)w[3]) { case SpvStorageClassUniform: case SpvStorageClassUniformConstant: if (without_array->block) { var->mode = vtn_variable_mode_ubo; b->shader->info.num_ubos++; } else if (without_array->buffer_block) { var->mode = vtn_variable_mode_ssbo; b->shader->info.num_ssbos++; } else if (glsl_type_is_image(without_array->type)) { var->mode = vtn_variable_mode_image; nir_mode = nir_var_uniform; b->shader->info.num_images++; } else if (glsl_type_is_sampler(without_array->type)) { var->mode = vtn_variable_mode_sampler; nir_mode = nir_var_uniform; b->shader->info.num_textures++; } else { assert(!"Invalid uniform variable type"); } break; case SpvStorageClassPushConstant: var->mode = vtn_variable_mode_push_constant; assert(b->shader->num_uniforms == 0); b->shader->num_uniforms = vtn_type_block_size(var->type) * 4; break; case SpvStorageClassInput: var->mode = vtn_variable_mode_input; nir_mode = nir_var_shader_in; break; case SpvStorageClassOutput: var->mode = vtn_variable_mode_output; nir_mode = nir_var_shader_out; break; case SpvStorageClassPrivate: var->mode = vtn_variable_mode_global; nir_mode = nir_var_global; break; case SpvStorageClassFunction: var->mode = vtn_variable_mode_local; nir_mode = nir_var_local; break; case SpvStorageClassWorkgroup: var->mode = vtn_variable_mode_workgroup; nir_mode = nir_var_shared; break; case SpvStorageClassCrossWorkgroup: case SpvStorageClassGeneric: case SpvStorageClassAtomicCounter: default: unreachable("Unhandled variable storage class"); } switch (var->mode) { case vtn_variable_mode_local: case vtn_variable_mode_global: case vtn_variable_mode_image: case vtn_variable_mode_sampler: case vtn_variable_mode_workgroup: /* For these, we create the variable normally */ var->var = rzalloc(b->shader, nir_variable); var->var->name = ralloc_strdup(var->var, val->name); var->var->type = var->type->type; var->var->data.mode = nir_mode; switch (var->mode) { case vtn_variable_mode_image: case vtn_variable_mode_sampler: var->var->interface_type = without_array->type; break; default: var->var->interface_type = NULL; break; } break; case vtn_variable_mode_input: case vtn_variable_mode_output: { /* For inputs and outputs, we immediately split structures. This * is for a couple of reasons. For one, builtins may all come in * a struct and we really want those split out into separate * variables. For another, interpolation qualifiers can be * applied to members of the top-level struct ane we need to be * able to preserve that information. */ int array_length = -1; struct vtn_type *interface_type = var->type; if (b->shader->stage == MESA_SHADER_GEOMETRY && glsl_type_is_array(var->type->type)) { /* In Geometry shaders (and some tessellation), inputs come * in per-vertex arrays. However, some builtins come in * non-per-vertex, hence the need for the is_array check. In * any case, there are no non-builtin arrays allowed so this * check should be sufficient. */ interface_type = var->type->array_element; array_length = glsl_get_length(var->type->type); } if (glsl_type_is_struct(interface_type->type)) { /* It's a struct. Split it. */ unsigned num_members = glsl_get_length(interface_type->type); var->members = ralloc_array(b, nir_variable *, num_members); for (unsigned i = 0; i < num_members; i++) { const struct glsl_type *mtype = interface_type->members[i]->type; if (array_length >= 0) mtype = glsl_array_type(mtype, array_length); var->members[i] = rzalloc(b->shader, nir_variable); var->members[i]->name = ralloc_asprintf(var->members[i], "%s.%d", val->name, i); var->members[i]->type = mtype; var->members[i]->interface_type = interface_type->members[i]->type; var->members[i]->data.mode = nir_mode; } } else { var->var = rzalloc(b->shader, nir_variable); var->var->name = ralloc_strdup(var->var, val->name); var->var->type = var->type->type; var->var->interface_type = interface_type->type; var->var->data.mode = nir_mode; } /* For inputs and outputs, we need to grab locations and builtin * information from the interface type. */ vtn_foreach_decoration(b, interface_type->val, var_decoration_cb, var); break; case vtn_variable_mode_param: unreachable("Not created through OpVariable"); } case vtn_variable_mode_ubo: case vtn_variable_mode_ssbo: case vtn_variable_mode_push_constant: /* These don't need actual variables. */ break; } if (count > 4) { assert(count == 5); nir_constant *constant = vtn_value(b, w[4], vtn_value_type_constant)->constant; var->var->constant_initializer = nir_constant_clone(constant, var->var); } vtn_foreach_decoration(b, val, var_decoration_cb, var); if (var->mode == vtn_variable_mode_image || var->mode == vtn_variable_mode_sampler) { /* XXX: We still need the binding information in the nir_variable * for these. We should fix that. */ var->var->data.binding = var->binding; var->var->data.descriptor_set = var->descriptor_set; if (var->mode == vtn_variable_mode_image) var->var->data.image.format = without_array->image_format; } if (var->mode == vtn_variable_mode_local) { assert(var->members == NULL && var->var != NULL); nir_function_impl_add_variable(b->impl, var->var); } else if (var->var) { nir_shader_add_variable(b->shader, var->var); } else if (var->members) { unsigned count = glsl_get_length(without_array->type); for (unsigned i = 0; i < count; i++) { assert(var->members[i]->data.mode != nir_var_local); nir_shader_add_variable(b->shader, var->members[i]); } } else { assert(var->mode == vtn_variable_mode_ubo || var->mode == vtn_variable_mode_ssbo || var->mode == vtn_variable_mode_push_constant); } break; }