• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "anv_nir.h"
25 #include "nir/nir_builder.h"
26 
27 static nir_ssa_def *
load_frag_coord(nir_builder * b)28 load_frag_coord(nir_builder *b)
29 {
30    nir_foreach_variable(var, &b->shader->inputs) {
31       if (var->data.location == VARYING_SLOT_POS)
32          return nir_load_var(b, var);
33    }
34 
35    nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in,
36                                            glsl_vec4_type(), NULL);
37    pos->data.location = VARYING_SLOT_POS;
38    pos->data.origin_upper_left = true;
39 
40    return nir_load_var(b, pos);
41 }
42 
43 static void
try_lower_input_load(nir_function_impl * impl,nir_intrinsic_instr * load)44 try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load)
45 {
46 
47    const struct glsl_type *image_type =
48       glsl_without_array(load->variables[0]->var->type);
49    enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(image_type);
50    if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&
51        image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
52       return;
53 
54    const bool multisampled = (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
55 
56    nir_builder b;
57    nir_builder_init(&b, impl);
58    b.cursor = nir_before_instr(&load->instr);
59 
60    nir_ssa_def *frag_coord = nir_f2i32(&b, load_frag_coord(&b));
61    nir_ssa_def *offset = nir_ssa_for_src(&b, load->src[0], 2);
62    nir_ssa_def *pos = nir_iadd(&b, frag_coord, offset);
63 
64    nir_ssa_def *layer =
65       nir_load_system_value(&b, nir_intrinsic_load_layer_id, 0);
66    nir_ssa_def *coord =
67       nir_vec3(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer);
68 
69    nir_tex_instr *tex = nir_tex_instr_create(b.shader, 2 + multisampled);
70 
71    tex->op = nir_texop_txf;
72 
73    switch (glsl_get_sampler_result_type(image_type)) {
74    case GLSL_TYPE_FLOAT:
75       tex->dest_type = nir_type_float;
76       break;
77    case GLSL_TYPE_INT:
78       tex->dest_type = nir_type_int;
79       break;
80    case GLSL_TYPE_UINT:
81       tex->dest_type = nir_type_uint;
82       break;
83    default:
84       unreachable("Invalid image type");
85    }
86    tex->is_array = true;
87    tex->is_shadow = false;
88 
89    tex->texture = nir_deref_var_clone(load->variables[0], tex);
90    tex->sampler = NULL;
91    tex->texture_index = 0;
92    tex->sampler_index = 0;
93 
94    tex->src[0].src_type = nir_tex_src_coord;
95    tex->src[0].src = nir_src_for_ssa(coord);
96    tex->coord_components = 3;
97 
98    tex->src[1].src_type = nir_tex_src_lod;
99    tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
100 
101    if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) {
102       tex->op = nir_texop_txf_ms;
103       tex->src[2].src_type = nir_tex_src_ms_index;
104       tex->src[2].src = load->src[1];
105    }
106 
107    nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
108    nir_builder_instr_insert(&b, &tex->instr);
109 
110    nir_ssa_def_rewrite_uses(&load->dest.ssa,
111                             nir_src_for_ssa(&tex->dest.ssa));
112 }
113 
114 void
anv_nir_lower_input_attachments(nir_shader * shader)115 anv_nir_lower_input_attachments(nir_shader *shader)
116 {
117    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
118 
119    nir_foreach_function(function, shader) {
120       if (!function->impl)
121          continue;
122 
123       nir_foreach_block(block, function->impl) {
124          nir_foreach_instr(instr, block) {
125             if (instr->type != nir_instr_type_intrinsic)
126                continue;
127 
128             nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
129 
130             if (load->intrinsic != nir_intrinsic_image_load)
131                continue;
132 
133             try_lower_input_load(function->impl, load);
134          }
135       }
136    }
137 }
138