• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         /* Let's grab the sampler parameters */
49         nir_intrinsic_instr *l = nir_intrinsic_instr_create(b->shader,
50                         nir_intrinsic_load_sampler_lod_parameters_pan);
51         l->num_components = 3;
52         nir_ssa_dest_init(&l->instr, &l->dest, 3, 32, NULL);
53 
54         /* TODO: Indirect samplers, separate sampler objects XXX */
55         nir_src idx = nir_src_for_ssa(nir_imm_int(b, tex->texture_index));
56         nir_src_copy(&l->src[0], &idx);
57 
58         nir_builder_instr_insert(b, &l->instr);
59         nir_ssa_def *params = &l->dest.ssa;
60 
61         /* Extract the individual components */
62         nir_ssa_def *min_lod = nir_channel(b, params, 0);
63         nir_ssa_def *max_lod = nir_channel(b, params, 1);
64         nir_ssa_def *lod_bias = nir_channel(b, params, 2);
65 
66         /* Rewrite the LOD with bias/clamps. Order sensitive. */
67         for (unsigned i = 0; i < tex->num_srcs; i++) {
68                 if (tex->src[i].src_type != nir_tex_src_lod)
69                         continue;
70 
71                 nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[i].src, 1);
72 
73                 nir_ssa_def *biased = nir_fadd(b, lod, lod_bias);
74                 nir_ssa_def *clamped = nir_fmin(b,
75                                 nir_fmax(b, biased, min_lod), max_lod);
76 
77                 nir_instr_rewrite_src(&tex->instr, &tex->src[i].src,
78                                 nir_src_for_ssa(clamped));
79         }
80 
81         return true;
82 }
83 
84 bool
midgard_nir_lod_errata(nir_shader * shader)85 midgard_nir_lod_errata(nir_shader *shader)
86 {
87         return nir_shader_instructions_pass(shader,
88                                             nir_lod_errata_instr,
89                                             nir_metadata_block_index | nir_metadata_dominance,
90                                             NULL);
91 }
92