Graphics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. return position_;
  147. return IntVector2::ZERO;
  148. }
  149. PODVector<IntVector3> Graphics::GetResolutions(int monitor) const
  150. {
  151. PODVector<IntVector3> ret;
  152. // Emscripten is not able to return a valid list
  153. #ifndef __EMSCRIPTEN__
  154. unsigned numModes = (unsigned)SDL_GetNumDisplayModes(monitor);
  155. for (unsigned i = 0; i < numModes; ++i)
  156. {
  157. SDL_DisplayMode mode;
  158. SDL_GetDisplayMode(monitor, i, &mode);
  159. int width = mode.w;
  160. int height = mode.h;
  161. int rate = mode.refresh_rate;
  162. // Store mode if unique
  163. bool unique = true;
  164. for (unsigned j = 0; j < ret.Size(); ++j)
  165. {
  166. if (ret[j].x_ == width && ret[j].y_ == height && ret[j].z_ == rate)
  167. {
  168. unique = false;
  169. break;
  170. }
  171. }
  172. if (unique)
  173. ret.Push(IntVector3(width, height, rate));
  174. }
  175. #endif
  176. return ret;
  177. }
  178. IntVector2 Graphics::GetDesktopResolution(int monitor) const
  179. {
  180. #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  181. SDL_DisplayMode mode;
  182. SDL_GetDesktopDisplayMode(monitor, &mode);
  183. return IntVector2(mode.w, mode.h);
  184. #else
  185. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  186. return IntVector2(width_, height_);
  187. #endif
  188. }
  189. int Graphics::GetMonitorCount() const
  190. {
  191. return SDL_GetNumVideoDisplays();
  192. }
  193. void Graphics::Maximize()
  194. {
  195. if (!window_)
  196. return;
  197. SDL_MaximizeWindow(window_);
  198. }
  199. void Graphics::Minimize()
  200. {
  201. if (!window_)
  202. return;
  203. SDL_MinimizeWindow(window_);
  204. }
  205. void Graphics::BeginDumpShaders(const String& fileName)
  206. {
  207. shaderPrecache_ = new ShaderPrecache(context_, fileName);
  208. }
  209. void Graphics::EndDumpShaders()
  210. {
  211. shaderPrecache_.Reset();
  212. }
  213. void Graphics::PrecacheShaders(Deserializer& source)
  214. {
  215. URHO3D_PROFILE(PrecacheShaders);
  216. ShaderPrecache::LoadShaders(this, source);
  217. }
  218. void Graphics::SetShaderCacheDir(const String& path)
  219. {
  220. String trimmedPath = path.Trimmed();
  221. if (trimmedPath.Length())
  222. shaderCacheDir_ = AddTrailingSlash(trimmedPath);
  223. }
  224. void Graphics::AddGPUObject(GPUObject* object)
  225. {
  226. MutexLock lock(gpuObjectMutex_);
  227. gpuObjects_.Push(object);
  228. }
  229. void Graphics::RemoveGPUObject(GPUObject* object)
  230. {
  231. MutexLock lock(gpuObjectMutex_);
  232. gpuObjects_.Remove(object);
  233. }
  234. void* Graphics::ReserveScratchBuffer(unsigned size)
  235. {
  236. if (!size)
  237. return nullptr;
  238. if (size > maxScratchBufferRequest_)
  239. maxScratchBufferRequest_ = size;
  240. // First check for a free buffer that is large enough
  241. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  242. {
  243. if (!i->reserved_ && i->size_ >= size)
  244. {
  245. i->reserved_ = true;
  246. return i->data_.Get();
  247. }
  248. }
  249. // Then check if a free buffer can be resized
  250. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  251. {
  252. if (!i->reserved_)
  253. {
  254. i->data_ = new unsigned char[size];
  255. i->size_ = size;
  256. i->reserved_ = true;
  257. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
  258. return i->data_.Get();
  259. }
  260. }
  261. // Finally allocate a new buffer
  262. ScratchBuffer newBuffer;
  263. newBuffer.data_ = new unsigned char[size];
  264. newBuffer.size_ = size;
  265. newBuffer.reserved_ = true;
  266. scratchBuffers_.Push(newBuffer);
  267. return newBuffer.data_.Get();
  268. URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
  269. }
  270. void Graphics::FreeScratchBuffer(void* buffer)
  271. {
  272. if (!buffer)
  273. return;
  274. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  275. {
  276. if (i->reserved_ && i->data_.Get() == buffer)
  277. {
  278. i->reserved_ = false;
  279. return;
  280. }
  281. }
  282. URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  283. }
  284. void Graphics::CleanupScratchBuffers()
  285. {
  286. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  287. {
  288. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
  289. {
  290. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : nullptr;
  291. i->size_ = maxScratchBufferRequest_;
  292. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  293. }
  294. }
  295. maxScratchBufferRequest_ = 0;
  296. }
  297. void Graphics::CreateWindowIcon()
  298. {
  299. if (windowIcon_)
  300. {
  301. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  302. if (surface)
  303. {
  304. SDL_SetWindowIcon(window_, surface);
  305. SDL_FreeSurface(surface);
  306. }
  307. }
  308. }
  309. void RegisterGraphicsLibrary(Context* context)
  310. {
  311. Animation::RegisterObject(context);
  312. Material::RegisterObject(context);
  313. Model::RegisterObject(context);
  314. Shader::RegisterObject(context);
  315. Technique::RegisterObject(context);
  316. Texture2D::RegisterObject(context);
  317. Texture2DArray::RegisterObject(context);
  318. Texture3D::RegisterObject(context);
  319. TextureCube::RegisterObject(context);
  320. Camera::RegisterObject(context);
  321. Drawable::RegisterObject(context);
  322. Light::RegisterObject(context);
  323. StaticModel::RegisterObject(context);
  324. StaticModelGroup::RegisterObject(context);
  325. Skybox::RegisterObject(context);
  326. AnimatedModel::RegisterObject(context);
  327. AnimationController::RegisterObject(context);
  328. BillboardSet::RegisterObject(context);
  329. ParticleEffect::RegisterObject(context);
  330. ParticleEmitter::RegisterObject(context);
  331. RibbonTrail::RegisterObject(context);
  332. CustomGeometry::RegisterObject(context);
  333. DecalSet::RegisterObject(context);
  334. Terrain::RegisterObject(context);
  335. TerrainPatch::RegisterObject(context);
  336. DebugRenderer::RegisterObject(context);
  337. Octree::RegisterObject(context);
  338. Zone::RegisterObject(context);
  339. }
  340. }