VVISF & VVGL
Sample Code III- Texture copy/upload/download

An instance of GLBuffer represents an OpenGL resource, or it contains a pointer to system memory. In either case, one thing is certain: if you want "a copy of a GLBuffer instance", you cannot simply make another copy of the struct- bad things will happen as the underlying image data either won't be duplicated.

It is strongly recommended that you use GLBufferRef instead of GLBuffer if you just want to share a given GLBuffer instance with another object (displaying the same texture in two different views, for example). As long as you use GLBufferRef and ensure that you set any GLBufferRefs you maintain to nullptr as soon as you no longer use them, all the underlying GPU/CPU resources will be freed/pooled and you'll have plenty of VRAM available. Remember, GLBuffers come from and are returned to pools: obtaining a new buffer and setting a buffer to nil are typically very fast and very cheap (within reason).

// allocate an RGBA OpenGL texture
GLBufferRef origTex = CreateRGBATex(VVGL::Size(1920,1080));
// print the name and source rect of the RGBA texture
cout << "origTex is " << orgTex->name << ", srcRect is " << origTex->srcRect << endl;
// make another var- both vars refer to the same underlying GLBuffer instance
GLBufferRef sameGLBufferNewVar = origTex;
// modify the srcRect of the new texture var
sameGLBufferNewVar->srcRect = VVGL::Rect(10,10,1900,1060);
// print the name and source rect of the new var
cout << "sameGLBufferNewVar is " << sameGLBufferNewVar->name;
cout << ", srcRect is " << sameGLBufferNewVar->srcRect << endl;
// print the source rect of the original- observe that it has changed!
cout << "origTex srcRect is now " << origTex->srcRect << endl;

GLBufferRef is great, but if you're working with it and you modify a property- like 'srcRect', for example- then every other GLBufferRef for that same GLBuffer will immediately inherit that change. This is not always desirable- for example, you may want to crop a GLBufferRef for use in one particular scene, or use a larger texture-based GLBuffer as the source of many smaller texture-based GLBuffers that all share the same GL resource (texture atlas). The function GLBufferCopy() accepts a GLBufferRef, makes a new GLBuffer instance that has a strong reference to the GLBuffer instance you passed in, and returns a GLBufferRef for the new instance. Because it's an entirely different GLBuffer, you can modify the superficial properties without affecting the original GLBuffer or any of its refs- but because it's the same underlying GL resource, nothing was actually copied and the resource will automatically be retained as long as necessary. Copying buffers in this manner is quick and cheap because the actual image data isn't being duplicated.

// allocate an RGBA OpenGL texture
GLBufferRef origTex = CreateRGBATex(VVGL::Size(1920,1080));
// print the name and source rect of the RGBA texture
cout << "origTex is " << orgTex->name << ", srcRect is " << origTex->srcRect << endl;
// make a copy of the GLBufferRef- this makes another
// instance of GLBuffer that retains the passed GLBuffer.
GLBufferRef sameTexNewBufferNewVar = GLBufferCopy(origTex);
// modify the srcRect of the new texture var
sameTexNewBufferNewVar->srcRect = VVGL::Rect(10,10,1900,1060);
// print the name and source rect of the new var
cout << "sameTexNewBufferNewVar is " << sameTexNewBufferNewVar->name;
cout << ", srcRect is " << sameTexNewBufferNewVar->srcRect << endl;
// print the source rect of the original- observe that it has changed!
cout << "origTex srcRect is " << origTex->srcRect << endl;

Sometimes you actually want to copy the contents of a GLBuffer to another GLBuffer. For this, VVGL offers a couple different classes that specialize in the more commonly encountered types of copies so far: