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