Graphics.cpp 12 KB

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