New vbo_set_draw_func() to keep vbo context opaque to state tracker and tnl module.
[mesa.git] / src / mesa / tnl / t_context.c
index 457aa1c85fc0f2d116ff4dc97eabeccaffc057d7..0ace5c2d6f182f9994cf2189d2c10b48cafc1b4f 100644 (file)
-#include "mtypes.h"
-#include "mem.h"
-#include "vtxfmt.h"
-
-#include "t_context.h"
-#include "t_clip.h"
-#include "t_cva.h"
-#include "t_dlist.h"
-#include "t_eval.h"
-#include "t_pipeline.h"
-#include "t_shade.h"
-#include "t_light.h"
-#include "t_texture.h"
-#include "t_stages.h"
-#include "t_varray.h"
-#include "t_vb.h"
-#include "t_vbrender.h"
-#include "t_vbxform.h"
-#include "t_vtxfmt.h"
-#include "tnl.h"
-
-#if !defined(THREADS)
-struct immediate *_tnl_CurrentInput = NULL;
-#endif
-
-
-GLboolean
-_tnl_flush_vertices( GLcontext *ctx, GLuint flush_flags )
-{
-   TNLcontext *tnl = TNL_CONTEXT(ctx);
-   struct immediate *IM = TNL_CURRENT_IM(ctx);
-
-   if ((IM->Flag[IM->Count] & (VERT_BEGIN|VERT_END)) != VERT_END ||
-       (flush_flags & (FLUSH_STORED_VERTICES|FLUSH_UPDATE_CURRENT)))
-   {
-      if (IM->Flag[IM->Start]) 
-        _tnl_maybe_transform_vb( IM );
-      
-      /* Although this code updates the ctx->Current values, that bit
-       * is left set as there is no easy mechanism to set it
-       * elsewhere.  This means that each time core wants to examine
-       * ctx->Current, this function will be called.  After the first
-       * time, however, it will be a no-op.
-       */
-      ctx->Driver.NeedFlush &= ~(FLUSH_STORED_VERTICES |
-                                FLUSH_INSIDE_BEGIN_END);
-
-      return (tnl->_CurrentPrimitive == GL_POLYGON+1);
-   }
-   else
-      return GL_TRUE;
-}
-
-
-
- void
-_tnl_MakeCurrent( GLcontext *ctx, 
-                 GLframebuffer *drawBuffer, 
-                 GLframebuffer *readBuffer )
-{
-#ifndef THREADS
-   SET_IMMEDIATE( ctx, TNL_VB(ctx)->IM );
-#endif
-}
-
-
-/* Update all state that references _NeedEyeCoords 
+/*
+ * Mesa 3-D graphics library
+ * Version:  6.5.2
+ *
+ * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
+ *
+ * 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 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
+ * BRIAN PAUL 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.
+ *
+ * Authors:
+ *    Keith Whitwell <keith@tungstengraphics.com>
  */
- void
-_tnl_LightingSpaceChange( GLcontext *ctx )
-{
-   _tnl_update_normal_transform( ctx ); 
-}
 
 
-static void
-install_driver_callbacks( GLcontext *ctx )
-{
-   ctx->Driver.RenderVBCulledTab = _tnl_render_tab_cull;
-   ctx->Driver.RenderVBClippedTab = _tnl_render_tab_clipped;
-   ctx->Driver.RenderVBRawTab = _tnl_render_tab_raw;
-   ctx->Driver.NewList = _tnl_NewList;
-   ctx->Driver.EndList = _tnl_EndList;
-   ctx->Driver.FlushVertices = _tnl_flush_vertices;
-   ctx->Driver.NeedFlush = FLUSH_UPDATE_CURRENT;
-   ctx->Driver.LightingSpaceChange = _tnl_LightingSpaceChange;
-   ctx->Driver.MakeCurrent = _tnl_MakeCurrent;
-   ctx->Driver.VertexPointer = _tnl_VertexPointer;
-   ctx->Driver.NormalPointer = _tnl_NormalPointer;
-   ctx->Driver.ColorPointer = _tnl_ColorPointer;
-   ctx->Driver.FogCoordPointer = _tnl_FogCoordPointer;
-   ctx->Driver.IndexPointer = _tnl_IndexPointer;
-   ctx->Driver.SecondaryColorPointer = _tnl_SecondaryColorPointer;
-   ctx->Driver.TexCoordPointer = _tnl_TexCoordPointer;
-   ctx->Driver.EdgeFlagPointer = _tnl_EdgeFlagPointer;
-   ctx->Driver.LockArraysEXT = _tnl_LockArraysEXT;
-   ctx->Driver.UnlockArraysEXT = _tnl_UnlockArraysEXT;
-}
+#include "main/glheader.h"
+#include "main/imports.h"
+#include "main/context.h"
+#include "main/macros.h"
+#include "main/mtypes.h"
+#include "main/light.h"
 
+#include "tnl.h"
+#include "t_context.h"
+#include "t_pipeline.h"
+#include "t_vp_build.h"
 
+#include "vbo/vbo.h"
 
 GLboolean
 _tnl_CreateContext( GLcontext *ctx )
 {
    TNLcontext *tnl;
-   static int firsttime = 1;
-
-   /* Onetime initializations.  Doesn't really matter if this gets
-    * done twice: no need for mutexes.
-    */
-   if (firsttime) {
-      firsttime = 0;
-      _tnl_clip_init();
-      _tnl_eval_init();
-      _tnl_shade_init();
-      _tnl_texture_init();
-      _tnl_trans_elt_init();
-      _tnl_vbrender_init();
-      _tnl_stages_init();
-   }
 
    /* Create the TNLcontext structure
     */
-   ctx->swtnl_context = tnl = CALLOC( sizeof(TNLcontext) );
+   ctx->swtnl_context = tnl = (TNLcontext *) CALLOC( sizeof(TNLcontext) );
+
    if (!tnl) {
       return GL_FALSE;
    }
 
-   /* Create and hook in the data structures available from ctx.
+   /* Initialize the VB.
     */
-   ctx->swtnl_vb = (void *)_tnl_vb_create_for_immediate( ctx );
-   if (!ctx->swtnl_vb) {
-      FREE(tnl);
-      ctx->swtnl_context = 0;
-      return GL_FALSE;
-   }
-
-   ctx->swtnl_im = (void *)TNL_VB(ctx)->IM;
+   tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
 
 
    /* Initialize tnl state.
     */
-   _tnl_dlist_init( ctx );
-   _tnl_pipeline_init( ctx );
-   _tnl_vtxfmt_init( ctx );
-   _tnl_cva_init( ctx );
-
-   _tnl_reset_vb( TNL_VB(ctx) );
-   _tnl_reset_input( ctx, 0, 0 );      /* initially outside begin/end */
-
-
-   tnl->_CurrentFlag = (VERT_NORM |
-                        VERT_INDEX |
-                        VERT_RGBA |
-                        VERT_SPEC_RGB |
-                        VERT_FOG_COORD |
-                        VERT_EDGE |
-                        VERT_TEX0_12 |
-                        VERT_TEX1_12 |
-                        VERT_TEX2_12 |
-                        VERT_TEX3_12 |
-                        VERT_MATERIAL);
-
-   tnl->_CurrentPrimitive = GL_POLYGON+1;
-
-   /* Hook our functions into exec and compile dispatch tables.
-    */
-   _mesa_install_save_vtxfmt( ctx, &tnl->vtxfmt );
-   _mesa_install_exec_vtxfmt( ctx, &tnl->vtxfmt );
+   if (ctx->VertexProgram._MaintainTnlProgram) {
+      _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
+   } else {
+      _tnl_install_pipeline( ctx, _tnl_default_pipeline );
+   }
+
+   tnl->NeedNdcCoords = GL_TRUE;
+   tnl->AllowVertexFog = GL_TRUE;
+   tnl->AllowPixelFog = GL_TRUE;
 
    /* Set a few default values in the driver struct.
     */
-   install_driver_callbacks(ctx);
+   tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
+   tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
+   tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
+
+   tnl->nr_blocks = 0;
+
+   /* plug in the VBO drawing function */
+   vbo_set_draw_func(ctx, _tnl_draw_prims);
 
    return GL_TRUE;
 }
@@ -181,86 +90,128 @@ _tnl_DestroyContext( GLcontext *ctx )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
 
-   if (TNL_CURRENT_IM(ctx) != TNL_VB(ctx)->IM)
-      _tnl_immediate_free( TNL_CURRENT_IM(ctx) );
+   _tnl_destroy_pipeline( ctx );
 
-   _tnl_vb_free( TNL_VB(ctx) );
-
-   /* Free cache of immediate buffers. */
-   while (tnl->nr_im_queued-- > 0) {
-      struct immediate * next = tnl->freed_im_queue->next;
-      ALIGN_FREE( tnl->freed_im_queue );
-      tnl->freed_im_queue = next;
-   }
+   FREE(tnl);
+   ctx->swtnl_context = NULL;
 }
 
 
-
 void
 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
 {
-   if (new_state & _NEW_LIGHT)
-      _tnl_update_lighting_function(ctx);
-
-   if (new_state & _NEW_ARRAY)
-      _tnl_update_client_state( ctx );
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   const struct gl_vertex_program *vp = ctx->VertexProgram._Current;
+   const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
 
-   if (new_state & _NEW_TEXTURE)
-      if (ctx->_Enabled & ENABLE_TEXGEN_ANY)
-        _tnl_update_texgen( ctx );
+   if (new_state & (_NEW_HINT)) {
+      ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
+      tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
+         || !tnl->AllowPixelFog;
+   }
 
-   if (new_state & (_NEW_LIGHT|_NEW_TEXTURE|_NEW_FOG|
-                   _DD_NEW_TRI_LIGHT_TWOSIDE |
-                   _DD_NEW_SEPERATE_SPECULAR |
-                   _DD_NEW_TRI_UNFILLED ))
-      _tnl_update_clipmask(ctx);
+   tnl->pipeline.new_state |= new_state;
 
-   if (new_state & _TNL_NEW_NORMAL_TRANSFORM)
-      _tnl_update_normal_transform( ctx );
+   /* Calculate tnl->render_inputs:
+    */
+   if (ctx->Visual.rgbMode) {
+      GLuint i;
+
+      RENDERINPUTS_ZERO( tnl->render_inputs_bitset );
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
+      if (!fp || (fp->Base.InputsRead & FRAG_BIT_COL0)) {
+         RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0 );
+      }
+      for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
+         if (ctx->Texture._EnabledCoordUnits & (1 << i)) {
+            RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) );
+         }
+      }
+
+      if (NEED_SECONDARY_COLOR(ctx))
+         RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 );
+   }
+   else {
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR_INDEX );
+   }
 
-   _tnl_update_pipelines(ctx);
+   if (ctx->Fog.Enabled ||
+       ((ctx->FragmentProgram._Active || ctx->FragmentProgram._Current) &&
+        (ctx->FragmentProgram._Current->FogOption != GL_NONE ||
+         (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_FOGC))))
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
+
+   if (ctx->Polygon.FrontMode != GL_FILL || 
+       ctx->Polygon.BackMode != GL_FILL)
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_EDGEFLAG );
+
+   if (ctx->RenderMode == GL_FEEDBACK)
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX0 );
+
+   if (ctx->Point._Attenuated ||
+       (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
+      RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE );
+
+   /* check for varying vars which are written by the vertex program */
+   if (vp) {
+      GLuint i;
+      for (i = 0; i < MAX_VARYING; i++) {
+         if (vp->Base.OutputsWritten & (1 << (VERT_RESULT_VAR0 + i))) {
+            RENDERINPUTS_SET(tnl->render_inputs_bitset,
+                             _TNL_ATTRIB_GENERIC(i));
+         }
+      }
+   }
 }
 
+
 void
-_tnl_wakeup_exec( GLcontext *ctx )
+_tnl_wakeup( GLcontext *ctx )
 {
-   TNLcontext *tnl = TNL_CONTEXT(ctx);
-   
-   fprintf(stderr, "%s\n", __FUNCTION__);
+   /* Assume we haven't been getting state updates either:
+    */
+   _tnl_InvalidateState( ctx, ~0 );
 
-   install_driver_callbacks(ctx);
+#if 0
+   if (ctx->Light.ColorMaterialEnabled) {
+      _mesa_update_color_material( ctx, 
+                                  ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
+   }
+#endif
+}
 
-   /* Hook our functions into exec and compile dispatch tables.
-    */
-   _mesa_install_exec_vtxfmt( ctx, &tnl->vtxfmt );
 
-   /* Call all appropriate driver callbacks to revive state.
-    */
-   _tnl_MakeCurrent( ctx, ctx->DrawBuffer, ctx->ReadBuffer );
-   _tnl_UnlockArraysEXT( ctx );
-   _tnl_LockArraysEXT( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
 
-   /* Equivalent to calling all _tnl_*Pointer functions:
-    */
-   tnl->_ArrayNewState = ~0;   
 
-   /* Assume we haven't been getting state updates either:
-    */
-   _tnl_InvalidateState( ctx, ~0 );
-   
-   /* Special state not restored by other methods:
-    */
-   _tnl_recalc_current_flag( ctx );
+/**
+ * Drivers call this function to tell the TCL module whether or not
+ * it wants Normalized Device Coords (NDC) computed.  I.e. whether
+ * we should "Divide-by-W".  Software renders will want that.
+ */
+void
+_tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   tnl->NeedNdcCoords = mode;
 }
 
 void
-_tnl_wakeup_save_exec( GLcontext *ctx )
+_tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
+   tnl->AllowVertexFog = value;
+   tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
+      || !tnl->AllowPixelFog;
 
-   fprintf(stderr, "%s\n", __FUNCTION__);
+}
 
-   _tnl_wakeup_exec( ctx );
-   _mesa_install_save_vtxfmt( ctx, &tnl->vtxfmt );
+void
+_tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   tnl->AllowPixelFog = value;
+   tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
+      || !tnl->AllowPixelFog;
 }