2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include "nir_builder.h"
28 load_frag_coord(const nir_input_attachment_options
*options
, nir_builder
*b
)
30 if (options
->use_fragcoord_sysval
)
31 return nir_load_frag_coord(b
);
34 nir_find_variable_with_location(b
->shader
, nir_var_shader_in
,
37 pos
= nir_variable_create(b
->shader
, nir_var_shader_in
,
38 glsl_vec4_type(), NULL
);
39 pos
->data
.location
= VARYING_SLOT_POS
;
43 * "The OriginLowerLeft execution mode must not be used; fragment entry
44 * points must declare OriginUpperLeft."
46 * So at this point origin_upper_left should be true
48 assert(b
->shader
->info
.fs
.origin_upper_left
== true);
50 return nir_load_var(b
, pos
);
54 load_layer_id(const nir_input_attachment_options
*options
, nir_builder
*b
)
56 if (options
->use_layer_id_sysval
)
57 return nir_load_layer_id(b
);
59 nir_variable
*layer_id
=
60 nir_find_variable_with_location(b
->shader
, nir_var_shader_in
,
63 if (layer_id
== NULL
) {
64 layer_id
= nir_variable_create(b
->shader
, nir_var_shader_in
,
65 glsl_int_type(), NULL
);
66 layer_id
->data
.location
= VARYING_SLOT_LAYER
;
67 layer_id
->data
.interpolation
= INTERP_MODE_FLAT
;
68 layer_id
->data
.driver_location
= b
->shader
->num_inputs
++;
71 return nir_load_var(b
, layer_id
);
75 try_lower_input_load(nir_function_impl
*impl
, nir_intrinsic_instr
*load
,
76 const nir_input_attachment_options
*options
)
78 nir_deref_instr
*deref
= nir_src_as_deref(load
->src
[0]);
79 assert(glsl_type_is_image(deref
->type
));
81 enum glsl_sampler_dim image_dim
= glsl_get_sampler_dim(deref
->type
);
82 if (image_dim
!= GLSL_SAMPLER_DIM_SUBPASS
&&
83 image_dim
!= GLSL_SAMPLER_DIM_SUBPASS_MS
)
86 const bool multisampled
= (image_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
);
89 nir_builder_init(&b
, impl
);
90 b
.cursor
= nir_instr_remove(&load
->instr
);
92 nir_ssa_def
*frag_coord
= load_frag_coord(options
, &b
);
93 frag_coord
= nir_f2i32(&b
, frag_coord
);
94 nir_ssa_def
*offset
= nir_ssa_for_src(&b
, load
->src
[1], 2);
95 nir_ssa_def
*pos
= nir_iadd(&b
, frag_coord
, offset
);
97 nir_ssa_def
*layer
= load_layer_id(options
, &b
);
99 nir_vec3(&b
, nir_channel(&b
, pos
, 0), nir_channel(&b
, pos
, 1), layer
);
101 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 3 + multisampled
);
103 tex
->op
= nir_texop_txf
;
104 tex
->sampler_dim
= image_dim
;
106 switch (glsl_get_sampler_result_type(deref
->type
)) {
107 case GLSL_TYPE_FLOAT
:
108 tex
->dest_type
= nir_type_float
;
111 tex
->dest_type
= nir_type_int
;
114 tex
->dest_type
= nir_type_uint
;
117 unreachable("Invalid image type");
119 tex
->is_array
= true;
120 tex
->is_shadow
= false;
122 tex
->texture_index
= 0;
123 tex
->sampler_index
= 0;
125 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
126 tex
->src
[0].src
= nir_src_for_ssa(&deref
->dest
.ssa
);
128 tex
->src
[1].src_type
= nir_tex_src_coord
;
129 tex
->src
[1].src
= nir_src_for_ssa(coord
);
130 tex
->coord_components
= 3;
132 tex
->src
[2].src_type
= nir_tex_src_lod
;
133 tex
->src
[2].src
= nir_src_for_ssa(nir_imm_int(&b
, 0));
135 if (image_dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) {
136 tex
->op
= nir_texop_txf_ms
;
137 tex
->src
[3].src_type
= nir_tex_src_ms_index
;
138 tex
->src
[3].src
= load
->src
[2];
141 tex
->texture_non_uniform
= nir_intrinsic_access(load
) & ACCESS_NON_UNIFORM
;
143 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
144 nir_builder_instr_insert(&b
, &tex
->instr
);
146 nir_ssa_def_rewrite_uses(&load
->dest
.ssa
,
147 nir_src_for_ssa(&tex
->dest
.ssa
));
153 try_lower_input_texop(nir_function_impl
*impl
, nir_tex_instr
*tex
,
154 const nir_input_attachment_options
*options
)
156 nir_deref_instr
*deref
= nir_src_as_deref(tex
->src
[0].src
);
158 if (glsl_get_sampler_dim(deref
->type
) != GLSL_SAMPLER_DIM_SUBPASS_MS
)
162 nir_builder_init(&b
, impl
);
163 b
.cursor
= nir_before_instr(&tex
->instr
);
165 nir_ssa_def
*frag_coord
= load_frag_coord(options
, &b
);
166 frag_coord
= nir_f2i32(&b
, frag_coord
);
168 nir_ssa_def
*layer
= load_layer_id(options
, &b
);
169 nir_ssa_def
*coord
= nir_vec3(&b
, nir_channel(&b
, frag_coord
, 0),
170 nir_channel(&b
, frag_coord
, 1), layer
);
172 tex
->coord_components
= 3;
174 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[1].src
, nir_src_for_ssa(coord
));
180 nir_lower_input_attachments(nir_shader
*shader
,
181 const nir_input_attachment_options
*options
)
183 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
184 bool progress
= false;
186 nir_foreach_function(function
, shader
) {
190 nir_foreach_block(block
, function
->impl
) {
191 nir_foreach_instr_safe(instr
, block
) {
192 switch (instr
->type
) {
193 case nir_instr_type_tex
: {
194 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
196 if (tex
->op
== nir_texop_fragment_mask_fetch
||
197 tex
->op
== nir_texop_fragment_fetch
) {
198 progress
|= try_lower_input_texop(function
->impl
, tex
,
203 case nir_instr_type_intrinsic
: {
204 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
206 if (load
->intrinsic
== nir_intrinsic_image_deref_load
) {
207 progress
|= try_lower_input_load(function
->impl
, load
,