-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGLPProgram.m
223 lines (171 loc) · 4.45 KB
/
GLPProgram.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
#import "GLPProgram.h"
#import "GLPShader.h"
#import "GLPProgramBase.h"
#import "NSError+GLPError.h"
@implementation GLPProgram
+ (instancetype)program
{
return [[self alloc] init];
}
- (instancetype)init
{
self = [super init];
if(self != nil)
{
program = glCreateProgram();
}
return self;
}
- (void)dealloc
{
if(program != 0)
{
glDeleteProgram(program);
}
}
@synthesize GLProgram = program;
- (void)attachShader:(GLPShader*)shader
{
[self attachGLShader:shader.GLShader];
}
- (void)attachGLShader:(GLuint)shader
{
glAttachShader(program, shader);
}
- (void)detachShader:(GLPShader*)shader
{
[self detachGLShader:shader.GLShader];
}
- (void)detachGLShader:(GLuint)shader
{
glDetachShader(program, shader);
}
- (BOOL)link:(NSError**)error
{
BOOL linked = glpProgramLink(program);
if(!linked)
{
NSString *infoLog = self.infoLog;
if(error != nil)
{
NSDictionary *userInfo = @{
GLPErrorInfoLogKey: infoLog,
NSLocalizedDescriptionKey: infoLog,
};
*error = [NSError errorWithDomain:GLPErrorDomain code:GLPErrorProgramLinkerError userInfo:userInfo];
}
else
{
NSLog(@"%@ linker error: %@", self.class, infoLog);
}
}
return linked;
}
- (BOOL)linked
{
return glpProgramGetLinkStatus(program);
}
- (NSString*)infoLog
{
char* infoLog = glpProgramCopyInfoLog(program);
NSString* string = [[NSString alloc] initWithCString:infoLog encoding:NSUTF8StringEncoding];
free(infoLog), infoLog = 0;
return string;
}
- (GLint)attributeLocation:(NSString *)name
{
return glGetAttribLocation(program, name.UTF8String);
}
- (GLint)uniformLocation:(NSString *)name
{
return glGetUniformLocation(program, name.UTF8String);
}
- (void)bindAttribute:(NSString *)name toLocation:(GLint)location
{
glBindAttribLocation(program, location, name.UTF8String);
}
- (void)bindAttributes:(NSDictionary *)attributes
{
for(NSNumber* location in attributes)
{
NSString *attribute = attributes[location];
[self bindAttribute:attribute toLocation:location.intValue];
}
}
- (void)use
{
glUseProgram(program);
}
- (NSString *)description
{
NSMutableString* description = [NSMutableString string];
[description appendFormat:@"<%@: %p", [self class], (void*)self];
#if 0
// attributes
{
GLuint count = 0;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, (GLint*)&count);
if(count > 0) {
[description appendFormat:@"\nattributes = {\n"];
for(GLuint index = 0; index < count; ++index) {
GLsizei nameLength = 0;
GLint size = 0;
GLenum type = 0;
GLchar name[96];
glGetActiveAttrib(program, index, sizeof(name), &nameLength, &size, &type, name);
GLint location = glGetAttribLocation(program, name);
[description appendFormat:@" %d %s %d %s\n", location, name, size, glpTypeName(type)];
}
[description appendFormat:@"}"];
}
}
// uniforms
{
GLuint count = 0;
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, (GLint*)&count);
if(count > 0) {
[description appendFormat:@"\nuniforms = {\n"];
for(GLuint index = 0; index < count; ++index) {
GLsizei nameLength = 0;
GLint size = 0;
GLenum type = 0;
GLchar name[96];
glGetActiveUniform(program, index, sizeof(name), &nameLength, &size, &type, name);
GLint location = glGetUniformLocation(program, name);
[description appendFormat:@" %d %s %d %s", location, name, size, glpTypeName(type)];
GLuint components = glpTypeComponents(type);
if(components * size <= 16) {
[description appendFormat:@" = "];
GLboolean isFloat = glpTypeIsFloat(type);
if(isFloat) {
float values[16];
glGetUniformfv(program, location, values);
for(unsigned int s = 0; s < size; ++s) {
[description appendFormat:@"{ "];
for(unsigned int c = 0; c < components; ++c) {
[description appendFormat:@"%.3f ", values[s * components + c]];
}
[description appendFormat:@"} "];
}
} else {
int values[16];
glGetUniformiv(program, location, values);
for(unsigned int s = 0; s < size; ++s) {
[description appendFormat:@"{ "];
for(unsigned int c = 0; c < components; ++c) {
[description appendFormat:@"%i ", values[s * components + c]];
}
[description appendFormat:@"} "];
}
}
}
[description appendFormat:@"\n"];
}
[description appendFormat:@"}\n"];
}
}
#endif
[description appendFormat:@">"];
return description;
}
@end