PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Atomic/Graphics/Graphics.cpp

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