简体   繁体   中英

Does fragment shader process all pixels from vertex shader?

If I have only the vertex shader and fragment shader in my pipeline, what I would like to know is does the fragment shader receive each individual processed vertex from the vertex shader or does it wait until all of the values that are passed in to the vertex shader are processed before giving them to the fragment shader to then individually process each pixel or process them all at once?

Also does the fragment shader get immediately executed after each vertex shader execution before the vertex shader gets the next vertices or vertex input to process?

Meaning if I have my fragment shader doing this:

const GLchar * vertex_shader_source = {
    "#version 430 core\n"
    "layout(location = 0) in vert;\n"
    "void main(void)\n"
    "{\n"
    "gl_Position = vert;\n"
    "}\n"
};
const GLchar * fragment_shader_source = {
    "#version 430 core\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "color = vec4(gl_VertexID/100,gl_VertexID/100,gl_VertexID/100,1.0f);\n"
    "}\n"
};

Would the fragment shader actually be in sync with each vertex index being processed from the buffer?

Your question is misleading. It's not the case that fragment shaders receive vertices. They receive fragments. Between vertex and fragment shaders, there's a lot of hardware that takes multiple vertices, form triangles out of them, do a bunch of processing on them (culling, clipping...) and then rasterize them (transform triangles to a bunch of covered squares) to generate the fragments that will get executed on the fragment shader.

All that to say that there isn't a natural mapping from vertex ids to fragments (and indeed you need multiple vertices to generate a fragment, and in many cases, a single vertex will contribute to multiple triangles too).

So there is simply no way to meaningfully use a gl_VertexID inside a fragment shader.

Now, with respect to "do vertex shaders run all before fragments" or such questions... Any fragment that was generated necessarily had the vertex positions already computed (since it's required to compute the fragment coverage). But it's about the only guarantee you'll get.

Tile-base deferred renderers, eg will typically process all the vertices first if it can (on the whole frame!), and only later a frame later will it process all the fragments. In contrast, "Immediate Mode Renderers" will tend to process triangles as they become available. There's isn't a lot of ways to really observe those things, and I don't know that many people did do the observation on the few methods available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM