BsWin32RenderWindow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Win32/BsWin32RenderWindow.h"
  4. #include "Win32/BsWin32Platform.h"
  5. #include "Win32/BsWin32Window.h"
  6. #include "Win32/BsWin32VideoModeInfo.h"
  7. #include "Corethread/BsCoreThread.h"
  8. #include "Profiling/BsRenderStats.h"
  9. #include "Managers/BsRenderWindowManager.h"
  10. #include "BsVulkanRenderAPI.h"
  11. #include "BsVulkanDevice.h"
  12. #include "BsVulkanSwapChain.h"
  13. #include "BsVulkanDevice.h"
  14. #include "BsVulkanCommandBuffer.h"
  15. #include "Managers/BsVulkanCommandBufferManager.h"
  16. #include "BsVulkanQueue.h"
  17. #include "Math/BsMath.h"
  18. namespace bs
  19. {
  20. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId)
  21. : RenderWindow(desc, windowId), mProperties(desc)
  22. { }
  23. Win32RenderWindow::~Win32RenderWindow()
  24. {
  25. Platform::resetNonClientAreas(*this);
  26. }
  27. void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
  28. {
  29. if (name == "WINDOW")
  30. {
  31. UINT64 *pHwnd = (UINT64*)pData;
  32. *pHwnd = (UINT64)getHWnd();
  33. return;
  34. }
  35. }
  36. Vector2I Win32RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  37. {
  38. POINT pos;
  39. pos.x = screenPos.x;
  40. pos.y = screenPos.y;
  41. ScreenToClient(getHWnd(), &pos);
  42. return Vector2I(pos.x, pos.y);
  43. }
  44. Vector2I Win32RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  45. {
  46. POINT pos;
  47. pos.x = windowPos.x;
  48. pos.y = windowPos.y;
  49. ClientToScreen(getHWnd(), &pos);
  50. return Vector2I(pos.x, pos.y);
  51. }
  52. SPtr<ct::Win32RenderWindow> Win32RenderWindow::getCore() const
  53. {
  54. return std::static_pointer_cast<ct::Win32RenderWindow>(mCoreSpecific);
  55. }
  56. HWND Win32RenderWindow::getHWnd() const
  57. {
  58. blockUntilCoreInitialized();
  59. return getCore()->_getWindowHandle();
  60. }
  61. void Win32RenderWindow::syncProperties()
  62. {
  63. ScopedSpinLock lock(getCore()->mLock);
  64. mProperties = getCore()->mSyncedProperties;
  65. }
  66. namespace ct
  67. {
  68. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, VulkanRenderAPI& renderAPI)
  69. : RenderWindow(desc, windowId), mProperties(desc), mSyncedProperties(desc), mWindow(nullptr), mIsChild(false)
  70. , mShowOnSwap(false), mDisplayFrequency(0), mRenderAPI(renderAPI), mRequiresNewBackBuffer(true)
  71. { }
  72. Win32RenderWindow::~Win32RenderWindow()
  73. {
  74. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  75. presentDevice->waitIdle();
  76. if (mWindow != nullptr)
  77. {
  78. bs_delete(mWindow);
  79. mWindow = nullptr;
  80. }
  81. mSwapChain = nullptr;
  82. vkDestroySurfaceKHR(mRenderAPI._getInstance(), mSurface, gVulkanAllocator);
  83. }
  84. void Win32RenderWindow::initialize()
  85. {
  86. RenderWindowProperties& props = mProperties;
  87. // Create a window
  88. WINDOW_DESC windowDesc;
  89. windowDesc.showTitleBar = mDesc.showTitleBar;
  90. windowDesc.showBorder = mDesc.showBorder;
  91. windowDesc.allowResize = mDesc.allowResize;
  92. windowDesc.enableDoubleClick = true;
  93. windowDesc.fullscreen = mDesc.fullscreen;
  94. windowDesc.width = mDesc.videoMode.getWidth();
  95. windowDesc.height = mDesc.videoMode.getHeight();
  96. windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
  97. windowDesc.left = mDesc.left;
  98. windowDesc.top = mDesc.top;
  99. windowDesc.outerDimensions = false;
  100. windowDesc.title = mDesc.title;
  101. windowDesc.toolWindow = mDesc.toolWindow;
  102. windowDesc.creationParams = this;
  103. windowDesc.modal = mDesc.modal;
  104. windowDesc.wndProc = &Win32Platform::_win32WndProc;
  105. #ifdef BS_STATIC_LIB
  106. windowDesc.module = GetModuleHandle(NULL);
  107. #else
  108. windowDesc.module = GetModuleHandle("BansheeVulkanRenderAPI.dll");
  109. #endif
  110. NameValuePairList::const_iterator opt;
  111. opt = mDesc.platformSpecific.find("parentWindowHandle");
  112. if (opt != mDesc.platformSpecific.end())
  113. windowDesc.parent = (HWND)parseUINT64(opt->second);
  114. opt = mDesc.platformSpecific.find("externalWindowHandle");
  115. if (opt != mDesc.platformSpecific.end())
  116. windowDesc.external = (HWND)parseUINT64(opt->second);
  117. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  118. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  119. if (numOutputs > 0)
  120. {
  121. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  122. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  123. windowDesc.monitor = outputInfo.getMonitorHandle();
  124. }
  125. // Must be set before creating a window, since wndProc will call ShowWindow if needed after creation
  126. if (!windowDesc.external)
  127. {
  128. mShowOnSwap = mDesc.hideUntilSwap;
  129. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  130. }
  131. mWindow = bs_new<Win32Window>(windowDesc);
  132. mIsChild = windowDesc.parent != nullptr;
  133. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  134. // Update local properties
  135. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  136. props.width = mWindow->getWidth();
  137. props.height = mWindow->getHeight();
  138. props.top = mWindow->getTop();
  139. props.left = mWindow->getLeft();
  140. props.hwGamma = mDesc.gamma;
  141. props.multisampleCount = 1;
  142. // Create Vulkan surface
  143. VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
  144. surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
  145. surfaceCreateInfo.pNext = nullptr;
  146. surfaceCreateInfo.flags = 0;
  147. surfaceCreateInfo.hwnd = mWindow->getHWnd();
  148. surfaceCreateInfo.hinstance = windowDesc.module;
  149. VkInstance instance = mRenderAPI._getInstance();
  150. VkResult result = vkCreateWin32SurfaceKHR(instance, &surfaceCreateInfo, gVulkanAllocator, &mSurface);
  151. assert(result == VK_SUCCESS);
  152. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  153. VkPhysicalDevice physicalDevice = presentDevice->getPhysical();
  154. mPresentQueueFamily = presentDevice->getQueueFamily(GQT_GRAPHICS);
  155. VkBool32 supportsPresent;
  156. vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, mPresentQueueFamily, mSurface, &supportsPresent);
  157. if(!supportsPresent)
  158. {
  159. // Note: Not supporting present only queues at the moment
  160. // Note: Also present device can only return one family of graphics queue, while there could be more (some of
  161. // which support present)
  162. BS_EXCEPT(RenderingAPIException, "Cannot find a graphics queue that also supports present operations.");
  163. }
  164. uint32_t numFormats;
  165. result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numFormats, nullptr);
  166. assert(result == VK_SUCCESS);
  167. assert(numFormats > 0);
  168. VkSurfaceFormatKHR* surfaceFormats = bs_stack_alloc<VkSurfaceFormatKHR>(numFormats);
  169. result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numFormats, surfaceFormats);
  170. assert(result == VK_SUCCESS);
  171. // If there is no preferred format, use standard RGBA
  172. if ((numFormats == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED))
  173. {
  174. if (mDesc.gamma)
  175. mColorFormat = VK_FORMAT_R8G8B8A8_SRGB;
  176. else
  177. mColorFormat = VK_FORMAT_B8G8R8A8_UNORM;
  178. mColorSpace = surfaceFormats[0].colorSpace;
  179. }
  180. else
  181. {
  182. bool foundFormat = false;
  183. VkFormat wantedFormatsUNORM[] =
  184. {
  185. VK_FORMAT_R8G8B8A8_UNORM,
  186. VK_FORMAT_B8G8R8A8_UNORM,
  187. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  188. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  189. VK_FORMAT_R8G8B8_UNORM,
  190. VK_FORMAT_B8G8R8_UNORM
  191. };
  192. VkFormat wantedFormatsSRGB[] =
  193. {
  194. VK_FORMAT_R8G8B8A8_SRGB,
  195. VK_FORMAT_B8G8R8A8_SRGB,
  196. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  197. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  198. VK_FORMAT_R8G8B8_SRGB,
  199. VK_FORMAT_B8G8R8_SRGB
  200. };
  201. UINT32 numWantedFormats;
  202. VkFormat* wantedFormats;
  203. if (mDesc.gamma)
  204. {
  205. numWantedFormats = sizeof(wantedFormatsSRGB) / sizeof(wantedFormatsSRGB[0]);
  206. wantedFormats = wantedFormatsSRGB;
  207. }
  208. else
  209. {
  210. numWantedFormats = sizeof(wantedFormatsUNORM) / sizeof(wantedFormatsUNORM[0]);
  211. wantedFormats = wantedFormatsUNORM;
  212. }
  213. for(UINT32 i = 0; i < numWantedFormats; i++)
  214. {
  215. for(UINT32 j = 0; j < numFormats; j++)
  216. {
  217. if(surfaceFormats[j].format == wantedFormats[i])
  218. {
  219. mColorFormat = surfaceFormats[j].format;
  220. mColorSpace = surfaceFormats[j].colorSpace;
  221. foundFormat = true;
  222. break;
  223. }
  224. }
  225. if (foundFormat)
  226. break;
  227. }
  228. // If we haven't found anything, fall back to first available
  229. if(!foundFormat)
  230. {
  231. mColorFormat = surfaceFormats[0].format;
  232. mColorSpace = surfaceFormats[0].colorSpace;
  233. if (mDesc.gamma)
  234. LOGERR("Cannot find a valid sRGB format for a render window surface, falling back to a default format.");
  235. }
  236. }
  237. mDepthFormat = VK_FORMAT_D24_UNORM_S8_UINT;
  238. bs_stack_free(surfaceFormats);
  239. // Create swap chain
  240. mSwapChain = bs_shared_ptr_new<VulkanSwapChain>();
  241. mSwapChain->rebuild(presentDevice, mSurface, props.width, props.height, props.vsync, mColorFormat, mColorSpace,
  242. mDesc.depthBuffer, mDepthFormat);
  243. // Make the window full screen if required
  244. if (!windowDesc.external)
  245. {
  246. if (props.isFullScreen)
  247. {
  248. DEVMODE displayDeviceMode;
  249. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  250. displayDeviceMode.dmSize = sizeof(DEVMODE);
  251. displayDeviceMode.dmBitsPerPel = 32;
  252. displayDeviceMode.dmPelsWidth = props.width;
  253. displayDeviceMode.dmPelsHeight = props.height;
  254. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  255. if (mDisplayFrequency)
  256. {
  257. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  258. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  259. if (ChangeDisplaySettingsEx(NULL, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  260. {
  261. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
  262. }
  263. }
  264. if (ChangeDisplaySettingsEx(NULL, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  265. {
  266. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
  267. }
  268. }
  269. }
  270. {
  271. ScopedSpinLock lock(mLock);
  272. mSyncedProperties = props;
  273. }
  274. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  275. RenderWindow::initialize();
  276. }
  277. void Win32RenderWindow::acquireBackBuffer()
  278. {
  279. // We haven't presented the current back buffer yet, so just use that one
  280. if (!mRequiresNewBackBuffer)
  281. return;
  282. mSwapChain->acquireBackBuffer();
  283. mRequiresNewBackBuffer = false;
  284. }
  285. void Win32RenderWindow::swapBuffers(UINT32 syncMask)
  286. {
  287. THROW_IF_NOT_CORE_THREAD;
  288. if (mShowOnSwap)
  289. setHidden(false);
  290. // Get a command buffer on which we'll submit
  291. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  292. // Assuming present queue is always graphics
  293. assert(presentDevice->getQueueFamily(GQT_GRAPHICS) == mPresentQueueFamily);
  294. // Find an appropriate queue to execute on
  295. VulkanQueue* queue = presentDevice->getQueue(GQT_GRAPHICS, 0);
  296. UINT32 queueMask = presentDevice->getQueueMask(GQT_GRAPHICS, 0);
  297. // Ignore myself
  298. syncMask &= ~queueMask;
  299. UINT32 deviceIdx = presentDevice->getIndex();
  300. VulkanCommandBufferManager& cbm = static_cast<VulkanCommandBufferManager&>(CommandBufferManager::instance());
  301. UINT32 numSemaphores;
  302. cbm.getSyncSemaphores(deviceIdx, syncMask, mSemaphoresTemp, numSemaphores);
  303. // Wait on present (i.e. until the back buffer becomes available), if we haven't already done so
  304. const SwapChainSurface& surface = mSwapChain->getBackBuffer();
  305. if(surface.needsWait)
  306. {
  307. mSemaphoresTemp[numSemaphores] = mSwapChain->getBackBuffer().sync;
  308. numSemaphores++;
  309. mSwapChain->notifyBackBufferWaitIssued();
  310. }
  311. queue->present(mSwapChain.get(), mSemaphoresTemp, numSemaphores);
  312. mRequiresNewBackBuffer = true;
  313. }
  314. void Win32RenderWindow::move(INT32 left, INT32 top)
  315. {
  316. THROW_IF_NOT_CORE_THREAD;
  317. RenderWindowProperties& props = mProperties;
  318. if (!props.isFullScreen)
  319. {
  320. mWindow->move(left, top);
  321. props.top = mWindow->getTop();
  322. props.left = mWindow->getLeft();
  323. {
  324. ScopedSpinLock lock(mLock);
  325. mSyncedProperties.top = props.top;
  326. mSyncedProperties.left = props.left;
  327. }
  328. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  329. }
  330. }
  331. void Win32RenderWindow::resize(UINT32 width, UINT32 height)
  332. {
  333. THROW_IF_NOT_CORE_THREAD;
  334. RenderWindowProperties& props = mProperties;
  335. if (!props.isFullScreen)
  336. {
  337. mWindow->resize(width, height);
  338. props.width = mWindow->getWidth();
  339. props.height = mWindow->getHeight();
  340. {
  341. ScopedSpinLock lock(mLock);
  342. mSyncedProperties.width = props.width;
  343. mSyncedProperties.height = props.height;
  344. }
  345. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  346. }
  347. }
  348. void Win32RenderWindow::setActive(bool state)
  349. {
  350. THROW_IF_NOT_CORE_THREAD;
  351. mWindow->setActive(state);
  352. RenderWindow::setActive(state);
  353. }
  354. void Win32RenderWindow::setHidden(bool hidden)
  355. {
  356. THROW_IF_NOT_CORE_THREAD;
  357. mShowOnSwap = false;
  358. mWindow->setHidden(hidden);
  359. RenderWindow::setHidden(hidden);
  360. }
  361. void Win32RenderWindow::minimize()
  362. {
  363. THROW_IF_NOT_CORE_THREAD;
  364. mWindow->minimize();
  365. }
  366. void Win32RenderWindow::maximize()
  367. {
  368. THROW_IF_NOT_CORE_THREAD;
  369. mWindow->maximize();
  370. }
  371. void Win32RenderWindow::restore()
  372. {
  373. THROW_IF_NOT_CORE_THREAD;
  374. mWindow->restore();
  375. }
  376. void Win32RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  377. {
  378. THROW_IF_NOT_CORE_THREAD;
  379. if (mIsChild)
  380. return;
  381. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  382. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  383. if (numOutputs == 0)
  384. return;
  385. RenderWindowProperties& props = mProperties;
  386. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  387. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  388. mDisplayFrequency = Math::roundToInt(refreshRate);
  389. props.isFullScreen = true;
  390. DEVMODE displayDeviceMode;
  391. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  392. displayDeviceMode.dmSize = sizeof(DEVMODE);
  393. displayDeviceMode.dmBitsPerPel = 32;
  394. displayDeviceMode.dmPelsWidth = width;
  395. displayDeviceMode.dmPelsHeight = height;
  396. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  397. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  398. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  399. MONITORINFOEX monitorInfo;
  400. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  401. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  402. GetMonitorInfo(hMonitor, &monitorInfo);
  403. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  404. {
  405. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  406. }
  407. props.top = monitorInfo.rcMonitor.top;
  408. props.left = monitorInfo.rcMonitor.left;
  409. props.width = width;
  410. props.height = height;
  411. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  412. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, 0);
  413. SetWindowPos(mWindow->getHWnd(), HWND_TOP, props.left, props.top, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  414. }
  415. void Win32RenderWindow::setFullscreen(const VideoMode& mode)
  416. {
  417. THROW_IF_NOT_CORE_THREAD;
  418. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  419. }
  420. void Win32RenderWindow::setWindowed(UINT32 width, UINT32 height)
  421. {
  422. THROW_IF_NOT_CORE_THREAD;
  423. RenderWindowProperties& props = mProperties;
  424. if (!props.isFullScreen)
  425. return;
  426. props.isFullScreen = false;
  427. props.width = width;
  428. props.height = height;
  429. // Drop out of fullscreen
  430. ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
  431. UINT32 winWidth = width;
  432. UINT32 winHeight = height;
  433. RECT rect;
  434. SetRect(&rect, 0, 0, winWidth, winHeight);
  435. AdjustWindowRect(&rect, mWindow->getStyle(), false);
  436. winWidth = rect.right - rect.left;
  437. winHeight = rect.bottom - rect.top;
  438. // Deal with centering when switching down to smaller resolution
  439. HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
  440. MONITORINFO monitorInfo;
  441. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  442. monitorInfo.cbSize = sizeof(MONITORINFO);
  443. GetMonitorInfo(hMonitor, &monitorInfo);
  444. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  445. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  446. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  447. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  448. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle() | WS_VISIBLE);
  449. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
  450. SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
  451. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  452. {
  453. ScopedSpinLock lock(mLock);
  454. mSyncedProperties.width = props.width;
  455. mSyncedProperties.height = props.height;
  456. }
  457. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  458. }
  459. HWND Win32RenderWindow::_getWindowHandle() const
  460. {
  461. return mWindow->getHWnd();
  462. }
  463. void Win32RenderWindow::getCustomAttribute(const String& name, void* data) const
  464. {
  465. if (name == "FB")
  466. {
  467. VulkanFramebuffer** fb = (VulkanFramebuffer**)data;
  468. *fb = mSwapChain->getBackBuffer().framebuffer;
  469. return;
  470. }
  471. if(name == "SC")
  472. {
  473. VulkanSwapChain** sc = (VulkanSwapChain**)data;
  474. *sc = mSwapChain.get();
  475. return;
  476. }
  477. if(name == "WINDOW")
  478. {
  479. UINT64 *pWnd = (UINT64*)data;
  480. *pWnd = (UINT64)mWindow->getHWnd();
  481. return;
  482. }
  483. RenderWindow::getCustomAttribute(name, data);
  484. }
  485. void Win32RenderWindow::_windowMovedOrResized()
  486. {
  487. THROW_IF_NOT_CORE_THREAD;
  488. if (!mWindow)
  489. return;
  490. mWindow->_windowMovedOrResized();
  491. RenderWindowProperties& props = mProperties;
  492. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  493. {
  494. props.top = mWindow->getTop();
  495. props.left = mWindow->getLeft();
  496. props.width = mWindow->getWidth();
  497. props.height = mWindow->getHeight();
  498. }
  499. // Resize swap chain
  500. //// Need to make sure nothing is using the swap buffer before we re-create it
  501. // Note: Optionally I can detect exactly on which queues (if any) are the swap chain images used on, and only wait
  502. // on those
  503. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  504. presentDevice->waitIdle();
  505. mSwapChain->rebuild(presentDevice, mSurface, props.width, props.height, props.vsync, mColorFormat, mColorSpace,
  506. mDesc.depthBuffer, mDepthFormat);
  507. RenderWindow::_windowMovedOrResized();
  508. }
  509. void Win32RenderWindow::syncProperties()
  510. {
  511. ScopedSpinLock lock(mLock);
  512. mProperties = mSyncedProperties;
  513. }
  514. }
  515. }