13 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 ...
  • 566
11 votes
Accepted

Performance of Compute Shaders vs. Fragment Shaders for Deferred Rendering

After more analysis, the TL;DR here is that, yes, the slowdown is due to memory locality, and yes the pixel order is to blame. More interestingly, by writing the shader differently, we can greatly ...
  • 1,800
9 votes

Using a LUT to speed up a trig heavy shader for mobile devices

It has been suggested in comments repeatedly, but noone felt the need to give a proper answer, so for the sake of completeness, a straight-forward and common solution to this problem might be to use a ...
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 ...
6 votes
Accepted

How to ensure a fragment shader only runs once

I don't think your approach is viable for a variety of reasons. a GPU is heavily cache-dependent for performance. Pulling the same pixels over and over will just measure how fast a cache hit is. ...
6 votes
Accepted

Fragment shader's output variables

It works almost exactly the same way as vertex shader inputs. In vertex shaders, you have inputs. And these inputs represent vertex attribute indices, the numbers you pass to ...
  • 8,574
5 votes

Advice on how to create GLSL 2D soft smoke/cloud shader

There is good glsl source of noise (simplex noise) online to make real time noise. In addition to this, to make effect of moving fog/smoke like in this video you can make 3D FBM function. This is my ...
  • 2,047
5 votes
Accepted

Advice on how to create GLSL 2D soft smoke/cloud shader

Noise functions are definitely your friend here—FBM would be one good candidate. You’re right that it can look too uniform on its own, but if you blend multiple layers of it together, using different ...
4 votes
Accepted

Ray tracing - partial derivatives for texture lookup

Yes, ray differentials are the way to go. The Paper by Igehy introduces them for the use case of filtered texture lookups. When generating the primary rays, you initialise the differentials to reflect ...
4 votes
Accepted

Z buffer working in OpenGL

When depth testing is enabled, the testing occurs after the fragment shader executes. The procedure is relatively simple (and spelled out pretty well here): Get depth of fragment Test against depth ...
  • 1,323
4 votes

Will the gaussian kernels in this fragment shader be computed for every fragment?

It depends. On a desktop, if the Gaussian function is always called with the same arguments, the shader compiler will probably optimize it so it's evaluated only ...
4 votes
Accepted

Curve rotation in GLSL fragment shader

You are doing completely different things in Matlab than in your Fragment Shaders. In Matlab you are generating bunch of points that can be drawn anywhere on the screen and then you are transforming ...
  • 56
4 votes
Accepted

Help me understand the voronoi shader

1) Scale the texture coordinates from [0,1]x[0,1] -> [0,3]x[0,3]. It works with higher integer scales as well. This creates an imaginary 3x3 tiles grid. st *= 3.; ...
  • 438
3 votes
Accepted

SDF rendering for large world

You tile idea seems to be a good one to cut down on local complexity. You can't reasonably evaluate every primitive everywhere. Not if you want good performance anyway. So you need a simplified ...
  • 1,575
3 votes

gl_FragCoord and glViewport

since that is the case for window-space coordinates in the vertex stage. There are no "window-space coordinates in the vertex stage". Or at least, not in the vertex shader. Window-space is ...
  • 8,574
3 votes
Accepted

Count number of green fragments that are within a given distance from each screen fragment

You're getting a cross shape in the output because in your loop, you're counting pixels on the center row and column multiple times. For example the pixel at (0, 0) offset will be counted on every ...
3 votes
Accepted

OpenGL ES Frag Shader - Branching

I think the most efficient way might be either return float(shadowValue > shadowTexZ); or ...
  • 1,416
2 votes

Is it possible to override or disable blending per pixel

You seem to be using additive blending against its purpose. Additive blending is supposed to represent light from multiple sources being combined. It is not physically possible for one source of light ...
  • 8,574
2 votes

Will the gaussian kernels in this fragment shader be computed for every fragment?

The shader compiler will very likely perform common-subexpression consolidation, and compute that function once, and reuse it each time it appears in the shader. You might think that, since ...
2 votes
Accepted

How to correctly implement Lambertian BRDF with point light

It doesn’t matter that it’s a point light; the 1/π normalization factor is what’s causing your surface to be 1/π as bright as you’d like it to be. You can either up your light intensity so the maximum ...
2 votes
Accepted

Shader won't work

If you are using Processing the variable lightPosition must be declared as a vec4, otherwise it won't be passed in, and will be ...
  • 218
2 votes
Accepted

Alpha component in color buffer affects RGB components?

It looks like you're drawing with pre-multiplied alpha. This means that the red, green, and blue channels have been multiplied by the alpha channel. So your shader should look more like this: ...
  • 3,331
2 votes

Interpolation in Graphics Pipeline

The rasterizer determines, which pixel is rendered depending on vertex positions that come out of the vertex shader. For each rasterized pixel, the rasterizer knows where it lies inside the triangle ...
  • 1,175
2 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 ...
  • 1,175
1 vote

Can I write histogram equalization, blurring, sharpening grayscale image etc.. as shader programs with openGL?

Yes you can. There are basically two approaches: You draw everything to a framebuffer object. Then you apply image processing fragment shaders to resulting texture of the previous draw call, where ...
  • 441
1 vote

What is the most efficient line algorithm using a shader program

Here is a (mostly) minimal version of an SDF for a line segment using a signed distance field as suggested in the comments. I take no credit for this code, it is your function pieced together with IQ'...
  • 2,257
1 vote
Accepted

How to pass only the needed portion of the data calculated in a fragment shader to another fragment shader?

I'm assuming that you don't know how many objects there will be or have any way to index them until the image processing operations are completed, i.e. you have some final pass that determines for ...
1 vote

Shader cannot move texture x, y location

What you do is shifting the texture coordinates. This will move the texture on your polygon, and depending on your settings it will handle the borders which are now probably wrong. however, this will ...
  • 1,467
1 vote

gaussFactorForOffset cannot call a non-function how to fixed this code?

It would be helpful if you posted some more information about what you're trying to do (rather than problem links) and some screenshots of what is actually happening. Being here I basically see "My ...
  • 1,467
1 vote

Writing data to a shader resource at runtime in DX12

So, I figured it out. While UAV will be necessary when I start manipulating data in Compute Shaders, for the time being, SRV works fine provided the resource is read-only from the fragment shader. ...

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