1/* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is 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 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#include "nir.h" 25#include "nir_builder.h" 26 27static nir_ssa_def * 28load_frag_coord(nir_builder *b, const nir_input_attachment_options *options) 29{ 30 if (options->use_fragcoord_sysval) 31 return nir_load_frag_coord(b); 32 33 nir_variable *pos = 34 nir_find_variable_with_location(b->shader, nir_var_shader_in, 35 VARYING_SLOT_POS); 36 if (pos == NULL) { 37 pos = nir_variable_create(b->shader, nir_var_shader_in, 38 glsl_vec4_type(), NULL); 39 pos->data.location = VARYING_SLOT_POS; 40 } 41 /** 42 * From Vulkan spec: 43 * "The OriginLowerLeft execution mode must not be used; fragment entry 44 * points must declare OriginUpperLeft." 45 * 46 * So at this point origin_upper_left should be true 47 */ 48 assert(b->shader->info.fs.origin_upper_left == true); 49 50 return nir_load_var(b, pos); 51} 52 53static nir_ssa_def * 54load_layer_id(nir_builder *b, const nir_input_attachment_options *options) 55{ 56 if (options->use_layer_id_sysval) { 57 if (options->use_view_id_for_layer) 58 return nir_load_view_index(b); 59 else 60 return nir_load_layer_id(b); 61 } 62 63 gl_varying_slot slot = options->use_view_id_for_layer ? 64 VARYING_SLOT_VIEW_INDEX : VARYING_SLOT_LAYER; 65 nir_variable *layer_id = 66 nir_find_variable_with_location(b->shader, nir_var_shader_in, slot); 67 68 if (layer_id == NULL) { 69 layer_id = nir_variable_create(b->shader, nir_var_shader_in, 70 glsl_int_type(), NULL); 71 layer_id->data.location = slot; 72 layer_id->data.interpolation = INTERP_MODE_FLAT; 73 layer_id->data.driver_location = b->shader->num_inputs++; 74 } 75 76 return nir_load_var(b, layer_id); 77} 78 79static bool 80try_lower_input_load(nir_builder *b, nir_intrinsic_instr *load, 81 const nir_input_attachment_options *options) 82{ 83 nir_deref_instr *deref = nir_src_as_deref(load->src[0]); 84 assert(glsl_type_is_image(deref->type)); 85 86 enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type); 87 if (image_dim != GLSL_SAMPLER_DIM_SUBPASS && 88 image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS) 89 return false; 90 91 const bool multisampled = (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS); 92 93 b->cursor = nir_instr_remove(&load->instr); 94 95 nir_ssa_def *frag_coord = load_frag_coord(b, options); 96 frag_coord = nir_f2i32(b, frag_coord); 97 nir_ssa_def *offset = nir_ssa_for_src(b, load->src[1], 2); 98 nir_ssa_def *pos = nir_iadd(b, frag_coord, offset); 99 100 nir_ssa_def *layer = load_layer_id(b, options); 101 nir_ssa_def *coord = 102 nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1), layer); 103 104 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3 + multisampled); 105 106 tex->op = nir_texop_txf; 107 tex->sampler_dim = image_dim; 108 109 tex->dest_type = 110 nir_get_nir_type_for_glsl_base_type(glsl_get_sampler_result_type(deref->type)); 111 tex->is_array = true; 112 tex->is_shadow = false; 113 tex->is_sparse = load->intrinsic == nir_intrinsic_image_deref_sparse_load; 114 115 tex->texture_index = 0; 116 tex->sampler_index = 0; 117 118 tex->src[0].src_type = nir_tex_src_texture_deref; 119 tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa); 120 121 tex->src[1].src_type = nir_tex_src_coord; 122 tex->src[1].src = nir_src_for_ssa(coord); 123 tex->coord_components = 3; 124 125 tex->src[2].src_type = nir_tex_src_lod; 126 tex->src[2].src = nir_src_for_ssa(nir_imm_int(b, 0)); 127 128 if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) { 129 tex->op = nir_texop_txf_ms; 130 tex->src[3].src_type = nir_tex_src_ms_index; 131 tex->src[3].src = load->src[2]; 132 } 133 134 tex->texture_non_uniform = nir_intrinsic_access(load) & ACCESS_NON_UNIFORM; 135 136 nir_ssa_dest_init(&tex->instr, &tex->dest, nir_tex_instr_dest_size(tex), 32, NULL); 137 nir_builder_instr_insert(b, &tex->instr); 138 139 if (tex->is_sparse) { 140 unsigned load_result_size = load->dest.ssa.num_components - 1; 141 nir_component_mask_t load_result_mask = nir_component_mask(load_result_size); 142 nir_ssa_def *res = nir_channels( 143 b, &tex->dest.ssa, load_result_mask | 0x10); 144 145 nir_ssa_def_rewrite_uses(&load->dest.ssa, res); 146 } else { 147 nir_ssa_def_rewrite_uses(&load->dest.ssa, 148 &tex->dest.ssa); 149 } 150 151 return true; 152} 153 154static bool 155try_lower_input_texop(nir_builder *b, nir_tex_instr *tex, 156 const nir_input_attachment_options *options) 157{ 158 nir_deref_instr *deref = nir_src_as_deref(tex->src[0].src); 159 160 if (glsl_get_sampler_dim(deref->type) != GLSL_SAMPLER_DIM_SUBPASS_MS) 161 return false; 162 163 b->cursor = nir_before_instr(&tex->instr); 164 165 nir_ssa_def *frag_coord = load_frag_coord(b, options); 166 frag_coord = nir_f2i32(b, frag_coord); 167 168 nir_ssa_def *layer = load_layer_id(b, options); 169 nir_ssa_def *coord = nir_vec3(b, nir_channel(b, frag_coord, 0), 170 nir_channel(b, frag_coord, 1), layer); 171 172 tex->coord_components = 3; 173 174 nir_instr_rewrite_src(&tex->instr, &tex->src[1].src, nir_src_for_ssa(coord)); 175 176 return true; 177} 178 179static bool 180lower_input_attachments_instr(nir_builder *b, nir_instr *instr, void *_data) 181{ 182 const nir_input_attachment_options *options = _data; 183 184 switch (instr->type) { 185 case nir_instr_type_tex: { 186 nir_tex_instr *tex = nir_instr_as_tex(instr); 187 188 if (tex->op == nir_texop_fragment_mask_fetch_amd || 189 tex->op == nir_texop_fragment_fetch_amd) 190 return try_lower_input_texop(b, tex, options); 191 192 return false; 193 } 194 case nir_instr_type_intrinsic: { 195 nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr); 196 197 if (load->intrinsic == nir_intrinsic_image_deref_load || 198 load->intrinsic == nir_intrinsic_image_deref_sparse_load) 199 return try_lower_input_load(b, load, options); 200 201 return false; 202 } 203 204 default: 205 return false; 206 } 207} 208 209bool 210nir_lower_input_attachments(nir_shader *shader, 211 const nir_input_attachment_options *options) 212{ 213 assert(shader->info.stage == MESA_SHADER_FRAGMENT); 214 215 return nir_shader_instructions_pass(shader, lower_input_attachments_instr, 216 nir_metadata_block_index | 217 nir_metadata_dominance, 218 (void *)options); 219} 220