我的GLSL着色器程序是否由浏览器或驱动程序优化?

我的GLSL着色器程序是否由浏览器或驱动程序优化?,glsl,webgl,compiler-optimization,Glsl,Webgl,Compiler Optimization,在我最近的一个项目中,我按程序创建了片段着色器,看起来像这样(但可能更大),我使用WebGL显示: precision mediump float; uniform vec2 u_windowSize; void main() { float s = 2.0 / min(u_windowSize.x, u_windowSize.y); vec2 pos0 = s * (gl_FragCoord.xy - 0.5 * u_windowSize); if (length(pos0) &g

在我最近的一个项目中,我按程序创建了片段着色器,看起来像这样(但可能更大),我使用WebGL显示:

precision mediump float;
uniform vec2 u_windowSize;
void main() {
  float s = 2.0 / min(u_windowSize.x, u_windowSize.y);
  vec2 pos0 = s * (gl_FragCoord.xy - 0.5 * u_windowSize);
  if (length(pos0) > 1.0) { gl_FragColor = vec4(0,0,0,0); return; }
  vec2 pos1 = pos0/0.8;
  vec2 pos2 = ((1.0-length(pos1))/length(pos1)) * pos1;
  vec3 col2 = vec3(1.0,1.0,1.0);
  vec2 pos3 = pos2;
  vec3 col3 = vec3(1.0,0.0,0.0);
  vec2 tmp2 = 6.0*(1.0/sqrt(2.0)) * mat2(1.0,1.0,-1.0,1.0) * pos2;
  vec3 col4;
  if (mod(tmp2.x, 2.0) < 1.0 != mod(tmp2.y, 2.0) < 1.0) {
     col4 = col2;
  } else {
     col4 = col3;
  };      
  vec2 pos5 = pos0;
  vec3 col5 = vec3(0.0,1.0,1.0);
  vec3 col6;
  if (length(pos0) < 0.8) {
     col6 = col4;
  } else {
     col6 = col5;
  };
  gl_FragColor = vec4(col6, 1.0);
}
precision mediump float;
统一的vec2窗口大小;
void main(){
浮动s=2.0/min(u_windowSize.x,u_windowSize.y);
vec2 pos0=s*(gl_FragCoord.xy-0.5*u_windowSize);
如果(长度(pos0)>1.0){gl_FragColor=vec4(0,0,0,0);返回;}
vec2-pos1=pos0/0.8;
vec2-pos2=((1.0-length(pos1))/length(pos1))*pos1;
vec3 col2=vec3(1.0,1.0,1.0);
vec2-pos3=pos2;
vec3 col3=vec3(1.0,0.0,0.0);
vec2-tmp2=6.0*(1.0/sqrt(2.0))*mat2(1.0,1.0,-1.0,1.0)*pos2;
vec3-col4;
如果(mod(tmp2.x,2.0)<1.0!=mod(tmp2.y,2.0)<1.0){
col4=col2;
}否则{
col4=col3;
};      
vec2-pos5=pos0;
vec3 col5=vec3(0.0,1.0,1.0);
vec3-col6;
if(长度(pos0)<0.8){
col6=col4;
}否则{
col6=col5;
};
gl_FragColor=vec4(col6,1.0);
}
显然,这里有一些冗余,您无法手工编写–例如,将
pos2
复制到
pos3
是毫无意义的。但由于我生成了这段代码,这很方便

在我现在过早地开始优化并使我的生成器产生更高效的代码之前,我想知道:


浏览器和/或图形驱动程序是否已经优化了这些内容(因此我不必这么做)?

浏览器不需要进行任何优化。您可以使用
WEBGL\u debug\u着色器
扩展查看浏览器发送给驱动程序的内容

例如:

const gl=document.createElement('canvas').getContext('webgl');
const ext=gl.getExtension('WEBGL_debug_着色器');
常数vs=`
属性向量4位置;
一致mat4矩阵;
void main(){
浮点a=1.+2.*3.///是否将其优化为7?
float b=a;//这会被丢弃吗?
gl_位置=矩阵*位置*向量4(b,a,b,a);
}
`;
常量s=gl.createShader(gl.VERTEX_着色器);
gl.着色器源(s,vs);
总帐编辑主管;

log(ext.getTranslatedShaderSource)谢谢,所以浏览器没有太多功能。司机呢?离线优化在这种情况下是很棘手的,因为着色器是在浏览器上用JavaScript计算的,所以(对我来说)很难预测什么是值得优化的,什么比它节省的成本更高。你的链接指向哪个有趣(虽然有点过时……我想知道情况是否有所改善)。不知道。您可以将其用作自己的优化器的起点