Sign in to follow this  
thomasfn1

OpenGL Material blending in a deferred renderer

Recommended Posts

So I'm about to start writing a scene manager and renderer for a game and I'm going to be using deferred shading. My scenes are going to be populated by lots of objects so I'll be using instancing to cut down on draw calls. I also want to try and defer material and texture sampling by having my objects output a material ID into a single-channel FB. Some kind of mega-shader would then convert the material ID into a diffuse and normal sample (probably by having a texture store all material attributes and using the material ID as a v coordinate, and by using texture arrays to get access to different textures).

 

This raises an issue with material blending. My scene will also feature a terrain at all times (other than when the player is looking up) and I want the textures on my terrain to blend from one to another across a tile. Of course, since the mapping between a pixel and material ID is 1-1, there's no way to represent blending between multiple materials using the setup described above. The way I see it, I have 2 options:

 

  1. Instead of using a single material ID per pixel, use multiple material IDs and have a second output which would be the weights of the materials to blend with in the mega-shader.
  2. Draw the terrain seperately straight into the GBuffer after the mega-shader has been run on the scene, using the depth-buffer from the material ID FB. Texture sampling would occur normally here. Lights would have to be rendered in a seperate pass from the mega-shader.

Both options have pros and cons. With option 1, the advantages of drawing into a relatively small FB are lost (now we have 2 textures to render into with weights) and you can only fit up to 4 materials to blend at once (though this might be enough for terrain blending?). Additionally, the uber-shader now has to make up to 4 samples into the material texture and corresponding diffuse/normal textures and blend everything together, potentially taking 4x as long to draw. I'm not sure if it's possible to optimise this somehow such that only 1 sample is made if only 1 material is used with full weight.

 

With option 2, the uber-shader remains relatively light-weight but there's an extra pass involved and there's probably some caveat of drawing everything in a strange order. Also benefits of the deferred rendering in this case are lost for the terrain (which will have a very large surface area and is also likely to be occluded by many polygons (trees, grass etc) and so would probably benefit from deferred rendering the most).

 

My question is are there any other options I haven't considered? There is surpsingly a lack of documentation and literature on this matter floating around. If there are no other better options, which option do you think is best for me to choose? Alternatively, is it worth me using deferred rendering at all and should I spend my time making a more intelligent batching system/render queue and try and squeeze more frames out of there?

 

If it makes any difference I'll be using OpenGL core profile 3.3. My host language is C# / OpenTK.

Share this post


Link to post
Share on other sites

I'm curious - what's the motivation for doing the material buffer and the second pass expansion of shading attributes? Deferred shading is already somewhat limited in what kind of material variation you can create - but having interesting materials that control what gets painted into the gbuffer can mitigate that. Your design is going to severely limit what you can do - and you're just hitting the first example of that. Are you worried about performance, or is there some other goal here?

Share this post


Link to post
Share on other sites

Writing out material IDs in a first pass and then creating a G-buffer from that in a second pass then doing lighting in a third pass sounds like an overly complicated solution to a problem you don't even know you have (the problem of doing too many texture samples). How do you know writing out an entire frame buffer of material IDs is going to be faster than doing a small number of unnecessary texture samples?

 

I would ditch that idea and just use a regular deferred renderer. Write out the G-buffer in the first pass, and do lighting in the second pass. Using whatever materials your objects have during the G-buffer pass. This will let you blend the diffuse color of your terrain in the G-buffer pass just by using the correct terrain shader. No hassle. If you're concerned about wasting time drawing terrain pixels that eventually get occluded, then simply draw the terrain last. The G-buffer pass should render everything front to back, anyway, to mitigate overdraw.

 

Also, typically if one were to write out a material ID at any point in the deferred pipeline, it would be written directly into the G-buffer, and it would be used to select a set of shading parameters (specular power, surface roughness, etc etc) for the lighting pass.

Share this post


Link to post
Share on other sites

Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

I suppose I'm probably over-thinking things and inventing unnecessary solutions to problems which haven't surfaced yet, and may never surface. The biggest thing that will help me is instancing (since I'm likely to have many trees on-screen at any given point), so in theory each material is only going to cost 1 draw call anyway if my render queue batches things intelligently enough. I don't think I can control the order in which objects are drawn via instancing though.

Share this post


Link to post
Share on other sites

I feel like you would need very uniform materials and/or a unique virtual texturing system for your approach to work.

You may also want to keep in mind that it's very common in games to render dynamic "decals" into your G-Buffer to handle things like scorch marks and bullet holes, and this also wouldn't play nicely with your material ID system.

Share this post


Link to post
Share on other sites


Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

If performance is your largest concern I would stay away from deferred rendering altogether, and instead use forward rendering. I've used both in our game and although deferred allows you to do more fancy stuff it comes with a cost.

Share this post


Link to post
Share on other sites

Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

You may disable writing to z-buffer for the other passes than the first smile.png Also, you may find a way to combine the 5 materials into one.

 

Otherwise you were talking about a terrain rendering, in BF3 they used a terrain tile cache. Instead of blending 5 materials in the g-buffer, you may render the terrain materials into a tile texture and "flatten" the materials, then in the G-buffer pass you can draw pretty complex terrains with very simple shader since the materials have been blended already. So there is no need to overcomplicate your deferred renderer. 

 

Of course, there are limitations, such as tile texture detail, but this can be fixed to certain degree with detail texturing and volume decals etc. Also, the amount of tiles will eat up some GPU memory. 

 

Cheers!

Edited by kauna

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  

  • Forum Statistics

    • Total Topics
      627776
    • Total Posts
      2979021
  • Similar Content

    • By lonewolff
      Hi guys,
      With OpenGL not having a dedicated SDK, how were libraries like GLUT and the likes ever written?
      Could someone these days write an OpenGL library from scratch? How would you even go about this?
      Obviously this question stems from the fact that there is no OpenGL SDK.
      DirectX is a bit different as MS has the advantage of having the relationship with the vendors and having full access to OS source code and the entire works.
      If I were to attempt to write the most absolute basic lib to access OpenGL on the GPU, how would I go about this?
    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
  • Popular Now