1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "nir.h" 25bf215546Sopenharmony_ci#include "nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_cistatic bool 28bf215546Sopenharmony_cilower_single_sampled_instr(nir_builder *b, 29bf215546Sopenharmony_ci nir_instr *instr, 30bf215546Sopenharmony_ci UNUSED void *cb_data) 31bf215546Sopenharmony_ci{ 32bf215546Sopenharmony_ci if (instr->type != nir_instr_type_intrinsic) 33bf215546Sopenharmony_ci return false; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci nir_ssa_def *lowered; 38bf215546Sopenharmony_ci switch (intrin->intrinsic) { 39bf215546Sopenharmony_ci case nir_intrinsic_load_sample_id: 40bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 41bf215546Sopenharmony_ci lowered = nir_imm_int(b, 0); 42bf215546Sopenharmony_ci break; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci case nir_intrinsic_load_sample_pos: 45bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 46bf215546Sopenharmony_ci lowered = nir_imm_vec2(b, 0.5, 0.5); 47bf215546Sopenharmony_ci break; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci case nir_intrinsic_load_sample_mask_in: 50bf215546Sopenharmony_ci /* Don't lower to helper invocations if helper invocations are going 51bf215546Sopenharmony_ci * to be lowered right back to sample mask. 52bf215546Sopenharmony_ci */ 53bf215546Sopenharmony_ci if (b->shader->options->lower_helper_invocation) 54bf215546Sopenharmony_ci return false; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 57bf215546Sopenharmony_ci lowered = nir_b2i32(b, nir_inot(b, nir_load_helper_invocation(b, 1))); 58bf215546Sopenharmony_ci break; 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci case nir_intrinsic_interp_deref_at_centroid: 61bf215546Sopenharmony_ci case nir_intrinsic_interp_deref_at_sample: 62bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 63bf215546Sopenharmony_ci assert(intrin->src[0].is_ssa); 64bf215546Sopenharmony_ci lowered = nir_load_deref(b, nir_src_as_deref(intrin->src[0])); 65bf215546Sopenharmony_ci break; 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci case nir_intrinsic_load_barycentric_centroid: 68bf215546Sopenharmony_ci case nir_intrinsic_load_barycentric_sample: 69bf215546Sopenharmony_ci case nir_intrinsic_load_barycentric_at_sample: 70bf215546Sopenharmony_ci b->cursor = nir_before_instr(instr); 71bf215546Sopenharmony_ci lowered = nir_load_barycentric(b, nir_intrinsic_load_barycentric_pixel, 72bf215546Sopenharmony_ci nir_intrinsic_interp_mode(intrin)); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci if (nir_intrinsic_interp_mode(intrin) == INTERP_MODE_NOPERSPECTIVE) { 75bf215546Sopenharmony_ci BITSET_SET(b->shader->info.system_values_read, 76bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL); 77bf215546Sopenharmony_ci } else { 78bf215546Sopenharmony_ci BITSET_SET(b->shader->info.system_values_read, 79bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL); 80bf215546Sopenharmony_ci } 81bf215546Sopenharmony_ci break; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci default: 84bf215546Sopenharmony_ci return false; 85bf215546Sopenharmony_ci } 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci nir_ssa_def_rewrite_uses(&intrin->dest.ssa, lowered); 88bf215546Sopenharmony_ci nir_instr_remove(instr); 89bf215546Sopenharmony_ci return true; 90bf215546Sopenharmony_ci} 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci/* Assume the fragment shader is single-sampled and lower accordingly 93bf215546Sopenharmony_ci * 94bf215546Sopenharmony_ci * This drops sample/centroid qualifiers from all input variables, forces 95bf215546Sopenharmony_ci * barycentrics to pixel, and constant-folds various built-ins. 96bf215546Sopenharmony_ci */ 97bf215546Sopenharmony_cibool 98bf215546Sopenharmony_cinir_lower_single_sampled(nir_shader *shader) 99bf215546Sopenharmony_ci{ 100bf215546Sopenharmony_ci assert(shader->info.stage == MESA_SHADER_FRAGMENT); 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci bool progress = false; 103bf215546Sopenharmony_ci nir_foreach_shader_in_variable(var, shader) { 104bf215546Sopenharmony_ci if (var->data.sample) { 105bf215546Sopenharmony_ci var->data.sample = false; 106bf215546Sopenharmony_ci progress = true; 107bf215546Sopenharmony_ci } 108bf215546Sopenharmony_ci if (var->data.centroid) { 109bf215546Sopenharmony_ci var->data.centroid = false; 110bf215546Sopenharmony_ci progress = true; 111bf215546Sopenharmony_ci } 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci /* We're going to get rid of any uses of these */ 115bf215546Sopenharmony_ci BITSET_CLEAR(shader->info.system_values_read, 116bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE); 117bf215546Sopenharmony_ci BITSET_CLEAR(shader->info.system_values_read, 118bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID); 119bf215546Sopenharmony_ci BITSET_CLEAR(shader->info.system_values_read, 120bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE); 121bf215546Sopenharmony_ci BITSET_CLEAR(shader->info.system_values_read, 122bf215546Sopenharmony_ci SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci return nir_shader_instructions_pass(shader, lower_single_sampled_instr, 125bf215546Sopenharmony_ci nir_metadata_block_index | 126bf215546Sopenharmony_ci nir_metadata_dominance, 127bf215546Sopenharmony_ci NULL) || progress; 128bf215546Sopenharmony_ci} 129