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
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.
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.
17#ifndef OSG_BUFFERINDEXBINDING
18#define OSG_BUFFERINDEXBINDING 1
22#include <osg/BufferObject>
23#include <osg/StateAttribute>
25#ifndef GL_TRANSFORM_FEEDBACK_BUFFER
26 #define GL_TRANSFORM_FEEDBACK_BUFFER 0x8C8E
34/** Encapsulate binding buffer objects to index targets. This
35 * specifically supports the uniform buffer and transform feedback
39// Common implementation superclass
40class OSG_EXPORT BufferIndexBinding : public StateAttribute
43 BufferIndexBinding(GLenum target, GLuint index);
44 BufferIndexBinding(GLenum target, GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
45 BufferIndexBinding(const BufferIndexBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
47 // The member value is part of the key to this state attribute in
48 // the State class. Using the index target, we can separately
49 // track the bindings for many different index targets.
50 virtual unsigned getMember() const { return static_cast<unsigned int>(_index); }
51 GLenum getTarget() const { return _target; }
52 ///enable arbitrary BufferBinding (user is responsible for _target mismatch with bufferdata
53 /// what can be done is setting wrong _target and use the one of bd if not subclassed
54 void setTarget(GLenum t){_target=t;}
56 inline void setBufferData(BufferData *bufferdata) {
57 if (_bufferData.valid())
59 _bufferData->removeClient(this);
62 _bufferData=bufferdata;
64 if (_bufferData.valid())
66 if(!_bufferData->getBufferObject())
67 _bufferData->setBufferObject(new VertexBufferObject());
69 _size=_bufferData->getTotalDataSize();
72 /** Get the buffer data to be bound.
74 inline const BufferData* getBufferData() const { return _bufferData.get(); }
75 inline BufferData* getBufferData(){ return _bufferData.get(); }
77 /** Get the index target.
79 inline GLuint getIndex() const { return _index; }
80 /** Set the index target. (and update parents StateSets)
82 void setIndex(GLuint index);
85 /** Set the starting offset into the buffer data for
86 the indexed target. Note: the required alignment on the offset
87 may be quite large (e.g., 256 bytes on NVidia 8600M). This
88 should be checked with glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT...).
90 inline void setOffset(GLintptr offset) { _offset = offset; }
91 inline GLintptr getOffset() const { return _offset; }
93 /** Set the size override of bufferdata binded for the indexed target.
95 inline void setSize(GLsizeiptr size) { _size = size; }
96 inline GLsizeiptr getSize() const { return _size; }
98 virtual void apply(State& state) const;
101 virtual ~BufferIndexBinding();
102 /*const*/ GLenum _target;
103 ref_ptr<BufferData> _bufferData;
109/** StateAttribute for binding a uniform buffer index target.
111class OSG_EXPORT UniformBufferBinding : public BufferIndexBinding
114 UniformBufferBinding();
115 UniformBufferBinding(GLuint index);
116 /** Create a binding for a uniform buffer index target.
117 * @param index the index target
118 * @param bd associated buffer data
119 * @param offset offset into buffer data
120 * @param size size of data in buffer data
122 UniformBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
123 UniformBufferBinding(const UniformBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
124 META_StateAttribute(osg, UniformBufferBinding, UNIFORMBUFFERBINDING);
126 virtual int compare(const StateAttribute& bb) const
128 COMPARE_StateAttribute_Types(UniformBufferBinding, bb)
129 COMPARE_StateAttribute_Parameter(_target)
130 COMPARE_StateAttribute_Parameter(_index)
131 COMPARE_StateAttribute_Parameter(_bufferData)
132 COMPARE_StateAttribute_Parameter(_offset)
133 COMPARE_StateAttribute_Parameter(_size)
138/** StateAttribute for binding a transform feedback index target.
140class OSG_EXPORT TransformFeedbackBufferBinding : public BufferIndexBinding
143 TransformFeedbackBufferBinding(GLuint index = 0);
144 TransformFeedbackBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
145 TransformFeedbackBufferBinding(const TransformFeedbackBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
146 META_StateAttribute(osg, TransformFeedbackBufferBinding, TRANSFORMFEEDBACKBUFFERBINDING);
148 virtual int compare(const StateAttribute& bb) const
150 COMPARE_StateAttribute_Types(TransformFeedbackBufferBinding, bb)
151 COMPARE_StateAttribute_Parameter(_target)
152 COMPARE_StateAttribute_Parameter(_index)
153 COMPARE_StateAttribute_Parameter(_bufferData)
154 COMPARE_StateAttribute_Parameter(_offset)
155 COMPARE_StateAttribute_Parameter(_size)
160/** StateAttribute for binding a atomic counter buffer index target.
162class OSG_EXPORT AtomicCounterBufferBinding : public BufferIndexBinding
165 AtomicCounterBufferBinding(GLuint index=0);
166 /** Create a binding for a atomic counter buffer index target.
167 * @param index the index target
168 * @param bd associated buffer data
169 * @param offset offset into buffer data
170 * @param size size of data in buffer data
172 AtomicCounterBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
173 AtomicCounterBufferBinding(const AtomicCounterBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
174 META_StateAttribute(osg, AtomicCounterBufferBinding, ATOMICCOUNTERBUFFERBINDING);
176 void readData(osg::State & state, osg::UIntArray & uintArray) const;
178 virtual int compare(const StateAttribute& bb) const
180 COMPARE_StateAttribute_Types(AtomicCounterBufferBinding, 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)
190class OSG_EXPORT ShaderStorageBufferBinding : public BufferIndexBinding
193 ShaderStorageBufferBinding(GLuint index=0);
194 /** Create a binding for a shader storage buffer index target.
195 * @param index the index target
196 * @param bd associated buffer data
197 * @param offset offset into buffer data
198 * @param size size of data in buffer data
200 ShaderStorageBufferBinding(GLuint index, BufferData* bd, GLintptr offset=0, GLsizeiptr size=0);
201 ShaderStorageBufferBinding(const ShaderStorageBufferBinding& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
202 META_StateAttribute(osg, ShaderStorageBufferBinding, SHADERSTORAGEBUFFERBINDING);
204 virtual int compare(const StateAttribute& bb) const
206 COMPARE_StateAttribute_Types(ShaderStorageBufferBinding, bb)
207 COMPARE_StateAttribute_Parameter(_target)
208 COMPARE_StateAttribute_Parameter(_index)
209 COMPARE_StateAttribute_Parameter(_bufferData)
210 COMPARE_StateAttribute_Parameter(_offset)
211 COMPARE_StateAttribute_Parameter(_size)