OpenGL着色器编译器可以优化制服上的表达式吗? [英] Can OpenGL shader compilers optimize expressions on uniforms?

查看:127
本文介绍了OpenGL着色器编译器可以优化制服上的表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些制服的OpenGL ES着色器.我在片段着色器中对制服进行一些数学运算.着色器编译器是否会在制服上优化这些表达式,使它们一次出现而不是在每个像素上出现?还是应该在着色器之外进行计算,然后将结果传递给我?

I've got an OpenGL ES shader with some uniforms in it. I do some math on the uniforms in my fragment shader. Will the shader compiler generally optimize those expressions on the uniforms so they happen once instead of on every pixel? Or should I do the computations outside the shader and pass the results in?

具体地说,我将三个统一坐标传递给着色器:

Specifically, I've got three uniform coordinates I'm passing into the shader:

uniform vec2 u_a;
uniform vec2 u_b;
uniform vec2 u_c;

然后我计算出这些点中的一些向量:

And then I compute some vectors among those points:

vec2 v0 = u_c - u_a;
vec2 v1 = u_b - u_a;

我很好奇着色器编译器是否可以优化它们,使它们在每个渲染中发生一次,或者我是否应该在着色器外部进行计算并将它们作为其他统一形式传递.当然,为了进行优化,我应该真正衡量一下事情以发现我的特定情况下的差异(因为GPU在每个像素上执行此操作的速度可能比CPU快),但是我对着色器编译器的作用范围有兴趣像这样的优化.

I'm curious if the shader compiler can optimize these such that they happen once per render, or if I should compute these outside the shader and pass them in as additional uniforms. Of course, for optimizations, I should really measure things to find the difference in my specific situation (since the GPU might be faster doing this on every pixel than my CPU), but I'm interested in how much scope the shader compilers have for optimizations like this in general.

如果您知道Android/iPhone设备上的着色器编译器的行为可能有所不同,或者不同的GLSL版本会有所不同,则加分.

Bonus points if you know that shader compilers on Android/iPhone devices might behave differently, or if different GLSL versions make a difference.

推荐答案

编译器可以对此进行优化吗?是的,可以. 吗?可能不是,因为这将在开始渲染对象时需要大量的开销(它们必须为着色器预先计算所有可预先计算的内容).

Could a compiler optimize that? Yes, it could. Will it? Probably not, as this would require significant overhead when beginning to render an object (they have to pre-compute all the pre-computable stuff for shaders).

这篇关于OpenGL着色器编译器可以优化制服上的表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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