Skip to main content
35 votes
Accepted

GLSL. Can someone explain why gl_FragCoord.xy / screenSize is performed and for what reason?

First, gl_FragCoord.xy are screen space coordinates of current pixel based on viewport size. So if viewport size is width=5, <...
mdkdy's user avatar
  • 2,179
21 votes
Accepted

Dynamic Array in GLSL

I don't think uniform arrays can be dynamically sized. In your case you should define the array as the maximum number of lights you will process and then use a uniform to control the number of ...
PaulHK's user avatar
  • 2,322
21 votes
Accepted

How to use GLSL texelFetch?

OK, I have figured out this problem. For the texture coordinates, there are two kind of texture coordinates. One is normalized texture coordinates, which is in the range [0, 1]. The other is texel ...
yuchen's user avatar
  • 475
13 votes
Accepted

Double precision in shader

The problem is, glVertexAttribPointer(0, 4, GL_DOUBLE... doesn't do what you think it does. Using actual double-precision vertex attributes and performing double ...
Christian Rau's user avatar
11 votes

Confused with PBR implementations

DFG pops up in the family of microfacet based BRDFs. It is simply the product of three terms: D : The microfacet distribution. F : The fresnel coefficient. G : The geometric attenuation between ...
Olivier's user avatar
  • 1,585
10 votes
Accepted

OpenGL specular shading - Gradient banding issues

This is a common problem with very subtle gradients. The problem is that the image is displayed with 8 bits per component and this is not quite enough precision to make a gradient that is smooth to ...
Nathan Reed's user avatar
  • 25.2k
10 votes
Accepted

Getting rid of branching to check if a variable is within a range in GLSL

This could be done that way: First condition is float val = 1.0-step(shouldY, ActualY); Then, the condition: ...
mdkdy's user avatar
  • 2,179
8 votes
Accepted

What are Jittering and Dithering

Jittering and dithering are both techniques of adding noise to reduce visible artefacts (such as banding) in an image. They solve different kinds of artefacts so they are used in different situations. ...
Dan Hulme's user avatar
  • 6,870
7 votes

Is there a objective reason for matrix naming conventions?

I think the naming order is intuitive because it is in reading order (left to right), e.g., worldViewProjection means that your point/direction is first multiplied by the world matrix, then the view ...
vgs's user avatar
  • 321
7 votes
Accepted

Why do we multiply vertex from left side in vertex shader with matrices?

There is no 100% consensus on what order matrix multiplication should model things, worse the industry is split along this. Some sources use row major and some sources use column major matrices. Great ...
joojaa's user avatar
  • 8,447
7 votes
Accepted

Shader storage buffer indexing by 4 bytes instead of 3?

Does anyone ahve[sic] any idea of where to start looking You fell victim to one of the classic blunders. Never use a vec3 in a UBO or SSBO. While ...
Nicol Bolas's user avatar
  • 9,932
7 votes
Accepted

Is a branched discard to be avoided

You can use a stencil instead, This allows the gpu to do an early out test before the fragment shader even starts. You only have to build the stencil once (possibly with a discard in a simple shader) ...
ratchet freak's user avatar
6 votes

What is the difference between a Sampler and an Image variables in GLSL?

Image and Samplers differ in many ways. Unlike Samplers, images can only be addressed with an integer coordinate that ranges from (0,0) to (x,y), where x and y are the width and height of the image. ...
Fred Garnier's user avatar
6 votes

What is the difference between a Sampler and an Image variables in GLSL?

Images creation is the same as texture, but it must be binded as image to image units. These are equivalent of "texture units". With them you can arbitrarily perform read/write/modify operations using ...
mdkdy's user avatar
  • 2,179
6 votes

Creating an "ink spread" effect with a glsl fragment shader

If you want to implement this with GLSL, the general concept that could be applied is multi-pass rendering. In a first render pass, draw some ink on the screen and write the result in a frame buffer ...
NodeCode's user avatar
  • 341
6 votes

I dont understand how a GPU computes and branches, could you please help me fix this shader preformance

Rather than trying to speed up your shader by fiddling with the code, can I suggest you make use of the GPU's inherent ability to rapidly fill polygons? This will allow it to eliminate the vast bulk ...
Simon F's user avatar
  • 4,306
6 votes

GLSL. Can someone explain why gl_FragCoord.xy / screenSize is performed and for what reason?

Refer to narthex's excellent answer for details and example, but simply put, If you want to map the pixel coordinate values to the range 0 to 1 , you divide by viewPortSize.
WizardOfRobots's user avatar
6 votes
Accepted

GLSL shapes signed distance field implementation explanation?

Torus A torus is defined by two parameters: the major radius, and the minor radius. The major radius (t.x) is the radius of the big ring (in red in the diagram), ...
Dan Hulme's user avatar
  • 6,870
6 votes

How to access several textures from huge amount of textures in a shader?

Well, it's unlikely that 10,000 cubemaps could fit into memory at all (at 128x128x4-bytes-per-pixel, 10,000 cubemaps would require upwards of 4GB of RAM). But as far as the mechanism to use a large ...
Nicol Bolas's user avatar
  • 9,932
6 votes
Accepted

Clarifying vulkan glsl std140

The block should total up to 8320 bytes. No, it shouldn't. You use a uint[16] array. Well, std140 layout makes it clear that the array stride for any array is ...
Nicol Bolas's user avatar
  • 9,932
5 votes
Accepted

Texture coordinates are not updating according to the vertex divisor specified

Now this texture coordinates supposed to be updated for each vertex processed by vertex shader because i set the glVertexAttribDivisor() to 0 which means it should be. No, it doesn't. A divisor of 0 ...
Nicol Bolas's user avatar
  • 9,932
5 votes

Is there a objective reason for matrix naming conventions?

The order is arbitrary, but if you want to be compatible with physics textbooks then your notation is mostly set. The difference is that you seem to think that its more natural to observe the systen ...
joojaa's user avatar
  • 8,447
5 votes
Accepted

Strange behaviour in vertex shader with divisions

The vertex shader only runs per vertex, not for every point in the square. So the four vertices are mapped to (±2, ±2), and then the GPU draws a polygon between those vertices, which does cross the ...
Nathan Reed's user avatar
  • 25.2k
5 votes
Accepted

GLSL function parameters

The GLSL Specification in section 6.1.1 "Function Calling Conventions" states: The keyword in is used as a qualifier to denote a parameter is to be copied in, but not copied out. The ...
user1118321's user avatar
  • 3,441
5 votes

Why do we multiply vertex from left side in vertex shader with matrices?

OpenGL uses column-major matrices. For example, the translation values will be in the last row rather than the last column of the matrix. For example when loading matrices into uniforms in glsl, the <...
user1118321's user avatar
  • 3,441
5 votes
Accepted

Tesselation sine distortion?

Not enough triangles. The situation is analogous to the sampling theorem that states that you can not reconstruct a signal if your sample frequency is below a certain threshold. Although in this, case ...
joojaa's user avatar
  • 8,447
5 votes

Is sub-texel shading possible?

The GPU hardware only supports nearest-neighbor, bilinear, trilinear, and anisotropic texture filtering. However, nothing stops you from implementing your own texture filtering in the pixel shader. ...
Nathan Reed's user avatar
  • 25.2k
5 votes
Accepted

Difference between isnan and comparison to self

IEEE-754 defines the isNaN property, which determines if a value is NaN. However, it is also defined to be a non-signaling operation; calling ...
Nicol Bolas's user avatar
  • 9,932
5 votes
Accepted

Understanding fragment shader coordinate system

Everything in this code functions as intended, until I try to translate the entire grid (grid lines) to the center of the screen by adding v_Resolution.x / 2 to uv.x. Not sure what your intention is ...
wychmaster's user avatar
  • 1,251
5 votes
Accepted

Why does GLSL smoothstep return 0 when all parameters are literal floats?

Undefined behavior is undefined. smoothstep requires that the first argument is less than the second. If that is not the case, undefined behavior results.
Nicol Bolas's user avatar
  • 9,932

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