Skip to main content
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: ...
Stephane Hockenhull's user avatar
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 ...
DMGregory's user avatar
  • 135k
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 ...
DMGregory's user avatar
  • 135k
15 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: ...
kolenda's user avatar
  • 1,370
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 ...
DMGregory's user avatar
  • 135k
15 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 ...
Rabbid76's user avatar
  • 301
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 ...
Gaxil's user avatar
  • 141
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. ...
Bálint's user avatar
  • 14.9k
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 ...
Bálint's user avatar
  • 14.9k
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 ...
Trevor Powell's user avatar
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 ...
Elva's user avatar
  • 3,223
8 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 ...
GettingShwifty's user avatar
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 ...
Adam's user avatar
  • 7,384
8 votes
Accepted

How do I avoid using the wrong texture2D function in GLSL?

You could use a macro: ...
James Lambert's user avatar
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 ...
Greffin28's user avatar
  • 999
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 ...
rutter's user avatar
  • 746
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, ...
Adam's user avatar
  • 7,384
7 votes
Accepted

Screen tearing in fragment shader on R9 380 GPUs

I came back to this today and after some more investigation and trial and error I discovered that the culprit was targetTexture. With some more investigation I learned that reading and writing to the ...
jmcmorris's user avatar
  • 151
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 ...
Kromster's user avatar
  • 10.7k
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 ...
Bálint's user avatar
  • 14.9k
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 ...
Krupip's user avatar
  • 1,763
6 votes

Optimized linear to sRGB GLSL

Adjusted version of Tynach's answer that leaves the alpha channel alone, since it's already linear. ...
Pieter-Jan Briers's user avatar
6 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 ...
Philipp's user avatar
  • 120k
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, ...
Tynach's user avatar
  • 151
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 ...
DMGregory's user avatar
  • 135k
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: ...
Lasse's user avatar
  • 3,241
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 ...
J22o's user avatar
  • 482
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 ...
Bálint's user avatar
  • 14.9k
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 ...
Bálint's user avatar
  • 14.9k
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 ...
user1118321's user avatar
  • 2,632

Only top scored, non community-wiki answers of a minimum length are eligible