我想用我的iPhone应用程序中的GLSL着色器在Photoshop中为屏幕的中心像素创建类似“魔棒”的效果(从相机捕捉图像)。现在,我已经通过获取像素数组并对中心像素应用某种泛洪填充算法(全部使用Objective-C代码)来实现这一点。这是在CPU上执行的,这对我来说有点太慢了,所以我想尝试使用GLSL着色器。
实际上,我所需要的就是在片段着色器中重写泛洪填充,更准确地说,是为了知道当前碎片的颜色是否接近阈值颜色,以及当前碎片是否是区域中先前检测到的碎片的邻居。这听起来对我来说太困惑了,我甚至不能理解这是否可能。
泛洪填充的算法是(伪代码):
Flood-fill (node, target-color, replacement-color):
1. Set Q to the empty queue.
2. If the color of node is not equal to target-color, return.
3. Add node to Q.
4. For each element n of Q:
5. If the color of n is equal to target-color:
6. Set w and e equal to n.
7. Move w to the west until the color of the node to the west of w no longer matches target-color.
8. Move e to the east until the color of the node to the east of e no longer matches target-color.
9. Set the color of nodes between w and e to replacement-color.
10. For each node n between w and e:
11. If the color of the node to the north of n is target-color, add that node to Q.
12. If the color of the node to the south of n is target-color, add that node to Q.
13. Continue looping until Q is exhausted.
14. Return.问题:是否可以在shader中做到这一点,如果可以,我如何做到这一点?
谢谢!
发布于 2012-10-04 13:03:31
不,着色器不会以这种方式工作。在着色器中,始终只能读取或写入,而不能同时读取或写入。如果你回过头来看看你的算法,它确实读写了相同的数据。
你可以试试乒乓球方案,但我怀疑它会不会很快:
for ( sometime )
for (every pixel in dest)
if source has filled neighbours (up,left,top,bottom) and is above threshold, then write fill
else write source
flip source and dest这将在每次迭代中增加一个像素--但你只有一个完成时间的上限(图像大小)。
你可以更聪明地尝试一些金字塔计划:首先以较低2倍的分辨率运行,然后用它来确定填充区域。但它真的不是一个在GPU上运行良好的算法。我建议使用手工优化的汇编CPU版本。
https://stackoverflow.com/questions/12718461
复制相似问题