Graphics.cpp 11 KB

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