2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "osal_files.h"
22 #define M64P_PLUGIN_PROTOTYPES 1
23 #include "m64p_plugin.h"
25 #include "ConvertImage.h"
26 #include "DeviceBuilder.h"
27 #include "TextureFilters.h"
31 #include "liblinux/BMGLibPNG.h"
32 #include "liblinux/BMGDLL.h"
33 #include <sys/types.h>
40 /************************************************************************/
42 /************************************************************************/
43 // Basic 2x R8G8B8A8 filter with interpolation
45 void Texture2x_32( DrawInfo &srcInfo, DrawInfo &destInfo)
47 uint32 *pDst1, *pDst2;
49 uint32 nWidth = srcInfo.dwWidth;
50 uint32 nHeight = srcInfo.dwHeight;
70 for (uint32 ySrc = 0; ySrc < nHeight; ySrc++)
72 pSrc = (uint32*)(((uint8*)srcInfo.lpSurface)+ySrc*srcInfo.lPitch);
73 pSrc2 = (uint32*)(((uint8*)srcInfo.lpSurface)+(ySrc+1)*srcInfo.lPitch);
74 pDst1 = (uint32*)(((uint8*)destInfo.lpSurface)+(ySrc*2)*destInfo.lPitch);
75 pDst2 = (uint32*)(((uint8*)destInfo.lpSurface)+(ySrc*2+1)*destInfo.lPitch);
77 for (uint32 xSrc = 0; xSrc < nWidth; xSrc++)
79 b1 = (pSrc[xSrc]>>0)&0xFF;
80 g1 = (pSrc[xSrc]>>8)&0xFF;
81 r1 = (pSrc[xSrc]>>16)&0xFF;
82 a1 = (pSrc[xSrc]>>24)&0xFF;
86 b2 = (pSrc[xSrc+1]>>0)&0xFF;
87 g2 = (pSrc[xSrc+1]>>8)&0xFF;
88 r2 = (pSrc[xSrc+1]>>16)&0xFF;
89 a2 = (pSrc[xSrc+1]>>24)&0xFF;
94 b3 = (pSrc2[xSrc]>>0)&0xFF;
95 g3 = (pSrc2[xSrc]>>8)&0xFF;
96 r3 = (pSrc2[xSrc]>>16)&0xFF;
97 a3 = (pSrc2[xSrc]>>24)&0xFF;
100 b4 = (pSrc2[xSrc+1]>>0)&0xFF;
101 g4 = (pSrc2[xSrc+1]>>8)&0xFF;
102 r4 = (pSrc2[xSrc+1]>>16)&0xFF;
103 a4 = (pSrc2[xSrc+1]>>24)&0xFF;
109 pDst1[xSrc*2] = pSrc[xSrc];
114 pDst1[xSrc*2+1] = DWORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
117 pDst1[xSrc*2+1] = pSrc[xSrc];
123 pDst2[xSrc*2] = DWORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
126 pDst2[xSrc*2] = pSrc[xSrc];
133 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r2+r3+r4)/4, (g1+g2+g3+g4)/4, (b1+b2+b3+b4)/4, (a1+a2+a3+a4)/4);
137 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
144 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
147 pDst2[xSrc*2+1] = pSrc[xSrc];
153 // Basic 2x R4G4B4A4 filter with interpolation
154 void Texture2x_16( DrawInfo &srcInfo, DrawInfo &destInfo )
156 uint16 *pDst1, *pDst2;
157 uint16 *pSrc, *pSrc2;
158 uint32 nWidth = srcInfo.dwWidth;
159 uint32 nHeight = srcInfo.dwHeight;
178 for (uint16 ySrc = 0; ySrc < nHeight; ySrc++)
180 pSrc = (uint16*)(((uint8*)srcInfo.lpSurface)+ySrc*srcInfo.lPitch);
181 pSrc2 = (uint16*)(((uint8*)srcInfo.lpSurface)+(ySrc+1)*srcInfo.lPitch);
182 pDst1 = (uint16*)(((uint8*)destInfo.lpSurface)+(ySrc*2)*destInfo.lPitch);
183 pDst2 = (uint16*)(((uint8*)destInfo.lpSurface)+(ySrc*2+1)*destInfo.lPitch);
185 for (uint16 xSrc = 0; xSrc < nWidth; xSrc++)
187 b1 = (pSrc[xSrc]>> 0)&0xF;
188 g1 = (pSrc[xSrc]>> 4)&0xF;
189 r1 = (pSrc[xSrc]>> 8)&0xF;
190 a1 = (pSrc[xSrc]>>12)&0xF;
194 b2 = (pSrc[xSrc+1]>> 0)&0xF;
195 g2 = (pSrc[xSrc+1]>> 4)&0xF;
196 r2 = (pSrc[xSrc+1]>> 8)&0xF;
197 a2 = (pSrc[xSrc+1]>>12)&0xF;
202 b3 = (pSrc2[xSrc]>> 0)&0xF;
203 g3 = (pSrc2[xSrc]>> 4)&0xF;
204 r3 = (pSrc2[xSrc]>> 8)&0xF;
205 a3 = (pSrc2[xSrc]>>12)&0xF;
208 b4 = (pSrc2[xSrc+1]>> 0)&0xF;
209 g4 = (pSrc2[xSrc+1]>> 4)&0xF;
210 r4 = (pSrc2[xSrc+1]>> 8)&0xF;
211 a4 = (pSrc2[xSrc+1]>>12)&0xF;
216 pDst1[xSrc*2] = pSrc[xSrc];
221 pDst1[xSrc*2+1] = WORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
224 pDst1[xSrc*2+1] = pSrc[xSrc];
230 pDst2[xSrc*2] = WORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
233 pDst2[xSrc*2] = pSrc[xSrc];
240 pDst2[xSrc*2+1] = WORD_MAKE((r1+r2+r3+r4)/4, (g1+g2+g3+g4)/4, (b1+b2+b3+b4)/4, (a1+a2+a3+a4)/4);
244 pDst2[xSrc*2+1] = WORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
251 pDst2[xSrc*2+1] = WORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
254 pDst2[xSrc*2+1] = pSrc[xSrc];
260 /************************************************************************/
261 /* Sharpen filters */
262 /************************************************************************/
263 void SharpenFilter_32(uint32 *pdata, uint32 width, uint32 height, uint32 pitch, uint32 filter)
265 uint32 len=height*pitch;
266 uint32 *pcopy = new uint32[len];
270 memcpy(pcopy, pdata, len<<2);
272 uint32 mul1, mul2, mul3, shift4;
275 case TEXTURE_SHARPEN_MORE_ENHANCEMENT:
281 case TEXTURE_SHARPEN_ENHANCEMENT:
291 uint32 *src1, *src2, *src3, *dest;
293 uint32 t1,t2,t3,t4,t5,t6,t7,t8,t9;
295 for( y=1; y<height-1; y++)
297 dest = pdata+y*pitch;
298 src1 = pcopy+(y-1)*pitch;
301 for( x=1; x<width-1; x++)
305 t1 = *((uint8*)(src1+x-1)+z);
306 t2 = *((uint8*)(src1+x )+z);
307 t3 = *((uint8*)(src1+x+1)+z);
308 t4 = *((uint8*)(src2+x-1)+z);
309 t5 = *((uint8*)(src2+x )+z);
310 t6 = *((uint8*)(src2+x+1)+z);
311 t7 = *((uint8*)(src3+x-1)+z);
312 t8 = *((uint8*)(src3+x )+z);
313 t9 = *((uint8*)(src3+x+1)+z);
315 if( (t5*mul2) > (t1+t3+t7+t9+t2+t4+t6+t8)*mul1 )
317 val[z]= std::min((((t5*mul3) - (t1+t3+t7+t9+t2+t4+t6+t8)*mul1)>>shift4),0xFFU);
320 dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
326 void SharpenFilter_16(uint16 *pdata, uint32 width, uint32 height, uint32 pitch, uint32 filter)
328 //return; // Sharpen does not make sense for 16 bits
330 uint32 len=height*pitch;
331 uint16 *pcopy = new uint16[len];
335 memcpy(pcopy, pdata, len<<1);
337 uint16 mul1, mul2, mul3, shift4;
340 case TEXTURE_SHARPEN_MORE_ENHANCEMENT:
346 case TEXTURE_SHARPEN_ENHANCEMENT:
356 uint16 *src1, *src2, *src3, *dest;
358 uint16 t1,t2,t3,t4,t5,t6,t7,t8,t9;
360 for( y=1; y<height-1; y++)
362 dest = pdata+y*pitch;
363 src1 = pcopy+(y-1)*pitch;
366 for( x=1; x<width-1; x++)
370 uint32 shift = (z%1)?4:0;
371 t1 = (*((uint8*)(src1+x-1)+(z>>1)))>>shift;
372 t2 = (*((uint8*)(src1+x )+(z>>1)))>>shift;
373 t3 = (*((uint8*)(src1+x+1)+(z>>1)))>>shift;
374 t4 = (*((uint8*)(src2+x-1)+(z>>1)))>>shift;
375 t5 = (*((uint8*)(src2+x )+(z>>1)))>>shift;
376 t6 = (*((uint8*)(src2+x+1)+(z>>1)))>>shift;
377 t7 = (*((uint8*)(src3+x-1)+(z>>1)))>>shift;
378 t8 = (*((uint8*)(src3+x )+(z>>1)))>>shift;
379 t9 = (*((uint8*)(src3+x+1)+(z>>1)))>>shift;
381 if( (t5*mul2) > (t1+t3+t7+t9+t2+t4+t6+t8)*mul1 )
383 val[z] = (((t5*mul3) - (t1+t3+t7+t9+t2+t4+t6+t8)*mul1)>>shift4);
384 val[z]= std::min(val[z],(unsigned short)0xFU);
387 dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
393 /************************************************************************/
395 /************************************************************************/
396 void SmoothFilter_32(uint32 *pdata, uint32 width, uint32 height, uint32 pitch, uint32 filter)
398 uint32 len=height*pitch;
399 uint32 *pcopy = new uint32[len];
403 memcpy(pcopy, pdata, len<<2);
405 uint32 mul1, mul2, mul3, shift4;
408 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_1:
414 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_2:
420 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_3:
426 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_4:
436 uint32 *src1, *src2, *src3, *dest;
438 uint32 t1,t2,t3,t4,t5,t6,t7,t8,t9;
440 if( filter == TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_3 || filter == TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_4 )
442 for( y=1; y<height-1; y+=2)
444 dest = pdata+y*pitch;
445 src1 = pcopy+(y-1)*pitch;
448 for( x=0; x<width; x++)
452 t2 = *((uint8*)(src1+x )+z);
453 t5 = *((uint8*)(src2+x )+z);
454 t8 = *((uint8*)(src3+x )+z);
455 val[z] = ((t2+t8)*mul2+(t5*mul3))>>shift4;
457 dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
463 for( y=0; y<height; y++)
465 dest = pdata+y*pitch;
468 src1 = pcopy+(y-1)*pitch;
477 if( y<height-1) src3 += pitch;
479 for( x=1; x<width-1; x++)
483 t1 = *((uint8*)(src1+x-1)+z);
484 t2 = *((uint8*)(src1+x )+z);
485 t3 = *((uint8*)(src1+x+1)+z);
486 t4 = *((uint8*)(src2+x-1)+z);
487 t5 = *((uint8*)(src2+x )+z);
488 t6 = *((uint8*)(src2+x+1)+z);
489 t7 = *((uint8*)(src3+x-1)+z);
490 t8 = *((uint8*)(src3+x )+z);
491 t9 = *((uint8*)(src3+x+1)+z);
492 val[z] = ((t1+t3+t7+t9)*mul1+((t2+t4+t6+t8)*mul2)+(t5*mul3))>>shift4;
494 dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
501 void SmoothFilter_16(uint16 *pdata, uint32 width, uint32 height, uint32 pitch, uint32 filter)
503 uint32 len=height*pitch;
504 uint16 *pcopy = new uint16[len];
509 memcpy(pcopy, pdata, len<<1);
511 uint16 mul1, mul2, mul3, shift4;
514 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_1:
520 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_2:
526 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_3:
532 case TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_4:
542 uint16 *src1, *src2, *src3, *dest;
544 uint16 t1,t2,t3,t4,t5,t6,t7,t8,t9;
546 if( filter == TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_3 || filter == TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_4 )
548 for( y=1; y<height-1; y+=2)
550 dest = pdata+y*pitch;
551 src1 = pcopy+(y-1)*pitch;
554 for( x=0; x<width; x++)
558 uint32 shift = (z&1)?4:0;
559 t2 = (*((uint8*)(src1+x )+(z>>1)))>>shift;
560 t5 = (*((uint8*)(src2+x )+(z>>1)))>>shift;
561 t8 = (*((uint8*)(src3+x )+(z>>1)))>>shift;
562 val[z] = ((t2+t8)*mul2+(t5*mul3))>>shift4;
564 dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
570 for( y=0; y<height; y++)
572 dest = pdata+y*pitch;
575 src1 = pcopy+(y-1)*pitch;
584 if( y<height-1) src3 += pitch;
586 for( x=1; x<width-1; x++)
590 uint32 shift = (z&1)?4:0;
591 t1 = (*((uint8*)(src1+x-1)+(z>>1)))>>shift;
592 t2 = (*((uint8*)(src1+x )+(z>>1)))>>shift;
593 t3 = (*((uint8*)(src1+x+1)+(z>>1)))>>shift;
594 t4 = (*((uint8*)(src2+x-1)+(z>>1)))>>shift;
595 t5 = (*((uint8*)(src2+x )+(z>>1)))>>shift;
596 t6 = (*((uint8*)(src2+x+1)+(z>>1)))>>shift;
597 t7 = (*((uint8*)(src3+x-1)+(z>>1)))>>shift;
598 t8 = (*((uint8*)(src3+x )+(z>>1)))>>shift;
599 t9 = (*((uint8*)(src3+x+1)+(z>>1)))>>shift;
600 val[z] = ((t1+t3+t7+t9)*mul1+((t2+t4+t6+t8)*mul2)+(t5*mul3))>>shift4;
602 dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
610 void EnhanceTexture(TxtrCacheEntry *pEntry)
612 if( pEntry->dwEnhancementFlag == options.textureEnhancement )
614 // The texture has already been enhanced
617 else if( options.textureEnhancement == TEXTURE_NO_ENHANCEMENT )
619 //Texture enhancement has being turned off
620 //Delete any allocated memory for the enhanced texture
621 SAFE_DELETE(pEntry->pEnhancedTexture);
622 //Set the enhancement flag so the texture wont be processed again
623 pEntry->dwEnhancementFlag = TEXTURE_NO_ENHANCEMENT;
627 if( status.primitiveType != PRIM_TEXTRECT && options.bTexRectOnly )
633 //Start the draw update
634 if( pEntry->pTexture->StartUpdate(&srcInfo) == false )
636 //If we get here we were unable to start the draw update
637 //Delete any allocated memory for the enhanced texture
638 SAFE_DELETE(pEntry->pEnhancedTexture);
642 uint32 realwidth = srcInfo.dwWidth;
643 uint32 realheight = srcInfo.dwHeight;
644 uint32 nWidth = srcInfo.dwCreatedWidth;
645 uint32 nHeight = srcInfo.dwCreatedHeight;
647 //Sharpen option is enabled, sharpen the texture
648 if( options.textureEnhancement == TEXTURE_SHARPEN_ENHANCEMENT || options.textureEnhancement == TEXTURE_SHARPEN_MORE_ENHANCEMENT )
650 if( pEntry->pTexture->GetPixelSize() == 4 )
651 SharpenFilter_32((uint32*)srcInfo.lpSurface, nWidth, nHeight, nWidth, options.textureEnhancement);
653 SharpenFilter_16((uint16*)srcInfo.lpSurface, nWidth, nHeight, nWidth, options.textureEnhancement);
654 pEntry->dwEnhancementFlag = options.textureEnhancement;
655 //End the draw update
656 pEntry->pTexture->EndUpdate(&srcInfo);
657 //Delete any allocated memory for the enhanced texture
658 SAFE_DELETE(pEntry->pEnhancedTexture);
662 pEntry->dwEnhancementFlag = options.textureEnhancement;
663 if( options.bSmallTextureOnly )
665 if( nWidth + nHeight > 256 )
667 //End the draw update
668 pEntry->pTexture->EndUpdate(&srcInfo);
669 //Delete any data allocated for the enhanced texture
670 SAFE_DELETE(pEntry->pEnhancedTexture);
671 //Set the enhancement flag so the texture wont be processed again
672 pEntry->dwEnhancementFlag = TEXTURE_NO_ENHANCEMENT;
678 CTexture* pSurfaceHandler = NULL;
679 if( options.textureEnhancement == TEXTURE_HQ4X_ENHANCEMENT )
681 if( nWidth + nHeight > 1024/4 )
683 // Don't enhance for large textures
684 pEntry->pTexture->EndUpdate(&srcInfo);
685 SAFE_DELETE(pEntry->pEnhancedTexture);
686 pEntry->dwEnhancementFlag = TEXTURE_NO_ENHANCEMENT;
689 pSurfaceHandler = CDeviceBuilder::GetBuilder()->CreateTexture(nWidth*4, nHeight*4);
693 if( nWidth + nHeight > 1024/2 )
695 // Don't enhance for large textures
696 pEntry->pTexture->EndUpdate(&srcInfo);
697 SAFE_DELETE(pEntry->pEnhancedTexture);
698 pEntry->dwEnhancementFlag = TEXTURE_NO_ENHANCEMENT;
701 pSurfaceHandler = CDeviceBuilder::GetBuilder()->CreateTexture(nWidth*2, nHeight*2);
706 if(pSurfaceHandler->StartUpdate(&destInfo))
708 if( options.textureEnhancement == TEXTURE_2XSAI_ENHANCEMENT )
710 if( pEntry->pTexture->GetPixelSize() == 4 )
711 Super2xSaI_32((uint32*)(srcInfo.lpSurface),(uint32*)(destInfo.lpSurface), nWidth, realheight, nWidth);
713 Super2xSaI_16((uint16*)(srcInfo.lpSurface),(uint16*)(destInfo.lpSurface), nWidth, realheight, nWidth);
715 else if( options.textureEnhancement == TEXTURE_HQ2X_ENHANCEMENT )
717 if( pEntry->pTexture->GetPixelSize() == 4 )
720 hq2x_32((uint8*)(srcInfo.lpSurface), srcInfo.lPitch, (uint8*)(destInfo.lpSurface), destInfo.lPitch, nWidth, realheight);
725 hq2x_16((uint8*)(srcInfo.lpSurface), srcInfo.lPitch, (uint8*)(destInfo.lpSurface), destInfo.lPitch, nWidth, realheight);
728 else if( options.textureEnhancement == TEXTURE_LQ2X_ENHANCEMENT )
730 if( pEntry->pTexture->GetPixelSize() == 4 )
733 lq2x_32((uint8*)(srcInfo.lpSurface), srcInfo.lPitch, (uint8*)(destInfo.lpSurface), destInfo.lPitch, nWidth, realheight);
738 lq2x_16((uint8*)(srcInfo.lpSurface), srcInfo.lPitch, (uint8*)(destInfo.lpSurface), destInfo.lPitch, nWidth, realheight);
741 else if( options.textureEnhancement == TEXTURE_HQ4X_ENHANCEMENT )
743 if( pEntry->pTexture->GetPixelSize() == 4 )
746 hq4x_32((uint8*)(srcInfo.lpSurface), (uint8*)(destInfo.lpSurface), realwidth, realheight, nWidth, destInfo.lPitch);
751 hq4x_16((uint8*)(srcInfo.lpSurface), (uint8*)(destInfo.lpSurface), realwidth, realheight, nWidth, destInfo.lPitch);
756 if( pEntry->pTexture->GetPixelSize() == 4 )
757 Texture2x_32( srcInfo, destInfo);
759 Texture2x_16( srcInfo, destInfo);
762 if( options.textureEnhancementControl >= TEXTURE_ENHANCEMENT_WITH_SMOOTH_FILTER_1 )
764 if( options.textureEnhancement != TEXTURE_HQ4X_ENHANCEMENT )
766 if( pEntry->pTexture->GetPixelSize() == 4 )
767 SmoothFilter_32((uint32*)destInfo.lpSurface, realwidth<<1, realheight<<1, nWidth<<1, options.textureEnhancementControl);
769 SmoothFilter_16((uint16*)destInfo.lpSurface, realwidth<<1, realheight<<1, nWidth<<1, options.textureEnhancementControl);
773 if( pEntry->pTexture->GetPixelSize() == 4 )
774 SmoothFilter_32((uint32*)destInfo.lpSurface, realwidth<<2, realheight<<2, nWidth<<2, options.textureEnhancementControl);
776 SmoothFilter_16((uint16*)destInfo.lpSurface, realwidth<<2, realheight<<2, nWidth<<2, options.textureEnhancementControl);
780 pSurfaceHandler->EndUpdate(&destInfo);
783 pSurfaceHandler->SetOthersVariables();
784 pSurfaceHandler->m_bIsEnhancedTexture = true;
787 pEntry->pTexture->EndUpdate(&srcInfo);
789 pEntry->pEnhancedTexture = pSurfaceHandler;
793 /************************************************************************/
795 /************************************************************************/
796 void MirrorEmulator_DrawLine(DrawInfo& destInfo, DrawInfo& srcInfo, uint32 *pSource, uint32 *pDest, uint32 nWidth, BOOL bFlipLeftRight)
800 memcpy(pDest, pSource, nWidth * 4);
804 uint32 *pMaxDest = pDest + nWidth;
805 pSource += nWidth - 1;
806 for(; pDest < pMaxDest; pDest++, pSource--)
814 void MirrorEmulator_Draw(DrawInfo& destInfo, DrawInfo& srcInfo, uint32 nDestX, uint32 nDestY, BOOL bFlipLeftRight, BOOL bFlipUpDown)
816 uint8 *pDest = (uint8 *) destInfo.lpSurface + (destInfo.lPitch * nDestY) + (4 * nDestX);
817 uint8 *pMaxDest = pDest + (destInfo.lPitch * srcInfo.dwHeight);
818 uint8 *pSource = (uint8 *)(srcInfo.lpSurface);
821 for(; pDest < pMaxDest; pDest += destInfo.lPitch, pSource += srcInfo.lPitch)
823 MirrorEmulator_DrawLine(destInfo, srcInfo, (uint32*)pSource, (uint32*)pDest, srcInfo.dwWidth, bFlipLeftRight);
828 pSource += (srcInfo.lPitch * (srcInfo.dwHeight - 1));
829 for(; pDest < pMaxDest; pDest += destInfo.lPitch, pSource -= srcInfo.lPitch)
831 MirrorEmulator_DrawLine(destInfo, srcInfo, (uint32*)pSource, (uint32*)pDest, srcInfo.dwWidth, bFlipLeftRight);
836 void MirrorTexture(uint32 dwTile, TxtrCacheEntry *pEntry)
838 if( ((gRDP.tiles[dwTile].bMirrorS) || (gRDP.tiles[dwTile].bMirrorT)) && CGraphicsContext::Get()->m_supportTextureMirror == false )
840 if(pEntry->pEnhancedTexture)
846 CTexture* pSurfaceHandler = NULL;
848 // FIXME: Compute the correct values. 2/2 seems to always work correctly in Mario64
849 uint32 nXTimes = gRDP.tiles[dwTile].bMirrorS ? 2 : 1;
850 uint32 nYTimes = gRDP.tiles[dwTile].bMirrorT ? 2 : 1;
852 // For any texture need to use mirror, we should not need to rescale it
853 // because texture need to be mirrored must with MaskS and MaskT
855 // But again, check me
857 //if( pEntry->pTexture->m_bScaledS == false || pEntry->pTexture->m_bScaledT == false)
859 // pEntry->pTexture->ScaleImageToSurface();
863 if( pEntry->pTexture->StartUpdate(&srcInfo) )
865 uint32 nWidth = srcInfo.dwWidth;
866 uint32 nHeight = srcInfo.dwHeight;
868 pSurfaceHandler = CDeviceBuilder::GetBuilder()->CreateTexture(nWidth * nXTimes, nHeight * nYTimes);
869 if( pSurfaceHandler )
872 if( pSurfaceHandler->StartUpdate(&destInfo) )
874 for(uint32 nY = 0; nY < nYTimes; nY++)
876 for(uint32 nX = 0; nX < nXTimes; nX++)
878 MirrorEmulator_Draw(destInfo, srcInfo, nWidth * nX, nHeight * nY, nX & 0x1, nY & 0x1);
882 pSurfaceHandler->EndUpdate(&destInfo);
885 // FIXME: There should be a flag to tell that it is a mirrored texture handler
886 // not the original texture handlers, so all texture coordinate should be divided by 2
887 pSurfaceHandler->SetOthersVariables();
890 pEntry->pTexture->EndUpdate(&srcInfo);
891 pEntry->dwEnhancementFlag = TEXTURE_MIRRORED;
895 pEntry->pEnhancedTexture = pSurfaceHandler;
900 All code bellow, CLEAN ME
908 RGB_WITH_ALPHA_TOGETHER_PNG,
924 bool bSeparatedAlpha;
927 CSortedList<uint64,ExtTxtrInfo> gTxtrDumpInfos;
928 CSortedList<uint64,ExtTxtrInfo> gHiresTxtrInfos;
930 extern char * right(const char * src, int nchars);
932 #define SURFFMT_P8 41
934 int GetImageInfoFromFile(char* pSrcFile, IMAGE_INFO *pSrcInfo)
936 unsigned char sig[8];
939 f = fopen(pSrcFile, "rb");
942 DebugMessage(M64MSG_ERROR, "GetImageInfoFromFile() error: couldn't open file '%s'", pSrcFile);
945 if (fread(sig, 1, 8, f) != 8)
947 DebugMessage(M64MSG_ERROR, "GetImageInfoFromFile() error: couldn't read first 8 bytes of file '%s'", pSrcFile);
953 if(sig[0] == 'B' && sig[1] == 'M') // BMP
955 struct BMGImageStruct img;
956 memset(&img, 0, sizeof(BMGImageStruct));
957 BMG_Error code = ReadBMP(pSrcFile, &img);
960 pSrcInfo->Width = img.width;
961 pSrcInfo->Height = img.height;
962 pSrcInfo->Depth = img.bits_per_pixel;
963 pSrcInfo->MipLevels = 1;
964 if(img.bits_per_pixel == 32)
965 pSrcInfo->Format = SURFFMT_A8R8G8B8;
966 else if(img.bits_per_pixel == 8)
967 pSrcInfo->Format = SURFFMT_P8;
968 // Resource and File Format ignored
972 DebugMessage(M64MSG_ERROR, "Couldn't read BMP file '%s'; error = %i", pSrcFile, code);
975 else if(sig[0] == 137 && sig[1] == 'P' && sig[2] == 'N' && sig[3] == 'G' && sig[4] == '\r' && sig[5] == '\n' &&
976 sig[6] == 26 && sig[7] == '\n') // PNG
978 struct BMGImageStruct img;
979 memset(&img, 0, sizeof(BMGImageStruct));
980 BMG_Error code = ReadPNGInfo(pSrcFile, &img);
983 pSrcInfo->Width = img.width;
984 pSrcInfo->Height = img.height;
985 pSrcInfo->Depth = img.bits_per_pixel;
986 pSrcInfo->MipLevels = 1;
987 if(img.bits_per_pixel == 32)
988 pSrcInfo->Format = SURFFMT_A8R8G8B8;
989 else if(img.bits_per_pixel == 8)
990 pSrcInfo->Format = SURFFMT_P8;
991 // Resource and File Format ignored
995 DebugMessage(M64MSG_ERROR, "Couldn't read PNG file '%s'; error = %i", pSrcFile, code);
999 DebugMessage(M64MSG_ERROR, "GetImageInfoFromFile : unknown file format (%s)", pSrcFile);
1003 BOOL PathFileExists(char* pszPath)
1006 f = fopen(pszPath, "rb");
1015 /********************************************************************************************************************
1016 * Truncates the current list with information about hires textures and scans the hires folder for hires textures and
1017 * creates a list with records of properties of the hires textures.
1019 * foldername: the folder that should be scaned for valid hires textures.
1020 * infos: a pointer that will point to the list containing the records with the infos about the found hires textures.
1021 * In case of enabled caching, these records will also contain the actual textures.
1023 * bRecursive: flag that indicates if also subfolders should be scanned for hires textures
1024 * bCacheTextures: flag that indicates if the identified hires textures should also be cached
1025 * bMainFolder: indicates if the folder is the main folder that will be scanned. That way, texture counting does not
1026 * start at 1 each time a subfolder is accessed. (microdev: I know that is not important but it really
1029 * infos: the list with the records of the identified hires textures. Be aware that these records also contains the
1030 * actual textures if caching is enabled.
1031 ********************************************************************************************************************/
1032 void FindAllTexturesFromFolder(char *foldername, CSortedList<uint64,ExtTxtrInfo> &infos, bool extraCheck, bool bRecursive)
1034 // check if folder actually exists
1035 if (!osal_is_directory(foldername))
1038 // the path of the texture
1039 char texturefilename[PATH_MAX];
1043 IMAGE_INFO imgInfo2;
1046 dir = osal_search_dir_open(foldername);
1047 const char *foundfilename;
1050 unsigned int fmt, siz;
1051 char crcstr[16], crcstr2[16];
1055 foundfilename = osal_search_dir_read_next(dir);
1057 // The array is empty, break the current operation
1058 if (foundfilename == NULL)
1060 // The current file is a hidden one
1061 if (foundfilename[0] == '.' )
1062 // These files we don't need
1065 // Get the folder name
1066 strcpy(texturefilename, foldername);
1067 // And append the file name
1068 strcat(texturefilename, foundfilename);
1070 // Check if the current file is a directory and if recursive scanning is enabled
1071 if (osal_is_directory(texturefilename) && bRecursive )
1073 // Add file-separator
1074 strcat(texturefilename, OSAL_DIR_SEPARATOR_STR);
1075 // Scan detected folder for hires textures (recursive call)
1076 FindAllTexturesFromFolder(texturefilename, infos, extraCheck, bRecursive);
1079 // well, the current file is actually no file (probably a directory & recursive scanning is not enabled)
1080 if( strstr(foundfilename,(const char*)g_curRomInfo.szGameName) == 0 )
1081 // go on with the next one
1084 TextureType type = NO_TEXTURE;
1085 bool bSeparatedAlpha = false;
1087 // Detect the texture type by it's extention
1088 // microdev: this is not the smartest way. Should be done by header analysis if possible
1089 if( strcasecmp(right(foundfilename,7), "_ci.bmp") == 0 )
1092 if( GetImageInfoFromFile(texturefilename, &imgInfo) != 0)
1094 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", foundfilename);
1098 if( imgInfo.Format == SURFFMT_P8 )
1099 // and store it to the record
1100 type = COLOR_INDEXED_BMP;
1102 // Type is not supported, go on with the next one
1105 // Detect the texture type by its extention
1106 else if( strcasecmp(right(foundfilename,13), "_ciByRGBA.png") == 0 )
1109 if( GetImageInfoFromFile(texturefilename, &imgInfo) != 0 )
1111 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", foundfilename);
1115 if( imgInfo.Format == SURFFMT_A8R8G8B8 )
1116 // and store it to the record
1117 type = RGBA_PNG_FOR_CI;
1119 // Type is not supported, go on with the next one
1122 // Detect the texture type by its extention
1123 else if( strcasecmp(right(foundfilename,16), "_allciByRGBA.png") == 0 )
1126 if( GetImageInfoFromFile(texturefilename, &imgInfo) != 0 )
1128 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", foundfilename);
1131 if( imgInfo.Format == SURFFMT_A8R8G8B8 )
1132 // and store it to the record
1133 type = RGBA_PNG_FOR_ALL_CI;
1135 // Type not supported, go on with next one
1138 // Detect the texture type by its extention
1139 else if( strcasecmp(right(foundfilename,8), "_rgb.png") == 0 )
1142 if( GetImageInfoFromFile(texturefilename, &imgInfo) != 0 )
1144 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", foundfilename);
1148 // Store type to the record
1151 char filename2[PATH_MAX];
1152 // Assemble the file name for the separate alpha channel file
1153 strcpy(filename2,texturefilename);
1154 strcpy(filename2+strlen(filename2)-8,"_a.png");
1155 // Check if the file actually exists
1156 if( PathFileExists(filename2) )
1158 // Check if the file with this name is actually a texture (well an alpha channel one)
1159 if( GetImageInfoFromFile(filename2, &imgInfo2) != 0 )
1161 // Nope, it isn't. => Go on with the next file
1162 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", filename2);
1166 // Yes it is a texture file. Check if the size of the alpha channel is the same as the one of the texture
1167 if( extraCheck && (imgInfo2.Width != imgInfo.Width || imgInfo2.Height != imgInfo.Height) )
1169 // Nope, it isn't => go on with next file
1170 DebugMessage(M64MSG_WARNING, "RGB and alpha texture size mismatch: %s", filename2);
1174 bSeparatedAlpha = true;
1177 // Detect the texture type by its extention
1178 else if( strcasecmp(right(foundfilename,8), "_all.png") == 0 )
1180 // Check if texture is of expected type
1181 if( GetImageInfoFromFile(texturefilename, &imgInfo) != 0 )
1183 DebugMessage(M64MSG_WARNING, "Cannot get image info for file: %s", foundfilename);
1184 // Nope, continue with next file
1188 // Indicate file type
1189 type = RGB_WITH_ALPHA_TOGETHER_PNG;
1192 // If a known texture format has been detected...
1193 if( type != NO_TEXTURE )
1196 Try to read image information here.
1198 (CASTLEVANIA2)#(58E2333F)#(2)#(0)#(D7A5C6D9)_ciByRGBA.png
1199 (------1-----)#(---2----)#(3)#(4)#(---5----)_ciByRGBA.png
1201 1. Internal ROM name
1203 3. The image pixel size (8b=0, 16b=1, 24b=2, 32b=3)
1204 4. The texture format (RGBA=0, YUV=1, CI=2, IA=3, I=4)
1207 <internal Rom name>#<DRAM CRC>#<24bit>#<RGBA>#<PAL CRC>_ciByRGBA.png
1210 // Get the actual file name
1211 strcpy(texturefilename, foundfilename);
1212 // Place the pointer before the DRAM-CRC (first occurrence of '#')
1213 char *ptr = strchr(texturefilename,'#');
1214 // Terminate the string ('0' means end of string - or in this case begin of string)
1216 if( type == RGBA_PNG_FOR_CI )
1218 // Extract the information from the file name; information is:
1219 // <DRAM(or texture)-CRC><texture type><texture format><PAL(or palette)-CRC>
1220 sscanf(ptr,"%8c#%d#%d#%8c", crcstr, &fmt, &siz,crcstr2);
1221 // Terminate the ascii represntation of the palette crc
1223 // Transform the ascii presentation of the hex value to an unsigned integer
1224 palcrc32 = strtoul(crcstr2,NULL,16);
1228 // Extract the information from the file name - this file does not have a palette crc; information is:
1229 // <DRAM(or texture)-CRC><texture type><texture format>
1230 // o gosh, commenting source code is really boring - but necessary!! Thus do it! (and don't use drugs ;-))
1231 sscanf(ptr,"%8c#%d#%d", crcstr, &fmt, &siz);
1232 // Use dummy for palette crc - that way each texture can be handled in a heterogeneous way
1233 palcrc32 = 0xFFFFFFFF;
1235 // Terminate the ascii represntation of the texture crc
1237 // Transform the ascii presentation of the hex value to an unsigned integer
1238 crc = strtoul(crcstr,NULL,16);
1239 // For the detection of an existing item
1242 for( int k=0; k<infos.size(); k++)
1244 // Check if texture already exists in the list
1245 // microdev: that's why I somehow love documenting code: that makes the implementation of a WIP folder check
1247 if( infos[k].crc32 == crc && infos[k].pal_crc32 == palcrc32 )
1249 // Indeeed, the texture already exists
1250 // microdev: MAYBE ADD CODE TO MOVE IT TO A 'DUBLICATE' FOLDER TO EASE WORK OF RETEXTURERS
1256 if( foundIdx < 0 || type != infos[foundIdx].type)
1258 // Create a new entry
1259 ExtTxtrInfo newinfo;
1261 newinfo.width = imgInfo.Width;
1263 newinfo.height = imgInfo.Height;
1264 // Store the name of the folder it has been found in
1265 //strcpy(newinfo.name,g_curRomInfo.szGameName);
1266 newinfo.foldername = new char[strlen(foldername)+1];
1267 strcpy(newinfo.foldername,foldername);
1268 // store the filename
1269 newinfo.filename = strdup(foundfilename);
1270 newinfo.filename_a = NULL;
1273 // Store the size (bit-size, not texture size)
1275 // Store DRAM (texture) CRC
1276 newinfo.crc32 = crc;
1277 // Store PAL (palette) CRC (the actual one, or the dummy value ('FFFFFFFF'))
1278 newinfo.pal_crc32 = palcrc32;
1279 // Store the texture type
1280 newinfo.type = type;
1281 //Indicate if there is a separate alpha file that has to be loaded
1282 newinfo.bSeparatedAlpha = bSeparatedAlpha;
1283 if (bSeparatedAlpha) {
1284 char filename2[PATH_MAX];
1285 strcpy(filename2, foundfilename);
1286 strcpy(filename2+strlen(filename2)-8,"_a.png");
1287 newinfo.filename_a = strdup(filename2);
1289 // Generate the key for the record describing the hires texture.
1290 // This key is used to find it back in the list
1291 // The key format is: <DRAM(texture)-CRC-8byte><PAL(palette)-CRC-6byte(2bytes have been truncated to have space for format and size)><format-1byte><size-1byte>
1292 uint64 crc64 = newinfo.crc32;
1294 if (options.bLoadHiResCRCOnly)
1295 crc64 |= newinfo.pal_crc32&0xFFFFFFFF;
1297 crc64 |= (newinfo.pal_crc32&0xFFFFFF00)|(newinfo.fmt<<4)|newinfo.siz;
1298 // Add the new record to the list
1299 infos.add(crc64,newinfo);
1302 } while(foundfilename != NULL);
1304 osal_search_dir_close(dir);
1306 /********************************************************************************************************************
1307 * Checks if a folder is actually existant. If not, it tries to create this folder
1309 * pathname: the name of the folder that should be checked or created if not existant
1311 * return value: flag that indicates true if the folder is existant or could be created. If none was the case,
1312 * false will be returned
1313 ********************************************************************************************************************/
1315 bool CheckAndCreateFolder(const char* pathname)
1317 // Check if provided folder already exists
1318 if( !PathFileExists((char*)pathname) )
1320 // It didn't. Try creating it.
1321 if (osal_mkdirp(pathname, 0700) != 0)
1323 // It didn't work (probably insufficient permissions or read-only media) ==> return false
1324 DebugMessage(M64MSG_WARNING, "Can not create new folder: %s", pathname);
1332 // microdev: THIS HAS TO BE CLEANED UP...
1335 // Texture dumping filenaming
1336 // GameName_FrameCount_CRC_Fmt_Siz.bmp
1338 // GameName: N64 game internal name
1339 // CRC: 32 bit, 8 hex digits
1343 const char *subfolders[] = {
1347 "ci_bmp_with_pal_crc",
1351 void FindAllDumpedTextures(void)
1353 char foldername[PATH_MAX + 64];
1354 strncpy(foldername, ConfigGetUserDataPath(), PATH_MAX);
1355 foldername[PATH_MAX] = 0;
1357 if (foldername[strlen(foldername) - 1] != OSAL_DIR_SEPARATOR_CHAR)
1358 strcat(foldername, OSAL_DIR_SEPARATOR_STR);
1359 strcat(foldername,"texture_dump" OSAL_DIR_SEPARATOR_STR);
1361 CheckAndCreateFolder(foldername);
1363 strcat(foldername,(const char*)g_curRomInfo.szGameName);
1364 strcat(foldername, OSAL_DIR_SEPARATOR_STR);
1366 gTxtrDumpInfos.clear();
1367 if( !PathFileExists(foldername) )
1369 CheckAndCreateFolder(foldername);
1370 char foldername2[PATH_MAX];
1371 for( int i=0; i<5; i++)
1373 strcpy(foldername2,foldername);
1374 strcat(foldername2,subfolders[i]);
1375 CheckAndCreateFolder(foldername2);
1381 gTxtrDumpInfos.clear();
1382 FindAllTexturesFromFolder(foldername,gTxtrDumpInfos, false, true);
1384 char foldername2[PATH_MAX];
1385 for( int i=0; i<5; i++)
1387 strcpy(foldername2,foldername);
1388 strcat(foldername2,subfolders[i]);
1389 CheckAndCreateFolder(foldername2);
1394 /********************************************************************************************************************
1395 * Truncates the current list with information about hires textures and scans the hires folder for hires textures and
1396 * creates a list with records of properties of the hires textures.
1401 ********************************************************************************************************************/
1402 void FindAllHiResTextures(void)
1404 char foldername[PATH_MAX + 64];
1405 strncpy(foldername, ConfigGetUserDataPath(), PATH_MAX);
1406 foldername[PATH_MAX] = 0;
1408 // Assure that a backslash exists at the end (should be handled by GetPluginDir())
1409 if(foldername[strlen(foldername) - 1] != OSAL_DIR_SEPARATOR_CHAR)
1410 strcat(foldername, OSAL_DIR_SEPARATOR_STR);
1411 // Add the relative path to the hires folder
1412 strcat(foldername,"hires_texture" OSAL_DIR_SEPARATOR_STR);
1413 // It does not exist? => Create it
1414 CheckAndCreateFolder(foldername);
1416 // Add the path to a sub-folder corresponding to the rom name
1417 // HOOK IN: PACK SELECT
1418 strcat(foldername,(const char*)g_curRomInfo.szGameName);
1419 strcat(foldername, OSAL_DIR_SEPARATOR_STR);
1420 // Truncate the current list with the hires texture info
1421 gHiresTxtrInfos.clear();
1422 if (!osal_is_directory(foldername))
1424 DebugMessage(M64MSG_WARNING, "Couldn't open hi-res texture directory: %s", foldername);
1429 // Find all hires textures and also cache them if configured to do so
1430 FindAllTexturesFromFolder(foldername,gHiresTxtrInfos, true, true);
1434 void CloseHiresTextures(void)
1436 for( int i=0; i<gHiresTxtrInfos.size(); i++)
1438 if( gHiresTxtrInfos[i].foldername )
1439 delete [] gHiresTxtrInfos[i].foldername;
1440 if( gHiresTxtrInfos[i].filename )
1441 delete [] gHiresTxtrInfos[i].filename;
1442 if( gHiresTxtrInfos[i].filename_a )
1443 delete [] gHiresTxtrInfos[i].filename_a;
1446 gHiresTxtrInfos.clear();
1449 void CloseTextureDump(void)
1451 for( int i=0; i<gTxtrDumpInfos.size(); i++)
1453 if( gTxtrDumpInfos[i].foldername )
1454 delete [] gTxtrDumpInfos[i].foldername;
1455 if( gTxtrDumpInfos[i].filename )
1456 delete [] gTxtrDumpInfos[i].filename;
1457 if( gTxtrDumpInfos[i].filename_a )
1458 delete [] gTxtrDumpInfos[i].filename_a;
1461 gTxtrDumpInfos.clear();
1464 void CloseExternalTextures(void)
1466 CloseHiresTextures();
1470 /********************************************************************************************************************
1471 * Scans the hires folder for hires textures and creates a list with records of properties of the hires textures.
1472 * in case of enabled hires caching also the actual hires textures will be added to the record. Before textures will
1473 * be loaded, existing list of texture information will be truncated.
1475 * bWIPFolder: Indicates if all textures should be inited or just the WIP folder. Just the content of the WIP folder
1476 * will be reloaded if a savestate has been loaded or if there has been a switch between window and full-
1477 * screen mode. (Not implemented yet)
1480 ********************************************************************************************************************/
1481 void InitHiresTextures(void)
1483 if( options.bLoadHiResTextures )
1485 DebugMessage(M64MSG_INFO, "Texture loading option is enabled. Finding all hires textures");
1486 FindAllHiResTextures();
1490 void InitTextureDump(void)
1492 if( options.bDumpTexturesToFiles )
1494 DebugMessage(M64MSG_INFO, "Texture dump option is enabled. Finding all dumpped textures");
1495 FindAllDumpedTextures();
1499 /********************************************************************************************************************
1500 * Inits the hires textures. For doing so, all hires textures info & the cached textures (for dumping and the hires ones)
1501 * are deleted. Afterwards they are reloaded from file system. This only takes place if a new rom has been loaded.
1506 ********************************************************************************************************************/
1507 void InitExternalTextures(void)
1509 DebugMessage(M64MSG_VERBOSE, "InitExternalTextures");
1510 // remove all hires & dump textures from cache
1511 CloseExternalTextures();
1512 // reload and recache hires textures
1513 InitHiresTextures();
1514 // prepare list of already dumped textures (for avoiding to redump them). Available hires textures will
1515 // also be excluded from dumping
1519 /********************************************************************************************************************
1520 * Determines the scale factor for resizing the original texture to the hires replacement. The scale factor is a left
1521 * shift. That means scale factor 1 = size(original texture)*2= size(hires texture),
1522 * factor 2 = size(original texture)*4= size(hires texture), etc. (I'm not yet sure why it has to be 2^x. Most probably
1523 * because of block size. Has to be further determined.
1525 * info: the record describing the external texture
1526 * entry: the original texture in the texture cache
1528 * info.scaleShift: the value for left shift the original texture size to the corresponding hires texture size
1529 * return value: the value for left shift the original texture size to the corresponding hires texture size.
1530 * The function returns -1 if the dimensions of the hires texture are not a power of two of the
1532 ********************************************************************************************************************/
1533 int FindScaleFactor(const ExtTxtrInfo &info, TxtrCacheEntry &entry)
1537 // check if the original texture dimensions (x and y) scaled with the current shift is still smaller or of the same size as the hires one
1538 while(info.height >= entry.ti.HeightToLoad*(1<<scaleShift) && info.width >= entry.ti.WidthToLoad*(1<<scaleShift))
1540 // check if the original texture dimensions (x and y)scaled with the current shift have the same size as the hires one
1541 if(info.height == entry.ti.HeightToLoad*(1<<scaleShift) && info.width == entry.ti.WidthToLoad*(1<<scaleShift))
1542 // found appropriate scale shift, return it
1548 // original texture dimensions (x or y or both) scaled with the last scale shift have become larger than the dimensions
1549 // of the hires texture. That means the dimensions of the hires replacement are not power of 2 of the original texture.
1550 // Therefore indicate a crop shift (or -1 when the hires_texture was smaller from the beginning)
1556 /********************************************************************************************************************
1557 * Checks if a hires replacement for a texture is available.
1559 * infos: The list of external textures
1560 * entry: the original texture in the texture cache
1562 * indexa: returns the index in "infos" where a hires replacement for a texture without
1563 * palette crc or a RGBA_PNG_FOR_ALL_CI texture has been found
1564 * return value: the index in "infos" where the corresponding hires texture has been found
1565 ********************************************************************************************************************/
1566 int CheckTextureInfos( CSortedList<uint64,ExtTxtrInfo> &infos, TxtrCacheEntry &entry, int &indexa, int &scaleShift, bool bForDump = false)
1568 if ((entry.ti.WidthToLoad != 0 && entry.ti.WidthToCreate / entry.ti.WidthToLoad > 2) ||
1569 (entry.ti.HeightToLoad != 0 && entry.ti.HeightToCreate / entry.ti.HeightToLoad > 2 ))
1571 //DebugMessage(M64MSG_WARNING, "Hires texture does not support extreme texture replication");
1574 // determine if texture is a color-indexed (CI) texture
1576 bool bCI = (gRDP.otherMode.text_tlut>=2 || entry.ti.Format == TXT_FMT_CI || entry.ti.Format == TXT_FMT_RGBA) && entry.ti.Size <= TXT_SIZE_8b;
1577 // generate two alternative ids
1579 uint64 crc64a = entry.dwCRC;
1581 uint64 crc64b = crc64a;
1582 if (options.bLoadHiResCRCOnly) {
1583 crc64a |= (0xFFFFFFFF);
1584 crc64b |= (entry.dwPalCRC&0xFFFFFFFF);
1586 crc64a |= (0xFFFFFF00|(entry.ti.Format<<4)|entry.ti.Size);
1587 crc64b |= ((entry.dwPalCRC&0xFFFFFF00)|(entry.ti.Format<<4)|entry.ti.Size);
1590 // infos is the list containing the references to the detected external textures
1591 // get the number of items contained in this list
1592 int infosize = infos.size();
1594 // try to identify the external texture that
1595 // corresponds to the original texture
1596 indexa = infos.find(crc64a); // For CI without pal CRC, and for RGBA_PNG_FOR_ALL_CI
1598 // and also for textures with separate alpha channel
1599 indexb = infos.find(crc64b); // For CI or PNG with pal CRC
1601 // did not found the ext. text.
1602 if( indexa >= infosize )
1604 // did not found the ext. text. w/ sep. alpha channel
1605 if( indexb >= infosize )
1610 // found texture with sep. alpha channel
1614 // determine the factor for scaling
1615 scaleShift = FindScaleFactor(infos[indexb], entry);
1616 // ok. the scale factor is supported. A valid replacement has been found
1617 if( scaleShift >= 0 )
1620 // if texture is 4bit, should be dumped and there is no match in the list of external textures
1622 if( bForDump && bCI && indexb < 0)
1623 // than return that there is no replacement & therefore texture can be dumped (microdev: not sure about that...)
1626 // texture has no separate alpha channel, try to find it in the ext. text. list
1628 scaleShift = FindScaleFactor(infos[indexa], entry);
1629 // ok. the scale factor is supported. A valid replacement has been found
1630 // this is a texture without ext. alpha channel
1632 if( scaleShift >= 0 )
1634 // no luck at all. there is no valid replacement
1639 bool SaveCITextureToFile(TxtrCacheEntry &entry, char *filename, bool bShow, bool bWhole);
1641 void DumpCachedTexture( TxtrCacheEntry &entry )
1645 CTexture *pSrcTexture = entry.pTexture;
1648 // Check the vector table
1649 int ciidx, scaleShift;
1650 if( CheckTextureInfos(gTxtrDumpInfos,entry,ciidx,scaleShift,true) >= 0 )
1651 return; // This texture has been dumpped
1653 char filename1[PATH_MAX + 64];
1654 char filename2[PATH_MAX + 64];
1655 char filename3[PATH_MAX + 64];
1656 char gamefolder[PATH_MAX + 64];
1657 strncpy(gamefolder, ConfigGetUserDataPath(), PATH_MAX);
1658 gamefolder[PATH_MAX] = 0;
1660 strcat(gamefolder,"texture_dump" OSAL_DIR_SEPARATOR_STR);
1661 strcat(gamefolder,(const char*)g_curRomInfo.szGameName);
1662 strcat(gamefolder, OSAL_DIR_SEPARATOR_STR);
1664 //sprintf(filename1+strlen(filename1), "%08X#%d#%d", entry.dwCRC, entry.ti.Format, entry.ti.Size);
1665 sprintf(filename1, "%s%s#%08X#%d#%d", gamefolder, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size);
1667 if( (gRDP.otherMode.text_tlut>=2 || entry.ti.Format == TXT_FMT_CI || entry.ti.Format == TXT_FMT_RGBA) && entry.ti.Size <= TXT_SIZE_8b )
1671 sprintf(filename1, "%sci_bmp%c%s#%08X#%d#%d_ci", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size);
1672 SaveCITextureToFile(entry, filename1, false, false);
1675 sprintf(filename1, "%sci_bmp_with_pal_crc%c%s#%08X#%d#%d#%08X_ci", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size,entry.dwPalCRC);
1676 SaveCITextureToFile(entry, filename1, false, false);
1678 sprintf(filename1, "%sci_by_png%c%s#%08X#%d#%d#%08X_ciByRGBA", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size,entry.dwPalCRC);
1679 CRender::g_pRender->SaveTextureToFile(*pSrcTexture, filename1, TXT_RGBA, false, false, entry.ti.WidthToLoad, entry.ti.HeightToLoad);
1683 sprintf(filename1, "%spng_by_rgb_a%c%s#%08X#%d#%d_rgb", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size);
1684 sprintf(filename2, "%spng_by_rgb_a%c%s#%08X#%d#%d_a", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size);
1685 sprintf(filename3, "%spng_all%c%s#%08X#%d#%d_all", gamefolder, cSep, g_curRomInfo.szGameName, entry.dwCRC, entry.ti.Format, entry.ti.Size);
1688 CRender::g_pRender->SaveTextureToFile(*pSrcTexture, filename1, TXT_RGB, false, false, entry.ti.WidthToLoad, entry.ti.HeightToLoad);
1689 CRender::g_pRender->SaveTextureToFile(*pSrcTexture, filename3, TXT_RGBA, false, false, entry.ti.WidthToLoad, entry.ti.HeightToLoad);
1690 if( entry.ti.Format != TXT_FMT_I )
1694 if( pSrcTexture->StartUpdate(&srcInfo) )
1696 // Copy RGB to buffer
1697 for( int i=entry.ti.HeightToLoad-1; i>=0; i--)
1699 unsigned char *pSrc = (unsigned char*)srcInfo.lpSurface+srcInfo.lPitch * i;
1700 for( uint32 j=0; j<entry.ti.WidthToLoad; j++)
1706 pSrcTexture->EndUpdate(&srcInfo);
1710 CRender::g_pRender->SaveTextureToFile(*pSrcTexture, filename2, TXT_ALPHA, false, false);
1714 ExtTxtrInfo newinfo;
1715 newinfo.width = entry.ti.WidthToLoad;
1716 newinfo.height = entry.ti.HeightToLoad;
1717 //strcpy(newinfo.name,g_curRomInfo.szGameName);
1718 newinfo.fmt = entry.ti.Format;
1719 newinfo.siz = entry.ti.Size;
1720 newinfo.crc32 = entry.dwCRC;
1722 newinfo.pal_crc32 = entry.dwPalCRC;
1723 newinfo.foldername = NULL;
1724 newinfo.filename = NULL;
1725 newinfo.filename_a = NULL;
1726 newinfo.type = NO_TEXTURE;
1727 newinfo.bSeparatedAlpha = false;
1729 uint64 crc64 = newinfo.crc32;
1731 if (options.bLoadHiResCRCOnly)
1732 crc64 |= newinfo.pal_crc32&0xFFFFFFFF;
1734 crc64 |= (newinfo.pal_crc32&0xFFFFFF00)|(newinfo.fmt<<4)|newinfo.siz;
1735 gTxtrDumpInfos.add(crc64,newinfo);
1740 bool LoadRGBBufferFromPNGFile(char *filename, unsigned char **pbuf, int &width, int &height, int bits_per_pixel = 24 )
1742 struct BMGImageStruct img;
1743 memset(&img, 0, sizeof(BMGImageStruct));
1744 if (!PathFileExists(filename))
1746 DebugMessage(M64MSG_ERROR, "File at '%s' doesn't exist in LoadRGBBufferFromPNGFile!", filename);
1750 BMG_Error code = ReadPNG( filename, &img );
1751 if( code == BMG_OK )
1755 *pbuf = new unsigned char[img.width*img.height*bits_per_pixel/8];
1758 DebugMessage(M64MSG_ERROR, "new[] returned NULL for image width=%i height=%i bpp=%i", img.width, img.height, bits_per_pixel);
1761 if (img.bits_per_pixel == bits_per_pixel)
1763 memcpy(*pbuf, img.bits, img.width*img.height*bits_per_pixel/8);
1765 else if (img.bits_per_pixel == 24 && bits_per_pixel == 32)
1767 unsigned char *pSrc = img.bits;
1768 unsigned char *pDst = *pbuf;
1769 for (int i = 0; i < (int)(img.width*img.height); i++)
1777 // loaded image has alpha, needed image has to be without alpha channel
1778 else if (img.bits_per_pixel == 32 && bits_per_pixel == 24)
1780 // pointer to source image data
1781 unsigned char *pSrc = img.bits;
1782 // buffer for destination image
1783 unsigned char *pDst = *pbuf;
1784 // copy data of the loaded image to the buffer by skipping the alpha byte
1785 for (int i = 0; i < (int)(img.width*img.height); i++)
1793 // skip the alpha byte of the loaded image
1797 else if (img.bits_per_pixel == 8 && (bits_per_pixel == 24 || bits_per_pixel == 32))
1799 // do palette lookup and convert 8bpp to 24/32bpp
1800 int destBytePP = bits_per_pixel / 8;
1801 int paletteBytePP = img.bytes_per_palette_entry;
1802 unsigned char *pSrc = img.bits;
1803 unsigned char *pDst = *pbuf;
1804 // clear the destination image data (just to clear alpha if bpp=32)
1805 memset(*pbuf, 0, img.width*img.height*destBytePP);
1806 for (int i = 0; i < (int)(img.width*img.height); i++)
1808 unsigned char clridx = *pSrc++;
1809 unsigned char *palcolor = img.palette + clridx * paletteBytePP;
1810 pDst[0] = palcolor[2]; // red
1811 pDst[1] = palcolor[1]; // green
1812 pDst[2] = palcolor[0]; // blue
1818 DebugMessage(M64MSG_ERROR, "PNG file '%s' is %i bpp but texture is %i bpp.", filename, img.bits_per_pixel, bits_per_pixel);
1824 height = img.height;
1831 DebugMessage(M64MSG_ERROR, "ReadPNG() returned error for '%s' in LoadRGBBufferFromPNGFile!", filename);
1837 bool LoadRGBABufferFromColorIndexedFile(char *filename, TxtrCacheEntry &entry, unsigned char **pbuf, int &width, int &height)
1839 BITMAPFILEHEADER fileHeader;
1840 BITMAPINFOHEADER infoHeader;
1843 f = fopen(filename, "rb");
1846 if (fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f) != 1 ||
1847 fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f) != 1)
1849 DebugMessage(M64MSG_ERROR, "Couldn't read BMP headers in file '%s'", filename);
1853 if( infoHeader.biBitCount != 4 && infoHeader.biBitCount != 8 )
1856 DebugMessage(M64MSG_ERROR, "Unsupported BMP file format: %s", filename);
1861 int tablesize = infoHeader.biBitCount == 4 ? 16 : 256;
1862 uint32 *pTable = new uint32[tablesize];
1863 if (fread(pTable, tablesize*4, 1, f) != 1)
1865 DebugMessage(M64MSG_ERROR, "Couldn't read BMP palette in file '%s'", filename);
1870 // Create the pallette table
1871 uint16 * pPal = (uint16 *)entry.ti.PalAddress;
1872 if( entry.ti.Size == TXT_SIZE_4b )
1875 for( int i=0; i<16; i++ )
1877 pTable[i] = entry.ti.TLutFmt == TLUT_FMT_RGBA16 ? Convert555ToRGBA(pPal[i^1]) : ConvertIA16ToRGBA(pPal[i^1]);
1883 for( int i=0; i<256; i++ )
1885 pTable[i] = entry.ti.TLutFmt == TLUT_FMT_RGBA16 ? Convert555ToRGBA(pPal[i^1]) : ConvertIA16ToRGBA(pPal[i^1]);
1889 *pbuf = new unsigned char[infoHeader.biWidth*infoHeader.biHeight*4];
1892 unsigned char *colorIdxBuf = new unsigned char[infoHeader.biSizeImage];
1895 if (fread(colorIdxBuf, infoHeader.biSizeImage, 1, f) != 1)
1897 DebugMessage(M64MSG_ERROR, "Couldn't read BMP image data in file '%s'", filename);
1900 width = infoHeader.biWidth;
1901 height = infoHeader.biHeight;
1903 // Converting pallette texture to RGBA texture
1905 uint32 *pbuf2 = (uint32*) *pbuf;
1907 for( int i=height-1; i>=0; i--)
1909 for( int j=0; j<width; j++)
1911 if( entry.ti.Size == TXT_SIZE_4b )
1917 *pbuf2++ = pTable[colorIdxBuf[(idx++)>>1]&0xF];
1922 *pbuf2++ = pTable[(colorIdxBuf[(idx++)>>1]>>4)&0xF];
1928 *pbuf2++ = pTable[colorIdxBuf[idx++]];
1931 if( entry.ti.Size == TXT_SIZE_4b )
1933 if( idx%8 ) idx = (idx/8+1)*8;
1937 if( idx%4 ) idx = (idx/4+1)*4;
1941 delete [] colorIdxBuf;
1945 TRACE0("Out of memory");
1961 TRACE1("Fail to open file %s", filename);
1967 bool LoadRGBBufferFromBMPFile(char *filename, unsigned char **pbuf, int &width, int &height)
1969 BITMAPFILEHEADER fileHeader;
1970 BITMAPINFOHEADER infoHeader;
1973 f = fopen(filename, "rb");
1976 if (fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f) != 1 ||
1977 fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f) != 1)
1979 DebugMessage(M64MSG_ERROR, "Couldn't read BMP headers in file '%s'", filename);
1983 if( infoHeader.biBitCount != 24 )
1986 DebugMessage(M64MSG_ERROR, "Unsupported BMP file 16 bits format: %s", filename);
1991 *pbuf = new unsigned char[infoHeader.biWidth*infoHeader.biHeight*3];
1994 if (fread(*pbuf, infoHeader.biWidth*infoHeader.biHeight*3, 1, f) != 1)
1995 DebugMessage(M64MSG_ERROR, "Couldn't read RGB BMP image data in file '%s'", filename);
1997 width = infoHeader.biWidth;
1998 height = infoHeader.biHeight;
2010 DebugMessage(M64MSG_WARNING, "Fail to open file %s", filename);
2016 /*******************************************************
2017 * Loads the hires equivaltent of a texture
2019 * TxtrCacheEntry: The original texture in the texture cache
2022 *******************************************************/
2023 void LoadHiresTexture( TxtrCacheEntry &entry )
2025 // check if the external texture has already been loaded
2026 if( entry.bExternalTxtrChecked )
2028 // there is already an enhanced texture (e.g. a filtered one)
2030 if( entry.pEnhancedTexture )
2032 // delete it from memory before loading the external one
2033 SAFE_DELETE(entry.pEnhancedTexture);
2036 int ciidx, scaleShift;
2037 // search the index of the appropriate hires replacement texture
2038 // in the list containing the infos of the external textures
2039 // ciidx is not needed here (just needed for dumping)
2040 int idx = CheckTextureInfos(gHiresTxtrInfos,entry,ciidx,scaleShift,false);
2043 // there is no hires replacement => indicate that
2044 entry.bExternalTxtrChecked = true;
2048 // Load the bitmap file
2049 char filename_rgb[PATH_MAX];
2050 char filename_a[PATH_MAX];
2052 strcpy(filename_rgb, gHiresTxtrInfos[idx].foldername);
2053 strcat(filename_rgb, gHiresTxtrInfos[idx].filename);
2055 if (gHiresTxtrInfos[idx].filename_a) {
2056 strcpy(filename_a, gHiresTxtrInfos[idx].foldername);
2057 strcat(filename_a, gHiresTxtrInfos[idx].filename_a);
2059 strcpy(filename_a, "");
2062 // Load BMP image to buffer_rbg
2063 unsigned char *buf_rgba = NULL;
2064 unsigned char *buf_a = NULL;
2067 bool bResRGBA=false, bResA=false;
2068 bool bCI = ((gRDP.otherMode.text_tlut>=2 || entry.ti.Format == TXT_FMT_CI || entry.ti.Format == TXT_FMT_RGBA) && entry.ti.Size <= TXT_SIZE_8b );
2070 switch( gHiresTxtrInfos[idx].type )
2077 bResRGBA = LoadRGBBufferFromPNGFile(filename_rgb, &buf_rgba, width, height);
2078 if( bResRGBA && gHiresTxtrInfos[idx].bSeparatedAlpha )
2079 bResA = LoadRGBBufferFromPNGFile(filename_a, &buf_a, width, height);
2082 case COLOR_INDEXED_BMP:
2084 bResRGBA = LoadRGBABufferFromColorIndexedFile(filename_rgb, entry, &buf_rgba, width, height);
2088 case RGBA_PNG_FOR_CI:
2089 case RGBA_PNG_FOR_ALL_CI:
2091 bResRGBA = LoadRGBBufferFromPNGFile(filename_rgb, &buf_rgba, width, height, 32);
2095 case RGB_WITH_ALPHA_TOGETHER_PNG:
2099 bResRGBA = LoadRGBBufferFromPNGFile(filename_rgb, &buf_rgba, width, height, 32);
2105 if( !bResRGBA || !buf_rgba )
2107 DebugMessage(M64MSG_ERROR, "RGBBuffer creation failed for file '%s'.", filename_rgb);
2110 // check if the alpha channel has been loaded if the texture has a separate alpha channel
2111 else if( gHiresTxtrInfos[idx].bSeparatedAlpha && !bResA )
2113 DebugMessage(M64MSG_ERROR, "Alpha buffer creation failed for file '%s'.", filename_a);
2118 // calculate the texture size magnification by comparing the N64 texture size and the hi-res texture size
2119 int scale = 1 << scaleShift;
2122 int input_height_shift = height - entry.ti.HeightToLoad * scale;
2123 int input_pitch_a = width;
2124 int input_pitch_rgb = width;
2125 width = entry.ti.WidthToLoad * scale;
2126 height = entry.ti.HeightToLoad * scale;
2127 if (entry.ti.WidthToCreate/entry.ti.WidthToLoad == 2) mirrorx = 2;
2128 if (entry.ti.HeightToCreate/entry.ti.HeightToLoad == 2) mirrory = 2;
2129 entry.pEnhancedTexture = CDeviceBuilder::GetBuilder()->CreateTexture(entry.ti.WidthToCreate*scale, entry.ti.HeightToCreate*scale);
2132 if( entry.pEnhancedTexture && entry.pEnhancedTexture->StartUpdate(&info) )
2135 if( gHiresTxtrInfos[idx].type == RGB_PNG )
2137 input_pitch_rgb *= 3;
2140 if (info.lPitch < width * 4)
2141 DebugMessage(M64MSG_ERROR, "Texture pitch %i less than width %i times 4", info.lPitch, width);
2142 if (height > info.dwHeight)
2143 DebugMessage(M64MSG_ERROR, "Texture source height %i greater than destination height %i", height, info.dwHeight);
2145 // Update the texture by using the buffer
2146 for( int i=0; i<height; i++)
2148 unsigned char *pRGB = buf_rgba + (input_height_shift + i) * input_pitch_rgb;
2149 unsigned char *pA = buf_a + (input_height_shift + i) * input_pitch_a;
2150 unsigned char* pdst = (unsigned char*)info.lpSurface + (height - i - 1)*info.lPitch;
2151 for( int j=0; j<width; j++)
2153 *pdst++ = *pRGB++; // R
2154 *pdst++ = *pRGB++; // G
2155 *pdst++ = *pRGB++; // B
2157 if( gHiresTxtrInfos[idx].bSeparatedAlpha )
2162 else if( entry.ti.Format == TXT_FMT_I )
2175 input_pitch_rgb *= 4;
2177 // Update the texture by using the buffer
2178 for( int i=height-1; i>=0; i--)
2180 uint32 *pRGB = (uint32*)(buf_rgba + (input_height_shift + i) * input_pitch_rgb);
2181 uint32 *pdst = (uint32*)((unsigned char*)info.lpSurface + (height - i - 1)*info.lPitch);
2182 for( int j=0; j<width; j++)
2184 *pdst++ = *pRGB++; // RGBA
2191 //printf("Mirror: ToCreate: (%d,%d) ToLoad: (%d,%d) Scale: (%i,%i) Mirror: (%i,%i) Size: (%i,%i) Mask: %i\n", entry.ti.WidthToCreate, entry.ti.HeightToCreate, entry.ti.WidthToLoad, entry.ti.HeightToLoad, scale, scale, mirrorx, mirrory, width, height, entry.ti.maskS+scaleShift);
2192 gTextureManager.Mirror(info.lpSurface, width, entry.ti.maskS+scaleShift, width*2, width*2, height, S_FLAG, 4 );
2197 //printf("Mirror: ToCreate: (%d,%d) ToLoad: (%d,%d) Scale: (%i,%i) Mirror: (%i,%i) Size: (%i,%i) Mask: %i\n", entry.ti.WidthToCreate, entry.ti.HeightToCreate, entry.ti.WidthToLoad, entry.ti.HeightToLoad, scale, scale, mirrorx, mirrory, width, height, entry.ti.maskT+scaleShift);
2198 gTextureManager.Mirror(info.lpSurface, height, entry.ti.maskT+scaleShift, height*2, entry.pEnhancedTexture->m_dwCreatedTextureWidth, height, T_FLAG, 4 );
2201 if( entry.ti.WidthToCreate*scale < entry.pEnhancedTexture->m_dwCreatedTextureWidth )
2204 gTextureManager.Clamp(info.lpSurface, width, entry.pEnhancedTexture->m_dwCreatedTextureWidth, entry.pEnhancedTexture->m_dwCreatedTextureWidth, height, S_FLAG, 4 );
2206 if( entry.ti.HeightToCreate*scale < entry.pEnhancedTexture->m_dwCreatedTextureHeight )
2209 gTextureManager.Clamp(info.lpSurface, height, entry.pEnhancedTexture->m_dwCreatedTextureHeight, entry.pEnhancedTexture->m_dwCreatedTextureWidth, height, T_FLAG, 4 );
2212 entry.pEnhancedTexture->EndUpdate(&info);
2213 entry.pEnhancedTexture->SetOthersVariables();
2214 entry.pEnhancedTexture->m_bIsEnhancedTexture = true;
2215 entry.dwEnhancementFlag = TEXTURE_EXTERNAL;
2217 DebugMessage(M64MSG_VERBOSE, "Loaded hi-res texture: %s", filename_rgb);
2221 DebugMessage(M64MSG_ERROR, "New texture creation failed.");
2222 TRACE0("Cannot create a new texture");