BufferIndexBinding 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. * Copyright (C) 2010 Tim Moore
  3. * Copyright (C) 2012 David Callu
  4. * Copyright (C) 2017 Julien Valentin
  5. *
  6. * This library is open source and may be redistributed and/or modified under
  7. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  8. * (at your option) any later version. The full license is in LICENSE file
  9. * included with this distribution, and on the openscenegraph.org website.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * OpenSceneGraph Public License for more details.
  15. */
  16. #ifndef OSG_BUFFERINDEXBINDING
  17. #define OSG_BUFFERINDEXBINDING 1
  18. #include <osg/Array>
  19. #include <osg/Export>
  20. #include <osg/BufferObject>
  21. #include <osg/StateAttribute>
  22. #ifndef GL_TRANSFORM_FEEDBACK_BUFFER
  23. #define GL_TRANSFORM_FEEDBACK_BUFFER 0x8C8E
  24. #endif
  25. namespace osg {
  26. class State;
  27. /** Encapsulate binding buffer objects to index targets. This
  28. * specifically supports the uniform buffer and transform feedback
  29. * targets.
  30. */
  31. // Common implementation superclass
  32. class OSG_EXPORT BufferIndexBinding : public StateAttribute
  33. {
  34. protected:
  35. BufferIndexBinding(GLenum target, GLuint index);
  36. BufferIndexBinding(GLenum target, GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
  37. BufferIndexBinding(const BufferIndexBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  38. public:
  39. // The member value is part of the key to this state attribute in
  40. // the State class. Using the index target, we can separately
  41. // track the bindings for many different index targets.
  42. virtual unsigned getMember() const { return static_cast<unsigned int>(_index); }
  43. GLenum getTarget() const { return _target; }
  44. ///enable arbitrary BufferBinding (user is responsible for _target mismatch with bufferdata
  45. /// what can be done is setting wrong _target and use the one of bd if not subclassed
  46. void setTarget(GLenum t){_target=t;}
  47. inline void setBufferData(BufferData *bufferdata) {
  48. if (_bufferData.valid())
  49. {
  50. _bufferData->removeClient(this);
  51. }
  52. _bufferData=bufferdata;
  53. if (_bufferData.valid())
  54. {
  55. if(!_bufferData->getBufferObject())
  56. _bufferData->setBufferObject(new VertexBufferObject());
  57. if(_size==0)
  58. _size=_bufferData->getTotalDataSize();
  59. }
  60. }
  61. /** Get the buffer data to be bound.
  62. */
  63. inline const BufferData* getBufferData() const { return _bufferData.get(); }
  64. inline BufferData* getBufferData(){ return _bufferData.get(); }
  65. /** Get the index target.
  66. */
  67. inline GLuint getIndex() const { return _index; }
  68. /** Set the index target. (and update parents StateSets)
  69. */
  70. void setIndex(GLuint index);
  71. /** Set the starting offset into the buffer data for
  72. the indexed target. Note: the required alignment on the offset
  73. may be quite large (e.g., 256 bytes on NVidia 8600M). This
  74. should be checked with glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT...).
  75. */
  76. inline void setOffset(GLintptr offset) { _offset = offset; }
  77. inline GLintptr getOffset() const { return _offset; }
  78. /** Set the size override of bufferdata binded for the indexed target.
  79. */
  80. inline void setSize(GLsizeiptr size) { _size = size; }
  81. inline GLsizeiptr getSize() const { return _size; }
  82. virtual void apply(State& state) const;
  83. protected:
  84. virtual ~BufferIndexBinding();
  85. /*const*/ GLenum _target;
  86. ref_ptr<BufferData> _bufferData;
  87. GLuint _index;
  88. GLintptr _offset;
  89. GLsizeiptr _size;
  90. };
  91. /** StateAttribute for binding a uniform buffer index target.
  92. */
  93. class OSG_EXPORT UniformBufferBinding : public BufferIndexBinding
  94. {
  95. public:
  96. UniformBufferBinding();
  97. UniformBufferBinding(GLuint index);
  98. /** Create a binding for a uniform buffer index target.
  99. * @param index the index target
  100. * @param bd associated buffer data
  101. * @param offset offset into buffer data
  102. * @param size size of data in buffer data
  103. */
  104. UniformBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
  105. UniformBufferBinding(const UniformBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  106. META_StateAttribute(osg, UniformBufferBinding, UNIFORMBUFFERBINDING);
  107. virtual int compare(const StateAttribute& bb) const
  108. {
  109. COMPARE_StateAttribute_Types(UniformBufferBinding, bb)
  110. COMPARE_StateAttribute_Parameter(_target)
  111. COMPARE_StateAttribute_Parameter(_index)
  112. COMPARE_StateAttribute_Parameter(_bufferData)
  113. COMPARE_StateAttribute_Parameter(_offset)
  114. COMPARE_StateAttribute_Parameter(_size)
  115. return 0;
  116. }
  117. };
  118. /** StateAttribute for binding a transform feedback index target.
  119. */
  120. class OSG_EXPORT TransformFeedbackBufferBinding : public BufferIndexBinding
  121. {
  122. public:
  123. TransformFeedbackBufferBinding(GLuint index = 0);
  124. TransformFeedbackBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
  125. TransformFeedbackBufferBinding(const TransformFeedbackBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  126. META_StateAttribute(osg, TransformFeedbackBufferBinding, TRANSFORMFEEDBACKBUFFERBINDING);
  127. virtual int compare(const StateAttribute& bb) const
  128. {
  129. COMPARE_StateAttribute_Types(TransformFeedbackBufferBinding, bb)
  130. COMPARE_StateAttribute_Parameter(_target)
  131. COMPARE_StateAttribute_Parameter(_index)
  132. COMPARE_StateAttribute_Parameter(_bufferData)
  133. COMPARE_StateAttribute_Parameter(_offset)
  134. COMPARE_StateAttribute_Parameter(_size)
  135. return 0;
  136. }
  137. };
  138. /** StateAttribute for binding a atomic counter buffer index target.
  139. */
  140. class OSG_EXPORT AtomicCounterBufferBinding : public BufferIndexBinding
  141. {
  142. public:
  143. AtomicCounterBufferBinding(GLuint index=0);
  144. /** Create a binding for a atomic counter buffer index target.
  145. * @param index the index target
  146. * @param bd associated buffer data
  147. * @param offset offset into buffer data
  148. * @param size size of data in buffer data
  149. */
  150. AtomicCounterBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
  151. AtomicCounterBufferBinding(const AtomicCounterBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  152. META_StateAttribute(osg, AtomicCounterBufferBinding, ATOMICCOUNTERBUFFERBINDING);
  153. void readData(osg::State & state, osg::UIntArray & uintArray) const;
  154. virtual int compare(const StateAttribute& bb) const
  155. {
  156. COMPARE_StateAttribute_Types(AtomicCounterBufferBinding, bb)
  157. COMPARE_StateAttribute_Parameter(_target)
  158. COMPARE_StateAttribute_Parameter(_index)
  159. COMPARE_StateAttribute_Parameter(_bufferData)
  160. COMPARE_StateAttribute_Parameter(_offset)
  161. COMPARE_StateAttribute_Parameter(_size)
  162. return 0;
  163. }
  164. };
  165. class OSG_EXPORT ShaderStorageBufferBinding : public BufferIndexBinding
  166. {
  167. public:
  168. ShaderStorageBufferBinding(GLuint index=0);
  169. /** Create a binding for a shader storage buffer index target.
  170. * @param index the index target
  171. * @param bd associated buffer data
  172. * @param offset offset into buffer data
  173. * @param size size of data in buffer data
  174. */
  175. ShaderStorageBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
  176. ShaderStorageBufferBinding(const ShaderStorageBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  177. META_StateAttribute(osg, ShaderStorageBufferBinding, SHADERSTORAGEBUFFERBINDING);
  178. virtual int compare(const StateAttribute& bb) const
  179. {
  180. COMPARE_StateAttribute_Types(ShaderStorageBufferBinding, bb)
  181. COMPARE_StateAttribute_Parameter(_target)
  182. COMPARE_StateAttribute_Parameter(_index)
  183. COMPARE_StateAttribute_Parameter(_bufferData)
  184. COMPARE_StateAttribute_Parameter(_offset)
  185. COMPARE_StateAttribute_Parameter(_size)
  186. return 0;
  187. }
  188. };
  189. } // namespace osg
  190. #endif