-
Notifications
You must be signed in to change notification settings - Fork 4
/
v002GPUHSFlowBlurPlugin.m
287 lines (218 loc) · 9.5 KB
/
v002GPUHSFlowBlurPlugin.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
//
// v002GlitchGPUHSFlowDistort.m
// v002 Optical Flow
//
// Created by vade on 3/31/09.
// Copyright 2009 __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 "v002GPUHSFlowBlurPlugin.h"
#define kQCPlugIn_Name @"v002 Optical Flow Blur"
#define kQCPlugIn_Description @"Bluring via Optical Flow input. Feed the output of GPU Optical Flow into this plugin to distort based on motion vectors."
static void _TextureReleaseCallback(CGLContextObj cgl_ctx, GLuint name, void* info)
{
glDeleteTextures(1, &name);
}
@implementation v002GPUHSFlowBlurPlugin
@dynamic inputImage;
@dynamic inputImage2;
@dynamic inputAmount;
@dynamic outputImage;
+ (NSDictionary*) attributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey,
[kQCPlugIn_Description stringByAppendingString:kv002DescriptionAddOnText], QCPlugInAttributeDescriptionKey,
kQCPlugIn_Category, @"categories", nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
if([key isEqualToString:@"inputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputImage2"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Reposition Image", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputAmountX"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Amount (X)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputAmountY"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Amount (Y)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputFilterMode"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Filter Mode", QCPortAttributeNameKey,
[NSArray arrayWithObjects:@"Nearest", @"Linear", nil], QCPortAttributeMenuItemsKey,
[NSNumber numberWithUnsignedInteger:0.0], QCPortAttributeMinimumValueKey,
[NSNumber numberWithUnsignedInteger:1], QCPortAttributeMaximumValueKey,
[NSNumber numberWithUnsignedInteger:1.0], QCPortAttributeDefaultValueKey,
nil];
return [NSDictionary dictionaryWithObjectsAndKeys:@"Amount (Y)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputScaleR"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Scale (Red)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputScaleG"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Scale (Green)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputScaleB"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Scale (Blue)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputBiasR"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Bias (Red)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputBiasG"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Bias (Green)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"inputBiasB"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Bias (Blue)", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"outputImage"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Image", QCPortAttributeNameKey, nil];
}
return nil;
}
+ (NSArray*) sortedPropertyPortKeys
{
return [NSArray arrayWithObjects:@"inputImage",@"inputImage2" , @"inputAmountX", @"inputAmountY", @"inputFilterMode", @"inputScaleR", @"inputScaleG", @"inputScaleB", @"inputBiasR", @"inputBiasG", @"inputBiasB", nil];
}
+ (QCPlugInExecutionMode) executionMode
{
return kQCPlugInExecutionModeProcessor;
}
+ (QCPlugInTimeMode) timeMode
{
return kQCPlugInTimeModeNone;
}
- (id) init
{
if(self = [super init])
{
self.pluginShaderName = @"v002.GPUHSFLowFlowBlur";
}
return self;
}
@end
@implementation v002GPUHSFlowBlurPlugin (Execution)
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
CGLContextObj cgl_ctx = [context CGLContextObj];
CGLLockContext(cgl_ctx);
//glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &previousFBO);
id<QCPlugInInputImageSource> image = self.inputImage;
id<QCPlugInInputImageSource> previousImage = self.inputImage2;
NSUInteger width = [image imageBounds].size.width;
NSUInteger height = [image imageBounds].size.height;
NSRect bounds = [image imageBounds];
NSUInteger previousWidth = [previousImage imageBounds].size.width;
NSUInteger previousHeight = [previousImage imageBounds].size.height;
GLfloat amount = self.inputAmount;
CGColorSpaceRef cspace = ([image shouldColorMatch]) ? [context colorSpace] : [image imageColorSpace];
CGColorSpaceRef prevcspace = ([previousImage shouldColorMatch]) ? [context colorSpace] : [previousImage imageColorSpace];
if(image && [image lockTextureRepresentationWithColorSpace:cspace forBounds:[image imageBounds]]
&& previousImage && [previousImage lockTextureRepresentationWithColorSpace:prevcspace forBounds:[previousImage imageBounds]])
{
[image bindTextureRepresentationToCGLContext:cgl_ctx textureUnit:GL_TEXTURE0 normalizeCoordinates:YES];
[previousImage bindTextureRepresentationToCGLContext:cgl_ctx textureUnit:GL_TEXTURE1 normalizeCoordinates:YES];
GLuint finalOutput;
finalOutput = [self renderToFBO:cgl_ctx width:width height:height previousWidth:previousWidth previousHeight:previousHeight bounds:bounds texture:[image textureName] previousTexture:[previousImage textureName] amount:amount];
id provider = nil;
if(finalOutput != 0)
{
#if __BIG_ENDIAN__
#define v002QCPluginPixelFormat QCPlugInPixelFormatARGB8
#else
#define v002QCPluginPixelFormat QCPlugInPixelFormatBGRA8
#endif
// we have to use a 4 channel output format, I8 does not support alpha at fucking all, so if we want text with alpha, we need to use this and waste space. Ugh.
provider = [context outputImageProviderFromTextureWithPixelFormat:v002QCPluginPixelFormat pixelsWide:[image imageBounds].size.width pixelsHigh:[image imageBounds].size.height name:finalOutput flipped:NO releaseCallback:_TextureReleaseCallback releaseContext:NULL colorSpace:[context colorSpace] shouldColorMatch:[image shouldColorMatch]];
self.outputImage = provider;
}
[previousImage unbindTextureRepresentationFromCGLContext:cgl_ctx textureUnit:GL_TEXTURE1];
[previousImage unlockTextureRepresentation];
[image unbindTextureRepresentationFromCGLContext:cgl_ctx textureUnit:GL_TEXTURE0];
[image unlockTextureRepresentation];
}
else
self.outputImage = nil;
// return to our previous FBO;
//glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, previousFBO);
CGLUnlockContext(cgl_ctx);
return YES;
}
- (GLuint) renderToFBO:(CGLContextObj)cgl_ctx width:(NSUInteger)pixelsWide height:(NSUInteger)pixelsHigh previousWidth:(NSUInteger)previousPixelsWide previousHeight:(NSUInteger)previousPixelsHigh bounds:(NSRect)bounds texture:(GLuint)videoTexture previousTexture:(GLuint)previousVideoTexture amount:(double)amount
{
GLsizei width = bounds.size.width, height = bounds.size.height;
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
// new texture
GLuint fboTex = 0;
glGenTextures(1, &fboTex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, fboTex);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// this must be called before any other FBO stuff can happen for 10.6
[pluginFBO pushFBO:cgl_ctx];
[pluginFBO attachFBO:cgl_ctx withTexture:fboTex width:width height:height];
glColor4f(1.0, 1.0, 1.0, 1.0);
// do not need blending if we use black border for alpha and replace env mode, saves a buffer wipe
// we can do this since our image draws over the complete surface of the FBO, no pixel goes untouched.
glDisable(GL_BLEND);
// draw our input video
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, previousVideoTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, videoTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// bind our shader program
glUseProgramObjectARB([pluginShader programObject]);
// set program vars
glUniform1iARB([pluginShader getUniformLocation:"tex0"], 0); // load tex0 sampler to texture unit 0
glUniform1iARB([pluginShader getUniformLocation:"tex1"], 1); // load tex1 sampler to texture unit 1
glUniform1fARB([pluginShader getUniformLocation:"amt"], amount);
// Use VA for speed
GLfloat texcoords[] =
{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
GLfloat verts[] =
{
0, 0,
(GLfloat) width, 0,
(GLfloat) width, (GLfloat) height,
0, (GLfloat) height
};
glClientActiveTexture(GL_TEXTURE1);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glClientActiveTexture(GL_TEXTURE0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, verts);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// disable shader program
glUseProgramObjectARB(NULL);
[pluginFBO detachFBO:cgl_ctx]; // pops out and resets cached FBO state from above.
[pluginFBO popFBO:cgl_ctx];
glPopClientAttrib();
glPopAttrib();
return fboTex;
}
@end