summaryrefslogtreecommitdiff
path: root/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp
blob: 2a2be10dec507a27d175e0a087d1261baef1c5c8 (plain)
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
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include <vector>

#include "Globals.h"
#include "CommonPaths.h"
#include "StringUtil.h"
#include <fstream>
#ifdef _WIN32
#define _interlockedbittestandset workaround_ms_header_bug_platform_sdk6_set
#define _interlockedbittestandreset workaround_ms_header_bug_platform_sdk6_reset
#define _interlockedbittestandset64 workaround_ms_header_bug_platform_sdk6_set64
#define _interlockedbittestandreset64 workaround_ms_header_bug_platform_sdk6_reset64
#include <intrin.h>
#undef _interlockedbittestandset
#undef _interlockedbittestandreset
#undef _interlockedbittestandset64
#undef _interlockedbittestandreset64
#endif

#include "VideoConfig.h"
#include "Hash.h"
#include "Statistics.h"
#include "Profiler.h"
#include "ImageWrite.h"

#include "Render.h"

#include "MemoryUtil.h"
#include "BPStructs.h"
#include "TextureDecoder.h"
#include "TextureMngr.h"
#include "PixelShaderCache.h"
#include "PixelShaderManager.h"
#include "VertexShaderManager.h"
#include "FramebufferManager.h"
#include "FileUtil.h"
#include "HiresTextures.h"

u8 *TextureMngr::temp = NULL;
TextureMngr::TexCache TextureMngr::textures;

extern int frameCount;
static u32 s_TempFramebuffer = 0;

#define TEMP_SIZE (1024*1024*4)
#define TEXTURE_KILL_THRESHOLD 200

static const GLint c_MinLinearFilter[8] = {
	GL_NEAREST,
	GL_NEAREST_MIPMAP_NEAREST,
	GL_NEAREST_MIPMAP_LINEAR,
	GL_NEAREST,
	GL_LINEAR,
	GL_LINEAR_MIPMAP_NEAREST,
	GL_LINEAR_MIPMAP_LINEAR,
	GL_LINEAR,
};

static const GLint c_WrapSettings[4] = {
	GL_CLAMP_TO_EDGE,
	GL_REPEAT,
	GL_MIRRORED_REPEAT,
	GL_REPEAT,
};

bool SaveTexture(const char* filename, u32 textarget, u32 tex, int width, int height)
{
	std::vector<u32> data(width * height);
    glBindTexture(textarget, tex);
    glGetTexImage(textarget, 0, GL_BGRA, GL_UNSIGNED_BYTE, &data[0]);
    GLenum err = GL_REPORT_ERROR();
    if (err != GL_NO_ERROR)
	{
		PanicAlert("Can't save texture, GL Error: %s", gluErrorString(err));
        return false;
    }

    return SaveTGA(filename, width, height, &data[0]);
}

bool TextureMngr::TCacheEntry::IntersectsMemoryRange(u32 range_address, u32 range_size)
{
	if (addr + size_in_bytes < range_address)
		return false;
	if (addr >= range_address + range_size)
		return false;
	return true;
}

void TextureMngr::TCacheEntry::SetTextureParameters(TexMode0 &newmode)
{
    mode = newmode;
    if (isRectangle) 
	{
        // very limited!
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
			            (newmode.mag_filter || g_ActiveConfig.bForceFiltering) ? GL_LINEAR : GL_NEAREST);
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
			            (g_ActiveConfig.bForceFiltering || newmode.min_filter >= 4) ? GL_LINEAR : GL_NEAREST);

		if (newmode.wrap_s == 2 || newmode.wrap_t == 2) 
            DEBUG_LOG(VIDEO, "cannot support mirrorred repeat mode");

		if (newmode.wrap_s == 1 || newmode.wrap_t == 1)
            DEBUG_LOG(VIDEO, "cannot support repeat mode");
    }
    else 
	{
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
			            (newmode.mag_filter || g_Config.bForceFiltering) ? GL_LINEAR : GL_NEAREST);

        if (bHaveMipMaps) {
            int filt = newmode.min_filter;
            if (g_ActiveConfig.bForceFiltering && newmode.min_filter < 4)
                newmode.min_filter += 4; // take equivalent forced linear
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, c_MinLinearFilter[filt]);
        }
        else
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
			                (g_ActiveConfig.bForceFiltering || newmode.min_filter >= 4) ? GL_LINEAR : GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, c_WrapSettings[newmode.wrap_s]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, c_WrapSettings[newmode.wrap_t]);
    }
    
    if (g_Config.iMaxAnisotropy >= 1)
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)(1 << g_ActiveConfig.iMaxAnisotropy));
}

void TextureMngr::TCacheEntry::Destroy(bool shutdown)
{
    if (!texture)
        return;
    glDeleteTextures(1, &texture);
    if (!isRenderTarget && !shutdown && !g_ActiveConfig.bSafeTextureCache) {
        u32 *ptr = (u32*)g_VideoInitialize.pGetMemoryPointer(addr);
        if (ptr && *ptr == hash)
            *ptr = oldpixel;
    }
    texture = 0;
} 

void TextureMngr::Init()
{
    temp = (u8*)AllocateMemoryPages(TEMP_SIZE);
	TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, g_ActiveConfig.bTexFmtOverlayCenter);
	HiresTextures::Init(globals->unique_id);
}

void TextureMngr::Invalidate(bool shutdown)
{
    for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter)
        iter->second.Destroy(shutdown);
    textures.clear();
	HiresTextures::Shutdown();
}

void TextureMngr::Shutdown()
{
    Invalidate(true);

    if (s_TempFramebuffer) {
        glDeleteFramebuffersEXT(1, (GLuint *)&s_TempFramebuffer);
        s_TempFramebuffer = 0;
    }

    FreeMemoryPages(temp, TEMP_SIZE);	
    temp = NULL;
}

void TextureMngr::ProgressiveCleanup()
{
    TexCache::iterator iter = textures.begin();
    while (iter != textures.end())
	{
        if (frameCount > TEXTURE_KILL_THRESHOLD + iter->second.frameCount)
		{
            if (!iter->second.isRenderTarget) {
                iter->second.Destroy(false);
				ERASE_THROUGH_ITERATOR(textures, iter);
            }
            else {
                iter->second.Destroy(false);
				ERASE_THROUGH_ITERATOR(textures, iter);
            }
        }
        else
            iter++;
    }
}

void TextureMngr::InvalidateRange(u32 start_address, u32 size)
{
	TexCache::iterator iter = textures.begin();
	while (iter != textures.end())
	{
		if (iter->second.IntersectsMemoryRange(start_address, size))
		{
			iter->second.Destroy(false);
			ERASE_THROUGH_ITERATOR(textures, iter);
		}
		else {
			++iter;
		}
	}
}

TextureMngr::TCacheEntry* TextureMngr::Load(int texstage, u32 address, int width, int height, u32 tex_format, int tlutaddr, int tlutfmt)
{
	// notes (about "UNsafe texture cache"):
	//	Have to be removed soon.
	//	But we keep it until the "safe" way became rock solid
	//	pros: it has an unique ID held by the texture data itself (@address) once cached.
	//	cons: it writes this unique ID in the gc RAM <- very dangerous (break MP1) and ugly 

	// notes (about "safe texture cache"):
	//	Metroids text issue (character table):
	//	Same addr, same GX_TF_C4 texture data but different TLUT (hence different outputs).
	//	That's why we have to hash the TLUT too for TLUT tex_format dependent textures (ie. GX_TF_C4, GX_TF_C8, GX_TF_C14X2).
	//	And since the address and tex data don't change, the key index in the cacheEntry map can't be the address but 
	//	have to be a real unique ID. 
	//	DONE but not satifiying yet -> may break copyEFBToTexture sometimes.

	//	Pokemon Colosseum text issue (plain text):
	//	Use a GX_TF_I4 512x512 text-flush-texture at a const address.
	//	The problem here was just the sparse hash on the texture. This texture is partly overwrited (what is needed only) 
	//	so lot's of remaning old text. 	Thin white chars on black bg too.

	// TODO:	- clean this up when ready to kill old "unsafe texture cache"
	//			- fix the key index situation with CopyRenderTargetToTexture. 
	//			Could happen only for GX_TF_C4, GX_TF_C8 and GX_TF_C14X2 fmt.
	//			Wonder if we can't use tex width&height to know if EFB might be copied to it...
	//			raw idea:  TOCHECK if addresses are aligned we have few bits left...

    if (address == 0)
        return NULL;

    TexMode0 &tm0 = bpmem.tex[texstage > 3].texMode0[texstage & 3];
    u8 *ptr = g_VideoInitialize.pGetMemoryPointer(address);
	int bsw = TexDecoder_GetBlockWidthInTexels(tex_format) - 1;
	int bsh = TexDecoder_GetBlockHeightInTexels(tex_format) - 1;
    int expandedWidth = (width + bsw) & (~bsw);
	int expandedHeight = (height + bsh) & (~bsh);

	u64 hash_value;
    u32 texID = address;
	u64 texHash;
	u32 FullFormat = tex_format;
	if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
		FullFormat = (tex_format | (tlutfmt << 16));
	if (g_ActiveConfig.bSafeTextureCache || g_ActiveConfig.bHiresTextures || g_ActiveConfig.bDumpTextures)
	{
		texHash =  TexDecoder_GetHash64(ptr,TexDecoder_GetTextureSizeInBytes(expandedWidth, expandedHeight, tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
		if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
		{
			// WARNING! texID != address now => may break CopyRenderTargetToTexture (cf. TODO up)
			// tlut size can be up to 32768B (GX_TF_C14X2) but Safer == Slower.
			// This trick (to change the texID depending on the TLUT addr) is a trick to get around
			// an issue with metroid prime's fonts, where it has multiple sets of fonts on top of
			// each other stored in a single texture, and uses the palette to make different characters
			// visible or invisible. Thus, unless we want to recreate the textures for every drawn character,
			// we must make sure that texture with different tluts get different IDs.
			u64 tlutHash = TexDecoder_GetHash64(&texMem[tlutaddr], TexDecoder_GetPaletteSize(tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
			texHash ^= tlutHash;
			if (g_ActiveConfig.bSafeTextureCache)
			{
				texID = texID ^ ((u32)(tlutHash & 0xFFFFFFFF)) ^ ((u32)((tlutHash >> 32) & 0xFFFFFFFF));
			}
		}
		if (g_ActiveConfig.bSafeTextureCache)
			hash_value = texHash;
	}

	bool skip_texture_create = false;
	TexCache::iterator iter = textures.find(texID);

	if (iter != textures.end())
	{
        TCacheEntry &entry = iter->second;

		if (!g_ActiveConfig.bSafeTextureCache)
			hash_value = ((u32 *)ptr)[0];

        if (entry.isRenderTarget || ((address == entry.addr) && (hash_value == entry.hash) && (int) FullFormat == entry.fmt))
		{
            entry.frameCount = frameCount;
			glEnable(entry.isRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D);
//			entry.isRectangle ? TextureMngr::EnableTex2D(texstage) : TextureMngr::EnableTexRECT(texstage);
            glBindTexture(entry.isRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, entry.texture);
			GL_REPORT_ERRORD();
            if (entry.mode.hex != tm0.hex)
                entry.SetTextureParameters(tm0);
			//DebugLog("%cC addr: %08x | fmt: %i | e.hash: %08x | w:%04i h:%04i", g_ActiveConfig.bSafeTextureCache ? 'S' : 'U'
 			//		, address, tex_format, entry.hash, width, height);
			return &entry;
        }
        else
        {
            // Let's reload the new texture data into the same texture,
			// instead of destroying it and having to create a new one.
			// Might speed up movie playback very, very slightly.
			if (width == entry.w && height == entry.h && (int) FullFormat == entry.fmt)
            {
				glBindTexture(entry.isRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, entry.texture);
				GL_REPORT_ERRORD();
				if (entry.mode.hex != tm0.hex)
					entry.SetTextureParameters(tm0);
				skip_texture_create = true;
            }
            else
            {
                entry.Destroy(false);
                textures.erase(iter);
            }
        }
    }

    //Make an entry in the table
	TCacheEntry& entry = textures[texID];
	PC_TexFormat dfmt = PC_TEX_FMT_NONE;

	if (g_ActiveConfig.bHiresTextures)
	{
		//Load Custom textures
		char texPathTemp[MAX_PATH];
		int oldWidth = width;
		int oldHeight = height;

		sprintf(texPathTemp, "%s_%08x_%i", globals->unique_id, (unsigned int) texHash, tex_format);
		dfmt = HiresTextures::GetHiresTex(texPathTemp, &width, &height, tex_format, temp);

		if (dfmt != PC_TEX_FMT_NONE)
		{
			expandedWidth = width;
			expandedHeight = height;
			entry.scaleX = (float) width / oldWidth;
			entry.scaleY = (float) height / oldHeight;
		}
	}

	if (dfmt == PC_TEX_FMT_NONE)
		dfmt = TexDecoder_Decode(temp, ptr, expandedWidth, expandedHeight, tex_format, tlutaddr, tlutfmt);

    entry.oldpixel = ((u32 *)ptr)[0];

	if (g_ActiveConfig.bSafeTextureCache)
		entry.hash = hash_value;
	else 
	{
		entry.hash = (u32)(((double)rand() / RAND_MAX) * 0xFFFFFFFF);
	    ((u32 *)ptr)[0] = entry.hash;
	}

    entry.addr = address;
	entry.size_in_bytes = TexDecoder_GetTextureSizeInBytes(width, height, tex_format);
    entry.isRenderTarget = false;

	// For static textures, we use NPOT.
	entry.isRectangle = false;
    // old code: entry.isRectangle = ((width & (width - 1)) || (height & (height - 1)));

	GLenum target = entry.isRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
	if (!skip_texture_create) {
		glGenTextures(1, (GLuint *)&entry.texture);
		glBindTexture(target, entry.texture);
	}

	if (dfmt != PC_TEX_FMT_DXT1)
	{
		if (expandedWidth != width)
			glPixelStorei(GL_UNPACK_ROW_LENGTH, expandedWidth);

		int gl_format;
		int gl_iformat;
		int gl_type;
		switch (dfmt)
		{
		default:
		case PC_TEX_FMT_NONE:
			PanicAlert("Invalid PC texture format %i", dfmt); 
		case PC_TEX_FMT_BGRA32:
			gl_format = GL_BGRA;
			gl_iformat = 4;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_RGBA32:
			gl_format = GL_RGBA;
			gl_iformat = 4;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_I4_AS_I8:
			gl_format = GL_LUMINANCE;
			gl_iformat = GL_INTENSITY4;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_IA4_AS_IA8:
			gl_format = GL_LUMINANCE_ALPHA;
			gl_iformat = GL_LUMINANCE4_ALPHA4;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_I8:
			gl_format = GL_LUMINANCE;
			gl_iformat = GL_INTENSITY8;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_IA8:
			gl_format = GL_LUMINANCE_ALPHA;
			gl_iformat = GL_LUMINANCE8_ALPHA8;
			gl_type = GL_UNSIGNED_BYTE;
			break;
		case PC_TEX_FMT_RGB565:
			gl_format = GL_RGB;
			gl_iformat = GL_RGB;
			gl_type = GL_UNSIGNED_SHORT_5_6_5;
			break;
		}
		bool GenerateMipmaps = !entry.isRectangle && ((tm0.min_filter & 3) == 1 || (tm0.min_filter & 3) == 2);
		if (GenerateMipmaps) 
		{
			glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
		}
		if(skip_texture_create)
		{
			glTexSubImage2D(target, 0,0,0,width, height, gl_format, gl_type, temp);
		}
		else
		{
			glTexImage2D(target, 0, gl_iformat, width, height, 0, gl_format, gl_type, temp);
		}
		if (GenerateMipmaps) 
		{
			glTexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
			entry.bHaveMipMaps = true;
		}

		if (expandedWidth != width) // reset
			glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
	}
	else
	{
		if(skip_texture_create)
		{
			glCompressedTexSubImage2D(target, 0,0,0,width, height, 
				GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,expandedWidth*expandedHeight/2, temp);
		}
		else
		{
			glCompressedTexImage2D(target, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
				width, height, 0, expandedWidth*expandedHeight/2, temp);
		}		
	}

    entry.frameCount = frameCount;
    entry.w = width;
    entry.h = height;
    entry.fmt = FullFormat;
    entry.SetTextureParameters(tm0);

    if (g_ActiveConfig.bDumpTextures) // dump texture to file
	{ 

        char szTemp[MAX_PATH];
		char szDir[MAX_PATH];
		const char* uniqueId = globals->unique_id;
		bool bCheckedDumpDir = false;

		sprintf(szDir,"%s%s",File::GetUserPath(D_DUMPTEXTURES_IDX), uniqueId);

		if(!bCheckedDumpDir)
		{
			if (!File::Exists(szDir) || !File::IsDirectory(szDir))
				File::CreateDir(szDir);

			bCheckedDumpDir = true;
		}

		sprintf(szTemp, "%s/%s_%08x_%i.tga",szDir, uniqueId, (unsigned int) texHash, tex_format);
		if (!File::Exists(szTemp))
		{
			SaveTexture(szTemp, target, entry.texture, expandedWidth, expandedHeight);
		}
    }

    INCSTAT(stats.numTexturesCreated);
    SETSTAT(stats.numTexturesAlive, textures.size());
    return &entry;
}


void TextureMngr::CopyRenderTargetToTexture(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle &source_rect)
{
	DVSTARTPROFILE();
	GL_REPORT_ERRORD();

	// for intensity values, use Y of YUV format!
	// for all purposes, treat 4bit equivalents as 8bit (probably just used for compression)
	// RGBA8 - RGBA8
	// RGB565 - RGB565
	// RGB5A3 - RGB5A3
	// I4,R4,Z4 - I4
	// IA4,RA4 - IA4
	// Z8M,G8,I8,A8,Z8,R8,B8,Z8L - I8
	// Z16,GB8,RG8,Z16L,IA8,RA8 - IA8
	GLenum gl_format = GL_RGBA;
	GLenum gl_iformat = 4;
	GLenum gl_type = GL_UNSIGNED_BYTE;
	float colmat[16];
	float fConstAdd[4] = {0};
	memset(colmat, 0, sizeof(colmat));

    if (bFromZBuffer) 
	{
        switch(copyfmt) 
		{
            case 0: // Z4				
            case 1: // Z8
				colmat[2] = colmat[6] = colmat[10] = colmat[14] = 1;
                break;            
            case 3: // Z16 //?
				colmat[1] = colmat[5] = colmat[9] = colmat[14] = 1;
				break;
            case 11: // Z16 (reverse order)
				colmat[2] = colmat[6] = colmat[10] = colmat[13] = 1;
                break;
            case 6: // Z24X8
				colmat[2] = colmat[5] = colmat[8] = colmat[15] = 1;
                break;
            case 9: // Z8M
				colmat[1] = colmat[5] = colmat[9] = colmat[13] = 1;
                break;
            case 10: // Z8L
				colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
                break;
            case 12: // Z16L
				colmat[0] = colmat[4] = colmat[8] = colmat[13] = 1;
                break;
            default:
                ERROR_LOG(VIDEO, "Unknown copy zbuf format: 0x%x", copyfmt);
				colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
                break;
        }
    }
    else if (bIsIntensityFmt) 
	{
			// TODO - verify these coefficients
        fConstAdd[0] = fConstAdd[1] = fConstAdd[2] = 16.0f/255.0f;
		colmat[0] = 0.257f; colmat[1] = 0.504f; colmat[2] = 0.098f;
        colmat[4] = 0.257f; colmat[5] = 0.504f; colmat[6] = 0.098f;
        colmat[8] = 0.257f; colmat[9] = 0.504f; colmat[10] = 0.098f;
		if (copyfmt < 2) 
		{
            fConstAdd[3] = 16.0f / 255.0f;
            colmat[12] = 0.257f; colmat[13] = 0.504f; colmat[14] = 0.098f;
        }
        else// alpha
            colmat[15] = 1;        
    }
    else 
	{
        switch (copyfmt) 
		{
            case 0: // R4
				colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
				break;
            case 8: // R8
				colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
                break;
            case 2: // RA4
				colmat[0] = colmat[4] = colmat[8] = colmat[15] = 1;
                break;
            case 3: // RA8
				colmat[0] = colmat[4] = colmat[8] = colmat[15] = 1;
                break;
            case 7: // A8
				colmat[3] = colmat[7] = colmat[11] = colmat[15] = 1; 
                break;
            case 9: // G8
				colmat[1] = colmat[5] = colmat[9] = colmat[13] = 1;
                break;
            case 10: // B8
				colmat[2] = colmat[6] = colmat[10] = colmat[14] = 1;
                break;
            case 11: // RG8
				colmat[0] = colmat[4] = colmat[8] = colmat[13] = 1;
                break;
            case 12: // GB8
				colmat[1] = colmat[5] = colmat[9] = colmat[14] = 1;
                break;
            case 4: // RGB565
				colmat[0] = colmat[5] = colmat[10] = 1;
                fConstAdd[3] = 1; // set alpha to 1
                break;
            case 5: // RGB5A3
				colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
                break;
            case 6: // RGBA8
				colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
                break;
            default:
                ERROR_LOG(VIDEO, "Unknown copy color format: 0x%x", copyfmt);
				colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
                break;
        }
    }

	bool bIsInit = textures.find(address) != textures.end();

	PRIM_LOG("copytarg: addr=0x%x, fromz=%d, intfmt=%d, copyfmt=%d", address, (int)bFromZBuffer, (int)bIsIntensityFmt,copyfmt);

	TCacheEntry& entry = textures[address];
	entry.hash = 0;
	entry.frameCount = frameCount;

	int w = (abs(source_rect.GetWidth()) >> bScaleByHalf);
	int h = (abs(source_rect.GetHeight()) >> bScaleByHalf);



	GL_REPORT_ERRORD();

	if (!bIsInit) 
	{
		glGenTextures(1, (GLuint *)&entry.texture);
		glBindTexture(GL_TEXTURE_RECTANGLE_ARB, entry.texture);
		GL_REPORT_ERRORD();
		glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, gl_iformat, w, h, 0, gl_format, gl_type, NULL);
		GL_REPORT_ERRORD();
	}
	else 
	{
		_assert_(entry.texture);
		GL_REPORT_ERRORD();
		if (entry.w == w && entry.h == h && entry.isRectangle && entry.fmt == (int) copyfmt) 
		{
			glBindTexture(GL_TEXTURE_RECTANGLE_ARB, entry.texture);
			// for some reason mario sunshine errors here...
			// Beyond Good and Evil does too, occasionally.
			GL_REPORT_ERRORD();
		} else {
			// Delete existing texture.
			glDeleteTextures(1,(GLuint *)&entry.texture);
			glGenTextures(1, (GLuint *)&entry.texture);
			glBindTexture(GL_TEXTURE_RECTANGLE_ARB, entry.texture);
			glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, gl_iformat, w, h, 0, gl_format, gl_type, NULL);
			GL_REPORT_ERRORD();
		}
	}

	if (!bIsInit || !entry.isRenderTarget) 
	{
		glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

		glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		if (glGetError() != GL_NO_ERROR) {
			glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
			glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
			GL_REPORT_ERRORD();
		}
	}

	entry.w = w;
	entry.h = h;
	entry.isRectangle = true;
	entry.isRenderTarget = true;
	entry.fmt = copyfmt;	

	// Make sure to resolve anything we need to read from.
	GLuint read_texture = bFromZBuffer ? g_framebufferManager.ResolveAndGetDepthTarget(source_rect) : g_framebufferManager.ResolveAndGetRenderTarget(source_rect);

    GL_REPORT_ERRORD();

    // We have to run a pixel shader, for color conversion.
    Renderer::ResetAPIState(); // reset any game specific settings

    if (s_TempFramebuffer == 0)
        glGenFramebuffersEXT(1, (GLuint *)&s_TempFramebuffer);

    g_framebufferManager.SetFramebuffer(s_TempFramebuffer);
	// Bind texture to temporary framebuffer
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, entry.texture, 0);
	GL_REPORT_FBO_ERROR();
    GL_REPORT_ERRORD();
    
    glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
    glActiveTexture(GL_TEXTURE0);
	glEnable(GL_TEXTURE_RECTANGLE_ARB);
	glBindTexture(GL_TEXTURE_RECTANGLE_ARB, read_texture);
   
    glViewport(0, 0, w, h);

	PixelShaderCache::EnableShader(bFromZBuffer ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram());    
    PixelShaderManager::SetColorMatrix(colmat, fConstAdd); // set transformation
    GL_REPORT_ERRORD();

	TargetRectangle targetSource = Renderer::ConvertEFBRectangle(source_rect);

    glBegin(GL_QUADS);
	glTexCoord2f((GLfloat)targetSource.left,  (GLfloat)targetSource.bottom); glVertex2f(-1,  1);
    glTexCoord2f((GLfloat)targetSource.left,  (GLfloat)targetSource.top  ); glVertex2f(-1, -1);
    glTexCoord2f((GLfloat)targetSource.right, (GLfloat)targetSource.top  ); glVertex2f( 1, -1);
    glTexCoord2f((GLfloat)targetSource.right, (GLfloat)targetSource.bottom); glVertex2f( 1,  1);
    glEnd();

    GL_REPORT_ERRORD();

	// Unbind texture from temporary framebuffer
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, 0, 0);

	// Return to the EFB.
    g_framebufferManager.SetFramebuffer(0);
    Renderer::RestoreAPIState();
    VertexShaderManager::SetViewportChanged();
    TextureMngr::DisableStage(0);

    GL_REPORT_ERRORD();

	if (g_ActiveConfig.bDumpEFBTarget)
	{
		static int count = 0;
		SaveTexture(StringFromFormat("%sefb_frame_%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), count++).c_str(), GL_TEXTURE_RECTANGLE_ARB, entry.texture, entry.w, entry.h);
	}
}

void TextureMngr::DisableStage(int stage)
{
	glActiveTexture(GL_TEXTURE0 + stage);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_TEXTURE_RECTANGLE_ARB);
}

void TextureMngr::ClearRenderTargets()
{
    for (TexCache::iterator iter = textures.begin(); iter != textures.end(); iter++)
        iter->second.isRenderTarget = false;
}