summaryrefslogtreecommitdiffstats
path: root/src/compiler/nir/nir_lower_drawpixels.c
blob: 462b9c308b2649bd301d0728f624e4acb0ea3972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright © 2015 Red Hat
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "nir.h"
#include "nir_builder.h"

/* Lower glDrawPixels().
 *
 * This is based on the logic in st_get_drawpix_shader() in TGSI compiler.
 *
 * Run before nir_lower_io.
 */

typedef struct {
   const nir_lower_drawpixels_options *options;
   nir_shader   *shader;
   nir_builder   b;
   nir_variable *texcoord, *scale, *bias;
} lower_drawpixels_state;

static nir_ssa_def *
get_texcoord(lower_drawpixels_state *state)
{
   if (state->texcoord == NULL) {
      nir_variable *texcoord = NULL;

      /* find gl_TexCoord, if it exists: */
      nir_foreach_variable(var, &state->shader->inputs) {
         if (var->data.location == VARYING_SLOT_TEX0) {
            texcoord = var;
            break;
         }
      }

      /* otherwise create it: */
      if (texcoord == NULL) {
         texcoord = nir_variable_create(state->shader,
                                        nir_var_shader_in,
                                        glsl_vec4_type(),
                                        "gl_TexCoord");
         texcoord->data.location = VARYING_SLOT_TEX0;
      }

      state->texcoord = texcoord;
   }
   return nir_load_var(&state->b, state->texcoord);
}

static nir_variable *
create_uniform(nir_shader *shader, const char *name,
               const gl_state_index16 state_tokens[STATE_LENGTH])
{
   nir_variable *var = nir_variable_create(shader,
                                           nir_var_uniform,
                                           glsl_vec4_type(),
                                           name);
   var->num_state_slots = 1;
   var->state_slots = ralloc_array(var, nir_state_slot, 1);
   memcpy(var->state_slots[0].tokens, state_tokens,
          sizeof(var->state_slots[0].tokens));
   return var;
}

static nir_ssa_def *
get_scale(lower_drawpixels_state *state)
{
   if (state->scale == NULL) {
      state->scale = create_uniform(state->shader, "gl_PTscale",
                                    state->options->scale_state_tokens);
   }
   return nir_load_var(&state->b, state->scale);
}

static nir_ssa_def *
get_bias(lower_drawpixels_state *state)
{
   if (state->bias == NULL) {
      state->bias = create_uniform(state->shader, "gl_PTbias",
                                   state->options->bias_state_tokens);
   }
   return nir_load_var(&state->b, state->bias);
}

static nir_ssa_def *
get_texcoord_const(lower_drawpixels_state *state)
{
   if (state->bias == NULL) {
      state->bias = create_uniform(state->shader, "gl_MultiTexCoord0",
                                   state->options->texcoord_state_tokens);
   }
   return nir_load_var(&state->b, state->bias);
}

static void
lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
{
   nir_builder *b = &state->b;
   nir_ssa_def *texcoord;
   nir_tex_instr *tex;
   nir_ssa_def *def;

   assert(intr->dest.is_ssa);

   b->cursor = nir_before_instr(&intr->instr);

   texcoord = get_texcoord(state);

   /* replace load_var(gl_Color) w/ texture sample:
    *   TEX def, texcoord, drawpix_sampler, 2D
    */
   tex = nir_tex_instr_create(state->shader, 1);
   tex->op = nir_texop_tex;
   tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
   tex->coord_components = 2;
   tex->sampler_index = state->options->drawpix_sampler;
   tex->texture_index = state->options->drawpix_sampler;
   tex->dest_type = nir_type_float;
   tex->src[0].src_type = nir_tex_src_coord;
   tex->src[0].src =
      nir_src_for_ssa(nir_channels(b, texcoord,
                                   (1 << tex->coord_components) - 1));

   nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
   nir_builder_instr_insert(b, &tex->instr);
   def = &tex->dest.ssa;

   /* Apply the scale and bias. */
   if (state->options->scale_and_bias) {
      /* MAD def, def, scale, bias; */
      def = nir_ffma(b, def, get_scale(state), get_bias(state));
   }

   if (state->options->pixel_maps) {
      /* do four pixel map look-ups with two TEX instructions: */
      nir_ssa_def *def_xy, *def_zw;

      /* TEX def.xy, def.xyyy, pixelmap_sampler, 2D; */
      tex = nir_tex_instr_create(state->shader, 1);
      tex->op = nir_texop_tex;
      tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
      tex->coord_components = 2;
      tex->sampler_index = state->options->pixelmap_sampler;
      tex->texture_index = state->options->pixelmap_sampler;
      tex->dest_type = nir_type_float;
      tex->src[0].src_type = nir_tex_src_coord;
      tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0x3));

      nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
      nir_builder_instr_insert(b, &tex->instr);
      def_xy = &tex->dest.ssa;

      /* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
      tex = nir_tex_instr_create(state->shader, 1);
      tex->op = nir_texop_tex;
      tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
      tex->coord_components = 2;
      tex->sampler_index = state->options->pixelmap_sampler;
      tex->dest_type = nir_type_float;
      tex->src[0].src_type = nir_tex_src_coord;
      tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0xc));

      nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
      nir_builder_instr_insert(b, &tex->instr);
      def_zw = &tex->dest.ssa;

      /* def = vec4(def.xy, def.zw); */
      def = nir_vec4(b,
                     nir_channel(b, def_xy, 0),
                     nir_channel(b, def_xy, 1),
                     nir_channel(b, def_zw, 0),
                     nir_channel(b, def_zw, 1));
   }

   nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(def));
}

static void
lower_texcoord(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
{
   state->b.cursor = nir_before_instr(&intr->instr);

   nir_ssa_def *texcoord_const = get_texcoord_const(state);
   nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(texcoord_const));
}

static bool
lower_drawpixels_block(lower_drawpixels_state *state, nir_block *block)
{
   nir_foreach_instr_safe(instr, block) {
      if (instr->type == nir_instr_type_intrinsic) {
         nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
         if (intr->intrinsic == nir_intrinsic_load_deref) {
            nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
            nir_variable *var = nir_deref_instr_get_variable(deref);

            if (var->data.location == VARYING_SLOT_COL0) {
               /* gl_Color should not have array/struct derefs: */
               assert(deref->deref_type == nir_deref_type_var);
               lower_color(state, intr);
            } else if (var->data.location == VARYING_SLOT_TEX0) {
               /* gl_TexCoord should not have array/struct derefs: */
               assert(deref->deref_type == nir_deref_type_var);
               lower_texcoord(state, intr);
            }
         }
      }
   }

   return true;
}

static void
lower_drawpixels_impl(lower_drawpixels_state *state, nir_function_impl *impl)
{
   nir_builder_init(&state->b, impl);

   nir_foreach_block(block, impl) {
      lower_drawpixels_block(state, block);
   }
   nir_metadata_preserve(impl, nir_metadata_block_index |
                               nir_metadata_dominance);
}

void
nir_lower_drawpixels(nir_shader *shader,
                     const nir_lower_drawpixels_options *options)
{
   lower_drawpixels_state state = {
      .options = options,
      .shader = shader,
   };

   assert(shader->info.stage == MESA_SHADER_FRAGMENT);

   nir_foreach_function(function, shader) {
      if (function->impl)
         lower_drawpixels_impl(&state, function->impl);
   }
}