is there any way I can query which primitive the current vertex belong inside a vertex shader? I'm using a TBO to output some data that I need to access inside a vertex shader, I can access this data inside the fragment shader using gl_PrimitiveID and it works fine but I can't find a way to do the same inside a vertex shader, gl_VertexID/3 won't work for me because I'm using glDrawElements.
thank you!
luiz
This is impossible if you understand how vertex shader invocations work.
While it is true that the vertex shader has to be evaluated for every vertex in a primitive, the transformed clip-space value will be stored in a post-transform cache. As a result, a vertex shared by 10 different triangles may only be computed once (subsequent primitives that use said vertex will hit the post-T&L cache first). There is no clear relationship between the primitive that caused the vertex to be computed and the vertex shader invocation.
The Geometry Shader stage, on the other hand, has gl_PrimitiveIDIn, which may help you - it is not clear what you are trying to accomplish.
Related
As far as I understand, the color of a pixel is determined by the fragment shader. Why do we need a vertex shader then? Is there anything a fragment shader cannot do (or cannot easily do) but a vertex shader can do (easily)?
But I still can't quite understand why it is named a "shader".
Because that's what programs executed as part of the rendering process are called. The Renderman interface specification was one of the first programmable rendering processes, and they called all of their programmable elements "shaders", even though they didn't all compute colors.
And therefore, "shader" has become the term used for describing any such program.
Vertex shaders convert vertex data, creating a 1:1 mapping from input vertices to output vertices. Fragment shaders operate on fragments. An FS invocation has no control over where it will be executed. They are generated in the location that the rasterizer says they go, and the FS has no way to affect this.
By contrast, a vertex shader has complete control over where the vertices will go.
I have got a texture which is updated from a fragment shader that calculates points positions.
What is the good way to read it back so it could be drawn as primitives ?
If you want to draw using the data from the texture, reading back to host memory is a waste and slow (But for reference you could use glGetTexImage or glReadPixels).
Instead, you can draw primitives without providing vertex positions and read them from your texture in the vertex shader (bound as a sampler and using texelFetch for example).
The coordinates for texel fetch can come from a per-vertex attribute (just like regular texture coordinates), or you can use gl_VertexID to calculate them implicitly.
As #ColonelThirtyTwo said, you can also use transform feedback. Not using your texture and doing the computation in the fragment shader, but replacing it with computation in a vertex shader. Here the varying variables normally interpolated to the fragment shader get packed and saved in a buffer, still on the GPU.
It is available as output variable in all shaders except fragment shader. So which shader stage must write it? Is its value taken from the last shader stage that wrote it?
Also please explan what is the purpose of having the value gl_ClipDistance in fragment shader?
As long as you only work with vertex and fragment shaders, you write it in the vertex shader. According to the GLSL spec, geometry and tessellation shaders can write it as well.
The fragment shader can read the value. Based on the way I read the documentation, it would give you the interpolated value of the clip distance for your fragment.
Considering it is really only useful for clipping... you need to write it in the last stage of vertex processing in your GLSL program. Currently there is only one stage that does not fall under the category of vertex processing, so whatever comes immediately before the fragment shader needs to output this.
If you are using a geometry shader, that would be where you write it. Now, generally in a situation like this you might also write it in the vertex shader that runs before the geometry shader, passing it through. You don't have to do anything like that, but that is typical. Since it is part of gl_PerVertex, it is designed to be passed through multiple vertex processing stages that way.
Name
gl_ClipDistance — provides a forward-compatible mechanism for vertex clipping
Description
[...]
The value of gl_ClipDistance (or the gl_ClipDistance member of the gl_out[] array, in the case of the tessellation control shader) is undefined after the vertex, tessellation control, and tessellation evaluation shading stages if the corresponding shader executable does not write to gl_ClipDistance.
If you do not write to it in the final vertex processing stage, then it becomes undefined immediately before clipping occurs.
If i use opengl 3.2+ with compatibility context and have a fragment shader, is it necessary to have a vertex shader? I would like to know if per vertex lighting calculation and other per vertex calculations can be done by the fixed function pipeline and i can just use the fragment shader.
Also what implications would this have for per-vertex attribute binding locations?
if per vertex lighting calculation and other per vertex calculations
can be done by the fixed function pipeline
They can be done if you use fixed pipeline lights.Otherwise, part of it (like transformed normals,uv's and positions) must be computed elsewhere before being passed to the fragment shader.This "elsewhere" is called vertex shader.So yes,if you don't use fixed pipeline lightning system you must use vertex and fragment shader to process it.
Also,if you use fixed pipeline lightning you can still use shaders where you can access fixed light and material properties.But I see no point doing so unless you wish to break the defaul behavior.
I know that I can get vertex normal in vertex shader from gl_Normal.
Now I want to set up the color of a fragment to be the vertex normal of its first vertex. May I ask how can I do that? How can I know how many vertex the fragment has, and what is the position and normal for each vertex in fragment shader?
What you ask is not quite possible, but you might be able to get close enough.
You cannot get the normal from the 'first vertex' of a primitive. What you can do is pass the normal from the vertex shader to the fragment shader as a varying, but then each fragment will get a normal that is interpolated from each vertex of the polygon.
You can't access specific properties of the vertex from the fragment shader, only interpolated values.
==EDIT==
Looks like I might be incorrect in some cases, see below comments.