forked from sapphyrus/SteamworksSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcglmprogram.cpp
1439 lines (1148 loc) · 39.2 KB
/
cglmprogram.cpp
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
//============ Copyright (c) Valve Corporation, All rights reserved. ============
//
// cglmprogram.cpp
//
//===============================================================================
#include "glmgr.h"
#include "cglmprogram.h"
#include "dxabstract.h"
//===============================================================================
#if 0
ConVar gl_shaderpair_cacherows_lg2( "gl_paircache_rows_lg2", "10"); // 10 is minimum
ConVar gl_shaderpair_cacheways_lg2( "gl_paircache_ways_lg2", "5"); // 5 is minimum
ConVar gl_shaderpair_cachelog( "gl_shaderpair_cachelog", "0" );
#else
int gl_shaderpair_cacherows_lg2 = 10;
int gl_shaderpair_cacheways_lg2 = 5;
int gl_shaderpair_cachelog = 0;
#endif
//===============================================================================
GLenum GLMProgTypeToARBEnum( EGLMProgramType type )
{
GLenum result = 0;
switch(type)
{
case kGLMVertexProgram: result = GL_VERTEX_PROGRAM_ARB; break;
case kGLMFragmentProgram: result = GL_FRAGMENT_PROGRAM_ARB; break;
default: Assert( !"bad program type"); result = 0; break;
}
return result;
}
GLenum GLMProgTypeToGLSLEnum( EGLMProgramType type )
{
GLenum result = 0;
switch(type)
{
case kGLMVertexProgram: result = GL_VERTEX_SHADER_ARB; break;
case kGLMFragmentProgram: result = GL_FRAGMENT_SHADER_ARB; break;
default: Assert( !"bad program type"); result = 0; break;
}
return result;
}
static uint g_shader_serial = 1000000;
CGLMProgram::CGLMProgram( GLMContext *ctx, EGLMProgramType type )
{
m_ctx = ctx;
m_ctx->CheckCurrent();
m_type = type;
m_serial = g_shader_serial++;
m_text = NULL; // no text yet
#if GLMDEBUG
m_editable = NULL;
#endif
memset( &m_descs, 0, sizeof( m_descs ) );
m_samplerMask = 0; // dxabstract sets this field later
// create an ARB vp/fp program object name. No need to bind it yet.
GLMShaderDesc *arbDesc = &m_descs[ kGLMARB ];
glGenProgramsARB( 1, &arbDesc->m_object.arb );
// create a GLSL shader object.
GLMShaderDesc *glslDesc = &m_descs[ kGLMGLSL ];
GLenum glslStage = GLMProgTypeToGLSLEnum( m_type );
glslDesc->m_object.glsl = glCreateShaderObjectARB( glslStage );;
// no text has arrived yet. That's done in SetProgramText.
}
CGLMProgram::~CGLMProgram( )
{
m_ctx->CheckCurrent();
// if there is an arb program, delete it
GLMShaderDesc *arbDesc = &m_descs[ kGLMARB ];
if (arbDesc->m_object.arb)
{
glDeleteProgramsARB( 1, &arbDesc->m_object.arb );
GLMCheckError();
arbDesc->m_object.arb = 0;
}
// if there is a GLSL shader, delete it
GLMShaderDesc *glslDesc = &m_descs[kGLMGLSL];
if (glslDesc->m_object.glsl)
{
glDeleteShader( (uintptr_t)glslDesc->m_object.glsl ); // why do I need a cast here again ?
GLMCheckError();
glslDesc->m_object.glsl = 0;
}
#if GLMDEBUG
if (m_editable)
{
delete m_editable;
m_editable = NULL;
}
#endif
if (m_text)
{
free( m_text );
m_text = NULL;
}
m_ctx = NULL;
}
enum EShaderSection
{
kGLMARBVertex, kGLMARBVertexDisabled,
kGLMARBFragment, kGLMARBFragmentDisabled,
kGLMGLSLVertex, kGLMGLSLVertexDisabled,
kGLMGLSLFragment, kGLMGLSLFragmentDisabled,
};
const char *g_shaderSectionMarkers[] = // match ordering of enum
{
"!!ARBvp", "-!!ARBvp", // enabled and disabled markers. so you can have multiple flavors in a blob and activate the one you want.
"!!ARBfp", "-!!ARBfp",
"//GLSLvp", "-//GLSLvp",
"//GLSLfp", "-//GLSLfp",
NULL
};
void CGLMProgram::SetProgramText( char *text )
{
// free old text if any
// clone new text
// scan newtext to find sections
// walk sections, and mark descs to indicate where text is at
if (m_text)
{
free( m_text );
m_text = NULL;
}
// scrub desc text references
for( int i=0; i<kGLMNumProgramTypes; i++)
{
GLMShaderDesc *desc = &m_descs[i];
desc->m_textPresent = false;
desc->m_textOffset = 0;
desc->m_textLength = 0;
}
m_text = strdup( text );
Assert( m_text != NULL );
#if 0 // disabled in sample for now GLMDEBUG
// create editable text item, if it does not already exist
if (!m_editable)
{
char *suffix = "";
switch(m_type)
{
case kGLMVertexProgram: suffix = ".vsh"; break;
case kGLMFragmentProgram: suffix = ".fsh"; break;
default: GLMDebugger();
}
m_editable = new CGLMEditableTextItem( m_text, strlen(m_text), false, "/debugshaders/", suffix );
// pull our string back from the editable (it has probably munged it)
if (m_editable->HasData())
{
ReloadStringFromEditable();
}
}
#endif
// scan the text and find sections
CGLMTextSectioner sections( m_text, strlen( m_text ), g_shaderSectionMarkers );
int sectionCount = sections.Count();
for( int i=0; i < sectionCount; i++ )
{
uint subtextOffset = 0;
uint subtextLength = 0;
int markerIndex = 0;
sections.GetSection( i, &subtextOffset, &subtextLength, &markerIndex );
// act on the section
GLMShaderDesc *desc = NULL;
switch( m_type )
{
case kGLMVertexProgram:
switch( markerIndex )
{
case kGLMARBVertex:
case kGLMGLSLVertex:
desc = &m_descs[ (markerIndex==kGLMARBVertex) ? kGLMARB : kGLMGLSL];
// these steps are generic across both langs
desc->m_textPresent = true;
desc->m_textOffset = subtextOffset;
desc->m_textLength = subtextLength;
desc->m_compiled = false;
desc->m_valid = false;
break;
case kGLMARBVertexDisabled:
case kGLMGLSLVertexDisabled:
// ignore quietly
break;
default: Assert(!"Mismatched section marker seen in SetProgramText (VP)"); break;
}
break;
case kGLMFragmentProgram:
switch( markerIndex )
{
case kGLMARBFragment:
case kGLMGLSLFragment:
desc = &m_descs[ (markerIndex==kGLMARBFragment) ? kGLMARB : kGLMGLSL];
// these steps are generic across both langs
desc->m_textPresent = true;
desc->m_textOffset = subtextOffset;
desc->m_textLength = subtextLength;
desc->m_compiled = false;
desc->m_valid = false;
break;
case kGLMARBFragmentDisabled:
case kGLMGLSLFragmentDisabled:
// ignore quietly
break;
default: Assert(!"Mismatched section marker seen in SetProgramText (VP)"); break;
}
break;
default:
break;
}
}
}
bool CGLMProgram::CompileActiveSources ( void )
{
bool result = true; // assume success
// compile everything we have text for
for( int i=0; i<kGLMNumProgramTypes; i++)
{
if (m_descs[i].m_textPresent)
{
if (!Compile( (EGLMProgramLang)i ))
{
result = false;
}
}
}
return result;
}
bool CGLMProgram::Compile( EGLMProgramLang lang )
{
bool result = true; // indicating validity..
bool noisy = false;
int loglevel = gl_shaderpair_cachelog/* .GetInt() */;
switch( lang )
{
case kGLMARB:
{
GLMShaderDesc *arbDesc;
arbDesc = &m_descs[ kGLMARB ];
// make sure no GLSL program is set up
glUseProgram(0);
GLMCheckError();
// bind our program container to context
GLenum arbTarget = GLMProgTypeToARBEnum( m_type );
glSetEnable( arbTarget, true ); // unclear if I need this to compile or just to draw...
GLMCheckError();
glBindProgramARB( arbTarget, arbDesc->m_object.arb ); // object created or just re-bound
GLMCheckError();
char *section = m_text + arbDesc->m_textOffset;
char *lastCharOfSection = section + arbDesc->m_textLength; // actually it's one past the last textual character
#if GLMDEBUG
if(noisy)
{
GLMPRINTF((">-D- CGLMProgram::Compile submitting following text for ARB %s program (name %d) ---------------------",
arbTarget == GL_FRAGMENT_PROGRAM_ARB ? "fragment" : "vertex",
arbDesc->m_object.arb ));
// we don't have a "print this many chars" call yet
// just temporarily null terminate the text we want to print
char saveChar = *lastCharOfSection;
*lastCharOfSection= 0;
GLMPRINTTEXT(( section, eDebugDump ));
*lastCharOfSection= saveChar;
GLMPRINTF(("<-D- CGLMProgram::Compile ARB EOT--" ));
}
#endif
glProgramStringARB( arbTarget, GL_PROGRAM_FORMAT_ASCII_ARB, arbDesc->m_textLength, section );
GLMCheckError( true, false );
arbDesc->m_compiled = true; // compiled but not necessarily valid
CheckValidity( lang );
GLMCheckError();
// leave it bound n enabled, don't care (draw will sort it all out)
result = arbDesc->m_valid;
}
break;
case kGLMGLSL:
{
GLMShaderDesc *glslDesc;
glslDesc = &m_descs[ kGLMGLSL ];
GLenum glslStage = GLMProgTypeToGLSLEnum( m_type );
// there's no binding to do for GLSL. but make sure no ARB stuff is bound for tidiness.
glSetEnable( GL_VERTEX_PROGRAM_ARB, false );
glSetEnable( GL_FRAGMENT_PROGRAM_ARB, false ); // add check errors on these
glBindProgramARB( GL_VERTEX_PROGRAM_ARB, 0 );
glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, 0 );
// no GLSL program either
glUseProgram(0);
// pump text into GLSL shader object
char *section = m_text + glslDesc->m_textOffset;
char *lastCharOfSection = section + glslDesc->m_textLength; // actually it's one past the last textual character
#if GLMDEBUG
if(noisy)
{
GLMPRINTF((">-D- CGLMProgram::Compile submitting following text for GLSL %s program (name %d) ---------------------",
glslStage == GL_FRAGMENT_SHADER_ARB ? "fragment" : "vertex",
glslDesc->m_object.glsl ));
// we don't have a "print this many chars" call yet
// just temporarily null terminate the text we want to print
char saveChar = *lastCharOfSection;
*lastCharOfSection= 0;
GLMPRINTTEXT(( section, eDebugDump ));
*lastCharOfSection= saveChar;
GLMPRINTF(("<-D- CGLMProgram::Compile GLSL EOT--" ));
}
#endif
glShaderSourceARB( glslDesc->m_object.glsl, 1, (const GLchar **)§ion, &glslDesc->m_textLength);
GLMCheckError( true, false );
// compile
glCompileShaderARB( glslDesc->m_object.glsl );
glslDesc->m_compiled = true; // compiled but not necessarily valid
GLMCheckError( true, false );
CheckValidity( lang );
GLMCheckError();
if (loglevel>=2)
{
char tempname[128];
int tempindex = -1;
int tempcombo = -1;
//GetLabelIndexCombo( tempname, sizeof(tempname), &tempindex, &tempcombo );
//printf("\ncompile: - [ %s/%d/%d ] on GL name %d ", tempname, tempindex, tempcombo, glslDesc->m_object.glsl );
GetComboIndexNameString( tempname, sizeof(tempname) );
printf("\ncompile: %s on GL name %p ", tempname, glslDesc->m_object.glsl );
}
result = glslDesc->m_valid;
}
break;
default:
break;
}
return result;
}
#if GLMDEBUG
bool CGLMProgram::PollForChanges( void )
{
bool result = false;
if (m_editable)
{
result = m_editable->PollForChanges();
}
return result;
}
void CGLMProgram::ReloadStringFromEditable( void )
{
uint dataSize=0;
char *data=NULL;
m_editable->GetCurrentText( &data, &dataSize );
char *buf = (char *)malloc( dataSize+1 ); // we will NULL terminate it, since the mirror copy might not be
memcpy( buf, data, dataSize );
buf[dataSize] = 0;
SetProgramText( buf );
free( buf );
}
bool CGLMProgram::SyncWithEditable( void )
{
bool result = false;
if (m_editable->PollForChanges())
{
ReloadStringFromEditable();
CompileActiveSources();
// invalidate shader pair cache entries using this shader..
m_ctx->m_pairCache->PurgePairsWithShader( this );
result = true; // result true means "it changed"
}
return result;
}
#endif
// attributes which are general to both stages
// VP and FP:
//
// 0x88A0 PROGRAM_INSTRUCTIONS_ARB VP FP
// 0x88A1 MAX_PROGRAM_INSTRUCTIONS_ARB VP FP
// 0x88A2 PROGRAM_NATIVE_INSTRUCTIONS_ARB VP FP
// 0x88A3 MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB VP FP
//
// 0x88A4 PROGRAM_TEMPORARIES_ARB VP FP
// 0x88A5 MAX_PROGRAM_TEMPORARIES_ARB VP FP
// 0x88A6 PROGRAM_NATIVE_TEMPORARIES_ARB VP FP
// 0x88A7 MAX_PROGRAM_NATIVE_TEMPORARIES_ARB VP FP
//
// 0x88A8 PROGRAM_PARAMETERS_ARB VP FP
// 0x88A9 MAX_PROGRAM_PARAMETERS_ARB VP FP
// 0x88AA PROGRAM_NATIVE_PARAMETERS_ARB VP FP
// 0x88AB MAX_PROGRAM_NATIVE_PARAMETERS_ARB VP FP
//
// 0x88AC PROGRAM_ATTRIBS_ARB VP FP
// 0x88AD MAX_PROGRAM_ATTRIBS_ARB VP FP
// 0x88AE PROGRAM_NATIVE_ATTRIBS_ARB VP FP
// 0x88AF MAX_PROGRAM_NATIVE_ATTRIBS_ARB VP FP
//
// 0x88B4 MAX_PROGRAM_LOCAL_PARAMETERS_ARB VP FP
// 0x88B5 MAX_PROGRAM_ENV_PARAMETERS_ARB VP FP
// 0x88B6 PROGRAM_UNDER_NATIVE_LIMITS_ARB VP FP
//
// VP only:
//
// 0x88B0 PROGRAM_ADDRESS_REGISTERS_ARB VP
// 0x88B1 MAX_PROGRAM_ADDRESS_REGISTERS_ARB VP
// 0x88B2 PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB VP
// 0x88B3 MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB VP
//
// FP only:
//
// 0x8805 PROGRAM_ALU_INSTRUCTIONS_ARB FP
// 0x880B MAX_PROGRAM_ALU_INSTRUCTIONS_ARB FP
// 0x8808 PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB FP
// 0x880E MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB FP
// 0x8806 PROGRAM_TEX_INSTRUCTIONS_ARB FP
// 0x880C MAX_PROGRAM_TEX_INSTRUCTIONS_ARB FP
// 0x8809 PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB FP
// 0x880F MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB FP
// 0x8807 PROGRAM_TEX_INDIRECTIONS_ARB FP
// 0x880D MAX_PROGRAM_TEX_INDIRECTIONS_ARB FP
// 0x880A PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB FP
// 0x8810 MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB FP
struct GLMShaderLimitDesc
{
GLenum m_valueEnum;
GLenum m_limitEnum;
const char *m_debugName;
const char m_flags;
// m_flags - 0x01 for VP, 0x02 for FP, or set both if applicable to both
};
// macro to help make the table of what to check
#ifndef LMD
#define LMD( val, flags ) { GL_PROGRAM_##val##_ARB, GL_MAX_PROGRAM_##val##_ARB, #val, flags }
#else
#error you need to use a different name for this macro.
#endif
GLMShaderLimitDesc g_glmShaderLimitDescs[] =
{
// VP and FP..
LMD( INSTRUCTIONS, 3 ),
LMD( NATIVE_INSTRUCTIONS, 3 ),
LMD( NATIVE_TEMPORARIES, 3 ),
LMD( PARAMETERS, 3 ),
LMD( NATIVE_PARAMETERS, 3 ),
LMD( ATTRIBS, 3 ),
LMD( NATIVE_ATTRIBS, 3 ),
// VP only..
LMD( ADDRESS_REGISTERS, 1 ),
LMD( NATIVE_ADDRESS_REGISTERS, 1 ),
// FP only..
LMD( ALU_INSTRUCTIONS, 2 ),
LMD( NATIVE_ALU_INSTRUCTIONS, 2 ),
LMD( TEX_INSTRUCTIONS, 2 ),
LMD( NATIVE_TEX_INSTRUCTIONS, 2 ),
LMD( TEX_INDIRECTIONS, 2 ),
LMD( NATIVE_TEX_INDIRECTIONS, 2 ),
{ 0, 0, NULL, 0 }
};
#undef LMD
bool CGLMProgram::CheckValidity( EGLMProgramLang lang )
{
static const char *targnames[] = { "vertex", "fragment" };
switch(lang)
{
case kGLMARB:
{
GLMShaderDesc *arbDesc;
arbDesc = &m_descs[ kGLMARB ];
GLenum arbTarget = GLMProgTypeToARBEnum( m_type );
Assert( arbDesc->m_compiled );
arbDesc->m_valid = true; // assume success til we see otherwise
// assume program is bound. is there anything wrong with it ?
GLint isNative=0;
glGetProgramivARB( arbTarget, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &isNative );
GLMCheckError();
// If the program is over the hardware's limits, print out some information
if (isNative!=1)
{
arbDesc->m_valid = false;
// check everything we can check
char checkmask = (1<<m_type); // 1 for VP, 2 for FP
for( GLMShaderLimitDesc *desc = g_glmShaderLimitDescs; desc->m_valueEnum !=0; desc++ )
{
if ( desc->m_flags & checkmask )
{
// test it
GLint value = 0;
GLint limit = 0;
glGetProgramivARB(arbTarget, desc->m_valueEnum, &value);
GLMCheckError();
glGetProgramivARB(arbTarget, desc->m_limitEnum, &limit);
GLMCheckError();
if (value > limit)
{
GLMPRINTF(("-D- Invalid %s program: program has %d %s; limit is %d", targnames[ m_type ], value, desc->m_debugName, limit ));
}
}
}
}
// syntax error check
GLint errorLine;
glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &errorLine );
GLMCheckError();
if ( errorLine!=-1 )
{
const GLubyte* errorString = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
GLMPRINTF(( "-D- Syntax error in ARB %s program: %s",targnames[ m_type ], errorString ));
arbDesc->m_valid = false;
}
if (!arbDesc->m_valid)
{
char *temp = strdup(m_text);
temp[ arbDesc->m_textOffset + arbDesc->m_textLength ] = 0;
GLMPRINTF(("-D- ----- ARB compile failed; bad source follows -----" ));
GLMPRINTTEXT(( temp + arbDesc->m_textOffset, eDebugDump, GLMPRINTTEXT_NUMBEREDLINES ));
GLMPRINTF(("-D- -----end-----" ));
free( temp );
}
return arbDesc->m_valid;
}
break;
case kGLMGLSL:
{
GLMShaderDesc *glslDesc;
glslDesc = &m_descs[ kGLMGLSL ];
GLenum glslStage = GLMProgTypeToGLSLEnum( m_type );
Assert( glslDesc->m_compiled );
glslDesc->m_valid = true; // assume success til we see otherwise
// GLSL error check
int compiled = 0, length = 0, laux = 0;
glGetObjectParameterivARB( (GLhandleARB)glslDesc->m_object.glsl, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
glGetObjectParameterivARB( (GLhandleARB)glslDesc->m_object.glsl, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
GLcharARB *logString = (GLcharARB *)malloc(length * sizeof(GLcharARB));
glGetInfoLogARB((GLhandleARB)glslDesc->m_object.glsl, length, &laux, logString);
// we may not be able to check "native limits" stuff until link time. meh
if (!compiled)
{
glslDesc->m_valid = false;
}
if (!glslDesc->m_valid)
{
char *temp = strdup(m_text);
temp[ glslDesc->m_textOffset + glslDesc->m_textLength ] = 0;
GLMPRINTF(("-D- ----- GLSL compile failed: \n %s \n",logString ));
GLMPRINTTEXT(( temp + glslDesc->m_textOffset, eDebugDump, GLMPRINTTEXT_NUMBEREDLINES ));
GLMPRINTF(("-D- -----end-----" ));
free( temp );
}
free( logString );
return glslDesc->m_valid;
}
break;
default:
break;
}
return false;
}
void CGLMProgram::LogSlow( EGLMProgramLang lang )
{
// find the desc, see if it's marked
GLMShaderDesc *desc = &m_descs[ lang ];
if (!desc->m_slowMark)
{
// log it
printf( "\n-------------- Slow %s ( CGLMProgram @ %p, lang %s, name %d ) : \n%s \n",
m_type==kGLMVertexProgram ? "VS" : "FS",
this,
lang==kGLMGLSL ? "GLSL" : "ARB",
(int)(uintptr_t)(lang==kGLMGLSL ? (uintptr_t)desc->m_object.glsl : (uintptr_t)desc->m_object.arb),
m_text
);
}
else // complain on a decreasing basis (powers of two)
{
if ( (desc->m_slowMark & (desc->m_slowMark-1)) == 0 )
{
// short blurb
printf( "\n Slow %s ( CGLMProgram @ %p, lang %s, name %d ) (%d times)",
m_type==kGLMVertexProgram ? "VS" : "FS",
this,
lang==kGLMGLSL ? "GLSL" : "ARB",
(int)(lang==kGLMGLSL ? (uintptr_t)desc->m_object.glsl : (uintptr_t)desc->m_object.arb),
desc->m_slowMark+1
);
}
}
// mark it
desc->m_slowMark++;
}
void CGLMProgram::GetLabelIndexCombo ( char *labelOut, int labelOutMaxChars, int *indexOut, int *comboOut )
{
// find the label string
// example:
// trans#2871 label:vs-file vertexlit_and_unlit_generic_vs20 vs-index 294912 vs-combo 1234
*labelOut = 0;
*indexOut = -1;
char *lineStr = strstr( m_text, "// trans#" );
if (lineStr)
{
char temp1[1024];
int temp2,temp3;
int scratch=-1;
temp1[0] = 0;
temp2 = -1;
temp3 = -1;
if (this->m_type==kGLMVertexProgram)
{
sscanf( lineStr, "// trans#%d label:vs-file %s vs-index %d vs-combo %d", &scratch, temp1, &temp2, &temp3 );
}
else
{
sscanf( lineStr, "// trans#%d label:ps-file %s ps-index %d ps-combo %d", &scratch, temp1, &temp2, &temp3 );
}
if ( (strlen(temp1)!=0) )
{
strncpy( labelOut, temp1, labelOutMaxChars );
*indexOut = temp2;
*comboOut = temp3;
}
}
}
void CGLMProgram::GetComboIndexNameString ( char *stringOut, int stringOutMaxChars ) // mmmmmmmm-nnnnnnnn-filename
{
// find the label string
// example:
// trans#2871 label:vs-file vertexlit_and_unlit_generic_vs20 vs-index 294912 vs-combo 1234
*stringOut = 0;
char *lineStr = strstr( m_text, "// trans#" );
if (lineStr)
{
char temp1[1024];
int temp2,temp3;
int scratch=-1;
temp1[0] = 0;
temp2 = -1;
temp3 = -1;
if (this->m_type==kGLMVertexProgram)
{
sscanf( lineStr, "// trans#%d label:vs-file %s vs-index %d vs-combo %d", &scratch, temp1, &temp2, &temp3 );
}
else
{
sscanf( lineStr, "// trans#%d label:ps-file %s ps-index %d ps-combo %d", &scratch, temp1, &temp2, &temp3 );
}
int len = strlen(temp1);
if ( (len+20) < stringOutMaxChars )
{
// output formatted version
sprintf( stringOut, "%08X-%08X-%s", temp3, temp2, temp1 );
}
}
}
//===============================================================================
CGLMShaderPair::CGLMShaderPair( GLMContext *ctx )
{
m_ctx = ctx;
m_ctx->MakeCurrent();
m_vertexProg = m_fragmentProg = NULL;
m_program = glCreateProgramObjectARB();
GLMCheckError();
m_locVertexParams = -1;
m_locVertexInteger0 = -1; // "i0"
m_locVertexBool0 = -1; // "b0"
m_locVertexBool1 = -1; // "b1"
m_locVertexBool2 = -1; // "b2"
m_locVertexBool3 = -1; // "b3"
m_locFragmentParams = -1;
m_locFragmentFakeSRGBEnable = -1;
m_fakeSRGBEnableValue = -1.0f;
memset( m_locSamplers, 0xFF, sizeof( m_locSamplers ) );
m_valid = false;
m_samplersFixed = false; // fix them at draw time, and only do it once.
m_revision = 0; // bumps to 1 once linked
}
CGLMShaderPair::~CGLMShaderPair( )
{
if (m_program)
{
glDeleteObjectARB( (GLhandleARB)m_program );
m_program = 0;
}
}
bool CGLMShaderPair::SetProgramPair ( CGLMProgram *vp, CGLMProgram *fp )
{
m_valid = false; // assume failure
// true result means successful link and query
bool vpgood = (vp!=NULL) && (vp->m_descs[ kGLMGLSL ].m_valid);
bool fpgood = (fp!=NULL) && (fp->m_descs[ kGLMGLSL ].m_valid);
if (!fpgood)
{
// fragment side allowed to be "null".
fp = m_ctx->m_nullFragmentProgram;
}
if (vpgood && fpgood)
{
// attempt link. but first, detach any previously attached programs
if (m_vertexProg)
{
glDetachObjectARB(m_program, m_vertexProg->m_descs[kGLMGLSL].m_object.glsl);
GLMCheckError();
m_vertexProg = NULL;
}
if (m_fragmentProg)
{
glDetachObjectARB(m_program, m_fragmentProg->m_descs[kGLMGLSL].m_object.glsl);
GLMCheckError();
m_fragmentProg = NULL;
}
// now attach
glAttachObjectARB(m_program, vp->m_descs[kGLMGLSL].m_object.glsl);
m_vertexProg = vp;
GLMCheckError();
glAttachObjectARB(m_program, fp->m_descs[kGLMGLSL].m_object.glsl);
m_fragmentProg = fp;
GLMCheckError();
// force the locations for input attributes v0-vN to be at locations 0-N
// use the vertex attrib map to know which slots are live or not... oy! we don't have that map yet... but it's OK.
// fallback - just force v0-v15 to land in locations 0-15 as a standard.
if (vp->m_descs[kGLMGLSL].m_valid)
{
for( int i=0; i < 16; i++)
{
char tmp[16];
sprintf(tmp, "v%d", i); // v0 v1 v2 ... et al
glBindAttribLocationARB( m_program, i, tmp );
GLMCheckError();
}
}
// now link
glLinkProgramARB( m_program );
GLMCheckError();
// check for success
GLint result = 0;
glGetObjectParameterivARB(m_program,GL_OBJECT_LINK_STATUS_ARB,&result); // want GL_TRUE
if (result == GL_TRUE)
{
// success
m_valid = true;
m_revision++;
}
else
{
GLint length = 0;
GLint laux = 0;
// do some digging
glGetObjectParameterivARB(m_program,GL_OBJECT_INFO_LOG_LENGTH_ARB,&length);
GLcharARB *logString = (GLcharARB *)malloc(length * sizeof(GLcharARB));
glGetInfoLogARB(m_program, length, &laux, logString);
char *vtemp = strdup(vp->m_text);
vtemp[ vp->m_descs[kGLMGLSL].m_textOffset + vp->m_descs[kGLMGLSL].m_textLength ] = 0;
char *ftemp = strdup(fp->m_text);
ftemp[ fp->m_descs[kGLMGLSL].m_textOffset + fp->m_descs[kGLMGLSL].m_textLength ] = 0;
GLMPRINTF(("-D- ----- GLSL link failed: \n %s ",logString ));
GLMPRINTF(("-D- ----- GLSL vertex program selected: %08x (handle %08x)", vp, vp->m_descs[kGLMGLSL].m_object.glsl ));
GLMPRINTTEXT(( vtemp + vp->m_descs[kGLMGLSL].m_textOffset, eDebugDump, GLMPRINTTEXT_NUMBEREDLINES ));
GLMPRINTF(("-D- ----- GLSL fragment program selected: %08x (handle %08x)", fp, vp->m_descs[kGLMGLSL].m_object.glsl ));
GLMPRINTTEXT(( ftemp + fp->m_descs[kGLMGLSL].m_textOffset, eDebugDump, GLMPRINTTEXT_NUMBEREDLINES ));
GLMPRINTF(("-D- -----end-----" ));
free( ftemp );
free( vtemp );
free( logString );
}
}
else
{
// fail
Assert(!"Can't link these programs");
}
if (m_valid)
{
m_locVertexParams = glGetUniformLocationARB( m_program, "vc");
GLMCheckError();
m_locVertexInteger0 = glGetUniformLocationARB( m_program, "i0");
GLMCheckError();
m_locVertexBool0 = glGetUniformLocationARB( m_program, "b0");
GLMCheckError();
m_locVertexBool1 = glGetUniformLocationARB( m_program, "b1");
GLMCheckError();
m_locVertexBool2 = glGetUniformLocationARB( m_program, "b2");
GLMCheckError();
m_locVertexBool3 = glGetUniformLocationARB( m_program, "b3");
GLMCheckError();
m_locFragmentParams = glGetUniformLocationARB( m_program, "pc");
GLMCheckError();
m_locFragmentFakeSRGBEnable = glGetUniformLocationARB( m_program, "flSRGBWrite");
GLMCheckError();
m_fakeSRGBEnableValue = -1.0f;
for( int sampler=0; sampler<16; sampler++)
{
char tmp[16];
sprintf(tmp, "sampler%d", sampler); // sampler0 .. sampler1.. etc
m_locSamplers[sampler] = glGetUniformLocationARB( m_program, tmp );
GLMCheckError();
}
}
else
{
m_locVertexParams = -1;
m_locVertexInteger0 = -1;
m_locVertexBool0 = -1;
m_locVertexBool1 = -1;
m_locVertexBool2 = -1;
m_locVertexBool3 = -1;
m_locFragmentParams = -1;
m_locFragmentFakeSRGBEnable = -1;
m_fakeSRGBEnableValue = -999;
memset( m_locSamplers, 0xFF, sizeof( m_locSamplers ) );
m_revision = 0;
}
return m_valid;
}