⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 via_tex.c

📁 Mesa is an open-source implementation of the OpenGL specification - a system for rendering interacti
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved. * Copyright 2001-2003 S3 Graphics, Inc. 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, sub license, * 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 (including the * next paragraph) 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 NON-INFRINGEMENT. IN NO EVENT SHALL * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS 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. */#include <stdlib.h>#include <stdio.h>#include "glheader.h"#include "macros.h"#include "mtypes.h"#include "enums.h"#include "colortab.h"#include "convolve.h"#include "context.h"#include "mipmap.h"#include "simple_list.h"#include "texcompress.h"#include "texformat.h"#include "texobj.h"#include "texstore.h"#include "mm.h"#include "via_context.h"#include "via_fb.h"#include "via_tex.h"#include "via_state.h"#include "via_ioctl.h"#include "via_3d_reg.h"static const struct gl_texture_format *viaChooseTexFormat( GLcontext *ctx, GLint internalFormat,		    GLenum format, GLenum type ){   struct via_context *vmesa = VIA_CONTEXT(ctx);   const GLboolean do32bpt = ( vmesa->viaScreen->bitsPerPixel == 32/* 			       && vmesa->viaScreen->textureSize > 4*1024*1024 */      );   switch ( internalFormat ) {   case 4:   case GL_RGBA:   case GL_COMPRESSED_RGBA:      if ( format == GL_BGRA ) {	 if ( type == GL_UNSIGNED_INT_8_8_8_8_REV ||	      type == GL_UNSIGNED_BYTE ) {	    return &_mesa_texformat_argb8888;	 }         else if ( type == GL_UNSIGNED_SHORT_4_4_4_4_REV ) {            return &_mesa_texformat_argb4444;	 }         else if ( type == GL_UNSIGNED_SHORT_1_5_5_5_REV ) {	    return &_mesa_texformat_argb1555;	 }      }      else if ( type == GL_UNSIGNED_BYTE ||		type == GL_UNSIGNED_INT_8_8_8_8_REV ||		type == GL_UNSIGNED_INT_8_8_8_8 ) {	 return &_mesa_texformat_argb8888;      }      return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;   case 3:   case GL_RGB:   case GL_COMPRESSED_RGB:      if ( format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5 ) {	 return &_mesa_texformat_rgb565;      }      else if ( type == GL_UNSIGNED_BYTE ) {	 return &_mesa_texformat_argb8888;      }      return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;   case GL_RGBA8:   case GL_RGB10_A2:   case GL_RGBA12:   case GL_RGBA16:      return &_mesa_texformat_argb8888;   case GL_RGBA4:   case GL_RGBA2:      return &_mesa_texformat_argb4444;   case GL_RGB5_A1:      return &_mesa_texformat_argb1555;   case GL_RGB8:   case GL_RGB10:   case GL_RGB12:   case GL_RGB16:      return &_mesa_texformat_argb8888;   case GL_RGB5:   case GL_RGB4:   case GL_R3_G3_B2:      return &_mesa_texformat_rgb565;   case GL_ALPHA:   case GL_ALPHA4:   case GL_ALPHA8:   case GL_ALPHA12:   case GL_ALPHA16:   case GL_COMPRESSED_ALPHA:      return &_mesa_texformat_a8;   case 1:   case GL_LUMINANCE:   case GL_LUMINANCE4:   case GL_LUMINANCE8:   case GL_LUMINANCE12:   case GL_LUMINANCE16:   case GL_COMPRESSED_LUMINANCE:      return &_mesa_texformat_l8;   case 2:   case GL_LUMINANCE_ALPHA:   case GL_LUMINANCE4_ALPHA4:   case GL_LUMINANCE6_ALPHA2:   case GL_LUMINANCE8_ALPHA8:   case GL_LUMINANCE12_ALPHA4:   case GL_LUMINANCE12_ALPHA12:   case GL_LUMINANCE16_ALPHA16:   case GL_COMPRESSED_LUMINANCE_ALPHA:      return &_mesa_texformat_al88;   case GL_INTENSITY:   case GL_INTENSITY4:   case GL_INTENSITY8:   case GL_INTENSITY12:   case GL_INTENSITY16:   case GL_COMPRESSED_INTENSITY:      return &_mesa_texformat_i8;   case GL_YCBCR_MESA:      if (type == GL_UNSIGNED_SHORT_8_8_MESA ||	  type == GL_UNSIGNED_BYTE)         return &_mesa_texformat_ycbcr;      else         return &_mesa_texformat_ycbcr_rev;   case GL_COMPRESSED_RGB_FXT1_3DFX:      return &_mesa_texformat_rgb_fxt1;   case GL_COMPRESSED_RGBA_FXT1_3DFX:      return &_mesa_texformat_rgba_fxt1;   case GL_RGB_S3TC:   case GL_RGB4_S3TC:   case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:      return &_mesa_texformat_rgb_dxt1;   case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:      return &_mesa_texformat_rgba_dxt1;   case GL_RGBA_S3TC:   case GL_RGBA4_S3TC:   case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:      return &_mesa_texformat_rgba_dxt3;   case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:      return &_mesa_texformat_rgba_dxt5;   case GL_COLOR_INDEX:	   case GL_COLOR_INDEX1_EXT:	   case GL_COLOR_INDEX2_EXT:	   case GL_COLOR_INDEX4_EXT:	   case GL_COLOR_INDEX8_EXT:	   case GL_COLOR_INDEX12_EXT:	       case GL_COLOR_INDEX16_EXT:      return &_mesa_texformat_ci8;       default:      fprintf(stderr, "unexpected texture format %s in %s\n", 	      _mesa_lookup_enum_by_nr(internalFormat),	      __FUNCTION__);      return NULL;   }   return NULL; /* never get here */}static int logbase2(int n){   GLint i = 1;   GLint log2 = 0;   while (n > i) {      i *= 2;      log2++;   }   return log2;}static const char *get_memtype_name( GLint memType ){   static const char *names[] = {      "VIA_MEM_VIDEO",      "VIA_MEM_AGP",      "VIA_MEM_SYSTEM",      "VIA_MEM_MIXED",      "VIA_MEM_UNKNOWN"   };   return names[memType];}static GLboolean viaMoveTexBuffers( struct via_context *vmesa,				    struct via_tex_buffer **buffers,				    GLuint nr,				    GLint newMemType ){   struct via_tex_buffer *newTexBuf[VIA_MAX_TEXLEVELS];   GLint i;   if (VIA_DEBUG & DEBUG_TEXTURE)      fprintf(stderr, "%s to %s\n",	      __FUNCTION__,	      get_memtype_name(newMemType));   memset(newTexBuf, 0, sizeof(newTexBuf));   /* First do all the allocations (or fail):    */    for (i = 0; i < nr; i++) {          if (buffers[i]->memType != newMemType) {	 	 /* Don't allow uploads in a thrash state.  Should try and	  * catch this earlier.	  */	 if (vmesa->thrashing && newMemType != VIA_MEM_SYSTEM)	    goto cleanup;	 newTexBuf[i] = via_alloc_texture(vmesa, 					  buffers[i]->size,					  newMemType);	 if (!newTexBuf[i]) 	    goto cleanup;      }   }   /* Now copy all the image data and free the old texture memory.    */   for (i = 0; i < nr; i++) {          if (newTexBuf[i]) {	 memcpy(newTexBuf[i]->bufAddr,		buffers[i]->bufAddr, 		buffers[i]->size);	 newTexBuf[i]->image = buffers[i]->image;	 newTexBuf[i]->image->texMem = newTexBuf[i];	 newTexBuf[i]->image->image.Data = newTexBuf[i]->bufAddr;	 via_free_texture(vmesa, buffers[i]);      }   }   if (VIA_DEBUG & DEBUG_TEXTURE)      fprintf(stderr, "%s - success\n", __FUNCTION__);   return GL_TRUE; cleanup:   /* Release any allocations made prior to failure:    */   if (VIA_DEBUG & DEBUG_TEXTURE)      fprintf(stderr, "%s - failed\n", __FUNCTION__);   for (i = 0; i < nr; i++) {          if (newTexBuf[i]) {	 via_free_texture(vmesa, newTexBuf[i]);      }   }      return GL_FALSE;   }static GLboolean viaMoveTexObject( struct via_context *vmesa,				   struct via_texture_object *viaObj,				   GLint newMemType ){      struct via_texture_image **viaImage =       (struct via_texture_image **)&viaObj->obj.Image[0][0];   struct via_tex_buffer *buffers[VIA_MAX_TEXLEVELS];   GLuint i, nr = 0;   for (i = viaObj->firstLevel; i <= viaObj->lastLevel; i++)      buffers[nr++] = viaImage[i]->texMem;   if (viaMoveTexBuffers( vmesa, &buffers[0], nr, newMemType )) {      viaObj->memType = newMemType;      return GL_TRUE;   }   return GL_FALSE;}static GLboolean viaSwapInTexObject( struct via_context *vmesa,				     struct via_texture_object *viaObj ){   const struct via_texture_image *baseImage =       (struct via_texture_image *)viaObj->obj.Image[0][viaObj->obj.BaseLevel];    if (VIA_DEBUG & DEBUG_TEXTURE)      fprintf(stderr, "%s\n", __FUNCTION__);   if (baseImage->texMem->memType != VIA_MEM_SYSTEM)       return viaMoveTexObject( vmesa, viaObj, baseImage->texMem->memType );   return (viaMoveTexObject( vmesa, viaObj, VIA_MEM_AGP ) ||	   viaMoveTexObject( vmesa, viaObj, VIA_MEM_VIDEO ));}/* This seems crude, but it asks a fairly pertinent question and gives * an accurate answer: */static GLboolean viaIsTexMemLow( struct via_context *vmesa,				 GLuint heap ){   struct via_tex_buffer *buf =  via_alloc_texture(vmesa, 512 * 1024, heap );   if (!buf)      return GL_TRUE;      via_free_texture(vmesa, buf);   return GL_FALSE;}/* Speculatively move texture images which haven't been used in a * while back to system memory.  *  * TODO: only do this when texture memory is low. *  * TODO: use dma. * * TODO: keep the fb/agp version hanging around and use the local * version as backing store, so re-upload might be avoided. * * TODO: do this properly in the kernel... */GLboolean viaSwapOutWork( struct via_context *vmesa ){   struct via_tex_buffer *s, *tmp;   GLuint done = 0;   GLuint heap, target;   if (VIA_DEBUG & DEBUG_TEXTURE)      fprintf(stderr, "%s VID %d AGP %d SYS %d\n", __FUNCTION__,	      vmesa->total_alloc[VIA_MEM_VIDEO],	      vmesa->total_alloc[VIA_MEM_AGP],	      vmesa->total_alloc[VIA_MEM_SYSTEM]);      for (heap = VIA_MEM_VIDEO; heap <= VIA_MEM_AGP; heap++) {      GLuint nr = 0, sz = 0;      if (vmesa->thrashing) { 	 if (VIA_DEBUG & DEBUG_TEXTURE)	    fprintf(stderr, "Heap %d: trash flag\n", heap);	 target = 1*1024*1024;      }      else if (viaIsTexMemLow(vmesa, heap)) { 	 if (VIA_DEBUG & DEBUG_TEXTURE)	    fprintf(stderr, "Heap %d: low memory\n", heap);	 target = 64*1024;      }      else { 	 if (VIA_DEBUG & DEBUG_TEXTURE)	    fprintf(stderr, "Heap %d: nothing to do\n", heap);	 continue;      }      foreach_s( s, tmp, &vmesa->tex_image_list[heap] ) {	 if (s->lastUsed < vmesa->lastSwap[1]) {	    struct via_texture_object *viaObj = 	       (struct via_texture_object *) s->image->image.TexObject;	    if (VIA_DEBUG & DEBUG_TEXTURE)	       fprintf(stderr, 		       "back copy tex sz %d, lastUsed %d lastSwap %d\n", 		       s->size, s->lastUsed, vmesa->lastSwap[1]);	    if (viaMoveTexBuffers( vmesa, &s, 1, VIA_MEM_SYSTEM )) {	       viaObj->memType = VIA_MEM_MIXED;	       done += s->size;	    }	    else {	       if (VIA_DEBUG & DEBUG_TEXTURE)		  fprintf(stderr, "Failed to back copy texture!\n");	       sz += s->size;	    }	 }	 else {	    nr ++;	    sz += s->size;	 }	 if (done > target) {	    vmesa->thrashing = GL_FALSE; /* might not get set otherwise? */	    return GL_TRUE;	 }      }      assert(sz == vmesa->total_alloc[heap]);	       if (VIA_DEBUG & DEBUG_TEXTURE)	 fprintf(stderr, "Heap %d: nr %d tot sz %d\n", heap, nr, sz);   }      return done != 0;}/* Basically, just collect the image dimensions and addresses for each * image and update the texture object state accordingly. */static GLboolean viaSetTexImages(GLcontext *ctx,				 struct gl_texture_object *texObj){   struct via_context *vmesa = VIA_CONTEXT(ctx);   struct via_texture_object *viaObj = (struct via_texture_object *)texObj;   const struct via_texture_image *baseImage =       (struct via_texture_image *)texObj->Image[0][texObj->BaseLevel];   GLint firstLevel, lastLevel, numLevels;   GLuint texFormat;   GLint w, h, p;   GLint i, j = 0, k = 0, l = 0, m = 0;   GLuint texBase;   GLuint basH = 0;   GLuint widthExp = 0;   GLuint heightExp = 0;       switch (baseImage->image.TexFormat->MesaFormat) {   case MESA_FORMAT_ARGB8888:      texFormat = HC_HTXnFM_ARGB8888;      break;   case MESA_FORMAT_ARGB4444:      texFormat = HC_HTXnFM_ARGB4444;       break;   case MESA_FORMAT_RGB565:      texFormat = HC_HTXnFM_RGB565;         break;   case MESA_FORMAT_ARGB1555:      texFormat = HC_HTXnFM_ARGB1555;         break;   case MESA_FORMAT_RGB888:      texFormat = HC_HTXnFM_ARGB0888;      break;   case MESA_FORMAT_L8:      texFormat = HC_HTXnFM_L8;             break;   case MESA_FORMAT_I8:      texFormat = HC_HTXnFM_T8;             break;   case MESA_FORMAT_CI8:      texFormat = HC_HTXnFM_Index8;         break;   case MESA_FORMAT_AL88:      texFormat = HC_HTXnFM_AL88;           break;   case MESA_FORMAT_A8:      texFormat = HC_HTXnFM_A8;           break;   default:      _mesa_problem(vmesa->glCtx, "Bad texture format in viaSetTexImages");      return GL_FALSE;   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -