- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-ILUT/src/ilut_directx9.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Utility Toolkit Sources
    00004 // Copyright (C) 2000-2009 by Denton Woods
    00005 // Last modified: 01/30/2009
    00006 //
    00007 // Filename: src-ILUT/src/ilut_directx9.c
    00008 //
    00009 // Description: DirectX 9 functions for textures
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "ilut_internal.h"
    00015 #ifdef ILUT_USE_DIRECTX9
    00016
    00017
    00018 #include <d3d9.h>
    00019
    00020 //#include <d3dx9math.h>
    00021 //#include <d3dx9tex.h>
    00022 #pragma comment(lib, "d3d9.lib")
    00023 #pragma comment(lib, "d3dx9.lib")
    00024
    00025
    00026 ILimage*        MakeD3D9Compliant(IDirect3DDevice9 *Device, D3DFORMAT *DestFormat);
    00027 ILenum          GetD3D9Compat(ILenum Format);
    00028 //D3DFORMAT     GetD3DFormat(ILenum Format);
    00029 D3DFORMAT       D3DGetDXTCNumDX9(ILenum DXTCFormat);
    00030 ILenum          D3DGetDXTCFormat(D3DFORMAT DXTCNum);
    00031 ILboolean       iD3D9CreateMipmaps(IDirect3DTexture9 *Texture, ILimage *Image);
    00032 IDirect3DTexture9* iD3DMakeTexture( IDirect3DDevice9 *Device, void *Data, ILuint DLen, ILuint Width, ILuint Height, D3DFORMAT Format, D3DPOOL Pool, ILuint Levels );
    00033
    00034
    00035 #define ILUT_TEXTUREFORMAT_D3D9_COUNT           7
    00036 ILboolean       FormatsDX9Checked = IL_FALSE;
    00037 ILboolean       FormatsDX9supported[ILUT_TEXTUREFORMAT_D3D9_COUNT] =
    00038         { IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE };
    00039 D3DFORMAT       FormatsDX9[ILUT_TEXTUREFORMAT_D3D9_COUNT] =
    00040         { D3DFMT_R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_L8, D3DFMT_DXT1, D3DFMT_DXT3, D3DFMT_DXT5, D3DFMT_A16B16G16R16F};
    00041
    00042
    00043 ILboolean ilutD3D9Init()
    00044 {
    00045
    00046         return IL_TRUE;
    00047 }
    00048
    00049
    00050 void CheckFormatsDX9(IDirect3DDevice9 *Device)
    00051 {
    00052         D3DDISPLAYMODE  DispMode;
    00053         HRESULT                 hr;
    00054         IDirect3D9              *TestD3D9;
    00055         ILuint                  i;
    00056
    00057         IDirect3DDevice9_GetDirect3D(Device, (IDirect3D9**)&TestD3D9);
    00058         IDirect3DDevice9_GetDisplayMode(Device, 0, &DispMode);
    00059
    00060         for (i = 0; i < ILUT_TEXTUREFORMAT_D3D9_COUNT; i++) {
    00061                 hr = IDirect3D9_CheckDeviceFormat(TestD3D9, D3DADAPTER_DEFAULT,
    00062                         D3DDEVTYPE_HAL, DispMode.Format, 0, D3DRTYPE_TEXTURE, FormatsDX9[i]);
    00063                 FormatsDX9supported[i] = (ILboolean)SUCCEEDED(hr);
    00064         }
    00065
    00066         IDirect3D9_Release(TestD3D9);
    00067         FormatsDX9Checked = IL_TRUE;
    00068
    00069         return;
    00070 }
    00071
    00072
    00073 #ifndef _WIN32_WCE
    00074 ILboolean ILAPIENTRY ilutD3D9TexFromFile(IDirect3DDevice9 *Device, ILconst_string FileName, IDirect3DTexture9 **Texture)
    00075 {
    00076         iBindImageTemp();
    00077         if (!ilLoadImage(FileName))
    00078                 return IL_FALSE;
    00079
    00080         *Texture = ilutD3D9Texture(Device);
    00081
    00082         return IL_TRUE;
    00083 }
    00084 #endif//_WIN32_WCE
    00085
    00086 #ifndef _WIN32_WCE
    00087 ILboolean ILAPIENTRY ilutD3D9CubeTexFromFile(IDirect3DDevice9 *Device,
    00088                         ILconst_string FileName, IDirect3DCubeTexture9 **Texture)
    00089 {
    00090         iBindImageTemp();
    00091         if (!ilLoadImage(FileName))
    00092         return IL_FALSE;
    00093
    00094     *Texture = ilutD3D9CubeTexture(Device);
    00095
    00096     return IL_TRUE;
    00097 }
    00098
    00099 #endif//_WIN32_WCE
    00100 ILboolean ILAPIENTRY ilutD3D9CubeTexFromFileInMemory(IDirect3DDevice9 *Device,
    00101                                 void *Lump, ILuint Size, IDirect3DCubeTexture9 **Texture)
    00102 {
    00103         iBindImageTemp();
    00104     if( !ilLoadL(IL_TYPE_UNKNOWN, Lump, Size) )
    00105         return IL_FALSE;
    00106
    00107     *Texture = ilutD3D9CubeTexture(Device);
    00108
    00109     return IL_TRUE;
    00110  }
    00111
    00112 ILboolean ILAPIENTRY ilutD3D9CubeTexFromResource(IDirect3DDevice9 *Device,
    00113                 HMODULE SrcModule, ILconst_string SrcResource, IDirect3DCubeTexture9 **Texture)
    00114 {
    00115         HRSRC   Resource;
    00116     ILubyte *Data;
    00117
    00118     iBindImageTemp();
    00119
    00120     Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
    00121     Data = (ILubyte*)LockResource(Resource);
    00122     if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
    00123         return IL_FALSE;
    00124
    00125     *Texture = ilutD3D9CubeTexture(Device);
    00126
    00127     return IL_TRUE;
    00128  }
    00129
    00130 ILboolean ILAPIENTRY ilutD3D9CubeTexFromFileHandle(IDirect3DDevice9 *Device,
    00131                         ILHANDLE File, IDirect3DCubeTexture9 **Texture)
    00132 {
    00133         iBindImageTemp();
    00134     if( !ilLoadF(IL_TYPE_UNKNOWN, File) )
    00135         return IL_FALSE;
    00136
    00137     *Texture = ilutD3D9CubeTexture(Device);
    00138
    00139     return IL_TRUE;
    00140 }
    00141
    00142 D3DCUBEMAP_FACES iToD3D9Cube(ILuint cube)
    00143 {
    00144         switch (cube)
    00145         {
    00146         case IL_CUBEMAP_POSITIVEX:
    00147                 return D3DCUBEMAP_FACE_POSITIVE_X;
    00148         case IL_CUBEMAP_NEGATIVEX:
    00149             return D3DCUBEMAP_FACE_NEGATIVE_X;
    00150         case IL_CUBEMAP_POSITIVEY:
    00151             return D3DCUBEMAP_FACE_POSITIVE_Y;
    00152         case IL_CUBEMAP_NEGATIVEY:
    00153             return D3DCUBEMAP_FACE_NEGATIVE_Y;
    00154         case IL_CUBEMAP_POSITIVEZ:
    00155             return D3DCUBEMAP_FACE_POSITIVE_Z;
    00156         case IL_CUBEMAP_NEGATIVEZ:
    00157             return D3DCUBEMAP_FACE_NEGATIVE_Z;
    00158         default:
    00159             return D3DCUBEMAP_FACE_POSITIVE_X; //???
    00160         }
    00161  }
    00162
    00163  IDirect3DCubeTexture9* ILAPIENTRY ilutD3D9CubeTexture(IDirect3DDevice9 *Device)
    00164  {
    00165         IDirect3DCubeTexture9   *Texture;
    00166         D3DLOCKED_RECT  Box;
    00167     D3DFORMAT               Format;
    00168     ILimage                 *StartImage;
    00169     ILimage                 *Image;
    00170     int i;
    00171     Texture=NULL;
    00172     Image=NULL;
    00173     ilutCurImage = ilGetCurImage();
    00174     if (ilutCurImage == NULL) {
    00175         ilSetError(ILUT_ILLEGAL_OPERATION);
    00176         return NULL;
    00177     }
    00178
    00179     if (!FormatsDX9Checked)
    00180         CheckFormatsDX9(Device);
    00181
    00182         Image = MakeD3D9Compliant(Device, &Format);
    00183         if (Image == NULL) {
    00184                 if (Image != ilutCurImage)
    00185                         ilCloseImage(Image);
    00186                 return NULL;
    00187         }
    00188
    00189     StartImage = ilutCurImage;
    00190     if (FAILED(IDirect3DDevice9_CreateCubeTexture(Device, StartImage->Width,
    00191                        ilutGetInteger(ILUT_D3D_MIPLEVELS),0,Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL)))
    00192                return NULL;
    00193                 for (i = 0; i < CUBEMAP_SIDES; i++) {
    00194                 if (ilutCurImage==NULL || ilutCurImage->CubeFlags == 0) {
    00195                 SAFE_RELEASE(Texture)
    00196                 return NULL;
    00197             }
    00198             Image = ilutCurImage;
    00199
    00200                         Image = MakeD3D9Compliant(Device, &Format);
    00201             if( Image == NULL ) {
    00202                 SAFE_RELEASE(Texture)
    00203                 return NULL;
    00204             }
    00205             if( FAILED(IDirect3DCubeTexture9_LockRect(Texture,iToD3D9Cube(Image->CubeFlags), 0, &Box, NULL, /*D3DLOCK_DISCARD*/0))) {
    00206                 SAFE_RELEASE(Texture)
    00207                 return NULL;
    00208             }
    00209
    00210             memcpy(Box.pBits, Image->Data, Image->SizeOfData);
    00211                                 if (IDirect3DCubeTexture9_UnlockRect(Texture,iToD3D9Cube(Image->CubeFlags), 0) != D3D_OK) {
    00212                         SAFE_RELEASE(Texture)
    00213                 return IL_FALSE;
    00214                 }
    00215                 ilutCurImage=ilutCurImage->Faces;
    00216         }
    00217         ilutCurImage = StartImage;
    00218
    00219         // We don't want to have mipmaps for such a large image.
    00220                 //if (Image != ilutCurImage)
    00221          //             ilCloseImage(Image);
    00222
    00223         return Texture;
    00224 }
    00225
    00226 #ifndef _WIN32_WCE
    00227 ILboolean ILAPIENTRY ilutD3D9VolTexFromFile(IDirect3DDevice9 *Device, ILconst_string FileName, IDirect3DVolumeTexture9 **Texture) {
    00228         iBindImageTemp();
    00229         if (!ilLoadImage(FileName))
    00230                 return IL_FALSE;
    00231
    00232         *Texture = ilutD3D9VolumeTexture(Device);
    00233
    00234         return IL_TRUE;
    00235 }
    00236 #endif//_WIN32_WCE
    00237
    00238
    00239 ILboolean ILAPIENTRY ilutD3D9TexFromFileInMemory(IDirect3DDevice9 *Device, void *Lump, ILuint Size, IDirect3DTexture9 **Texture)
    00240 {
    00241         iBindImageTemp();
    00242         if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
    00243                 return IL_FALSE;
    00244
    00245         *Texture = ilutD3D9Texture(Device);
    00246
    00247         return IL_TRUE;
    00248 }
    00249
    00250
    00251 ILboolean ILAPIENTRY ilutD3D9VolTexFromFileInMemory(IDirect3DDevice9 *Device, void *Lump, ILuint Size, IDirect3DVolumeTexture9 **Texture)
    00252 {
    00253         iBindImageTemp();
    00254         if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
    00255                 return IL_FALSE;
    00256
    00257         *Texture = ilutD3D9VolumeTexture(Device);
    00258
    00259         return IL_TRUE;
    00260 }
    00261
    00262
    00263 ILboolean ILAPIENTRY ilutD3D9TexFromResource(IDirect3DDevice9 *Device, HMODULE SrcModule, ILconst_string SrcResource, IDirect3DTexture9 **Texture)
    00264 {
    00265         HRSRC   Resource;
    00266         ILubyte *Data;
    00267
    00268         iBindImageTemp();
    00269
    00270         Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
    00271         Data = (ILubyte*)LockResource(Resource);
    00272         if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
    00273                 return IL_FALSE;
    00274
    00275         *Texture = ilutD3D9Texture(Device);
    00276
    00277         return IL_TRUE;
    00278 }
    00279
    00280
    00281 ILboolean ILAPIENTRY ilutD3D9VolTexFromResource(IDirect3DDevice9 *Device, HMODULE SrcModule, ILconst_string SrcResource, IDirect3DVolumeTexture9 **Texture)
    00282 {
    00283         HRSRC   Resource;
    00284         ILubyte *Data;
    00285
    00286         iBindImageTemp();
    00287
    00288         Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
    00289         Data = (ILubyte*)LockResource(Resource);
    00290         if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
    00291                 return IL_FALSE;
    00292
    00293         *Texture = ilutD3D9VolumeTexture(Device);
    00294
    00295         return IL_TRUE;
    00296 }
    00297
    00298
    00299 ILboolean ILAPIENTRY ilutD3D9TexFromFileHandle(IDirect3DDevice9 *Device, ILHANDLE File, IDirect3DTexture9 **Texture)
    00300 {
    00301         iBindImageTemp();
    00302         if (!ilLoadF(IL_TYPE_UNKNOWN, File))
    00303                 return IL_FALSE;
    00304
    00305         *Texture = ilutD3D9Texture(Device);
    00306
    00307         return IL_TRUE;
    00308 }
    00309
    00310
    00311 ILboolean ILAPIENTRY ilutD3D9VolTexFromFileHandle(IDirect3DDevice9 *Device, ILHANDLE File, IDirect3DVolumeTexture9 **Texture)
    00312 {
    00313         iBindImageTemp();
    00314         if (!ilLoadF(IL_TYPE_UNKNOWN, File))
    00315                 return IL_FALSE;
    00316
    00317         *Texture = ilutD3D9VolumeTexture(Device);
    00318
    00319         return IL_TRUE;
    00320 }
    00321
    00322
    00323 D3DFORMAT D3DGetDXTCNumDX9(ILenum DXTCFormat)
    00324 {
    00325         switch (DXTCFormat)
    00326         {
    00327                 case IL_DXT1:
    00328                         return D3DFMT_DXT1;
    00329                 case IL_DXT3:
    00330                         return D3DFMT_DXT3;
    00331                 case IL_DXT5:
    00332                         return D3DFMT_DXT5;
    00333         }
    00334
    00335         return D3DFMT_UNKNOWN;
    00336 }
    00337
    00338
    00339 ILenum D3DGetDXTCFormat(D3DFORMAT DXTCNum)
    00340 {
    00341         switch (DXTCNum)
    00342         {
    00343                 case D3DFMT_DXT1:
    00344                         return IL_DXT1;
    00345                 case D3DFMT_DXT3:
    00346                         return IL_DXT3;
    00347                 case D3DFMT_DXT5:
    00348                         return IL_DXT5;
    00349         }
    00350
    00351         return D3DFMT_UNKNOWN;
    00352 }
    00353
    00354
    00355 IDirect3DTexture9* iD3DMakeTexture( IDirect3DDevice9 *Device, void *Data, ILuint DLen, ILuint Width, ILuint Height, D3DFORMAT Format, D3DPOOL Pool, ILuint Levels )
    00356 {
    00357         IDirect3DTexture9 *Texture;
    00358         D3DLOCKED_RECT Rect;
    00359
    00360         if (FAILED(IDirect3DDevice9_CreateTexture(Device, Width, Height, Levels,
    00361                         0, Format, Pool, &Texture, NULL)))
    00362                 return NULL;
    00363         if (FAILED(IDirect3DTexture9_LockRect(Texture, 0, &Rect, NULL, 0)))
    00364                 return NULL;
    00365         memcpy(Rect.pBits, Data, DLen);
    00366         IDirect3DTexture9_UnlockRect(Texture, 0);
    00367
    00368         return Texture;
    00369 }
    00370
    00371 IDirect3DTexture9* ILAPIENTRY ilutD3D9Texture(IDirect3DDevice9 *Device)
    00372 {
    00373         IDirect3DTexture9 *Texture;
    00374 //      D3DLOCKED_RECT Rect;
    00375         D3DFORMAT Format;
    00376         ILimage *Image;
    00377         ILenum  DXTCFormat;
    00378         ILuint  Size;
    00379         ILubyte *Buffer;
    00380
    00381         Image = ilutCurImage = ilGetCurImage();
    00382         if (ilutCurImage == NULL) {
    00383                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00384                 return NULL;
    00385         }
    00386
    00387         if (!FormatsDX9Checked)
    00388                 CheckFormatsDX9(Device);
    00389
    00390         if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX9supported[3] && FormatsDX9supported[4] && FormatsDX9supported[5]) {
    00391                 if (ilutCurImage->DxtcData != NULL && ilutCurImage->DxtcSize != 0) {
    00392                         ILuint  dxtcFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
    00393                         Format = D3DGetDXTCNumDX9(ilutCurImage->DxtcFormat);
    00394                         ilutSetInteger(ILUT_DXTC_FORMAT, ilutCurImage->DxtcFormat);
    00395
    00396                         Texture = iD3DMakeTexture(Device, ilutCurImage->DxtcData, ilutCurImage->DxtcSize,
    00397                                 ilutCurImage->Width, ilutCurImage->Height, Format,
    00398                                 (D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
    00399                         if (!Texture)
    00400                                 return NULL;
    00401                         iD3D9CreateMipmaps(Texture, Image);
    00402                         if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
    00403                                 IDirect3DTexture9 *SysTex = Texture;
    00404                                 // copy texture to device memory
    00405                                 if (FAILED(IDirect3DDevice9_CreateTexture(Device, ilutCurImage->Width,
    00406                                                 ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
    00407                                                 D3DPOOL_DEFAULT, &Texture, NULL))) {
    00408                                         IDirect3DTexture9_Release(SysTex);
    00409                                         return NULL;
    00410                                 }
    00411                                 if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
    00412                                         IDirect3DTexture9_Release(SysTex);
    00413                                         return NULL;
    00414                                 }
    00415                                 IDirect3DTexture9_Release(SysTex);
    00416                         }
    00417                         ilutSetInteger(ILUT_DXTC_FORMAT, dxtcFormat);
    00418
    00419                         goto success;
    00420                 }
    00421
    00422                 if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
    00423                         DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
    00424
    00425 /*
    00426 Image = MakeD3D9Compliant(Device, &Format);
    00427                         if (Image == NULL) {
    00428                                 if (Image != ilutCurImage)
    00429                                         ilCloseImage(Image);
    00430                                 return NULL;
    00431                         }
    00432 */
    00433
    00434                         Size = ilGetDXTCData(NULL, 0, DXTCFormat);
    00435                         if (Size != 0) {
    00436                                 Buffer = (ILubyte*)ialloc(Size);
    00437                                 if (Buffer == NULL)
    00438                                         return NULL;
    00439                                 Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
    00440                                 if (Size == 0) {
    00441                                         ifree(Buffer);
    00442                                         return NULL;
    00443                                 }
    00444
    00445                                 Format = D3DGetDXTCNumDX9(DXTCFormat);
    00446                                 Texture = iD3DMakeTexture(Device, Buffer, Size,
    00447                                         ilutCurImage->Width, ilutCurImage->Height, Format,
    00448                                         (D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
    00449                                 if (!Texture)
    00450                                         return NULL;
    00451                                 iD3D9CreateMipmaps(Texture, Image);
    00452                                 if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
    00453                                         IDirect3DTexture9 *SysTex = Texture;
    00454
    00455                                         if (FAILED(IDirect3DDevice9_CreateTexture(Device, ilutCurImage->Width,
    00456                                                         ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
    00457                                                         D3DPOOL_DEFAULT, &Texture, NULL))) {
    00458                                                 IDirect3DTexture9_Release(SysTex);
    00459                                                 return NULL;
    00460                                         }
    00461                                         if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
    00462                                                 IDirect3DTexture9_Release(SysTex);
    00463                                                 return NULL;
    00464                                         }
    00465                                         IDirect3DTexture9_Release(SysTex);
    00466                                 }
    00467
    00468                                 goto success;
    00469                         }
    00470                 }
    00471         }
    00472
    00473         Image = MakeD3D9Compliant(Device, &Format);
    00474         if (Image == NULL) {
    00475                 if (Image != ilutCurImage)
    00476                         ilCloseImage(Image);
    00477                 return NULL;
    00478         }
    00479
    00480         Texture = iD3DMakeTexture(Device, Image->Data, Image->SizeOfPlane,
    00481                 Image->Width, Image->Height, Format,
    00482                 (D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
    00483         if (!Texture)
    00484                 return NULL;
    00485         iD3D9CreateMipmaps(Texture, Image);
    00486         if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
    00487                 IDirect3DTexture9 *SysTex = Texture;
    00488                 // create texture in system memory
    00489                 if (FAILED(IDirect3DDevice9_CreateTexture(Device, Image->Width,
    00490                                 Image->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
    00491                                 (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL))) {
    00492                         IDirect3DTexture9_Release(SysTex);
    00493                         return NULL;
    00494                 }
    00495                 if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
    00496                         IDirect3DTexture9_Release(SysTex);
    00497                         return NULL;
    00498                 }
    00499                 IDirect3DTexture9_Release(SysTex);
    00500         }
    00501 //      if (Image != ilutCurImage)
    00502 //              ilCloseImage(Image);
    00503
    00504 success:
    00505
    00506         if (Image != ilutCurImage)
    00507                 ilCloseImage(Image);
    00508
    00509         return Texture;
    00510 }
    00511
    00512
    00513 IDirect3DVolumeTexture9* ILAPIENTRY ilutD3D9VolumeTexture(IDirect3DDevice9 *Device)
    00514 {
    00515         IDirect3DVolumeTexture9 *Texture;
    00516         D3DLOCKED_BOX   Box;
    00517         D3DFORMAT               Format;
    00518         ILimage                 *Image;
    00519
    00520         ilutCurImage = ilGetCurImage();
    00521         if (ilutCurImage == NULL) {
    00522                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00523                 return NULL;
    00524         }
    00525
    00526         if (!FormatsDX9Checked)
    00527                 CheckFormatsDX9(Device);
    00528
    00529         Image = MakeD3D9Compliant(Device, &Format);
    00530         if (Image == NULL)
    00531                 return NULL;
    00532         if (FAILED(IDirect3DDevice9_CreateVolumeTexture(Device, Image->Width, Image->Height,
    00533                 Image->Depth, 1, 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL)))
    00534                 return NULL;
    00535         if (FAILED(IDirect3DVolumeTexture9_LockBox(Texture, 0, &Box, NULL, 0)))
    00536                 return NULL;
    00537
    00538         memcpy(Box.pBits, Image->Data, Image->SizeOfData);
    00539         if (!IDirect3DVolumeTexture9_UnlockBox(Texture, 0))
    00540                 return IL_FALSE;
    00541
    00542         // We don't want to have mipmaps for such a large image.
    00543
    00544         if (Image != ilutCurImage)
    00545                 ilCloseImage(Image);
    00546
    00547         return Texture;
    00548 }
    00549
    00550
    00551
    00552 ILimage *MakeD3D9Compliant(IDirect3DDevice9 *Device, D3DFORMAT *DestFormat)
    00553 {
    00554         ILuint  color;
    00555         ILimage *Converted, *Scaled, *CurImage;
    00556         ILuint nConversionType, ilutFormat;
    00557         ILboolean bForceIntegerFormat = ilutGetBoolean(ILUT_FORCE_INTEGER_FORMAT);
    00558
    00559         Device;
    00560         ilutFormat = ilutCurImage->Format;
    00561         nConversionType = ilutCurImage->Type;
    00562
    00563         if (!ilutCurImage)
    00564                 return NULL;
    00565
    00566         switch (ilutCurImage->Type)
    00567         {
    00568         case IL_UNSIGNED_BYTE:
    00569         case IL_BYTE:
    00570         case IL_SHORT:
    00571         case IL_UNSIGNED_SHORT:
    00572         case IL_INT:
    00573         case IL_UNSIGNED_INT:
    00574                 *DestFormat     = D3DFMT_A8R8G8B8;
    00575                 nConversionType = IL_UNSIGNED_BYTE;
    00576                 ilutFormat      = IL_BGRA;
    00577                 break;
    00578         case IL_FLOAT:
    00579         case IL_DOUBLE:
    00580         case IL_HALF:
    00581                 if (bForceIntegerFormat || (!FormatsDX9supported[6]))
    00582                 {
    00583                         *DestFormat     = D3DFMT_A8R8G8B8;
    00584                         nConversionType = IL_UNSIGNED_BYTE;
    00585                         ilutFormat      = IL_BGRA;
    00586                 }
    00587                 else
    00588                 {
    00589                         *DestFormat     = D3DFMT_A16B16G16R16F;
    00590                         nConversionType = IL_HALF;
    00591                         ilutFormat      = IL_RGBA;
    00592
    00593                         //
    00594                 }
    00595                 break;
    00596         }
    00597
    00598         // Images must be in BGRA format.
    00599         if (((ilutCurImage->Format != ilutFormat))
    00600                 || (ilutCurImage->Type != nConversionType))
    00601         {
    00602                 Converted = iConvertImage(ilutCurImage, ilutFormat, nConversionType);
    00603                 if (Converted == NULL)
    00604                         return NULL;
    00605         }
    00606         else
    00607         {
    00608                 Converted = ilutCurImage;
    00609         }
    00610
    00611         // perform alpha key on images if requested
    00612         color=ilutGetInteger(ILUT_D3D_ALPHA_KEY_COLOR);
    00613         if((color>=0) && (nConversionType == IL_UNSIGNED_BYTE))
    00614         {
    00615                 ILubyte *data;
    00616                 ILubyte *maxdata;
    00617                 ILuint t;
    00618
    00619                         data=(Converted->Data);
    00620                         maxdata=(Converted->Data+Converted->SizeOfData);
    00621                         while(data<maxdata)
    00622                         {
    00623                                 t= (data[2]<<16) + (data[1]<<8) + (data[0]) ;
    00624
    00625                                 if((t&0x00ffffff)==(color&0x00ffffff))
    00626                                 {
    00627                                         data[0]=0;
    00628                                         data[1]=0;
    00629                                         data[2]=0;
    00630                                         data[3]=0;
    00631                                 }
    00632                                 data+=4;
    00633                         }
    00634         }
    00635
    00636         // Images must have their origin in the upper left.
    00637         if (Converted->Origin != IL_ORIGIN_UPPER_LEFT)
    00638         {
    00639                 CurImage = ilutCurImage;
    00640                 ilSetCurImage(Converted);
    00641                 iluFlipImage();
    00642                 ilSetCurImage(CurImage);
    00643         }
    00644
    00645         // Images must have powers-of-2 dimensions.
    00646         if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
    00647                 ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
    00648                 ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
    00649                         Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
    00650                                                 ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
    00651                         if (Converted != ilutCurImage) {
    00652                                 ilCloseImage(Converted);
    00653                         }
    00654                         if (Scaled == NULL) {
    00655                                 return NULL;
    00656                         }
    00657                         Converted = Scaled;
    00658         }
    00659
    00660         return Converted;
    00661 }
    00662
    00663
    00664 ILboolean iD3D9CreateMipmaps(IDirect3DTexture9 *Texture, ILimage *Image)
    00665 {
    00666         D3DLOCKED_RECT  Rect;
    00667         D3DSURFACE_DESC Desc;
    00668         ILuint                  NumMips,  srcMips, Width, Height, i;
    00669         ILimage                 *CurImage, *MipImage, *Temp;
    00670         ILenum                  DXTCFormat;
    00671         ILuint                  Size;
    00672         ILubyte                 *Buffer;
    00673         ILboolean               useDXTC = IL_FALSE;
    00674
    00675         NumMips = IDirect3DTexture9_GetLevelCount(Texture);
    00676         Width = Image->Width;
    00677         Height = Image->Height;
    00678
    00679         if (NumMips == 1)
    00680                 return IL_TRUE;
    00681
    00682         CurImage = ilGetCurImage();
    00683         MipImage = Image;
    00684         iGetIntegervImage(MipImage, IL_NUM_MIPMAPS, (ILint*) &srcMips);
    00685         if ( srcMips != NumMips-1) {
    00686                 MipImage = ilCopyImage_(Image);
    00687                 ilSetCurImage(MipImage);
    00688                 if (!iluBuildMipmaps()) {
    00689                         ilCloseImage(MipImage);
    00690                         ilSetCurImage(CurImage);
    00691                         return IL_FALSE;
    00692                 }
    00693         }
    00694 //      ilSetCurImage(CurImage);
    00695         Temp = MipImage->Mipmaps;
    00696
    00697         if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX9supported[3] && FormatsDX9supported[4] && FormatsDX9supported[5])
    00698                 useDXTC = IL_TRUE;
    00699
    00700         // Counts the base texture as 1.
    00701         for (i = 1; i < NumMips && Temp != NULL; i++) {
    00702                 ilSetCurImage(Temp);
    00703                 if (FAILED(IDirect3DTexture9_LockRect(Texture, i, &Rect, NULL, 0)))
    00704                         return IL_FALSE;
    00705
    00706                 Width = IL_MAX(1, Width / 2);
    00707                 Height = IL_MAX(1, Height / 2);
    00708
    00709                 IDirect3DTexture9_GetLevelDesc(Texture, i, &Desc);
    00710                 if (Desc.Width != Width || Desc.Height != Height) {
    00711                         IDirect3DTexture9_UnlockRect(Texture, i);
    00712                         return IL_FALSE;
    00713                 }
    00714
    00715                 if (useDXTC) {
    00716                         if (Temp->DxtcData != NULL && Temp->DxtcSize != 0) {
    00717                                 memcpy(Rect.pBits, Temp->DxtcData, Temp->DxtcSize);
    00718                         } else if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
    00719                                 DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
    00720
    00721                                 Size = ilGetDXTCData(NULL, 0, DXTCFormat);
    00722                                 if (Size != 0) {
    00723                                         Buffer = (ILubyte*)ialloc(Size);
    00724                                         if (Buffer == NULL) {
    00725                                                 IDirect3DTexture9_UnlockRect(Texture, i);
    00726                                                 return IL_FALSE;
    00727                                         }
    00728                                         Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
    00729                                         if (Size == 0) {
    00730                                                 ifree(Buffer);
    00731                                                 IDirect3DTexture9_UnlockRect(Texture, i);
    00732                                                 return IL_FALSE;
    00733                                         }
    00734                                         memcpy(Rect.pBits, Buffer, Size);
    00735                                 } else {
    00736                                         IDirect3DTexture9_UnlockRect(Texture, i);
    00737                                         return IL_FALSE;
    00738                                 }
    00739                         } else {
    00740                                 IDirect3DTexture9_UnlockRect(Texture, i);
    00741                                 return IL_FALSE;
    00742                         }
    00743                 } else
    00744                         memcpy(Rect.pBits, Temp->Data, Temp->SizeOfData);
    00745
    00746                 IDirect3DTexture9_UnlockRect(Texture, i);
    00747                 Temp = Temp->Mipmaps;
    00748         }
    00749
    00750         if (MipImage != Image)
    00751                 ilCloseImage(MipImage);
    00752         ilSetCurImage(CurImage);
    00753
    00754         return IL_TRUE;
    00755 }
    00756
    00757
    00758 //
    00759 // SaveSurfaceToFile.cpp
    00760 //
    00761 // Copyright (c) 2001 David Galeano
    00762 //
    00763 // Permission to use, copy, modify and distribute this software
    00764 // is hereby granted, provided that both the copyright notice and
    00765 // this permission notice appear in all copies of the software,
    00766 // derivative works or modified versions.
    00767 //
    00768 // THE AUTHOR ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
    00769 // CONDITION AND DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
    00770 // WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
    00771 //
    00772
    00773 ILAPI ILboolean ILAPIENTRY ilutD3D9LoadSurface(IDirect3DDevice9 *Device, IDirect3DSurface9 *Surface)
    00774 {
    00775         HRESULT                         hr;
    00776         D3DSURFACE_DESC         d3dsd;
    00777         LPDIRECT3DSURFACE9      SurfaceCopy;
    00778         D3DLOCKED_RECT          d3dLR;
    00779         ILboolean                       bHasAlpha;
    00780         ILubyte                         *Image, *ImageAux, *Data;
    00781         ILuint                          y, x;
    00782         ILushort                        dwColor;
    00783
    00784         IDirect3DSurface9_GetDesc(Surface, &d3dsd);
    00785
    00786         bHasAlpha = (ILboolean)(d3dsd.Format == D3DFMT_A8R8G8B8 || d3dsd.Format == D3DFMT_A1R5G5B5);
    00787
    00788         if (bHasAlpha) {
    00789                 if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL)) {
    00790                         return IL_FALSE;
    00791                 }
    00792         }
    00793         else {
    00794                 if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) {
    00795                         return IL_FALSE;
    00796                 }
    00797         }
    00798
    00799         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(Device, d3dsd.Width, d3dsd.Height, d3dsd.Format, D3DPOOL_SCRATCH, &SurfaceCopy, NULL);
    00800         if (FAILED(hr)) {
    00801                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00802                 return IL_FALSE;
    00803         }
    00804
    00805         hr = IDirect3DDevice9_StretchRect(Device, Surface, NULL, SurfaceCopy, NULL, D3DTEXF_NONE);
    00806         if (FAILED(hr)) {
    00807                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00808                 return IL_FALSE;
    00809         }
    00810
    00811         hr = IDirect3DSurface9_LockRect(SurfaceCopy, &d3dLR, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
    00812         if (FAILED(hr)) {
    00813                 IDirect3DSurface9_Release(SurfaceCopy);
    00814                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00815                 return IL_FALSE;
    00816         }
    00817
    00818         Image = (ILubyte*)d3dLR.pBits;
    00819         Data = ilutCurImage->Data;
    00820
    00821         for (y = 0; y < d3dsd.Height; y++) {
    00822                 if (d3dsd.Format == D3DFMT_X8R8G8B8) {
    00823                         ImageAux = Image;
    00824                         for (x = 0; x < d3dsd.Width; x++) {
    00825                                 Data[0] = ImageAux[0];
    00826                                 Data[1] = ImageAux[1];
    00827                                 Data[2] = ImageAux[2];
    00828
    00829                                 Data += 3;
    00830                                 ImageAux += 4;
    00831                         }
    00832                 }
    00833                 else if (d3dsd.Format == D3DFMT_A8R8G8B8) {
    00834                         memcpy(Data, Image, d3dsd.Width * 4);
    00835                 }
    00836                 else if (d3dsd.Format == D3DFMT_R5G6B5) {
    00837                         ImageAux = Image;
    00838                         for (x = 0; x < d3dsd.Width; x++) {
    00839                                 dwColor = *((ILushort*)ImageAux);
    00840
    00841                                 Data[0] = (ILubyte)((dwColor&0x001f)<<3);
    00842                                 Data[1] = (ILubyte)(((dwColor&0x7e0)>>5)<<2);
    00843                                 Data[2] = (ILubyte)(((dwColor&0xf800)>>11)<<3);
    00844
    00845                                 Data += 3;
    00846                                 ImageAux += 2;
    00847                         }
    00848                 }
    00849                 else if (d3dsd.Format == D3DFMT_X1R5G5B5) {
    00850                         ImageAux = Image;
    00851                         for (x = 0; x < d3dsd.Width; x++) {
    00852                                 dwColor = *((ILushort*)ImageAux);
    00853
    00854                                 Data[0] = (ILubyte)((dwColor&0x001f)<<3);
    00855                                 Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
    00856                                 Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
    00857
    00858                                 Data += 3;
    00859                                 ImageAux += 2;
    00860                         }
    00861                 }
    00862                 else if (d3dsd.Format == D3DFMT_A1R5G5B5) {
    00863                         ImageAux = Image;
    00864                         for (x = 0; x < d3dsd.Width; x++) {
    00865                                 dwColor = *((ILushort*)ImageAux);
    00866
    00867                                 Data[0] = (ILubyte)((dwColor&0x001f)<<3);
    00868                                 Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
    00869                                 Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
    00870                                 Data[3] = (ILubyte)(((dwColor&0x8000)>>15)*255);
    00871
    00872                                 Data += 4;
    00873                                 ImageAux += 2;
    00874                         }
    00875                 }
    00876
    00877                 Image += d3dLR.Pitch;
    00878         }
    00879
    00880         IDirect3DSurface9_UnlockRect(SurfaceCopy);
    00881         IDirect3DSurface9_Release(SurfaceCopy);
    00882
    00883         return IL_TRUE;
    00884 }
    00885
    00886 #endif//ILUT_USE_DIRECTX9