2014-12-02 06:01:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Connor Abbott (cwabbott0@gmail.com)
|
|
|
|
* Jason Ekstrand (jason@jlekstrand.net)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This lowering pass converts references to input/output variables with
|
|
|
|
* loads/stores to actual input/output intrinsics.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nir.h"
|
2015-08-12 19:26:34 +01:00
|
|
|
#include "nir_builder.h"
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
struct lower_io_state {
|
2015-08-12 19:26:34 +01:00
|
|
|
nir_builder builder;
|
2014-12-02 06:01:05 +00:00
|
|
|
void *mem_ctx;
|
2015-08-12 22:29:25 +01:00
|
|
|
int (*type_size)(const struct glsl_type *type);
|
2015-08-12 23:14:35 +01:00
|
|
|
nir_variable_mode mode;
|
2014-12-02 06:01:05 +00:00
|
|
|
};
|
|
|
|
|
2015-03-18 22:04:15 +00:00
|
|
|
void
|
2015-08-12 22:29:25 +01:00
|
|
|
nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
|
|
|
|
int (*type_size)(const struct glsl_type *))
|
2014-12-02 06:01:05 +00:00
|
|
|
{
|
|
|
|
unsigned location = 0;
|
|
|
|
|
2015-10-03 02:15:06 +01:00
|
|
|
nir_foreach_variable(var, var_list) {
|
2014-12-02 06:01:05 +00:00
|
|
|
/*
|
|
|
|
* UBO's have their own address spaces, so don't count them towards the
|
|
|
|
* number of global uniforms
|
|
|
|
*/
|
2015-05-18 14:47:18 +01:00
|
|
|
if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
|
|
|
|
var->interface_type != NULL)
|
2014-12-02 06:01:05 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
var->data.driver_location = location;
|
2015-08-12 22:29:25 +01:00
|
|
|
location += type_size(var->type);
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*size = location;
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:46:19 +01:00
|
|
|
/**
|
|
|
|
* Returns true if we're processing a stage whose inputs are arrays indexed
|
|
|
|
* by a vertex number (such as geometry shader inputs).
|
|
|
|
*/
|
|
|
|
static bool
|
2015-10-01 01:17:35 +01:00
|
|
|
is_per_vertex_input(struct lower_io_state *state, nir_variable *var)
|
2015-10-01 08:46:19 +01:00
|
|
|
{
|
|
|
|
gl_shader_stage stage = state->builder.shader->stage;
|
2015-10-01 01:17:35 +01:00
|
|
|
|
|
|
|
return var->data.mode == nir_var_shader_in && !var->data.patch &&
|
|
|
|
(stage == MESA_SHADER_TESS_CTRL ||
|
|
|
|
stage == MESA_SHADER_TESS_EVAL ||
|
|
|
|
stage == MESA_SHADER_GEOMETRY);
|
2015-10-01 08:46:19 +01:00
|
|
|
}
|
|
|
|
|
2014-12-02 06:01:05 +00:00
|
|
|
static unsigned
|
2015-10-01 08:36:25 +01:00
|
|
|
get_io_offset(nir_deref_var *deref, nir_instr *instr,
|
2015-10-01 08:46:19 +01:00
|
|
|
nir_ssa_def **vertex_index,
|
2015-10-01 08:36:25 +01:00
|
|
|
nir_ssa_def **out_indirect,
|
2014-12-02 06:01:05 +00:00
|
|
|
struct lower_io_state *state)
|
|
|
|
{
|
2015-10-01 08:36:25 +01:00
|
|
|
nir_ssa_def *indirect = NULL;
|
2014-12-02 06:01:05 +00:00
|
|
|
unsigned base_offset = 0;
|
|
|
|
|
2015-08-12 19:26:34 +01:00
|
|
|
nir_builder *b = &state->builder;
|
2015-08-06 15:16:07 +01:00
|
|
|
b->cursor = nir_before_instr(instr);
|
2015-08-12 19:26:34 +01:00
|
|
|
|
2014-12-02 06:01:05 +00:00
|
|
|
nir_deref *tail = &deref->deref;
|
2015-10-01 08:46:19 +01:00
|
|
|
|
|
|
|
/* For per-vertex input arrays (i.e. geometry shader inputs), keep the
|
|
|
|
* outermost array index separate. Process the rest normally.
|
|
|
|
*/
|
|
|
|
if (vertex_index != NULL) {
|
|
|
|
tail = tail->child;
|
|
|
|
assert(tail->deref_type == nir_deref_type_array);
|
|
|
|
nir_deref_array *deref_array = nir_deref_as_array(tail);
|
|
|
|
|
|
|
|
nir_ssa_def *vtx = nir_imm_int(b, deref_array->base_offset);
|
|
|
|
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
|
|
|
|
vtx = nir_iadd(b, vtx, nir_ssa_for_src(b, deref_array->indirect, 1));
|
|
|
|
}
|
|
|
|
*vertex_index = vtx;
|
|
|
|
}
|
|
|
|
|
2014-12-02 06:01:05 +00:00
|
|
|
while (tail->child != NULL) {
|
|
|
|
const struct glsl_type *parent_type = tail->type;
|
|
|
|
tail = tail->child;
|
|
|
|
|
|
|
|
if (tail->deref_type == nir_deref_type_array) {
|
|
|
|
nir_deref_array *deref_array = nir_deref_as_array(tail);
|
2015-08-12 22:29:25 +01:00
|
|
|
unsigned size = state->type_size(tail->type);
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
base_offset += size * deref_array->base_offset;
|
|
|
|
|
|
|
|
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
|
2015-08-12 19:26:34 +01:00
|
|
|
nir_ssa_def *mul =
|
|
|
|
nir_imul(b, nir_imm_int(b, size),
|
|
|
|
nir_ssa_for_src(b, deref_array->indirect, 1));
|
2014-12-02 06:01:05 +00:00
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
indirect = indirect ? nir_iadd(b, indirect, mul) : mul;
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
} else if (tail->deref_type == nir_deref_type_struct) {
|
|
|
|
nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
|
|
|
|
|
2015-08-12 22:29:25 +01:00
|
|
|
for (unsigned i = 0; i < deref_struct->index; i++) {
|
|
|
|
base_offset +=
|
|
|
|
state->type_size(glsl_get_struct_field(parent_type, i));
|
|
|
|
}
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
*out_indirect = indirect;
|
2014-12-02 06:01:05 +00:00
|
|
|
return base_offset;
|
|
|
|
}
|
|
|
|
|
2015-08-12 18:57:31 +01:00
|
|
|
static nir_intrinsic_op
|
2015-10-01 08:46:19 +01:00
|
|
|
load_op(struct lower_io_state *state,
|
|
|
|
nir_variable_mode mode, bool per_vertex, bool has_indirect)
|
2015-08-12 18:57:31 +01:00
|
|
|
{
|
|
|
|
nir_intrinsic_op op;
|
|
|
|
switch (mode) {
|
|
|
|
case nir_var_shader_in:
|
2015-10-01 08:46:19 +01:00
|
|
|
if (per_vertex) {
|
|
|
|
op = has_indirect ? nir_intrinsic_load_per_vertex_input_indirect :
|
|
|
|
nir_intrinsic_load_per_vertex_input;
|
|
|
|
} else {
|
|
|
|
op = has_indirect ? nir_intrinsic_load_input_indirect :
|
|
|
|
nir_intrinsic_load_input;
|
|
|
|
}
|
2015-08-12 18:57:31 +01:00
|
|
|
break;
|
|
|
|
case nir_var_uniform:
|
|
|
|
op = has_indirect ? nir_intrinsic_load_uniform_indirect :
|
|
|
|
nir_intrinsic_load_uniform;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable("Unknown variable mode");
|
|
|
|
}
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2014-12-02 06:01:05 +00:00
|
|
|
static bool
|
|
|
|
nir_lower_io_block(nir_block *block, void *void_state)
|
|
|
|
{
|
|
|
|
struct lower_io_state *state = void_state;
|
|
|
|
|
|
|
|
nir_foreach_instr_safe(block, instr) {
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
|
|
|
|
2015-08-12 23:14:35 +01:00
|
|
|
if (intrin->intrinsic != nir_intrinsic_load_var &&
|
|
|
|
intrin->intrinsic != nir_intrinsic_store_var)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nir_variable_mode mode = intrin->variables[0]->var->data.mode;
|
|
|
|
|
|
|
|
if (state->mode != -1 && state->mode != mode)
|
|
|
|
continue;
|
|
|
|
|
2014-12-02 06:01:05 +00:00
|
|
|
switch (intrin->intrinsic) {
|
2014-12-04 01:03:19 +00:00
|
|
|
case nir_intrinsic_load_var: {
|
2014-12-02 06:01:05 +00:00
|
|
|
if (mode != nir_var_shader_in && mode != nir_var_uniform)
|
|
|
|
continue;
|
|
|
|
|
2015-10-01 01:17:35 +01:00
|
|
|
bool per_vertex =
|
|
|
|
is_per_vertex_input(state, intrin->variables[0]->var);
|
2015-10-01 08:46:19 +01:00
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
nir_ssa_def *indirect;
|
2015-10-01 08:46:19 +01:00
|
|
|
nir_ssa_def *vertex_index;
|
2015-10-01 08:36:25 +01:00
|
|
|
|
|
|
|
unsigned offset = get_io_offset(intrin->variables[0], &intrin->instr,
|
2015-10-01 08:46:19 +01:00
|
|
|
per_vertex ? &vertex_index : NULL,
|
2015-10-01 08:36:25 +01:00
|
|
|
&indirect, state);
|
2014-12-02 06:01:05 +00:00
|
|
|
|
2015-08-12 18:57:31 +01:00
|
|
|
nir_intrinsic_instr *load =
|
|
|
|
nir_intrinsic_instr_create(state->mem_ctx,
|
2015-10-01 08:46:19 +01:00
|
|
|
load_op(state, mode, per_vertex,
|
|
|
|
indirect));
|
2014-12-04 01:03:19 +00:00
|
|
|
load->num_components = intrin->num_components;
|
2014-12-02 06:01:05 +00:00
|
|
|
|
2015-08-18 19:20:40 +01:00
|
|
|
unsigned location = intrin->variables[0]->var->data.driver_location;
|
|
|
|
if (mode == nir_var_uniform) {
|
|
|
|
load->const_index[0] = location;
|
|
|
|
load->const_index[1] = offset;
|
|
|
|
} else {
|
|
|
|
load->const_index[0] = location + offset;
|
|
|
|
}
|
2014-12-02 06:01:05 +00:00
|
|
|
|
2015-10-01 08:46:19 +01:00
|
|
|
if (per_vertex)
|
|
|
|
load->src[0] = nir_src_for_ssa(vertex_index);
|
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
if (indirect)
|
2015-10-01 08:46:19 +01:00
|
|
|
load->src[per_vertex ? 1 : 0] = nir_src_for_ssa(indirect);
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
if (intrin->dest.is_ssa) {
|
2015-01-21 00:23:51 +00:00
|
|
|
nir_ssa_dest_init(&load->instr, &load->dest,
|
|
|
|
intrin->num_components, NULL);
|
2015-01-21 19:11:03 +00:00
|
|
|
nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
|
2015-09-09 21:24:35 +01:00
|
|
|
nir_src_for_ssa(&load->dest.ssa));
|
2014-12-02 06:01:05 +00:00
|
|
|
} else {
|
2015-01-24 00:57:40 +00:00
|
|
|
nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nir_instr_insert_before(&intrin->instr, &load->instr);
|
|
|
|
nir_instr_remove(&intrin->instr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:03:19 +00:00
|
|
|
case nir_intrinsic_store_var: {
|
2014-12-02 06:01:05 +00:00
|
|
|
if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
|
|
|
|
continue;
|
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
nir_ssa_def *indirect;
|
|
|
|
|
|
|
|
unsigned offset = get_io_offset(intrin->variables[0], &intrin->instr,
|
2015-10-01 08:46:19 +01:00
|
|
|
NULL, &indirect, state);
|
2015-10-01 08:36:25 +01:00
|
|
|
offset += intrin->variables[0]->var->data.driver_location;
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
nir_intrinsic_op store_op;
|
2015-10-01 08:36:25 +01:00
|
|
|
if (indirect) {
|
2014-12-04 01:03:19 +00:00
|
|
|
store_op = nir_intrinsic_store_output_indirect;
|
2014-12-02 06:01:05 +00:00
|
|
|
} else {
|
2014-12-04 01:03:19 +00:00
|
|
|
store_op = nir_intrinsic_store_output;
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
|
|
|
|
store_op);
|
2014-12-04 01:03:19 +00:00
|
|
|
store->num_components = intrin->num_components;
|
2014-12-02 06:01:05 +00:00
|
|
|
store->const_index[0] = offset;
|
|
|
|
|
2015-09-09 21:18:29 +01:00
|
|
|
nir_src_copy(&store->src[0], &intrin->src[0], store);
|
2014-12-02 06:01:05 +00:00
|
|
|
|
2015-10-01 08:36:25 +01:00
|
|
|
if (indirect)
|
|
|
|
store->src[1] = nir_src_for_ssa(indirect);
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
nir_instr_insert_before(&intrin->instr, &store->instr);
|
|
|
|
nir_instr_remove(&intrin->instr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-08-12 23:14:35 +01:00
|
|
|
nir_lower_io_impl(nir_function_impl *impl,
|
|
|
|
nir_variable_mode mode,
|
|
|
|
int (*type_size)(const struct glsl_type *))
|
2014-12-02 06:01:05 +00:00
|
|
|
{
|
|
|
|
struct lower_io_state state;
|
|
|
|
|
2015-08-12 19:26:34 +01:00
|
|
|
nir_builder_init(&state.builder, impl);
|
2014-12-02 06:01:05 +00:00
|
|
|
state.mem_ctx = ralloc_parent(impl);
|
2015-08-12 23:14:35 +01:00
|
|
|
state.mode = mode;
|
2015-08-12 22:29:25 +01:00
|
|
|
state.type_size = type_size;
|
2014-12-02 06:01:05 +00:00
|
|
|
|
|
|
|
nir_foreach_block(impl, nir_lower_io_block, &state);
|
2014-12-13 00:25:38 +00:00
|
|
|
|
|
|
|
nir_metadata_preserve(impl, nir_metadata_block_index |
|
|
|
|
nir_metadata_dominance);
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-12 23:14:35 +01:00
|
|
|
nir_lower_io(nir_shader *shader, nir_variable_mode mode,
|
|
|
|
int (*type_size)(const struct glsl_type *))
|
2014-12-02 06:01:05 +00:00
|
|
|
{
|
|
|
|
nir_foreach_overload(shader, overload) {
|
|
|
|
if (overload->impl)
|
2015-08-12 23:14:35 +01:00
|
|
|
nir_lower_io_impl(overload->impl, mode, type_size);
|
2014-12-02 06:01:05 +00:00
|
|
|
}
|
|
|
|
}
|