gfxNullDevice.cpp 12 KB

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