2 * Copyright © 2020 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
24 #include "compiler/nir/nir_builder.h"
30 * Gen12+ allows to set RENDER_SURFACE_STATE::SurfaceArray to 1 only if
31 * array_len > 1. Setting RENDER_SURFACE_STATE::SurfaceArray to 0 results in
32 * the HW RESINFO message to report an array size of 0 which breaks texture
35 * This NIR pass works around this by patching the array size with a
36 * MAX(array_size, 1) for array textures.
40 brw_nir_clamp_image_1d_2d_array_sizes(nir_shader
*shader
)
42 bool progress
= false;
45 nir_foreach_function(func
, shader
) {
46 bool function_progress
= false;
51 nir_builder_init(&b
, func
->impl
);
53 nir_foreach_block(block
, func
->impl
) {
54 nir_foreach_instr_safe(instr
, block
) {
55 nir_ssa_def
*image_size
= NULL
;
57 switch (instr
->type
) {
58 case nir_instr_type_intrinsic
: {
59 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
61 switch (intr
->intrinsic
) {
62 case nir_intrinsic_image_size
:
63 case nir_intrinsic_bindless_image_size
:
64 if (!nir_intrinsic_image_array(intr
))
67 image_size
= &intr
->dest
.ssa
;
70 case nir_intrinsic_image_deref_size
: {
71 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
73 assert(glsl_type_is_image(deref
->type
));
75 if (!glsl_sampler_type_is_array(deref
->type
))
78 image_size
= &intr
->dest
.ssa
;
88 case nir_instr_type_tex
: {
89 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
90 if (tex_instr
->op
!= nir_texop_txs
)
93 if (!tex_instr
->is_array
)
96 image_size
= &tex_instr
->dest
.ssa
;
107 b
.cursor
= nir_after_instr(instr
);
109 nir_ssa_def
*components
[4];
110 for (int i
= 0; i
< image_size
->num_components
; i
++) {
111 if (i
== (image_size
->num_components
- 1)) {
112 components
[i
] = nir_imax(&b
, nir_channel(&b
, image_size
, i
),
115 components
[i
] = nir_channel(&b
, image_size
, i
);
118 nir_ssa_def
*image_size_replacement
=
119 nir_vec(&b
, components
, image_size
->num_components
);
121 b
.cursor
= nir_after_instr(instr
);
123 nir_ssa_def_rewrite_uses_after(image_size
,
124 nir_src_for_ssa(image_size_replacement
),
125 image_size_replacement
->parent_instr
);
127 function_progress
= true;
131 if (function_progress
) {
132 nir_metadata_preserve(func
->impl
, nir_metadata_block_index
|
133 nir_metadata_dominance
);
134 progress
= function_progress
;
136 nir_metadata_preserve(func
->impl
, nir_metadata_all
);