-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGLShader.m
227 lines (191 loc) · 6.13 KB
/
GLShader.m
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
/*
* GLShader.h/.m - Manages a shader program.
* jamesghurley<at>gmail.com
*/
#import "GLShader.h"
@interface GLShader (Private)
- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file;
- (BOOL)linkProgram:(GLuint)prog;
- (BOOL)validateProgram:(GLuint)prog;
- (BOOL)loadShaders: (NSString*) shaderFileName;
@end
@implementation GLShader
@synthesize program;
//---------------------------------------------------------------------------
-(GLuint) getUniform: (NSString*) uniformName{
NSNumber * val = [uniforms objectForKey:uniformName];
if(val != nil){
return (GLuint) [val unsignedIntValue];
}
return -1;
}
//---------------------------------------------------------------------------
-(GLuint) getAttribute: (NSString*) attributeName{
NSNumber * val = [attributes objectForKey:attributeName];
if(val != nil){
return (GLuint) [val unsignedIntValue];
}
return -1;
}
//---------------------------------------------------------------------------
- (void) dealloc{
if (program)
{
glDeleteProgram(program);
program = 0;
}
[uniforms release];
[attributes release];
[super dealloc];
}
//---------------------------------------------------------------------------
-(id) initWithFileName: (NSString*) shaderFileName
attributes: (NSDictionary*) attribs
uniforms: (NSDictionary*) unis
{
self = [super init];
uniforms = [[NSMutableDictionary alloc] initWithDictionary:unis];
attributes = [[NSMutableDictionary alloc] initWithDictionary:attribs];
if(![self loadShaders:shaderFileName])
{
// [self release];
return nil;
}
return self;
}
//---------------------------------------------------------------------------
- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file
{
GLint status;
const GLchar *source;
source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (!source)
{
NSLog(@"Failed to load vertex shader");
return FALSE;
}
*shader = glCreateShader(type);
glShaderSource(*shader, 1, &source, NULL);
glCompileShader(*shader);
#if defined(DEBUG)
GLint logLength;
glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(*shader, logLength, &logLength, log);
NSLog(@"Shader compile log:\n%s", log);
free(log);
}
#endif
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
if (status == 0)
{
glDeleteShader(*shader);
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
- (BOOL)linkProgram:(GLuint)prog
{
GLint status;
glLinkProgram(prog);
#if defined(DEBUG)
GLint logLength;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log);
free(log);
}
#endif
glGetProgramiv(prog, GL_LINK_STATUS, &status);
if (status == 0)
return FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
- (BOOL)validateProgram:(GLuint)prog
{
GLint logLength, status;
glValidateProgram(prog);
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log);
NSLog(@"Program validate log:\n%s", log);
free(log);
}
glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
if (status == 0)
return FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
- (BOOL)loadShaders: (NSString*) shaderFileName
{
NSString *vertShaderPathname= nil, *fragShaderPathname=nil;
NSEnumerator *uni_keys = [uniforms keyEnumerator];
NSEnumerator *attrib_keys = [attributes keyEnumerator];
NSMutableDictionary *uniTemp = [NSMutableDictionary dictionary];
NSString* key;
// Create shader program
program = glCreateProgram();
// Create and compile vertex shader
NSBundle *vBundle = [NSBundle bundleWithPath:kShaderBundlePath];
vertShaderPathname = [vBundle pathForResource:shaderFileName ofType:@"vsh"];
if (![self compileShader:&vertex type:GL_VERTEX_SHADER file:vertShaderPathname])
{
NSLog(@"Failed to compile vertex shader");
return FALSE;
}
// Create and compile fragment shader
fragShaderPathname = [vBundle pathForResource:shaderFileName ofType:@"fsh"];
if (![self compileShader:&fragment type:GL_FRAGMENT_SHADER file:fragShaderPathname])
{
NSLog(@"Failed to compile fragment shader");
return FALSE;
}
glAttachShader(program, vertex);
glAttachShader(program, fragment);
while(key = [attrib_keys nextObject]){
glBindAttribLocation(program, [[attributes objectForKey:key] unsignedIntValue], [key UTF8String]);
}
// Link program
if (![self linkProgram:program])
{
NSLog(@"Failed to link program: %d", program);
if (vertex)
{
glDeleteShader(vertex);
vertex = 0;
}
if (fragment)
{
glDeleteShader(fragment);
fragment = 0;
}
if (program)
{
glDeleteProgram(program);
program = 0;
}
return FALSE;
}
while(key = [uni_keys nextObject]){
GLuint val = glGetUniformLocation(program, [key UTF8String]);
[uniTemp setObject:[NSNumber numberWithUnsignedInt:val] forKey:key];
}
// [uniforms release];
uniforms = [[NSMutableDictionary alloc] initWithDictionary:uniTemp];
if (vertex)
glDeleteShader(vertex);
if (fragment)
glDeleteShader(fragment);
return TRUE;
}
@end