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
25 votes
Accepted

Why are normal maps blue and purple in color?

Only tangent space normal maps are primarily blue. This is because the colour blue represents a normal of (0,0,1) which would be an unchanged normal when the triangle lies in the plane x and y, i.e. ...
Syntac_'s user avatar
  • 561
15 votes

Why are normal maps blue and purple in color?

Because a normal map is covering vectors from -1 to 1 it makes sense to stretch this range into 0->1 so all of it can be fit inside the range of RGB. So usually we apply a transform on the normal to ...
PaulHK's user avatar
  • 2,322
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
10 votes
Accepted

How does this small fractal shader work?

Here is a line by line breakdown of my shader: ...
mathmasterzach's user avatar
9 votes
Accepted

Modern way of light sources in OpenGL?

Why are they deprecated? Those functions are deprecated because the OpenGL API moved in favor of a programmable pipeline in contrast with the old fixed pipeline. The programmable pipeline allows the ...
Samu's user avatar
  • 546
9 votes
Accepted

How can I offset/shrink a triangular polygon in GLSL?

Given triangle ▲ABC, we bisect angle ∠BAC with line AD, derived with Angle Bisector Theorem: BA / BD = CA / CD Point E represents our objective refined position on the desired resulting inset ...
Jack's user avatar
  • 341
9 votes

Why are oct trees so much more common than hash tables?

Lots of things here. "When reading papers". What papers? If the topic of the paper is about something other than the spatial partitioning structure, it could be fair to use whatever knowing that the ...
Angelo Pesce'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,880
7 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 ...
Nicol Bolas's user avatar
  • 9,997
7 votes
Accepted

What's the difference between Sample and tex2D

They represent the same operation, but Sample is what it's called in D3D10 and newer versions of HLSL, while tex2D is what it's ...
Nathan Reed's user avatar
  • 25.3k
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,997
7 votes
Accepted

Can i compile my shaders to SPIR-V without using glslangValidator?

Typically compiling GLSL to SPIRV would be something you do as part of the build process, so that you end up distributing SPIRV instead of GLSL. Then you don't need to compile it at runtime. ...
Jherico's user avatar
  • 556
6 votes
Accepted

Colorblending shader recompilation scenarios

In GL's model of the GPU, the colour blending step is performed by special-purpose hardware. This idea dates back to the fixed-function origins of GL, before programmable shaders were even a thing. ...
Dan Hulme's user avatar
  • 6,880
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

What actually happens in frame buffer when an image or document is scrolled?

In the olden days of text-mode terminals, the "frame buffer" was a buffer of text, not an image. Scrolling worked in the way you described. The buffer was larger than would fit on the screen, and to "...
Dan Hulme's user avatar
  • 6,880
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,997
6 votes
Accepted

Is a single shader and 1x1 pixel white sampler more efficient than frequent switching between shaders with and without samplers?

This is a really hardware-dependent question. But in general the best way to think about this is bottlenecks. You mentioned: one set of shaders, one slightly larger vertex struct and just uniform, ...
Calvin's user avatar
  • 506
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 ...
mdkdy's user avatar
  • 2,179
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 ...
Noah Witherspoon's user avatar
5 votes
Accepted

Derivative maps vs. Tangent Space Normal maps

After some researches and some answers from professionals here is my conclusion. Pros Don’t require tangents or binormals. Less interpolators. Only need two channels. less texture memory. Don’t ...
MaT's user avatar
  • 1,229
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.3k
5 votes
Accepted

Jagged texture's edge on perfectly straight UV of sphere mesh

It looks like distortion because the trapezoids are rendered as two triangles by the GPU. This leads to incorrect UV interpolation. Try this: draw a horizontally symmetrical trapezoid with the top ...
Olivier's user avatar
  • 1,585
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.3k
5 votes
Accepted

What is shader warming, and why does Direct3D seem to avoid it?

I hadn't heard the phrase 'shader warming' before, but from some googling around, it seems that some games/engines use this term for a method of forcing the shaders to be fully compiled during level ...
Nathan Reed's user avatar
  • 25.3k
5 votes
Accepted

Why are oct trees so much more common than hash tables?

My 2 cents from writting the Chipmunk2D physics engine is that spatial hashing is great when you have a lot of objects that are all the same size. I had a demo 10 years ago that ran with 20k ...
slembcke's user avatar
  • 166
5 votes
Accepted

Where should I learn shaders as a vulkan user?

Perhaps you should begin with classic opengl shaders in a more friendly enviroment. ShaderToy is a great community where you'll find tons of tutorials, code examples, masterpieces and great experts ...
David's user avatar
  • 301
5 votes

Why is OpenGL recompiling my depth map vertex shader based on the GL state?

This is an example of implementation details leaking through the abstraction. Parts of the GL state are often implemented via native GPU ISA instructions that the driver injects into user-defined ...
IneQuation's user avatar
5 votes

How do I efficiently calculate the distance to the edge of a shape?

A texture that stores distance from the edge of the shape, like you described, is called a "distance field" (you'll find lots of results if you google that phrase). Distance fields can be ...
Nathan Reed's user avatar
  • 25.3k

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