- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Utility Toolkit Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 12/17/2008
    00006 //
    00007 // Filename: src-ILUT/src/ilut_directx10.c
    00008 //
    00009 // Description: DirectX 10 functions for textures
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 // Needed to use the C interface to DirectX 10.
    00015 #define COBJMACROS
    00016
    00017 #include "ilut_internal.h"
    00018 #ifdef ILUT_USE_DIRECTX10
    00019
    00020 #include <d3d10.h>
    00021
    00022 #pragma comment(lib, "d3d10.lib")
    00023 #pragma comment(lib, "d3dx10.lib")
    00024
    00025
    00026 ILimage*        MakeD3D10Compliant(ID3D10Device *Device, DXGI_FORMAT *DestFormat);
    00027 ILenum          GetD3D10Compat(ILenum Format);
    00028 //D3DFORMAT     GetD3D10Format(ILenum Format);
    00029 D3DFORMAT       D3DGetDXTCNumDX10(ILenum DXTCFormat);
    00030 ILenum          D3DGetDXTCFormat(D3DFORMAT DXTCNum);
    00031 ILboolean       iD3D10CreateMipmaps(IDirect3DTexture9 *Texture, ILimage *Image);
    00032 //IDirect3DTexture10* iD3DMakeTexture( IDirect3DDevice9 *Device, void *Data, ILuint DLen, ILuint Width, ILuint Height, D3DFORMAT Format, D3DPOOL Pool, ILuint Levels );
    00033
    00034
    00035 #define ILUT_TEXTUREFORMAT_D3D10_COUNT          7
    00036 ILboolean       FormatsDX10Checked = IL_FALSE;
    00037 ILboolean       FormatsDX10supported[ILUT_TEXTUREFORMAT_D3D10_COUNT] =
    00038         { IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE };
    00039 D3DFORMAT       FormatsDX10[ILUT_TEXTUREFORMAT_D3D10_COUNT] =
    00040 { DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R8G8B8A8_UINT, DXGI_FORMAT_R8_UINT, DXGI_FORMAT_BC1_UNORM_SRGB, DXGI_FORMAT_BC3_UNORM_SRGB, DXGI_FORMAT_BC5_UNORM};
    00041
    00042
    00043 // Nothing to do at the moment
    00044 ILboolean ilutD3D10Init()
    00045 {
    00046
    00047         return IL_TRUE;
    00048 }
    00049
    00050
    00051 void CheckFormatsDX10(ID3D10Device *Device)
    00052 {
    00053         ILuint  i;
    00054         UINT    FormatSupport;
    00055
    00056         for (i = 0; i < ILUT_TEXTUREFORMAT_D3D10_COUNT; i++) {
    00057                 ID3D10Device_CheckFormatSupport(Device, FormatsDX10[i], &FormatSupport);
    00058                 FormatsDX10supported[i] = (FormatSupport & D3D10_FORMAT_SUPPORT_TEXTURE2D) ? IL_TRUE : IL_FALSE;
    00059         }
    00060         FormatsDX10Checked = IL_TRUE;
    00061
    00062         return;
    00063 }
    00064
    00065 #ifndef _WIN32_WCE
    00066 ILboolean ILAPIENTRY ilutD3D10TexFromFile(ID3D10Device *Device, ILconst_string FileName, ID3D10Texture2D **Texture)
    00067 {
    00068         iBindImageTemp();
    00069         if (!ilLoadImage(FileName))
    00070                 return IL_FALSE;
    00071
    00072         *Texture = ilutD3D10Texture(Device);
    00073
    00074         return IL_TRUE;
    00075 }
    00076 #endif//_WIN32_WCE
    00077
    00078
    00079 ILboolean ILAPIENTRY ilutD3D10TexFromFileInMemory(ID3D10Device *Device, void *Lump, ILuint Size, ID3D10Texture2D **Texture)
    00080 {
    00081         iBindImageTemp();
    00082         if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
    00083                 return IL_FALSE;
    00084
    00085         *Texture = ilutD3D10Texture(Device);
    00086
    00087         return IL_TRUE;
    00088 }
    00089
    00090
    00091 ILboolean ILAPIENTRY ilutD3D10TexFromResource(ID3D10Device *Device, HMODULE SrcModule, ILconst_string SrcResource, ID3D10Texture2D **Texture)
    00092 {
    00093         HRSRC   Resource;
    00094         ILubyte *Data;
    00095
    00096         iBindImageTemp();
    00097
    00098         Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
    00099         Data = (ILubyte*)LockResource(Resource);
    00100         if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
    00101                 return IL_FALSE;
    00102
    00103         *Texture = ilutD3D10Texture(Device);
    00104
    00105         return IL_TRUE;
    00106 }
    00107
    00108
    00109 ILboolean ILAPIENTRY ilutD3D10TexFromFileHandle(ID3D10Device *Device, ILHANDLE File, ID3D10Texture2D **Texture)
    00110 {
    00111         iBindImageTemp();
    00112         if (!ilLoadF(IL_TYPE_UNKNOWN, File))
    00113                 return IL_FALSE;
    00114
    00115         *Texture = ilutD3D10Texture(Device);
    00116
    00117         return IL_TRUE;
    00118 }
    00119
    00120
    00121 D3DFORMAT D3DGetDXTCNumDX10(ILenum DXTCFormat)
    00122 {
    00123         switch (DXTCFormat)
    00124         {
    00125                 case IL_DXT1:
    00126                         return D3DFMT_DXT1;
    00127                 case IL_DXT3:
    00128                         return D3DFMT_DXT3;
    00129                 case IL_DXT5:
    00130                         return D3DFMT_DXT5;
    00131         }
    00132
    00133         return D3DFMT_UNKNOWN;
    00134 }
    00135
    00136
    00137 //ILenum D3DGetDXTCFormat(D3DFORMAT DXTCNum)
    00138 //{
    00139 //      switch (DXTCNum)
    00140 //      {
    00141 //              case D3DFMT_DXT1:
    00142 //                      return IL_DXT1;
    00143 //              case D3DFMT_DXT3:
    00144 //                      return IL_DXT3;
    00145 //              case D3DFMT_DXT5:
    00146 //                      return IL_DXT5;
    00147 //      }
    00148 //
    00149 //      return D3DFMT_UNKNOWN;
    00150 //}
    00151
    00152
    00153 // Code from http://msdn.microsoft.com/en-us/library/bb205131(VS.85).aspx#Creating_Empty_Textures
    00154 ID3D10Texture2D* iD3D10MakeTexture(ID3D10Device *Device, void *Data, ILuint Width, ILuint Height, DXGI_FORMAT Format, ILuint Levels)
    00155 {
    00156         ID3D10Texture2D *Texture = NULL;
    00157         D3D10_TEXTURE2D_DESC Desc;
    00158         D3D10_SUBRESOURCE_DATA InitialData;
    00159
    00160         Levels;
    00161
    00162         Desc.Width = Width;
    00163         Desc.Height = Height;
    00164         Desc.MipLevels = 1;  //@TODO: Change for mipmaps.
    00165         Desc.ArraySize = 1;  //@TODO: Change for mipmaps.
    00166         Desc.Format = Format;
    00167         Desc.SampleDesc.Count = 1;
    00168         Desc.Usage = D3D10_USAGE_DEFAULT;
    00169         Desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
    00170         Desc.CPUAccessFlags = 0;  // Only need to put the data in initially.
    00171
    00172         InitialData.pSysMem = Data;
    00173         InitialData.SysMemPitch = 0;
    00174         InitialData.SysMemSlicePitch = 0;
    00175
    00176         if (FAILED(ID3D10Device_CreateTexture2D(Device, &Desc, &InitialData, &Texture)))
    00177                 return NULL;
    00178
    00179         return Texture;
    00180 }
    00181
    00182 ID3D10Texture2D* ILAPIENTRY ilutD3D10Texture(ID3D10Device *Device)
    00183 {
    00184         ID3D10Texture2D *Texture;
    00185         DXGI_FORMAT Format;
    00186         ILimage *Image;
    00187
    00188         Image = ilutCurImage = ilGetCurImage();
    00189         if (ilutCurImage == NULL) {
    00190                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00191                 return NULL;
    00192         }
    00193
    00194         if (!FormatsDX10Checked)
    00195                 CheckFormatsDX10(Device);
    00196
    00197         Image = MakeD3D10Compliant(Device, &Format);
    00198         if (Image == NULL) {
    00199                 if (Image != ilutCurImage)
    00200                         ilCloseImage(Image);
    00201                 return NULL;
    00202         }
    00203
    00204         Texture = iD3D10MakeTexture(Device, Image->Data, Image->Width, Image->Height, Format, 1);  //@TODO: The 1 should be ilutGetInteger(ILUT_D3D_MIPLEVELS).
    00205         if (!Texture)
    00206                 return NULL;
    00207         //iD3D10CreateMipmaps(Texture, Image);
    00208
    00209 //success:
    00210
    00211         if (Image != ilutCurImage)
    00212                 ilCloseImage(Image);
    00213
    00214         return Texture;
    00215 }
    00216
    00217
    00218 ILimage *MakeD3D10Compliant(ID3D10Device *Device, DXGI_FORMAT *DestFormat)
    00219 {
    00220         ILimage *Converted, *Scaled, *CurImage;
    00221         ILuint nConversionType, ilutFormat;
    00222         ILboolean bForceIntegerFormat = ilutGetBoolean(ILUT_FORCE_INTEGER_FORMAT);
    00223
    00224         Device;
    00225         ilutFormat = ilutCurImage->Format;
    00226         nConversionType = ilutCurImage->Type;
    00227
    00228         if (!ilutCurImage)
    00229                 return NULL;
    00230
    00231         switch (ilutCurImage->Type)
    00232         {
    00233         case IL_UNSIGNED_BYTE:
    00234         case IL_BYTE:
    00235         case IL_SHORT:
    00236         case IL_UNSIGNED_SHORT:
    00237         case IL_INT:
    00238         case IL_UNSIGNED_INT:
    00239                 *DestFormat     = DXGI_FORMAT_R8G8B8A8_UINT;
    00240                 nConversionType = IL_UNSIGNED_BYTE;
    00241                 ilutFormat      = IL_RGBA;
    00242                 break;
    00243         case IL_FLOAT:
    00244         case IL_DOUBLE:
    00245         case IL_HALF:
    00246                 if (bForceIntegerFormat || (!FormatsDX10supported[6]))
    00247                 {
    00248                         *DestFormat     = DXGI_FORMAT_R8G8B8A8_UINT;
    00249                         nConversionType = IL_UNSIGNED_BYTE;
    00250                         ilutFormat      = IL_RGBA;
    00251                 }
    00252                 else
    00253                 {
    00254                         *DestFormat     = DXGI_FORMAT_R32G32B32A32_FLOAT;
    00255                         nConversionType = IL_HALF;
    00256                         ilutFormat      = IL_RGBA;
    00257                 }
    00258                 break;
    00259         }
    00260
    00261         // Images must be in BGRA format.
    00262         if (((ilutCurImage->Format != ilutFormat))
    00263                 || (ilutCurImage->Type != nConversionType))
    00264         {
    00265                 Converted = iConvertImage(ilutCurImage, ilutFormat, nConversionType);
    00266                 if (Converted == NULL)
    00267                         return NULL;
    00268         }
    00269         else
    00270         {
    00271                 Converted = ilutCurImage;
    00272         }
    00273
    00274         // Images must have their origin in the upper left.
    00275         if (Converted->Origin != IL_ORIGIN_UPPER_LEFT)
    00276         {
    00277                 CurImage = ilutCurImage;
    00278                 ilSetCurImage(Converted);
    00279                 iluFlipImage();
    00280                 ilSetCurImage(CurImage);
    00281         }
    00282
    00283         // Images must have powers-of-2 dimensions.
    00284         if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
    00285                 ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
    00286                 ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
    00287                         Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
    00288                                                 ilNextPower2(ilutCurImage->Height), 1);  //@TODO: 1 should be ilNextPower2(ilutCurImage->Depth)
    00289                         if (Converted != ilutCurImage) {
    00290                                 ilCloseImage(Converted);
    00291                         }
    00292                         if (Scaled == NULL) {
    00293                                 return NULL;
    00294                         }
    00295                         Converted = Scaled;
    00296         }
    00297
    00298         return Converted;
    00299 }
    00300
    00301
    00302 #endif//ILUT_USE_DIRECTX10