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
28 static bool
nir_lower_tex_shadow_filter(const nir_instr * instr,UNUSED const void * _options)29 nir_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
48 static const struct glsl_type *
strip_shadow(const struct glsl_type * type)49 strip_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 static const struct glsl_type *
strip_shadow_with_array(const struct glsl_type * type)60 strip_shadow_with_array(const struct glsl_type *type)
61 {
62 if (glsl_type_is_array(type))
63 return glsl_array_type(strip_shadow(glsl_without_array(type)),
64 glsl_get_length(type), 0);
65 return strip_shadow(type);
66 }
67
68 typedef struct {
69 unsigned n_states;
70 enum compare_func *compare_func;
71 nir_lower_tex_shadow_swizzle *tex_swizzles;
72 } sampler_state;
73
74 static nir_def *
nir_lower_tex_shadow_impl(nir_builder * b,nir_instr * instr,void * options)75 nir_lower_tex_shadow_impl(nir_builder *b, nir_instr *instr, void *options)
76
77 {
78 nir_tex_instr *tex = nir_instr_as_tex(instr);
79
80 sampler_state *state = (sampler_state *)options;
81 unsigned num_components = nir_tex_instr_result_size(tex);
82
83 b->cursor = nir_after_instr(instr);
84 tex->is_shadow = false;
85
86 int comp_index = nir_tex_instr_src_index(tex, nir_tex_src_comparator);
87 unsigned sampler_binding = tex->texture_index;
88
89 nir_deref_instr *sampler_deref = NULL;
90 nir_variable *sampler = NULL;
91
92 int sampler_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
93 if (sampler_index >= 0) {
94 sampler_deref = nir_instr_as_deref(tex->src[sampler_index].src.ssa->parent_instr);
95 sampler = nir_deref_instr_get_variable(sampler_deref);
96 sampler_binding = sampler ? sampler->data.binding : 0;
97 }
98
99 /* NIR expects a vec4 result from the above texture instructions */
100 nir_def_init(&tex->instr, &tex->def, 4, 32);
101
102 nir_def *tex_r = nir_channel(b, &tex->def, 0);
103 nir_def *cmp = tex->src[comp_index].src.ssa;
104
105 int proj_index = nir_tex_instr_src_index(tex, nir_tex_src_projector);
106 if (proj_index >= 0)
107 cmp = nir_fmul(b, cmp, nir_frcp(b, tex->src[proj_index].src.ssa));
108
109 nir_def *result =
110 nir_compare_func(b,
111 sampler_binding < state->n_states ? state->compare_func[sampler_binding] : COMPARE_FUNC_ALWAYS,
112 cmp, tex_r);
113
114 result = nir_b2f32(b, result);
115 nir_def *one = nir_imm_float(b, 1.0);
116 nir_def *zero = nir_imm_float(b, 0.0);
117
118 nir_def *lookup[6] = { result, zero, zero, one, zero, one };
119 nir_def *r[4] = { result, result, result, result };
120
121 if (sampler_binding < state->n_states) {
122 r[0] = lookup[state->tex_swizzles[sampler_binding].swizzle_r];
123 r[1] = lookup[state->tex_swizzles[sampler_binding].swizzle_g];
124 r[2] = lookup[state->tex_swizzles[sampler_binding].swizzle_b];
125 r[3] = lookup[state->tex_swizzles[sampler_binding].swizzle_a];
126 }
127
128 result = nir_vec(b, r, num_components);
129
130 if (sampler_index >= 0) {
131 sampler->type = strip_shadow_with_array(sampler->type);
132 sampler_deref->type = sampler->type;
133 }
134
135 tex->is_shadow = false;
136 nir_tex_instr_remove_src(tex, comp_index);
137
138 return result;
139 }
140
141 bool
nir_lower_tex_shadow(nir_shader * s,unsigned n_states,enum compare_func * compare_func,nir_lower_tex_shadow_swizzle * tex_swizzles)142 nir_lower_tex_shadow(nir_shader *s,
143 unsigned n_states,
144 enum compare_func *compare_func,
145 nir_lower_tex_shadow_swizzle *tex_swizzles)
146 {
147 sampler_state state = { n_states, compare_func, tex_swizzles };
148
149 bool result =
150 nir_shader_lower_instructions(s,
151 nir_lower_tex_shadow_filter,
152 nir_lower_tex_shadow_impl,
153 &state);
154 return result;
155 }
156