-
Notifications
You must be signed in to change notification settings - Fork 1
/
v002CoreVideoOpenGLGlitchPlugIn.m
289 lines (226 loc) · 8.11 KB
/
v002CoreVideoOpenGLGlitchPlugIn.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
//
// v002CoreVideoGlitchPlugIn.m
// v002CoreVideoGlitch
//
// Created by vade on 8/18/08.
// Copyright (c) 2008 __MyCompanyName__. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "v002CoreVideoOpenGLGlitchPlugIn.h"
#define kQCPlugIn_Name @"v002 Glitch Core Video OpenGL"
#define kQCPlugIn_Description @"Core Video OpenGL Buffer/Texture glitch exploits some Core Video functions to create some dynamic glitch from pools of PBuffers"
static void _TextureReleaseCallback(CGLContextObj cgl_ctx, GLuint name, void* info)
{
// this is a noop, since we handle releasing/flushing cache in the main execute loop
//CVOpenGLTextureRelease((CVOpenGLTextureRef) info);
}
@implementation v002CoreVideoOpenGLGlitchPlugIn
@dynamic outputImage, inputFlushDelay, inputWidth, inputHeight;
/*
Here you need to declare the input / output properties as dynamic as Quartz Composer will handle their implementation
@dynamic inputFoo, outputBar;
*/
+ (NSDictionary*) attributes
{
/*
Return a dictionary of attributes describing the plug-in (QCPlugInAttributeNameKey, QCPlugInAttributeDescriptionKey...).
*/
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
/*
Specify the optional attributes for property based ports (QCPortAttributeNameKey, QCPortAttributeDefaultValueKey...).
*/
if([key isEqualToString:@"inputFlushDelay"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Flush Delay", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"inputWidth"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Width", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"inputHeight"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Height", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
}
if([key isEqualToString:@"outputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
return nil;
}
+ (NSArray*) sortedPropertyPortKeys
{
return [NSArray arrayWithObjects:@"inputFlushDelay", @"inputWidth", @"inputHeight", nil];
}
+ (QCPlugInExecutionMode) executionMode
{
/*
Return the execution mode of the plug-in: kQCPlugInExecutionModeProvider, kQCPlugInExecutionModeProcessor, or kQCPlugInExecutionModeConsumer.
*/
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
/*
Return the time dependency mode of the plug-in: kQCPlugInTimeModeNone, kQCPlugInTimeModeIdle or kQCPlugInTimeModeTimeBase.
*/
return kQCPlugInTimeModeIdle;
}
- (id) init
{
self = [super init];
if(self)
{
/*
Allocate any permanent resource required by the plug-in.
*/
}
return self;
}
- (void) finalize
{
/*
Release any non garbage collected resources created in -init.
*/
[super finalize];
}
- (void) dealloc
{
/*
Release any resources created in -init.
*/
// [cvGLContext release];
// [cvPixelFormat release];
[super dealloc];
}
@end
@implementation v002CoreVideoOpenGLGlitchPlugIn (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when rendering of the composition starts: perform any required setup for the plug-in.
Return NO in case of fatal failure (this will prevent rendering of the composition to start).
*/
// et our parent contexts pixel format
cvPixelFormat = CGLGetPixelFormat([context CGLContextObj]);
CGLCreateContext(cvPixelFormat, [context CGLContextObj], &cvGLContext);
[self createCVResources];
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when the plug-in instance starts being used by Quartz Composer.
*/
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
/*
Called by Quartz Composer whenever the plug-in instance needs to execute.
Only read from the plug-in inputs and produce a result (by writing to the plug-in outputs or rendering to the destination OpenGL context) within that method and nowhere else.
Return NO in case of failure during the execution (this will prevent rendering of the current frame to complete).
The OpenGL context for rendering can be accessed and defined for CGL macros using:
*/
// bind to core video context...
CGLContextObj cgl_ctx = cvGLContext;
// handle buffer pool resizing..
if([self didValueForInputKeyChange:@"inputWidth"] || [self didValueForInputKeyChange:@"inputHeight"])
{
NSLog(@"Resizing buffer Pool");
// resize our CVOpenGLBufferPool
[self destroyCVResources];
[self createCVResources];
}
// we have to periodically call CVOpenGLTextureCacheFlush or we get a leak.
// the longer we wait, the more vram we use, but the more 'frames' of glitch we get.
// the end user can control this amount as an additional 'effect'.
static int frameCount = 0;
frameCount = frameCount % (self.inputFlushDelay + 1);
if(frameCount == 0)
{
CVOpenGLTextureCacheFlush(cvTextureCache, 0);
}
frameCount++;
// delete old frame, if we need to... this avoids VRam / texture leak
if(cvTexture != NULL)
{
CVOpenGLTextureRelease(cvTexture);
cvTexture = NULL;
}
if(cvImageBuffer != NULL)
{
CVOpenGLBufferRelease(cvImageBuffer);
cvImageBuffer = NULL;
}
if(CVOpenGLBufferPoolCreateOpenGLBuffer(NULL, cvBufferPool, &cvImageBuffer) == kCVReturnSuccess)
{
//Use the buffer as the OpenGL context destination
if(CVOpenGLBufferAttach(cvImageBuffer, cvGLContext, 0, 0, 0) == kCVReturnSuccess)
{
// dont clear or draw anything in our CVOpenGLBuffer
// this causes our glitch :)
}
else
{
NSLog(@"Failed Attaching CVOGL buffer");
CVOpenGLBufferRelease(cvImageBuffer);
cvImageBuffer = NULL;
}
// convert out cvImageBuffer to a texture from our texture cache
NSDictionary * emptyAttributes = [NSDictionary dictionary];
if(CVOpenGLTextureCacheCreateTextureFromImage(NULL, cvTextureCache, cvImageBuffer, (__bridge CFDictionaryRef) emptyAttributes, &cvTexture) != kCVReturnSuccess)
{
cvTexture = NULL;
}
}
// switch to our QC plugin GL context...
cgl_ctx = [context CGLContextObj];
#if __BIG_ENDIAN__
#define v002PlugInPixelFormat QCPlugInPixelFormatARGB8
#else
#define v002PlugInPixelFormat QCPlugInPixelFormatBGRA8
#endif
self.outputImage = [context outputImageProviderFromTextureWithPixelFormat:v002PlugInPixelFormat pixelsWide:self.inputWidth pixelsHigh:self.inputHeight name:CVOpenGLTextureGetName(cvTexture) flipped:NO releaseCallback:_TextureReleaseCallback releaseContext:cvGLContext colorSpace:[context colorSpace] shouldColorMatch:YES];
return YES;
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
}
- (void) destroyCVResources
{
CVOpenGLTextureCacheRelease(cvTextureCache);
CVOpenGLBufferPoolRelease(cvBufferPool);
}
- (void) createCVResources
{
if(cvGLContext != nil)
{
NSMutableDictionary * bufferOptions = [NSMutableDictionary dictionary];
[bufferOptions setValue:[NSNumber numberWithDouble:self.inputWidth] forKey:(NSString*)kCVOpenGLBufferWidth];
[bufferOptions setValue:[NSNumber numberWithDouble:self.inputHeight] forKey:(NSString*)kCVOpenGLBufferHeight];
if(CVOpenGLBufferPoolCreate(NULL, NULL, (__bridge CFDictionaryRef)bufferOptions, &cvBufferPool) != kCVReturnSuccess)
cvBufferPool = NULL;
if(CVOpenGLTextureCacheCreate(NULL, NULL, cvGLContext, cvPixelFormat, NULL, &cvTextureCache) != kCVReturnSuccess)
cvTextureCache = NULL;
}
else
{
NSLog(@"could not create cvGLcontext... :(");
}
}
@end