• 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 
32 typedef struct {
33    const nir_lower_drawpixels_options *options;
34    nir_shader *shader;
35    nir_variable *texcoord_const, *scale, *bias, *tex, *pixelmap;
36 } lower_drawpixels_state;
37 
38 static nir_def *
get_texcoord(nir_builder * b,lower_drawpixels_state * state)39 get_texcoord(nir_builder *b, lower_drawpixels_state *state)
40 {
41    nir_def *baryc =
42       nir_load_barycentric_pixel(b, 32, .interp_mode = INTERP_MODE_SMOOTH);
43    return nir_load_interpolated_input(b, 4, 32, baryc, nir_imm_int(b, 0),
44                                       .io_semantics.location = VARYING_SLOT_TEX0);
45 }
46 
47 static nir_def *
get_scale(nir_builder * b,lower_drawpixels_state * state)48 get_scale(nir_builder *b, lower_drawpixels_state *state)
49 {
50    if (state->scale == NULL) {
51       state->scale = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTscale",
52                                                state->options->scale_state_tokens);
53    }
54    return nir_load_var(b, state->scale);
55 }
56 
57 static nir_def *
get_bias(nir_builder * b,lower_drawpixels_state * state)58 get_bias(nir_builder *b, lower_drawpixels_state *state)
59 {
60    if (state->bias == NULL) {
61       state->bias = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTbias",
62                                               state->options->bias_state_tokens);
63    }
64    return nir_load_var(b, state->bias);
65 }
66 
67 static nir_def *
get_texcoord_const(nir_builder * b,lower_drawpixels_state * state)68 get_texcoord_const(nir_builder *b, lower_drawpixels_state *state)
69 {
70    if (state->texcoord_const == NULL) {
71       state->texcoord_const = nir_state_variable_create(state->shader, glsl_vec4_type(),
72                                                         "gl_MultiTexCoord0",
73                                                         state->options->texcoord_state_tokens);
74    }
75    return nir_load_var(b, state->texcoord_const);
76 }
77 
78 static bool
lower_color(nir_builder * b,lower_drawpixels_state * state,nir_intrinsic_instr * intr)79 lower_color(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *intr)
80 {
81    nir_def *texcoord;
82    nir_tex_instr *tex;
83    nir_def *def;
84 
85    b->cursor = nir_before_instr(&intr->instr);
86 
87    texcoord = get_texcoord(b, state);
88 
89    const struct glsl_type *sampler2D =
90       glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
91 
92    if (!state->tex) {
93       state->tex =
94          nir_variable_create(b->shader, nir_var_uniform, sampler2D, "drawpix");
95       state->tex->data.binding = state->options->drawpix_sampler;
96       state->tex->data.explicit_binding = true;
97       state->tex->data.how_declared = nir_var_hidden;
98    }
99 
100    nir_deref_instr *tex_deref = nir_build_deref_var(b, state->tex);
101 
102    /* replace load_var(gl_Color) w/ texture sample:
103     *   TEX def, texcoord, drawpix_sampler, 2D
104     */
105    tex = nir_tex_instr_create(state->shader, 3);
106    tex->op = nir_texop_tex;
107    tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
108    tex->coord_components = 2;
109    tex->dest_type = nir_type_float32;
110    tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
111                                      &tex_deref->def);
112    tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
113                                      &tex_deref->def);
114    tex->src[2] =
115       nir_tex_src_for_ssa(nir_tex_src_coord,
116                           nir_trim_vector(b, texcoord, tex->coord_components));
117 
118    nir_def_init(&tex->instr, &tex->def, 4, 32);
119    nir_builder_instr_insert(b, &tex->instr);
120    def = &tex->def;
121 
122    /* Apply the scale and bias. */
123    if (state->options->scale_and_bias) {
124       /* MAD def, def, scale, bias; */
125       def = nir_ffma(b, def, get_scale(b, state), get_bias(b, state));
126    }
127 
128    if (state->options->pixel_maps) {
129       if (!state->pixelmap) {
130          state->pixelmap = nir_variable_create(b->shader, nir_var_uniform,
131                                                sampler2D, "pixelmap");
132          state->pixelmap->data.binding = state->options->pixelmap_sampler;
133          state->pixelmap->data.explicit_binding = true;
134          state->pixelmap->data.how_declared = nir_var_hidden;
135       }
136 
137       nir_deref_instr *pixelmap_deref =
138          nir_build_deref_var(b, state->pixelmap);
139 
140       /* do four pixel map look-ups with two TEX instructions: */
141       nir_def *def_xy, *def_zw;
142 
143       /* TEX def.xy, def.xyyy, pixelmap_sampler, 2D; */
144       tex = nir_tex_instr_create(state->shader, 3);
145       tex->op = nir_texop_tex;
146       tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
147       tex->coord_components = 2;
148       tex->sampler_index = state->options->pixelmap_sampler;
149       tex->texture_index = state->options->pixelmap_sampler;
150       tex->dest_type = nir_type_float32;
151       tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
152                                         &pixelmap_deref->def);
153       tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_sampler_deref,
154                                         &pixelmap_deref->def);
155       tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_coord,
156                                         nir_trim_vector(b, def, 2));
157 
158       nir_def_init(&tex->instr, &tex->def, 4, 32);
159       nir_builder_instr_insert(b, &tex->instr);
160       def_xy = &tex->def;
161 
162       /* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
163       tex = nir_tex_instr_create(state->shader, 1);
164       tex->op = nir_texop_tex;
165       tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
166       tex->coord_components = 2;
167       tex->sampler_index = state->options->pixelmap_sampler;
168       tex->dest_type = nir_type_float32;
169       tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_coord,
170                                         nir_channels(b, def, 0xc));
171 
172       nir_def_init(&tex->instr, &tex->def, 4, 32);
173       nir_builder_instr_insert(b, &tex->instr);
174       def_zw = &tex->def;
175 
176       /* def = vec4(def.xy, def.zw); */
177       def = nir_vec4(b,
178                      nir_channel(b, def_xy, 0),
179                      nir_channel(b, def_xy, 1),
180                      nir_channel(b, def_zw, 0),
181                      nir_channel(b, def_zw, 1));
182    }
183 
184    nir_def_rewrite_uses(&intr->def, def);
185    return true;
186 }
187 
188 static bool
lower_texcoord(nir_builder * b,lower_drawpixels_state * state,nir_intrinsic_instr * intr)189 lower_texcoord(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *intr)
190 {
191    b->cursor = nir_before_instr(&intr->instr);
192 
193    nir_def *texcoord_const = get_texcoord_const(b, state);
194    nir_def_rewrite_uses(&intr->def, texcoord_const);
195    return true;
196 }
197 
198 static bool
lower_drawpixels_instr(nir_builder * b,nir_instr * instr,void * cb_data)199 lower_drawpixels_instr(nir_builder *b, nir_instr *instr, void *cb_data)
200 {
201    lower_drawpixels_state *state = cb_data;
202    if (instr->type != nir_instr_type_intrinsic)
203       return false;
204 
205    nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
206 
207    switch (intr->intrinsic) {
208    case nir_intrinsic_load_color0:
209       return lower_color(b, state, intr);
210 
211    case nir_intrinsic_load_interpolated_input:
212    case nir_intrinsic_load_input: {
213       if (nir_intrinsic_io_semantics(intr).location == VARYING_SLOT_TEX0)
214          return lower_texcoord(b, state, intr);
215       if (nir_intrinsic_io_semantics(intr).location == VARYING_SLOT_COL0)
216          return lower_color(b, state, intr);
217       break;
218    }
219    default:
220       break;
221    }
222 
223    return false;
224 }
225 
226 bool
nir_lower_drawpixels(nir_shader * shader,const nir_lower_drawpixels_options * options)227 nir_lower_drawpixels(nir_shader *shader,
228                      const nir_lower_drawpixels_options *options)
229 {
230    assert(shader->info.io_lowered);
231 
232    lower_drawpixels_state state = {
233       .options = options,
234       .shader = shader,
235    };
236 
237    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
238 
239    return nir_shader_instructions_pass(shader, lower_drawpixels_instr,
240                                        nir_metadata_control_flow,
241                                        &state);
242 }
243