Jump to content
  • Advertisement
nOoNEE

OpenGL what happen behind glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)?

Recommended Posts

i want to know what exactly happen behind this code when we use this

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

, i mean what processes it takes , (for more detail i want to know what  " | " does here and want to know  that glClear , clears both of them in one term or one by one)

Share this post


Link to post
Share on other sites
Advertisement

It would really need someone familiar with the OpenGL innards to answer your question, but here is my understanding:

"|" just means bitwise OR, as the argument to glClear is passed as one value (presumably an int or uint) by combining flags (e.g. 1, 2, 4, 8, 16 etc). This is just a method of communicating via the API, and says nothing about how the command operates within OpenGL (the driver and GPU).

Some operations are well defined in the OpenGL specs, and others are left for the hardware guys to do in the most optimal way for their hardware. You can check the specs, but I suspect this particular operation may not be tightly defined, except in terms of what the client 'sees'.. i.e. as long as to the client the result is the same, it should not matter how the implementation achieves the result.

An example of this divergence is the difference between tile renderers on mobile, and desktop renderers. On desktop not clearing the back buffer used to be used as a performance optimization (as the colours are going to be drawn over in the next frame anyway). However on tile renderers this can adversely affect performance because the old contents need to be explicitly copied to the 'operating tile', just in case some pixels are not overwritten. So in tile renderers it can be faster to explicitly clear the entire frame buffer.

Even on desktop modern GPUs may not be 'clearing' the memory in the way that you think, they may be using tricks to achieve the same effect.

Share this post


Link to post
Share on other sites
9 hours ago, nOoNEE said:

i want to know what exactly happen behind this code when we use this


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

, i mean what processes it takes , (for more detail i want to know what  " | " does here and want to know  that glClear , clears both of them in one term or one by one)

glClear clears the buffer specified by the bitwise mask passed. So if you use glClear(GL_COLOR_BUFFER_BIT) you clear that based on what you set with glClearColor, or if you place more than one in glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) would clear all three to what you had set in glClearColor, glClearDepth, glClearStencil in a single call.

Share this post


Link to post
Share on other sites
Posted (edited)

http://docs.gl/

This site should be your very best friend.  It will be a lot faster then asking a question in a forum.  You can search for the specific version of OpenGL you wish also.

Edited by CrazyCdn
Fixed my Engrish...

Share this post


Link to post
Share on other sites
On 7/9/2018 at 5:26 PM, lawnjelly said:

It would really need someone familiar with the OpenGL innards to answer your question, but here is my understanding:

"|" just means bitwise OR, as the argument to glClear is passed as one value (presumably an int or uint) by combining flags (e.g. 1, 2, 4, 8, 16 etc). This is just a method of communicating via the API, and says nothing about how the command operates within OpenGL (the driver and GPU).

Some operations are well defined in the OpenGL specs, and others are left for the hardware guys to do in the most optimal way for their hardware. You can check the specs, but I suspect this particular operation may not be tightly defined, except in terms of what the client 'sees'.. i.e. as long as to the client the result is the same, it should not matter how the implementation achieves the result.

An example of this divergence is the difference between tile renderers on mobile, and desktop renderers. On desktop not clearing the back buffer used to be used as a performance optimization (as the colours are going to be drawn over in the next frame anyway). However on tile renderers this can adversely affect performance because the old contents need to be explicitly copied to the 'operating tile', just in case some pixels are not overwritten. So in tile renderers it can be faster to explicitly clear the entire frame buffer.

Even on desktop modern GPUs may not be 'clearing' the memory in the way that you think, they may be using tricks to achieve the same effect.

it solved my problem thanks 

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!