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 bool is_fixed_point_format;
73 } sampler_state;
74
75 static nir_def *
nir_lower_tex_shadow_impl(nir_builder * b,nir_instr * instr,void * options)76 nir_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_def_init(&tex->instr, &tex->def, 4, 32);
102
103 nir_def *tex_r = nir_channel(b, &tex->def, 0);
104 nir_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 if (state->is_fixed_point_format)
111 cmp = nir_fsat(b, cmp);
112
113 nir_def *result =
114 nir_compare_func(b,
115 sampler_binding < state->n_states ? state->compare_func[sampler_binding] : COMPARE_FUNC_ALWAYS,
116 cmp, tex_r);
117
118 result = nir_b2f32(b, result);
119 nir_def *one = nir_imm_float(b, 1.0);
120 nir_def *zero = nir_imm_float(b, 0.0);
121
122 nir_def *lookup[6] = { result, zero, zero, one, zero, one };
123 nir_def *r[4] = { result, result, result, result };
124
125 if (sampler_binding < state->n_states) {
126 r[0] = lookup[state->tex_swizzles[sampler_binding].swizzle_r];
127 r[1] = lookup[state->tex_swizzles[sampler_binding].swizzle_g];
128 r[2] = lookup[state->tex_swizzles[sampler_binding].swizzle_b];
129 r[3] = lookup[state->tex_swizzles[sampler_binding].swizzle_a];
130 }
131
132 result = nir_vec(b, r, num_components);
133
134 if (sampler_index >= 0) {
135 sampler->type = strip_shadow_with_array(sampler->type);
136 sampler_deref->type = sampler->type;
137 }
138
139 tex->is_shadow = false;
140 nir_tex_instr_remove_src(tex, comp_index);
141
142 return result;
143 }
144
145 bool
nir_lower_tex_shadow(nir_shader * s,unsigned n_states,enum compare_func * compare_func,nir_lower_tex_shadow_swizzle * tex_swizzles,bool is_fixed_point_format)146 nir_lower_tex_shadow(nir_shader *s,
147 unsigned n_states,
148 enum compare_func *compare_func,
149 nir_lower_tex_shadow_swizzle *tex_swizzles,
150 bool is_fixed_point_format)
151 {
152 sampler_state state = { n_states, compare_func, tex_swizzles, is_fixed_point_format };
153
154 bool result =
155 nir_shader_lower_instructions(s,
156 nir_lower_tex_shadow_filter,
157 nir_lower_tex_shadow_impl,
158 &state);
159 return result;
160 }
161