Skip to content

Commit

Permalink
Merge pull request BradLarson#1046 from frankschlegel/texture-options
Browse files Browse the repository at this point in the history
Introduced outputTextureOptions for GPUImageOutput.
  • Loading branch information
BradLarson committed Jun 25, 2013
2 parents ec99dcc + 860ef84 commit fe658e3
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 56 deletions.
22 changes: 17 additions & 5 deletions framework/Source/GPUImageAverageColor.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ - (void)initializeOutputTextureIfNeeded;
GLuint textureForStage;
glGenTextures(1, &textureForStage);
glBindTexture(GL_TEXTURE_2D, textureForStage);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);
[stageTextures addObject:[NSNumber numberWithInt:textureForStage]];

// NSLog(@"At reduction: %d size in X: %f, size in Y:%f", currentReduction, currentStageSize.width, currentStageSize.height);
Expand Down Expand Up @@ -220,7 +220,15 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize;
#endif

// NSLog(@"FBO stage size: %f, %f", currentFramebufferSize.width, currentFramebufferSize.height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFramebufferSize.width, (int)currentFramebufferSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexImage2D(GL_TEXTURE_2D,
0,
self.outputTextureOptions.internalFormat,
(int)currentFramebufferSize.width,
(int)currentFramebufferSize.height,
0,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, currentTexture, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

Expand Down Expand Up @@ -339,6 +347,10 @@ - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteg

- (void)extractAverageColorAtFrameTime:(CMTime)frameTime;
{
// we need a normal color texture for averaging the color values
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"The output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"The type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGSize finalStageSize = [[stageSizes lastObject] CGSizeValue];
#else
Expand Down
18 changes: 13 additions & 5 deletions framework/Source/GPUImageBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,22 @@ - (GLuint)generateTexture;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &newTextureName);
glBindTexture(GL_TEXTURE_2D, newTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);

CGSize currentFBOSize = [self sizeOfFBO];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexImage2D(GL_TEXTURE_2D,
0,
self.outputTextureOptions.internalFormat,
(int)currentFBOSize.width,
(int)currentFBOSize.height,
0,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
0);
glBindTexture(GL_TEXTURE_2D, 0);

});
Expand Down
25 changes: 19 additions & 6 deletions framework/Source/GPUImageFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ void dataProviderUnlockCallback (void *info, const void *data, size_t size)

- (CGImageRef)newCGImageFromCurrentlyProcessedOutputWithOrientation:(UIImageOrientation)imageOrientation
{

// a CGImage can only be created from a 'normal' color texture
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"For conversion to a CGImage the output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"For conversion to a CGImage the type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

__block CGImageRef cgImageFromBytes;

runSynchronouslyOnVideoProcessingQueue(^{
Expand Down Expand Up @@ -316,11 +321,11 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize;
filterTextureCache, renderTarget,
NULL, // texture attributes
GL_TEXTURE_2D,
GL_RGBA, // opengl format
self.outputTextureOptions.internalFormat, // opengl format
(int)currentFBOSize.width,
(int)currentFBOSize.height,
GL_BGRA, // native iOS format
GL_UNSIGNED_BYTE,
self.outputTextureOptions.format, // native iOS format
self.outputTextureOptions.type,
0,
&renderTexture);
if (err)
Expand All @@ -333,8 +338,8 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize;

glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), CVOpenGLESTextureGetName(renderTexture));
outputTexture = CVOpenGLESTextureGetName(renderTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);

Expand All @@ -353,7 +358,15 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize;
// }
// else
// {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexImage2D(GL_TEXTURE_2D,
0,
self.outputTextureOptions.internalFormat,
(int)currentFBOSize.width,
(int)currentFBOSize.height,
0,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
0);
// }
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, 0);
// glBindFramebuffer(GL_FRAMEBUFFER, filterFramebuffer);
Expand Down
3 changes: 3 additions & 0 deletions framework/Source/GPUImageHarrisCornerDetectionFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ - (void)dealloc;

- (void)extractCornerLocationsFromImageAtFrameTime:(CMTime)frameTime;
{
// we need a normal color texture for this filter
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"The output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"The type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

NSUInteger numberOfCorners = 0;
CGSize imageSize = nonMaximumSuppressionFilter.outputFrameSize;
Expand Down
4 changes: 4 additions & 0 deletions framework/Source/GPUImageHistogramFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteg

- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates sourceTexture:(GLuint)sourceTexture;
{
// we need a normal color texture for this filter
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"The output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"The type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

if (self.preventRendering)
{
return;
Expand Down
3 changes: 3 additions & 0 deletions framework/Source/GPUImageHoughTransformLineDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ - (void)dealloc;

- (void)extractLineParametersFromImageAtFrameTime:(CMTime)frameTime;
{
// we need a normal color texture for this filter
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"The output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"The type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

NSUInteger numberOfLines = 0;
CGSize imageSize = nonMaximumSuppressionFilter.outputFrameSize;
Expand Down
44 changes: 26 additions & 18 deletions framework/Source/GPUImageJFAVoronoiFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ - (void)initializeOutputTextureIfNeeded;
glActiveTexture(GL_TEXTURE2);
glGenTextures(1, &outputTexture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);
glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures(1, &secondFilterOutputTexture);
glBindTexture(GL_TEXTURE_2D, secondFilterOutputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);
glBindTexture(GL_TEXTURE_2D, 0);
}

Expand Down Expand Up @@ -459,11 +459,11 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize
filterTextureCache, renderTarget,
NULL, // texture attributes
GL_TEXTURE_2D,
GL_RGBA, // opengl format
self.outputTextureOptions.internalFormat, // opengl format
(int)currentFBOSize.width,
(int)currentFBOSize.height,
GL_BGRA, // native iOS format
GL_UNSIGNED_BYTE,
self.outputTextureOptions.format, // native iOS format
self.outputTextureOptions.type,
0,
&renderTexture);
if (err)
Expand All @@ -475,8 +475,8 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize
CFRelease(empty);
glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), CVOpenGLESTextureGetName(renderTexture));
secondFilterOutputTexture = CVOpenGLESTextureGetName(renderTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapS);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);

Expand All @@ -488,19 +488,27 @@ - (void)createFilterFBOofSize:(CGSize)currentFBOSize
[self initializeOutputTextureIfNeeded];

glBindTexture(GL_TEXTURE_2D, secondFilterOutputTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexImage2D(GL_TEXTURE_2D,
0,
self.outputTextureOptions.internalFormat,
(int)currentFBOSize.width,
(int)currentFBOSize.height,
0,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, secondFilterOutputTexture, 0);

[self notifyTargetsAboutNewOutputTexture];
}

glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);

glBindTexture(GL_TEXTURE_2D, secondFilterOutputTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

Expand Down
4 changes: 4 additions & 0 deletions framework/Source/GPUImageLuminosity.m
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates

- (void)extractLuminosityAtFrameTime:(CMTime)frameTime;
{
// we need a normal color texture for this filter
NSAssert(self.outputTextureOptions.format == GL_RGBA, @"The output texture format for this filter must be GL_RGBA.");
NSAssert(self.outputTextureOptions.type == GL_UNSIGNED_BYTE, @"The type of the output texture of this filter must be GL_UNSIGNED_BYTE.");

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGSize finalStageSize = [[stageSizes lastObject] CGSizeValue];
#else
Expand Down
31 changes: 25 additions & 6 deletions framework/Source/GPUImageMovie.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,18 @@ - (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer;

[GPUImageContext useImageProcessingContext];
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, coreVideoTextureCache, movieFrame, NULL, GL_TEXTURE_2D, GL_RGBA, bufferWidth, bufferHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0, &texture);
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
coreVideoTextureCache,
movieFrame,
NULL,
GL_TEXTURE_2D,
self.outputTextureOptions.internalFormat,
bufferWidth,
bufferHeight,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
0,
&texture);

if (!texture || err) {
NSLog(@"Movie CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
Expand All @@ -312,10 +323,10 @@ - (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer;
outputTexture = CVOpenGLESTextureGetName(texture);
// glBindTexture(CVOpenGLESTextureGetTarget(texture), outputTexture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.outputTextureOptions.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.outputTextureOptions.magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.outputTextureOptions.wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.outputTextureOptions.wrapT);

for (id<GPUImageInput> currentTarget in targets)
{
Expand Down Expand Up @@ -343,7 +354,15 @@ - (void)processMovieFrame:(CMSampleBufferRef)movieSampleBuffer;

glBindTexture(GL_TEXTURE_2D, outputTexture);
// Using BGRA extension to pull in video frame data directly
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(movieFrame));
glTexImage2D(GL_TEXTURE_2D,
0,
self.outputTextureOptions.internalFormat,
bufferWidth,
bufferHeight,
0,
self.outputTextureOptions.format,
self.outputTextureOptions.type,
CVPixelBufferGetBaseAddress(movieFrame));

CGSize currentSize = CGSizeMake(bufferWidth, bufferHeight);
for (id<GPUImageInput> currentTarget in targets)
Expand Down
Loading

0 comments on commit fe658e3

Please sign in to comment.