Torque3D Documentation / _generateds / gfxNullDevice.cpp

gfxNullDevice.cpp

Engine/source/gfx/Null/gfxNullDevice.cpp

More...

Classes:

Detailed Description

Public Variables

GFXNullRegisterDevice pNullRegisterDevice 
  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 "platform/platform.h"
 25#include "gfx/Null/gfxNullDevice.h"
 26
 27#include "core/strings/stringFunctions.h"
 28#include "gfx/gfxCubemap.h"
 29#include "gfx/screenshot.h"
 30#include "gfx/gfxPrimitiveBuffer.h"
 31#include "gfx/gfxCardProfile.h"
 32#include "gfx/gfxTextureManager.h"
 33#include "gfx/bitmap/gBitmap.h"
 34#include "core/util/safeDelete.h"
 35
 36
 37GFXAdapter::CreateDeviceInstanceDelegate GFXNullDevice::mCreateDeviceInstance(GFXNullDevice::createInstance); 
 38
 39class GFXNullCardProfiler: public GFXCardProfiler
 40{
 41private:
 42   typedef GFXCardProfiler Parent;
 43public:
 44
 45   ///
 46   virtual const String &getRendererString() const { static String sRS("GFX Null Device Renderer"); return sRS; }
 47
 48protected:
 49
 50   virtual void setupCardCapabilities() { };
 51
 52   virtual bool _queryCardCap(const String &query, U32 &foundResult){ return false; }
 53   virtual bool _queryFormat(const GFXFormat fmt, const GFXTextureProfile *profile, bool &inOutAutogenMips) { inOutAutogenMips = false; return false; }
 54   
 55public:
 56   virtual void init()
 57   {
 58      mCardDescription = "GFX Null Device Card";
 59      mChipSet = "NULL Device";
 60      mVersionString = "0";
 61
 62      Parent::init(); // other code notes that not calling this is "BAD".
 63   };
 64};
 65
 66class GFXNullTextureObject : public GFXTextureObject 
 67{
 68public:
 69   GFXNullTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile); 
 70   ~GFXNullTextureObject() { kill(); };
 71
 72   virtual void pureVirtualCrash() { };
 73
 74   virtual GFXLockedRect * lock( U32 mipLevel = 0, RectI *inRect = NULL ) { return NULL; };
 75   virtual void unlock( U32 mipLevel = 0) {};
 76   virtual bool copyToBmp(GBitmap *) { return false; };
 77
 78   virtual void zombify() {}
 79   virtual void resurrect() {}
 80};
 81
 82GFXNullTextureObject::GFXNullTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile) :
 83   GFXTextureObject(aDevice, profile) 
 84{
 85   mProfile = profile;
 86   mTextureSize.set( 0, 0, 0 );
 87}
 88
 89class GFXNullTextureManager : public GFXTextureManager
 90{
 91protected:
 92      virtual GFXTextureObject *_createTextureObject( U32 height, 
 93                                                      U32 width, 
 94                                                      U32 depth, 
 95                                                      GFXFormat format, 
 96                                                      GFXTextureProfile *profile, 
 97                                                      U32 numMipLevels, 
 98                                                      bool forceMips = false, 
 99                                                      S32 antialiasLevel = 0, 
100                                                      GFXTextureObject *inTex = NULL )
101      { 
102         GFXNullTextureObject *retTex;
103         if ( inTex )
104         {
105            AssertFatal( dynamic_cast<GFXNullTextureObject*>( inTex ), "GFXNullTextureManager::_createTexture() - Bad inTex type!" );
106            retTex = static_cast<GFXNullTextureObject*>( inTex );
107         }      
108         else
109         {
110            retTex = new GFXNullTextureObject( GFX, profile );
111            retTex->registerResourceWithDevice( GFX );
112         }
113
114         SAFE_DELETE( retTex->mBitmap );
115         retTex->mBitmap = new GBitmap(width, height);
116         return retTex;
117      };
118
119      /// Load a texture from a proper DDSFile instance.
120      virtual bool _loadTexture(GFXTextureObject *texture, DDSFile *dds){ return true; };
121
122      /// Load data into a texture from a GBitmap using the internal API.
123      virtual bool _loadTexture(GFXTextureObject *texture, GBitmap *bmp){ return true; };
124
125      /// Load data into a texture from a raw buffer using the internal API.
126      ///
127      /// Note that the size of the buffer is assumed from the parameters used
128      /// for this GFXTextureObject's _createTexture call.
129      virtual bool _loadTexture(GFXTextureObject *texture, void *raw){ return true; };
130
131      /// Refresh a texture using the internal API.
132      virtual bool _refreshTexture(GFXTextureObject *texture){ return true; };
133
134      /// Free a texture (but do not delete the GFXTextureObject) using the internal
135      /// API.
136      ///
137      /// This is only called during zombification for textures which need it, so you
138      /// don't need to do any internal safety checks.
139      virtual bool _freeTexture(GFXTextureObject *texture, bool zombify=false) { return true; };
140
141      virtual U32 _getTotalVideoMemory() { return 0; };
142      virtual U32 _getFreeVideoMemory() { return 0; };
143};
144
145class GFXNullCubemap : public GFXCubemap
146{
147   friend class GFXDevice;
148private:
149   // should only be called by GFXDevice
150   virtual void setToTexUnit( U32 tuNum ) { };
151
152public:
153   virtual void initStatic( GFXTexHandle *faces ) { };
154   virtual void initStatic( DDSFile *dds ) { };
155   virtual void initDynamic( U32 texSize, GFXFormat faceFormat = GFXFormatR8G8B8A8 ) { };
156   virtual U32 getSize() const { return 0; }
157   virtual GFXFormat getFormat() const { return GFXFormatR8G8B8A8; }
158
159   virtual ~GFXNullCubemap(){};
160
161   virtual void zombify() {}
162   virtual void resurrect() {}
163};
164
165class GFXNullVertexBuffer : public GFXVertexBuffer 
166{
167   unsigned char* tempBuf;
168public:
169   GFXNullVertexBuffer( GFXDevice *device, 
170                        U32 numVerts, 
171                        const GFXVertexFormat *vertexFormat, 
172                        U32 vertexSize, 
173                        GFXBufferType bufferType ) :
174      GFXVertexBuffer(device, numVerts, vertexFormat, vertexSize, bufferType) { };
175   virtual void lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr);
176   virtual void unlock();
177   virtual void prepare();
178
179   virtual void zombify() {}
180   virtual void resurrect() {}
181};
182
183void GFXNullVertexBuffer::lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr) 
184{
185   tempBuf = new unsigned char[(vertexEnd - vertexStart) * mVertexSize];
186   *vertexPtr = (void*) tempBuf;
187   lockedVertexStart = vertexStart;
188   lockedVertexEnd   = vertexEnd;
189}
190
191void GFXNullVertexBuffer::unlock() 
192{
193   delete[] tempBuf;
194   tempBuf = NULL;
195}
196
197void GFXNullVertexBuffer::prepare() 
198{
199}
200
201class GFXNullPrimitiveBuffer : public GFXPrimitiveBuffer
202{
203private:
204   U16* temp;
205public:
206   GFXNullPrimitiveBuffer( GFXDevice *device, 
207                           U32 indexCount, 
208                           U32 primitiveCount, 
209                           GFXBufferType bufferType ) :
210      GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType), temp( NULL ) {};
211
212   virtual void lock(U32 indexStart, U32 indexEnd, void **indexPtr); ///< locks this primitive buffer for writing into
213   virtual void unlock(); ///< unlocks this primitive buffer.
214   virtual void prepare() { };  ///< prepares this primitive buffer for use on the device it was allocated on
215
216   virtual void zombify() {}
217   virtual void resurrect() {}
218};
219
220void GFXNullPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
221{
222   temp = new U16[indexEnd - indexStart];
223   *indexPtr = temp;
224}
225
226void GFXNullPrimitiveBuffer::unlock() 
227{
228   delete[] temp;
229   temp = NULL;
230}
231
232//
233// GFXNullStateBlock
234//
235class GFXNullStateBlock : public GFXStateBlock
236{
237public:
238   /// Returns the hash value of the desc that created this block
239   virtual U32 getHashValue() const { return 0; };
240
241   /// Returns a GFXStateBlockDesc that this block represents
242   virtual const GFXStateBlockDesc& getDesc() const { return mDefaultDesc; }
243
244   //
245   // GFXResource
246   //
247   virtual void zombify() { }
248   /// When called the resource should restore all device sensitive information destroyed by zombify()
249   virtual void resurrect() { }
250private:
251   GFXStateBlockDesc mDefaultDesc;
252};
253
254//
255// GFXNullDevice
256//
257
258GFXDevice *GFXNullDevice::createInstance( U32 adapterIndex )
259{
260   return new GFXNullDevice();
261}
262
263GFXNullDevice::GFXNullDevice()
264{
265   clip.set(0, 0, 800, 800);
266
267   mTextureManager = new GFXNullTextureManager();
268   gScreenShot = new ScreenShot();
269   mCardProfiler = new GFXNullCardProfiler();
270   mCardProfiler->init();
271}
272
273GFXNullDevice::~GFXNullDevice()
274{
275}
276
277GFXVertexBuffer *GFXNullDevice::allocVertexBuffer( U32 numVerts, 
278                                                   const GFXVertexFormat *vertexFormat,
279                                                   U32 vertSize, 
280                                                   GFXBufferType bufferType,
281                                                   void* data ) 
282{
283   return new GFXNullVertexBuffer(GFX, numVerts, vertexFormat, vertSize, bufferType);
284}
285
286GFXPrimitiveBuffer *GFXNullDevice::allocPrimitiveBuffer( U32 numIndices, 
287                                                         U32 numPrimitives, 
288                                                         GFXBufferType bufferType,
289                                                         void* data ) 
290{
291   return new GFXNullPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
292}
293
294GFXCubemap* GFXNullDevice::createCubemap()
295{ 
296   return new GFXNullCubemap(); 
297};
298
299void GFXNullDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
300{
301   // Add the NULL renderer
302   GFXAdapter *toAdd = new GFXAdapter();
303
304   toAdd->mIndex = 0;
305   toAdd->mType  = NullDevice;
306   toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
307
308   GFXVideoMode vm;
309   vm.bitDepth = 32;
310   vm.resolution.set(800,600);
311   toAdd->mAvailableModes.push_back(vm);
312
313   dStrcpy(toAdd->mName, "GFX Null Device");
314
315   adapterList.push_back(toAdd);
316}
317
318void GFXNullDevice::setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable)
319{
320
321}
322
323void GFXNullDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
324{
325   mCardProfiler = new GFXNullCardProfiler();
326   mCardProfiler->init();
327}
328
329GFXStateBlockRef GFXNullDevice::createStateBlockInternal(const GFXStateBlockDesc& desc)
330{
331   return new GFXNullStateBlock();
332}
333
334//
335// Register this device with GFXInit
336//
337class GFXNullRegisterDevice
338{
339public:
340   GFXNullRegisterDevice()
341   {
342      GFXInit::getRegisterDeviceSignal().notify(&GFXNullDevice::enumerateAdapters);
343   }
344};
345
346static GFXNullRegisterDevice pNullRegisterDevice;
347