| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/nir/nir.h" |
| 7 |
#include "compiler/nir/nir_builder.h" |
| 8 |
|
| 9 |
bool midgard_nir_lod_errata(nir_shader *shader); |
| 10 |
|
| 11 |
/* Workarounds errata pertaining to early Midgard chips where the settings for |
| 12 |
* min_lod/max_lod/lod_bias are ignored in the sampler descriptor when |
| 13 |
* texturing with a textureLod instruction. The workaround is to load these |
| 14 |
* constants in as system values and perform the bias/clamp in the shader. |
| 15 |
*/ |
| 16 |
|
| 17 |
static bool |
| 18 |
nir_lod_errata_instr(nir_builder *b, nir_tex_instr *tex, void *data) |
| 19 |
{ |
| 20 |
b->cursor = nir_before_instr(&tex->instr); |
| 21 |
|
| 22 |
/* The errata only applies to textureLod ("TEXGRD") */ |
| 23 |
if (tex->op != nir_texop_txl) |
| 24 |
return false; |
| 25 |
|
| 26 |
/* TODO: Indirect samplers, separate sampler objects XXX */ |
| 27 |
nir_def *sampler = nir_imm_int(b, tex->texture_index); |
| 28 |
|
| 29 |
/* Let's grab the sampler parameters */ |
| 30 |
nir_def *params = nir_load_sampler_lod_parameters(b, 3, 32, sampler); |
| 31 |
|
| 32 |
/* Extract the individual components */ |
| 33 |
nir_def *min_lod = nir_channel(b, params, 0); |
| 34 |
nir_def *max_lod = nir_channel(b, params, 1); |
| 35 |
nir_def *lod_bias = nir_channel(b, params, 2); |
| 36 |
|
| 37 |
/* Rewrite the LOD with bias/clamps. Order sensitive. */ |
| 38 |
for (unsigned i = 0; i < tex->num_srcs; i++) { |
| 39 |
if (tex->src[i].src_type != nir_tex_src_lod) |
| 40 |
continue; |
| 41 |
|
| 42 |
nir_def *lod = tex->src[i].src.ssa; |
| 43 |
|
| 44 |
nir_def *biased = nir_fadd(b, lod, lod_bias); |
| 45 |
nir_def *clamped = nir_fmin(b, nir_fmax(b, biased, min_lod), max_lod); |
| 46 |
|
| 47 |
nir_src_rewrite(&tex->src[i].src, clamped); |
| 48 |
} |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
bool |
| 54 |
midgard_nir_lod_errata(nir_shader *shader) |
| 55 |
{ |
| 56 |
return nir_shader_tex_pass(shader, nir_lod_errata_instr, |
| 57 |
nir_metadata_control_flow, NULL); |
| 58 |
} |