nir: Support deref instructions in lower_drawpixels
[mesa.git] / src / compiler / nir / nir_lower_drawpixels.c
index a71bccddfb07e1a62496e864e5fe4f6dc681d641..05131affdb29282aebe2c037950256b16b221a34 100644 (file)
@@ -67,7 +67,8 @@ get_texcoord(lower_drawpixels_state *state)
 }
 
 static nir_variable *
-create_uniform(nir_shader *shader, const char *name, const int state_tokens[5])
+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,
@@ -134,7 +135,10 @@ lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
    tex->sampler_index = state->options->drawpix_sampler;
    tex->texture_index = state->options->drawpix_sampler;
    tex->dest_type = nir_type_float;
-   tex->src[0].src = nir_src_for_ssa(texcoord);
+   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);
@@ -161,6 +165,7 @@ lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
       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_swizzle(b, def, swiz_xy, 2, true));
 
       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
@@ -174,6 +179,7 @@ lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
       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_swizzle(b, def, swiz_zw, 2, true));
 
       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
@@ -194,6 +200,8 @@ lower_color(lower_drawpixels_state *state, nir_intrinsic_instr *intr)
 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));
 }
@@ -204,16 +212,29 @@ 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_var) {
+         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);
+            }
+         } else if (intr->intrinsic == nir_intrinsic_load_var) {
             nir_deref_var *dvar = intr->variables[0];
             nir_variable *var = dvar->var;
 
             if (var->data.location == VARYING_SLOT_COL0) {
-               /* gl_Color should not have array/struct deref's: */
+               /* gl_Color should not have array/struct derefs: */
                assert(dvar->deref.child == NULL);
                lower_color(state, intr);
             } else if (var->data.location == VARYING_SLOT_TEX0) {
-               /* gl_TexCoord should not have array/struct deref's: */
+               /* gl_TexCoord should not have array/struct derefs: */
                assert(dvar->deref.child == NULL);
                lower_texcoord(state, intr);
             }
@@ -245,7 +266,7 @@ nir_lower_drawpixels(nir_shader *shader,
       .shader = shader,
    };
 
-   assert(shader->stage == MESA_SHADER_FRAGMENT);
+   assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
    nir_foreach_function(function, shader) {
       if (function->impl)