Graphics.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../Graphics/AnimatedModel.h"
  25. #include "../Graphics/Animation.h"
  26. #include "../Graphics/AnimationController.h"
  27. #include "../Graphics/Camera.h"
  28. #include "../Graphics/CustomGeometry.h"
  29. #include "../Graphics/DebugRenderer.h"
  30. #include "../Graphics/DecalSet.h"
  31. #include "../Graphics/Graphics.h"
  32. #include "../Graphics/GraphicsImpl.h"
  33. #include "../Graphics/Material.h"
  34. #include "../Graphics/Octree.h"
  35. #include "../Graphics/ParticleEffect.h"
  36. #include "../Graphics/ParticleEmitter.h"
  37. #include "../Graphics/RibbonTrail.h"
  38. #include "../Graphics/Shader.h"
  39. #include "../Graphics/ShaderPrecache.h"
  40. #include "../Graphics/Skybox.h"
  41. #include "../Graphics/StaticModelGroup.h"
  42. #include "../Graphics/Technique.h"
  43. #include "../Graphics/Terrain.h"
  44. #include "../Graphics/TerrainPatch.h"
  45. #include "../Graphics/Texture2D.h"
  46. #include "../Graphics/Texture2DArray.h"
  47. #include "../Graphics/Texture3D.h"
  48. #include "../Graphics/TextureCube.h"
  49. #include "../Graphics/Zone.h"
  50. #include "../IO/FileSystem.h"
  51. #include "../IO/Log.h"
  52. #include <SDL/SDL.h>
  53. #include <SDL/SDL_syswm.h>
  54. #include "../DebugNew.h"
  55. namespace Urho3D
  56. {
  57. void Graphics::SetExternalWindow(void* window)
  58. {
  59. if (!window_)
  60. externalWindow_ = window;
  61. else
  62. URHO3D_LOGERROR("Window already opened, can not set external window");
  63. }
  64. void Graphics::SetWindowTitle(const String& windowTitle)
  65. {
  66. windowTitle_ = windowTitle;
  67. if (window_)
  68. SDL_SetWindowTitle(window_, windowTitle_.CString());
  69. }
  70. void Graphics::SetWindowIcon(Image* windowIcon)
  71. {
  72. windowIcon_ = windowIcon;
  73. if (window_)
  74. CreateWindowIcon();
  75. }
  76. void Graphics::SetWindowPosition(const IntVector2& position)
  77. {
  78. if (window_)
  79. SDL_SetWindowPosition(window_, position.x_, position.y_);
  80. else
  81. position_ = position; // Sets as initial position for OpenWindow()
  82. }
  83. void Graphics::SetWindowPosition(int x, int y)
  84. {
  85. SetWindowPosition(IntVector2(x, y));
  86. }
  87. void Graphics::SetOrientations(const String& orientations)
  88. {
  89. orientations_ = orientations.Trimmed();
  90. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  91. }
  92. bool Graphics::ToggleFullscreen()
  93. {
  94. return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
  95. }
  96. IntVector2 Graphics::GetWindowPosition() const
  97. {
  98. if (window_)
  99. return position_;
  100. return IntVector2::ZERO;
  101. }
  102. PODVector<IntVector2> Graphics::GetResolutions() const
  103. {
  104. PODVector<IntVector2> ret;
  105. // Emscripten is not able to return a valid list
  106. #ifndef __EMSCRIPTEN__
  107. unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
  108. for (unsigned i = 0; i < numModes; ++i)
  109. {
  110. SDL_DisplayMode mode;
  111. SDL_GetDisplayMode(0, i, &mode);
  112. int width = mode.w;
  113. int height = mode.h;
  114. // Store mode if unique
  115. bool unique = true;
  116. for (unsigned j = 0; j < ret.Size(); ++j)
  117. {
  118. if (ret[j].x_ == width && ret[j].y_ == height)
  119. {
  120. unique = false;
  121. break;
  122. }
  123. }
  124. if (unique)
  125. ret.Push(IntVector2(width, height));
  126. }
  127. #endif
  128. return ret;
  129. }
  130. IntVector2 Graphics::GetDesktopResolution() const
  131. {
  132. #if !defined(__ANDROID__) && !defined(IOS)
  133. SDL_DisplayMode mode;
  134. SDL_GetDesktopDisplayMode(0, &mode);
  135. return IntVector2(mode.w, mode.h);
  136. #else
  137. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  138. return IntVector2(width_, height_);
  139. #endif
  140. }
  141. void Graphics::Maximize()
  142. {
  143. if (!window_)
  144. return;
  145. SDL_MaximizeWindow(window_);
  146. }
  147. void Graphics::Minimize()
  148. {
  149. if (!window_)
  150. return;
  151. SDL_MinimizeWindow(window_);
  152. }
  153. void Graphics::BeginDumpShaders(const String& fileName)
  154. {
  155. shaderPrecache_ = new ShaderPrecache(context_, fileName);
  156. }
  157. void Graphics::EndDumpShaders()
  158. {
  159. shaderPrecache_.Reset();
  160. }
  161. void Graphics::PrecacheShaders(Deserializer& source)
  162. {
  163. URHO3D_PROFILE(PrecacheShaders);
  164. ShaderPrecache::LoadShaders(this, source);
  165. }
  166. void Graphics::SetShaderCacheDir(const String& path)
  167. {
  168. String trimmedPath = path.Trimmed();
  169. if (trimmedPath.Length())
  170. shaderCacheDir_ = AddTrailingSlash(trimmedPath);
  171. }
  172. void Graphics::AddGPUObject(GPUObject* object)
  173. {
  174. MutexLock lock(gpuObjectMutex_);
  175. gpuObjects_.Push(object);
  176. }
  177. void Graphics::RemoveGPUObject(GPUObject* object)
  178. {
  179. MutexLock lock(gpuObjectMutex_);
  180. gpuObjects_.Remove(object);
  181. }
  182. void* Graphics::ReserveScratchBuffer(unsigned size)
  183. {
  184. if (!size)
  185. return 0;
  186. if (size > maxScratchBufferRequest_)
  187. maxScratchBufferRequest_ = size;
  188. // First check for a free buffer that is large enough
  189. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  190. {
  191. if (!i->reserved_ && i->size_ >= size)
  192. {
  193. i->reserved_ = true;
  194. return i->data_.Get();
  195. }
  196. }
  197. // Then check if a free buffer can be resized
  198. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  199. {
  200. if (!i->reserved_)
  201. {
  202. i->data_ = new unsigned char[size];
  203. i->size_ = size;
  204. i->reserved_ = true;
  205. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
  206. return i->data_.Get();
  207. }
  208. }
  209. // Finally allocate a new buffer
  210. ScratchBuffer newBuffer;
  211. newBuffer.data_ = new unsigned char[size];
  212. newBuffer.size_ = size;
  213. newBuffer.reserved_ = true;
  214. scratchBuffers_.Push(newBuffer);
  215. return newBuffer.data_.Get();
  216. URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
  217. }
  218. void Graphics::FreeScratchBuffer(void* buffer)
  219. {
  220. if (!buffer)
  221. return;
  222. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  223. {
  224. if (i->reserved_ && i->data_.Get() == buffer)
  225. {
  226. i->reserved_ = false;
  227. return;
  228. }
  229. }
  230. URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  231. }
  232. void Graphics::CleanupScratchBuffers()
  233. {
  234. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  235. {
  236. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
  237. {
  238. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  239. i->size_ = maxScratchBufferRequest_;
  240. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  241. }
  242. }
  243. maxScratchBufferRequest_ = 0;
  244. }
  245. void Graphics::CreateWindowIcon()
  246. {
  247. if (windowIcon_)
  248. {
  249. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  250. if (surface)
  251. {
  252. SDL_SetWindowIcon(window_, surface);
  253. SDL_FreeSurface(surface);
  254. }
  255. }
  256. }
  257. void RegisterGraphicsLibrary(Context* context)
  258. {
  259. Animation::RegisterObject(context);
  260. Material::RegisterObject(context);
  261. Model::RegisterObject(context);
  262. Shader::RegisterObject(context);
  263. Technique::RegisterObject(context);
  264. Texture2D::RegisterObject(context);
  265. Texture2DArray::RegisterObject(context);
  266. Texture3D::RegisterObject(context);
  267. TextureCube::RegisterObject(context);
  268. Camera::RegisterObject(context);
  269. Drawable::RegisterObject(context);
  270. Light::RegisterObject(context);
  271. StaticModel::RegisterObject(context);
  272. StaticModelGroup::RegisterObject(context);
  273. Skybox::RegisterObject(context);
  274. AnimatedModel::RegisterObject(context);
  275. AnimationController::RegisterObject(context);
  276. BillboardSet::RegisterObject(context);
  277. ParticleEffect::RegisterObject(context);
  278. ParticleEmitter::RegisterObject(context);
  279. RibbonTrail::RegisterObject(context);
  280. CustomGeometry::RegisterObject(context);
  281. DecalSet::RegisterObject(context);
  282. Terrain::RegisterObject(context);
  283. TerrainPatch::RegisterObject(context);
  284. DebugRenderer::RegisterObject(context);
  285. Octree::RegisterObject(context);
  286. Zone::RegisterObject(context);
  287. }
  288. }