gfxNullDevice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/Null/gfxNullDevice.h"
  24. #include "core/strings/stringFunctions.h"
  25. #include "gfx/gfxCubemap.h"
  26. #include "gfx/screenshot.h"
  27. #include "gfx/gfxPrimitiveBuffer.h"
  28. #include "gfx/gfxCardProfile.h"
  29. #include "gfx/gfxTextureManager.h"
  30. #include "gfx/bitmap/gBitmap.h"
  31. #include "core/util/safeDelete.h"
  32. GFXAdapter::CreateDeviceInstanceDelegate GFXNullDevice::mCreateDeviceInstance(GFXNullDevice::createInstance);
  33. class GFXNullCardProfiler: public GFXCardProfiler
  34. {
  35. private:
  36. typedef GFXCardProfiler Parent;
  37. public:
  38. ///
  39. virtual const String &getRendererString() const { static String sRS("GFX Null Device Renderer"); return sRS; }
  40. protected:
  41. virtual void setupCardCapabilities() { };
  42. virtual bool _queryCardCap(const String &query, U32 &foundResult){ return false; }
  43. virtual bool _queryFormat(const GFXFormat fmt, const GFXTextureProfile *profile, bool &inOutAutogenMips) { inOutAutogenMips = false; return false; }
  44. public:
  45. virtual void init()
  46. {
  47. mCardDescription = "GFX Null Device Card";
  48. mChipSet = "NULL Device";
  49. mVersionString = "0";
  50. Parent::init(); // other code notes that not calling this is "BAD".
  51. };
  52. };
  53. class GFXNullTextureObject : public GFXTextureObject
  54. {
  55. public:
  56. GFXNullTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile);
  57. ~GFXNullTextureObject() { kill(); };
  58. virtual void pureVirtualCrash() { };
  59. virtual GFXLockedRect * lock( U32 mipLevel = 0, RectI *inRect = NULL ) { return NULL; };
  60. virtual void unlock( U32 mipLevel = 0) {};
  61. virtual bool copyToBmp(GBitmap *) { return false; };
  62. virtual void zombify() {}
  63. virtual void resurrect() {}
  64. };
  65. GFXNullTextureObject::GFXNullTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile) :
  66. GFXTextureObject(aDevice, profile)
  67. {
  68. mProfile = profile;
  69. mTextureSize.set( 0, 0, 0 );
  70. }
  71. class GFXNullTextureManager : public GFXTextureManager
  72. {
  73. protected:
  74. virtual GFXTextureObject *_createTextureObject( U32 height,
  75. U32 width,
  76. U32 depth,
  77. GFXFormat format,
  78. GFXTextureProfile *profile,
  79. U32 numMipLevels,
  80. bool forceMips = false,
  81. S32 antialiasLevel = 0,
  82. GFXTextureObject *inTex = NULL )
  83. {
  84. GFXNullTextureObject *retTex;
  85. if ( inTex )
  86. {
  87. AssertFatal( dynamic_cast<GFXNullTextureObject*>( inTex ), "GFXNullTextureManager::_createTexture() - Bad inTex type!" );
  88. retTex = static_cast<GFXNullTextureObject*>( inTex );
  89. }
  90. else
  91. {
  92. retTex = new GFXNullTextureObject( GFX, profile );
  93. retTex->registerResourceWithDevice( GFX );
  94. }
  95. SAFE_DELETE( retTex->mBitmap );
  96. retTex->mBitmap = new GBitmap(width, height);
  97. return retTex;
  98. };
  99. /// Load a texture from a proper DDSFile instance.
  100. virtual bool _loadTexture(GFXTextureObject *texture, DDSFile *dds){ return true; };
  101. /// Load data into a texture from a GBitmap using the internal API.
  102. virtual bool _loadTexture(GFXTextureObject *texture, GBitmap *bmp){ return true; };
  103. /// Load data into a texture from a raw buffer using the internal API.
  104. ///
  105. /// Note that the size of the buffer is assumed from the parameters used
  106. /// for this GFXTextureObject's _createTexture call.
  107. virtual bool _loadTexture(GFXTextureObject *texture, void *raw){ return true; };
  108. /// Refresh a texture using the internal API.
  109. virtual bool _refreshTexture(GFXTextureObject *texture){ return true; };
  110. /// Free a texture (but do not delete the GFXTextureObject) using the internal
  111. /// API.
  112. ///
  113. /// This is only called during zombification for textures which need it, so you
  114. /// don't need to do any internal safety checks.
  115. virtual bool _freeTexture(GFXTextureObject *texture, bool zombify=false) { return true; };
  116. virtual U32 _getTotalVideoMemory() { return 0; };
  117. virtual U32 _getFreeVideoMemory() { return 0; };
  118. };
  119. class GFXNullCubemap : public GFXCubemap
  120. {
  121. friend class GFXDevice;
  122. private:
  123. // should only be called by GFXDevice
  124. virtual void setToTexUnit( U32 tuNum ) { };
  125. public:
  126. virtual void initStatic( GFXTexHandle *faces ) { };
  127. virtual void initStatic( DDSFile *dds ) { };
  128. virtual void initDynamic( U32 texSize, GFXFormat faceFormat = GFXFormatR8G8B8A8 ) { };
  129. virtual U32 getSize() const { return 0; }
  130. virtual GFXFormat getFormat() const { return GFXFormatR8G8B8A8; }
  131. virtual ~GFXNullCubemap(){};
  132. virtual void zombify() {}
  133. virtual void resurrect() {}
  134. };
  135. class GFXNullVertexBuffer : public GFXVertexBuffer
  136. {
  137. unsigned char* tempBuf;
  138. public:
  139. GFXNullVertexBuffer( GFXDevice *device,
  140. U32 numVerts,
  141. const GFXVertexFormat *vertexFormat,
  142. U32 vertexSize,
  143. GFXBufferType bufferType ) :
  144. GFXVertexBuffer(device, numVerts, vertexFormat, vertexSize, bufferType) { };
  145. virtual void lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr);
  146. virtual void unlock();
  147. virtual void prepare();
  148. virtual void zombify() {}
  149. virtual void resurrect() {}
  150. };
  151. void GFXNullVertexBuffer::lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr)
  152. {
  153. tempBuf = new unsigned char[(vertexEnd - vertexStart) * mVertexSize];
  154. *vertexPtr = (void*) tempBuf;
  155. lockedVertexStart = vertexStart;
  156. lockedVertexEnd = vertexEnd;
  157. }
  158. void GFXNullVertexBuffer::unlock()
  159. {
  160. delete[] tempBuf;
  161. tempBuf = NULL;
  162. }
  163. void GFXNullVertexBuffer::prepare()
  164. {
  165. }
  166. class GFXNullPrimitiveBuffer : public GFXPrimitiveBuffer
  167. {
  168. private:
  169. U16* temp;
  170. public:
  171. GFXNullPrimitiveBuffer( GFXDevice *device,
  172. U32 indexCount,
  173. U32 primitiveCount,
  174. GFXBufferType bufferType ) :
  175. GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType), temp( NULL ) {};
  176. virtual void lock(U32 indexStart, U32 indexEnd, void **indexPtr); ///< locks this primitive buffer for writing into
  177. virtual void unlock(); ///< unlocks this primitive buffer.
  178. virtual void prepare() { }; ///< prepares this primitive buffer for use on the device it was allocated on
  179. virtual void zombify() {}
  180. virtual void resurrect() {}
  181. };
  182. void GFXNullPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  183. {
  184. temp = new U16[indexEnd - indexStart];
  185. *indexPtr = temp;
  186. }
  187. void GFXNullPrimitiveBuffer::unlock()
  188. {
  189. delete[] temp;
  190. temp = NULL;
  191. }
  192. //
  193. // GFXNullStateBlock
  194. //
  195. class GFXNullStateBlock : public GFXStateBlock
  196. {
  197. public:
  198. /// Returns the hash value of the desc that created this block
  199. virtual U32 getHashValue() const { return 0; };
  200. /// Returns a GFXStateBlockDesc that this block represents
  201. virtual const GFXStateBlockDesc& getDesc() const { return mDefaultDesc; }
  202. //
  203. // GFXResource
  204. //
  205. virtual void zombify() { }
  206. /// When called the resource should restore all device sensitive information destroyed by zombify()
  207. virtual void resurrect() { }
  208. private:
  209. GFXStateBlockDesc mDefaultDesc;
  210. };
  211. //
  212. // GFXNullDevice
  213. //
  214. GFXDevice *GFXNullDevice::createInstance( U32 adapterIndex )
  215. {
  216. return new GFXNullDevice();
  217. }
  218. GFXNullDevice::GFXNullDevice()
  219. {
  220. clip.set(0, 0, 800, 800);
  221. mTextureManager = new GFXNullTextureManager();
  222. gScreenShot = new ScreenShot();
  223. mCardProfiler = new GFXNullCardProfiler();
  224. mCardProfiler->init();
  225. }
  226. GFXNullDevice::~GFXNullDevice()
  227. {
  228. }
  229. GFXVertexBuffer *GFXNullDevice::allocVertexBuffer( U32 numVerts,
  230. const GFXVertexFormat *vertexFormat,
  231. U32 vertSize,
  232. GFXBufferType bufferType )
  233. {
  234. return new GFXNullVertexBuffer(GFX, numVerts, vertexFormat, vertSize, bufferType);
  235. }
  236. GFXPrimitiveBuffer *GFXNullDevice::allocPrimitiveBuffer( U32 numIndices,
  237. U32 numPrimitives,
  238. GFXBufferType bufferType)
  239. {
  240. return new GFXNullPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
  241. }
  242. GFXCubemap* GFXNullDevice::createCubemap()
  243. {
  244. return new GFXNullCubemap();
  245. };
  246. void GFXNullDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
  247. {
  248. // Add the NULL renderer
  249. GFXAdapter *toAdd = new GFXAdapter();
  250. toAdd->mIndex = 0;
  251. toAdd->mType = NullDevice;
  252. toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
  253. GFXVideoMode vm;
  254. vm.bitDepth = 32;
  255. vm.resolution.set(800,600);
  256. toAdd->mAvailableModes.push_back(vm);
  257. dStrcpy(toAdd->mName, "GFX Null Device");
  258. adapterList.push_back(toAdd);
  259. }
  260. void GFXNullDevice::setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable)
  261. {
  262. }
  263. void GFXNullDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
  264. {
  265. mCardProfiler = new GFXNullCardProfiler();
  266. mCardProfiler->init();
  267. }
  268. GFXStateBlockRef GFXNullDevice::createStateBlockInternal(const GFXStateBlockDesc& desc)
  269. {
  270. return new GFXNullStateBlock();
  271. }
  272. //
  273. // Register this device with GFXInit
  274. //
  275. class GFXNullRegisterDevice
  276. {
  277. public:
  278. GFXNullRegisterDevice()
  279. {
  280. GFXInit::getRegisterDeviceSignal().notify(&GFXNullDevice::enumerateAdapters);
  281. }
  282. };
  283. static GFXNullRegisterDevice pNullRegisterDevice;