javascript - Workaround to use uniforms (or similar) in WebGL for loops? - Stack Overflow

I'm working on implementing a fragment shader in WebGL, and came across the limitation of being ab

I'm working on implementing a fragment shader in WebGL, and came across the limitation of being able to only use constant expressions in for loops. Does anyone have any suitable workarounds for this?

In my specific case, I'm implementing a bilateral filter, and currently have a window size specified as a const in my fragment shader, but would like to be able to change this from JavaScript. Uniforms aren't considered constants and thus can't be used in a for loop, so I'm looking for some other way of implementing this.

The only thing I can think of is to read the shader source from JavaScript, parse it and replace the value of the const with the desired window size, then repile the shader. This would work for my purpose, but I'd like to know if there's an easier way.

I'm working on implementing a fragment shader in WebGL, and came across the limitation of being able to only use constant expressions in for loops. Does anyone have any suitable workarounds for this?

In my specific case, I'm implementing a bilateral filter, and currently have a window size specified as a const in my fragment shader, but would like to be able to change this from JavaScript. Uniforms aren't considered constants and thus can't be used in a for loop, so I'm looking for some other way of implementing this.

The only thing I can think of is to read the shader source from JavaScript, parse it and replace the value of the const with the desired window size, then repile the shader. This would work for my purpose, but I'd like to know if there's an easier way.

Share Improve this question asked Sep 11, 2015 at 17:38 TJ PottsTJ Potts 631 silver badge6 bronze badges 2
  • Your best bet imo is to use js to generate the shader on the fly as you said. Its very easy to do, as js already have good support for re and es6 has template strings. I am not familiar with bilateral filter but is it basically a convolution effect? – WacławJasper Commented Sep 12, 2015 at 3:44
  • Thanks, I haven't gotten into ES6 yet, so wasn't aware of template strings. Wikipedia has a decent page on the filter, if you're familiar with some image processing theory. The method of calculation is fairly similar to the brute-force method of performing convolution filters in the time domain, but unfortunately the bilateral filter is nonlinear meaning most of the optimizations you can do with convolution filters will result only in an approximation of the actual output of the filter at best – TJ Potts Commented Sep 13, 2015 at 18:04
Add a ment  | 

1 Answer 1

Reset to default 8

If you want/need the ability to dynamically change your loop length you may use a for loop counting to a very large number / infinity and break once your limit is reached:

uniform int loopLimit;

for (int i = 0; i < 10000; i++) {
  if (i == loopLimit) break;
  // Do your stuff
}  

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647790a4638075.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信