nir: Use an integer index for specifying structure fields
Previously, we used a string name. It was nice for translating out of GLSL IR (which also does that) but cumbersome the rest of the time. Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
This commit is contained in:
@@ -1753,7 +1753,10 @@ nir_visitor::visit(ir_dereference_record *ir)
|
|||||||
{
|
{
|
||||||
ir->record->accept(this);
|
ir->record->accept(this);
|
||||||
|
|
||||||
nir_deref_struct *deref = nir_deref_struct_create(this->shader, ir->field);
|
int field_index = this->deref_tail->type->field_index(ir->field);
|
||||||
|
assert(field_index >= 0);
|
||||||
|
|
||||||
|
nir_deref_struct *deref = nir_deref_struct_create(this->shader, field_index);
|
||||||
deref->deref.type = ir->type;
|
deref->deref.type = ir->type;
|
||||||
this->deref_tail->child = &deref->deref;
|
this->deref_tail->child = &deref->deref;
|
||||||
this->deref_tail = &deref->deref;
|
this->deref_tail = &deref->deref;
|
||||||
|
@@ -529,12 +529,12 @@ nir_deref_array_create(void *mem_ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
nir_deref_struct *
|
nir_deref_struct *
|
||||||
nir_deref_struct_create(void *mem_ctx, const char *field)
|
nir_deref_struct_create(void *mem_ctx, unsigned field_index)
|
||||||
{
|
{
|
||||||
nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
|
nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
|
||||||
deref->deref.deref_type = nir_deref_type_struct;
|
deref->deref.deref_type = nir_deref_type_struct;
|
||||||
deref->deref.child = NULL;
|
deref->deref.child = NULL;
|
||||||
deref->elem = ralloc_strdup(deref, field);
|
deref->index = field_index;
|
||||||
return deref;
|
return deref;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -566,7 +566,7 @@ copy_deref_array(void *mem_ctx, nir_deref_array *deref)
|
|||||||
static nir_deref_struct *
|
static nir_deref_struct *
|
||||||
copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
|
copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
|
||||||
{
|
{
|
||||||
nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->elem);
|
nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
|
||||||
ret->deref.type = deref->deref.type;
|
ret->deref.type = deref->deref.type;
|
||||||
if (deref->deref.child)
|
if (deref->deref.child)
|
||||||
ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
|
ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
|
||||||
|
@@ -650,7 +650,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
nir_deref deref;
|
nir_deref deref;
|
||||||
|
|
||||||
const char *elem;
|
unsigned index;
|
||||||
} nir_deref_struct;
|
} nir_deref_struct;
|
||||||
|
|
||||||
#define nir_deref_as_var(_deref) exec_node_data(nir_deref_var, _deref, deref)
|
#define nir_deref_as_var(_deref) exec_node_data(nir_deref_var, _deref, deref)
|
||||||
@@ -1292,7 +1292,7 @@ nir_ssa_undef_instr *nir_ssa_undef_instr_create(void *mem_ctx);
|
|||||||
|
|
||||||
nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
|
nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
|
||||||
nir_deref_array *nir_deref_array_create(void *mem_ctx);
|
nir_deref_array *nir_deref_array_create(void *mem_ctx);
|
||||||
nir_deref_struct *nir_deref_struct_create(void *mem_ctx, const char *field);
|
nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
|
||||||
|
|
||||||
nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
|
nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
|
||||||
|
|
||||||
|
@@ -47,38 +47,41 @@ get_deref_name_offset(nir_deref_var *deref_var,
|
|||||||
*name = ralloc_strdup(mem_ctx, deref_var->var->name);
|
*name = ralloc_strdup(mem_ctx, deref_var->var->name);
|
||||||
|
|
||||||
while (deref->child != NULL) {
|
while (deref->child != NULL) {
|
||||||
deref = deref->child;
|
switch (deref->child->deref_type) {
|
||||||
switch (deref->deref_type) {
|
case nir_deref_type_array:
|
||||||
case nir_deref_type_array:
|
deref_array = nir_deref_as_array(deref->child);
|
||||||
deref_array = nir_deref_as_array(deref);
|
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
|
||||||
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
|
/* GLSL 1.10 and 1.20 allowed variable sampler array indices,
|
||||||
/* GLSL 1.10 and 1.20 allowed variable sampler array indices,
|
* while GLSL 1.30 requires that the array indices be
|
||||||
* while GLSL 1.30 requires that the array indices be
|
* constant integer expressions. We don't expect any driver
|
||||||
* constant integer expressions. We don't expect any driver
|
* to actually work with a really variable array index, so
|
||||||
* to actually work with a really variable array index, so
|
* all that would work would be an unrolled loop counter that
|
||||||
* all that would work would be an unrolled loop counter that
|
* ends up being constant.
|
||||||
* ends up being constant.
|
*/
|
||||||
*/
|
ralloc_strcat(&shader_program->InfoLog,
|
||||||
ralloc_strcat(&shader_program->InfoLog,
|
"warning: Variable sampler array index unsupported.\n"
|
||||||
"warning: Variable sampler array index unsupported.\n"
|
"This feature of the language was removed in GLSL 1.20 "
|
||||||
"This feature of the language was removed in GLSL 1.20 "
|
"and is unlikely to be supported for 1.10 in Mesa.\n");
|
||||||
"and is unlikely to be supported for 1.10 in Mesa.\n");
|
}
|
||||||
}
|
if (deref_array->deref.child == NULL) {
|
||||||
if (deref->child == NULL) {
|
return deref_array->base_offset;
|
||||||
return deref_array->base_offset;
|
}
|
||||||
}
|
ralloc_asprintf_append(name, "[%u]", deref_array->base_offset);
|
||||||
ralloc_asprintf_append(name, "[%u]", deref_array->base_offset);
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case nir_deref_type_struct:
|
case nir_deref_type_struct: {
|
||||||
deref_struct = nir_deref_as_struct(deref);
|
deref_struct = nir_deref_as_struct(deref->child);
|
||||||
ralloc_asprintf_append(name, ".%s", deref_struct->elem);
|
const char *field = glsl_get_struct_elem_name(deref->type,
|
||||||
break;
|
deref_struct->index);
|
||||||
|
ralloc_asprintf_append(name, ".%s", field);
|
||||||
default:
|
break;
|
||||||
assert(0);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
deref = deref->child;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@@ -51,7 +51,7 @@ type_size(const struct glsl_type *type)
|
|||||||
case GLSL_TYPE_STRUCT:
|
case GLSL_TYPE_STRUCT:
|
||||||
size = 0;
|
size = 0;
|
||||||
for (i = 0; i < glsl_get_length(type); i++) {
|
for (i = 0; i < glsl_get_length(type); i++) {
|
||||||
size += type_size(glsl_get_struct_elem_type(type, i));
|
size += type_size(glsl_get_struct_field(type, i));
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
case GLSL_TYPE_SAMPLER:
|
case GLSL_TYPE_SAMPLER:
|
||||||
@@ -267,12 +267,8 @@ get_deref_offset(nir_deref_var *deref_var, nir_instr *instr,
|
|||||||
} else {
|
} else {
|
||||||
nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
|
nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
|
||||||
|
|
||||||
unsigned i = 0;
|
for (unsigned i = 0; i < deref_struct->index; i++)
|
||||||
while(strcmp(glsl_get_struct_elem_name(parent_type, i),
|
base_offset += type_size(glsl_get_struct_field(parent_type, i));
|
||||||
deref_struct->elem) != 0) {
|
|
||||||
base_offset += type_size(glsl_get_struct_elem_type(parent_type, i));
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,7 +407,7 @@ reg_const_load(nir_reg_dest reg, nir_constant *constant,
|
|||||||
|
|
||||||
case GLSL_TYPE_STRUCT:
|
case GLSL_TYPE_STRUCT:
|
||||||
for (unsigned i = 0; i < glsl_get_length(type); i++) {
|
for (unsigned i = 0; i < glsl_get_length(type); i++) {
|
||||||
const struct glsl_type *field = glsl_get_struct_elem_type(type, i);
|
const struct glsl_type *field = glsl_get_struct_field(type, i);
|
||||||
nir_reg_dest new_reg = reg;
|
nir_reg_dest new_reg = reg;
|
||||||
new_reg.base_offset += offset;
|
new_reg.base_offset += offset;
|
||||||
reg_const_load(new_reg, constant->elements[i], field, impl,
|
reg_const_load(new_reg, constant->elements[i], field, impl,
|
||||||
@@ -528,14 +524,10 @@ var_reg_block_copy_impl(nir_reg_src reg, nir_deref_var *deref_head,
|
|||||||
case GLSL_TYPE_STRUCT:
|
case GLSL_TYPE_STRUCT:
|
||||||
offset = 0;
|
offset = 0;
|
||||||
for (unsigned i = 0; i < glsl_get_length(type); i++) {
|
for (unsigned i = 0; i < glsl_get_length(type); i++) {
|
||||||
const struct glsl_type *field_type =
|
const struct glsl_type *field_type = glsl_get_struct_field(type, i);
|
||||||
glsl_get_struct_elem_type(type, i);
|
|
||||||
const char *field_name = glsl_get_struct_elem_name(type, i);
|
|
||||||
|
|
||||||
nir_deref_struct *deref_struct =
|
nir_deref_struct *deref_struct = nir_deref_struct_create(mem_ctx, i);
|
||||||
nir_deref_struct_create(mem_ctx, field_name);
|
|
||||||
deref_struct->deref.type = field_type;
|
deref_struct->deref.type = field_type;
|
||||||
deref_struct->elem = field_name;
|
|
||||||
|
|
||||||
nir_deref_var *new_deref_head =
|
nir_deref_var *new_deref_head =
|
||||||
nir_deref_as_var(nir_copy_deref(mem_ctx, &deref_head->deref));
|
nir_deref_as_var(nir_copy_deref(mem_ctx, &deref_head->deref));
|
||||||
|
@@ -282,33 +282,42 @@ print_deref_array(nir_deref_array *deref, print_var_state *state, FILE *fp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_deref_struct(nir_deref_struct *deref, print_var_state *state, FILE *fp)
|
print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
|
||||||
|
print_var_state *state, FILE *fp)
|
||||||
{
|
{
|
||||||
fprintf(fp, ".%s", deref->elem);
|
fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_deref(nir_deref *deref, print_var_state *state, FILE *fp)
|
print_deref(nir_deref_var *deref, print_var_state *state, FILE *fp)
|
||||||
{
|
{
|
||||||
while (deref != NULL) {
|
nir_deref *tail = &deref->deref;
|
||||||
switch (deref->deref_type) {
|
nir_deref *pretail = NULL;
|
||||||
|
while (tail != NULL) {
|
||||||
|
switch (tail->deref_type) {
|
||||||
case nir_deref_type_var:
|
case nir_deref_type_var:
|
||||||
print_deref_var(nir_deref_as_var(deref), state, fp);
|
assert(pretail == NULL);
|
||||||
|
assert(tail == &deref->deref);
|
||||||
|
print_deref_var(deref, state, fp);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case nir_deref_type_array:
|
case nir_deref_type_array:
|
||||||
print_deref_array(nir_deref_as_array(deref), state, fp);
|
assert(pretail != NULL);
|
||||||
|
print_deref_array(nir_deref_as_array(tail), state, fp);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case nir_deref_type_struct:
|
case nir_deref_type_struct:
|
||||||
print_deref_struct(nir_deref_as_struct(deref), state, fp);
|
assert(pretail != NULL);
|
||||||
|
print_deref_struct(nir_deref_as_struct(tail),
|
||||||
|
pretail->type, state, fp);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
unreachable("Invalid deref type");
|
unreachable("Invalid deref type");
|
||||||
}
|
}
|
||||||
|
|
||||||
deref = deref->child;
|
pretail = tail;
|
||||||
|
tail = pretail->child;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,7 +359,7 @@ print_intrinsic_instr(nir_intrinsic_instr *instr, print_var_state *state,
|
|||||||
if (!first)
|
if (!first)
|
||||||
fprintf(fp, ", ");
|
fprintf(fp, ", ");
|
||||||
|
|
||||||
print_deref(&instr->variables[i]->deref, state, fp);
|
print_deref(instr->variables[i], state, fp);
|
||||||
|
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
@@ -485,7 +494,7 @@ print_tex_instr(nir_tex_instr *instr, print_var_state *state, FILE *fp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (instr->sampler) {
|
if (instr->sampler) {
|
||||||
print_deref(&instr->sampler->deref, state, fp);
|
print_deref(instr->sampler, state, fp);
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "%u", instr->sampler_index);
|
fprintf(fp, "%u", instr->sampler_index);
|
||||||
}
|
}
|
||||||
@@ -508,14 +517,14 @@ print_call_instr(nir_call_instr *instr, print_var_state *state, FILE *fp)
|
|||||||
if (i != 0)
|
if (i != 0)
|
||||||
fprintf(fp, ", ");
|
fprintf(fp, ", ");
|
||||||
|
|
||||||
print_deref(&instr->params[i]->deref, state, fp);
|
print_deref(instr->params[i], state, fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instr->return_deref != NULL) {
|
if (instr->return_deref != NULL) {
|
||||||
if (instr->num_params != 0)
|
if (instr->num_params != 0)
|
||||||
fprintf(fp, ", ");
|
fprintf(fp, ", ");
|
||||||
fprintf(fp, "returning ");
|
fprintf(fp, "returning ");
|
||||||
print_deref(&instr->return_deref->deref, state, fp);
|
print_deref(instr->return_deref, state, fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -65,15 +65,9 @@ glsl_get_array_element(const glsl_type* type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const glsl_type *
|
const glsl_type *
|
||||||
glsl_get_struct_field(const glsl_type *type, const char *field)
|
glsl_get_struct_field(const glsl_type *type, unsigned index)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < type->length; i++) {
|
return type->fields.structure[index].type;
|
||||||
if (strcmp(type->fields.structure[i].name, field) == 0) {
|
|
||||||
return type->fields.structure[i].type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct glsl_type *
|
const struct glsl_type *
|
||||||
@@ -112,12 +106,6 @@ glsl_get_length(const struct glsl_type *type)
|
|||||||
return type->length;
|
return type->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct glsl_type *
|
|
||||||
glsl_get_struct_elem_type(const struct glsl_type *type, unsigned index)
|
|
||||||
{
|
|
||||||
return type->fields.structure[index].type;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
|
glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
|
||||||
{
|
{
|
||||||
|
@@ -43,7 +43,7 @@ void glsl_print_type(const struct glsl_type *type, FILE *fp);
|
|||||||
void glsl_print_struct(const struct glsl_type *type, FILE *fp);
|
void glsl_print_struct(const struct glsl_type *type, FILE *fp);
|
||||||
|
|
||||||
const struct glsl_type *glsl_get_struct_field(const struct glsl_type *type,
|
const struct glsl_type *glsl_get_struct_field(const struct glsl_type *type,
|
||||||
const char *field);
|
unsigned index);
|
||||||
|
|
||||||
const struct glsl_type *glsl_get_array_element(const struct glsl_type *type);
|
const struct glsl_type *glsl_get_array_element(const struct glsl_type *type);
|
||||||
|
|
||||||
@@ -59,9 +59,6 @@ unsigned glsl_get_matrix_columns(const struct glsl_type *type);
|
|||||||
|
|
||||||
unsigned glsl_get_length(const struct glsl_type *type);
|
unsigned glsl_get_length(const struct glsl_type *type);
|
||||||
|
|
||||||
const struct glsl_type *glsl_get_struct_elem_type(const struct glsl_type *type,
|
|
||||||
unsigned index);
|
|
||||||
|
|
||||||
const char *glsl_get_struct_elem_name(const struct glsl_type *type,
|
const char *glsl_get_struct_elem_name(const struct glsl_type *type,
|
||||||
unsigned index);
|
unsigned index);
|
||||||
|
|
||||||
|
@@ -275,7 +275,7 @@ validate_deref_chain(nir_deref *deref, validate_state *state)
|
|||||||
case nir_deref_type_struct:
|
case nir_deref_type_struct:
|
||||||
assert(deref->type ==
|
assert(deref->type ==
|
||||||
glsl_get_struct_field(parent->type,
|
glsl_get_struct_field(parent->type,
|
||||||
nir_deref_as_struct(deref)->elem));
|
nir_deref_as_struct(deref)->index));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case nir_deref_type_var:
|
case nir_deref_type_var:
|
||||||
|
Reference in New Issue
Block a user