1/* 2 * Copyright © Microsoft 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 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 * USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24#include "nir.h" 25#include "nir_builder.h" 26#include "nir_builtin_builder.h" 27 28static bool 29nir_lower_tex_shadow_filter(const nir_instr *instr, 30 UNUSED const void *_options) 31{ 32 if (instr->type != nir_instr_type_tex) 33 return false; 34 35 /* To be consistent we also want to lower tex when we lower anything, 36 * otherwise the differences in evaluating the shadow value might lead 37 * to artifacts. */ 38 nir_tex_instr *tex = nir_instr_as_tex(instr); 39 if (tex->op != nir_texop_txb && 40 tex->op != nir_texop_txl && 41 tex->op != nir_texop_txd && 42 tex->op != nir_texop_tex) 43 return false; 44 45 return tex->is_shadow; 46} 47 48static const struct glsl_type * 49strip_shadow(const struct glsl_type *type) 50{ 51 const struct glsl_type *new_type = 52 glsl_sampler_type( 53 glsl_get_sampler_dim(type), 54 false, glsl_sampler_type_is_array(type), 55 GLSL_TYPE_FLOAT); 56 return new_type; 57} 58 59 60static const struct glsl_type * 61strip_shadow_with_array(const struct glsl_type *type) 62{ 63 if (glsl_type_is_array(type)) 64 return glsl_array_type(strip_shadow(glsl_without_array(type)), 65 glsl_get_length(type), 0); 66 return strip_shadow(type); 67} 68 69typedef struct { 70 unsigned n_states; 71 enum compare_func *compare_func; 72 nir_lower_tex_shadow_swizzle *tex_swizzles; 73} sampler_state; 74 75static nir_ssa_def * 76nir_lower_tex_shadow_impl(nir_builder *b, nir_instr *instr, void *options) 77 78{ 79 nir_tex_instr *tex = nir_instr_as_tex(instr); 80 81 sampler_state *state = (sampler_state *)options; 82 unsigned num_components = nir_tex_instr_result_size(tex); 83 84 b->cursor = nir_after_instr(instr); 85 tex->is_shadow = false; 86 87 int comp_index = nir_tex_instr_src_index(tex, nir_tex_src_comparator); 88 unsigned sampler_binding = tex->texture_index; 89 90 nir_deref_instr *sampler_deref = NULL; 91 nir_variable *sampler = NULL; 92 93 int sampler_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref); 94 if (sampler_index >= 0) { 95 sampler_deref = nir_instr_as_deref(tex->src[sampler_index].src.ssa->parent_instr); 96 sampler = nir_deref_instr_get_variable(sampler_deref); 97 sampler_binding = sampler ? sampler->data.binding : 0; 98 } 99 100 /* NIR expects a vec4 result from the above texture instructions */ 101 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL); 102 103 nir_ssa_def *tex_r = nir_channel(b, &tex->dest.ssa, 0); 104 nir_ssa_def *cmp = tex->src[comp_index].src.ssa; 105 106 int proj_index = nir_tex_instr_src_index(tex, nir_tex_src_projector); 107 if (proj_index >= 0) 108 cmp = nir_fmul(b, cmp, nir_frcp(b, tex->src[proj_index].src.ssa)); 109 110 nir_ssa_def * result = 111 nir_compare_func(b, 112 sampler_binding < state->n_states ? 113 state->compare_func[sampler_binding] : COMPARE_FUNC_ALWAYS, 114 cmp, tex_r); 115 116 result = nir_b2f32(b, result); 117 nir_ssa_def *one = nir_imm_float(b, 1.0); 118 nir_ssa_def *zero = nir_imm_float(b, 0.0); 119 120 nir_ssa_def *lookup[6] = {result, NULL, NULL, NULL, zero, one}; 121 nir_ssa_def *r[4] = {lookup[state->tex_swizzles[sampler_binding].swizzle_r], 122 lookup[state->tex_swizzles[sampler_binding].swizzle_g], 123 lookup[state->tex_swizzles[sampler_binding].swizzle_b], 124 lookup[state->tex_swizzles[sampler_binding].swizzle_a] 125 }; 126 127 result = nir_vec(b, r, num_components); 128 129 if (sampler_index >= 0) { 130 sampler->type = strip_shadow_with_array(sampler->type); 131 sampler_deref->type = sampler->type; 132 } 133 134 tex->is_shadow = false; 135 nir_tex_instr_remove_src(tex, comp_index); 136 137 return result; 138} 139 140bool 141nir_lower_tex_shadow(nir_shader *s, 142 unsigned n_states, 143 enum compare_func *compare_func, 144 nir_lower_tex_shadow_swizzle *tex_swizzles) 145{ 146 sampler_state state = {n_states, compare_func, tex_swizzles}; 147 148 bool result = 149 nir_shader_lower_instructions(s, 150 nir_lower_tex_shadow_filter, 151 nir_lower_tex_shadow_impl, 152 &state); 153 return result; 154} 155