Path: blob/21.2-virgl/src/intel/vulkan/anv_nir_lower_multiview.c
4547 views
/*1* Copyright © 2016 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "anv_nir.h"24#include "nir/nir_builder.h"25#include "util/debug.h"2627/**28* This file implements the lowering required for VK_KHR_multiview.29*30* When possible, Primitive Replication is used and the shader is modified to31* make gl_Position an array and fill it with values for each view.32*33* Otherwise we implement multiview using instanced rendering. The number of34* instances in each draw call is multiplied by the number of views in the35* subpass. Then, in the shader, we divide gl_InstanceId by the number of36* views and use gl_InstanceId % view_count to compute the actual ViewIndex.37*/3839struct lower_multiview_state {40nir_builder builder;4142uint32_t view_mask;4344nir_ssa_def *instance_id;45nir_ssa_def *view_index;46};4748static nir_ssa_def *49build_instance_id(struct lower_multiview_state *state)50{51assert(state->builder.shader->info.stage == MESA_SHADER_VERTEX);5253if (state->instance_id == NULL) {54nir_builder *b = &state->builder;5556b->cursor = nir_before_block(nir_start_block(b->impl));5758/* We use instancing for implementing multiview. The actual instance id59* is given by dividing instance_id by the number of views in this60* subpass.61*/62state->instance_id =63nir_idiv(b, nir_load_instance_id(b),64nir_imm_int(b, util_bitcount(state->view_mask)));65}6667return state->instance_id;68}6970static nir_ssa_def *71build_view_index(struct lower_multiview_state *state)72{73if (state->view_index == NULL) {74nir_builder *b = &state->builder;7576b->cursor = nir_before_block(nir_start_block(b->impl));7778assert(state->view_mask != 0);79if (util_bitcount(state->view_mask) == 1) {80/* Set the view index directly. */81state->view_index = nir_imm_int(b, ffs(state->view_mask) - 1);82} else if (state->builder.shader->info.stage == MESA_SHADER_VERTEX) {83/* We only support 16 viewports */84assert((state->view_mask & 0xffff0000) == 0);8586/* We use instancing for implementing multiview. The compacted view87* id is given by instance_id % view_count. We then have to convert88* that to an actual view id.89*/90nir_ssa_def *compacted =91nir_umod(b, nir_load_instance_id(b),92nir_imm_int(b, util_bitcount(state->view_mask)));9394if (util_is_power_of_two_or_zero(state->view_mask + 1)) {95/* If we have a full view mask, then compacted is what we want */96state->view_index = compacted;97} else {98/* Now we define a map from compacted view index to the actual99* view index that's based on the view_mask. The map is given by100* 16 nibbles, each of which is a value from 0 to 15.101*/102uint64_t remap = 0;103uint32_t i = 0;104u_foreach_bit(bit, state->view_mask) {105assert(bit < 16);106remap |= (uint64_t)bit << (i++ * 4);107}108109nir_ssa_def *shift = nir_imul(b, compacted, nir_imm_int(b, 4));110111/* One of these days, when we have int64 everywhere, this will be112* easier.113*/114nir_ssa_def *shifted;115if (remap <= UINT32_MAX) {116shifted = nir_ushr(b, nir_imm_int(b, remap), shift);117} else {118nir_ssa_def *shifted_low =119nir_ushr(b, nir_imm_int(b, remap), shift);120nir_ssa_def *shifted_high =121nir_ushr(b, nir_imm_int(b, remap >> 32),122nir_isub(b, shift, nir_imm_int(b, 32)));123shifted = nir_bcsel(b, nir_ilt(b, shift, nir_imm_int(b, 32)),124shifted_low, shifted_high);125}126state->view_index = nir_iand(b, shifted, nir_imm_int(b, 0xf));127}128} else {129const struct glsl_type *type = glsl_int_type();130if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||131b->shader->info.stage == MESA_SHADER_GEOMETRY)132type = glsl_array_type(type, 1, 0);133134nir_variable *idx_var =135nir_variable_create(b->shader, nir_var_shader_in,136type, "view index");137idx_var->data.location = VARYING_SLOT_VIEW_INDEX;138if (b->shader->info.stage == MESA_SHADER_FRAGMENT)139idx_var->data.interpolation = INTERP_MODE_FLAT;140141nir_deref_instr *deref = nir_build_deref_var(b, idx_var);142if (glsl_type_is_array(type))143deref = nir_build_deref_array_imm(b, deref, 0);144145state->view_index = nir_load_deref(b, deref);146}147}148149return state->view_index;150}151152static bool153is_load_view_index(const nir_instr *instr, const void *data)154{155return instr->type == nir_instr_type_intrinsic &&156nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_load_view_index;157}158159static nir_ssa_def *160replace_load_view_index_with_zero(struct nir_builder *b,161nir_instr *instr, void *data)162{163assert(is_load_view_index(instr, data));164return nir_imm_zero(b, 1, 32);165}166167bool168anv_nir_lower_multiview(nir_shader *shader,169struct anv_graphics_pipeline *pipeline)170{171assert(shader->info.stage != MESA_SHADER_COMPUTE);172uint32_t view_mask = pipeline->subpass->view_mask;173174/* If multiview isn't enabled, just lower the ViewIndex builtin to zero. */175if (view_mask == 0) {176return nir_shader_lower_instructions(shader, is_load_view_index,177replace_load_view_index_with_zero, NULL);178}179180/* This pass assumes a single entrypoint */181nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);182183/* Primitive Replication allows a shader to write different positions for184* each view in the same execution. If only the position depends on the185* view, then it is possible to use the feature instead of instancing to186* implement multiview.187*/188if (pipeline->use_primitive_replication) {189if (shader->info.stage == MESA_SHADER_FRAGMENT)190return false;191192bool progress = nir_lower_multiview(shader, pipeline->subpass->view_mask);193194if (progress) {195nir_builder b;196nir_builder_init(&b, entrypoint);197b.cursor = nir_before_cf_list(&entrypoint->body);198199/* Fill Layer ID with zero. Replication will use that as base to200* apply the RTAI offsets.201*/202nir_variable *layer_id_out =203nir_variable_create(shader, nir_var_shader_out,204glsl_int_type(), "layer ID");205layer_id_out->data.location = VARYING_SLOT_LAYER;206nir_store_var(&b, layer_id_out, nir_imm_zero(&b, 1, 32), 0x1);207}208209return progress;210}211212struct lower_multiview_state state = {213.view_mask = view_mask,214};215216nir_builder_init(&state.builder, entrypoint);217218bool progress = false;219nir_foreach_block(block, entrypoint) {220nir_foreach_instr_safe(instr, block) {221if (instr->type != nir_instr_type_intrinsic)222continue;223224nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);225226if (load->intrinsic != nir_intrinsic_load_instance_id &&227load->intrinsic != nir_intrinsic_load_view_index)228continue;229230assert(load->dest.is_ssa);231232nir_ssa_def *value;233if (load->intrinsic == nir_intrinsic_load_instance_id) {234value = build_instance_id(&state);235} else {236assert(load->intrinsic == nir_intrinsic_load_view_index);237value = build_view_index(&state);238}239240nir_ssa_def_rewrite_uses(&load->dest.ssa, value);241242nir_instr_remove(&load->instr);243progress = true;244}245}246247/* The view index is available in all stages but the instance id is only248* available in the VS. If it's not a fragment shader, we need to pass249* the view index on to the next stage.250*/251if (shader->info.stage != MESA_SHADER_FRAGMENT) {252nir_ssa_def *view_index = build_view_index(&state);253254nir_builder *b = &state.builder;255256assert(view_index->parent_instr->block == nir_start_block(entrypoint));257b->cursor = nir_after_instr(view_index->parent_instr);258259/* Unless there is only one possible view index (that would be set260* directly), pass it to the next stage. */261if (util_bitcount(state.view_mask) != 1) {262nir_variable *view_index_out =263nir_variable_create(shader, nir_var_shader_out,264glsl_int_type(), "view index");265view_index_out->data.location = VARYING_SLOT_VIEW_INDEX;266nir_store_var(b, view_index_out, view_index, 0x1);267}268269nir_variable *layer_id_out =270nir_variable_create(shader, nir_var_shader_out,271glsl_int_type(), "layer ID");272layer_id_out->data.location = VARYING_SLOT_LAYER;273nir_store_var(b, layer_id_out, view_index, 0x1);274275progress = true;276}277278if (progress) {279nir_metadata_preserve(entrypoint, nir_metadata_block_index |280nir_metadata_dominance);281}282283return progress;284}285286bool287anv_check_for_primitive_replication(nir_shader **shaders,288struct anv_graphics_pipeline *pipeline)289{290assert(pipeline->base.device->info.ver >= 12);291292static int primitive_replication_max_views = -1;293if (primitive_replication_max_views < 0) {294/* TODO: Figure out why we are not getting same benefits for larger than295* 2 views. For now use Primitive Replication just for the 2-view case296* by default.297*/298const unsigned default_max_views = 2;299300primitive_replication_max_views =301MIN2(MAX_VIEWS_FOR_PRIMITIVE_REPLICATION,302env_var_as_unsigned("ANV_PRIMITIVE_REPLICATION_MAX_VIEWS",303default_max_views));304}305306/* TODO: We should be able to support replication at 'geometry' stages307* later than Vertex. In that case only the last stage can refer to308* gl_ViewIndex.309*/310if (pipeline->active_stages != (VK_SHADER_STAGE_VERTEX_BIT |311VK_SHADER_STAGE_FRAGMENT_BIT)) {312return false;313}314315uint32_t view_mask = pipeline->subpass->view_mask;316int view_count = util_bitcount(view_mask);317if (view_count == 1 || view_count > primitive_replication_max_views)318return false;319320/* We can't access the view index in the fragment shader. */321if (nir_shader_uses_view_index(shaders[MESA_SHADER_FRAGMENT]))322return false;323324return nir_can_lower_multiview(shaders[MESA_SHADER_VERTEX]);325}326327328