Questions tagged [fragment-shader]

A GPU program used in rendering. It is executed for each sample taken from a rasterized primitive. The output of this process is a number of values and a floating-point depth.

Resources

1506 questions
162
votes
5 answers

Vertex shader vs Fragment Shader

I've read some tutorials regarding Cg, yet one thing is not quite clear to me. What exactly is the difference between vertex and fragment shaders? And for what situations is one better suited than the other?
adivasile
  • 2,377
  • 3
  • 24
  • 32
75
votes
1 answer

What does `precision mediump float` mean?

In the learningwebgl tutorial1 I've found an interesting line in the fragment shader. precision mediump float; I've found an article about it here, but I still can't understand what does it mean? And if I remove this line, nothing changes.…
Hard Rain
  • 1,380
  • 4
  • 14
  • 19
71
votes
5 answers

WebGL/GLSL - How does a ShaderToy work?

I've been knocking around Shadertoy - https://www.shadertoy.com/ - recently, in an effort to learn more about OpenGL and GLSL in particular. From what I understand so far, the OpenGL user first has to prepare all the geometry to be used and…
Charlie
  • 4,197
  • 5
  • 42
  • 59
42
votes
2 answers

How exactly does OpenGL do perspectively correct linear interpolation?

If linear interpolation happens during the rasterization stage in the OpenGL pipeline, and the vertices have already been transformed to screen-space, where does the depth information used for perspectively correct interpolation come from? Can…
AIGuy110
  • 1,025
  • 1
  • 10
  • 11
38
votes
7 answers

How do I get the current color of a fragment?

I'm trying to wrap my head around shaders in GLSL, and I've found some useful resources and tutorials, but I keep running into a wall for something that ought to be fundamental and trivial: how does my fragment shader retrieve the color of the…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
34
votes
1 answer

Custom Texture Shader in Three.js

I'm just looking to create a very simple Fragment Shader that draws a specified texture to the mesh. I've looked at a handful of custom fragment shaders that accomplished the same and built my own shaders and supporting JS code around it. However,…
rrowland
  • 2,734
  • 2
  • 17
  • 32
31
votes
2 answers

Why is writing to a buffer from within a fragment shader disallowed in Metal?

As stated in the Metal Shading Language Guide: Writes to a buffer or a texture are disallowed from a fragment function. I understand that this is the case, but I'm curious as to why. Being able to write to a buffer from within a fragment shader is…
lcmylin
  • 2,552
  • 2
  • 19
  • 31
30
votes
1 answer

Uniform versus attributes in GLSL ES

I have some parameters being passed from CPU to GPU that are constant for all fragments but which change on every frame (I'm using GLSL ES 1.1). Should I use uniforms or attributes for such values? Attributes can vary from vertex to vertex so my…
Alex Flint
  • 6,040
  • 8
  • 41
  • 80
28
votes
4 answers

GLSL: How to get pixel x,y,z world position?

I want to adjust the colors depending on which xyz position they are in the world. I tried this in my fragment shader: varying vec4 verpos; void main(){ vec4 c; c.x = verpos.x; c.y = verpos.y; c.z = verpos.z; c.w = 1.0; …
Rookie
  • 1,242
  • 3
  • 17
  • 22
26
votes
1 answer

Drawing a border on a 2d polygon with a fragment shader

I have some simple polygons (fewer than 20 vertices) rendering flat on a simple xy plane, using GL_TRIANGLES and a flat color, a 2d simulation. I would like to add a border of variable thickness and a different color to these polygons. I have…
24
votes
2 answers

Why do I need to define a precision value in webgl shaders?

I'm trying to get this tutorial to work but I ran into two issues, one of which is the following. When I run the code as is I get an error in the fragment shader saying: THREE.WebGLShader: gl.getShaderInfoLog() ERROR: 0:2: '' : No precision…
Flavio
  • 1,507
  • 5
  • 17
  • 30
23
votes
3 answers

Can you have multiple pixel (fragment) shaders in the same program?

I would like to have two pixel shaders; the first doing one thing, and then the next doing something else. Is this possible, or do I have to pack everything into the one shader?
Harry
  • 3,076
  • 4
  • 28
  • 44
22
votes
2 answers

How to define constant array in GLSL (OpenGL ES 2.0)?

I just want to store an array of weights that needs to every fragment calculation. This: float weights[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); Just throws this: ERROR: 0:30: ']' : syntax error syntax error ERROR: 0:30: ';' : syntax error syntax…
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
18
votes
2 answers

How to access automatic mipmap level in GLSL fragment shader texture?

How do I determine what mipmap level was used when sampling a texture in a GLSL fragment shader? I understand that I can manually sample a particular mipmap level of a texture using the textureLod(...) method: uniform sampler2D myTexture; void…
Christopher Bruns
  • 9,160
  • 7
  • 46
  • 61
18
votes
2 answers

OpenGL - How to access depth buffer values? - Or: gl_FragCoord.z vs. Rendering depth to texture

I want to access the depth buffer value at the currently processed pixel in a pixel shader. How can we achieve this goal? Basically, there seems to be two options: Render depth to texture. How can we do this and what is the tradeoff? Use the value…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1
2 3
99 100