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

THandle.cpp

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  THandle.cpp                                                                         */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: THandle manager for D3DDrv                                             */
00006 /*                                                                                      */
00007 /*   02/25/2001 Wendell Buckner
00008 /*    This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00009 /*    something there when  the code does assert checks.        
00010 /*   05/28/2000 Wendell Buckner
00011 /*    Running out of texture handles...
00012 /*  The contents of this file are subject to the Genesis3D Public License               */
00013 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00014 /*  compliance with the License. You may obtain a copy of the License at                */
00015 /*  http://www.genesis3d.com                                                            */
00016 /*                                                                                      */
00017 /*  Software distributed under the License is distributed on an "AS IS"                 */
00018 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00019 /*  the License for the specific language governing rights and limitations              */
00020 /*  under the License.                                                                  */
00021 /*                                                                                      */
00022 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00023 /*Genesis3D Version 1.1 released November 15, 1999                            */
00024 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00025 /*                                                                                      */
00026 /****************************************************************************************/
00027 #include <Windows.h>
00028 #include <DDraw.h>
00029 #include <D3D.h>
00030 
00031 #include "THandle.h"
00032 #include "BaseType.h"
00033 
00034 /* 07/16/2000 Wendell Buckner
00035     Convert to Directx7...    
00036 #include "D3DDrv.h"           */
00037 #include "D3DDrv7x.h"
00038 
00039 #include "DCommon.h"
00040 #include "D3DCache.h"
00041 #include "D3D_Main.h"
00042 #include "PCache.h"
00043 #include "D3d_FX.h"
00044 
00045 #include "TPage.h"
00046 
00047 //#define D3D_MANAGE_TEXTURES
00048 
00049 //#define USE_ONE_CACHE
00050 
00051 //05/28/2000 Wendell Buckner
00052 //Running out of texture handles...
00053 //#define MAX_TEXTURE_HANDLES                                   15000
00054   #define MAX_TEXTURE_HANDLES                                   32000
00055 
00056 #define TEXTURE_CACHE_PERCENT                           0.75f
00057 #define LMAP_CACHE_PERCENT                                      0.25f
00058 
00059 #define TSTAGE_0                        0
00060 #define TSTAGE_1                        1
00061 
00062 //============================================================================================
00063 //============================================================================================
00064 
00065 geRDriver_THandle       TextureHandles[MAX_TEXTURE_HANDLES];
00066 
00067 DDMemMgr                        *MemMgr;
00068 DDMemMgr_Partition      *Partition[2];
00069 
00070 D3DCache                        *TextureCache;
00071 D3DCache                        *LMapCache;
00072 
00073 TPage_Mgr                       *TPageMgr;
00074 
00075 DDSURFACEDESC2          CurrentSurfDesc;
00076 
00077 THandle_MipData         SystemToVideo[MAX_LMAP_LOG_SIZE];       
00078 
00079 geBoolean                       CacheNeedsUpdate;
00080 /*
00081 #ifdef D3D_MANAGE_TEXTURES
00082         #define                 NUM_LMAP_VIDEO_SURFACES         10;
00083         THandle_MipData         SystemToVideo[MAX_LMAP_LOG_SIZE];       
00084 #endif
00085 */
00086 
00087 //============================================================================================
00088 //============================================================================================
00089 
00090 //============================================================================================
00091 //      FreeAllcaches
00092 //============================================================================================
00093 void FreeAllCaches(void)
00094 {
00095         if (LMapCache)
00096                 D3DCache_Destroy(LMapCache);
00097 
00098         if (TextureCache)
00099                 D3DCache_Destroy(TextureCache);
00100 
00101         LMapCache = NULL;
00102         TextureCache = NULL;
00103 
00104         if (Partition[1])
00105                 DDMemMgr_PartitionDestroy(Partition[1]);
00106         if (Partition[0])
00107                 DDMemMgr_PartitionDestroy(Partition[0]);
00108         if (MemMgr)
00109                 DDMemMgr_Destroy(MemMgr);
00110 
00111         
00112 #ifdef USE_TPAGES
00113         if (TPageMgr)
00114         {
00115                 TPage_MgrDestroy(&TPageMgr);
00116                 TPageMgr = NULL;
00117         }
00118 #endif
00119 
00120         Partition[1] = NULL;
00121         Partition[0] = NULL;
00122         MemMgr = NULL;
00123 }
00124 
00125 //============================================================================================
00126 //      FindTextureHandle
00127 //============================================================================================
00128 geRDriver_THandle *FindTextureHandle(void)
00129 {
00130         int32                           i;
00131         geRDriver_THandle       *pHandle;
00132 
00133         pHandle = TextureHandles;
00134 
00135         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pHandle++)
00136         {
00137                 if (!pHandle->Active)
00138                 {
00139                         memset(pHandle, 0, sizeof(geRDriver_THandle));
00140                         pHandle->Active = 1;
00141                         return pHandle;
00142                 }
00143         }
00144 
00145         SetLastDrvError(DRV_ERROR_GENERIC, "D3D_FindTextureHandle:  No more handles left.\n");
00146 
00147         return NULL;
00148 }
00149 
00150 //============================================================================================
00151 //      FreeAllTextureHandles
00152 //============================================================================================
00153 geBoolean FreeAllTextureHandles(void)
00154 {
00155         int32                           i;
00156         geRDriver_THandle       *pHandle;
00157 
00158         pHandle = TextureHandles;
00159 
00160         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pHandle++)
00161         {
00162                 if (!pHandle->Active)
00163                         continue;
00164 
00165                 if (!THandle_Destroy(pHandle))
00166                         return GE_FALSE;
00167         }
00168 
00169         return GE_TRUE;
00170 }
00171 
00172 //============================================================================================
00173 //============================================================================================
00174 geBoolean THandle_Startup(void)
00175 {
00176         // Create the main memory manager
00177         MemMgr = DDMemMgr_Create(AppInfo.VidMemFree);
00178 
00179         if (!MemMgr)
00180                 goto ExitWithError;
00181 
00182         // Create partition 0
00183         Partition[0] = DDMemMgr_PartitionCreate(MemMgr, (uint32)((float)DDMemMgr_GetFreeMem(MemMgr)*TEXTURE_CACHE_PERCENT));
00184 
00185         if (!Partition[0])
00186                 goto ExitWithError;
00187 
00188         // Create partition 1
00189         Partition[1] = DDMemMgr_PartitionCreate(MemMgr, DDMemMgr_GetFreeMem(MemMgr));
00190 
00191         if (!Partition[1])
00192                 goto ExitWithError;
00193 
00194         // Create the texture cache from partition 0
00195         TextureCache = D3DCache_Create("Main Texture Cache", AppInfo.lpDD, Partition[0], AppInfo.CanDoMultiTexture);
00196 
00197         if (!TextureCache)
00198                 goto ExitWithError;
00199 
00200 #ifndef USE_ONE_CACHE
00201         // Create the lmap cache from partition 1
00202         LMapCache = D3DCache_Create("Lightmap Cache", AppInfo.lpDD, Partition[1], AppInfo.CanDoMultiTexture);
00203 
00204         if (!LMapCache)
00205                 goto ExitWithError;
00206 #endif
00207 
00208         // Create all the system to video surfaces (for lmaps)
00209         if (!CreateSystemToVideoSurfaces())
00210                 goto ExitWithError;
00211 
00212         #ifdef USE_TPAGES
00213                 TPageMgr = TPage_MgrCreate(AppInfo.lpDD, &AppInfo.ddTexFormat, 512);
00214                 if (!TPageMgr)
00215                         goto ExitWithError;
00216         #endif
00217 
00218         return GE_TRUE;
00219 
00220         ExitWithError:
00221         {
00222                 THandle_Shutdown();
00223                 return GE_FALSE;
00224         }
00225 }
00226 
00227 //============================================================================================
00228 //============================================================================================
00229 void THandle_Shutdown(void)
00230 {
00231         FreeAllTextureHandles();
00232         FreeAllCaches();
00233         DestroySystemToVideoSurfaces();
00234 
00235         CacheNeedsUpdate = GE_FALSE;
00236 }
00237 
00238 //============================================================================================
00239 //      Create3DTHandle
00240 //============================================================================================
00241 geRDriver_THandle *Create3DTHandle(geRDriver_THandle *THandle, int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00242 {
00243         int32                           Size, i;
00244 
00245         assert(NumMipLevels < THANDLE_MAX_MIP_LEVELS);
00246         assert(NumMipLevels <= 4);
00247 
00248         // Store width/height info
00249         THandle->Width = Width;
00250         THandle->Height = Height;
00251         THandle->NumMipLevels = (uint8)NumMipLevels;
00252         THandle->Log = (uint8)GetLog(Width, Height);
00253         THandle->Stride = (1<<THandle->Log);
00254 
00255         // Create the surfaces to hold all the mips
00256         THandle->MipData = (THandle_MipData*)malloc(sizeof(THandle_MipData)*NumMipLevels);
00257         memset(THandle->MipData, 0, sizeof(THandle_MipData)*NumMipLevels);
00258         
00259         if (!THandle->MipData)
00260         {
00261                 THandle_Destroy(THandle);
00262                 return NULL;
00263         }
00264 
00265         Size = 1<<THandle->Log;
00266 
00267         // Create all the surfaces for each mip level
00268         for (i=0; i< NumMipLevels; i++)
00269         {
00270                 int32   Stage;
00271 
00272                 if (!THandle_CreateSurfaces(&THandle->MipData[i], Size, Size, &CurrentSurfDesc, GE_FALSE, 0))
00273                 {
00274                         THandle_Destroy(THandle);
00275                         return NULL;
00276                 }
00277 
00278                 // get a cache type for this surface since it is a 3d surface, and will need to be cached on the video card
00279                 //THandle->MipData[i].CacheType = D3DCache_TypeCreate(TextureCache, Size, Size, NumMipLevels, &CurrentSurfDesc);
00280                 // We can use 1 miplevel for the type, since we are createing types for each miplevel...
00281                 if (AppInfo.CanDoMultiTexture)
00282                         Stage = TSTAGE_0;
00283                 else
00284                         Stage = 0;
00285 
00286                 THandle->MipData[i].CacheType = D3DCache_TypeCreate(TextureCache, Size, Size, 1, Stage, &CurrentSurfDesc);
00287 
00288                 if (!THandle->MipData[i].CacheType)
00289                 {
00290                         THandle_Destroy(THandle);
00291                         return NULL;
00292                 }
00293 
00294                 Size>>=1;
00295         }
00296 
00297         return THandle;
00298 }
00299 
00300 //============================================================================================
00301 //      CreateLightmapTHandle
00302 //============================================================================================
00303 geRDriver_THandle *CreateLightmapTHandle(geRDriver_THandle *THandle, int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00304 {
00305         int32                           Size, Stage;
00306 
00307         assert(NumMipLevels < THANDLE_MAX_MIP_LEVELS);
00308 
00309         assert(NumMipLevels == 1);
00310         
00311         // Save some info about the lightmap
00312         THandle->Width = Width;
00313         THandle->Height = Height;
00314         THandle->NumMipLevels = (uint8)NumMipLevels;
00315         THandle->Log = (uint8)GetLog(Width, Height);
00316         THandle->Stride = 1<<THandle->Log;
00317 
00318         assert(THandle->Log < MAX_LMAP_LOG_SIZE);
00319 
00320         Size = 1<<THandle->Log;
00321 
00322         THandle->MipData = (THandle_MipData*)malloc(sizeof(THandle_MipData)*NumMipLevels);
00323         memset(THandle->MipData, 0, sizeof(THandle_MipData)*NumMipLevels);
00324 
00325         THandle->MipData[0].Flags = THANDLE_UPDATE;
00326 
00327 #ifdef D3D_MANAGE_TEXTURES
00328         #ifndef USE_TPAGES
00329         {
00330                 int32           Stage;
00331 
00332                 if (AppInfo.CanDoMultiTexture)
00333                         Stage = STAGE_1;
00334                 else
00335                         Stage = 0;
00336 
00337                 if (!THandle_CreateSurfaces(&THandle->MipData[0], Size, Size, &CurrentSurfDesc, GE_FALSE, Stage))
00338                 {
00339                         THandle_Destroy(THandle);
00340                         return NULL;
00341                 }
00342 
00343 /*   02/25/2001 Wendell Buckner
00344 /*    This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00345 /*    something there when  the code does assert checks.        
00346                 D3DSetTexture(0, THandle->MipData[0].Texture); */
00347                 D3DSetTexture(0, THandle->MipData[0].Surface);          
00348 
00349         }
00350         #endif
00351 #endif
00352 
00353         if (AppInfo.CanDoMultiTexture)
00354                 Stage = TSTAGE_1;
00355         else
00356                 Stage = 0;
00357 
00358 #ifdef USE_ONE_CACHE
00359         THandle->MipData[0].CacheType = D3DCache_TypeCreate(TextureCache, Size, Size, NumMipLevels, Stage, &CurrentSurfDesc);
00360 #else
00361         THandle->MipData[0].CacheType = D3DCache_TypeCreate(LMapCache, Size, Size, NumMipLevels, Stage, &CurrentSurfDesc);
00362 #endif
00363 
00364         if (!THandle->MipData[0].CacheType)
00365         {
00366                 THandle_Destroy(THandle);
00367                 return NULL;
00368         }
00369 
00370         return THandle;
00371 }
00372 
00373 //============================================================================================
00374 //      Create2DTHandle
00375 //============================================================================================
00376 geRDriver_THandle *Create2DTHandle(geRDriver_THandle *THandle, int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00377 {
00378         assert(NumMipLevels < THANDLE_MAX_MIP_LEVELS);
00379         assert(NumMipLevels == 1);
00380 
00381         // Save some info about the lightmap
00382         THandle->Width = Width;
00383         THandle->Height = Height;
00384         THandle->NumMipLevels = (uint8)NumMipLevels;
00385         THandle->Log = (uint8)GetLog(Width, Height);
00386         THandle->Stride = Width;
00387 
00388         // Create the surfaces to hold all the mips
00389         THandle->MipData = (THandle_MipData*)malloc(sizeof(THandle_MipData)*NumMipLevels);
00390         memset(THandle->MipData, 0, sizeof(THandle_MipData)*NumMipLevels);
00391         
00392         if (!THandle->MipData)
00393         {
00394                 THandle_Destroy(THandle);
00395                 return NULL;
00396         }
00397         
00398         if (!THandle_CreateSurfaces(&THandle->MipData[0], Width, Height, &CurrentSurfDesc, GE_TRUE, 0))
00399         {
00400                 THandle_Destroy(THandle);
00401                 return NULL;
00402         }
00403 
00404         return THandle;
00405 }
00406 
00407 //============================================================================================
00408 //      SetupCurrent3dDesc
00409 //============================================================================================
00410 geBoolean SetupCurrent3dDesc(gePixelFormat PixelFormat)
00411 {
00412         switch (PixelFormat)
00413         {
00414                 case GE_PIXELFORMAT_16BIT_555_RGB:
00415                 case GE_PIXELFORMAT_16BIT_565_RGB:
00416                 {
00417                         memcpy(&CurrentSurfDesc, &AppInfo.ddTexFormat, sizeof(DDSURFACEDESC2));
00418                         break;
00419                 }
00420                 case GE_PIXELFORMAT_16BIT_4444_ARGB:
00421                 {
00422                         memcpy(&CurrentSurfDesc, &AppInfo.ddFourBitAlphaSurfFormat, sizeof(DDSURFACEDESC2));
00423                         break;
00424                 }
00425                 case GE_PIXELFORMAT_16BIT_1555_ARGB:
00426                 {
00427                         memcpy(&CurrentSurfDesc, &AppInfo.ddOneBitAlphaSurfFormat, sizeof(DDSURFACEDESC2));
00428                         break;
00429                 }
00430 
00431                 default:
00432                 {
00433                         SetLastDrvError(DRV_ERROR_GENERIC, "SetupCurrent3dDesc:  Invalid pixel format.\n");
00434                         return GE_FALSE;
00435                 }
00436         }
00437 
00438         return GE_TRUE;
00439 }
00440 //============================================================================================
00441 //      THandle_Create
00442 //============================================================================================
00443 geRDriver_THandle *DRIVERCC THandle_Create(int32 Width, int32 Height, int32 NumMipLevels, const geRDriver_PixelFormat *PixelFormat)
00444 {
00445         geRDriver_THandle       *THandle;
00446 
00447         THandle = FindTextureHandle();
00448 
00449         if (!THandle)
00450         {
00451                 SetLastDrvError(DRV_ERROR_GENERIC, "D3DDRV:THandle_Create:  Out of texture handles.\n");
00452                 return NULL;
00453         }
00454 
00455         THandle->PixelFormat = *PixelFormat;
00456 
00457         if (PixelFormat->Flags & RDRIVER_PF_3D)
00458         {
00459                 // Get the pixel format desc for this thandle
00460                 if (!SetupCurrent3dDesc(PixelFormat->PixelFormat))
00461                         return NULL;
00462 
00463                 if (!Create3DTHandle(THandle, Width, Height, NumMipLevels, PixelFormat))
00464                         return NULL;
00465 
00466                 CacheNeedsUpdate = GE_TRUE;
00467         }
00468         else if (PixelFormat->Flags & RDRIVER_PF_LIGHTMAP)
00469         {
00470                 // Get the pixel format desc for this thandle
00471                 if (!SetupCurrent3dDesc(PixelFormat->PixelFormat))
00472                         return NULL;
00473 
00474                 if (!CreateLightmapTHandle(THandle, Width, Height, NumMipLevels, PixelFormat))
00475                         return NULL;
00476 
00477                 CacheNeedsUpdate = GE_TRUE;
00478         }
00479         else if (PixelFormat->Flags & RDRIVER_PF_2D)
00480         {
00481                 // 2d surfaces are always this format for now
00482                 memcpy(&CurrentSurfDesc, &AppInfo.ddSurfFormat, sizeof(DDSURFACEDESC2));
00483 
00484                 if (!Create2DTHandle(THandle, Width, Height, NumMipLevels, PixelFormat))
00485                         return NULL;
00486         }
00487 
00488         return THandle;
00489 }
00490 
00491 //============================================================================================
00492 //      THandle_Destroy
00493 //============================================================================================
00494 geBoolean DRIVERCC THandle_Destroy(geRDriver_THandle *THandle)
00495 {
00496         int32           i;
00497 
00498         assert(THandle);
00499         assert(THandle->Active);
00500 
00501         for (i=0; i< THandle->NumMipLevels; i++)
00502         {
00503                 assert(THandle->MipData);
00504 
00505                 if (THandle->MipData[i].CacheType)
00506                 {
00507                         D3DCache_TypeDestroy(THandle->MipData[i].CacheType);
00508                         CacheNeedsUpdate = GE_TRUE;
00509                         THandle->MipData[i].CacheType = NULL;
00510                 }
00511                 
00512                 if (THandle->MipData[i].Surface)
00513                 {
00514                         assert(THandle->MipData[i].Texture);
00515 
00516                         THandle_DestroySurfaces(&THandle->MipData[i]);
00517                         THandle->MipData[i].Surface = NULL;
00518                         THandle->MipData[i].Texture = NULL;
00519                 }
00520         }
00521 
00522         if (THandle->MipData)
00523                 free(THandle->MipData);
00524 
00525         memset(THandle, 0, sizeof(geRDriver_THandle));
00526 
00527         return GE_TRUE;
00528 }
00529 
00530 //=====================================================================================
00531 //=====================================================================================
00532 geBoolean DRIVERCC THandle_Lock(geRDriver_THandle *THandle, int32 MipLevel, void **Bits)
00533 {
00534     DDSURFACEDESC2              SurfDesc;
00535     HRESULT                             Result;
00536 
00537     assert(!(THandle->MipData[MipLevel].Flags & THANDLE_LOCKED));
00538 
00539         // Lock the surface so it can be filled with the data
00540     memset(&SurfDesc, 0, sizeof(DDSURFACEDESC2));
00541     SurfDesc.dwSize = sizeof(DDSURFACEDESC2);
00542 
00543 /* 03/10/2002 Wendell Buckner
00544     Procedural Textures
00545     if you must lock a texture specify the flags WRITEONLY and DISCARDCONTENTS when 
00546     Result = THandle->MipData[MipLevel].Surface->Lock(NULL, &SurfDesc , DDLOCK_WAIT, NULL);*/
00547     Result = THandle->MipData[MipLevel].Surface->Lock(NULL, &SurfDesc , DDLOCK_WAIT | DDLOCK_WRITEONLY | DDLOCK_DISCARDCONTENTS, NULL);
00548 
00549     if (Result != DD_OK) 
00550         {
00551         return GE_FALSE;
00552     }
00553 
00554         THandle->MipData[MipLevel].Flags |= THANDLE_LOCKED;
00555 
00556         *Bits = (void*)SurfDesc.lpSurface;
00557 
00558         return GE_TRUE;
00559 }
00560 
00561 //=====================================================================================
00562 //      THandle_UnLock
00563 //=====================================================================================
00564 geBoolean DRIVERCC THandle_UnLock(geRDriver_THandle *THandle, int32 MipLevel)
00565 {
00566     HRESULT                             Result;
00567 
00568     assert(MipLevel <= THandle->NumMipLevels);
00569     assert(THandle->MipData[MipLevel].Flags & THANDLE_LOCKED);
00570 
00571         // Unlock the surface
00572     Result = THandle->MipData[MipLevel].Surface->Unlock(NULL);
00573 
00574     if (Result != DD_OK) 
00575         {
00576         return GE_FALSE;
00577     }
00578 
00579         THandle->MipData[MipLevel].Flags |= THANDLE_UPDATE;
00580         THandle->MipData[MipLevel].Flags &= ~THANDLE_LOCKED;
00581 
00582         return GE_TRUE;
00583 }
00584 
00585 #ifndef NDEBUG
00586 #define DebugIf(a, b) if (a) b
00587 #else
00588 #define DebugIf(a, b)
00589 #endif
00590 
00591 //=====================================================================================
00592 //      THandle_GetInfo
00593 //=====================================================================================
00594 geBoolean DRIVERCC THandle_GetInfo(geRDriver_THandle *THandle, int32 MipLevel, geRDriver_THandleInfo *Info)
00595 {
00596         DebugIf (MipLevel > THandle->Log, return GE_FALSE);
00597 
00598         Info->Width = THandle->Width>>MipLevel;
00599         Info->Height = THandle->Height>>MipLevel;
00600         Info->Stride = THandle->Stride>>MipLevel;
00601 
00602         if (THandle->PixelFormat.Flags & RDRIVER_PF_CAN_DO_COLORKEY)
00603         {
00604                 Info->Flags = RDRIVER_THANDLE_HAS_COLORKEY;
00605                 Info->ColorKey = 1;
00606         }
00607         else
00608         {
00609                 Info->Flags = 0;
00610                 Info->ColorKey = 0;
00611         }
00612 
00613         Info->PixelFormat = THandle->PixelFormat;
00614 
00615         return GE_TRUE;
00616 }
00617 
00618 //=====================================================================================
00619 //      CreateSystemToVideoSurfaces
00620 //      System surfaces to copy from system to video
00621 //=====================================================================================
00622 geBoolean CreateSystemToVideoSurfaces(void)
00623 {
00624         int32                           i;
00625         DDSURFACEDESC2          SurfDesc;
00626 
00627         memcpy(&SurfDesc, &AppInfo.ddTexFormat, sizeof(DDSURFACEDESC2));
00628 
00629         for (i=0; i<MAX_LMAP_LOG_SIZE; i++)
00630         {
00631                 int32           Size;
00632 
00633                 Size = 1<<i;
00634                 
00635                 if (!THandle_CreateSurfaces(&SystemToVideo[i], Size, Size, &SurfDesc, GE_FALSE, 1))
00636                 {
00637                         DestroySystemToVideoSurfaces();
00638                         return GE_FALSE;
00639                 }
00640         }
00641 
00642         return GE_TRUE;
00643 }
00644 
00645 //=====================================================================================
00646 //      DestroySystemToVideoSurfaces
00647 //=====================================================================================
00648 void DestroySystemToVideoSurfaces(void)
00649 {
00650         int32                           i;
00651 
00652         for (i=0; i<MAX_LMAP_LOG_SIZE; i++)
00653                 THandle_DestroySurfaces(&SystemToVideo[i]);
00654 }
00655 
00656 //=====================================================================================
00657 //      THandle_CreateSurfaces
00658 //=====================================================================================
00659 geBoolean THandle_CreateSurfaces(THandle_MipData *Surf, int32 Width, int32 Height, DDSURFACEDESC2 *SurfDesc, geBoolean ColorKey, int32 Stage)
00660 {
00661 /* 02/25/2001 Wendell Buckner
00662    This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00663         LPDIRECTDRAWSURFACE4 Surface;*/
00664         LPDIRECTDRAWSURFACE7 Surface;
00665 
00666         DDSURFACEDESC2          ddsd;
00667         HRESULT                         Hr;
00668                         
00669         memcpy(&ddsd, SurfDesc, sizeof(DDSURFACEDESC2));
00670 
00671         ddsd.dwSize = sizeof(DDSURFACEDESC2);
00672         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_TEXTURESTAGE;
00673 
00674 #ifdef D3D_MANAGE_TEXTURES
00675         ColorKey = GE_TRUE;                     // Force a colorkey on system surfaces since we are letting D3D do our cacheing...
00676 
00677         ddsd.ddsCaps.dwCaps = 0;
00678         ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC;
00679 #else
00680         ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY;
00681         ddsd.ddsCaps.dwCaps2 = 0;//DDSCAPS2_HINTDYNAMIC;
00682 #endif
00683 
00684         ddsd.ddsCaps.dwCaps |= DDSCAPS_TEXTURE;
00685         ddsd.ddsCaps.dwCaps2 |= DDSCAPS2_HINTDYNAMIC;
00686 
00687         ddsd.ddsCaps.dwCaps3 = 0;
00688         ddsd.ddsCaps.dwCaps4 = 0;
00689         ddsd.dwWidth = Width;
00690         ddsd.dwHeight = Height;
00691 
00692         ddsd.dwTextureStage = Stage;
00693                         
00694         Hr = AppInfo.lpDD->CreateSurface(&ddsd, &Surface, NULL);
00695 
00696         if(Hr != DD_OK) 
00697         { 
00698                 return FALSE;
00699         }
00700 
00701         Surf->Surface = Surface;
00702         
00703 /* 02/25/2001 Wendell Buckner
00704    This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00705    something there when  the code does assert checks.
00706         Surf->Texture = NULL; */
00707     Surf->Texture = Surface;
00708 
00709 /* 02/25/2001 Wendell Buckner
00710    This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00711    something there when  the code does assert checks.
00712         Hr = Surface->QueryInterface(IID_IDirect3DTexture2, (void**)&Surf->Texture);  
00713 
00714         if(Hr != DD_OK) 
00715         { 
00716                 Surface->Release();
00717                 return GE_FALSE;
00718         }*/
00719 
00720         if (ColorKey)
00721         {
00722                 DDCOLORKEY                      CKey;
00723                 
00724                 // Create the color key for this surface
00725                 CKey.dwColorSpaceLowValue = 1;
00726                 CKey.dwColorSpaceHighValue = 1;
00727                 
00728                 if (Surf->Surface->SetColorKey(DDCKEY_SRCBLT , &CKey) != DD_OK)
00729                 {
00730                         SetLastDrvError(DRV_ERROR_GENERIC, "THandle_CreateSurfaces: SetColorKey failed for texture.");
00731                         Surf->Surface->Release();
00732                         Surf->Surface = NULL;
00733 
00734 /*   02/25/2001 Wendell Buckner
00735       This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00736                         Surf->Texture->Release();*/
00737                         Surf->Texture = NULL;
00738                         
00739                         return FALSE;
00740                 }
00741         }
00742         return GE_TRUE;         // All good dude
00743 }
00744 
00745 //=====================================================================================
00746 //      DestroySystemSurface
00747 //=====================================================================================
00748 void THandle_DestroySurfaces(THandle_MipData *Surf)
00749 {
00750 /*   02/25/2001 Wendell Buckner
00751       This texture pointer is no longer valid under directx 7.  Set it to TRUE so there is
00752         if (Surf->Texture)
00753                 Surf->Texture->Release();*/
00754 
00755         if (Surf->Surface)
00756                 Surf->Surface->Release();
00757 
00758         memset(Surf, 0, sizeof (THandle_MipData));
00759 }
00760 
00761 //=====================================================================================
00762 //      THandle_CheckCache
00763 //=====================================================================================
00764 geBoolean THandle_CheckCache(void)
00765 {
00766         geRDriver_THandle       *pTHandle;
00767         int32                           i, Stage0, Stage1;
00768         int32                           MaxTable1[9], MaxTable2[9];
00769 
00770         if (!CacheNeedsUpdate)
00771                 return GE_TRUE;
00772 
00773 #ifndef D3D_MANAGE_TEXTURES             // If D3D is managing textures, then we don't ned to do any of this...
00774         D3DMain_Log("THandle_CheckCache:  Resetting texture cache...\n");
00775 
00776 #ifdef USE_ONE_CACHE
00777                 #pragma message ("There numbers ARE NOT DONE.  So if USE_ONE_CACHE is defined, please finish this...")
00778                 // Texture cache & Lightmap cache (we are only using one cache, so the combine into the TextureCache)
00779                 MaxTable1[0] = 256;                     //  1x1
00780                 MaxTable1[1] = 256;                     //  2x2
00781                 MaxTable1[2] = 256;                     //  4x4
00782                 MaxTable1[3] = 512;                     //  8x8
00783                 MaxTable1[4] = 512;                     // 16x16
00784                 MaxTable1[5] = 512;                     // 32x32
00785                 MaxTable1[6] = 512;                     // 64x64
00786                 MaxTable1[7] = 256;                     //128x128
00787                 MaxTable1[8] = 256;                     //256x256
00788 #else
00789         if (AppInfo.DeviceIdentifier.dwVendorId == 4634)                // 3dfx series have a limit on the number of texture handles
00790         {
00791                 D3DMain_Log("   3dfx card detected, using smaller number of handles...\n");
00792 
00793                 // Texture cache
00794                 MaxTable1[0] = 24;                      //  1x1
00795                 MaxTable1[1] = 24;                      //  2x2
00796                 MaxTable1[2] = 24;                      //  4x4
00797                 MaxTable1[3] = 24;                      //  8x8
00798                 MaxTable1[4] = 24;                      // 16x16
00799                 MaxTable1[5] = 128;                     // 32x32
00800                 MaxTable1[6] = 128;                     // 64x64
00801                 MaxTable1[7] = 128;                     //128x128
00802                 MaxTable1[8] = 128;                     //256x256
00803 
00804                 // Lightmap cache
00805                 MaxTable2[0] = 128;                     //  1x1
00806                 MaxTable2[1] = 128;                     //  2x2
00807                 MaxTable2[2] = 256;                     //  4x4
00808                 MaxTable2[3] = 256;                     //  8x8
00809                 MaxTable2[4] = 256;                     // 16x16
00810                 MaxTable2[5] = 128;                     // 32x32
00811                 MaxTable2[6] = 128;                     // 64x64
00812                 MaxTable2[7] = 128;                     //128x128
00813                 MaxTable2[8] = 128;                     //256x256
00814         }
00815         else
00816         {
00817                 D3DMain_Log("   NO 3dfx card detected, using larger number of handles...\n");
00818 
00819                 // Texture cache
00820                 MaxTable1[0] = 32;                      //  1x1
00821                 MaxTable1[1] = 32;                      //  2x2
00822                 MaxTable1[2] = 32;                      //  4x4
00823                 MaxTable1[3] = 32;                      //  8x8
00824                 MaxTable1[4] = 32;                      // 16x16
00825                 MaxTable1[5] = 32;                      // 32x32
00826                 MaxTable1[6] = 128;                     // 64x64
00827                 MaxTable1[7] = 128;                     //128x128
00828                 MaxTable1[8] = 128;                     //256x256
00829 
00830                 MaxTable2[0] = 128;                     //  1x1
00831                 MaxTable2[1] = 128;                     //  2x2
00832                 MaxTable2[2] = 256;                     //  4x4
00833                 MaxTable2[3] = 1024;            //  8x8
00834                 MaxTable2[4] = 1024;            // 16x16
00835                 MaxTable2[5] = 512;                     // 32x32
00836                 MaxTable2[6] = 256;                     // 64x64
00837                 MaxTable2[7] = 256;                     //128x128
00838                 MaxTable2[8] = 256;                     //256x256
00839         }
00840 #endif
00841 
00842         if (AppInfo.CanDoMultiTexture)
00843         {
00844                 Stage0 = TSTAGE_0;
00845                 Stage1 = TSTAGE_1;
00846         }
00847         else
00848         {
00849                 Stage0 = 0;                                                        
00850                 Stage1 = 0;
00851         }
00852 
00853         #ifndef USE_ONE_CACHE
00854                 if (!D3DCache_AdjustSlots(LMapCache, MaxTable2, GE_TRUE))
00855                 {
00856                         D3DMain_Log("THandle_CheckCache:  D3DCache_AdjustSlots failed for LMapCache.\n");
00857                         return GE_FALSE;
00858                 }
00859         #endif
00860 
00861         if (!D3DCache_AdjustSlots(TextureCache, MaxTable1, GE_FALSE))
00862         {
00863                 D3DMain_Log("THandle_CheckCache:  D3DCache_AdjustSlots failed for TextureCache.\n");
00864                 return GE_FALSE;
00865         }
00866 
00867 #endif
00868 
00869         // Make sure no THandles reference any slots, because they mave have been moved around, or gotten destroyed...
00870         //  (Evict all textures in the cache)
00871         pTHandle = TextureHandles;
00872 
00873         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pTHandle++)
00874         {
00875                 int32           m;
00876 
00877                 for (m=0; m< pTHandle->NumMipLevels; m++)
00878                 {
00879                         pTHandle->MipData[m].Slot = NULL;
00880                 }
00881         }
00882 
00883         CacheNeedsUpdate = GE_FALSE;
00884 
00885         return GE_TRUE;
00886 }

Generated on Tue Sep 30 12:36:29 2003 for GTestAndEngine by doxygen 1.3.2