Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

GThandle.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  GTHandle.c                                                                          */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: THandle manager for glide                                              */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*Genesis3D Version 1.1 released November 15, 1999                            */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00020 /*                                                                                      */
00021 /****************************************************************************************/
00022 #include <Windows.h>
00023 #include <Assert.h>
00024 
00025 #include "GTHandle.h"
00026 #include "GCache.h"
00027 #include "GMain.h"
00028 #include "GlideDrv.h"
00029 
00030 
00031 #define TEXTURE_CACHE_PERCENT                   0.75f
00032 #define LMAP_CACHE_PERCENT                              0.25f
00033 
00034 // Memory managers
00035 GMemMgr                         *MemMgr[2];
00036 
00037 // Texture caches
00038 GCache                          *TextureCache;                  // Texture cache
00039 GCache                          *LMapCache;                             // Lightmap texture cache
00040 
00041 geRDriver_THandle       TextureHandles[MAX_TEXTURE_HANDLES];            // Contain Texture/Decal/Lightmap handles
00042 
00043 geBoolean                       TexturesChanged;
00044 geBoolean                       LMapsChanged;
00045 
00046 //==================================================================================
00047 //      GTHandle_Startup
00048 //==================================================================================
00049 geBoolean GTHandle_Startup(void)
00050 {
00051         if (g_BoardInfo.NumTMU >= 2)
00052         {
00053                 TMU[0] = GR_TMU0;
00054                 TMU[1] = GR_TMU1;
00055 
00056                 MemMgr[0] = GMemMgr_Create(GR_TMU0, grTexMinAddress(GR_TMU0), grTexMaxAddress(GR_TMU0));
00057                 if (!MemMgr[0])
00058                         goto ExitWithError;
00059                 MemMgr[1] = GMemMgr_Create(GR_TMU1, grTexMinAddress(GR_TMU1), grTexMaxAddress(GR_TMU1));
00060                 if (!MemMgr[1])
00061                         goto ExitWithError;
00062 
00063         }
00064         else
00065         {
00066                 uint32  MinAddress, MaxAddress, MidAddress;
00067 
00068                 TMU[0] = GR_TMU0;
00069                 TMU[1] = GR_TMU0;
00070 
00071                 MinAddress = grTexMinAddress(GR_TMU0);
00072                 MaxAddress = grTexMaxAddress(GR_TMU0);
00073                 MidAddress = MinAddress + (uint32)((geFloat)(MaxAddress - MinAddress)*TEXTURE_CACHE_PERCENT);
00074 
00075                 MemMgr[0] = GMemMgr_Create(GR_TMU0, MinAddress, MidAddress);
00076                 if (!MemMgr[0])
00077                         goto ExitWithError;
00078                 MemMgr[1] = GMemMgr_Create(GR_TMU0, MidAddress+1, MaxAddress);
00079                 if (!MemMgr[1])
00080                         goto ExitWithError;
00081         }
00082 
00083         TextureCache = GCache_Create("Texture Cache", MemMgr[0]);
00084 
00085         if (!TextureCache)
00086                 goto ExitWithError;
00087 
00088         LMapCache = GCache_Create("Lightmap Cache", MemMgr[1]);
00089 
00090         if (!LMapCache)
00091                 goto ExitWithError;
00092 
00093         return GE_TRUE;
00094 
00095         ExitWithError:
00096         {
00097                 GTHandle_FreeAllTextureHandles();
00098                 GTHandle_FreeAllCaches();
00099 
00100                 return GE_FALSE;
00101         }
00102 }
00103 
00104 //==================================================================================
00105 //      GTHandle_Shutdown
00106 //==================================================================================
00107 void GTHandle_Shutdown(void)
00108 {
00109         // Free all the texture handles first!!!
00110         GTHandle_FreeAllTextureHandles();
00111         // Then free all the caches
00112         GTHandle_FreeAllCaches();
00113 
00114         TexturesChanged = GE_FALSE;
00115         LMapsChanged = GE_FALSE;
00116 }
00117 
00118 //==================================================================================
00119 //==================================================================================
00120 void GTHandle_FreeAllCaches(void)
00121 {
00122         if (LMapCache)
00123                 GCache_Destroy(LMapCache);
00124         if (TextureCache)
00125                 GCache_Destroy(TextureCache);
00126 
00127         TextureCache = NULL;
00128         LMapCache = NULL;
00129 
00130         if (MemMgr[1])
00131                 GMemMgr_Destroy(MemMgr[1]);
00132         if (MemMgr[0])
00133                 GMemMgr_Destroy(MemMgr[0]);
00134 
00135         MemMgr[0] = NULL;
00136         MemMgr[1] = NULL;
00137 }
00138 
00139 //========================================================================================
00140 //      FindTextureHandle
00141 //========================================================================================
00142 geRDriver_THandle *GTHandle_FindTextureHandle()
00143 {
00144         int32                           i;
00145         geRDriver_THandle       *THandle;
00146 
00147         THandle = TextureHandles;
00148 
00149         for (i=0; i< MAX_TEXTURE_HANDLES; i++, THandle++)
00150         {
00151                 if (!THandle->Active)
00152                 {
00153                         assert(THandle->Data == NULL);
00154 
00155                         memset(THandle, 0, sizeof(geRDriver_THandle));
00156 
00157                         THandle->Active = GE_TRUE;
00158 
00159                         return THandle;
00160                 }
00161         }
00162 
00163         return NULL;
00164 }
00165 
00166 //========================================================================================
00167 //      GTHandle_FreeTextureHandle
00168 //========================================================================================
00169 void GTHandle_FreeTextureHandle(geRDriver_THandle *THandle)
00170 {
00171         assert(THandle);
00172         assert(THandle->Active == GE_TRUE);
00173 
00174         if (THandle->Data)
00175                 free(THandle->Data);
00176 
00177         if (THandle->CacheType)
00178         {
00179                 GCache_TypeDestroy(THandle->CacheType);
00180                 TexturesChanged = GE_TRUE;
00181         }
00182 
00183         memset(THandle, 0, sizeof(geRDriver_THandle));  
00184 }
00185 
00186 //==================================================================================
00187 //      GTHandle_FreeAllTextureHandles
00188 //==================================================================================
00189 void GTHandle_FreeAllTextureHandles(void)
00190 {
00191         int32                           i;
00192         geRDriver_THandle       *pTHandle;
00193 
00194         pTHandle = TextureHandles;
00195 
00196         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pTHandle++)
00197         {
00198                 if (!pTHandle->Active)
00199                 {
00200                         assert(pTHandle->Data == NULL);
00201                         continue;
00202                 }
00203                 
00204                 GTHandle_FreeTextureHandle(pTHandle);
00205         }
00206 }
00207 
00208 //========================================================================================
00209 //      GTHandle_SetupInfo
00210 //========================================================================================
00211 geBoolean GTHandle_SetupInfo(GrTexInfo *Info, int32 Width, int32 Height, int32 NumMipLevels, GrTextureFormat_t Format, int32 *Size)
00212 {
00213         GrLOD_t                 LLod, SLod;
00214         GrAspectRatio_t Ratio;
00215         int32                   SquareSize;
00216 
00217         SquareSize = max(Width, Height);
00218         SquareSize = SnapToPower2(SquareSize);
00219 
00220         if (!GetLod(SquareSize, &LLod))
00221                 return GE_FALSE;
00222                 
00223         if (!GetLod((SquareSize>>(NumMipLevels-1)), &SLod))
00224                 return GE_FALSE;
00225 
00226 #if 1
00227         Ratio = GR_ASPECT_1x1;
00228 #else
00229         if (!GetAspectRatio(SquareSize, SquareSize, &Ratio))
00230         {
00231                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_SetupInfo: Invalid aspect ratio.");
00232                 return GE_FALSE;
00233         }
00234 #endif
00235 
00236         *Size = SquareSize;
00237 
00238         Info->largeLod          = LLod;
00239         Info->smallLod          = SLod;
00240         Info->aspectRatio       = Ratio;
00241         Info->format            = Format;
00242 
00243         return GE_TRUE;
00244 }
00245 
00246 //============================================================================================
00247 //      GlideFormatFromGenesisFormat
00248 //============================================================================================
00249 geBoolean GlideFormatFromGenesisFormat(gePixelFormat Format, GrTextureFormat_t *Out)
00250 {
00251         switch(Format)
00252         {
00253                 case GE_PIXELFORMAT_16BIT_565_RGB:
00254                 {
00255                         *Out = GR_TEXFMT_RGB_565;
00256                         break;
00257                 }
00258                 case GE_PIXELFORMAT_16BIT_4444_ARGB:
00259                 {
00260                         *Out = GR_TEXFMT_ARGB_4444;
00261                         break;
00262                 
00263                 }
00264                 case GE_PIXELFORMAT_16BIT_1555_ARGB:
00265                 {
00266                         *Out = GR_TEXFMT_ARGB_1555;
00267                         break;
00268                 
00269                 }
00270                 case GE_PIXELFORMAT_8BIT:
00271                 {
00272                         #ifdef ALPHA_PALETTE
00273                                 *Out = GR_TEXFMT_AP_88;
00274                         #else
00275                                 *Out = GR_TEXFMT_P_8;
00276                         #endif
00277                         break;
00278                 }
00279                 
00280                 default:
00281                 {
00282                         assert(!"GlideFormatFromGenesisFormat:  Invalid Pixel format.");
00283                         *Out = GE_PIXELFORMAT_8BIT;     
00284                         return GE_FALSE;
00285                 }
00286         }
00287 
00288         return GE_TRUE;
00289 }
00290 
00291 //========================================================================================
00292 //      GTHandle_Create3DTexture
00293 //      Creates a 3d texture surface
00294 //========================================================================================
00295 geRDriver_THandle *Create3DTexture(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00296 {
00297         int32                           SWidth, SHeight;                        // Snapped to power of 2 width/height
00298         GrTexInfo                       Info;
00299         int32                           DataSize;
00300         uint8                           *Data;
00301         geRDriver_THandle       *THandle;
00302         GrTextureFormat_t       Format;
00303 
00304         assert(PixelFormat);
00305         
00306         Data = NULL;
00307 
00308         THandle = GTHandle_FindTextureHandle();
00309 
00310         if (!THandle)
00311         {
00312                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_Create3DTexture: No more handles left.");
00313                 goto ExitWithError;
00314         }
00315 
00316         if (Width > 256)
00317         {
00318                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_Create3DTexture: Width > 256.");
00319                 goto ExitWithError;
00320         }
00321 
00322         if (Height > 256)
00323         {
00324                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_Create3DTexture: Height > 256.");
00325                 goto ExitWithError;
00326         }
00327 
00328         SWidth = SnapToPower2(Width);
00329         SHeight = SnapToPower2(Height);
00330 
00331         if (Width != SWidth)
00332         {
00333                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_Create3DTexture: Not a power of 2.");
00334                 goto ExitWithError;
00335         }
00336 
00337         if (Height != SHeight)
00338         {
00339                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_Create3DTexture: Not a power of 2.");
00340                 goto ExitWithError;
00341         }
00342         
00343         if (!GlideFormatFromGenesisFormat(PixelFormat->PixelFormat, &Format))
00344         {
00345                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_RegisterMiscTexture: GlideFormatFromGenesisFormat failed...");
00346                 goto ExitWithError;
00347         }
00348 
00349         if (!GTHandle_SetupInfo(&Info, Width, Height, NumMipLevels, Format, &THandle->LogSize))
00350         {
00351                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_RegisterMiscTexture: SetupInfo failed...");
00352                 goto ExitWithError;
00353         }
00354 
00355         THandle->Log = (uint8)GetLog(THandle->LogSize);
00356 
00357         // Get the size of the data so we can create a block of memory for it
00358         DataSize = grTexTextureMemRequired(GR_MIPMAPLEVELMASK_BOTH, &Info);
00359 
00360         // Allocate the data
00361         Data = (uint8*)malloc(sizeof(uint8)*DataSize);
00362 
00363         if (!Data)
00364         {
00365                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_RegisterMiscTexture: out of memory for data.");
00366                 goto ExitWithError;
00367         }
00368 
00369 #if 0
00370         memset(Data, 0xff, DataSize);           // For debugging
00371 #endif
00372 
00373         // Save the data pointer...
00374         Info.data = (void*)Data;
00375         
00376         // Save the data
00377         THandle->Data = Data;
00378         THandle->Active = GE_TRUE;
00379         THandle->NumMipLevels = (uint8)NumMipLevels;
00380         //THandle->Info = Info;
00381 
00382         // Save off some other goot info
00383         THandle->Width = Width;                                 // Original Width
00384         THandle->Height = Height;
00385         
00386         THandle->OneOverLogSize_255 = 255.0f / (geFloat)THandle->LogSize;
00387 
00388         THandle->CacheType = GCache_TypeCreate(TextureCache, THandle->LogSize, THandle->LogSize, NumMipLevels, &Info);
00389 
00390         THandle->PixelFormat = *PixelFormat;
00391 
00392         TexturesChanged = GE_TRUE;
00393 
00394         return THandle;
00395                 
00396         ExitWithError:
00397         {
00398                 if (Data)
00399                         free(Data);
00400 
00401                 return NULL;
00402         }
00403 }
00404 
00405 //**********************************************************************************
00406 //      Loads a lightmap texture
00407 //**********************************************************************************
00408 geRDriver_THandle *CreateLightmapTexture(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00409 {       
00410         geRDriver_THandle       *THandle;
00411         geFloat                         Size;
00412 
00413         THandle = GTHandle_FindTextureHandle();
00414 
00415         if (!THandle)
00416         {
00417                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreateLightmapTexture:  Max texture handles.");
00418                 return NULL;
00419         }
00420 
00421         if ( ! (PixelFormat->Flags&RDRIVER_PF_LIGHTMAP) )       // This should be the only bit set (except for Can_Do_ColorKey)
00422         {       
00423                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreateLightmapTexture:  Invalid pixel format.");
00424                 return NULL;
00425         }
00426         
00427         if (PixelFormat->PixelFormat != GE_PIXELFORMAT_16BIT_565_RGB)
00428         {
00429                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreateLightmapTexture:  PixelFormat != GE_PIXELFORMAT_16BIT_565_RGB.");
00430                 return NULL;
00431         }
00432 
00433         THandle->Width = Width;
00434         THandle->Height = Height;
00435 
00436         {
00437                 GrTexInfo               Info;
00438 
00439                 if (!GTHandle_SetupInfo(&Info, Width, Height, 1, GR_TEXFMT_RGB_565, &THandle->LogSize))
00440                         return GE_FALSE;
00441                 THandle->Log = (uint8)GetLog(THandle->LogSize);
00442 
00443                 THandle->CacheType = GCache_TypeCreate(LMapCache, THandle->LogSize, THandle->LogSize, NumMipLevels, &Info);
00444         }
00445 
00446         Size = (geFloat)(THandle->LogSize<<4);
00447 
00448         THandle->OneOverLogSize_255 = 255.0f / Size;
00449 
00450         TexturesChanged = GE_TRUE;
00451 
00452         return THandle;
00453 }
00454 
00455 //==================================================================================
00456 //      Create2DTexture
00457 //==================================================================================
00458 geRDriver_THandle *Create2DTexture(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00459 {
00460         geRDriver_THandle       *THandle;
00461         int32                           Size;
00462 
00463         THandle = GTHandle_FindTextureHandle();
00464 
00465         if (!THandle)
00466         {
00467                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreateTexture: No more handles left.");
00468                 return NULL;
00469         }
00470 
00471         THandle->Width = Width;
00472         THandle->Height = Height;
00473         THandle->LogSize = Width;
00474         THandle->NumMipLevels = (uint8)NumMipLevels;
00475 
00476         Size = Width*Height;
00477 
00478         THandle->Data = (uint16*)malloc(sizeof(uint16)*Size);
00479         THandle->PixelFormat = *PixelFormat;
00480 
00481         return THandle;
00482 }
00483 
00484 //========================================================================================
00485 //      GTHandle_CreatePalTexture
00486 //========================================================================================
00487 geRDriver_THandle *CreatePalTexture(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00488 {
00489         int32                           DataSize;
00490         uint32                          *Data;
00491         geRDriver_THandle       *THandle;
00492         GrTextureFormat_t       Format;
00493 
00494         assert(PixelFormat);
00495         
00496         Data = NULL;
00497 
00498         THandle = GTHandle_FindTextureHandle();
00499 
00500         if (!THandle)
00501         {
00502                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreatePalTexture: No more handles left.");
00503                 goto ExitWithError;
00504         }
00505 
00506         assert(Width == 256);
00507         assert(Height == 1);
00508         assert(NumMipLevels == 1);
00509 
00510         switch (PixelFormat->PixelFormat)
00511         {
00512                 case THANDLE_PALETTE_FORMAT:
00513                 {
00514                         Format = GR_TEXFMT_P_8;
00515                         break;
00516                 }
00517                 
00518                 default:
00519                 {
00520                         SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreatePalTexture: Invalid pixel format.");
00521                         goto ExitWithError;
00522                 }
00523         }
00524 
00525         // Allocate the data
00526         DataSize = sizeof(uint32)*256;
00527 
00528         Data = (uint32*)malloc(DataSize);
00529 
00530         if (!Data)
00531         {
00532                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_RegisterMiscTexture: out of memory for data.");
00533                 goto ExitWithError;
00534         }
00535 
00536 #if 0
00537         memset(Data, 0xff, DataSize);           // For debugging
00538 #endif
00539 
00540         // Save the data
00541         THandle->Data = Data;
00542         THandle->Active = GE_TRUE;
00543         THandle->NumMipLevels = (uint8)NumMipLevels;
00544 
00545         THandle->Width = Width;                                 // Original Width
00546         THandle->Height = Height;
00547         THandle->LogSize = Width;
00548         THandle->PixelFormat = *PixelFormat;
00549 
00550         return THandle;
00551                 
00552         ExitWithError:
00553         {
00554                 if (Data)
00555                         free(Data);
00556 
00557                 return NULL;
00558         }
00559 }
00560 
00561 //==================================================================================
00562 //      GTHandle_Create
00563 //==================================================================================
00564 geRDriver_THandle *DRIVERCC GTHandle_Create(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00565 {
00566         if (PixelFormat->Flags & RDRIVER_PF_2D)
00567                 return Create2DTexture(Width, Height, NumMipLevels, PixelFormat);
00568         else if (PixelFormat->Flags & RDRIVER_PF_3D)
00569                 return Create3DTexture(Width, Height, NumMipLevels, PixelFormat);
00570         else if (PixelFormat->Flags & RDRIVER_PF_LIGHTMAP)
00571                 return CreateLightmapTexture(Width, Height, NumMipLevels, PixelFormat);
00572         else if (PixelFormat->Flags & RDRIVER_PF_PALETTE)
00573                 return CreatePalTexture(Width, Height, NumMipLevels, PixelFormat);
00574         else
00575         {
00576                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_CreateTexture: Invalid pixelformat.");
00577                 return GE_FALSE;
00578         }
00579 }
00580 
00581 //==================================================================================
00582 //      GTHandle_Destroy
00583 //==================================================================================
00584 geBoolean DRIVERCC GTHandle_Destroy(geRDriver_THandle *THandle)
00585 {
00586         GTHandle_FreeTextureHandle(THandle);
00587 
00588         return GE_TRUE;
00589 }
00590 
00591 //==================================================================================
00592 //      GTHandle_Lock
00593 //==================================================================================
00594 geBoolean DRIVERCC GTHandle_Lock(geRDriver_THandle *THandle, int32 MipLevel, void **Data)
00595 {
00596         int32           i, Size;
00597         uint16          *pData;
00598         uint8           *pData8;
00599 
00600         assert(MipLevel <= THandle->NumMipLevels);
00601 
00602         if (THandle->PixelFormat.Flags & RDRIVER_PF_LIGHTMAP)
00603         {
00604                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_LockTextureHandle:  Attempt to lock a lightmap texture.");
00605                 return GE_FALSE;
00606         }
00607 
00608         THandle->Flags |= THANDLE_LOCKED;               // This texture is now locked
00609 
00610         Size = THandle->LogSize*THandle->LogSize;
00611 
00612         if (THandle->PixelFormat.PixelFormat == GE_PIXELFORMAT_8BIT)
00613         {
00614                 pData8 = (uint8*)THandle->Data;
00615 
00616                 for (i=0; i<MipLevel; i++)
00617                 {
00618                         pData8 += Size;
00619                         Size >>=2;
00620                 }
00621                 
00622                 *Data = pData8;
00623         }
00624         else
00625         {
00626                 pData = (uint16*)THandle->Data;
00627 
00628                 for (i=0; i<MipLevel; i++)
00629                 {
00630                         pData += Size;
00631                         Size >>=2;
00632                 }
00633                 
00634                 *Data = pData;
00635         }
00636 
00637         return GE_TRUE;
00638 }
00639 
00640 //==================================================================================
00641 //      GTHandle_UnLock
00642 //==================================================================================
00643 geBoolean DRIVERCC GTHandle_UnLock(geRDriver_THandle *THandle, int32 MipLevel)
00644 {
00645         #pragma message ("FIXME:  Flags needs to be per-mip!!!")
00646         
00647         //if (!(THandle->Flags & THANDLE_LOCKED))
00648         //      return GE_FALSE;
00649 
00650         THandle->Flags &= ~THANDLE_LOCKED;              // This handle is now unlocked
00651         THandle->Flags |= THANDLE_UPDATE;               // Mark it for updating...
00652 
00653         return GE_TRUE;
00654 }
00655 
00656 //==================================================================================
00657 //      GThandle_SetPal
00658 //==================================================================================
00659 geBoolean DRIVERCC GThandle_SetPal(geRDriver_THandle *THandle, geRDriver_THandle *PalHandle)
00660 {
00661         assert(PalHandle->PixelFormat.PixelFormat == THANDLE_PALETTE_FORMAT);
00662         assert(PalHandle->PixelFormat.Flags & RDRIVER_PF_PALETTE);
00663         assert(PalHandle->Width = 256);
00664         assert(PalHandle->LogSize = 256);
00665         assert(PalHandle->Height = 1);
00666 
00667         THandle->PalHandle = PalHandle;
00668 
00669         return GE_TRUE;
00670 }
00671 
00672 //==================================================================================
00673 //      GThandle_GetPal
00674 //==================================================================================
00675 geRDriver_THandle *DRIVERCC GThandle_GetPal(geRDriver_THandle *THandle)
00676 {
00677         return THandle->PalHandle;
00678 }
00679 
00680 //==================================================================================
00681 //      GTHandle_GetInfo
00682 //==================================================================================
00683 geBoolean DRIVERCC GTHandle_GetInfo(geRDriver_THandle *THandle, int32 MipLevel, geRDriver_THandleInfo *Info)
00684 {
00685         Info->Width = THandle->Width>>MipLevel;
00686         Info->Height = THandle->Height>>MipLevel;
00687         Info->Stride = THandle->LogSize>>MipLevel;
00688         Info->PixelFormat = THandle->PixelFormat;
00689 
00690         if (THandle->PixelFormat.Flags & RDRIVER_PF_CAN_DO_COLORKEY)
00691         {
00692                 // Color keys are allways on for surfaces that support it for now...
00693                 Info->Flags = RDRIVER_THANDLE_HAS_COLORKEY;             // Color keys are allways on for surfaces that support it for now...
00694 
00695                 if (THandle->PixelFormat.Flags & RDRIVER_PF_PALETTE)
00696                         Info->ColorKey = (1<<16);
00697                 else
00698                         Info->ColorKey = 1;
00699         }
00700         else
00701         {
00702                 Info->Flags = 0;                        
00703                 Info->ColorKey = 0;
00704         }
00705         
00706         return GE_TRUE;
00707 }
00708 
00709 //==================================================================================
00710 //      GTHandle_CheckTextures
00711 //==================================================================================
00712 geBoolean GTHandle_CheckTextures(void)
00713 {
00714         int32                           i;
00715         geRDriver_THandle       *pTHandle;
00716 
00717         if (!TexturesChanged)
00718                 return GE_TRUE;
00719 
00720         if (!GCache_AdjustSlots(TextureCache))
00721         {
00722                 SetLastDrvError(DRV_ERROR_INVALID_REGISTER_MODE, "GTHandle_CheckTextures: GCache_AdjustSlots for textures.");
00723                 return GE_FALSE;
00724         }
00725 
00726         if (!GCache_AdjustSlots(LMapCache))
00727         {
00728                 SetLastDrvError(DRV_ERROR_INVALID_REGISTER_MODE, "GTHandle_CheckTextures: GCache_AdjustSlots for lightmaps.");
00729                 return GE_FALSE;
00730         }
00731 
00732         // Make sure no THandles reference any slots, because they mave have been moved around, or gotten destroyed...
00733         //  (Evict all textures in the cache)
00734         pTHandle = TextureHandles;
00735 
00736         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pTHandle++)
00737                 pTHandle->Slot = NULL;
00738 
00739         // Reset the tedxture handle changed boolean
00740         TexturesChanged = GE_FALSE;
00741 
00742         return GE_TRUE;
00743 }
00744 
00745 //**********************************************************************************
00746 //      Return the LOD of a mip-map
00747 //**********************************************************************************
00748 geBoolean GetLod(S32 Width, GrLOD_t *Lod)
00749 {
00750         if (Width == 1)
00751                 *Lod = GR_LOD_1;
00752         else if (Width == 2)
00753                 *Lod = GR_LOD_2;
00754         else if (Width == 4)
00755                 *Lod = GR_LOD_4;
00756         else if (Width == 8)
00757                 *Lod = GR_LOD_8;
00758         else if (Width == 16)
00759                 *Lod = GR_LOD_16;
00760         else if (Width == 32)
00761                 *Lod = GR_LOD_32;
00762         else if (Width == 64)
00763                 *Lod = GR_LOD_64;
00764         else if (Width == 128)
00765                 *Lod = GR_LOD_128;
00766         else if (Width == 256)
00767                 *Lod = GR_LOD_256;
00768         else
00769         {
00770                 SetLastDrvError(DRV_ERROR_GENERIC, "GLIDE_GetLod: Invalid texture width.");
00771                 return GE_FALSE;
00772         }
00773         
00774         return GE_TRUE;
00775 }
00776 
00777 //**********************************************************************************
00778 //      Returns the aspect ratio of a texture mip-map
00779 //**********************************************************************************
00780 geBoolean GetAspectRatio(int32 Width, int32 Height, GrAspectRatio_t *Aspect)
00781 {
00782         int Aspect2;
00783 
00784         Aspect2 = ( 8 * Width ) / Height;
00785 
00786         switch( Aspect2)
00787         {
00788                 case 64:
00789                         *Aspect = GR_ASPECT_8x1;
00790                         break;
00791                 case 32:
00792                         *Aspect = GR_ASPECT_4x1;
00793                         break;
00794                 case 16:
00795                         *Aspect = GR_ASPECT_2x1;
00796                         break;
00797                 case 8:
00798                         *Aspect = GR_ASPECT_1x1;
00799                         break;
00800                 case 4:
00801                         *Aspect = GR_ASPECT_1x2;
00802                         break;
00803                 case 2:
00804                         *Aspect = GR_ASPECT_1x4;
00805                         break;
00806                 case 1:
00807                         *Aspect = GR_ASPECT_1x8;
00808                         break;
00809 
00810                 default:
00811                         return GE_FALSE;
00812     }
00813         
00814         return GE_TRUE;
00815 }
00816 
00817 //**********************************************************************************
00818 // Returns the log of a number (number must be a power of 2)
00819 //**********************************************************************************
00820 uint32 GetLog(uint32 P2)
00821 {
00822         U32             p = 0;
00823         S32             i = 0;
00824         
00825         for (i = P2; i > 0; i>>=1)
00826                 p++;
00827 
00828         return (p-1);
00829 }
00830 
00831 //**********************************************************************************
00832 //      Snaps a number to a power of 2
00833 //**********************************************************************************
00834 int32 SnapToPower2(int32 Width)
00835 {
00836 int RetWidth;
00837         // CB : I couldn't stand that big branch of if's !
00838         for ( RetWidth = 1; RetWidth < Width; RetWidth <<= 1 ) ;
00839         assert( RetWidth <= 256 );
00840 return RetWidth;
00841 }
00842 
00843 

Generated on Tue Sep 30 12:35:55 2003 for GTestAndEngine by doxygen 1.3.2