1 /*
2 * Copyright © 2020 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 "compiler/nir/nir_builder.h"
25 #include "brw_nir.h"
26
27 /**
28 * Wa_1806565034:
29 *
30 * Gfx12+ 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
33 * array size queries.
34 *
35 * This NIR pass works around this by patching the array size with a
36 * MAX(array_size, 1) for array textures.
37 */
38
39 static bool
brw_nir_clamp_image_1d_2d_array_sizes_instr(nir_builder * b,nir_instr * instr,UNUSED void * cb_data)40 brw_nir_clamp_image_1d_2d_array_sizes_instr(nir_builder *b,
41 nir_instr *instr,
42 UNUSED void *cb_data)
43 {
44 nir_ssa_def *image_size = NULL;
45
46 switch (instr->type) {
47 case nir_instr_type_intrinsic: {
48 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
49
50 switch (intr->intrinsic) {
51 case nir_intrinsic_image_size:
52 case nir_intrinsic_bindless_image_size:
53 if (!nir_intrinsic_image_array(intr))
54 break;
55
56 image_size = &intr->dest.ssa;
57 break;
58
59 case nir_intrinsic_image_deref_size: {
60 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
61
62 assert(glsl_type_is_image(deref->type));
63
64 if (!glsl_sampler_type_is_array(deref->type))
65 break;
66
67 image_size = &intr->dest.ssa;
68 break;
69 }
70
71 default:
72 break;
73 }
74 break;
75 }
76
77 case nir_instr_type_tex: {
78 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
79 if (tex_instr->op != nir_texop_txs)
80 break;
81
82 if (!tex_instr->is_array)
83 break;
84
85 image_size = &tex_instr->dest.ssa;
86 break;
87 }
88
89 default:
90 break;
91 }
92
93 if (!image_size)
94 return false;
95
96 b->cursor = nir_after_instr(instr);
97
98 nir_ssa_def *components[4];
99 /* OR all the sizes for all components but the last. */
100 nir_ssa_def *or_components = nir_imm_int(b, 0);
101 for (int i = 0; i < image_size->num_components; i++) {
102 if (i == (image_size->num_components - 1)) {
103 nir_ssa_def *null_or_size[2] = {
104 nir_imm_int(b, 0),
105 nir_imax(b, nir_channel(b, image_size, i),
106 nir_imm_int(b, 1)),
107 };
108 nir_ssa_def *vec2_null_or_size = nir_vec(b, null_or_size, 2);
109
110 /* Using the ORed sizes select either the element 0 or 1
111 * from this vec2. For NULL textures which have a size of
112 * 0x0x0, we'll select the first element which is 0 and for
113 * the rest MAX(depth, 1).
114 */
115 components[i] =
116 nir_vector_extract(b, vec2_null_or_size,
117 nir_imin(b, or_components,
118 nir_imm_int(b, 1)));
119 } else {
120 components[i] = nir_channel(b, image_size, i);
121 or_components = nir_ior(b, components[i], or_components);
122 }
123 }
124 nir_ssa_def *image_size_replacement =
125 nir_vec(b, components, image_size->num_components);
126
127 b->cursor = nir_after_instr(instr);
128
129 nir_ssa_def_rewrite_uses_after(image_size,
130 image_size_replacement,
131 image_size_replacement->parent_instr);
132
133 return true;
134 }
135
136 bool
brw_nir_clamp_image_1d_2d_array_sizes(nir_shader * shader)137 brw_nir_clamp_image_1d_2d_array_sizes(nir_shader *shader)
138 {
139 return nir_shader_instructions_pass(shader,
140 brw_nir_clamp_image_1d_2d_array_sizes_instr,
141 nir_metadata_block_index |
142 nir_metadata_dominance,
143 NULL);
144 }
145