-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathshader_program.cpp
More file actions
178 lines (143 loc) · 5.13 KB
/
Copy pathshader_program.cpp
File metadata and controls
178 lines (143 loc) · 5.13 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
#include "util/shader_program.hpp"
#include "util/errors.hpp"
#include <boost/bind.hpp>
namespace cvisual {
shader_program::shader_program( const std::string& source )
: source(source), program(-1)
{
}
shader_program::~shader_program() {
if (program > 0)
on_gl_free.free( boost::bind( &shader_program::gl_free, glDeleteObjectARB, program ) );
}
int shader_program::get_uniform_location( const view& v, const char* name ) {
// TODO: change interface to cache the uniforms we actually want and avoid string comparisons
if (program <= 0 || !v.glext.ARB_shader_objects) return -1;
int& cache = uniforms[ name ];
if (cache == 0)
cache = 2 + v.glext.glGetUniformLocationARB( program, name );
return cache - 2;
}
void shader_program::set_uniform_matrix( const view& v, int loc, const tmatrix& in ) {
float matrix[16];
const double* in_p = in.matrix_addr();
for(int i=0; i<16; i++)
matrix[i] = (float)in_p[i];
v.glext.glUniformMatrix4fvARB( loc, 1, false, matrix );
}
void shader_program::realize( const view& v ) {
if (program != -1) return;
if ( !v.enable_shaders ) return;
if ( !v.glext.ARB_shader_objects )
return;
program = v.glext.glCreateProgramObjectARB();
check_gl_error();
compile( v, GL_VERTEX_SHADER_ARB, getSection("varying")+getSection("vertex") );
compile( v, GL_FRAGMENT_SHADER_ARB, getSection("varying")+getSection("fragment") );
v.glext.glLinkProgramARB( program );
// Check if linking succeeded
GLint link_ok = 0;
v.glext.glGetObjectParameterivARB( program, GL_OBJECT_LINK_STATUS_ARB, &link_ok );
if ( !link_ok ) {
// Some drivers (incorrectly?) set the GL error in glLinkProgramARB() in this situation
printf("!linkok\n");
clear_gl_error();
std::string infoLog;
GLint length = 0;
v.glext.glGetObjectParameterivARB( program, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
boost::scoped_array<char> temp( new char[length+2] );
v.glext.glGetInfoLogARB( program, length+1, &length, &temp[0] );
infoLog.append( &temp[0], length );
// TODO: A way to report infoLog to the program?
write_stderr( "VPython WARNING: errors in shader program:\n" + infoLog + "\n");
// Get rid of the program, since it can't be used without generating GL errors. We set
// program to 0 instead of -1 so that binding it will revert to the fixed function pipeline,
// and realize() won't be called again.
v.glext.glDeleteObjectARB( program );
program = 0;
return;
}
check_gl_error();
#ifdef __APPLE__
v.glext.glUseProgramObjectARB( program );
GLint gpuVertexProcessing=0; // OS X 10.4 wants a long
CGLGetParameter(CGLGetCurrentContext(), kCGLCPGPUVertexProcessing, &gpuVertexProcessing);
v.glext.glUseProgramObjectARB( 0 );
// gpuVertexProcessing=1 on MacBook Pro (GeForce); gpuVertexProcessing=0 on MacBook (no graphics)
if (!gpuVertexProcessing) {
write_stderr("Shader would be emulated in software; disabling.\n");
v.glext.glDeleteObjectARB( program );
program = 0;
return;
}
#endif
// TODO: It's probably not technically legal to call glext functions from on_gl_free callbacks,
// since they might run in a different context, even though the program _handle_ is shared. Plus
// this is kind of ugly.
glDeleteObjectARB = v.glext.glDeleteObjectARB;
on_gl_free.connect( boost::bind( &shader_program::gl_free, v.glext.glDeleteObjectARB, program ) );
}
void shader_program::compile( const view& v, int type, const std::string& source ) {
int shader = v.glext.glCreateShaderObjectARB( type );
const char* str = source.c_str();
GLint len = source.size();
v.glext.glShaderSourceARB( shader, 1, &str, &len );
v.glext.glCompileShaderARB( shader );
v.glext.glAttachObjectARB( program, shader );
v.glext.glDeleteObjectARB( shader );
}
std::string shader_program::getSection( const std::string& name ) {
/* Extract section beginning with \n[name]\n and ending with \n[
e.g.
[vertex]
void main() {}
[fragment]
void main() {}
*/
std::string section;
std::string header = "\n[" + name + "]\n";
std::string source = "\n" + this->source;
int p = 0;
while ( (p = source.find( header, p )) != source.npos ) {
p += header.size();
int end = source.find( "\n[", p );
if (end == source.npos) end = source.size();
section += source.substr( p, end-p );
p = end;
}
return section;
}
void
shader_program::gl_free( PFNGLDELETEOBJECTARBPROC glDeleteObjectARB, int program )
{
glDeleteObjectARB(program);
}
use_shader_program::use_shader_program( const view& v, shader_program& program )
: v(v)
{
init(&program);
}
use_shader_program::use_shader_program( const view& v, shader_program* program )
: v(v)
{
init(program);
}
void use_shader_program::init(shader_program* program) {
m_ok = false;
if (!program || !v.glext.ARB_shader_objects || !v.enable_shaders) {
oldProgram = -1;
return;
}
program->realize(v);
// For now, nested shader invocations aren't supported.
//oldProgram = v.glext.glGetHandleARB( GL_PROGRAM_OBJECT_ARB );
oldProgram = 0;
v.glext.glUseProgramObjectARB( program->program );
check_gl_error();
m_ok = (program->program != 0);
}
use_shader_program::~use_shader_program() {
if (oldProgram<0 || !v.glext.ARB_shader_objects) return;
v.glext.glUseProgramObjectARB( oldProgram );
}
} // namespace cvisual