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 on other sites

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 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 on other sites
Posted (edited)

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 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

Create an account

Register a new account

1. 1
2. 2
Rutin
23
3. 3
4. 4
JoeJ
19
5. 5

• 14
• 25
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631770
• Total Posts
3002250
×