2 * Copyright © 2015 Red Hat
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:
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
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
25 #include "nir_builder.h"
27 /* Lower glDrawPixels().
29 * This is based on the logic in st_get_drawpix_shader() in TGSI compiler.
31 * Run before nir_lower_io.
35 const nir_lower_drawpixels_options
*options
;
38 nir_variable
*texcoord
, *scale
, *bias
, *tex
, *pixelmap
;
39 } lower_drawpixels_state
;
42 get_texcoord(lower_drawpixels_state
*state
)
44 if (state
->texcoord
== NULL
) {
45 nir_variable
*texcoord
= NULL
;
47 /* find gl_TexCoord, if it exists: */
48 nir_foreach_variable(var
, &state
->shader
->inputs
) {
49 if (var
->data
.location
== VARYING_SLOT_TEX0
) {
55 /* otherwise create it: */
56 if (texcoord
== NULL
) {
57 texcoord
= nir_variable_create(state
->shader
,
61 texcoord
->data
.location
= VARYING_SLOT_TEX0
;
64 state
->texcoord
= texcoord
;
66 return nir_load_var(&state
->b
, state
->texcoord
);
70 create_uniform(nir_shader
*shader
, const char *name
,
71 const gl_state_index16 state_tokens
[STATE_LENGTH
])
73 nir_variable
*var
= nir_variable_create(shader
,
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
));
85 get_scale(lower_drawpixels_state
*state
)
87 if (state
->scale
== NULL
) {
88 state
->scale
= create_uniform(state
->shader
, "gl_PTscale",
89 state
->options
->scale_state_tokens
);
91 return nir_load_var(&state
->b
, state
->scale
);
95 get_bias(lower_drawpixels_state
*state
)
97 if (state
->bias
== NULL
) {
98 state
->bias
= create_uniform(state
->shader
, "gl_PTbias",
99 state
->options
->bias_state_tokens
);
101 return nir_load_var(&state
->b
, state
->bias
);
105 get_texcoord_const(lower_drawpixels_state
*state
)
107 if (state
->bias
== NULL
) {
108 state
->bias
= create_uniform(state
->shader
, "gl_MultiTexCoord0",
109 state
->options
->texcoord_state_tokens
);
111 return nir_load_var(&state
->b
, state
->bias
);
115 lower_color(lower_drawpixels_state
*state
, nir_intrinsic_instr
*intr
)
117 nir_builder
*b
= &state
->b
;
118 nir_ssa_def
*texcoord
;
122 assert(intr
->dest
.is_ssa
);
124 b
->cursor
= nir_before_instr(&intr
->instr
);
126 texcoord
= get_texcoord(state
);
128 const struct glsl_type
*sampler2D
=
129 glsl_sampler_type(GLSL_SAMPLER_DIM_2D
, false, false, GLSL_TYPE_FLOAT
);
133 nir_variable_create(b
->shader
, nir_var_uniform
, sampler2D
, "drawpix");
134 state
->tex
->data
.binding
= state
->options
->drawpix_sampler
;
135 state
->tex
->data
.explicit_binding
= true;
136 state
->tex
->data
.how_declared
= nir_var_hidden
;
139 nir_deref_instr
*tex_deref
= nir_build_deref_var(b
, state
->tex
);
141 /* replace load_var(gl_Color) w/ texture sample:
142 * TEX def, texcoord, drawpix_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
->dest_type
= nir_type_float
;
149 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
150 tex
->src
[0].src
= nir_src_for_ssa(&tex_deref
->dest
.ssa
);
151 tex
->src
[1].src_type
= nir_tex_src_sampler_deref
;
152 tex
->src
[1].src
= nir_src_for_ssa(&tex_deref
->dest
.ssa
);
153 tex
->src
[2].src_type
= nir_tex_src_coord
;
155 nir_src_for_ssa(nir_channels(b
, texcoord
,
156 (1 << tex
->coord_components
) - 1));
158 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
159 nir_builder_instr_insert(b
, &tex
->instr
);
160 def
= &tex
->dest
.ssa
;
162 /* Apply the scale and bias. */
163 if (state
->options
->scale_and_bias
) {
164 /* MAD def, def, scale, bias; */
165 def
= nir_ffma(b
, def
, get_scale(state
), get_bias(state
));
168 if (state
->options
->pixel_maps
) {
169 if (!state
->pixelmap
) {
170 state
->pixelmap
= nir_variable_create(b
->shader
, nir_var_uniform
,
171 sampler2D
, "pixelmap");
172 state
->pixelmap
->data
.binding
= state
->options
->pixelmap_sampler
;
173 state
->pixelmap
->data
.explicit_binding
= true;
174 state
->pixelmap
->data
.how_declared
= nir_var_hidden
;
177 nir_deref_instr
*pixelmap_deref
=
178 nir_build_deref_var(b
, state
->pixelmap
);
180 /* do four pixel map look-ups with two TEX instructions: */
181 nir_ssa_def
*def_xy
, *def_zw
;
183 /* TEX def.xy, def.xyyy, pixelmap_sampler, 2D; */
184 tex
= nir_tex_instr_create(state
->shader
, 3);
185 tex
->op
= nir_texop_tex
;
186 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
187 tex
->coord_components
= 2;
188 tex
->sampler_index
= state
->options
->pixelmap_sampler
;
189 tex
->texture_index
= state
->options
->pixelmap_sampler
;
190 tex
->dest_type
= nir_type_float
;
191 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
192 tex
->src
[0].src
= nir_src_for_ssa(&pixelmap_deref
->dest
.ssa
);
193 tex
->src
[1].src_type
= nir_tex_src_sampler_deref
;
194 tex
->src
[1].src
= nir_src_for_ssa(&pixelmap_deref
->dest
.ssa
);
195 tex
->src
[2].src_type
= nir_tex_src_coord
;
196 tex
->src
[2].src
= nir_src_for_ssa(nir_channels(b
, def
, 0x3));
198 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
199 nir_builder_instr_insert(b
, &tex
->instr
);
200 def_xy
= &tex
->dest
.ssa
;
202 /* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
203 tex
= nir_tex_instr_create(state
->shader
, 1);
204 tex
->op
= nir_texop_tex
;
205 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
206 tex
->coord_components
= 2;
207 tex
->sampler_index
= state
->options
->pixelmap_sampler
;
208 tex
->dest_type
= nir_type_float
;
209 tex
->src
[0].src_type
= nir_tex_src_coord
;
210 tex
->src
[0].src
= nir_src_for_ssa(nir_channels(b
, def
, 0xc));
212 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
213 nir_builder_instr_insert(b
, &tex
->instr
);
214 def_zw
= &tex
->dest
.ssa
;
216 /* def = vec4(def.xy, def.zw); */
218 nir_channel(b
, def_xy
, 0),
219 nir_channel(b
, def_xy
, 1),
220 nir_channel(b
, def_zw
, 0),
221 nir_channel(b
, def_zw
, 1));
224 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
, nir_src_for_ssa(def
));
228 lower_texcoord(lower_drawpixels_state
*state
, nir_intrinsic_instr
*intr
)
230 state
->b
.cursor
= nir_before_instr(&intr
->instr
);
232 nir_ssa_def
*texcoord_const
= get_texcoord_const(state
);
233 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
, nir_src_for_ssa(texcoord_const
));
237 lower_drawpixels_block(lower_drawpixels_state
*state
, nir_block
*block
)
239 nir_foreach_instr_safe(instr
, block
) {
240 if (instr
->type
== nir_instr_type_intrinsic
) {
241 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
242 if (intr
->intrinsic
== nir_intrinsic_load_deref
) {
243 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
244 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
246 if (var
->data
.location
== VARYING_SLOT_COL0
) {
247 /* gl_Color should not have array/struct derefs: */
248 assert(deref
->deref_type
== nir_deref_type_var
);
249 lower_color(state
, intr
);
250 } else if (var
->data
.location
== VARYING_SLOT_TEX0
) {
251 /* gl_TexCoord should not have array/struct derefs: */
252 assert(deref
->deref_type
== nir_deref_type_var
);
253 lower_texcoord(state
, intr
);
263 lower_drawpixels_impl(lower_drawpixels_state
*state
, nir_function_impl
*impl
)
265 nir_builder_init(&state
->b
, impl
);
267 nir_foreach_block(block
, impl
) {
268 lower_drawpixels_block(state
, block
);
270 nir_metadata_preserve(impl
, nir_metadata_block_index
|
271 nir_metadata_dominance
);
275 nir_lower_drawpixels(nir_shader
*shader
,
276 const nir_lower_drawpixels_options
*options
)
278 lower_drawpixels_state state
= {
283 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
285 nir_foreach_function(function
, shader
) {
287 lower_drawpixels_impl(&state
, function
->impl
);