Hot answers tagged

34 votes
Accepted

How can I debug GLSL shaders?

As far as I know there are no tools that allows you to steps through code in a shader (also, in that case you would have to be able to select just a pixel/vertex you want to "debug", the ...
user avatar
  • 2,468
32 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, <...
user avatar
  • 2,037
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 ...
user avatar
  • 23.7k
23 votes
Accepted

Sharing code between multiple GLSL shaders

There's a bunch of a approaches, but none is perfect. It's possible to share code by using glAttachShader to combine shaders, but this doesn't make it possible to ...
user avatar
16 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 ...
user avatar
  • 2,234
16 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 ...
user avatar
  • 405
15 votes
Accepted

When to disable perspective correct interpolation ( noperspective )

Use cases are only limited by your imagination! noperspective means that the attribute is interpolated across the triangle as though the triangle was completely ...
user avatar
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 ...
user avatar
12 votes
Accepted

Why is accessing textures much slower when calculating the texture coordinate in the fragment shader?

What you're talking about is commonly called "dependent texture reads" in the mobile development community. It's an implementation detail of certain hardware, and therefore it really depends on the ...
user avatar
  • 556
12 votes
Accepted

Do I need to rebind uniforms or attributes when changing shader programs?

You don't need to rebind the attributes, so long as you ensure that their location stays the same in both shaders. (Usually using the layout(location = X) syntax in ...
user avatar
  • 1,100
11 votes

Sharing code between multiple GLSL shaders

I generally just use the fact that glShaderSource(...) accepts an array of strings as it's input. I use a json-based shader definition file, which specifies how a shader (or a program to be more ...
user avatar
11 votes

How can I debug GLSL shaders?

There is also GLSL-Debugger. It is a debugger used to be known as "GLSL Devil". The Debugger itself is super handy not only for GLSL code, but for OpenGL itself as well. You have the ability to jump ...
user avatar
  • 241
11 votes
Accepted

Shadow Mapping artefacts

This issue looks like standard shadow map acne artifacts. Additionally your's lighting equation is incomplete or wrong. Light shouldn't influence faces with normals facing away from it. This also ...
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 ...
user avatar
  • 1,575
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: ...
user avatar
  • 2,037
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 ...
user avatar
9 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 ...
user avatar
  • 23.7k
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. ...
user avatar
  • 6,440
7 votes

What factors affect which shader language to learn?

The shader language is bound to the APIs/engines that support it (glsl to openGL & WebGL and hlsl to D3D). There are tools to translate from one to the other but they aren't perfect. This means ...
user avatar
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 ...
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. ...
user avatar
  • 161
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 ...
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 ...
user avatar
  • 311
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 ...
user avatar
  • 8,159
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) ...
user avatar
6 votes
Accepted

Loop performance in a shader

Shader compilers are extremely aggressive about unrolling since early HW often didn't have flow control and the cost on more recent HW can vary. If you have a benchmark you are actively testing ...
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 ...
user avatar
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 ...
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 ...
user avatar
  • 3,941
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. ...
user avatar

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