nir/algebraic: Make algebraic_parser_test.sh executable.
[mesa.git] / src / compiler / nir / nir_lower_drawpixels.c
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, *tex, *pixelmap;
39 } lower_drawpixels_state;
40
41 static nir_ssa_def *
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 *
70 create_uniform(nir_shader *shader, const char *name,
71 const gl_state_index16 state_tokens[STATE_LENGTH])
72 {
73 nir_variable *var = nir_variable_create(shader,
74 nir_var_uniform,
75 glsl_vec4_type(),
76 name);
77 var->num_state_slots = 1;
78 var->state_slots = ralloc_array(var, nir_state_slot, 1);
79 memcpy(var->state_slots[0].tokens, state_tokens,
80 sizeof(var->state_slots[0].tokens));
81 return var;
82 }
83
84 static nir_ssa_def *
85 get_scale(lower_drawpixels_state *state)
86 {
87 if (state->scale == NULL) {
88 state->scale = create_uniform(state->shader, "gl_PTscale",
89 state->options->scale_state_tokens);
90 }
91 return nir_load_var(&state->b, state->scale);
92 }
93
94 static nir_ssa_def *
95 get_bias(lower_drawpixels_state *state)
96 {
97 if (state->bias == NULL) {
98 state->bias = create_uniform(state->shader, "gl_PTbias",
99 state->options->bias_state_tokens);
100 }
101 return nir_load_var(&state->b, state->bias);
102 }
103
104 static nir_ssa_def *
105 get_texcoord_const(lower_drawpixels_state *state)
106 {
107 if (state->bias == NULL) {
108 state->bias = create_uniform(state->shader, "gl_MultiTexCoord0",
109 state->options->texcoord_state_tokens);
110 }
111 return nir_load_var(&state->b, state->bias);
112 }
113
114 static void
115 lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
116 {
117 nir_builder *b = &state->b;
118 nir_ssa_def *texcoord;
119 nir_tex_instr *tex;
120 nir_ssa_def *def;
121
122 assert(intr->dest.is_ssa);
123
124 b->cursor = nir_before_instr(&intr->instr);
125
126 texcoord = get_texcoord(state);
127
128 const struct glsl_type *sampler2D =
129 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
130
131 if (!state->tex) {
132 state->tex =
133 nir_variable_create(b->shader, nir_var_uniform, sampler2D, "drawpix");
134 state->tex->data.binding = state->options->drawpix_sampler;
135 }
136
137 /* replace load_var(gl_Color) w/ texture sample:
138 * TEX def, texcoord, drawpix_sampler, 2D
139 */
140 tex = nir_tex_instr_create(state->shader, 1);
141 tex->op = nir_texop_tex;
142 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
143 tex->coord_components = 2;
144 tex->sampler_index = state->options->drawpix_sampler;
145 tex->texture_index = state->options->drawpix_sampler;
146 tex->dest_type = nir_type_float;
147 tex->src[0].src_type = nir_tex_src_coord;
148 tex->src[0].src =
149 nir_src_for_ssa(nir_channels(b, texcoord,
150 (1 << tex->coord_components) - 1));
151
152 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
153 nir_builder_instr_insert(b, &tex->instr);
154 def = &tex->dest.ssa;
155
156 /* Apply the scale and bias. */
157 if (state->options->scale_and_bias) {
158 /* MAD def, def, scale, bias; */
159 def = nir_ffma(b, def, get_scale(state), get_bias(state));
160 }
161
162 if (state->options->pixel_maps) {
163 if (!state->pixelmap) {
164 state->pixelmap = nir_variable_create(b->shader, nir_var_uniform,
165 sampler2D, "pixelmap");
166 state->pixelmap->data.binding = state->options->pixelmap_sampler;
167 }
168
169 /* do four pixel map look-ups with two TEX instructions: */
170 nir_ssa_def *def_xy, *def_zw;
171
172 /* TEX def.xy, def.xyyy, 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->texture_index = state->options->pixelmap_sampler;
179 tex->dest_type = nir_type_float;
180 tex->src[0].src_type = nir_tex_src_coord;
181 tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0x3));
182
183 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
184 nir_builder_instr_insert(b, &tex->instr);
185 def_xy = &tex->dest.ssa;
186
187 /* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
188 tex = nir_tex_instr_create(state->shader, 1);
189 tex->op = nir_texop_tex;
190 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
191 tex->coord_components = 2;
192 tex->sampler_index = state->options->pixelmap_sampler;
193 tex->dest_type = nir_type_float;
194 tex->src[0].src_type = nir_tex_src_coord;
195 tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0xc));
196
197 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
198 nir_builder_instr_insert(b, &tex->instr);
199 def_zw = &tex->dest.ssa;
200
201 /* def = vec4(def.xy, def.zw); */
202 def = nir_vec4(b,
203 nir_channel(b, def_xy, 0),
204 nir_channel(b, def_xy, 1),
205 nir_channel(b, def_zw, 0),
206 nir_channel(b, def_zw, 1));
207 }
208
209 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(def));
210 }
211
212 static void
213 lower_texcoord(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
214 {
215 state->b.cursor = nir_before_instr(&intr->instr);
216
217 nir_ssa_def *texcoord_const = get_texcoord_const(state);
218 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(texcoord_const));
219 }
220
221 static bool
222 lower_drawpixels_block(lower_drawpixels_state *state, nir_block *block)
223 {
224 nir_foreach_instr_safe(instr, block) {
225 if (instr->type == nir_instr_type_intrinsic) {
226 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
227 if (intr->intrinsic == nir_intrinsic_load_deref) {
228 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
229 nir_variable *var = nir_deref_instr_get_variable(deref);
230
231 if (var->data.location == VARYING_SLOT_COL0) {
232 /* gl_Color should not have array/struct derefs: */
233 assert(deref->deref_type == nir_deref_type_var);
234 lower_color(state, intr);
235 } else if (var->data.location == VARYING_SLOT_TEX0) {
236 /* gl_TexCoord should not have array/struct derefs: */
237 assert(deref->deref_type == nir_deref_type_var);
238 lower_texcoord(state, intr);
239 }
240 }
241 }
242 }
243
244 return true;
245 }
246
247 static void
248 lower_drawpixels_impl(lower_drawpixels_state *state, nir_function_impl *impl)
249 {
250 nir_builder_init(&state->b, impl);
251
252 nir_foreach_block(block, impl) {
253 lower_drawpixels_block(state, block);
254 }
255 nir_metadata_preserve(impl, nir_metadata_block_index |
256 nir_metadata_dominance);
257 }
258
259 void
260 nir_lower_drawpixels(nir_shader *shader,
261 const nir_lower_drawpixels_options *options)
262 {
263 lower_drawpixels_state state = {
264 .options = options,
265 .shader = shader,
266 };
267
268 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
269
270 nir_foreach_function(function, shader) {
271 if (function->impl)
272 lower_drawpixels_impl(&state, function->impl);
273 }
274 }