Hot answers tagged

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,119
24 votes
Accepted

Why does my Perlin Noise look "blocky"?

The interpolation looks fine. The main problem here is that the hash function you're using isn't very good. If I look at just one octave, and visualize the hash result by outputting ...
Nathan Reed's user avatar
  • 24.8k
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,312
19 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
  • 455
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
  • 24.8k
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,119
9 votes

How can I debug GLSL shaders?

There are several offerings by GPU vendors like AMD's CodeXL or NVIDIA's nSight/Linux GFX Debugger which allow stepping through shaders but are tied to the respective vendor's hardware. Let me note ...
Nobody moving away from SE's user avatar
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,700
7 votes
Accepted

Why does substracting -1 in shader fail for a float data type?

Without seeing the error message I can't be sure but I think it's failing on the 1 being int instead of a float. float inv_coord = v_coord - 1.0; There is a ...
ratchet freak's user avatar
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
  • 311
7 votes

Flat shading for non-planar polygons

Without knowing the internals of Blender, I would say it uses shared normals for the quads, splitting the normals only on edges between the quads, not between the triangles. So your idea of uploading ...
Mikkel Gjoel's user avatar
7 votes
Accepted

WebGL Omnidirectional Shadow Mapping Issue

SOLUTION After a couple of days I realized that I was computing my projection matrix using a FOV angle in degrees and It should be in radians. I made the conversion and now everything works great. ...
czapata91's user avatar
  • 171
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,397
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,697
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
Accepted

How to convert a thread ID into Screen Space Coord in an OpenGL Compute Shader?

The problem is actually in your fragment shader: color_out = texture(source_image, gl_FragCoord.xy); The texture() function accepts normalized coordinates which ...
Jason Allen's user avatar
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,171
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

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,119
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
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,700
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,697
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,697
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,397
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,697
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
  • 24.8k
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,401

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