Torque3D Documentation / _generateds / gfxGLPrimitiveBuffer.cpp

gfxGLPrimitiveBuffer.cpp

Engine/source/gfx/gl/gfxGLPrimitiveBuffer.cpp

More...

Public Variables

Detailed Description

Public Variables

 MODULE_INIT 
 MODULE_SHUTDOWN 

Public Functions

getCircularVolatileIndexBuffer()

  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#include "gfx/gl/gfxGLDevice.h"
 25#include "gfx/gl/gfxGLPrimitiveBuffer.h"
 26#include "gfx/gl/gfxGLEnumTranslate.h"
 27
 28#include "gfx/gl/tGL/tGL.h"
 29#include "gfx/gl/gfxGLUtils.h"
 30
 31#include "gfx/gl/gfxGLCircularVolatileBuffer.h"
 32
 33GLCircularVolatileBuffer* getCircularVolatileIndexBuffer()
 34{
 35   static GLCircularVolatileBuffer sCircularVolatileIndexBuffer(GL_ELEMENT_ARRAY_BUFFER);
 36   return &sCircularVolatileIndexBuffer;
 37}
 38
 39GFXGLPrimitiveBuffer::GFXGLPrimitiveBuffer(GFXDevice *device, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType) :
 40   GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType),
 41   mBufferOffset(0),
 42   mZombieCache(NULL),
 43   lockedIndexEnd(0),
 44   lockedIndexStart(0)
 45{
 46   if( mBufferType == GFXBufferTypeVolatile )
 47   {
 48      mBuffer = getCircularVolatileIndexBuffer()->getHandle();
 49      return;
 50   }
 51
 52   // Generate a buffer and allocate the needed memory
 53   glGenBuffers(1, &mBuffer);
 54   
 55   PRESERVE_INDEX_BUFFER();
 56   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
 57   glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(U16), NULL, GFXGLBufferType[bufferType]);   
 58}
 59
 60GFXGLPrimitiveBuffer::~GFXGLPrimitiveBuffer()
 61{
 62   // This is heavy handed, but it frees the buffer memory
 63   if( mBufferType != GFXBufferTypeVolatile )
 64      glDeleteBuffers(1, &mBuffer);
 65   
 66   if( mZombieCache )
 67      delete [] mZombieCache;
 68}
 69
 70void GFXGLPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
 71{
 72   if( mBufferType == GFXBufferTypeVolatile )
 73   {
 74      AssertFatal(indexStart == 0, "");
 75      getCircularVolatileIndexBuffer()->lock( mIndexCount * sizeof(U16), 0, mBufferOffset, *indexPtr );
 76   }
 77   else
 78   {
 79      mFrameAllocator.lock( mIndexCount * sizeof(U16) );
 80
 81      *indexPtr = (void*)(mFrameAllocator.getlockedPtr() + (indexStart * sizeof(U16)) );
 82   }
 83
 84   lockedIndexStart = indexStart;
 85   lockedIndexEnd = indexEnd;
 86}
 87
 88void GFXGLPrimitiveBuffer::unlock()
 89{
 90   PROFILE_SCOPE(GFXGLPrimitiveBuffer_unlock);
 91
 92   if( mBufferType == GFXBufferTypeVolatile )
 93   {
 94      getCircularVolatileIndexBuffer()->unlock();
 95   }
 96   else
 97   {   
 98      U32 offset = lockedIndexStart * sizeof(U16);
 99      U32 length = (lockedIndexEnd - lockedIndexStart) * sizeof(U16);
100   
101      // Preserve previous binding
102      PRESERVE_INDEX_BUFFER();
103   
104      // Bind ourselves
105      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
106
107      if( !lockedIndexStart && lockedIndexEnd == mIndexCount)
108         glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), NULL, GFXGLBufferType[mBufferType]); // orphan the buffer
109
110      glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, length, mFrameAllocator.getlockedPtr() + offset );
111   
112      mFrameAllocator.unlock();
113   }
114
115   lockedIndexStart = 0;
116   lockedIndexEnd = 0;
117}
118
119void GFXGLPrimitiveBuffer::prepare()
120{
121   // Bind
122   GFXGLDevice* glDevice = static_cast<GFXGLDevice*>(mDevice);
123   glDevice->setPB(this);
124   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
125   glDevice->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
126}
127
128void GFXGLPrimitiveBuffer::finish()
129{
130   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
131   static_cast<GFXGLDevice*>(mDevice)->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, 0);
132}
133
134GLvoid* GFXGLPrimitiveBuffer::getBuffer()
135{
136   // NULL specifies no offset into the hardware buffer
137   return (GLvoid*)(uintptr_t)mBufferOffset;
138}
139
140void GFXGLPrimitiveBuffer::zombify()
141{
142   if(mZombieCache)
143      return;
144      
145   mZombieCache = new U8[mIndexCount * sizeof(U16)];
146   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
147   glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mIndexCount * sizeof(U16), mZombieCache);
148   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
149   glDeleteBuffers(1, &mBuffer);
150   mBuffer = 0;
151}
152
153void GFXGLPrimitiveBuffer::resurrect()
154{
155   if(!mZombieCache)
156      return;
157   
158   glGenBuffers(1, &mBuffer);
159   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
160   glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), mZombieCache, GFXGLBufferType[mBufferType]);
161   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
162   
163   delete[] mZombieCache;
164   mZombieCache = NULL;
165}
166
167namespace
168{
169   bool onGFXDeviceSignal( GFXDevice::GFXDeviceEventType type )
170   {
171      if( GFX->getAdapterType() == OpenGL && GFXDevice::deEndOfFrame == type )
172         getCircularVolatileIndexBuffer()->protectUsedRange();
173
174      return true;
175   }
176}
177
178MODULE_BEGIN( GFX_GL_PrimitiveBuffer )
179   MODULE_INIT_AFTER( gfx )
180   MODULE_SHUTDOWN_BEFORE( gfx )
181
182   MODULE_INIT
183   {
184      GFXDevice::getDeviceEventSignal( ).notify( &onGFXDeviceSignal );
185   }
186
187   MODULE_SHUTDOWN
188   {
189      GFXDevice::getDeviceEventSignal( ).remove( &onGFXDeviceSignal );
190   }
191MODULE_END
192