Explanation of dFdx

To understand how these instructions work, it helps to understand the basic execution architecture of GPUs and how fragment programs map to that architecture. GPUs run a bunch of threads in ‘lock-step’ over the same program, which each thread having its own set of registers. So it fetches an instruction, then executes that instruction N … Read more

How does the fragment shader know what variable to use for the color of a pixel?

Furthermore, we have a predefined gl_FragColor. Let’s start with this. No, you don’t have the predefined gl_FragColor. That was removed from core OpenGL 3.1 and above. Unless you’re using compatibility (in which case, your 3.30 shaders should say #version 330 compatibility at the top), you should never use this. Now, back to user-defined fragment shader … Read more

Do conditional statements slow down shaders?

What is it about shaders that even potentially makes if statements performance problems? It has to do with how shaders get executed and where GPUs get their massive computing performance from. Separate shader invocations are usually executed in parallel, executing the same instructions at the same time. They’re simply executing them on different sets of … Read more

What’s the origin of this GLSL rand() one-liner?

Very interesting question! I am trying to figure this out while typing the answer 🙂 First an easy way to play with it: http://www.wolframalpha.com/input/?i=plot%28+mod%28+sin%28x*12.9898+%2B+y*78.233%29+*+43758.5453%2C1%29x%3D0..2%2C+y%3D0..2%29 Then let’s think about what we are trying to do here: For two input coordinates x,y we return a “random number”. Now this is not a random number though. It’s the … Read more

What is the correct file extension for GLSL shaders? [closed]

There’s no official extension in the spec. OpenGL doesn’t handle loading shaders from files; you just pass in the shader code as a string, so there’s no specific file format. However, glslang, Khronos’ reference GLSL compiler/validator, uses the following extensions to determine what type of shader that the file is for: .vert – a vertex … Read more

What is state-of-the-art for text rendering in OpenGL as of version 4.1? [closed]

Rendering outlines, unless you render only a dozen characters total, remains a “no go” due to the number of vertices needed per character to approximate curvature. Though there have been approaches to evaluate bezier curves in the pixel shader instead, these suffer from not being easily antialiased, which is trivial using a distance-map-textured quad, and … Read more

Random / noise functions for GLSL

For very simple pseudorandom-looking stuff, I use this oneliner that I found on the internet somewhere: float rand(vec2 co){ return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); } You can also generate a noise texture using whatever PRNG you like, then upload this in the normal fashion and sample the values in your shader; I can dig … Read more