Platform_Win32_3D_D3D9_Texture.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D_D3D9_Texture.cpp *** 00002 00003 #define WIN32_LEAN_AND_MEAN 00004 #define VC_EXTRALEAN 00005 #include <d3d9.h> 00006 00007 #include "Platform_Win32_3D_D3D9_Texture.h" 00008 #include "Debug.h" 00009 #include "Asset.h" 00010 #include "StandardLibrary.h" 00011 #include "ColorHelper.h" 00012 00013 00014 //*** Constructor *** 00015 00016 Platform_Win32_3D_D3D9_Texture::Platform_Win32_3D_D3D9_Texture(IDirect3DDevice9* device, int width, int height, void* data): 00017 device_(device), 00018 d3dTexture_(0), 00019 width_(width), 00020 height_(height), 00021 depth_(0), 00022 mipLevels_(0), 00023 format_(D3DFMT_UNKNOWN) 00024 { 00025 int count=0; 00026 int wc=width_; 00027 int hc=height_; 00028 while(wc>0 && hc>0) 00029 { 00030 count++; 00031 wc>>=1; 00032 hc>>=1; 00033 } 00034 00035 device_->CreateTexture(width_,height_,count,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&d3dTexture_,0); 00036 int mipLevelCount=d3dTexture_->GetLevelCount(); 00037 int w=width_; 00038 int h=height_; 00039 00040 unsigned int* temp=new unsigned int[width_*height_]; 00041 MemCpy(temp,data,width_*height_*sizeof(unsigned int)); 00042 00043 for (int i=0; i<mipLevelCount; i++) 00044 { 00045 D3DLOCKED_RECT rect; 00046 d3dTexture_->LockRect(i,&rect,0,D3DLOCK_DISCARD); 00047 unsigned char* dest=static_cast<unsigned char*>(rect.pBits); 00048 00049 if (rect.Pitch==(int)sizeof(unsigned int)*w) 00050 { 00051 MemCpy(dest,temp,sizeof(unsigned int)*w*h); 00052 } 00053 else 00054 { 00055 unsigned char* dat=reinterpret_cast<unsigned char*>(temp); 00056 for (int y=0; y<h; y++) 00057 { 00058 MemCpy(dest,dat,sizeof(unsigned int)*w); 00059 dat+=w; 00060 dest+=rect.Pitch; 00061 } 00062 } 00063 d3dTexture_->UnlockRect(i); 00064 00065 if (i+1<mipLevelCount) 00066 { 00067 w/=2; 00068 h/=2; 00069 for (int y=0; y<h; y++) 00070 { 00071 for (int x=0; x<w; x++) 00072 { 00073 unsigned int c0=temp[x*2+0+(y*2+0)*(w*2)]; 00074 unsigned int c1=temp[x*2+1+(y*2+0)*(w*2)]; 00075 unsigned int c2=temp[x*2+0+(y*2+1)*(w*2)]; 00076 unsigned int c3=temp[x*2+1+(y*2+1)*(w*2)]; 00077 unsigned int a=(((c0 & 0xff000000)>>24)+((c1 & 0xff000000)>>24)+((c2 & 0xff000000)>>24)+((c3 & 0xff000000)>>24))>>2; 00078 unsigned int r=(((c0 & 0x00ff0000)>>16)+((c1 & 0x00ff0000)>>16)+((c2 & 0x00ff0000)>>16)+((c3 & 0x00ff0000)>>16))>>2; 00079 unsigned int g=(((c0 & 0x0000ff00)>> 8)+((c1 & 0x0000ff00)>> 8)+((c2 & 0x0000ff00)>> 8)+((c3 & 0x0000ff00)>> 8))>>2; 00080 unsigned int b=(((c0 & 0x000000ff) )+((c1 & 0x000000ff) )+((c2 & 0x000000ff) )+((c3 & 0x000000ff) ))>>2; 00081 temp[x+y*w]=RGBA32((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)a); 00082 } 00083 } 00084 } 00085 } 00086 00087 delete[] temp; 00088 } 00089 00090 00091 //*** Constructor *** 00092 00093 Platform_Win32_3D_D3D9_Texture::Platform_Win32_3D_D3D9_Texture(IDirect3DDevice9* device, void* data, int size): 00094 device_(device), 00095 d3dTexture_(0), 00096 lockedData_(0), 00097 lockedSurface_(0), 00098 lockedPitch_(0), 00099 width_(0), 00100 height_(0), 00101 depth_(0), 00102 mipLevels_(0), 00103 format_(D3DFMT_UNKNOWN) 00104 { 00105 Assert(false,"Not implemented yet"); 00106 /* D3DXIMAGE_INFO info; 00107 HRESULT hr=D3DXCreateTextureFromFileInMemoryEx( 00108 device_, 00109 data, 00110 size, 00111 D3DX_DEFAULT, 00112 D3DX_DEFAULT, 00113 D3DX_DEFAULT, 00114 0, 00115 D3DFMT_FROM_FILE, 00116 D3DPOOL_MANAGED, 00117 D3DX_FILTER_BOX, 00118 D3DX_FILTER_BOX, 00119 0, 00120 &info, 00121 0, 00122 &d3dTexture_ 00123 ); 00124 Assert(SUCCEEDED(hr),"Failed to create texture"); 00125 */ } 00126 00127 00128 //*** Destructor *** 00129 00130 Platform_Win32_3D_D3D9_Texture::~Platform_Win32_3D_D3D9_Texture() 00131 { 00132 if (d3dTexture_) 00133 { 00134 d3dTexture_->Release(); 00135 } 00136 } 00137 00138 00139 //*** Bind *** 00140 00141 void Platform_Win32_3D_D3D9_Texture::Bind(int stage) 00142 { 00143 device_->SetTexture(stage,d3dTexture_); 00144 } 00145 00146 00147 //*** HasAlphaChannel *** 00148 00149 bool Platform_Win32_3D_D3D9_Texture::HasAlphaChannel() 00150 { 00151 if (format_==D3DFMT_A8R8G8B8) 00152 { 00153 return true; 00154 } 00155 00156 if (format_==D3DFMT_DXT5) 00157 { 00158 return true; 00159 } 00160 00161 return false; 00162 } 00163 00164 00165 //*** GetWidth *** 00166 00167 int Platform_Win32_3D_D3D9_Texture::GetWidth() 00168 { 00169 return width_; 00170 } 00171 00172 00173 //*** GetHeight *** 00174 00175 int Platform_Win32_3D_D3D9_Texture::GetHeight() 00176 { 00177 return height_; 00178 } 00179 00180 00181 //*** Lock *** 00182 00183 void Platform_Win32_3D_D3D9_Texture::Lock() 00184 { 00185 Assert(!lockedData_,"Texture is already locked"); 00186 if (lockedData_) 00187 { 00188 return; 00189 } 00190 00191 HRESULT hr=device_->CreateOffscreenPlainSurface(width_,height_,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&lockedSurface_,0); 00192 Assert(SUCCEEDED(hr),"Failed to create plain surface"); 00193 IDirect3DSurface9* thisSurface=0; 00194 hr=d3dTexture_->GetSurfaceLevel(0,&thisSurface); 00195 Assert(SUCCEEDED(hr),"Failed to get surface"); 00196 // hr=D3DXLoadSurfaceFromSurface(lockedSurface_,0,0,thisSurface,0,0,D3DX_FILTER_NONE,0); 00197 Assert(SUCCEEDED(hr),"Failed to copy"); 00198 thisSurface->Release(); 00199 00200 D3DLOCKED_RECT lockedRect; 00201 00202 lockedSurface_->LockRect(&lockedRect,0,D3DLOCK_READONLY); 00203 lockedData_=static_cast<unsigned char*>(lockedRect.pBits); 00204 lockedPitch_=lockedRect.Pitch; 00205 00206 } 00207 00208 00209 //*** Unlock *** 00210 00211 void Platform_Win32_3D_D3D9_Texture::Unlock() 00212 { 00213 Assert(lockedData_,"Texture is not locked"); 00214 if (!lockedData_) 00215 { 00216 return; 00217 } 00218 00219 d3dTexture_->UnlockRect(0); 00220 lockedData_=0; 00221 lockedPitch_=0; 00222 lockedSurface_->Release(); 00223 lockedSurface_=0; 00224 } 00225 00226 00227 //*** GetTexelData *** 00228 00229 unsigned char* Platform_Win32_3D_D3D9_Texture::GetTexelData() 00230 { 00231 Assert(lockedData_,"Texture is not locked"); 00232 if (!lockedData_) 00233 { 00234 return 0; 00235 } 00236 00237 return lockedData_; 00238 } 00239 00240 00241 //*** GetPitch *** 00242 00243 int Platform_Win32_3D_D3D9_Texture::GetPitch() 00244 { 00245 Assert(lockedData_,"Texture is not locked"); 00246 if (!lockedData_) 00247 { 00248 return 0; 00249 } 00250 00251 return lockedPitch_; 00252 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
