Graphics.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // Copyright (c) 2008-2017 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. // ATOMIC BEGIN
  53. #include <SDL/include/SDL.h>
  54. #include <SDL/include/SDL_syswm.h>
  55. // ATOMIC END
  56. #include "../DebugNew.h"
  57. namespace Atomic
  58. {
  59. // ATOMIC BEGIN
  60. unsigned Graphics::numPasses_ = 0;
  61. unsigned Graphics::numSinglePassPrimitives_ = 0;
  62. // ATOMIC END
  63. void Graphics::SetExternalWindow(void* window)
  64. {
  65. if (!window_)
  66. externalWindow_ = window;
  67. else
  68. ATOMIC_LOGERROR("Window already opened, can not set external window");
  69. }
  70. void Graphics::SetWindowTitle(const String& windowTitle)
  71. {
  72. windowTitle_ = windowTitle;
  73. if (window_)
  74. SDL_SetWindowTitle(window_, windowTitle_.CString());
  75. }
  76. void Graphics::SetWindowIcon(Image* windowIcon)
  77. {
  78. windowIcon_ = windowIcon;
  79. if (window_)
  80. CreateWindowIcon();
  81. }
  82. void Graphics::SetWindowPosition(const IntVector2& position)
  83. {
  84. if (window_)
  85. SDL_SetWindowPosition(window_, position.x_, position.y_);
  86. else
  87. position_ = position; // Sets as initial position for OpenWindow()
  88. }
  89. void Graphics::SetWindowPosition(int x, int y)
  90. {
  91. SetWindowPosition(IntVector2(x, y));
  92. }
  93. void Graphics::SetOrientations(const String& orientations)
  94. {
  95. orientations_ = orientations.Trimmed();
  96. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  97. }
  98. bool Graphics::ToggleFullscreen()
  99. {
  100. return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_, monitor_, refreshRate_);
  101. }
  102. void Graphics::SetShaderParameter(StringHash param, const Variant& value)
  103. {
  104. switch (value.GetType())
  105. {
  106. case VAR_BOOL:
  107. SetShaderParameter(param, value.GetBool());
  108. break;
  109. case VAR_INT:
  110. SetShaderParameter(param, value.GetInt());
  111. break;
  112. case VAR_FLOAT:
  113. case VAR_DOUBLE:
  114. SetShaderParameter(param, value.GetFloat());
  115. break;
  116. case VAR_VECTOR2:
  117. SetShaderParameter(param, value.GetVector2());
  118. break;
  119. case VAR_VECTOR3:
  120. SetShaderParameter(param, value.GetVector3());
  121. break;
  122. case VAR_VECTOR4:
  123. SetShaderParameter(param, value.GetVector4());
  124. break;
  125. case VAR_COLOR:
  126. SetShaderParameter(param, value.GetColor());
  127. break;
  128. case VAR_MATRIX3:
  129. SetShaderParameter(param, value.GetMatrix3());
  130. break;
  131. case VAR_MATRIX3X4:
  132. SetShaderParameter(param, value.GetMatrix3x4());
  133. break;
  134. case VAR_MATRIX4:
  135. SetShaderParameter(param, value.GetMatrix4());
  136. break;
  137. case VAR_BUFFER:
  138. {
  139. const PODVector<unsigned char>& buffer = value.GetBuffer();
  140. if (buffer.Size() >= sizeof(float))
  141. SetShaderParameter(param, reinterpret_cast<const float*>(&buffer[0]), buffer.Size() / sizeof(float));
  142. }
  143. break;
  144. default:
  145. // Unsupported parameter type, do nothing
  146. break;
  147. }
  148. }
  149. IntVector2 Graphics::GetWindowPosition() const
  150. {
  151. if (window_)
  152. return position_;
  153. return IntVector2::ZERO;
  154. }
  155. PODVector<IntVector3> Graphics::GetResolutions(int monitor) const
  156. {
  157. PODVector<IntVector3> ret;
  158. // Emscripten is not able to return a valid list
  159. #ifndef __EMSCRIPTEN__
  160. unsigned numModes = (unsigned)SDL_GetNumDisplayModes(monitor);
  161. for (unsigned i = 0; i < numModes; ++i)
  162. {
  163. SDL_DisplayMode mode;
  164. SDL_GetDisplayMode(monitor, i, &mode);
  165. int width = mode.w;
  166. int height = mode.h;
  167. int rate = mode.refresh_rate;
  168. // Store mode if unique
  169. bool unique = true;
  170. for (unsigned j = 0; j < ret.Size(); ++j)
  171. {
  172. if (ret[j].x_ == width && ret[j].y_ == height && ret[j].z_ == rate)
  173. {
  174. unique = false;
  175. break;
  176. }
  177. }
  178. if (unique)
  179. ret.Push(IntVector3(width, height, rate));
  180. }
  181. #endif
  182. return ret;
  183. }
  184. IntVector2 Graphics::GetDesktopResolution(int monitor) const
  185. {
  186. #if !defined(__ANDROID__) && !defined(IOS)
  187. SDL_DisplayMode mode;
  188. SDL_GetDesktopDisplayMode(monitor, &mode);
  189. return IntVector2(mode.w, mode.h);
  190. #else
  191. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  192. return IntVector2(width_, height_);
  193. #endif
  194. }
  195. int Graphics::GetMonitorCount() const
  196. {
  197. return SDL_GetNumVideoDisplays();
  198. }
  199. void Graphics::Maximize()
  200. {
  201. if (!window_)
  202. return;
  203. SDL_MaximizeWindow(window_);
  204. }
  205. void Graphics::Minimize()
  206. {
  207. if (!window_)
  208. return;
  209. SDL_MinimizeWindow(window_);
  210. }
  211. void Graphics::BeginDumpShaders(const String& fileName)
  212. {
  213. shaderPrecache_ = new ShaderPrecache(context_, fileName);
  214. }
  215. void Graphics::EndDumpShaders()
  216. {
  217. shaderPrecache_.Reset();
  218. }
  219. void Graphics::PrecacheShaders(Deserializer& source)
  220. {
  221. ATOMIC_PROFILE(PrecacheShaders);
  222. ShaderPrecache::LoadShaders(this, source);
  223. }
  224. void Graphics::SetShaderCacheDir(const String& path)
  225. {
  226. String trimmedPath = path.Trimmed();
  227. if (trimmedPath.Length())
  228. shaderCacheDir_ = AddTrailingSlash(trimmedPath);
  229. }
  230. void Graphics::AddGPUObject(GPUObject* object)
  231. {
  232. MutexLock lock(gpuObjectMutex_);
  233. gpuObjects_.Push(object);
  234. }
  235. void Graphics::RemoveGPUObject(GPUObject* object)
  236. {
  237. MutexLock lock(gpuObjectMutex_);
  238. gpuObjects_.Remove(object);
  239. }
  240. void* Graphics::ReserveScratchBuffer(unsigned size)
  241. {
  242. if (!size)
  243. return 0;
  244. if (size > maxScratchBufferRequest_)
  245. maxScratchBufferRequest_ = size;
  246. // First check for a free buffer that is large enough
  247. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  248. {
  249. if (!i->reserved_ && i->size_ >= size)
  250. {
  251. i->reserved_ = true;
  252. return i->data_.Get();
  253. }
  254. }
  255. // Then check if a free buffer can be resized
  256. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  257. {
  258. if (!i->reserved_)
  259. {
  260. i->data_ = new unsigned char[size];
  261. i->size_ = size;
  262. i->reserved_ = true;
  263. ATOMIC_LOGDEBUG("Resized scratch buffer to size " + String(size));
  264. return i->data_.Get();
  265. }
  266. }
  267. // Finally allocate a new buffer
  268. ScratchBuffer newBuffer;
  269. newBuffer.data_ = new unsigned char[size];
  270. newBuffer.size_ = size;
  271. newBuffer.reserved_ = true;
  272. scratchBuffers_.Push(newBuffer);
  273. return newBuffer.data_.Get();
  274. ATOMIC_LOGDEBUG("Allocated scratch buffer with size " + String(size));
  275. }
  276. void Graphics::FreeScratchBuffer(void* buffer)
  277. {
  278. if (!buffer)
  279. return;
  280. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  281. {
  282. if (i->reserved_ && i->data_.Get() == buffer)
  283. {
  284. i->reserved_ = false;
  285. return;
  286. }
  287. }
  288. ATOMIC_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  289. }
  290. void Graphics::CleanupScratchBuffers()
  291. {
  292. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  293. {
  294. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
  295. {
  296. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  297. i->size_ = maxScratchBufferRequest_;
  298. ATOMIC_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  299. }
  300. }
  301. maxScratchBufferRequest_ = 0;
  302. }
  303. void Graphics::CreateWindowIcon()
  304. {
  305. if (windowIcon_)
  306. {
  307. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  308. if (surface)
  309. {
  310. SDL_SetWindowIcon(window_, surface);
  311. SDL_FreeSurface(surface);
  312. }
  313. }
  314. }
  315. void RegisterGraphicsLibrary(Context* context)
  316. {
  317. Animation::RegisterObject(context);
  318. Material::RegisterObject(context);
  319. Model::RegisterObject(context);
  320. Shader::RegisterObject(context);
  321. Technique::RegisterObject(context);
  322. Texture2D::RegisterObject(context);
  323. Texture2DArray::RegisterObject(context);
  324. Texture3D::RegisterObject(context);
  325. TextureCube::RegisterObject(context);
  326. Camera::RegisterObject(context);
  327. Drawable::RegisterObject(context);
  328. Light::RegisterObject(context);
  329. StaticModel::RegisterObject(context);
  330. StaticModelGroup::RegisterObject(context);
  331. Skybox::RegisterObject(context);
  332. AnimatedModel::RegisterObject(context);
  333. AnimationController::RegisterObject(context);
  334. BillboardSet::RegisterObject(context);
  335. ParticleEffect::RegisterObject(context);
  336. ParticleEmitter::RegisterObject(context);
  337. RibbonTrail::RegisterObject(context);
  338. CustomGeometry::RegisterObject(context);
  339. DecalSet::RegisterObject(context);
  340. Terrain::RegisterObject(context);
  341. TerrainPatch::RegisterObject(context);
  342. DebugRenderer::RegisterObject(context);
  343. Octree::RegisterObject(context);
  344. Zone::RegisterObject(context);
  345. }
  346. // ATOMIC BEGIN
  347. int Graphics::GetCurrentMonitor()
  348. {
  349. return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
  350. }
  351. int Graphics::GetNumMonitors()
  352. {
  353. return SDL_GetNumVideoDisplays();
  354. }
  355. bool Graphics::GetMaximized()
  356. {
  357. if (!window_)
  358. return false;
  359. return SDL_GetWindowFlags(window_) & SDL_WINDOW_MAXIMIZED;
  360. }
  361. IntVector2 Graphics::GetMonitorResolution(int monitorId) const
  362. {
  363. SDL_DisplayMode mode;
  364. SDL_GetDesktopDisplayMode(monitorId, &mode);
  365. return IntVector2(mode.w, mode.h);
  366. }
  367. void Graphics::RaiseWindow()
  368. {
  369. if (window_)
  370. SDL_RaiseWindow(window_);
  371. }
  372. // ATOMIC END
  373. }