Saturday, September 28, 2019

In OpenGL, is it a bad idea to "combine" immediate mode and retained mode for the sake of GUIs?


Suppose I have a 3D scene being drawn with glDrawArrays(...) and I want to render something simple, like a 2D overlay. Would it be a bad idea to use immediate mode for such a thing, instead of setting up a 2D renderer with retained mode?


Example:


void Draw()
{
// Draw the 3D scene:
// *does stuff*

glDrawArrays(...);
// *some other stuff*

// Setup a quad with immediate mode (for something like a vignette overlay)
glBegin(GL_QUADS);
glTexCoord2f(...); glVertex2f(...);
glEnd();
}

Answer



It is a bad idea insofar as it is nonsensical. There is nothing to "set up" that you have not already done, except for an ortho projection matrix (which you will have to do in any case).



Portability is probably not a problem, although it should be. IHVs seem to be very reluctant to drop support for immediate mode for the foreseeable future (it seems to "work fine" even in core profiles), so although immediate mode should have died out long ago, it will probably stay forever. Performance is another story. Do you really want something that accounts for 0.1% of the scene complexity to consume 15-20% of the CPU time -- GL calls are relatively lightweight, compared to e.g. DX, but they are not free -- and 15-20% of real frame time due to stalling the pipeline? Can you even afford that with your time budget? Most games can't.


And then of course, the quirks. Oh the quirks. The apparent beauty of immediate mode is that it is (seemingly) easy and straightforward to do something like drawing a couple of quads quick. In reality, immediate mode can be such a pain in the rear, it's so full of non-obvious pitfalls.
On the other hand side, it is just as easy (and more straightforward if you ask me!) to just write a small 5-LOC function DrawQuad which when called adds vertex coordinates and indices to a memory buffer and increments a counter. Which, surprise, you can then draw with glDrawArrays/Elements and which you can reuse (the GPU copy, not just the userland copy!) the next frame as-is as long as nothing is changed.
Immediate mode must, there is no other way, allocate a buffer and do a PCIe transfer every time. Or, something equivalent in latency (GPU reading main memory over the bus, whatever). The driver cannot possibly know (or assume) that the data remained exactly the same as the frame before.


Getting data to the GPU is a high-speed, but also a high-latency operation. Not only is the bus transfer per se a complicated, high latency protocol (several protocol layers, packetizing, acknowledgements, etc.), but also not all GPUs are even able to transfer and draw at the same time, or if they can do it, they may not be able at all times to switch between or initiate new operations freely while doing something different. Read as: Thou shalt not transfer vainly.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...