⬆️ ⬇️

The expediency of using Adobe Pixel Bender shaders

It would seem that this technology will provide a very significant plus to the flash, because it gave the applications some kind of multithreading, which should greatly increase productivity.



In tandem with other innovations, such as hardware acceleration for a video player, one could also expect acceleration from shaders (I think everyone associates a shader with video cards), but disappointment started here, but he didn’t present any apart acceleration. Then it would be advisable to ask, and where in general can this new technology be applied, on what practical task, and what is the practical meaning of this?



By connecting the imagination, you can certainly come up with so many ways to use shaders. But how much they are in demand? And how much benefit do they bring?

For example, by using some kind of color correction of photography using a shader, you will save a tenth of a second if you did the same with pure AS3. Such benefits in such tasks put the rationality of the use of shaders into question.



But then I remembered one of the projects I had to work on, and decided that it would be possible to optimize it significantly using a shader.

This is a stereo video player (3D). There are several algorithms for constructing three-dimensional images, but in a nutshell I will tell you about one of the most resource-intensive ones.

At the entrance you have a video, visually divided into 2 parts. The left part is for example for the left eye, the right part for the right one (although it is often divided into the upper part and the lower part).

The output should be a video for a special polarizing 3D monitor, which displays an image under different polarization planes in a checkerboard pattern — for example, white cells under one plane, black ones under another.

All we need to do is split the video into 2 pictures, and display the third, where the “white” cells are cells from the left picture, and the “black” cells are from the right.

')

Schematically it looks like this:



image



With the most obvious implementation algorithm, our program will work like this:

- using the standard ActionScipt 3.0 tools (getPixel, setPixel), we each frame generate a new image, each pixel of which is equal to a pixel on the left side, if the x and y coordinates are paired or x and y are unpaired, or otherwise each pixel is which is equal to the pixel on the right side of the image.



The second implementation is to do the same dirty work on the shader.

- we divide the image into the left and right pictures, we impose a shader as a filter on the left picture, passing it to it in advance and the right one.



By setting the number of frames per second to a flash application equal to 30, the Intel Core 2 Duo E7500 processor (2.93 GHz) could actually squeeze only 7 frames per second for 1290x720 video, and when using a shader, this indicator was 24 frames per second.

Significant gains, and one would be happy to rejoice and praise Adobe shaders.



But there is another option for the implementation of this task:

pre-draw a chess mask

We divide the video into 2 pictures, do not touch the left at all, and over it we throw the right picture with the imposed chess mask. We achieve the desired effect at a rate of 30.1 Fps.



Performance Chart for Intel Core 2 Duo E7500 Processor (2.93 GHz)

image



On an older processor, dual-core Pentium 2.8 GHz, the results were as follows: using a mask we achieve 6.8 frames per second, using a shader 4.7, using pixel-by-pixel rendering using AS3 2.9. As we see here, the advantage of using a shader was even more insignificant.



Performance Chart for Pentium 2.8 GHz Processor

image



The bottom line: the correct application of high-level methods ActionScript 3.0 has proven its effectiveness before applying advanced technologies to work with a low-level shader. Another practical task where the shader was absolutely useless.

Source: https://habr.com/ru/post/113315/



All Articles