glsl - WebGL:循环索引不能与非常量表达式进行比较

标签 glsl webgl

我有一个 webgl 模糊着色器:

precision mediump float;
precision mediump int;

uniform sampler2D u_image;
uniform float blur;       
uniform int u_horizontalpass; // 0 or 1 to indicate vertical or horizontal pass
uniform float sigma;        // The sigma value for the gaussian function: higher value means more blur
                            // A good value for 9x9 is around 3 to 5
                            // A good value for 7x7 is around 2.5 to 4
                            // A good value for 5x5 is around 2 to 3.5
                            // ... play around with this based on what you need :)

varying vec4 v_texCoord;

const vec2 texOffset = vec2(1.0, 1.0);
// uniform vec2 texOffset;
const float PI = 3.14159265;

void main() {  
  vec2 p = v_texCoord.st;
  float numBlurPixelsPerSide = blur / 2.0; 

  // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  vec3 incrementalGaussian;
  incrementalGaussian.x = 1.0 / (sqrt(2.0 * PI) * sigma);
  incrementalGaussian.y = exp(-0.5 / (sigma * sigma));
  incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;

  vec4 avgValue = vec4(0.0, 0.0, 0.0, 0.0);
  float coefficientSum = 0.0;

  // Take the central sample first...
  avgValue += texture2D(u_image, p) * incrementalGaussian.x;
  coefficientSum += incrementalGaussian.x;
  incrementalGaussian.xy *= incrementalGaussian.yz;

  // Go through the remaining 8 vertical samples (4 on each side of the center)
  for (float i = 1.0; i <= numBlurPixelsPerSide; i += 1.0) { 
    avgValue += texture2D(u_image, p - i * texOffset) * incrementalGaussian.x;         
    avgValue += texture2D(u_image, p + i * texOffset) * incrementalGaussian.x;         
    coefficientSum += 2.0 * incrementalGaussian.x;
    incrementalGaussian.xy *= incrementalGaussian.yz;
  }

  gl_FragColor = avgValue / coefficientSum;
}

当我构建时,我收到以下错误消息:

webgl-renderer.js?2eb3:137 Uncaught could not compile shader:ERROR: 0:38: 'i' : Loop index cannot be compared with non-constant expression



我还尝试仅使用统一的 float 模糊来比较 i 。有没有什么办法解决这一问题?

问题在这里更详细:https://www.khronos.org/webgl/public-mailing-list/archives/1012/msg00063.php

我发现环顾四周的解决方案是在比较循环变量时仅使用常量表达式。这不符合我需要做的事情,这是根据模糊半径改变我循环的次数。

对此有何想法?

最佳答案

尝试这样的事情:

const float MAX_ITERATIONS = 100.0;

// Go through the remaining 8 vertical samples (4 on each side of the center)
for (float i = 1.0; i <= MAX_ITERATIONS; i += 1.0) { 
    if (i >= numBlurPixelsPerSide){break;}
    avgValue += texture2D(u_image, p - i * texOffset) * incrementalGaussian.x;         
    avgValue += texture2D(u_image, p + i * texOffset) * incrementalGaussian.x;         
    coefficientSum += 2.0 * incrementalGaussian.x;
    incrementalGaussian.xy *= incrementalGaussian.yz;
}

关于glsl - WebGL:循环索引不能与非常量表达式进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38986208/

相关文章:

opengl - 绘制抗锯齿粗镶嵌曲线

python - opengl中有没有办法循环旋转二维线

java - OpenGL-GLSL纹理()调用: 1282 Invalid Operation

c++ - QtVirtualKeyboard 使用平台 WebGL 卡住 QML 应用程序,但桌面工作正常。如何解冻或解决它?

opengl - 为什么 GLSL 更改为 in/out?

控制 Mandelbrot 集的放大位置

java - 将 JME 移植到 WebGL

javascript - WebGL 大纹理导致 Chrome 中隐藏的停顿

matrix - 对象旋转时法线旋转错误

text - 在 WebGL 中渲染带符号距离场的文本