draw: Fix build -- rename Size to NrTokens.
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_pstipple.c
index aec485a6e72cbb70ba417db15d5131c547effc62..a0f9716dac2e10df009eda6cc199bd9186cb3658 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 "tgsi/util/tgsi_transform.h"
-#include "tgsi/util/tgsi_dump.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
+
+#include "tgsi/tgsi_transform.h"
+#include "tgsi/tgsi_dump.h"
 
 #include "draw_context.h"
 #include "draw_pipe.h"
@@ -132,20 +134,20 @@ pstip_transform_decl(struct tgsi_transform_context *ctx,
 
    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++) {
          pctx->samplersUsed |= 1 << i;
       }
    }
    else if (decl->Declaration.File == TGSI_FILE_INPUT) {
-      pctx->maxInput = MAX2(pctx->maxInput, (int) decl->u.DeclarationRange.Last);
+      pctx->maxInput = MAX2(pctx->maxInput, (int) decl->DeclarationRange.Last);
       if (decl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION)
-         pctx->wincoordInput = (int) decl->u.DeclarationRange.First;
+         pctx->wincoordInput = (int) decl->DeclarationRange.First;
    }
    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++) {
          pctx->tempsUsed |= (1 << i);
       }
    }
@@ -223,28 +225,27 @@ pstip_transform_inst(struct tgsi_transform_context *ctx,
          /* declare new position input reg */
          decl = tgsi_default_full_declaration();
          decl.Declaration.File = TGSI_FILE_INPUT;
+         decl.Declaration.Interpolate = TGSI_INTERPOLATE_LINEAR; /* XXX? */
          decl.Declaration.Semantic = 1;
          decl.Semantic.SemanticName = TGSI_SEMANTIC_POSITION;
          decl.Semantic.SemanticIndex = 0;
-         decl.Declaration.Interpolate = 1;
-         decl.Interpolation.Interpolate = TGSI_INTERPOLATE_LINEAR; /* XXX? */
-         decl.u.DeclarationRange.First = 
-            decl.u.DeclarationRange.Last = wincoordInput;
+         decl.DeclarationRange.First = 
+            decl.DeclarationRange.Last = wincoordInput;
          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 = pctx->freeSampler;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = pctx->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 = pctx->texTemp;
+      decl.DeclarationRange.First = 
+      decl.DeclarationRange.Last = pctx->texTemp;
       ctx->emit_declaration(ctx, &decl);
 
       /* emit immediate = {1/32, 1/32, 1, 1}
@@ -255,8 +256,8 @@ pstip_transform_inst(struct tgsi_transform_context *ctx,
          struct tgsi_full_immediate immed;
          uint size = 4;
          immed = tgsi_default_full_immediate();
-         immed.Immediate.Size = 1 + size; /* one for the token itself */
-         immed.u.ImmediateFloat32 = (struct tgsi_immediate_float32 *) value;
+         immed.Immediate.NrTokens = 1 + size; /* one for the token itself */
+         immed.u.Pointer = (void *) value;
          ctx->emit_immediate(ctx, &immed);
       }
 
@@ -300,9 +301,9 @@ pstip_transform_inst(struct tgsi_transform_context *ctx,
       newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->freeSampler;
       ctx->emit_instruction(ctx, &newInst);
 
-      /* KILP texTemp;   # if texTemp < 0, KILL fragment */
+      /* KIL -texTemp;   # if -texTemp < 0, KILL fragment */
       newInst = tgsi_default_full_instruction();
-      newInst.Instruction.Opcode = TGSI_OPCODE_KILP;
+      newInst.Instruction.Opcode = TGSI_OPCODE_KIL;
       newInst.Instruction.NumDstRegs = 0;
       newInst.Instruction.NumSrcRegs = 1;
       newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
@@ -376,8 +377,14 @@ pstip_update_texture(struct pstip_stage *pstip)
    uint i, j;
    ubyte *data;
 
-   surface = screen->get_tex_surface(screen, pstip->texture, 0, 0, 0);
-   data = pipe_surface_map(surface);
+   /* XXX: want to avoid flushing just because we use stipple: 
+    */
+   pipe->flush( pipe, PIPE_FLUSH_TEXTURE_CACHE, NULL );
+
+   surface = screen->get_tex_surface(screen, pstip->texture, 0, 0, 0,
+                                     PIPE_BUFFER_USAGE_CPU_WRITE);
+   data = screen->surface_map(screen, surface,
+                              PIPE_BUFFER_USAGE_CPU_WRITE);
 
    /*
     * Load alpha texture.
@@ -389,19 +396,18 @@ pstip_update_texture(struct pstip_stage *pstip)
       for (j = 0; j < 32; j++) {
          if (stipple[i] & (bit31 >> j)) {
             /* fragment "on" */
-            data[i * surface->pitch + j] = 0;
+            data[i * surface->stride + j] = 0;
          }
          else {
             /* fragment "off" */
-            data[i * surface->pitch + j] = 255;
+            data[i * surface->stride + j] = 255;
          }
       }
    }
 
    /* unmap */
-   pipe_surface_unmap(surface);
-   pipe_surface_reference(&surface, NULL);
-   pipe->texture_update(pipe, pstip->texture, 0, 0x1);
+   screen->surface_unmap(screen, surface);
+   screen->tex_surface_release(screen, &surface);
 }
 
 
@@ -417,12 +423,12 @@ pstip_create_texture(struct pstip_stage *pstip)
 
    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 = 0;
    texTemp.width[0] = 32;
    texTemp.height[0] = 32;
    texTemp.depth[0] = 1;
-   texTemp.cpp = 1;
+   pf_get_block(texTemp.format, &texTemp.block);
 
    pstip->texture = screen->texture_create(screen, &texTemp);
    if (pstip->texture == NULL)
@@ -433,9 +439,7 @@ pstip_create_texture(struct pstip_stage *pstip)
 
 
 /**
- * Create the sampler CSO that'll be used for antialiasing.
- * By using a mipmapped texture, we don't have to generate a different
- * texture image for each line size.
+ * Create the sampler CSO that'll be used for stippling.
  */
 static boolean
 pstip_create_sampler(struct pstip_stage *pstip)
@@ -463,22 +467,24 @@ pstip_create_sampler(struct pstip_stage *pstip)
 
 
 /**
- * When we're about to draw our first AA line in a batch, this function is
- * called to tell the driver to bind our modified fragment shader.
+ * When we're about to draw our first stipple polygon in a batch, this function
+ * is called to tell the driver to bind our modified fragment shader.
  */
 static boolean
 bind_pstip_fragment_shader(struct pstip_stage *pstip)
 {
+   struct draw_context *draw = pstip->stage.draw;
    if (!pstip->fs->pstip_fs &&
        !generate_pstip_fs(pstip))
       return FALSE;
 
+   draw->suspend_flushing = TRUE;
    pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
+   draw->suspend_flushing = FALSE;
    return TRUE;
 }
 
 
-
 static INLINE struct pstip_stage *
 pstip_stage( struct draw_stage *stage )
 {
@@ -486,14 +492,12 @@ pstip_stage( struct draw_stage *stage )
 }
 
 
-
-
-
 static void
 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
 {
    struct pstip_stage *pstip = pstip_stage(stage);
    struct pipe_context *pipe = pstip->pipe;
+   struct draw_context *draw = stage->draw;
    uint num_samplers;
 
    assert(stage->draw->rasterizer->poly_stipple_enable);
@@ -518,10 +522,12 @@ pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
 
    assert(num_samplers <= PIPE_MAX_SAMPLERS);
 
+   draw->suspend_flushing = TRUE;
    pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers);
    pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures);
+   draw->suspend_flushing = FALSE;
 
-   /* now really draw first line */
+   /* now really draw first triangle */
    stage->tri = draw_pipe_passthrough_tri;
    stage->tri(stage, header);
 }
@@ -530,21 +536,21 @@ pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
 static void
 pstip_flush(struct draw_stage *stage, unsigned flags)
 {
-   /*struct draw_context *draw = stage->draw;*/
+   struct draw_context *draw = stage->draw;
    struct pstip_stage *pstip = pstip_stage(stage);
    struct pipe_context *pipe = pstip->pipe;
 
    stage->tri = pstip_first_tri;
    stage->next->flush( stage->next, flags );
 
-   /* restore original frag shader */
+   /* restore original frag shader, texture, sampler state */
+   draw->suspend_flushing = TRUE;
    pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs);
-
-   /* XXX restore original texture, sampler state */
    pstip->driver_bind_sampler_states(pipe, pstip->num_samplers,
                                      pstip->state.samplers);
    pstip->driver_set_sampler_textures(pipe, pstip->num_textures,
                                       pstip->state.textures);
+   draw->suspend_flushing = FALSE;
 }
 
 
@@ -559,6 +565,11 @@ static void
 pstip_destroy(struct draw_stage *stage)
 {
    struct pstip_stage *pstip = pstip_stage(stage);
+   uint i;
+
+   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
+      pipe_texture_reference(&pstip->state.textures[i], NULL);
+   }
 
    pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
 
@@ -689,8 +700,10 @@ pstip_set_polygon_stipple(struct pipe_context *pipe,
                           const struct pipe_poly_stipple *stipple)
 {
    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
+
    /* save current */
    pstip->state.stipple = stipple;
+
    /* pass-through */
    pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
 
@@ -698,11 +711,10 @@ pstip_set_polygon_stipple(struct pipe_context *pipe,
 }
 
 
-
 /**
- * Called by drivers that want to install this AA line prim stage
+ * Called by drivers that want to install this polygon stipple stage
  * into the draw module's pipeline.  This will not be used if the
- * hardware has native support for AA lines.
+ * hardware has native support for polygon stipple.
  */
 boolean
 draw_install_pstipple_stage(struct draw_context *draw,
@@ -713,7 +725,7 @@ draw_install_pstipple_stage(struct draw_context *draw,
    pipe->draw = (void *) draw;
 
    /*
-    * Create / install AA line drawing / prim stage
+    * Create / install pgon stipple drawing / prim stage
     */
    pstip = draw_pstip_stage( draw );
    if (pstip == NULL)