丢弃对OpenGL中的程序性能有害吗? [英] Is discard bad for program performance in OpenGL?

查看:14
本文介绍了丢弃对OpenGL中的程序性能有害吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 这篇文章,作者写道:

I was reading this article, and the author writes:

以下是如何通过两个简单的步骤在每个平台上编写高性能应用程序:
[...]
遵循最佳实践.对于 Android 和 OpenGL,这包括诸如批量绘制调用"、不要在片段着色器中使用丢弃"等内容.

Here's how to write high-performance applications on every platform in two easy steps:
[...]
Follow best practices. In the case of Android and OpenGL, this includes things like "batch draw calls", "don't use discard in fragment shaders", and so on.

我以前从未听说过丢弃会对性能等产生不良影响,并且一直在使用它来避免在不需要详细的 alpha 时进行混合.

I have never before heard that discard would have a bad impact on performance or such, and have been using it to avoid blending when a detailed alpha hasn't been necessary.

有人可以解释一下为什么以及何时使用丢弃可能被认为是一种不好的做法,以及丢弃 + depthtest 与 alpha + blend 相比如何?

Could someone please explain why and when using discard might be considered a bad practise, and how discard + depthtest compares with alpha + blend?

在收到关于这个问题的答案后,我做了一些测试,方法是在其上渲染带有纹理四边形的背景渐变.

After having received an answer on this question I did some testing by rendering a background gradient with a textured quad on top of that.

  • 使用 GL_DEPTH_TEST 和以if(gl_FragColor.a <0.5 ){ 丢弃;}" 大约 32 fps.
  • 从片段着色器中删除 if/discard 语句增加了渲染速度约为 44 fps.
  • 将 GL_BLEND 与混合函数(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)" 而不是 GL_DEPTH_TEST 也导致了大约 44 fps.
  • Using GL_DEPTH_TEST and a fragment-shader ending with the line "if( gl_FragColor.a < 0.5 ){ discard; }" gave about 32 fps.
  • Removing the if/discard statement from the fragment-shader increased the rendering speed to about 44 fps.
  • Using GL_BLEND with the blend function "(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)" instead of GL_DEPTH_TEST also resulted in around 44 fps.

推荐答案

依赖于硬件.对于 PowerVR 硬件和其他使用基于 tile 的渲染的 GPU,使用 discard 意味着 TBR 不能再假设绘制的每个片段都会变成一个像素.这个假设很重要,因为它允许 TBR首先评估所有深度,然后只评估最顶层片段的片段着色器.一种延迟渲染方法,硬件除外.

It's hardware-dependent. For PowerVR hardware, and other GPUs that use tile-based rendering, using discard means that the TBR can no longer assume that every fragment drawn will become a pixel. This assumption is important because it allows the TBR to evaluate all the depths first, then only evaluate the fragment shaders for the top-most fragments. A sort of deferred rendering approach, except in hardware.

请注意,开启 alpha 测试也会遇到同样的问题.

Note that you would get the same issue from turning on alpha test.

这篇关于丢弃对OpenGL中的程序性能有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆