gfxVertexBuffer.h
Engine/source/gfx/gfxVertexBuffer.h
Classes:
class
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 mVolatileStart( 0 ) 73 { 74 if ( vertexFormat ) 75 { 76 vertexFormat->getDecl(); 77 mVertexFormat.copy( *vertexFormat ); 78 } 79 } 80 81 virtual void lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr) = 0; 82 virtual void unlock() = 0; 83 virtual void prepare() = 0; 84 85 // GFXResource 86 virtual const String describeSelf() const; 87}; 88 89 90//***************************************************************************** 91// GFXVertexBufferHandleBase 92//***************************************************************************** 93class GFXVertexBufferHandleBase : public StrongRefPtr<GFXVertexBuffer> 94{ 95 friend class GFXDevice; 96 97protected: 98 99 void set( GFXDevice *theDevice, 100 U32 numVerts, 101 const GFXVertexFormat *vertexFormat, 102 U32 vertexSize, 103 GFXBufferType type ); 104 105 void* lock(U32 vertexStart, U32 vertexEnd) 106 { 107 if(vertexEnd == 0) 108 vertexEnd = getPointer()->mNumVerts; 109 AssertFatal(vertexEnd > vertexStart, "Can't get a lock with the end before the start."); 110 AssertFatal(vertexEnd <= getPointer()->mNumVerts || getPointer()->mBufferType == GFXBufferTypeVolatile, "Tried to get vertices beyond the end of the buffer!"); 111 getPointer()->lock(vertexStart, vertexEnd, &getPointer()->lockedVertexPtr); 112 return getPointer()->lockedVertexPtr; 113 } 114 115 void unlock() ///< unlocks the vertex data, making changes illegal. 116 { 117 getPointer()->unlock(); 118 } 119}; 120 121 122/// A handle object for allocating, filling, and reading a vertex buffer. 123template<class T> 124class GFXVertexBufferHandle : public GFXVertexBufferHandleBase 125{ 126 typedef GFXVertexBufferHandleBase Parent; 127 128 /// Sets this vertex buffer as the current 129 /// vertex buffer for the device it was allocated on 130 void prepare() { getPointer()->prepare(); } 131 132public: 133 134 GFXVertexBufferHandle() {} 135 136 GFXVertexBufferHandle( GFXDevice *theDevice, 137 U32 numVerts, 138 GFXBufferType type = GFXBufferTypeVolatile ) 139 { 140 set( theDevice, numVerts, type ); 141 } 142 143 ~GFXVertexBufferHandle() {} 144 145 void set( GFXDevice *theDevice, 146 U32 numVerts, 147 GFXBufferType type = GFXBufferTypeVolatile ) 148 { 149 Parent::set( theDevice, numVerts, getGFXVertexFormat<T>(), sizeof(T), type ); 150 } 151 152 T *lock(U32 vertexStart = 0, U32 vertexEnd = 0) ///< locks the vertex buffer range, and returns a pointer to the beginning of the vertex array 153 ///< also allows the array operators to work on this vertex buffer. 154 { 155 return (T*)Parent::lock(vertexStart, vertexEnd); 156 } 157 158 void unlock() { Parent::unlock(); } 159 160 T& operator[](U32 index) ///< Array operator allows indexing into a locked vertex buffer. The debug version of the code 161 ///< will range check the array access as well as validate the locked vertex buffer pointer. 162 { 163 return ((T*)getPointer()->lockedVertexPtr)[index]; 164 } 165 166 const T& operator[](U32 index) const ///< Array operator allows indexing into a locked vertex buffer. The debug version of the code 167 ///< will range check the array access as well as validate the locked vertex buffer pointer. 168 { 169 index += getPointer()->mVolatileStart; 170 AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!"); 171 AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!"); 172 index -= getPointer()->mVolatileStart; 173 return ((T*)getPointer()->lockedVertexPtr)[index]; 174 } 175 176 T& operator[](S32 index) ///< Array operator allows indexing into a locked vertex buffer. The debug version of the code 177 ///< will range check the array access as well as validate the locked vertex buffer pointer. 178 { 179 index += getPointer()->mVolatileStart; 180 AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!"); 181 AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!"); 182 index -= getPointer()->mVolatileStart; 183 return ((T*)getPointer()->lockedVertexPtr)[index]; 184 } 185 186 const T& operator[](S32 index) const ///< Array operator allows indexing into a locked vertex buffer. The debug version of the code 187 ///< will range check the array access as well as validate the locked vertex buffer pointer. 188 { 189 index += getPointer()->mVolatileStart; 190 AssertFatal(getPointer()->lockedVertexPtr != NULL, "Cannot access verts from an unlocked vertex buffer!!!"); 191 AssertFatal(index >= getPointer()->lockedVertexStart && index < getPointer()->lockedVertexEnd, "Out of range vertex access!"); 192 index -= getPointer()->mVolatileStart; 193 return ((T*)getPointer()->lockedVertexPtr)[index]; 194 } 195 196 GFXVertexBufferHandle<T>& operator=(GFXVertexBuffer *ptr) 197 { 198 StrongObjectRef::set(ptr); 199 return *this; 200 } 201 202}; 203 204 205/// This is a non-typed vertex buffer handle which can be 206/// used when your vertex type is undefined until runtime. 207class GFXVertexBufferDataHandle : public GFXVertexBufferHandleBase 208{ 209 typedef GFXVertexBufferHandleBase Parent; 210 211protected: 212 213 void prepare() { getPointer()->prepare(); } 214 215public: 216 217 GFXVertexBufferDataHandle() 218 { 219 } 220 221 void set( GFXDevice *theDevice, 222 U32 vertSize, 223 const GFXVertexFormat *vertexFormat, 224 U32 numVerts, 225 GFXBufferType type ) 226 { 227 Parent::set( theDevice, numVerts, vertexFormat, vertSize, type ); 228 } 229 230 U8* lock( U32 vertexStart = 0, U32 vertexEnd = 0 ) 231 { 232 return (U8*)Parent::lock( vertexStart, vertexEnd ); 233 } 234 235 void unlock() { Parent::unlock(); } 236 237 GFXVertexBufferDataHandle& operator=( GFXVertexBuffer *ptr ) 238 { 239 StrongObjectRef::set(ptr); 240 return *this; 241 } 242}; 243 244 245#endif // _GFXVERTEXBUFFER_H_ 246 247 248
