• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Red Hat
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /* Lower glDrawPixels().
28  *
29  * This is based on the logic in st_get_drawpix_shader() in TGSI compiler.
30  *
31  * Run before nir_lower_io.
32  */
33 
34 typedef struct {
35    const nir_lower_drawpixels_options *options;
36    nir_shader   *shader;
37    nir_builder   b;
38    nir_variable *texcoord, *scale, *bias;
39 } lower_drawpixels_state;
40 
41 static nir_ssa_def *
get_texcoord(lower_drawpixels_state * state)42 get_texcoord(lower_drawpixels_state *state)
43 {
44    if (state->texcoord == NULL) {
45       nir_variable *texcoord = NULL;
46 
47       /* find gl_TexCoord, if it exists: */
48       nir_foreach_variable(var, &state->shader->inputs) {
49          if (var->data.location == VARYING_SLOT_TEX0) {
50             texcoord = var;
51             break;
52          }
53       }
54 
55       /* otherwise create it: */
56       if (texcoord == NULL) {
57          texcoord = nir_variable_create(state->shader,
58                                         nir_var_shader_in,
59                                         glsl_vec4_type(),
60                                         "gl_TexCoord");
61          texcoord->data.location = VARYING_SLOT_TEX0;
62       }
63 
64       state->texcoord = texcoord;
65    }
66    return nir_load_var(&state->b, state->texcoord);
67 }
68 
69 static nir_variable *
create_uniform(nir_shader * shader,const char * name,const int state_tokens[5])70 create_uniform(nir_shader *shader, const char *name, const int state_tokens[5])
71 {
72    nir_variable *var = nir_variable_create(shader,
73                                            nir_var_uniform,
74                                            glsl_vec4_type(),
75                                            name);
76    var->num_state_slots = 1;
77    var->state_slots = ralloc_array(var, nir_state_slot, 1);
78    memcpy(var->state_slots[0].tokens, state_tokens,
79           sizeof(var->state_slots[0].tokens));
80    return var;
81 }
82 
83 static nir_ssa_def *
get_scale(lower_drawpixels_state * state)84 get_scale(lower_drawpixels_state *state)
85 {
86    if (state->scale == NULL) {
87       state->scale = create_uniform(state->shader, "gl_PTscale",
88                                     state->options->scale_state_tokens);
89    }
90    return nir_load_var(&state->b, state->scale);
91 }
92 
93 static nir_ssa_def *
get_bias(lower_drawpixels_state * state)94 get_bias(lower_drawpixels_state *state)
95 {
96    if (state->bias == NULL) {
97       state->bias = create_uniform(state->shader, "gl_PTbias",
98                                    state->options->bias_state_tokens);
99    }
100    return nir_load_var(&state->b, state->bias);
101 }
102 
103 static nir_ssa_def *
get_texcoord_const(lower_drawpixels_state * state)104 get_texcoord_const(lower_drawpixels_state *state)
105 {
106    if (state->bias == NULL) {
107       state->bias = create_uniform(state->shader, "gl_MultiTexCoord0",
108                                    state->options->texcoord_state_tokens);
109    }
110    return nir_load_var(&state->b, state->bias);
111 }
112 
113 static void
lower_color(lower_drawpixels_state * state,nir_intrinsic_instr * intr)114 lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
115 {
116    nir_builder *b = &state->b;
117    nir_ssa_def *texcoord;
118    nir_tex_instr *tex;
119    nir_ssa_def *def;
120 
121    assert(intr->dest.is_ssa);
122 
123    b->cursor = nir_before_instr(&intr->instr);
124 
125    texcoord = get_texcoord(state);
126 
127    /* replace load_var(gl_Color) w/ texture sample:
128     *   TEX def, texcoord, drawpix_sampler, 2D
129     */
130    tex = nir_tex_instr_create(state->shader, 1);
131    tex->op = nir_texop_tex;
132    tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
133    tex->coord_components = 2;
134    tex->sampler_index = state->options->drawpix_sampler;
135    tex->texture_index = state->options->drawpix_sampler;
136    tex->dest_type = nir_type_float;
137    tex->src[0].src_type = nir_tex_src_coord;
138    tex->src[0].src =
139       nir_src_for_ssa(nir_channels(b, texcoord,
140                                    (1 << tex->coord_components) - 1));
141 
142    nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
143    nir_builder_instr_insert(b, &tex->instr);
144    def = &tex->dest.ssa;
145 
146    /* Apply the scale and bias. */
147    if (state->options->scale_and_bias) {
148       /* MAD def, def, scale, bias; */
149       def = nir_ffma(b, def, get_scale(state), get_bias(state));
150    }
151 
152    if (state->options->pixel_maps) {
153       static const unsigned swiz_xy[4] = {0,1};
154       static const unsigned swiz_zw[4] = {2,3};
155 
156       /* do four pixel map look-ups with two TEX instructions: */
157       nir_ssa_def *def_xy, *def_zw;
158 
159       /* TEX def.xy, def.xyyy, pixelmap_sampler, 2D; */
160       tex = nir_tex_instr_create(state->shader, 1);
161       tex->op = nir_texop_tex;
162       tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
163       tex->coord_components = 2;
164       tex->sampler_index = state->options->pixelmap_sampler;
165       tex->texture_index = state->options->pixelmap_sampler;
166       tex->dest_type = nir_type_float;
167       tex->src[0].src_type = nir_tex_src_coord;
168       tex->src[0].src = nir_src_for_ssa(nir_swizzle(b, def, swiz_xy, 2, true));
169 
170       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
171       nir_builder_instr_insert(b, &tex->instr);
172       def_xy = &tex->dest.ssa;
173 
174       /* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
175       tex = nir_tex_instr_create(state->shader, 1);
176       tex->op = nir_texop_tex;
177       tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
178       tex->coord_components = 2;
179       tex->sampler_index = state->options->pixelmap_sampler;
180       tex->dest_type = nir_type_float;
181       tex->src[0].src_type = nir_tex_src_coord;
182       tex->src[0].src = nir_src_for_ssa(nir_swizzle(b, def, swiz_zw, 2, true));
183 
184       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
185       nir_builder_instr_insert(b, &tex->instr);
186       def_zw = &tex->dest.ssa;
187 
188       /* def = vec4(def.xy, def.zw); */
189       def = nir_vec4(b,
190                      nir_channel(b, def_xy, 0),
191                      nir_channel(b, def_xy, 1),
192                      nir_channel(b, def_zw, 0),
193                      nir_channel(b, def_zw, 1));
194    }
195 
196    nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(def));
197 }
198 
199 static void
lower_texcoord(lower_drawpixels_state * state,nir_intrinsic_instr * intr)200 lower_texcoord(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
201 {
202    state->b.cursor = nir_before_instr(&intr->instr);
203 
204    nir_ssa_def *texcoord_const = get_texcoord_const(state);
205    nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(texcoord_const));
206 }
207 
208 static bool
lower_drawpixels_block(lower_drawpixels_state * state,nir_block * block)209 lower_drawpixels_block(lower_drawpixels_state *state, nir_block *block)
210 {
211    nir_foreach_instr_safe(instr, block) {
212       if (instr->type == nir_instr_type_intrinsic) {
213          nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
214          if (intr->intrinsic == nir_intrinsic_load_var) {
215             nir_deref_var *dvar = intr->variables[0];
216             nir_variable *var = dvar->var;
217 
218             if (var->data.location == VARYING_SLOT_COL0) {
219                /* gl_Color should not have array/struct derefs: */
220                assert(dvar->deref.child == NULL);
221                lower_color(state, intr);
222             } else if (var->data.location == VARYING_SLOT_TEX0) {
223                /* gl_TexCoord should not have array/struct derefs: */
224                assert(dvar->deref.child == NULL);
225                lower_texcoord(state, intr);
226             }
227          }
228       }
229    }
230 
231    return true;
232 }
233 
234 static void
lower_drawpixels_impl(lower_drawpixels_state * state,nir_function_impl * impl)235 lower_drawpixels_impl(lower_drawpixels_state *state, nir_function_impl *impl)
236 {
237    nir_builder_init(&state->b, impl);
238 
239    nir_foreach_block(block, impl) {
240       lower_drawpixels_block(state, block);
241    }
242    nir_metadata_preserve(impl, nir_metadata_block_index |
243                                nir_metadata_dominance);
244 }
245 
246 void
nir_lower_drawpixels(nir_shader * shader,const nir_lower_drawpixels_options * options)247 nir_lower_drawpixels(nir_shader *shader,
248                      const nir_lower_drawpixels_options *options)
249 {
250    lower_drawpixels_state state = {
251       .options = options,
252       .shader = shader,
253    };
254 
255    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
256 
257    nir_foreach_function(function, shader) {
258       if (function->impl)
259          lower_drawpixels_impl(&state, function->impl);
260    }
261 }
262