2 * Copyright (C) 2019 Collabora, Ltd.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "compiler/nir/nir.h"
25 #include "compiler/nir/nir_builder.h"
27 void midgard_nir_lod_errata(nir_shader
*shader
);
29 /* Workarounds errata pertaining to early Midgard chips where the settings for
30 * min_lod/max_lod/lod_bias are ignored in the sampler descriptor when
31 * texturing with a textureLod instruction. The workaround is to load these
32 * constants in as system values and perform the bias/clamp in the shader.
36 mir_lod_errata_body(nir_builder
*b
, nir_tex_instr
*tex
)
38 /* The errata only applies to textureLod ("TEXGRD") */
39 if (tex
->op
!= nir_texop_txl
)
42 /* Let's grab the sampler parameters */
43 nir_intrinsic_instr
*l
= nir_intrinsic_instr_create(b
->shader
,
44 nir_intrinsic_load_sampler_lod_parameters_pan
);
45 l
->num_components
= 3;
46 nir_ssa_dest_init(&l
->instr
, &l
->dest
, 3, 32, NULL
);
48 /* TODO: Indirect samplers, separate sampler objects XXX */
49 nir_src idx
= nir_src_for_ssa(nir_imm_int(b
, tex
->texture_index
));
50 nir_src_copy(&l
->src
[0], &idx
, l
);
52 nir_builder_instr_insert(b
, &l
->instr
);
53 nir_ssa_def
*params
= &l
->dest
.ssa
;
55 /* Extract the individual components */
56 nir_ssa_def
*min_lod
= nir_channel(b
, params
, 0);
57 nir_ssa_def
*max_lod
= nir_channel(b
, params
, 1);
58 nir_ssa_def
*lod_bias
= nir_channel(b
, params
, 2);
60 /* Rewrite the LOD with bias/clamps. Order sensitive. */
61 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
62 if (tex
->src
[i
].src_type
!= nir_tex_src_lod
)
65 nir_ssa_def
*lod
= nir_ssa_for_src(b
, tex
->src
[i
].src
, 1);
67 nir_ssa_def
*biased
= nir_fadd(b
, lod
, lod_bias
);
68 nir_ssa_def
*clamped
= nir_fmin(b
,
69 nir_fmax(b
, biased
, min_lod
), max_lod
);
71 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[i
].src
,
72 nir_src_for_ssa(clamped
));
77 midgard_nir_lod_errata(nir_shader
*shader
)
79 nir_foreach_function(function
, shader
) {
80 if (!function
->impl
) continue;
83 nir_builder_init(&b
, function
->impl
);
85 nir_foreach_block(block
, function
->impl
) {
86 nir_foreach_instr_safe(instr
, block
) {
87 if (instr
->type
!= nir_instr_type_tex
) continue;
89 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
90 b
.cursor
= nir_before_instr(instr
);
91 mir_lod_errata_body(&b
, tex
);
95 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);