| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org/
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #include "OgreD3D9Texture.h"
- #include "OgreD3D9HardwarePixelBuffer.h"
- #include "OgreException.h"
- #include "OgreStringConverter.h"
- #include "OgreBitwise.h"
- #include "OgreD3D9Mappings.h"
- #include "OgreD3D9RenderSystem.h"
- #include "OgreD3D9TextureManager.h"
- #include "OgreD3D9Device.h"
- #include "OgreD3D9DeviceManager.h"
- #include "OgreD3D9ResourceManager.h"
- #include "CmRenderSystemManager.h"
- namespace Ogre
- {
- /****************************************************************************************/
- D3D9Texture::D3D9Texture()
- :Texture(),
- mD3DPool(D3DPOOL_MANAGED),
- mDynamicTextures(false),
- mHwGammaReadSupported(false),
- mHwGammaWriteSupported(false),
- mFSAAType(D3DMULTISAMPLE_NONE),
- mFSAAQuality(0)
- {
-
- }
- /****************************************************************************************/
- D3D9Texture::~D3D9Texture()
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
-
- // have to call this here reather than in Resource destructor
- // since calling virtual methods in base destructors causes crash
- freeInternalResources();
- // Free memory allocated per device.
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
- while (it != mMapDeviceToTextureResources.end())
- {
- TextureResources* textureResource = it->second;
-
- SAFE_DELETE(textureResource);
- ++it;
- }
- mMapDeviceToTextureResources.clear();
- mSurfaceList.clear();
- }
- /****************************************************************************************/
- void D3D9Texture::copyToTexture(TexturePtr& target)
- {
- // check if this & target are the same format and type
- // blitting from or to cube textures is not supported yet
- if (target->getUsage() != getUsage() ||
- target->getTextureType() != getTextureType())
- {
- OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
- "Src. and dest. textures must be of same type and must have the same usage !!!",
- "D3D9Texture::copyToTexture" );
- }
- HRESULT hr;
- D3D9Texture *other;
- // get the target
- other = reinterpret_cast< D3D9Texture * >( target.get() );
- // target rectangle (whole surface)
- RECT dstRC = {0, 0, static_cast<LONG>(other->getWidth()), static_cast<LONG>(other->getHeight())};
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
- while (it != mMapDeviceToTextureResources.end())
- {
- TextureResources* srcTextureResource = it->second;
- TextureResources* dstTextureResource = other->getTextureResources(it->first);
-
- // do it plain for normal texture
- if (getTextureType() == TEX_TYPE_2D &&
- srcTextureResource->pNormTex &&
- dstTextureResource->pNormTex)
- {
- // get our source surface
- IDirect3DSurface9 *pSrcSurface = 0;
- if( FAILED( hr = srcTextureResource->pNormTex->GetSurfaceLevel(0, &pSrcSurface) ) )
- {
- String msg = DXGetErrorDescription(hr);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // get our target surface
- IDirect3DSurface9 *pDstSurface = 0;
- if( FAILED( hr = dstTextureResource->pNormTex->GetSurfaceLevel(0, &pDstSurface) ) )
- {
- String msg = DXGetErrorDescription(hr);
- SAFE_RELEASE(pSrcSurface);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // do the blit, it's called StretchRect in D3D9 :)
- if( FAILED( hr = it->first->StretchRect( pSrcSurface, NULL, pDstSurface, &dstRC, D3DTEXF_NONE) ) )
- {
- String msg = DXGetErrorDescription(hr);
- SAFE_RELEASE(pSrcSurface);
- SAFE_RELEASE(pDstSurface);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // release temp. surfaces
- SAFE_RELEASE(pSrcSurface);
- SAFE_RELEASE(pDstSurface);
- }
- else if (getTextureType() == TEX_TYPE_CUBE_MAP &&
- srcTextureResource->pCubeTex &&
- dstTextureResource->pCubeTex)
- {
- // blit to 6 cube faces
- for (size_t face = 0; face < 6; face++)
- {
- // get our source surface
- IDirect3DSurface9 *pSrcSurface = 0;
- if( FAILED( hr =srcTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, 0, &pSrcSurface) ) )
- {
- String msg = DXGetErrorDescription(hr);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // get our target surface
- IDirect3DSurface9 *pDstSurface = 0;
- if( FAILED( hr = dstTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, 0, &pDstSurface) ) )
- {
- String msg = DXGetErrorDescription(hr);
- SAFE_RELEASE(pSrcSurface);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // do the blit, it's called StretchRect in D3D9 :)
- if( FAILED( hr = it->first->StretchRect( pSrcSurface, NULL, pDstSurface, &dstRC, D3DTEXF_NONE) ) )
- {
- String msg = DXGetErrorDescription(hr);
- SAFE_RELEASE(pSrcSurface);
- SAFE_RELEASE(pDstSurface);
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn't blit : " + msg, "D3D9Texture::copyToTexture" );
- }
- // release temp. surfaces
- SAFE_RELEASE(pSrcSurface);
- SAFE_RELEASE(pDstSurface);
- }
- }
- else
- {
- OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED,
- "Copy to texture is implemented only for 2D and cube textures !!!",
- "D3D9Texture::copyToTexture" );
- }
- ++it;
- }
- }
- /****************************************************************************************/
- void D3D9Texture::loadImpl()
- {
- if (!mInternalResourcesCreated)
- {
- // NB: Need to initialise pool to some value other than D3DPOOL_DEFAULT,
- // otherwise, if the texture loading failed, it might re-create as empty
- // texture when device lost/restore. The actual pool will determine later.
- mD3DPool = D3DPOOL_MANAGED;
- }
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- for (uint i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
- {
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
- loadImpl(d3d9Device);
- }
- }
- /****************************************************************************************/
- void D3D9Texture::loadImpl(IDirect3DDevice9* d3d9Device)
- {
- if (mUsage & TU_RENDERTARGET)
- {
- createInternalResourcesImpl(d3d9Device);
- return;
- }
- // TODO PORT - Loading tex from streams not supported
- //// Make sure streams prepared.
- //if (mLoadedStreams.isNull())
- //{
- // prepareImpl();
- //}
- //
- //// Set reading positions of loaded streams to the beginning.
- //for (uint i = 0; i < mLoadedStreams->size(); ++i)
- //{
- // MemoryDataStreamPtr curDataStream = (*mLoadedStreams)[i];
- // curDataStream->seek(0);
- //}
- //// only copy is on the stack so well-behaved if exception thrown
- //LoadedStreams loadedStreams = mLoadedStreams;
-
- //// load based on tex.type
- //switch (getTextureType())
- //{
- //case TEX_TYPE_1D:
- //case TEX_TYPE_2D:
- // _loadNormTex(d3d9Device, loadedStreams);
- // break;
- //case TEX_TYPE_3D:
- // _loadVolumeTex(d3d9Device, loadedStreams);
- // break;
- //case TEX_TYPE_CUBE_MAP:
- // _loadCubeTex(d3d9Device, loadedStreams);
- // break;
- //default:
- // OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Unknown texture type", "D3D9Texture::loadImpl" );
- //}
- }
- /****************************************************************************************/
- void D3D9Texture::prepareImpl()
- {
- if (mUsage & TU_RENDERTARGET)
- {
- return;
- }
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
-
- // TODO PORT - Loading tex from streams not supported
- // LoadedStreams loadedStreams;
- //// prepare load based on tex.type
- //switch (getTextureType())
- //{
- //case TEX_TYPE_1D:
- //case TEX_TYPE_2D:
- // loadedStreams = _prepareNormTex();
- // break;
- //case TEX_TYPE_3D:
- // loadedStreams = _prepareVolumeTex();
- // break;
- //case TEX_TYPE_CUBE_MAP:
- // loadedStreams = _prepareCubeTex();
- // break;
- //default:
- // OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Unknown texture type", "D3D9Texture::prepareImpl" );
- //}
- //mLoadedStreams = loadedStreams;
- }
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- //D3D9Texture::LoadedStreams D3D9Texture::_prepareCubeTex()
- //{
- // assert(getTextureType() == TEX_TYPE_CUBE_MAP);
- // LoadedStreams loadedStreams = LoadedStreams(new vector<MemoryDataStreamPtr>::type());
- // // DDS load?
- // if (getSourceFileType() == "dds")
- // {
- // // find & load resource data
- // DataStreamPtr dstream =
- // ResourceGroupManager::getSingleton().openResource(
- // mName, mGroup, true, this);
- // loadedStreams->push_back(MemoryDataStreamPtr(new MemoryDataStream(dstream)));
- // }
- // else
- // {
- // // Load from 6 separate files
- // // Use OGRE its own codecs
- // String baseName, ext;
- // size_t pos = mName.find_last_of(".");
- // baseName = mName.substr(0, pos);
- // if ( pos != String::npos )
- // ext = mName.substr(pos+1);
- // static const String suffixes[6] = {"_rt", "_lf", "_up", "_dn", "_fr", "_bk"};
- // for(size_t i = 0; i < 6; i++)
- // {
- // String fullName = baseName + suffixes[i];
- // if (!ext.empty())
- // fullName = fullName + "." + ext;
- // // find & load resource data intro stream to allow resource
- // // group changes if required
- // DataStreamPtr dstream =
- // ResourceGroupManager::getSingleton().openResource(
- // fullName, mGroup, true, this);
- // loadedStreams->push_back(MemoryDataStreamPtr(new MemoryDataStream(dstream)));
- // }
- // }
- // return loadedStreams;
- //}
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- //D3D9Texture::LoadedStreams D3D9Texture::_prepareVolumeTex()
- //{
- // assert(getTextureType() == TEX_TYPE_3D);
- // // find & load resource data
- // DataStreamPtr dstream =
- // ResourceGroupManager::getSingleton().openResource(
- // mName, mGroup, true, this);
- // LoadedStreams loadedStreams = LoadedStreams(new vector<MemoryDataStreamPtr>::type());
- // loadedStreams->push_back(MemoryDataStreamPtr(new MemoryDataStream(dstream)));
- // return loadedStreams;
- // }
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- //D3D9Texture::LoadedStreams D3D9Texture::_prepareNormTex()
- //{
- // assert(getTextureType() == TEX_TYPE_1D || getTextureType() == TEX_TYPE_2D);
- // // find & load resource data
- // DataStreamPtr dstream =
- // ResourceGroupManager::getSingleton().openResource(
- // mName, mGroup, true, this);
- // LoadedStreams loadedStreams = LoadedStreams(new vector<MemoryDataStreamPtr>::type());
- // loadedStreams->push_back(MemoryDataStreamPtr(new MemoryDataStream(dstream)));
- // return loadedStreams;
- //}
- /****************************************************************************************/
- void D3D9Texture::unprepareImpl()
- {
- if (mUsage & TU_RENDERTARGET)
- {
- return;
- }
- }
- /****************************************************************************************/
- void D3D9Texture::postLoadImpl(void)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- // TODO PORT - Loading tex from streams not supported
- //mLoadedStreams.setNull();
- }
- /****************************************************************************************/
- void D3D9Texture::freeInternalResources(void)
- {
- freeInternalResourcesImpl();
- }
- /****************************************************************************************/
- void D3D9Texture::freeInternalResourcesImpl()
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
- while (it != mMapDeviceToTextureResources.end())
- {
- TextureResources* textureResource = it->second;
- freeTextureResources(it->first, textureResource);
- ++it;
- }
- }
- /****************************************************************************************/
- D3D9Texture::TextureResources* D3D9Texture::getTextureResources(IDirect3DDevice9* d3d9Device)
- {
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
- if (it == mMapDeviceToTextureResources.end())
- return NULL;
-
- return it->second;
- }
- /****************************************************************************************/
- D3D9Texture::TextureResources* D3D9Texture::allocateTextureResources(IDirect3DDevice9* d3d9Device)
- {
- assert(mMapDeviceToTextureResources.find(d3d9Device) == mMapDeviceToTextureResources.end());
- TextureResources* textureResources = new TextureResources;
- textureResources->pNormTex = NULL;
- textureResources->pCubeTex = NULL;
- textureResources->pVolumeTex = NULL;
- textureResources->pBaseTex = NULL;
- textureResources->pFSAASurface = NULL;
- mMapDeviceToTextureResources[d3d9Device] = textureResources;
- return textureResources;
- }
- /****************************************************************************************/
- void D3D9Texture::createTextureResources(IDirect3DDevice9* d3d9Device)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
-
- prepareImpl();
- loadImpl(d3d9Device);
- postLoadImpl();
- }
- /****************************************************************************************/
- void D3D9Texture::freeTextureResources(IDirect3DDevice9* d3d9Device, D3D9Texture::TextureResources* textureResources)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- // Release surfaces from each mip level.
- for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
- {
- D3D9HardwarePixelBuffer* pixelBuffer = static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[i].get());
- pixelBuffer->releaseSurfaces(d3d9Device);
- }
-
- // Release the rest of the resources.
- SAFE_RELEASE(textureResources->pBaseTex);
- SAFE_RELEASE(textureResources->pNormTex);
- SAFE_RELEASE(textureResources->pCubeTex);
- SAFE_RELEASE(textureResources->pVolumeTex);
- SAFE_RELEASE(textureResources->pFSAASurface);
- }
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- // void D3D9Texture::_loadCubeTex(IDirect3DDevice9* d3d9Device, const D3D9Texture::LoadedStreams &loadedStreams)
- // {
-
- //assert(getTextureType() == TEX_TYPE_CUBE_MAP);
- // // DDS load?
- //if (getSourceFileType() == "dds")
- //{
- // // find & load resource data
- // assert(loadedStreams->size()==1);
- // DWORD usage = 0;
- // UINT numMips = (mNumRequestedMipmaps == MIP_UNLIMITED) ?
- // D3DX_DEFAULT : mNumRequestedMipmaps + 1;
- //
- // D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- // const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- // // check if mip map volume textures are supported
- // if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
- // {
- // // no mip map support for this kind of textures :(
- // mNumMipmaps = 0;
- // numMips = 1;
- // }
- // // Determine D3D pool to use
- // D3DPOOL pool;
- // if (useDefaultPool())
- // {
- // pool = D3DPOOL_DEFAULT;
- // }
- // else
- // {
- // pool = D3DPOOL_MANAGED;
- // }
- //
- // TextureResources* textureResources;
- //
- // // Get or create new texture resources structure.
- // textureResources = getTextureResources(d3d9Device);
- // if (textureResources != NULL)
- // freeTextureResources(d3d9Device, textureResources);
- // else
- // textureResources = allocateTextureResources(d3d9Device);
- // HRESULT hr;
- // hr = D3DXCreateCubeTextureFromFileInMemoryEx(
- // d3d9Device,
- // (*loadedStreams)[0]->getPtr(),
- // static_cast<UINT>((*loadedStreams)[0]->size()),
- // D3DX_DEFAULT, // dims (square)
- // numMips,
- // usage,
- // D3DFMT_UNKNOWN,
- // pool,
- // D3DX_DEFAULT,
- // D3DX_DEFAULT,
- // 0, // colour key
- // NULL, // src box
- // NULL, // palette
- // &textureResources->pCubeTex);
- // if (FAILED(hr))
- // {
- // freeInternalResources();
- // OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't create cube texture: " + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadCubeTex" );
- // }
- // hr = textureResources->pCubeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
- // if (FAILED(hr))
- // {
- // freeInternalResources();
- // OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadCubeTex" );
- // }
- // D3DSURFACE_DESC texDesc;
- // textureResources->pCubeTex->GetLevelDesc(0, &texDesc);
- // mD3DPool = texDesc.Pool;
- // // set src and dest attributes to the same, we can't know
- // _setSrcAttributes(texDesc.Width, texDesc.Height, 1, D3D9Mappings::_getPF(texDesc.Format));
- // _setFinalAttributes(d3d9Device, textureResources,
- // texDesc.Width, texDesc.Height, 1, D3D9Mappings::_getPF(texDesc.Format));
- // if( mHwGamma )
- // {
- // mHwGammaReadSupported = _canUseHardwareGammaCorrection( d3d9Device, texDesc.Usage,
- // D3DRTYPE_CUBETEXTURE,
- // texDesc.Format, false);
- // }
- // mInternalResourcesCreated = true;
- // }
- // else
- // {
- // assert(loadedStreams->size()==6);
- // String ext;
- // size_t pos = mName.find_last_of(".");
- // if ( pos != String::npos )
- // ext = mName.substr(pos+1);
- // vector<Image>::type images(6);
- // ConstImagePtrList imagePtrs;
- // for(size_t i = 0; i < 6; i++)
- // {
- // DataStreamPtr stream((*loadedStreams)[i]);
- // images[i].load(stream, ext);
- // imagePtrs.push_back(&images[i]);
- // }
- // _loadImages( imagePtrs );
- // }
- // }
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- // void D3D9Texture::_loadVolumeTex(IDirect3DDevice9* d3d9Device, const D3D9Texture::LoadedStreams &loadedStreams)
- // {
- // TODO PORT - Loading tex from streams not supported
- //assert(getTextureType() == TEX_TYPE_3D);
- //// DDS load?
- //if (getSourceFileType() == "dds")
- //{
- // // find & load resource data
- // assert(loadedStreams->size()==1);
-
- // DWORD usage = 0;
- // UINT numMips = (mNumRequestedMipmaps == MIP_UNLIMITED) ?
- // D3DX_DEFAULT : mNumRequestedMipmaps + 1;
- //
- // D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- // const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- // // check if mip map volume textures are supported
- // if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
- // {
- // // no mip map support for this kind of textures :(
- // mNumMipmaps = 0;
- // numMips = 1;
- // }
- // // Determine D3D pool to use
- // D3DPOOL pool;
- // if (useDefaultPool())
- // {
- // pool = D3DPOOL_DEFAULT;
- // }
- // else
- // {
- // pool = D3DPOOL_MANAGED;
- // }
- //
- // TextureResources* textureResources;
- //
- // // Get or create new texture resources structure.
- // textureResources = getTextureResources(d3d9Device);
- // if (textureResources != NULL)
- // freeTextureResources(d3d9Device, textureResources);
- // else
- // textureResources = allocateTextureResources(d3d9Device);
- // HRESULT hr;
- // hr = D3DXCreateVolumeTextureFromFileInMemoryEx(
- // d3d9Device,
- // (*loadedStreams)[0]->getPtr(),
- // static_cast<UINT>((*loadedStreams)[0]->size()),
- // D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, // dims
- // numMips,
- // usage,
- // D3DFMT_UNKNOWN,
- // pool,
- // D3DX_DEFAULT,
- // D3DX_DEFAULT,
- // 0, // colour key
- // NULL, // src box
- // NULL, // palette
- // &textureResources->pVolumeTex);
-
- // if (FAILED(hr))
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Unable to load volume texture from: " + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadVolumeTex");
- // }
-
- // hr = textureResources->pVolumeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
-
- // if (FAILED(hr))
- // {
- // freeInternalResources();
- // OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadVolumeTex" );
- // }
-
- // D3DVOLUME_DESC texDesc;
- // hr = textureResources->pVolumeTex->GetLevelDesc(0, &texDesc);
- // mD3DPool = texDesc.Pool;
- // // set src and dest attributes to the same, we can't know
- // _setSrcAttributes(texDesc.Width, texDesc.Height, texDesc.Depth, D3D9Mappings::_getPF(texDesc.Format));
- // _setFinalAttributes(d3d9Device, textureResources,
- // texDesc.Width, texDesc.Height, texDesc.Depth, D3D9Mappings::_getPF(texDesc.Format));
- // if( mHwGamma )
- // {
- // mHwGammaReadSupported = _canUseHardwareGammaCorrection( d3d9Device, texDesc.Usage,
- // D3DRTYPE_VOLUMETEXTURE,
- // texDesc.Format, false);
- // }
- // mInternalResourcesCreated = true;
- // }
- //else
- //{
- // Image img;
- // assert(loadedStreams->size()==1);
- // size_t pos = mName.find_last_of(".");
- // String ext;
- // if ( pos != String::npos )
- // ext = mName.substr(pos+1);
-
- // DataStreamPtr stream((*loadedStreams)[0]);
- // img.load(stream, ext);
- // if (img.getHeight() == 0)
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Image height == 0 in " + getName(),
- // "D3D9Texture::_loadVolumeTex");
- // }
- // if (img.getWidth() == 0)
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Image width == 0 in " + getName(),
- // "D3D9Texture::_loadVolumeTex");
- // }
- // if (img.getDepth() == 0)
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Image depth == 0 in " + getName(),
- // "D3D9Texture::_loadVolumeTex");
- // }
- // // Call internal _loadImages, not loadImage since that's external and
- // // will determine load status etc again
- // ConstImagePtrList imagePtrs;
- // imagePtrs.push_back(&img);
- // _loadImages( imagePtrs );
- //}
- // }
- /****************************************************************************************/
- // TODO PORT - Loading tex from streams not supported
- // void D3D9Texture::_loadNormTex(IDirect3DDevice9* d3d9Device, const D3D9Texture::LoadedStreams &loadedStreams)
- // {
- // TODO PORT - Loading tex from streams not supported
- //assert(getTextureType() == TEX_TYPE_1D || getTextureType() == TEX_TYPE_2D);
- //// DDS load?
- //if (getSourceFileType() == "dds")
- //{
- // // Use D3DX
- // assert(loadedStreams->size()==1);
-
- // DWORD usage = 0;
- // UINT numMips;
- // if (mNumRequestedMipmaps == MIP_UNLIMITED)
- // numMips = D3DX_DEFAULT;
- // else if (mNumRequestedMipmaps == 0)
- // numMips = D3DX_FROM_FILE;
- // else
- // numMips = static_cast<UINT>(mNumRequestedMipmaps + 1);
- //
- // D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- // const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- // // check if mip map volume textures are supported
- // if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP))
- // {
- // // no mip map support for this kind of textures :(
- // mNumMipmaps = 0;
- // numMips = 1;
- // }
- // // Determine D3D pool to use
- // D3DPOOL pool;
- // if (useDefaultPool())
- // {
- // pool = D3DPOOL_DEFAULT;
- // }
- // else
- // {
- // pool = D3DPOOL_MANAGED;
- // }
- //
- // TextureResources* textureResources;
- //
- // // Get or create new texture resources structure.
- // textureResources = getTextureResources(d3d9Device);
- // if (textureResources != NULL)
- // freeTextureResources(d3d9Device, textureResources);
- // else
- // textureResources = allocateTextureResources(d3d9Device);
- // HRESULT hr;
- // hr = D3DXCreateTextureFromFileInMemoryEx(
- // d3d9Device,
- // (*loadedStreams)[0]->getPtr(),
- // static_cast<UINT>((*loadedStreams)[0]->size()),
- // D3DX_DEFAULT, D3DX_DEFAULT, // dims
- // numMips,
- // usage,
- // D3DFMT_UNKNOWN,
- // pool,
- // D3DX_DEFAULT,
- // D3DX_DEFAULT,
- // 0, // colour key
- // NULL, // src box
- // NULL, // palette
- // &textureResources->pNormTex);
-
- // if (FAILED(hr))
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Unable to load texture from :" + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadNormTex");
- // }
-
- // hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
-
- // if (FAILED(hr))
- // {
- // freeInternalResources();
- // OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- // "D3D9Texture::_loadNormTex" );
- // }
-
- // D3DSURFACE_DESC texDesc;
- // textureResources->pNormTex->GetLevelDesc(0, &texDesc);
- // mD3DPool = texDesc.Pool;
- // // set src and dest attributes to the same, we can't know
- // _setSrcAttributes(texDesc.Width, texDesc.Height, 1, D3D9Mappings::_getPF(texDesc.Format));
- // _setFinalAttributes(d3d9Device, textureResources,
- // texDesc.Width, texDesc.Height, 1, D3D9Mappings::_getPF(texDesc.Format));
- // if( mHwGamma )
- // {
- // mHwGammaReadSupported = _canUseHardwareGammaCorrection( d3d9Device, texDesc.Usage,
- // D3DRTYPE_TEXTURE,
- // texDesc.Format, false);
- // }
- // mInternalResourcesCreated = true;
- // }
- //else
- //{
- // Image img;
- // // find & load resource data intro stream to allow resource
- // // group changes if required
- // assert(loadedStreams->size()==1);
-
- // size_t pos = mName.find_last_of(".");
- // String ext;
- // if ( pos != String::npos )
- // ext = mName.substr(pos+1);
- // DataStreamPtr stream((*loadedStreams)[0]);
- // img.load(stream, ext);
- // if (img.getHeight() == 0)
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Image height == 0 in " + getName(),
- // "D3D9Texture::_loadNormTex");
- // }
- // if (img.getWidth() == 0)
- // {
- // OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- // "Image width == 0 in " + getName(),
- // "D3D9Texture::_loadNormTex");
- // }
- // // Call internal _loadImages, not loadImage since that's external and
- // // will determine load status etc again
- // ConstImagePtrList imagePtrs;
- // imagePtrs.push_back(&img);
- // _loadImages( imagePtrs );
- //}
- // }
- /****************************************************************************************/
- size_t D3D9Texture::calculateSize(void) const
- {
- size_t instanceSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
- return instanceSize * mMapDeviceToTextureResources.size();
- }
- /****************************************************************************************/
- void D3D9Texture::createInternalResources(void)
- {
- createInternalResourcesImpl();
- }
- /****************************************************************************************/
- void D3D9Texture::determinePool()
- {
- if (useDefaultPool())
- {
- mD3DPool = D3DPOOL_DEFAULT;
- }
- else
- {
- mD3DPool = D3DPOOL_MANAGED;
- }
- }
- /****************************************************************************************/
- void D3D9Texture::createInternalResourcesImpl(void)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- for (uint i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
- {
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
- createInternalResourcesImpl(d3d9Device);
- }
- }
- /****************************************************************************************/
- void D3D9Texture::createInternalResourcesImpl(IDirect3DDevice9* d3d9Device)
- {
- TextureResources* textureResources;
- // Check if resources already exist.
- textureResources = getTextureResources(d3d9Device);
- if (textureResources != NULL && textureResources->pBaseTex != NULL)
- return;
-
- // If mSrcWidth and mSrcHeight are zero, the requested extents have probably been set
- // through setWidth and setHeight, which set mWidth and mHeight. Take those values.
- if(mSrcWidth == 0 || mSrcHeight == 0) {
- mSrcWidth = mWidth;
- mSrcHeight = mHeight;
- }
- // load based on tex.type
- switch (getTextureType())
- {
- case TEX_TYPE_1D:
- case TEX_TYPE_2D:
- _createNormTex(d3d9Device);
- break;
- case TEX_TYPE_CUBE_MAP:
- _createCubeTex(d3d9Device);
- break;
- case TEX_TYPE_3D:
- _createVolumeTex(d3d9Device);
- break;
- default:
- freeInternalResources();
- OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Unknown texture type", "D3D9Texture::createInternalResources" );
- }
- }
- /****************************************************************************************/
- void D3D9Texture::_createNormTex(IDirect3DDevice9* d3d9Device)
- {
- // we must have those defined here
- assert(mSrcWidth > 0 || mSrcHeight > 0);
- // determine wich D3D9 pixel format we'll use
- HRESULT hr;
- D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
- // let's D3DX check the corrected pixel format
- hr = D3DXCheckTextureRequirements(d3d9Device, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
- // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
- DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
- UINT numMips = (mNumRequestedMipmaps == MIP_UNLIMITED) ?
- D3DX_DEFAULT : mNumRequestedMipmaps + 1;
- // Check dynamic textures
- if (mUsage & TU_DYNAMIC)
- {
- if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF))
- {
- usage |= D3DUSAGE_DYNAMIC;
- mDynamicTextures = true;
- }
- else
- {
- mDynamicTextures = false;
- }
- }
- // Check sRGB support
- if (mHwGamma)
- {
- mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF, false);
- if (mUsage & TU_RENDERTARGET)
- mHwGammaWriteSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF, true);
- }
- // Check FSAA level
- if (mUsage & TU_RENDERTARGET)
- {
- D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
- rsys->determineFSAASettings(d3d9Device, mFSAA, mFSAAHint, d3dPF, false,
- &mFSAAType, &mFSAAQuality);
- }
- else
- {
- mFSAAType = D3DMULTISAMPLE_NONE;
- mFSAAQuality = 0;
- }
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- // check if mip maps are supported on hardware
- mMipmapsHardwareGenerated = false;
- if (rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP)
- {
- if (mUsage & TU_AUTOMIPMAP && mNumRequestedMipmaps != 0)
- {
- // use auto.gen. if available, and if desired
- mMipmapsHardwareGenerated = _canAutoGenMipmaps(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF);
- if (mMipmapsHardwareGenerated)
- {
- usage |= D3DUSAGE_AUTOGENMIPMAP;
- numMips = 0;
- }
- }
- }
- else
- {
- // no mip map support for this kind of textures :(
- mNumMipmaps = 0;
- numMips = 1;
- }
- // derive the pool to use
- determinePool();
- TextureResources* textureResources;
-
- // Get or create new texture resources structure.
- textureResources = getTextureResources(d3d9Device);
- if (textureResources != NULL)
- freeTextureResources(d3d9Device, textureResources);
- else
- textureResources = allocateTextureResources(d3d9Device);
- // create the texture
- hr = D3DXCreateTexture(
- d3d9Device, // device
- static_cast<UINT>(mSrcWidth), // width
- static_cast<UINT>(mSrcHeight), // height
- numMips, // number of mip map levels
- usage, // usage
- d3dPF, // pixel format
- mD3DPool,
- &textureResources->pNormTex); // data pointer
- // check result and except if failed
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error creating texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createNormTex" );
- }
-
- // set the base texture we'll use in the render system
- hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createNormTex" );
- }
-
- // set final tex. attributes from tex. description
- // they may differ from the source image !!!
- D3DSURFACE_DESC desc;
- hr = textureResources->pNormTex->GetLevelDesc(0, &desc);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get texture description: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createNormTex" );
- }
- if (mFSAAType)
- {
- // create AA surface
- HRESULT hr = d3d9Device->CreateRenderTarget(desc.Width, desc.Height, d3dPF,
- mFSAAType,
- mFSAAQuality,
- FALSE, // not lockable
- &textureResources->pFSAASurface, NULL);
- if (FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
- "Unable to create AA render target: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createNormTex");
- }
- }
- _setFinalAttributes(d3d9Device, textureResources,
- desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
-
- // Set best filter type
- if(mMipmapsHardwareGenerated)
- {
- hr = textureResources->pBaseTex->SetAutoGenFilterType(_getBestFilterMethod(d3d9Device));
- if(FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not set best autogen filter type: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createNormTex" );
- }
- }
- }
- /****************************************************************************************/
- void D3D9Texture::_createCubeTex(IDirect3DDevice9* d3d9Device)
- {
- // we must have those defined here
- assert(mSrcWidth > 0 || mSrcHeight > 0);
- // determine wich D3D9 pixel format we'll use
- HRESULT hr;
- D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
- // let's D3DX check the corrected pixel format
- hr = D3DXCheckCubeTextureRequirements(d3d9Device, NULL, NULL, 0, &d3dPF, mD3DPool);
- // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
- DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
- UINT numMips = (mNumRequestedMipmaps == MIP_UNLIMITED) ?
- D3DX_DEFAULT : mNumRequestedMipmaps + 1;
- // Check dynamic textures
- if (mUsage & TU_DYNAMIC)
- {
- if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF))
- {
- usage |= D3DUSAGE_DYNAMIC;
- mDynamicTextures = true;
- }
- else
- {
- mDynamicTextures = false;
- }
- }
- // Check sRGB support
- if (mHwGamma)
- {
- mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, false);
- if (mUsage & TU_RENDERTARGET)
- mHwGammaWriteSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, true);
- }
- // Check FSAA level
- if (mUsage & TU_RENDERTARGET)
- {
- D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
- rsys->determineFSAASettings(d3d9Device, mFSAA, mFSAAHint, d3dPF, false,
- &mFSAAType, &mFSAAQuality);
- }
- else
- {
- mFSAAType = D3DMULTISAMPLE_NONE;
- mFSAAQuality = 0;
- }
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
-
- // check if mip map cube textures are supported
- mMipmapsHardwareGenerated = false;
- if (rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
- {
- if (mUsage & TU_AUTOMIPMAP && mNumRequestedMipmaps != 0)
- {
- // use auto.gen. if available
- mMipmapsHardwareGenerated = _canAutoGenMipmaps(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF);
- if (mMipmapsHardwareGenerated)
- {
- usage |= D3DUSAGE_AUTOGENMIPMAP;
- numMips = 0;
- }
- }
- }
- else
- {
- // no mip map support for this kind of textures :(
- mNumMipmaps = 0;
- numMips = 1;
- }
- // derive the pool to use
- determinePool();
- TextureResources* textureResources;
- // Get or create new texture resources structure.
- textureResources = getTextureResources(d3d9Device);
- if (textureResources != NULL)
- freeTextureResources(d3d9Device, textureResources);
- else
- textureResources = allocateTextureResources(d3d9Device);
- // create the texture
- hr = D3DXCreateCubeTexture(
- d3d9Device, // device
- static_cast<UINT>(mSrcWidth), // dimension
- numMips, // number of mip map levels
- usage, // usage
- d3dPF, // pixel format
- mD3DPool,
- &textureResources->pCubeTex); // data pointer
- // check result and except if failed
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error creating texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex" );
- }
- // set the base texture we'll use in the render system
- hr = textureResources->pCubeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex" );
- }
-
- // set final tex. attributes from tex. description
- // they may differ from the source image !!!
- D3DSURFACE_DESC desc;
- hr = textureResources->pCubeTex->GetLevelDesc(0, &desc);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get texture description: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex" );
- }
- if (mFSAAType)
- {
- // create AA surface
- HRESULT hr = d3d9Device->CreateRenderTarget(desc.Width, desc.Height, d3dPF,
- mFSAAType,
- mFSAAQuality,
- FALSE, // not lockable
- &textureResources->pFSAASurface, NULL);
- if (FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
- "Unable to create AA render target: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex");
- }
- }
- _setFinalAttributes(d3d9Device, textureResources,
- desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
- // Set best filter type
- if(mMipmapsHardwareGenerated)
- {
- hr = textureResources->pBaseTex->SetAutoGenFilterType(_getBestFilterMethod(d3d9Device));
- if(FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not set best autogen filter type: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex" );
- }
- }
- }
- /****************************************************************************************/
- void D3D9Texture::_createVolumeTex(IDirect3DDevice9* d3d9Device)
- {
- // we must have those defined here
- assert(mWidth > 0 && mHeight > 0 && mDepth>0);
- if (mUsage & TU_RENDERTARGET)
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3D9 Volume texture can not be created as render target !!",
- "D3D9Texture::_createVolumeTex" );
- }
- // determine which D3D9 pixel format we'll use
- HRESULT hr;
- D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
- // let's D3DX check the corrected pixel format
- hr = D3DXCheckVolumeTextureRequirements(d3d9Device, NULL, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
- // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
- DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
- UINT numMips = (mNumRequestedMipmaps == MIP_UNLIMITED) ?
- D3DX_DEFAULT : mNumRequestedMipmaps + 1;
- // Check dynamic textures
- if (mUsage & TU_DYNAMIC)
- {
- if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF))
- {
- usage |= D3DUSAGE_DYNAMIC;
- mDynamicTextures = true;
- }
- else
- {
- mDynamicTextures = false;
- }
- }
- // Check sRGB support
- if (mHwGamma)
- {
- mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, false);
- if (mUsage & TU_RENDERTARGET)
- mHwGammaWriteSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, true);
- }
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- // check if mip map volume textures are supported
- mMipmapsHardwareGenerated = false;
- if (rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
- {
- if (mUsage & TU_AUTOMIPMAP && mNumRequestedMipmaps != 0)
- {
- // use auto.gen. if available
- mMipmapsHardwareGenerated = _canAutoGenMipmaps(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF);
- if (mMipmapsHardwareGenerated)
- {
- usage |= D3DUSAGE_AUTOGENMIPMAP;
- numMips = 0;
- }
- }
- }
- else
- {
- // no mip map support for this kind of textures :(
- mNumMipmaps = 0;
- numMips = 1;
- }
- // derive the pool to use
- determinePool();
- TextureResources* textureResources;
- // Get or create new texture resources structure.
- textureResources = getTextureResources(d3d9Device);
- if (textureResources != NULL)
- freeTextureResources(d3d9Device, textureResources);
- else
- textureResources = allocateTextureResources(d3d9Device);
- // create the texture
- hr = D3DXCreateVolumeTexture(
- d3d9Device, // device
- static_cast<UINT>(mWidth), // dimension
- static_cast<UINT>(mHeight),
- static_cast<UINT>(mDepth),
- numMips, // number of mip map levels
- usage, // usage
- d3dPF, // pixel format
- mD3DPool,
- &textureResources->pVolumeTex); // data pointer
- // check result and except if failed
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error creating texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createVolumeTex" );
- }
- // set the base texture we'll use in the render system
- hr = textureResources->pVolumeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get base texture: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createVolumeTex" );
- }
-
- // set final tex. attributes from tex. description
- // they may differ from the source image !!!
- D3DVOLUME_DESC desc;
- hr = textureResources->pVolumeTex->GetLevelDesc(0, &desc);
- if (FAILED(hr))
- {
- freeInternalResources();
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Can't get texture description: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createVolumeTex" );
- }
- _setFinalAttributes(d3d9Device, textureResources,
- desc.Width, desc.Height, desc.Depth, D3D9Mappings::_getPF(desc.Format));
-
- // Set best filter type
- if(mMipmapsHardwareGenerated)
- {
- hr = textureResources->pBaseTex->SetAutoGenFilterType(_getBestFilterMethod(d3d9Device));
- if(FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not set best autogen filter type: " + String(DXGetErrorDescription(hr)),
- "D3D9Texture::_createCubeTex" );
- }
- }
- }
- /****************************************************************************************/
- void D3D9Texture::_setFinalAttributes(IDirect3DDevice9* d3d9Device,
- TextureResources* textureResources,
- unsigned long width, unsigned long height,
- unsigned long depth, PixelFormat format)
- {
- // set target texture attributes
- mHeight = height;
- mWidth = width;
- mDepth = depth;
- mFormat = format;
-
- // Create list of subsurfaces for getBuffer()
- _createSurfaceList(d3d9Device, textureResources);
- }
- /****************************************************************************************/
- void D3D9Texture::_setSrcAttributes(unsigned long width, unsigned long height,
- unsigned long depth, PixelFormat format)
- {
- // set source image attributes
- mSrcWidth = width;
- mSrcHeight = height;
- mSrcDepth = depth;
- mSrcFormat = format;
- }
- /****************************************************************************************/
- D3DTEXTUREFILTERTYPE D3D9Texture::_getBestFilterMethod(IDirect3DDevice9* d3d9Device)
- {
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
-
-
- DWORD filterCaps = 0;
- // Minification filter is used for mipmap generation
- // Pick the best one supported for this tex type
- switch (getTextureType())
- {
- case TEX_TYPE_1D: // Same as 2D
- case TEX_TYPE_2D: filterCaps = rkCurCaps.TextureFilterCaps; break;
- case TEX_TYPE_3D: filterCaps = rkCurCaps.VolumeTextureFilterCaps; break;
- case TEX_TYPE_CUBE_MAP: filterCaps = rkCurCaps.CubeTextureFilterCaps; break;
- }
- if(filterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
- return D3DTEXF_GAUSSIANQUAD;
-
- if(filterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
- return D3DTEXF_PYRAMIDALQUAD;
-
- if(filterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
- return D3DTEXF_ANISOTROPIC;
-
- if(filterCaps & D3DPTFILTERCAPS_MINFLINEAR)
- return D3DTEXF_LINEAR;
-
- if(filterCaps & D3DPTFILTERCAPS_MINFPOINT)
- return D3DTEXF_POINT;
-
- return D3DTEXF_POINT;
- }
- /****************************************************************************************/
- bool D3D9Texture::_canUseDynamicTextures(IDirect3DDevice9* d3d9Device,
- DWORD srcUsage,
- D3DRESOURCETYPE srcType,
- D3DFORMAT srcFormat)
- {
- HRESULT hr;
- IDirect3D9* pD3D = NULL;
- hr = d3d9Device->GetDirect3D(&pD3D);
- if (FAILED(hr))
- {
- OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
- "GetDirect3D failed !!!",
- "D3D9Texture::_canUseDynamicTextures" );
- }
- if (pD3D != NULL)
- pD3D->Release();
-
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
-
- // Check for dynamic texture support
-
- // check for auto gen. mip maps support
- hr = pD3D->CheckDeviceFormat(
- rkCurCaps.AdapterOrdinal,
- rkCurCaps.DeviceType,
- eBackBufferFormat,
- srcUsage | D3DUSAGE_DYNAMIC,
- srcType,
- srcFormat);
- if (hr == D3D_OK)
- return true;
- else
- return false;
- }
- /****************************************************************************************/
- bool D3D9Texture::_canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device,
- DWORD srcUsage,
- D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting)
- {
- HRESULT hr;
- IDirect3D9* pD3D = NULL;
- hr = d3d9Device->GetDirect3D(&pD3D);
- if (FAILED(hr))
- {
- OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
- "GetDirect3D failed !!!",
- "D3D9Texture::_canUseDynamicTextures" );
- }
- if (pD3D != NULL)
- pD3D->Release();
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
- // Always check 'read' capability here
- // We will check 'write' capability only in the context of a render target
- if (forwriting)
- srcUsage |= D3DUSAGE_QUERY_SRGBWRITE;
- else
- srcUsage |= D3DUSAGE_QUERY_SRGBREAD;
- // Check for sRGB support
- // check for auto gen. mip maps support
- hr = pD3D->CheckDeviceFormat(
- rkCurCaps.AdapterOrdinal,
- rkCurCaps.DeviceType,
- eBackBufferFormat,
- srcUsage,
- srcType,
- srcFormat);
- if (hr == D3D_OK)
- return true;
- else
- return false;
- }
- /****************************************************************************************/
- bool D3D9Texture::_canAutoGenMipmaps(IDirect3DDevice9* d3d9Device,
- DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat)
- {
- HRESULT hr;
- IDirect3D9* pD3D = NULL;
- hr = d3d9Device->GetDirect3D(&pD3D);
- if (FAILED(hr))
- {
- OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
- "GetDirect3D failed !!!",
- "D3D9Texture::_canUseDynamicTextures" );
- }
- if (pD3D != NULL)
- pD3D->Release();
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
- const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
- D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
- // Hacky override - many (all?) cards seem to not be able to autogen on
- // textures which are not a power of two
- // Can we even mipmap on 3D textures? Well
- if ((mWidth & mWidth-1) || (mHeight & mHeight-1) || (mDepth & mDepth-1))
- return false;
- if (rkCurCaps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
- {
- HRESULT hr;
- // check for auto gen. mip maps support
- hr = pD3D->CheckDeviceFormat(
- rkCurCaps.AdapterOrdinal,
- rkCurCaps.DeviceType,
- eBackBufferFormat,
- srcUsage | D3DUSAGE_AUTOGENMIPMAP,
- srcType,
- srcFormat);
- // this HR could a SUCCES
- // but mip maps will not be generated
- if (hr == D3D_OK)
- return true;
- else
- return false;
- }
- else
- return false;
- }
- /****************************************************************************************/
- D3DFORMAT D3D9Texture::_chooseD3DFormat(IDirect3DDevice9* d3d9Device)
- {
- // Choose frame buffer pixel format in case PF_UNKNOWN was requested
- if(mFormat == PF_UNKNOWN)
- {
- D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
-
- return device->getBackBufferFormat();
- }
- // Choose closest supported D3D format as a D3D format
- return D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(mFormat));
- }
- /****************************************************************************************/
- // Macro to hide ugly cast
- #define GETLEVEL(face,mip) \
- static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[face*(mNumMipmaps+1)+mip].get())
- void D3D9Texture::_createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources)
- {
- IDirect3DSurface9 *surface;
- IDirect3DVolume9 *volume;
- D3D9HardwarePixelBuffer *buffer;
- size_t mip, face;
-
- assert(textureResources != NULL);
- assert(textureResources->pBaseTex);
- // Make sure number of mips is right
- mNumMipmaps = textureResources->pBaseTex->GetLevelCount() - 1;
- // Need to know static / dynamic
- unsigned int bufusage;
- if ((mUsage & TU_DYNAMIC) && mDynamicTextures)
- {
- bufusage = HardwareBuffer::HBU_DYNAMIC;
- }
- else
- {
- bufusage = HardwareBuffer::HBU_STATIC;
- }
- if (mUsage & TU_RENDERTARGET)
- {
- bufusage |= TU_RENDERTARGET;
- }
-
- uint surfaceCount = static_cast<uint>((getNumFaces() * (mNumMipmaps + 1)));
- bool updateOldList = mSurfaceList.size() == surfaceCount;
- if(!updateOldList)
- {
- // Create new list of surfaces
- mSurfaceList.clear();
- for(size_t face=0; face<getNumFaces(); ++face)
- {
- for(size_t mip=0; mip<=mNumMipmaps; ++mip)
- {
- buffer = new D3D9HardwarePixelBuffer((HardwareBuffer::Usage)bufusage, this);
- mSurfaceList.push_back(HardwarePixelBufferPtr(buffer));
- }
- }
- }
-
- switch(getTextureType()) {
- case TEX_TYPE_2D:
- case TEX_TYPE_1D:
- assert(textureResources->pNormTex);
- // For all mipmaps, store surfaces as HardwarePixelBufferPtr
- for(mip=0; mip<=mNumMipmaps; ++mip)
- {
- if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get surface level failed",
- "D3D9Texture::_createSurfaceList");
- D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
-
- if (mip == 0 && mNumRequestedMipmaps != 0 && (mUsage & TU_AUTOMIPMAP))
- currPixelBuffer->_setMipmapping(true, mMipmapsHardwareGenerated);
- currPixelBuffer->bind(d3d9Device, surface, textureResources->pFSAASurface,
- mHwGammaWriteSupported, mFSAA, "PortNoName", textureResources->pBaseTex);
- // decrement reference count, the GetSurfaceLevel call increments this
- // this is safe because the pixel buffer keeps a reference as well
- surface->Release();
- }
-
- break;
- case TEX_TYPE_CUBE_MAP:
- assert(textureResources->pCubeTex);
- // For all faces and mipmaps, store surfaces as HardwarePixelBufferPtr
- for(face=0; face<6; ++face)
- {
- for(mip=0; mip<=mNumMipmaps; ++mip)
- {
- if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get cubemap surface failed",
- "D3D9Texture::getBuffer");
- D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(face, mip);
-
-
- if (mip == 0 && mNumRequestedMipmaps != 0 && (mUsage & TU_AUTOMIPMAP))
- currPixelBuffer->_setMipmapping(true, mMipmapsHardwareGenerated);
- currPixelBuffer->bind(d3d9Device, surface, textureResources->pFSAASurface,
- mHwGammaWriteSupported, mFSAA, "NoNamePort", textureResources->pBaseTex);
- // decrement reference count, the GetSurfaceLevel call increments this
- // this is safe because the pixel buffer keeps a reference as well
- surface->Release();
- }
- }
- break;
- case TEX_TYPE_3D:
- assert(textureResources->pVolumeTex);
- // For all mipmaps, store surfaces as HardwarePixelBufferPtr
- for(mip=0; mip<=mNumMipmaps; ++mip)
- {
- if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get volume level failed",
- "D3D9Texture::getBuffer");
-
- D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
- currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
- if (mip == 0 && mNumRequestedMipmaps != 0 && (mUsage & TU_AUTOMIPMAP))
- currPixelBuffer->_setMipmapping(true, mMipmapsHardwareGenerated);
- // decrement reference count, the GetSurfaceLevel call increments this
- // this is safe because the pixel buffer keeps a reference as well
- volume->Release();
- }
- break;
- };
- }
- #undef GETLEVEL
- /****************************************************************************************/
- HardwarePixelBufferPtr D3D9Texture::getBuffer(size_t face, size_t mipmap)
- {
- if(face >= getNumFaces())
- OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "A three dimensional cube has six faces",
- "D3D9Texture::getBuffer");
- if(mipmap > mNumMipmaps)
- OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Mipmap index out of range",
- "D3D9Texture::getBuffer");
- size_t idx = face*(mNumMipmaps+1) + mipmap;
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
- TextureResources* textureResources = getTextureResources(d3d9Device);
- if (textureResources == NULL || textureResources->pBaseTex == NULL)
- {
- createTextureResources(d3d9Device);
- textureResources = getTextureResources(d3d9Device);
- }
-
- assert(textureResources != NULL);
- assert(idx < mSurfaceList.size());
- return mSurfaceList[idx];
- }
- //---------------------------------------------------------------------
- bool D3D9Texture::useDefaultPool()
- {
- // Determine D3D pool to use
- // Use managed unless we're a render target or user has asked for
- // a dynamic texture, and device supports D3DUSAGE_DYNAMIC (because default pool
- // resources without the dynamic flag are not lockable)
- return (mUsage & TU_RENDERTARGET) || ((mUsage & TU_DYNAMIC) && mDynamicTextures);
- }
-
- //---------------------------------------------------------------------
- void D3D9Texture::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
- createTextureResources(d3d9Device);
- }
- //---------------------------------------------------------------------
- void D3D9Texture::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
- if (it != mMapDeviceToTextureResources.end())
- {
- TextureResources* textureResource = it->second;
- // Destroy surfaces from each mip level.
- for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
- {
- D3D9HardwarePixelBuffer* pixelBuffer = static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[i].get());
- pixelBuffer->destroyBufferResources(d3d9Device);
- }
- // Just free any internal resources, don't call unload() here
- // because we want the un-touched resource to keep its unloaded status
- // after device reset.
- freeTextureResources(d3d9Device, textureResource);
- SAFE_DELETE(textureResource);
- mMapDeviceToTextureResources.erase(it);
- }
- }
- //---------------------------------------------------------------------
- void D3D9Texture::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- if(mD3DPool == D3DPOOL_DEFAULT)
- {
- DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
- if (it != mMapDeviceToTextureResources.end())
- {
- TextureResources* textureResource = it->second;
- // Just free any internal resources, don't call unload() here
- // because we want the un-touched resource to keep its unloaded status
- // after device reset.
- freeTextureResources(d3d9Device, textureResource);
- }
- }
- }
- //---------------------------------------------------------------------
- void D3D9Texture::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
- {
- D3D9_DEVICE_ACCESS_CRITICAL_SECTION
- if(mD3DPool == D3DPOOL_DEFAULT)
- {
- createTextureResources(d3d9Device);
- }
- }
- //---------------------------------------------------------------------
- IDirect3DBaseTexture9* D3D9Texture::getTexture()
- {
- TextureResources* textureResources;
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
-
- textureResources = getTextureResources(d3d9Device);
- if (textureResources == NULL || textureResources->pBaseTex == NULL)
- {
- createTextureResources(d3d9Device);
- textureResources = getTextureResources(d3d9Device);
- }
- assert(textureResources);
- assert(textureResources->pBaseTex);
- return textureResources->pBaseTex;
- }
- //---------------------------------------------------------------------
- IDirect3DTexture9* D3D9Texture::getNormTexture()
- {
- TextureResources* textureResources;
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
-
- textureResources = getTextureResources(d3d9Device);
- if (textureResources == NULL || textureResources->pNormTex == NULL)
- {
- createTextureResources(d3d9Device);
- textureResources = getTextureResources(d3d9Device);
- }
- assert(textureResources);
- assert(textureResources->pNormTex);
- return textureResources->pNormTex;
- }
- //---------------------------------------------------------------------
- IDirect3DCubeTexture9* D3D9Texture::getCubeTexture()
- {
- TextureResources* textureResources;
- IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
-
- textureResources = getTextureResources(d3d9Device);
- if (textureResources == NULL || textureResources->pCubeTex)
- {
- createTextureResources(d3d9Device);
- textureResources = getTextureResources(d3d9Device);
- }
- assert(textureResources);
- assert(textureResources->pCubeTex);
- return textureResources->pCubeTex;
- }
- /****************************************************************************************/
- D3D9RenderTexture::D3D9RenderTexture(const String &name,
- D3D9HardwarePixelBuffer *buffer,
- bool writeGamma,
- uint fsaa) : RenderTexture(buffer, 0)
- {
- mName = name;
- mHwGamma = writeGamma;
- mFSAA = fsaa;
- }
- //---------------------------------------------------------------------
- void D3D9RenderTexture::update(bool swap)
- {
- D3D9DeviceManager* deviceManager = D3D9RenderSystem::getDeviceManager();
- D3D9Device* currRenderWindowDevice = deviceManager->getActiveRenderTargetDevice();
- if (currRenderWindowDevice != NULL)
- {
- if (currRenderWindowDevice->isDeviceLost() == false)
- RenderTexture::update(swap);
- }
- else
- {
- for (UINT i=0; i < deviceManager->getDeviceCount(); ++i)
- {
- D3D9Device* device = deviceManager->getDevice(i);
- if (device->isDeviceLost() == false)
- {
- deviceManager->setActiveRenderTargetDevice(device);
- RenderTexture::update(swap);
- deviceManager->setActiveRenderTargetDevice(NULL);
- }
- }
- }
- }
- //---------------------------------------------------------------------
- void D3D9RenderTexture::getCustomAttribute( const String& name, void *pData )
- {
- if(name == "DDBACKBUFFER")
- {
- if (mFSAA > 0)
- {
- // rendering to AA surface
- IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
- *pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getFSAASurface(D3D9RenderSystem::getActiveD3D9Device());
- return;
- }
- else
- {
- IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
- *pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getSurface(D3D9RenderSystem::getActiveD3D9Device());
- return;
- }
- }
- else if(name == "HWND")
- {
- HWND *pHwnd = (HWND*)pData;
- *pHwnd = NULL;
- return;
- }
- else if(name == "BUFFER")
- {
- *static_cast<HardwarePixelBuffer**>(pData) = mBuffer;
- return;
- }
- }
- //---------------------------------------------------------------------
- void D3D9RenderTexture::swapBuffers(bool waitForVSync /* = true */)
- {
- // Only needed if we have to blit from AA surface
- if (mFSAA > 0)
- {
- D3D9DeviceManager* deviceManager = D3D9RenderSystem::getDeviceManager();
- D3D9HardwarePixelBuffer* buf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer);
- for (UINT i=0; i < deviceManager->getDeviceCount(); ++i)
- {
- D3D9Device* device = deviceManager->getDevice(i);
-
- if (device->isDeviceLost() == false)
- {
- IDirect3DDevice9* d3d9Device = device->getD3D9Device();
- HRESULT hr = d3d9Device->StretchRect(buf->getFSAASurface(d3d9Device), 0,
- buf->getSurface(d3d9Device), 0, D3DTEXF_NONE);
- if (FAILED(hr))
- {
- OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
- "Unable to copy AA buffer to final buffer: " + String(DXGetErrorDescription(hr)),
- "D3D9RenderTexture::swapBuffers");
- }
- }
- }
- }
- }
- }
|