00001
00002 #ifndef _TP_BUFFER_H
00003 #define _TP_BUFFER_H
00004
00005 #include <cstdio>
00006 #include <cassert>
00007
00008 #include "GLResource.h"
00009 #include "GL/GLPlatform.h"
00010 #include "GL/TPProgramName.h"
00011 #include "GL/TPAttributes.h"
00012
00013
00014 namespace gk {
00015
00016
00017 class GLBuffer : public GLResource
00018 {
00019 protected:
00020 GLuint m_name;
00021 GLenum m_usage;
00022 unsigned int m_length;
00023 unsigned int m_count;
00024
00025
00026 GLBuffer( const GLBuffer& );
00027 GLBuffer& operator=( const GLBuffer& );
00028
00029
00030
00031
00032
00033
00034 GLBuffer( const int state,
00035 const unsigned int count, const unsigned int length, const void *data, const GLenum usage= GL_STATIC_DRAW )
00036 :
00037 m_name(0),
00038 m_usage(usage),
00039 m_length(length),
00040 m_count(count)
00041 {
00042 glGenBuffers(1, &m_name);
00043
00044 ActiveAttributes[state].setBuffer(this);
00045
00046 glBufferData(ActiveAttributes[state].target(), m_length, data, m_usage);
00047 }
00048
00049
00050 int clear( const int state )
00051 {
00052 if(m_name == 0)
00053 return -1;
00054
00055 ActiveAttributes[state].setBuffer(this);
00056
00057 glBufferData(ActiveAttributes[state].target(), m_length, NULL, m_usage);
00058 return 0;
00059 }
00060
00061
00062
00063
00064 int update( const int state,
00065 const unsigned long int offset, const unsigned long int length, const void *data )
00066 {
00067 if(m_name == 0)
00068 return -1;
00069 if(offset + length > m_length)
00070 return -1;
00071
00072 ActiveAttributes[state].setBuffer(this);
00073 glBufferSubData(ActiveAttributes[state].target(), offset, length, data);
00074 return 0;
00075 }
00076
00077
00078 void *map( const int state,
00079 const unsigned long int offset, const unsigned int length, const GLbitfield access )
00080 {
00081 if(m_name == 0)
00082 return NULL;
00083 if(offset + length > m_length)
00084 {
00085 printf("GLBuffer::map( ): offset + length > buffer length\n");
00086 return NULL;
00087 }
00088
00089 ActiveAttributes[state].setBuffer(this);
00090
00091 return glMapBufferRange(ActiveAttributes[state].target(), offset, length, access);
00092 }
00093
00094 int unmap( const int state )
00095 {
00096 if(m_name == 0)
00097 return -1;
00098
00099 ActiveAttributes[state].setBuffer(this);
00100 glUnmapBuffer(ActiveAttributes[state].target());
00101 return 0;
00102 }
00103
00104 int flush( const int state,
00105 const unsigned long int offset, const unsigned int length )
00106 {
00107 if(m_name == 0)
00108 return -1;
00109 if(offset + length > m_length)
00110 {
00111 printf("GLBuffer::flush( ): offset + length > buffer length\n");
00112 return -1;
00113 }
00114
00115 ActiveAttributes[state].setBuffer(this);
00116
00117 glFlushMappedBufferRange(ActiveAttributes[state].target(), offset, length);
00118 return 0;
00119 }
00120
00121 public:
00122
00123 int bindAsFeedbackAttribute( const ProgramFeedback& attribute )
00124 {
00125 if(m_name == 0)
00126 return -1;
00127 if(attribute.index() < 0)
00128 return -1;
00129
00130
00131 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, attribute.index(), m_name);
00132 return 0;
00133 }
00134
00135 #ifdef GK_OPENGL4
00136
00137 int bindAsIndirect( )
00138 {
00139 if(m_name == 0)
00140 return -1;
00141
00142
00143 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_name);
00144 return 0;
00145 }
00146 #endif
00147
00148
00149 int bindAsIndex( )
00150 {
00151 if(m_name == 0)
00152 return -1;
00153
00154 ActiveAttributes[INDEX].setBuffer(this);
00155
00156 return 0;
00157 }
00158
00159 public:
00160
00161 virtual ~GLBuffer( )
00162 {
00163 if(m_name != 0)
00164 glDeleteBuffers(1, &m_name);
00165 }
00166
00167
00168 int createGLResource( )
00169 {
00170 return (m_name != 0) ? 0 : -1;
00171 }
00172
00173
00174 int releaseGLResource( )
00175 {
00176 return (m_name != 0) ? 0 : -1;
00177 }
00178
00179
00180 GLuint name( ) const
00181 {
00182 return m_name;
00183 }
00184
00185
00186 unsigned int count( ) const
00187 {
00188 if(m_name == 0)
00189 return 0;
00190 return m_count;
00191 }
00192
00193 static
00194 void unbindIndex( )
00195 {
00196
00197 }
00198
00199 static
00200 void unbindVertexAttribute( const ProgramAttribute& attribute )
00201 {
00202 if(attribute.location() < 0)
00203 return;
00204 glDisableVertexAttribArray(attribute.location());
00205 }
00206
00207 static
00208 void unbindInstanceAttribute( const ProgramAttribute& attribute )
00209 {
00210 if(attribute.location() < 0)
00211 return;
00212 glDisableVertexAttribArray(attribute.location());
00213 }
00214
00215 static
00216 void unbindFeedbackAttribute( const ProgramFeedback& attribute )
00217 {
00218
00219
00220 }
00221
00222 #ifdef GK_OPENGL4
00223 static
00224 void unbindIndirect( )
00225 {
00226
00227 }
00228 #endif
00229 };
00230
00231
00232 class GLAttributeBuffer : public GLBuffer
00233 {
00234 public:
00235
00236 GLAttributeBuffer( const unsigned int count, const unsigned int length, const void *data, const GLenum usage= GL_STATIC_DRAW )
00237 :
00238 GLBuffer(ATTRIBUTE, count, length, data, usage)
00239 {}
00240
00241
00242 ~GLAttributeBuffer( ) {}
00243
00244
00245 int clear( )
00246 {
00247 return GLBuffer::clear(ATTRIBUTE);
00248 }
00249
00250
00251 int update( const unsigned long int offset, const unsigned long int length, const void *data )
00252 {
00253 return GLBuffer::update(ATTRIBUTE, offset, length, data );
00254 }
00255
00256
00257 void *map( const unsigned long int offset, const unsigned long int length, const GLbitfield access )
00258 {
00259 return GLBuffer::map(ATTRIBUTE, offset, length, access );
00260 }
00261
00262
00263 int unmap( )
00264 {
00265 return GLBuffer::unmap(ATTRIBUTE);
00266 }
00267
00268
00269 int flush( const unsigned long int offset, const unsigned long int length )
00270 {
00271 return GLBuffer::flush(ATTRIBUTE, offset, length);
00272 }
00273 };
00274
00275
00276 class GLIndexBuffer : public GLBuffer
00277 {
00278 public:
00279 GLIndexBuffer( const unsigned int count, const unsigned int length, const void *data, const GLenum usage= GL_STATIC_DRAW )
00280 :
00281 GLBuffer(INDEX, count, length, data, usage )
00282 {}
00283
00284 ~GLIndexBuffer( ) {}
00285
00286 int clear( )
00287 {
00288 return GLBuffer::clear(INDEX);
00289 }
00290
00291 int update( const unsigned long int offset, const unsigned long int length, const void *data )
00292 {
00293 return GLBuffer::update(INDEX, offset, length, data );
00294 }
00295
00296 void *map( const unsigned long int offset, const unsigned long int length, const GLbitfield access )
00297 {
00298 return GLBuffer::map(INDEX, offset, length, access );
00299 }
00300
00301 int unmap( )
00302 {
00303 return GLBuffer::unmap(INDEX);
00304 }
00305
00306 int flush( const unsigned long int offset, const unsigned long int length )
00307 {
00308 return GLBuffer::flush(INDEX, offset, length);
00309 }
00310 };
00311
00312 }
00313
00314 #endif