From: Timur Kristóf Date: Mon, 30 Mar 2020 13:58:07 +0000 (+0200) Subject: nir: Add new linking helper to set linked driver locations. X-Git-Tag: upstream/20.1.8~567 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7aa61c84fe47f139b96b29d39b3298f30b96c89c;p=platform%2Fupstream%2Fmesa.git nir: Add new linking helper to set linked driver locations. This commit introduces a new function nir_assign_linked_io_var_locations which is intended to help with assigning driver locations to shaders during linking, primarily aimed at the VS->TCS->TES->GS stages. It ensures that the linked shaders have the same driver locations, and it also packs these as close to each other as possible. Signed-off-by: Timur Kristóf Reviewed-by: Rhys Perry Reviewed-by: Samuel Pitoiset Part-of: --- diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 59a034f..9f6d2c5 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -3809,6 +3809,15 @@ void nir_assign_io_var_locations(struct exec_list *var_list, unsigned *size, gl_shader_stage stage); +typedef struct { + uint8_t num_linked_io_vars; + uint8_t num_linked_patch_io_vars; +} nir_linked_io_var_info; + +nir_linked_io_var_info +nir_assign_linked_io_var_locations(nir_shader *producer, + nir_shader *consumer); + typedef enum { /* If set, this causes all 64-bit IO operations to be lowered on-the-fly * to 32-bit operations. This is only valid for nir_var_shader_in/out diff --git a/src/compiler/nir/nir_linking_helpers.c b/src/compiler/nir/nir_linking_helpers.c index 34a6f9a..e626326 100644 --- a/src/compiler/nir/nir_linking_helpers.c +++ b/src/compiler/nir/nir_linking_helpers.c @@ -1201,3 +1201,102 @@ nir_assign_io_var_locations(struct exec_list *var_list, unsigned *size, *size = location; } +static uint64_t +get_linked_variable_location(unsigned location, bool patch) +{ + if (!patch) + return location; + + /* Reserve locations 0...3 for special patch variables + * like tess factors and bounding boxes, and the generic patch + * variables will come after them. + */ + if (location >= VARYING_SLOT_PATCH0) + return location - VARYING_SLOT_PATCH0 + 4; + else if (location >= VARYING_SLOT_TESS_LEVEL_OUTER && + location <= VARYING_SLOT_BOUNDING_BOX1) + return location - VARYING_SLOT_TESS_LEVEL_OUTER; + else + unreachable("Unsupported variable in get_linked_variable_location."); +} + +static uint64_t +get_linked_variable_io_mask(nir_variable *variable, gl_shader_stage stage) +{ + const struct glsl_type *type = variable->type; + + if (nir_is_per_vertex_io(variable, stage)) { + assert(glsl_type_is_array(type)); + type = glsl_get_array_element(type); + } + + unsigned slots = glsl_count_attribute_slots(type, false); + if (variable->data.compact) { + unsigned component_count = variable->data.location_frac + glsl_get_length(type); + slots = DIV_ROUND_UP(component_count, 4); + } + + uint64_t mask = u_bit_consecutive64(0, slots); + return mask; +} + +nir_linked_io_var_info +nir_assign_linked_io_var_locations(nir_shader *producer, nir_shader *consumer) +{ + assert(producer); + assert(consumer); + + uint64_t producer_output_mask = 0; + uint64_t producer_patch_output_mask = 0; + + nir_foreach_variable(variable, &producer->outputs) { + uint64_t mask = get_linked_variable_io_mask(variable, producer->info.stage); + uint64_t loc = get_linked_variable_location(variable->data.location, variable->data.patch); + + if (variable->data.patch) + producer_patch_output_mask |= mask << loc; + else + producer_output_mask |= mask << loc; + } + + uint64_t consumer_input_mask = 0; + uint64_t consumer_patch_input_mask = 0; + + nir_foreach_variable(variable, &consumer->inputs) { + uint64_t mask = get_linked_variable_io_mask(variable, consumer->info.stage); + uint64_t loc = get_linked_variable_location(variable->data.location, variable->data.patch); + + if (variable->data.patch) + consumer_patch_input_mask |= mask << loc; + else + consumer_input_mask |= mask << loc; + } + + uint64_t io_mask = producer_output_mask | consumer_input_mask; + uint64_t patch_io_mask = producer_patch_output_mask | consumer_patch_input_mask; + + nir_foreach_variable(variable, &producer->outputs) { + uint64_t loc = get_linked_variable_location(variable->data.location, variable->data.patch); + + if (variable->data.patch) + variable->data.driver_location = util_bitcount64(patch_io_mask & u_bit_consecutive64(0, loc)) * 4; + else + variable->data.driver_location = util_bitcount64(io_mask & u_bit_consecutive64(0, loc)) * 4; + } + + nir_foreach_variable(variable, &consumer->inputs) { + uint64_t loc = get_linked_variable_location(variable->data.location, variable->data.patch); + + if (variable->data.patch) + variable->data.driver_location = util_bitcount64(patch_io_mask & u_bit_consecutive64(0, loc)) * 4; + else + variable->data.driver_location = util_bitcount64(io_mask & u_bit_consecutive64(0, loc)) * 4; + } + + nir_linked_io_var_info result = { + .num_linked_io_vars = util_bitcount64(io_mask), + .num_linked_patch_io_vars = util_bitcount64(patch_io_mask), + }; + + return result; +}