gfxNullDevice.cpp 12 KB

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