Jump to content

  • Log In with Google      Sign In   
  • Create Account

Overhead of GL_DYNAMIC_STORAGE_BIT​ or GL_MAP_WRITE_BIT​ when there's no writing being done?


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
7 replies to this topic

#1 Prune   Members   -  Reputation: 223

Like
0Likes
Like

Posted 03 June 2014 - 06:13 PM

Do these two flags have any overhead?

 

I use immutable buffer storage. Most of my meshes are static, so I just upload the data at creation time by passing a pointer to glBufferStorage(). However, this presents me with a problem now that I've switched to putting all of a given type of vertex attributes in a single buffer (so all vertex positions for all meshes in one buffer, another one for normals, and so on), because I'm using a single multi-draw call per shader switch to draw a render pass.

 

The problem is now I either have to copy all my mesh data from mesh class instances to these buffers, instead of loading directly from the underlying std::vectors, or use a custom allocator for the vectors that my mesh class uses, where the allocator contiguously stores the elements of multiple vector instances into the underlying bigger buffer. Unfortunately, Visual C++ in debug mode uses a vector container's allocator not just for the element data store, but also rebinds it and allocates some _Container_proxy debugging data structure, and I haven't figured out how to handle that.

 

I'm wondering if, instead of having static buffer objects, I glBufferStorage() with null data source pointer but set either GL_DYNAMIC_STORAGE_BIT​ and load each mesh's data with glBufferSubData(), or GL_MAP_WRITE_BIT​ and write the data to the mapped buffers. But in this case, is there any potential overhead impacting performance after the buffers are filled with data by making the buffers writeable with either of these flags?


Edited by Prune, 03 June 2014 - 06:15 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

Sponsor:

#2 Promit   Moderators   -  Reputation: 10951

Like
3Likes
Like

Posted 03 June 2014 - 06:28 PM

As with anything in OpenGL, maybe. If the driver decides to take your flag at face value, then yes, there are implications for internal memory allocation that will create overhead. This may or may not have actual consequences in practice, especially depending on the driver involved. Keep in mind that until recently, pretty much everything had WRITE set anyway because that was the only way to get data into buffers in the first place.

 

That said, i don't understand what the problem is which is leading you to this question in the first place. But it sounds like you're abusing std::vector in a way you shouldn't be. It's also worth noting that drivers and hardware HATE when you keep separate vertex streams like that. Interleaved = good. Separate = bad. I'd deal with that mistake first.


Edited by Promit, 03 June 2014 - 06:33 PM.

SlimDX | Shark Eaters for iOS | Ventspace Blog | Twitter | Proud supporter of diversity and inclusiveness in game development

#3 Prune   Members   -  Reputation: 223

Like
0Likes
Like

Posted 03 June 2014 - 06:48 PM

I don't follow. Why would I interleave positions with other vertex attributes when positions are the only thing used for the early-Z pass and all the shadow map generation passes? Interleaving means that for all passes but the shading one, strided addressing would walk over a far larger chunk of memory that needed.

Then there's tangent data, which is only used for a few shaders, and so that attribute doesn't make sense to be interleaved either as it's not used most of the time.

I only see benefit interleaving normals and texture coordinates, as the two are used together almost all of the time.

Of course, I could have positions both by themselves for early-Z and shadows, and interleaved for shaded calculations, but that's a waste of graphics memory.

i don't understand what the problem is which is leading you to this question in the first place

The multi-draw calls use the same set of attribute buffers used by the underlying individual draws. I can't have a different buffer per object. I'm not sure what's not to understand. If a buffer is created with glBufferStorage(), and neither the dynamic nor the write bits are set, then all the data has to be provided at creation time. That means all meshes I need in for a given multi-draw need their data in a single contiguous memory region passed to glBufferStorage(). Thus, all data my mesh class instances store has to be allocated contiguously from the same underlying chunk of RAM. The two alternatives--make the buffer objects writeable, or copy each mesh into the larger RAM buffer to pass to glBufferStorage(), add overhead (well, as you said, "maybe" for the first option listed).


Edited by Prune, 03 June 2014 - 06:57 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

#4 mhagain   Crossbones+   -  Reputation: 10523

Like
0Likes
Like

Posted 03 June 2014 - 07:01 PM

As Promit said, in theory MAP_WRITE_BIT should cause the driver to allocate the buffer object in memory that is reasonably fast for the GPU to read from and for the CPU to write to, and this may not be the best option if all that you're doing is having the GPU read from it.

 

In practice, up until recently OpenGL drivers used your options as hints, and could freely move buffer objects around based on heuristics during actual use.  Buffer storage is quite new functionality, so who knows if that's still the case?  If in doubt specify your options based on how you actually intend to use the buffer and your driver has a better chance of doing the right thing.

 

Interleaving.

 

I hear what you're saying about the Z prepass and the shadow passes, but I'd counter that with "why wouldn't you interleave when it would made your other passes faster?"  The bottom line here is that no matter which you do, you're going to make one type of pass potentially slower in exchange for making another type potentially faster.  Despite that I'd still interleave as the first option and only consider splitting out vertex attributes if I had benchmark numbers to prove they were a problem.  Advantages I can see include having a common vertex format for all passes, so you get to reduce buffer changes and vertex setup.  My experience is that walking over extra memory for vertex operations is rarely a bottleneck (your real bottlenecks will be deeper in the pipeline) and you seem to be pre-emptively optimizing here.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Prune   Members   -  Reputation: 223

Like
0Likes
Like

Posted 03 June 2014 - 07:23 PM

I'll consider interleaving, but whether I do or not doesn't affect the main question of the thread. To avoid needing dynamic/writeable buffers for static data, I need to have all the data in a contiguous memory region (in interleaved case, or one contiguous region per attribute in the non-interleaved case--it's really besides the point).

 

Here's my attempt at a custom allocator. And it works in gcc regardless of build options, and on MSVC2012 in Release mode. But in MSVC in Debug mode, it doesn't (that is, it runs, but the semantics don't work):

template<class T>
class ContigAlloc
{
public:
	typedef T value_type;
	typedef T *pointer;
	typedef const T *const_pointer;
	typedef T &reference;
	typedef T const &const_reference;
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;
	template<class U>
	struct rebind
	{
		typedef ContigAlloc<U> other; // TODO: typedef -> using when VC++2013
	};
	inline ContigAlloc(MemBuff &buff); // TODO: noexcept when VC++2013
	template<class U>
	inline ContigAlloc(ContigAlloc<U> const &other); // TODO: noexcept when VC++2013
	inline pointer address(reference x) const;
	inline const_pointer address(const_reference x) const;
	inline pointer allocate(std::size_t n);
	inline void deallocate(pointer p, std::size_t n); // TODO: noexcept when VC++2013
	inline size_type max_size(void) const;
	// TODO: Variadic temlates for construct() when VC++2013
	template<class U>
	inline void construct(U *p);
	template<class U, class A>
	inline void construct(U *p, A &&a);
	template<class U, class A0, class A1>
	inline void construct(U *p, A0 &&a0, A1 &&a1);
	template<class U, class A0, class A1, class A2>
	inline void construct(U *p, A0 &&a0, A1 &&a1, A2 &&a2);
	template<class U, class A0, class A1, class A2, class A3>
	inline void construct(U *p, A0 &&a0, A1 &&a1, A2 &&a2, A3 &&a3);
	template<class U>
	inline void destroy(U *p);
	template<class T0, class U>
	inline friend bool operator==(ContigAlloc<T0> const &x, ContigAlloc<U> const&y); // TODO: noexcept when VC++2013
	template<class T0, class U>
	inline friend bool operator!=(ContigAlloc<T0> const &x, ContigAlloc<U> const&y); // TODO: noexcept when VC++2013
private:
	ContigAlloc(void); // TODO: = delete instead of private when VC++2013
	ContigAlloc &operator=(ContigAlloc const &);
	template<class U>
	friend class ContigAlloc;
	MemBuff &_buff;
};

template<class T>
inline ContigAlloc<T>::ContigAlloc(MemBuff &buff) : _buff(buff)
{
}

template<class T>
template<class U>
inline ContigAlloc<T>::ContigAlloc(ContigAlloc<U> const &other) : _buff(other._buff) // TODO: noexcept when VC++2013
{
}

template<class T>
inline typename ContigAlloc<T>::pointer ContigAlloc<T>::address(reference x) const
{
	return ::std::addressof(x);
}

template<class T>
inline typename ContigAlloc<T>::const_pointer ContigAlloc<T>::address(const_reference x) const
{
	return ::std::addressof(x);
}

template<class T>
inline typename ContigAlloc<T>::pointer ContigAlloc<T>::allocate(std::size_t n)
{
	return reinterpret_cast<T *>(_buff.alloc(sizeof(T) * n);
}

template<class T>
inline void ContigAlloc<T>::deallocate(T *p, std::size_t n) // TODO: noexcept when VC++2013
{
	_buff.deall(p, sizeof(T) * n);
}

template<class T>
inline typename ContigAlloc<T>::size_type ContigAlloc<T>::max_size(void) const
{
	return _buff.remain();
}

template<class T>
template<class U>
inline void ContigAlloc<T>::construct(U *p)
{
	::new(reinterpret_cast<void *>(p)) U;
}

template<class T>
template<class U, class A>
inline void ContigAlloc<T>::construct(U *p, A &&a)
{
	::new(reinterpret_cast<void *>(p)) U(std::forward<A>(a));
}

template<class T>
template<class U, class A0, class A1>
inline void ContigAlloc<T>::construct(U *p, A0 &&a0, A1 &&a1)
{
	::new(reinterpret_cast<void *>(p)) U(std::forward<A0>(a0), std::forward<A1>(a1));
}

template<class T>
template<class U, class A0, class A1, class A2>
inline void ContigAlloc<T>::construct(U *p, A0 &&a0, A1 &&a1, A2 &&a2)
{
	::new(reinterpret_cast<void *>(p)) U(std::forward<A0>(a0), std::forward<A1>(a1), std::forward<A2>(a2));
}

template<class T>
template<class U, class A0, class A1, class A2, class A3>
inline void ContigAlloc<T>::construct(U *p, A0 &&a0, A1 &&a1, A2 &&a2, A3 &&a3)
{
	::new(reinterpret_cast<void *>(p)) U(std::forward<A0>(a0), std::forward<A1>(a1), std::forward<A2>(a2), std::forward<A3>(a3));
}

template<class T>
template<class U>
inline void ContigAlloc<T>::destroy(U *p)
{
	p->~U();
}

template<class T0, class U>
inline bool operator==(ContigAlloc<T0> const &x, ContigAlloc<U> const&y) // TODO: noexcept when VC++2013
{
	return x._buff == y._buff;
}

template<class T0, class U>
inline bool operator!=(ContigAlloc<T0> const &x, ContigAlloc<U> const&y) // TODO: noexcept when VC++2013
{
	return x._buff != y._buff;
}

I then feed this allocator to the std::vector container(s) I use in my mesh class. If, in allocate I check the types with std::is_same(), I find that only in Debug builds in MSVC does the allocator get rebinded to another type. The thing is, I don't know how to handle that. What I would optimally want to do is delegate to the default std::allocator in any case where the a rebind to another type has been used by the container, as that means the container is using the allocator to store other things such as members of the class or debugging crap rather than element storage. I tried changing the rebind declaration to the following, but it doesn't work in MSVC Debug:

struct rebind
{
	typedef typename std::conditional<std::is_same<T, U>::value, ContigAlloc<U>, std::allocator<U>>::type other;
}

Edited by Prune, 03 June 2014 - 07:26 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

#6 Promit   Moderators   -  Reputation: 10951

Like
1Likes
Like

Posted 03 June 2014 - 11:25 PM

Personally I think you're making things needlessly difficult. Vectors aren't supposed to support this usage. Just block out your own memory (array/vector/whatever) and give the meshes a pointer and an offset. As far as the interleaving thing, just keep in mind that on-chip GPU bandwidth is gigantic and vertices are few. Interleaving unused attributes just means a bit of extra copy bandwidth. On the other hand, some hardware (all NV chips circa 2008, not sure what the situation is these days) can't use separated streams, so the driver has to go in and interleave the streams into spare memory before the draw is executed. I also suspect that using mismatched vertex formats between the vertex buffer and the shader will cause a few internal recompiles, though that's relatively minor.


Edited by Promit, 03 June 2014 - 11:30 PM.

SlimDX | Shark Eaters for iOS | Ventspace Blog | Twitter | Proud supporter of diversity and inclusiveness in game development

#7 swiftcoder   Senior Moderators   -  Reputation: 16760

Like
0Likes
Like

Posted 04 June 2014 - 06:07 AM

To avoid needing dynamic/writeable buffers for static data, I need to have all the data in a contiguous memory region

Why don't you just load all of the data into a contiguous memory region yourself?

Abusing allocators to have multiple containers end up contiguous is just asking for trouble.

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]


#8 Prune   Members   -  Reputation: 223

Like
0Likes
Like

Posted 04 June 2014 - 01:34 PM

I also suspect that using mismatched vertex formats between the vertex buffer and the shader will cause a few internal recompiles

I don't understand. Why would they be mismatched?

 

The reason I want to stick with vector is that the mesh class does a lot more than just loading and storage. I have a bunch of mesh processing functions in there. Refactoring to a new datatype will mean changing a class and supporting code that's around a thousand lines.

 

[Edit:] The driver interleaving the streams doesn't make sense for modern hardware, as if that were necessary, then I don't see how vertex pulling from multiple SSBOs could work.


Edited by Prune, 04 June 2014 - 04:25 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS