-
Notifications
You must be signed in to change notification settings - Fork 22
/
ZennyGLView.m
481 lines (388 loc) · 12.7 KB
/
ZennyGLView.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//
// ZennyGLView.m
// CPU Dasher
//
// Created by zenny_chen on 13-4-17.
//
//
#import "ZennyGLView.h"
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES2/gl.h>
enum
{
UNIFORM_ORTHO_MATRIX,
UNIFORM_TEXTURE_SAMPLER,
UNIFORM_WIDTH_PER_PIXEL,
NUM_UNIFORMS
};
static GLint uniforms[NUM_UNIFORMS];
// attribute index
enum
{
ATTRIB_VERTEX,
ATTRIB_TEXTURE,
NUM_ATTRIBUTES
};
@implementation ZennyGLView
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
#pragma mark - Interfaces
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
mContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if(mContext == nil)
return nil;
if(![EAGLContext setCurrentContext:mContext])
return nil;
mCurrentFrameIndex = 0;
}
return self;
}
- (void)dealloc
{
[self destroyShaders:0 fragShader:0];
[self destroyFramebuffers];
[EAGLContext setCurrentContext:nil];
if(mContext != nil)
[mContext release];
[super dealloc];
}
- (BOOL)isCapableForGLProcessing
{
GLint maxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
if(maxTextureSize < 2048)
return NO;
return YES;
}
- (void)determineShaders:(enum ZENNY_SHADER_CALC)calcForm
{
mCalcForm = calcForm;
[self loadShaders];
}
#pragma mark - Framebuffer & Draws
- (void)createFramebuffers
{
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffers(1, &mFrameBuffer);
glGenRenderbuffers(1, &mRenderBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mRenderBuffer);
CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
// Allocate color buffer backing based on the current layer size
glBindRenderbuffer(GL_RENDERBUFFER, mRenderBuffer);
[mContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
GLint backingWidth, backingHeight;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
NSLog(@"The width is: %d, height is: %d", backingWidth, backingHeight);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return;
}
if(![self validateProgram])
return;
glViewport(0, 0, backingWidth, backingHeight);
// use shader program
glUseProgram(mProgram);
static const GLfloat vertices[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f
};
memcpy(mVertexCorrds, vertices, sizeof(vertices));
GLfloat left = -1.0f, right = 1.0f, bottom = -1.0f, top = 1.0f;
if(backingWidth < backingHeight)
{
GLfloat normalizedX = (GLfloat)backingWidth / (GLfloat)backingHeight;
left = -normalizedX;
right = normalizedX;
mVertexCorrds[0] = left;
mVertexCorrds[4] = left;
mVertexCorrds[8] = right;
mVertexCorrds[12] = right;
}
else if(backingWidth > backingHeight)
{
GLfloat normalizedY = (GLfloat)backingHeight / (GLfloat)backingWidth;
bottom = -normalizedY;
top = normalizedY;
mVertexCorrds[1] = top;
mVertexCorrds[5] = bottom;
mVertexCorrds[9] = top;
mVertexCorrds[13] = bottom;
}
glVertexAttribPointer(ATTRIB_VERTEX, 4, GL_FLOAT, GL_FALSE, 0, mVertexCorrds);
glEnableVertexAttribArray(ATTRIB_VERTEX);
// Set ortho matrix. The matrix is in column major order
// column 0
mOrthoMatrix[0] = 2.0f / (right - left);
mOrthoMatrix[1] = 0.0f;
mOrthoMatrix[2] = 0.0f;
mOrthoMatrix[3] = -(right + left) / (right - left);
// column 1
mOrthoMatrix[4] = 0.0f;
mOrthoMatrix[5] = 2.0f / (top - bottom);
mOrthoMatrix[6] = 0.0f;
mOrthoMatrix[7] = -(top + bottom) / (top - bottom);
// column 2
mOrthoMatrix[8] = 0.0f;
mOrthoMatrix[9] = 0.0f;
mOrthoMatrix[10] = -1.0f;
mOrthoMatrix[11] = 0.0f;
// column 3
mOrthoMatrix[12] = 0.0f;
mOrthoMatrix[13] = 0.0f;
mOrthoMatrix[14] = 0.0f;
mOrthoMatrix[15] = 1.0f;
glUniformMatrix4fv(uniforms[UNIFORM_ORTHO_MATRIX], 1, GL_FALSE, mOrthoMatrix);
// Generate textures
glActiveTexture(GL_TEXTURE0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &mTextureName);
glBindTexture(GL_TEXTURE_2D, mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2048, 2048, 0, GL_RGBA, GL_UNSIGNED_BYTE, ipTextureData);
static const GLfloat texturePoints[] = {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, GL_FALSE, 0, texturePoints);
glEnableVertexAttribArray(ATTRIB_TEXTURE);
// The sampler is bound to TEXTURE0
glUniform1i(uniforms[UNIFORM_TEXTURE_SAMPLER], 0);
if(mCalcForm == ZENNY_SHADER_CALC_CONVOLUTION_KERNEL3X3)
glUniform1f(uniforms[UNIFORM_WIDTH_PER_PIXEL], 1.0f / 2048.0f);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
}
- (void)destroyFramebuffers
{
if(mTextureName != 0)
{
glDeleteTextures(1, &mTextureName);
mTextureName = 0;
}
if(mFrameBuffer != 0)
{
glDeleteFramebuffers(1, &mFrameBuffer);
mFrameBuffer = 0;
}
if(mRenderBuffer != 0)
{
glDeleteRenderbuffers(1, &mRenderBuffer);
mRenderBuffer = 0;
}
}
- (void)startAnimate
{
mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
mDisplayLink.frameInterval = 6;
[mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)drawView
{
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBuffer);
mBeginTimes[mCurrentFrameIndex] = [[NSProcessInfo processInfo] systemUptime];
// Draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFinish();
mEndTimes[mCurrentFrameIndex] = [[NSProcessInfo processInfo] systemUptime];
if(++mCurrentFrameIndex == 5)
[self statsComplete];
glBindRenderbuffer(GL_RENDERBUFFER, mRenderBuffer);
[mContext presentRenderbuffer:GL_RENDERBUFFER];
}
- (void)statsComplete
{
NSTimeInterval deltaTimes[5];
deltaTimes[0] = mEndTimes[0] - mBeginTimes[0];
NSTimeInterval minTime = deltaTimes[0];
NSTimeInterval maxTime = minTime;
NSTimeInterval sumTime = minTime;
for(int i = 1; i < 5; i++)
{
deltaTimes[i] = mEndTimes[i] - mBeginTimes[i];
if(minTime > deltaTimes[i])
minTime = deltaTimes[i];
if(maxTime < deltaTimes[i])
maxTime = deltaTimes[i];
sumTime += deltaTimes[i];
}
iMinTime = minTime * 1000.0;
iMaxTime = maxTime * 1000.0;
iAverageTime = sumTime * 1000.0 / 5.0;
if(mDisplayLink != nil)
{
[mDisplayLink invalidate];
mDisplayLink = nil;
}
if(iTargetObject != nil && [iTargetObject respondsToSelector:iCompleteSelector])
[iTargetObject performSelector:iCompleteSelector];
}
- (void)layoutSubviews
{
[self destroyFramebuffers];
[self createFramebuffers];
[self drawView];
}
#pragma mark - load shaders
- (BOOL)loadShaders
{
GLuint vertShader, fragShader;
NSString *vertShaderPathname, *fragShaderPathname;
// create and compile vertex shader
vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"zennyShader" ofType:@"vsh"];
vertShader = [self compileShader:vertShaderPathname type:GL_VERTEX_SHADER];
if(vertShader == 0)
return NO;
// create and compile fragment shader
fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"zennyShader" ofType:@"fsh"];
if(mCalcForm == ZENNY_SHADER_CALC_CONVOLUTION_KERNEL3X3)
fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"kernelconv" ofType:@"fsh"];
fragShader = [self compileShader:fragShaderPathname type:GL_FRAGMENT_SHADER];
if(fragShader == 0)
return NO;
// create shader program
mProgram = glCreateProgram();
// attach vertex shader to program
glAttachShader(mProgram, vertShader);
// attach fragment shader to program
glAttachShader(mProgram, fragShader);
// bind attribute locations
// this needs to be done prior to linking
glBindAttribLocation(mProgram, ATTRIB_VERTEX, "position");
glBindAttribLocation(mProgram, ATTRIB_TEXTURE, "texturePoints");
// link program
if (![self linkProgram]) {
[self destroyShaders:vertShader fragShader:fragShader];
return NO;
}
// get uniform locations
uniforms[UNIFORM_TEXTURE_SAMPLER] = glGetUniformLocation(mProgram, "sampler");
uniforms[UNIFORM_ORTHO_MATRIX] = glGetUniformLocation(mProgram, "orthoMatrix");
if(mCalcForm == ZENNY_SHADER_CALC_CONVOLUTION_KERNEL3X3)
uniforms[UNIFORM_WIDTH_PER_PIXEL] = glGetUniformLocation(mProgram, "widthPerPixel");
// release vertex and fragment shaders
if (vertShader)
glDeleteShader(vertShader);
if (fragShader)
glDeleteShader(fragShader);
return YES;
}
- (GLuint)compileShader:(NSString*)shaderPath type:(GLenum)shaderType
{
GLint status;
const GLchar *sources;
GLuint shader;
// get source code
sources = [[NSString stringWithContentsOfFile:shaderPath encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (!sources)
{
NSLog(@"Failed to load vertex shader");
return 0;
}
shader = glCreateShader(shaderType); // create shader
glShaderSource(shader, 1, &sources, NULL); // set source code in the shader
glCompileShader(shader); // compile 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 == GL_FALSE)
{
NSLog(@"Failed to compile shader: %s", sources);
glDeleteShader(shader);
return 0;
}
return shader;
}
/* Link a program with all currently attached shaders */
- (GLint)linkProgram
{
GLint status;
glLinkProgram(mProgram);
#if defined(DEBUG)
GLint logLength;
glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(mProgram, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log);
free(log);
}
#endif
glGetProgramiv(mProgram, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
NSLog(@"Failed to link program %d", mProgram);
return status;
}
/* Validate a program (for i.e. inconsistent samplers) */
- (GLint)validateProgram
{
GLint logLength, status;
glValidateProgram(mProgram);
glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(mProgram, logLength, &logLength, log);
NSLog(@"Program validate log:\n%s", log);
free(log);
}
glGetProgramiv(mProgram, GL_VALIDATE_STATUS, &status);
if (status == GL_FALSE)
NSLog(@"Failed to validate program %d", mProgram);
return status;
}
/* delete shader resources */
- (void)destroyShaders:(GLuint)vertShader fragShader:(GLuint)fragShader
{
if (vertShader)
glDeleteShader(vertShader);
if (fragShader)
glDeleteShader(fragShader);
if (mProgram)
{
glDeleteProgram(mProgram);
mProgram = 0;
}
}
#pragma mark -
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end