Path: blob/21.2-virgl/src/panfrost/util/pan_lower_writeout.c
4560 views
/*1* Copyright (C) 2018-2020 Collabora, Ltd.2* Copyright (C) 2019-2020 Icecream953*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "pan_ir.h"25#include "compiler/nir/nir_builder.h"2627/* Midgard can write all of color, depth and stencil in a single writeout28* operation, so we merge depth/stencil stores with color stores.29* If there are no color stores, we add a write to the "depth RT".30*31* For Bifrost, we want these combined so we can properly order32* +ZS_EMIT with respect to +ATEST and +BLEND, as well as combining33* depth/stencil stores into a single +ZS_EMIT op.34*/35bool36pan_nir_lower_zs_store(nir_shader *nir)37{38if (nir->info.stage != MESA_SHADER_FRAGMENT)39return false;4041nir_variable *z_var = NULL, *s_var = NULL;4243nir_foreach_shader_out_variable(var, nir) {44if (var->data.location == FRAG_RESULT_DEPTH)45z_var = var;46else if (var->data.location == FRAG_RESULT_STENCIL)47s_var = var;48}4950if (!z_var && !s_var)51return false;5253bool progress = false;5455nir_foreach_function(function, nir) {56if (!function->impl) continue;5758nir_intrinsic_instr *z_store = NULL, *s_store = NULL;5960nir_foreach_block(block, function->impl) {61nir_foreach_instr_safe(instr, block) {62if (instr->type != nir_instr_type_intrinsic)63continue;6465nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);66if (intr->intrinsic != nir_intrinsic_store_output)67continue;6869if (z_var && nir_intrinsic_base(intr) == z_var->data.driver_location) {70assert(!z_store);71z_store = intr;72}7374if (s_var && nir_intrinsic_base(intr) == s_var->data.driver_location) {75assert(!s_store);76s_store = intr;77}78}79}8081if (!z_store && !s_store) continue;8283bool replaced = false;8485nir_foreach_block(block, function->impl) {86nir_foreach_instr_safe(instr, block) {87if (instr->type != nir_instr_type_intrinsic)88continue;8990nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);91if (intr->intrinsic != nir_intrinsic_store_output)92continue;9394const nir_variable *var = nir_find_variable_with_driver_location(nir, nir_var_shader_out, nir_intrinsic_base(intr));95assert(var);9697if (var->data.location < FRAG_RESULT_DATA0)98continue;99100if (var->data.index)101continue;102103assert(nir_src_is_const(intr->src[1]) && "no indirect outputs");104105nir_builder b;106nir_builder_init(&b, function->impl);107108assert(!z_store || z_store->instr.block == instr->block);109assert(!s_store || s_store->instr.block == instr->block);110b.cursor = nir_after_block_before_jump(instr->block);111112nir_intrinsic_instr *combined_store;113combined_store = nir_intrinsic_instr_create(b.shader, nir_intrinsic_store_combined_output_pan);114115combined_store->num_components = intr->src[0].ssa->num_components;116117nir_intrinsic_set_base(combined_store, nir_intrinsic_base(intr));118nir_intrinsic_set_src_type(combined_store, nir_intrinsic_src_type(intr));119120unsigned writeout = PAN_WRITEOUT_C;121if (z_store)122writeout |= PAN_WRITEOUT_Z;123if (s_store)124writeout |= PAN_WRITEOUT_S;125126nir_intrinsic_set_component(combined_store, writeout);127128struct nir_ssa_def *zero = nir_imm_int(&b, 0);129130struct nir_ssa_def *src[4] = {131intr->src[0].ssa,132intr->src[1].ssa,133z_store ? z_store->src[0].ssa : zero,134s_store ? s_store->src[0].ssa : zero,135};136137for (int i = 0; i < 4; ++i)138combined_store->src[i] = nir_src_for_ssa(src[i]);139140nir_builder_instr_insert(&b, &combined_store->instr);141142nir_instr_remove(instr);143144replaced = true;145}146}147148/* Insert a store to the depth RT (0xff) if needed */149if (!replaced) {150nir_builder b;151nir_builder_init(&b, function->impl);152153nir_block *block = NULL;154if (z_store && s_store)155assert(z_store->instr.block == s_store->instr.block);156157if (z_store)158block = z_store->instr.block;159else160block = s_store->instr.block;161162b.cursor = nir_after_block_before_jump(block);163164nir_intrinsic_instr *combined_store;165combined_store = nir_intrinsic_instr_create(b.shader, nir_intrinsic_store_combined_output_pan);166167combined_store->num_components = 4;168169unsigned base;170if (z_store)171base = nir_intrinsic_base(z_store);172else173base = nir_intrinsic_base(s_store);174nir_intrinsic_set_base(combined_store, base);175nir_intrinsic_set_src_type(combined_store, nir_type_float32);176177unsigned writeout = 0;178if (z_store)179writeout |= PAN_WRITEOUT_Z;180if (s_store)181writeout |= PAN_WRITEOUT_S;182183nir_intrinsic_set_component(combined_store, writeout);184185struct nir_ssa_def *zero = nir_imm_int(&b, 0);186187struct nir_ssa_def *src[4] = {188nir_imm_vec4(&b, 0, 0, 0, 0),189zero,190z_store ? z_store->src[0].ssa : zero,191s_store ? s_store->src[0].ssa : zero,192};193194for (int i = 0; i < 4; ++i)195combined_store->src[i] = nir_src_for_ssa(src[i]);196197nir_builder_instr_insert(&b, &combined_store->instr);198}199200if (z_store)201nir_instr_remove(&z_store->instr);202203if (s_store)204nir_instr_remove(&s_store->instr);205206nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);207progress = true;208}209210return progress;211}212213/* Real writeout stores, which break execution, need to be moved to after214* dual-source stores, which are just standard register writes. */215bool216pan_nir_reorder_writeout(nir_shader *nir)217{218bool progress = false;219220nir_foreach_function(function, nir) {221if (!function->impl) continue;222223nir_foreach_block(block, function->impl) {224nir_instr *last_writeout = NULL;225226nir_foreach_instr_reverse_safe(instr, block) {227if (instr->type != nir_instr_type_intrinsic)228continue;229230nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);231if (intr->intrinsic != nir_intrinsic_store_output)232continue;233234const nir_variable *var = nir_find_variable_with_driver_location(nir, nir_var_shader_out, nir_intrinsic_base(intr));235236if (var->data.index) {237if (!last_writeout)238last_writeout = instr;239continue;240}241242if (!last_writeout)243continue;244245/* This is a real store, so move it to after dual-source stores */246exec_node_remove(&instr->node);247exec_node_insert_after(&last_writeout->node, &instr->node);248249progress = true;250}251}252}253254return progress;255}256257258