tags:

views:

45

answers:

1

I don't think I am the first one to think about this ... but would it be possible to write a STL allocator that manages VRAM/Buffer Objects(BO) in OpenGL?

As a result of this Question I currently use vectors to write to and read from BOs.

Additionally i use some templating to map BOs as almost anything i like. Like this:

TypedBufferObject<someKindOfStruct> tbo = getTBO();
someKindOfStruct* mapPtr = tbo.map(GL_READ_WRITE);

This works quite well ... but isn't really safe when it comes to the number of elements that map contains.

Has someone implemented a BO-based allocator for STL? And (if not) would it actually be possible?

+1  A: 

This article by Matt Austern is the classic reference for creating your own allocator. Allocator have their nooks and crannies, but when you know them writing your own isn't really all that hard.

sbi
Having read that i can say, that it would be possible to allocate memory from a BO by mapping it into main memory.But that doesn't help cause in order to use the BO it has to be unmapped after each write or read which can't be done directly in the allocator.
Florian
@Florian: As I've said in my comment to an answer to your other question regarding this, it's probably best your create a light-weight class (template) that behaves like an STL container as much as possible. It would be passed a BO at construction, maps that, allows working with it, and unmaps it in its destructor.
sbi
Actually i am using the 'TypedBufferObject' for exactly that purpose ... i just was hoping to use a more standardized way.
Florian
@Florian: You might want to have `TypedBufferObject<T>::map()` return an STL container-like object, though, instead of a naked pointer.
sbi
I tried to wrap the memory range into a std::vector like this: return std::vector<T>(mapPtr, mapPtr + mapSize);(something along this lines)This works quite well ... but involves too copy operations. One at vector construction time and the other just before unmapping.Would it be possible to create a allocater for this? .. Should be possible.
Florian
@Florian: Even if you get this to work (which I doubt, really), what happens when (note: not _if_) someone calls `push_back()` on such a vector? Or applies the swap trick? Or..? What you have is not your normal, copyable, deletable, dynamically resize-able `std::vector`. It's an non-copyable, non-deletable, fixed-size array which gets its memory, located at a special place, handed during construction. (It's closest well-known relative would probably be `std::array`, but that allocates its memory on the stack.) No, I believe you will have to write your own class for that. Not that hard, really.
sbi
i'd guess push_back would cause the vector to ask the allocator for new memory ... and the allocator would throw an invalid operation exceptionplease notice that i am talking theoretically now, in practice i'm happy with a typecasted pointer ;)
Florian