Minor tweaks to help out at a driver level.
[mesa.git] / src / mesa / main / context.c
index 9151c74899fb5c6964d3a53c19478a6bd38f522f..b2ab4916107790cd20b8b383c10dcb878d4eb48e 100644 (file)
  * Selecting the <b>Main page</b> link will display a summary of the module
  * (this page).
  *
- * Selecting <b>Compound List</b> will list all C structures.
+ * Selecting <b>Data Structures</b> will list all C structures.
  *
  * Selecting the <b>File List</b> link will list all the source files in
  * the module.
  * Selecting a filename will show a list of all functions defined in that file.
  *
- * Selecting the <b>Compound Members</b> link will display a list of all
+ * Selecting the <b>Data Fields</b> link will display a list of all
  * documented structure members.
  *
- * Selecting the <b>File Members</b> link will display a list
+ * Selecting the <b>Globals</b> link will display a list
  * of all functions, structures, global variables and macros in the module.
  *
  */
@@ -77,6 +77,7 @@
 #include "attrib.h"
 #include "blend.h"
 #include "buffers.h"
+#include "bufferobj.h"
 #include "colortab.h"
 #include "context.h"
 #include "debug.h"
 #include "pixel.h"
 #include "points.h"
 #include "polygon.h"
+#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
+#include "program.h"
+#endif
 #include "rastpos.h"
 #include "simple_list.h"
 #include "state.h"
 #include "texstate.h"
 #include "mtypes.h"
 #include "varray.h"
-#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
-#include "program.h"
-#endif
 #include "vtxfmt.h"
 #if _HAVE_FULL_GL
 #include "math/m_translate.h"
 #include "math/mathmod.h"
 #endif
 
-#if defined(MESA_TRACE)
-#include "Trace/tr_context.h"
-#include "Trace/tr_wrapper.h"
-#endif
-
 #ifdef USE_SPARC_ASM
 #include "SPARC/sparc.h"
 #endif
@@ -391,7 +387,8 @@ __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
        return NULL;
     }
 
-    _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
+    /* XXX doesn't work at this time */
+    _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
     ctx->imports = *imports;
 
     return ctx;
@@ -840,6 +837,8 @@ alloc_shared_state( GLcontext *ctx )
       goto cleanup;
 #endif
 
+   ss->BufferObjects = _mesa_NewHashTable();
+
    ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
    if (!ss->Default1D)
       goto cleanup;
@@ -860,14 +859,6 @@ alloc_shared_state( GLcontext *ctx )
    if (!ss->DefaultRect)
       goto cleanup;
 
-#if 0
-   _mesa_save_texture_object(ctx, ss->Default1D);
-   _mesa_save_texture_object(ctx, ss->Default2D);
-   _mesa_save_texture_object(ctx, ss->Default3D);
-   _mesa_save_texture_object(ctx, ss->DefaultCubeMap);
-   _mesa_save_texture_object(ctx, ss->DefaultRect);
-#endif
-
    /* Effectively bind the default textures to all texture units */
    ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
    ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
@@ -895,6 +886,9 @@ alloc_shared_state( GLcontext *ctx )
    if (ss->DefaultFragmentProgram)
       _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
 #endif
+   if (ss->BufferObjects)
+      _mesa_DeleteHashTable(ss->BufferObjects);
+
    if (ss->Default1D)
       (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
    if (ss->Default2D)
@@ -972,6 +966,8 @@ free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
    _mesa_DeleteHashTable(ss->Programs);
 #endif
 
+   _mesa_DeleteHashTable(ss->BufferObjects);
+
    _glthread_DESTROY_MUTEX(ss->Mutex);
 
    FREE(ss);
@@ -1059,6 +1055,15 @@ _mesa_init_constants( GLcontext *ctx )
    ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
    ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
 
+   /* If we're running in the X server, do bounds checking to prevent
+    * segfaults and server crashes!
+    */
+#if defined(XFree86LOADER) && defined(IN_MODULE)
+   ctx->Const.CheckArrayBounds = GL_TRUE;
+#else
+   ctx->Const.CheckArrayBounds = GL_FALSE;
+#endif
+
    ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
 }
 
@@ -1085,6 +1090,7 @@ init_attrib_groups( GLcontext *ctx )
    _mesa_init_accum( ctx );
    _mesa_init_attrib( ctx );
    _mesa_init_buffers( ctx );
+   _mesa_init_buffer_objects( ctx );
    _mesa_init_color( ctx );
    _mesa_init_colortables( ctx );
    _mesa_init_current( ctx );
@@ -1322,12 +1328,15 @@ add_newer_entrypoints(void)
 
 
 /**
- * Initialize a GLcontext struct
+ * Initialize a GLcontext struct (rendering context).
  *
  * This includes allocating all the other structs and arrays which hang off of
  * the context by pointers.
+ * Note that the driver needs to pass in its dd_function_table here since
+ * we need to at least call driverFunctions->NewTextureObject to create the
+ * default texture objects.
  * 
- * \sa _mesa_create_context() for the parameter description.
+ * Called by _mesa_create_context().
  *
  * Performs the imports and exports callback tables initialization, and
  * miscellaneous one-time initializations. If no shared context is supplied one
@@ -1336,23 +1345,30 @@ add_newer_entrypoints(void)
  * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
  * for debug flags.
  *
- * \note the direct parameter is ignored (obsolete).
+ * \param ctx the context to initialize
+ * \param visual describes the visual attributes for this context
+ * \param share_list points to context to share textures, display lists,
+ *        etc with, or NULL
+ * \param driverFunctions table of device driver functions for this context
+ *        to use
+ * \param driverContext pointer to driver-specific context data
  */
 GLboolean
 _mesa_initialize_context( GLcontext *ctx,
                           const GLvisual *visual,
                           GLcontext *share_list,
-                          void *driver_ctx,
-                          GLboolean direct )
+                          const struct dd_function_table *driverFunctions,
+                          void *driverContext )
 {
    GLuint dispatchSize;
 
-   ASSERT(driver_ctx);
+   ASSERT(driverContext);
+   assert(driverFunctions->NewTextureObject);
 
    /* If the driver wants core Mesa to use special imports, it'll have to
     * override these defaults.
     */
-   _mesa_init_default_imports( &(ctx->imports), driver_ctx );
+   _mesa_init_default_imports( &(ctx->imports), driverContext );
 
    /* initialize the exports (Mesa functions called by the window system) */
    _mesa_init_default_exports( &(ctx->exports) );
@@ -1360,20 +1376,17 @@ _mesa_initialize_context( GLcontext *ctx,
    /* misc one-time initializations */
    one_time_init(ctx);
 
-   ctx->DriverCtx = driver_ctx;
    ctx->Visual = *visual;
    ctx->DrawBuffer = NULL;
    ctx->ReadBuffer = NULL;
 
-   /* Set these pointers to defaults now in case they're not set since
-    * we need them while creating the default textures.
+   /* Plug in driver functions and context pointer here.
+    * This is important because when we call alloc_shared_state() below
+    * we'll call ctx->Driver.NewTextureObject() to create the default
+    * textures.
     */
-   if (!ctx->Driver.NewTextureObject)
-      ctx->Driver.NewTextureObject = _mesa_new_texture_object;
-   if (!ctx->Driver.DeleteTexture)
-      ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
-   if (!ctx->Driver.NewTextureImage)
-      ctx->Driver.NewTextureImage = _mesa_new_texture_image;
+   ctx->Driver = *driverFunctions;
+   ctx->DriverCtx = driverContext;
 
    if (share_list) {
       /* share state with another context */
@@ -1418,47 +1431,51 @@ _mesa_initialize_context( GLcontext *ctx,
 
 #if _HAVE_FULL_GL
    _mesa_init_dlist_table(ctx->Save, dispatchSize);
+   _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
 
-   ctx->ExecPrefersFloat = GL_FALSE;
-   ctx->SavePrefersFloat = GL_FALSE;
 
    /* Neutral tnl module stuff */
    _mesa_init_exec_vtxfmt( ctx ); 
    ctx->TnlModule.Current = NULL;
    ctx->TnlModule.SwapCount = 0;
-
 #endif
 
    return GL_TRUE;
 }
 
+
 /**
  * Allocate and initialize a GLcontext structure.
+ * Note that the driver needs to pass in its dd_function_table here since
+ * we need to at least call driverFunctions->NewTextureObject to initialize
+ * the rendering context.
  *
  * \param visual a GLvisual pointer (we copy the struct contents)
  * \param share_list another context to share display lists with or NULL
- * \param driver_ctx pointer to device driver's context state struct
- * \param direct obsolete, ignored
+ * \param driverFunctions points to the dd_function_table into which the
+ *        driver has plugged in all its special functions.
+ * \param driverCtx points to the device driver's private context state
  * 
  * \return pointer to a new __GLcontextRec or NULL if error.
  */
 GLcontext *
 _mesa_create_context( const GLvisual *visual,
                       GLcontext *share_list,
-                      void *driver_ctx,
-                      GLboolean direct )
+                      const struct dd_function_table *driverFunctions,
+                      void *driverContext )
 
 {
    GLcontext *ctx;
 
    ASSERT(visual);
-   ASSERT(driver_ctx);
+   ASSERT(driverContext);
 
    ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
    if (!ctx)
       return NULL;
 
-   if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
+   if (_mesa_initialize_context(ctx, visual, share_list,
+                                driverFunctions, driverContext)) {
       return ctx;
    }
    else {
@@ -1467,6 +1484,7 @@ _mesa_create_context( const GLvisual *visual,
    }
 }
 
+
 /**
  * Free the data associated with the given context.
  * 
@@ -1520,12 +1538,13 @@ _mesa_free_context_data( GLcontext *ctx )
    FREE(ctx->Save);
 }
 
+
 /**
  * Destroy a GLcontext structure.
  *
  * \param ctx GL context.
  * 
- * Calls _mesa_free_context_data() and free the structure.
+ * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
  */
 void
 _mesa_destroy_context( GLcontext *ctx )
@@ -1536,6 +1555,7 @@ _mesa_destroy_context( GLcontext *ctx )
    }
 }
 
+
 #if _HAVE_FULL_GL
 /**
  * Copy attribute groups from one context to another.
@@ -1904,7 +1924,7 @@ _mesa_record_error( GLcontext *ctx, GLenum error )
  * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
  * dd_function_table::Finish driver callback, if not NULL.
  */
-void
+void GLAPIENTRY
 _mesa_Finish( void )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -1920,7 +1940,7 @@ _mesa_Finish( void )
  * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
  * dd_function_table::Flush driver callback, if not NULL.
  */
-void
+void GLAPIENTRY
 _mesa_Flush( void )
 {
    GET_CURRENT_CONTEXT(ctx);