44
votes
Accepted
Does == cause branching in GLSL?
What causes branching in GLSL depends on the GPU model and OpenGL driver version.
Most GPUs seem to have a form of "select one of two values" operation that has no branching cost:
...
41
votes
Accepted
How to do color post processing in WebGL, when you can not read the color of the current pixel?
This is not a WebGL 2 restriction. This is fundamentally how GPUs and their 3D rendering APIs all work.
Because they're designed to render many triangles and fragments in parallel, they structurally ...
31
votes
Accepted
What are screen space derivatives and when would I use them?
First, it helps to know that GPUs always evaluate fragment/pixel shaders on 2x2 blocks of pixels at a time. (Even if only some of those pixels ultimately need to be drawn, while others are outside the ...
16
votes
Accepted
How to improve this shader's performance?
At first you should pre-compute as much data as you can and avoid computing the same values for every pixel.
You have such a fragment:
...
16
votes
Accepted
What can I do with the 4th component of gl_Position?
gl_Position is a Homogeneous coordinates. Homogeneous coordinates are needed for perspective projection.
Note, if a vector ...
15
votes
Accepted
How do I render 2D top-down tiled, directed water flow?
I didn't have any tiles handy that looked good with distortion, so here's a version of the effect I mocked up with these Kenney tiles instead:
I'm using a flowmap like this, where red = rightward ...
14
votes
How can I achieve simulated fluid surface deformation in a bottle?
I've been redirected here after posting this on twitter : https://twitter.com/Gaxil/status/1268165270541078529
I can give you some details of my implementation, even if I know that it's a quite ...
13
votes
What can I do with the 4th component of gl_Position?
It's not particularly useful for you but you should definitely leave it at 1. It's used when applying transformations to the model.
Matrix multiplication can only multiply numbers, it can't add. ...
12
votes
Accepted
How can I round out the corners of a quad in OpenGL?
This is not technically an answer to your question, but is a better work around in my opinion.
You can pass the dimensions of the boxes and the radius of the corners to the fragment shaders and round ...
12
votes
How do I implement flat shading in GLSL?
As @rutter says, your #1 option is the correct thing to do in the overwhelming majority of all cases. However, if you're really adamant that you want everything to be hard-edged, then you can do this ...
9
votes
Confused About My Code Suggesting The Normal Matrix Is Equivalent To The ModelView Matrix
When a matrix is orthogonal, inverse and transpose are equivalent making an inverse transpose equal to the original matrix. So if your model view matrix is orthogonal, the normal matrix will be equal ...
9
votes
Accepted
Why do we multiply perspective * modelview * point?
Yes, it isn't what we want to do. Matrix multiplication is not communative, which is to say that switching the order produces different things.
Think of it this way, if you do these operations:
Turn ...
8
votes
Random black pixels GLSL shader bug
Unexpectedly black pixels sometimes indicate that you've got an infinity or NaN in the shader somewhere. For example normalize(vec3(0,0,0)) will generate a NaN. To me, the most obvious candidate in ...
8
votes
Accepted
8
votes
Accepted
Why does Unity use HLSL instead of GLSL?
As far as I know, Unity uses Cg (which is deprecated by NVIDIA since 2012, I have no idea why they still use it) as its shader language (which is really similar to HLSL) instead of HLSL or GLSL as ...
8
votes
Accepted
How do I implement flat shading in GLSL?
You may have heard 3D modelers talk about "hard edges" or "sharp edges", which are roughly equivalent to what you're looking for here. When modelers create a hard edge, their software will internally ...
8
votes
It's possible to use more than 4 channels of a texture in GLSL?
The maximum for a single texture is four channels in every GPU rendering API that I know.
While it's technically possible to encode more data into those four channels (e.g. storing eight 4-bit values, ...
7
votes
Accepted
Can I make color data not render as gradient?
You can try doing this with flat attribute qualifier in shaders, like so:
flat vec3 surfColor;
It tells GLSL to pass values ...
7
votes
How do I avoid using the wrong texture2D function in GLSL?
Your home PC probably has a higher shader version than the school PC, because the GPU is older. Older glsl versions used the texture2d function, while newer ones ...
7
votes
Does == cause branching in GLSL?
@Stephane Hockenhull's answer pretty much gives you what you need to know, its going to be entirely hardware dependent.
But let me give you some examples of how it can be hardware dependent, and why ...
7
votes
Accepted
Any fast alternative to sine in GLSL?
ATI made an announcement that the sine / cosine instructions in their newest GPUs now requires only a single clock cycle... in 2004! Modern GPUs in fact have special function units specifically for ...
6
votes
Optimized linear to sRGB GLSL
Adjusted version of Tynach's answer that leaves the alpha channel alone, since it's already linear.
...
5
votes
Optimized linear to sRGB GLSL
A few days ago, I ran into this when messing around on Shadertoy. I was curious and some solutions to the various problems I faced, so I may as well post what I've made here for you.
Note, however, ...
5
votes
Accepted
Single pass box blur is not giving same result as double pass box blur
I can see this question is old, but the answer is kind of trivial. Omitting the initialization in a nested loop is not equivalent to omitting it when it's the topmost level of the loop.
Let's walk ...
5
votes
Accepted
Calculating the weights of matrices/bones in opengl/glsl
You need to add together all the end results of each weighted bone for the vertex. The weights should add up to 1 in total for the weights to be correct. So basically:
...
5
votes
Calculating vertex normals in OpenGL C++
Finding the vertex normal is actual really easy. Lets say that you want to find to normal for the vertex i (i is a 2D/3D vector), where i1 is the vertex after i and i2 is the vertex before i, this is ...
5
votes
Accepted
What technique should I use in a shader to give this reflective effect?
Well, a good approach would be to create a bit mask, where the time a specific pixel should be fully lit is indicated by the amount of red it contains. So for example a pixel would be completely ...
5
votes
Accepted
Is there an HLSL equivalent to GLSL's "map" function?
Answer with best guess of what map could be
Based on the link you posted, map should be provided by you and it should return the distance of the provided 3d ...
5
votes
Does == cause branching in GLSL?
I concur with everything said in @Stephane Hockenhull's answer. To expand on the last point:
You never know in advance how a specific GLSL compiler or a specific GPU will perform until you ...
5
votes
Accepted
Why is clip-space 3 dimensional?
There are a few reasons it's valuable to retain a third dimension of information.
Depth Buffering
To get near content to show up in front of far content, we need to retain some information about what ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
glsl × 956opengl × 600
shaders × 314
c++ × 108
fragment-shader × 87
textures × 57
lighting × 54
hlsl × 52
opengl-es × 41
opengl-es2 × 37
java × 33
lwjgl × 32
webgl × 29
shadows × 29
rendering × 28
shadow-mapping × 28
deferred-rendering × 25
2d × 22
libgdx × 22
graphics × 22
directx × 21
mathematics × 19
graphics-programming × 19
unity × 18
3d × 18