gfxNullDevice.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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, U32 mipLevels = 0) { };
  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 GFXNullCubemapArray : public GFXCubemapArray
  136. {
  137. friend class GFXDevice;
  138. private:
  139. // should only be called by GFXDevice
  140. virtual void setToTexUnit(U32 tuNum) { };
  141. public:
  142. virtual void init(GFXCubemapHandle *cubemaps, const U32 cubemapCount) { };
  143. virtual void init(const U32 cubemapCount, const U32 cubemapFaceSize, const GFXFormat format) { };
  144. virtual void updateTexture(const GFXCubemapHandle &cubemap, const U32 slot) { };
  145. virtual void copyTo(GFXCubemapArray *pDstCubemap) { }
  146. virtual ~GFXNullCubemapArray() {};
  147. virtual void zombify() {}
  148. virtual void resurrect() {}
  149. };
  150. class GFXNullVertexBuffer : public GFXVertexBuffer
  151. {
  152. unsigned char* tempBuf;
  153. public:
  154. GFXNullVertexBuffer( GFXDevice *device,
  155. U32 numVerts,
  156. const GFXVertexFormat *vertexFormat,
  157. U32 vertexSize,
  158. GFXBufferType bufferType ) :
  159. GFXVertexBuffer(device, numVerts, vertexFormat, vertexSize, bufferType) { };
  160. virtual void lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr);
  161. virtual void unlock();
  162. virtual void prepare();
  163. virtual void zombify() {}
  164. virtual void resurrect() {}
  165. };
  166. void GFXNullVertexBuffer::lock(U32 vertexStart, U32 vertexEnd, void **vertexPtr)
  167. {
  168. tempBuf = new unsigned char[(vertexEnd - vertexStart) * mVertexSize];
  169. *vertexPtr = (void*) tempBuf;
  170. lockedVertexStart = vertexStart;
  171. lockedVertexEnd = vertexEnd;
  172. }
  173. void GFXNullVertexBuffer::unlock()
  174. {
  175. delete[] tempBuf;
  176. tempBuf = NULL;
  177. }
  178. void GFXNullVertexBuffer::prepare()
  179. {
  180. }
  181. class GFXNullPrimitiveBuffer : public GFXPrimitiveBuffer
  182. {
  183. private:
  184. U16* temp;
  185. public:
  186. GFXNullPrimitiveBuffer( GFXDevice *device,
  187. U32 indexCount,
  188. U32 primitiveCount,
  189. GFXBufferType bufferType ) :
  190. GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType), temp( NULL ) {};
  191. virtual void lock(U32 indexStart, U32 indexEnd, void **indexPtr); ///< locks this primitive buffer for writing into
  192. virtual void unlock(); ///< unlocks this primitive buffer.
  193. virtual void prepare() { }; ///< prepares this primitive buffer for use on the device it was allocated on
  194. virtual void zombify() {}
  195. virtual void resurrect() {}
  196. };
  197. void GFXNullPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  198. {
  199. temp = new U16[indexEnd - indexStart];
  200. *indexPtr = temp;
  201. }
  202. void GFXNullPrimitiveBuffer::unlock()
  203. {
  204. delete[] temp;
  205. temp = NULL;
  206. }
  207. //
  208. // GFXNullStateBlock
  209. //
  210. class GFXNullStateBlock : public GFXStateBlock
  211. {
  212. public:
  213. /// Returns the hash value of the desc that created this block
  214. virtual U32 getHashValue() const { return 0; };
  215. /// Returns a GFXStateBlockDesc that this block represents
  216. virtual const GFXStateBlockDesc& getDesc() const { return mDefaultDesc; }
  217. //
  218. // GFXResource
  219. //
  220. virtual void zombify() { }
  221. /// When called the resource should restore all device sensitive information destroyed by zombify()
  222. virtual void resurrect() { }
  223. private:
  224. GFXStateBlockDesc mDefaultDesc;
  225. };
  226. //
  227. // GFXNullDevice
  228. //
  229. GFXDevice *GFXNullDevice::createInstance( U32 adapterIndex )
  230. {
  231. return new GFXNullDevice();
  232. }
  233. GFXNullDevice::GFXNullDevice()
  234. {
  235. clip.set(0, 0, 800, 800);
  236. mTextureManager = new GFXNullTextureManager();
  237. gScreenShot = new ScreenShot();
  238. mCardProfiler = new GFXNullCardProfiler();
  239. mCardProfiler->init();
  240. }
  241. GFXNullDevice::~GFXNullDevice()
  242. {
  243. }
  244. GFXVertexBuffer *GFXNullDevice::allocVertexBuffer( U32 numVerts,
  245. const GFXVertexFormat *vertexFormat,
  246. U32 vertSize,
  247. GFXBufferType bufferType,
  248. void* data )
  249. {
  250. return new GFXNullVertexBuffer(GFX, numVerts, vertexFormat, vertSize, bufferType);
  251. }
  252. GFXPrimitiveBuffer *GFXNullDevice::allocPrimitiveBuffer( U32 numIndices,
  253. U32 numPrimitives,
  254. GFXBufferType bufferType,
  255. void* data )
  256. {
  257. return new GFXNullPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
  258. }
  259. GFXCubemap* GFXNullDevice::createCubemap()
  260. {
  261. return new GFXNullCubemap();
  262. };
  263. GFXCubemapArray* GFXNullDevice::createCubemapArray()
  264. {
  265. return new GFXNullCubemapArray();
  266. };
  267. void GFXNullDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
  268. {
  269. // Add the NULL renderer
  270. GFXAdapter *toAdd = new GFXAdapter();
  271. toAdd->mIndex = 0;
  272. toAdd->mType = NullDevice;
  273. toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
  274. GFXVideoMode vm;
  275. vm.bitDepth = 32;
  276. vm.resolution.set(800,600);
  277. toAdd->mAvailableModes.push_back(vm);
  278. dStrcpy(toAdd->mName, "GFX Null Device", GFXAdapter::MaxAdapterNameLen);
  279. adapterList.push_back(toAdd);
  280. }
  281. void GFXNullDevice::setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable)
  282. {
  283. }
  284. void GFXNullDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
  285. {
  286. mCardProfiler = new GFXNullCardProfiler();
  287. mCardProfiler->init();
  288. }
  289. GFXStateBlockRef GFXNullDevice::createStateBlockInternal(const GFXStateBlockDesc& desc)
  290. {
  291. return new GFXNullStateBlock();
  292. }
  293. //
  294. // Register this device with GFXInit
  295. //
  296. class GFXNullRegisterDevice
  297. {
  298. public:
  299. GFXNullRegisterDevice()
  300. {
  301. GFXInit::getRegisterDeviceSignal().notify(&GFXNullDevice::enumerateAdapters);
  302. }
  303. };
  304. static GFXNullRegisterDevice pNullRegisterDevice;