Prune

Members
  • Content count

    931
  • Joined

  • Last visited

Community Reputation

224 Neutral

About Prune

  • Rank
    Advanced Member
  1. OpenGL Batch Rendering

    Is there any reason you're not using multi-draw indirect? With that, you can do a single draw call per shader. What I do is pack all of a given type of vertex attribute for all meshes in a single VBO (I don't interleave because most passes only need positions: the shadow map passes and the z-prepass), then I build a separate command buffer for each <shader,draw-call> pair. Transforms and material properties, including bindless texture handles, are in other buffers that the shader indexes by indirectionBuffer[gl_DrawIDARB], and those that change can be written into persistently mapped buffers. The render thread then becomes very simple:   Init: persistently map all dynamic buffers; initial glFenceSync()   bool newSync(false): if (new dynamic update data available) { glClientWaitSync(...); copy data (whatever's changed of transforms, material parameters, texture handles, indirection buffer, draw command buffer) into triple buffer to begin DMA transfer increment indexes newSync = true; } ... if (newSync) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); bind shadow shader, disable color writes for each shadow-casting light, bind render target and glMultiDrawElementsIndirectCountARB(...) bind z-prepass shader glMultiDrawElementsIndirectCountARB(...) enable color writes for each shading pass, set state, bind shader, and glMultiDrawElementsIndirectCountARB(...) if (newSync) syncs[index] = glFenceSync(); for each postporcess pass, bind shader and glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // No VAO/VBO bound, just use gl_VertexID to index constant array in shader   The update data is created by the compute threads; I only do interpolation based on timestamp on the render thread to avoid jitter, as the compute threads run asynchronously.
  2. glGetError

    Will I get a significant performance hit if I call glGetError() per frame, even if just before the SwapBuffers call (and I'm using vsync)?   For example, if I have a persistently mapped buffer, and I've initiated a transfer, then does the glGetError() act like a barrier and would thus potentially be a significant hit?
  3. 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.
  4. 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; }
  5. 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. 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).
  6. 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?
  7. Any comment on the last one? Does this only apply go glTexSubimage(), or are other buffer transfers, especially mapped buffers, handled the same way, and require a second context to occur concurrent with rendering, or only texture upload can trigger the copy engine?
  8. The thing is, since we're not talking about indexing using an arbitrary value, such as an ID read from a texture, the GLSL runtime _does_ have the information that it needs to optimally allocate registers _per draw_. Indexes dependent solely on gl_DrawIDARB (and gl_InstanceID) are, by the spec, dynamically uniform expressions. Surely there is already some sort of runtime partial specialization of shaders--else why have the defined concept of dynamically uniform expressions (which are constant within a work group) at all? So why can't register allocations that depend on subroutine selection that's dependend on not just constant expressions, but dynamically uniform expressions as well, be part of that specialization?
  9. Very informative, thanks. I didn't know that registers are not allocated dynamically per subroutine. Is this likely to stay this way for a long time? What do you think about grouping subroutines in several shaders based on complexity, then, couldn't that be a good compromise?   You mention variable interpolation is likely done in software on newer cards. Do we expect that eventually texture filtering will also be done that way? A common optimization in convolution shaders this far has been in relying on bilinear interpolation to reduce the number of samples, but if this ends up being shader code anyway, then there'd be no point to bother with the added complexity (in terms of calculation of the sample locations and weights).
  10. Most of my drawing is with glMultiDrawElementsIndirectCountARB(), but I'm still splitting up the draw calls between groups of objects with different material types, in order to bind different shader programs. I've been considering just indexing by object ID into an array of subroutines so I don't have to switch shaders and thus have only one draw call per pass, but I'm wondering if an array of subroutines would have significant overhead, as it would be combining the additional dereference (the array) with a function call that can't be inlined (subroutine). Is this a realistic concern, or is it likely to be negligible? The other issue I'm worried about is that different shaders in general require different inputs/outputs, and I'm not sure how much performance would be wasted by interpolation of input/output pairs that are unused by given subroutines.
  11. Hmm the documentation for glBindBufferRange() states that the target must be GL_TRANSFORM_FEEDBACK_BUFFER or GL_UNIFORM_BUFFER. And in ARB_draw_indirect I see in the revision history "Remove BindBufferRange/Base from commands for which DRAW_INDIRECT_BUFFER is a valid target". So I guess not.
  12. Specifically, can I glBindBufferRange() a portion of a buffer object to GL_DRAW_INDIRECT_BUFFER and another to GL_PARAMETER_BUFFER_ARB? It's kind of lame to have a whole separate buffer for the latter, given it's such a tiny buffer.
  13. Thanks. I actually feel dumb to have asked that since I was already using block like this for transforms.   Just a note that std430 can't be used for UBO as in your example, only SSBO.
  14. Given that UBOs use a "binding" index instead of "location", do I need to switch "location" to "binding" in my GLSL declaration to use a UBO for texture handles? layout(location = SAMPLERS, bindless_sampler) uniform sampler2D textures[MAX_TEX]; --> layout(binding = SAMPLERS, bindless_sampler) uniform sampler2D textures[MAX_TEX]; The reason I'm not sure is because ARB_bindless_texture says:   If this "binding" refers to an image unit, is it not different than the "binding" index of UBOs?
  15. ARB_bindless_texture specifies functions like glProgramUniformHandleui64vARB to update an array of texture handles. Can one, instead, use a regular uniform buffer object of GLuint64 and map it and write the handles to it, as one would write elements to any other mapped buffer object?