Merge branch 'master' into asm-shader-rework-2
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sf.c
index fd7fb9659f7d3902a99f2c8b1e83020431ed54b2..e1c2c7777b518b0875d4c0e4274d703f024403a0 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
  Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
@@ -31,9 +30,9 @@
   */
   
 
-#include "glheader.h"
-#include "macros.h"
-#include "enums.h"
+#include "main/glheader.h"
+#include "main/macros.h"
+#include "main/enums.h"
 
 #include "intel_batchbuffer.h"
 
 #include "brw_sf.h"
 #include "brw_state.h"
 
-#define DO_SETUP_BITS ((1<<(FRAG_ATTRIB_MAX)) - 1)
-
 static void compile_sf_prog( struct brw_context *brw,
                             struct brw_sf_prog_key *key )
 {
+   GLcontext *ctx = &brw->intel.ctx;
    struct brw_sf_compile c;
    const GLuint *program;
    GLuint program_size;
@@ -58,7 +56,7 @@ static void compile_sf_prog( struct brw_context *brw,
 
    /* Begin the compilation:
     */
-   brw_init_compile(&c.func);
+   brw_init_compile(brw, &c.func);
 
    c.key = *key;
    c.nr_attrs = brw_count_bits(c.key.attrs);
@@ -76,10 +74,12 @@ static void compile_sf_prog( struct brw_context *brw,
         c.attr_to_idx[i] = idx;
         c.idx_to_attr[idx] = i;
         if (i >= VERT_RESULT_TEX0 && i <= VERT_RESULT_TEX7) {
-                c.point_attrs[i].CoordReplace = 
-                       brw->attribs.Point->CoordReplace[i - VERT_RESULT_TEX0];
-        } else
-                c.point_attrs[i].CoordReplace = GL_FALSE;
+            c.point_attrs[i].CoordReplace = 
+               ctx->Point.CoordReplace[i - VERT_RESULT_TEX0];
+        }
+         else {
+            c.point_attrs[i].CoordReplace = GL_FALSE;
+         }
         idx++;
       }
    
@@ -88,18 +88,18 @@ static void compile_sf_prog( struct brw_context *brw,
    switch (key->primitive) {
    case SF_TRIANGLES:
       c.nr_verts = 3;
-      brw_emit_tri_setup( &c );
+      brw_emit_tri_setup( &c, GL_TRUE );
       break;
    case SF_LINES:
       c.nr_verts = 2;
-      brw_emit_line_setup( &c );
+      brw_emit_line_setup( &c, GL_TRUE );
       break;
    case SF_POINTS:
       c.nr_verts = 1;
       if (key->do_point_sprite)
-         brw_emit_point_sprite_setup( &c );
+         brw_emit_point_sprite_setup( &c, GL_TRUE );
       else
-         brw_emit_point_setup( &c );
+         brw_emit_point_setup( &c, GL_TRUE );
       break;
    case SF_UNFILLED_TRIS:
       c.nr_verts = 3;
@@ -109,7 +109,6 @@ static void compile_sf_prog( struct brw_context *brw,
       assert(0);
       return;
    }
-        
 
    /* get the program
     */
@@ -117,22 +116,21 @@ static void compile_sf_prog( struct brw_context *brw,
 
    /* Upload
     */
-   brw->sf.prog_gs_offset = brw_upload_cache( &brw->cache[BRW_SF_PROG],
-                                             &c.key,
-                                             sizeof(c.key),
-                                             program,
-                                             program_size,
-                                             &c.prog_data,
-                                             &brw->sf.prog_data );
+   dri_bo_unreference(brw->sf.prog_bo);
+   brw->sf.prog_bo = brw_upload_cache( &brw->cache, BRW_SF_PROG,
+                                      &c.key, sizeof(c.key),
+                                      NULL, 0,
+                                      program, program_size,
+                                      &c.prog_data,
+                                      &brw->sf.prog_data );
 }
 
 /* Calculate interpolants for triangle and line rasterization.
  */
-static void upload_sf_prog( struct brw_context *brw )
+static void upload_sf_prog(struct brw_context *brw)
 {
+   GLcontext *ctx = &brw->intel.ctx;
    struct brw_sf_prog_key key;
-   struct brw_sf_prog_data *prog_data;
-   uint32_t offset;
 
    memset(&key, 0, sizeof(key));
 
@@ -162,42 +160,41 @@ static void upload_sf_prog( struct brw_context *brw )
       break;
    }
 
-   key.do_point_sprite = brw->attribs.Point->PointSprite;
-   key.SpriteOrigin = brw->attribs.Point->SpriteOrigin;
+   key.do_point_sprite = ctx->Point.PointSprite;
+   key.SpriteOrigin = ctx->Point.SpriteOrigin;
    /* _NEW_LIGHT */
-   key.do_flat_shading = (brw->attribs.Light->ShadeModel == GL_FLAT);
-   key.do_twoside_color = (brw->attribs.Light->Enabled && brw->attribs.Light->Model.TwoSide);
+   key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT);
+   key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
+
+   /* _NEW_HINT */
+   key.linear_color = (ctx->Hint.PerspectiveCorrection == GL_FASTEST);
 
    /* _NEW_POLYGON */
-   if (key.do_twoside_color)
-      key.frontface_ccw = (brw->attribs.Polygon->FrontFace == GL_CCW);
-
-   if (brw_search_cache(&brw->cache[BRW_SF_PROG],
-                       &key, sizeof(key),
-                       &prog_data,
-                       &offset)) {
-      if (offset != brw->sf.prog_gs_offset ||
-         !brw->sf.prog_data ||
-         memcmp(prog_data, &brw->sf.prog_data,
-                sizeof(*brw->sf.prog_data)) != 0)
-      {
-        brw->sf.prog_gs_offset = offset;
-        brw->sf.prog_data = prog_data;
-        brw->state.dirty.cache |= CACHE_NEW_SF_PROG;
-      }
-   } else {
-      compile_sf_prog( brw, &key );
-      brw->state.dirty.cache |= CACHE_NEW_SF_PROG;
+   if (key.do_twoside_color) {
+      /* If we're rendering to a FBO, we have to invert the polygon
+       * face orientation, just as we invert the viewport in
+       * sf_unit_create_from_key().  ctx->DrawBuffer->Name will be
+       * nonzero if we're rendering to such an FBO.
+       */
+      key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) ^ (ctx->DrawBuffer->Name != 0);
    }
+
+   dri_bo_unreference(brw->sf.prog_bo);
+   brw->sf.prog_bo = brw_search_cache(&brw->cache, BRW_SF_PROG,
+                                     &key, sizeof(key),
+                                     NULL, 0,
+                                     &brw->sf.prog_data);
+   if (brw->sf.prog_bo == NULL)
+      compile_sf_prog( brw, &key );
 }
 
 
 const struct brw_tracked_state brw_sf_prog = {
    .dirty = {
-      .mesa  = (_NEW_LIGHT|_NEW_POLYGON|_NEW_POINT),
+      .mesa  = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT),
       .brw   = (BRW_NEW_REDUCED_PRIMITIVE),
       .cache = CACHE_NEW_VS_PROG
    },
-   .update = upload_sf_prog
+   .prepare = upload_sf_prog
 };