• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "d3d12_nir_lower_texcmp.h"
25 #include "nir_builder.h"
26 #include "nir_builtin_builder.h"
27 
28 static bool
lower_sample_tex_compare_filter(const nir_instr * instr,UNUSED const void * _options)29 lower_sample_tex_compare_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 
60 static const struct glsl_type *
strip_shadow_with_array(const struct glsl_type * type)61 strip_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 
69 typedef struct {
70    unsigned n_states;
71    enum compare_func *compare_func;
72    dxil_texture_swizzle_state *tex_swizzles;
73 } sampler_state;
74 
75 static nir_ssa_def *
lower_sample_tex_compare_impl(nir_builder * b,nir_instr * instr,void * options)76 lower_sample_tex_compare_impl(nir_builder *b, nir_instr *instr,
77                               void *options)
78 
79 {
80    nir_tex_instr *tex = nir_instr_as_tex(instr);
81 
82    sampler_state *state = (sampler_state *)options;
83    unsigned num_components = nir_tex_instr_result_size(tex);
84 
85    b->cursor = nir_after_instr(instr);
86    tex->is_shadow = false;
87 
88    int comp_index = nir_tex_instr_src_index(tex, nir_tex_src_comparator);
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    assert(sampler_index >= 0);
95 
96    sampler_deref = nir_instr_as_deref(tex->src[sampler_index].src.ssa->parent_instr);
97    sampler = nir_deref_instr_get_variable(sampler_deref);
98 
99    /* NIR expects a vec4 result from the above texture instructions */
100    nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
101 
102    nir_ssa_def *tex_r = nir_channel(b, &tex->dest.ssa, 0);
103    nir_ssa_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_ssa_def * result =
110          nir_compare_func(b,
111                           sampler->data.binding < state->n_states ?
112                              state->compare_func[sampler->data.binding] : COMPARE_FUNC_ALWAYS,
113                           cmp, tex_r);
114 
115    result = nir_b2f32(b, result);
116    nir_ssa_def *one = nir_imm_float(b, 1.0);
117    nir_ssa_def *zero = nir_imm_float(b, 0.0);
118 
119    nir_ssa_def *lookup[6] = {result, NULL, NULL, NULL, zero, one};
120    nir_ssa_def *r[4] = {lookup[state->tex_swizzles[sampler->data.binding].swizzle_r],
121                         lookup[state->tex_swizzles[sampler->data.binding].swizzle_g],
122                         lookup[state->tex_swizzles[sampler->data.binding].swizzle_b],
123                         lookup[state->tex_swizzles[sampler->data.binding].swizzle_a]
124                        };
125 
126    result = nir_vec(b, r, num_components);
127 
128    sampler->type = strip_shadow_with_array(sampler->type);
129    sampler_deref->type = sampler->type;
130 
131    tex->is_shadow = false;
132    nir_tex_instr_remove_src(tex, comp_index);
133 
134    return result;
135 }
136 
137 bool
d3d12_lower_sample_tex_compare(nir_shader * s,unsigned n_states,enum compare_func * compare_func,dxil_texture_swizzle_state * tex_swizzles)138 d3d12_lower_sample_tex_compare(nir_shader *s,
139                                unsigned n_states,
140                                enum compare_func *compare_func,
141                                dxil_texture_swizzle_state *tex_swizzles)
142 {
143    sampler_state state = {n_states, compare_func, tex_swizzles};
144 
145    bool result =
146          nir_shader_lower_instructions(s,
147                                        lower_sample_tex_compare_filter,
148                                        lower_sample_tex_compare_impl,
149                                        &state);
150    return result;
151 }
152