-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGLPShader.m
179 lines (149 loc) · 3.22 KB
/
GLPShader.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
#import "GLPShader.h"
#import "GLPShaderBase.h"
#import "NSError+GLPError.h"
@implementation GLPShader
+ (instancetype)shaderWithType:(GLenum)type
{
return [[self alloc] initWithType:type];
}
+ (instancetype)vertexShader
{
return [self shaderWithType:GL_VERTEX_SHADER];
}
+ (instancetype)fragmentShader
{
return [self shaderWithType:GL_FRAGMENT_SHADER];
}
+ (instancetype)init
{
return nil;
}
- (instancetype)initWithType:(GLenum)type_
{
self = [super init];
if(self != nil)
{
type = type_;
shader = glCreateShader(type_);
if(shader == 0)
{
return nil;
}
}
return self;
}
- (void)dealloc
{
if(shader != 0)
{
glDeleteShader(shader);
}
}
@synthesize GLType = type;
@synthesize GLShader = shader;
- (BOOL)compileSource:(NSString *)source error:(NSError **)error
{
if(source.length == 0)
{
NSLog(@"Error empty source");
return NO;
}
BOOL compiled = glpShaderCompileSource(shader, source.UTF8String);
if(!compiled)
{
NSString* infoLog = self.infoLog;
if(error != nil)
{
NSDictionary *userInfo = @{
GLPErrorSourceKey : source,
GLPErrorInfoLogKey : infoLog,
NSLocalizedDescriptionKey: infoLog,
};
*error = [NSError errorWithDomain:GLPErrorDomain code:GLPErrorShaderCompilerError userInfo:userInfo];
}
else
{
NSLog(@"%s:%d:error: %@:%@", __FUNCTION__, __LINE__, type == GL_VERTEX_SHADER ? @"Vertex" : @"Fragment", infoLog);
}
return NO;
}
return YES;
}
- (BOOL)compileContentsOfFile:(NSString *)path error:(NSError **)outError
{
NSError *error = nil;
NSString *source = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if(source == nil)
{
if(outError != nil)
{
*outError = error;
}
else
{
NSLog(@"%s:%d:error: %@", __FUNCTION__, __LINE__, error);
}
return NO;
}
return [self compileSource:source error:outError];
}
- (BOOL)compileContentsOfURL:(NSURL *)URL error:(NSError **)outError
{
NSError *error = nil;
NSString *source = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
if(source == nil)
{
if(outError != nil)
{
*outError = error;
}
else
{
NSLog(@"%s:%d:error: %@", __FUNCTION__, __LINE__, error);
}
return NO;
}
return [self compileSource:source error:outError];
}
- (BOOL)compileResource:(NSString *)resource bundle:(NSBundle *)bundle error:(NSError **)error
{
if(bundle == nil)
{
bundle = NSBundle.mainBundle;
}
NSURL *URL = [bundle URLForResource:resource withExtension:nil];
if(URL == nil)
{
// try to find a matching URL by adding an extension
NSArray *extensions = nil;
if(type == GL_VERTEX_SHADER)
{
extensions = @[ @"vsh", @"vs" ];
}
else if(type == GL_FRAGMENT_SHADER)
{
extensions = @[ @"fsh", @"fs" ];
}
for(NSString *extension in extensions)
{
URL = [bundle URLForResource:resource withExtension:extension];
if(URL != nil)
{
break;
}
}
}
return [self compileContentsOfURL:URL error:error];
}
- (BOOL)compiled
{
return glpShaderGetCompileStatus(shader);
}
- (NSString*)infoLog
{
char *infoLog = glpShaderCopyInfoLog(shader);
NSString *string = [[NSString alloc] initWithCString:infoLog encoding:NSUTF8StringEncoding];
free(infoLog), infoLog = 0;
return string;
}
@end