Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

gpu - In WebGL or OpenGL is it bad to use an output fragment variable as temp storage?

Ignoring coding patterns and code clarity/quality:

This is a question I just don't know if it's inherently bad or inconsequential. I can't find enough on the inner workings of assigning to say: gl_FragColor in WebGL 1.0 or to an out variable in WebGL 2 (layout(location = 0) out vec4 color).

Is there some inherent additional performance cost for doing something like:

void main() {
  gl_FragColor = vec4(0., 0., 0., 1.);
  vec4 val = gl_FragColor * something;
  ...
  gl_FragColor = val;
}

Or is it better to work entirely with interim declared variables and then assign to the out value a single time?

void main() {
  vec4 thing = vec4(0., 0., 0., 1.);
  vec4 val = thing * something;
  ...
  gl_FragColor = val;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm only guessing the answer is "no", there is no consequence. The driver can do whatever it wants/needs to get the correct answer. If gl_FragColor is special the driver can make its own temp. GPU Vendors compete for perf. Shaders are translated to the assembly language of the GPU itself so it's unlikely gl_FragColor is special except as a way to tell the compiler which value to actually output when it's all done computing.

I do it all the time. So does three.js as an example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...