-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGLPProgram+XML.m
335 lines (282 loc) · 7.37 KB
/
GLPProgram+XML.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
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
#import "GLPProgram+XML.h"
#import "GLPProgram+SetUniform.h"
#import "GLPShader.h"
#import "NSError+GLPError.h"
@interface GLPProgramXMLParser : NSXMLParser <NSXMLParserDelegate>
@property (nonatomic, assign) Class programClass;
@property (nonatomic, strong) NSDictionary* attributes;
@property (nonatomic, strong) NSMutableString* text;
@property (nonatomic, assign) NSUInteger textLineNumber;
@property (nonatomic, strong) GLPProgram* program;
@property (nonatomic, strong) NSError* error;
- (GLPProgram *)loadProgramWithClass:(Class)class error:(NSError **)error;
@end
@implementation GLPProgram (XML)
+ (instancetype)linkedProgramWithXMLFile:(NSString *)path error:(NSError **)error
{
if(path == nil)
{
return nil;
}
return [self linkedProgramWithXMLURL:[NSURL fileURLWithPath:path] error:error];
}
+ (instancetype)linkedProgramWithXMLURL:(NSURL *)URL error:(NSError** )error
{
if(URL == nil)
{
return nil;
}
GLPProgramXMLParser *parser = [[GLPProgramXMLParser alloc] initWithContentsOfURL:URL];
return [parser loadProgramWithClass:self error:error];
}
+ (instancetype)linkedProgramWithXMLResource:(NSString *)resource bundle:(NSBundle *)bundle error:(NSError **)error
{
if(bundle == nil) {
bundle = NSBundle.mainBundle;
}
NSString* file = [bundle pathForResource:resource ofType:nil];
if(!file) {
file = [bundle pathForResource:resource ofType:@"xml"];
}
return [self linkedProgramWithXMLFile:file error:error];
}
@end
@implementation GLPProgramXMLParser
- (GLPProgram *)loadProgramWithClass:(Class)class error:(NSError **)outError {
self.programClass = class;
self.delegate = self;
BOOL success = NO;
@autoreleasepool
{
success = [self parse];
}
NSError *error = self.error;
if(error != nil)
{
if(outError != nil)
{
*outError = error;
}
else
{
NSLog(@"XML error: %@", error);
}
}
return success ? self.program : nil;
}
#pragma mark - NSXMLParserDelegate
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
if(!self.program.linked)
{
NSError *error = nil;
if(![self.program link:&error])
{
self.error = error;
return;
}
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributes
{
self.attributes = attributes;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSDictionary *attributes = self.attributes;
if([elementName isEqualToString:@"shader"]) {
GLenum type = 0;
NSString* attributeType = attributes[@"type"];
if([attributeType isEqualToString:@"vertex"])
{
type = GL_VERTEX_SHADER;
}
else if([attributeType isEqualToString:@"fragment"])
{
type = GL_FRAGMENT_SHADER;
}
else
{
self.error = [NSError errorWithDomain:GLPErrorDomain code:GLPErrorInvalidShaderTypeError userInfo:nil];
[parser abortParsing];
return;
}
GLPShader *shader = [GLPShader shaderWithType:type];
NSString *attributeSource = attributes[@"src"];
if(attributeSource.length > 0)
{
NSString *path = [NSBundle.mainBundle pathForResource:attributeSource ofType:nil];
NSError *error = nil;
if(![shader compileContentsOfFile:path error:&error])
{
self.error = error;
[self abortParsing];
return;
}
}
else
{
NSString *source = [NSString stringWithFormat:@"#pragma line %ld\n%@", (unsigned long)self.textLineNumber, self.text];
NSError *error = nil;
if(![shader compileSource:source error:&error])
{
self.error = error;
[self abortParsing];
return;
}
}
if(self.program == nil)
{
self.program = [self.programClass program];
}
[self.program attachShader:shader];
}
else if([elementName isEqualToString:@"attribute"])
{
NSString* name = self.attributes[@"name"];
NSString* location = self.attributes[@"location"];
if([name isKindOfClass:NSString.class] && [location isKindOfClass:NSString.class])
{
//NSLog(@"attribute name:%@ loction:%@", name, location);
[self.program bindAttribute:name toLocation:location.intValue];
}
}
else if([elementName isEqualToString:@"uniform"])
{
if(!self.program.linked)
{
NSError *error = nil;
if(![self.program link:&error])
{
self.error = error;
[self abortParsing];
return;
}
}
NSString *name = attributes[@"name"];
NSString *type = attributes[@"type"];
NSString *value = attributes[@"value"];
NSScanner *valueScanner = [NSScanner scannerWithString:value];
if([type hasPrefix:@"int"])
{
int index = 0;
int values[4] = { 0, 0, 0, 0 };
while(index < 4 && [valueScanner scanInt:values + index])
{
++index;
}
if([type isEqualToString:@"int"])
{
[self.program setUniformNamed:name intValue:values[0]];
}
else if([type isEqualToString:@"int2"])
{
[self.program setUniformNamed:name intValue:values[0] :values[1]];
}
else if([type isEqualToString:@"int3"])
{
[self.program setUniformNamed:name intValue:values[0] :values[1] :values[2]];
}
else
{
[self.program setUniformNamed:name intValue:values[0] :values[1] :values[2] :values[3]];
}
}
else if([type isEqualToString:@"float"] || [type hasPrefix:@"vec"])
{
int index = 0;
float values[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
while(index < 4 && [valueScanner scanFloat:values + index])
{
++index;
}
if([type isEqualToString:@"float"])
{
[self.program setUniformNamed:name floatValue:values[0]];
}
else if([type isEqualToString:@"vec2"])
{
[self.program setUniformNamed:name floatValue:values[0] :values[1]];
}
else if([type isEqualToString:@"vec3"])
{
[self.program setUniformNamed:name floatValue:values[0] :values[1] :values[2]];
}
else
{
[self.program setUniformNamed:name floatValue:values[0] :values[1] :values[2] :values[3]];
}
}
else if([type isEqualToString:@"mat2"])
{
int index = 0;
float values[4] = {
1.0f, 0.0f,
0.0f, 1.0f,
};
if(![value isEqualToString:@"IDENTITY"])
{
while(index < 4 && [valueScanner scanFloat:values + index])
{
++index;
}
}
[self.program setUniformNamed:name matrix2Value:values];
}
else if([type isEqualToString:@"mat3"])
{
int index = 0;
float values[9] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
if(![value isEqualToString:@"IDENTITY"])
{
while(index < 9 && [valueScanner scanFloat:values + index])
{
++index;
}
}
[self.program setUniformNamed:name matrix3Value:values];
}
else if([type isEqualToString:@"mat4"])
{
int index = 0;
float values[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
if(![value isEqualToString:@"IDENTITY"])
{
while(index < 16 && [valueScanner scanFloat:values + index])
{
++index;
}
}
[self.program setUniformNamed:name matrix4Value:values];
}
else
{
NSLog(@"invalid uniform: name:%@ type:%@ value:%@", name, type, value);
}
}
// clean up
self.attributes = nil;
self.text = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(self.text == nil)
{
self.text = [NSMutableString stringWithString:string];
self.textLineNumber = self.lineNumber;
}
else
{
[self.text appendString:string];
}
}
@end