Altering Filter Chain with GPUImageTwoInputFilter
When altering the targets of a pipeline that contains GPUImageTwoInputFilters (blends, etc) does not work.
For example:
GPUImageScreenBlendFilter -> GPUImageEmbossFilter -> GPUImageScreenBlendFilter -> GPUImageView
If I attempt to remove GPUImageEmbossFilter from the chain and connect the first ScreenBlend with the Second the output does update properly (either get a white screen or black depending on ordering, etc.)
The issue is there is so much "state" maintained in a GPUImageScreenBlendFilter that it becomes a maintenance nightmare. GPUImageTwoInputFilter is also modified by the fact that someone set it as a target (i.e. something that should be transparent is no longer so) because it modifies the way nextAvailableTextureIndex is used. The major culprit seems to be hasSetFirstTexture but my attempts to reset this to NO has not seemed to help.
I resolved this by using addTarget:atTextureLocation and just managing the texture locations myself.
Did you try calling removeTarget on the first screenBlend?
@powellmark anyway you could you post an example snippet? Or explain why setting the texture directly at an index does solve it? I tried doing this using a GPUImagePicture but couldn't get it to work. It wouldn't change if I had a two input filter in the chain. Is there a special ordering that you call processImage, etc in?
Sure. I can show an example of what I'm doing. Let's say I have a chain: Picture -> Blend -> Emboss -> Blend. So, there are two blends (with two additional Pictures for blending).
I set the main filter like:
[picture addTarget:blend1 atTextureLocation:0]; [blend1 addTarget:emboss atTextureLocation:0]; [emboss addTarget:blend2 atTextureLocation:0]; [blend2 addTarget:outputView atTextureLocation:0];
then I set the blend pictures to texture location 1 [blendPic1 addTarget:blend1 atTextureLocation:1]; [blendPic2 addTarget:blend2 atTextureLocation:1];
When I make changes to rebuild the entire chain from scratch, making sure to "Remove all targets" from each filter and picture.
@powellmark Thanks! Do you mind showing where are you calling processImage on the pictures? Does that ordering matter? Should it only be called once on each blend pic? or each time the chain is re-established?
pull request #935 might fix this issue. it changes the way removeTarget: works to correctly reset GPUImageTwoInputFilters.
In my usecase the pull request did not fix the issue. The solution was to call processImage on the secondary images when updating the filterPipeline. As I have wrapped TwoInputFilters in a FilterGroup (like the Amatorka-Filter), I am calling prepareForImageCapture on every filter when the new values are set and I want to update my Output (GPUImageView).
AFAICT the ordering does matter. Calling processImage on the blend images before calling processImage on the source image is what I use. Be aware that source and blend textures might be interchanged when calling processImage in a different order.
I have a problem with GPUImageTwoInputFilter http://stackoverflow.com/questions/20654483/gpuimage-gpuimagetwoinputfilter-two-videos
Can anyone help me?
I am having a similar white screen issue when trying to use a twoInputFilter. In my case the code is generating an image buffer on the fly. This image buffer is created on the CPU in response to a RawDataOutput setNewFrameAvailableBlock. I am then taking the rawBytesForImage and populating my buffer. Like in the code above I am setting two targets on the filter.
Initial setup:
rawDataInput = [[GPUImageRawDataInput alloc] init];
[rawDataInput setPixelFormat:GPUPixelFormatRGBA];
[boxBlurFilter addTarget:twoInputFilter atTextureLocation:0];
[rawDataInput addTarget:twoInputFilter atTextureLocation:1];
In the setNewFrameAvailableBlock I call a function on my filter that edits and feeds the rawDataoutput. For simplicity the code in my function is only taking the bytes and passing them to the raw data. Then I try to pass that buffer via a RawDataInput to my twoInputFilter.
GLubyte *imageBytes = [cameraOutput rawBytesForImage];
[rawDataInput updateDataFromBytes:imageBytes size:cameraOutput.maximumOutputSize];
In my shader there is only a debug line:
gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
Still my screen is white, any ideas?