• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Prune

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

7 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0