Torque3D Documentation / _generateds / gfxVertexBuffer.h

gfxVertexBuffer.h

Engine/source/gfx/gfxVertexBuffer.h

More...

Classes:

class

This is a non-typed vertex buffer handle which can be used when your vertex type is undefined until runtime.

class

A handle object for allocating, filling, and reading a vertex buffer.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GFXVERTEXBUFFER_H_
 25#define _GFXVERTEXBUFFER_H_
 26
 27#ifndef _GFXSTRUCTS_H_
 28#include "gfx/gfxStructs.h"
 29#endif
 30
 31
 32//*****************************************************************************
 33// GFXVertexBuffer - base vertex buffer class
 34//*****************************************************************************
 35class GFXVertexBuffer : public StrongRefBase, public GFXResource
 36{
 37   friend class GFXVertexBufferHandleBase;
 38   friend class GFXDevice;
 39
 40public:
 41
 42   /// Number of vertices in this buffer.
 43   U32 mNumVerts;
 44
 45   /// A copy of the vertex format for this buffer.
 46   GFXVertexFormat mVertexFormat;
 47
 48   /// Vertex size in bytes.
 49   U32 mVertexSize;
 50
 51   /// GFX buffer type (static, dynamic or volatile).
 52   GFXBufferType mBufferType;
 53
 54   /// Device this vertex buffer was allocated on.
 55   GFXDevice *mDevice;
 56
 57   bool  isLocked;
 58   U32   lockedVertexStart;
 59   U32   lockedVertexEnd;
 60   void* lockedVertexPtr;
 61   U32   mVolatileStart;
 62
 63   GFXVertexBuffer(  GFXDevice *device, 
 64                     U32 numVerts, 
 65                     const GFXVertexFormat *vertexFormat, 
 66                     U32 vertexSize, 
 67                     GFXBufferType bufferType )
 68      :  mNumVerts( numVerts ),
 69         mVertexSize( vertexSize ),
 70         mBufferType( bufferType ),
 71         mDevice( device ),
 72         isLocked(false),
 73         lockedVertexStart(0),
 74         lockedVertexEnd(0),
 75         lockedVertexPtr(NULL),
 76         mVolatileStart( 0 )
 77   {
 78      if ( vertexFormat )
 79      {
 80         vertexFormat->getDecl();
 81         mVertexFormat.copy( *vertexFormat );
 82      }
 83   }
 84   
 85   virtual void lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr) = 0;
 86   virtual void unlock() = 0;
 87   virtual void prepare() = 0;
 88
 89   // GFXResource
 90   virtual const String describeSelf() const;
 91};
 92
 93
 94//*****************************************************************************
 95// GFXVertexBufferHandleBase
 96//*****************************************************************************
 97class GFXVertexBufferHandleBase : public StrongRefPtr<GFXVertexBuffer>
 98{
 99   friend class GFXDevice;
100
101protected:
102
103   void set(   GFXDevice *theDevice,
104               U32 numVerts, 
105               const GFXVertexFormat *vertexFormat, 
106               U32 vertexSize,
107               GFXBufferType type );
108
109   void* lock(U32 vertexStart, U32 vertexEnd)
110   {
111      if(vertexEnd == 0)
112         vertexEnd = getPointer()->mNumVerts;
113      AssertFatal(vertexEnd > vertexStart, "Can't get a lock with the end before the start.");
114      AssertFatal(vertexEnd <= getPointer()->mNumVerts || getPointer()->mBufferType == GFXBufferTypeVolatile, "Tried to get vertices beyond the end of the buffer!");
115      getPointer()->lock(vertexStart, vertexEnd, &getPointer()->lockedVertexPtr);
116      return getPointer()->lockedVertexPtr;
117   }
118
119   void unlock() ///< unlocks the vertex data, making changes illegal.
120   {
121      getPointer()->unlock();
122   }
123};
124
125
126/// A handle object for allocating, filling, and reading a vertex buffer.
127template<class T> 
128class GFXVertexBufferHandle : public GFXVertexBufferHandleBase
129{
130   typedef GFXVertexBufferHandleBase Parent;
131
132   /// Sets this vertex buffer as the current 
133   /// vertex buffer for the device it was allocated on
134   void prepare() { getPointer()->prepare(); }
135
136public:
137
138   GFXVertexBufferHandle() {}
139
140   GFXVertexBufferHandle(  GFXDevice *theDevice, 
141                           U32 numVerts, 
142                           GFXBufferType type = GFXBufferTypeVolatile )
143   {
144      set( theDevice, numVerts, type );
145   }
146
147   ~GFXVertexBufferHandle() {}
148
149   void set(   GFXDevice *theDevice, 
150               U32 numVerts,
151               GFXBufferType type = GFXBufferTypeVolatile )
152   {
153      Parent::set( theDevice, numVerts, getGFXVertexFormat<T>(), sizeof(T), type );
154   }
155
156   T *lock(U32 vertexStart = 0, U32 vertexEnd = 0) ///< locks the vertex buffer range, and returns a pointer to the beginning of the vertex array
157                                                   ///< also allows the array operators to work on this vertex buffer.
158   {
159      return (T*)Parent::lock(vertexStart, vertexEnd);
160   }
161
162   void unlock() { Parent::unlock(); }
163
164   T& operator[](U32 index) ///< Array operator allows indexing into a locked vertex buffer.  The debug version of the code
165                            ///< will range check the array access as well as validate the locked vertex buffer pointer.
166   {
167      return ((T*)getPointer()->lockedVertexPtr)[index];
168   }
169
170   const T& operator[](U32 index) const ///< Array operator allows indexing into a locked vertex buffer.  The debug version of the code
171                                        ///< will range check the array access as well as validate the locked vertex buffer pointer.
172   {
173      index += getPointer()->mVolatileStart;
174      AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!");
175      AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!");
176      index -= getPointer()->mVolatileStart;
177      return ((T*)getPointer()->lockedVertexPtr)[index];
178   }
179
180   T& operator[](S32 index) ///< Array operator allows indexing into a locked vertex buffer.  The debug version of the code
181                            ///< will range check the array access as well as validate the locked vertex buffer pointer.
182   {
183      index += getPointer()->mVolatileStart;
184      AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!");
185      AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!");
186      index -= getPointer()->mVolatileStart;
187      return ((T*)getPointer()->lockedVertexPtr)[index];
188   }
189
190   const T& operator[](S32 index) const ///< Array operator allows indexing into a locked vertex buffer.  The debug version of the code
191                                        ///< will range check the array access as well as validate the locked vertex buffer pointer.
192   {
193      index += getPointer()->mVolatileStart;
194      AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!");
195      AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!");
196      index -= getPointer()->mVolatileStart;
197      return ((T*)getPointer()->lockedVertexPtr)[index];
198   }
199
200   GFXVertexBufferHandle<T>& operator=(GFXVertexBuffer *ptr)
201   {
202      StrongObjectRef::set(ptr);
203      return *this;
204   }
205
206};
207
208
209/// This is a non-typed vertex buffer handle which can be
210/// used when your vertex type is undefined until runtime.
211class GFXVertexBufferDataHandle : public GFXVertexBufferHandleBase         
212{
213   typedef GFXVertexBufferHandleBase Parent;
214
215protected:
216
217   void prepare() { getPointer()->prepare(); }
218
219public:
220
221   GFXVertexBufferDataHandle()
222   {
223   }
224
225   void set(   GFXDevice *theDevice, 
226               U32 vertSize, 
227               const GFXVertexFormat *vertexFormat, 
228               U32 numVerts, 
229               GFXBufferType type )
230   {
231      Parent::set( theDevice, numVerts, vertexFormat, vertSize, type );
232   }
233
234   U8* lock( U32 vertexStart = 0, U32 vertexEnd = 0 )
235   {
236      return (U8*)Parent::lock( vertexStart, vertexEnd );
237   }
238
239   void unlock() { Parent::unlock(); }
240
241   GFXVertexBufferDataHandle& operator=( GFXVertexBuffer *ptr )
242   {
243      StrongObjectRef::set(ptr);
244      return *this;
245   }
246};
247
248
249#endif // _GFXVERTEXBUFFER_H_
250
251
252