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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
#include "UnityPrefix.h"
#if GFX_SUPPORTS_OPENGLES20
#include "GpuProgramsGLES20.h"
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Utilities/ArrayUtility.h"
#include "Runtime/Utilities/File.h"
#include "Runtime/File/ApplicationSpecificPersistentDataPath.h"
#include "Runtime/Shaders/GraphicsCaps.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "External/shaderlab/Library/ShaderLabErrors.h"
#include "Runtime/GfxDevice/ChannelAssigns.h"
#include "External/shaderlab/Library/shaderlab.h"
#include "External/shaderlab/Library/texenv.h"
#include "External/shaderlab/Library/program.h"
#include "Runtime/Math/Matrix4x4.h"
#include "Runtime/Math/Matrix3x3.h"
#include "Runtime/Math/Vector4.h"
#include "IncludesGLES20.h"
#include "AssertGLES20.h"
#include "GpuPropertiesGLES20.h"
#include "Runtime/Utilities/GLSLUtilities.h"
#include "VBOGLES20.h"
#include "DebugGLES20.h"
#include "UnityGLES20Ext.h"
#if UNITY_ANDROID
#include "PlatformDependent/AndroidPlayer/EntryPoint.h"
#include "PlatformDependent/AndroidPlayer/AndroidSystemInfo.h"
#endif
#if UNITY_BLACKBERRY
#include "ctype.h"
#endif
#include <stdio.h>
#define DEBUG_GLSL_BINDINGS 0
#define DEBUG_SHADER_CACHE 0
// from shader_yacc.hpp
extern std::string g_LastParsedShaderName;
void GLSLUseProgramGLES20 (UInt32 programID); // defined in GfxDeviceGLES20.cpp
bool CompileGLSLVertexShader (const std::string& source, ChannelAssigns& channels, GLShaderID programID, GLShaderID parentProgramID, GLShaderID* outShaderID);
bool CompileGLSLFragmentShader (const std::string& source, GLShaderID* outShaderID);
bool BindVProgAttrbutes(const std::string& source, ChannelAssigns& channels, GLShaderID programID);
bool RebindVProgAttrbutes(GLShaderID programID, GLShaderID parentProgramID);
static void GetCachedBinaryName(const std::string& vprog, const std::string& fshader, char filename[33]);
// --------------------------------------------------------------------------
// GLSL
// AttributeConversionTable
const static UInt32 kAttribLookupTableSize = 12;
const static char* s_GLSLESAttributes[kAttribLookupTableSize] = {
"_glesVertex", "_glesColor", "_glesNormal",
"_gles_unused__", // unused
"_glesMultiTexCoord0", "_glesMultiTexCoord1", "_glesMultiTexCoord2", "_glesMultiTexCoord3",
"_glesMultiTexCoord4", "_glesMultiTexCoord5", "_glesMultiTexCoord6", "_glesMultiTexCoord7"
};
const static char* s_UnityAttributes[kAttribLookupTableSize] = {
"Vertex", "Color", "Normal",
"", // unused
"TexCoord", "TexCoord1", "TexCoord2", "TexCoord3",
"TexCoord4", "TexCoord5", "TexCoord6", "TexCoord7"
};
const VertexComponent s_UnityVertexComponents[kAttribLookupTableSize] = {
kVertexCompVertex,
kVertexCompColor,
kVertexCompNormal,
kVertexCompTexCoord,
kVertexCompTexCoord0, kVertexCompTexCoord1, kVertexCompTexCoord2, kVertexCompTexCoord3,
kVertexCompTexCoord4, kVertexCompTexCoord5, kVertexCompTexCoord6, kVertexCompTexCoord7
};
const GLuint s_GLESVertexComponents[kAttribLookupTableSize] = {
GL_VERTEX_ARRAY,
GL_COLOR_ARRAY,
GL_NORMAL_ARRAY,
~0 /*kVertexCompTexCoord*/,
GL_TEXTURE_ARRAY0, GL_TEXTURE_ARRAY1, GL_TEXTURE_ARRAY2, GL_TEXTURE_ARRAY3,
GL_TEXTURE_ARRAY4, GL_TEXTURE_ARRAY5, GL_TEXTURE_ARRAY6, GL_TEXTURE_ARRAY7
};
GlslGpuProgramGLES20::GlslGpuProgramGLES20 (const std::string& source, CreateGpuProgramOutput& output)
{
output.SetPerFogModeParamsEnabled(true);
m_ImplType = kShaderImplBoth;
for (int i = 0; i < kFogModeCount; ++i)
{
m_GLSLVertexShader[i] = 0;
m_GLSLFragmentShader[i] = 0;
m_FogColorIndex[i] = -1;
m_FogParamsIndex[i] = -1;
m_FogFailed[i] = false;
}
// Fragment shaders come out as dummy GLSL text. Just ignore them; the real shader was part of
// the vertex shader text anyway.
if (source.empty())
return;
if (Create (source, output.CreateChannelAssigns()))
{
GpuProgramParameters& params = output.CreateParams();
FillParams (m_Programs[kFogDisabled], params, output.GetOutNames());
if (params.GetTextureParams().size() > gGraphicsCaps.maxTexImageUnits)
m_NotSupported = true;
m_UniformCache[kFogDisabled].Create(¶ms, -1, -1);
}
else
{
m_NotSupported = true;
}
}
GlslGpuProgramGLES20::~GlslGpuProgramGLES20 ()
{
Assert (m_ImplType == kShaderImplBoth);
for (int i = 0; i < kFogModeCount; ++i)
{
if (m_GLSLVertexShader[i]) { GLES_CHK(glDeleteShader(m_GLSLVertexShader[i])); }
if (m_GLSLFragmentShader[i]) { GLES_CHK(glDeleteShader(m_GLSLFragmentShader[i])); }
if (m_Programs[i]) { GLES_CHK(glDeleteProgram(m_Programs[i])); }
m_UniformCache[i].Destroy();
}
}
static bool ParseGlslErrors (GLuint type, GLSLErrorType errorType, const char* source = 0)
{
bool hadErrors = false;
char compileInfoLog[4096];
GLsizei infoLogLength = 0;
switch(errorType)
{
case kErrorCompileVertexShader:
case kErrorCompileFragShader:
glGetShaderInfoLog(type, 4096, &infoLogLength, compileInfoLog);
break;
case kErrorLinkProgram:
glGetProgramInfoLog(type, 4096, &infoLogLength, compileInfoLog);
break;
default:
FatalErrorMsg("Unknown error type");
break;
}
if(infoLogLength)
{
hadErrors = true;
if(source)
{
ErrorStringMsg("-------- failed compiling %s:\n", errorType==kErrorCompileVertexShader?"vertex program":"fragment shader");
DebugTextLineByLine(source);
}
ErrorStringMsg("-------- GLSL error: %s\n\n", compileInfoLog);
}
return hadErrors;
}
static std::string ExtractDefineBock(const std::string& defineName, const std::string& str, std::string* remainderStr)
{
const std::string beginsWith = "#ifdef " + defineName;
const std::string endsWith = "#endif";
size_t b;
if ((b = str.find(beginsWith))==std::string::npos)
return "";
b += beginsWith.size();
size_t e = b;
size_t n = 1;
do
{
size_t nextEnd = str.find(endsWith, e);
size_t nextIf = str.find("#if", e);
if (nextEnd == std::string::npos)
return "";
if (nextIf != std::string::npos && nextIf < nextEnd)
{
++n;
e = nextIf + 1;
}
else
{
--n;
e = nextEnd + 1;
}
}
while (n > 0);
std::string retVal = str.substr(b, e-b-1);
if (remainderStr)
{
*remainderStr = str.substr(0, b - beginsWith.size());
if (e + endsWith.size() < str.length()) *remainderStr += str.substr(e + endsWith.size());
}
return retVal;
}
static void FindProgramStart(const char* source, std::string* header, std::string* prog)
{
const char* line_start = source;
while(*line_start)
{
while(isspace(*line_start))
++line_start;
if(*line_start == '#')
{
while(*line_start != '\n' && *line_start != '\r')
++line_start;
}
else
{
header->assign(source, line_start - source);
prog->assign(line_start);
break;
}
}
}
bool CompileGlslShader(GLShaderID shader, GLSLErrorType type, const char* source)
{
GLES_CHK(glShaderSource(shader, 1, &source, NULL));
GLES_CHK(glCompileShader(shader));
int compiled = 10;
GLES_CHK(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));
if (compiled==0)
{
ParseGlslErrors(shader, type, source);
return false;
}
return true;
}
bool GlslGpuProgramGLES20::Create (const std::string &shaderString, ChannelAssigns& channels)
{
if( !gGraphicsCaps.gles20.hasGLSL )
return false;
GLESAssert(); // Clear any GL errors
GLES_CHK(m_Programs[0] = glCreateProgram());
m_ImplType = kShaderImplBoth;
// NOTE: pre-pending shader with VERTEX/FRAGMENT defines doesn't work with ES/GLSL compilers for some reason
// therefore we extract VERTEX/FRAGMENT sections from the shaderString
std::string remainder = shaderString;
std::string vertexShaderSource = ExtractDefineBock("VERTEX", shaderString, &remainder);
std::string fragmentShaderSource = ExtractDefineBock("FRAGMENT", remainder, &remainder);
vertexShaderSource = remainder + vertexShaderSource;
fragmentShaderSource = remainder + fragmentShaderSource;
if(!CompileProgram(0, vertexShaderSource, fragmentShaderSource, channels))
{
ParseGlslErrors(m_Programs[0], kErrorLinkProgram);
// TODO: cleanup
return false;
}
m_VertexShaderSourceForFog = vertexShaderSource;
m_SourceForFog = fragmentShaderSource;
return true;
}
std::string GlslGpuProgramGLES20::_CachePath;
bool GlslGpuProgramGLES20::InitBinaryShadersSupport()
{
#if UNITY_ANDROID
// the implementation is broken on pre-HC on pvr at least, but we'd rather play safe with this dino stuff
if(android::systeminfo::ApiLevel() < android::apiHoneycomb)
return false;
// Huawei is osam in general
if(gGraphicsCaps.rendererString.find("Immersion") != std::string::npos)
return false;
#endif
if(QueryExtension("GL_OES_get_program_binary") && gGlesExtFunc.glGetProgramBinaryOES && gGlesExtFunc.glProgramBinaryOES)
{
int binFormatCount = 0;
GLES_CHK(glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &binFormatCount));
if(binFormatCount > 0)
{
_CachePath = GetTemporaryCachePathApplicationSpecific() + "/UnityShaderCache/";
if(!IsDirectoryCreated(_CachePath))
CreateDirectory(_CachePath);
return true;
}
}
return false;
}
void Internal_ClearShaderCache()
{
std::string shaderCache = GetTemporaryCachePathApplicationSpecific() + "/UnityShaderCache/";
DeleteFileOrDirectory(shaderCache);
CreateDirectory(shaderCache);
}
bool GlslGpuProgramGLES20::CompileProgram(unsigned index, const std::string& vprog, const std::string& fshader, ChannelAssigns& channels)
{
std::string binaryPath;
if(gGraphicsCaps.gles20.hasBinaryShaders)
{
char filename[33] = {0};
GetCachedBinaryName(vprog, fshader, filename);
binaryPath = _CachePath + filename;
#if DEBUG_SHADER_CACHE
::printf_console("Starting compilation \"%s\" variation with md5:%s\n", g_LastParsedShaderName.c_str(), filename);
#endif
}
// first 4 bytes are for binary format
char* binaryData = 0;
char* binaryProgram = 0;
unsigned binaryLength = 0;
bool loadedBinary = false;
if(gGraphicsCaps.gles20.hasBinaryShaders)
{
FILE* binaryFile = ::fopen(binaryPath.c_str(), "rb");
if(binaryFile)
{
::fseek(binaryFile, 0, SEEK_END);
unsigned datasz = (unsigned)::ftell(binaryFile);
::fseek(binaryFile, 0, SEEK_SET);
binaryData = (char*)::malloc(datasz);
binaryProgram = binaryData + sizeof(GLenum);
binaryLength = datasz - sizeof(GLenum);
::fread(binaryData, datasz, 1, binaryFile);
::fclose(binaryFile);
loadedBinary = true;
#if DEBUG_SHADER_CACHE
::printf_console("Loaded from cache");
#endif
}
}
if(loadedBinary)
{
loadedBinary = false;
bool attrBound = index==0 ? BindVProgAttrbutes(vprog, channels, m_Programs[0])
: RebindVProgAttrbutes(m_Programs[index], m_Programs[0]);
if(attrBound)
{
GLES_CHK(gGlesExtFunc.glProgramBinaryOES(m_Programs[index], *((GLenum*)binaryData), binaryProgram, binaryLength));
int linked = 0;
GLES_CHK(glGetProgramiv(m_Programs[index], GL_LINK_STATUS, &linked));
loadedBinary = linked != 0;
}
if(!loadedBinary)
{
#if DEBUG_SHADER_CACHE
::printf_console("Bad cached version\n");
#endif
::free(binaryData);
binaryProgram = binaryData = 0;
}
}
// fallback to compiling shaders at runtime
if(!loadedBinary)
{
DBG_SHADER_VERBOSE_GLES20("Compiling shader: %s\n", g_LastParsedShaderName.c_str());
#if DEBUG_SHADER_CACHE
::printf_console("Actually compiling");
#endif
if(!CompileGLSLVertexShader(vprog, channels, m_Programs[index], m_Programs[0], &m_GLSLVertexShader[index]))
return false;
if(!CompileGLSLFragmentShader(fshader, &m_GLSLFragmentShader[index]))
return false;
GLES_CHK(glAttachShader(m_Programs[index], m_GLSLVertexShader[index]));
GLES_CHK(glAttachShader(m_Programs[index], m_GLSLFragmentShader[index]));
GLES_CHK(glLinkProgram(m_Programs[index]));
int linked = 0;
GLES_CHK(glGetProgramiv(m_Programs[index], GL_LINK_STATUS, &linked));
if(linked == 0)
return false;
if(gGraphicsCaps.gles20.hasBinaryShaders)
{
Assert(binaryData == 0 && binaryProgram == 0);
GLES_CHK(glGetProgramiv(m_Programs[index], GL_PROGRAM_BINARY_LENGTH_OES, (GLint*)&binaryLength));
binaryData = (char*)::malloc(binaryLength + sizeof(GLenum));
binaryProgram = binaryData + sizeof(GLenum);
GLES_CHK(gGlesExtFunc.glGetProgramBinaryOES(m_Programs[index], binaryLength, 0, (GLenum*)binaryData, binaryProgram));
FILE* binaryFile = ::fopen(binaryPath.c_str(), "wb");
if(binaryFile)
{
::fwrite(binaryData, binaryLength + sizeof(GLenum), 1, binaryFile);
::fclose(binaryFile);
#if DEBUG_SHADER_CACHE
::printf_console("Saved to cache\n");
#endif
}
}
}
if(binaryData)
::free(binaryData);
return true;
}
static void PrintNumber (char* s, int i, bool brackets)
{
DebugAssert (i >= 0 && i < 100);
if (brackets)
*s++ = '[';
if (i < 10) {
*s++ = '0' + i;
} else {
*s++ = '0' + i/10;
*s++ = '0' + i%10;
}
if (brackets)
*s++ = ']';
*s++ = 0;
}
static void AddSizedVectorParam (GpuProgramParameters& params, ShaderParamType type, GLuint program, int vectorSize, int uniformNumber, int arraySize, const char* unityName, char* glName, int glNameIndexOffset, PropertyNamesSet* outNames)
{
if (arraySize <= 1)
{
params.AddVectorParam (uniformNumber, type, vectorSize, unityName, -1, outNames);
}
else
{
for (int j = 0; j < arraySize; ++j)
{
PrintNumber (glName+glNameIndexOffset, j, true);
uniformNumber = glGetUniformLocation (program, glName);
PrintNumber (glName+glNameIndexOffset, j, false);
params.AddVectorParam (uniformNumber, type, vectorSize, glName, -1, outNames);
}
}
}
static void AddSizedMatrixParam (GpuProgramParameters& params, GLuint program, int rows, int cols, int uniformNumber, int arraySize, const char* unityName, char* glName, int glNameIndexOffset, PropertyNamesSet* outNames)
{
if (arraySize <= 1)
{
params.AddMatrixParam (uniformNumber, unityName, rows, cols, -1, outNames);
}
else
{
for (int j = 0; j < arraySize; ++j)
{
PrintNumber (glName+glNameIndexOffset, j, true);
uniformNumber = glGetUniformLocation (program, glName);
PrintNumber (glName+glNameIndexOffset, j, false);
params.AddMatrixParam (uniformNumber, glName, rows, cols, -1, outNames);
}
}
}
void GlslGpuProgramGLES20::FillParams (unsigned int programID, GpuProgramParameters& params, PropertyNamesSet* outNames)
{
if (!programID)
return;
int activeUniforms;
char name[1024];
GLenum type;
int arraySize = 0,
nameLength = 0,
bufSize = sizeof(name);
DBG_LOG_GLES20("GLSL: apply params to program id=%i\n", programID);
GLSLUseProgramGLES20 (programID);
// Figure out the uniforms
GLES_CHK(glGetProgramiv (programID, GL_ACTIVE_UNIFORMS, &activeUniforms));
for(int i=0; i < activeUniforms; i++)
{
//chai: 获得Uniform变量名
GLES_CHK(glGetActiveUniform (programID, i, bufSize, &nameLength, &arraySize, &type, name));
if (!strcmp (name, "_unity_FogParams") || !strcmp(name, "_unity_FogColor"))
continue;
// some Unity builtin properties are mapped to GLSL structure fields
// hijack them here
const char* glslName = GetGLSLESPropertyNameRemap(name);
const char* unityName = glslName ? glslName : name;
if (!strncmp (name, "gl_", 3)) // skip "gl_" names
continue;
int uniformNumber = glGetUniformLocation (programID, name);
Assert(uniformNumber != -1);
char* glName = name;
int glNameIndexOffset = 0;
bool isElemZero = false;
bool isArray = IsShaderParameterArray(name, nameLength, arraySize, &isElemZero);
if (isArray)
{
// for array parameters, transform name a bit: Foo[0] becomes Foo0
if (arraySize >= 100) {
ErrorString( "GLSL: array sizes larger than 99 not supported" ); // TODO: SL error
arraySize = 99;
}
// TODO: wrong? what if we use only array[1] for example
if (isElemZero)
{
glNameIndexOffset = nameLength-3;
glName[glNameIndexOffset] = '0';
glName[glNameIndexOffset+1] = 0;
}
else
{
glNameIndexOffset = nameLength;
}
}
if (type == GL_FLOAT)
AddSizedVectorParam (params, kShaderParamFloat, programID, 1, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_FLOAT_VEC2)
AddSizedVectorParam (params, kShaderParamFloat, programID, 2, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_FLOAT_VEC3)
AddSizedVectorParam (params, kShaderParamFloat, programID, 3, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_FLOAT_VEC4)
AddSizedVectorParam (params, kShaderParamFloat, programID, 4, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_INT)
AddSizedVectorParam (params, kShaderParamInt, programID, 1, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_INT_VEC2)
AddSizedVectorParam (params, kShaderParamInt, programID, 2, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_INT_VEC3)
AddSizedVectorParam (params, kShaderParamInt, programID, 3, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_INT_VEC4)
AddSizedVectorParam (params, kShaderParamInt, programID, 4, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_BOOL)
AddSizedVectorParam (params, kShaderParamBool, programID, 1, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_BOOL_VEC2)
AddSizedVectorParam (params, kShaderParamBool, programID, 2, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_BOOL_VEC3)
AddSizedVectorParam (params, kShaderParamBool, programID, 3, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_BOOL_VEC4)
AddSizedVectorParam (params, kShaderParamBool, programID, 4, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_FLOAT_MAT4)
AddSizedMatrixParam (params, programID, 4, 4, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_FLOAT_MAT3)
AddSizedMatrixParam (params, programID, 3, 3, uniformNumber, arraySize, unityName, glName, glNameIndexOffset, outNames);
else if (type == GL_SAMPLER_2D || type == GL_SAMPLER_2D_SHADOW_EXT) {
const int texIndex = params.GetTextureParams().size(); // statically bind this uniform to sequential texture index
GLES_CHK(glUniform1i (uniformNumber, texIndex));
params.AddTextureParam (texIndex, -1, unityName, kTexDim2D, outNames);
}
else if (type == GL_SAMPLER_CUBE) {
const int texIndex = params.GetTextureParams().size(); // statically bind this uniform to sequential texture index
GLES_CHK(glUniform1i (uniformNumber, texIndex));
params.AddTextureParam (texIndex, -1, unityName, kTexDimCUBE, outNames);
}
/*
else if(type == GL_SAMPLER_3D) {
GLES_CHK(glUniform1i (uniformNumber, params.GetTextureParams().size()));
params.AddTextureParam( name, kTexDim3D, uniformNumber );
}
else if(type == GL_SAMPLER_2D_SHADOW) {
GLES_CHK(glUniform1i (uniformNumber, params.GetTextureParams().size()));
params.AddTextureParam( name, kTexDim2D, uniformNumber );
}
*/
else {
AssertString( "Unrecognized GLSL uniform type" );
}
}
GLESAssert();
}
bool RebindVProgAttrbutes(GLShaderID programID, GLShaderID parentProgramID)
{
int attribCount = 0;
GLES_CHK(glGetProgramiv(parentProgramID, GL_ACTIVE_ATTRIBUTES, &attribCount));
const int kBufSize = 256;
char name[kBufSize];
for(int i = 0 ; i < attribCount ; ++i)
{
int nameLength = 0, arraySize = 0;
GLenum type;
GLES_CHK(glGetActiveAttrib (parentProgramID, i, kBufSize, &nameLength, &arraySize, &type, name));
int location = glGetAttribLocation (parentProgramID, name);
if (location != -1)
GLES_CHK(glBindAttribLocation(programID, location, name));
}
return true;
}
bool BindVProgAttrbutes(const std::string& source, ChannelAssigns& channels, GLShaderID programID)
{
// Add necessary attribute tags
for (UInt32 j = 0; j < kAttribLookupTableSize; j++)
{
if (source.find(s_GLSLESAttributes[j]) != std::string::npos)
{
if (s_GLESVertexComponents[j] >= gGraphicsCaps.gles20.maxAttributes)
{
ErrorString("Shader uses too many vertex attributes for this platform");
return false;
}
GLES_CHK(glBindAttribLocation(programID, s_GLESVertexComponents[j], s_GLSLESAttributes[j]));
ShaderChannel shaderChannel = GetShaderChannelFromName(s_UnityAttributes[j]);
if( shaderChannel != kShaderChannelNone )
channels.Bind (shaderChannel, s_UnityVertexComponents[j]);
}
}
// UGLY HACK:
// somewhere deep inside shader generation we just put attribute TANGENT
// without ever using it after
// look for TANGENT twice to be sure we use it
size_t firstTangentUsage = source.find("TANGENT");
if( firstTangentUsage != std::string::npos && source.find("TANGENT", firstTangentUsage+1) != std::string::npos )
{
// Find first free slot for tangents and use it.
// Unity normally supports 2 UV slots (0&1), so start looking from
// kVertexTexCoord2
for (int i = kVertexCompTexCoord2; i < kVertexCompTexCoord7; i++)
{
if (channels.GetSourceForTarget((VertexComponent)i) == kShaderChannelNone)
{
channels.Bind (kShaderChannelTangent, (VertexComponent)i);
GLES_CHK(glBindAttribLocation(programID, GL_TEXTURE_ARRAY0 + i - kVertexCompTexCoord0, "_glesTANGENT"));
break;
}
}
}
return true;
}
bool CompileGLSLVertexShader (const std::string& source, ChannelAssigns& channels, GLShaderID programID, GLShaderID parentProgramID, GLShaderID* outShaderID)
{
GLES_CHK(*outShaderID = glCreateShader(GL_VERTEX_SHADER));
if(parentProgramID == programID)
BindVProgAttrbutes(source, channels, programID);
else
RebindVProgAttrbutes(programID, parentProgramID);
#if UNITY_ANDROID
if(gGraphicsCaps.gles20.buggyVprogTextures)
{
if( source.find("texture2D") != std::string::npos || source.find("tex2D") != std::string::npos )
{
ErrorString("GLES20: Running on platform with buggy vprog textures.\n");
ErrorString("GLES20: Compiling this shader may result in crash.\n");
ErrorString("GLES20: Shader in question:\n");
DebugTextLineByLine(source.c_str());
ErrorString("\n---------------\n");
}
}
#endif
const char* text = source.c_str();
DBG_SHADER_VERBOSE_GLES20_DUMP_SHADER("Compiling VERTEX program:", text);
return CompileGlslShader(*outShaderID, kErrorCompileVertexShader, text);
}
bool CompileGLSLFragmentShader (const std::string& source, GLShaderID* outShaderID)
{
*outShaderID = glCreateShader(GL_FRAGMENT_SHADER);
///@TODO: find any existing precision statement
std::string modSourceHeader, modSourceProg;
FindProgramStart(source.c_str(), &modSourceHeader, &modSourceProg);
std::string prec_modifier = gGraphicsCaps.gles20.forceHighpFSPrec ? "precision highp float;\n" : "precision mediump float;\n";
std::string modSource = modSourceHeader + prec_modifier + modSourceProg;
const char* text = modSource.c_str();
DBG_SHADER_VERBOSE_GLES20_DUMP_SHADER("Compiling FRAGMENT program:", text);
return CompileGlslShader(*outShaderID, kErrorCompileFragShader, text);
}
int GlslGpuProgramGLES20::GetGLProgram (FogMode fog, GpuProgramParameters& outParams, ChannelAssigns &channels)
{
int index = 0;
if (fog > kFogDisabled && !m_FogFailed[fog] && !m_SourceForFog.empty())
{
index = fog;
Assert (index >= 0 && index < kFogModeCount);
// create patched fog program if needed
if(!m_Programs[index])
{
std::string srcVS = m_VertexShaderSourceForFog;
std::string srcPS = m_SourceForFog;
if(PatchShaderFogGLES (srcVS, srcPS, fog, CanUseOptimizedFogCodeGLES(srcVS)))
{
// create program, shaders, link
GLES_CHK(m_Programs[index] = glCreateProgram());
ShaderErrors errors;
if(CompileProgram(index, srcVS, srcPS, channels))
{
FillParams (m_Programs[index], outParams, NULL);
m_FogParamsIndex[index] = glGetUniformLocation(m_Programs[index], "_unity_FogParams");
m_FogColorIndex[index] = glGetUniformLocation(m_Programs[index], "_unity_FogColor");
m_UniformCache[index].Create(&outParams, m_FogParamsIndex[index], m_FogColorIndex[index]);
}
else
{
if(m_GLSLVertexShader[index])
glDeleteShader(m_GLSLVertexShader[index]);
if(m_GLSLFragmentShader[index])
glDeleteShader(m_GLSLFragmentShader[index]);
m_GLSLVertexShader[index] = 0;
m_GLSLFragmentShader[index] = 0;
glDeleteProgram(m_Programs[index]);
m_Programs[index] = 0;
m_FogFailed[index] = true;
index = 0;
}
}
else
{
m_FogFailed[index] = true;
index = 0;
}
}
}
return index;
}
// chai: 在BeforDrawCall调用
int GlslGpuProgramGLES20::ApplyGpuProgramES20 (const GpuProgramParameters& params, const UInt8 *buffer)
{
DBG_LOG_GLES20("GlslGpuProgramGLES20::ApplyGpuProgramES20()");
// m_Programs[0] == 0, is when Unity tries to build a dummy shader with empty source for fragment shaders, do nothing in this case
if (m_Programs[0] == 0)
return 0;
GfxDevice& device = GetRealGfxDevice();
const GfxFogParams& fog = device.GetFogParams();
const int index = (int)fog.mode;
DBG_LOG_GLES20("GLSL: apply program id=%i\n", m_Programs[index]);
//chai: glUseProgram
GLSLUseProgramGLES20 (m_Programs[index]);
// Apply value parameters
const GpuProgramParameters::ValueParameterArray& valueParams = params.GetValueParams();
GpuProgramParameters::ValueParameterArray::const_iterator valueParamsEnd = valueParams.end();
for( GpuProgramParameters::ValueParameterArray::const_iterator i = valueParams.begin(); i != valueParamsEnd; ++i )
{
if (i->m_RowCount == 1 && i->m_ArraySize == 1)
{
UniformCacheGLES20* cache = m_UniformCache+index;
const float * val = reinterpret_cast<const float*>(buffer);
switch (i->m_ColCount)
{
case 1: CachedUniform1(cache, i->m_Type, i->m_Index, val); break;
case 2: CachedUniform2(cache, i->m_Type, i->m_Index, val); break;
case 3: CachedUniform3(cache, i->m_Type, i->m_Index, val); break;
case 4: CachedUniform4(cache, i->m_Type, i->m_Index, val); break;
default: break;
}
#if DEBUG_GLSL_BINDINGS
;;printf_console(" vector %i dim=%i\n", i->m_Index, i->m_Dim );
#endif
buffer += 4*sizeof(float);
}
else
{
// Apply matrix parameters
DebugAssert (i->m_ArraySize == 1);
int size = *reinterpret_cast<const int*>(buffer); buffer += sizeof(int);
Assert (size == 16);
const Matrix4x4f* mat = reinterpret_cast<const Matrix4x4f*>(buffer);
if (i->m_RowCount == 3 && i->m_ColCount == 3)
{
Matrix3x3f m33 = Matrix3x3f(*mat);
GLES_CHK(glUniformMatrix3fv (i->m_Index, 1, GL_FALSE, m33.GetPtr()));
}
else
{
const float *ptr = mat->GetPtr ();
GLES_CHK(glUniformMatrix4fv (i->m_Index, 1, GL_FALSE, ptr));
}
#if DEBUG_GLSL_BINDINGS
;;printf_console(" matrix %i (%s)\n", i->m_Index, i->m_Name.GetName() );
#endif
buffer += size * sizeof(float);
}
}
// Apply textures
const GpuProgramParameters::TextureParameterList& textureParams = params.GetTextureParams();
GpuProgramParameters::TextureParameterList::const_iterator textureParamsEnd = textureParams.end();
for( GpuProgramParameters::TextureParameterList::const_iterator i = textureParams.begin(); i != textureParamsEnd; ++i )
{
const GpuProgramParameters::TextureParameter& t = *i;
const TexEnvData* texdata = reinterpret_cast<const TexEnvData*>(buffer);
#if DEBUG_GLSL_BINDINGS
;;printf_console(" sampler %i (%s) id=%i dim=%i\n", t.m_Index, t.m_Name.GetName(), tex->GetActualTextureID().m_ID, tex->GetTexDim() );
#endif
ApplyTexEnvData (t.m_Index, t.m_Index, *texdata);
buffer += sizeof(TexEnvData);
}
// Fog parameters if needed
if (index > 0)
{
if (m_FogColorIndex[fog.mode] >= 0)
CachedUniform4(m_UniformCache+index, kShaderParamFloat, m_FogColorIndex[fog.mode], fog.color.GetPtr());
Vector4f params(
fog.density * 1.2011224087f,// density / sqrt(ln(2))
fog.density * 1.4426950408f, // density / ln(2)
0.0f,
0.0f
);
if (fog.mode == kFogLinear)
{
float diff = fog.end - fog.start;
float invDiff = Abs(diff) > 0.0001f ? 1.0f/diff : 0.0f;
params[2] = -invDiff;
params[3] = fog.end * invDiff;
}
if (m_FogParamsIndex[fog.mode] >= 0)
CachedUniform4(m_UniformCache+index, kShaderParamFloat, m_FogParamsIndex[fog.mode], params.GetPtr());
}
GLESAssert();
return index;
}
// --------------------------------------------------------------------------
FixedFunctionProgramGLES20::FixedFunctionProgramGLES20(GLShaderID vertexShader, GLShaderID fragmentShader)
: m_GLSLProgram(0)
, m_GLSLVertexShader(vertexShader)
, m_GLSLFragmentShader(fragmentShader)
{
m_GLSLProgram = Create(m_GLSLVertexShader, m_GLSLFragmentShader);
}
FixedFunctionProgramGLES20::~FixedFunctionProgramGLES20 ()
{
// NOTE: do not delete vertex/fragment shaders; they can be shared between multiple programs
// and are deleted in ClearFixedFunctionPrograms
if (m_GLSLProgram != 0 ) // only delete valid programs
GLES_CHK(glDeleteProgram(m_GLSLProgram));
m_UniformCache.Destroy();
}
GLShaderID FixedFunctionProgramGLES20::Create(GLShaderID vertexShader, GLShaderID fragmentShader)
{
if( !gGraphicsCaps.gles20.hasGLSL )
return false;
GLESAssert(); // Clear any GL errors
GLuint program;
GLES_CHK(program = glCreateProgram());
for (int i = 0; i < kAttribLookupTableSize; i++)
{
if(s_GLESVertexComponents[i] < gGraphicsCaps.gles20.maxAttributes)
GLES_CHK(glBindAttribLocation(program, s_GLESVertexComponents[i], s_GLSLESAttributes[i]));
}
GLES_CHK(glAttachShader(program, m_GLSLVertexShader));
GLES_CHK(glAttachShader(program, m_GLSLFragmentShader));
//We must link only after binding the attributes
GLES_CHK(glLinkProgram(program));
int linked = 0;
GLES_CHK(glGetProgramiv(program, GL_LINK_STATUS, &linked));
if (linked == 0)
{
ParseGlslErrors(program, kErrorLinkProgram);
GLES_CHK(glDeleteProgram(program));
return 0;
}
// Figure out the uniforms
int activeUniforms;
char name[1024];
GLenum type;
int size = 0,
length = 0,
bufSize = sizeof(name);
// Fetch texture stage samplers. Bind GLSL uniform to the OpenGL texture unit
// just once, cause it never changes after that for this program; and could cause
// internal shader recompiles when changing them.
DBG_LOG_GLES20("GLSL: apply fixed-function program id=%i\n", program);
GLSLUseProgramGLES20 (program);
for (int i = 0; i < kMaxSupportedTextureUnitsGLES; i++)
{
std::string samplerName = Format("u_sampler%d", i);
GLint uniformNumber = glGetUniformLocation(program, samplerName.c_str());
if (uniformNumber != -1)
{
GLES_CHK(glUniform1i (uniformNumber, i));
DBG_GLSL_BINDINGS_GLES20(" FFsampler: %s nr=%d", samplerName.c_str(), uniformNumber);
}
}
// fetch generic params
GLES_CHK(glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &activeUniforms));
for(int i=0; i < activeUniforms; i++)
{
GLES_CHK(glGetActiveUniform(program, i, bufSize, &length, &size, &type, name));
int uniformNumber = glGetUniformLocation(program, name);
const char* glslName = GetGLSLESPropertyNameRemap(name);
const char* unityName = glslName ? glslName : name;
if(type == GL_FLOAT) {
m_Params.AddVectorParam(uniformNumber, kShaderParamFloat, 1, unityName, -1, NULL);
}
else if(type == GL_FLOAT_VEC2) {
m_Params.AddVectorParam(uniformNumber, kShaderParamFloat, 2, unityName, -1, NULL);
}
else if(type == GL_FLOAT_VEC3) {
m_Params.AddVectorParam(uniformNumber, kShaderParamFloat, 3, unityName, -1, NULL);
}
else if(type == GL_FLOAT_VEC4) {
m_Params.AddVectorParam(uniformNumber, kShaderParamFloat, 4, unityName, -1, NULL);
}
else if(type == GL_FLOAT_MAT4) {
m_Params.AddMatrixParam(uniformNumber, unityName, 4, 4, -1, NULL);
}
else if(type == GL_FLOAT_MAT3) {
m_Params.AddMatrixParam(uniformNumber, unityName, 3, 3, -1, NULL);
}
else if(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
}
else {
AssertString( "Unrecognized GLSL uniform type" );
}
DBG_GLSL_BINDINGS_GLES20(" FFuniform: %s nr=%d type=%d", unityName, uniformNumber, type);
}
m_UniformCache.Create(&m_Params, -1,-1);
GLESAssert();
return program;
}
void FixedFunctionProgramGLES20::ApplyFFGpuProgram(const BuiltinShaderParamValues& values) const
{
DBG_LOG_GLES20("FixedFunctionProgramGLES20::ApplyFFGpuProgram()");
DBG_LOG_GLES20("GLSL: apply fixed-function program id=%i\n", m_GLSLProgram);
GLSLUseProgramGLES20 (m_GLSLProgram);
// Apply float/vector parameters
const GpuProgramParameters::ValueParameterArray& valueParams = m_Params.GetValueParams();
GpuProgramParameters::ValueParameterArray::const_iterator valueParamsEnd = valueParams.end();
for( GpuProgramParameters::ValueParameterArray::const_iterator i = valueParams.begin(); i != valueParamsEnd; ++i )
{
if(i->m_RowCount == 1 && i->m_ArraySize == 1)
{
UniformCacheGLES20* cache = &m_UniformCache;
const float * val = values.GetVectorParam((BuiltinShaderVectorParam)i->m_Name.BuiltinIndex()).GetPtr();
switch (i->m_ColCount)
{
case 1: CachedUniform1(cache, kShaderParamFloat, i->m_Index, val); break;
case 2: CachedUniform2(cache, kShaderParamFloat, i->m_Index, val); break;
case 3: CachedUniform3(cache, kShaderParamFloat, i->m_Index, val); break;
case 4: CachedUniform4(cache, kShaderParamFloat, i->m_Index, val); break;
default: break;
}
}
else
{
// Apply matrix parameters
DebugAssert (i->m_ArraySize == 1);
const Matrix4x4f& mat = values.GetMatrixParam((BuiltinShaderMatrixParam)i->m_Name.BuiltinIndex());
if (i->m_RowCount == 3 && i->m_ColCount == 3)
{
Matrix3x3f m33 = Matrix3x3f(mat);
GLES_CHK(glUniformMatrix3fv (i->m_Index, 1, GL_FALSE, m33.GetPtr()));
}
else
{
const float *ptr = mat.GetPtr ();
GLES_CHK(glUniformMatrix4fv (i->m_Index, 1, GL_FALSE, ptr));
}
DBG_GLSL_BINDINGS_GLES20(" FFmatrix %i (%s)", i->m_Index, i->m_Name.GetName() );
}
}
GLESAssert();
}
#if UNITY_ANDROID || UNITY_BLACKBERRY || UNITY_TIZEN
//-----------------------------------------------------------------------------
// md5 internals are extracted from External/MurmurHash/md5.cpp
struct
md5_context
{
unsigned long total[2];
unsigned long state[4];
unsigned char buffer[64];
unsigned char ipad[64];
unsigned char opad[64];
};
extern void md5_starts(md5_context* ctx);
extern void md5_update(md5_context* ctx, unsigned char* input, int ilen);
extern void md5_finish(md5_context* ctx, unsigned char output[16]);
#endif
static void GetCachedBinaryName(const std::string& vprog, const std::string& fshader, char filename[33])
{
// we have caching only on android, so no need to worry (at least for now) about md5
#if UNITY_ANDROID || UNITY_BLACKBERRY || UNITY_TIZEN
unsigned char hash[16] = {0};
md5_context ctx;
md5_starts(&ctx);
md5_update(&ctx, (unsigned char*)vprog.c_str(), vprog.length());
md5_update(&ctx, (unsigned char*)fshader.c_str(), fshader.length());
md5_finish(&ctx, hash);
BytesToHexString(hash, 16, filename);
#else
(void)vprog;
(void)fshader;
::memset(filename, '1', 33);
#endif
}
#endif // GFX_SUPPORTS_OPENGLES20
|