-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUFDShaderView.m
280 lines (220 loc) · 9.32 KB
/
UFDShaderView.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
//
// UFDShaderView.m
// UFDShaderView
//
// Created by Ulrik Damm on 24/9/13.
// Copyright (c) 2013 Robocat. All rights reserved.
//
#import "UFDShaderView.h"
#import <OpenGLES/ES2/gl.h>
#import <QuartzCore/QuartzCore.h>
static const char * const vertexShaderSource =
"attribute vec4 attr_position; "
" "
"void main() { "
" gl_Position = attr_position; "
"} "
" ";
static GLfloat squareVertices[] = {
1, -1, 0,
1, 1, 0,
-1, -1, 0,
-1, 1, 0,
};
static GLubyte squareIndicies[] = { 0, 1, 2, 3 };
@interface UFDShaderView ()
@property (assign, nonatomic) BOOL setupComplete;
@property (strong, nonatomic) CADisplayLink *displayLink;
@property (strong, nonatomic) EAGLContext *glContext;
@property (assign, nonatomic) GLuint framebufferId;
@property (assign, nonatomic) GLuint renderbufferId;
@property (assign, nonatomic) GLuint vertexShaderId;
@property (assign, nonatomic) GLuint shaderProgramId;
@property (assign, nonatomic) GLuint vertexBufferId;
@property (assign, nonatomic) GLuint vertexIndexBufferId;
@property (assign, nonatomic) GLuint attrPositionLoc;
@property (assign, nonatomic) GLuint uniTimeLoc;
@property (assign, nonatomic) GLuint uniResolutionLoc;
@property (assign, nonatomic) GLuint uniTouchLoc;
@property (strong, nonatomic) UITouch *currentTouch;
@property (assign, nonatomic) CGPoint touchLocation;
@end
@implementation UFDShaderView
+ (Class)layerClass {
return [CAEAGLLayer class];
}
- (void)loadFragmentShader:(NSString *)shaderSource error:(out NSError **)error {
if (![self setupComplete]) {
// [self setContentScaleFactor:[[UIScreen mainScreen] scale]];
self.glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.glContext || ![EAGLContext setCurrentContext:self.glContext]) {
self.glContext = nil;
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: @"Couldn't set OpenGL context" }];
return;
}
glGenRenderbuffers(1, &_renderbufferId);
glBindRenderbuffer(GL_RENDERBUFFER, self.renderbufferId);
[self.glContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glGenFramebuffers(1, &_framebufferId);
glBindFramebuffer(GL_FRAMEBUFFER, self.framebufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, self.renderbufferId);
GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE) {
NSString *errorString = [NSString stringWithFormat:@"Incomplete framebuffer: %04x", framebufferStatus];
*error = [NSError errorWithDomain:@"dk.ufd.shaderView" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
glDeleteFramebuffers(1, &_framebufferId);
glDeleteRenderbuffers(1, &_renderbufferId);
return;
}
NSError *compilerError = nil;
self.vertexShaderId = [self shaderWithSource:vertexShaderSource shaderType:GL_VERTEX_SHADER error:&compilerError];
if (compilerError) {
NSString *errorString = [NSString stringWithFormat:@"Vertex shader compilation failed with error: '%@'", compilerError.localizedDescription];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
glDeleteFramebuffers(1, &_framebufferId);
glDeleteRenderbuffers(1, &_renderbufferId);
return;
}
glGenBuffers(1, &_vertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
glGenBuffers(1, &_vertexIndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vertexIndexBufferId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(squareIndicies), squareIndicies, GL_STATIC_DRAW);
self.setupComplete = YES;
}
const char *fragmentShaderSource = [shaderSource cStringUsingEncoding:NSUTF8StringEncoding];
NSError *compilerError;
GLuint fragmentShaderId = [self shaderWithSource:fragmentShaderSource shaderType:GL_FRAGMENT_SHADER error:&compilerError];
if (compilerError) {
NSString *errorString = [NSString stringWithFormat:@"Fragment shader compilation failed with error: '%@'", compilerError.localizedDescription];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
return;
}
NSError *linkError;
self.shaderProgramId = [self programWithVertexShader:self.vertexShaderId fragmentShader:fragmentShaderId error:&linkError];
if (linkError) {
NSString *errorString = [NSString stringWithFormat:@"Program linking failed with error: '%@'", linkError.localizedDescription];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
return;
}
glUseProgram(self.shaderProgramId);
self.attrPositionLoc = glGetAttribLocation(self.shaderProgramId, "attr_position");
self.uniTimeLoc = glGetUniformLocation(self.shaderProgramId, "time");
self.uniResolutionLoc = glGetUniformLocation(self.shaderProgramId, "resolution");
self.uniTouchLoc = glGetUniformLocation(self.shaderProgramId, "touch");
GLenum glError = glGetError();
if (glError != GL_NO_ERROR) {
NSString *errorString = [NSString stringWithFormat:@"OpenGL error: %04x", glError];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
return;
}
if ([self isAnimating]) {
[self setupDisplayLink];
} else {
[self redraw];
}
}
- (void)redraw {
[self render:nil];
}
- (void)render:(CADisplayLink *)sender {
[EAGLContext setCurrentContext:self.glContext];
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
glEnableVertexAttribArray(self.attrPositionLoc);
glUniform1f(self.uniTimeLoc, self.displayLink.timestamp);
glUniform2f(self.uniResolutionLoc, self.bounds.size.width, self.bounds.size.height);
glUniform2f(self.uniTouchLoc, self.touchLocation.x / self.bounds.size.width, 1-self.touchLocation.y / self.bounds.size.height);
glVertexAttribPointer(self.attrPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (void *)(sizeof(GLfloat) * 0));
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, 0);
glDisableVertexAttribArray(self.attrPositionLoc);
[self.glContext presentRenderbuffer:GL_RENDERBUFFER];
GLenum glError = glGetError();
if (glError != GL_NO_ERROR) {
NSString *errorString = [NSString stringWithFormat:@"OpenGL error: %04x", glError];
NSLog(@"%@", errorString);
return;
}
}
- (void)setupDisplayLink {
if (!self.displayLink) {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
[self setFramerate:self.framerate];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
}
#pragma mark - Properties
- (void)setFramerate:(UFDShaderViewFramerate)framerate {
self.displayLink.frameInterval = (self.framerate == kUFDShaderViewFramerate30FPS? 2: 1);
}
- (void)setAnimating:(BOOL)animating {
if (animating == [self isAnimating]) return;
if (animating) {
[self setupDisplayLink];
} else {
[self.displayLink invalidate];
self.displayLink = nil;
}
}
#pragma mark - Helpers
- (GLuint)shaderWithSource:(const char *)source shaderType:(GLenum)type error:(out NSError **)error {
GLuint shaderId = glCreateShader(type);
int sourceLength = strlen(source);
glShaderSource(shaderId, 1, &source, &sourceLength);
glCompileShader(shaderId);
GLint compileStatus;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus != GL_TRUE) {
GLchar buffer[512];
glGetShaderInfoLog(shaderId, sizeof(buffer), NULL, buffer);
NSString *errorString = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
glDeleteShader(shaderId);
return 0;
}
*error = nil;
return shaderId;
}
- (GLuint)programWithVertexShader:(GLuint)vertexShaderId fragmentShader:(GLuint)fragmentShaderId error:(out NSError **)error {
GLuint programId = glCreateProgram();
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glLinkProgram(programId);
GLint linkStatus;
glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLchar buffer[512];
glGetProgramInfoLog(programId, sizeof(buffer), NULL, buffer);
NSString *errorString = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
*error = [NSError errorWithDomain:@"dk.ufd.shaderview" code:0 userInfo:@{ NSLocalizedDescriptionKey: errorString }];
glDeleteProgram(programId);
return 0;
}
*error = nil;
return programId;
}
#pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.currentTouch) {
self.currentTouch = [touches anyObject];
self.touchLocation = [self.currentTouch locationInView:self];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches containsObject:self.currentTouch]) {
self.touchLocation = [self.currentTouch locationInView:self];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches containsObject:self.currentTouch]) {
self.currentTouch = nil;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches containsObject:self.currentTouch]) {
self.currentTouch = nil;
}
}
@end