GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

GPUImageFilterGroup and memory constraints

Open peterhwong opened this issue 14 years ago • 3 comments

Hi Brad, I know you've said that each shader requires (width x height x 4) of memory to back it.

How hard would it be to change the code so that in a filter group, the result of each shader is put into one and only one block of memory regardless of how many shaders are in the group? I'm sure this would impact other things which I'm not aware of. But it would allow the use of many shaders on a high resolution image right?

Thanks Peter

peterhwong avatar Aug 09 '12 18:08 peterhwong

Not each shader (which is merely a program that runs against a given image or geometry input), but each output texture at each filter stage. The way that the framework is currently architected is that each individual filter pass takes in one or more textures from a previous pass, runs a vertex and fragment shader against it, and then writes the output to a texture-backed framebuffer. This gives the flexibility of being able to send that texture to multiple outputs and makes filters completely independent of one another.

One optimization that could be made would be to use a couple of shared textures for input and output at multiple stages, but that would require a linear filter chain (breaking things like unsharp masks, blends, and other filter types in the framework) and a shared texture pool that these could be drawn from. More than one texture would still be required, because OpenGL ES does not (currently) allow for reading from and writing to the same texture in one shader pass. You'd need to ping-pong between a couple of textures at least.

At some point, I'll probably work on a way of optimizing this, but like with multithreading support it will be a significant undertaking and will break stuff at first. In the meantime, my recommendation has been to write your own custom shaders that combine multiple filters together into a single pass where appropriate.

BradLarson avatar Aug 09 '12 19:08 BradLarson

Hi Brad, I think the linear filter chain optimization would work for me since that's how I use it. We are planning on combining as many of the shaders that we can together but even so, we would run into issues with the number of filters needed. Do you think a flag in the current filter group class would be best or a new linear filter group class? Any suggestions you have before I give this a shot?

Thanks Peter

On Aug 9, 2012, at 12:51 PM, Brad Larson wrote:

Not each shader (which is merely a program that runs against a given image or geometry input), but each output texture at each filter stage. The way that the framework is currently architected is that each individual filter pass takes in one or more textures from a previous pass, runs a vertex and fragment shader against it, and then writes the output to a texture-backed framebuffer. This gives the flexibility of being able to send that texture to multiple outputs and makes filters completely independent of one another.

One optimization that could be made would be to use a couple of shared textures for input and output at multiple stages, but that would require a linear filter chain (breaking things like unsharp masks, blends, and other filter types in the framework) and a shared texture pool that these could be drawn from. More than one texture would still be required, because OpenGL ES does not (currently) allow for reading from and writing to the same texture in one shader pass. You'd need to ping-pong between a couple of textures at least.

At some point, I'll probably work on a way of optimizing this, but like with multithreading support it will be a significant undertaking and will break stuff at first. In the meantime, my recommendation has been to write your own custom shaders that combine multiple filters together into a single pass where appropriate.

— Reply to this email directly or view it on GitHub.

peterhwong avatar Aug 09 '12 23:08 peterhwong

As I said, this will require some significant work on the underlying rendering architecture to get the texture pool set up right, and to provide for pulling from elements in this pool in the right order and then binding them to each framebuffer as needed. You're going to need to get fairly deep in the OpenGL ES side to make this work.

BradLarson avatar Aug 10 '12 16:08 BradLarson