assert(!"nir_shader_add_variable cannot be used for local variables");
break;
+ case nir_var_param:
+ assert(!"nir_shader_add_variable cannot be used for function parameters");
+ break;
+
case nir_var_global:
exec_list_push_tail(&shader->globals, &var->node);
break;
impl->params = ralloc_array(function->shader,
nir_variable *, impl->num_params);
+ for (unsigned i = 0; i < impl->num_params; i++) {
+ impl->params[i] = rzalloc(function->shader, nir_variable);
+ impl->params[i]->type = function->params[i].type;
+ impl->params[i]->data.mode = nir_var_param;
+ impl->params[i]->data.location = i;
+ }
+
+ if (!glsl_type_is_void(function->return_type)) {
+ impl->return_var = rzalloc(function->shader, nir_variable);
+ impl->return_var->type = function->return_type;
+ impl->return_var->data.mode = nir_var_param;
+ impl->return_var->data.location = -1;
+ }
+
return impl;
}
nir_var_uniform,
nir_var_shader_storage,
nir_var_shared,
- nir_var_system_value
+ nir_var_system_value,
+ nir_var_param,
} nir_variable_mode;
/**
*
* \sa nir_variable_mode
*/
- nir_variable_mode mode:4;
+ nir_variable_mode mode:5;
/**
* Interpolation mode for shader inputs / outputs
#define nir_foreach_variable(var, var_list) \
foreach_list_typed(nir_variable, var, node, var_list)
+static inline bool
+nir_variable_is_global(const nir_variable *var)
+{
+ return var->data.mode != nir_var_local && var->data.mode != nir_var_param;
+}
+
/**
* Returns the bits in the inputs_read, outputs_written, or
* system_values_read bitfield corresponding to this variable.
static nir_variable *
remap_var(clone_state *state, const nir_variable *var)
{
- return _lookup_ptr(state, var, var->data.mode != nir_var_local);
+ return _lookup_ptr(state, var, nir_variable_is_global(var));
}
nir_constant *
nfi->num_params = fi->num_params;
nfi->params = ralloc_array(state->ns, nir_variable *, fi->num_params);
for (unsigned i = 0; i < fi->num_params; i++) {
- nfi->params[i] = remap_local(state, fi->params[i]);
+ nfi->params[i] = clone_variable(state, fi->params[i]);
}
- nfi->return_var = remap_local(state, fi->return_var);
+ if (fi->return_var)
+ nfi->return_var = clone_variable(state, fi->return_var);
assert(list_empty(&state->phi_srcs));
/* Add copies of all in parameters */
assert(call->num_params == callee_copy->num_params);
for (unsigned i = 0; i < callee_copy->num_params; i++) {
+ nir_variable *param = callee_copy->params[i];
+
+ /* Turn it into a local variable */
+ param->data.mode = nir_var_local;
+ exec_list_push_head(&b->impl->locals, ¶m->node);
+
/* Only in or inout parameters */
if (call->callee->params[i].param_type == nir_parameter_out)
continue;
- nir_copy_deref_var(b, nir_deref_var_create(b->shader,
- callee_copy->params[i]),
+ nir_copy_deref_var(b, nir_deref_var_create(b->shader, param),
call->params[i]);
}
callee_copy->params[i]));
}
if (!glsl_type_is_void(call->callee->return_type)) {
+ /* Turn it into a local variable */
+ callee_copy->return_var->data.mode = nir_var_local;
+ exec_list_push_head(&b->impl->locals, &callee_copy->return_var->node);
+
nir_copy_deref_var(b, call->return_deref,
nir_deref_var_create(b->shader,
callee_copy->return_var));
fprintf(fp, "{\n");
+ for (unsigned i = 0; i < impl->num_params; i++) {
+ fprintf(fp, "\t");
+ print_var_decl(impl->params[i], state);
+ }
+
+ if (impl->return_var) {
+ fprintf(fp, "\t");
+ print_var_decl(impl->return_var, state);
+ }
+
nir_foreach_variable(var, &impl->locals) {
fprintf(fp, "\t");
print_var_decl(var, state);
ralloc_steal(nir, impl);
ralloc_steal(nir, impl->params);
+ for (unsigned i = 0; i < impl->num_params; i++)
+ ralloc_steal(nir, impl->params[i]);
ralloc_steal(nir, impl->return_var);
steal_list(nir, nir_variable, &impl->locals);
steal_list(nir, nir_register, &impl->registers);
static void
validate_var_decl(nir_variable *var, bool is_global, validate_state *state)
{
- assert(is_global != (var->data.mode == nir_var_local));
+ assert(is_global == nir_variable_is_global(var));
/*
* TODO validate some things ir_validate.cpp does (requires more GLSL type
assert(impl->cf_node.parent == NULL);
assert(impl->num_params == impl->function->num_params);
- for (unsigned i = 0; i < impl->num_params; i++)
+ for (unsigned i = 0; i < impl->num_params; i++) {
assert(impl->params[i]->type == impl->function->params[i].type);
+ assert(impl->params[i]->data.location == i);
+ validate_var_decl(impl->params[i], false, state);
+ }
- if (glsl_type_is_void(impl->function->return_type))
+ if (glsl_type_is_void(impl->function->return_type)) {
assert(impl->return_var == NULL);
- else
+ } else {
assert(impl->return_var->type == impl->function->return_type);
+ assert(impl->return_var->data.location == -1);
+ validate_var_decl(impl->return_var, false, state);
+ }
assert(exec_list_is_empty(&impl->end_block->instr_list));
assert(impl->end_block->successors[0] == NULL);
func->return_type = glsl_get_function_return_type(func_type);
b->func->impl = nir_function_impl_create(func);
- if (!glsl_type_is_void(func->return_type)) {
- b->func->impl->return_var =
- nir_local_variable_create(b->func->impl, func->return_type, "ret");
- }
b->func_param_idx = 0;
break;
vtn_push_value(b, w[2], vtn_value_type_access_chain);
assert(b->func_param_idx < b->func->impl->num_params);
- unsigned idx = b->func_param_idx++;
+ nir_variable *param = b->func->impl->params[b->func_param_idx++];
- nir_variable *param =
- nir_local_variable_create(b->func->impl,
- b->func->impl->function->params[idx].type,
- val->name);
- b->func->impl->params[idx] = param;
+ /* Name the parameter so it shows up nicely in NIR */
+ param->name = ralloc_strdup(param, val->name);
struct vtn_variable *vtn_var = rzalloc(b, struct vtn_variable);
vtn_var->mode = vtn_variable_mode_param;