-
-
Notifications
You must be signed in to change notification settings - Fork 944
/
OgreD3D9Texture.cpp
1479 lines (1284 loc) · 59.5 KB
/
OgreD3D9Texture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 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 "OgreLogManager.h"
#include "OgreStringConverter.h"
#include "OgreBitwise.h"
#include "OgreD3D9Mappings.h"
#include "OgreD3D9RenderSystem.h"
#include "OgreD3D9TextureManager.h"
#include "OgreRoot.h"
#include "OgreD3D9Device.h"
#include "OgreD3D9DeviceManager.h"
#include "OgreD3D9ResourceManager.h"
namespace Ogre
{
/****************************************************************************************/
D3D9Texture::D3D9Texture(ResourceManager* creator, const String& name,
ResourceHandle handle, const String& group, bool isManual,
ManualResourceLoader* loader)
:Texture(creator, name, handle, group, isManual, loader),
mD3DPool(D3D9RenderSystem::isDirectX9Ex() ? D3DPOOL_DEFAULT : 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
if (isLoaded())
{
unload();
}
else
{
freeInternalResources();
}
// Free memory allocated per device.
DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
while (it != mMapDeviceToTextureResources.end())
{
TextureResources* textureResource = it->second;
OGRE_DELETE_T(textureResource, TextureResources, MEMCATEGORY_RENDERSYS);
++it;
}
mMapDeviceToTextureResources.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.
//
// In directX9Ex this is not the case there is no managed pool and no device loss
mD3DPool = D3D9RenderSystem::isDirectX9Ex() ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
}
Texture::loadImpl();
}
/****************************************************************************************/
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 = OGRE_NEW_T(TextureResources, MEMCATEGORY_RENDERSYS);
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
if (mIsManual)
{
preLoadImpl();
// create the internal resources.
createInternalResourcesImpl(d3d9Device);
// Load from manual loader
if (mLoader != NULL)
{
mLoader->loadResource(this);
}
postLoadImpl();
}
else
{
prepareImpl();
preLoadImpl();
loadImpl();
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);
}
/****************************************************************************************/
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;
}
if(mUsage & TU_AUTOMIPMAP && D3D9RenderSystem::isDirectX9Ex())
{
mUsage |= TU_DYNAMIC;
}
// 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 which 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*>(Root::getSingleton().getRenderSystem());
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 which 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*>(Root::getSingleton().getRenderSystem());
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;
}
}
}
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;
// Update size (the final size, including temp space because in consumed memory)
// this is needed in Resource class
mSize = calculateSize();
// say to the world what we are doing
if (mWidth != mSrcWidth ||
mHeight != mSrcHeight)
{
LogManager::getSingleton().logMessage("D3D9 : ***** Dimensions altered by the render system");
LogManager::getSingleton().logMessage("D3D9 : ***** Source image dimensions : " + StringConverter::toString(mSrcWidth) + "x" + StringConverter::toString(mSrcHeight));
LogManager::getSingleton().logMessage("D3D9 : ***** Texture dimensions : " + StringConverter::toString(mWidth) + "x" + StringConverter::toString(mHeight));
}
// 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;
// say to the world what we are doing
if (!TextureManager::getSingleton().getVerbose()) return;
switch (getTextureType())
{
case TEX_TYPE_1D:
if (mUsage & TU_RENDERTARGET)
LogManager::getSingleton().logMessage("D3D9 : Creating 1D RenderTarget, name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
else
LogManager::getSingleton().logMessage("D3D9 : Loading 1D Texture, image name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
break;
case TEX_TYPE_2D:
if (mUsage & TU_RENDERTARGET)
LogManager::getSingleton().logMessage("D3D9 : Creating 2D RenderTarget, name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
else
LogManager::getSingleton().logMessage("D3D9 : Loading 2D Texture, image name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
break;
case TEX_TYPE_3D:
if (mUsage & TU_RENDERTARGET)
LogManager::getSingleton().logMessage("D3D9 : Creating 3D RenderTarget, name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
else
LogManager::getSingleton().logMessage("D3D9 : Loading 3D Texture, image name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
break;
case TEX_TYPE_CUBE_MAP:
if (mUsage & TU_RENDERTARGET)
LogManager::getSingleton().logMessage("D3D9 : Creating Cube map RenderTarget, name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
else
LogManager::getSingleton().logMessage("D3D9 : Loading Cube Texture, base image name : '" + getName() + "' with " + StringConverter::toString(mNumMipmaps) + " mip map levels");
break;
default:
freeInternalResources();
OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Unknown texture type", "D3D9Texture::_setSrcAttributes" );
}
}
/****************************************************************************************/
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,