1

I'm trying to integrate GPUImage in a cocos2d-swift (SpriteBuilder) project so that I can use the video filters within my app. I got GPUImage working fine in the samples so I moved on and added the framework and followed the README to get GPUImage into my exported SpriteBuilder project which loads the MainScene. However as soon as I try to use anything from GPUImage I get a bunch of OpenGL errors and the app crashes.

OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized commit] 179
OpenGL error GL_INVALID_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized commit] 179
OpenGL error GL_INVALID_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized prepare] 170
OpenGL error GL_INVALID_VALUE detected at -[CCGraphicsBufferGLUnsynchronized prepare] 170
OpenGL error GL_INVALID_VALUE detected at -[CCGLView presentFrame] 463
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized commit] 179
OpenGL error GL_INVALID_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized commit] 179
OpenGL error GL_INVALID_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at CCRenderStateGLTransition 285
OpenGL error GL_INVALID_FRAMEBUFFER_OPERATION detected at -[CCGraphicsBufferGLUnsynchronized prepare] 170
OpenGL error GL_INVALID_VALUE detected at -[CCGraphicsBufferGLUnsynchronized prepare] 170
OpenGL error GL_INVALID_VALUE detected at -[CCGLView presentFrame] 463

I've commented out everything and tried uncommenting line by line to see what causes the crash and it happens whenever any of the GPUImage classes are instantiated. In this case, simply creating a GPUImageMovie object causes the crash:

let movieFile = GPUImageMovie(playerItem: playerItem)

Creating the GPUImageGrayscaleFilter or GPUImageView cause it as well. All I need to be able to do in my app is display a video or the camera and apply a filter to it, pretty simple on it's own; but as soon as cocos2d-swft was introduced a lot of problems showed up.

I see some people mentioning things about share groups and gl contexts and that GPUImage and cocos2d are conflicting with each other, but I've yet to find someone explaining how to actually get it working.

The entire MainScene.swift file is following (there is a button in the scene that calls the playVideo function, that's it).

import Foundation
import GPUImage

class MainScene: CCNode {
    func playVideo() {
        let mediaURL : NSURL! = NSBundle.mainBundle().URLForResource("BigBuckBunny_640x360", withExtension: "m4v")
        let mainPlayer : AVPlayer! = AVPlayer()
        let playerItem : AVPlayerItem! = AVPlayerItem(URL: mediaURL)

        let movieFile = GPUImageMovie(playerItem: playerItem)
        movieFile.runBenchmark = false
        movieFile.playAtActualSpeed = true
        let filter = GPUImageGrayscaleFilter()
        movieFile.addTarget(filter)

        let mainView = CCDirector.sharedDirector().view;

        let filterView : GPUImageView! = GPUImageView(frame: CGRectMake(0, 0, 640, 360))
        mainView.addSubview(filterView)
        filter.addTarget(filterView)

        movieFile.startProcessing()
        mainPlayer.play()
    }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Since you're using cocos2d v3 you would have to add any custom rendering to a specific CCRenderer method. GPUImage doesn't support that kind of rendering, and is generally in conflict with cocos2d since both use OpenGL but there can only be a single OpenGL context. You may want to look into CCEffect/CCEffectNode that SB/CC provide, they offer similar visual effects. – CodeSmile Dec 18 '14 at 08:36
  • CCEffect/CCEffectNode look promising, and I was able to get my video to display where I wanted it to in just a few minutes using CCUIViewWrapper and AVPlayer. However the CCEffects don't affect the video, do you know if it will be possible to have them work on the video added to the UIView ? or if there is a better/the right way to add the video and have it react to the effects ? I'm currently using `let videoViewWrapper = CCUIViewWrapper(forUIView: videoView); videoViewWrapper.effect = CCEffectSaturation(saturation: 1); self.addChild(videoViewWrapper)` – Richard Roylance Dec 18 '14 at 15:41
  • Video? That won't work, there's no "video node" in Cocos2D and CCEffect can only affect nodes, not other UIViews like AVVideoPlayer. Perhaps it might be worth considering to launch the video + GPUImage in one view (controller), and have the cocos2d view in another - if you can separate the two, for instance if the video is supposed to be a fullscreen video anyway. – CodeSmile Dec 18 '14 at 16:02
  • I'll take a look at doing that, the video needs to be shown in conduction with other UI elements but with a grayscale filter and an overlay. If it was just viewing the video, using SpriteBuilder would be great, but needing the extras is causing issues. I'm in the research phase to see what the best options for the app is, and SpriteBuilder/cocos is great for everything except the video extras portion, damn. – Richard Roylance Dec 18 '14 at 16:06

0 Answers0