Sign in to follow this  
ehsan2004

OpenGL simple question about shaders

Recommended Posts

ehsan2004    178
Hi I'm currently reading the book more OpenGL game programming. I have some basic questions about the shaders which haven't been discussed in that book. We know that the equation for OpenGL matrices is: Projection matrix * View matrix * Model matrix * local vertex position = final vertex position; So when a vertex shader is applied to a vertex?For example before all these matrices are applied in a program? Another question is that what's the meaning of "fragment" in a fragment shader? Is it simply a pixel or is it a triangle for example? When it is applied to a program? ( before and after stages in a fragment processing ) Thanks

Share this post


Link to post
Share on other sites
karwosts    840
A vertex shader typically takes a vertex's local coordinates and any model/view/projection matrices and applies the matrices to the vertex, and then emits the projection space coordinates. You don't typically apply these matrices in your program, although you can concatenate the model, view, and projection matrices in the program to reduce the number of operations in the vertex shader.

A fragment is a pixel (or texel). Once all of the vertices of a primitive are transformed through the vertex shader, the primitive is rasterized, and each pixel is sent to fragment shader for coloring. The fragment shader emits a color for the pixel, which is then written into the framebuffer or whatever target you are rendering to.

Share this post


Link to post
Share on other sites
ehsan2004    178
I have still problem with fragment shader :(
The subject is complex and I need to know what stages are involved in the rasterization process .
Assuming that we have 2 triangles and one of them is in front of the other, What stages are involved in the raster level?For example, does it convert the first triangle to 40 pixels and the second one to 60 pixels ?(values are just optional for my example) We know that z buffer is done after fragment shader, so I guess these triangles are still available after raster level?Does fragment shader process all the primitives at once and send them to the fragment shader or does it converts each primitive( for example that triangle) to pixels and sends it to the fragment shader, then processes the next one and sends it to the fragment shader, etc ?
Assuming that a triangle consists of 40 pixels after raster level, does it mean that the fragment shader processes all these 40 pixels?

Share this post


Link to post
Share on other sites
karwosts    840
Quote:

We know that z buffer is done after fragment shader, so I guess these triangles are still available after raster level?

After rasterization the original primitives are gone. The z-buffer has a value for each pixel and is compared per-pixel, not per triangle.

Quote:

Does fragment (do you mean vertex?) shader process all the primitives at once and send them to the fragment shader or does it converts each primitive( for example that triangle) to pixels and sends it to the fragment shader, then processes the next one and sends it to the fragment shader, etc ?

I would imagine that the GPU does not wait for every primitive to be rasterized before sending them to the fragment shader, its just a pipeline and they are processed as soon as they are available. Keep in mind there is not just one "vertex or fragment shader" in the GPU, there are hundreds that are cranking out pixels in parallel. I think your question is implementation dependent, and I can't imagine any good reason why you would want to have this information. If you would elaborate on what your problem is maybe someone could help you.


This poster might answer some of your questions too:

The OpenGL Machine

Unfortunately it is only from version 1.1, I could not find a digital copy of the most recent one. I have a physical copy of the version 3.1 machine, and the operations after the Fragment shader look pretty similar.

If you're talking about fragment shaders, just imagine a box around the Texel Generation, Texture Application, and Fog components. This represents what is now the fragment shader. Everything after fragment shader starting with "Coverage (antialiasing) Application" looks pretty much the same as the 3.1 poster.

Share this post


Link to post
Share on other sites
ehsan2004    178
OK,
1)What about the varying variables? Assuming that we have only 3 vertexes and compute a varying variable for each vertex, how does a fragment shader understand that which varying variable must be used for which fragment?We have only 3 vertices, but the rasterization may generate multiple pixels.
2)When the output of the vertex shader is sent to the fragment shader?For example when it processes a primitive of type "type" given by glBegin(type)?

Share this post


Link to post
Share on other sites
karwosts    840
Quote:

1)What about the varying variables? Assuming that we have only 3 vertexes and compute a varying variable for each vertex, how does a fragment shader understand that which varying variable must be used for which fragment?We have only 3 vertices, but the rasterization may generate multiple pixels.


These are interpolated across the triangle. If one vertex has a varying value of zero, and another has a value of 1, then the pixels between those vertices will be in the range from zero to one.

Quote:

2)When the output of the vertex shader is sent to the fragment shader?When it processes 3 vertexes that creates a triangle?


The vertices must go through primitive assembly before being rasterized, so you would need to wait at least until all vertices of a primitive are through the vertex shader.

Share this post


Link to post
Share on other sites
ehsan2004    178
So when we use per fragment lighting, each fragment deals with its own interpolated varying variables. Here I have another question. If the fixed function is able to interpolate the color values of vertexes and send them to fragments, why do we need to specify color for a fragment inside a fragment shader? Just to change the behavior of the fixed function color interpolation?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now