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

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