渲染时在 WebGL 中重用纹理

Reusing textures in WebGL while rendering

我在 WebGL 中有一个很长的着色器管道,其中每个着色器从一个输入纹理读取,渲染到另一个纹理,最后一个纹理包含所需的结果。

我可以在管道中重用纹理吗,就好像它是同步的运行?

// ... init texA to contain input  ...
shader1.samplingFrom(texA).renderTo(texB);
shader2.samplingFrom(texB).renderTo(texA);
shader3.samplingFrom(texA).renderTo(texB);
return readPixels(texB); // Always the same answer?

就此而言,我什至可以依靠着色器在下一阶段开始之前完成吗?

// ... init texA to contain input  ...
shader1.samplingFrom(texA).renderTo(texB);
shader2.samplingFrom(texB).renderTo(texC);
shader3.samplingFrom(texC).renderTo(texD);
return readPixels(texD); // Always the same answer?

我最初认为我可以重用纹理,但我注意到如果我停止管道(暗示某种竞争条件),奇怪的行为就会消失,所以现在我不确定提供了什么保证。

是的,您可以重复使用纹理。在 OpenGL/WebGL 中你不能做的是在同一个绘图调用中读取并渲染到同一个纹理。

还有着色器 运行 一个接一个,不是并行的,至少在 OpenGL 中是这样。单个着色器可能会在内部并行执行某些操作,但结果需要与串行执行 运行 相同。