简体   繁体   English

GPU上的纹理图像处理?

[英]Texture Image processing on the GPU?

I'm rendering a certain scene into a texture and then I need to process that image in some simple way. 我正在将某个场景渲染到纹理中,然后我需要以一种简单的方式处理该图像。 How I'm doing this now is to read the texture using glReadPixels() and then process it on the CPU. 我现在如何做这个是使用glReadPixels()读取纹理,然后在CPU上处理它。 This is however too slow so I was thinking about moving the processing to the GPU. 然而这太慢了,所以我在考虑将处理转移到GPU。

The simplest setup to do this I could think of is to display a simple white quad that takes up the entire viewport in an orthogonal projection and then write the image processing bit as a fragment shader. 我能想到的最简单的设置是显示一个简单的白色四边形,它在正交投影中占据整个视口,然后将图像处理位写为片段着色器。 This will allow many instances of the processing to run in parallel as well as to access any pixel of the texture it requires for the processing. 这将允许处理的许多实例并行运行以及访问处理所需的纹理的任何像素。

Is this a viable course of action? 这是一个可行的行动方案吗? is it common to do things this way? 以这种方式做事常见吗? Is there maybe a better way to do it? 有没有更好的方法呢?

Yes, this is the usual way of doing things. 是的,这是通常的做事方式。

  1. Render something into a texture. 将某些东西渲染成纹理。
  2. Draw a fullscreen quad with a shader that reads that texture and does some operations. 使用着色器绘制全屏四边形,该着色器读取该纹理并执行一些操作。

Simple effects (eg grayscale, color correction, etc.) can be done by reading one pixel and outputting one pixel in the fragment shader. 可以通过读取一个像素并在片段着色器中输出一个像素来完成简单的效果(例如灰度,颜色校正等)。 More complex operations (eg swirling patterns) can be done by reading one pixel from offset location and outputting one pixel. 可以通过从偏移位置读取一个像素并输出一个像素来完成更复杂的操作(例如旋转图案)。 Even more complex operations can be done by reading multiple pixels. 通过读取多个像素可以完成更复杂的操作。

In some cases multiple temporary textures would be needed. 在某些情况下,需要多个临时纹理。 Eg blur with high radius is often done this way: 例如,高半径的模糊通常以这种方式完成:

  1. Render into a texture. 渲染成纹理。
  2. Render into another (smaller) texture, with a shader that computes each output pixel as average of multiple source pixels. 渲染为另一个(较小的)纹理,使用着色器将每个输出像素计算为多个源像素的平均值。
  3. Use this smaller texture to render into another small texture, with a shader that does proper Gaussian blur or something. 使用这个较小的纹理渲染成另一个小纹理,使用着色器进行适当的高斯模糊或其他。
  4. ... repeat ......重复一遍

In all of the above cases though, each output pixel should be independent of other output pixels. 但是,在所有上述情况中,每个输出像素应独立于其他输出像素。 It can use one more more input pixels just fine. 它可以使用更多的输入像素。

An example of processing operation that does not map well is Summed Area Table, where each output pixel is dependent on input pixel and the value of adjacent output pixel. 不能很好地映射的处理操作的示例是Summed Area Table,其中每个输出像素取决于输入像素和相邻输出像素的值。 Still, it is possible to do those kinds on the GPU ( example pdf ). 仍然可以在GPU上进行这些操作( 例如pdf )。

Yes, it's the normal way to do image processing. 是的,这是进行图像处理的常用方法。 The color of the quad doesn't really matter if you'll be setting the color for every pixel. 如果您要为每个像素设置颜色,则四边形的颜色并不重要。 Depending on your application, you might need to careful about pixel sampling issues (ie ensuring that you sample from exactly the correct pixel on the source texture, rather than halfway between two pixels). 根据您的应用,您可能需要注意像素采样问题(即确保您从源纹理上的正确像素进行采样,而不是在两个像素之间的中间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM