summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/RenderTexture.cpp
blob: b47cf0f85d70218a1b88d644f101515d209884ba (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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
#include "UnityPrefix.h"
#include "RenderTexture.h"
#if UNITY_XENON
#include "PlatformDependent/Xbox360/Source/GfxDevice/TexturesXenon.h"
#endif
#include "Runtime/Camera/RenderManager.h"
#include "Runtime/Shaders/GraphicsCaps.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "External/shaderlab/Library/properties.h"
#include "External/shaderlab/Library/texenv.h"
#include "Runtime/Misc/BuildSettings.h"
#include "Runtime/Utilities/BitUtility.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Graphics/RenderSurface.h"
#include "Runtime/Graphics/Image.h"
#include "Runtime/Camera/CameraUtil.h"
#include "Runtime/Utilities/MemoryPool.h"
#include "Runtime/Misc/PlayerSettings.h"
#include "Runtime/Math/ColorSpaceConversion.h"
#include "Runtime/Profiler/Profiler.h"

PROFILER_INFORMATION(gGrabPixels, "RenderTexture.GrabPixels", kProfilerRender);
PROFILER_INFORMATION(gSetRenderTargets, "RenderTexture.SetActive", kProfilerRender);

static const int kRenderTextureFormatBPP[kRTFormatCount] = {
	4, // ARGB32
	4, // Depth various amounts on various HW, but we'll assume 4
	8, // ARGBHalf
	4, // Shadowmap various amounts on various HW, but we'll assume 4
	2, // RGB565
	2, // ARGB4444
	2, // ARGB1555
	4, // Default
	4, // A2R10G10B10
	8, // DefaultHDR
	8, // ARGB64
	16,// ARGBFloat
	8, // RGFloat
	4, // RGHalf
	4, // RFloat
	2, // RHalf
	1, // R8
	16,// ARGBInt
	8, // RGInt
	4, // RInt
	4, // BGRA32
};

static int kDepthFormatBPP[kDepthFormatCount] = {
	0, // None
	2, // 16
	4, // 24
};


typedef List< ListNode<RenderTexture> > RenderTextureList;
RenderTextureList gRenderTextures;

static bool gIsRenderTexEnabled = true;
int gTemporarilyAllowIndieRenderTextures = 0;

void RenderTexture::SetTemporarilyAllowIndieRenderTexture (bool allow)
{
	if (allow)
		gTemporarilyAllowIndieRenderTextures++;
	else
		gTemporarilyAllowIndieRenderTextures--;
}

void RenderTexture::FindAndSetSRGBWrite( RenderTexture* newActive )
{
	bool wantLinear = GetActiveColorSpace() == kLinearColorSpace;
	GetGfxDevice().SetSRGBWrite(newActive ? newActive->GetSRGBReadWrite() && wantLinear : wantLinear);
}

void RenderTexture::SetActive (RenderTexture* newActive, int mipLevel, CubemapFace face, UInt32 flags)
{
	newActive = EnsureRenderTextureIsCreated(newActive);

	RenderSurfaceHandle newColorSurface = newActive ? newActive->m_ColorHandle : GetGfxDevice().GetBackBufferColorSurface();
	RenderSurfaceHandle newDepthSurface = newActive ? newActive->m_DepthHandle : GetGfxDevice().GetBackBufferDepthSurface();
	int mips = newActive && newActive->HasMipMap() ? mipLevel : 0;

	SetActive (1, &newColorSurface, newDepthSurface, newActive, mips, face, flags);
}

bool RenderTexture::SetActive (int count, RenderSurfaceHandle* newColorSurfaces, RenderSurfaceHandle newDepthSurface, RenderTexture* rt, int mipLevel, CubemapFace face, UInt32 flags)
{
	DebugAssert(count > 0);
	if( !IsEnabled() )
	{
		for (int i = 0; i < count; ++i)
			newColorSurfaces[i].Reset();

		// ??? should we keep them invalid just in case?
		newColorSurfaces[0] = GetGfxDevice().GetBackBufferColorSurface();
		newDepthSurface		= GetGfxDevice().GetBackBufferDepthSurface();
	}

	int width = 1, height = 1;
	if(newColorSurfaces[0].IsValid() && !newColorSurfaces[0].object->backBuffer)
	{
		width = newColorSurfaces[0].object->width;
		height= newColorSurfaces[0].object->height;
	}

	// Clamp mip level to valid range
	const int mipCount = CalculateMipMapCount3D(width, height, 1);
	mipLevel = clamp(mipLevel, 0, mipCount-1);

	GfxDevice& device = GetGfxDevice();
	RenderSurfaceHandle oldColorSurface = device.GetActiveRenderColorSurface(0);
	RenderSurfaceHandle oldDepthSurface = device.GetActiveRenderDepthSurface();

	Assert(newColorSurfaces[0].IsValid() == newDepthSurface.IsValid());
	bool renderTarget = newColorSurfaces[0].IsValid() && !newColorSurfaces[0].object->backBuffer;

	if( oldColorSurface != newColorSurfaces[0] || (flags & kFlagForceResolve) )
	{
		// MSAA surface has to be resolved
		RenderTexture* oldRt = GetActive();
		if( oldRt && oldRt->IsAntiAliased() )
			oldRt->ResolveAntiAliasedSurface();
	}

	UInt32 rtFlags = 0;
	rtFlags |= (flags & kFlagDontRestoreColor) ? GfxDevice::kFlagDontRestoreColor : 0;
	rtFlags |= (flags & kFlagDontRestoreDepth) ? GfxDevice::kFlagDontRestoreDepth : 0;
	rtFlags |= (flags & kFlagForceResolve) ? GfxDevice::kFlagForceResolve : 0;

	PROFILER_AUTO(gSetRenderTargets, rt);
	device.SetRenderTargets (count, newColorSurfaces, newDepthSurface, mipLevel, face, rtFlags);
	GPU_TIMESTAMP();

	// we can call RenderTexture::SetActive before device init (d3d11)
	if(		oldColorSurface == newColorSurfaces[0] && oldDepthSurface == newDepthSurface
		&&	newColorSurfaces[0].IsValid() && !newColorSurfaces[0].object->backBuffer
	  )
	{
		return false;
	}


	// NOTE: important to set ActiveRenderTexture to null/non-null value before setting up viewport (as that might involve
	// flipping vertically based on whether there is a RT or not).
	GetGfxDevice().SetActiveRenderTexture(rt);

	// Setup the viewport for the render texture
	if( !(flags & kFlagDontSetViewport) )
	{
		if (renderTarget)
		{
			width  >>= mipLevel;
			height >>= mipLevel;
			device.SetViewport (0, 0, width, height);
		}
		else
		{
			// When switching to main window, restore the viewport to the one of the current
			// camera.
			const int* cameraViewPort = GetRenderManager().GetCurrentViewPort();
			int viewcoord[4];
			viewcoord[0] = cameraViewPort[0];
			viewcoord[1] = cameraViewPort[1];
			viewcoord[2] = cameraViewPort[2];
			viewcoord[3] = cameraViewPort[3];

			// Flip viewport just before applying to device, but don't store it flipped in the render manager
			FlipScreenRectIfNeeded( device, viewcoord );
			device.SetViewport( viewcoord[0], viewcoord[1], viewcoord[2], viewcoord[3] );
		}
	}

	if (renderTarget)
	{
		// If texture coordinates go from top to bottom, then we need to
		// invert projection matrix when rendering into a texture.
		#if UNITY_WII
		device.SetInvertProjectionMatrix( true );
		#else
		device.SetInvertProjectionMatrix( !device.UsesOpenGLTextureCoords() );
		#endif
	}
	else
	{
		device.SetInvertProjectionMatrix( false );
	}

	return true;
}


bool RenderTexture::GetSupportedMipMapFlag(bool mipMap) const
{
	if (!gGraphicsCaps.hasAutoMipMapGeneration)
		mipMap = false;
	if (m_Dimension == kTexDimCUBE && gGraphicsCaps.buggyMipmappedCubemaps)
		mipMap = false;
	if (m_Dimension == kTexDim3D && gGraphicsCaps.buggyMipmapped3DTextures)
		mipMap = false;
	return mipMap;
}


bool RenderTexture::Create ()
{
	if( !IsEnabled() )
		return false;

	DestroySurfaces();
	GfxDevice& device = GetGfxDevice();

	if (m_Width <= 0 || m_Height <= 0)
	{
		ErrorString("RenderTexture.Create failed: width & height must be larger than 0");
		return false;
	}

	if (m_Dimension == kTexDimCUBE && (!GetIsPowerOfTwo() || m_Width != m_Height))
	{
		ErrorString("RenderTexture.Create failed: cube maps must be power of two and width must match height");
		return false;
	}

	if( !device.IsRenderTargetConfigValid(m_Width, m_Height, static_cast<RenderTextureFormat>(m_ColorFormat), static_cast<DepthBufferFormat>(m_DepthFormat)) )
	{
		if( GetIsPowerOfTwo() )
		{
			if (gGraphicsCaps.maxRenderTextureSize < 4)
			{
				ErrorString("RenderTexture.Create failed: maxRenderTextureSize is too small");
				return false;
			}

				// Decrease too large render textures while maintaining aspect ratio
				do
				{
				m_Width = std::max( m_Width / 2, 4 );
				m_Height = std::max( m_Height / 2, 4 );
			}
				while ( !device.IsRenderTargetConfigValid(m_Width, m_Height, static_cast<RenderTextureFormat>(m_ColorFormat), static_cast<DepthBufferFormat>(m_DepthFormat)) );
		}
		else
		{
				ErrorString("RenderTexture.Create failed: requested size is too large.");
				return false;
		}
	}

	if( !gGraphicsCaps.supportsRenderTextureFormat[m_ColorFormat] )
	{
		ErrorString("RenderTexture.Create failed: format unsupported.");
		return false;
	}

	if (!GetIsPowerOfTwo() && (gGraphicsCaps.npotRT == kNPOTNone))
	{
		ErrorString("RenderTexture.Create failed: non-power-of-two sizes not supported.");
		return false;
	}

	if (m_Dimension == kTexDimCUBE && (!gGraphicsCaps.hasRenderToCubemap || IsDepthRTFormat((RenderTextureFormat)m_ColorFormat)))
	{
		ErrorString("RenderTexture.Create failed: cubemap not supported.");
		return false;
	}

	if (m_Dimension == kTexDim3D && (!gGraphicsCaps.has3DTexture || !gGraphicsCaps.hasRenderTo3D))
	{
		ErrorString("RenderTexture.Create failed: volume texture not supported.");
		return false;
	}


	bool mipMap = GetSupportedMipMapFlag(m_MipMap);
	if (!GetIsPowerOfTwo())
		mipMap = false;

	int samples = m_AntiAliasing;
	samples = clamp<int>(samples, 1, 8);
	if (!gGraphicsCaps.hasMultiSample)
		samples = 1;
	if (m_Dimension != kTexDim2D)
		samples = 1;

	if (samples > 1)
		mipMap = false;

	// If we have native depth textures, we should use our regular textureID for the depth
	// surface.
	TextureID colorTID, resolvedColorTID, depthTID;
	if ((m_ColorFormat == kRTFormatDepth && gGraphicsCaps.hasNativeDepthTexture) ||
		(m_ColorFormat == kRTFormatShadowMap && gGraphicsCaps.hasNativeShadowMap))
	{
		depthTID = m_TexID;
		m_SecondaryTexIDUsed = false;
	}
	else
	{
		// Use resolved surface as texture if MSAA enabled
		if (samples > 1 && !gGraphicsCaps.hasMultiSampleAutoResolve)
			resolvedColorTID = m_TexID;
		else
			colorTID = m_TexID;
		// For regular non-MSAA render textures, also provide capability to treat depth buffer as a texture.
		// Only do this if HW supports native depth textures AND texture is 2D AND
		// we actually have a depth buffer AND driver is not broken for this case.
		bool useDepthAsTexture = false;
		if (m_Dimension == kTexDim2D && m_DepthFormat != kDepthFormatNone && samples <= 1)
			useDepthAsTexture = gGraphicsCaps.hasStencilInDepthTexture && !gGraphicsCaps.buggyTextureBothColorAndDepth;
		if (useDepthAsTexture)
		{
			depthTID = m_SecondaryTexID;
			m_SecondaryTexIDUsed = true;
		}
		else
			m_SecondaryTexIDUsed = false;
	}

	UInt32 colorFlags = 0;
	if (mipMap) colorFlags |= kSurfaceCreateMipmap;
	if (m_GenerateMips) colorFlags |= kSurfaceCreateAutoGenMips;
	if (m_SRGB) colorFlags |= kSurfaceCreateSRGB;
	if (m_EnableRandomWrite) colorFlags |= kSurfaceCreateRandomWrite;
	if (colorTID == TextureID() && samples <= 1) colorFlags |= kSurfaceCreateNeverUsed; // never sampled or resolved
	m_ColorHandle = device.CreateRenderColorSurface (colorTID,
		m_Width, m_Height, samples, m_VolumeDepth, m_Dimension, static_cast<RenderTextureFormat>(m_ColorFormat), colorFlags);

	if (samples > 1 && !gGraphicsCaps.hasMultiSampleAutoResolve)
	{
		m_ResolvedColorHandle = device.CreateRenderColorSurface (resolvedColorTID,
			m_Width, m_Height, 1, m_VolumeDepth, m_Dimension, static_cast<RenderTextureFormat>(m_ColorFormat), colorFlags);
	}

	UInt32 depthFlags = 0;
	if (m_ColorFormat==kRTFormatShadowMap) depthFlags |= kSurfaceCreateShadowmap;
	if (m_SampleOnlyDepth) depthFlags |= kSurfaceCreateSampleOnly;
	m_DepthHandle = device.CreateRenderDepthSurface (depthTID,
		m_Width, m_Height, samples, m_Dimension, static_cast<DepthBufferFormat>(m_DepthFormat), depthFlags);

	if (!(m_ColorHandle.IsValid() || m_DepthHandle.IsValid()))
	{
		ErrorString("RenderTexture.Create failed");
		return false;
	}

	if (IsCreated())
	{
		Assert(m_RegisteredSizeForStats == 0);
		m_RegisteredSizeForStats = GetRuntimeMemorySize();
		device.GetFrameStats().ChangeRenderTextureBytes (m_RegisteredSizeForStats);
	}

	if (m_CreatedFromScript)
	{
		// We can't currently read the minds of users, so let's always restore their render targets.
		// TODO: extend RenderTexture API
		device.SetSurfaceFlags(m_ColorHandle, GfxDevice::kSurfaceAlwaysRestore, ~GfxDevice::kSurfaceRestoreMask);
		device.SetSurfaceFlags(m_DepthHandle, GfxDevice::kSurfaceAlwaysRestore, ~GfxDevice::kSurfaceRestoreMask);
	}

	SetStoredColorSpaceNoDirtyNoApply(m_SRGB ? kTexColorSpaceSRGB : kTexColorSpaceLinear);

	// Set filtering, etc. mode
	ApplySettings();

	UpdateTexelSize();

	return true;
}

void RenderTexture::UpdateTexelSize()
{
	SetUVScale( 1.0f, 1.0f );
	if (m_Width != 0 && m_Height != 0)
	{
		int width = m_Width;
		int height = m_Height;
		#if GFX_EMULATES_NPOT_RENDERTEXTURES
		width = NextPowerOfTwo (width);
		height = NextPowerOfTwo (height);
		#endif
		SetTexelSize( 1.0f / width, 1.0f / height );
	}
}


void RenderTexture::ApplySettings()
{
	TextureDimension dim = GetDimension();
	bool mip = HasMipMap();
	// Don't ever use Aniso on depth textures or textures where we
	// sample depth buffer.
	bool depth = IsDepthRTFormat((RenderTextureFormat)m_ColorFormat);
	if (depth || m_SecondaryTexIDUsed)
		m_TextureSettings.m_Aniso = 0;

	m_TextureSettings.Apply (GetTextureID(), dim, mip, GetActiveTextureColorSpace());
	if (m_SecondaryTexIDUsed)
		m_TextureSettings.Apply (m_SecondaryTexID, dim, mip, GetActiveTextureColorSpace());
	NotifyMipBiasChanged();
}


void RenderTexture::Release()
{
	if (GetGfxDevice().GetActiveRenderTexture() == this)
	{
		ErrorString("Releasing render texture that is set to be RenderTexture.active!");
		GetGfxDevice().SetActiveRenderTexture(NULL);
	}
	DestroySurfaces();
}

void RenderTexture::DestroySurfaces()
{
	if(!IsCreated())
		return;

	GfxDevice& device = GetGfxDevice();
	// Different graphics caps can change the calculated runtime size (case 555046)
	// so we store the size we added and make sure to subtract the same amount.
	// Graphics caps can change during an editor session due to emulation.
	device.GetFrameStats().ChangeRenderTextureBytes (-m_RegisteredSizeForStats);
	m_RegisteredSizeForStats = 0;
	if( m_ColorHandle.IsValid() )
		device.DestroyRenderSurface (m_ColorHandle);
	if( m_ResolvedColorHandle.IsValid() )
		device.DestroyRenderSurface (m_ResolvedColorHandle);
	if( m_DepthHandle.IsValid() )
		device.DestroyRenderSurface (m_DepthHandle);
}

void RenderTexture::DiscardContents()
{
	if(IsCreated())
		RenderTextureDiscardContents(this, true, true);

}
void RenderTexture::DiscardContents(bool discardColor, bool discardDepth)
{
	if(IsCreated())
		RenderTextureDiscardContents(this, discardColor, discardDepth);
}


void RenderTexture::MarkRestoreExpected()
{
	GfxDevice& device = GetGfxDevice();
	device.IgnoreNextUnresolveOnRS (m_ColorHandle);
	device.IgnoreNextUnresolveOnRS (m_DepthHandle);
	device.IgnoreNextUnresolveOnRS (m_ResolvedColorHandle);
}


void RenderTexture::GrabPixels (int left, int bottom, int width, int height)
{
	if (!IsCreated())
		Create();

	const RenderSurfaceHandle& handle = IsAntiAliased() ? m_ResolvedColorHandle : m_ColorHandle;

	if (!handle.IsValid())
		return;

	if (left < 0) {
		width += left;
		left = 0;
	}
	if (bottom < 0) {
		height += bottom;
		bottom = 0;
	}
	if (width > m_Width)
		width = m_Width;
	if (height > m_Height)
		height = m_Height;

	PROFILER_AUTO(gGrabPixels, NULL);
	GfxDevice& device = GetGfxDevice();
	device.GrabIntoRenderTexture (handle, m_DepthHandle, left, bottom, width, height);
	GPU_TIMESTAMP();
	device.GetFrameStats().AddRenderTextureChange(); // treat resolve as RT change
}
void RenderTexture::CorrectVerticalTexelSize(bool shouldBePositive)
{
	if (!GetGfxDevice().UsesOpenGLTextureCoords())
	{
		if (m_TexelSizeY < 0.0f && shouldBePositive) m_TexelSizeY = -m_TexelSizeY;
		else if (m_TexelSizeY > 0.0f && !shouldBePositive) m_TexelSizeY = -m_TexelSizeY;
	}
}
RenderTexture::RenderTexture (MemLabelId label, ObjectCreationMode mode)
:	Super(label, mode)
,	m_DepthFormat(kDepthFormat24)
,	m_ColorFormat(kRTFormatARGB32)
,	m_Dimension(kTexDim2D)
,	m_MipMap(false)
,	m_GenerateMips(true)
,	m_SRGB(false)
,	m_EnableRandomWrite(false)
,	m_CreatedFromScript(false)
,	m_SampleOnlyDepth(false)
,	m_RegisteredSizeForStats(0)
,	m_RenderTexturesNode(this)
{
	m_Width = 256;
	m_Height = 256;
	m_VolumeDepth = 1;
	m_AntiAliasing = 1;

	GetSettings().m_WrapMode = kTexWrapClamp;

	// We use unchecked version since we may not be on the main thread
	// This means CreateTextureID() implementation must be thread safe!
	m_SecondaryTexID = GetUncheckedGfxDevice().CreateTextureID();
	m_SecondaryTexIDUsed = false;
}

RenderTexture::~RenderTexture ()
{
	Release();
	m_RenderTexturesNode.RemoveFromList();
	Texture::s_TextureIDMap.erase (m_SecondaryTexID);
	// FreeTextureID() implementation must be thread safe!
	GetUncheckedGfxDevice().FreeTextureID(m_SecondaryTexID);
}

void RenderTexture::SetDimension (TextureDimension dim)
{
	if (m_Dimension == dim)
		return;

	if (!IsCreated ())
		m_Dimension = dim;
	else
		ErrorString ("Setting dimension of already created render texture is not supported!");
}

void RenderTexture::SetVolumeDepth (int v)
{
	if (m_VolumeDepth == v)
		return;

	if (!IsCreated ()) {
		m_VolumeDepth = v;
	} else
		ErrorString ("Setting volume depth of already created render texture is not supported!");
}

void RenderTexture::SetAntiAliasing (int aa)
{
	if (m_AntiAliasing == aa)
		return;

	if (aa < 1 || aa > 8 || !IsPowerOfTwo(aa))
	{
		ErrorString( "Invalid antiAliasing value (must be 1, 2, 4 or 8)" );
		return;
	}

	if (!IsCreated ()) {
		m_AntiAliasing = aa;
	} else
		ErrorString ("Setting anti-aliasing of already created render texture is not supported!");
}


void RenderTexture::SetMipMap( bool mipMap )
{
	mipMap = GetSupportedMipMapFlag(mipMap);

	if (mipMap == m_MipMap)
		return;

	if (!IsCreated ()) {
		m_MipMap = mipMap;
	} else {
		ErrorString ("Setting mipmap mode of render texture that is loaded not supported!");
	}
}

void RenderTexture::SetGenerateMips (bool v)
{
	if (v == m_GenerateMips)
		return;

	if (!IsCreated ())
	{
		if (m_MipMap && m_DepthFormat != kDepthFormatNone && !v)
		{
			WarningStringObject ("Mipmapped RenderTextures with manual mip generation can't have depth buffer", this);
			v = true;
		}
		m_GenerateMips = v;
	}
	else
		ErrorString ("Can't change mipmap generation of already created RenderTexture");
}

void RenderTexture::SetSRGBReadWrite ( bool sRGB )
{
	if (!IsCreated ()) {
		bool setSRGB = sRGB ? (GetActiveColorSpace() == kLinearColorSpace) : false;
		setSRGB = setSRGB && (m_ColorFormat != GetGfxDevice().GetDefaultHDRRTFormat());
		m_SRGB = setSRGB;
	} else
		ErrorString ("Changing srgb mode of render texture that is loaded not supported!");
}

void RenderTexture::SetEnableRandomWrite (bool v)
{
	if (v == m_EnableRandomWrite)
		return;

	if (!IsCreated ()) {
		m_EnableRandomWrite = v;
	} else
		ErrorString ("Can't change random write mode of already created render texture!");
}

void RenderTexture::AwakeFromLoad(AwakeFromLoadMode awakeMode)
{
	m_Width = std::max (1, m_Width);
	m_Height = std::max (1, m_Height);
	m_VolumeDepth = std::max (1, m_VolumeDepth);
	m_AntiAliasing = clamp<int> (m_AntiAliasing, 1, 8);

	if( IsDepthRTFormat( (RenderTextureFormat)m_ColorFormat ) )
		m_MipMap = false;
	if (m_Dimension==kTexDimCUBE)
		m_Height = m_Width;

	if( !GetIsPowerOfTwo() && GetSettings().m_WrapMode == kTexWrapRepeat )
		GetSettings().m_WrapMode = kTexWrapClamp;

	if( IsDepthRTFormat((RenderTextureFormat)m_ColorFormat) ) // always clamp depth textures
		GetSettings().m_WrapMode = kTexWrapClamp;

#if GFX_OPENGLESxx_ONLY
	// currently i have no idea about possibility of linear filter for fp rts (seems like gl do support it)
	if( IsHalfRTFormat((RenderTextureFormat)m_ColorFormat) && !gGraphicsCaps.gles20.hasHalfLinearFilter )
		GetSettings().m_FilterMode = kTexFilterNearest;
#endif

	gRenderTextures.push_back(m_RenderTexturesNode);

	UpdateTexelSize();

	Super::AwakeFromLoad (awakeMode);
}

void RenderTexture::SetWidth (int width)
{
	if (!IsCreated ()) {
		m_Width = width;
		UpdateTexelSize();
		SetDirty();
	} else
		ErrorString ("Resizing of render texture that is loaded not supported!");
}

void RenderTexture::SetHeight (int height)
{
	if (!IsCreated ()) {
		m_Height = height;
		UpdateTexelSize();
		SetDirty();
	} else
		ErrorString ("Resizing of render texture that is loaded not supported!");
}

void RenderTexture::SetDepthFormat( DepthBufferFormat depth )
{
	if (!IsCreated ())
		m_DepthFormat = depth;
	else
		ErrorString ("Setting depth of render texture that is loaded not supported!");
}

void RenderTexture::SetColorFormat( RenderTextureFormat format )
{
	if (!IsCreated ())
	{
		if( format == kRTFormatDefault )
			format = GetGfxDevice().GetDefaultRTFormat();

		m_ColorFormat = format;
		if (IsDepthRTFormat(format)) m_TextureSettings.m_Aniso = 0; // never use Anisotropic on depth textures
	} else
		ErrorString ("Changing format of render texture that is loaded not supported!");
}


RenderTexture* RenderTexture::GetActive ()
{
	return GetGfxDevice().GetActiveRenderTexture();
}

ShaderLab::TexEnv *RenderTexture::SetGlobalProperty (const ShaderLab::FastPropertyName& name)
{
	ShaderLab::TexEnv *te = ShaderLab::g_GlobalProperties->SetTexture (name, this);
	te->ClearMatrix();
	return te;
}

bool RenderTexture::IsEnabled ()
{
	if (gGraphicsCaps.hasRenderToTexture)
		return gIsRenderTexEnabled && (GetBuildSettings().hasRenderTexture || gTemporarilyAllowIndieRenderTextures);
	else
		return false;
}

void RenderTexture::SetEnabled (bool enable)
{
	if (!enable)
		ReleaseAll();

	gIsRenderTexEnabled = enable;
}

void RenderTexture::ReleaseAll ()
{
	SetActive (NULL);

	for (RenderTextureList::iterator i=gRenderTextures.begin ();i != gRenderTextures.end ();i++)
		(**i).Release ();
}


#if ENABLE_PROFILER || UNITY_EDITOR

int RenderTexture::GetCreatedRenderTextureCount ()
{
	int count = 0;
	for (RenderTextureList::iterator i=gRenderTextures.begin ();i != gRenderTextures.end ();i++)
	{
		if ((**i).IsCreated ())
			count++;
	}

	return count;
}

int RenderTexture::GetCreatedRenderTextureBytes ()
{
	int count = 0;
	for (RenderTextureList::iterator i=gRenderTextures.begin ();i != gRenderTextures.end ();i++)
	{
		if ((**i).IsCreated ())
			count += (**i).GetRuntimeMemorySize();
	}

	return count;
}

#endif



int EstimateRenderTextureSize (int width, int height, int depth, RenderTextureFormat format, DepthBufferFormat depthFormat, TextureDimension dim, bool mipMap)
{
	// color buffer
	int colorBPP;
	if (format == kRTFormatDepth && gGraphicsCaps.hasNativeDepthTexture)
		colorBPP = 0;
	else if (format == kRTFormatShadowMap && gGraphicsCaps.hasNativeShadowMap)
		colorBPP = 0;
	else
		colorBPP = kRenderTextureFormatBPP[format];
	int size = width * height * colorBPP;

	if (dim == kTexDim3D)
		size *= depth;
	else if (dim == kTexDimCUBE)
		size *= 6;
	if (mipMap && gGraphicsCaps.hasAutoMipMapGeneration)
		size += size / 3;

	// depth buffer
	size += width * height * kDepthFormatBPP[depthFormat];
	return size;
}



int RenderTexture::GetRuntimeMemorySize() const
{
	int bytes = EstimateRenderTextureSize (m_Width, m_Height, m_VolumeDepth, (RenderTextureFormat)m_ColorFormat, (DepthBufferFormat)m_DepthFormat, m_Dimension, m_MipMap);
	bytes *= m_AntiAliasing;
	return bytes;// + Super::GetRuntimeMemorySize(); Since the name is assigned after this value is used for gfxstats accumulate, the string will add to the mem usage later - Causes Assert
}

void RenderTexture::ResolveAntiAliasedSurface()
{
	if (!m_ResolvedColorHandle.IsValid())
		return;

	GfxDevice& device = GetGfxDevice();
	device.ResolveColorSurface(m_ColorHandle, m_ResolvedColorHandle);
}

RenderTexture* EnsureRenderTextureIsCreated(RenderTexture* tex)
{
	RenderTexture* ret = tex;

	if(!RenderTexture::IsEnabled())
		ret = 0;

	if(ret && !ret->IsCreated())
		ret->Create();

	// check if we could create
	if(ret && !ret->IsCreated())
		ret = 0;

	return ret;
}

template<class TransferFunction>
void RenderTexture::Transfer (TransferFunction& transfer)
{
	Super::Transfer (transfer);
	TRANSFER (m_Width);
	TRANSFER (m_Height);
	TRANSFER (m_AntiAliasing);
	TRANSFER (m_DepthFormat);
	TRANSFER (m_ColorFormat);
	TRANSFER (m_MipMap);
	TRANSFER (m_GenerateMips);
	TRANSFER (m_SRGB);
	transfer.Align();
	TRANSFER (m_TextureSettings);
}

IMPLEMENT_CLASS (RenderTexture)
IMPLEMENT_OBJECT_SERIALIZE (RenderTexture)

bool RenderTextureSupportsStencil (RenderTexture* rt)
{
	if(!(gGraphicsCaps.hasStencil && GetBuildSettings ().hasAdvancedVersion))
		return false;

	if (rt)
		return rt->GetDepthFormat() >= kDepthFormat24;

	return GetGfxDevice().GetFramebufferDepthFormat() >= kDepthFormat24;
}

void RenderTextureDiscardContents(RenderTexture* rt, bool discardColor, bool discardDepth)
{
	GfxDevice& device = GetGfxDevice();

	RenderSurfaceHandle color  = rt ? rt->GetColorSurfaceHandle() : device.GetBackBufferColorSurface();
	RenderSurfaceHandle rcolor = rt ? rt->GetResolvedColorSurfaceHandle() : RenderSurfaceHandle();
	RenderSurfaceHandle depth  = rt ? rt->GetDepthSurfaceHandle() : device.GetBackBufferDepthSurface();

	if(discardColor && color.IsValid())
		device.DiscardContents(color);
	if(discardColor && rcolor.IsValid())
		device.DiscardContents(rcolor);
	if(discardDepth && depth.IsValid())
		device.DiscardContents(depth);
}


// --------------------------------------------------------------------------


#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"
SUITE (RenderTextureTests)
{
TEST(RenderTextureTests_BPPTableCorrect)
{
	// checks that you did not forget to update BPP counts when you added a new format :)
	for (int i = 0; i < kRTFormatCount; ++i)
	{
		CHECK(kRenderTextureFormatBPP[i] != 0);
	}
	for (int i = 1; i < kDepthFormatCount; ++i) // skip DepthFormatNone, since it is expected to have zero
	{
		CHECK(kDepthFormatBPP[i] != 0);
	}
}
}

#endif