1/* 2 * Copyright © 2022 Imagination Technologies Ltd. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * 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 THE 21 * SOFTWARE. 22 */ 23 24#include "nir/nir.h" 25#include "nir/nir_builder.h" 26#include "nir/nir_search_helpers.h" 27#include "rogue_nir.h" 28 29static void insert_pfo(nir_builder *b, 30 nir_intrinsic_instr *store_output, 31 nir_src *output_src) 32{ 33 /* TODO: Support complex PFO with blending. */ 34 /* TODO: Verify type is vec4. */ 35 36 /* Pack the output color components into U8888 format. */ 37 nir_ssa_def *new_output_src_ssa = nir_pack_unorm_4x8(b, output_src->ssa); 38 nir_src new_output_src = nir_src_for_ssa(new_output_src_ssa); 39 40 /* Update the store_output intrinsic. */ 41 nir_instr_rewrite_src(&store_output->instr, output_src, new_output_src); 42 nir_intrinsic_set_write_mask(store_output, 1); 43 store_output->num_components = 1; 44 nir_intrinsic_set_src_type(store_output, nir_type_uint32); 45} 46 47void rogue_nir_pfo(nir_shader *shader) 48{ 49 nir_function_impl *impl = nir_shader_get_entrypoint(shader); 50 nir_builder b; 51 52 /* Only apply to fragment shaders. */ 53 if (shader->info.stage != MESA_SHADER_FRAGMENT) 54 return; 55 56 nir_builder_init(&b, impl); 57 58 nir_foreach_block (block, impl) { 59 nir_foreach_instr_safe (instr, block) { 60 if (instr->type == nir_instr_type_intrinsic) { 61 /* Find the store_output intrinsic and pack the output value. */ 62 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 63 64 if (intr->intrinsic != nir_intrinsic_store_output) 65 continue; 66 67 b.cursor = nir_before_instr(&intr->instr); 68 insert_pfo(&b, intr, &intr->src[0]); 69 } else if (instr->type == nir_instr_type_deref) { 70 /* Find variable derefs and update their type. */ 71 nir_deref_instr *deref = nir_instr_as_deref(instr); 72 73 if (!nir_deref_mode_is(deref, nir_var_shader_out)) 74 continue; 75 76 if (deref->deref_type != nir_deref_type_var) 77 continue; 78 79 nir_variable *out = nir_deref_instr_get_variable(deref); 80 81 deref->type = glsl_uintN_t_type(32); 82 out->type = glsl_uintN_t_type(32); 83 } 84 } 85 } 86} 87