写入gl_FragColor会导致glUseProgram抛出GL_INVALID_OPERATION [英] Writing to gl_FragColor causes glUseProgram to throw GL_INVALID_OPERATION

查看:350
本文介绍了写入gl_FragColor会导致glUseProgram抛出GL_INVALID_OPERATION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GLSL ES 2.0中编写模糊滤镜,并且在分配gl_FragColor的行中出现错误.我不知道为什么

I'm trying to write a blur filter in GLSL ES 2.0 and I'm getting an Error with the line assigning gl_FragColor. I've not been able to figure out why

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 textureCoordinate;
uniform samplerExternalOES s_texture;
void main() {
  float gaus[25] = float[25](0.01739, 0.03478, 0.04347, 0.03478, 0.01739,
                             0.03478, 0.07282, 0.10434, 0.07282, 0.03478,
                             0.04347, 0.10434, 0.13043, 0.10434, 0.04347,
                             0.03478, 0.07282, 0.10434, 0.07282, 0.03478,
                             0.01739, 0.03478, 0.04347, 0.03478, 0.01739);
  float offset[5] = float[5](-2.0, -1.0, 0.0, 1.0, 2.0);
  vec4 outSum = vec4(0.0);
  int rowi = 0;
  for(int i = 0; i < 5; i++){
    vec4 inSum = vec4(0.0);
    for(int j = 0; j < 5; j++){
      inSum += texture2D(s_texture, textureCoordinate + vec2(offset[i], offset[j]))*gaus[j*5+i];
    }
    outSum += inSum*gaus[rowi+i];
    rowi += 3;
  }
  gl_FragColor = outSum;
}

gl_FragColor的分配导致对glUseProgram的调用因GL_INVALID_OPERATION错误.我试过没有它,它可以编译和运行而不会出现错误.我希望有人可以指出我尚未看到的方向,至少是因为我看不到任何不起作用的原因.

The assignment of gl_FragColor causes calls to glUseProgram to error with GL_INVALID_OPERATION. I've tried this without it and it compiles and operates without the error. I'm hoping someone can point me in a direction i haven't looked yet at least because I can't see any reason this isn't working.

我解决了这个问题.最好的是,我可以告诉Android上的GLSL-ES不允许索引具有非常量变量的数组. GLSE-ES 2.0规范第97页10.25指出并非所有实现都直接支持它,在109页上指出可以将循环索引视为常量表达式,但并非必须如此.我推出了自己的循环,现在链接很好.

I solved this. As best I can tell the GLSL-ES on android doesn't allow indexing arrays with non-constant variables. GLSE-ES 2.0 specification page 97 10.25 states it's not directly supported by all implementations and on page 109 it states that loop indices can be considered constant-expressions but not must. I rolled out my loop and it's linking fine now.

谢谢大家的回复,感谢您的见解,我能够缩小范围.

Thank you everyone who responded, I was able to narrow this down thanks to your insight.

推荐答案

如果删除这些行会发生什么?

What happens if you remove these lines?

uniform float gaus[25];
uniform float offset[5];

高斯和偏移量不是统一的.它们在main()中分配了常量值.而且我不认为您应该使用与统一名称相同的名称声明变量.

gaus and offset are not uniforms. They are assigned constant values inside main(). And I don't think you should declare variables with the same names as uniforms.

我记得读过一篇文章,当编译着色器时,编译器确实擅长从着色器中剥离不必要的代码.当您省略该行时

I remember reading that when a shader is compiled, the compiler is really good at stripping unnecessary code from the shader. When you leave out the line

gl_FragColor = outSum; 

或分配

texture2D(s_texture, textureCoordinate) 

对于gl_FragColor,高斯和偏移量不用于计算gl_FragColor的最终值,因此有可能将其剥离,并且不会发生变量命名冲突.当您将outSum分配给gl_FragColor时,将使用高斯和偏移量来计算outSum,因此不会剥离它们,并且会发生命名冲突,从而导致错误.

to gl_FragColor, gaus and offset are not used to calculate the final value of gl_FragColor, so it is possible that they are being stripped out and the variable naming collisions don't occur. When you assign outSum to gl_FragColor, gaus and offset are used to calculate outSum, so they are not stripped and naming collisions occur, causing errors.

这篇关于写入gl_FragColor会导致glUseProgram抛出GL_INVALID_OPERATION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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