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, <...
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 ...
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 ...
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 ...
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 ...
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 ...
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: ...
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. ...
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 ...
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 ...
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 ...
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) ...
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. ...
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 ...
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 ...
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 ...
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.
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), ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 <...
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 ...
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.
...
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 ...
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 ...
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.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
glsl × 267opengl × 137
shader × 72
texture × 20
vulkan × 19
c++ × 16
hlsl × 16
fragment-shader × 16
webgl × 15
raytracing × 12
rendering × 12
mathematics × 12
compute-shader × 11
lighting × 10
uv-mapping × 8
raymarching × 6
signed-distance-field × 6
transformations × 5
pbr × 5
performance × 5
tesselation × 5
three.js × 5
3d × 4
color × 4
sampling × 4