1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "compiler/nir/nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 bool midgard_nir_lod_errata(nir_shader *shader);
28
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.
33 */
34
35 static bool
nir_lod_errata_instr(nir_builder * b,nir_instr * instr,void * data)36 nir_lod_errata_instr(nir_builder *b, nir_instr *instr, void *data)
37 {
38 if (instr->type != nir_instr_type_tex)
39 return false;
40
41 nir_tex_instr *tex = nir_instr_as_tex(instr);
42 b->cursor = nir_before_instr(instr);
43
44 /* The errata only applies to textureLod ("TEXGRD") */
45 if (tex->op != nir_texop_txl)
46 return false;
47
48 /* TODO: Indirect samplers, separate sampler objects XXX */
49 nir_def *sampler = nir_imm_int(b, tex->texture_index);
50
51 /* Let's grab the sampler parameters */
52 nir_def *params = nir_load_sampler_lod_parameters_pan(b, 3, 32, sampler);
53
54 /* Extract the individual components */
55 nir_def *min_lod = nir_channel(b, params, 0);
56 nir_def *max_lod = nir_channel(b, params, 1);
57 nir_def *lod_bias = nir_channel(b, params, 2);
58
59 /* Rewrite the LOD with bias/clamps. Order sensitive. */
60 for (unsigned i = 0; i < tex->num_srcs; i++) {
61 if (tex->src[i].src_type != nir_tex_src_lod)
62 continue;
63
64 nir_def *lod = tex->src[i].src.ssa;
65
66 nir_def *biased = nir_fadd(b, lod, lod_bias);
67 nir_def *clamped = nir_fmin(b, nir_fmax(b, biased, min_lod), max_lod);
68
69 nir_src_rewrite(&tex->src[i].src, clamped);
70 }
71
72 return true;
73 }
74
75 bool
midgard_nir_lod_errata(nir_shader * shader)76 midgard_nir_lod_errata(nir_shader *shader)
77 {
78 return nir_shader_instructions_pass(
79 shader, nir_lod_errata_instr,
80 nir_metadata_control_flow, NULL);
81 }
82