mesa: added gl_program_constants::MaxAddressOffset
[mesa.git] / src / mesa / main / context.c
index 1625e4c50198a6da101677af6299ae89edc13684..5d581c84002383334699ae9ea3ffea3b6791c35e 100644 (file)
@@ -96,6 +96,7 @@
 #include "fbobject.h"
 #include "feedback.h"
 #include "fog.h"
+#include "formats.h"
 #include "framebuffer.h"
 #include "hint.h"
 #include "hash.h"
 #if _HAVE_FULL_GL
 #include "math/m_matrix.h"
 #endif
+#include "main/dispatch.h" /* for _gloffset_COUNT */
 
 #ifdef USE_SPARC_ASM
 #include "sparc/sparc.h"
@@ -218,7 +220,7 @@ _mesa_create_visual( GLboolean dbFlag,
                      GLint accumAlphaBits,
                      GLint numSamples )
 {
-   struct gl_config *vis = (struct gl_config *) calloc(1, sizeof(struct gl_config));
+   struct gl_config *vis = CALLOC_STRUCT(gl_config);
    if (vis) {
       if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
                                    redBits, greenBits, blueBits, alphaBits,
@@ -233,11 +235,13 @@ _mesa_create_visual( GLboolean dbFlag,
    return vis;
 }
 
+
 /**
- * Makes some sanity checks and fills in the fields of the
- * struct gl_config object with the given parameters.  If the caller needs
- * to set additional fields, he should just probably init the whole struct gl_config
- * object himself.
+ * Makes some sanity checks and fills in the fields of the struct
+ * gl_config object with the given parameters.  If the caller needs to
+ * set additional fields, he should just probably init the whole
+ * gl_config object himself.
+ *
  * \return GL_TRUE on success, or GL_FALSE on failure.
  *
  * \sa _mesa_create_visual() above for the parameter description.
@@ -367,6 +371,8 @@ dummy_enum_func(void)
  */
 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
 
+
+
 /**
  * Calls all the various one-time-init functions in Mesa.
  *
@@ -379,10 +385,12 @@ _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
 static void
 one_time_init( struct gl_context *ctx )
 {
-   static GLboolean alreadyCalled = GL_FALSE;
-   (void) ctx;
+   static GLbitfield api_init_mask = 0x0;
+
    _glthread_LOCK_MUTEX(OneTimeLock);
-   if (!alreadyCalled) {
+
+   /* truly one-time init */
+   if (!api_init_mask) {
       GLuint i;
 
       /* do some implementation tests */
@@ -395,27 +403,9 @@ one_time_init( struct gl_context *ctx )
 
       _mesa_get_cpu_features();
 
-      switch (ctx->API) {
-#if FEATURE_GL
-      case API_OPENGL:
-        _mesa_init_remap_table();
-        break;
-#endif
-#if FEATURE_ES1
-      case API_OPENGLES:
-        _mesa_init_remap_table_es1();
-        break;
-#endif
-#if FEATURE_ES2
-      case API_OPENGLES2:
-        _mesa_init_remap_table_es2();
-        break;
-#endif
-      default:
-        break;
-      }
-
       _mesa_init_sqrt_table();
+
+      /* context dependence is never a one-time thing... */
       _mesa_init_get_hash(ctx);
 
       for (i = 0; i < 256; i++) {
@@ -423,12 +413,31 @@ one_time_init( struct gl_context *ctx )
       }
 
 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
-      _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
-                  MESA_VERSION_STRING, __DATE__, __TIME__);
+      if (MESA_VERBOSE != 0) {
+        _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
+                    MESA_VERSION_STRING, __DATE__, __TIME__);
+      }
+#endif
+
+#ifdef DEBUG
+      _mesa_test_formats();
 #endif
+   }
 
-      alreadyCalled = GL_TRUE;
+   /* per-API one-time init */
+   if (!(api_init_mask & (1 << ctx->API))) {
+      /*
+       * This is fine as ES does not use the remap table, but it may not be
+       * future-proof.  We cannot always initialize the remap table because
+       * when an app is linked to libGLES*, there are not enough dynamic
+       * entries.
+       */
+      if (ctx->API == API_OPENGL)
+         _mesa_init_remap_table();
    }
+
+   api_init_mask |= 1 << ctx->API;
+
    _glthread_UNLOCK_MUTEX(OneTimeLock);
 
    /* Hopefully atexit() is widely available.  If not, we may need some
@@ -478,6 +487,7 @@ init_program_limits(GLenum type, struct gl_program_constants *prog)
    prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
    prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
    prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
+   prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
 
    switch (type) {
    case GL_VERTEX_PROGRAM_ARB:
@@ -517,6 +527,25 @@ init_program_limits(GLenum type, struct gl_program_constants *prog)
    prog->MaxNativeTemps = 0;
    prog->MaxNativeAddressRegs = 0;
    prog->MaxNativeParameters = 0;
+
+   /* Set GLSL datatype range/precision info assuming IEEE float values.
+    * Drivers should override these defaults as needed.
+    */
+   prog->MediumFloat.RangeMin = 127;
+   prog->MediumFloat.RangeMax = 127;
+   prog->MediumFloat.Precision = 23;
+   prog->LowFloat = prog->HighFloat = prog->MediumFloat;
+
+   /* Assume ints are stored as floats for now, since this is the least-common
+    * denominator.  The OpenGL ES spec implies (page 132) that the precision
+    * of integer types should be 0.  Practically speaking, IEEE
+    * single-precision floating point values can only store integers in the
+    * range [-0x01000000, 0x01000000] without loss of precision.
+    */
+   prog->MediumInt.RangeMin = 24;
+   prog->MediumInt.RangeMax = 24;
+   prog->MediumInt.Precision = 0;
+   prog->LowInt = prog->HighInt = prog->MediumInt;
 }
 
 
@@ -531,6 +560,7 @@ _mesa_init_constants(struct gl_context *ctx)
    assert(ctx);
 
    /* Constants, may be overriden (usually only reduced) by device drivers */
+   ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
    ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
    ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
    ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
@@ -592,11 +622,7 @@ _mesa_init_constants(struct gl_context *ctx)
 
    /* Shading language version */
    if (ctx->API == API_OPENGL) {
-#if FEATURE_ARB_shading_language_120
       ctx->Const.GLSLVersion = 120;
-#else
-      ctx->Const.GLSLVersion = 110;
-#endif
    }
    else if (ctx->API == API_OPENGLES2) {
       ctx->Const.GLSLVersion = 100;
@@ -624,6 +650,10 @@ _mesa_init_constants(struct gl_context *ctx)
 
    /* GL 3.2: hard-coded for now: */
    ctx->Const.ProfileMask = GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
+
+   /** GL_EXT_gpu_shader4 */
+   ctx->Const.MinProgramTexelOffset = -8;
+   ctx->Const.MaxProgramTexelOffset = 7;
 }
 
 
@@ -811,10 +841,13 @@ _mesa_alloc_dispatch_table(int size)
     * Mesa we do this to accomodate different versions of libGL and various
     * DRI drivers.
     */
-   GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
-                           size / sizeof(_glapi_proc));
-   struct _glapi_table *table =
-      (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
+   GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
+   struct _glapi_table *table;
+
+   /* should never happen, but just in case */
+   numEntries = MAX2(numEntries, size);
+
+   table = (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
    if (table) {
       _glapi_proc *entry = (_glapi_proc *) table;
       GLint i;
@@ -854,12 +887,12 @@ _mesa_alloc_dispatch_table(int size)
  * \param driverContext pointer to driver-specific context data
  */
 GLboolean
-_mesa_initialize_context_for_api(struct gl_context *ctx,
-                                gl_api api,
-                                const struct gl_config *visual,
-                                struct gl_context *share_list,
-                                const struct dd_function_table *driverFunctions,
-                                void *driverContext)
+_mesa_initialize_context(struct gl_context *ctx,
+                         gl_api api,
+                         const struct gl_config *visual,
+                         struct gl_context *share_list,
+                         const struct dd_function_table *driverFunctions,
+                         void *driverContext)
 {
    struct gl_shared_state *shared;
    int i;
@@ -947,6 +980,14 @@ _mesa_initialize_context_for_api(struct gl_context *ctx,
       ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
    }
 
+   /* Mesa core handles all the formats that mesa core knows about.
+    * Drivers will want to override this list with just the formats
+    * they can handle, and confirm that appropriate fallbacks exist in
+    * _mesa_choose_tex_format().
+    */
+   memset(&ctx->TextureFormatSupported, GL_TRUE,
+         sizeof(ctx->TextureFormatSupported));
+
    switch (ctx->API) {
    case API_OPENGL:
 #if FEATURE_dlist
@@ -987,20 +1028,6 @@ _mesa_initialize_context_for_api(struct gl_context *ctx,
    return GL_TRUE;
 }
 
-GLboolean
-_mesa_initialize_context(struct gl_context *ctx,
-                         const struct gl_config *visual,
-                         struct gl_context *share_list,
-                         const struct dd_function_table *driverFunctions,
-                         void *driverContext)
-{
-   return _mesa_initialize_context_for_api(ctx,
-                                          API_OPENGL,
-                                          visual,
-                                          share_list,
-                                          driverFunctions,
-                                          driverContext);
-}
 
 /**
  * Allocate and initialize a struct gl_context structure.
@@ -1018,11 +1045,11 @@ _mesa_initialize_context(struct gl_context *ctx,
  * \return pointer to a new __struct gl_contextRec or NULL if error.
  */
 struct gl_context *
-_mesa_create_context_for_api(gl_api api,
-                            const struct gl_config *visual,
-                            struct gl_context *share_list,
-                            const struct dd_function_table *driverFunctions,
-                            void *driverContext)
+_mesa_create_context(gl_api api,
+                     const struct gl_config *visual,
+                     struct gl_context *share_list,
+                     const struct dd_function_table *driverFunctions,
+                     void *driverContext)
 {
    struct gl_context *ctx;
 
@@ -1033,8 +1060,8 @@ _mesa_create_context_for_api(gl_api api,
    if (!ctx)
       return NULL;
 
-   if (_mesa_initialize_context_for_api(ctx, api, visual, share_list,
-                                       driverFunctions, driverContext)) {
+   if (_mesa_initialize_context(ctx, api, visual, share_list,
+                                driverFunctions, driverContext)) {
       return ctx;
    }
    else {
@@ -1043,17 +1070,6 @@ _mesa_create_context_for_api(gl_api api,
    }
 }
 
-struct gl_context *
-_mesa_create_context(const struct gl_config *visual,
-                    struct gl_context *share_list,
-                    const struct dd_function_table *driverFunctions,
-                    void *driverContext)
-{
-   return _mesa_create_context_for_api(API_OPENGL, visual,
-                                      share_list,
-                                      driverFunctions,
-                                      driverContext);
-}
 
 /**
  * Free the data associated with the given context.
@@ -1141,7 +1157,7 @@ _mesa_free_context_data( struct gl_context *ctx )
  *
  * \param ctx GL context.
  * 
- * Calls _mesa_free_context_data() and frees the struct gl_context structure itself.
+ * Calls _mesa_free_context_data() and frees the gl_context object itself.
  */
 void
 _mesa_destroy_context( struct gl_context *ctx )
@@ -1286,14 +1302,12 @@ _mesa_copy_context( const struct gl_context *src, struct gl_context *dst, GLuint
  * \return GL_TRUE if compatible, GL_FALSE otherwise.
  */
 static GLboolean 
-check_compatible(const struct gl_context *ctx, const struct gl_framebuffer *buffer)
+check_compatible(const struct gl_context *ctx,
+                 const struct gl_framebuffer *buffer)
 {
    const struct gl_config *ctxvis = &ctx->Visual;
    const struct gl_config *bufvis = &buffer->Visual;
 
-   if (ctxvis == bufvis)
-      return GL_TRUE;
-
    if (buffer == _mesa_get_incomplete_framebuffer())
       return GL_TRUE;
 
@@ -1380,9 +1394,12 @@ _mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
  * \param readBuffer  the reading framebuffer
  */
 GLboolean
-_mesa_make_current( struct gl_context *newCtx, struct gl_framebuffer *drawBuffer,
+_mesa_make_current( struct gl_context *newCtx,
+                    struct gl_framebuffer *drawBuffer,
                     struct gl_framebuffer *readBuffer )
 {
+   GET_CURRENT_CONTEXT(curCtx);
+
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(newCtx, "_mesa_make_current()\n");
 
@@ -1403,6 +1420,11 @@ _mesa_make_current( struct gl_context *newCtx, struct gl_framebuffer *drawBuffer
       }
    }
 
+   if (curCtx && 
+      (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) && /* make sure this context is valid for flushing */
+      curCtx != newCtx)
+      _mesa_flush(curCtx);
+
    /* We used to call _glapi_check_multithread() here.  Now do it in drivers */
    _glapi_set_context((void *) newCtx);
    ASSERT(_mesa_get_current_context() == newCtx);
@@ -1441,7 +1463,8 @@ _mesa_make_current( struct gl_context *newCtx, struct gl_framebuffer *drawBuffer
                buffers[i] = newCtx->Color.DrawBuffer[i];
             }
 
-            _mesa_drawbuffers(newCtx, newCtx->Const.MaxDrawBuffers, buffers, NULL);
+            _mesa_drawbuffers(newCtx, newCtx->Const.MaxDrawBuffers,
+                              buffers, NULL);
          }
          if (!newCtx->ReadBuffer || newCtx->ReadBuffer->Name == 0) {
             _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
@@ -1701,11 +1724,10 @@ _mesa_valid_to_render(struct gl_context *ctx, const char *where)
    if (ctx->NewState)
       _mesa_update_state(ctx);
 
-   if (ctx->Shader.CurrentProgram) {
-      struct gl_shader_program *const prog = ctx->Shader.CurrentProgram;
+   if (ctx->Shader.CurrentVertexProgram) {
+      vert_from_glsl_shader = true;
 
-      /* using shaders */
-      if (!prog->LinkStatus) {
+      if (!ctx->Shader.CurrentVertexProgram->LinkStatus) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "%s(shader not linked)", where);
          return GL_FALSE;
@@ -1713,25 +1735,57 @@ _mesa_valid_to_render(struct gl_context *ctx, const char *where)
 #if 0 /* not normally enabled */
       {
          char errMsg[100];
-         if (!_mesa_validate_shader_program(ctx, prog, errMsg)) {
+         if (!_mesa_validate_shader_program(ctx,
+                                           ctx->Shader.CurrentVertexProgram,
+                                            errMsg)) {
             _mesa_warning(ctx, "Shader program %u is invalid: %s",
-                          prog->Name, errMsg);
+                          ctx->Shader.CurrentVertexProgram->Name, errMsg);
          }
       }
 #endif
+   }
 
-      /* Figure out which shader stages are provided by the GLSL program.  For
-       * any stages that are not provided, the corresponding assembly shader
-       * target will be validated below.
-       */
-      vert_from_glsl_shader =
-        prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL;
-      geom_from_glsl_shader =
-        prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL;
-      frag_from_glsl_shader =
-        prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL;
+   if (ctx->Shader.CurrentGeometryProgram) {
+      geom_from_glsl_shader = true;
+
+      if (!ctx->Shader.CurrentGeometryProgram->LinkStatus) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(shader not linked)", where);
+         return GL_FALSE;
+      }
+#if 0 /* not normally enabled */
+      {
+         char errMsg[100];
+         if (!_mesa_validate_shader_program(ctx,
+                                           ctx->Shader.CurrentGeometryProgram,
+                                            errMsg)) {
+            _mesa_warning(ctx, "Shader program %u is invalid: %s",
+                          ctx->Shader.CurrentGeometryProgram->Name, errMsg);
+         }
+      }
+#endif
    }
 
+   if (ctx->Shader.CurrentFragmentProgram) {
+      frag_from_glsl_shader = true;
+
+      if (!ctx->Shader.CurrentFragmentProgram->LinkStatus) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(shader not linked)", where);
+         return GL_FALSE;
+      }
+#if 0 /* not normally enabled */
+      {
+         char errMsg[100];
+         if (!_mesa_validate_shader_program(ctx,
+                                           ctx->Shader.CurrentFragmentProgram,
+                                            errMsg)) {
+            _mesa_warning(ctx, "Shader program %u is invalid: %s",
+                          ctx->Shader.CurrentFragmentProgram->Name, errMsg);
+         }
+      }
+#endif
+   }
 
    /* Any shader stages that are not supplied by the GLSL shader and have
     * assembly shaders enabled must now be validated.
@@ -1748,11 +1802,21 @@ _mesa_valid_to_render(struct gl_context *ctx, const char *where)
     */
    (void) geom_from_glsl_shader;
 
-   if (!frag_from_glsl_shader
-       && ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                 "%s(fragment program not valid)", where);
-      return GL_FALSE;
+   if (!frag_from_glsl_shader) {
+      if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
+        _mesa_error(ctx, GL_INVALID_OPERATION,
+                    "%s(fragment program not valid)", where);
+        return GL_FALSE;
+      }
+
+      /* If drawing to integer-valued color buffers, there must be an
+       * active fragment shader (GL_EXT_texture_integer).
+       */
+      if (ctx->DrawBuffer && ctx->DrawBuffer->_IntegerColor) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(integer format but no fragment shader)", where);
+         return GL_FALSE;
+      }
    }
 
    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
@@ -1763,26 +1827,51 @@ _mesa_valid_to_render(struct gl_context *ctx, const char *where)
 
 #ifdef DEBUG
    if (ctx->Shader.Flags & GLSL_LOG) {
-      struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
-      if (shProg) {
-         if (!shProg->_Used) {
-            /* This is the first time this shader is being used.
-             * Append shader's constants/uniforms to log file.
-             */
-            GLuint i;
-            for (i = 0; i < shProg->NumShaders; i++) {
-               struct gl_shader *sh = shProg->Shaders[i];
-               if (sh->Type == GL_VERTEX_SHADER) {
-                  _mesa_append_uniforms_to_file(sh,
-                                                &shProg->VertexProgram->Base);
-               }
-               else if (sh->Type == GL_FRAGMENT_SHADER) {
-                  _mesa_append_uniforms_to_file(sh,
-                                                &shProg->FragmentProgram->Base);
-               }
-            }
-            shProg->_Used = GL_TRUE;
-         }
+      struct gl_shader_program *shProg[MESA_SHADER_TYPES];
+      gl_shader_type i;
+
+      shProg[MESA_SHADER_VERTEX] = ctx->Shader.CurrentVertexProgram;
+      shProg[MESA_SHADER_GEOMETRY] = ctx->Shader.CurrentGeometryProgram;
+      shProg[MESA_SHADER_FRAGMENT] = ctx->Shader.CurrentFragmentProgram;
+
+      for (i = 0; i < MESA_SHADER_TYPES; i++) {
+        struct gl_shader *sh;
+
+        if (shProg[i] == NULL || shProg[i]->_Used
+            || shProg[i]->_LinkedShaders[i] == NULL)
+           continue;
+
+        /* This is the first time this shader is being used.
+         * Append shader's constants/uniforms to log file.
+         *
+         * The logic is a little odd here.  We only want to log data for each
+         * shader target that will actually be used, and we only want to log
+         * it once.  It's possible to have a program bound to the vertex
+         * shader target that also supplied a fragment shader.  If that
+         * program isn't also bound to the fragment shader target we don't
+         * want to log its fragment data.
+         */
+        sh = shProg[i]->_LinkedShaders[i];
+        switch (sh->Type) {
+        case GL_VERTEX_SHADER:
+           _mesa_append_uniforms_to_file(sh, &shProg[i]->VertexProgram->Base);
+           break;
+
+        case GL_GEOMETRY_SHADER_ARB:
+           _mesa_append_uniforms_to_file(sh,
+                                         &shProg[i]->GeometryProgram->Base);
+           break;
+
+        case GL_FRAGMENT_SHADER:
+           _mesa_append_uniforms_to_file(sh,
+                                         &shProg[i]->FragmentProgram->Base);
+           break;
+        }
+      }
+
+      for (i = 0; i < MESA_SHADER_TYPES; i++) {
+        if (shProg[i] != NULL)
+           shProg[i]->_Used = GL_TRUE;
       }
    }
 #endif