最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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
}  
Post a comment

comment list (0)

  1. No comments so far