Browse Source

support varying texture sizes

David Rose 17 years ago
parent
commit
0b5894f27e

+ 0 - 77
panda/src/tinydisplay/api.c

@@ -502,83 +502,6 @@ void glClearDepth(double depth)
 }
 }
 
 
 
 
-/* textures */
-
-void glTexImage2D( int target, int level, int components,
-                   int width, int height, int border,
-                   int format, int type, void *pixels)
-{
-  GLParam p[10];
-
-  p[0].op=OP_TexImage2D;
-  p[1].i=target;
-  p[2].i=level;
-  p[3].i=components;
-  p[4].i=width;
-  p[5].i=height;
-  p[6].i=border;
-  p[7].i=format;
-  p[8].i=type;
-  p[9].p=pixels;
-
-  gl_add_op(p);
-}
-
-
-void glBindTexture(int target,int texture)
-{
-  GLParam p[3];
-
-  p[0].op=OP_BindTexture;
-  p[1].i=target;
-  p[2].i=texture;
-
-  gl_add_op(p);
-}
-
-void glTexEnvi(int target,int pname,int param)
-{
-  GLParam p[8];
-  
-  p[0].op=OP_TexEnv;
-  p[1].i=target;
-  p[2].i=pname;
-  p[3].i=param;
-  p[4].f=0;
-  p[5].f=0;
-  p[6].f=0;
-  p[7].f=0;
-
-  gl_add_op(p);
-}
-
-void glTexParameteri(int target,int pname,int param)
-{
-  GLParam p[8];
-  
-  p[0].op=OP_TexParameter;
-  p[1].i=target;
-  p[2].i=pname;
-  p[3].i=param;
-  p[4].f=0;
-  p[5].f=0;
-  p[6].f=0;
-  p[7].f=0;
-
-  gl_add_op(p);
-}
-
-void glPixelStorei(int pname,int param)
-{
-  GLParam p[3];
-
-  p[0].op=OP_PixelStore;
-  p[1].i=pname;
-  p[2].i=param;
-
-  gl_add_op(p);
-}
-
 /* selection */
 /* selection */
 
 
 void glInitNames(void)
 void glInitNames(void)

+ 2 - 4
panda/src/tinydisplay/clip.c

@@ -35,10 +35,8 @@ void gl_transform_to_viewport(GLContext *c,GLVertex *v)
   /* texture */
   /* texture */
 
 
   if (c->texture_2d_enabled) {
   if (c->texture_2d_enabled) {
-    v->zp.s=(int)(v->tex_coord.X * (ZB_POINT_ST_MAX - ZB_POINT_ST_MIN) 
-                  + ZB_POINT_ST_MIN);
-    v->zp.t=(int)(v->tex_coord.Y * (ZB_POINT_ST_MAX - ZB_POINT_ST_MIN) 
-                  + ZB_POINT_ST_MIN);
+    v->zp.s = (int)(v->tex_coord.X * c->current_texture->s_max); 
+    v->zp.t = (int)(v->tex_coord.Y * c->current_texture->t_max);
   }
   }
 }
 }
 
 

+ 0 - 32
panda/src/tinydisplay/init.c

@@ -2,31 +2,6 @@
 
 
 GLContext *gl_ctx;
 GLContext *gl_ctx;
 
 
-
-void initSharedState(GLContext *c)
-{
-  GLSharedState *s=&c->shared_state;
-  s->lists=gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
-  s->texture_hash_table=
-      gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE);
-
-  alloc_texture(c,0);
-}
-
-void endSharedState(GLContext *c)
-{
-  GLSharedState *s=&c->shared_state;
-  int i;
-
-  for(i=0;i<MAX_DISPLAY_LISTS;i++) {
-    /* TODO */
-  }
-  gl_free(s->lists);
-
-  gl_free(s->texture_hash_table);
-}
-
-
 void glInit(void *zbuffer1)
 void glInit(void *zbuffer1)
 {
 {
   ZBuffer *zbuffer=(ZBuffer *)zbuffer1;
   ZBuffer *zbuffer=(ZBuffer *)zbuffer1;
@@ -51,9 +26,6 @@ void glInit(void *zbuffer1)
   v->ysize=zbuffer->ysize;
   v->ysize=zbuffer->ysize;
   v->updated=1;
   v->updated=1;
 
 
-  /* shared state */
-  initSharedState(c);
-
   /* lists */
   /* lists */
 
 
   c->exec_flag=1;
   c->exec_flag=1;
@@ -98,9 +70,6 @@ void glInit(void *zbuffer1)
   c->current_color_material_type=GL_AMBIENT_AND_DIFFUSE;
   c->current_color_material_type=GL_AMBIENT_AND_DIFFUSE;
   c->color_material_enabled=0;
   c->color_material_enabled=0;
 
 
-  /* textures */
-  glInitTextures(c);
-
   /* default state */
   /* default state */
   c->current_color.X=1.0;
   c->current_color.X=1.0;
   c->current_color.Y=1.0;
   c->current_color.Y=1.0;
@@ -184,6 +153,5 @@ void glInit(void *zbuffer1)
 void glClose(void)
 void glClose(void)
 {
 {
   GLContext *c=gl_get_context();
   GLContext *c=gl_get_context();
-  endSharedState(c);
   gl_free(c);
   gl_free(c);
 }
 }

+ 0 - 6
panda/src/tinydisplay/opinfo.h

@@ -38,12 +38,6 @@ ADD_OP(PushName,1,"%d")
 ADD_OP(PopName,0,"")
 ADD_OP(PopName,0,"")
 ADD_OP(LoadName,1,"%d")
 ADD_OP(LoadName,1,"%d")
 
 
-ADD_OP(TexImage2D,9,"%d %d %d %d %d %d %d %d %d")
-ADD_OP(BindTexture,2,"%C %d")
-ADD_OP(TexEnv,7,"%C %C %C %f %f %f %f")
-ADD_OP(TexParameter,7,"%C %C %C %f %f %f %f")
-ADD_OP(PixelStore,2,"%C %C")
-
 ADD_OP(ShadeModel,1,"%C")
 ADD_OP(ShadeModel,1,"%C")
 ADD_OP(CullFace,1,"%C")
 ADD_OP(CullFace,1,"%C")
 ADD_OP(FrontFace,1,"%C")
 ADD_OP(FrontFace,1,"%C")

+ 0 - 220
panda/src/tinydisplay/texture.c

@@ -3,223 +3,3 @@
  */
  */
 
 
 #include "zgl.h"
 #include "zgl.h"
-
-static GLTexture *find_texture(GLContext *c,int h)
-{
-  GLTexture *t;
-
-  t=c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
-  while (t!=NULL) {
-    if (t->handle == h) return t;
-    t=t->next;
-  }
-  return NULL;
-}
-
-static void free_texture(GLContext *c,int h)
-{
-  GLTexture *t,**ht;
-  GLImage *im;
-  int i;
-
-  t=find_texture(c,h);
-  if (t->prev==NULL) {
-    ht=&c->shared_state.texture_hash_table
-      [t->handle % TEXTURE_HASH_TABLE_SIZE];
-    *ht=t->next;
-  } else {
-    t->prev->next=t->next;
-  }
-  if (t->next!=NULL) t->next->prev=t->prev;
-
-  for(i=0;i<MAX_TEXTURE_LEVELS;i++) {
-    im=&t->images[i];
-    if (im->pixmap != NULL) gl_free(im->pixmap);
-  }
-
-  gl_free(t);
-}
-
-GLTexture *alloc_texture(GLContext *c,int h)
-{
-  GLTexture *t,**ht;
-  
-  t=gl_zalloc(sizeof(GLTexture));
-
-  ht=&c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
-
-  t->next=*ht;
-  t->prev=NULL;
-  if (t->next != NULL) t->next->prev=t;
-  *ht=t;
-
-  t->handle=h;
-  
-  return t;
-}
-
-
-void glInitTextures(GLContext *c)
-{
-  /* textures */
-
-  c->texture_2d_enabled=0;
-  c->current_texture=find_texture(c,0);
-}
-
-void glGenTextures(int n, unsigned int *textures)
-{
-  GLContext *c=gl_get_context();
-  int max,i;
-  GLTexture *t;
-
-  max=0;
-  for(i=0;i<TEXTURE_HASH_TABLE_SIZE;i++) {
-    t=c->shared_state.texture_hash_table[i];
-    while (t!=NULL) {
-      if (t->handle>max) max=t->handle;
-      t=t->next;
-    }
-
-  }
-  for(i=0;i<n;i++) {
-    textures[i]=max+i+1;
-  }
-}
-
-
-void glDeleteTextures(int n, const unsigned int *textures)
-{
-  GLContext *c=gl_get_context();
-  int i;
-  GLTexture *t;
-
-  for(i=0;i<n;i++) {
-    t=find_texture(c,textures[i]);
-    if (t!=NULL && t!=0) {
-      if (t==c->current_texture) {
-	glBindTexture(GL_TEXTURE_2D,0);
-      }
-      free_texture(c,textures[i]);
-    }
-  }
-}
-
-
-void glopBindTexture(GLContext *c,GLParam *p)
-{
-  int target=p[1].i;
-  int texture=p[2].i;
-  GLTexture *t;
-
-  assert(target == GL_TEXTURE_2D && texture >= 0);
-
-  t=find_texture(c,texture);
-  if (t==NULL) {
-    t=alloc_texture(c,texture);
-  }
-  c->current_texture=t;
-}
-
-void glopTexImage2D(GLContext *c,GLParam *p)
-{
-  int target=p[1].i;
-  int level=p[2].i;
-  int components=p[3].i;
-  int width=p[4].i;
-  int height=p[5].i;
-  int border=p[6].i;
-  int format=p[7].i;
-  int type=p[8].i;
-  void *pixels=p[9].p;
-  GLImage *im;
-  unsigned char *pixels1;
-  int do_free;
-  
-  if ((target == GL_TEXTURE_2D && level == 0 && components == 3 && 
-       border == 0 && format == GL_RGB &&
-       type == GL_UNSIGNED_BYTE)) {
-    do_free=0;
-    if (width != 256 || height != 256) {
-      pixels1 = gl_malloc(256 * 256 * 3);
-      /* no interpolation is done here to respect the original image aliasing ! */
-      gl_resizeImageNoInterpolate(pixels1,256,256,pixels,width,height);
-      do_free=1;
-      width=256;
-      height=256;
-    } else {
-      pixels1=pixels;
-    }
-
-  } else if ((target == GL_TEXTURE_2D && level == 0 && components == 4 && 
-       border == 0 && format == GL_RGBA &&
-       type == GL_UNSIGNED_BYTE && width == 256 && height == 256)) {
-    // RGBA format is acceptable if we have 32 render bits.
-
-  } else {
-    gl_fatal_error("glTexImage2D: combinaison of parameters not handled");
-  }
-  
-  im=&c->current_texture->images[level];
-  im->xsize=width;
-  im->ysize=height;
-  if (im->pixmap!=NULL) gl_free(im->pixmap);
-  im->pixmap=gl_malloc(width*height*4);
-  if(im->pixmap) {
-    if (components == 4) {
-      gl_convertRGBA_to_8A8R8G8B(im->pixmap,pixels1,width,height);
-    } else {
-      gl_convertRGB_to_8A8R8G8B(im->pixmap,pixels1,width,height);
-    }
-  }
-  if (do_free) gl_free(pixels1);
-}
-
-
-/* TODO: not all tests are done */
-void glopTexEnv(GLContext *c,GLParam *p)
-{
-  int target=p[1].i;
-  int pname=p[2].i;
-  int param=p[3].i;
-
-  if (target != GL_TEXTURE_ENV) {
-  error:
-    gl_fatal_error("glTexParameter: unsupported option");
-  }
-
-  if (pname != GL_TEXTURE_ENV_MODE) goto error;
-
-  if (param != GL_DECAL) goto error;
-}
-
-/* TODO: not all tests are done */
-void glopTexParameter(GLContext *c,GLParam *p)
-{
-  int target=p[1].i;
-  int pname=p[2].i;
-  int param=p[3].i;
-  
-  if (target != GL_TEXTURE_2D) {
-  error:
-    gl_fatal_error("glTexParameter: unsupported option");
-  }
-
-  switch(pname) {
-  case GL_TEXTURE_WRAP_S:
-  case GL_TEXTURE_WRAP_T:
-    if (param != GL_REPEAT) goto error;
-    break;
-  }
-}
-
-void glopPixelStore(GLContext *c,GLParam *p)
-{
-  int pname=p[1].i;
-  int param=p[2].i;
-
-  if (pname != GL_UNPACK_ALIGNMENT ||
-      param != 1) {
-    gl_fatal_error("glPixelStore: unsupported option");
-  }
-}

+ 97 - 59
panda/src/tinydisplay/tinyGraphicsStateGuardian.cxx

@@ -26,6 +26,7 @@
 #include "pointLight.h"
 #include "pointLight.h"
 #include "directionalLight.h"
 #include "directionalLight.h"
 #include "spotlight.h"
 #include "spotlight.h"
+#include "bitMask.h"
 
 
 extern "C" {
 extern "C" {
 #include "zgl.h"
 #include "zgl.h"
@@ -603,7 +604,7 @@ reset() {
     Geom::GR_indexed_other |
     Geom::GR_indexed_other |
     Geom::GR_flat_last_vertex;
     Geom::GR_flat_last_vertex;
 
 
-  _max_texture_dimension = 256;
+  _max_texture_dimension = (1 << ZB_POINT_ST_FRAC_BITS);
   _max_lights = MAX_LIGHTS;
   _max_lights = MAX_LIGHTS;
 
 
   _color_scale_via_lighting = false;
   _color_scale_via_lighting = false;
@@ -1575,21 +1576,19 @@ void TinyGraphicsStateGuardian::
 release_texture(TextureContext *tc) {
 release_texture(TextureContext *tc) {
   TinyTextureContext *gtc = DCAST(TinyTextureContext, tc);
   TinyTextureContext *gtc = DCAST(TinyTextureContext, tc);
 
 
-  if (_c->current_texture == gtc->_gltex) {
+  GLTexture *gltex = gtc->_gltex;
+  gtc->_gltex = NULL;
+
+  if (_c->current_texture == gltex) {
     _c->current_texture = NULL;
     _c->current_texture = NULL;
-    _c->zb->current_texture = NULL;
     _c->texture_2d_enabled = false;
     _c->texture_2d_enabled = false;
   }
   }
 
 
-  for (int i = 0; i < MAX_TEXTURE_LEVELS; i++) {
-    GLImage *im = &gtc->_gltex->images[i];
-    if (im->pixmap != NULL) {
-      gl_free(im->pixmap);
-    }
+  if (gltex->pixmap != NULL) {
+    gl_free(gltex->pixmap);
   }
   }
 
 
-  gl_free(gtc->_gltex);
-  gtc->_gltex = NULL;
+  gl_free(gltex);
 
 
   delete gtc;
   delete gtc;
 }
 }
@@ -2035,7 +2034,11 @@ apply_texture(TextureContext *tc) {
     gtc->mark_loaded();
     gtc->mark_loaded();
   }
   }
 
 
-  _c->zb->current_texture = (PIXEL *)gtc->_gltex->images[0].pixmap;
+  GLTexture *gltex = gtc->_gltex;
+  _c->zb->current_texture.pixmap = gltex->pixmap;
+  _c->zb->current_texture.s_mask = gltex->s_mask;
+  _c->zb->current_texture.t_mask = gltex->t_mask;
+  _c->zb->current_texture.t_shift = gltex->t_shift;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -2059,17 +2062,29 @@ upload_texture(TinyTextureContext *gtc) {
   _data_transferred_pcollector.add_level(tex->get_ram_image_size());
   _data_transferred_pcollector.add_level(tex->get_ram_image_size());
 #endif
 #endif
 
 
-  // Internal texture size is always 256 x 256 x 4.
-  static const int iwidth = 256;
-  static const int iheight = 256;
-  static const int ibytecount = iwidth * iheight * 4;
+  // Ensure we have a power of two size, no more than our max.
+  int s_size, s_bits;
+  choose_tex_size(s_size, s_bits, tex->get_x_size());
 
 
-  GLImage *im = &gtc->_gltex->images[0];
-  im->xsize = iwidth;
-  im->ysize = iheight;
-  if (im->pixmap == NULL) {
-    im->pixmap = gl_malloc(ibytecount);
+  int t_size, t_bits;
+  choose_tex_size(t_size, t_bits, tex->get_y_size());
+
+  int bytecount = s_size * t_size * 4;
+
+  GLTexture *gltex = gtc->_gltex;
+  gltex->xsize = s_size;
+  gltex->ysize = t_size;
+
+  if (gltex->pixmap != NULL) {
+    gl_free(gltex->pixmap);
   }
   }
+  gltex->pixmap = (PIXEL *)gl_malloc(bytecount);
+
+  gltex->s_max = 1 << (s_bits + ZB_POINT_ST_FRAC_BITS);
+  gltex->s_mask = (1 << (s_bits + ZB_POINT_ST_FRAC_BITS)) - (1 << ZB_POINT_ST_FRAC_BITS);
+  gltex->t_max = 1 << (t_bits + ZB_POINT_ST_FRAC_BITS);
+  gltex->t_mask = (1 << (t_bits + ZB_POINT_ST_FRAC_BITS)) - (1 << ZB_POINT_ST_FRAC_BITS);
+  gltex->t_shift = (ZB_POINT_ST_FRAC_BITS - s_bits);
 
 
   switch (tex->get_format()) {
   switch (tex->get_format()) {
   case Texture::F_rgb:
   case Texture::F_rgb:
@@ -2077,7 +2092,7 @@ upload_texture(TinyTextureContext *gtc) {
   case Texture::F_rgb8:
   case Texture::F_rgb8:
   case Texture::F_rgb12:
   case Texture::F_rgb12:
   case Texture::F_rgb332:
   case Texture::F_rgb332:
-    copy_rgb_image(im, tex);
+    copy_rgb_image(gltex, tex);
     break;
     break;
 
 
   case Texture::F_rgba:
   case Texture::F_rgba:
@@ -2088,37 +2103,37 @@ upload_texture(TinyTextureContext *gtc) {
   case Texture::F_rgba12:
   case Texture::F_rgba12:
   case Texture::F_rgba16:
   case Texture::F_rgba16:
   case Texture::F_rgba32:
   case Texture::F_rgba32:
-    copy_rgba_image(im, tex);
+    copy_rgba_image(gltex, tex);
     break;
     break;
 
 
   case Texture::F_luminance:
   case Texture::F_luminance:
-    copy_lum_image(im, tex);
+    copy_lum_image(gltex, tex);
     break;
     break;
 
 
   case Texture::F_red:
   case Texture::F_red:
-    copy_one_channel_image(im, tex, 0);
+    copy_one_channel_image(gltex, tex, 0);
     break;
     break;
 
 
   case Texture::F_green:
   case Texture::F_green:
-    copy_one_channel_image(im, tex, 1);
+    copy_one_channel_image(gltex, tex, 1);
     break;
     break;
 
 
   case Texture::F_blue:
   case Texture::F_blue:
-    copy_one_channel_image(im, tex, 2);
+    copy_one_channel_image(gltex, tex, 2);
     break;
     break;
 
 
   case Texture::F_alpha:
   case Texture::F_alpha:
-    copy_alpha_image(im, tex);
+    copy_alpha_image(gltex, tex);
     break;
     break;
 
 
   case Texture::F_luminance_alphamask:
   case Texture::F_luminance_alphamask:
   case Texture::F_luminance_alpha:
   case Texture::F_luminance_alpha:
-    copy_la_image(im, tex);
+    copy_la_image(gltex, tex);
     break;
     break;
   }
   }
   
   
 #ifdef DO_PSTATS 
 #ifdef DO_PSTATS 
-  gtc->update_data_size_bytes(ibytecount);
+  gtc->update_data_size_bytes(bytecount);
 #endif
 #endif
   
   
   tex->texture_uploaded();
   tex->texture_uploaded();
@@ -2126,14 +2141,37 @@ upload_texture(TinyTextureContext *gtc) {
   return true;
   return true;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsStateGuardian::choose_tex_size
+//       Access: Private
+//  Description: Chooses the suitable texture size that is the closest
+//               power-of-2 match to the indicated original size.
+//               Also calculates the bit shift count, such that (1 <<
+//               bits) == size.
+////////////////////////////////////////////////////////////////////
+void TinyGraphicsStateGuardian::
+choose_tex_size(int &size, int &bits, int orig_size) {
+  unsigned int filled = flood_bits_down((unsigned int)orig_size);
+  size = filled + 1;
+  if (size > orig_size) {
+    // Round down, not up, to next lowest power of 2.
+    size >>= 1;
+  }
+  if (size > _max_texture_dimension) {
+    size = _max_texture_dimension;
+  }
+
+  bits = count_bits_in_word((unsigned int)size - 1);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TinyGraphicsStateGuardian::copy_lum_image
 //     Function: TinyGraphicsStateGuardian::copy_lum_image
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the one-channel luminance image
 //  Description: Copies and scales the one-channel luminance image
-//               from the texture into the indicated GLImage pixmap.
+//               from the texture into the indicated GLTexture.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_lum_image(GLImage *im, Texture *tex) {
+copy_lum_image(GLTexture *gltex, Texture *tex) {
   nassertv(tex->get_num_components() == 1);
   nassertv(tex->get_num_components() == 1);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2152,9 +2190,9 @@ copy_lum_image(GLImage *im, Texture *tex) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
@@ -2185,10 +2223,10 @@ copy_lum_image(GLImage *im, Texture *tex) {
 //     Function: TinyGraphicsStateGuardian::copy_alpha_image
 //     Function: TinyGraphicsStateGuardian::copy_alpha_image
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the one-channel alpha image
 //  Description: Copies and scales the one-channel alpha image
-//               from the texture into the indicated GLImage pixmap.
+//               from the texture into the indicated GLTexture pixmap.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_alpha_image(GLImage *im, Texture *tex) {
+copy_alpha_image(GLTexture *gltex, Texture *tex) {
   nassertv(tex->get_num_components() == 1);
   nassertv(tex->get_num_components() == 1);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2207,9 +2245,9 @@ copy_alpha_image(GLImage *im, Texture *tex) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
@@ -2241,10 +2279,10 @@ copy_alpha_image(GLImage *im, Texture *tex) {
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the one-channel image (with a
 //  Description: Copies and scales the one-channel image (with a
 //               single channel, e.g. red, green, or blue) from
 //               single channel, e.g. red, green, or blue) from
-//               the texture into the indicated GLImage pixmap.
+//               the texture into the indicated GLTexture pixmap.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_one_channel_image(GLImage *im, Texture *tex, int channel) {
+copy_one_channel_image(GLTexture *gltex, Texture *tex, int channel) {
   nassertv(tex->get_num_components() == 1);
   nassertv(tex->get_num_components() == 1);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2263,9 +2301,9 @@ copy_one_channel_image(GLImage *im, Texture *tex, int channel) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
@@ -2297,11 +2335,11 @@ copy_one_channel_image(GLImage *im, Texture *tex, int channel) {
 //     Function: TinyGraphicsStateGuardian::copy_la_image
 //     Function: TinyGraphicsStateGuardian::copy_la_image
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the two-channel luminance-alpha
 //  Description: Copies and scales the two-channel luminance-alpha
-//               image from the texture into the indicated GLImage
+//               image from the texture into the indicated GLTexture
 //               pixmap.
 //               pixmap.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_la_image(GLImage *im, Texture *tex) {
+copy_la_image(GLTexture *gltex, Texture *tex) {
   nassertv(tex->get_num_components() == 2);
   nassertv(tex->get_num_components() == 2);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2320,9 +2358,9 @@ copy_la_image(GLImage *im, Texture *tex) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
@@ -2353,10 +2391,10 @@ copy_la_image(GLImage *im, Texture *tex) {
 //     Function: TinyGraphicsStateGuardian::copy_rgb_image
 //     Function: TinyGraphicsStateGuardian::copy_rgb_image
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the three-channel RGB image from
 //  Description: Copies and scales the three-channel RGB image from
-//               the texture into the indicated GLImage pixmap.
+//               the texture into the indicated GLTexture pixmap.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_rgb_image(GLImage *im, Texture *tex) {
+copy_rgb_image(GLTexture *gltex, Texture *tex) {
   nassertv(tex->get_num_components() == 3);
   nassertv(tex->get_num_components() == 3);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2375,9 +2413,9 @@ copy_rgb_image(GLImage *im, Texture *tex) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
@@ -2408,10 +2446,10 @@ copy_rgb_image(GLImage *im, Texture *tex) {
 //     Function: TinyGraphicsStateGuardian::copy_rgba_image
 //     Function: TinyGraphicsStateGuardian::copy_rgba_image
 //       Access: Private, Static
 //       Access: Private, Static
 //  Description: Copies and scales the four-channel RGBA image from
 //  Description: Copies and scales the four-channel RGBA image from
-//               the texture into the indicated GLImage pixmap.
+//               the texture into the indicated GLTexture pixmap.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void TinyGraphicsStateGuardian::
 void TinyGraphicsStateGuardian::
-copy_rgba_image(GLImage *im, Texture *tex) {
+copy_rgba_image(GLTexture *gltex, Texture *tex) {
   nassertv(tex->get_num_components() == 4);
   nassertv(tex->get_num_components() == 4);
 
 
   int xsize_src = tex->get_x_size();
   int xsize_src = tex->get_x_size();
@@ -2430,9 +2468,9 @@ copy_rgba_image(GLImage *im, Texture *tex) {
   int co = cw - 1;
   int co = cw - 1;
 #endif
 #endif
 
 
-  int xsize_dest = im->xsize;
-  int ysize_dest = im->xsize;
-  unsigned char *dest = (unsigned char *)im->pixmap;
+  int xsize_dest = gltex->xsize;
+  int ysize_dest = gltex->ysize;
+  unsigned char *dest = (unsigned char *)gltex->pixmap;
   nassertv(dest != NULL);
   nassertv(dest != NULL);
 
 
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));
   int sx_inc = (int)((float)(xsize_src) / (float)(xsize_dest));

+ 9 - 8
panda/src/tinydisplay/tinyGraphicsStateGuardian.h

@@ -33,7 +33,7 @@ extern "C" {
 class TinyTextureContext;
 class TinyTextureContext;
 struct GLContext;
 struct GLContext;
 struct GLVertex;
 struct GLVertex;
-struct GLImage;
+struct GLTexture;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : TinyGraphicsStateGuardian
 //       Class : TinyGraphicsStateGuardian
@@ -107,13 +107,14 @@ private:
 
 
   void apply_texture(TextureContext *tc);
   void apply_texture(TextureContext *tc);
   bool upload_texture(TinyTextureContext *gtc);
   bool upload_texture(TinyTextureContext *gtc);
-
-  static void copy_lum_image(GLImage *im, Texture *tex);
-  static void copy_alpha_image(GLImage *im, Texture *tex);
-  static void copy_one_channel_image(GLImage *im, Texture *tex, int channel);
-  static void copy_la_image(GLImage *im, Texture *tex);
-  static void copy_rgb_image(GLImage *im, Texture *tex);
-  static void copy_rgba_image(GLImage *im, Texture *tex);
+  void choose_tex_size(int &size, int &bits, int orig_size);
+
+  static void copy_lum_image(GLTexture *gltex, Texture *tex);
+  static void copy_alpha_image(GLTexture *gltex, Texture *tex);
+  static void copy_one_channel_image(GLTexture *gltex, Texture *tex, int channel);
+  static void copy_la_image(GLTexture *gltex, Texture *tex);
+  static void copy_rgb_image(GLTexture *gltex, Texture *tex);
+  static void copy_rgba_image(GLTexture *gltex, Texture *tex);
 
 
   static void load_matrix(M4 *matrix, const TransformState *transform);
   static void load_matrix(M4 *matrix, const TransformState *transform);
 
 

+ 2 - 2
panda/src/tinydisplay/zbuffer.c

@@ -67,7 +67,7 @@ ZBuffer *ZB_open(int xsize, int ysize, int mode,
 	zb->pbuf = frame_buffer;
 	zb->pbuf = frame_buffer;
     }
     }
 
 
-    zb->current_texture = NULL;
+    zb->current_texture.pixmap = NULL;
 
 
     return zb;
     return zb;
   error:
   error:
@@ -344,7 +344,7 @@ void ZB_clear_viewport(ZBuffer * zb, int clear_z, int z,
 #define ZB_ST_FRAC_HIGH (1 << ZB_POINT_ST_FRAC_BITS)
 #define ZB_ST_FRAC_HIGH (1 << ZB_POINT_ST_FRAC_BITS)
 #define ZB_ST_FRAC_MASK (ZB_ST_FRAC_HIGH - 1)
 #define ZB_ST_FRAC_MASK (ZB_ST_FRAC_HIGH - 1)
 
 
-PIXEL lookup_texture_bilinear(PIXEL *texture, int s, int t)
+PIXEL lookup_texture_bilinear(ZTexture *texture, int s, int t)
 {
 {
   PIXEL p1, p2, p3, p4;
   PIXEL p1, p2, p3, p4;
   int sf, tf;
   int sf, tf;

+ 14 - 14
panda/src/tinydisplay/zbuffer.h

@@ -18,22 +18,18 @@
 /* The number of fractional bits below the S and T texture coords.
 /* The number of fractional bits below the S and T texture coords.
    The more we have, the more precise the texel calculation will be
    The more we have, the more precise the texel calculation will be
    when we zoom into small details of a texture; but the greater
    when we zoom into small details of a texture; but the greater
-   chance we might overflow our 32-bit integer. */
+   chance we might overflow our 32-bit integer when texcoords get
+   large.  This also limits our greatest texture size (its T dimension
+   cannot exceed this number of bits).*/
 #define ZB_POINT_ST_FRAC_BITS 12
 #define ZB_POINT_ST_FRAC_BITS 12
 
 
-/* Various parameters and accessors based on the above bits. */
-#define ZB_POINT_ST_MIN 0
-#define ZB_POINT_ST_MAX (1 << (ZB_POINT_ST_BITS + ZB_POINT_ST_FRAC_BITS))
-#define ZB_POINT_ST_MASK ((1 << (ZB_POINT_ST_BITS + ZB_POINT_ST_FRAC_BITS)) - (1 << ZB_POINT_ST_FRAC_BITS))
-
-/* Returns the index within a 256x256 texture for the given (s, t)
-   texel. */
-#define ZB_TEXEL(s, t) \
-  ((((t) & ZB_POINT_ST_MASK) >> (ZB_POINT_ST_FRAC_BITS - ZB_POINT_ST_BITS)) | \
-   (((s) & ZB_POINT_ST_MASK) >> ZB_POINT_ST_FRAC_BITS))
+/* Returns the index within a texture for the given (s, t) texel. */
+#define ZB_TEXEL(texture, s, t)                                         \
+  ((((t) & (texture)->t_mask) >> (texture)->t_shift) |                  \
+   (((s) & (texture)->s_mask) >> ZB_POINT_ST_FRAC_BITS))
 
 
 #define ZB_LOOKUP_TEXTURE_NEAREST(texture, s, t) \
 #define ZB_LOOKUP_TEXTURE_NEAREST(texture, s, t) \
-  (texture)[ZB_TEXEL(s, t)]
+  (texture)->pixmap[ZB_TEXEL(texture, s, t)]
 
 
 #if 1
 #if 1
 /* Use no texture filtering by default.  It's faster, even though it
 /* Use no texture filtering by default.  It's faster, even though it
@@ -90,6 +86,10 @@ typedef unsigned int PIXEL;
 #define PIXEL_BLEND_RGB(rgb, r, g, b, a) \
 #define PIXEL_BLEND_RGB(rgb, r, g, b, a) \
   PIXEL_BLEND(PIXEL_R(rgb), PIXEL_G(rgb), PIXEL_B(rgb), r, g, b, a)
   PIXEL_BLEND(PIXEL_R(rgb), PIXEL_G(rgb), PIXEL_B(rgb), r, g, b, a)
 
 
+typedef struct {
+  PIXEL *pixmap;
+  int s_mask, t_mask, t_shift;
+} ZTexture;
 
 
 typedef struct {
 typedef struct {
     int xsize,ysize;
     int xsize,ysize;
@@ -103,7 +103,7 @@ typedef struct {
     int nb_colors;
     int nb_colors;
     unsigned char *dctable;
     unsigned char *dctable;
     int *ctable;
     int *ctable;
-    PIXEL *current_texture;
+    ZTexture current_texture;
     unsigned int reference_alpha;
     unsigned int reference_alpha;
 } ZBuffer;
 } ZBuffer;
 
 
@@ -133,7 +133,7 @@ void ZB_clear_viewport(ZBuffer * zb, int clear_z, int z,
                        int clear_color, int r, int g, int b, int a,
                        int clear_color, int r, int g, int b, int a,
                        int xmin, int ymin, int xsize, int ysize);
                        int xmin, int ymin, int xsize, int ysize);
 
 
-PIXEL lookup_texture_bilinear(PIXEL *texture, int s, int t);
+PIXEL lookup_texture_bilinear(ZTexture *texture, int s, int t);
 
 
 /* linesize is in BYTES */
 /* linesize is in BYTES */
 void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,int linesize);
 void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,int linesize);

+ 4 - 15
panda/src/tinydisplay/zgl.h

@@ -125,22 +125,15 @@ typedef struct GLVertex {
   ZBufferPoint zp;      /* integer coordinates for the rasterization */
   ZBufferPoint zp;      /* integer coordinates for the rasterization */
 } GLVertex;
 } GLVertex;
 
 
-typedef struct GLImage {
-  void *pixmap;
-  int xsize,ysize;
-} GLImage;
-
 /* textures */
 /* textures */
 
 
-#define TEXTURE_HASH_TABLE_SIZE 256
-
 typedef struct GLTexture {
 typedef struct GLTexture {
-  GLImage images[MAX_TEXTURE_LEVELS];
-  int handle;
-  struct GLTexture *next,*prev;
+  PIXEL *pixmap;
+  int xsize, ysize;
+  int s_mask, t_mask, t_shift;
+  int s_max, t_max;
 } GLTexture;
 } GLTexture;
 
 
-
 /* shared state */
 /* shared state */
 
 
 typedef struct GLSharedState {
 typedef struct GLSharedState {
@@ -307,10 +300,6 @@ void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax);
 void gl_enable_disable_light(GLContext *c,int light,int v);
 void gl_enable_disable_light(GLContext *c,int light,int v);
 void gl_shade_vertex(GLContext *c,GLVertex *v);
 void gl_shade_vertex(GLContext *c,GLVertex *v);
 
 
-void glInitTextures(GLContext *c);
-void glEndTextures(GLContext *c);
-GLTexture *alloc_texture(GLContext *c,int h);
-
 /* vertex.c */
 /* vertex.c */
 void gl_eval_viewport(GLContext *c);
 void gl_eval_viewport(GLContext *c);
 
 

+ 12 - 12
panda/src/tinydisplay/ztriangle_two.h

@@ -68,14 +68,14 @@ void FNAME(ZB_fillTriangleSmooth) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMapping) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMapping) (ZBuffer *zb,
                                     ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                     ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
 
 
 #define INTERP_Z
 #define INTERP_Z
 #define INTERP_ST
 #define INTERP_ST
 
 
 #define DRAW_INIT()				\
 #define DRAW_INIT()				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;                \
   }
   }
 
 
 #define PUT_PIXEL(_a)                                                   \
 #define PUT_PIXEL(_a)                                                   \
@@ -99,7 +99,7 @@ void FNAME(ZB_fillTriangleMapping) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingFlat) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingFlat) (ZBuffer *zb,
                                         ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                         ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
   int or, og, ob, oa;
   int or, og, ob, oa;
 
 
 #define INTERP_Z
 #define INTERP_Z
@@ -107,7 +107,7 @@ void FNAME(ZB_fillTriangleMappingFlat) (ZBuffer *zb,
 
 
 #define DRAW_INIT()				\
 #define DRAW_INIT()				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;                \
     or = p2->r;                                 \
     or = p2->r;                                 \
     og = p2->g;                                 \
     og = p2->g;                                 \
     ob = p2->b;                                 \
     ob = p2->b;                                 \
@@ -144,7 +144,7 @@ void FNAME(ZB_fillTriangleMappingFlat) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingSmooth) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingSmooth) (ZBuffer *zb,
                                           ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                           ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
 
 
 #define INTERP_Z
 #define INTERP_Z
 #define INTERP_ST
 #define INTERP_ST
@@ -152,7 +152,7 @@ void FNAME(ZB_fillTriangleMappingSmooth) (ZBuffer *zb,
 
 
 #define DRAW_INIT()				\
 #define DRAW_INIT()				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;                \
   }
   }
 
 
 #define PUT_PIXEL(_a)                                                   \
 #define PUT_PIXEL(_a)                                                   \
@@ -194,7 +194,7 @@ void FNAME(ZB_fillTriangleMappingSmooth) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspective) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspective) (ZBuffer *zb,
                                                ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                                ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
 
 
 #define INTERP_Z
 #define INTERP_Z
@@ -204,7 +204,7 @@ void FNAME(ZB_fillTriangleMappingPerspective) (ZBuffer *zb,
 
 
 #define DRAW_INIT()				\
 #define DRAW_INIT()				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;                \
     fdzdx=(float)dzdx;                          \
     fdzdx=(float)dzdx;                          \
     fndzdx=NB_INTERP * fdzdx;                   \
     fndzdx=NB_INTERP * fdzdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \
@@ -295,7 +295,7 @@ void FNAME(ZB_fillTriangleMappingPerspective) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspectiveFlat) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspectiveFlat) (ZBuffer *zb,
                                                    ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                                    ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
   int or, og, ob, oa;
   int or, og, ob, oa;
 
 
@@ -306,7 +306,7 @@ void FNAME(ZB_fillTriangleMappingPerspectiveFlat) (ZBuffer *zb,
 
 
 #define DRAW_INIT() 				\
 #define DRAW_INIT() 				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;                \
     fdzdx=(float)dzdx;                          \
     fdzdx=(float)dzdx;                          \
     fndzdx=NB_INTERP * fdzdx;                   \
     fndzdx=NB_INTERP * fdzdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \
@@ -414,7 +414,7 @@ void FNAME(ZB_fillTriangleMappingPerspectiveFlat) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspectiveSmooth) (ZBuffer *zb,
 void FNAME(ZB_fillTriangleMappingPerspectiveSmooth) (ZBuffer *zb,
                                                      ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
                                                      ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
 {
-  PIXEL *texture;
+  ZTexture *texture;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
   float fdzdx,fndzdx,ndszdx,ndtzdx;
 
 
 #define INTERP_Z
 #define INTERP_Z
@@ -423,7 +423,7 @@ void FNAME(ZB_fillTriangleMappingPerspectiveSmooth) (ZBuffer *zb,
 
 
 #define DRAW_INIT() 				\
 #define DRAW_INIT() 				\
   {						\
   {						\
-    texture=zb->current_texture;                \
+    texture = &zb->current_texture;             \
     fdzdx=(float)dzdx;                          \
     fdzdx=(float)dzdx;                          \
     fndzdx=NB_INTERP * fdzdx;                   \
     fndzdx=NB_INTERP * fdzdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \
     ndszdx=NB_INTERP * dszdx;                   \