00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <assert.h>
00020 #include <time.h>
00021 #include "dxTextureContext8.h"
00022 #include "config_dxgsg8.h"
00023 #include "dxGraphicsStateGuardian8.h"
00024
00025 #include "d3dx8tex.h"
00026
00027
00028 static const DWORD g_LowByteMask = 0x000000FF;
00029
00030 #define PANDA_BGRA_ORDER
00031
00032 #ifdef PANDA_BGRA_ORDER
00033
00034
00035 #define GET_RED_BYTE(PIXEL_DWORD) ((BYTE)((PIXEL_DWORD >> 16) & g_LowByteMask))
00036 #define GET_BLUE_BYTE(PIXEL_DWORD) ((BYTE)((PIXEL_DWORD) & g_LowByteMask))
00037 #else
00038
00039 #define GET_RED_BYTE(PIXEL_DWORD) ((BYTE)(PIXEL_DWORD & g_LowByteMask))
00040 #define GET_BLUE_BYTE(PIXEL_DWORD) ((BYTE)((PIXEL_DWORD >> 16) & g_LowByteMask))
00041 #endif
00042
00043 #define GET_GREEN_BYTE(PIXEL_DWORD) ((BYTE)((PIXEL_DWORD >> 8) & g_LowByteMask))
00044 #define GET_ALPHA_BYTE(PIXEL_DWORD) ((BYTE)(((DWORD)PIXEL_DWORD) >> 24)) // unsigned >> shifts in 0's, so dont need to mask off upper bits
00045
00046 #ifdef DO_CUSTOM_CONVERSIONS
00047 typedef enum {
00048 None,Conv32to32,Conv32to32_NoAlpha,Conv32to24,Conv32to16_X555,
00049 Conv32to16_1555,Conv32to16_0565,Conv32to16_4444,Conv24to32,Conv24to24,
00050 Conv24to16_X555,Conv24to16_0565,ConvLum16to16_1555,ConvLum16to16_4444,
00051 ConvLum16to32,ConvLum16to16,ConvLum8to8,ConvLum8to24,ConvLum8to32,ConvLum8to16_X555,ConvLum8to16_0565,ConvLum8to16_A8L8,
00052 ConvAlpha8to16_4444,ConvAlpha8to32,ConvAlpha8to8,ConvAlpha8to16_A8L8
00053 } ConversionType;
00054
00055 #ifndef NDEBUG
00056 char *ConvNameStrs[] = {"None","Conv32to32","Conv32to32_NoAlpha","Conv32to24","Conv32to16_X555",
00057 "Conv32to16_1555","Conv32to16_0565","Conv32to16_4444","Conv24to32","Conv24to24","Conv24to16_X555","Conv24to16_0565",
00058 "ConvLum16to16_1555","ConvLum16to16_4444","ConvLum16to32","ConvLum16to16","ConvLum8to8","ConvLum8to24","ConvLum8to32",
00059 "ConvLum8to16_X555","ConvLum8to16_0565","ConvLum8to16_A8L8",
00060 "ConvAlpha8to16_4444","ConvAlpha8to32","ConvAlpha8to8","ConvAlpha8to16_A8L8"
00061 };
00062 #endif
00063 #endif
00064
00065 char *PandaFilterNameStrs[] = {"FT_nearest","FT_linear","FT_nearest_mipmap_nearest","FT_linear_mipmap_nearest",
00066 "FT_nearest_mipmap_linear", "FT_linear_mipmap_linear"
00067 };
00068
00069
00070 TypeHandle DXTextureContext8::_type_handle;
00071
00072 #define SWAPDWORDS(X,Y) { DWORD temp=X; X=Y; Y=temp; }
00073
00074 #ifdef _DEBUG
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void PrintLastError(char *msgbuf) {
00102 DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
00103
00104 if(msgbuf==NULL) {
00105 LPVOID lpMsgBuf;
00106 dwFlags|=FORMAT_MESSAGE_ALLOCATE_BUFFER;
00107 FormatMessage( dwFlags,
00108 NULL,GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00109 (LPTSTR) &lpMsgBuf,0,NULL );
00110 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
00111 LocalFree(lpMsgBuf);
00112 } else {
00113 FormatMessage( dwFlags,
00114 NULL,GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00115 (LPTSTR) msgbuf,500,NULL );
00116 }
00117 }
00118
00119 #endif
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 unsigned int DXTextureContext8::
00164 get_bits_per_pixel(PixelBuffer::Format format, int *alphbits) {
00165 *alphbits = 0;
00166 switch(format) {
00167 case PixelBuffer::F_alpha:
00168 *alphbits = 8;
00169 case PixelBuffer::F_color_index:
00170 case PixelBuffer::F_red:
00171 case PixelBuffer::F_green:
00172 case PixelBuffer::F_blue:
00173 case PixelBuffer::F_rgb332:
00174 return 8;
00175 case PixelBuffer::F_luminance_alphamask:
00176 *alphbits = 1;
00177 return 16;
00178 case PixelBuffer::F_luminance_alpha:
00179 *alphbits = 8;
00180 return 16;
00181 case PixelBuffer::F_luminance:
00182 return 8;
00183 case PixelBuffer::F_rgba4:
00184 *alphbits = 4;
00185 return 16;
00186 case PixelBuffer::F_rgba5:
00187 *alphbits = 1;
00188 return 16;
00189 case PixelBuffer::F_depth_component:
00190 case PixelBuffer::F_rgb5:
00191 return 16;
00192 case PixelBuffer::F_rgb8:
00193 case PixelBuffer::F_rgb:
00194 return 24;
00195 case PixelBuffer::F_rgba8:
00196 case PixelBuffer::F_rgba:
00197 case PixelBuffer::F_rgbm:
00198 if(format==PixelBuffer::F_rgbm)
00199 *alphbits = 1;
00200 else *alphbits = 8;
00201 return 32;
00202 case PixelBuffer::F_rgb12:
00203 return 36;
00204 case PixelBuffer::F_rgba12:
00205 *alphbits = 12;
00206 return 48;
00207 }
00208 return 8;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715 HRESULT ConvertD3DSurftoPixBuf(RECT &SrcRect,IDirect3DSurface8 *pD3DSurf8,PixelBuffer *pixbuf) {
00716
00717 HRESULT hr;
00718 DWORD dwNumComponents=pixbuf->get_num_components();
00719
00720 assert(pixbuf->get_component_width()==sizeof(BYTE));
00721 assert(pixbuf->get_image_type()==PixelBuffer::T_unsigned_byte);
00722 assert((dwNumComponents==3) || (dwNumComponents==4));
00723 assert(IS_VALID_PTR(pD3DSurf8));
00724
00725 BYTE *pbuf=pixbuf->_image.p();
00726
00727 if(IsBadWritePtr(pD3DSurf8,sizeof(DWORD))) {
00728 dxgsg8_cat.error() << "ConvertDDSurftoPixBuf failed: bad pD3DSurf ptr value (" << ((void*)pD3DSurf8) << ")\n";
00729 exit(1);
00730 }
00731
00732 DWORD dwXWindowOffset,dwYWindowOffset;
00733 DWORD dwCopyWidth,dwCopyHeight;
00734
00735 D3DLOCKED_RECT LockedRect;
00736 D3DSURFACE_DESC SurfDesc;
00737
00738 hr = pD3DSurf8->GetDesc(&SurfDesc);
00739
00740 dwXWindowOffset=SrcRect.left,dwYWindowOffset=SrcRect.top;
00741 dwCopyWidth=RECT_XSIZE(SrcRect);
00742 dwCopyHeight=RECT_YSIZE(SrcRect);
00743
00744
00745
00746
00747 if(!((dwCopyWidth==pixbuf->get_xsize()) && (dwCopyHeight<=(DWORD)pixbuf->get_ysize()))) {
00748 dxgsg8_cat.error() << "ConvertDDSurftoPixBuf, PixBuf size too small to hold display surface!\n";
00749 assert(0);
00750 return E_FAIL;
00751 }
00752
00753 hr = pD3DSurf8->LockRect(&LockedRect,(CONST RECT*)NULL,(D3DLOCK_READONLY | D3DLOCK_NO_DIRTY_UPDATE ));
00754 if(FAILED(hr)) {
00755 dxgsg8_cat.error() << "ConvertDDSurftoPixBuf LockRect() failed!" << D3DERRORSTRING(hr);
00756 return hr;
00757 }
00758
00759
00760 assert((SurfDesc.Format==D3DFMT_A8R8G8B8)||(SurfDesc.Format==D3DFMT_X8R8G8B8)||(SurfDesc.Format==D3DFMT_R8G8B8)||
00761 (SurfDesc.Format==D3DFMT_R5G6B5)||(SurfDesc.Format==D3DFMT_X1R5G5B5)||(SurfDesc.Format==D3DFMT_A1R5G5B5)||
00762 (SurfDesc.Format==D3DFMT_A4R4G4B4));
00763
00764
00765
00766 DWORD BytePitch = LockedRect.Pitch;
00767 BYTE* pSurfBytes = (BYTE*)LockedRect.pBits;
00768
00769
00770
00771 if(dxgsg8_cat.is_debug()) {
00772 dxgsg8_cat.debug() << "ConvertD3DSurftoPixBuf converting " << D3DFormatStr(SurfDesc.Format) << "bpp DDSurf to "
00773 << dwNumComponents << "-channel panda PixelBuffer\n";
00774 }
00775
00776 DWORD *pDstWord = (DWORD *) pbuf;
00777 BYTE *pDstByte = (BYTE *) pbuf;
00778
00779 switch(SurfDesc.Format) {
00780 case D3DFMT_A8R8G8B8:
00781 case D3DFMT_X8R8G8B8: {
00782 if(dwNumComponents==4) {
00783 DWORD *pSrcWord;
00784 #ifdef PANDA_BGRA_ORDER
00785 BYTE *pDstLine = (BYTE*)pDstWord;
00786 #endif
00787
00788 pSurfBytes+=BytePitch*(dwYWindowOffset+dwCopyHeight-1);
00789 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00790 pSrcWord = ((DWORD*)pSurfBytes)+dwXWindowOffset;
00791 #ifdef PANDA_BGRA_ORDER
00792 memcpy(pDstLine,pSrcWord,BytePitch);
00793 pDstLine+=BytePitch;
00794 #else
00795 for(DWORD x=0; x<dwCopyWidth; x++,pSrcWord++,pDstWord++) {
00796 DWORD dwPixel = *pSrcWord;
00797 BYTE r,b;
00798
00799
00800 r= (BYTE) ((dwPixel >> 16) & g_LowByteMask);
00801 b = (BYTE) (dwPixel & g_LowByteMask);
00802
00803
00804 *pDstWord = (dwPixel & 0xFF00FF00) | (b<<16) | r;
00805 }
00806 #endif
00807 }
00808 } else {
00809
00810 DWORD *pSrcWord;
00811 pSurfBytes+=BytePitch*(dwYWindowOffset+dwCopyHeight-1);
00812 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00813 pSrcWord = ((DWORD*)pSurfBytes)+dwXWindowOffset;
00814
00815 for(DWORD x=0; x<dwCopyWidth; x++,pSrcWord++) {
00816 BYTE r,g,b;
00817 DWORD dwPixel = *pSrcWord;
00818
00819 r = (BYTE)((dwPixel>>16) & g_LowByteMask);
00820 g = (BYTE)((dwPixel>> 8) & g_LowByteMask);
00821 b = (BYTE)((dwPixel ) & g_LowByteMask);
00822
00823 #ifdef PANDA_BGRA_ORDER
00824 *pDstByte++ = b;
00825 *pDstByte++ = g;
00826 *pDstByte++ = r;
00827 #else
00828 *pDstByte++ = r;
00829 *pDstByte++ = g;
00830 *pDstByte++ = b;
00831 #endif
00832 }
00833 }
00834 }
00835 break;
00836 }
00837
00838 case D3DFMT_R8G8B8: {
00839 BYTE *pSrcByte;
00840 pSurfBytes+=BytePitch*(dwYWindowOffset+dwCopyHeight-1);
00841
00842 if(dwNumComponents==4) {
00843 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00844 pSrcByte = pSurfBytes+dwXWindowOffset*3*sizeof(BYTE);
00845 for(DWORD x=0; x<dwCopyWidth; x++,pDstWord++) {
00846 DWORD r,g,b;
00847
00848 b = *pSrcByte++;
00849 g = *pSrcByte++;
00850 r = *pSrcByte++;
00851
00852 #ifdef PANDA_BGRA_ORDER
00853 *pDstWord = 0xFF000000 | (r << 16) | (g << 8) | b;
00854 #else
00855 *pDstWord = 0xFF000000 | (b << 16) | (g << 8) | r;
00856 #endif
00857 }
00858 }
00859 } else {
00860
00861 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00862 pSrcByte = pSurfBytes+dwXWindowOffset*3*sizeof(BYTE);
00863 #ifdef PANDA_BGRA_ORDER
00864 memcpy(pDstByte,pSrcByte,BytePitch);
00865 pDstByte+=BytePitch;
00866 #else
00867 for(DWORD x=0; x<dwCopyWidth; x++) {
00868 BYTE r,g,b;
00869
00870
00871
00872
00873 b = *pSrcByte++;
00874 g = *pSrcByte++;
00875 r = *pSrcByte++;
00876
00877 *pDstByte++ = r;
00878 *pDstByte++ = g;
00879 *pDstByte++ = b;
00880 }
00881 #endif
00882 }
00883 }
00884 break;
00885 }
00886
00887 case D3DFMT_R5G6B5:
00888 case D3DFMT_X1R5G5B5:
00889 case D3DFMT_A1R5G5B5:
00890 case D3DFMT_A4R4G4B4: {
00891 WORD *pSrcWord;
00892
00893
00894 BYTE redshift,greenshift,blueshift;
00895 DWORD redmask,greenmask,bluemask;
00896
00897 if(SurfDesc.Format==D3DFMT_R5G6B5) {
00898 redshift=(11-3);
00899 redmask=0xF800;
00900 greenmask=0x07E0;
00901 greenshift=(5-2);
00902 bluemask=0x001F;
00903 blueshift=3;
00904 } else if(SurfDesc.Format==D3DFMT_A4R4G4B4) {
00905 redmask=0x0F00;
00906 redshift=4;
00907 greenmask=0x00F0;
00908 greenshift=0;
00909 bluemask=0x000F;
00910 blueshift=4;
00911 } else {
00912 redmask=0x7C00;
00913 redshift=(10-3);
00914 greenmask=0x03E0;
00915 greenshift=(5-3);
00916 bluemask=0x001F;
00917 blueshift=3;
00918 }
00919
00920 pSurfBytes+=BytePitch*(dwYWindowOffset+dwCopyHeight-1);
00921 if(dwNumComponents==4) {
00922
00923
00924
00925
00926 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00927 pSrcWord = ((WORD*)pSurfBytes)+dwXWindowOffset;
00928 for(DWORD x=0; x<dwCopyWidth; x++,pSrcWord++,pDstWord++) {
00929 WORD dwPixel = *pSrcWord;
00930 BYTE r,g,b;
00931
00932 b = (dwPixel & bluemask) << blueshift;
00933 g = (dwPixel & greenmask) >> greenshift;
00934 r = (dwPixel & redmask) >> redshift;
00935
00936
00937
00938 #ifdef PANDA_BGRA_ORDER
00939 *pDstWord = 0xFF000000 | (r << 16) | (g << 8) | b;
00940 #else
00941 *pDstWord = 0xFF000000 | (b << 16) | (g << 8) | r;
00942 #endif
00943 }
00944 }
00945 } else {
00946
00947 for(DWORD y=0; y<dwCopyHeight; y++,pSurfBytes-=BytePitch) {
00948 pSrcWord = ((WORD*)pSurfBytes)+dwXWindowOffset;
00949 for(DWORD x=0; x<dwCopyWidth; x++,pSrcWord++) {
00950 WORD dwPixel = *pSrcWord;
00951 BYTE r,g,b;
00952
00953 b = (dwPixel & bluemask) << blueshift;
00954 g = (dwPixel & greenmask) >> greenshift;
00955 r = (dwPixel & redmask) >> redshift;
00956
00957 #ifdef PANDA_BGRA_ORDER
00958 *pDstByte++ = b;
00959 *pDstByte++ = g;
00960 *pDstByte++ = r;
00961 #else
00962 *pDstByte++ = r;
00963 *pDstByte++ = g;
00964 *pDstByte++ = b;
00965 #endif
00966 }
00967 }
00968 }
00969 break;
00970 }
00971
00972 default:
00973 dxgsg8_cat.error() << "ConvertD3DSurftoPixBuf: unsupported D3DFORMAT!\n";
00974 }
00975
00976 pD3DSurf8->UnlockRect();
00977 return S_OK;
00978 }
00979
00980
00981
00982
00983
00984
00985
00986 IDirect3DTexture8 *DXTextureContext8::CreateTexture(DXScreenData &scrn) {
00987 HRESULT hr;
00988 int cNumAlphaBits;
00989 D3DFORMAT TargetPixFmt=D3DFMT_UNKNOWN;
00990 bool bNeedLuminance = false;
00991
00992 assert(IS_VALID_PTR(_texture));
00993
00994 PixelBuffer *pbuf = _texture->_pbuffer;
00995
00996 DWORD target_bpp = get_bits_per_pixel(pbuf->get_format(), &cNumAlphaBits);
00997 PixelBuffer::Type pixbuf_type = pbuf->get_image_type();
00998 DWORD cNumColorChannels = pbuf->get_num_components();
00999
01000 assert(pbuf->get_component_width()==sizeof(BYTE));
01001 assert(pixbuf_type==PixelBuffer::T_unsigned_byte);
01002
01003
01004
01005 if((pixbuf_type!=PixelBuffer::T_unsigned_byte) || (pbuf->get_component_width()!=1)) {
01006 dxgsg8_cat.error() << "CreateTexture failed, havent handled non 8-bit channel pixelbuffer types yet! \n";
01007 return NULL;
01008 }
01009
01010 DWORD dwOrigWidth = (DWORD)pbuf->get_xsize();
01011 DWORD dwOrigHeight = (DWORD)pbuf->get_ysize();
01012
01013 if((pbuf->get_format() == PixelBuffer::F_luminance_alpha)||
01014 (pbuf->get_format() == PixelBuffer::F_luminance_alphamask) ||
01015 (pbuf->get_format() == PixelBuffer::F_luminance)) {
01016 bNeedLuminance = true;
01017 }
01018
01019 if(cNumAlphaBits>0) {
01020 if(cNumColorChannels==3) {
01021 dxgsg8_cat.error() << "ERROR: texture " << _tex->get_name() << " has no inherent alpha channel, but alpha format is requested (that would be wasteful)!\n";
01022 exit(1);
01023 }
01024 }
01025
01026 _PixBufD3DFmt=D3DFMT_UNKNOWN;
01027
01028 #ifndef DO_CUSTOM_CONVERSIONS
01029
01030
01031 switch(cNumColorChannels) {
01032 case 1:
01033 if(cNumAlphaBits>0)
01034 _PixBufD3DFmt=D3DFMT_A8;
01035 else if(bNeedLuminance)
01036 _PixBufD3DFmt=D3DFMT_L8;
01037 break;
01038 case 2:
01039 assert(bNeedLuminance && (cNumAlphaBits>0));
01040 _PixBufD3DFmt=D3DFMT_A8L8;
01041 break;
01042 case 3:
01043 _PixBufD3DFmt=D3DFMT_R8G8B8;
01044 break;
01045 case 4:
01046 _PixBufD3DFmt=D3DFMT_A8R8G8B8;
01047 break;
01048 }
01049
01050
01051 assert(_PixBufD3DFmt!=D3DFMT_UNKNOWN);
01052 #endif
01053
01054 DWORD TargetWidth=dwOrigWidth;
01055 DWORD TargetHeight=dwOrigHeight;
01056
01057 if(!ISPOW2(dwOrigWidth) || !ISPOW2(dwOrigHeight)) {
01058 dxgsg8_cat.error() << "ERROR: texture dimensions are not a power of 2 for " << _tex->get_name() << "! Please rescale them so it doesnt have to be done at runtime.\n";
01059 #ifndef NDEBUG
01060 exit(1);
01061 #else
01062 goto error_exit;
01063 #endif
01064 }
01065
01066 bool bShrinkOriginal;
01067 bShrinkOriginal=false;
01068
01069 if((dwOrigWidth>scrn.d3dcaps.MaxTextureWidth)||(dwOrigHeight>scrn.d3dcaps.MaxTextureHeight)) {
01070 #ifdef _DEBUG
01071 dxgsg8_cat.error() << "WARNING: " <<_tex->get_name() << ": Image size exceeds max texture dimensions of (" << scrn.d3dcaps.MaxTextureWidth << "," << scrn.d3dcaps.MaxTextureHeight << ") !!\n"
01072 << "Scaling "<< _tex->get_name() << " ("<< dwOrigWidth<<"," <<dwOrigHeight << ") => ("<< scrn.d3dcaps.MaxTextureWidth << "," << scrn.d3dcaps.MaxTextureHeight << ") !\n";
01073 #endif
01074
01075 if(dwOrigWidth>scrn.d3dcaps.MaxTextureWidth)
01076 TargetWidth=scrn.d3dcaps.MaxTextureWidth;
01077 if(dwOrigHeight>scrn.d3dcaps.MaxTextureHeight)
01078 TargetHeight=scrn.d3dcaps.MaxTextureHeight;
01079 bShrinkOriginal=true;
01080 }
01081
01082
01083 if((TargetWidth != TargetHeight) && (scrn.d3dcaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)) {
01084
01085 int i,width_exp,height_exp;
01086 for(i=TargetWidth,width_exp=0;i>1;width_exp++,i>>=1);
01087 for(i=TargetHeight,height_exp=0;i>1;height_exp++,i>>=1);
01088 TargetHeight = TargetWidth = 1<<((width_exp+height_exp)>>1);
01089 bShrinkOriginal=true;
01090
01091 #ifdef _DEBUG
01092 dxgsg8_cat.debug() << "Scaling "<< _tex->get_name() << " ("<< dwOrigWidth<<"," <<dwOrigHeight << ") => ("<< TargetWidth<<"," << TargetHeight << ") to meet HW square texture reqmt\n";
01093 #endif
01094 }
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112 char *szErrorMsg;
01113
01114 szErrorMsg = "CreateTexture failed: couldn't find compatible device Texture Pixel Format for input texture";
01115
01116 if(dxgsg8_cat.is_spam())
01117 dxgsg8_cat.spam() << "CreateTexture handling target bitdepth: " << target_bpp << " alphabits: " << cNumAlphaBits << endl;
01118
01119
01120
01121
01122 #ifdef DO_CUSTOM_CONVERSIONS
01123 ConversionType ConvNeeded;
01124
01125 #define CONVTYPE_STMT ConvNeeded=CONV
01126 #else
01127 #define CONVTYPE_STMT
01128 #endif
01129
01130 #define CHECK_FOR_FMT(FMT,CONV) \
01131 if(scrn.SupportedTexFmtsMask & FMT##_FLAG) { \
01132 CONVTYPE_STMT; \
01133 TargetPixFmt=D3DFMT_##FMT; \
01134 goto found_matching_format; }
01135
01136
01137
01138 switch(target_bpp) {
01139
01140
01141
01142
01143 case 32:
01144 if(!((cNumColorChannels==3) || (cNumColorChannels==4)))
01145 break;
01146
01147 if(!dx_force_16bpptextures) {
01148 if(cNumColorChannels==4) {
01149 CHECK_FOR_FMT(A8R8G8B8,Conv32to32);
01150 } else {
01151 CHECK_FOR_FMT(A8R8G8B8,Conv24to32);
01152 }
01153 }
01154
01155 if(cNumAlphaBits>0) {
01156 assert(cNumColorChannels==4);
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169 #ifndef FORCE_16bpp_1555
01170 if(cNumAlphaBits==1)
01171 #endif
01172 {
01173 CHECK_FOR_FMT(A1R5G5B5,Conv32to16_1555);
01174 }
01175
01176
01177 CHECK_FOR_FMT(A4R4G4B4,Conv32to16_4444);
01178 CHECK_FOR_FMT(A1R5G5B5,Conv32to16_1555);
01179
01180
01181
01182 szErrorMsg = "CreateTexture failed: couldn't find compatible Tex DDPIXELFORMAT! no available 16 or 32-bit alpha formats!";
01183 } else {
01184
01185
01186 if(cNumColorChannels==3) {
01187 CHECK_FOR_FMT(R5G6B5,Conv24to16_4444);
01188 CHECK_FOR_FMT(X1R5G5B5,Conv24to16_X555);
01189 } else {
01190 CHECK_FOR_FMT(R5G6B5,Conv32to16_4444);
01191 CHECK_FOR_FMT(X1R5G5B5,Conv32to16_X555);
01192 }
01193 }
01194 break;
01195
01196 case 24:
01197 assert(cNumColorChannels==3);
01198
01199 if(!dx_force_16bpptextures) {
01200 CHECK_FOR_FMT(R8G8B8,Conv24to24);
01201
01202
01203
01204
01205 CHECK_FOR_FMT(X8R8G8B8,Conv24to32);
01206 }
01207
01208
01209 CHECK_FOR_FMT(R5G6B5,Conv24to16_0565);
01210 CHECK_FOR_FMT(X1R5G5B5,Conv24to16_X555);
01211 break;
01212
01213 case 16:
01214 if(bNeedLuminance) {
01215 assert(cNumAlphaBits>0);
01216 assert(cNumColorChannels==2);
01217
01218 CHECK_FOR_FMT(A8L8,ConvLum16to16);
01219
01220 if(!dx_force_16bpptextures) {
01221 CHECK_FOR_FMT(A8R8G8B8,ConvLum16to32);
01222 }
01223
01224 #ifndef FORCE_16bpp_1555
01225 if(cNumAlphaBits==1)
01226 #endif
01227 {
01228 CHECK_FOR_FMT(A1R5G5B5,ConvLum16to16_1555);
01229 }
01230
01231
01232 CHECK_FOR_FMT(A4R4G4B4,ConvLum16to16_4444);
01233 CHECK_FOR_FMT(A1R5G5B5,ConvLum16to16_1555);
01234 } else {
01235 assert((cNumColorChannels==3)||(cNumColorChannels==4));
01236
01237
01238 switch(cNumAlphaBits) {
01239 case 0:
01240 if(cNumColorChannels==3) {
01241 CHECK_FOR_FMT(R5G6B5,Conv24to16_0565);
01242 CHECK_FOR_FMT(X1R5G5B5,Conv24to16_X555);
01243 } else {
01244 assert(cNumColorChannels==4);
01245
01246 CHECK_FOR_FMT(R5G6B5,Conv32to16_0565);
01247 CHECK_FOR_FMT(X1R5G5B5,Conv32to16_X555);
01248 }
01249 break;
01250 case 1:
01251
01252
01253 assert(cNumColorChannels==4);
01254 CHECK_FOR_FMT(X1R5G5B5,Conv32to16_X555);
01255 break;
01256 case 4:
01257
01258 assert(cNumColorChannels==4);
01259 CHECK_FOR_FMT(A4R4G4B4,Conv32to16_4444);
01260 break;
01261 default: assert(0);
01262 }
01263 }
01264 case 8:
01265 if(bNeedLuminance) {
01266
01267 assert(cNumColorChannels==1);
01268
01269
01270 CHECK_FOR_FMT(L8,ConvLum8to8);
01271 CHECK_FOR_FMT(L8,ConvLum8to16_A8L8);
01272
01273 if(!dx_force_16bpptextures) {
01274 CHECK_FOR_FMT(R8G8B8,ConvLum8to24);
01275 CHECK_FOR_FMT(X8R8G8B8,ConvLum8to32);
01276 }
01277
01278 CHECK_FOR_FMT(R5G6B5,ConvLum8to16_0565);
01279 CHECK_FOR_FMT(X1R5G5B5,ConvLum8to16_X555);
01280
01281 } else if(cNumAlphaBits==8) {
01282
01283
01284
01285
01286
01287
01288 CHECK_FOR_FMT(A8L8,ConvAlpha8to16_A8L8);
01289
01290 if(!dx_force_16bpptextures) {
01291 CHECK_FOR_FMT(A8R8G8B8,ConvAlpha8to32);
01292 }
01293
01294 CHECK_FOR_FMT(A4R4G4B4,ConvAlpha8to16_4444);
01295 }
01296 break;
01297
01298 default:
01299 szErrorMsg = "CreateTexture failed: unhandled pixel bitdepth in DX loader";
01300 }
01301
01302
01303 dxgsg8_cat.error() << szErrorMsg << ": " << _tex->get_name() << endl
01304 << "NumColorChannels: " <<cNumColorChannels << "; NumAlphaBits: " << cNumAlphaBits
01305 << "; targetbpp: " <<target_bpp << "; SupportedTexFmtsMask: 0x" << (void*)scrn.SupportedTexFmtsMask
01306 << "; NeedLuminance: " << bNeedLuminance << endl;
01307 goto error_exit;
01308
01309
01310
01311 found_matching_format:
01312
01313
01314
01315 Texture::FilterType ft;
01316
01317 ft =_tex->get_magfilter();
01318 if((ft!=Texture::FT_linear) && ft!=Texture::FT_nearest) {
01319
01320 if(ft==Texture::FT_nearest_mipmap_nearest)
01321 ft=Texture::FT_nearest;
01322 else ft=Texture::FT_linear;
01323 }
01324
01325 if((ft==Texture::FT_linear) && !(scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR))
01326 ft=Texture::FT_nearest;
01327 _tex->set_magfilter(ft);
01328
01329
01330 ft =_tex->get_minfilter();
01331 _bHasMipMaps=FALSE;
01332
01333 if(!dx_ignore_mipmaps) {
01334 switch(ft) {
01335 case Texture::FT_nearest_mipmap_nearest:
01336 case Texture::FT_linear_mipmap_nearest:
01337 case Texture::FT_nearest_mipmap_linear:
01338 case Texture::FT_linear_mipmap_linear:
01339 _bHasMipMaps=TRUE;
01340 }
01341
01342 if(dx_mipmap_everything) {
01343 _bHasMipMaps=TRUE;
01344 if(dxgsg8_cat.is_spam()) {
01345 if(ft != Texture::FT_linear_mipmap_linear)
01346 dxgsg8_cat.spam() << "Forcing trilinear mipmapping on DX texture [" << _tex->get_name() << "]\n";
01347 }
01348 ft = Texture::FT_linear_mipmap_linear;
01349 _tex->set_minfilter(ft);
01350 }
01351 } else if((ft==Texture::FT_nearest_mipmap_nearest) ||
01352 (ft==Texture::FT_nearest_mipmap_linear)) {
01353 ft=Texture::FT_nearest;
01354 } else if((ft==Texture::FT_linear_mipmap_nearest) ||
01355 (ft==Texture::FT_linear_mipmap_linear)) {
01356 ft=Texture::FT_linear;
01357 }
01358
01359 assert((scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)!=0);
01360
01361 #define TRILINEAR_MIPMAP_TEXFILTERCAPS (D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MINFLINEAR)
01362
01363
01364 switch(ft) {
01365 case Texture::FT_linear_mipmap_linear:
01366 if((scrn.d3dcaps.TextureFilterCaps & TRILINEAR_MIPMAP_TEXFILTERCAPS)!=TRILINEAR_MIPMAP_TEXFILTERCAPS) {
01367 if(scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
01368 ft=Texture::FT_linear_mipmap_nearest;
01369 else ft=Texture::FT_nearest_mipmap_nearest;
01370 }
01371 break;
01372 case Texture::FT_nearest_mipmap_linear:
01373
01374 if(!((scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT) &&
01375 (scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)))
01376 ft=Texture::FT_nearest_mipmap_nearest;
01377 break;
01378 case Texture::FT_linear_mipmap_nearest:
01379
01380 if(!(scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR))
01381 ft=Texture::FT_nearest_mipmap_nearest;
01382 break;
01383 case Texture::FT_linear:
01384 if(!(scrn.d3dcaps.TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR))
01385 ft=Texture::FT_nearest;
01386 break;
01387 }
01388
01389 _tex->set_minfilter(ft);
01390
01391 uint aniso_degree;
01392
01393 aniso_degree=1;
01394 if(scrn.d3dcaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) {
01395 aniso_degree=_tex->get_anisotropic_degree();
01396 if((aniso_degree>scrn.d3dcaps.MaxAnisotropy) || dx_force_anisotropic_filtering)
01397 aniso_degree=scrn.d3dcaps.MaxAnisotropy;
01398 }
01399 _tex->set_anisotropic_degree(aniso_degree);
01400
01401 #ifdef _DEBUG
01402 dxgsg8_cat.spam() << "CreateTexture: setting aniso degree for "<< _tex->get_name() << " to: " << aniso_degree << endl;
01403 #endif
01404
01405 UINT cMipLevelCount;
01406
01407 if(_bHasMipMaps) {
01408 cMipLevelCount=0;
01409
01410 if(dxgsg8_cat.is_debug())
01411 dxgsg8_cat.debug() << "CreateTexture: generating mipmaps for "<< _tex->get_name() << endl;
01412 } else cMipLevelCount=1;
01413
01414 if(FAILED( hr = scrn.pD3DDevice->CreateTexture(TargetWidth,TargetHeight,cMipLevelCount,0x0,
01415 TargetPixFmt,D3DPOOL_MANAGED,&_pD3DTexture8) )) {
01416 dxgsg8_cat.error() << "D3D CreateTexture failed!" << D3DERRORSTRING(hr);
01417 goto error_exit;
01418 }
01419
01420 #ifdef DO_CUSTOM_CONVERSIONS
01421 _PixBufConversionType=ConvNeeded;
01422 #endif
01423
01424 #ifdef _DEBUG
01425 #ifdef DO_CUSTOM_CONVERSIONS
01426 dxgsg8_cat.debug() << "CreateTexture: "<< _tex->get_name() <<" converting " << ConvNameStrs[ConvNeeded] << " \n";
01427 #else
01428 dxgsg8_cat.debug() << "CreateTexture: "<< _tex->get_name() <<" converting panda equivalent of " << D3DFormatStr(_PixBufD3DFmt) << " => " << D3DFormatStr(TargetPixFmt) << endl;
01429 #endif
01430 #endif
01431
01432
01433
01434
01435
01436
01437
01438 if(_texture->has_ram_image()) {
01439 hr = FillDDSurfTexturePixels();
01440 if(FAILED(hr)) {
01441 goto error_exit;
01442 }
01443 }
01444
01445
01446
01447
01448 return _pD3DTexture8;
01449
01450 error_exit:
01451
01452 RELEASE(_pD3DTexture8,dxgsg8,"texture",RELEASE_ONCE);
01453 return NULL;
01454 }
01455
01456 HRESULT DXTextureContext8::
01457 FillDDSurfTexturePixels(void) {
01458 HRESULT hr=E_FAIL;
01459 assert(IS_VALID_PTR(_texture));
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477 PixelBuffer *pbuf = _texture->get_ram_image();
01478 if (pbuf == (PixelBuffer *)NULL) {
01479 dxgsg8_cat.fatal() << "CreateTexture: get_ram_image() failed\n";
01480
01481 return E_FAIL;
01482 }
01483
01484 assert(IS_VALID_PTR(_pD3DTexture8));
01485
01486 DWORD OrigWidth = (DWORD) pbuf->get_xsize();
01487 DWORD OrigHeight = (DWORD) pbuf->get_ysize();
01488 DWORD cNumColorChannels = pbuf->get_num_components();
01489 D3DFORMAT SrcFormat=_PixBufD3DFmt;
01490 BYTE *pPixels=(BYTE*)pbuf->_image.p();
01491
01492 assert(IS_VALID_PTR(pPixels));
01493
01494 IDirect3DSurface8 *pMipLevel0;
01495 hr=_pD3DTexture8->GetSurfaceLevel(0,&pMipLevel0);
01496 if(FAILED(hr)) {
01497 dxgsg8_cat.error() << "FillDDSurfaceTexturePixels failed for "<< _tex->get_name() <<", GetSurfaceLevel failed" << D3DERRORSTRING(hr);
01498 return E_FAIL;
01499 }
01500
01501 RECT SrcSize;
01502 SrcSize.left = SrcSize.top = 0;
01503 SrcSize.right = OrigWidth;
01504 SrcSize.bottom = OrigHeight;
01505
01506 UINT SrcPixBufRowByteLength=OrigWidth*cNumColorChannels;
01507
01508 DWORD Lev0Filter,MipFilterFlags;
01509 bool bUsingTempPixBuf=false;
01510
01511
01512 Lev0Filter = D3DX_FILTER_LINEAR ;
01513
01514
01515
01516 if(_PixBufD3DFmt==D3DFMT_A8) {
01517
01518 USHORT *pTempPixBuf=new USHORT[OrigWidth*OrigHeight];
01519 if(!IS_VALID_PTR(pTempPixBuf)) {
01520 dxgsg8_cat.error() << "FillDDSurfaceTexturePixels couldnt alloc mem for temp pixbuf!\n";
01521 goto exit_FillDDSurf;
01522 }
01523 bUsingTempPixBuf=true;
01524
01525 USHORT *pOutPix=pTempPixBuf;
01526 BYTE *pSrcPix=pPixels;
01527 for(UINT y=0;y<OrigHeight;y++)
01528 for(UINT x=0;x<OrigWidth;x++,pSrcPix++,pOutPix++)
01529 *pOutPix = ((*pSrcPix) << 8 ) | 0xFF;
01530
01531 SrcFormat=D3DFMT_A8L8;
01532 SrcPixBufRowByteLength=OrigWidth*sizeof(USHORT);
01533 pPixels=(BYTE*)pTempPixBuf;
01534 }
01535
01536
01537 hr=D3DXLoadSurfaceFromMemory(pMipLevel0,(PALETTEENTRY*)NULL,(RECT*)NULL,(LPCVOID)pPixels,SrcFormat,
01538 SrcPixBufRowByteLength,(PALETTEENTRY*)NULL,&SrcSize,Lev0Filter,(D3DCOLOR)0x0);
01539 if(FAILED(hr)) {
01540 dxgsg8_cat.error() << "FillDDSurfaceTexturePixels failed for "<< _tex->get_name() <<", D3DXLoadSurfFromMem failed" << D3DERRORSTRING(hr);
01541 goto exit_FillDDSurf;
01542 }
01543
01544 if(_bHasMipMaps) {
01545 if(!dx_use_triangle_mipgen_filter)
01546 MipFilterFlags = D3DX_FILTER_BOX;
01547 else MipFilterFlags = D3DX_FILTER_TRIANGLE;
01548
01549
01550
01551 hr=D3DXFilterTexture(_pD3DTexture8,(PALETTEENTRY*)NULL,0,MipFilterFlags);
01552 if(FAILED(hr)) {
01553 dxgsg8_cat.error() << "FillDDSurfaceTexturePixels failed for "<< _tex->get_name() <<", D3DXFilterTex failed" << D3DERRORSTRING(hr);
01554 goto exit_FillDDSurf;
01555 }
01556 }
01557
01558 exit_FillDDSurf:
01559 if(bUsingTempPixBuf) {
01560 SAFE_DELETE_ARRAY(pPixels);
01561 }
01562 RELEASE(pMipLevel0,dxgsg8,"FillDDSurf MipLev0 texture ptr",RELEASE_ONCE);
01563 return hr;
01564 }
01565
01566
01567
01568
01569
01570 void DXTextureContext8::
01571 DeleteTexture( ) {
01572 if(_pD3DTexture8==NULL) {
01573
01574 return;
01575 }
01576
01577 if(dxgsg8_cat.is_spam()) {
01578 dxgsg8_cat.spam() << "Deleting DX texture for " << _tex->get_name() << "\n";
01579 }
01580
01581 RELEASE(_pD3DTexture8,dxgsg8,"texture",RELEASE_ONCE);
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598 }
01599
01600
01601
01602
01603
01604
01605
01606 DXTextureContext8::
01607 DXTextureContext8(Texture *tex) :
01608 TextureContext(tex) {
01609
01610 if(dxgsg8_cat.is_spam()) {
01611 dxgsg8_cat.spam() << "Creating DX texture [" << tex->get_name() << "], minfilter(" << PandaFilterNameStrs[tex->get_minfilter()] << "), magfilter("<<PandaFilterNameStrs[tex->get_magfilter()] << "), anisodeg(" << tex->get_anisotropic_degree() << ")\n";
01612 }
01613
01614 _pD3DTexture8 = NULL;
01615 _bHasMipMaps = FALSE;
01616 _tex = tex;
01617 }
01618
01619 DXTextureContext8::
01620 ~DXTextureContext8() {
01621 if(dxgsg8_cat.is_spam()) {
01622 dxgsg8_cat.spam() << "Deleting DX8 TexContext for " << _tex->get_name() << "\n";
01623 }
01624 DeleteTexture();
01625 TextureContext::~TextureContext();
01626 _tex = NULL;
01627 }
01628