1 /*
2 * Copyright © 2023 Intel 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 * 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 static const struct glsl_type *
strip_shadow(const struct glsl_type * type)28 strip_shadow(const struct glsl_type *type)
29 {
30 const struct glsl_type *new_type =
31 glsl_sampler_type(
32 glsl_get_sampler_dim(type),
33 false, glsl_sampler_type_is_array(type),
34 GLSL_TYPE_FLOAT);
35 return new_type;
36 }
37
38 static inline const struct glsl_type *
strip_shadow_with_array(const struct glsl_type * type)39 strip_shadow_with_array(const struct glsl_type *type)
40 {
41 return glsl_type_wrap_in_arrays(strip_shadow(glsl_without_array(type)), type);
42 }
43
44 static bool
change_deref_var_type(struct nir_builder * b,nir_instr * instr,void * data)45 change_deref_var_type(struct nir_builder *b, nir_instr *instr, void *data)
46 {
47 if (instr->type != nir_instr_type_deref)
48 return false;
49
50 nir_variable *sampler = data;
51 nir_deref_instr *deref = nir_instr_as_deref(instr);
52 if (deref->var == sampler) {
53 deref->type = sampler->type;
54 return true;
55 }
56
57 return false;
58 }
59
60 static bool
remove_tex_shadow(struct nir_builder * b,nir_instr * instr,void * data)61 remove_tex_shadow(struct nir_builder *b, nir_instr *instr, void *data)
62 {
63 if (instr->type != nir_instr_type_tex)
64 return false;
65
66 nir_tex_instr *tex = nir_instr_as_tex(instr);
67
68 if (!tex->is_shadow)
69 return false;
70
71 unsigned *textures_bitmask = data;
72
73 if (BITFIELD_BIT(tex->texture_index) & ~*textures_bitmask)
74 return false;
75
76 int index = nir_tex_instr_src_index(tex, nir_tex_src_comparator);
77
78 if (index != -1) {
79 nir_deref_instr *sampler_deref = NULL;
80 nir_variable *sampler = NULL;
81 int sampler_src_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
82 if (sampler_src_index >= 0) {
83 sampler_deref = nir_instr_as_deref(tex->src[sampler_src_index].src.ssa->parent_instr);
84 sampler = nir_deref_instr_get_variable(sampler_deref);
85 sampler->type = strip_shadow_with_array(sampler->type);
86 sampler_deref->type = sampler->type;
87 } else {
88 sampler = nir_find_sampler_variable_with_tex_index(b->shader,
89 tex->texture_index);
90 sampler->type = strip_shadow_with_array(sampler->type);
91 }
92
93 nir_shader_instructions_pass(b->shader, change_deref_var_type,
94 nir_metadata_none, sampler);
95 tex->is_shadow = false;
96 nir_tex_instr_remove_src(tex, index);
97 return true;
98 }
99
100 return false;
101 }
102
103 bool
nir_remove_tex_shadow(nir_shader * shader,unsigned textures_bitmask)104 nir_remove_tex_shadow(nir_shader *shader, unsigned textures_bitmask)
105 {
106 return nir_shader_instructions_pass(shader, remove_tex_shadow,
107 nir_metadata_none, &textures_bitmask);
108 }
109