summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/GLSLUtilities.cpp
blob: 2152ef3f2f11d7be80fdcd376e070db1bc9ccc07 (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
#include "UnityPrefix.h"
#include "GLSLUtilities.h"
#include "External/shaderlab/Library/ShaderLabErrors.h"

#if UNITY_ANDROID || UNITY_IPHONE
	#include "Runtime/Shaders/GraphicsCaps.h"
#endif

using namespace std;

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


void	OutputGLSLShaderError(const char* log, GLSLErrorType errorType, ShaderErrors& outErrors)
{

	const char* strErrorIds[kErrorCount] =
	{
		"GLSL Error in Vertex Shader: ",
		"GLSL Error in Fragment Shader: ",
		"GLSL Error while linking: "
	};

	string errorMessage = string(strErrorIds[errorType]) + log;

	int errorLine = 0;
	switch(errorType)
	{
	case kErrorCompileVertexShader:
	case kErrorCompileFragShader:
		{
			size_t b = errorMessage.find('(');
			size_t e = errorMessage.find(')');
			if(b != string::npos && e != string::npos && e>b)
			{
				string errorLineText = errorMessage.substr(b + 1, e - b - 1);
				errorLine = StringToInt(errorLineText);
			}

			outErrors.AddShaderError (errorMessage, errorLine, false);
		}
		break;
	case kErrorLinkProgram:
		errorMessage += log;
		outErrors.AddShaderError (errorMessage, errorLine, false);
		break;
	default:
		break;
	}
}


// --------------------------------------------------------------------------
// GLSL patching for fog

#define DEBUG_FOG_PATCHING 0

static inline bool IsNewline( char c ) { return c == '\n' || c == '\r'; }

static size_t FindStartOfBlock (const std::string& src, const std::string& token)
{
	size_t pos = src.find (token);
	if (pos == std::string::npos)
		return pos;
	const size_t n = src.size();
	while (pos < n && !IsNewline(src[pos])) ++pos; // skip until newline
	while (pos < n && IsNewline(src[pos])) ++pos; // skip newlines
	if (pos >= n)
		return std::string::npos;
	return pos;
}

static bool SkipUntilStatementEnd (const std::string& src, size_t& pos)
{
	const size_t n = src.size();
	while (pos < n && src[pos] != ';') ++pos; if (pos < n) ++pos;
	while (pos < n && IsNewline(src[pos])) ++pos; // skip following newlines
	return pos < n;
}

static void SkipUntilStatementBegin (const std::string& src, size_t& pos)
{
	const size_t n = src.size();
	while (pos > 0 && src[pos] != ';' && src[pos] != '{') --pos; if (pos < n) ++pos;
	while (pos < n && IsSpace(src[pos])) ++pos; // skip following whitespace
}

static size_t SkipGLSLDirectives (const std::string& src, size_t startPos = 0)
{
	size_t pos = startPos;
	const size_t n = src.size();
	while (pos < n)
	{
		// skip whitespace
		while (pos < n && IsSpace(src[pos])) ++pos;
		// have a '#'?
		if (pos >= n || src[pos] != '#')
			break;
		// skip until end of line
		while (pos < n && !IsNewline(src[pos])) ++pos;
	}
	return pos;
}


static int ParseGLSLVersion (const std::string& src, size_t startPos = 0)
{
	size_t pos = startPos;
	const size_t n = src.size();
	// skip whitespace
	while (pos < n && IsSpace(src[pos])) ++pos;
	// have a '#'?
	if (pos >= n || src[pos] != '#')
		return 0;
	++pos;
	// skip whitespace
	while (pos < n && IsSpace(src[pos])) ++pos;
	// have a "version"?
	if (0 != strncmp(src.c_str() + pos, "version", 7))
		return 0;
	pos += 7;
	// skip whitespace
	while (pos < n && IsSpace(src[pos])) ++pos;
	// parse a number
	int v = atoi(src.c_str() + pos);
	return v;
}

static const char* GetGLSLFogData (int version, bool vertex)
{
	if (version >= 150)
	{
		if (vertex)
			return "uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\nout float _unity_FogVar;\n";
		else
			return "uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\nin float _unity_FogVar;\n";
	}
	return "uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\nvarying float _unity_FogVar;\n";
}

static bool InsertFogVertexCode (std::string& src)
{
	size_t vertexStart = FindStartOfBlock (src, "#ifdef VERTEX");
	if (vertexStart == std::string::npos)
		return false;
	const int version = ParseGLSLVersion (src, vertexStart);
	vertexStart = SkipGLSLDirectives (src, vertexStart);

	const char* fogData = GetGLSLFogData(version, true);
	src.insert (vertexStart, fogData);

	size_t posWriteStart = src.find ("gl_Position", vertexStart);
	if (posWriteStart == std::string::npos)
		return false;

	size_t pos = posWriteStart;
	if (!SkipUntilStatementEnd (src, pos))
		return false;

	// insert fog code at pos!
	src.insert (pos, "_unity_FogVar = gl_Position.z;\n");

	return true;
}

static bool InsertFogFragmentCode (std::string& src, FogMode fog)
{
	size_t fragmentStart = FindStartOfBlock (src, "#ifdef FRAGMENT");
	if (fragmentStart == std::string::npos)
		return false;
	const int version = ParseGLSLVersion (src, fragmentStart);
	fragmentStart = SkipGLSLDirectives (src, fragmentStart);

	const char* fogData = GetGLSLFogData(version, false);
	src.insert (fragmentStart, fogData);

	bool writesToData = true; // writes to gl_FragData vs. gl_FragColor
	size_t colorWriteStart = src.find ("gl_FragData[0]", fragmentStart);
	if (colorWriteStart == std::string::npos)
	{
		colorWriteStart = src.find ("gl_FragColor", fragmentStart);
		if (colorWriteStart == std::string::npos)
			return false;
		writesToData = false;
	}

	size_t pos = colorWriteStart;
	if (!SkipUntilStatementEnd (src, pos))
		return false;

	// insert fog code at pos!
	std::string color = writesToData ? "gl_FragData[0]" : "gl_FragColor";
	if (fog == kFogExp2)
	{
		// fog = exp(-(density*z)^2)
		src.insert (pos,
			"  float _patchFog = _unity_FogParams.x * _unity_FogVar;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  " + color + ".rgb = mix (_unity_FogColor.rgb, " + color + ".rgb, _patchFog);\n");
	}
	else if (fog == kFogExp)
	{
		// fog = exp(-density*z)
		src.insert (pos,
			"  float _patchFog = _unity_FogParams.y * _unity_FogVar;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  " + color + ".rgb = mix (_unity_FogColor.rgb, " + color + ".rgb, _patchFog);\n");
	}
	else if (fog == kFogLinear)
	{
		// fog = (end-z)/(end-start)
		src.insert (pos,
			"  float _patchFog = clamp (_unity_FogParams.z * _unity_FogVar + _unity_FogParams.w, 0.0, 1.0);\n"
			"  " + color + ".rgb = mix (_unity_FogColor.rgb, " + color + ".rgb, _patchFog);\n");
	}

	return true;
}

bool PatchShaderFogGLSL (std::string& src, FogMode fog)
{
	#if DEBUG_FOG_PATCHING
	printf_console ("GLSL fog patching: original shader:\n%s\n", src.c_str());
	#endif

	if (!InsertFogVertexCode (src))
		return false;
	if (!InsertFogFragmentCode (src, fog))
		return false;

	#if DEBUG_FOG_PATCHING
	printf_console ("GLSL fog patching: after patching, fog mode %d:\n%s\n", fog, src.c_str());
	#endif
	return true;
}



static bool InsertFogVertexCodeGLES (std::string& src, FogMode fog, bool canUseOptimizedCode)
{
	size_t posWriteStart = src.find ("gl_Position");
	if (posWriteStart == std::string::npos)
		return false;

	size_t pos = posWriteStart;
	if (!SkipUntilStatementEnd (src, pos))
		return false;

	// TODO: remove duplication between optimized/normal fog code

	// insert fog code at pos!
	if (fog == kFogExp2)
	{
		// fog = exp(-(density*z)^2)
		if(canUseOptimizedCode)
		{
			src.insert (pos,
				"  float _patchFog = _unity_FogParams.x * gl_Position.z;\n"
				"  _patchFog = _patchFog * _patchFog;\n"
				"  _unity_FogVar = vec4(clamp (exp2(-_patchFog), 0.0, 1.0)); _unity_FogVar.a = 1.0;\n"
				"  _unity_FogColorPreMul = _unity_FogColor * (vec4(1.0)-_unity_FogVar);\n"
			);
		}
		else
		{
			src.insert (pos,
				"  float _patchFog = _unity_FogParams.x * gl_Position.z;\n"
				"  _patchFog = _patchFog * _patchFog;\n"
				"  _unity_FogVar = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			);
		}
	}
	else if (fog == kFogExp)
	{
		// fog = exp(-density*z)
		if(canUseOptimizedCode)
		{
			src.insert (pos,
				"  float _patchFog = _unity_FogParams.y * gl_Position.z;\n"
				"  _unity_FogVar = vec4(clamp (exp2(-_patchFog), 0.0, 1.0)); _unity_FogVar.a = 1.0;\n"
				"  _unity_FogColorPreMul = _unity_FogColor * (vec4(1.0)-_unity_FogVar);\n"
			);
		}
		else
		{
			src.insert (pos,
				"  float _patchFog = _unity_FogParams.y * gl_Position.z;\n"
				"  _unity_FogVar = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			);
		}
	}
	else if (fog == kFogLinear)
	{
		// fog = (end-z)/(end-start)
		if(canUseOptimizedCode)
		{
			src.insert (pos,
				"  _unity_FogVar = vec4(clamp (_unity_FogParams.z * gl_Position.z + _unity_FogParams.w, 0.0, 1.0)); _unity_FogVar.a = 1.0;\n"
				"  _unity_FogColorPreMul = _unity_FogColor * (vec4(1.0)-_unity_FogVar);\n"
			);
		}
		else
		{
			src.insert (pos,
				"  _unity_FogVar = clamp (_unity_FogParams.z * gl_Position.z + _unity_FogParams.w, 0.0, 1.0);\n"
			);
		}
	}

	return true;
}

static bool InsertFogFragmentCodeGLES (std::string& src, bool canUseOptimizedCode)
{
	bool writesToData = true; // writes to gl_FragData vs. gl_FragColor
	size_t colorWriteStart = src.find ("gl_FragData[0]");
	if (colorWriteStart == std::string::npos)
	{
		colorWriteStart = src.find ("gl_FragColor");
		if (colorWriteStart == std::string::npos)
			return false;
		writesToData = false;
	}

	// iPad2 crashes with some shaders using fog (not all of them though), case 399638.
	// Seems to avoid the crash if we don't read gl_FragColor after writing it. So instead,
	// introduce a new local variable, replace any previous uses with it, do fog patching
	// on it, and write out to final destination in the end.

	// replace color with new variable name
	const std::string color = writesToData ? "gl_FragData[0]" : "gl_FragColor";
	replace_string (src, color, "_patchFragColor");

	// find color output statement start & end
	size_t posBegin, posEnd;
	posBegin = posEnd = colorWriteStart;
	if (!SkipUntilStatementEnd (src, posEnd))
		return false;
	SkipUntilStatementBegin (src, posBegin);

	// insert new output variable declaration
	src.insert (posBegin, "lowp vec4 _patchFragColor;\n");
	posEnd += strlen("lowp vec4 _patchFragColor;\n");

	// insert fog code
	if(canUseOptimizedCode)
		src.insert (posEnd, "  _patchFragColor = _patchFragColor * _unity_FogVar + _unity_FogColorPreMul; " + color + " = _patchFragColor;\n");
	else
		src.insert (posEnd, "  _patchFragColor.rgb = mix (_unity_FogColor.rgb, _patchFragColor.rgb, _unity_FogVar); " + color + " = _patchFragColor;\n");

	return true;
}

bool PatchShaderFogGLES (std::string& srcVS, std::string& srcPS, FogMode fog, bool useOptimizedFogCode)
{
	#if DEBUG_FOG_PATCHING
	printf_console ("GLES fog patching: original vertex shader:\n%s\n...and pixel shader:\n%s\n", srcVS.c_str(), srcPS.c_str());
	#endif

	const int versionVS = ParseGLSLVersion (srcVS);
	const int versionPS = ParseGLSLVersion (srcPS);
	const size_t startVS = SkipGLSLDirectives(srcVS);
	const size_t startPS = SkipGLSLDirectives(srcPS);
	const char* fogDataVS = NULL;
	const char* fogDataPS = NULL;
	if (useOptimizedFogCode)
	{
		if (versionVS >= 150)
		{
			fogDataVS = "uniform highp vec4 _unity_FogParams;\nuniform lowp vec4 _unity_FogColor;\nout lowp vec4 _unity_FogColorPreMul;\nout lowp vec4 _unity_FogVar;\n";
			fogDataPS = "in lowp vec4 _unity_FogColorPreMul;\nin lowp vec4 _unity_FogVar;\n";
		}
		else
		{
			fogDataVS = "uniform highp vec4 _unity_FogParams;\nuniform lowp vec4 _unity_FogColor;\nvarying lowp vec4 _unity_FogColorPreMul;\nvarying lowp vec4 _unity_FogVar;\n";
			fogDataPS = "varying lowp vec4 _unity_FogColorPreMul;\nvarying lowp vec4 _unity_FogVar;\n";
		}
	}
	else
	{
		if (versionPS >= 150)
		{
			fogDataVS = "uniform highp vec4 _unity_FogParams;\nout lowp float _unity_FogVar;\n";
			fogDataPS = "uniform lowp vec4 _unity_FogColor;\nin lowp float _unity_FogVar;\n";
		}
		else
		{
			fogDataVS = "uniform highp vec4 _unity_FogParams;\nvarying lowp float _unity_FogVar;\n";
			fogDataPS = "uniform lowp vec4 _unity_FogColor;\nvarying lowp float _unity_FogVar;\n";
		}
	}
	srcVS.insert (startVS, fogDataVS);
	srcPS.insert (startPS, fogDataPS);


	if (!InsertFogVertexCodeGLES (srcVS, fog, useOptimizedFogCode))
		return false;
	if (!InsertFogFragmentCodeGLES (srcPS, useOptimizedFogCode))
		return false;

	#if DEBUG_FOG_PATCHING
	printf_console ("GLES fog patching: after patching, fog mode %d:\n%s\n...and pixel shader:\n%s\n", fog, srcVS.c_str(), srcPS.c_str());
	#endif
	return true;
}

static unsigned CalculateVaryingCount(const std::string& src)
{
	unsigned varCount = 0;

	size_t varUsage = src.find("varying ");
	while(varUsage != std::string::npos)
	{
		++varCount;
		varUsage = src.find("varying ", varUsage+7);
	}

	return varCount;
}

bool CanUseOptimizedFogCodeGLES(const std::string& srcVS)
{
#if UNITY_ANDROID || UNITY_IPHONE
	// we add 2 varyings in optimized path
	// in normal path we add one, so would be good to check it too, but we dont take "packing" into account
	// so we just act conservatively and apply optimization only when we are sure it works
	return (CalculateVaryingCount(srcVS) + 2 <= gGraphicsCaps.gles20.maxVaryings);
#else
	(void)srcVS;
	return true;
#endif
}

bool IsShaderParameterArray(const char* name, unsigned nameLen, int arraySize, bool* isZeroElem)
{
	bool zeroElem = nameLen > 3 && strcmp (name+nameLen-3, "[0]") == 0;
	if(isZeroElem)
		*isZeroElem = zeroElem;

	return arraySize > 1 || zeroElem;
}


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

#if ENABLE_UNIT_TESTS

#include "External/UnitTest++/src/UnitTest++.h"

SUITE (GlslFogPatchingTests)
{
	struct GlslPatchTestFixture
	{
		std::string vs, fs, s;
		std::string expvs, expfs, exps;
		bool DoPatching(FogMode mode, bool checkOptimizedGLESCode)
		{
			s = "#ifdef VERTEX\n" + vs + "\n#endif\n#ifdef FRAGMENT\n" + fs + "\n#endif\n";
			bool ok = true;
			ok &= PatchShaderFogGLSL (s, mode);
			ok &= PatchShaderFogGLES (vs, fs, mode, checkOptimizedGLESCode);
			return ok;
		}
	};

	TEST(SkipGLSLDirectivesWorks)
	{
		const char* s;
		CHECK_EQUAL(0, SkipGLSLDirectives("void")); // no directives
		CHECK_EQUAL(2, SkipGLSLDirectives("  void")); // skipped space
		CHECK_EQUAL(2, SkipGLSLDirectives("\n\nvoid")); // skipped newlines
		CHECK_EQUAL(12, SkipGLSLDirectives("#pragma foo\n")); // skipped whole line
		CHECK_EQUAL(14, SkipGLSLDirectives("  #pragma foo\n")); // skipped whole line
		CHECK_EQUAL(15, SkipGLSLDirectives("#pragma foo\r\n\r\n")); // skipped whole line, including various newlines
		CHECK_EQUAL(24, SkipGLSLDirectives("#pragma foo\n#pragma foo\nvoid")); // skipped two lines
	}

	TEST(ParseGLSLVersionWorks)
	{
		CHECK_EQUAL(0, ParseGLSLVersion(""));
		CHECK_EQUAL(0, ParseGLSLVersion("void"));
		CHECK_EQUAL(0, ParseGLSLVersion("  # pragma"));
		CHECK_EQUAL(0, ParseGLSLVersion("#version"));

		CHECK_EQUAL(120, ParseGLSLVersion("#version 120"));
		CHECK_EQUAL(200, ParseGLSLVersion("  #  version   200"));
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchSkipsVersionAndExtensionDirectives)
	{
		vs =
			"#version 120\n"
			"#extension bar : enable\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"}";
		fs =
			"#extension bar : enable\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp2,true));
		expvs =
			"#version 120\n"
			"#extension bar : enable\n"
			"uniform highp vec4 _unity_FogParams;\n";
		expfs =
			"#extension bar : enable\n"
			"varying lowp vec4 _unity_FogColorPreMul;\n";
		exps =
			"#ifdef VERTEX\n"
			"#version 120\n"
			"#extension bar : enable\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"#extension bar : enable\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * _unity_FogVar;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragColor.rgb = mix (_unity_FogColor.rgb, gl_FragColor.rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK (BeginsWith(vs, expvs));
		CHECK (BeginsWith(fs, expfs));
		CHECK_EQUAL (exps, s);
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchTakesVersionIntoAccount)
	{
		vs =
			"#version 300 es\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"}";
		fs =
			"  #version  420\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp2,true));
		expvs =
			"#version 300 es\n"
			"uniform highp vec4 _unity_FogParams;\n"
			"uniform lowp vec4 _unity_FogColor;\n"
			"out lowp vec4 _unity_FogColorPreMul;\n"
			"out lowp vec4 _unity_FogVar;\n";
		expfs =
			"  #version  420\n"
			"in lowp vec4 _unity_FogColorPreMul;\n"
			"in lowp vec4 _unity_FogVar;\n";
		exps =
			"#ifdef VERTEX\n"
			"#version 300 es\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"out float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"  #version  420\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"in float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * _unity_FogVar;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragColor.rgb = mix (_unity_FogColor.rgb, gl_FragColor.rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK (BeginsWith(vs, expvs));
		CHECK (BeginsWith(fs, expfs));
		CHECK_EQUAL (exps, s);
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchExp2WriteToColorOptimized)
	{
		vs =
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"}";
		fs =
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp2,true));
		expvs =
			"uniform highp vec4 _unity_FogParams;\n"
			"uniform lowp vec4 _unity_FogColor;\n"
			"varying lowp vec4 _unity_FogColorPreMul;\n"
			"varying lowp vec4 _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * gl_Position.z;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _unity_FogVar = vec4(clamp (exp2(-_patchFog), 0.0, 1.0)); _unity_FogVar.a = 1.0;\n"
			"  _unity_FogColorPreMul = _unity_FogColor * (vec4(1.0)-_unity_FogVar);\n"
			"}";
		expfs =
			"varying lowp vec4 _unity_FogColorPreMul;\n"
			"varying lowp vec4 _unity_FogVar;\n"
			"void main() {\n"
			"  lowp vec4 _patchFragColor;\n"
			"_patchFragColor = vec4(1,2,3,4);\n"
			"  _patchFragColor = _patchFragColor * _unity_FogVar + _unity_FogColorPreMul; gl_FragColor = _patchFragColor;\n"
			"}";
		exps =
			"#ifdef VERTEX\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * _unity_FogVar;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragColor.rgb = mix (_unity_FogColor.rgb, gl_FragColor.rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK_EQUAL (expvs, vs);
		CHECK_EQUAL (expfs, fs);
		CHECK_EQUAL (exps, s);
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchExp2WriteToColor)
	{
		vs =
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"}";
		fs =
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp2,false));
		expvs =
			"uniform highp vec4 _unity_FogParams;\n"
			"varying lowp float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * gl_Position.z;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _unity_FogVar = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"}";
		expfs =
			"uniform lowp vec4 _unity_FogColor;\n"
			"varying lowp float _unity_FogVar;\n"
			"void main() {\n"
			"  lowp vec4 _patchFragColor;\n"
			"_patchFragColor = vec4(1,2,3,4);\n"
			"  _patchFragColor.rgb = mix (_unity_FogColor.rgb, _patchFragColor.rgb, _unity_FogVar); gl_FragColor = _patchFragColor;\n"
			"}";
		exps =
			"#ifdef VERTEX\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"void main() {\n"
			"  gl_FragColor = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.x * _unity_FogVar;\n"
			"  _patchFog = _patchFog * _patchFog;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragColor.rgb = mix (_unity_FogColor.rgb, gl_FragColor.rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK_EQUAL (expvs, vs);
		CHECK_EQUAL (expfs, fs);
		CHECK_EQUAL (exps, s);
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchExpWriteToDataWithStuffAroundOptimized)
	{
		vs =
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  foo = 1.0;\n"
			"}";
		fs =
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  gl_FragData[0] = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp,true));
		expvs =
			"uniform highp vec4 _unity_FogParams;\n"
			"uniform lowp vec4 _unity_FogColor;\n"
			"varying lowp vec4 _unity_FogColorPreMul;\n"
			"varying lowp vec4 _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.y * gl_Position.z;\n"
			"  _unity_FogVar = vec4(clamp (exp2(-_patchFog), 0.0, 1.0)); _unity_FogVar.a = 1.0;\n"
			"  _unity_FogColorPreMul = _unity_FogColor * (vec4(1.0)-_unity_FogVar);\n"
			"  foo = 1.0;\n"
			"}";
		expfs =
			"varying lowp vec4 _unity_FogColorPreMul;\n"
			"varying lowp vec4 _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  lowp vec4 _patchFragColor;\n"
			"_patchFragColor = vec4(1,2,3,4);\n"
			"  _patchFragColor = _patchFragColor * _unity_FogVar + _unity_FogColorPreMul; gl_FragData[0] = _patchFragColor;\n"
			"}";
		exps =
			"#ifdef VERTEX\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"  foo = 1.0;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  gl_FragData[0] = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.y * _unity_FogVar;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragData[0].rgb = mix (_unity_FogColor.rgb, gl_FragData[0].rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK_EQUAL (expvs, vs);
		CHECK_EQUAL (expfs, fs);
		CHECK_EQUAL (exps, s);
	}

	TEST_FIXTURE(GlslPatchTestFixture,PatchExpWriteToDataWithStuffAround)
	{
		vs =
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  foo = 1.0;\n"
			"}";
		fs =
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  gl_FragData[0] = vec4(1,2,3,4);\n"
			"}";
		CHECK(DoPatching(kFogExp,false));
		expvs =
			"uniform highp vec4 _unity_FogParams;\n"
			"varying lowp float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.y * gl_Position.z;\n"
			"  _unity_FogVar = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  foo = 1.0;\n"
			"}";
		expfs =
			"uniform lowp vec4 _unity_FogColor;\n"
			"varying lowp float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  lowp vec4 _patchFragColor;\n"
			"_patchFragColor = vec4(1,2,3,4);\n"
			"  _patchFragColor.rgb = mix (_unity_FogColor.rgb, _patchFragColor.rgb, _unity_FogVar); gl_FragData[0] = _patchFragColor;\n"
			"}";
		exps =
			"#ifdef VERTEX\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  gl_Position = vec4(1,2,3,4);\n"
			"_unity_FogVar = gl_Position.z;\n"
			"  foo = 1.0;\n"
			"}\n"
			"#endif\n"
			"#ifdef FRAGMENT\n"
			"uniform vec4 _unity_FogColor; uniform vec4 _unity_FogParams;\n"
			"varying float _unity_FogVar;\n"
			"varying float foo;\n"
			"void main() {\n"
			"  foo = 2.0;\n"
			"  gl_FragData[0] = vec4(1,2,3,4);\n"
			"  float _patchFog = _unity_FogParams.y * _unity_FogVar;\n"
			"  _patchFog = clamp (exp2(-_patchFog), 0.0, 1.0);\n"
			"  gl_FragData[0].rgb = mix (_unity_FogColor.rgb, gl_FragData[0].rgb, _patchFog);\n"
			"}\n"
			"#endif\n";
		CHECK_EQUAL (expvs, vs);
		CHECK_EQUAL (expfs, fs);
		CHECK_EQUAL (exps, s);
	}

	TEST(CalculateVaryingCount)
	{
		// TODO: should we handle packing? for now we dont need it, but point for the future
		std::string test = "varying float foo; varying float foo2;";
		CHECK_EQUAL(CalculateVaryingCount(test), 2);
	}
} // SUITE

#endif // ENABLE_UNIT_TESTS