Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / state_tracker / st_atom_texture.c
index e1528c4d7b97ca2fa371556bf1d28ffdf178570c..fb03766ff599cc4f0a9c59691a82eaac12105325 100644 (file)
 #include "st_cb_texture.h"
 #include "pipe/p_context.h"
 #include "pipe/p_inlines.h"
+#include "cso_cache/cso_context.h"
 
 
-/**
- * XXX This needs some work yet....
- * Need to "upload" texture images at appropriate times.
- */
 static void 
 update_textures(struct st_context *st)
 {
-   /* ST_NEW_FRAGMENT_PROGRAM
-    */
    struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
-   GLuint unit;
+   GLuint su;
 
    st->state.num_textures = 0;
 
-   for (unit = 0; unit < st->ctx->Const.MaxTextureCoordUnits; unit++) {
-      const GLuint su = fprog->Base.SamplerUnits[unit];
+   /*printf("%s samplers used = 0x%x\n", __FUNCTION__, fprog->Base.SamplersUsed);*/
+
+   for (su = 0; su < st->ctx->Const.MaxTextureCoordUnits; su++) {
       struct pipe_texture *pt = NULL;
 
       if (fprog->Base.SamplersUsed & (1 << su)) {
-         struct gl_texture_object *texObj = st->ctx->Texture.Unit[su]._Current;
+         const GLuint texUnit = fprog->Base.SamplerUnits[su];
+         struct gl_texture_object *texObj
+            = st->ctx->Texture.Unit[texUnit]._Current;
+         struct st_texture_object *stObj;
+         GLboolean flush, retval;
+
+         if (!texObj) {
+            texObj = st_get_default_texture(st);
+         }
+         stObj = st_texture_object(texObj);
+
+         retval = st_finalize_texture(st->ctx, st->pipe, texObj, &flush);
+         if (!retval) {
+            /* out of mem */
+            continue;
+         }
+
+         st->state.num_textures = su + 1;
+
+         pt = st_get_stobj_texture(stObj);
+      }
+
+      /*
+      if (pt) {
+         printf("%s su=%u non-null\n", __FUNCTION__, su);
+      }
+      else {
+         printf("%s su=%u null\n", __FUNCTION__, su);
+      }
+      */
+
+      pipe_texture_reference(&st->state.sampler_texture[su], pt);
+   }
+
+   cso_set_sampler_textures(st->cso_context,
+                            st->state.num_textures,
+                            st->state.sampler_texture);
+}
+
+
+const struct st_tracked_state st_update_texture = {
+   "st_update_texture",                                        /* name */
+   {                                                   /* dirty */
+      _NEW_TEXTURE,                                    /* mesa */
+      ST_NEW_FRAGMENT_PROGRAM,                         /* st */
+   },
+   update_textures                                     /* update */
+};
+
+
+
+
+static void 
+finalize_textures(struct st_context *st)
+{
+   struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
+   const GLboolean prev_missing_textures = st->missing_textures;
+   GLuint su;
+
+   st->missing_textures = GL_FALSE;
+
+   for (su = 0; su < st->ctx->Const.MaxTextureCoordUnits; su++) {
+      if (fprog->Base.SamplersUsed & (1 << su)) {
+         const GLuint texUnit = fprog->Base.SamplerUnits[su];
+         struct gl_texture_object *texObj
+            = st->ctx->Texture.Unit[texUnit]._Current;
          struct st_texture_object *stObj = st_texture_object(texObj);
 
          if (texObj) {
@@ -68,33 +129,26 @@ update_textures(struct st_context *st)
             retval = st_finalize_texture(st->ctx, st->pipe, texObj, &flush);
             if (!retval) {
                /* out of mem */
+               st->missing_textures = GL_TRUE;
                continue;
             }
 
-            st->state.num_textures = unit + 1;
+            stObj->teximage_realloc = TRUE;
          }
-
-         pt = st_get_stobj_texture(stObj);
       }
-
-      pipe_texture_reference(&st->state.sampler_texture[unit], pt);
    }
 
-   st->pipe->set_sampler_textures(st->pipe, st->state.num_textures,
-                                  st->state.sampler_texture);
+   if (prev_missing_textures != st->missing_textures)
+      st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
 }
 
 
-const struct st_tracked_state st_update_texture = {
-   .name = "st_update_texture",
-   .dirty = {
-      .mesa = _NEW_TEXTURE,
-      .st  = ST_NEW_FRAGMENT_PROGRAM,
+
+const struct st_tracked_state st_finalize_textures = {
+   "st_finalize_textures",             /* name */
+   {                                   /* dirty */
+      _NEW_TEXTURE,                    /* mesa */
+      0,                               /* st */
    },
-   .update = update_textures
+   finalize_textures                   /* update */
 };
-
-
-
-
-