gallium: call tgsi_set_exec_mask() and use exec mask in SSE ARL code
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_aaline.c
index ae1abf91c2307e93ca9986abce189a772c620b1d..20841bb5d69b0abda50c61354e803f399ad72cb0 100644 (file)
  */
 
 
-#include "pipe/p_util.h"
 #include "pipe/p_inlines.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_shader_tokens.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
 
-#include "tgsi/util/tgsi_transform.h"
-#include "tgsi/util/tgsi_dump.h"
+#include "tgsi/tgsi_transform.h"
+#include "tgsi/tgsi_dump.h"
 
 #include "draw_context.h"
 #include "draw_private.h"
@@ -78,6 +79,8 @@ struct aaline_stage
 
    /** For AA lines, this is the vertex attrib slot for the new texcoords */
    uint tex_slot;
+   /** position, not necessarily output zero */
+   uint pos_slot;
 
    void *sampler_cso;
    struct pipe_texture *texture;
@@ -141,18 +144,18 @@ aa_transform_decl(struct tgsi_transform_context *ctx,
    if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
        decl->Semantic.SemanticName == TGSI_SEMANTIC_COLOR &&
        decl->Semantic.SemanticIndex == 0) {
-      aactx->colorOutput = decl->u.DeclarationRange.First;
+      aactx->colorOutput = decl->DeclarationRange.First;
    }
    else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
       uint i;
-      for (i = decl->u.DeclarationRange.First;
-           i <= decl->u.DeclarationRange.Last; i++) {
+      for (i = decl->DeclarationRange.First;
+           i <= decl->DeclarationRange.Last; i++) {
          aactx->samplersUsed |= 1 << i;
       }
    }
    else if (decl->Declaration.File == TGSI_FILE_INPUT) {
-      if ((int) decl->u.DeclarationRange.Last > aactx->maxInput)
-         aactx->maxInput = decl->u.DeclarationRange.Last;
+      if ((int) decl->DeclarationRange.Last > aactx->maxInput)
+         aactx->maxInput = decl->DeclarationRange.Last;
       if (decl->Semantic.SemanticName == TGSI_SEMANTIC_GENERIC &&
            (int) decl->Semantic.SemanticIndex > aactx->maxGeneric) {
          aactx->maxGeneric = decl->Semantic.SemanticIndex;
@@ -160,8 +163,8 @@ aa_transform_decl(struct tgsi_transform_context *ctx,
    }
    else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
       uint i;
-      for (i = decl->u.DeclarationRange.First;
-           i <= decl->u.DeclarationRange.Last; i++) {
+      for (i = decl->DeclarationRange.First;
+           i <= decl->DeclarationRange.Last; i++) {
          aactx->tempsUsed |= (1 << i);
       }
    }
@@ -225,34 +228,33 @@ aa_transform_inst(struct tgsi_transform_context *ctx,
       /* declare new generic input/texcoord */
       decl = tgsi_default_full_declaration();
       decl.Declaration.File = TGSI_FILE_INPUT;
+      /* XXX this could be linear... */
+      decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
       decl.Declaration.Semantic = 1;
       decl.Semantic.SemanticName = TGSI_SEMANTIC_GENERIC;
       decl.Semantic.SemanticIndex = aactx->maxGeneric + 1;
-      decl.Declaration.Interpolate = 1;
-      /* XXX this could be linear... */
-      decl.Interpolation.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
-      decl.u.DeclarationRange.First = 
-      decl.u.DeclarationRange.Last = aactx->maxInput + 1;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = aactx->maxInput + 1;
       ctx->emit_declaration(ctx, &decl);
 
       /* declare new sampler */
       decl = tgsi_default_full_declaration();
       decl.Declaration.File = TGSI_FILE_SAMPLER;
-      decl.u.DeclarationRange.First = 
-      decl.u.DeclarationRange.Last = aactx->freeSampler;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = aactx->freeSampler;
       ctx->emit_declaration(ctx, &decl);
 
       /* declare new temp regs */
       decl = tgsi_default_full_declaration();
       decl.Declaration.File = TGSI_FILE_TEMPORARY;
-      decl.u.DeclarationRange.First = 
-      decl.u.DeclarationRange.Last = aactx->texTemp;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = aactx->texTemp;
       ctx->emit_declaration(ctx, &decl);
 
       decl = tgsi_default_full_declaration();
       decl.Declaration.File = TGSI_FILE_TEMPORARY;
-      decl.u.DeclarationRange.First = 
-      decl.u.DeclarationRange.Last = aactx->colorTemp;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = aactx->colorTemp;
       ctx->emit_declaration(ctx, &decl);
 
       aactx->firstInstruction = FALSE;
@@ -392,12 +394,12 @@ aaline_create_texture(struct aaline_stage *aaline)
 
    memset(&texTemp, 0, sizeof(texTemp));
    texTemp.target = PIPE_TEXTURE_2D;
-   texTemp.format = PIPE_FORMAT_U_A8; /* XXX verify supported by driver! */
+   texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
    texTemp.last_level = MAX_TEXTURE_LEVEL;
    texTemp.width[0] = 1 << MAX_TEXTURE_LEVEL;
    texTemp.height[0] = 1 << MAX_TEXTURE_LEVEL;
    texTemp.depth[0] = 1;
-   texTemp.cpp = 1;
+   pf_get_block(texTemp.format, &texTemp.block);
 
    aaline->texture = screen->texture_create(screen, &texTemp);
    if (!aaline->texture)
@@ -415,8 +417,11 @@ aaline_create_texture(struct aaline_stage *aaline)
 
       assert(aaline->texture->width[level] == aaline->texture->height[level]);
 
-      surface = screen->get_tex_surface(screen, aaline->texture, 0, level, 0);
-      data = pipe_surface_map(surface);
+      /* This texture is new, no need to flush. 
+       */
+      surface = screen->get_tex_surface(screen, aaline->texture, 0, level, 0,
+                                        PIPE_BUFFER_USAGE_CPU_WRITE);
+      data = screen->surface_map(screen, surface, PIPE_BUFFER_USAGE_CPU_WRITE);
       if (data == NULL)
          return FALSE;
 
@@ -435,14 +440,13 @@ aaline_create_texture(struct aaline_stage *aaline)
             else {
                d = 255;
             }
-            data[i * surface->pitch + j] = d;
+            data[i * surface->stride + j] = d;
          }
       }
 
       /* unmap */
-      pipe_surface_unmap(surface);
-      pipe_surface_reference(&surface, NULL);
-      pipe->texture_update(pipe, aaline->texture, 0, (1 << level));
+      screen->surface_unmap(screen, surface);
+      screen->tex_surface_release(screen, &surface);
    }
    return TRUE;
 }
@@ -485,11 +489,16 @@ aaline_create_sampler(struct aaline_stage *aaline)
 static boolean
 bind_aaline_fragment_shader(struct aaline_stage *aaline)
 {
+   struct draw_context *draw = aaline->stage.draw;
+
    if (!aaline->fs->aaline_fs && 
        !generate_aaline_fs(aaline))
       return FALSE;
 
+   draw->suspend_flushing = TRUE;
    aaline->driver_bind_fs_state(aaline->pipe, aaline->fs->aaline_fs);
+   draw->suspend_flushing = FALSE;
+
    return TRUE;
 }
 
@@ -514,9 +523,10 @@ aaline_line(struct draw_stage *stage, struct prim_header *header)
    struct prim_header tri;
    struct vertex_header *v[8];
    uint texPos = aaline->tex_slot;
+   uint posPos = aaline->pos_slot;
    float *pos, *tex;
-   float dx = header->v[1]->data[0][0] - header->v[0]->data[0][0];
-   float dy = header->v[1]->data[0][1] - header->v[0]->data[0][1];
+   float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
+   float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
    double a = atan2(dy, dx);
    float c_a = (float) cos(a), s_a = (float) sin(a);
    uint i;
@@ -543,35 +553,35 @@ aaline_line(struct draw_stage *stage, struct prim_header *header)
     */
 
    /* new verts */
-   pos = v[0]->data[0];
+   pos = v[0]->data[posPos];
    pos[0] += (-dx * c_a -  dy * s_a);
    pos[1] += (-dx * s_a +  dy * c_a);
 
-   pos = v[1]->data[0];
+   pos = v[1]->data[posPos];
    pos[0] += (-dx * c_a - -dy * s_a);
    pos[1] += (-dx * s_a + -dy * c_a);
 
-   pos = v[2]->data[0];
+   pos = v[2]->data[posPos];
    pos[0] += ( dx * c_a -  dy * s_a);
    pos[1] += ( dx * s_a +  dy * c_a);
 
-   pos = v[3]->data[0];
+   pos = v[3]->data[posPos];
    pos[0] += ( dx * c_a - -dy * s_a);
    pos[1] += ( dx * s_a + -dy * c_a);
 
-   pos = v[4]->data[0];
+   pos = v[4]->data[posPos];
    pos[0] += (-dx * c_a -  dy * s_a);
    pos[1] += (-dx * s_a +  dy * c_a);
 
-   pos = v[5]->data[0];
+   pos = v[5]->data[posPos];
    pos[0] += (-dx * c_a - -dy * s_a);
    pos[1] += (-dx * s_a + -dy * c_a);
 
-   pos = v[6]->data[0];
+   pos = v[6]->data[posPos];
    pos[0] += ( dx * c_a -  dy * s_a);
    pos[1] += ( dx * s_a +  dy * c_a);
 
-   pos = v[7]->data[0];
+   pos = v[7]->data[posPos];
    pos[0] += ( dx * c_a - -dy * s_a);
    pos[1] += ( dx * s_a + -dy * c_a);
 
@@ -641,12 +651,13 @@ aaline_first_line(struct draw_stage *stage, struct prim_header *header)
     */
    if (!bind_aaline_fragment_shader(aaline)) {
       stage->line = draw_pipe_passthrough_line;
+      stage->line(stage, header);
       return;
    }
 
    /* update vertex attrib info */
-   aaline->tex_slot = draw->num_vs_outputs;
-   assert(aaline->tex_slot > 0); /* output[0] is vertex pos */
+   aaline->tex_slot = draw->vs.num_vs_outputs;
+   aaline->pos_slot = draw->vs.position_output;
 
    /* advertise the extra post-transformed vertex attribute */
    draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
@@ -662,8 +673,10 @@ aaline_first_line(struct draw_stage *stage, struct prim_header *header)
    pipe_texture_reference(&aaline->state.texture[aaline->fs->sampler_unit],
                           aaline->texture);
 
+   draw->suspend_flushing = TRUE;
    aaline->driver_bind_sampler_states(pipe, num_samplers, aaline->state.sampler);
    aaline->driver_set_sampler_textures(pipe, num_samplers, aaline->state.texture);
+   draw->suspend_flushing = FALSE;
 
    /* now really draw first line */
    stage->line = aaline_line;
@@ -681,14 +694,14 @@ aaline_flush(struct draw_stage *stage, unsigned flags)
    stage->line = aaline_first_line;
    stage->next->flush( stage->next, flags );
 
-   /* restore original frag shader */
+   /* restore original frag shader, texture, sampler state */
+   draw->suspend_flushing = TRUE;
    aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
-
-   /* XXX restore original texture, sampler state */
    aaline->driver_bind_sampler_states(pipe, aaline->num_samplers,
                                       aaline->state.sampler);
    aaline->driver_set_sampler_textures(pipe, aaline->num_textures,
                                        aaline->state.texture);
+   draw->suspend_flushing = FALSE;
 
    draw->extra_vp_outputs.slot = 0;
 }
@@ -705,6 +718,11 @@ static void
 aaline_destroy(struct draw_stage *stage)
 {
    struct aaline_stage *aaline = aaline_stage(stage);
+   uint i;
+
+   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
+      pipe_texture_reference(&aaline->state.texture[i], NULL);
+   }
 
    if (aaline->sampler_cso)
       aaline->pipe->delete_sampler_state(aaline->pipe, aaline->sampler_cso);
@@ -725,7 +743,7 @@ draw_aaline_stage(struct draw_context *draw)
    if (aaline == NULL)
       return NULL;
 
-   if (draw_alloc_temp_verts( &aaline->stage, 8 ))
+   if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
       goto fail;
 
    aaline->stage.draw = draw;
@@ -765,13 +783,13 @@ aaline_create_fs_state(struct pipe_context *pipe,
 {
    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
+   if (aafs == NULL)
+      return NULL;
 
-   if (aafs) {
-      aafs->state = *fs;
+   aafs->state = *fs;
 
-      /* pass-through */
-      aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
-   }
+   /* pass-through */
+   aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
 
    return aafs;
 }
@@ -782,6 +800,7 @@ aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
 {
    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
    struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
+
    /* save current */
    aaline->fs = aafs;
    /* pass-through */
@@ -806,9 +825,11 @@ aaline_bind_sampler_states(struct pipe_context *pipe,
                            unsigned num, void **sampler)
 {
    struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
+
    /* save current */
    memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
    aaline->num_samplers = num;
+
    /* pass-through */
    aaline->driver_bind_sampler_states(aaline->pipe, num, sampler);
 }
@@ -825,6 +846,9 @@ aaline_set_sampler_textures(struct pipe_context *pipe,
    for (i = 0; i < num; i++) {
       pipe_texture_reference(&aaline->state.texture[i], texture[i]);
    }
+   for ( ; i < PIPE_MAX_SAMPLERS; i++) {
+      pipe_texture_reference(&aaline->state.texture[i], NULL);
+   }
    aaline->num_textures = num;
 
    /* pass-through */