简体   繁体   English

WebGL:什么更快?

[英]WebGL: What is faster?

What is faster in WebGL? WebGL 中什么速度更快?

once: create 1000 shaders for 1000 objects and set uniforms to them一次:为 1000 个对象创建 1000 个着色器并为它们设置统一

every frame: bind shaders when rendering them每帧:渲染时绑定着色器

Or或者

once: create 10 shaders for 1000 objects一次:为 1000 个对象创建 10 个着色器

every frame: bind shaders + update uniforms according to objects?每帧:根据对象绑定着色器 + 更新制服?

I know I can write test on it.我知道我可以在上面写测试。 But I feel that someone surely thought about it before me.但我觉得肯定有人在我之前考虑过。 Thank you very much.非常感谢。

更改着色器很昂贵(它会使指令缓存无效),更新制服很便宜(它只是更新寄存器文件中的值)。

It's helpful to remember that the Graphics Pipeline is an actual pipeline typically implemented in hardware.记住图形管道是一个通常在硬件中实现的实际管道是有帮助的。 You get to configure the pipeline by assigning shaders and setting uniforms, and then you get to activate the pipeline (by calling drawElements or one of its friends).您可以通过分配着色器和设置制服来配置管道,然后您可以激活管道(通过调用drawElements或其朋友之一)。 This essentially loads a pile of input data into the start of the pipeline, and kicks off a process that is highly parallel.这实际上将一堆输入数据加载到管道的开头,并启动了一个高度并行的过程。 For example, in the middle of a run, some early vertices will have made it through the vertex shader and rasterizer, and the resulting fragments are being shaded, while other vertices are still back at the vertex shader stage being transformed.例如,在运行的中间,一些早期的顶点将通过顶点着色器和光栅化器,并且生成的片段正在被着色,而其他顶点仍然回到正在转换的顶点着色器阶段。 The different sections of the pipeline are all doing their thing to the data flowing by.管道的不同部分都在为流经的数据做自己的事情。

After you kick off this process, the CPU is free to do other stuff while the pipeline runs.在您开始这个过程后,CPU 可以在管道运行时自由地做其他事情。 But, if you want to reconfigure the pipeline, such as by changing shaders or altering uniforms, the CPU will block your thread and wait for the pipeline to be completely done to the last pixel.但是,如果您想重新配置管道,例如通过更改着色器或更改制服,CPU 将阻塞您的线程并等待管道完成到最后一个像素。

This means you want to avoid stopping and restarting the pipeline, to the extent possible.这意味着您希望尽可能避免停止和重新启动管道。 So the usual strategy is batching: Get as much work done as possible in a single draw call, with a single set of uniforms.所以通常的策略是批处理:在单个绘制调用中完成尽可能多的工作,使用一组制服。 That way, you exploit the parallel nature of the pipeline to the best extent possible in your app.这样,您就可以在您的应用程序中最大程度地利用管道的并行特性。

管道图像由 OpenGLInsights.com 提供

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

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