PDA

View Full Version : CPU side copies of data



Mukund
01-19-2015, 12:36 AM
Hi All,

I was wondering when exactly a copy of CPU data is made into the GPU. For example, we say glGenBuffers() followed by glBufferData().
Now, is the CPU vertex data copied to the GPU when the glDraw* call is made? What is the general approach(if any) or does OpenGL suggests something for driver implementors?
Same applies to textures(glTexImage* functions).

Can anyone please clarify this?
Thanks!

GClements
01-19-2015, 02:00 AM
It's up to the implementation when data is copied between CPU and GPU memory (if these are even different, which isn't necessarily the case).

However, most functions which accept a pointer to input data require that data to be copied somewhere before the function returns.

So data passed to glBufferData, glTexImage*, etc will be copied before the call returns. It may be copied directly to GPU memory, or copied into a buffer in CPU memory and copied to GPU memory later. But whichever approach is used, modifying the passed-in buffer after the call returns will have no effect upon the operation of OpenGL.

The main exception to this principle is for glVertexAttribPointer (and fixed-function equivalents such as glVertexPointer, glNormalPointer, etc). In that case, the data is read during subsequent draw calls, not during the gl*Pointer call, so modifying the contents of the buffer will affect the results of subsequent draw calls.

Alfonse Reinheart
01-19-2015, 09:04 AM
The main exception to this principle is

Small point: That's the only exception. Indeed, that's the point of the "Pointer" nomenclature: to specify that the pointer in question will be retained by OpenGL and used at some later time. In all other cases (unless you're using an extension of some sort), pointers given by the user will never be retained by OpenGL. By the end of the function call, the implementation will have done whatever it is going to do with the memory you gave it.

Note also that, while the pointer can be discarded after the call, this does not mean that it has been copied from CPU memory to GPU memory. Generally speaking, implementations will copy your memory out to a block of internally allocated CPU memory. Then it will do the GPU DMA from there. After the DMA completes, the internal memory will be deleted.

The whole point of buffer mapping (https://www.opengl.org/wiki_132/index.php?title=Buffer_Object&redirect=no#Mapping) and pixel buffers (https://www.opengl.org/wiki/Pixel_Buffer_Object) is to avoid the use of this internal copying.