gfxPrimitiveBuffer.h
Engine/source/gfx/gfxPrimitiveBuffer.h
Classes:
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 _GFXPRIMITIVEBUFFER_H_ 25#define _GFXPRIMITIVEBUFFER_H_ 26 27#ifndef _GFXSTRUCTS_H_ 28#include "gfx/gfxStructs.h" 29#endif 30 31#ifdef TORQUE_ENABLE_PROFILER 32#include "platform/profiler.h" 33#endif 34 35 36class GFXPrimitiveBuffer : public StrongRefBase, public GFXResource 37{ 38 friend class GFXPrimitiveBufferHandle; 39 friend class GFXDevice; 40 41public: //protected: 42 43 U32 mIndexCount; 44 U32 mPrimitiveCount; 45 GFXBufferType mBufferType; 46 GFXPrimitive *mPrimitiveArray; 47 GFXDevice *mDevice; 48 49 U32 mVolatileStart; 50 51#ifdef TORQUE_DEBUG 52 // In debug builds we provide a TOC leak tracking system. 53 static U32 smActivePBCount; 54 static GFXPrimitiveBuffer *smHead; 55 static void dumpActivePBs(); 56 57 String mDebugCreationPath; 58 GFXPrimitiveBuffer *mDebugNext; 59 GFXPrimitiveBuffer *mDebugPrev; 60#endif 61 62 GFXPrimitiveBuffer( GFXDevice *device, 63 U32 indexCount, 64 U32 primitiveCount, 65 GFXBufferType bufferType ) : 66 mVolatileStart(0) 67 { 68 mDevice = device; 69 mIndexCount = indexCount; 70 mPrimitiveCount = primitiveCount; 71 mBufferType = bufferType; 72 if(primitiveCount) 73 { 74 mPrimitiveArray = new GFXPrimitive[primitiveCount]; 75 dMemset((void *) mPrimitiveArray, 0, primitiveCount * sizeof(GFXPrimitive)); 76 } 77 else 78 mPrimitiveArray = NULL; 79 80#if defined(TORQUE_DEBUG) 81 // Active copy tracking. 82 smActivePBCount++; 83#if defined(TORQUE_ENABLE_PROFILE_PATH) 84 mDebugCreationPath = gProfiler->getProfilePath(); 85#endif 86 mDebugNext = smHead; 87 mDebugPrev = NULL; 88 89 if(smHead) 90 { 91 AssertFatal(smHead->mDebugPrev == NULL, "GFXPrimitiveBuffer::GFXPrimitiveBuffer - found unexpected previous in current head!"); 92 smHead->mDebugPrev = this; 93 } 94 95 smHead = this; 96#endif 97 } 98 99 virtual ~GFXPrimitiveBuffer() 100 { 101 if( mPrimitiveArray != NULL ) 102 { 103 delete [] mPrimitiveArray; 104 mPrimitiveArray = NULL; 105 } 106 107#ifdef TORQUE_DEBUG 108 if(smHead == this) 109 smHead = this->mDebugNext; 110 111 if(mDebugNext) 112 mDebugNext->mDebugPrev = mDebugPrev; 113 114 if(mDebugPrev) 115 mDebugPrev->mDebugNext = mDebugNext; 116 117 mDebugPrev = mDebugNext = NULL; 118 119 smActivePBCount--; 120#endif 121 } 122 123 virtual void lock(U32 indexStart, U32 indexEnd, void **indexPtr)=0; ///< locks this primitive buffer for writing into 124 virtual void unlock()=0; ///< unlocks this primitive buffer. 125 virtual void prepare()=0; ///< prepares this primitive buffer for use on the device it was allocated on 126 127 // GFXResource interface 128 /// The resource should put a description of itself (number of vertices, size/width of texture, etc.) in buffer 129 virtual const String describeSelf() const; 130}; 131 132class GFXPrimitiveBufferHandle : public StrongRefPtr<GFXPrimitiveBuffer> 133{ 134 typedef StrongRefPtr<GFXPrimitiveBuffer> Parent; 135public: 136 enum Constants { 137 MaxIndexCount = 65535, 138 }; 139 GFXPrimitiveBufferHandle() {}; 140 141 GFXPrimitiveBufferHandle(GFXDevice *theDevice, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType, String desc = String::EmptyString ) 142 { 143 set(theDevice, indexCount, primitiveCount, bufferType, desc); 144 } 145 146 void set(GFXDevice *theDevice, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType, String desc = String::EmptyString ); 147 148 void immutable(GFXDevice *theDevice, U32 indexCount, U32 primitiveCount, void* data, String desc = String::EmptyString ); 149 150 void lock(U16 **indexBuffer, GFXPrimitive **primitiveBuffer = NULL, U32 indexStart = 0, U32 indexEnd = 0) 151 { 152 if(indexEnd == 0) 153 indexEnd = getPointer()->mIndexCount; 154 AssertFatal(indexStart < indexEnd && indexEnd <= getPointer()->mIndexCount, "Out of range index lock!"); 155 getPointer()->lock(indexStart, indexEnd, (void**)indexBuffer); 156 if(primitiveBuffer) 157 *primitiveBuffer = getPointer()->mPrimitiveArray; 158 } 159 160 void unlock() 161 { 162 getPointer()->unlock(); 163 } 164 165 void prepare() 166 { 167 getPointer()->prepare(); 168 } 169 170 bool operator==(const GFXPrimitiveBufferHandle &buffer) const { 171 return getPointer() == buffer.getPointer(); 172 } 173 174 GFXPrimitiveBufferHandle& operator=(GFXPrimitiveBuffer *ptr) 175 { 176 StrongObjectRef::set(ptr); 177 return *this; 178 } 179}; 180 181#endif // _GFXPRIMITIVEBUFFER_H_ 182