Repository navigation
Expand file tree
/
Copy pathShader.cpp
More file actions
1147 lines (968 loc) · 30.7 KB
/
Copy pathShader.cpp
File metadata and controls
1147 lines (968 loc) · 30.7 KB
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) 2007-2013, Image Engine Design Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of Image Engine Design nor the names of any
// other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////
#include "IECoreGL/Shader.h"
#include "IECoreGL/Buffer.h"
#include "IECoreGL/CachedConverter.h"
#include "IECoreGL/Exception.h"
#include "IECoreGL/NumericTraits.h"
#include "IECoreGL/Selector.h"
#include "IECoreGL/Texture.h"
#include "IECoreGL/UniformFunctions.h"
#include "IECore/DespatchTypedData.h"
#include "IECore/MessageHandler.h"
#include "IECore/SimpleTypedData.h"
#include "IECore/TypeTraits.h"
#include "boost/algorithm/string/predicate.hpp"
#include "boost/container/flat_map.hpp"
#include "boost/tokenizer.hpp"
#include <iostream>
#include <vector>
using namespace std;
using namespace boost;
using namespace IECore;
using namespace IECoreGL;
//////////////////////////////////////////////////////////////////////////
// Shader::Implementation
//////////////////////////////////////////////////////////////////////////
class Shader::Implementation : public IECore::RefCounted
{
public :
Implementation( const std::string &vertexSource, const std::string &geometrySource, const std::string &fragmentSource )
: m_vertexSource( vertexSource ), m_geometrySource( geometrySource ), m_fragmentSource( fragmentSource ),
m_vertexShader( 0 ), m_geometryShader( 0 ), m_fragmentShader( 0 ), m_program( 0 ),
m_csParameter( nullptr )
{
string actualVertexSource = vertexSource;
string actualFragmentSource = fragmentSource;
if( vertexSource == "" )
{
actualVertexSource = defaultVertexSource();
}
if( fragmentSource == "" )
{
actualFragmentSource = defaultFragmentSource();
}
compile( actualVertexSource, GL_VERTEX_SHADER, m_vertexShader );
compile( geometrySource, GL_GEOMETRY_SHADER, m_geometryShader );
compile( actualFragmentSource, GL_FRAGMENT_SHADER, m_fragmentShader );
m_program = glCreateProgram();
glAttachShader( m_program, m_vertexShader );
if( m_geometryShader )
{
glAttachShader( m_program, m_geometryShader );
}
glAttachShader( m_program, m_fragmentShader );
glLinkProgram( m_program );
GLint linkStatus = 0;
glGetProgramiv( m_program, GL_LINK_STATUS, &linkStatus );
GLint logLength = 0;
glGetProgramiv( m_program, GL_INFO_LOG_LENGTH, &logLength );
if( !linkStatus )
{
std::string message = "Unknown linking error.";
if( logLength )
{
vector<char> log( logLength );
glGetProgramInfoLog( m_program, logLength, nullptr, &log[0] );
message = &log[0];
}
release();
throw IECoreGL::Exception( message );
}
else if ( logLength > 1 )
{
std::string message;
vector<char> log( logLength, ' ' );
glGetProgramInfoLog( m_program, logLength, nullptr, &log[0] );
message = &log[0];
// os x spews warnings rather overzealously, so we split them
// into warnings and debug messages. the debug messages will generally
// be filtered out by the message level, but they're still there
// if someone really wants them.
std::string warning;
std::string debug;
typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
Tokenizer lines( message, boost::char_separator<char>( "\n" ) );
for( Tokenizer::iterator it = lines.begin(); it != lines.end(); ++it )
{
if( starts_with( *it, "WARNING: Output of vertex shader" ) && ends_with( *it, "not read by fragment shader" ) )
{
debug += *it + "\n";
}
else
{
warning += *it + "\n";
}
}
if( debug.size() )
{
IECore::msg( IECore::Msg::Debug, "IECoreGL::Shader", debug );
}
if( warning.size() )
{
IECore::msg( IECore::Msg::Warning, "IECoreGL::Shader", warning );
}
}
{
// build the uniform parameter description map
GLint numUniforms = 0;
glGetProgramiv( m_program, GL_ACTIVE_UNIFORMS, &numUniforms );
GLint maxUniformNameLength = 0;
glGetProgramiv( m_program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformNameLength );
vector<char> nameChars( maxUniformNameLength );
GLuint textureUnit = 0;
for( int i=0; i<numUniforms; i++ )
{
Parameter p;
glGetActiveUniform( m_program, i, maxUniformNameLength, nullptr, &p.size, &p.type, &nameChars[0] );
p.location = glGetUniformLocation( m_program, &nameChars[0] );
std::string name = &nameChars[0];
// ignore native parameters
if( 0 == name.compare( 0, 3, "gl_" ) )
{
continue;
}
if( p.size > 1 )
{
// remove the "[0]" from the end of the string
size_t bracketPos = name.rfind( "[" );
if( bracketPos != std::string::npos )
{
name = name.substr( 0, bracketPos );
}
}
if(
p.type == GL_SAMPLER_1D || p.type == GL_SAMPLER_2D || p.type == GL_SAMPLER_3D ||
p.type == GL_INT_SAMPLER_1D || p.type == GL_INT_SAMPLER_2D || p.type == GL_INT_SAMPLER_3D ||
p.type == GL_UNSIGNED_INT_SAMPLER_1D || p.type == GL_UNSIGNED_INT_SAMPLER_2D || p.type == GL_UNSIGNED_INT_SAMPLER_3D ||
p.type == GL_SAMPLER_BUFFER || p.type == GL_INT_SAMPLER_BUFFER || p.type == GL_UNSIGNED_INT_SAMPLER_BUFFER
)
{
// we assign a specific texture unit to each individual
// sampler parameter - this makes it much easier to save
// and restore state when applying nested Setups.
p.textureUnit = textureUnit++;
}
else
{
p.textureUnit = 0;
}
m_uniformParameters[name] = p;
if( name == "Cs" )
{
m_csParameter = &(m_uniformParameters[name]);
}
}
}
{
// build the vertex parameter description map
GLint numVertexs = 0;
glGetProgramiv( m_program, GL_ACTIVE_ATTRIBUTES, &numVertexs );
GLint maxVertexNameLength = 0;
glGetProgramiv( m_program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxVertexNameLength );
// some versions of the nvidia drivers are returning maxVertexNameLength==0 when
// no attributes other than built in gl_* ones are defined by the shader. we don't
// bother retrieving anything in this case, as we skip built in parameters anyway.
if( numVertexs && maxVertexNameLength )
{
vector<char> nameChars( maxVertexNameLength );
for( int i=0; i<numVertexs; i++ )
{
Parameter p;
glGetActiveAttrib( m_program, i, maxVertexNameLength, nullptr, &p.size, &p.type, &nameChars[0] );
p.location = glGetAttribLocation( m_program, &nameChars[0] );
std::string name = &nameChars[0];
// ignore native parameters
if( 0 == name.compare( 0, 3, "gl_" ) )
{
continue;
}
/// \todo implement arrays
if( p.size != 1 )
{
continue;
}
m_vertexAttributes[name] = p;
}
}
}
}
~Implementation() override
{
release();
}
GLuint program() const
{
return m_program;
}
const std::string &vertexSource() const
{
return m_vertexSource;
}
const std::string &geometrySource() const
{
return m_geometrySource;
}
const std::string &fragmentSource() const
{
return m_fragmentSource;
}
void uniformParameterNames( std::vector<std::string> &names ) const
{
for( ParameterMap::const_iterator it = m_uniformParameters.begin(); it != m_uniformParameters.end(); it++ )
{
names.push_back( it->first );
}
}
const Shader::Parameter *uniformParameter( const std::string &name ) const
{
ParameterMap::const_iterator it = m_uniformParameters.find( name );
if( it != m_uniformParameters.end() )
{
return &(it->second);
}
return nullptr;
}
void vertexAttributeNames( std::vector<std::string> &names ) const
{
for( ParameterMap::const_iterator it = m_vertexAttributes.begin(); it != m_vertexAttributes.end(); it++ )
{
names.push_back( it->first );
}
}
const Shader::Parameter *vertexAttribute( const std::string &name ) const
{
ParameterMap::const_iterator it = m_vertexAttributes.find( name );
if( it != m_vertexAttributes.end() )
{
return &(it->second);
}
return nullptr;
}
const Shader::Parameter *csParameter() const
{
return m_csParameter;
}
private :
friend class Shader::Setup;
std::string m_vertexSource;
std::string m_geometrySource;
std::string m_fragmentSource;
GLuint m_vertexShader;
GLuint m_geometryShader;
GLuint m_fragmentShader;
GLuint m_program;
/// Maps from the uniform location to the parameter details.
typedef std::map<std::string, Shader::Parameter> ParameterMap;
ParameterMap m_uniformParameters;
ParameterMap m_vertexAttributes;
const Shader::Parameter *m_csParameter;
void compile( const std::string &source, GLenum type, GLuint &shader )
{
if( source == "" )
{
return;
}
const char *s = source.c_str();
shader = glCreateShader( type );
glShaderSource( shader, 1, &s, nullptr );
glCompileShader( shader );
GLint compileStatus = 0;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compileStatus );
GLint logLength = 0;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logLength );
if( !compileStatus )
{
std::string message = "Unknown compilation error.";
if( logLength )
{
vector<char> log( logLength, ' ' );
GLsizei l;
glGetShaderInfoLog( shader, logLength, &l, &log[0] );
message = &log[0];
}
release();
throw IECoreGL::Exception( message );
}
else if ( logLength > 1 )
{
std::string message;
vector<char> log( logLength, ' ' );
GLsizei l;
glGetShaderInfoLog( shader, logLength, &l, &log[0] );
message = &log[0];
IECore::msg( IECore::Msg::Warning, "IECoreGL::Shader", message );
}
}
void release()
{
glDeleteShader( m_vertexShader );
glDeleteShader( m_geometryShader );
glDeleteShader( m_fragmentShader );
glDeleteProgram( m_program );
}
};
//////////////////////////////////////////////////////////////////////////
// Shader
//////////////////////////////////////////////////////////////////////////
IE_CORE_DEFINERUNTIMETYPED( Shader );
Shader::Shader( const std::string &vertexSource, const std::string &fragmentSource )
: m_implementation( new Implementation( vertexSource, "", fragmentSource ) )
{
}
Shader::Shader( const std::string &vertexSource, const std::string &geometrySource, const std::string &fragmentSource )
: m_implementation( new Implementation( vertexSource, geometrySource, fragmentSource ) )
{
}
Shader::~Shader()
{
}
GLuint Shader::program() const
{
return m_implementation->program();
}
const std::string &Shader::vertexSource() const
{
return m_implementation->vertexSource();
}
const std::string &Shader::geometrySource() const
{
return m_implementation->geometrySource();
}
const std::string &Shader::fragmentSource() const
{
return m_implementation->fragmentSource();
}
void Shader::uniformParameterNames( std::vector<std::string> &names ) const
{
m_implementation->uniformParameterNames( names );
}
void Shader::vertexAttributeNames( std::vector<std::string> &names ) const
{
m_implementation->vertexAttributeNames( names );
}
const Shader::Parameter *Shader::uniformParameter( const std::string &name ) const
{
return m_implementation->uniformParameter( name );
}
const Shader::Parameter *Shader::vertexAttribute( const std::string &name ) const
{
return m_implementation->vertexAttribute( name );
}
const Shader::Parameter *Shader::csParameter() const
{
return m_implementation->csParameter();
}
bool Shader::Parameter::operator == ( const Shader::Parameter &other ) const
{
return
type == other.type &&
size == other.size &&
location == other.location &&
textureUnit == other.textureUnit;
}
///////////////////////////////////////////////////////////////////////////////
// Setup implementation
///////////////////////////////////////////////////////////////////////////////
class Shader::Setup::MemberData : public IECore::RefCounted
{
public :
MemberData( ConstShaderPtr &s )
: shader( s ), hasCsValue( false )
{
}
// base class for objects which can bind a value of some sort
// to a shader, and later unbind it.
struct Value : public IECore::RefCounted
{
virtual void bind() = 0;
virtual void unbind() = 0;
};
IE_CORE_DECLAREPTR( Value );
// value class for specifying vertex attributes
struct VertexValue : public Value
{
VertexValue( GLuint attributeIndex, GLenum type, GLint size, ConstBufferPtr buffer, GLuint divisor )
: m_attributeIndex( attributeIndex ), m_type( type ), m_size( size ), m_buffer( buffer ), m_divisor( divisor )
{
}
void bind() override
{
Buffer::ScopedBinding binding( *m_buffer );
glEnableVertexAttribArrayARB( m_attributeIndex );
glVertexAttribPointerARB( m_attributeIndex, m_size, m_type, false, 0, nullptr );
glVertexAttribDivisorARB( m_attributeIndex, m_divisor );
}
void unbind() override
{
glVertexAttribDivisorARB( m_attributeIndex, 0 );
glDisableVertexAttribArrayARB( m_attributeIndex );
}
private :
GLuint m_attributeIndex;
GLenum m_type;
GLint m_size;
ConstBufferPtr m_buffer;
GLuint m_divisor;
};
// value class for specifying textures
struct TextureValue : public Value
{
TextureValue( GLuint uniformIndex, GLuint textureUnit, GLenum targetType, ConstTexturePtr texture )
: m_uniformIndex( uniformIndex ), m_textureUnit( textureUnit ), m_targetType( targetType ), m_texture( texture )
{
}
void bind() override
{
glActiveTexture( GL_TEXTURE0 + m_textureUnit );
if( m_targetType == GL_TEXTURE_1D )
{
glGetIntegerv( GL_TEXTURE_BINDING_1D, &m_previousTexture );
}
else if( m_targetType == GL_TEXTURE_2D )
{
glGetIntegerv( GL_TEXTURE_BINDING_2D, &m_previousTexture );
}
else if( m_targetType == GL_TEXTURE_3D )
{
glGetIntegerv( GL_TEXTURE_BINDING_3D, &m_previousTexture );
}
else
{
throw IECore::Exception( "Cannot bind texture in Shader::Setup with target type " + std::to_string( m_targetType ) );
}
if( m_texture )
{
glBindTexture( m_targetType, m_texture->m_texture );
}
else
{
glBindTexture( m_targetType, 0 );
}
glUniform1i( m_uniformIndex, m_textureUnit );
}
void unbind() override
{
glActiveTexture( GL_TEXTURE0 + m_textureUnit );
glBindTexture( m_targetType, m_previousTexture );
}
private :
GLuint m_uniformIndex;
GLuint m_textureUnit;
GLenum m_targetType;
ConstTexturePtr m_texture;
GLint m_previousTexture;
};
// value class for specifying uniform values
struct UniformFloatValue : public Value
{
UniformFloatValue( GLuint program, GLuint uniformIndex, unsigned char dimensions, GLsizei count, std::vector<GLfloat> &values )
: m_program( program ), m_uniformIndex( uniformIndex ), m_dimensions( dimensions ), m_count( count ), m_values( values )
{
m_previousValues.resize( m_values.size() );
}
void bind() override
{
glGetUniformfv( m_program, m_uniformIndex, &(m_previousValues[0]) );
uniformFloatFunctions()[m_dimensions]( m_uniformIndex, m_count, &(m_values[0]) );
}
void unbind() override
{
uniformFloatFunctions()[m_dimensions]( m_uniformIndex, m_count, &(m_previousValues[0]) );
}
private :
GLuint m_program;
GLuint m_uniformIndex;
unsigned char m_dimensions;
GLsizei m_count;
std::vector<GLfloat> m_values;
std::vector<GLfloat> m_previousValues;
};
// value class for specifying uniform values
struct UniformIntegerValue : public Value
{
UniformIntegerValue( GLuint program, GLuint uniformIndex, unsigned char dimensions, GLsizei count, std::vector<GLint> &values )
: m_program( program ), m_uniformIndex( uniformIndex ), m_dimensions( dimensions ), m_count( count ), m_values( values )
{
m_previousValues.resize( m_values.size() );
}
void bind() override
{
glGetUniformiv( m_program, m_uniformIndex, &(m_previousValues[0]) );
uniformIntFunctions()[m_dimensions]( m_uniformIndex, m_count, &(m_values[0]) );
}
void unbind() override
{
uniformIntFunctions()[m_dimensions]( m_uniformIndex, m_count, &(m_previousValues[0]) );
}
private :
GLuint m_program;
GLuint m_uniformIndex;
unsigned char m_dimensions;
GLsizei m_count;
std::vector<GLint> m_values;
std::vector<GLint> m_previousValues;
};
struct UniformMatrixValue : public Value
{
UniformMatrixValue( GLuint program, GLuint uniformIndex, unsigned char dimensions0, unsigned char dimensions1, GLsizei count, std::vector<GLfloat> &values )
: m_program( program ), m_uniformIndex( uniformIndex ), m_dimensions0( dimensions0 ), m_dimensions1( dimensions1 ), m_count( count ), m_values( values )
{
m_previousValues.resize( m_values.size(), 0 );
}
void bind() override
{
glGetUniformfv( m_program, m_uniformIndex, &(m_previousValues[0]) );
uniformMatrixFunctions()[m_dimensions0][m_dimensions1]( m_uniformIndex, m_count, GL_FALSE, &(m_values[0]) );
}
void unbind() override
{
uniformMatrixFunctions()[m_dimensions0][m_dimensions1]( m_uniformIndex, m_count, GL_FALSE, &(m_previousValues[0]) );
}
private :
GLuint m_program;
GLuint m_uniformIndex;
unsigned char m_dimensions0;
unsigned char m_dimensions1;
GLsizei m_count;
std::vector<GLfloat> m_values;
std::vector<GLfloat> m_previousValues;
};
ConstShaderPtr shader;
boost::container::flat_map<GLint, ValuePtr> vertexValues;
boost::container::flat_map<GLint, ValuePtr> uniformValues;
bool hasCsValue;
};
Shader::Setup::Setup( ConstShaderPtr shader )
: m_memberData( new MemberData( shader ) )
{
}
Shader::Setup::~Setup()
{
}
const Shader *Shader::Setup::shader() const
{
return m_memberData->shader.get();
}
void Shader::Setup::addUniformParameter( const std::string &name, ConstTexturePtr value )
{
const Parameter *p = m_memberData->shader->uniformParameter( name );
GLenum targetType;
if( !p )
{
// No target uniform to bind to
return;
}
else if( p->type == GL_SAMPLER_2D || p->type == GL_INT_SAMPLER_2D || p->type == GL_UNSIGNED_INT_SAMPLER_2D )
{
// This is the kind of texture that is currently officially supported by texture.
targetType = GL_TEXTURE_2D;
}
else if( p->type == GL_SAMPLER_3D || p->type == GL_INT_SAMPLER_3D || p->type == GL_UNSIGNED_INT_SAMPLER_3D )
{
// We haven't yet officially added support for 3D textures because we are currently maintaining ABI,
// but we support 3D textures in Setup.
// \todo : Add targetType to Texture, and check here that the targetType of the Texture matches
// the corresponding uniform, rather than just overriding to force a match because we don't know what
// target the texture is intended to have
targetType = GL_TEXTURE_3D;
}
else if( p->type == GL_SAMPLER_1D || p->type == GL_INT_SAMPLER_1D || p->type == GL_UNSIGNED_INT_SAMPLER_1D )
{
targetType = GL_TEXTURE_1D;
}
else
{
// Target uniform not of supported type
return;
}
m_memberData->uniformValues[ p->location ] = new MemberData::TextureValue( p->location, p->textureUnit, targetType, value );
}
template<typename Container>
struct UniformDataConverter
{
typedef bool ReturnType;
UniformDataConverter( Container &container )
: m_container( container )
{
}
template<typename T>
ReturnType operator()( typename T::ConstPtr data ) const
{
const typename T::BaseType *begin = data->baseReadable();
const typename T::BaseType *end = begin + data->baseSize();
for( ; begin != end; begin++ )
{
m_container.push_back( (typename Container::value_type)*begin );
}
return true;
}
private :
Container &m_container;
};
void Shader::Setup::addUniformParameter( const std::string &name, IECore::ConstDataPtr value )
{
const Parameter *p = m_memberData->shader->uniformParameter( name );
if( !p )
{
return;
}
if( p->type == GL_BOOL || p->type == GL_INT || p->type == GL_INT_VEC2 || p->type == GL_INT_VEC3 )
{
// integer value
vector<GLint> integers;
if( value->isInstanceOf( IECore::BoolDataTypeId ) )
{
integers.push_back( static_cast<const IECore::BoolData *>( value.get() )->readable() );
}
else
{
UniformDataConverter<vector<GLint> > converter( integers );
IECore::despatchTypedData< UniformDataConverter<vector<GLint> >, IECore::TypeTraits::IsNumericBasedTypedData, DespatchTypedDataIgnoreError>( const_cast<IECore::Data *>( value.get() ), converter );
}
if( !integers.size() )
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addUniformParameter", "Uniform parameter \"{}\" has unsuitable data type \"{}\"", name, value->typeName() );
}
else
{
unsigned char dimensions = 0;
switch( p->type )
{
case GL_BOOL :
case GL_INT :
dimensions = 1;
break;
case GL_INT_VEC2 :
dimensions = 2;
break;
case GL_INT_VEC3 :
dimensions = 3;
}
m_memberData->uniformValues[ p->location ] = new MemberData::UniformIntegerValue( m_memberData->shader->program(), p->location, dimensions, p->size, integers );
}
}
else if( p->type == GL_FLOAT || p->type == GL_FLOAT_VEC2 || p->type == GL_FLOAT_VEC3 || p->type == GL_FLOAT_VEC4 )
{
// float value
vector<GLfloat> floats;
UniformDataConverter<vector<GLfloat> > converter( floats );
IECore::despatchTypedData< UniformDataConverter<vector<GLfloat> >, IECore::TypeTraits::IsNumericBasedTypedData, DespatchTypedDataIgnoreError>( const_cast<IECore::Data *>( value.get() ), converter );
if( !floats.size() )
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addUniformParameter", "Uniform parameter \"{}\" has unsuitable data type \"{}\"", name, value->typeName() );
return;
}
else
{
unsigned char dimensions = 0;
switch( p->type )
{
case GL_FLOAT :
dimensions = 1;
break;
case GL_FLOAT_VEC2 :
dimensions = 2;
break;
case GL_FLOAT_VEC3 :
dimensions = 3;
break;
case GL_FLOAT_VEC4 :
dimensions = 4;
break;
}
m_memberData->uniformValues[ p->location ] = new MemberData::UniformFloatValue( m_memberData->shader->program(), p->location, dimensions, p->size, floats );
}
if( name == "Cs" )
{
m_memberData->hasCsValue = true;
}
}
else if( p->type == GL_FLOAT_MAT3 || p->type == GL_FLOAT_MAT4 )
{
// matrix value
unsigned char dimensions0 = 0;
unsigned char dimensions1 = 0;
switch( p->type )
{
case GL_FLOAT_MAT3 :
dimensions0 = dimensions1 = 3;
break;
case GL_FLOAT_MAT4 :
dimensions0 = dimensions1 = 4;
break;
};
vector<GLfloat> floats;
UniformDataConverter<vector<GLfloat> > converter( floats );
IECore::despatchTypedData< UniformDataConverter<vector<GLfloat> >, IECore::TypeTraits::IsNumericBasedTypedData, DespatchTypedDataIgnoreError>( const_cast<IECore::Data *>( value.get() ), converter );
if( int( floats.size() ) != dimensions0 * dimensions1 * p->size )
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addUniformParameter", "Matrix parameter \"{}\" requires {} values but value of type \"{}\" provided {}", name, (dimensions0 * dimensions1), value->typeName(), floats.size() );
return;
}
m_memberData->uniformValues[ p->location ] = new MemberData::UniformMatrixValue( m_memberData->shader->program(), p->location, dimensions0, dimensions1, p->size, floats );
}
else
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addUniformParameter", "Uniform parameter \"{}\" has unsupported OpenGL type \"{}\"", name, p->type );
}
}
void Shader::Setup::addVertexAttribute( const std::string &name, IECore::ConstDataPtr value, GLuint divisor )
{
const Parameter *p = m_memberData->shader->vertexAttribute( name );
if( !p )
{
return;
}
if( p->size > 1 )
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addVertexAttribute", "Array attribute \"{}\" is currently unsupported.", name );
}
GLenum dataGLType = glType( value.get() );
if( !dataGLType )
{
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addVertexAttribute", "Vertex attribute \"{}\" has unsuitable data type \"{}\"", name, value->typeName() );
}
GLint size = 0;
switch( p->type )
{
case GL_INT :
case GL_FLOAT :
size = 1;
break;
case GL_INT_VEC2 :
case GL_FLOAT_VEC2 :
size = 2;
break;
case GL_INT_VEC3 :
case GL_FLOAT_VEC3 :
size = 3;
break;
case GL_FLOAT_VEC4 :
size = 4;
break;
default :
IECore::msg( IECore::Msg::Warning, "Shader::Setup::addVertexAttribute", "Vertex attribute \"{}\" has unsupported OpenGL type \"{}\"", name, p->type );
return;
}
CachedConverterPtr converter = CachedConverter::defaultCachedConverter();
ConstBufferPtr buffer = IECore::runTimeCast<const Buffer>( converter->convert( value.get() ) );
m_memberData->vertexValues[ p->location ] = new MemberData::VertexValue( p->location, dataGLType, size, buffer, divisor );
}
bool Shader::Setup::hasCsValue() const
{
return m_memberData->hasCsValue;
}
Shader::Setup::ScopedBinding::ScopedBinding( const Setup &setup )
: m_previousProgram( 0 ), m_setup( setup )
{
glGetIntegerv( GL_CURRENT_PROGRAM, &m_previousProgram );
glUseProgram( m_setup.shader()->m_implementation->m_program );
for( const auto &keyValue : m_setup.m_memberData->uniformValues )
{
keyValue.second->bind();
}
for( const auto &keyValue : m_setup.m_memberData->vertexValues )
{
keyValue.second->bind();
}
if( Selector *currentSelector = Selector::currentSelector() )
{
if( currentSelector->mode() == Selector::IDRender )
{
currentSelector->pushIDShader( m_setup.shader() );
}
}
}
Shader::Setup::ScopedBinding::~ScopedBinding()
{
for( const auto &keyValue : m_setup.m_memberData->uniformValues )
{
keyValue.second->unbind();
}
for( const auto &keyValue : m_setup.m_memberData->vertexValues )
{
keyValue.second->unbind();
}
glUseProgram( m_previousProgram );
if( Selector *currentSelector = Selector::currentSelector() )
{
if( currentSelector->mode() == Selector::IDRender )
{
currentSelector->popIDShader();
}
}
}
///////////////////////////////////////////////////////////////////////////////
// common shader sources
///////////////////////////////////////////////////////////////////////////////
const std::string &Shader::defaultVertexSource()
{
static const string g_s = R"(
#version 120
#if __VERSION__ <= 120
#define in attribute
#define out varying
#endif
uniform vec3 Cs = vec3( 1, 1, 1 );
uniform bool vertexCsActive = false;
uniform bool vertexNActive = false;
in vec3 vertexP;
in vec3 vertexN;
in vec2 vertexuv;
in vec3 vertexCs;
in float vertexIsCurveEndPoint;
out vec3 geometryI;
out vec3 geometryP;
out vec3 geometryN;
out vec2 geometryuv;
out vec3 geometryCs;
out float geometryIsCurveEndPoint;
out vec3 fragmentI;
out vec3 fragmentP;
out vec3 fragmentN;
out vec2 fragmentuv;